]>
Commit | Line | Data |
---|---|---|
237d9e82 HX |
1 | /* |
2 | * link_gre.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: Herbert Xu <herbert@gondor.apana.org.au> | |
10 | * | |
11 | */ | |
12 | ||
13 | #include <string.h> | |
14 | #include <net/if.h> | |
237d9e82 HX |
15 | #include <sys/types.h> |
16 | #include <sys/socket.h> | |
17 | #include <arpa/inet.h> | |
18 | ||
1957a322 SH |
19 | #include <linux/ip.h> |
20 | #include <linux/if_tunnel.h> | |
237d9e82 HX |
21 | #include "rt_names.h" |
22 | #include "utils.h" | |
23 | #include "ip_common.h" | |
24 | #include "tunnel.h" | |
25 | ||
561e650e | 26 | static void print_usage(FILE *f) |
27 | { | |
8b471354 | 28 | fprintf(f, |
9a1381d5 | 29 | "Usage: ... { gre | gretap | erspan } [ remote ADDR ]\n" |
8b471354 PS |
30 | " [ local ADDR ]\n" |
31 | " [ [i|o]seq ]\n" | |
32 | " [ [i|o]key KEY ]\n" | |
33 | " [ [i|o]csum ]\n" | |
34 | " [ ttl TTL ]\n" | |
35 | " [ tos TOS ]\n" | |
36 | " [ [no]pmtudisc ]\n" | |
adbb2965 | 37 | " [ [no]ignore-df ]\n" |
8b471354 PS |
38 | " [ dev PHYS_DEV ]\n" |
39 | " [ noencap ]\n" | |
40 | " [ encap { fou | gue | none } ]\n" | |
41 | " [ encap-sport PORT ]\n" | |
42 | " [ encap-dport PORT ]\n" | |
43 | " [ [no]encap-csum ]\n" | |
44 | " [ [no]encap-csum6 ]\n" | |
45 | " [ [no]encap-remcsum ]\n" | |
ad4b1425 | 46 | " [ fwmark MARK ]\n" |
9a1381d5 | 47 | " [ erspan IDX ]\n" |
8b471354 PS |
48 | "\n" |
49 | "Where: ADDR := { IP_ADDRESS | any }\n" | |
50 | " TOS := { NUMBER | inherit }\n" | |
51 | " TTL := { 1..255 | inherit }\n" | |
52 | " KEY := { DOTTED_QUAD | NUMBER }\n" | |
ad4b1425 | 53 | " MARK := { 0x0..0xffffffff }\n" |
8b471354 | 54 | ); |
561e650e | 55 | } |
56 | ||
237d9e82 HX |
57 | static void usage(void) __attribute__((noreturn)); |
58 | static void usage(void) | |
59 | { | |
561e650e | 60 | print_usage(stderr); |
237d9e82 HX |
61 | exit(-1); |
62 | } | |
63 | ||
64 | static int gre_parse_opt(struct link_util *lu, int argc, char **argv, | |
65 | struct nlmsghdr *n) | |
66 | { | |
d17b136f | 67 | struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1); |
72c771b2 HX |
68 | struct { |
69 | struct nlmsghdr n; | |
70 | struct ifinfomsg i; | |
d17b136f PS |
71 | } req = { |
72 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)), | |
73 | .n.nlmsg_flags = NLM_F_REQUEST, | |
74 | .n.nlmsg_type = RTM_GETLINK, | |
75 | .i.ifi_family = preferred_family, | |
76 | .i.ifi_index = ifi->ifi_index, | |
77 | }; | |
86bf43c7 | 78 | struct nlmsghdr *answer = NULL; |
72c771b2 HX |
79 | struct rtattr *tb[IFLA_MAX + 1]; |
80 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; | |
81 | struct rtattr *greinfo[IFLA_GRE_MAX + 1]; | |
237d9e82 HX |
82 | __u16 iflags = 0; |
83 | __u16 oflags = 0; | |
56f5daac SH |
84 | unsigned int ikey = 0; |
85 | unsigned int okey = 0; | |
86 | unsigned int saddr = 0; | |
87 | unsigned int daddr = 0; | |
88 | unsigned int link = 0; | |
72c771b2 HX |
89 | __u8 pmtudisc = 1; |
90 | __u8 ttl = 0; | |
91 | __u8 tos = 0; | |
92 | int len; | |
80c24b09 TH |
93 | __u16 encaptype = 0; |
94 | __u16 encapflags = 0; | |
95 | __u16 encapsport = 0; | |
96 | __u16 encapdport = 0; | |
926b39e1 | 97 | __u8 metadata = 0; |
adbb2965 | 98 | __u8 ignore_df = 0; |
ad4b1425 | 99 | __u32 fwmark = 0; |
9a1381d5 | 100 | __u32 erspan_idx = 0; |
72c771b2 HX |
101 | |
102 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { | |
86bf43c7 | 103 | if (rtnl_talk(&rth, &req.n, &answer) < 0) { |
72c771b2 HX |
104 | get_failed: |
105 | fprintf(stderr, | |
106 | "Failed to get existing tunnel info.\n"); | |
86bf43c7 | 107 | free(answer); |
72c771b2 HX |
108 | return -1; |
109 | } | |
110 | ||
86bf43c7 | 111 | len = answer->nlmsg_len; |
72c771b2 HX |
112 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
113 | if (len < 0) | |
114 | goto get_failed; | |
115 | ||
86bf43c7 | 116 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(NLMSG_DATA(answer)), len); |
72c771b2 HX |
117 | |
118 | if (!tb[IFLA_LINKINFO]) | |
119 | goto get_failed; | |
120 | ||
121 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
122 | ||
123 | if (!linkinfo[IFLA_INFO_DATA]) | |
124 | goto get_failed; | |
125 | ||
126 | parse_rtattr_nested(greinfo, IFLA_GRE_MAX, | |
127 | linkinfo[IFLA_INFO_DATA]); | |
128 | ||
129 | if (greinfo[IFLA_GRE_IKEY]) | |
ff24746c | 130 | ikey = rta_getattr_u32(greinfo[IFLA_GRE_IKEY]); |
72c771b2 HX |
131 | |
132 | if (greinfo[IFLA_GRE_OKEY]) | |
ff24746c | 133 | okey = rta_getattr_u32(greinfo[IFLA_GRE_OKEY]); |
72c771b2 HX |
134 | |
135 | if (greinfo[IFLA_GRE_IFLAGS]) | |
ff24746c | 136 | iflags = rta_getattr_u16(greinfo[IFLA_GRE_IFLAGS]); |
72c771b2 HX |
137 | |
138 | if (greinfo[IFLA_GRE_OFLAGS]) | |
ff24746c | 139 | oflags = rta_getattr_u16(greinfo[IFLA_GRE_OFLAGS]); |
72c771b2 HX |
140 | |
141 | if (greinfo[IFLA_GRE_LOCAL]) | |
ff24746c | 142 | saddr = rta_getattr_u32(greinfo[IFLA_GRE_LOCAL]); |
72c771b2 HX |
143 | |
144 | if (greinfo[IFLA_GRE_REMOTE]) | |
ff24746c | 145 | daddr = rta_getattr_u32(greinfo[IFLA_GRE_REMOTE]); |
72c771b2 HX |
146 | |
147 | if (greinfo[IFLA_GRE_PMTUDISC]) | |
ff24746c | 148 | pmtudisc = rta_getattr_u8( |
72c771b2 HX |
149 | greinfo[IFLA_GRE_PMTUDISC]); |
150 | ||
151 | if (greinfo[IFLA_GRE_TTL]) | |
ff24746c | 152 | ttl = rta_getattr_u8(greinfo[IFLA_GRE_TTL]); |
72c771b2 HX |
153 | |
154 | if (greinfo[IFLA_GRE_TOS]) | |
ff24746c | 155 | tos = rta_getattr_u8(greinfo[IFLA_GRE_TOS]); |
72c771b2 HX |
156 | |
157 | if (greinfo[IFLA_GRE_LINK]) | |
a6addd5c | 158 | link = rta_getattr_u32(greinfo[IFLA_GRE_LINK]); |
80c24b09 TH |
159 | |
160 | if (greinfo[IFLA_GRE_ENCAP_TYPE]) | |
161 | encaptype = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_TYPE]); | |
162 | if (greinfo[IFLA_GRE_ENCAP_FLAGS]) | |
163 | encapflags = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_FLAGS]); | |
164 | if (greinfo[IFLA_GRE_ENCAP_SPORT]) | |
165 | encapsport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_SPORT]); | |
166 | if (greinfo[IFLA_GRE_ENCAP_DPORT]) | |
167 | encapdport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_DPORT]); | |
926b39e1 PA |
168 | |
169 | if (greinfo[IFLA_GRE_COLLECT_METADATA]) | |
170 | metadata = 1; | |
ad4b1425 | 171 | |
adbb2965 PP |
172 | if (greinfo[IFLA_GRE_IGNORE_DF]) |
173 | ignore_df = | |
174 | !!rta_getattr_u8(greinfo[IFLA_GRE_IGNORE_DF]); | |
175 | ||
ad4b1425 CG |
176 | if (greinfo[IFLA_GRE_FWMARK]) |
177 | fwmark = rta_getattr_u32(greinfo[IFLA_GRE_FWMARK]); | |
9a1381d5 WT |
178 | |
179 | if (greinfo[IFLA_GRE_ERSPAN_INDEX]) | |
180 | erspan_idx = rta_getattr_u32(greinfo[IFLA_GRE_ERSPAN_INDEX]); | |
86bf43c7 HL |
181 | |
182 | free(answer); | |
72c771b2 | 183 | } |
237d9e82 HX |
184 | |
185 | while (argc > 0) { | |
186 | if (!matches(*argv, "key")) { | |
56f5daac | 187 | unsigned int uval; |
237d9e82 HX |
188 | |
189 | NEXT_ARG(); | |
190 | iflags |= GRE_KEY; | |
191 | oflags |= GRE_KEY; | |
192 | if (strchr(*argv, '.')) | |
193 | uval = get_addr32(*argv); | |
194 | else { | |
195 | if (get_unsigned(&uval, *argv, 0) < 0) { | |
196 | fprintf(stderr, | |
14645ec2 | 197 | "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv); |
237d9e82 HX |
198 | exit(-1); |
199 | } | |
200 | uval = htonl(uval); | |
201 | } | |
202 | ||
203 | ikey = okey = uval; | |
204 | } else if (!matches(*argv, "ikey")) { | |
56f5daac | 205 | unsigned int uval; |
237d9e82 HX |
206 | |
207 | NEXT_ARG(); | |
208 | iflags |= GRE_KEY; | |
209 | if (strchr(*argv, '.')) | |
210 | uval = get_addr32(*argv); | |
211 | else { | |
56f5daac | 212 | if (get_unsigned(&uval, *argv, 0) < 0) { |
14645ec2 | 213 | fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv); |
237d9e82 HX |
214 | exit(-1); |
215 | } | |
216 | uval = htonl(uval); | |
217 | } | |
218 | ikey = uval; | |
219 | } else if (!matches(*argv, "okey")) { | |
56f5daac | 220 | unsigned int uval; |
237d9e82 HX |
221 | |
222 | NEXT_ARG(); | |
223 | oflags |= GRE_KEY; | |
224 | if (strchr(*argv, '.')) | |
225 | uval = get_addr32(*argv); | |
226 | else { | |
56f5daac | 227 | if (get_unsigned(&uval, *argv, 0) < 0) { |
14645ec2 | 228 | fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv); |
237d9e82 HX |
229 | exit(-1); |
230 | } | |
231 | uval = htonl(uval); | |
232 | } | |
233 | okey = uval; | |
234 | } else if (!matches(*argv, "seq")) { | |
235 | iflags |= GRE_SEQ; | |
236 | oflags |= GRE_SEQ; | |
237 | } else if (!matches(*argv, "iseq")) { | |
238 | iflags |= GRE_SEQ; | |
239 | } else if (!matches(*argv, "oseq")) { | |
240 | oflags |= GRE_SEQ; | |
241 | } else if (!matches(*argv, "csum")) { | |
242 | iflags |= GRE_CSUM; | |
243 | oflags |= GRE_CSUM; | |
244 | } else if (!matches(*argv, "icsum")) { | |
245 | iflags |= GRE_CSUM; | |
246 | } else if (!matches(*argv, "ocsum")) { | |
247 | oflags |= GRE_CSUM; | |
248 | } else if (!matches(*argv, "nopmtudisc")) { | |
72c771b2 | 249 | pmtudisc = 0; |
237d9e82 | 250 | } else if (!matches(*argv, "pmtudisc")) { |
72c771b2 | 251 | pmtudisc = 1; |
237d9e82 HX |
252 | } else if (!matches(*argv, "remote")) { |
253 | NEXT_ARG(); | |
57daab1e | 254 | daddr = get_addr32(*argv); |
237d9e82 HX |
255 | } else if (!matches(*argv, "local")) { |
256 | NEXT_ARG(); | |
57daab1e | 257 | saddr = get_addr32(*argv); |
237d9e82 | 258 | } else if (!matches(*argv, "dev")) { |
237d9e82 | 259 | NEXT_ARG(); |
ea71beac | 260 | link = if_nametoindex(*argv); |
0cb6bb51 CW |
261 | if (link == 0) { |
262 | fprintf(stderr, "Cannot find device \"%s\"\n", | |
263 | *argv); | |
237d9e82 | 264 | exit(-1); |
0cb6bb51 | 265 | } |
237d9e82 HX |
266 | } else if (!matches(*argv, "ttl") || |
267 | !matches(*argv, "hoplimit")) { | |
56f5daac | 268 | unsigned int uval; |
237d9e82 HX |
269 | |
270 | NEXT_ARG(); | |
271 | if (strcmp(*argv, "inherit") != 0) { | |
272 | if (get_unsigned(&uval, *argv, 0)) | |
273 | invarg("invalid TTL\n", *argv); | |
274 | if (uval > 255) | |
275 | invarg("TTL must be <= 255\n", *argv); | |
276 | ttl = uval; | |
079e6781 RS |
277 | } else |
278 | ttl = 0; | |
237d9e82 HX |
279 | } else if (!matches(*argv, "tos") || |
280 | !matches(*argv, "tclass") || | |
281 | !matches(*argv, "dsfield")) { | |
282 | __u32 uval; | |
237d9e82 HX |
283 | |
284 | NEXT_ARG(); | |
285 | if (strcmp(*argv, "inherit") != 0) { | |
286 | if (rtnl_dsfield_a2n(&uval, *argv)) | |
287 | invarg("bad TOS value", *argv); | |
288 | tos = uval; | |
289 | } else | |
290 | tos = 1; | |
80c24b09 TH |
291 | } else if (strcmp(*argv, "noencap") == 0) { |
292 | encaptype = TUNNEL_ENCAP_NONE; | |
293 | } else if (strcmp(*argv, "encap") == 0) { | |
294 | NEXT_ARG(); | |
295 | if (strcmp(*argv, "fou") == 0) | |
296 | encaptype = TUNNEL_ENCAP_FOU; | |
297 | else if (strcmp(*argv, "gue") == 0) | |
298 | encaptype = TUNNEL_ENCAP_GUE; | |
299 | else if (strcmp(*argv, "none") == 0) | |
300 | encaptype = TUNNEL_ENCAP_NONE; | |
301 | else | |
302 | invarg("Invalid encap type.", *argv); | |
303 | } else if (strcmp(*argv, "encap-sport") == 0) { | |
304 | NEXT_ARG(); | |
305 | if (strcmp(*argv, "auto") == 0) | |
306 | encapsport = 0; | |
307 | else if (get_u16(&encapsport, *argv, 0)) | |
308 | invarg("Invalid source port.", *argv); | |
309 | } else if (strcmp(*argv, "encap-dport") == 0) { | |
310 | NEXT_ARG(); | |
311 | if (get_u16(&encapdport, *argv, 0)) | |
312 | invarg("Invalid destination port.", *argv); | |
313 | } else if (strcmp(*argv, "encap-csum") == 0) { | |
314 | encapflags |= TUNNEL_ENCAP_FLAG_CSUM; | |
315 | } else if (strcmp(*argv, "noencap-csum") == 0) { | |
316 | encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM; | |
317 | } else if (strcmp(*argv, "encap-udp6-csum") == 0) { | |
318 | encapflags |= TUNNEL_ENCAP_FLAG_CSUM6; | |
319 | } else if (strcmp(*argv, "noencap-udp6-csum") == 0) { | |
320 | encapflags |= ~TUNNEL_ENCAP_FLAG_CSUM6; | |
858dbb20 TH |
321 | } else if (strcmp(*argv, "encap-remcsum") == 0) { |
322 | encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM; | |
323 | } else if (strcmp(*argv, "noencap-remcsum") == 0) { | |
324 | encapflags |= ~TUNNEL_ENCAP_FLAG_REMCSUM; | |
926b39e1 PA |
325 | } else if (strcmp(*argv, "external") == 0) { |
326 | metadata = 1; | |
adbb2965 PP |
327 | } else if (strcmp(*argv, "ignore-df") == 0) { |
328 | ignore_df = 1; | |
329 | } else if (strcmp(*argv, "noignore-df") == 0) { | |
330 | /* | |
331 | *only the lsb is significant, use 2 for presence | |
332 | */ | |
333 | ignore_df = 2; | |
ad4b1425 CG |
334 | } else if (strcmp(*argv, "fwmark") == 0) { |
335 | NEXT_ARG(); | |
336 | if (get_u32(&fwmark, *argv, 0)) | |
337 | invarg("invalid fwmark\n", *argv); | |
9a1381d5 WT |
338 | } else if (strcmp(*argv, "erspan") == 0) { |
339 | NEXT_ARG(); | |
340 | if (get_u32(&erspan_idx, *argv, 0)) | |
341 | invarg("invalid erspan index\n", *argv); | |
342 | if (erspan_idx & ~((1<<20) - 1) || erspan_idx == 0) | |
343 | invarg("erspan index must be > 0 and <= 20-bit\n", *argv); | |
0612519e | 344 | } else |
237d9e82 HX |
345 | usage(); |
346 | argc--; argv++; | |
347 | } | |
348 | ||
349 | if (!ikey && IN_MULTICAST(ntohl(daddr))) { | |
350 | ikey = daddr; | |
351 | iflags |= GRE_KEY; | |
352 | } | |
353 | if (!okey && IN_MULTICAST(ntohl(daddr))) { | |
354 | okey = daddr; | |
355 | oflags |= GRE_KEY; | |
356 | } | |
357 | if (IN_MULTICAST(ntohl(daddr)) && !saddr) { | |
14645ec2 | 358 | fprintf(stderr, "A broadcast tunnel requires a source address.\n"); |
237d9e82 HX |
359 | return -1; |
360 | } | |
361 | ||
df217d5d JB |
362 | if (!metadata) { |
363 | addattr32(n, 1024, IFLA_GRE_IKEY, ikey); | |
364 | addattr32(n, 1024, IFLA_GRE_OKEY, okey); | |
365 | addattr_l(n, 1024, IFLA_GRE_IFLAGS, &iflags, 2); | |
366 | addattr_l(n, 1024, IFLA_GRE_OFLAGS, &oflags, 2); | |
367 | addattr_l(n, 1024, IFLA_GRE_LOCAL, &saddr, 4); | |
368 | addattr_l(n, 1024, IFLA_GRE_REMOTE, &daddr, 4); | |
369 | addattr_l(n, 1024, IFLA_GRE_PMTUDISC, &pmtudisc, 1); | |
370 | if (link) | |
371 | addattr32(n, 1024, IFLA_GRE_LINK, link); | |
372 | addattr_l(n, 1024, IFLA_GRE_TTL, &ttl, 1); | |
373 | addattr_l(n, 1024, IFLA_GRE_TOS, &tos, 1); | |
ad4b1425 | 374 | addattr32(n, 1024, IFLA_GRE_FWMARK, fwmark); |
9a1381d5 WT |
375 | if (erspan_idx != 0) |
376 | addattr32(n, 1024, IFLA_GRE_ERSPAN_INDEX, erspan_idx); | |
df217d5d JB |
377 | } else { |
378 | addattr_l(n, 1024, IFLA_GRE_COLLECT_METADATA, NULL, 0); | |
379 | } | |
237d9e82 | 380 | |
80c24b09 TH |
381 | addattr16(n, 1024, IFLA_GRE_ENCAP_TYPE, encaptype); |
382 | addattr16(n, 1024, IFLA_GRE_ENCAP_FLAGS, encapflags); | |
383 | addattr16(n, 1024, IFLA_GRE_ENCAP_SPORT, htons(encapsport)); | |
384 | addattr16(n, 1024, IFLA_GRE_ENCAP_DPORT, htons(encapdport)); | |
385 | ||
adbb2965 PP |
386 | if (ignore_df) |
387 | addattr8(n, 1024, IFLA_GRE_IGNORE_DF, ignore_df & 1); | |
388 | ||
237d9e82 HX |
389 | return 0; |
390 | } | |
391 | ||
7c337e2c | 392 | static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) |
237d9e82 | 393 | { |
237d9e82 HX |
394 | char s2[64]; |
395 | const char *local = "any"; | |
396 | const char *remote = "any"; | |
56f5daac SH |
397 | unsigned int iflags = 0; |
398 | unsigned int oflags = 0; | |
237d9e82 | 399 | |
237d9e82 | 400 | if (tb[IFLA_GRE_REMOTE]) { |
56f5daac | 401 | unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]); |
237d9e82 HX |
402 | |
403 | if (addr) | |
a418e451 | 404 | remote = format_host(AF_INET, 4, &addr); |
237d9e82 HX |
405 | } |
406 | ||
e2d45883 | 407 | print_string(PRINT_ANY, "remote", "remote %s ", remote); |
237d9e82 HX |
408 | |
409 | if (tb[IFLA_GRE_LOCAL]) { | |
56f5daac | 410 | unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_LOCAL]); |
237d9e82 HX |
411 | |
412 | if (addr) | |
a418e451 | 413 | local = format_host(AF_INET, 4, &addr); |
237d9e82 HX |
414 | } |
415 | ||
e2d45883 | 416 | print_string(PRINT_ANY, "local", "local %s ", local); |
237d9e82 | 417 | |
ff24746c | 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]); |
ea71beac | 420 | const char *n = if_indextoname(link, s2); |
237d9e82 HX |
421 | |
422 | if (n) | |
e2d45883 | 423 | print_string(PRINT_ANY, "link", "dev %s ", n); |
237d9e82 | 424 | else |
e2d45883 | 425 | print_uint(PRINT_ANY, "link_index", "dev %u ", link); |
237d9e82 HX |
426 | } |
427 | ||
e2d45883 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", "ttl %d ", ttl); | |
433 | else | |
434 | print_int(PRINT_JSON, "ttl", NULL, ttl); | |
435 | } else { | |
436 | print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); | |
437 | } | |
237d9e82 | 438 | |
ff24746c SH |
439 | if (tb[IFLA_GRE_TOS] && rta_getattr_u8(tb[IFLA_GRE_TOS])) { |
440 | int tos = rta_getattr_u8(tb[IFLA_GRE_TOS]); | |
237d9e82 | 441 | |
e2d45883 JF |
442 | if (is_json_context()) { |
443 | SPRINT_BUF(b1); | |
444 | ||
445 | snprintf(b1, sizeof(b1), "0x%x", tos); | |
446 | print_string(PRINT_JSON, "tos", NULL, b1); | |
447 | } else { | |
448 | fputs("tos ", f); | |
449 | if (tos == 1) | |
450 | fputs("inherit ", f); | |
451 | else | |
452 | fprintf(f, "0x%x ", tos); | |
453 | } | |
237d9e82 HX |
454 | } |
455 | ||
e2d45883 JF |
456 | if (tb[IFLA_GRE_PMTUDISC]) { |
457 | if (!rta_getattr_u8(tb[IFLA_GRE_PMTUDISC])) | |
458 | print_bool(PRINT_ANY, "pmtudisc", "nopmtudisc ", false); | |
459 | else | |
460 | print_bool(PRINT_JSON, "pmtudisc", NULL, true); | |
461 | } | |
237d9e82 HX |
462 | |
463 | if (tb[IFLA_GRE_IFLAGS]) | |
ff24746c | 464 | iflags = rta_getattr_u16(tb[IFLA_GRE_IFLAGS]); |
237d9e82 HX |
465 | |
466 | if (tb[IFLA_GRE_OFLAGS]) | |
ff24746c | 467 | oflags = rta_getattr_u16(tb[IFLA_GRE_OFLAGS]); |
237d9e82 | 468 | |
71816553 | 469 | if ((iflags & GRE_KEY) && tb[IFLA_GRE_IKEY]) { |
237d9e82 | 470 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_IKEY]), s2, sizeof(s2)); |
e2d45883 | 471 | print_string(PRINT_ANY, "ikey", "ikey %s ", s2); |
237d9e82 HX |
472 | } |
473 | ||
71816553 | 474 | if ((oflags & GRE_KEY) && tb[IFLA_GRE_OKEY]) { |
237d9e82 | 475 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_OKEY]), s2, sizeof(s2)); |
e2d45883 | 476 | print_string(PRINT_ANY, "okey", "okey %s ", s2); |
237d9e82 HX |
477 | } |
478 | ||
479 | if (iflags & GRE_SEQ) | |
e2d45883 | 480 | print_bool(PRINT_ANY, "iseq", "iseq ", true); |
237d9e82 | 481 | if (oflags & GRE_SEQ) |
e2d45883 | 482 | print_bool(PRINT_ANY, "oseq", "oseq ", true); |
237d9e82 | 483 | if (iflags & GRE_CSUM) |
e2d45883 | 484 | print_bool(PRINT_ANY, "icsum", "icsum ", true); |
237d9e82 | 485 | if (oflags & GRE_CSUM) |
e2d45883 JF |
486 | print_bool(PRINT_ANY, "ocsum", "ocsum ", true); |
487 | ||
488 | if (tb[IFLA_GRE_FWMARK]) { | |
489 | __u32 fwmark = rta_getattr_u32(tb[IFLA_GRE_FWMARK]); | |
490 | ||
491 | if (fwmark) { | |
492 | snprintf(s2, sizeof(s2), "0x%x", fwmark); | |
ad4b1425 | 493 | |
e2d45883 JF |
494 | print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2); |
495 | } | |
ad4b1425 | 496 | } |
7c337e2c | 497 | } |
80c24b09 | 498 | |
7c337e2c JB |
499 | static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) |
500 | { | |
501 | if (!tb) | |
502 | return; | |
503 | ||
504 | if (!tb[IFLA_GRE_COLLECT_METADATA]) | |
505 | gre_print_direct_opt(f, tb); | |
506 | else | |
e2d45883 | 507 | print_bool(PRINT_ANY, "external", "external ", true); |
926b39e1 | 508 | |
adbb2965 | 509 | if (tb[IFLA_GRE_IGNORE_DF] && rta_getattr_u8(tb[IFLA_GRE_IGNORE_DF])) |
e2d45883 | 510 | print_bool(PRINT_ANY, "ignore_df", "ignore-df ", true); |
adbb2965 | 511 | |
9a1381d5 WT |
512 | if (tb[IFLA_GRE_ERSPAN_INDEX]) { |
513 | __u32 erspan_idx = rta_getattr_u32(tb[IFLA_GRE_ERSPAN_INDEX]); | |
514 | ||
515 | fprintf(f, "erspan_index %u ", erspan_idx); | |
516 | } | |
517 | ||
80c24b09 | 518 | if (tb[IFLA_GRE_ENCAP_TYPE] && |
0b2fbb73 | 519 | rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) { |
80c24b09 TH |
520 | __u16 type = rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]); |
521 | __u16 flags = rta_getattr_u16(tb[IFLA_GRE_ENCAP_FLAGS]); | |
522 | __u16 sport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_SPORT]); | |
523 | __u16 dport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_DPORT]); | |
524 | ||
e2d45883 JF |
525 | |
526 | open_json_object("encap"); | |
527 | print_string(PRINT_FP, NULL, "encap ", NULL); | |
528 | ||
80c24b09 TH |
529 | switch (type) { |
530 | case TUNNEL_ENCAP_FOU: | |
e2d45883 | 531 | print_string(PRINT_ANY, "type", "%s ", "fou"); |
80c24b09 TH |
532 | break; |
533 | case TUNNEL_ENCAP_GUE: | |
e2d45883 | 534 | print_string(PRINT_ANY, "type", "%s ", "gue"); |
80c24b09 TH |
535 | break; |
536 | default: | |
e2d45883 | 537 | print_null(PRINT_ANY, "type", "%s ", "unknown"); |
80c24b09 TH |
538 | break; |
539 | } | |
540 | ||
e2d45883 JF |
541 | if (is_json_context()) { |
542 | print_uint(PRINT_JSON, | |
543 | "sport", | |
544 | NULL, | |
545 | sport ? ntohs(sport) : 0); | |
546 | print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); | |
547 | ||
548 | print_bool(PRINT_JSON, | |
549 | "csum", | |
550 | NULL, | |
551 | flags & TUNNEL_ENCAP_FLAG_CSUM); | |
552 | ||
553 | print_bool(PRINT_JSON, | |
554 | "csum6", | |
555 | NULL, | |
556 | flags & TUNNEL_ENCAP_FLAG_CSUM6); | |
557 | ||
558 | print_bool(PRINT_JSON, | |
559 | "remcsum", | |
560 | NULL, | |
561 | flags & TUNNEL_ENCAP_FLAG_REMCSUM); | |
562 | ||
563 | close_json_object(); | |
564 | } else { | |
565 | if (sport == 0) | |
566 | fputs("encap-sport auto ", f); | |
567 | else | |
568 | fprintf(f, "encap-sport %u", ntohs(sport)); | |
80c24b09 | 569 | |
e2d45883 | 570 | fprintf(f, "encap-dport %u ", ntohs(dport)); |
80c24b09 | 571 | |
e2d45883 JF |
572 | if (flags & TUNNEL_ENCAP_FLAG_CSUM) |
573 | fputs("encap-csum ", f); | |
574 | else | |
575 | fputs("noencap-csum ", f); | |
80c24b09 | 576 | |
e2d45883 JF |
577 | if (flags & TUNNEL_ENCAP_FLAG_CSUM6) |
578 | fputs("encap-csum6 ", f); | |
579 | else | |
580 | fputs("noencap-csum6 ", f); | |
858dbb20 | 581 | |
e2d45883 JF |
582 | if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) |
583 | fputs("encap-remcsum ", f); | |
584 | else | |
585 | fputs("noencap-remcsum ", f); | |
586 | } | |
80c24b09 | 587 | } |
237d9e82 HX |
588 | } |
589 | ||
561e650e | 590 | static void gre_print_help(struct link_util *lu, int argc, char **argv, |
e2d45883 | 591 | FILE *f) |
561e650e | 592 | { |
593 | print_usage(f); | |
594 | } | |
595 | ||
237d9e82 HX |
596 | struct link_util gre_link_util = { |
597 | .id = "gre", | |
598 | .maxattr = IFLA_GRE_MAX, | |
599 | .parse_opt = gre_parse_opt, | |
600 | .print_opt = gre_print_opt, | |
561e650e | 601 | .print_help = gre_print_help, |
237d9e82 HX |
602 | }; |
603 | ||
604 | struct link_util gretap_link_util = { | |
605 | .id = "gretap", | |
606 | .maxattr = IFLA_GRE_MAX, | |
607 | .parse_opt = gre_parse_opt, | |
608 | .print_opt = gre_print_opt, | |
561e650e | 609 | .print_help = gre_print_help, |
237d9e82 | 610 | }; |
9a1381d5 WT |
611 | |
612 | struct link_util erspan_link_util = { | |
613 | .id = "erspan", | |
614 | .maxattr = IFLA_GRE_MAX, | |
615 | .parse_opt = gre_parse_opt, | |
616 | .print_opt = gre_print_opt, | |
617 | .print_help = gre_print_help, | |
618 | }; |