libnl  3.4.0
link.c
1 /*
2  * lib/route/link.c Links (Interfaces)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @ingroup rtnl
14  * @defgroup link Links (Interfaces)
15  *
16  * @details
17  * @route_doc{route_link, Link Documentation}
18  * @{
19  */
20 
21 #include <netlink-private/netlink.h>
22 #include <netlink/netlink.h>
23 #include <netlink/attr.h>
24 #include <netlink/utils.h>
25 #include <netlink/object.h>
26 #include <netlink/hashtable.h>
27 #include <netlink/data.h>
28 #include <netlink/route/rtnl.h>
29 #include <netlink/route/link.h>
30 #include <netlink-private/route/link/api.h>
31 #include <netlink-private/route/link/sriov.h>
32 #include <netlink-private/utils.h>
33 
34 /** @cond SKIP */
35 #define LINK_ATTR_MTU (1 << 0)
36 #define LINK_ATTR_LINK (1 << 1)
37 #define LINK_ATTR_TXQLEN (1 << 2)
38 #define LINK_ATTR_WEIGHT (1 << 3)
39 #define LINK_ATTR_MASTER (1 << 4)
40 #define LINK_ATTR_QDISC (1 << 5)
41 #define LINK_ATTR_MAP (1 << 6)
42 #define LINK_ATTR_ADDR (1 << 7)
43 #define LINK_ATTR_BRD (1 << 8)
44 #define LINK_ATTR_FLAGS (1 << 9)
45 #define LINK_ATTR_IFNAME (1 << 10)
46 #define LINK_ATTR_IFINDEX (1 << 11)
47 #define LINK_ATTR_FAMILY (1 << 12)
48 #define LINK_ATTR_ARPTYPE (1 << 13)
49 #define LINK_ATTR_STATS (1 << 14)
50 #define LINK_ATTR_CHANGE (1 << 15)
51 #define LINK_ATTR_OPERSTATE (1 << 16)
52 #define LINK_ATTR_LINKMODE (1 << 17)
53 #define LINK_ATTR_LINKINFO (1 << 18)
54 #define LINK_ATTR_IFALIAS (1 << 19)
55 #define LINK_ATTR_NUM_VF (1 << 20)
56 #define LINK_ATTR_PROMISCUITY (1 << 21)
57 #define LINK_ATTR_NUM_TX_QUEUES (1 << 22)
58 #define LINK_ATTR_NUM_RX_QUEUES (1 << 23)
59 #define LINK_ATTR_GROUP (1 << 24)
60 #define LINK_ATTR_CARRIER (1 << 25)
61 #define LINK_ATTR_PROTINFO (1 << 26)
62 #define LINK_ATTR_AF_SPEC (1 << 27)
63 #define LINK_ATTR_PHYS_PORT_ID (1 << 28)
64 #define LINK_ATTR_NS_FD (1 << 29)
65 #define LINK_ATTR_NS_PID (1 << 30)
66 /* 31 used by 32-bit api */
67 #define LINK_ATTR_LINK_NETNSID ((uint64_t) 1 << 32)
68 #define LINK_ATTR_VF_LIST ((uint64_t) 1 << 33)
69 #define LINK_ATTR_CARRIER_CHANGES ((uint64_t) 1 << 34)
70 #define LINK_ATTR_PHYS_PORT_NAME ((uint64_t) 1 << 35)
71 #define LINK_ATTR_PHYS_SWITCH_ID ((uint64_t) 1 << 36)
72 #define LINK_ATTR_GSO_MAX_SEGS ((uint64_t) 1 << 37)
73 #define LINK_ATTR_GSO_MAX_SIZE ((uint64_t) 1 << 38)
74 
75 static struct nl_cache_ops rtnl_link_ops;
76 static struct nl_object_ops link_obj_ops;
77 /** @endcond */
78 
79 struct rtnl_link *link_lookup(struct nl_cache *cache, int ifindex)
80 {
81  if (!cache) {
82  cache = __nl_cache_mngt_require("route/link");
83  if (!cache)
84  return NULL;
85  }
86 
87  return rtnl_link_get(cache, ifindex);
88 }
89 
90 static struct rtnl_link_af_ops *af_lookup_and_alloc(struct rtnl_link *link,
91  int family)
92 {
93  struct rtnl_link_af_ops *af_ops;
94  void *data;
95 
96  af_ops = rtnl_link_af_ops_lookup(family);
97  if (!af_ops)
98  return NULL;
99 
100  if (!(data = rtnl_link_af_alloc(link, af_ops))) {
101  rtnl_link_af_ops_put(af_ops);
102  return NULL;
103  }
104 
105  return af_ops;
106 }
107 
108 static int af_free(struct rtnl_link *link, struct rtnl_link_af_ops *ops,
109  void *data, void *arg)
110 {
111  if (ops->ao_free)
112  ops->ao_free(link, data);
113 
115 
116  return 0;
117 }
118 
119 static int af_request_type(int af_type)
120 {
121  struct rtnl_link_af_ops *ops;
122 
123  ops = rtnl_link_af_ops_lookup(af_type);
124  if (ops && ops->ao_override_rtm)
125  return RTM_SETLINK;
126 
127  return RTM_NEWLINK;
128 }
129 
130 static int af_clone(struct rtnl_link *link, struct rtnl_link_af_ops *ops,
131  void *data, void *arg)
132 {
133  struct rtnl_link *dst = arg;
134 
135  if (ops->ao_clone &&
136  !(dst->l_af_data[ops->ao_family] = ops->ao_clone(dst, data)))
137  return -NLE_NOMEM;
138 
139  return 0;
140 }
141 
142 static int af_fill(struct rtnl_link *link, struct rtnl_link_af_ops *ops,
143  void *data, void *arg)
144 {
145  struct nl_msg *msg = arg;
146  struct nlattr *af_attr = NULL;
147  int err;
148 
149  if (!ops->ao_fill_af)
150  return 0;
151 
152  if (!ops->ao_fill_af_no_nest)
153  if (!(af_attr = nla_nest_start(msg, ops->ao_family)))
154  return -NLE_MSGSIZE;
155 
156  if ((err = ops->ao_fill_af(link, arg, data)) < 0)
157  return err;
158 
159  if (!ops->ao_fill_af_no_nest)
160  nla_nest_end(msg, af_attr);
161 
162  return 0;
163 }
164 
165 static int af_fill_pi(struct rtnl_link *link, struct rtnl_link_af_ops *ops,
166  void *data, void *arg)
167 {
168  struct nl_msg *msg = arg;
169  struct nlattr *pi_attr;
170  int err, pi_type = IFLA_PROTINFO;
171 
172  if (!ops->ao_fill_pi)
173  return 0;
174 
175  if (ops->ao_fill_pi_flags > 0)
176  pi_type |= ops->ao_fill_pi_flags;
177 
178  if (!(pi_attr = nla_nest_start(msg, pi_type)))
179  return -NLE_MSGSIZE;
180 
181  if ((err = ops->ao_fill_pi(link, arg, data)) < 0)
182  return err;
183 
184  nla_nest_end(msg, pi_attr);
185 
186  return 0;
187 }
188 
189 static int af_dump_line(struct rtnl_link *link, struct rtnl_link_af_ops *ops,
190  void *data, void *arg)
191 {
192  struct nl_dump_params *p = arg;
193 
194  if (ops->ao_dump[NL_DUMP_LINE])
195  ops->ao_dump[NL_DUMP_LINE](link, p, data);
196 
197  return 0;
198 }
199 
200 static int af_dump_details(struct rtnl_link *link, struct rtnl_link_af_ops *ops,
201  void *data, void *arg)
202 {
203  struct nl_dump_params *p = arg;
204 
205  if (ops->ao_dump[NL_DUMP_DETAILS])
206  ops->ao_dump[NL_DUMP_DETAILS](link, p, data);
207 
208  return 0;
209 }
210 
211 static int af_dump_stats(struct rtnl_link *link, struct rtnl_link_af_ops *ops,
212  void *data, void *arg)
213 {
214  struct nl_dump_params *p = arg;
215 
216  if (ops->ao_dump[NL_DUMP_STATS])
217  ops->ao_dump[NL_DUMP_STATS](link, p, data);
218 
219  return 0;
220 }
221 
222 static int do_foreach_af(struct rtnl_link *link,
223  int (*cb)(struct rtnl_link *,
224  struct rtnl_link_af_ops *, void *, void *),
225  void *arg)
226 {
227  int i, err;
228 
229  for (i = 0; i < AF_MAX; i++) {
230  if (link->l_af_data[i]) {
231  struct rtnl_link_af_ops *ops;
232 
233  if (!(ops = rtnl_link_af_ops_lookup(i)))
234  BUG();
235 
236  err = cb(link, ops, link->l_af_data[i], arg);
237 
239 
240  if (err < 0)
241  return err;
242  }
243  }
244 
245  return 0;
246 }
247 
248 static void release_link_info(struct rtnl_link *link)
249 {
250  struct rtnl_link_info_ops *io = link->l_info_ops;
251 
252  if (io != NULL) {
253  if (io->io_free)
254  io->io_free(link);
255  else {
256  /* Catch missing io_free() implementations */
257  BUG_ON(link->l_info);
258  }
260  link->l_info_ops = NULL;
261  }
262 }
263 
264 static void link_free_data(struct nl_object *c)
265 {
266  struct rtnl_link *link = nl_object_priv(c);
267 
268  if (link) {
269  release_link_info(link);
270 
271  /* proto info af reference */
272  rtnl_link_af_ops_put(link->l_af_ops);
273 
274  nl_addr_put(link->l_addr);
275  nl_addr_put(link->l_bcast);
276 
277  free(link->l_ifalias);
278  free(link->l_info_kind);
279 
280  do_foreach_af(link, af_free, NULL);
281 
282  nl_data_free(link->l_phys_port_id);
283  nl_data_free(link->l_phys_switch_id);
284 
285  if (link->ce_mask & LINK_ATTR_VF_LIST)
286  rtnl_link_sriov_free_data(link);
287  }
288 }
289 
290 static int link_clone(struct nl_object *_dst, struct nl_object *_src)
291 {
292  struct rtnl_link *dst = nl_object_priv(_dst);
293  struct rtnl_link *src = nl_object_priv(_src);
294  int err;
295 
296  if (src->l_addr)
297  if (!(dst->l_addr = nl_addr_clone(src->l_addr)))
298  return -NLE_NOMEM;
299 
300  if (src->l_bcast)
301  if (!(dst->l_bcast = nl_addr_clone(src->l_bcast)))
302  return -NLE_NOMEM;
303 
304  if (src->l_ifalias)
305  if (!(dst->l_ifalias = strdup(src->l_ifalias)))
306  return -NLE_NOMEM;
307 
308  if (src->l_info_kind)
309  if (!(dst->l_info_kind = strdup(src->l_info_kind)))
310  return -NLE_NOMEM;
311 
312  if (src->l_info_ops && src->l_info_ops->io_clone) {
313  err = src->l_info_ops->io_clone(dst, src);
314  if (err < 0)
315  return err;
316  }
317 
318  if ((err = do_foreach_af(src, af_clone, dst)) < 0)
319  return err;
320 
321  if (src->l_phys_port_id)
322  if (!(dst->l_phys_port_id = nl_data_clone(src->l_phys_port_id)))
323  return -NLE_NOMEM;
324 
325  if (src->l_phys_switch_id)
326  if (!(dst->l_phys_switch_id = nl_data_clone(src->l_phys_switch_id)))
327  return -NLE_NOMEM;
328 
329  if (src->ce_mask & LINK_ATTR_VF_LIST)
330  if ((err = rtnl_link_sriov_clone(dst, src)) < 0)
331  return err;
332 
333  return 0;
334 }
335 
336 struct nla_policy rtln_link_policy[IFLA_MAX+1] = {
337  [IFLA_IFNAME] = { .type = NLA_STRING,
338  .maxlen = IFNAMSIZ },
339  [IFLA_MTU] = { .type = NLA_U32 },
340  [IFLA_TXQLEN] = { .type = NLA_U32 },
341  [IFLA_LINK] = { .type = NLA_U32 },
342  [IFLA_WEIGHT] = { .type = NLA_U32 },
343  [IFLA_MASTER] = { .type = NLA_U32 },
344  [IFLA_OPERSTATE] = { .type = NLA_U8 },
345  [IFLA_LINKMODE] = { .type = NLA_U8 },
346  [IFLA_LINKINFO] = { .type = NLA_NESTED },
347  [IFLA_QDISC] = { .type = NLA_STRING,
348  .maxlen = IFQDISCSIZ },
349  [IFLA_STATS] = { .minlen = _nl_offsetofend (struct rtnl_link_stats, tx_compressed) },
350  [IFLA_STATS64] = { .minlen = _nl_offsetofend (struct rtnl_link_stats64, tx_compressed) },
351  [IFLA_MAP] = { .minlen = sizeof(struct rtnl_link_ifmap) },
352  [IFLA_IFALIAS] = { .type = NLA_STRING, .maxlen = IFALIASZ },
353  [IFLA_NUM_VF] = { .type = NLA_U32 },
354  [IFLA_VFINFO_LIST] = { .type = NLA_NESTED },
355  [IFLA_AF_SPEC] = { .type = NLA_NESTED },
356  [IFLA_PROMISCUITY] = { .type = NLA_U32 },
357  [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
358  [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
359  [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
360  [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
361  [IFLA_GROUP] = { .type = NLA_U32 },
362  [IFLA_CARRIER] = { .type = NLA_U8 },
363  [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 },
364  [IFLA_PHYS_PORT_ID] = { .type = NLA_UNSPEC },
365  [IFLA_PHYS_PORT_NAME] = { .type = NLA_STRING, .maxlen = IFNAMSIZ },
366  [IFLA_PHYS_SWITCH_ID] = { .type = NLA_UNSPEC },
367  [IFLA_NET_NS_PID] = { .type = NLA_U32 },
368  [IFLA_NET_NS_FD] = { .type = NLA_U32 },
369 };
370 
371 static struct nla_policy link_info_policy[IFLA_INFO_MAX+1] = {
372  [IFLA_INFO_KIND] = { .type = NLA_STRING },
373  [IFLA_INFO_DATA] = { .type = NLA_NESTED },
374  [IFLA_INFO_XSTATS] = { .type = NLA_NESTED },
375 };
376 
377 int rtnl_link_info_parse(struct rtnl_link *link, struct nlattr **tb)
378 {
379  if (tb[IFLA_IFNAME] == NULL)
380  return -NLE_MISSING_ATTR;
381 
382  nla_strlcpy(link->l_name, tb[IFLA_IFNAME], IFNAMSIZ);
383 
384 
385  if (tb[IFLA_STATS]) {
386  struct rtnl_link_stats *st = nla_data(tb[IFLA_STATS]);
387 
388  link->l_stats[RTNL_LINK_RX_PACKETS] = st->rx_packets;
389  link->l_stats[RTNL_LINK_TX_PACKETS] = st->tx_packets;
390  link->l_stats[RTNL_LINK_RX_BYTES] = st->rx_bytes;
391  link->l_stats[RTNL_LINK_TX_BYTES] = st->tx_bytes;
392  link->l_stats[RTNL_LINK_RX_ERRORS] = st->rx_errors;
393  link->l_stats[RTNL_LINK_TX_ERRORS] = st->tx_errors;
394  link->l_stats[RTNL_LINK_RX_DROPPED] = st->rx_dropped;
395  link->l_stats[RTNL_LINK_TX_DROPPED] = st->tx_dropped;
396  link->l_stats[RTNL_LINK_MULTICAST] = st->multicast;
397  link->l_stats[RTNL_LINK_COLLISIONS] = st->collisions;
398 
399  link->l_stats[RTNL_LINK_RX_LEN_ERR] = st->rx_length_errors;
400  link->l_stats[RTNL_LINK_RX_OVER_ERR] = st->rx_over_errors;
401  link->l_stats[RTNL_LINK_RX_CRC_ERR] = st->rx_crc_errors;
402  link->l_stats[RTNL_LINK_RX_FRAME_ERR] = st->rx_frame_errors;
403  link->l_stats[RTNL_LINK_RX_FIFO_ERR] = st->rx_fifo_errors;
404  link->l_stats[RTNL_LINK_RX_MISSED_ERR] = st->rx_missed_errors;
405 
406  link->l_stats[RTNL_LINK_TX_ABORT_ERR] = st->tx_aborted_errors;
407  link->l_stats[RTNL_LINK_TX_CARRIER_ERR] = st->tx_carrier_errors;
408  link->l_stats[RTNL_LINK_TX_FIFO_ERR] = st->tx_fifo_errors;
409  link->l_stats[RTNL_LINK_TX_HBEAT_ERR] = st->tx_heartbeat_errors;
410  link->l_stats[RTNL_LINK_TX_WIN_ERR] = st->tx_window_errors;
411 
412  link->l_stats[RTNL_LINK_RX_COMPRESSED] = st->rx_compressed;
413  link->l_stats[RTNL_LINK_TX_COMPRESSED] = st->tx_compressed;
414 
415  /* beware: @st might not be the full struct, only fields up to
416  * tx_compressed are present. See _nl_offsetofend() above. */
417 
418  if (nla_len(tb[IFLA_STATS]) >= _nl_offsetofend (struct rtnl_link_stats, rx_nohandler))
419  link->l_stats[RTNL_LINK_RX_NOHANDLER] = st->rx_nohandler;
420  else
421  link->l_stats[RTNL_LINK_RX_NOHANDLER] = 0;
422 
423  link->ce_mask |= LINK_ATTR_STATS;
424  }
425 
426  if (tb[IFLA_STATS64]) {
427  /*
428  * This structure contains 64bit parameters, and per the
429  * documentation in lib/attr.c, must not be accessed
430  * directly (because of alignment to 4 instead of 8).
431  * Therefore, copy the data to the stack and access it from
432  * there, where it will be aligned to 8.
433  */
434  struct rtnl_link_stats64 st = { 0 };
435 
436  nla_memcpy(&st, tb[IFLA_STATS64], sizeof (st));
437 
438  link->l_stats[RTNL_LINK_RX_PACKETS] = st.rx_packets;
439  link->l_stats[RTNL_LINK_TX_PACKETS] = st.tx_packets;
440  link->l_stats[RTNL_LINK_RX_BYTES] = st.rx_bytes;
441  link->l_stats[RTNL_LINK_TX_BYTES] = st.tx_bytes;
442  link->l_stats[RTNL_LINK_RX_ERRORS] = st.rx_errors;
443  link->l_stats[RTNL_LINK_TX_ERRORS] = st.tx_errors;
444  link->l_stats[RTNL_LINK_RX_DROPPED] = st.rx_dropped;
445  link->l_stats[RTNL_LINK_TX_DROPPED] = st.tx_dropped;
446  link->l_stats[RTNL_LINK_MULTICAST] = st.multicast;
447  link->l_stats[RTNL_LINK_COLLISIONS] = st.collisions;
448 
449  link->l_stats[RTNL_LINK_RX_LEN_ERR] = st.rx_length_errors;
450  link->l_stats[RTNL_LINK_RX_OVER_ERR] = st.rx_over_errors;
451  link->l_stats[RTNL_LINK_RX_CRC_ERR] = st.rx_crc_errors;
452  link->l_stats[RTNL_LINK_RX_FRAME_ERR] = st.rx_frame_errors;
453  link->l_stats[RTNL_LINK_RX_FIFO_ERR] = st.rx_fifo_errors;
454  link->l_stats[RTNL_LINK_RX_MISSED_ERR] = st.rx_missed_errors;
455 
456  link->l_stats[RTNL_LINK_TX_ABORT_ERR] = st.tx_aborted_errors;
457  link->l_stats[RTNL_LINK_TX_CARRIER_ERR] = st.tx_carrier_errors;
458  link->l_stats[RTNL_LINK_TX_FIFO_ERR] = st.tx_fifo_errors;
459  link->l_stats[RTNL_LINK_TX_HBEAT_ERR] = st.tx_heartbeat_errors;
460  link->l_stats[RTNL_LINK_TX_WIN_ERR] = st.tx_window_errors;
461 
462  link->l_stats[RTNL_LINK_RX_COMPRESSED] = st.rx_compressed;
463  link->l_stats[RTNL_LINK_TX_COMPRESSED] = st.tx_compressed;
464 
465  /* beware: @st might not be the full struct, only fields up to
466  * tx_compressed are present. See _nl_offsetofend() above. */
467 
468  link->l_stats[RTNL_LINK_RX_NOHANDLER] = st.rx_nohandler;
469 
470  link->ce_mask |= LINK_ATTR_STATS;
471  }
472 
473  if (tb[IFLA_TXQLEN]) {
474  link->l_txqlen = nla_get_u32(tb[IFLA_TXQLEN]);
475  link->ce_mask |= LINK_ATTR_TXQLEN;
476  }
477 
478  if (tb[IFLA_MTU]) {
479  link->l_mtu = nla_get_u32(tb[IFLA_MTU]);
480  link->ce_mask |= LINK_ATTR_MTU;
481  }
482 
483  if (tb[IFLA_ADDRESS]) {
484  link->l_addr = nl_addr_alloc_attr(tb[IFLA_ADDRESS], AF_UNSPEC);
485  if (link->l_addr == NULL)
486  return -NLE_NOMEM;
487  nl_addr_set_family(link->l_addr,
488  nl_addr_guess_family(link->l_addr));
489  link->ce_mask |= LINK_ATTR_ADDR;
490  }
491 
492  if (tb[IFLA_BROADCAST]) {
493  link->l_bcast = nl_addr_alloc_attr(tb[IFLA_BROADCAST],
494  AF_UNSPEC);
495  if (link->l_bcast == NULL)
496  return -NLE_NOMEM;
497  nl_addr_set_family(link->l_bcast,
498  nl_addr_guess_family(link->l_bcast));
499  link->ce_mask |= LINK_ATTR_BRD;
500  }
501 
502  if (tb[IFLA_LINK]) {
503  link->l_link = nla_get_u32(tb[IFLA_LINK]);
504  link->ce_mask |= LINK_ATTR_LINK;
505  }
506 
507  if (tb[IFLA_LINK_NETNSID]) {
508  link->l_link_netnsid = nla_get_s32(tb[IFLA_LINK_NETNSID]);
509  link->ce_mask |= LINK_ATTR_LINK_NETNSID;
510  }
511 
512  if (tb[IFLA_WEIGHT]) {
513  link->l_weight = nla_get_u32(tb[IFLA_WEIGHT]);
514  link->ce_mask |= LINK_ATTR_WEIGHT;
515  }
516 
517  if (tb[IFLA_QDISC]) {
518  nla_strlcpy(link->l_qdisc, tb[IFLA_QDISC], IFQDISCSIZ);
519  link->ce_mask |= LINK_ATTR_QDISC;
520  }
521 
522  if (tb[IFLA_MAP]) {
523  nla_memcpy(&link->l_map, tb[IFLA_MAP],
524  sizeof(struct rtnl_link_ifmap));
525  link->ce_mask |= LINK_ATTR_MAP;
526  }
527 
528  if (tb[IFLA_MASTER]) {
529  link->l_master = nla_get_u32(tb[IFLA_MASTER]);
530  link->ce_mask |= LINK_ATTR_MASTER;
531  }
532 
533  if (tb[IFLA_CARRIER]) {
534  link->l_carrier = nla_get_u8(tb[IFLA_CARRIER]);
535  link->ce_mask |= LINK_ATTR_CARRIER;
536  }
537 
538  if (tb[IFLA_CARRIER_CHANGES]) {
539  link->l_carrier_changes = nla_get_u32(tb[IFLA_CARRIER_CHANGES]);
540  link->ce_mask |= LINK_ATTR_CARRIER_CHANGES;
541  }
542 
543  if (tb[IFLA_OPERSTATE]) {
544  link->l_operstate = nla_get_u8(tb[IFLA_OPERSTATE]);
545  link->ce_mask |= LINK_ATTR_OPERSTATE;
546  }
547 
548  if (tb[IFLA_LINKMODE]) {
549  link->l_linkmode = nla_get_u8(tb[IFLA_LINKMODE]);
550  link->ce_mask |= LINK_ATTR_LINKMODE;
551  }
552 
553  if (tb[IFLA_IFALIAS]) {
554  link->l_ifalias = nla_strdup(tb[IFLA_IFALIAS]);
555  if (link->l_ifalias == NULL)
556  return -NLE_NOMEM;
557  link->ce_mask |= LINK_ATTR_IFALIAS;
558  }
559 
560  if (tb[IFLA_NET_NS_FD]) {
561  link->l_ns_fd = nla_get_u32(tb[IFLA_NET_NS_FD]);
562  link->ce_mask |= LINK_ATTR_NS_FD;
563  }
564 
565  if (tb[IFLA_NET_NS_PID]) {
566  link->l_ns_pid = nla_get_u32(tb[IFLA_NET_NS_PID]);
567  link->ce_mask |= LINK_ATTR_NS_PID;
568  }
569 
570  return 0;
571 }
572 
573 static int link_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
574  struct nlmsghdr *n, struct nl_parser_param *pp)
575 {
576  struct rtnl_link *link;
577  struct ifinfomsg *ifi;
578  struct nlattr *tb[IFLA_MAX+1];
579  struct rtnl_link_af_ops *af_ops = NULL;
580  struct rtnl_link_af_ops *af_ops_family;
581  int err, family;
582  struct nla_policy real_link_policy[IFLA_MAX+1];
583 
584  memcpy(&real_link_policy, rtln_link_policy, sizeof(rtln_link_policy));
585 
586  link = rtnl_link_alloc();
587  if (link == NULL) {
588  err = -NLE_NOMEM;
589  goto errout;
590  }
591 
592  link->ce_msgtype = n->nlmsg_type;
593 
594  if (!nlmsg_valid_hdr(n, sizeof(*ifi))) {
595  err = -NLE_MSG_TOOSHORT;
596  goto errout;
597  }
598 
599  ifi = nlmsg_data(n);
600  link->l_family = family = ifi->ifi_family;
601  link->l_arptype = ifi->ifi_type;
602  link->l_index = ifi->ifi_index;
603  link->l_flags = ifi->ifi_flags;
604  link->l_change = ifi->ifi_change;
605  link->ce_mask = (LINK_ATTR_IFNAME | LINK_ATTR_FAMILY |
606  LINK_ATTR_ARPTYPE| LINK_ATTR_IFINDEX |
607  LINK_ATTR_FLAGS | LINK_ATTR_CHANGE);
608 
609  if ((af_ops_family = af_ops = af_lookup_and_alloc(link, family))) {
610  if (af_ops->ao_protinfo_policy) {
611  memcpy(&real_link_policy[IFLA_PROTINFO],
612  af_ops->ao_protinfo_policy,
613  sizeof(struct nla_policy));
614  }
615 
616  link->l_af_ops = af_ops;
617  }
618 
619  err = nlmsg_parse(n, sizeof(*ifi), tb, IFLA_MAX, real_link_policy);
620  if (err < 0)
621  goto errout;
622 
623  err = rtnl_link_info_parse(link, tb);
624  if (err < 0)
625  goto errout;
626 
627  if (tb[IFLA_NUM_VF]) {
628  link->l_num_vf = nla_get_u32(tb[IFLA_NUM_VF]);
629  link->ce_mask |= LINK_ATTR_NUM_VF;
630  if (link->l_num_vf && tb[IFLA_VFINFO_LIST]) {
631  if ((err = rtnl_link_sriov_parse_vflist(link, tb)) < 0) {
632  goto errout;
633  }
634  link->ce_mask |= LINK_ATTR_VF_LIST;
635  }
636  }
637 
638  if (tb[IFLA_LINKINFO]) {
639  struct nlattr *li[IFLA_INFO_MAX+1];
640 
641  err = nla_parse_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO],
642  link_info_policy);
643  if (err < 0)
644  goto errout;
645 
646  if (li[IFLA_INFO_KIND]) {
647  struct rtnl_link_info_ops *ops;
648  char *kind = nla_get_string(li[IFLA_INFO_KIND]);
649  int af;
650 
651  err = rtnl_link_set_type(link, kind);
652  if (err < 0)
653  goto errout;
654 
655  if ((af = nl_str2af(kind)) >= 0 &&
656  !af_ops && (af_ops = af_lookup_and_alloc(link, af))) {
657 
658  if (af_ops->ao_protinfo_policy) {
659  tb[IFLA_PROTINFO] = (struct nlattr *)af_ops->ao_protinfo_policy;
660  }
661  link->l_family = af;
662  link->l_af_ops = af_ops;
663  }
664 
665  ops = rtnl_link_info_ops_lookup(kind);
666  link->l_info_ops = ops;
667 
668  if (ops) {
669  if (ops->io_parse &&
670  (li[IFLA_INFO_DATA] || li[IFLA_INFO_XSTATS])) {
671  err = ops->io_parse(link, li[IFLA_INFO_DATA],
672  li[IFLA_INFO_XSTATS]);
673  if (err < 0)
674  goto errout;
675  } else {
676  /* XXX: Warn about unparsed info? */
677  }
678  }
679  }
680  link->ce_mask |= LINK_ATTR_LINKINFO;
681  }
682 
683  if (tb[IFLA_PROTINFO] && af_ops && af_ops->ao_parse_protinfo) {
684  err = af_ops->ao_parse_protinfo(link, tb[IFLA_PROTINFO],
685  link->l_af_data[link->l_family]);
686  if (err < 0)
687  goto errout;
688  link->ce_mask |= LINK_ATTR_PROTINFO;
689  }
690 
691  if (tb[IFLA_AF_SPEC]) {
692  /* parsing of IFLA_AF_SPEC is dependent on the family used
693  * in the request message.
694  */
695  if (af_ops_family && af_ops_family->ao_parse_af_full) {
696  err = af_ops_family->ao_parse_af_full(link,
697  tb[IFLA_AF_SPEC],
698  link->l_af_data[af_ops_family->ao_family]);
699  if (err < 0)
700  goto errout;
701  link->ce_mask |= LINK_ATTR_AF_SPEC;
702  } else if (family == AF_UNSPEC) {
703  struct nlattr *af_attr;
704  int remaining;
705 
706  nla_for_each_nested(af_attr, tb[IFLA_AF_SPEC], remaining) {
707  af_ops = af_lookup_and_alloc(link, nla_type(af_attr));
708  if (af_ops && af_ops->ao_parse_af) {
709  char *af_data = link->l_af_data[nla_type(af_attr)];
710 
711  err = af_ops->ao_parse_af(link, af_attr, af_data);
712  if (err < 0)
713  goto errout;
714  }
715  }
716  link->ce_mask |= LINK_ATTR_AF_SPEC;
717  } else {
718  NL_DBG(3, "IFLA_AF_SPEC parsing not implemented for family %d\n",
719  family);
720  }
721  }
722 
723  if (tb[IFLA_PROMISCUITY]) {
724  link->l_promiscuity = nla_get_u32(tb[IFLA_PROMISCUITY]);
725  link->ce_mask |= LINK_ATTR_PROMISCUITY;
726  }
727 
728  if (tb[IFLA_NUM_TX_QUEUES]) {
729  link->l_num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
730  link->ce_mask |= LINK_ATTR_NUM_TX_QUEUES;
731  }
732 
733  if (tb[IFLA_NUM_RX_QUEUES]) {
734  link->l_num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
735  link->ce_mask |= LINK_ATTR_NUM_RX_QUEUES;
736  }
737 
738  if (tb[IFLA_GSO_MAX_SEGS]) {
739  link->l_gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
740  link->ce_mask |= LINK_ATTR_GSO_MAX_SEGS;
741  }
742 
743  if (tb[IFLA_GSO_MAX_SIZE]) {
744  link->l_gso_max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
745  link->ce_mask |= LINK_ATTR_GSO_MAX_SIZE;
746  }
747 
748  if (tb[IFLA_GROUP]) {
749  link->l_group = nla_get_u32(tb[IFLA_GROUP]);
750  link->ce_mask |= LINK_ATTR_GROUP;
751  }
752 
753  if (tb[IFLA_PHYS_PORT_ID]) {
754  link->l_phys_port_id = nl_data_alloc_attr(tb[IFLA_PHYS_PORT_ID]);
755  if (link->l_phys_port_id == NULL) {
756  err = -NLE_NOMEM;
757  goto errout;
758  }
759  link->ce_mask |= LINK_ATTR_PHYS_PORT_ID;
760  }
761 
762  if (tb[IFLA_PHYS_PORT_NAME]) {
763  nla_strlcpy(link->l_phys_port_name, tb[IFLA_PHYS_PORT_NAME], IFNAMSIZ);
764  link->ce_mask |= LINK_ATTR_PHYS_PORT_NAME;
765  }
766 
767  if (tb[IFLA_PHYS_SWITCH_ID]) {
768  link->l_phys_switch_id = nl_data_alloc_attr(tb[IFLA_PHYS_SWITCH_ID]);
769  if (link->l_phys_switch_id == NULL) {
770  err = -NLE_NOMEM;
771  goto errout;
772  }
773  link->ce_mask |= LINK_ATTR_PHYS_SWITCH_ID;
774  }
775 
776  err = pp->pp_cb((struct nl_object *) link, pp);
777 errout:
778  rtnl_link_af_ops_put(af_ops);
779  rtnl_link_put(link);
780  return err;
781 }
782 
783 static int link_request_update(struct nl_cache *cache, struct nl_sock *sk)
784 {
785  int family = cache->c_iarg1;
786  struct ifinfomsg hdr = { .ifi_family = family };
787  struct rtnl_link_af_ops *ops;
788  struct nl_msg *msg;
789  int err;
790  __u32 ext_filter_mask = RTEXT_FILTER_VF;
791 
792  msg = nlmsg_alloc_simple(RTM_GETLINK, NLM_F_DUMP);
793  if (!msg)
794  return -NLE_NOMEM;
795 
796  err = -NLE_MSGSIZE;
797  if (nlmsg_append(msg, &hdr, sizeof(hdr), NLMSG_ALIGNTO) < 0)
798  goto nla_put_failure;
799 
800  ops = rtnl_link_af_ops_lookup(family);
801  if (ops && ops->ao_get_af) {
802  err = ops->ao_get_af(msg, &ext_filter_mask);
803  if (err)
804  goto nla_put_failure;
805  }
806 
807  if (ext_filter_mask) {
808  err = nla_put(msg, IFLA_EXT_MASK, sizeof(ext_filter_mask), &ext_filter_mask);
809  if (err)
810  goto nla_put_failure;
811  }
812 
813  err = nl_send_auto(sk, msg);
814  if (err > 0)
815  err = 0;
816 
817 nla_put_failure:
818  nlmsg_free(msg);
819  return err;
820 }
821 
822 static void link_dump_line(struct nl_object *obj, struct nl_dump_params *p)
823 {
824  char buf[128];
825  struct nl_cache *cache = obj->ce_cache;
826  struct rtnl_link *link = (struct rtnl_link *) obj;
827  int fetched_cache = 0;
828 
829  if (!cache) {
830  cache = nl_cache_mngt_require_safe("route/link");
831  fetched_cache = 1;
832  }
833 
834  nl_dump_line(p, "%s %s ", link->l_name,
835  nl_llproto2str(link->l_arptype, buf, sizeof(buf)));
836 
837  if (link->l_addr && !nl_addr_iszero(link->l_addr))
838  nl_dump(p, "%s ", nl_addr2str(link->l_addr, buf, sizeof(buf)));
839 
840  if (link->ce_mask & LINK_ATTR_MASTER) {
841  if (cache) {
842  struct rtnl_link *master = rtnl_link_get(cache, link->l_master);
843  nl_dump(p, "master %s ", master ? master->l_name : "inv");
844  if (master)
845  rtnl_link_put(master);
846  } else
847  nl_dump(p, "master %d ", link->l_master);
848  }
849 
850  rtnl_link_flags2str(link->l_flags, buf, sizeof(buf));
851  if (buf[0])
852  nl_dump(p, "<%s> ", buf);
853 
854  if (link->ce_mask & LINK_ATTR_LINK) {
855  if ( cache
856  && !(link->ce_mask & LINK_ATTR_LINK_NETNSID)) {
857  struct rtnl_link *ll = rtnl_link_get(cache, link->l_link);
858  nl_dump(p, "slave-of %s ", ll ? ll->l_name : "NONE");
859  if (ll)
860  rtnl_link_put(ll);
861  } else
862  nl_dump(p, "slave-of %d ", link->l_link);
863  }
864  if (link->ce_mask & LINK_ATTR_LINK_NETNSID)
865  nl_dump(p, "link-netnsid %d ", link->l_link_netnsid);
866 
867  if (link->ce_mask & LINK_ATTR_GROUP)
868  nl_dump(p, "group %u ", link->l_group);
869 
870  if (link->l_info_ops && link->l_info_ops->io_dump[NL_DUMP_LINE])
871  link->l_info_ops->io_dump[NL_DUMP_LINE](link, p);
872 
873  do_foreach_af(link, af_dump_line, p);
874 
875  nl_dump(p, "\n");
876 
877  if (fetched_cache)
878  nl_cache_put(cache);
879 }
880 
881 static void link_dump_details(struct nl_object *obj, struct nl_dump_params *p)
882 {
883  struct rtnl_link *link = (struct rtnl_link *) obj;
884  char buf[64];
885 
886  link_dump_line(obj, p);
887 
888  nl_dump_line(p, " mtu %u ", link->l_mtu);
889  nl_dump(p, "txqlen %u weight %u ", link->l_txqlen, link->l_weight);
890 
891  if (link->ce_mask & LINK_ATTR_QDISC)
892  nl_dump(p, "qdisc %s ", link->l_qdisc);
893 
894  if (link->ce_mask & LINK_ATTR_MAP && link->l_map.lm_irq)
895  nl_dump(p, "irq %u ", link->l_map.lm_irq);
896 
897  if (link->ce_mask & LINK_ATTR_IFINDEX)
898  nl_dump(p, "index %u ", link->l_index);
899 
900  if (link->ce_mask & LINK_ATTR_PROMISCUITY && link->l_promiscuity > 0)
901  nl_dump(p, "promisc-mode (%u users) ", link->l_promiscuity);
902 
903  nl_dump(p, "\n");
904 
905  if (link->ce_mask & LINK_ATTR_IFALIAS)
906  nl_dump_line(p, " alias %s\n", link->l_ifalias);
907 
908  nl_dump_line(p, " ");
909 
910  if (link->ce_mask & LINK_ATTR_NUM_TX_QUEUES)
911  nl_dump(p, "txq %u ", link->l_num_tx_queues);
912 
913  if (link->ce_mask & LINK_ATTR_NUM_RX_QUEUES)
914  nl_dump(p, "rxq %u ", link->l_num_rx_queues);
915 
916  if (link->ce_mask & LINK_ATTR_BRD)
917  nl_dump(p, "brd %s ", nl_addr2str(link->l_bcast, buf,
918  sizeof(buf)));
919 
920  if ((link->ce_mask & LINK_ATTR_OPERSTATE) &&
921  link->l_operstate != IF_OPER_UNKNOWN) {
922  rtnl_link_operstate2str(link->l_operstate, buf, sizeof(buf));
923  nl_dump(p, "state %s ", buf);
924  }
925 
926  if (link->ce_mask & LINK_ATTR_NUM_VF)
927  nl_dump(p, "num-vf %u ", link->l_num_vf);
928 
929  nl_dump(p, "mode %s ",
930  rtnl_link_mode2str(link->l_linkmode, buf, sizeof(buf)));
931 
932  nl_dump(p, "carrier %s",
933  rtnl_link_carrier2str(link->l_carrier, buf, sizeof(buf)));
934 
935  if (link->ce_mask & LINK_ATTR_CARRIER_CHANGES)
936  nl_dump(p, " carrier-changes %u", link->l_carrier_changes);
937 
938  nl_dump(p, "\n");
939 
940  if (link->l_info_ops && link->l_info_ops->io_dump[NL_DUMP_DETAILS])
941  link->l_info_ops->io_dump[NL_DUMP_DETAILS](link, p);
942 
943  do_foreach_af(link, af_dump_details, p);
944 
945  if (link->ce_mask & LINK_ATTR_VF_LIST)
946  rtnl_link_sriov_dump_details(link, p);
947 }
948 
949 static void link_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
950 {
951  struct rtnl_link *link = (struct rtnl_link *) obj;
952  char *unit, fmt[64];
953  float res;
954 
955  link_dump_details(obj, p);
956 
957  nl_dump_line(p, " Stats: bytes packets errors "
958  " dropped fifo-err compressed\n");
959 
960  res = nl_cancel_down_bytes(link->l_stats[RTNL_LINK_RX_BYTES], &unit);
961 
962  strcpy(fmt, " RX %X.2f %s %10" PRIu64 " %10" PRIu64 " %10" PRIu64 " %10" PRIu64 " %10" PRIu64 "\n");
963  fmt[9] = *unit == 'B' ? '9' : '7';
964 
965  nl_dump_line(p, fmt, res, unit,
966  link->l_stats[RTNL_LINK_RX_PACKETS],
967  link->l_stats[RTNL_LINK_RX_ERRORS],
968  link->l_stats[RTNL_LINK_RX_DROPPED],
969  link->l_stats[RTNL_LINK_RX_FIFO_ERR],
970  link->l_stats[RTNL_LINK_RX_COMPRESSED]);
971 
972  res = nl_cancel_down_bytes(link->l_stats[RTNL_LINK_TX_BYTES], &unit);
973 
974  strcpy(fmt, " TX %X.2f %s %10" PRIu64 " %10" PRIu64 " %10" PRIu64 " %10" PRIu64 " %10" PRIu64 "\n");
975  fmt[9] = *unit == 'B' ? '9' : '7';
976 
977  nl_dump_line(p, fmt, res, unit,
978  link->l_stats[RTNL_LINK_TX_PACKETS],
979  link->l_stats[RTNL_LINK_TX_ERRORS],
980  link->l_stats[RTNL_LINK_TX_DROPPED],
981  link->l_stats[RTNL_LINK_TX_FIFO_ERR],
982  link->l_stats[RTNL_LINK_TX_COMPRESSED]);
983 
984  nl_dump_line(p, " Errors: length over crc "
985  " frame missed multicast\n");
986 
987  nl_dump_line(p, " RX %10" PRIu64 " %10" PRIu64 " %10"
988  PRIu64 " %10" PRIu64 " %10" PRIu64 " %10"
989  PRIu64 "\n",
990  link->l_stats[RTNL_LINK_RX_LEN_ERR],
991  link->l_stats[RTNL_LINK_RX_OVER_ERR],
992  link->l_stats[RTNL_LINK_RX_CRC_ERR],
993  link->l_stats[RTNL_LINK_RX_FRAME_ERR],
994  link->l_stats[RTNL_LINK_RX_MISSED_ERR],
995  link->l_stats[RTNL_LINK_MULTICAST]);
996 
997  nl_dump_line(p, " aborted carrier heartbeat "
998  " window collision\n");
999 
1000  nl_dump_line(p, " TX %10" PRIu64 " %10" PRIu64 " %10"
1001  PRIu64 " %10" PRIu64 " %10" PRIu64 "\n",
1002  link->l_stats[RTNL_LINK_TX_ABORT_ERR],
1003  link->l_stats[RTNL_LINK_TX_CARRIER_ERR],
1004  link->l_stats[RTNL_LINK_TX_HBEAT_ERR],
1005  link->l_stats[RTNL_LINK_TX_WIN_ERR],
1006  link->l_stats[RTNL_LINK_COLLISIONS]);
1007 
1008  if (link->l_info_ops && link->l_info_ops->io_dump[NL_DUMP_STATS])
1009  link->l_info_ops->io_dump[NL_DUMP_STATS](link, p);
1010 
1011  do_foreach_af(link, af_dump_stats, p);
1012 
1013  if (link->ce_mask & LINK_ATTR_VF_LIST)
1014  rtnl_link_sriov_dump_stats(link, p);
1015 }
1016 
1017 #if 0
1018 static int link_handle_event(struct nl_object *a, struct rtnl_link_event_cb *cb)
1019 {
1020  struct rtnl_link *l = (struct rtnl_link *) a;
1021  struct nl_cache *c = dp_cache(a);
1022  int nevents = 0;
1023 
1024  if (l->l_change == ~0U) {
1025  if (l->ce_msgtype == RTM_NEWLINK)
1026  cb->le_register(l);
1027  else
1028  cb->le_unregister(l);
1029 
1030  return 1;
1031  }
1032 
1033  if (l->l_change & IFF_SLAVE) {
1034  if (l->l_flags & IFF_SLAVE) {
1035  struct rtnl_link *m = rtnl_link_get(c, l->l_master);
1036  cb->le_new_bonding(l, m);
1037  if (m)
1038  rtnl_link_put(m);
1039  } else
1040  cb->le_cancel_bonding(l);
1041  }
1042 
1043 #if 0
1044  if (l->l_change & IFF_UP && l->l_change & IFF_RUNNING)
1045  dp_dump_line(p, line++, "link %s changed state to %s.\n",
1046  l->l_name, l->l_flags & IFF_UP ? "up" : "down");
1047 
1048  if (l->l_change & IFF_PROMISC) {
1049  dp_new_line(p, line++);
1050  dp_dump(p, "link %s %s promiscuous mode.\n",
1051  l->l_name, l->l_flags & IFF_PROMISC ? "entered" : "left");
1052  }
1053 
1054  if (line == 0)
1055  dp_dump_line(p, line++, "link %s sent unknown event.\n",
1056  l->l_name);
1057 #endif
1058 
1059  return nevents;
1060 }
1061 #endif
1062 
1063 
1064 static void link_keygen(struct nl_object *obj, uint32_t *hashkey,
1065  uint32_t table_sz)
1066 {
1067  struct rtnl_link *link = (struct rtnl_link *) obj;
1068  unsigned int lkey_sz;
1069  struct link_hash_key {
1070  uint32_t l_index;
1071  uint32_t l_family;
1072  } __attribute__((packed)) lkey;
1073 
1074  lkey_sz = sizeof(lkey);
1075  lkey.l_index = link->l_index;
1076  lkey.l_family = link->l_family;
1077 
1078  *hashkey = nl_hash(&lkey, lkey_sz, 0) % table_sz;
1079 
1080  NL_DBG(5, "link %p key (dev %d fam %d) keysz %d, hash 0x%x\n",
1081  link, lkey.l_index, lkey.l_family, lkey_sz, *hashkey);
1082 
1083  return;
1084 }
1085 
1086 static uint64_t link_compare(struct nl_object *_a, struct nl_object *_b,
1087  uint64_t attrs, int flags)
1088 {
1089  struct rtnl_link *a = (struct rtnl_link *) _a;
1090  struct rtnl_link *b = (struct rtnl_link *) _b;
1091  uint64_t diff = 0;
1092 
1093 #define LINK_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, LINK_ATTR_##ATTR, a, b, EXPR)
1094 
1095  diff |= LINK_DIFF(IFINDEX, a->l_index != b->l_index);
1096  diff |= LINK_DIFF(MTU, a->l_mtu != b->l_mtu);
1097  diff |= LINK_DIFF(LINK, a->l_link != b->l_link);
1098  diff |= LINK_DIFF(LINK_NETNSID, a->l_link_netnsid != b->l_link_netnsid);
1099  diff |= LINK_DIFF(TXQLEN, a->l_txqlen != b->l_txqlen);
1100  diff |= LINK_DIFF(WEIGHT, a->l_weight != b->l_weight);
1101  diff |= LINK_DIFF(MASTER, a->l_master != b->l_master);
1102  diff |= LINK_DIFF(FAMILY, a->l_family != b->l_family);
1103  diff |= LINK_DIFF(OPERSTATE, a->l_operstate != b->l_operstate);
1104  diff |= LINK_DIFF(LINKMODE, a->l_linkmode != b->l_linkmode);
1105  diff |= LINK_DIFF(QDISC, strcmp(a->l_qdisc, b->l_qdisc));
1106  diff |= LINK_DIFF(IFNAME, strcmp(a->l_name, b->l_name));
1107  diff |= LINK_DIFF(ADDR, nl_addr_cmp(a->l_addr, b->l_addr));
1108  diff |= LINK_DIFF(BRD, nl_addr_cmp(a->l_bcast, b->l_bcast));
1109  diff |= LINK_DIFF(IFALIAS, strcmp(a->l_ifalias, b->l_ifalias));
1110  diff |= LINK_DIFF(NUM_VF, a->l_num_vf != b->l_num_vf);
1111  diff |= LINK_DIFF(PROMISCUITY, a->l_promiscuity != b->l_promiscuity);
1112  diff |= LINK_DIFF(NUM_TX_QUEUES,a->l_num_tx_queues != b->l_num_tx_queues);
1113  diff |= LINK_DIFF(NUM_RX_QUEUES,a->l_num_rx_queues != b->l_num_rx_queues);
1114  diff |= LINK_DIFF(GROUP, a->l_group != b->l_group);
1115 
1116  if (flags & LOOSE_COMPARISON)
1117  diff |= LINK_DIFF(FLAGS,
1118  (a->l_flags ^ b->l_flags) & b->l_flag_mask);
1119  else
1120  diff |= LINK_DIFF(FLAGS, a->l_flags != b->l_flags);
1121 
1122  /*
1123  * Compare LINK_ATTR_PROTINFO af_data
1124  */
1125  if (a->l_family == b->l_family) {
1126  if (rtnl_link_af_data_compare(a, b, a->l_family) != 0)
1127  goto protinfo_mismatch;
1128  }
1129 
1130  diff |= LINK_DIFF(LINKINFO, rtnl_link_info_data_compare(a, b, flags) != 0);
1131 out:
1132  return diff;
1133 
1134 protinfo_mismatch:
1135  diff |= LINK_DIFF(PROTINFO, 1);
1136  goto out;
1137 
1138 #undef LINK_DIFF
1139 }
1140 
1141 static const struct trans_tbl link_attrs[] = {
1142  __ADD(LINK_ATTR_MTU, mtu),
1143  __ADD(LINK_ATTR_LINK, link),
1144  __ADD(LINK_ATTR_TXQLEN, txqlen),
1145  __ADD(LINK_ATTR_WEIGHT, weight),
1146  __ADD(LINK_ATTR_MASTER, master),
1147  __ADD(LINK_ATTR_QDISC, qdisc),
1148  __ADD(LINK_ATTR_MAP, map),
1149  __ADD(LINK_ATTR_ADDR, address),
1150  __ADD(LINK_ATTR_BRD, broadcast),
1151  __ADD(LINK_ATTR_FLAGS, flags),
1152  __ADD(LINK_ATTR_IFNAME, name),
1153  __ADD(LINK_ATTR_IFINDEX, ifindex),
1154  __ADD(LINK_ATTR_FAMILY, family),
1155  __ADD(LINK_ATTR_ARPTYPE, arptype),
1156  __ADD(LINK_ATTR_STATS, stats),
1157  __ADD(LINK_ATTR_CHANGE, change),
1158  __ADD(LINK_ATTR_OPERSTATE, operstate),
1159  __ADD(LINK_ATTR_LINKMODE, linkmode),
1160  __ADD(LINK_ATTR_IFALIAS, ifalias),
1161  __ADD(LINK_ATTR_NUM_VF, num_vf),
1162  __ADD(LINK_ATTR_PROMISCUITY, promiscuity),
1163  __ADD(LINK_ATTR_NUM_TX_QUEUES, num_tx_queues),
1164  __ADD(LINK_ATTR_NUM_RX_QUEUES, num_rx_queues),
1165  __ADD(LINK_ATTR_GSO_MAX_SEGS, gso_max_segs),
1166  __ADD(LINK_ATTR_GSO_MAX_SIZE, gso_max_size),
1167  __ADD(LINK_ATTR_GROUP, group),
1168  __ADD(LINK_ATTR_CARRIER, carrier),
1169  __ADD(LINK_ATTR_CARRIER_CHANGES, carrier_changes),
1170  __ADD(LINK_ATTR_PHYS_PORT_ID, phys_port_id),
1171  __ADD(LINK_ATTR_PHYS_PORT_NAME, phys_port_name),
1172  __ADD(LINK_ATTR_PHYS_SWITCH_ID, phys_switch_id),
1173  __ADD(LINK_ATTR_NS_FD, ns_fd),
1174  __ADD(LINK_ATTR_NS_PID, ns_pid),
1175  __ADD(LINK_ATTR_LINK_NETNSID, link_netnsid),
1176 };
1177 
1178 static char *link_attrs2str(int attrs, char *buf, size_t len)
1179 {
1180  return __flags2str(attrs, buf, len, link_attrs,
1181  ARRAY_SIZE(link_attrs));
1182 }
1183 
1184 /**
1185  * @name Get / List
1186  * @{
1187  */
1188 
1189 
1190 /**
1191  * Allocate link cache and fill in all configured links.
1192  * @arg sk Netlink socket.
1193  * @arg family Link address family or AF_UNSPEC
1194  * @arg result Pointer to store resulting cache.
1195  * @arg flags Flags to set in link cache before filling
1196  *
1197  * Allocates and initializes a new link cache. If \c sk is valid, a netlink
1198  * message is sent to the kernel requesting a full dump of all configured
1199  * links. The returned messages are parsed and filled into the cache. If
1200  * the operation succeeds, the resulting cache will contain a link object for
1201  * each link configured in the kernel. If \c sk is NULL, returns 0 but the
1202  * cache is still empty.
1203  *
1204  * If \c family is set to an address family other than \c AF_UNSPEC the
1205  * contents of the cache can be limited to a specific address family.
1206  * Currently the following address families are supported:
1207  * - AF_BRIDGE
1208  * - AF_INET6
1209  *
1210  * @route_doc{link_list, Get List of Links}
1211  * @see rtnl_link_get()
1212  * @see rtnl_link_get_by_name()
1213  * @return 0 on success or a negative error code.
1214  */
1215 int rtnl_link_alloc_cache_flags(struct nl_sock *sk, int family,
1216  struct nl_cache **result, unsigned int flags)
1217 {
1218  struct nl_cache * cache;
1219  int err;
1220 
1221  cache = nl_cache_alloc(&rtnl_link_ops);
1222  if (!cache)
1223  return -NLE_NOMEM;
1224 
1225  cache->c_iarg1 = family;
1226 
1227  if (flags)
1228  nl_cache_set_flags(cache, flags);
1229 
1230  if (sk && (err = nl_cache_refill(sk, cache)) < 0) {
1231  nl_cache_free(cache);
1232  return err;
1233  }
1234 
1235  *result = cache;
1236  return 0;
1237 }
1238 
1239 /**
1240  * Allocate link cache and fill in all configured links.
1241  * @arg sk Netlink socket.
1242  * @arg family Link address family or AF_UNSPEC
1243  * @arg result Pointer to store resulting cache.
1244  *
1245  * Allocates and initializes a new link cache. If \c sk is valid, a netlink
1246  * message is sent to the kernel requesting a full dump of all configured
1247  * links. The returned messages are parsed and filled into the cache. If
1248  * the operation succeeds, the resulting cache will contain a link object for
1249  * each link configured in the kernel. If \c sk is NULL, returns 0 but the
1250  * cache is still empty.
1251  *
1252  * If \c family is set to an address family other than \c AF_UNSPEC the
1253  * contents of the cache can be limited to a specific address family.
1254  * Currently the following address families are supported:
1255  * - AF_BRIDGE
1256  * - AF_INET6
1257  *
1258  * @route_doc{link_list, Get List of Links}
1259  * @see rtnl_link_get()
1260  * @see rtnl_link_get_by_name()
1261  * @return 0 on success or a negative error code.
1262  */
1263 int rtnl_link_alloc_cache(struct nl_sock *sk, int family, struct nl_cache **result)
1264 {
1265  return rtnl_link_alloc_cache_flags(sk, family, result, 0);
1266 }
1267 
1268 
1269 /**
1270  * Lookup link in cache by interface index
1271  * @arg cache Link cache
1272  * @arg ifindex Interface index
1273  *
1274  * Searches through the provided cache looking for a link with matching
1275  * interface index.
1276  *
1277  * @attention The reference counter of the returned link object will be
1278  * incremented. Use rtnl_link_put() to release the reference.
1279  *
1280  * @route_doc{link_list, Get List of Links}
1281  * @see rtnl_link_get_by_name()
1282  * @return Link object or NULL if no match was found.
1283  */
1284 struct rtnl_link *rtnl_link_get(struct nl_cache *cache, int ifindex)
1285 {
1286  struct rtnl_link *link;
1287 
1288  if (cache->c_ops != &rtnl_link_ops)
1289  return NULL;
1290 
1291  nl_list_for_each_entry(link, &cache->c_items, ce_list) {
1292  if (link->l_index == ifindex) {
1293  nl_object_get((struct nl_object *) link);
1294  return link;
1295  }
1296  }
1297 
1298  return NULL;
1299 }
1300 
1301 /**
1302  * Lookup link in cache by link name
1303  * @arg cache Link cache
1304  * @arg name Name of link
1305  *
1306  * Searches through the provided cache looking for a link with matching
1307  * link name
1308  *
1309  * @attention The reference counter of the returned link object will be
1310  * incremented. Use rtnl_link_put() to release the reference.
1311  *
1312  * @route_doc{link_list, Get List of Links}
1313  * @see rtnl_link_get()
1314  * @return Link object or NULL if no match was found.
1315  */
1316 struct rtnl_link *rtnl_link_get_by_name(struct nl_cache *cache,
1317  const char *name)
1318 {
1319  struct rtnl_link *link;
1320 
1321  if (cache->c_ops != &rtnl_link_ops)
1322  return NULL;
1323 
1324  nl_list_for_each_entry(link, &cache->c_items, ce_list) {
1325  if (!strcmp(name, link->l_name)) {
1326  nl_object_get((struct nl_object *) link);
1327  return link;
1328  }
1329  }
1330 
1331  return NULL;
1332 }
1333 
1334 /**
1335  * Construct RTM_GETLINK netlink message
1336  * @arg ifindex Interface index
1337  * @arg name Name of link
1338  * @arg result Pointer to store resulting netlink message
1339  *
1340  * The behaviour of this function is identical to rtnl_link_get_kernel()
1341  * with the exception that it will not send the message but return it in
1342  * the provided return pointer instead.
1343  *
1344  * @see rtnl_link_get_kernel()
1345  *
1346  * @return 0 on success or a negative error code.
1347  */
1348 int rtnl_link_build_get_request(int ifindex, const char *name,
1349  struct nl_msg **result)
1350 {
1351  struct ifinfomsg ifi;
1352  struct nl_msg *msg;
1353  __u32 vf_mask = RTEXT_FILTER_VF;
1354  int err = -NLE_MSGSIZE;
1355 
1356  if (ifindex <= 0 && !name) {
1357  APPBUG("ifindex or name must be specified");
1358  return -NLE_MISSING_ATTR;
1359  }
1360 
1361  memset(&ifi, 0, sizeof(ifi));
1362 
1363  if (!(msg = nlmsg_alloc_simple(RTM_GETLINK, 0)))
1364  return -NLE_NOMEM;
1365 
1366  if (ifindex > 0)
1367  ifi.ifi_index = ifindex;
1368 
1369  if (nlmsg_append(msg, &ifi, sizeof(ifi), NLMSG_ALIGNTO) < 0) {
1370  err = -NLE_MSGSIZE;
1371  goto nla_put_failure;
1372  }
1373 
1374  if (name)
1375  NLA_PUT_STRING(msg, IFLA_IFNAME, name);
1376 
1377  err = nla_put(msg, IFLA_EXT_MASK, sizeof(vf_mask), &vf_mask);
1378  if (err)
1379  goto nla_put_failure;
1380 
1381  *result = msg;
1382  return 0;
1383 
1384 nla_put_failure:
1385  nlmsg_free(msg);
1386  return err;
1387 }
1388 
1389 /**
1390  * Get a link object directly from kernel
1391  * @arg sk Netlink socket
1392  * @arg ifindex Interface index
1393  * @arg name Name of link
1394  * @arg result Pointer to store resulting link object
1395  *
1396  * This function builds a \c RTM_GETLINK netlink message to request
1397  * a specific link directly from the kernel. The returned answer is
1398  * parsed into a struct rtnl_link object and returned via the result
1399  * pointer or -NLE_OBJ_NOTFOUND is returned if no matching link was
1400  * found.
1401  *
1402  * Older kernels do not support lookup by name. In that case, libnl
1403  * will fail with -NLE_OPNOTSUPP. Note that previous version of libnl
1404  * failed in this case with -NLE_INVAL. You can check libnl behavior
1405  * using NL_CAPABILITY_ROUTE_LINK_GET_KERNEL_FAIL_OPNOTSUPP capability.
1406  *
1407  * @route_doc{link_direct_lookup, Lookup Single Link (Direct Lookup)}
1408  * @return 0 on success or a negative error code.
1409  */
1410 int rtnl_link_get_kernel(struct nl_sock *sk, int ifindex, const char *name,
1411  struct rtnl_link **result)
1412 {
1413  struct nl_msg *msg = NULL;
1414  struct nl_object *obj;
1415  int err;
1416  int syserr;
1417 
1418  if ((err = rtnl_link_build_get_request(ifindex, name, &msg)) < 0)
1419  return err;
1420 
1421  err = nl_send_auto(sk, msg);
1422  nlmsg_free(msg);
1423  if (err < 0)
1424  return err;
1425 
1426  if ((err = nl_pickup_keep_syserr(sk, link_msg_parser, &obj, &syserr)) < 0) {
1427  if (syserr == -EINVAL &&
1428  ifindex <= 0 &&
1429  name && *name) {
1430  /* Older kernels do not support lookup by ifname. This was added
1431  * by commit kernel a3d1289126e7b14307074b76bf1677015ea5036f .
1432  * Detect this error case and return NLE_OPNOTSUPP instead of
1433  * NLE_INVAL. */
1434  return -NLE_OPNOTSUPP;
1435  }
1436  return err;
1437  }
1438 
1439  /* We have used link_msg_parser(), object is definitely a link */
1440  *result = (struct rtnl_link *) obj;
1441 
1442  /* If an object has been returned, we also need to wait for the ACK */
1443  if (err == 0 && obj)
1444  wait_for_ack(sk);
1445 
1446  return 0;
1447 }
1448 
1449 /**
1450  * Translate interface index to corresponding link name
1451  * @arg cache Link cache
1452  * @arg ifindex Interface index
1453  * @arg dst String to store name
1454  * @arg len Length of destination string
1455  *
1456  * Translates the specified interface index to the corresponding
1457  * link name and stores the name in the destination string.
1458  *
1459  * @route_doc{link_translate_ifindex, Translating interface index to link name}
1460  * @see rtnl_link_name2i()
1461  * @return Name of link or NULL if no match was found.
1462  */
1463 char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst,
1464  size_t len)
1465 {
1466  struct rtnl_link *link = rtnl_link_get(cache, ifindex);
1467 
1468  if (link) {
1469  strncpy(dst, link->l_name, len - 1);
1470  rtnl_link_put(link);
1471  return dst;
1472  }
1473 
1474  return NULL;
1475 }
1476 
1477 /**
1478  * Translate link name to corresponding interface index
1479  * @arg cache Link cache
1480  * @arg name Name of link
1481  *
1482  * @route_doc{link_translate_ifindex, Translating interface index to link name}
1483  * @see rtnl_link_i2name()
1484  * @return Interface index or 0 if no match was found.
1485  */
1486 int rtnl_link_name2i(struct nl_cache *cache, const char *name)
1487 {
1488  int ifindex = 0;
1489  struct rtnl_link *link;
1490 
1491  link = rtnl_link_get_by_name(cache, name);
1492  if (link) {
1493  ifindex = link->l_index;
1494  rtnl_link_put(link);
1495  }
1496 
1497  return ifindex;
1498 }
1499 
1500 /** @} */
1501 
1502 int rtnl_link_fill_info(struct nl_msg *msg, struct rtnl_link *link)
1503 {
1504  if (link->ce_mask & LINK_ATTR_ADDR)
1505  NLA_PUT_ADDR(msg, IFLA_ADDRESS, link->l_addr);
1506 
1507  if (link->ce_mask & LINK_ATTR_BRD)
1508  NLA_PUT_ADDR(msg, IFLA_BROADCAST, link->l_bcast);
1509 
1510  if (link->ce_mask & LINK_ATTR_MTU)
1511  NLA_PUT_U32(msg, IFLA_MTU, link->l_mtu);
1512 
1513  if (link->ce_mask & LINK_ATTR_TXQLEN)
1514  NLA_PUT_U32(msg, IFLA_TXQLEN, link->l_txqlen);
1515 
1516  if (link->ce_mask & LINK_ATTR_WEIGHT)
1517  NLA_PUT_U32(msg, IFLA_WEIGHT, link->l_weight);
1518 
1519  if (link->ce_mask & LINK_ATTR_IFNAME)
1520  NLA_PUT_STRING(msg, IFLA_IFNAME, link->l_name);
1521 
1522  if (link->ce_mask & LINK_ATTR_OPERSTATE)
1523  NLA_PUT_U8(msg, IFLA_OPERSTATE, link->l_operstate);
1524 
1525  if (link->ce_mask & LINK_ATTR_CARRIER)
1526  NLA_PUT_U8(msg, IFLA_CARRIER, link->l_carrier);
1527 
1528  if (link->ce_mask & LINK_ATTR_LINKMODE)
1529  NLA_PUT_U8(msg, IFLA_LINKMODE, link->l_linkmode);
1530 
1531  if (link->ce_mask & LINK_ATTR_IFALIAS)
1532  NLA_PUT_STRING(msg, IFLA_IFALIAS, link->l_ifalias);
1533 
1534  if (link->ce_mask & LINK_ATTR_LINK)
1535  NLA_PUT_U32(msg, IFLA_LINK, link->l_link);
1536 
1537  if (link->ce_mask & LINK_ATTR_LINK_NETNSID)
1538  NLA_PUT_S32(msg, IFLA_LINK_NETNSID, link->l_link_netnsid);
1539 
1540  if (link->ce_mask & LINK_ATTR_MASTER)
1541  NLA_PUT_U32(msg, IFLA_MASTER, link->l_master);
1542 
1543  if (link->ce_mask & LINK_ATTR_NUM_TX_QUEUES)
1544  NLA_PUT_U32(msg, IFLA_NUM_TX_QUEUES, link->l_num_tx_queues);
1545 
1546  if (link->ce_mask & LINK_ATTR_NUM_RX_QUEUES)
1547  NLA_PUT_U32(msg, IFLA_NUM_RX_QUEUES, link->l_num_rx_queues);
1548 
1549  if (link->ce_mask & LINK_ATTR_NS_FD)
1550  NLA_PUT_U32(msg, IFLA_NET_NS_FD, link->l_ns_fd);
1551 
1552  if (link->ce_mask & LINK_ATTR_NS_PID)
1553  NLA_PUT_U32(msg, IFLA_NET_NS_PID, link->l_ns_pid);
1554 
1555  return 0;
1556 
1557 nla_put_failure:
1558  return -NLE_MSGSIZE;
1559 }
1560 
1561 static int build_link_msg(int cmd, struct ifinfomsg *hdr,
1562  struct rtnl_link *link, int flags, struct nl_msg **result)
1563 {
1564  struct nl_msg *msg;
1565  struct nlattr *af_spec;
1566 
1567  msg = nlmsg_alloc_simple(cmd, flags);
1568  if (!msg)
1569  return -NLE_NOMEM;
1570 
1571  if (nlmsg_append(msg, hdr, sizeof(*hdr), NLMSG_ALIGNTO) < 0)
1572  goto nla_put_failure;
1573 
1574  if (rtnl_link_fill_info(msg, link))
1575  goto nla_put_failure;
1576 
1577  if (link->ce_mask & LINK_ATTR_GROUP)
1578  NLA_PUT_U32(msg, IFLA_GROUP, link->l_group);
1579 
1580  if (link->ce_mask & LINK_ATTR_LINKINFO) {
1581  struct nlattr *info;
1582 
1583  if (!(info = nla_nest_start(msg, IFLA_LINKINFO)))
1584  goto nla_put_failure;
1585 
1586  NLA_PUT_STRING(msg, IFLA_INFO_KIND, link->l_info_kind);
1587 
1588  if (link->l_info_ops) {
1589  if (link->l_info_ops->io_put_attrs &&
1590  link->l_info_ops->io_put_attrs(msg, link) < 0)
1591  goto nla_put_failure;
1592  }
1593 
1594  nla_nest_end(msg, info);
1595  }
1596 
1597  if (link->ce_mask & LINK_ATTR_VF_LIST) {
1598  if (rtnl_link_sriov_fill_vflist(msg, link) < 0)
1599  goto nla_put_failure;
1600  }
1601 
1602  if (do_foreach_af(link, af_fill_pi, msg) < 0)
1603  goto nla_put_failure;
1604 
1605  if (!(af_spec = nla_nest_start(msg, IFLA_AF_SPEC)))
1606  goto nla_put_failure;
1607 
1608  if (do_foreach_af(link, af_fill, msg) < 0)
1609  goto nla_put_failure;
1610 
1611  nla_nest_end(msg, af_spec);
1612 
1613  *result = msg;
1614  return 0;
1615 
1616 nla_put_failure:
1617  nlmsg_free(msg);
1618  return -NLE_MSGSIZE;
1619 }
1620 
1621 /**
1622  * @name Add / Modify
1623  * @{
1624  */
1625 
1626 /**
1627  * Build a netlink message requesting the addition of new virtual link
1628  * @arg link new link to add
1629  * @arg flags additional netlink message flags
1630  * @arg result pointer to store resulting netlink message
1631  *
1632  * The behaviour of this function is identical to rtnl_link_add() with
1633  * the exception that it will not send the message but return it in the
1634  * provided return pointer instead.
1635  *
1636  * @see rtnl_link_add()
1637  *
1638  * @note This operation is not supported on all kernel versions.
1639  *
1640  * @return 0 on success or a negative error code.
1641  */
1642 int rtnl_link_build_add_request(struct rtnl_link *link, int flags,
1643  struct nl_msg **result)
1644 {
1645  struct ifinfomsg ifi = {
1646  .ifi_family = link->l_family,
1647  .ifi_index = link->l_index,
1648  .ifi_flags = link->l_flags,
1649  .ifi_change = link->l_flag_mask,
1650  };
1651 
1652  return build_link_msg(RTM_NEWLINK, &ifi, link, flags, result);
1653 }
1654 
1655 /**
1656  * Add virtual link
1657  * @arg sk netlink socket.
1658  * @arg link new link to add
1659  * @arg flags additional netlink message flags
1660  *
1661  * Builds a \c RTM_NEWLINK netlink message requesting the addition of
1662  * a new virtual link.
1663  *
1664  * After sending, the function will wait for the ACK or an eventual
1665  * error message to be received and will therefore block until the
1666  * operation has been completed.
1667  *
1668  * @copydoc auto_ack_warning
1669  *
1670  * @return 0 on success or a negative error code.
1671  */
1672 int rtnl_link_add(struct nl_sock *sk, struct rtnl_link *link, int flags)
1673 {
1674  struct nl_msg *msg;
1675  int err;
1676 
1677  err = rtnl_link_build_add_request(link, flags, &msg);
1678  if (err < 0)
1679  return err;
1680 
1681  return nl_send_sync(sk, msg);
1682 }
1683 
1684 /**
1685  * Build a netlink message requesting the modification of link
1686  * @arg orig original link to change
1687  * @arg changes link containing the changes to be made
1688  * @arg flags additional netlink message flags
1689  * @arg result pointer to store resulting netlink message
1690  *
1691  * The behaviour of this function is identical to rtnl_link_change() with
1692  * the exception that it will not send the message but return it in the
1693  * provided return pointer instead.
1694  *
1695  * @see rtnl_link_change()
1696  *
1697  * @note The resulting message will have message type set to RTM_NEWLINK
1698  * which may not work with older kernels. You may have to modify it
1699  * to RTM_SETLINK (does not allow changing link info attributes) to
1700  * have the change request work with older kernels.
1701  *
1702  * @return 0 on success or a negative error code.
1703  */
1705  struct rtnl_link *changes, int flags,
1706  struct nl_msg **result)
1707 {
1708  struct ifinfomsg ifi = {
1709  .ifi_family = orig->l_family,
1710  .ifi_index = orig->l_index,
1711  };
1712  int err, rt;
1713 
1714  if (changes->ce_mask & LINK_ATTR_FLAGS) {
1715  ifi.ifi_flags = orig->l_flags & ~changes->l_flag_mask;
1716  ifi.ifi_flags |= changes->l_flags;
1717  ifi.ifi_change = changes->l_flag_mask;
1718  }
1719 
1720  if (changes->l_family && changes->l_family != orig->l_family) {
1721  APPBUG("link change: family is immutable");
1722  return -NLE_IMMUTABLE;
1723  }
1724 
1725  /* Avoid unnecessary name change requests */
1726  if (orig->ce_mask & LINK_ATTR_IFINDEX &&
1727  orig->ce_mask & LINK_ATTR_IFNAME &&
1728  changes->ce_mask & LINK_ATTR_IFNAME &&
1729  !strcmp(orig->l_name, changes->l_name))
1730  changes->ce_mask &= ~LINK_ATTR_IFNAME;
1731 
1732  rt = af_request_type(orig->l_family);
1733 
1734  if ((err = build_link_msg(rt, &ifi, changes, flags, result)) < 0)
1735  goto errout;
1736 
1737  return 0;
1738 
1739 errout:
1740  return err;
1741 }
1742 
1743 /**
1744  * Change link
1745  * @arg sk netlink socket.
1746  * @arg orig original link to be changed
1747  * @arg changes link containing the changes to be made
1748  * @arg flags additional netlink message flags
1749  *
1750  * Builds a \c RTM_NEWLINK netlink message requesting the change of
1751  * a network link. If -EOPNOTSUPP is returned by the kernel, the
1752  * message type will be changed to \c RTM_SETLINK and the message is
1753  * resent to work around older kernel versions.
1754  *
1755  * The link to be changed is looked up based on the interface index
1756  * supplied in the \p orig link. Optionaly the link name is used but
1757  * only if no interface index is provided, otherwise providing an
1758  * link name will result in the link name being changed.
1759  *
1760  * If no matching link exists, the function will return
1761  * -NLE_OBJ_NOTFOUND.
1762  *
1763  * After sending, the function will wait for the ACK or an eventual
1764  * error message to be received and will therefore block until the
1765  * operation has been completed.
1766  *
1767  * @copydoc auto_ack_warning
1768  *
1769  * @note The link name can only be changed if the link has been put
1770  * in opertional down state. (~IF_UP)
1771  *
1772  * @return 0 on success or a negative error code.
1773  */
1774 int rtnl_link_change(struct nl_sock *sk, struct rtnl_link *orig,
1775  struct rtnl_link *changes, int flags)
1776 {
1777  struct nl_msg *msg;
1778  int err;
1779 
1780  err = rtnl_link_build_change_request(orig, changes, flags, &msg);
1781  if (err < 0)
1782  return err;
1783 
1784 retry:
1785  err = nl_send_auto_complete(sk, msg);
1786  if (err < 0)
1787  goto errout;
1788 
1789  err = wait_for_ack(sk);
1790  if (err == -NLE_OPNOTSUPP && msg->nm_nlh->nlmsg_type == RTM_NEWLINK) {
1791  msg->nm_nlh->nlmsg_type = RTM_SETLINK;
1792  goto retry;
1793  }
1794 
1795 errout:
1796  nlmsg_free(msg);
1797  return err;
1798 }
1799 
1800 /** @} */
1801 
1802 /**
1803  * @name Delete
1804  * @{
1805  */
1806 
1807 /**
1808  * Build a netlink message requesting the deletion of a link
1809  * @arg link Link to delete
1810  * @arg result Pointer to store resulting netlink message
1811  *
1812  * The behaviour of this function is identical to rtnl_link_delete() with
1813  * the exception that it will not send the message but return it in the
1814  * provided return pointer instead.
1815  *
1816  * @see rtnl_link_delete()
1817  *
1818  * @return 0 on success or a negative error code.
1819  */
1821  struct nl_msg **result)
1822 {
1823  struct nl_msg *msg;
1824  struct ifinfomsg ifi = {
1825  .ifi_index = link->l_index,
1826  };
1827 
1828  if (!(link->ce_mask & (LINK_ATTR_IFINDEX | LINK_ATTR_IFNAME))) {
1829  APPBUG("ifindex or name must be specified");
1830  return -NLE_MISSING_ATTR;
1831  }
1832 
1833  if (!(msg = nlmsg_alloc_simple(RTM_DELLINK, 0)))
1834  return -NLE_NOMEM;
1835 
1836  if (nlmsg_append(msg, &ifi, sizeof(ifi), NLMSG_ALIGNTO) < 0)
1837  goto nla_put_failure;
1838 
1839  if (link->ce_mask & LINK_ATTR_IFNAME)
1840  NLA_PUT_STRING(msg, IFLA_IFNAME, link->l_name);
1841 
1842  *result = msg;
1843  return 0;
1844 
1845 nla_put_failure:
1846  nlmsg_free(msg);
1847  return -NLE_MSGSIZE;
1848 }
1849 
1850 /**
1851  * Delete link
1852  * @arg sk Netlink socket
1853  * @arg link Link to delete
1854  *
1855  * Builds a \c RTM_DELLINK netlink message requesting the deletion of
1856  * a network link which has been previously added to the kernel and
1857  * sends the message to the kernel.
1858  *
1859  * If no matching link exists, the function will return
1860  * -NLE_OBJ_NOTFOUND.
1861  *
1862  * After sending, the function will wait for the ACK or an eventual
1863  * error message to be received and will therefore block until the
1864  * operation has been completed.
1865  *
1866  * @copydoc auto_ack_warning
1867  *
1868  * @note Only virtual links such as dummy interface or vlan interfaces
1869  * can be deleted. It is not possible to delete physical interfaces
1870  * such as ethernet interfaces or the loopback device.
1871  *
1872  * @return 0 on success or a negative error code.
1873  */
1874 int rtnl_link_delete(struct nl_sock *sk, const struct rtnl_link *link)
1875 {
1876  struct nl_msg *msg;
1877  int err;
1878 
1879  if ((err = rtnl_link_build_delete_request(link, &msg)) < 0)
1880  return err;
1881 
1882  return nl_send_sync(sk, msg);
1883 }
1884 
1885 /** @} */
1886 
1887 /**
1888  * @name Link Object
1889  * @{
1890  */
1891 
1892 /**
1893  * Allocate link object
1894  *
1895  * @see rtnl_link_put()
1896  * @return New link object or NULL if allocation failed
1897  */
1899 {
1900  return (struct rtnl_link *) nl_object_alloc(&link_obj_ops);
1901 }
1902 
1903 /**
1904  * Return a link object reference
1905  * @arg link Link object
1906  */
1907 void rtnl_link_put(struct rtnl_link *link)
1908 {
1909  nl_object_put((struct nl_object *) link);
1910 }
1911 
1912 /**
1913  * Set name of link object
1914  * @arg link Link object
1915  * @arg name New name
1916  *
1917  * @note To change the name of a link in the kernel, set the interface
1918  * index to the link you wish to change, modify the link name using
1919  * this function and pass the link object to rtnl_link_change() or
1920  * rtnl_link_add().
1921  *
1922  * @route_doc{link_attr_name, Link Name}
1923  * @see rtnl_link_get_name()
1924  * @see rtnl_link_set_ifindex()
1925  */
1926 void rtnl_link_set_name(struct rtnl_link *link, const char *name)
1927 {
1928  strncpy(link->l_name, name, sizeof(link->l_name) - 1);
1929  link->ce_mask |= LINK_ATTR_IFNAME;
1930 }
1931 
1932 /**
1933  * Return name of link object
1934  * @arg link Link object
1935  *
1936  * @route_doc{link_attr_name, Link Name}
1937  * @see rtnl_link_set_name()
1938  * @return Link name or NULL if name is not specified
1939  */
1940 char *rtnl_link_get_name(struct rtnl_link *link)
1941 {
1942  return link->ce_mask & LINK_ATTR_IFNAME ? link->l_name : NULL;
1943 }
1944 
1945 /**
1946  * Set the group identifier of a link object
1947  * @arg link Link object
1948  * @arg group Group identifier
1949  */
1950 void rtnl_link_set_group(struct rtnl_link *link, uint32_t group)
1951 {
1952  link->l_group = group;
1953  link->ce_mask |= LINK_ATTR_GROUP;
1954 }
1955 
1956 /**
1957  * Return the group identifier of link object
1958  * @arg link Link object
1959  *
1960  * @return Group identifier or 0 if not set.
1961  */
1962 uint32_t rtnl_link_get_group(struct rtnl_link *link)
1963 {
1964  return link->l_group;
1965 }
1966 
1967 static inline void __assign_addr(struct rtnl_link *link, struct nl_addr **pos,
1968  struct nl_addr *new, int flag)
1969 {
1970  if (*pos)
1971  nl_addr_put(*pos);
1972 
1973  nl_addr_get(new);
1974  *pos = new;
1975 
1976  link->ce_mask |= flag;
1977 }
1978 
1979 /**
1980  * Set link layer address of link object
1981  * @arg link Link object
1982  * @arg addr New link layer address
1983  *
1984  * The function increments the reference counter of the address object
1985  * and overwrites any existing link layer address previously assigned.
1986  *
1987  * @route_doc{link_attr_address, Link layer address}
1988  * @see rtnl_link_get_addr()
1989  */
1990 void rtnl_link_set_addr(struct rtnl_link *link, struct nl_addr *addr)
1991 {
1992  __assign_addr(link, &link->l_addr, addr, LINK_ATTR_ADDR);
1993 }
1994 
1995 /**
1996  * Return link layer address of link object
1997  * @arg link Link object
1998  *
1999  * @copydoc pointer_lifetime_warning
2000  * @route_doc{link_attr_address, Link Layer Address}
2001  * @see rtnl_link_set_addr()
2002  * @return Link layer address or NULL if not set.
2003  */
2004 struct nl_addr *rtnl_link_get_addr(struct rtnl_link *link)
2005 {
2006  return link->ce_mask & LINK_ATTR_ADDR ? link->l_addr : NULL;
2007 }
2008 
2009 /**
2010  * Set link layer broadcast address of link object
2011  * @arg link Link object
2012  * @arg addr New broadcast address
2013  *
2014  * The function increments the reference counter of the address object
2015  * and overwrites any existing link layer broadcast address previously
2016  * assigned.
2017  *
2018  * @route_doc{link_attr_broadcast, Link Layer Broadcast Address}
2019  * @see rtnl_link_get_broadcast()
2020  */
2021 void rtnl_link_set_broadcast(struct rtnl_link *link, struct nl_addr *addr)
2022 {
2023  __assign_addr(link, &link->l_bcast, addr, LINK_ATTR_BRD);
2024 }
2025 
2026 /**
2027  * Return link layer broadcast address of link object
2028  * @arg link Link object
2029  *
2030  * @copydoc pointer_lifetime_warning
2031  * @route_doc{link_attr_address, Link Layer Address}
2032  * @see rtnl_link_set_broadcast()
2033  * @return Link layer address or NULL if not set.
2034  */
2035 struct nl_addr *rtnl_link_get_broadcast(struct rtnl_link *link)
2036 {
2037  return link->ce_mask & LINK_ATTR_BRD ? link->l_bcast : NULL;
2038 }
2039 
2040 /**
2041  * Set flags of link object
2042  * @arg link Link object
2043  * @arg flags Flags
2044  *
2045  * @see rtnl_link_get_flags()
2046  * @see rtnl_link_unset_flags()
2047  */
2048 void rtnl_link_set_flags(struct rtnl_link *link, unsigned int flags)
2049 {
2050  link->l_flag_mask |= flags;
2051  link->l_flags |= flags;
2052  link->ce_mask |= LINK_ATTR_FLAGS;
2053 }
2054 
2055 /**
2056  * Unset flags of link object
2057  * @arg link Link object
2058  * @arg flags Flags
2059  *
2060  * @see rtnl_link_set_flags()
2061  * @see rtnl_link_get_flags()
2062  */
2063 void rtnl_link_unset_flags(struct rtnl_link *link, unsigned int flags)
2064 {
2065  link->l_flag_mask |= flags;
2066  link->l_flags &= ~flags;
2067  link->ce_mask |= LINK_ATTR_FLAGS;
2068 }
2069 
2070 /**
2071  * Return flags of link object
2072  * @arg link Link object
2073  *
2074  * @route_doc{link_attr_flags, Link Flags}
2075  * @see rtnl_link_set_flags()
2076  * @see rtnl_link_unset_flags()
2077  * @return Link flags or 0 if none have been set.
2078  */
2079 unsigned int rtnl_link_get_flags(struct rtnl_link *link)
2080 {
2081  return link->l_flags;
2082 }
2083 
2084 /**
2085  * Set address family of link object
2086  *
2087  * @see rtnl_link_get_family()
2088  */
2089 void rtnl_link_set_family(struct rtnl_link *link, int family)
2090 {
2091  link->l_family = family;
2092  link->ce_mask |= LINK_ATTR_FAMILY;
2093 
2094  if (link->l_af_ops) {
2095  af_free(link, link->l_af_ops,
2096  link->l_af_data[link->l_af_ops->ao_family], NULL);
2097  link->l_af_data[link->l_af_ops->ao_family] = NULL;
2098  }
2099 
2100  link->l_af_ops = af_lookup_and_alloc(link, family);
2101 }
2102 
2103 /**
2104  * Return address family of link object
2105  * @arg link Link object
2106  *
2107  * @see rtnl_link_set_family()
2108  * @return Address family or \c AF_UNSPEC if not specified.
2109  */
2111 {
2112  return link->ce_mask & LINK_ATTR_FAMILY ? link->l_family : AF_UNSPEC;
2113 }
2114 
2115 /**
2116  * Set hardware type of link object
2117  * @arg link Link object
2118  * @arg arptype New hardware type \c (ARPHRD_*)
2119  *
2120  * @route_doc{link_attr_arptype, Hardware Type}
2121  * @copydoc read_only_attribute
2122  * @see rtnl_link_get_arptype()
2123  */
2124 void rtnl_link_set_arptype(struct rtnl_link *link, unsigned int arptype)
2125 {
2126  link->l_arptype = arptype;
2127  link->ce_mask |= LINK_ATTR_ARPTYPE;
2128 }
2129 
2130 /**
2131  * Get hardware type of link object
2132  * @arg link Link object
2133  *
2134  * @route_doc{link_attr_arptype, Hardware Type}
2135  * @see rtnl_link_set_arptype()
2136  * @return Hardware type \c (ARPHRD_ETHER *) or \c ARPHRD_VOID
2137  */
2138 unsigned int rtnl_link_get_arptype(struct rtnl_link *link)
2139 {
2140  if (link->ce_mask & LINK_ATTR_ARPTYPE)
2141  return link->l_arptype;
2142  else
2143  return ARPHRD_VOID;
2144 }
2145 
2146 /**
2147  * Set interface index of link object
2148  * @arg link Link object
2149  * @arg ifindex Interface index
2150  *
2151  * @route_doc{link_attr_ifindex, Interface Index}
2152  * @see rtnl_link_get_ifindex()
2153  */
2154 void rtnl_link_set_ifindex(struct rtnl_link *link, int ifindex)
2155 {
2156  link->l_index = ifindex;
2157  link->ce_mask |= LINK_ATTR_IFINDEX;
2158 }
2159 
2160 
2161 /**
2162  * Return interface index of link object
2163  * @arg link Link object
2164  *
2165  * @route_doc{link_attr_ifindex, Interface Index}
2166  * @see rtnl_link_set_ifindex()
2167  * @return Interface index or 0 if not set.
2168  */
2170 {
2171  return link->l_index;
2172 }
2173 
2174 /**
2175  * Set Maximum Transmission Unit of link object
2176  * @arg link Link object
2177  * @arg mtu New MTU value in number of bytes
2178  *
2179  * @route_doc{link_attr_mtu, Maximum Transmission Unit}
2180  * @see rtnl_link_get_mtu()
2181  */
2182 void rtnl_link_set_mtu(struct rtnl_link *link, unsigned int mtu)
2183 {
2184  link->l_mtu = mtu;
2185  link->ce_mask |= LINK_ATTR_MTU;
2186 }
2187 
2188 /**
2189  * Return maximum transmission unit of link object
2190  * @arg link Link object
2191  *
2192  * @route_doc{link_attr_mtu, Maximum Transmission Unit}
2193  * @see rtnl_link_set_mtu()
2194  * @return MTU in bytes or 0 if not set
2195  */
2196 unsigned int rtnl_link_get_mtu(struct rtnl_link *link)
2197 {
2198  return link->l_mtu;
2199 }
2200 
2201 /**
2202  * Set transmission queue length
2203  * @arg link Link object
2204  * @arg txqlen New queue length
2205  *
2206  * The unit is dependant on the link type. The most common units is number
2207  * of packets.
2208  *
2209  * @route_doc{link_attr_txqlen, Transmission Queue Length}
2210  */
2211 void rtnl_link_set_txqlen(struct rtnl_link *link, unsigned int txqlen)
2212 {
2213  link->l_txqlen = txqlen;
2214  link->ce_mask |= LINK_ATTR_TXQLEN;
2215 }
2216 
2217 /**
2218  * Return transmission queue length
2219  * @arg link Link object
2220  *
2221  * The unit is dependant on the link type. The most common units is number
2222  * of packets.
2223  *
2224  * @route_doc{link_attr_txqlen, Transmission Queue Length}
2225  * @return queue length or 0 if not specified.
2226  */
2227 unsigned int rtnl_link_get_txqlen(struct rtnl_link *link)
2228 {
2229  return link->ce_mask & LINK_ATTR_TXQLEN ? link->l_txqlen : 0;
2230 }
2231 
2232 void rtnl_link_set_link(struct rtnl_link *link, int ifindex)
2233 {
2234  link->l_link = ifindex;
2235  link->ce_mask |= LINK_ATTR_LINK;
2236 }
2237 
2238 int rtnl_link_get_link(struct rtnl_link *link)
2239 {
2240  return link->l_link;
2241 }
2242 
2243 /**
2244  * Set the netnsid of the link
2245  * @arg link Link object
2246  * @link_netnsid the netnsid to set
2247  *
2248  * Sets the IFLA_LINK_NETNSID attribute of the link
2249  * @returns 0 on success
2250  */
2251 int rtnl_link_set_link_netnsid(struct rtnl_link *link, int32_t link_netnsid)
2252 {
2253  link->l_link_netnsid = link_netnsid;
2254  link->ce_mask |= LINK_ATTR_LINK_NETNSID;
2255  return 0;
2256 }
2257 
2258 /**
2259  * Get the netnsid of the link
2260  * @arg link Link object
2261  * @out_link_netnsid the netnsid
2262  *
2263  * Gets the IFLA_LINK_NETNSID attribute of the link
2264  * or returns an error if the value is unset.
2265  *
2266  * @returns 0 on success
2267  */
2268 int rtnl_link_get_link_netnsid(const struct rtnl_link *link, int32_t *out_link_netnsid)
2269 {
2270  if (!(link->ce_mask & LINK_ATTR_LINK_NETNSID))
2271  return -NLE_INVAL;
2272 
2273  *out_link_netnsid = link->l_link_netnsid;
2274  return 0;
2275 }
2276 
2277 /**
2278  * Set master link of link object
2279  * @arg link Link object
2280  * @arg ifindex Interface index of master link
2281  *
2282  * @see rtnl_link_get_master()
2283  */
2284 void rtnl_link_set_master(struct rtnl_link *link, int ifindex)
2285 {
2286  link->l_master = ifindex;
2287  link->ce_mask |= LINK_ATTR_MASTER;
2288 }
2289 
2290 /**
2291  * Return master link of link object
2292  * @arg link Link object
2293  *
2294  * @see rtnl_link_set_master()
2295  * @return Interface index of master link or 0 if not specified
2296  */
2298 {
2299  return link->l_master;
2300 }
2301 
2302 /**
2303  * Set carrier of link object
2304  * @arg link Link object
2305  * @arg status New carrier status
2306  *
2307  * @see rtnl_link_get_carrier()
2308  */
2309 void rtnl_link_set_carrier(struct rtnl_link *link, uint8_t status)
2310 {
2311  link->l_carrier = status;
2312  link->ce_mask |= LINK_ATTR_CARRIER;
2313 }
2314 
2315 /**
2316  * Return carrier status of link object
2317  * @arg link Link object
2318  *
2319  * @see rtnl_link_set_master()
2320  * @return Carrier state.
2321  */
2322 uint8_t rtnl_link_get_carrier(struct rtnl_link *link)
2323 {
2324  return link->l_carrier;
2325 }
2326 
2327 /**
2328  * Return carrier on/off changes of link object
2329  * @arg link Link object
2330  * @arg carrier_changes Pointer to store number of carrier changes
2331  *
2332  * @return 0 on success, negative error number otherwise
2333  */
2334 int rtnl_link_get_carrier_changes(struct rtnl_link *link, uint32_t *carrier_changes)
2335 {
2336  if (!(link->ce_mask & LINK_ATTR_CARRIER_CHANGES))
2337  return -NLE_NOATTR;
2338 
2339  if (carrier_changes)
2340  *carrier_changes = link->l_carrier_changes;
2341 
2342  return 0;
2343 }
2344 
2345 /**
2346  * Set operational status of link object
2347  * @arg link Link object
2348  * @arg status New opertional status
2349  *
2350  * @route_doc{link_attr_operstate, Operational Status}}
2351  * @see rtnl_link_get_operstate()
2352  */
2353 void rtnl_link_set_operstate(struct rtnl_link *link, uint8_t status)
2354 {
2355  link->l_operstate = status;
2356  link->ce_mask |= LINK_ATTR_OPERSTATE;
2357 }
2358 
2359 /**
2360  * Return operational status of link object
2361  * @arg link Link object
2362  *
2363  * @route_doc{link_attr_operstate, Operational Status}
2364  * @see rtnl_link_set_operstate()
2365  * @return Opertional state or \c IF_OPER_UNKNOWN
2366  */
2367 uint8_t rtnl_link_get_operstate(struct rtnl_link *link)
2368 {
2369  return link->l_operstate;
2370 }
2371 
2372 /**
2373  * Set link mode of link object
2374  * @arg link Link object
2375  * @arg mode New link mode
2376  *
2377  * @route_doc{link_attr_mode, Mode}
2378  * @see rtnl_link_get_linkmode()
2379  */
2380 void rtnl_link_set_linkmode(struct rtnl_link *link, uint8_t mode)
2381 {
2382  link->l_linkmode = mode;
2383  link->ce_mask |= LINK_ATTR_LINKMODE;
2384 }
2385 
2386 /**
2387  * Return link mode of link object
2388  * @arg link Link object
2389  *
2390  * @route_doc{link_attr_mode, Mode}
2391  * @see rtnl_link_get_linkmode()
2392  * @return Link mode or \c IF_LINK_MODE_DEFAULT
2393  */
2394 uint8_t rtnl_link_get_linkmode(struct rtnl_link *link)
2395 {
2396  return link->l_linkmode;
2397 }
2398 
2399 /**
2400  * Return alias name of link object (SNMP IfAlias)
2401  * @arg link Link object
2402  *
2403  * @route_doc{link_attr_alias, Alias}
2404  * @see rtnl_link_set_ifalias()
2405  * @return Alias name or NULL if not set.
2406  */
2407 const char *rtnl_link_get_ifalias(struct rtnl_link *link)
2408 {
2409  return link->l_ifalias;
2410 }
2411 
2412 /**
2413  * Set alias name of link object (SNMP IfAlias)
2414  * @arg link Link object
2415  * @arg alias Alias name or NULL to unset
2416  *
2417  * Sets the alias name of the link to the specified name. The alias
2418  * name can be unset by specyfing NULL as the alias. The name will
2419  * be strdup()ed, so no need to provide a persistent character string.
2420  *
2421  * @route_doc{link_attr_alias, Alias}
2422  * @see rtnl_link_get_ifalias()
2423  */
2424 void rtnl_link_set_ifalias(struct rtnl_link *link, const char *alias)
2425 {
2426  free(link->l_ifalias);
2427 
2428  if (alias) {
2429  link->l_ifalias = strdup(alias);
2430  link->ce_mask |= LINK_ATTR_IFALIAS;
2431  } else {
2432  link->l_ifalias = NULL;
2433  link->ce_mask &= ~LINK_ATTR_IFALIAS;
2434  }
2435 }
2436 
2437 /**
2438  * Set queueing discipline name of link object
2439  * @arg link Link object
2440  * @arg name Name of queueing discipline
2441  *
2442  * @copydoc read_only_attribute
2443  *
2444  * For more information on how to modify the qdisc of a link, see section
2445  * @ref_route{route_tc, Traffic Control}.
2446  *
2447  * @route_doc{link_attr_qdisc, Queueing Discipline Name}
2448  * @see rtnl_link_get_qdisc()
2449  */
2450 void rtnl_link_set_qdisc(struct rtnl_link *link, const char *name)
2451 {
2452  strncpy(link->l_qdisc, name, sizeof(link->l_qdisc) - 1);
2453  link->ce_mask |= LINK_ATTR_QDISC;
2454 }
2455 
2456 /**
2457  * Return name of queueing discipline of link object
2458  * @arg link Link object
2459  *
2460  * @route_doc{link_attr_qdisc, Queueing Discipline Name}
2461  * @see rtnl_link_set_qdisc()
2462  * @return Name of qdisc or NULL if not specified.
2463  */
2464 char *rtnl_link_get_qdisc(struct rtnl_link *link)
2465 {
2466  return link->ce_mask & LINK_ATTR_QDISC ? link->l_qdisc : NULL;
2467 }
2468 
2469 
2470 /**
2471  * Return number of PCI virtual functions of link object
2472  * @arg link Link object
2473  * @arg num_vf Pointer to store number of VFs
2474  *
2475  * @return 0 on success or -NLE_OPNOTSUPP if not available
2476  */
2477 int rtnl_link_get_num_vf(struct rtnl_link *link, uint32_t *num_vf)
2478 {
2479  if (link->ce_mask & LINK_ATTR_NUM_VF) {
2480  *num_vf = link->l_num_vf;
2481  return 0;
2482  } else
2483  return -NLE_OPNOTSUPP;
2484 }
2485 
2486 /**
2487  * Return value of link statistics counter
2488  * @arg link Link object
2489  * @arg id Identifier of statistical counter
2490  *
2491  * @return Value of counter or 0 if not specified.
2492  */
2494 {
2495  if (id > RTNL_LINK_STATS_MAX)
2496  return 0;
2497 
2498  return link->l_stats[id];
2499 }
2500 
2501 /**
2502  * Set value of link statistics counter
2503  * @arg link Link object
2504  * @arg id Identifier of statistical counter
2505  * @arg value New value
2506  *
2507  * \note Changing the value of a statistical counter will not change the
2508  * value in the kernel.
2509  *
2510  * @return 0 on success or a negative error code
2511  */
2513  const uint64_t value)
2514 {
2515  if (id > RTNL_LINK_STATS_MAX)
2516  return -NLE_INVAL;
2517 
2518  link->l_stats[id] = value;
2519 
2520  return 0;
2521 }
2522 
2523 /**
2524  * Set type of link object
2525  * @arg link Link object
2526  * @arg type Name of link type
2527  *
2528  * Looks up the link type module and prepares the link to store type
2529  * specific attributes. If a type has been assigned already it will
2530  * be released with all link type specific attributes lost.
2531  *
2532  * @route_doc{link_modules, Link Modules}
2533  * @return 0 on success or a negative errror code.
2534  */
2535 int rtnl_link_set_type(struct rtnl_link *link, const char *type)
2536 {
2537  struct rtnl_link_info_ops *io;
2538  int err;
2539  char *kind;
2540 
2541  free(link->l_info_kind);
2542  link->ce_mask &= ~LINK_ATTR_LINKINFO;
2543  release_link_info(link);
2544 
2545  if (!type)
2546  return 0;
2547 
2548  kind = strdup(type);
2549  if (!kind)
2550  return -NLE_NOMEM;
2551 
2552  io = rtnl_link_info_ops_lookup(type);
2553  if (io) {
2554  if (io->io_alloc && (err = io->io_alloc(link)) < 0)
2555  goto errout;
2556 
2557  link->l_info_ops = io;
2558  }
2559 
2560  link->l_info_kind = kind;
2561  link->ce_mask |= LINK_ATTR_LINKINFO;
2562 
2563  return 0;
2564 
2565 errout:
2566  free(kind);
2567  return err;
2568 }
2569 
2570 /**
2571  * Return type of link
2572  * @arg link Link object
2573  *
2574  * @route_doc{link_modules, Link Modules}
2575  * @return Name of link type or NULL if not specified.
2576  */
2577 char *rtnl_link_get_type(struct rtnl_link *link)
2578 {
2579  return link->l_info_kind;
2580 }
2581 
2582 /**
2583  * Set link promiscuity count
2584  * @arg link Link object
2585  * @arg count New promiscuity count
2586  *
2587  * @copydoc read_only_attribute
2588  *
2589  * @see rtnl_link_get_promiscuity()
2590  */
2591 void rtnl_link_set_promiscuity(struct rtnl_link *link, uint32_t count)
2592 {
2593  link->l_promiscuity = count;
2594  link->ce_mask |= LINK_ATTR_PROMISCUITY;
2595 }
2596 
2597 /**
2598  * Return link promiscuity count
2599  * @arg link Link object
2600  *
2601  * @see rtnl_link_set_promiscuity()
2602  * @return Link promiscuity count or 0
2603  */
2604 uint32_t rtnl_link_get_promiscuity(struct rtnl_link *link)
2605 {
2606  return link->l_promiscuity;
2607 }
2608 
2609 /**
2610  * Set number of TX queues
2611  * @arg link Link object
2612  * @arg nqueues Number of queues
2613  *
2614  * Sets the number of TX queues of the link object. The value is considered
2615  * by the kernel when creating network devices that can be created via
2616  * netlink. The value will be passed on to alloc_netdev_mqs()
2617  *
2618  * Therefore use of rtnl_link_set_num_tx_queues() only makes sense in
2619  * combination with rtnl_link_add() or if the link object is used as a filter.
2620  *
2621  * @see rtnl_link_get_num_tx_queues()
2622  */
2623 void rtnl_link_set_num_tx_queues(struct rtnl_link *link, uint32_t nqueues)
2624 {
2625  link->l_num_tx_queues = nqueues;
2626  link->ce_mask |= LINK_ATTR_NUM_TX_QUEUES;
2627 }
2628 
2629 /**
2630  * Return number of TX queues
2631  * @arg link Link object
2632  *
2633  * @return Number of TX queues or 0
2634  */
2636 {
2637  return link->l_num_tx_queues;
2638 }
2639 
2640 /**
2641  * Set number of RX queues
2642  * @arg link Link object
2643  * @arg nqueues Number of queues
2644  *
2645  * Sets the number of RX queues of the link object. The value is considered
2646  * by the kernel when creating network devices that can be created via
2647  * netlink. The value will be passed on to alloc_netdev_mqs()
2648  *
2649  * Therefore use of rtnl_link_set_num_rx_queues() only makes sense in
2650  * combination with rtnl_link_add() or if the link object is used as a filter.
2651  *
2652  * @see rtnl_link_get_num_rx_queues()
2653  */
2654 void rtnl_link_set_num_rx_queues(struct rtnl_link *link, uint32_t nqueues)
2655 {
2656  link->l_num_rx_queues = nqueues;
2657  link->ce_mask |= LINK_ATTR_NUM_RX_QUEUES;
2658 }
2659 
2660 /**
2661  * Return number of RX queues
2662  * @arg link Link object
2663  *
2664  * @return Number of RX queues or 0
2665  */
2667 {
2668  return link->l_num_rx_queues;
2669 }
2670 
2671 /**
2672  * Return maximum number of segments for generic segmentation offload
2673  * @arg link Link object
2674  * @arg gso_max_segs Pointer to store maximum number GSO segments
2675  *
2676  * @return 0 on success, negative error number otherwise
2677  */
2678 int rtnl_link_get_gso_max_segs(struct rtnl_link *link, uint32_t *gso_max_segs)
2679 {
2680  if (!(link->ce_mask & LINK_ATTR_GSO_MAX_SEGS))
2681  return -NLE_NOATTR;
2682 
2683  if (gso_max_segs)
2684  *gso_max_segs = link->l_gso_max_segs;
2685 
2686  return 0;
2687 }
2688 
2689 /**
2690  * Return maximum size for generic segmentation offload
2691  * @arg link Link object
2692  * @arg gso_max_segs Pointer to store maximum GSO size
2693  *
2694  * @return 0 on success, negative error number otherwise
2695  */
2696 int rtnl_link_get_gso_max_size(struct rtnl_link *link, uint32_t *gso_max_size)
2697 {
2698  if (!(link->ce_mask & LINK_ATTR_GSO_MAX_SIZE))
2699  return -NLE_NOATTR;
2700 
2701  if (gso_max_size)
2702  *gso_max_size = link->l_gso_max_size;
2703 
2704  return 0;
2705 }
2706 
2707 /**
2708  * Return physical port id of link object
2709  * @arg link Link object
2710  *
2711  * @return Physical port id or NULL if not set.
2712  */
2713 struct nl_data *rtnl_link_get_phys_port_id(struct rtnl_link *link)
2714 {
2715  return link->l_phys_port_id;
2716 }
2717 
2718 /**
2719  * Return physical port name of link object
2720  * @arg link Link object
2721  *
2722  * @return Physical port name or NULL if not set.
2723  */
2725 {
2726  return link->l_phys_port_name;
2727 }
2728 
2729 /*
2730  * Return physical switch id of link object
2731  * @arg link Link object
2732  *
2733  * @return Physical switch id or NULL if not set.
2734  */
2735 struct nl_data *rtnl_link_get_phys_switch_id(struct rtnl_link *link)
2736 {
2737  return link->l_phys_switch_id;
2738 }
2739 
2740 void rtnl_link_set_ns_fd(struct rtnl_link *link, int fd)
2741 {
2742  link->l_ns_fd = fd;
2743  link->ce_mask |= LINK_ATTR_NS_FD;
2744 }
2745 
2746 int rtnl_link_get_ns_fd(struct rtnl_link *link)
2747 {
2748  return link->l_ns_fd;
2749 }
2750 
2751 void rtnl_link_set_ns_pid(struct rtnl_link *link, pid_t pid)
2752 {
2753  link->l_ns_pid = pid;
2754  link->ce_mask |= LINK_ATTR_NS_PID;
2755 }
2756 
2757 pid_t rtnl_link_get_ns_pid(struct rtnl_link *link)
2758 {
2759  return link->l_ns_pid;
2760 }
2761 
2762 /** @} */
2763 
2764 /**
2765  * @name Master/Slave
2766  * @{
2767  */
2768 
2769 /**
2770  * Enslave slave link to master link
2771  * @arg sock netlink socket
2772  * @arg master ifindex of master link
2773  * @arg slave ifindex of slave link
2774  *
2775  * This function is identical to rtnl_link_enslave() except that
2776  * it takes interface indices instead of rtnl_link objects.
2777  *
2778  * @see rtnl_link_enslave()
2779  *
2780  * @return 0 on success or a negative error code.
2781  */
2782 int rtnl_link_enslave_ifindex(struct nl_sock *sock, int master, int slave)
2783 {
2784  struct rtnl_link *link;
2785  int err;
2786 
2787  if (!(link = rtnl_link_alloc()))
2788  return -NLE_NOMEM;
2789 
2790  rtnl_link_set_ifindex(link, slave);
2791  rtnl_link_set_master(link, master);
2792 
2793  if ((err = rtnl_link_change(sock, link, link, 0)) < 0)
2794  goto errout;
2795 
2796  rtnl_link_put(link);
2797 
2798  /*
2799  * Due to the kernel not signaling whether this opertion is
2800  * supported or not, we will retrieve the attribute to see if the
2801  * request was successful. If the master assigned remains unchanged
2802  * we will return NLE_OPNOTSUPP to allow performing backwards
2803  * compatibility of some sort.
2804  */
2805  if ((err = rtnl_link_get_kernel(sock, slave, NULL, &link)) < 0)
2806  return err;
2807 
2808  if (rtnl_link_get_master(link) != master)
2809  err = -NLE_OPNOTSUPP;
2810 
2811 errout:
2812  rtnl_link_put(link);
2813 
2814  return err;
2815 }
2816 
2817 /**
2818  * Enslave slave link to master link
2819  * @arg sock netlink socket
2820  * @arg master master link
2821  * @arg slave slave link
2822  *
2823  * Constructs a RTM_NEWLINK or RTM_SETLINK message adding the slave to
2824  * the master and sends the request via the specified netlink socket.
2825  *
2826  * @note The feature of enslaving/releasing via netlink has only been added
2827  * recently to the kernel (Feb 2011). Also, the kernel does not signal
2828  * if the operation is not supported. Therefore this function will
2829  * verify if the master assignment has changed and will return
2830  * -NLE_OPNOTSUPP if it did not.
2831  *
2832  * @see rtnl_link_enslave_ifindex()
2833  * @see rtnl_link_release()
2834  *
2835  * @return 0 on success or a negative error code.
2836  */
2837 int rtnl_link_enslave(struct nl_sock *sock, struct rtnl_link *master,
2838  struct rtnl_link *slave)
2839 {
2840  return rtnl_link_enslave_ifindex(sock, rtnl_link_get_ifindex(master),
2841  rtnl_link_get_ifindex(slave));
2842 }
2843 
2844 /**
2845  * Release slave link from its master
2846  * @arg sock netlink socket
2847  * @arg slave slave link
2848  *
2849  * This function is identical to rtnl_link_release() except that
2850  * it takes an interface index instead of a rtnl_link object.
2851  *
2852  * @see rtnl_link_release()
2853  *
2854  * @return 0 on success or a negative error code.
2855  */
2856 int rtnl_link_release_ifindex(struct nl_sock *sock, int slave)
2857 {
2858  return rtnl_link_enslave_ifindex(sock, 0, slave);
2859 }
2860 
2861 /**
2862  * Release slave link from its master
2863  * @arg sock netlink socket
2864  * @arg slave slave link
2865  *
2866  * Constructs a RTM_NEWLINK or RTM_SETLINK message releasing the slave from
2867  * its master and sends the request via the specified netlink socket.
2868  *
2869  * @note The feature of enslaving/releasing via netlink has only been added
2870  * recently to the kernel (Feb 2011). Also, the kernel does not signal
2871  * if the operation is not supported. Therefore this function will
2872  * verify if the master assignment has changed and will return
2873  * -NLE_OPNOTSUPP if it did not.
2874  *
2875  * @see rtnl_link_release_ifindex()
2876  * @see rtnl_link_enslave()
2877  *
2878  * @return 0 on success or a negative error code.
2879  */
2880 int rtnl_link_release(struct nl_sock *sock, struct rtnl_link *slave)
2881 {
2882  return rtnl_link_release_ifindex(sock, rtnl_link_get_ifindex(slave));
2883 }
2884 
2885 /** @} */
2886 
2887 /**
2888  * @name Utilities
2889  * @{
2890  */
2891 
2892 static const struct trans_tbl link_flags[] = {
2893  __ADD(IFF_LOOPBACK, loopback),
2894  __ADD(IFF_BROADCAST, broadcast),
2895  __ADD(IFF_POINTOPOINT, pointopoint),
2896  __ADD(IFF_MULTICAST, multicast),
2897  __ADD(IFF_NOARP, noarp),
2898  __ADD(IFF_ALLMULTI, allmulti),
2899  __ADD(IFF_PROMISC, promisc),
2900  __ADD(IFF_MASTER, master),
2901  __ADD(IFF_SLAVE, slave),
2902  __ADD(IFF_DEBUG, debug),
2903  __ADD(IFF_DYNAMIC, dynamic),
2904  __ADD(IFF_AUTOMEDIA, automedia),
2905  __ADD(IFF_PORTSEL, portsel),
2906  __ADD(IFF_NOTRAILERS, notrailers),
2907  __ADD(IFF_UP, up),
2908  __ADD(IFF_RUNNING, running),
2909  __ADD(IFF_LOWER_UP, lowerup),
2910  __ADD(IFF_DORMANT, dormant),
2911  __ADD(IFF_ECHO, echo),
2912 };
2913 
2914 char *rtnl_link_flags2str(int flags, char *buf, size_t len)
2915 {
2916  return __flags2str(flags, buf, len, link_flags,
2917  ARRAY_SIZE(link_flags));
2918 }
2919 
2920 int rtnl_link_str2flags(const char *name)
2921 {
2922  return __str2flags(name, link_flags, ARRAY_SIZE(link_flags));
2923 }
2924 
2925 static const struct trans_tbl link_stats[] = {
2926  __ADD(RTNL_LINK_RX_PACKETS, rx_packets),
2927  __ADD(RTNL_LINK_TX_PACKETS, tx_packets),
2928  __ADD(RTNL_LINK_RX_BYTES, rx_bytes),
2929  __ADD(RTNL_LINK_TX_BYTES, tx_bytes),
2930  __ADD(RTNL_LINK_RX_ERRORS, rx_errors),
2931  __ADD(RTNL_LINK_TX_ERRORS, tx_errors),
2932  __ADD(RTNL_LINK_RX_DROPPED, rx_dropped),
2933  __ADD(RTNL_LINK_TX_DROPPED, tx_dropped),
2934  __ADD(RTNL_LINK_RX_COMPRESSED, rx_compressed),
2935  __ADD(RTNL_LINK_TX_COMPRESSED, tx_compressed),
2936  __ADD(RTNL_LINK_RX_FIFO_ERR, rx_fifo_err),
2937  __ADD(RTNL_LINK_TX_FIFO_ERR, tx_fifo_err),
2938  __ADD(RTNL_LINK_RX_LEN_ERR, rx_len_err),
2939  __ADD(RTNL_LINK_RX_OVER_ERR, rx_over_err),
2940  __ADD(RTNL_LINK_RX_CRC_ERR, rx_crc_err),
2941  __ADD(RTNL_LINK_RX_FRAME_ERR, rx_frame_err),
2942  __ADD(RTNL_LINK_RX_MISSED_ERR, rx_missed_err),
2943  __ADD(RTNL_LINK_TX_ABORT_ERR, tx_abort_err),
2944  __ADD(RTNL_LINK_TX_CARRIER_ERR, tx_carrier_err),
2945  __ADD(RTNL_LINK_TX_HBEAT_ERR, tx_hbeat_err),
2946  __ADD(RTNL_LINK_TX_WIN_ERR, tx_win_err),
2947  __ADD(RTNL_LINK_COLLISIONS, collisions),
2948  __ADD(RTNL_LINK_MULTICAST, multicast),
2949  __ADD(RTNL_LINK_IP6_INPKTS, Ip6InReceives),
2950  __ADD(RTNL_LINK_IP6_INHDRERRORS, Ip6InHdrErrors),
2951  __ADD(RTNL_LINK_IP6_INTOOBIGERRORS, Ip6InTooBigErrors),
2952  __ADD(RTNL_LINK_IP6_INNOROUTES, Ip6InNoRoutes),
2953  __ADD(RTNL_LINK_IP6_INADDRERRORS, Ip6InAddrErrors),
2954  __ADD(RTNL_LINK_IP6_INUNKNOWNPROTOS, Ip6InUnknownProtos),
2955  __ADD(RTNL_LINK_IP6_INTRUNCATEDPKTS, Ip6InTruncatedPkts),
2956  __ADD(RTNL_LINK_IP6_INDISCARDS, Ip6InDiscards),
2957  __ADD(RTNL_LINK_IP6_INDELIVERS, Ip6InDelivers),
2958  __ADD(RTNL_LINK_IP6_OUTFORWDATAGRAMS, Ip6OutForwDatagrams),
2959  __ADD(RTNL_LINK_IP6_OUTPKTS, Ip6OutRequests),
2960  __ADD(RTNL_LINK_IP6_OUTDISCARDS, Ip6OutDiscards),
2961  __ADD(RTNL_LINK_IP6_OUTNOROUTES, Ip6OutNoRoutes),
2962  __ADD(RTNL_LINK_IP6_REASMTIMEOUT, Ip6ReasmTimeout),
2963  __ADD(RTNL_LINK_IP6_REASMREQDS, Ip6ReasmReqds),
2964  __ADD(RTNL_LINK_IP6_REASMOKS, Ip6ReasmOKs),
2965  __ADD(RTNL_LINK_IP6_REASMFAILS, Ip6ReasmFails),
2966  __ADD(RTNL_LINK_IP6_FRAGOKS, Ip6FragOKs),
2967  __ADD(RTNL_LINK_IP6_FRAGFAILS, Ip6FragFails),
2968  __ADD(RTNL_LINK_IP6_FRAGCREATES, Ip6FragCreates),
2969  __ADD(RTNL_LINK_IP6_INMCASTPKTS, Ip6InMcastPkts),
2970  __ADD(RTNL_LINK_IP6_OUTMCASTPKTS, Ip6OutMcastPkts),
2971  __ADD(RTNL_LINK_IP6_INBCASTPKTS, Ip6InBcastPkts),
2972  __ADD(RTNL_LINK_IP6_OUTBCASTPKTS, Ip6OutBcastPkts),
2973  __ADD(RTNL_LINK_IP6_INOCTETS, Ip6InOctets),
2974  __ADD(RTNL_LINK_IP6_OUTOCTETS, Ip6OutOctets),
2975  __ADD(RTNL_LINK_IP6_INMCASTOCTETS, Ip6InMcastOctets),
2976  __ADD(RTNL_LINK_IP6_OUTMCASTOCTETS, Ip6OutMcastOctets),
2977  __ADD(RTNL_LINK_IP6_INBCASTOCTETS, Ip6InBcastOctets),
2978  __ADD(RTNL_LINK_IP6_OUTBCASTOCTETS, Ip6OutBcastOctets),
2979  __ADD(RTNL_LINK_ICMP6_INMSGS, ICMP6_InMsgs),
2980  __ADD(RTNL_LINK_ICMP6_INERRORS, ICMP6_InErrors),
2981  __ADD(RTNL_LINK_ICMP6_OUTMSGS, ICMP6_OutMsgs),
2982  __ADD(RTNL_LINK_ICMP6_OUTERRORS, ICMP6_OutErrors),
2983  __ADD(RTNL_LINK_ICMP6_CSUMERRORS, ICMP6_InCsumErrors),
2984  __ADD(RTNL_LINK_IP6_CSUMERRORS, Ip6_InCsumErrors),
2985  __ADD(RTNL_LINK_IP6_NOECTPKTS, Ip6_InNoECTPkts),
2986  __ADD(RTNL_LINK_IP6_ECT1PKTS, Ip6_InECT1Pkts),
2987  __ADD(RTNL_LINK_IP6_ECT0PKTS, Ip6_InECT0Pkts),
2988  __ADD(RTNL_LINK_IP6_CEPKTS, Ip6_InCEPkts),
2989  __ADD(RTNL_LINK_RX_NOHANDLER, rx_nohandler),
2990 };
2991 
2992 char *rtnl_link_stat2str(int st, char *buf, size_t len)
2993 {
2994  return __type2str(st, buf, len, link_stats, ARRAY_SIZE(link_stats));
2995 }
2996 
2997 int rtnl_link_str2stat(const char *name)
2998 {
2999  return __str2type(name, link_stats, ARRAY_SIZE(link_stats));
3000 }
3001 
3002 static const struct trans_tbl link_operstates[] = {
3003  __ADD(IF_OPER_UNKNOWN, unknown),
3004  __ADD(IF_OPER_NOTPRESENT, notpresent),
3005  __ADD(IF_OPER_DOWN, down),
3006  __ADD(IF_OPER_LOWERLAYERDOWN, lowerlayerdown),
3007  __ADD(IF_OPER_TESTING, testing),
3008  __ADD(IF_OPER_DORMANT, dormant),
3009  __ADD(IF_OPER_UP, up),
3010 };
3011 
3012 char *rtnl_link_operstate2str(uint8_t st, char *buf, size_t len)
3013 {
3014  return __type2str(st, buf, len, link_operstates,
3015  ARRAY_SIZE(link_operstates));
3016 }
3017 
3018 int rtnl_link_str2operstate(const char *name)
3019 {
3020  return __str2type(name, link_operstates,
3021  ARRAY_SIZE(link_operstates));
3022 }
3023 
3024 static const struct trans_tbl link_modes[] = {
3025  __ADD(IF_LINK_MODE_DEFAULT, default),
3026  __ADD(IF_LINK_MODE_DORMANT, dormant),
3027 };
3028 
3029 static const struct trans_tbl carrier_states[] = {
3030  __ADD(IF_CARRIER_DOWN, down),
3031  __ADD(IF_CARRIER_UP, up),
3032 };
3033 
3034 char *rtnl_link_mode2str(uint8_t st, char *buf, size_t len)
3035 {
3036  return __type2str(st, buf, len, link_modes, ARRAY_SIZE(link_modes));
3037 }
3038 
3039 int rtnl_link_str2mode(const char *name)
3040 {
3041  return __str2type(name, link_modes, ARRAY_SIZE(link_modes));
3042 }
3043 
3044 char *rtnl_link_carrier2str(uint8_t st, char *buf, size_t len)
3045 {
3046  return __type2str(st, buf, len, carrier_states,
3047  ARRAY_SIZE(carrier_states));
3048 }
3049 
3050 int rtnl_link_str2carrier(const char *name)
3051 {
3052  return __str2type(name, carrier_states, ARRAY_SIZE(carrier_states));
3053 }
3054 
3055 int rtnl_link_has_vf_list(struct rtnl_link *link) {
3056  if (link->ce_mask & LINK_ATTR_VF_LIST)
3057  return 1;
3058  else
3059  return 0;
3060 }
3061 
3062 void rtnl_link_set_vf_list(struct rtnl_link *link) {
3063  int err;
3064 
3065  if (!(err = rtnl_link_has_vf_list(link)))
3066  link->ce_mask |= LINK_ATTR_VF_LIST;
3067 
3068  return;
3069 }
3070 
3071 void rtnl_link_unset_vf_list(struct rtnl_link *link) {
3072  int err;
3073 
3074  if ((err = rtnl_link_has_vf_list(link)))
3075  link->ce_mask &= ~LINK_ATTR_VF_LIST;
3076 
3077  return;
3078 }
3079 
3080 /** @} */
3081 
3082 /**
3083  * @name Deprecated Functions
3084  */
3085 
3086 /**
3087  * @deprecated Use of this function is deprecated, use rtnl_link_set_type()
3088  */
3089 int rtnl_link_set_info_type(struct rtnl_link *link, const char *type)
3090 {
3091  return rtnl_link_set_type(link, type);
3092 }
3093 
3094 /**
3095  * @deprecated Use of this function is deprecated, use rtnl_link_get_type()
3096  */
3098 {
3099  return rtnl_link_get_type(link);
3100 }
3101 
3102 /**
3103  * @deprecated The weight attribute is unused and obsoleted in all recent kernels
3104  */
3105 void rtnl_link_set_weight(struct rtnl_link *link, unsigned int weight)
3106 {
3107  link->l_weight = weight;
3108  link->ce_mask |= LINK_ATTR_WEIGHT;
3109 }
3110 
3111 /**
3112  * @deprecated The weight attribute is unused and obsoleted in all recent kernels
3113  */
3114 unsigned int rtnl_link_get_weight(struct rtnl_link *link)
3115 {
3116  return link->l_weight;
3117 }
3118 
3119 /** @} */
3120 
3121 static struct nl_object_ops link_obj_ops = {
3122  .oo_name = "route/link",
3123  .oo_size = sizeof(struct rtnl_link),
3124  .oo_free_data = link_free_data,
3125  .oo_clone = link_clone,
3126  .oo_dump = {
3127  [NL_DUMP_LINE] = link_dump_line,
3128  [NL_DUMP_DETAILS] = link_dump_details,
3129  [NL_DUMP_STATS] = link_dump_stats,
3130  },
3131  .oo_compare = link_compare,
3132  .oo_keygen = link_keygen,
3133  .oo_attrs2str = link_attrs2str,
3134  .oo_id_attrs = LINK_ATTR_IFINDEX | LINK_ATTR_FAMILY,
3135 };
3136 
3137 static struct nl_af_group link_groups[] = {
3138  { AF_UNSPEC, RTNLGRP_LINK },
3139  { AF_BRIDGE, RTNLGRP_LINK },
3140  { END_OF_GROUP_LIST },
3141 };
3142 
3143 static struct nl_cache_ops rtnl_link_ops = {
3144  .co_name = "route/link",
3145  .co_hdrsize = sizeof(struct ifinfomsg),
3146  .co_msgtypes = {
3147  { RTM_NEWLINK, NL_ACT_NEW, "new" },
3148  { RTM_DELLINK, NL_ACT_DEL, "del" },
3149  { RTM_GETLINK, NL_ACT_GET, "get" },
3150  { RTM_SETLINK, NL_ACT_CHANGE, "set" },
3151  END_OF_MSGTYPES_LIST,
3152  },
3153  .co_protocol = NETLINK_ROUTE,
3154  .co_groups = link_groups,
3155  .co_request_update = link_request_update,
3156  .co_msg_parser = link_msg_parser,
3157  .co_obj_ops = &link_obj_ops,
3158 };
3159 
3160 static void __init link_init(void)
3161 {
3162  nl_cache_mngt_register(&rtnl_link_ops);
3163 }
3164 
3165 static void __exit link_exit(void)
3166 {
3167  nl_cache_mngt_unregister(&rtnl_link_ops);
3168 }
3169 
3170 /** @} */
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition: nl.c:1247
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
Definition: addr.c:493
Dump object briefly on one line.
Definition: types.h:22
8 bit integer
Definition: attr.h:41
int32_t nla_get_s32(const struct nlattr *nla)
Return payload of 32 bit signed integer attribute.
Definition: attr.c:681
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition: msg.c:562
int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
Definition: addr.c:585
int nl_addr_guess_family(const struct nl_addr *addr)
Guess address family of abstract address based on address size.
Definition: addr.c:701
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
Definition: msg.c:106
#define NLA_PUT_ADDR(msg, attrtype, addr)
Add address attribute to netlink message.
Definition: attr.h:288
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
Definition: object.c:54
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Definition: cache_mngt.c:287
Attribute validation policy.
Definition: attr.h:69
uint8_t nla_get_u8(const struct nlattr *nla)
Return value of 8 bit integer attribute.
Definition: attr.c:606
Unspecified type, binary data chunk.
Definition: attr.h:40
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
Definition: cache_mngt.c:430
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition: object.c:204
int nl_send_sync(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message and wait for ACK or error message.
Definition: nl.c:548
char * nla_get_string(const struct nlattr *nla)
Return payload of string attribute.
Definition: attr.c:797
int nl_pickup_keep_syserr(struct nl_sock *sk, int(*parser)(struct nl_cache_ops *, struct sockaddr_nl *, struct nlmsghdr *, struct nl_parser_param *), struct nl_object **result, int *syserror)
Pickup netlink answer, parse is and return object with preserving system error.
Definition: nl.c:1195
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition: attr.c:706
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
parse attributes of a netlink message
Definition: msg.c:214
struct nl_data * nl_data_alloc_attr(const struct nlattr *nla)
Allocate abstract data object based on netlink attribute.
Definition: data.c:84
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
Definition: addr.c:523
void nl_addr_set_family(struct nl_addr *addr, int family)
Set address family.
Definition: addr.c:872
struct nl_addr * nl_addr_alloc_attr(const struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
Definition: addr.c:262
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
Definition: attr.h:199
NUL terminated character string.
Definition: attr.h:45
Dump all attributes but no statistics.
Definition: types.h:23
int nl_addr_iszero(const struct nl_addr *addr)
Returns true if the address consists of all zeros.
Definition: addr.c:642
void nl_cache_free(struct nl_cache *cache)
Free a cache.
Definition: cache.c:408
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
Definition: attr.c:924
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
Definition: cache_mngt.c:252
double nl_cancel_down_bytes(unsigned long long l, char **unit)
Cancel down a byte counter.
Definition: utils.c:169
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
Definition: attr.c:353
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
Definition: attr.c:999
int nla_type(const struct nlattr *nla)
Return type of the attribute.
Definition: attr.c:109
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
Definition: attr.c:120
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition: attr.h:235
int nla_len(const struct nlattr *nla)
Return length of the payload .
Definition: attr.c:131
struct nl_data * nl_data_clone(const struct nl_data *src)
Clone an abstract data object.
Definition: data.c:95
#define nla_for_each_nested(pos, nla, rem)
Iterate over a stream of nested attributes.
Definition: attr.h:329
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition: msg.c:446
int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
Definition: cache.c:1040
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition: object.c:215
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
Definition: attr.h:262
Nested attributes.
Definition: attr.h:48
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
Definition: addr.c:539
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition: attr.h:71
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition: msg.c:347
32 bit integer
Definition: attr.h:43
Dumping parameters.
Definition: types.h:33
void nl_cache_set_flags(struct nl_cache *cache, unsigned int flags)
Set cache flags.
Definition: cache.c:613
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:961
int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message.
Definition: nl.c:516
int nla_put(struct nl_msg *msg, int attrtype, int datalen, const void *data)
Add a unspecific attribute to netlink message.
Definition: attr.c:500
#define NLA_PUT_S32(msg, attrtype, value)
Add 32 bit signed integer attribute to netlink message.
Definition: attr.h:226
Dump all attributes including statistics.
Definition: types.h:24
size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
Copy string attribute payload to a buffer.
Definition: attr.c:378
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate new cache.
Definition: cache.c:183
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
Definition: attr.c:902
void nl_data_free(struct nl_data *data)
Free an abstract data object.
Definition: data.c:134
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
Definition: addr.c:991