• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lib/route/link/ipgre.c        IPGRE 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 ipgre IPGRE
15  * ipgre link module
16  *
17  * @details
18  * \b Link Type Name: "ipgre"
19  *
20  * @route_doc{link_ipgre, IPGRE 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/ipgre.h>
32 #include <netlink-private/route/link/api.h>
33 #include <linux/if_tunnel.h>
34 
35 #define IPGRE_ATTR_LINK          (1 << 0)
36 #define IPGRE_ATTR_IFLAGS        (1 << 1)
37 #define IPGRE_ATTR_OFLAGS        (1 << 2)
38 #define IPGRE_ATTR_IKEY          (1 << 3)
39 #define IPGRE_ATTR_OKEY          (1 << 4)
40 #define IPGRE_ATTR_LOCAL         (1 << 5)
41 #define IPGRE_ATTR_REMOTE        (1 << 6)
42 #define IPGRE_ATTR_TTL           (1 << 7)
43 #define IPGRE_ATTR_TOS           (1 << 8)
44 #define IPGRE_ATTR_PMTUDISC      (1 << 9)
45 
46 struct ipgre_info
47 {
48 	uint8_t    ttl;
49 	uint8_t    tos;
50 	uint8_t    pmtudisc;
51 	uint16_t   iflags;
52 	uint16_t   oflags;
53 	uint32_t   ikey;
54 	uint32_t   okey;
55 	uint32_t   link;
56 	uint32_t   local;
57 	uint32_t   remote;
58 	uint32_t   ipgre_mask;
59 };
60 
61 static  struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
62 	[IFLA_GRE_LINK]     = { .type = NLA_U32 },
63 	[IFLA_GRE_IFLAGS]   = { .type = NLA_U16 },
64 	[IFLA_GRE_OFLAGS]   = { .type = NLA_U16 },
65 	[IFLA_GRE_IKEY]     = { .type = NLA_U32 },
66 	[IFLA_GRE_OKEY]     = { .type = NLA_U32 },
67 	[IFLA_GRE_LOCAL]    = { .type = NLA_U32 },
68 	[IFLA_GRE_REMOTE]   = { .type = NLA_U32 },
69 	[IFLA_GRE_TTL]      = { .type = NLA_U8 },
70 	[IFLA_GRE_TOS]      = { .type = NLA_U8 },
71 	[IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
72 };
73 
ipgre_alloc(struct rtnl_link * link)74 static int ipgre_alloc(struct rtnl_link *link)
75 {
76 	struct ipgre_info *ipgre;
77 
78 	if (link->l_info)
79 		memset(link->l_info, 0, sizeof(*ipgre));
80 	else {
81 		ipgre = calloc(1, sizeof(*ipgre));
82 		if (!ipgre)
83 			return -NLE_NOMEM;
84 
85 		link->l_info = ipgre;
86 	}
87 
88 	return 0;
89 }
90 
ipgre_parse(struct rtnl_link * link,struct nlattr * data,struct nlattr * xstats)91 static int ipgre_parse(struct rtnl_link *link, struct nlattr *data,
92 		       struct nlattr *xstats)
93 {
94 	struct nlattr *tb[IFLA_GRE_MAX + 1];
95 	struct ipgre_info *ipgre;
96 	int err;
97 
98 	NL_DBG(3, "Parsing IPGRE link info\n");
99 
100 	err = nla_parse_nested(tb, IFLA_GRE_MAX, data, ipgre_policy);
101 	if (err < 0)
102 		goto errout;
103 
104 	err = ipgre_alloc(link);
105 	if (err < 0)
106 		goto errout;
107 
108 	ipgre = link->l_info;
109 
110 	if (tb[IFLA_GRE_LINK]) {
111 		ipgre->link = nla_get_u32(tb[IFLA_GRE_LINK]);
112 		ipgre->ipgre_mask |= IPGRE_ATTR_LINK;
113 	}
114 
115 	if (tb[IFLA_GRE_IFLAGS]) {
116 		ipgre->iflags = nla_get_u16(tb[IFLA_GRE_IFLAGS]);
117 		ipgre->ipgre_mask |= IPGRE_ATTR_IFLAGS;
118 	}
119 
120 	if (tb[IFLA_GRE_OFLAGS]) {
121 		ipgre->oflags = nla_get_u16(tb[IFLA_GRE_OFLAGS]);
122 		ipgre->ipgre_mask |= IPGRE_ATTR_OFLAGS;
123 	}
124 
125 	if (tb[IFLA_GRE_IKEY]) {
126 		ipgre->ikey = nla_get_u32(tb[IFLA_GRE_IKEY]);
127 		ipgre->ipgre_mask |= IPGRE_ATTR_IKEY;
128 	}
129 
130 	if (tb[IFLA_GRE_OKEY]) {
131 		ipgre->okey = nla_get_u32(tb[IFLA_GRE_OKEY]);
132 		ipgre->ipgre_mask |= IPGRE_ATTR_OKEY;
133 	}
134 
135 	if (tb[IFLA_GRE_LOCAL]) {
136 		ipgre->local = nla_get_u32(tb[IFLA_GRE_LOCAL]);
137 		ipgre->ipgre_mask |= IPGRE_ATTR_LOCAL;
138 	}
139 
140 	if (tb[IFLA_GRE_REMOTE]) {
141 		ipgre->remote = nla_get_u32(tb[IFLA_GRE_REMOTE]);
142 		ipgre->ipgre_mask |= IPGRE_ATTR_REMOTE;
143 	}
144 
145 	if (tb[IFLA_GRE_TTL]) {
146 		ipgre->ttl = nla_get_u8(tb[IFLA_GRE_TTL]);
147 		ipgre->ipgre_mask |= IPGRE_ATTR_TTL;
148 	}
149 
150 	if (tb[IFLA_GRE_TOS]) {
151 		ipgre->tos = nla_get_u8(tb[IFLA_GRE_TOS]);
152 		ipgre->ipgre_mask |= IPGRE_ATTR_TOS;
153 	}
154 
155 	if (tb[IFLA_GRE_PMTUDISC]) {
156 		ipgre->pmtudisc = nla_get_u8(tb[IFLA_GRE_PMTUDISC]);
157 		ipgre->ipgre_mask |= IPGRE_ATTR_PMTUDISC;
158 	}
159 
160 	err = 0;
161 
162 errout:
163 	return err;
164 }
165 
ipgre_put_attrs(struct nl_msg * msg,struct rtnl_link * link)166 static int ipgre_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
167 {
168 	struct ipgre_info *ipgre = link->l_info;
169 	struct nlattr *data;
170 
171 	data = nla_nest_start(msg, IFLA_INFO_DATA);
172 	if (!data)
173 		return -NLE_MSGSIZE;
174 
175 	if (ipgre->ipgre_mask & IPGRE_ATTR_LINK)
176 		NLA_PUT_U32(msg, IFLA_GRE_LINK, ipgre->link);
177 
178 	if (ipgre->ipgre_mask & IFLA_GRE_IFLAGS)
179 		NLA_PUT_U16(msg, IFLA_GRE_IFLAGS, ipgre->iflags);
180 
181 	if (ipgre->ipgre_mask & IFLA_GRE_OFLAGS)
182 		NLA_PUT_U16(msg, IFLA_GRE_OFLAGS, ipgre->oflags);
183 
184 	if (ipgre->ipgre_mask & IPGRE_ATTR_IKEY)
185 		NLA_PUT_U32(msg, IFLA_GRE_IKEY, ipgre->ikey);
186 
187 	if (ipgre->ipgre_mask & IPGRE_ATTR_OKEY)
188 		NLA_PUT_U32(msg, IFLA_GRE_OKEY, ipgre->okey);
189 
190 	if (ipgre->ipgre_mask & IPGRE_ATTR_LOCAL)
191 		NLA_PUT_U32(msg, IFLA_GRE_LOCAL, ipgre->local);
192 
193 	if (ipgre->ipgre_mask & IPGRE_ATTR_REMOTE)
194 		NLA_PUT_U32(msg, IFLA_GRE_REMOTE, ipgre->remote);
195 
196 	if (ipgre->ipgre_mask & IPGRE_ATTR_TTL)
197 		NLA_PUT_U8(msg, IFLA_GRE_TTL, ipgre->ttl);
198 
199 	if (ipgre->ipgre_mask & IPGRE_ATTR_TOS)
200 		NLA_PUT_U8(msg, IFLA_GRE_TOS, ipgre->tos);
201 
202 	if (ipgre->ipgre_mask & IPGRE_ATTR_PMTUDISC)
203 		NLA_PUT_U8(msg, IFLA_GRE_PMTUDISC, ipgre->pmtudisc);
204 
205 	nla_nest_end(msg, data);
206 
207 nla_put_failure:
208 
209 	return 0;
210 }
211 
ipgre_free(struct rtnl_link * link)212 static void ipgre_free(struct rtnl_link *link)
213 {
214 	struct ipgre_info *ipgre = link->l_info;
215 
216 	free(ipgre);
217 	link->l_info = NULL;
218 }
219 
ipgre_dump_line(struct rtnl_link * link,struct nl_dump_params * p)220 static void ipgre_dump_line(struct rtnl_link *link, struct nl_dump_params *p)
221 {
222 	nl_dump(p, "ipgre : %s", link->l_name);
223 }
224 
ipgre_dump_details(struct rtnl_link * link,struct nl_dump_params * p)225 static void ipgre_dump_details(struct rtnl_link *link, struct nl_dump_params *p)
226 {
227 	struct ipgre_info *ipgre = link->l_info;
228 	char *name, addr[INET_ADDRSTRLEN];
229 	struct rtnl_link *parent;
230 
231 	if (ipgre->ipgre_mask & IPGRE_ATTR_LINK) {
232 		nl_dump(p, "      link ");
233 
234 		name = NULL;
235 		parent = link_lookup(link->ce_cache, ipgre->link);
236 		if (parent)
237 			name = rtnl_link_get_name(parent);
238 
239 		if (name)
240 			nl_dump_line(p, "%s\n", name);
241 		else
242 			nl_dump_line(p, "%u\n", ipgre->link);
243 	}
244 
245 	if (ipgre->ipgre_mask & IPGRE_ATTR_IFLAGS) {
246 		nl_dump(p, "      iflags ");
247 		nl_dump_line(p, "%x\n", ipgre->iflags);
248 	}
249 
250 	if (ipgre->ipgre_mask & IPGRE_ATTR_OFLAGS) {
251 		nl_dump(p, "      oflags ");
252 		nl_dump_line(p, "%x\n", ipgre->oflags);
253 	}
254 
255 	if (ipgre->ipgre_mask & IPGRE_ATTR_IKEY) {
256 		nl_dump(p, "    ikey   ");
257 		nl_dump_line(p, "%x\n",ipgre->ikey);
258 	}
259 
260 	if (ipgre->ipgre_mask & IPGRE_ATTR_OKEY) {
261 		nl_dump(p, "      okey ");
262 		nl_dump_line(p, "%x\n", ipgre->okey);
263 	}
264 
265 	if (ipgre->ipgre_mask & IPGRE_ATTR_LOCAL) {
266 		nl_dump(p, "      local ");
267 		if(inet_ntop(AF_INET, &ipgre->local, addr, sizeof(addr)))
268 			nl_dump_line(p, "%s\n", addr);
269 		else
270 			nl_dump_line(p, "%#x\n", ntohs(ipgre->local));
271 	}
272 
273 	if (ipgre->ipgre_mask & IPGRE_ATTR_REMOTE) {
274 		nl_dump(p, "      remote ");
275 		if(inet_ntop(AF_INET, &ipgre->remote, addr, sizeof(addr)))
276 			nl_dump_line(p, "%s\n", addr);
277 		else
278 			nl_dump_line(p, "%#x\n", ntohs(ipgre->remote));
279 	}
280 
281 	if (ipgre->ipgre_mask & IPGRE_ATTR_TTL) {
282 		nl_dump(p, "      ttl ");
283 		nl_dump_line(p, "%u\n", ipgre->ttl);
284 	}
285 
286 	if (ipgre->ipgre_mask & IPGRE_ATTR_TOS) {
287 		nl_dump(p, "      tos ");
288 		nl_dump_line(p, "%u\n", ipgre->tos);
289 	}
290 
291 	if (ipgre->ipgre_mask & IPGRE_ATTR_PMTUDISC) {
292 		nl_dump(p, "      pmtudisc ");
293 		nl_dump_line(p, "enabled (%#x)\n", ipgre->pmtudisc);
294 	}
295 }
296 
ipgre_clone(struct rtnl_link * dst,struct rtnl_link * src)297 static int ipgre_clone(struct rtnl_link *dst, struct rtnl_link *src)
298 {
299 	struct ipgre_info *ipgre_dst, *ipgre_src = src->l_info;
300 	int err;
301 
302 	dst->l_info = NULL;
303 
304 	err = rtnl_link_set_type(dst, "gre");
305 	if (err < 0)
306 		return err;
307 
308 	ipgre_dst = dst->l_info;
309 
310 	if (!ipgre_dst || !ipgre_src)
311 		BUG();
312 
313 	memcpy(ipgre_dst, ipgre_src, sizeof(struct ipgre_info));
314 
315 	return 0;
316 }
317 
ipgretap_clone(struct rtnl_link * dst,struct rtnl_link * src)318 static int ipgretap_clone(struct rtnl_link *dst, struct rtnl_link *src)
319 {
320 	struct ipgre_info *ipgre_dst, *ipgre_src = src->l_info;
321 	int err;
322 
323 	dst->l_info = NULL;
324 
325 	err = rtnl_link_set_type(dst, "gretap");
326 	if (err < 0)
327 		return err;
328 
329 	ipgre_dst = dst->l_info;
330 
331 	if (!ipgre_dst || !ipgre_src)
332 		BUG();
333 
334 	memcpy(ipgre_dst, ipgre_src, sizeof(struct ipgre_info));
335 
336 	return 0;
337 }
338 
339 static struct rtnl_link_info_ops ipgre_info_ops = {
340 	.io_name                = "gre",
341 	.io_alloc               = ipgre_alloc,
342 	.io_parse               = ipgre_parse,
343 	.io_dump = {
344 		[NL_DUMP_LINE]  = ipgre_dump_line,
345 		[NL_DUMP_DETAILS] = ipgre_dump_details,
346 	},
347 	.io_clone               = ipgre_clone,
348 	.io_put_attrs           = ipgre_put_attrs,
349 	.io_free                = ipgre_free,
350 };
351 
352 static struct rtnl_link_info_ops ipgretap_info_ops = {
353 	.io_name                = "gretap",
354 	.io_alloc               = ipgre_alloc,
355 	.io_parse               = ipgre_parse,
356 	.io_dump = {
357 		[NL_DUMP_LINE]  = ipgre_dump_line,
358 		[NL_DUMP_DETAILS] = ipgre_dump_details,
359 	},
360 	.io_clone               = ipgretap_clone,
361 	.io_put_attrs           = ipgre_put_attrs,
362 	.io_free                = ipgre_free,
363 };
364 
365 #define IS_IPGRE_LINK_ASSERT(link)                                                 \
366         if ((link)->l_info_ops != &ipgre_info_ops &&                               \
367             (link)->l_info_ops != &ipgretap_info_ops) {                            \
368                 APPBUG("Link is not a ipgre link. set type \"gre/gretap\" first.");\
369                 return -NLE_OPNOTSUPP;                                             \
370         }
371 
rtnl_link_ipgre_alloc(void)372 struct rtnl_link *rtnl_link_ipgre_alloc(void)
373 {
374 	struct rtnl_link *link;
375 	int err;
376 
377 	link = rtnl_link_alloc();
378 	if (!link)
379 		return NULL;
380 
381 	err = rtnl_link_set_type(link, "gre");
382 	if (err < 0) {
383 		rtnl_link_put(link);
384 		return NULL;
385 	}
386 
387 	return link;
388 }
389 
390 /**
391  * Check if link is a IPGRE link
392  * @arg link            Link object
393  *
394  * @return True if link is a IPGRE link, otherwise 0 is returned.
395  */
rtnl_link_is_ipgre(struct rtnl_link * link)396 int rtnl_link_is_ipgre(struct rtnl_link *link)
397 {
398 	return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "gre");
399 }
400 
401 /**
402  * Create a new IPGRE tunnel device
403  * @arg sock            netlink socket
404  * @arg name            name of the tunnel deviceL
405  *
406  * Creates a new ipip tunnel device in the kernel
407  * @return 0 on success or a negative error code
408  */
rtnl_link_ipgre_add(struct nl_sock * sk,const char * name)409 int rtnl_link_ipgre_add(struct nl_sock *sk, const char *name)
410 {
411 	struct rtnl_link *link;
412 	int err;
413 
414 	link = rtnl_link_ipgre_alloc();
415 	if (!link)
416 		return -NLE_NOMEM;
417 
418 	if(name)
419 		rtnl_link_set_name(link, name);
420 
421 	err = rtnl_link_add(sk, link, NLM_F_CREATE);
422 	rtnl_link_put(link);
423 
424 	return err;
425 }
426 
rtnl_link_ipgretap_alloc(void)427 struct rtnl_link *rtnl_link_ipgretap_alloc(void)
428 {
429 	struct rtnl_link *link;
430 	int err;
431 
432 	link = rtnl_link_alloc();
433 	if (!link)
434 		return NULL;
435 
436 	err = rtnl_link_set_type(link, "gretap");
437 	if (err < 0) {
438 		rtnl_link_put(link);
439 		return NULL;
440 	}
441 
442 	return link;
443 }
444 
445 /**
446  * Check if link is a IPGRETAP link
447  * @arg link            Link object
448  *
449  * @return True if link is a IPGRETAP link, otherwise 0 is returned.
450  */
rtnl_link_is_ipgretap(struct rtnl_link * link)451 int rtnl_link_is_ipgretap(struct rtnl_link *link)
452 {
453 	return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "gretap");
454 }
455 /**
456  * Create a new IPGRETAP tunnel device
457  * @arg sock            netlink socket
458  * @arg name            name of the tunnel deviceL
459  *
460  * Creates a new IPGRETAP tunnel device in the kernel
461  * @return 0 on success or a negative error code
462  */
rtnl_link_ipgretap_add(struct nl_sock * sk,const char * name)463 int rtnl_link_ipgretap_add(struct nl_sock *sk, const char *name)
464 {
465 	struct rtnl_link *link;
466 	int err;
467 
468 	link = rtnl_link_ipgretap_alloc();
469 	if (!link)
470 		return -NLE_NOMEM;
471 
472 	if(name)
473 		rtnl_link_set_name(link, name);
474 
475 	err = rtnl_link_add(sk, link, NLM_F_CREATE);
476 	rtnl_link_put(link);
477 
478 	return err;
479 }
480 
481 /**
482  * Set IPGRE tunnel interface index
483  * @arg link            Link object
484  * @arg index           interface index
485  *
486  * @return 0 on success or a negative error code
487  */
rtnl_link_ipgre_set_link(struct rtnl_link * link,uint32_t index)488 int rtnl_link_ipgre_set_link(struct rtnl_link *link,  uint32_t index)
489 {
490 	struct ipgre_info *ipgre = link->l_info;
491 
492 	IS_IPGRE_LINK_ASSERT(link);
493 
494 	ipgre->link = index;
495 	ipgre->ipgre_mask |= IPGRE_ATTR_LINK;
496 
497 	return 0;
498 }
499 
500 /**
501  * Get IPGRE tunnel interface index
502  * @arg link            Link object
503  *
504  * @return interface index
505  */
rtnl_link_ipgre_get_link(struct rtnl_link * link)506 uint32_t rtnl_link_ipgre_get_link(struct rtnl_link *link)
507 {
508 	struct ipgre_info *ipgre = link->l_info;
509 
510 	IS_IPGRE_LINK_ASSERT(link);
511 
512 	return ipgre->link;
513 }
514 
515 /**
516  * Set IPGRE tunnel set iflags
517  * @arg link            Link object
518  * @arg iflags          gre iflags
519  *
520  * @return 0 on success or a negative error code
521  */
rtnl_link_ipgre_set_iflags(struct rtnl_link * link,uint16_t iflags)522 int rtnl_link_ipgre_set_iflags(struct rtnl_link *link, uint16_t iflags)
523 {
524 	struct ipgre_info *ipgre = link->l_info;
525 
526 	IS_IPGRE_LINK_ASSERT(link);
527 
528 	ipgre->iflags = iflags;
529 	ipgre->ipgre_mask |= IPGRE_ATTR_IFLAGS;
530 
531 	return 0;
532 }
533 
534 /**
535  * Get IPGRE tunnel iflags
536  * @arg link            Link object
537  *
538  * @return iflags
539  */
rtnl_link_ipgre_get_iflags(struct rtnl_link * link)540 uint16_t rtnl_link_ipgre_get_iflags(struct rtnl_link *link)
541 {
542 	struct ipgre_info *ipgre = link->l_info;
543 
544 	IS_IPGRE_LINK_ASSERT(link);
545 
546 	return ipgre->iflags;
547 }
548 
549 /**
550  * Set IPGRE tunnel set oflags
551  * @arg link            Link object
552  * @arg iflags          gre oflags
553  *
554  * @return 0 on success or a negative error code
555  */
rtnl_link_ipgre_set_oflags(struct rtnl_link * link,uint16_t oflags)556 int rtnl_link_ipgre_set_oflags(struct rtnl_link *link, uint16_t oflags)
557 {
558 	struct ipgre_info *ipgre = link->l_info;
559 
560 	IS_IPGRE_LINK_ASSERT(link);
561 
562 	ipgre->oflags = oflags;
563 	ipgre->ipgre_mask |= IPGRE_ATTR_OFLAGS;
564 
565 	return 0;
566 }
567 
568 /**
569  * Get IPGRE tunnel oflags
570  * @arg link            Link object
571  *
572  * @return oflags
573  */
rtnl_link_ipgre_get_oflags(struct rtnl_link * link)574 uint16_t rtnl_link_ipgre_get_oflags(struct rtnl_link *link)
575 {
576 	struct ipgre_info *ipgre = link->l_info;
577 
578 	IS_IPGRE_LINK_ASSERT(link);
579 
580 	return ipgre->oflags;
581 }
582 
583 /**
584  * Set IPGRE tunnel set ikey
585  * @arg link            Link object
586  * @arg ikey            gre ikey
587  *
588  * @return 0 on success or a negative error code
589  */
rtnl_link_ipgre_set_ikey(struct rtnl_link * link,uint32_t ikey)590 int rtnl_link_ipgre_set_ikey(struct rtnl_link *link, uint32_t ikey)
591 {
592 	struct ipgre_info *ipgre = link->l_info;
593 
594 	IS_IPGRE_LINK_ASSERT(link);
595 
596 	ipgre->ikey = ikey;
597 	ipgre->ipgre_mask |= IPGRE_ATTR_IKEY;
598 
599 	return 0;
600 }
601 
602 /**
603  * Get IPGRE tunnel ikey
604  * @arg link            Link object
605  *
606  * @return ikey
607  */
rtnl_link_ipgre_get_ikey(struct rtnl_link * link)608 uint32_t rtnl_link_ipgre_get_ikey(struct rtnl_link *link)
609 {
610 	struct ipgre_info *ipgre = link->l_info;
611 
612 	IS_IPGRE_LINK_ASSERT(link);
613 
614 	return ipgre->ikey;
615 }
616 
617 /**
618  * Set IPGRE tunnel set okey
619  * @arg link            Link object
620  * @arg okey            gre okey
621  *
622  * @return 0 on success or a negative error code
623  */
rtnl_link_ipgre_set_okey(struct rtnl_link * link,uint32_t okey)624 int rtnl_link_ipgre_set_okey(struct rtnl_link *link, uint32_t okey)
625 {
626 	struct ipgre_info *ipgre = link->l_info;
627 
628 	IS_IPGRE_LINK_ASSERT(link);
629 
630 	ipgre->okey = okey;
631 	ipgre->ipgre_mask |= IPGRE_ATTR_OKEY;
632 
633 	return 0;
634 }
635 
636 /**
637  * Get IPGRE tunnel okey
638  * @arg link            Link object
639  *
640  * @return okey value
641  */
rtnl_link_ipgre_get_okey(struct rtnl_link * link)642 uint32_t rtnl_link_ipgre_get_okey(struct rtnl_link *link)
643 {
644 	struct ipgre_info *ipgre = link->l_info;
645 
646 	IS_IPGRE_LINK_ASSERT(link);
647 
648 	return ipgre->okey;
649 }
650 
651 /**
652  * Set IPGRE tunnel local address
653  * @arg link            Link object
654  * @arg addr            local address
655  *
656  * @return 0 on success or a negative error code
657  */
rtnl_link_ipgre_set_local(struct rtnl_link * link,uint32_t addr)658 int rtnl_link_ipgre_set_local(struct rtnl_link *link, uint32_t addr)
659 {
660 	struct ipgre_info *ipgre = link->l_info;
661 
662 	IS_IPGRE_LINK_ASSERT(link);
663 
664 	ipgre->local = addr;
665 	ipgre->ipgre_mask |= IPGRE_ATTR_LOCAL;
666 
667 	return 0;
668 }
669 
670 /**
671  * Get IPGRE tunnel local address
672  * @arg link            Link object
673  *
674  * @return local address
675  */
rtnl_link_ipgre_get_local(struct rtnl_link * link)676 uint32_t rtnl_link_ipgre_get_local(struct rtnl_link *link)
677 {
678 	struct ipgre_info *ipgre = link->l_info;
679 
680 	IS_IPGRE_LINK_ASSERT(link);
681 
682 	return ipgre->local;
683 }
684 
685 /**
686  * Set IPGRE tunnel remote address
687  * @arg link            Link object
688  * @arg remote          remote address
689  *
690  * @return 0 on success or a negative error code
691  */
rtnl_link_ipgre_set_remote(struct rtnl_link * link,uint32_t remote)692 int rtnl_link_ipgre_set_remote(struct rtnl_link *link, uint32_t remote)
693 {
694 	struct ipgre_info *ipgre = link->l_info;
695 
696 	IS_IPGRE_LINK_ASSERT(link);
697 
698 	ipgre->remote = remote;
699 	ipgre->ipgre_mask |= IPGRE_ATTR_REMOTE;
700 
701 	return 0;
702 }
703 
704 /**
705  * Get IPGRE tunnel remote address
706  * @arg link            Link object
707  *
708  * @return remote address  on success or a negative error code
709  */
rtnl_link_ipgre_get_remote(struct rtnl_link * link)710 uint32_t rtnl_link_ipgre_get_remote(struct rtnl_link *link)
711 {
712 	struct ipgre_info *ipgre = link->l_info;
713 
714 	IS_IPGRE_LINK_ASSERT(link);
715 
716 	return ipgre->remote;
717 }
718 
719 /**
720  * Set IPGRE tunnel ttl
721  * @arg link            Link object
722  * @arg ttl             tunnel ttl
723  *
724  * @return 0 on success or a negative error code
725  */
rtnl_link_ipgre_set_ttl(struct rtnl_link * link,uint8_t ttl)726 int rtnl_link_ipgre_set_ttl(struct rtnl_link *link, uint8_t ttl)
727 {
728 	struct ipgre_info *ipgre = link->l_info;
729 
730 	IS_IPGRE_LINK_ASSERT(link);
731 
732 	ipgre->ttl = ttl;
733 	ipgre->ipgre_mask |= IPGRE_ATTR_TTL;
734 
735 	return 0;
736 }
737 
738 /**
739  * Set IPGRE tunnel ttl
740  * @arg link            Link object
741  *
742  * @return ttl value
743  */
rtnl_link_ipgre_get_ttl(struct rtnl_link * link)744 uint8_t rtnl_link_ipgre_get_ttl(struct rtnl_link *link)
745 {
746 	struct ipgre_info *ipgre = link->l_info;
747 
748 	IS_IPGRE_LINK_ASSERT(link);
749 
750 	return ipgre->ttl;
751 }
752 
753 /**
754  * Set IPGRE tunnel tos
755  * @arg link            Link object
756  * @arg tos             tunnel tos
757  *
758  * @return 0 on success or a negative error code
759  */
rtnl_link_ipgre_set_tos(struct rtnl_link * link,uint8_t tos)760 int rtnl_link_ipgre_set_tos(struct rtnl_link *link, uint8_t tos)
761 {
762 	struct ipgre_info *ipgre = link->l_info;
763 
764 	IS_IPGRE_LINK_ASSERT(link);
765 
766 	ipgre->tos = tos;
767 	ipgre->ipgre_mask |= IPGRE_ATTR_TOS;
768 
769 	return 0;
770 }
771 
772 /**
773  * Get IPGRE tunnel tos
774  * @arg link            Link object
775  *
776  * @return tos value
777  */
rtnl_link_ipgre_get_tos(struct rtnl_link * link)778 uint8_t rtnl_link_ipgre_get_tos(struct rtnl_link *link)
779 {
780 	struct ipgre_info *ipgre = link->l_info;
781 
782 	IS_IPGRE_LINK_ASSERT(link);
783 
784 	return ipgre->tos;
785 }
786 
787 /**
788  * Set IPGRE tunnel path MTU discovery
789  * @arg link            Link object
790  * @arg pmtudisc        path MTU discovery
791  *
792  * @return 0 on success or a negative error code
793  */
rtnl_link_ipgre_set_pmtudisc(struct rtnl_link * link,uint8_t pmtudisc)794 int rtnl_link_ipgre_set_pmtudisc(struct rtnl_link *link, uint8_t pmtudisc)
795 {
796 	struct ipgre_info *ipgre = link->l_info;
797 
798 	IS_IPGRE_LINK_ASSERT(link);
799 
800 	ipgre->pmtudisc = pmtudisc;
801 	ipgre->ipgre_mask |= IPGRE_ATTR_PMTUDISC;
802 
803 	return 0;
804 }
805 
806 /**
807  * Get IPGRE path MTU discovery
808  * @arg link            Link object
809  *
810  * @return pmtudisc value
811  */
rtnl_link_ipgre_get_pmtudisc(struct rtnl_link * link)812 uint8_t rtnl_link_ipgre_get_pmtudisc(struct rtnl_link *link)
813 {
814 	struct ipgre_info *ipgre = link->l_info;
815 
816 	IS_IPGRE_LINK_ASSERT(link);
817 
818 	return ipgre->pmtudisc;
819 }
820 
821 /* Function prototype for ABI-preserving wrapper (not in public header) to avoid
822  * GCC warning about missing prototype. */
823 uint8_t rtnl_link_get_pmtudisc(struct rtnl_link *link);
824 
rtnl_link_get_pmtudisc(struct rtnl_link * link)825 uint8_t rtnl_link_get_pmtudisc(struct rtnl_link *link)
826 {
827 	/* rtnl_link_ipgre_get_pmtudisc() was wrongly named. Keep this
828 	 * to preserve ABI. */
829 	return rtnl_link_ipgre_get_pmtudisc (link);
830 }
831 
ipgre_init(void)832 static void __init ipgre_init(void)
833 {
834 	rtnl_link_register_info(&ipgre_info_ops);
835 	rtnl_link_register_info(&ipgretap_info_ops);
836 }
837 
ipgre_exit(void)838 static void __exit ipgre_exit(void)
839 {
840 	rtnl_link_unregister_info(&ipgre_info_ops);
841 	rtnl_link_unregister_info(&ipgretap_info_ops);
842 }
843