• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2014 Susant Sahani <susant@redhat.com>
4  */
5 
6 /**
7  * @ingroup link
8  * @defgroup sit SIT
9  * sit link module
10  *
11  * @details
12  * \b Link Type Name: "sit"
13  *
14  * @route_doc{link_sit, SIT Documentation}
15  *
16  * @{
17  */
18 
19 #include <netlink-private/netlink.h>
20 #include <netlink/netlink.h>
21 #include <netlink/attr.h>
22 #include <netlink/utils.h>
23 #include <netlink/object.h>
24 #include <netlink/route/rtnl.h>
25 #include <netlink/route/link/sit.h>
26 #include <netlink-private/route/link/api.h>
27 #include <linux/if_tunnel.h>
28 
29 #define SIT_ATTR_LINK          (1 << 0)
30 #define SIT_ATTR_LOCAL         (1 << 1)
31 #define SIT_ATTR_REMOTE        (1 << 2)
32 #define SIT_ATTR_TTL           (1 << 3)
33 #define SIT_ATTR_TOS           (1 << 4)
34 #define SIT_ATTR_PMTUDISC      (1 << 5)
35 #define SIT_ATTR_FLAGS         (1 << 6)
36 #define SIT_ATTR_PROTO         (1 << 7)
37 #define SIT_ATTR_6RD_PREFIX          (1 << 8)
38 #define SIT_ATTR_6RD_RELAY_PREFIX    (1 << 9)
39 #define SIT_ATTR_6RD_PREFIXLEN       (1 << 10)
40 #define SIT_ATTR_6RD_RELAY_PREFIXLEN (1 << 11)
41 #define SIT_ATTR_FWMARK        (1 << 12)
42 
43 struct sit_info
44 {
45 	uint8_t    ttl;
46 	uint8_t    tos;
47 	uint8_t    pmtudisc;
48 	uint8_t    proto;
49 	uint16_t   flags;
50 	uint32_t   link;
51 	uint32_t   local;
52 	uint32_t   remote;
53 	struct in6_addr ip6rd_prefix;
54 	uint32_t   ip6rd_relay_prefix;
55 	uint16_t   ip6rd_prefixlen;
56 	uint16_t   ip6rd_relay_prefixlen;
57 	uint32_t   fwmark;
58 	uint32_t   sit_mask;
59 };
60 
61 static struct nla_policy sit_policy[IFLA_IPTUN_MAX + 1] = {
62 	[IFLA_IPTUN_LINK]       = { .type = NLA_U32 },
63 	[IFLA_IPTUN_LOCAL]      = { .type = NLA_U32 },
64 	[IFLA_IPTUN_REMOTE]     = { .type = NLA_U32 },
65 	[IFLA_IPTUN_TTL]        = { .type = NLA_U8 },
66 	[IFLA_IPTUN_TOS]        = { .type = NLA_U8 },
67 	[IFLA_IPTUN_PMTUDISC]   = { .type = NLA_U8 },
68 	[IFLA_IPTUN_FLAGS]      = { .type = NLA_U16 },
69 	[IFLA_IPTUN_PROTO]      = { .type = NLA_U8 },
70 	[IFLA_IPTUN_6RD_PREFIX]          = { .minlen = sizeof(struct in6_addr) },
71 	[IFLA_IPTUN_6RD_RELAY_PREFIX]    = { .type = NLA_U32 },
72 	[IFLA_IPTUN_6RD_PREFIXLEN]       = { .type = NLA_U16 },
73 	[IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
74 	[IFLA_IPTUN_FWMARK]     = { .type = NLA_U32 },
75 };
76 
sit_alloc(struct rtnl_link * link)77 static int sit_alloc(struct rtnl_link *link)
78 {
79 	struct sit_info *sit;
80 
81 	if (link->l_info)
82 		memset(link->l_info, 0, sizeof(*sit));
83 	else {
84 		sit = calloc(1, sizeof(*sit));
85 		if (!sit)
86 			return -NLE_NOMEM;
87 
88 		link->l_info = sit;
89 	}
90 
91 	return 0;
92 }
93 
sit_parse(struct rtnl_link * link,struct nlattr * data,struct nlattr * xstats)94 static int sit_parse(struct rtnl_link *link, struct nlattr *data,
95 		     struct nlattr *xstats)
96 {
97 	struct nlattr *tb[IFLA_IPTUN_MAX + 1];
98 	struct sit_info *sit;
99 	int err;
100 
101 	NL_DBG(3, "Parsing SIT link info\n");
102 
103 	err = nla_parse_nested(tb, IFLA_IPTUN_MAX, data, sit_policy);
104 	if (err < 0)
105 		goto errout;
106 
107 	err = sit_alloc(link);
108 	if (err < 0)
109 		goto errout;
110 
111 	sit = link->l_info;
112 
113 	if (tb[IFLA_IPTUN_LINK]) {
114 		sit->link = nla_get_u32(tb[IFLA_IPTUN_LINK]);
115 		sit->sit_mask |= SIT_ATTR_LINK;
116 	}
117 
118 	if (tb[IFLA_IPTUN_LOCAL]) {
119 		sit->local = nla_get_u32(tb[IFLA_IPTUN_LOCAL]);
120 		sit->sit_mask |= SIT_ATTR_LOCAL;
121 	}
122 
123 	if (tb[IFLA_IPTUN_REMOTE]) {
124 		sit->remote = nla_get_u32(tb[IFLA_IPTUN_REMOTE]);
125 		sit->sit_mask |= SIT_ATTR_REMOTE;
126 	}
127 
128 	if (tb[IFLA_IPTUN_TTL]) {
129 		sit->ttl = nla_get_u8(tb[IFLA_IPTUN_TTL]);
130 		sit->sit_mask |= SIT_ATTR_TTL;
131 	}
132 
133 	if (tb[IFLA_IPTUN_TOS]) {
134 		sit->tos = nla_get_u8(tb[IFLA_IPTUN_TOS]);
135 		sit->sit_mask |= SIT_ATTR_TOS;
136 	}
137 
138 	if (tb[IFLA_IPTUN_PMTUDISC]) {
139 		sit->pmtudisc = nla_get_u8(tb[IFLA_IPTUN_PMTUDISC]);
140 		sit->sit_mask |= SIT_ATTR_PMTUDISC;
141 	}
142 
143 	if (tb[IFLA_IPTUN_FLAGS]) {
144 		sit->flags = nla_get_u16(tb[IFLA_IPTUN_FLAGS]);
145 		sit->sit_mask |= SIT_ATTR_FLAGS;
146 	}
147 
148 	if (tb[IFLA_IPTUN_PROTO]) {
149 		sit->proto = nla_get_u8(tb[IFLA_IPTUN_PROTO]);
150 		sit->sit_mask |= SIT_ATTR_PROTO;
151 	}
152 
153 	if (tb[IFLA_IPTUN_6RD_PREFIX]) {
154 		nla_memcpy(&sit->ip6rd_prefix, tb[IFLA_IPTUN_6RD_PREFIX],
155 			   sizeof(struct in6_addr));
156 		sit->sit_mask |= SIT_ATTR_6RD_PREFIX;
157 	}
158 
159 	if (tb[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
160 		sit->ip6rd_relay_prefix = nla_get_u32(tb[IFLA_IPTUN_6RD_RELAY_PREFIX]);
161 		sit->sit_mask |= SIT_ATTR_6RD_RELAY_PREFIX;
162 	}
163 
164 	if (tb[IFLA_IPTUN_6RD_PREFIXLEN]) {
165 		sit->ip6rd_prefixlen = nla_get_u16(tb[IFLA_IPTUN_6RD_PREFIXLEN]);
166 		sit->sit_mask |= SIT_ATTR_6RD_PREFIXLEN;
167 	}
168 
169 	if (tb[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
170 		sit->ip6rd_relay_prefixlen = nla_get_u16(tb[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
171 		sit->sit_mask |= SIT_ATTR_6RD_RELAY_PREFIXLEN;
172 	}
173 
174 	if (tb[IFLA_IPTUN_FWMARK]) {
175 		sit->fwmark = nla_get_u32(tb[IFLA_IPTUN_FWMARK]);
176 		sit->sit_mask |= SIT_ATTR_FWMARK;
177 	}
178 
179 	err = 0;
180 
181 errout:
182 	return err;
183 }
184 
sit_put_attrs(struct nl_msg * msg,struct rtnl_link * link)185 static int sit_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
186 {
187 	struct sit_info *sit = link->l_info;
188 	struct nlattr *data;
189 
190 	data = nla_nest_start(msg, IFLA_INFO_DATA);
191 	if (!data)
192 		return -NLE_MSGSIZE;
193 
194 	if (sit->sit_mask & SIT_ATTR_LINK)
195 		NLA_PUT_U32(msg, IFLA_IPTUN_LINK, sit->link);
196 
197 	if (sit->sit_mask & SIT_ATTR_LOCAL)
198 		NLA_PUT_U32(msg, IFLA_IPTUN_LOCAL, sit->local);
199 
200 	if (sit->sit_mask & SIT_ATTR_REMOTE)
201 		NLA_PUT_U32(msg, IFLA_IPTUN_REMOTE, sit->remote);
202 
203 	if (sit->sit_mask & SIT_ATTR_TTL)
204 		NLA_PUT_U8(msg, IFLA_IPTUN_TTL, sit->ttl);
205 
206 	if (sit->sit_mask & SIT_ATTR_TOS)
207 		NLA_PUT_U8(msg, IFLA_IPTUN_TOS, sit->tos);
208 
209 	if (sit->sit_mask & SIT_ATTR_PMTUDISC)
210 		NLA_PUT_U8(msg, IFLA_IPTUN_PMTUDISC, sit->pmtudisc);
211 
212 	if (sit->sit_mask & SIT_ATTR_FLAGS)
213 		NLA_PUT_U16(msg, IFLA_IPTUN_FLAGS, sit->flags);
214 
215 	if (sit->sit_mask & SIT_ATTR_PROTO)
216 		NLA_PUT_U8(msg, IFLA_IPTUN_PROTO, sit->proto);
217 
218 	if (sit->sit_mask & SIT_ATTR_6RD_PREFIX)
219 		NLA_PUT(msg, IFLA_IPTUN_6RD_PREFIX, sizeof(struct in6_addr), &sit->ip6rd_prefix);
220 
221 	if (sit->sit_mask & SIT_ATTR_6RD_RELAY_PREFIX)
222 		NLA_PUT_U32(msg, IFLA_IPTUN_6RD_RELAY_PREFIX, sit->ip6rd_relay_prefix);
223 
224 	if (sit->sit_mask & SIT_ATTR_6RD_PREFIXLEN)
225 		NLA_PUT_U16(msg, IFLA_IPTUN_6RD_PREFIXLEN, sit->ip6rd_prefixlen);
226 
227 	if (sit->sit_mask & SIT_ATTR_6RD_RELAY_PREFIXLEN)
228 		NLA_PUT_U16(msg, IFLA_IPTUN_6RD_RELAY_PREFIXLEN, sit->ip6rd_relay_prefixlen);
229 
230 	if (sit->sit_mask & SIT_ATTR_FWMARK)
231 		NLA_PUT_U32(msg, IFLA_IPTUN_FWMARK, sit->fwmark);
232 
233 	nla_nest_end(msg, data);
234 
235 nla_put_failure:
236 
237 	return 0;
238 }
239 
sit_free(struct rtnl_link * link)240 static void sit_free(struct rtnl_link *link)
241 {
242 	struct sit_info *sit = link->l_info;
243 
244 	free(sit);
245 	link->l_info = NULL;
246 }
247 
sit_dump_line(struct rtnl_link * link,struct nl_dump_params * p)248 static void sit_dump_line(struct rtnl_link *link, struct nl_dump_params *p)
249 {
250 	nl_dump(p, "sit : %s", link->l_name);
251 }
252 
sit_dump_details(struct rtnl_link * link,struct nl_dump_params * p)253 static void sit_dump_details(struct rtnl_link *link, struct nl_dump_params *p)
254 {
255 	struct sit_info *sit = link->l_info;
256 	char *name, addr[INET_ADDRSTRLEN], addr6[INET6_ADDRSTRLEN];
257 	struct rtnl_link *parent;
258 
259 	if (sit->sit_mask & SIT_ATTR_LINK) {
260 		nl_dump(p, "      link ");
261 
262 		name = NULL;
263 		parent = link_lookup(link->ce_cache, sit->link);
264 		if (parent)
265 			name = rtnl_link_get_name(parent);
266 
267 		if (name)
268 			nl_dump_line(p, "%s\n", name);
269 		else
270 			nl_dump_line(p, "%u\n", sit->link);
271 	}
272 
273 	if (sit->sit_mask & SIT_ATTR_LOCAL) {
274 		nl_dump(p, "      local ");
275 		if(inet_ntop(AF_INET, &sit->local, addr, sizeof(addr)))
276 			nl_dump_line(p, "%s\n", addr);
277 		else
278 			nl_dump_line(p, "%#x\n", ntohs(sit->local));
279 	}
280 
281 	if (sit->sit_mask & SIT_ATTR_REMOTE) {
282 		nl_dump(p, "      remote ");
283 		if(inet_ntop(AF_INET, &sit->remote, addr, sizeof(addr)))
284 			nl_dump_line(p, "%s\n", addr);
285 		else
286 			nl_dump_line(p, "%#x\n", ntohs(sit->remote));
287 	}
288 
289 	if (sit->sit_mask & SIT_ATTR_TTL) {
290 		nl_dump(p, "      ttl ");
291 		nl_dump_line(p, "%u\n", sit->ttl);
292 	}
293 
294 	if (sit->sit_mask & SIT_ATTR_TOS) {
295 		nl_dump(p, "      tos ");
296 		nl_dump_line(p, "%u\n", sit->tos);
297 	}
298 
299 	if (sit->sit_mask & SIT_ATTR_FLAGS) {
300 		nl_dump(p, "      flags ");
301 		nl_dump_line(p, " (%x)\n", sit->flags);
302 	}
303 
304 	if (sit->sit_mask & SIT_ATTR_PROTO) {
305 		nl_dump(p, "      proto   ");
306 		nl_dump_line(p, " (%x)\n", sit->proto);
307 	}
308 
309 	if (sit->sit_mask & SIT_ATTR_6RD_PREFIX) {
310 		nl_dump(p, "      6rd_prefix   ");
311 		if(inet_ntop(AF_INET6, &sit->ip6rd_prefix, addr6, INET6_ADDRSTRLEN))
312 			nl_dump_line(p, "%s\n", addr6);
313 		else
314 			nl_dump_line(p, "[unknown]\n");
315 	}
316 
317 	if (sit->sit_mask & SIT_ATTR_6RD_RELAY_PREFIX) {
318 		nl_dump(p, "      6rd_relay_prefix   ");
319 		if(inet_ntop(AF_INET, &sit->ip6rd_relay_prefix, addr, sizeof(addr)))
320 			nl_dump_line(p, "%s\n", addr);
321 		else
322 			nl_dump_line(p, "[unknown]\n");
323 	}
324 
325 	if (sit->sit_mask & SIT_ATTR_6RD_PREFIXLEN) {
326 		nl_dump(p, "      6rd_prefixlen   ");
327 		nl_dump_line(p, "%d\n", sit->ip6rd_prefixlen);
328 	}
329 
330 	if (sit->sit_mask & SIT_ATTR_6RD_RELAY_PREFIXLEN) {
331 		nl_dump(p, "      6rd_relay_prefixlen   ");
332 		nl_dump_line(p, "%d\n", sit->ip6rd_relay_prefixlen);
333 	}
334 
335 	if (sit->sit_mask & SIT_ATTR_FWMARK) {
336 		nl_dump(p, "      fwmark ");
337 		nl_dump_line(p, "%x\n", sit->fwmark);
338 	}
339 }
340 
sit_clone(struct rtnl_link * dst,struct rtnl_link * src)341 static int sit_clone(struct rtnl_link *dst, struct rtnl_link *src)
342 {
343 	struct sit_info *sit_dst, *sit_src = src->l_info;
344 	int err;
345 
346 	dst->l_info = NULL;
347 
348 	err = rtnl_link_set_type(dst, "sit");
349 	if (err < 0)
350 		return err;
351 
352 	sit_dst = dst->l_info;
353 
354 	if (!sit_dst || !sit_src)
355 		return -NLE_NOMEM;
356 
357 	memcpy(sit_dst, sit_src, sizeof(struct sit_info));
358 
359 	return 0;
360 }
361 
362 static struct rtnl_link_info_ops sit_info_ops = {
363 	.io_name                = "sit",
364 	.io_alloc               = sit_alloc,
365 	.io_parse               = sit_parse,
366 	.io_dump = {
367 		[NL_DUMP_LINE]  = sit_dump_line,
368 		[NL_DUMP_DETAILS] = sit_dump_details,
369 	},
370 	.io_clone               = sit_clone,
371 	.io_put_attrs           = sit_put_attrs,
372 	.io_free                = sit_free,
373 };
374 
375 #define IS_SIT_LINK_ASSERT(link, sit)                                              \
376         struct sit_info *sit;                                                      \
377         do {                                                                       \
378                 const struct rtnl_link *_link = (link);                            \
379                 if (!_link || _link->l_info_ops != &sit_info_ops) {                \
380                         APPBUG("Link is not a sit link. set type \"sit\" first."); \
381                         return -NLE_OPNOTSUPP;                                     \
382                 }                                                                  \
383                 (sit) = _link->l_info;                                             \
384         } while (0)
385 
rtnl_link_sit_alloc(void)386 struct rtnl_link *rtnl_link_sit_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, "sit");
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 SIT link
406  * @arg link            Link object
407  *
408  * @return True if link is a SIT link, otherwise false is returned.
409  */
rtnl_link_is_sit(struct rtnl_link * link)410 int rtnl_link_is_sit(struct rtnl_link *link)
411 {
412 	return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "sit");
413 }
414 
415 /**
416  * Create a new sit tunnel device
417  * @arg sock            netlink socket
418  * @arg name            name of the tunnel device
419  *
420  * Creates a new sit tunnel device in the kernel
421  * @return 0 on success or a negative error code
422  */
rtnl_link_sit_add(struct nl_sock * sk,const char * name)423 int rtnl_link_sit_add(struct nl_sock *sk, const char *name)
424 {
425 	struct rtnl_link *link;
426 	int err;
427 
428 	link = rtnl_link_sit_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 
441 /**
442  * Set SIT tunnel interface index
443  * @arg link            Link object
444  * @arg index           interface index
445  *
446  * @return 0 on success or a negative error code
447  */
rtnl_link_sit_set_link(struct rtnl_link * link,uint32_t index)448 int rtnl_link_sit_set_link(struct rtnl_link *link,  uint32_t index)
449 {
450 	IS_SIT_LINK_ASSERT(link, sit);
451 
452 	sit->link = index;
453 	sit->sit_mask |= SIT_ATTR_LINK;
454 
455 	return 0;
456 }
457 
458 /**
459  * Get SIT tunnel interface index
460  * @arg link            Link object
461  *
462  * @return interface index value
463  */
rtnl_link_sit_get_link(struct rtnl_link * link)464 uint32_t rtnl_link_sit_get_link(struct rtnl_link *link)
465 {
466 	IS_SIT_LINK_ASSERT(link, sit);
467 
468 	return sit->link;
469 }
470 
471 /**
472  * Set SIT tunnel local address
473  * @arg link            Link object
474  * @arg addr            local address
475  *
476  * @return 0 on success or a negative error code
477  */
rtnl_link_sit_set_local(struct rtnl_link * link,uint32_t addr)478 int rtnl_link_sit_set_local(struct rtnl_link *link, uint32_t addr)
479 {
480 	IS_SIT_LINK_ASSERT(link, sit);
481 
482 	sit->local = addr;
483 	sit->sit_mask |= SIT_ATTR_LOCAL;
484 
485 	return 0;
486 }
487 
488 /**
489  * Get SIT tunnel local address
490  * @arg link            Link object
491  *
492  * @return local address value
493  */
rtnl_link_sit_get_local(struct rtnl_link * link)494 uint32_t rtnl_link_sit_get_local(struct rtnl_link *link)
495 {
496 	IS_SIT_LINK_ASSERT(link, sit);
497 
498 	return sit->local;
499 }
500 
501 /**
502  * Set SIT tunnel remote address
503  * @arg link            Link object
504  * @arg remote          remote address
505  *
506  * @return 0 on success or a negative error code
507  */
rtnl_link_sit_set_remote(struct rtnl_link * link,uint32_t addr)508 int rtnl_link_sit_set_remote(struct rtnl_link *link, uint32_t addr)
509 {
510 	IS_SIT_LINK_ASSERT(link, sit);
511 
512 	sit->remote = addr;
513 	sit->sit_mask |= SIT_ATTR_REMOTE;
514 
515 	return 0;
516 }
517 
518 /**
519  * Get SIT tunnel remote address
520  * @arg link            Link object
521  *
522  * @return remote address
523  */
rtnl_link_sit_get_remote(struct rtnl_link * link)524 uint32_t rtnl_link_sit_get_remote(struct rtnl_link *link)
525 {
526 	IS_SIT_LINK_ASSERT(link, sit);
527 
528 	return sit->remote;
529 }
530 
531 /**
532  * Set SIT tunnel ttl
533  * @arg link            Link object
534  * @arg ttl             tunnel ttl
535  *
536  * @return 0 on success or a negative error code
537  */
rtnl_link_sit_set_ttl(struct rtnl_link * link,uint8_t ttl)538 int rtnl_link_sit_set_ttl(struct rtnl_link *link, uint8_t ttl)
539 {
540 	IS_SIT_LINK_ASSERT(link, sit);
541 
542 	sit->ttl = ttl;
543 	sit->sit_mask |= SIT_ATTR_TTL;
544 
545 	return 0;
546 }
547 
548 /**
549  * Get SIT tunnel ttl
550  * @arg link            Link object
551  *
552  * @return ttl value
553  */
rtnl_link_sit_get_ttl(struct rtnl_link * link)554 uint8_t rtnl_link_sit_get_ttl(struct rtnl_link *link)
555 {
556 	IS_SIT_LINK_ASSERT(link, sit);
557 
558 	return sit->ttl;
559 }
560 
561 /**
562  * Set SIT tunnel tos
563  * @arg link            Link object
564  * @arg tos             tunnel tos
565  *
566  * @return 0 on success or a negative error code
567  */
rtnl_link_sit_set_tos(struct rtnl_link * link,uint8_t tos)568 int rtnl_link_sit_set_tos(struct rtnl_link *link, uint8_t tos)
569 {
570 	IS_SIT_LINK_ASSERT(link, sit);
571 
572 	sit->tos = tos;
573 	sit->sit_mask |= SIT_ATTR_TOS;
574 
575 	return 0;
576 }
577 
578 /**
579  * Get SIT tunnel tos
580  * @arg link            Link object
581  *
582  * @return tos value
583  */
rtnl_link_sit_get_tos(struct rtnl_link * link)584 uint8_t rtnl_link_sit_get_tos(struct rtnl_link *link)
585 {
586 	IS_SIT_LINK_ASSERT(link, sit);
587 
588 	return sit->tos;
589 }
590 
591 /**
592  * Set SIT tunnel path MTU discovery
593  * @arg link            Link object
594  * @arg pmtudisc        path MTU discovery
595  *
596  * @return 0 on success or a negative error code
597  */
rtnl_link_sit_set_pmtudisc(struct rtnl_link * link,uint8_t pmtudisc)598 int rtnl_link_sit_set_pmtudisc(struct rtnl_link *link, uint8_t pmtudisc)
599 {
600 	IS_SIT_LINK_ASSERT(link, sit);
601 
602 	sit->pmtudisc = pmtudisc;
603 	sit->sit_mask |= SIT_ATTR_PMTUDISC;
604 
605 	return 0;
606 }
607 
608 /**
609  * Get SIT path MTU discovery
610  * @arg link            Link object
611  *
612  * @return pmtudisc value
613  */
rtnl_link_sit_get_pmtudisc(struct rtnl_link * link)614 uint8_t rtnl_link_sit_get_pmtudisc(struct rtnl_link *link)
615 {
616 	IS_SIT_LINK_ASSERT(link, sit);
617 
618 	return sit->pmtudisc;
619 }
620 
621 /**
622  * Set SIT tunnel flags
623  * @arg link            Link object
624  * @arg flags           tunnel flags
625  *
626  * @return 0 on success or a negative error code
627  */
rtnl_link_sit_set_flags(struct rtnl_link * link,uint16_t flags)628 int rtnl_link_sit_set_flags(struct rtnl_link *link, uint16_t flags)
629 {
630 	IS_SIT_LINK_ASSERT(link, sit);
631 
632 	sit->flags = flags;
633 	sit->sit_mask |= SIT_ATTR_FLAGS;
634 
635 	return 0;
636 }
637 
638 /**
639  * Get SIT path flags
640  * @arg link            Link object
641  *
642  * @return flags value
643  */
rtnl_link_sit_get_flags(struct rtnl_link * link)644 uint16_t rtnl_link_sit_get_flags(struct rtnl_link *link)
645 {
646 	IS_SIT_LINK_ASSERT(link, sit);
647 
648 	return sit->flags;
649 }
650 
651 /**
652  * Set SIT tunnel proto
653  * @arg link            Link object
654  * @arg proto           tunnel proto
655  *
656  * @return 0 on success or a negative error code
657  */
rtnl_link_sit_set_proto(struct rtnl_link * link,uint8_t proto)658 int rtnl_link_sit_set_proto(struct rtnl_link *link, uint8_t proto)
659 {
660 	IS_SIT_LINK_ASSERT(link, sit);
661 
662 	sit->proto = proto;
663 	sit->sit_mask |= SIT_ATTR_PROTO;
664 
665 	return 0;
666 }
667 
668 /**
669  * Get SIT proto
670  * @arg link            Link object
671  *
672  * @return proto value
673  */
rtnl_link_sit_get_proto(struct rtnl_link * link)674 uint8_t rtnl_link_sit_get_proto(struct rtnl_link *link)
675 {
676 	IS_SIT_LINK_ASSERT(link, sit);
677 
678 	return sit->proto;
679 }
680 
681 /**
682  * Set ip6rd prefix
683  * @arg link            Link object
684  * @arg prefix          The IPv6 prefix
685  *
686  * @return 0 on success or an error code.
687  */
rtnl_link_sit_set_ip6rd_prefix(struct rtnl_link * link,const struct in6_addr * prefix)688 int rtnl_link_sit_set_ip6rd_prefix(struct rtnl_link *link, const struct in6_addr *prefix)
689 {
690 	IS_SIT_LINK_ASSERT(link, sit);
691 
692 	sit->ip6rd_prefix = *prefix;
693 	sit->sit_mask |= SIT_ATTR_6RD_PREFIX;
694 	return 0;
695 }
696 
697 /**
698  * Get ip6rd prefix
699  * @arg link            Link object
700  * @arg prefix          The output IPv6 prefix
701  *
702  * @return 0 on success or an error code. If the property is unset,
703  *   this call fails too.
704  */
rtnl_link_sit_get_ip6rd_prefix(const struct rtnl_link * link,struct in6_addr * prefix)705 int rtnl_link_sit_get_ip6rd_prefix(const struct rtnl_link *link, struct in6_addr *prefix)
706 {
707 	IS_SIT_LINK_ASSERT(link, sit);
708 
709 	if (!(sit->sit_mask & SIT_ATTR_6RD_PREFIX))
710 		return -NLE_NOATTR;
711 
712 	if (prefix)
713 		*prefix = sit->ip6rd_prefix;
714 	return 0;
715 }
716 
717 /**
718  * Set ip6rd prefix length
719  * @arg link            Link object
720  * @arg prefixlen       The IPv6 prefix length
721  *
722  * @return 0 on success or an error code.
723  */
rtnl_link_sit_set_ip6rd_prefixlen(struct rtnl_link * link,uint16_t prefixlen)724 int rtnl_link_sit_set_ip6rd_prefixlen(struct rtnl_link *link, uint16_t prefixlen)
725 {
726 	IS_SIT_LINK_ASSERT(link, sit);
727 
728 	sit->sit_mask |= SIT_ATTR_6RD_PREFIXLEN;
729 	sit->ip6rd_prefixlen = prefixlen;
730 	return 0;
731 }
732 
733 /**
734  * Get ip6rd prefix length
735  * @arg link            Link object
736  * @arg prefixlen       Output pointer for the prefix length
737  *
738  * @return 0 on success or an error code. If the property is unset,
739  *   this call fails.
740  */
rtnl_link_sit_get_ip6rd_prefixlen(struct rtnl_link * link,uint16_t * prefixlen)741 int rtnl_link_sit_get_ip6rd_prefixlen(struct rtnl_link *link, uint16_t *prefixlen)
742 {
743 	IS_SIT_LINK_ASSERT(link, sit);
744 
745 	if (!(sit->sit_mask & SIT_ATTR_6RD_PREFIXLEN))
746 		return -NLE_NOATTR;
747 
748 	if (prefixlen)
749 		*prefixlen = sit->ip6rd_prefixlen;
750 	return 0;
751 }
752 
753 /**
754  * Set ip6rd relay prefix
755  * @arg link            Link object
756  * @arg prefix          The IPv6 prefix length
757  *
758  * @return 0 on success or an error code.
759  */
rtnl_link_sit_set_ip6rd_relay_prefix(struct rtnl_link * link,uint32_t prefix)760 int rtnl_link_sit_set_ip6rd_relay_prefix(struct rtnl_link *link, uint32_t prefix)
761 {
762 	IS_SIT_LINK_ASSERT(link, sit);
763 
764 	sit->sit_mask |= SIT_ATTR_6RD_RELAY_PREFIX;
765 	sit->ip6rd_relay_prefix = prefix;
766 	return 0;
767 }
768 
769 /**
770  * Get ip6rd prefix length
771  * @arg link            Link object
772  * @arg prefixlen       Output pointer for the prefix length
773  *
774  * @return 0 on success or an error code. If the property is unset,
775  *   this call fails.
776  */
rtnl_link_sit_get_ip6rd_relay_prefix(const struct rtnl_link * link,uint32_t * prefix)777 int rtnl_link_sit_get_ip6rd_relay_prefix(const struct rtnl_link *link, uint32_t *prefix)
778 {
779 	IS_SIT_LINK_ASSERT(link, sit);
780 
781 	if (!(sit->sit_mask & SIT_ATTR_6RD_RELAY_PREFIX))
782 		return -NLE_NOATTR;
783 
784 	if (prefix)
785 		*prefix = sit->ip6rd_relay_prefix;
786 	return 0;
787 }
788 
789 /**
790  * Set ip6rd relay prefix length
791  * @arg link            Link object
792  * @arg prefixlen       The IPv6 prefix length
793  *
794  * @return 0 on success or an error code.
795  */
rtnl_link_sit_set_ip6rd_relay_prefixlen(struct rtnl_link * link,uint16_t prefixlen)796 int rtnl_link_sit_set_ip6rd_relay_prefixlen(struct rtnl_link *link, uint16_t prefixlen)
797 {
798 	IS_SIT_LINK_ASSERT(link, sit);
799 
800 	sit->sit_mask |= SIT_ATTR_6RD_RELAY_PREFIXLEN;
801 	sit->ip6rd_relay_prefixlen = prefixlen;
802 	return 0;
803 }
804 
805 /**
806  * Get ip6rd relay prefix length
807  * @arg link            Link object
808  * @arg prefixlen       Output pointer for the prefix length
809  *
810  * @return 0 on success or an error code. If the property is unset,
811  *   this call fails.
812  */
rtnl_link_sit_get_ip6rd_relay_prefixlen(struct rtnl_link * link,uint16_t * prefixlen)813 int rtnl_link_sit_get_ip6rd_relay_prefixlen(struct rtnl_link *link, uint16_t *prefixlen)
814 {
815 	IS_SIT_LINK_ASSERT(link, sit);
816 
817 	if (!(sit->sit_mask & SIT_ATTR_6RD_RELAY_PREFIX))
818 		return -NLE_NOATTR;
819 
820 	if (prefixlen)
821 		*prefixlen = sit->ip6rd_relay_prefixlen;
822 	return 0;
823 }
824 
825 /**
826  * Set SIT tunnel fwmark
827  * @arg link            Link object
828  * @arg fwmark          fwmark
829  *
830  * @return 0 on success or a negative error code
831  */
rtnl_link_sit_set_fwmark(struct rtnl_link * link,uint32_t fwmark)832 int rtnl_link_sit_set_fwmark(struct rtnl_link *link, uint32_t fwmark)
833 {
834 	IS_SIT_LINK_ASSERT(link, sit);
835 
836 	sit->fwmark = fwmark;
837 	sit->sit_mask |= SIT_ATTR_FWMARK;
838 
839 	return 0;
840 }
841 
842 /**
843  * Get SIT tunnel fwmark
844  * @arg link            Link object
845  * @arg fwmark          addr to fill in with the fwmark
846  *
847  * @return 0 on success or a negative error code
848  */
rtnl_link_sit_get_fwmark(struct rtnl_link * link,uint32_t * fwmark)849 int rtnl_link_sit_get_fwmark(struct rtnl_link *link, uint32_t *fwmark)
850 {
851 	IS_SIT_LINK_ASSERT(link, sit);
852 
853 	if (!(sit->sit_mask & SIT_ATTR_FWMARK))
854 		return -NLE_NOATTR;
855 
856 	*fwmark = sit->fwmark;
857 
858 	return 0;
859 }
860 
sit_init(void)861 static void __init sit_init(void)
862 {
863 	rtnl_link_register_info(&sit_info_ops);
864 }
865 
sit_exit(void)866 static void __exit sit_exit(void)
867 {
868 	rtnl_link_unregister_info(&sit_info_ops);
869 }
870