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