]>
Commit | Line | Data |
---|---|---|
af89576d | 1 | /* |
2 | * link_gre6.c gre driver module | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Dmitry Kozlov <xeb@mail.ru> | |
10 | * | |
11 | */ | |
12 | ||
13 | #include <string.h> | |
14 | #include <net/if.h> | |
15 | #include <sys/types.h> | |
16 | #include <sys/socket.h> | |
17 | #include <arpa/inet.h> | |
18 | ||
19 | #include <linux/ip.h> | |
20 | #include <linux/if_tunnel.h> | |
21 | #include <linux/ip6_tunnel.h> | |
22 | ||
23 | #include "rt_names.h" | |
24 | #include "utils.h" | |
25 | #include "ip_common.h" | |
26 | #include "tunnel.h" | |
27 | ||
28 | #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000) | |
29 | #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF) | |
30 | ||
31 | #define DEFAULT_TNL_HOP_LIMIT (64) | |
32 | ||
ae91205c | 33 | static void gre_print_help(struct link_util *lu, int argc, char **argv, FILE *f) |
af89576d | 34 | { |
8b471354 | 35 | fprintf(f, |
ae91205c SP |
36 | "Usage: ... %-9s [ remote ADDR ]\n", |
37 | lu->id | |
38 | ); | |
39 | fprintf(f, | |
40 | " [ local ADDR ]\n" | |
41 | " [ [i|o]seq ]\n" | |
42 | " [ [i|o]key KEY ]\n" | |
43 | " [ [i|o]csum ]\n" | |
44 | " [ hoplimit TTL ]\n" | |
45 | " [ encaplimit ELIM ]\n" | |
46 | " [ tclass TCLASS ]\n" | |
47 | " [ flowlabel FLOWLABEL ]\n" | |
48 | " [ dscp inherit ]\n" | |
49 | " [ dev PHYS_DEV ]\n" | |
50 | " [ fwmark MARK ]\n" | |
51 | " [ external ]\n" | |
52 | " [ noencap ]\n" | |
53 | " [ encap { fou | gue | none } ]\n" | |
54 | " [ encap-sport PORT ]\n" | |
55 | " [ encap-dport PORT ]\n" | |
56 | " [ [no]encap-csum ]\n" | |
57 | " [ [no]encap-csum6 ]\n" | |
58 | " [ [no]encap-remcsum ]\n" | |
59 | " [ erspan_ver version ]\n" | |
60 | " [ erspan IDX ]\n" | |
61 | " [ erspan_dir { ingress | egress } ]\n" | |
62 | " [ erspan_hwid hwid ]\n" | |
8b471354 | 63 | "\n" |
ae91205c SP |
64 | ); |
65 | fprintf(f, | |
8b471354 PS |
66 | "Where: ADDR := IPV6_ADDRESS\n" |
67 | " TTL := { 0..255 } (default=%d)\n" | |
68 | " KEY := { DOTTED_QUAD | NUMBER }\n" | |
69 | " ELIM := { none | 0..255 }(default=%d)\n" | |
70 | " TCLASS := { 0x0..0xff | inherit }\n" | |
ad4b1425 CG |
71 | " FLOWLABEL := { 0x0..0xfffff | inherit }\n" |
72 | " MARK := { 0x0..0xffffffff | inherit }\n", | |
8b471354 PS |
73 | DEFAULT_TNL_HOP_LIMIT, IPV6_DEFAULT_TNL_ENCAP_LIMIT |
74 | ); | |
561e650e | 75 | } |
76 | ||
af89576d | 77 | static int gre_parse_opt(struct link_util *lu, int argc, char **argv, |
78 | struct nlmsghdr *n) | |
79 | { | |
28254695 | 80 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
af89576d | 81 | struct { |
82 | struct nlmsghdr n; | |
83 | struct ifinfomsg i; | |
d17b136f PS |
84 | } req = { |
85 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)), | |
86 | .n.nlmsg_flags = NLM_F_REQUEST, | |
87 | .n.nlmsg_type = RTM_GETLINK, | |
88 | .i.ifi_family = preferred_family, | |
89 | .i.ifi_index = ifi->ifi_index, | |
90 | }; | |
08ede25f | 91 | struct nlmsghdr *answer; |
af89576d | 92 | struct rtattr *tb[IFLA_MAX + 1]; |
93 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; | |
94 | struct rtattr *greinfo[IFLA_GRE_MAX + 1]; | |
95 | __u16 iflags = 0; | |
96 | __u16 oflags = 0; | |
1f44b937 SP |
97 | __be32 ikey = 0; |
98 | __be32 okey = 0; | |
af89576d | 99 | struct in6_addr raddr = IN6ADDR_ANY_INIT; |
100 | struct in6_addr laddr = IN6ADDR_ANY_INIT; | |
56f5daac SH |
101 | unsigned int link = 0; |
102 | unsigned int flowinfo = 0; | |
103 | unsigned int flags = 0; | |
af89576d | 104 | __u8 hop_limit = DEFAULT_TNL_HOP_LIMIT; |
105 | __u8 encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT; | |
0b2fbb73 TH |
106 | __u16 encaptype = 0; |
107 | __u16 encapflags = TUNNEL_ENCAP_FLAG_CSUM6; | |
108 | __u16 encapsport = 0; | |
109 | __u16 encapdport = 0; | |
6231c5be | 110 | __u8 metadata = 0; |
af89576d | 111 | int len; |
ad4b1425 | 112 | __u32 fwmark = 0; |
8595cc40 | 113 | __u32 erspan_idx = 0; |
28976362 WT |
114 | __u8 erspan_ver = 0; |
115 | __u8 erspan_dir = 0; | |
116 | __u16 erspan_hwid = 0; | |
af89576d | 117 | |
118 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { | |
86bf43c7 | 119 | if (rtnl_talk(&rth, &req.n, &answer) < 0) { |
af89576d | 120 | get_failed: |
121 | fprintf(stderr, | |
122 | "Failed to get existing tunnel info.\n"); | |
123 | return -1; | |
124 | } | |
125 | ||
86bf43c7 | 126 | len = answer->nlmsg_len; |
af89576d | 127 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
128 | if (len < 0) | |
129 | goto get_failed; | |
130 | ||
86bf43c7 | 131 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(NLMSG_DATA(answer)), len); |
af89576d | 132 | |
133 | if (!tb[IFLA_LINKINFO]) | |
134 | goto get_failed; | |
135 | ||
136 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
137 | ||
138 | if (!linkinfo[IFLA_INFO_DATA]) | |
139 | goto get_failed; | |
140 | ||
141 | parse_rtattr_nested(greinfo, IFLA_GRE_MAX, | |
142 | linkinfo[IFLA_INFO_DATA]); | |
143 | ||
144 | if (greinfo[IFLA_GRE_IKEY]) | |
145 | ikey = rta_getattr_u32(greinfo[IFLA_GRE_IKEY]); | |
146 | ||
147 | if (greinfo[IFLA_GRE_OKEY]) | |
148 | okey = rta_getattr_u32(greinfo[IFLA_GRE_OKEY]); | |
149 | ||
150 | if (greinfo[IFLA_GRE_IFLAGS]) | |
151 | iflags = rta_getattr_u16(greinfo[IFLA_GRE_IFLAGS]); | |
152 | ||
153 | if (greinfo[IFLA_GRE_OFLAGS]) | |
154 | oflags = rta_getattr_u16(greinfo[IFLA_GRE_OFLAGS]); | |
155 | ||
156 | if (greinfo[IFLA_GRE_LOCAL]) | |
157 | memcpy(&laddr, RTA_DATA(greinfo[IFLA_GRE_LOCAL]), sizeof(laddr)); | |
158 | ||
159 | if (greinfo[IFLA_GRE_REMOTE]) | |
160 | memcpy(&raddr, RTA_DATA(greinfo[IFLA_GRE_REMOTE]), sizeof(raddr)); | |
161 | ||
162 | if (greinfo[IFLA_GRE_TTL]) | |
163 | hop_limit = rta_getattr_u8(greinfo[IFLA_GRE_TTL]); | |
164 | ||
165 | if (greinfo[IFLA_GRE_LINK]) | |
166 | link = rta_getattr_u32(greinfo[IFLA_GRE_LINK]); | |
167 | ||
168 | if (greinfo[IFLA_GRE_ENCAP_LIMIT]) | |
169 | encap_limit = rta_getattr_u8(greinfo[IFLA_GRE_ENCAP_LIMIT]); | |
170 | ||
171 | if (greinfo[IFLA_GRE_FLOWINFO]) | |
172 | flowinfo = rta_getattr_u32(greinfo[IFLA_GRE_FLOWINFO]); | |
173 | ||
174 | if (greinfo[IFLA_GRE_FLAGS]) | |
175 | flags = rta_getattr_u32(greinfo[IFLA_GRE_FLAGS]); | |
0b2fbb73 TH |
176 | |
177 | if (greinfo[IFLA_GRE_ENCAP_TYPE]) | |
178 | encaptype = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_TYPE]); | |
179 | ||
180 | if (greinfo[IFLA_GRE_ENCAP_FLAGS]) | |
181 | encapflags = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_FLAGS]); | |
182 | ||
183 | if (greinfo[IFLA_GRE_ENCAP_SPORT]) | |
184 | encapsport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_SPORT]); | |
185 | ||
6231c5be WT |
186 | if (greinfo[IFLA_GRE_COLLECT_METADATA]) |
187 | metadata = 1; | |
188 | ||
0b2fbb73 TH |
189 | if (greinfo[IFLA_GRE_ENCAP_DPORT]) |
190 | encapdport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_DPORT]); | |
ad4b1425 CG |
191 | |
192 | if (greinfo[IFLA_GRE_FWMARK]) | |
193 | fwmark = rta_getattr_u32(greinfo[IFLA_GRE_FWMARK]); | |
86bf43c7 | 194 | |
8595cc40 WT |
195 | if (greinfo[IFLA_GRE_ERSPAN_INDEX]) |
196 | erspan_idx = rta_getattr_u32(greinfo[IFLA_GRE_ERSPAN_INDEX]); | |
197 | ||
28976362 WT |
198 | if (greinfo[IFLA_GRE_ERSPAN_VER]) |
199 | erspan_ver = rta_getattr_u8(greinfo[IFLA_GRE_ERSPAN_VER]); | |
200 | ||
201 | if (greinfo[IFLA_GRE_ERSPAN_DIR]) | |
202 | erspan_dir = rta_getattr_u8(greinfo[IFLA_GRE_ERSPAN_DIR]); | |
203 | ||
204 | if (greinfo[IFLA_GRE_ERSPAN_HWID]) | |
205 | erspan_hwid = rta_getattr_u16(greinfo[IFLA_GRE_ERSPAN_HWID]); | |
206 | ||
86bf43c7 | 207 | free(answer); |
af89576d | 208 | } |
209 | ||
210 | while (argc > 0) { | |
211 | if (!matches(*argv, "key")) { | |
af89576d | 212 | NEXT_ARG(); |
213 | iflags |= GRE_KEY; | |
214 | oflags |= GRE_KEY; | |
1f44b937 | 215 | ikey = okey = tnl_parse_key("key", *argv); |
af89576d | 216 | } else if (!matches(*argv, "ikey")) { |
af89576d | 217 | NEXT_ARG(); |
218 | iflags |= GRE_KEY; | |
1f44b937 | 219 | ikey = tnl_parse_key("ikey", *argv); |
af89576d | 220 | } else if (!matches(*argv, "okey")) { |
af89576d | 221 | NEXT_ARG(); |
222 | oflags |= GRE_KEY; | |
1f44b937 | 223 | okey = tnl_parse_key("okey", *argv); |
af89576d | 224 | } else if (!matches(*argv, "seq")) { |
225 | iflags |= GRE_SEQ; | |
226 | oflags |= GRE_SEQ; | |
227 | } else if (!matches(*argv, "iseq")) { | |
228 | iflags |= GRE_SEQ; | |
229 | } else if (!matches(*argv, "oseq")) { | |
230 | oflags |= GRE_SEQ; | |
231 | } else if (!matches(*argv, "csum")) { | |
232 | iflags |= GRE_CSUM; | |
233 | oflags |= GRE_CSUM; | |
234 | } else if (!matches(*argv, "icsum")) { | |
235 | iflags |= GRE_CSUM; | |
236 | } else if (!matches(*argv, "ocsum")) { | |
237 | oflags |= GRE_CSUM; | |
238 | } else if (!matches(*argv, "remote")) { | |
239 | inet_prefix addr; | |
56f5daac | 240 | |
af89576d | 241 | NEXT_ARG(); |
9aceaad7 | 242 | get_addr(&addr, *argv, AF_INET6); |
af89576d | 243 | memcpy(&raddr, &addr.data, sizeof(raddr)); |
244 | } else if (!matches(*argv, "local")) { | |
245 | inet_prefix addr; | |
56f5daac | 246 | |
af89576d | 247 | NEXT_ARG(); |
9aceaad7 | 248 | get_addr(&addr, *argv, AF_INET6); |
af89576d | 249 | memcpy(&laddr, &addr.data, sizeof(laddr)); |
250 | } else if (!matches(*argv, "dev")) { | |
251 | NEXT_ARG(); | |
7a14358b | 252 | link = ll_name_to_index(*argv); |
0cb6bb51 CW |
253 | if (link == 0) { |
254 | fprintf(stderr, "Cannot find device \"%s\"\n", | |
255 | *argv); | |
af89576d | 256 | exit(-1); |
0cb6bb51 | 257 | } |
af89576d | 258 | } else if (!matches(*argv, "ttl") || |
c4743c4d SP |
259 | !matches(*argv, "hoplimit") || |
260 | !matches(*argv, "hlim")) { | |
af89576d | 261 | NEXT_ARG(); |
c4743c4d SP |
262 | if (strcmp(*argv, "inherit") != 0) { |
263 | if (get_u8(&hop_limit, *argv, 0)) | |
264 | invarg("invalid HLIM\n", *argv); | |
265 | } else | |
266 | hop_limit = 0; | |
af89576d | 267 | } else if (!matches(*argv, "tos") || |
268 | !matches(*argv, "tclass") || | |
269 | !matches(*argv, "dsfield")) { | |
270 | __u8 uval; | |
56f5daac | 271 | |
af89576d | 272 | NEXT_ARG(); |
afdf9277 | 273 | flowinfo &= ~IP6_FLOWINFO_TCLASS; |
af89576d | 274 | if (strcmp(*argv, "inherit") == 0) |
275 | flags |= IP6_TNL_F_USE_ORIG_TCLASS; | |
276 | else { | |
277 | if (get_u8(&uval, *argv, 16)) | |
278 | invarg("invalid TClass", *argv); | |
279 | flowinfo |= htonl((__u32)uval << 20) & IP6_FLOWINFO_TCLASS; | |
280 | flags &= ~IP6_TNL_F_USE_ORIG_TCLASS; | |
281 | } | |
282 | } else if (strcmp(*argv, "flowlabel") == 0 || | |
283 | strcmp(*argv, "fl") == 0) { | |
284 | __u32 uval; | |
56f5daac | 285 | |
af89576d | 286 | NEXT_ARG(); |
afdf9277 | 287 | flowinfo &= ~IP6_FLOWINFO_FLOWLABEL; |
af89576d | 288 | if (strcmp(*argv, "inherit") == 0) |
289 | flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL; | |
290 | else { | |
291 | if (get_u32(&uval, *argv, 16)) | |
292 | invarg("invalid Flowlabel", *argv); | |
293 | if (uval > 0xFFFFF) | |
294 | invarg("invalid Flowlabel", *argv); | |
295 | flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL; | |
296 | flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL; | |
297 | } | |
298 | } else if (strcmp(*argv, "dscp") == 0) { | |
299 | NEXT_ARG(); | |
300 | if (strcmp(*argv, "inherit") != 0) | |
301 | invarg("not inherit", *argv); | |
302 | flags |= IP6_TNL_F_RCV_DSCP_COPY; | |
0b2fbb73 TH |
303 | } else if (strcmp(*argv, "noencap") == 0) { |
304 | encaptype = TUNNEL_ENCAP_NONE; | |
305 | } else if (strcmp(*argv, "encap") == 0) { | |
306 | NEXT_ARG(); | |
307 | if (strcmp(*argv, "fou") == 0) | |
308 | encaptype = TUNNEL_ENCAP_FOU; | |
309 | else if (strcmp(*argv, "gue") == 0) | |
310 | encaptype = TUNNEL_ENCAP_GUE; | |
311 | else if (strcmp(*argv, "none") == 0) | |
312 | encaptype = TUNNEL_ENCAP_NONE; | |
313 | else | |
314 | invarg("Invalid encap type.", *argv); | |
315 | } else if (strcmp(*argv, "encap-sport") == 0) { | |
316 | NEXT_ARG(); | |
317 | if (strcmp(*argv, "auto") == 0) | |
318 | encapsport = 0; | |
319 | else if (get_u16(&encapsport, *argv, 0)) | |
320 | invarg("Invalid source port.", *argv); | |
321 | } else if (strcmp(*argv, "encap-dport") == 0) { | |
322 | NEXT_ARG(); | |
323 | if (get_u16(&encapdport, *argv, 0)) | |
324 | invarg("Invalid destination port.", *argv); | |
325 | } else if (strcmp(*argv, "encap-csum") == 0) { | |
326 | encapflags |= TUNNEL_ENCAP_FLAG_CSUM; | |
327 | } else if (strcmp(*argv, "noencap-csum") == 0) { | |
328 | encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM; | |
329 | } else if (strcmp(*argv, "encap-udp6-csum") == 0) { | |
330 | encapflags |= TUNNEL_ENCAP_FLAG_CSUM6; | |
331 | } else if (strcmp(*argv, "noencap-udp6-csum") == 0) { | |
332 | encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM6; | |
333 | } else if (strcmp(*argv, "encap-remcsum") == 0) { | |
334 | encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM; | |
335 | } else if (strcmp(*argv, "noencap-remcsum") == 0) { | |
336 | encapflags &= ~TUNNEL_ENCAP_FLAG_REMCSUM; | |
6231c5be WT |
337 | } else if (strcmp(*argv, "external") == 0) { |
338 | metadata = 1; | |
ad4b1425 CG |
339 | } else if (strcmp(*argv, "fwmark") == 0) { |
340 | NEXT_ARG(); | |
341 | if (strcmp(*argv, "inherit") == 0) { | |
342 | flags |= IP6_TNL_F_USE_ORIG_FWMARK; | |
343 | fwmark = 0; | |
344 | } else { | |
345 | if (get_u32(&fwmark, *argv, 0)) | |
346 | invarg("invalid fwmark\n", *argv); | |
347 | flags &= ~IP6_TNL_F_USE_ORIG_FWMARK; | |
348 | } | |
a11b7b71 ND |
349 | } else if (strcmp(*argv, "encaplimit") == 0) { |
350 | NEXT_ARG(); | |
351 | if (strcmp(*argv, "none") == 0) { | |
352 | flags |= IP6_TNL_F_IGN_ENCAP_LIMIT; | |
353 | } else { | |
354 | __u8 uval; | |
355 | ||
56708ae7 | 356 | if (get_u8(&uval, *argv, 0)) |
a11b7b71 ND |
357 | invarg("invalid ELIM", *argv); |
358 | encap_limit = uval; | |
359 | flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT; | |
360 | } | |
8595cc40 WT |
361 | } else if (strcmp(*argv, "erspan") == 0) { |
362 | NEXT_ARG(); | |
363 | if (get_u32(&erspan_idx, *argv, 0)) | |
364 | invarg("invalid erspan index\n", *argv); | |
365 | if (erspan_idx & ~((1<<20) - 1) || erspan_idx == 0) | |
366 | invarg("erspan index must be > 0 and <= 20-bit\n", *argv); | |
28976362 WT |
367 | } else if (strcmp(*argv, "erspan_ver") == 0) { |
368 | NEXT_ARG(); | |
369 | if (get_u8(&erspan_ver, *argv, 0)) | |
370 | invarg("invalid erspan version\n", *argv); | |
371 | if (erspan_ver != 1 && erspan_ver != 2) | |
372 | invarg("erspan version must be 1 or 2\n", *argv); | |
373 | } else if (strcmp(*argv, "erspan_dir") == 0) { | |
374 | NEXT_ARG(); | |
375 | if (matches(*argv, "ingress") == 0) | |
376 | erspan_dir = 0; | |
377 | else if (matches(*argv, "egress") == 0) | |
378 | erspan_dir = 1; | |
379 | else | |
380 | invarg("Invalid erspan direction.", *argv); | |
381 | } else if (strcmp(*argv, "erspan_hwid") == 0) { | |
382 | NEXT_ARG(); | |
383 | if (get_u16(&erspan_hwid, *argv, 0)) | |
384 | invarg("invalid erspan hwid\n", *argv); | |
ae91205c SP |
385 | } else { |
386 | gre_print_help(lu, argc, argv, stderr); | |
387 | return -1; | |
388 | } | |
af89576d | 389 | argc--; argv++; |
390 | } | |
391 | ||
6231c5be WT |
392 | if (!metadata) { |
393 | addattr32(n, 1024, IFLA_GRE_IKEY, ikey); | |
394 | addattr32(n, 1024, IFLA_GRE_OKEY, okey); | |
395 | addattr_l(n, 1024, IFLA_GRE_IFLAGS, &iflags, 2); | |
396 | addattr_l(n, 1024, IFLA_GRE_OFLAGS, &oflags, 2); | |
397 | addattr_l(n, 1024, IFLA_GRE_LOCAL, &laddr, sizeof(laddr)); | |
398 | addattr_l(n, 1024, IFLA_GRE_REMOTE, &raddr, sizeof(raddr)); | |
399 | if (link) | |
400 | addattr32(n, 1024, IFLA_GRE_LINK, link); | |
401 | addattr_l(n, 1024, IFLA_GRE_TTL, &hop_limit, 1); | |
402 | addattr_l(n, 1024, IFLA_GRE_ENCAP_LIMIT, &encap_limit, 1); | |
403 | addattr_l(n, 1024, IFLA_GRE_FLOWINFO, &flowinfo, 4); | |
404 | addattr32(n, 1024, IFLA_GRE_FLAGS, flags); | |
405 | addattr32(n, 1024, IFLA_GRE_FWMARK, fwmark); | |
28976362 WT |
406 | if (erspan_ver) { |
407 | addattr8(n, 1024, IFLA_GRE_ERSPAN_VER, erspan_ver); | |
408 | if (erspan_ver == 1 && erspan_idx != 0) { | |
de54cdd3 SP |
409 | addattr32(n, 1024, |
410 | IFLA_GRE_ERSPAN_INDEX, erspan_idx); | |
411 | } else if (erspan_ver == 2) { | |
412 | addattr8(n, 1024, | |
413 | IFLA_GRE_ERSPAN_DIR, erspan_dir); | |
414 | addattr16(n, 1024, | |
415 | IFLA_GRE_ERSPAN_HWID, erspan_hwid); | |
28976362 WT |
416 | } |
417 | } | |
6231c5be WT |
418 | addattr16(n, 1024, IFLA_GRE_ENCAP_TYPE, encaptype); |
419 | addattr16(n, 1024, IFLA_GRE_ENCAP_FLAGS, encapflags); | |
420 | addattr16(n, 1024, IFLA_GRE_ENCAP_SPORT, htons(encapsport)); | |
421 | addattr16(n, 1024, IFLA_GRE_ENCAP_DPORT, htons(encapdport)); | |
422 | } else { | |
423 | addattr_l(n, 1024, IFLA_GRE_COLLECT_METADATA, NULL, 0); | |
424 | } | |
0b2fbb73 | 425 | |
af89576d | 426 | return 0; |
427 | } | |
428 | ||
429 | static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) | |
430 | { | |
af89576d | 431 | char s2[64]; |
56f5daac SH |
432 | unsigned int iflags = 0; |
433 | unsigned int oflags = 0; | |
434 | unsigned int flags = 0; | |
090524f8 | 435 | __u32 flowinfo = 0; |
375560c4 | 436 | __u8 ttl = 0; |
af89576d | 437 | |
438 | if (!tb) | |
439 | return; | |
440 | ||
6231c5be | 441 | if (tb[IFLA_GRE_COLLECT_METADATA]) { |
00ff4b8e | 442 | print_bool(PRINT_ANY, "external", "external", true); |
6231c5be WT |
443 | return; |
444 | } | |
445 | ||
af89576d | 446 | if (tb[IFLA_GRE_FLAGS]) |
447 | flags = rta_getattr_u32(tb[IFLA_GRE_FLAGS]); | |
448 | ||
449 | if (tb[IFLA_GRE_FLOWINFO]) | |
35893864 | 450 | flowinfo = rta_getattr_u32(tb[IFLA_GRE_FLOWINFO]); |
af89576d | 451 | |
b761fc41 SP |
452 | tnl_print_endpoint("remote", tb[IFLA_GRE_REMOTE], AF_INET6); |
453 | tnl_print_endpoint("local", tb[IFLA_GRE_LOCAL], AF_INET6); | |
af89576d | 454 | |
45d3a6ef | 455 | if (tb[IFLA_GRE_LINK]) { |
56f5daac | 456 | unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]); |
af89576d | 457 | |
45d3a6ef SP |
458 | if (link) { |
459 | print_string(PRINT_ANY, "link", "dev %s ", | |
460 | ll_index_to_name(link)); | |
461 | } | |
af89576d | 462 | } |
463 | ||
375560c4 SP |
464 | if (tb[IFLA_GRE_TTL]) |
465 | ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]); | |
466 | if (is_json_context() || ttl) | |
467 | print_uint(PRINT_ANY, "ttl", "hoplimit %u ", ttl); | |
468 | else | |
469 | print_string(PRINT_FP, NULL, "hoplimit %s ", "inherit"); | |
6856fb65 | 470 | |
e3945d92 | 471 | if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT) { |
6856fb65 JF |
472 | print_bool(PRINT_ANY, |
473 | "ip6_tnl_f_ign_encap_limit", | |
474 | "encaplimit none ", | |
475 | true); | |
e3945d92 SP |
476 | } else if (tb[IFLA_GRE_ENCAP_LIMIT]) { |
477 | __u8 val = rta_getattr_u8(tb[IFLA_GRE_ENCAP_LIMIT]); | |
af89576d | 478 | |
e3945d92 | 479 | print_uint(PRINT_ANY, "encap_limit", "encaplimit %u ", val); |
af89576d | 480 | } |
481 | ||
2e706e12 | 482 | if (flags & IP6_TNL_F_USE_ORIG_TCLASS) { |
6856fb65 | 483 | print_bool(PRINT_ANY, |
2e706e12 SH |
484 | "ip6_tnl_f_use_orig_tclass", |
485 | "tclass inherit ", | |
6856fb65 | 486 | true); |
090524f8 SP |
487 | } else if (tb[IFLA_GRE_FLOWINFO]) { |
488 | __u32 val = ntohl(flowinfo & IP6_FLOWINFO_TCLASS) >> 20; | |
6856fb65 | 489 | |
090524f8 SP |
490 | snprintf(s2, sizeof(s2), "0x%02x", val); |
491 | print_string(PRINT_ANY, "tclass", "tclass %s ", s2); | |
6856fb65 | 492 | } |
af89576d | 493 | |
763cf495 | 494 | if (flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) { |
2e706e12 | 495 | print_bool(PRINT_ANY, |
763cf495 SP |
496 | "ip6_tnl_f_use_orig_flowlabel", |
497 | "flowlabel inherit ", | |
2e706e12 | 498 | true); |
763cf495 SP |
499 | } else if (tb[IFLA_GRE_FLOWINFO]) { |
500 | __u32 val = ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL); | |
2e706e12 | 501 | |
763cf495 SP |
502 | snprintf(s2, sizeof(s2), "0x%05x", val); |
503 | print_string(PRINT_ANY, "flowlabel", "flowlabel %s ", s2); | |
2e706e12 | 504 | } |
af89576d | 505 | |
506 | if (flags & IP6_TNL_F_RCV_DSCP_COPY) | |
6856fb65 JF |
507 | print_bool(PRINT_ANY, |
508 | "ip6_tnl_f_rcv_dscp_copy", | |
509 | "dscp inherit ", | |
510 | true); | |
af89576d | 511 | |
512 | if (tb[IFLA_GRE_IFLAGS]) | |
513 | iflags = rta_getattr_u16(tb[IFLA_GRE_IFLAGS]); | |
514 | ||
515 | if (tb[IFLA_GRE_OFLAGS]) | |
516 | oflags = rta_getattr_u16(tb[IFLA_GRE_OFLAGS]); | |
517 | ||
518 | if ((iflags & GRE_KEY) && tb[IFLA_GRE_IKEY]) { | |
519 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_IKEY]), s2, sizeof(s2)); | |
6856fb65 | 520 | print_string(PRINT_ANY, "ikey", "ikey %s ", s2); |
af89576d | 521 | } |
522 | ||
523 | if ((oflags & GRE_KEY) && tb[IFLA_GRE_OKEY]) { | |
524 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_OKEY]), s2, sizeof(s2)); | |
6856fb65 | 525 | print_string(PRINT_ANY, "okey", "okey %s ", s2); |
af89576d | 526 | } |
527 | ||
528 | if (iflags & GRE_SEQ) | |
6856fb65 | 529 | print_bool(PRINT_ANY, "iseq", "iseq ", true); |
af89576d | 530 | if (oflags & GRE_SEQ) |
6856fb65 | 531 | print_bool(PRINT_ANY, "oseq", "oseq ", true); |
af89576d | 532 | if (iflags & GRE_CSUM) |
6856fb65 | 533 | print_bool(PRINT_ANY, "icsum", "icsum ", true); |
af89576d | 534 | if (oflags & GRE_CSUM) |
6856fb65 | 535 | print_bool(PRINT_ANY, "ocsum", "ocsum ", true); |
0b2fbb73 | 536 | |
e97ad3d2 | 537 | if (flags & IP6_TNL_F_USE_ORIG_FWMARK) { |
6856fb65 JF |
538 | print_bool(PRINT_ANY, |
539 | "ip6_tnl_f_use_orig_fwmark", | |
540 | "fwmark inherit ", | |
541 | true); | |
e97ad3d2 | 542 | } else if (tb[IFLA_GRE_FWMARK]) { |
6856fb65 JF |
543 | __u32 fwmark = rta_getattr_u32(tb[IFLA_GRE_FWMARK]); |
544 | ||
545 | if (fwmark) { | |
e97ad3d2 SP |
546 | print_0xhex(PRINT_ANY, |
547 | "fwmark", "fwmark 0x%x ", fwmark); | |
6856fb65 JF |
548 | } |
549 | } | |
ad4b1425 | 550 | |
8595cc40 WT |
551 | if (tb[IFLA_GRE_ERSPAN_INDEX]) { |
552 | __u32 erspan_idx = rta_getattr_u32(tb[IFLA_GRE_ERSPAN_INDEX]); | |
3cb92eb9 SP |
553 | print_uint(PRINT_ANY, |
554 | "erspan_index", "erspan_index %u ", erspan_idx); | |
28976362 WT |
555 | } |
556 | ||
557 | if (tb[IFLA_GRE_ERSPAN_VER]) { | |
558 | __u8 erspan_ver = rta_getattr_u8(tb[IFLA_GRE_ERSPAN_VER]); | |
559 | ||
560 | print_uint(PRINT_ANY, "erspan_ver", "erspan_ver %u ", erspan_ver); | |
561 | } | |
562 | ||
563 | if (tb[IFLA_GRE_ERSPAN_DIR]) { | |
564 | __u8 erspan_dir = rta_getattr_u8(tb[IFLA_GRE_ERSPAN_DIR]); | |
565 | ||
566 | if (erspan_dir == 0) | |
567 | print_string(PRINT_ANY, "erspan_dir", | |
568 | "erspan_dir ingress ", NULL); | |
569 | else | |
570 | print_string(PRINT_ANY, "erspan_dir", | |
571 | "erspan_dir egress ", NULL); | |
572 | } | |
573 | ||
574 | if (tb[IFLA_GRE_ERSPAN_HWID]) { | |
575 | __u16 erspan_hwid = rta_getattr_u16(tb[IFLA_GRE_ERSPAN_HWID]); | |
576 | ||
577 | print_hex(PRINT_ANY, "erspan_hwid", "erspan_hwid 0x%x ", erspan_hwid); | |
8595cc40 WT |
578 | } |
579 | ||
bad76e6b SP |
580 | tnl_print_encap(tb, |
581 | IFLA_GRE_ENCAP_TYPE, | |
582 | IFLA_GRE_ENCAP_FLAGS, | |
583 | IFLA_GRE_ENCAP_SPORT, | |
584 | IFLA_GRE_ENCAP_DPORT); | |
af89576d | 585 | } |
586 | ||
587 | struct link_util ip6gre_link_util = { | |
588 | .id = "ip6gre", | |
589 | .maxattr = IFLA_GRE_MAX, | |
590 | .parse_opt = gre_parse_opt, | |
591 | .print_opt = gre_print_opt, | |
561e650e | 592 | .print_help = gre_print_help, |
af89576d | 593 | }; |
594 | ||
595 | struct link_util ip6gretap_link_util = { | |
596 | .id = "ip6gretap", | |
597 | .maxattr = IFLA_GRE_MAX, | |
598 | .parse_opt = gre_parse_opt, | |
599 | .print_opt = gre_print_opt, | |
561e650e | 600 | .print_help = gre_print_help, |
af89576d | 601 | }; |
8595cc40 WT |
602 | |
603 | struct link_util ip6erspan_link_util = { | |
604 | .id = "ip6erspan", | |
605 | .maxattr = IFLA_GRE_MAX, | |
606 | .parse_opt = gre_parse_opt, | |
607 | .print_opt = gre_print_opt, | |
608 | .print_help = gre_print_help, | |
609 | }; |