libnl  3.4.0
ipip.c
1 /*
2  * lib/route/link/ipip.c IPIP Link Info
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) 2014 Susant Sahani <susant@redhat.com>
10  */
11 
12 /**
13  * @ingroup link
14  * @defgroup ipip IPIP
15  * ipip link module
16  *
17  * @details
18  * \b Link Type Name: "ipip"
19  *
20  * @route_doc{link_ipip, IPIP Documentation}
21  *
22  * @{
23  */
24 
25 #include <netlink-private/netlink.h>
26 #include <netlink/netlink.h>
27 #include <netlink/attr.h>
28 #include <netlink/utils.h>
29 #include <netlink/object.h>
30 #include <netlink/route/rtnl.h>
31 #include <netlink/route/link/ipip.h>
32 #include <netlink-private/route/link/api.h>
33 #include <linux/if_tunnel.h>
34 
35 #define IPIP_ATTR_LINK (1 << 0)
36 #define IPIP_ATTR_LOCAL (1 << 1)
37 #define IPIP_ATTR_REMOTE (1 << 2)
38 #define IPIP_ATTR_TTL (1 << 3)
39 #define IPIP_ATTR_TOS (1 << 4)
40 #define IPIP_ATTR_PMTUDISC (1 << 5)
41 
42 struct ipip_info
43 {
44  uint8_t ttl;
45  uint8_t tos;
46  uint8_t pmtudisc;
47  uint32_t link;
48  uint32_t local;
49  uint32_t remote;
50  uint32_t ipip_mask;
51 };
52 
53 static struct nla_policy ipip_policy[IFLA_IPTUN_MAX + 1] = {
54  [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
55  [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
56  [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
57  [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
58  [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
59  [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
60 };
61 
62 static int ipip_alloc(struct rtnl_link *link)
63 {
64  struct ipip_info *ipip;
65 
66  if (link->l_info)
67  memset(link->l_info, 0, sizeof(*ipip));
68  else {
69  ipip = calloc(1, sizeof(*ipip));
70  if (!ipip)
71  return -NLE_NOMEM;
72 
73  link->l_info = ipip;
74  }
75 
76  return 0;
77 }
78 
79 static int ipip_parse(struct rtnl_link *link, struct nlattr *data,
80  struct nlattr *xstats)
81 {
82  struct nlattr *tb[IFLA_IPTUN_MAX + 1];
83  struct ipip_info *ipip;
84  int err;
85 
86  NL_DBG(3, "Parsing IPIP link info\n");
87 
88  err = nla_parse_nested(tb, IFLA_IPTUN_MAX, data, ipip_policy);
89  if (err < 0)
90  goto errout;
91 
92  err = ipip_alloc(link);
93  if (err < 0)
94  goto errout;
95 
96  ipip = link->l_info;
97 
98  if (tb[IFLA_IPTUN_LINK]) {
99  ipip->link = nla_get_u32(tb[IFLA_IPTUN_LINK]);
100  ipip->ipip_mask |= IPIP_ATTR_LINK;
101  }
102 
103  if (tb[IFLA_IPTUN_LOCAL]) {
104  ipip->local = nla_get_u32(tb[IFLA_IPTUN_LOCAL]);
105  ipip->ipip_mask |= IPIP_ATTR_LOCAL;
106  }
107 
108  if (tb[IFLA_IPTUN_REMOTE]) {
109  ipip->remote = nla_get_u32(tb[IFLA_IPTUN_REMOTE]);
110  ipip->ipip_mask |= IPIP_ATTR_REMOTE;
111  }
112 
113  if (tb[IFLA_IPTUN_TTL]) {
114  ipip->ttl = nla_get_u8(tb[IFLA_IPTUN_TTL]);
115  ipip->ipip_mask |= IPIP_ATTR_TTL;
116  }
117 
118  if (tb[IFLA_IPTUN_TOS]) {
119  ipip->tos = nla_get_u8(tb[IFLA_IPTUN_TOS]);
120  ipip->ipip_mask |= IPIP_ATTR_TOS;
121  }
122 
123  if (tb[IFLA_IPTUN_PMTUDISC]) {
124  ipip->pmtudisc = nla_get_u8(tb[IFLA_IPTUN_PMTUDISC]);
125  ipip->ipip_mask |= IPIP_ATTR_PMTUDISC;
126  }
127 
128  err = 0;
129 
130 errout:
131  return err;
132 }
133 
134 static int ipip_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
135 {
136  struct ipip_info *ipip = link->l_info;
137  struct nlattr *data;
138 
139  data = nla_nest_start(msg, IFLA_INFO_DATA);
140  if (!data)
141  return -NLE_MSGSIZE;
142 
143  if (ipip->ipip_mask & IPIP_ATTR_LINK)
144  NLA_PUT_U32(msg, IFLA_IPTUN_LINK, ipip->link);
145 
146  if (ipip->ipip_mask & IPIP_ATTR_LOCAL)
147  NLA_PUT_U32(msg, IFLA_IPTUN_LOCAL, ipip->local);
148 
149  if (ipip->ipip_mask & IPIP_ATTR_REMOTE)
150  NLA_PUT_U32(msg, IFLA_IPTUN_REMOTE, ipip->remote);
151 
152  if (ipip->ipip_mask & IPIP_ATTR_TTL)
153  NLA_PUT_U8(msg, IFLA_IPTUN_TTL, ipip->ttl);
154 
155  if (ipip->ipip_mask & IPIP_ATTR_TOS)
156  NLA_PUT_U8(msg, IFLA_IPTUN_TOS, ipip->tos);
157 
158  if (ipip->ipip_mask & IPIP_ATTR_PMTUDISC)
159  NLA_PUT_U8(msg, IFLA_IPTUN_PMTUDISC, ipip->pmtudisc);
160 
161  nla_nest_end(msg, data);
162 
163 nla_put_failure:
164  return 0;
165 }
166 
167 static void ipip_free(struct rtnl_link *link)
168 {
169  struct ipip_info *ipip = link->l_info;
170 
171  free(ipip);
172  link->l_info = NULL;
173 }
174 
175 static void ipip_dump_line(struct rtnl_link *link, struct nl_dump_params *p)
176 {
177  nl_dump(p, "ipip : %s", link->l_name);
178 }
179 
180 static void ipip_dump_details(struct rtnl_link *link, struct nl_dump_params *p)
181 {
182  struct ipip_info *ipip = link->l_info;
183  char *name, addr[INET_ADDRSTRLEN];
184  struct rtnl_link *parent;
185 
186  if (ipip->ipip_mask & IPIP_ATTR_LINK) {
187  nl_dump(p, " link ");
188 
189  name = NULL;
190  parent = link_lookup(link->ce_cache, ipip->link);
191  if (parent)
192  name = rtnl_link_get_name(parent);
193 
194  if (name)
195  nl_dump_line(p, "%s\n", name);
196  else
197  nl_dump_line(p, "%u\n", ipip->link);
198  }
199 
200  if (ipip->ipip_mask & IPIP_ATTR_LOCAL) {
201  nl_dump(p, " local ");
202  if(inet_ntop(AF_INET, &ipip->local, addr, sizeof(addr)))
203  nl_dump_line(p, "%s\n", addr);
204  else
205  nl_dump_line(p, "%#x\n", ntohs(ipip->local));
206  }
207 
208  if (ipip->ipip_mask & IPIP_ATTR_REMOTE) {
209  nl_dump(p, " remote ");
210  if(inet_ntop(AF_INET, &ipip->remote, addr, sizeof(addr)))
211  nl_dump_line(p, "%s\n", addr);
212  else
213  nl_dump_line(p, "%#x\n", ntohs(ipip->remote));
214  }
215 
216  if (ipip->ipip_mask & IPIP_ATTR_TTL) {
217  nl_dump(p, " ttl ");
218  nl_dump_line(p, "%u\n", ipip->ttl);
219  }
220 
221  if (ipip->ipip_mask & IPIP_ATTR_TOS) {
222  nl_dump(p, " tos ");
223  nl_dump_line(p, "%u\n", ipip->tos);
224  }
225 
226  if (ipip->ipip_mask & IPIP_ATTR_PMTUDISC) {
227  nl_dump(p, " pmtudisc ");
228  nl_dump_line(p, "enabled (%#x)\n", ipip->pmtudisc);
229  }
230 }
231 
232 static int ipip_clone(struct rtnl_link *dst, struct rtnl_link *src)
233 {
234  struct ipip_info *ipip_dst, *ipip_src = src->l_info;
235  int err;
236 
237  dst->l_info = NULL;
238 
239  err = rtnl_link_set_type(dst, "ipip");
240  if (err < 0)
241  return err;
242 
243  ipip_dst = dst->l_info;
244 
245  if (!ipip_dst || !ipip_src)
246  BUG();
247 
248  memcpy(ipip_dst, ipip_src, sizeof(struct ipip_info));
249 
250  return 0;
251 }
252 
253 static struct rtnl_link_info_ops ipip_info_ops = {
254  .io_name = "ipip",
255  .io_alloc = ipip_alloc,
256  .io_parse = ipip_parse,
257  .io_dump = {
258  [NL_DUMP_LINE] = ipip_dump_line,
259  [NL_DUMP_DETAILS] = ipip_dump_details,
260  },
261  .io_clone = ipip_clone,
262  .io_put_attrs = ipip_put_attrs,
263  .io_free = ipip_free,
264 };
265 
266 #define IS_IPIP_LINK_ASSERT(link) \
267  if ((link)->l_info_ops != &ipip_info_ops) { \
268  APPBUG("Link is not a ipip link. set type \"ipip\" first."); \
269  return -NLE_OPNOTSUPP; \
270  }
271 
272 struct rtnl_link *rtnl_link_ipip_alloc(void)
273 {
274  struct rtnl_link *link;
275  int err;
276 
277  link = rtnl_link_alloc();
278  if (!link)
279  return NULL;
280 
281  err = rtnl_link_set_type(link, "ipip");
282  if (err < 0) {
283  rtnl_link_put(link);
284  return NULL;
285  }
286 
287  return link;
288 }
289 
290 /**
291  * Check if link is a IPIP link
292  * @arg link Link object
293  *
294  * @return True if link is a IPIP link, otherwise false is returned.
295  */
296 int rtnl_link_is_ipip(struct rtnl_link *link)
297 {
298  return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "ipip");
299 }
300 
301 /**
302  * Create a new ipip tunnel device
303  * @arg sock netlink socket
304  * @arg name name of the tunnel deviceL
305  *
306  * Creates a new ipip tunnel device in the kernel
307  * @return 0 on success or a negative error code
308  */
309 int rtnl_link_ipip_add(struct nl_sock *sk, const char *name)
310 {
311  struct rtnl_link *link;
312  int err;
313 
314  link = rtnl_link_ipip_alloc();
315  if (!link)
316  return -NLE_NOMEM;
317 
318  if(name)
319  rtnl_link_set_name(link, name);
320 
321  err = rtnl_link_add(sk, link, NLM_F_CREATE);
322  rtnl_link_put(link);
323 
324  return err;
325 }
326 
327 /**
328  * Set IPIP tunnel interface index
329  * @arg link Link object
330  * @arg index interface index
331  *
332  * @return 0 on success or a negative error code
333  */
334 int rtnl_link_ipip_set_link(struct rtnl_link *link, uint32_t index)
335 {
336  struct ipip_info *ipip = link->l_info;
337 
338  IS_IPIP_LINK_ASSERT(link);
339 
340  ipip->link = index;
341  ipip->ipip_mask |= IPIP_ATTR_LINK;
342 
343  return 0;
344 }
345 
346 /**
347  * Get IPIP tunnel interface index
348  * @arg link Link object
349  *
350  * @return interface index value
351  */
352 uint32_t rtnl_link_ipip_get_link(struct rtnl_link *link)
353 {
354  struct ipip_info *ipip = link->l_info;
355 
356  IS_IPIP_LINK_ASSERT(link);
357 
358  return ipip->link;
359 }
360 
361 /**
362  * Set IPIP tunnel local address
363  * @arg link Link object
364  * @arg addr local address
365  *
366  * @return 0 on success or a negative error code
367  */
368 int rtnl_link_ipip_set_local(struct rtnl_link *link, uint32_t addr)
369 {
370  struct ipip_info *ipip = link->l_info;
371 
372  IS_IPIP_LINK_ASSERT(link);
373 
374  ipip->local = addr;
375  ipip->ipip_mask |= IPIP_ATTR_LOCAL;
376 
377  return 0;
378 }
379 
380 /**
381  * Get IPIP tunnel local address
382  * @arg link Link object
383  *
384  * @return local address value
385  */
386 uint32_t rtnl_link_ipip_get_local(struct rtnl_link *link)
387 {
388  struct ipip_info *ipip = link->l_info;
389 
390  IS_IPIP_LINK_ASSERT(link);
391 
392  return ipip->local;
393 }
394 
395 /**
396  * Set IPIP tunnel remote address
397  * @arg link Link object
398  * @arg remote remote address
399  *
400  * @return 0 on success or a negative error code
401  */
402 int rtnl_link_ipip_set_remote(struct rtnl_link *link, uint32_t addr)
403 {
404  struct ipip_info *ipip = link->l_info;
405 
406  IS_IPIP_LINK_ASSERT(link);
407 
408  ipip->remote = addr;
409  ipip->ipip_mask |= IPIP_ATTR_REMOTE;
410 
411  return 0;
412 }
413 
414 /**
415  * Get IPIP tunnel remote address
416  * @arg link Link object
417  *
418  * @return remote address
419  */
420 uint32_t rtnl_link_ipip_get_remote(struct rtnl_link *link)
421 {
422  struct ipip_info *ipip = link->l_info;
423 
424  IS_IPIP_LINK_ASSERT(link);
425 
426  return ipip->remote;
427 }
428 
429 /**
430  * Set IPIP tunnel ttl
431  * @arg link Link object
432  * @arg ttl tunnel ttl
433  *
434  * @return 0 on success or a negative error code
435  */
436 int rtnl_link_ipip_set_ttl(struct rtnl_link *link, uint8_t ttl)
437 {
438  struct ipip_info *ipip = link->l_info;
439 
440  IS_IPIP_LINK_ASSERT(link);
441 
442  ipip->ttl = ttl;
443  ipip->ipip_mask |= IPIP_ATTR_TTL;
444 
445  return 0;
446 }
447 
448 /**
449  * Get IPIP tunnel ttl
450  * @arg link Link object
451  *
452  * @return ttl value
453  */
454 uint8_t rtnl_link_ipip_get_ttl(struct rtnl_link *link)
455 {
456  struct ipip_info *ipip = link->l_info;
457 
458  IS_IPIP_LINK_ASSERT(link);
459 
460  return ipip->ttl;
461 }
462 
463 /**
464  * Set IPIP tunnel tos
465  * @arg link Link object
466  * @arg tos tunnel tos
467  *
468  * @return 0 on success or a negative error code
469  */
470 int rtnl_link_ipip_set_tos(struct rtnl_link *link, uint8_t tos)
471 {
472  struct ipip_info *ipip = link->l_info;
473 
474  IS_IPIP_LINK_ASSERT(link);
475 
476  ipip->tos = tos;
477  ipip->ipip_mask |= IPIP_ATTR_TOS;
478 
479  return 0;
480 }
481 
482 /**
483  * Get IPIP tunnel tos
484  * @arg link Link object
485  *
486  * @return tos value
487  */
488 uint8_t rtnl_link_ipip_get_tos(struct rtnl_link *link)
489 {
490  struct ipip_info *ipip = link->l_info;
491 
492  IS_IPIP_LINK_ASSERT(link);
493 
494  return ipip->tos;
495 }
496 
497 /**
498  * Set IPIP tunnel path MTU discovery
499  * @arg link Link object
500  * @arg pmtudisc path MTU discovery
501  *
502  * @return 0 on success or a negative error code
503  */
504 int rtnl_link_ipip_set_pmtudisc(struct rtnl_link *link, uint8_t pmtudisc)
505 {
506  struct ipip_info *ipip = link->l_info;
507 
508  IS_IPIP_LINK_ASSERT(link);
509 
510  ipip->pmtudisc = pmtudisc;
511  ipip->ipip_mask |= IPIP_ATTR_PMTUDISC;
512 
513  return 0;
514 }
515 
516 /**
517  * Get IPIP path MTU discovery
518  * @arg link Link object
519  *
520  * @return pmtudisc value
521  */
523 {
524  struct ipip_info *ipip = link->l_info;
525 
526  IS_IPIP_LINK_ASSERT(link);
527 
528  return ipip->pmtudisc;
529 }
530 
531 static void __init ipip_init(void)
532 {
533  rtnl_link_register_info(&ipip_info_ops);
534 }
535 
536 static void __exit ipip_exit(void)
537 {
538  rtnl_link_unregister_info(&ipip_info_ops);
539 }
Dump object briefly on one line.
Definition: types.h:22
8 bit integer
Definition: attr.h:41
int rtnl_link_ipip_set_pmtudisc(struct rtnl_link *link, uint8_t pmtudisc)
Set IPIP tunnel path MTU discovery.
Definition: ipip.c:504
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
Definition: ipip.c:42
int rtnl_link_is_ipip(struct rtnl_link *link)
Check if link is a IPIP link.
Definition: ipip.c:296
int rtnl_link_ipip_set_remote(struct rtnl_link *link, uint32_t addr)
Set IPIP tunnel remote address.
Definition: ipip.c:402
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition: attr.c:706
int rtnl_link_ipip_add(struct nl_sock *sk, const char *name)
Create a new ipip tunnel device.
Definition: ipip.c:309
int rtnl_link_ipip_set_ttl(struct rtnl_link *link, uint8_t ttl)
Set IPIP tunnel ttl.
Definition: ipip.c:436
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
Definition: attr.h:199
Dump all attributes but no statistics.
Definition: types.h:23
int rtnl_link_ipip_set_local(struct rtnl_link *link, uint32_t addr)
Set IPIP tunnel local address.
Definition: ipip.c:368
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
Definition: attr.c:924
uint32_t rtnl_link_ipip_get_local(struct rtnl_link *link)
Get IPIP tunnel local address.
Definition: ipip.c:386
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
uint8_t rtnl_link_ipip_get_pmtudisc(struct rtnl_link *link)
Get IPIP path MTU discovery.
Definition: ipip.c:522
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition: attr.h:235
int rtnl_link_ipip_set_link(struct rtnl_link *link, uint32_t index)
Set IPIP tunnel interface index.
Definition: ipip.c:334
uint32_t rtnl_link_ipip_get_remote(struct rtnl_link *link)
Get IPIP tunnel remote address.
Definition: ipip.c:420
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition: attr.h:71
32 bit integer
Definition: attr.h:43
Dumping parameters.
Definition: types.h:33
int rtnl_link_ipip_set_tos(struct rtnl_link *link, uint8_t tos)
Set IPIP tunnel tos.
Definition: ipip.c:470
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:961
uint32_t rtnl_link_ipip_get_link(struct rtnl_link *link)
Get IPIP tunnel interface index.
Definition: ipip.c:352
uint8_t rtnl_link_ipip_get_tos(struct rtnl_link *link)
Get IPIP tunnel tos.
Definition: ipip.c:488
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
Definition: attr.c:902
uint8_t rtnl_link_ipip_get_ttl(struct rtnl_link *link)
Get IPIP tunnel ttl.
Definition: ipip.c:454