libnl  3.2.27
inet6.c
1 /*
2  * lib/route/link/inet6.c AF_INET6 link operations
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) 2010 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink-private/netlink.h>
13 #include <netlink/netlink.h>
14 #include <netlink/attr.h>
15 #include <netlink/route/rtnl.h>
16 #include <netlink-private/route/link/api.h>
17 
18 #define I6_ADDR_GEN_MODE_UNKNOWN UINT8_MAX
19 
20 struct inet6_data
21 {
22  uint32_t i6_flags;
23  struct ifla_cacheinfo i6_cacheinfo;
24  uint32_t i6_conf[DEVCONF_MAX];
25  struct in6_addr i6_token;
26  uint8_t i6_addr_gen_mode;
27 };
28 
29 static void *inet6_alloc(struct rtnl_link *link)
30 {
31  struct inet6_data *i6;
32 
33  i6 = calloc(1, sizeof(struct inet6_data));
34  if (i6)
35  i6->i6_addr_gen_mode = I6_ADDR_GEN_MODE_UNKNOWN;
36 
37  return i6;
38 }
39 
40 static void *inet6_clone(struct rtnl_link *link, void *data)
41 {
42  struct inet6_data *i6;
43 
44  if ((i6 = inet6_alloc(link)))
45  memcpy(i6, data, sizeof(*i6));
46 
47  return i6;
48 }
49 
50 static void inet6_free(struct rtnl_link *link, void *data)
51 {
52  free(data);
53 }
54 
55 static struct nla_policy inet6_policy[IFLA_INET6_MAX+1] = {
56  [IFLA_INET6_FLAGS] = { .type = NLA_U32 },
57  [IFLA_INET6_CACHEINFO] = { .minlen = sizeof(struct ifla_cacheinfo) },
58  [IFLA_INET6_CONF] = { .minlen = 4 },
59  [IFLA_INET6_STATS] = { .minlen = 8 },
60  [IFLA_INET6_ICMP6STATS] = { .minlen = 8 },
61  [IFLA_INET6_TOKEN] = { .minlen = sizeof(struct in6_addr) },
62  [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
63 };
64 
65 static const uint8_t map_stat_id_from_IPSTATS_MIB_v1[__IPSTATS_MIB_MAX] = {
66  /* 14a196807482e6fc74f15fc03176d5c08880588f^:include/linux/snmp.h
67  * version before the API change in commit 14a196807482e6fc74f15fc03176d5c08880588f.
68  * This version was valid since commit edf391ff17232f097d72441c9ad467bcb3b5db18, which
69  * predates support for parsing IFLA_PROTINFO in libnl3. Such an even older meaning of
70  * the flags is not supported in libnl3. */
71  [ 1] = RTNL_LINK_IP6_INPKTS, /* IPSTATS_MIB_INPKTS */
72  [ 2] = RTNL_LINK_IP6_INHDRERRORS, /* IPSTATS_MIB_INHDRERRORS */
73  [ 3] = RTNL_LINK_IP6_INTOOBIGERRORS, /* IPSTATS_MIB_INTOOBIGERRORS */
74  [ 4] = RTNL_LINK_IP6_INNOROUTES, /* IPSTATS_MIB_INNOROUTES */
75  [ 5] = RTNL_LINK_IP6_INADDRERRORS, /* IPSTATS_MIB_INADDRERRORS */
76  [ 6] = RTNL_LINK_IP6_INUNKNOWNPROTOS, /* IPSTATS_MIB_INUNKNOWNPROTOS */
77  [ 7] = RTNL_LINK_IP6_INTRUNCATEDPKTS, /* IPSTATS_MIB_INTRUNCATEDPKTS */
78  [ 8] = RTNL_LINK_IP6_INDISCARDS, /* IPSTATS_MIB_INDISCARDS */
79  [ 9] = RTNL_LINK_IP6_INDELIVERS, /* IPSTATS_MIB_INDELIVERS */
80  [10] = RTNL_LINK_IP6_OUTFORWDATAGRAMS, /* IPSTATS_MIB_OUTFORWDATAGRAMS */
81  [11] = RTNL_LINK_IP6_OUTPKTS, /* IPSTATS_MIB_OUTPKTS */
82  [12] = RTNL_LINK_IP6_OUTDISCARDS, /* IPSTATS_MIB_OUTDISCARDS */
83  [13] = RTNL_LINK_IP6_OUTNOROUTES, /* IPSTATS_MIB_OUTNOROUTES */
84  [14] = RTNL_LINK_IP6_REASMTIMEOUT, /* IPSTATS_MIB_REASMTIMEOUT */
85  [15] = RTNL_LINK_IP6_REASMREQDS, /* IPSTATS_MIB_REASMREQDS */
86  [16] = RTNL_LINK_IP6_REASMOKS, /* IPSTATS_MIB_REASMOKS */
87  [17] = RTNL_LINK_IP6_REASMFAILS, /* IPSTATS_MIB_REASMFAILS */
88  [18] = RTNL_LINK_IP6_FRAGOKS, /* IPSTATS_MIB_FRAGOKS */
89  [19] = RTNL_LINK_IP6_FRAGFAILS, /* IPSTATS_MIB_FRAGFAILS */
90  [20] = RTNL_LINK_IP6_FRAGCREATES, /* IPSTATS_MIB_FRAGCREATES */
91  [21] = RTNL_LINK_IP6_INMCASTPKTS, /* IPSTATS_MIB_INMCASTPKTS */
92  [22] = RTNL_LINK_IP6_OUTMCASTPKTS, /* IPSTATS_MIB_OUTMCASTPKTS */
93  [23] = RTNL_LINK_IP6_INBCASTPKTS, /* IPSTATS_MIB_INBCASTPKTS */
94  [24] = RTNL_LINK_IP6_OUTBCASTPKTS, /* IPSTATS_MIB_OUTBCASTPKTS */
95  [25] = RTNL_LINK_IP6_INOCTETS, /* IPSTATS_MIB_INOCTETS */
96  [26] = RTNL_LINK_IP6_OUTOCTETS, /* IPSTATS_MIB_OUTOCTETS */
97  [27] = RTNL_LINK_IP6_INMCASTOCTETS, /* IPSTATS_MIB_INMCASTOCTETS */
98  [28] = RTNL_LINK_IP6_OUTMCASTOCTETS, /* IPSTATS_MIB_OUTMCASTOCTETS */
99  [29] = RTNL_LINK_IP6_INBCASTOCTETS, /* IPSTATS_MIB_INBCASTOCTETS */
100  [30] = RTNL_LINK_IP6_OUTBCASTOCTETS, /* IPSTATS_MIB_OUTBCASTOCTETS */
101 };
102 
103 static const uint8_t map_stat_id_from_IPSTATS_MIB_v2[__IPSTATS_MIB_MAX] = {
104  /* d8ec26d7f8287f5788a494f56e8814210f0e64be:include/uapi/linux/snmp.h
105  * version since the API change in commit 14a196807482e6fc74f15fc03176d5c08880588f */
106  [ 1] = RTNL_LINK_IP6_INPKTS, /* IPSTATS_MIB_INPKTS */
107  [ 2] = RTNL_LINK_IP6_INOCTETS, /* IPSTATS_MIB_INOCTETS */
108  [ 3] = RTNL_LINK_IP6_INDELIVERS, /* IPSTATS_MIB_INDELIVERS */
109  [ 4] = RTNL_LINK_IP6_OUTFORWDATAGRAMS, /* IPSTATS_MIB_OUTFORWDATAGRAMS */
110  [ 5] = RTNL_LINK_IP6_OUTPKTS, /* IPSTATS_MIB_OUTPKTS */
111  [ 6] = RTNL_LINK_IP6_OUTOCTETS, /* IPSTATS_MIB_OUTOCTETS */
112  [ 7] = RTNL_LINK_IP6_INHDRERRORS, /* IPSTATS_MIB_INHDRERRORS */
113  [ 8] = RTNL_LINK_IP6_INTOOBIGERRORS, /* IPSTATS_MIB_INTOOBIGERRORS */
114  [ 9] = RTNL_LINK_IP6_INNOROUTES, /* IPSTATS_MIB_INNOROUTES */
115  [10] = RTNL_LINK_IP6_INADDRERRORS, /* IPSTATS_MIB_INADDRERRORS */
116  [11] = RTNL_LINK_IP6_INUNKNOWNPROTOS, /* IPSTATS_MIB_INUNKNOWNPROTOS */
117  [12] = RTNL_LINK_IP6_INTRUNCATEDPKTS, /* IPSTATS_MIB_INTRUNCATEDPKTS */
118  [13] = RTNL_LINK_IP6_INDISCARDS, /* IPSTATS_MIB_INDISCARDS */
119  [14] = RTNL_LINK_IP6_OUTDISCARDS, /* IPSTATS_MIB_OUTDISCARDS */
120  [15] = RTNL_LINK_IP6_OUTNOROUTES, /* IPSTATS_MIB_OUTNOROUTES */
121  [16] = RTNL_LINK_IP6_REASMTIMEOUT, /* IPSTATS_MIB_REASMTIMEOUT */
122  [17] = RTNL_LINK_IP6_REASMREQDS, /* IPSTATS_MIB_REASMREQDS */
123  [18] = RTNL_LINK_IP6_REASMOKS, /* IPSTATS_MIB_REASMOKS */
124  [19] = RTNL_LINK_IP6_REASMFAILS, /* IPSTATS_MIB_REASMFAILS */
125  [20] = RTNL_LINK_IP6_FRAGOKS, /* IPSTATS_MIB_FRAGOKS */
126  [21] = RTNL_LINK_IP6_FRAGFAILS, /* IPSTATS_MIB_FRAGFAILS */
127  [22] = RTNL_LINK_IP6_FRAGCREATES, /* IPSTATS_MIB_FRAGCREATES */
128  [23] = RTNL_LINK_IP6_INMCASTPKTS, /* IPSTATS_MIB_INMCASTPKTS */
129  [24] = RTNL_LINK_IP6_OUTMCASTPKTS, /* IPSTATS_MIB_OUTMCASTPKTS */
130  [25] = RTNL_LINK_IP6_INBCASTPKTS, /* IPSTATS_MIB_INBCASTPKTS */
131  [26] = RTNL_LINK_IP6_OUTBCASTPKTS, /* IPSTATS_MIB_OUTBCASTPKTS */
132  [27] = RTNL_LINK_IP6_INMCASTOCTETS, /* IPSTATS_MIB_INMCASTOCTETS */
133  [28] = RTNL_LINK_IP6_OUTMCASTOCTETS, /* IPSTATS_MIB_OUTMCASTOCTETS */
134  [29] = RTNL_LINK_IP6_INBCASTOCTETS, /* IPSTATS_MIB_INBCASTOCTETS */
135  [30] = RTNL_LINK_IP6_OUTBCASTOCTETS, /* IPSTATS_MIB_OUTBCASTOCTETS */
136  [31] = RTNL_LINK_IP6_CSUMERRORS, /* IPSTATS_MIB_CSUMERRORS */
137  [32] = RTNL_LINK_IP6_NOECTPKTS, /* IPSTATS_MIB_NOECTPKTS */
138  [33] = RTNL_LINK_IP6_ECT1PKTS, /* IPSTATS_MIB_ECT1PKTS */
139  [34] = RTNL_LINK_IP6_ECT0PKTS, /* IPSTATS_MIB_ECT0PKTS */
140  [35] = RTNL_LINK_IP6_CEPKTS, /* IPSTATS_MIB_CEPKTS */
141 };
142 
143 static int inet6_parse_protinfo(struct rtnl_link *link, struct nlattr *attr,
144  void *data)
145 {
146  struct inet6_data *i6 = data;
147  struct nlattr *tb[IFLA_INET6_MAX+1];
148  int err;
149 
150  err = nla_parse_nested(tb, IFLA_INET6_MAX, attr, inet6_policy);
151  if (err < 0)
152  return err;
153  if (tb[IFLA_INET6_CONF] && nla_len(tb[IFLA_INET6_CONF]) % 4)
154  return -EINVAL;
155  if (tb[IFLA_INET6_STATS] && nla_len(tb[IFLA_INET6_STATS]) % 8)
156  return -EINVAL;
157  if (tb[IFLA_INET6_ICMP6STATS] && nla_len(tb[IFLA_INET6_ICMP6STATS]) % 8)
158  return -EINVAL;
159 
160  if (tb[IFLA_INET6_FLAGS])
161  i6->i6_flags = nla_get_u32(tb[IFLA_INET6_FLAGS]);
162 
163  if (tb[IFLA_INET6_CACHEINFO])
164  nla_memcpy(&i6->i6_cacheinfo, tb[IFLA_INET6_CACHEINFO],
165  sizeof(i6->i6_cacheinfo));
166 
167  if (tb[IFLA_INET6_CONF])
168  nla_memcpy(&i6->i6_conf, tb[IFLA_INET6_CONF],
169  sizeof(i6->i6_conf));
170 
171  if (tb[IFLA_INET6_TOKEN])
172  nla_memcpy(&i6->i6_token, tb[IFLA_INET6_TOKEN],
173  sizeof(struct in6_addr));
174 
175  if (tb[IFLA_INET6_ADDR_GEN_MODE])
176  i6->i6_addr_gen_mode = nla_get_u8 (tb[IFLA_INET6_ADDR_GEN_MODE]);
177 
178  /*
179  * Due to 32bit data alignment, these addresses must be copied to an
180  * aligned location prior to access.
181  */
182  if (tb[IFLA_INET6_STATS]) {
183  unsigned char *cnt = nla_data(tb[IFLA_INET6_STATS]);
184  uint64_t stat;
185  int i;
186  int len = nla_len(tb[IFLA_INET6_STATS]) / 8;
187  const uint8_t *map_stat_id = map_stat_id_from_IPSTATS_MIB_v2;
188 
189  if (len < 32 ||
190  (tb[IFLA_INET6_ICMP6STATS] && nla_len(tb[IFLA_INET6_ICMP6STATS]) < 6)) {
191  /* kernel commit 14a196807482e6fc74f15fc03176d5c08880588f reordered the values.
192  * The later commit 6a5dc9e598fe90160fee7de098fa319665f5253e added values
193  * IPSTATS_MIB_CSUMERRORS/ICMP6_MIB_CSUMERRORS. If the netlink is shorter
194  * then this, assume that the kernel uses the previous meaning of the
195  * enumeration. */
196  map_stat_id = map_stat_id_from_IPSTATS_MIB_v1;
197  }
198 
199  len = min_t(int, __IPSTATS_MIB_MAX, len);
200  for (i = 1; i < len; i++) {
201  memcpy(&stat, &cnt[i * sizeof(stat)], sizeof(stat));
202  rtnl_link_set_stat(link, map_stat_id[i], stat);
203  }
204  }
205 
206  if (tb[IFLA_INET6_ICMP6STATS]) {
207  unsigned char *cnt = nla_data(tb[IFLA_INET6_ICMP6STATS]);
208  uint64_t stat;
209  int i;
210  int len = min_t(int, __ICMP6_MIB_MAX, nla_len(tb[IFLA_INET6_ICMP6STATS]) / 8);
211 
212  for (i = 1; i < len; i++) {
213  memcpy(&stat, &cnt[i * sizeof(stat)], sizeof(stat));
215  stat);
216  }
217  }
218 
219  return 0;
220 }
221 
222 static int inet6_fill_af(struct rtnl_link *link, struct nl_msg *msg, void *data)
223 {
224  struct inet6_data *id = data;
225 
226  if (id->i6_addr_gen_mode != I6_ADDR_GEN_MODE_UNKNOWN)
227  NLA_PUT_U8(msg, IFLA_INET6_ADDR_GEN_MODE, id->i6_addr_gen_mode);
228 
229  return 0;
230 
231 nla_put_failure:
232  return -NLE_MSGSIZE;
233 }
234 
235 /* These live in include/net/if_inet6.h and should be moved to include/linux */
236 #define IF_RA_OTHERCONF 0x80
237 #define IF_RA_MANAGED 0x40
238 #define IF_RA_RCVD 0x20
239 #define IF_RS_SENT 0x10
240 #define IF_READY 0x80000000
241 
242 static const struct trans_tbl inet6_flags[] = {
243  __ADD(IF_RA_OTHERCONF, ra_otherconf),
244  __ADD(IF_RA_MANAGED, ra_managed),
245  __ADD(IF_RA_RCVD, ra_rcvd),
246  __ADD(IF_RS_SENT, rs_sent),
247  __ADD(IF_READY, ready),
248 };
249 
250 static char *inet6_flags2str(int flags, char *buf, size_t len)
251 {
252  return __flags2str(flags, buf, len, inet6_flags,
253  ARRAY_SIZE(inet6_flags));
254 }
255 
256 static const struct trans_tbl inet6_devconf[] = {
257  __ADD(DEVCONF_FORWARDING, forwarding),
258  __ADD(DEVCONF_HOPLIMIT, hoplimit),
259  __ADD(DEVCONF_MTU6, mtu6),
260  __ADD(DEVCONF_ACCEPT_RA, accept_ra),
261  __ADD(DEVCONF_ACCEPT_REDIRECTS, accept_redirects),
262  __ADD(DEVCONF_AUTOCONF, autoconf),
263  __ADD(DEVCONF_DAD_TRANSMITS, dad_transmits),
264  __ADD(DEVCONF_RTR_SOLICITS, rtr_solicits),
265  __ADD(DEVCONF_RTR_SOLICIT_INTERVAL, rtr_solicit_interval),
266  __ADD(DEVCONF_RTR_SOLICIT_DELAY, rtr_solicit_delay),
267  __ADD(DEVCONF_USE_TEMPADDR, use_tempaddr),
268  __ADD(DEVCONF_TEMP_VALID_LFT, temp_valid_lft),
269  __ADD(DEVCONF_TEMP_PREFERED_LFT, temp_prefered_lft),
270  __ADD(DEVCONF_REGEN_MAX_RETRY, regen_max_retry),
271  __ADD(DEVCONF_MAX_DESYNC_FACTOR, max_desync_factor),
272  __ADD(DEVCONF_MAX_ADDRESSES, max_addresses),
273  __ADD(DEVCONF_FORCE_MLD_VERSION, force_mld_version),
274  __ADD(DEVCONF_ACCEPT_RA_DEFRTR, accept_ra_defrtr),
275  __ADD(DEVCONF_ACCEPT_RA_PINFO, accept_ra_pinfo),
276  __ADD(DEVCONF_ACCEPT_RA_RTR_PREF, accept_ra_rtr_pref),
277  __ADD(DEVCONF_RTR_PROBE_INTERVAL, rtr_probe_interval),
278  __ADD(DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN, accept_ra_rt_info),
279  __ADD(DEVCONF_PROXY_NDP, proxy_ndp),
280  __ADD(DEVCONF_OPTIMISTIC_DAD, optimistic_dad),
281  __ADD(DEVCONF_ACCEPT_SOURCE_ROUTE, accept_source_route),
282  __ADD(DEVCONF_MC_FORWARDING, mc_forwarding),
283  __ADD(DEVCONF_DISABLE_IPV6, disable_ipv6),
284  __ADD(DEVCONF_ACCEPT_DAD, accept_dad),
285  __ADD(DEVCONF_FORCE_TLLAO, force_tllao),
286 };
287 
288 static char *inet6_devconf2str(int type, char *buf, size_t len)
289 {
290  return __type2str(type, buf, len, inet6_devconf,
291  ARRAY_SIZE(inet6_devconf));
292 }
293 
294 static const struct trans_tbl inet6_addr_gen_mode[] = {
295  __ADD(IN6_ADDR_GEN_MODE_EUI64, eui64),
296  __ADD(IN6_ADDR_GEN_MODE_NONE, none),
297 };
298 
299 const char *rtnl_link_inet6_addrgenmode2str(uint8_t mode, char *buf, size_t len)
300 {
301  return __type2str(mode, buf, len, inet6_addr_gen_mode,
302  ARRAY_SIZE(inet6_addr_gen_mode));
303 }
304 
305 uint8_t rtnl_link_inet6_str2addrgenmode(const char *mode)
306 {
307  return (uint8_t) __str2type(mode, inet6_addr_gen_mode,
308  ARRAY_SIZE(inet6_addr_gen_mode));
309 }
310 
311 static void inet6_dump_details(struct rtnl_link *link,
312  struct nl_dump_params *p, void *data)
313 {
314  struct inet6_data *i6 = data;
315  struct nl_addr *addr;
316  char buf[64], buf2[64];
317  int i, n = 0;
318 
319  nl_dump_line(p, " ipv6 max-reasm-len %s",
320  nl_size2str(i6->i6_cacheinfo.max_reasm_len, buf, sizeof(buf)));
321 
322  nl_dump(p, " <%s>\n",
323  inet6_flags2str(i6->i6_flags, buf, sizeof(buf)));
324 
325 
326  nl_dump_line(p, " create-stamp %.2fs reachable-time %s",
327  (double) i6->i6_cacheinfo.tstamp / 100.,
328  nl_msec2str(i6->i6_cacheinfo.reachable_time, buf, sizeof(buf)));
329 
330  nl_dump(p, " retrans-time %s\n",
331  nl_msec2str(i6->i6_cacheinfo.retrans_time, buf, sizeof(buf)));
332 
333  addr = nl_addr_build(AF_INET6, &i6->i6_token, sizeof(i6->i6_token));
334  nl_dump(p, " token %s\n",
335  nl_addr2str(addr, buf, sizeof(buf)));
336  nl_addr_put(addr);
337 
338  nl_dump(p, " link-local address mode %s\n",
339  rtnl_link_inet6_addrgenmode2str(i6->i6_addr_gen_mode,
340  buf, sizeof(buf)));
341 
342  nl_dump_line(p, " devconf:\n");
343  nl_dump_line(p, " ");
344 
345  for (i = 0; i < DEVCONF_MAX; i++) {
346  uint32_t value = i6->i6_conf[i];
347  int x, offset;
348 
349  switch (i) {
350  case DEVCONF_TEMP_VALID_LFT:
351  case DEVCONF_TEMP_PREFERED_LFT:
352  nl_msec2str((uint64_t) value * 1000., buf2, sizeof(buf2));
353  break;
354 
355  case DEVCONF_RTR_PROBE_INTERVAL:
356  case DEVCONF_RTR_SOLICIT_INTERVAL:
357  case DEVCONF_RTR_SOLICIT_DELAY:
358  nl_msec2str(value, buf2, sizeof(buf2));
359  break;
360 
361  default:
362  snprintf(buf2, sizeof(buf2), "%u", value);
363  break;
364 
365  }
366 
367  inet6_devconf2str(i, buf, sizeof(buf));
368 
369  offset = 23 - strlen(buf2);
370  if (offset < 0)
371  offset = 0;
372 
373  for (x = strlen(buf); x < offset; x++)
374  buf[x] = ' ';
375 
376  strncpy(&buf[offset], buf2, strlen(buf2));
377 
378  nl_dump_line(p, "%s", buf);
379 
380  if (++n == 3) {
381  nl_dump(p, "\n");
382  nl_dump_line(p, " ");
383  n = 0;
384  } else
385  nl_dump(p, " ");
386  }
387 
388  if (n != 0)
389  nl_dump(p, "\n");
390 }
391 
392 static void inet6_dump_stats(struct rtnl_link *link,
393  struct nl_dump_params *p, void *data)
394 {
395  double octets;
396  char *octetsUnit;
397 
398  nl_dump(p, " IPv6: InPkts InOctets "
399  " InDiscards InDelivers\n");
400  nl_dump(p, " %18" PRIu64 " ", link->l_stats[RTNL_LINK_IP6_INPKTS]);
401 
402  octets = nl_cancel_down_bytes(link->l_stats[RTNL_LINK_IP6_INOCTETS],
403  &octetsUnit);
404  if (octets)
405  nl_dump(p, "%14.2f %3s ", octets, octetsUnit);
406  else
407  nl_dump(p, "%16" PRIu64 " B ", 0);
408 
409  nl_dump(p, "%18" PRIu64 " %18" PRIu64 "\n",
410  link->l_stats[RTNL_LINK_IP6_INDISCARDS],
411  link->l_stats[RTNL_LINK_IP6_INDELIVERS]);
412 
413  nl_dump(p, " OutPkts OutOctets "
414  " OutDiscards OutForwards\n");
415 
416  nl_dump(p, " %18" PRIu64 " ", link->l_stats[RTNL_LINK_IP6_OUTPKTS]);
417 
418  octets = nl_cancel_down_bytes(link->l_stats[RTNL_LINK_IP6_OUTOCTETS],
419  &octetsUnit);
420  if (octets)
421  nl_dump(p, "%14.2f %3s ", octets, octetsUnit);
422  else
423  nl_dump(p, "%16" PRIu64 " B ", 0);
424 
425  nl_dump(p, "%18" PRIu64 " %18" PRIu64 "\n",
426  link->l_stats[RTNL_LINK_IP6_OUTDISCARDS],
427  link->l_stats[RTNL_LINK_IP6_OUTFORWDATAGRAMS]);
428 
429  nl_dump(p, " InMcastPkts InMcastOctets "
430  " InBcastPkts InBcastOctests\n");
431 
432  nl_dump(p, " %18" PRIu64 " ", link->l_stats[RTNL_LINK_IP6_INMCASTPKTS]);
433 
434  octets = nl_cancel_down_bytes(link->l_stats[RTNL_LINK_IP6_INMCASTOCTETS],
435  &octetsUnit);
436  if (octets)
437  nl_dump(p, "%14.2f %3s ", octets, octetsUnit);
438  else
439  nl_dump(p, "%16" PRIu64 " B ", 0);
440 
441  nl_dump(p, "%18" PRIu64 " ", link->l_stats[RTNL_LINK_IP6_INBCASTPKTS]);
442  octets = nl_cancel_down_bytes(link->l_stats[RTNL_LINK_IP6_INBCASTOCTETS],
443  &octetsUnit);
444  if (octets)
445  nl_dump(p, "%14.2f %3s\n", octets, octetsUnit);
446  else
447  nl_dump(p, "%16" PRIu64 " B\n", 0);
448 
449  nl_dump(p, " OutMcastPkts OutMcastOctets "
450  " OutBcastPkts OutBcastOctests\n");
451 
452  nl_dump(p, " %18" PRIu64 " ", link->l_stats[RTNL_LINK_IP6_OUTMCASTPKTS]);
453 
454  octets = nl_cancel_down_bytes(link->l_stats[RTNL_LINK_IP6_OUTMCASTOCTETS],
455  &octetsUnit);
456  if (octets)
457  nl_dump(p, "%14.2f %3s ", octets, octetsUnit);
458  else
459  nl_dump(p, "%16" PRIu64 " B ", 0);
460 
461  nl_dump(p, "%18" PRIu64 " ", link->l_stats[RTNL_LINK_IP6_OUTBCASTPKTS]);
462  octets = nl_cancel_down_bytes(link->l_stats[RTNL_LINK_IP6_OUTBCASTOCTETS],
463  &octetsUnit);
464  if (octets)
465  nl_dump(p, "%14.2f %3s\n", octets, octetsUnit);
466  else
467  nl_dump(p, "%16" PRIu64 " B\n", 0);
468 
469  nl_dump(p, " ReasmOKs ReasmFails "
470  " ReasmReqds ReasmTimeout\n");
471  nl_dump(p, " %18" PRIu64 " %18" PRIu64 " %18" PRIu64 " %18" PRIu64 "\n",
472  link->l_stats[RTNL_LINK_IP6_REASMOKS],
473  link->l_stats[RTNL_LINK_IP6_REASMFAILS],
474  link->l_stats[RTNL_LINK_IP6_REASMREQDS],
475  link->l_stats[RTNL_LINK_IP6_REASMTIMEOUT]);
476 
477  nl_dump(p, " FragOKs FragFails "
478  " FragCreates\n");
479  nl_dump(p, " %18" PRIu64 " %18" PRIu64 " %18" PRIu64 "\n",
480  link->l_stats[RTNL_LINK_IP6_FRAGOKS],
481  link->l_stats[RTNL_LINK_IP6_FRAGFAILS],
482  link->l_stats[RTNL_LINK_IP6_FRAGCREATES]);
483 
484  nl_dump(p, " InHdrErrors InTooBigErrors "
485  " InNoRoutes InAddrErrors\n");
486  nl_dump(p, " %18" PRIu64 " %18" PRIu64 " %18" PRIu64 " %18" PRIu64 "\n",
487  link->l_stats[RTNL_LINK_IP6_INHDRERRORS],
488  link->l_stats[RTNL_LINK_IP6_INTOOBIGERRORS],
489  link->l_stats[RTNL_LINK_IP6_INNOROUTES],
490  link->l_stats[RTNL_LINK_IP6_INADDRERRORS]);
491 
492  nl_dump(p, " InUnknownProtos InTruncatedPkts "
493  " OutNoRoutes InCsumErrors\n");
494  nl_dump(p, " %18" PRIu64 " %18" PRIu64 " %18" PRIu64 " %18" PRIu64 "\n",
495  link->l_stats[RTNL_LINK_IP6_INUNKNOWNPROTOS],
496  link->l_stats[RTNL_LINK_IP6_INTRUNCATEDPKTS],
497  link->l_stats[RTNL_LINK_IP6_OUTNOROUTES],
498  link->l_stats[RTNL_LINK_IP6_CSUMERRORS]);
499 
500  nl_dump(p, " InNoECTPkts InECT1Pkts "
501  " InECT0Pkts InCEPkts\n");
502  nl_dump(p, " %18" PRIu64 " %18" PRIu64 " %18" PRIu64 " %18" PRIu64 "\n",
503  link->l_stats[RTNL_LINK_IP6_NOECTPKTS],
504  link->l_stats[RTNL_LINK_IP6_ECT1PKTS],
505  link->l_stats[RTNL_LINK_IP6_ECT0PKTS],
506  link->l_stats[RTNL_LINK_IP6_CEPKTS]);
507 
508  nl_dump(p, " ICMPv6: InMsgs InErrors "
509  " OutMsgs OutErrors InCsumErrors\n");
510  nl_dump(p, " %18" PRIu64 " %18" PRIu64 " %18" PRIu64 " %18" PRIu64 " %18" PRIu64 "\n",
511  link->l_stats[RTNL_LINK_ICMP6_INMSGS],
512  link->l_stats[RTNL_LINK_ICMP6_INERRORS],
513  link->l_stats[RTNL_LINK_ICMP6_OUTMSGS],
514  link->l_stats[RTNL_LINK_ICMP6_OUTERRORS],
515  link->l_stats[RTNL_LINK_ICMP6_CSUMERRORS]);
516 }
517 
518 static const struct nla_policy protinfo_policy = {
519  .type = NLA_NESTED,
520 };
521 
522 static struct rtnl_link_af_ops inet6_ops = {
523  .ao_family = AF_INET6,
524  .ao_alloc = &inet6_alloc,
525  .ao_clone = &inet6_clone,
526  .ao_free = &inet6_free,
527  .ao_parse_protinfo = &inet6_parse_protinfo,
528  .ao_parse_af = &inet6_parse_protinfo,
529  .ao_fill_af = &inet6_fill_af,
530  .ao_dump[NL_DUMP_DETAILS] = &inet6_dump_details,
531  .ao_dump[NL_DUMP_STATS] = &inet6_dump_stats,
532  .ao_protinfo_policy = &protinfo_policy,
533 };
534 
535 /**
536  * Get IPv6 tokenized interface identifier
537  * @arg link Link object
538  * @arg token Tokenized interface identifier on success
539  *
540  * Returns the link's IPv6 tokenized interface identifier.
541  *
542  * @return 0 on success
543  * @return -NLE_NOMEM failure to allocate struct nl_addr result
544  * @return -NLE_NOATTR configuration setting not available
545  * @return -NLE_NOADDR tokenized interface identifier is not set
546  */
547 int rtnl_link_inet6_get_token(struct rtnl_link *link, struct nl_addr **addr)
548 {
549  struct inet6_data *id;
550 
551  if (!(id = rtnl_link_af_data(link, &inet6_ops)))
552  return -NLE_NOATTR;
553 
554  *addr = nl_addr_build(AF_INET6, &id->i6_token, sizeof(id->i6_token));
555  if (!*addr)
556  return -NLE_NOMEM;
557  if (nl_addr_iszero(*addr)) {
558  nl_addr_put(*addr);
559  *addr = NULL;
560  return -NLE_NOADDR;
561  }
562 
563  return 0;
564 }
565 
566 /**
567  * Set IPv6 tokenized interface identifier
568  * @arg link Link object
569  * @arg token Tokenized interface identifier
570  *
571  * Sets the link's IPv6 tokenized interface identifier.
572  *
573  * @return 0 on success
574  * @return -NLE_NOMEM could not allocate inet6 data
575  * @return -NLE_INVAL addr is not a valid inet6 address
576  */
577 int rtnl_link_inet6_set_token(struct rtnl_link *link, struct nl_addr *addr)
578 {
579  struct inet6_data *id;
580 
581  if ((nl_addr_get_family(addr) != AF_INET6) ||
582  (nl_addr_get_len(addr) != sizeof(id->i6_token)))
583  return -NLE_INVAL;
584 
585  if (!(id = rtnl_link_af_alloc(link, &inet6_ops)))
586  return -NLE_NOMEM;
587 
588  memcpy(&id->i6_token, nl_addr_get_binary_addr(addr),
589  sizeof(id->i6_token));
590  return 0;
591 }
592 
593 /**
594  * Get IPv6 link-local address generation mode
595  * @arg link Link object
596  * @arg mode Generation mode on success
597  *
598  * Returns the link's IPv6 link-local address generation mode.
599  *
600  * @return 0 on success
601  * @return -NLE_NOATTR configuration setting not available
602  * @return -NLE_INVAL generation mode unknown. If the link was received via
603  * netlink, it means that address generation mode is not
604  * supported by the kernel.
605  */
606 int rtnl_link_inet6_get_addr_gen_mode(struct rtnl_link *link, uint8_t *mode)
607 {
608  struct inet6_data *id;
609 
610  if (!(id = rtnl_link_af_data(link, &inet6_ops)))
611  return -NLE_NOATTR;
612 
613  if (id->i6_addr_gen_mode == I6_ADDR_GEN_MODE_UNKNOWN)
614  return -NLE_INVAL;
615 
616  *mode = id->i6_addr_gen_mode;
617  return 0;
618 }
619 
620 /**
621  * Set IPv6 link-local address generation mode
622  * @arg link Link object
623  * @arg mode Generation mode
624  *
625  * Sets the link's IPv6 link-local address generation mode.
626  *
627  * @return 0 on success
628  * @return -NLE_NOMEM could not allocate inet6 data
629  */
630 int rtnl_link_inet6_set_addr_gen_mode(struct rtnl_link *link, uint8_t mode)
631 {
632  struct inet6_data *id;
633 
634  if (!(id = rtnl_link_af_alloc(link, &inet6_ops)))
635  return -NLE_NOMEM;
636 
637  id->i6_addr_gen_mode = mode;
638  return 0;
639 }
640 
641 static void __init inet6_init(void)
642 {
643  rtnl_link_af_register(&inet6_ops);
644 }
645 
646 static void __exit inet6_exit(void)
647 {
648  rtnl_link_af_unregister(&inet6_ops);
649 }
8 bit integer
Definition: attr.h:39
Attribute validation policy.
Definition: attr.h:67
uint8_t nla_get_u8(const struct nlattr *nla)
Return value of 8 bit integer attribute.
Definition: attr.c:599
struct nl_addr * nl_addr_build(int family, const void *buf, size_t size)
Allocate abstract address based on a binary address.
Definition: addr.c:216
char * nl_msec2str(uint64_t msec, char *buf, size_t len)
Convert milliseconds to a character string.
Definition: utils.c:547
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition: attr.c:699
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
Definition: attr.h:197
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:620
char * nl_size2str(const size_t size, char *buf, const size_t len)
Convert a size toa character string.
Definition: utils.c:327
double nl_cancel_down_bytes(unsigned long long l, char **unit)
Cancel down a byte counter.
Definition: utils.c:139
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:991
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
Definition: attr.c:120
int nla_len(const struct nlattr *nla)
Return length of the payload .
Definition: attr.c:131
Nested attributes.
Definition: attr.h:46
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
Definition: addr.c:517
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition: attr.h:69
32 bit integer
Definition: attr.h:41
Dumping parameters.
Definition: types.h:33
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:914
unsigned int nl_addr_get_len(const struct nl_addr *addr)
Get length of binary address of abstract address object.
Definition: addr.c:905
Dump all attributes including statistics.
Definition: types.h:24
void * nl_addr_get_binary_addr(const struct nl_addr *addr)
Get binary address of abstract address object.
Definition: addr.c:893
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
Definition: addr.c:951
int nl_addr_get_family(const struct nl_addr *addr)
Return address family.
Definition: addr.c:845