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