]>
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 | ||
ae91205c | 26 | static void gre_print_help(struct link_util *lu, int argc, char **argv, FILE *f) |
561e650e | 27 | { |
8b471354 | 28 | fprintf(f, |
8589eb4e MC |
29 | "Usage: ... %-9s [ remote ADDR ]\n" |
30 | " [ local ADDR ]\n" | |
31 | " [ [no][i|o]seq ]\n" | |
32 | " [ [i|o]key KEY | no[i|o]key ]\n" | |
33 | " [ [no][i|o]csum ]\n" | |
34 | " [ ttl TTL ]\n" | |
35 | " [ tos TOS ]\n" | |
36 | " [ [no]pmtudisc ]\n" | |
37 | " [ [no]ignore-df ]\n" | |
38 | " [ dev PHYS_DEV ]\n" | |
39 | " [ fwmark MARK ]\n" | |
40 | " [ external ]\n" | |
41 | " [ noencap ]\n" | |
42 | " [ encap { fou | gue | none } ]\n" | |
43 | " [ encap-sport PORT ]\n" | |
44 | " [ encap-dport PORT ]\n" | |
45 | " [ [no]encap-csum ]\n" | |
46 | " [ [no]encap-csum6 ]\n" | |
47 | " [ [no]encap-remcsum ]\n" | |
48 | " [ erspan_ver version ]\n" | |
49 | " [ erspan IDX ]\n" | |
50 | " [ erspan_dir { ingress | egress } ]\n" | |
51 | " [ erspan_hwid hwid ]\n" | |
8b471354 | 52 | "\n" |
8589eb4e MC |
53 | "Where: ADDR := { IP_ADDRESS | any }\n" |
54 | " TOS := { NUMBER | inherit }\n" | |
55 | " TTL := { 1..255 | inherit }\n" | |
56 | " KEY := { DOTTED_QUAD | NUMBER }\n" | |
57 | " MARK := { 0x0..0xffffffff }\n", | |
58 | lu->id); | |
561e650e | 59 | } |
60 | ||
237d9e82 HX |
61 | static int gre_parse_opt(struct link_util *lu, int argc, char **argv, |
62 | struct nlmsghdr *n) | |
63 | { | |
28254695 | 64 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
72c771b2 HX |
65 | struct { |
66 | struct nlmsghdr n; | |
67 | struct ifinfomsg i; | |
d17b136f PS |
68 | } req = { |
69 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)), | |
70 | .n.nlmsg_flags = NLM_F_REQUEST, | |
71 | .n.nlmsg_type = RTM_GETLINK, | |
72 | .i.ifi_family = preferred_family, | |
73 | .i.ifi_index = ifi->ifi_index, | |
74 | }; | |
08ede25f | 75 | struct nlmsghdr *answer; |
72c771b2 HX |
76 | struct rtattr *tb[IFLA_MAX + 1]; |
77 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; | |
78 | struct rtattr *greinfo[IFLA_GRE_MAX + 1]; | |
5bd93795 | 79 | int len; |
237d9e82 HX |
80 | __u16 iflags = 0; |
81 | __u16 oflags = 0; | |
1f44b937 SP |
82 | __be32 ikey = 0; |
83 | __be32 okey = 0; | |
a066cc66 | 84 | inet_prefix saddr, daddr; |
72c771b2 | 85 | __u8 pmtudisc = 1; |
5bd93795 | 86 | __u8 ignore_df = 0; |
72c771b2 | 87 | __u8 tos = 0; |
5bd93795 SP |
88 | __u8 ttl = 0; |
89 | __u32 link = 0; | |
80c24b09 TH |
90 | __u16 encaptype = 0; |
91 | __u16 encapflags = 0; | |
92 | __u16 encapsport = 0; | |
93 | __u16 encapdport = 0; | |
926b39e1 | 94 | __u8 metadata = 0; |
ad4b1425 | 95 | __u32 fwmark = 0; |
9a1381d5 | 96 | __u32 erspan_idx = 0; |
28976362 WT |
97 | __u8 erspan_ver = 0; |
98 | __u8 erspan_dir = 0; | |
99 | __u16 erspan_hwid = 0; | |
72c771b2 | 100 | |
a066cc66 SP |
101 | inet_prefix_reset(&saddr); |
102 | inet_prefix_reset(&daddr); | |
103 | ||
72c771b2 | 104 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
a066cc66 SP |
105 | const struct rtattr *rta; |
106 | ||
86bf43c7 | 107 | if (rtnl_talk(&rth, &req.n, &answer) < 0) { |
72c771b2 HX |
108 | get_failed: |
109 | fprintf(stderr, | |
110 | "Failed to get existing tunnel info.\n"); | |
111 | return -1; | |
112 | } | |
113 | ||
86bf43c7 | 114 | len = answer->nlmsg_len; |
72c771b2 HX |
115 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
116 | if (len < 0) | |
117 | goto get_failed; | |
118 | ||
86bf43c7 | 119 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(NLMSG_DATA(answer)), len); |
72c771b2 HX |
120 | |
121 | if (!tb[IFLA_LINKINFO]) | |
122 | goto get_failed; | |
123 | ||
124 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
125 | ||
126 | if (!linkinfo[IFLA_INFO_DATA]) | |
127 | goto get_failed; | |
128 | ||
129 | parse_rtattr_nested(greinfo, IFLA_GRE_MAX, | |
130 | linkinfo[IFLA_INFO_DATA]); | |
131 | ||
a066cc66 SP |
132 | rta = greinfo[IFLA_GRE_LOCAL]; |
133 | if (rta && get_addr_rta(&saddr, rta, AF_INET)) | |
134 | goto get_failed; | |
135 | ||
136 | rta = greinfo[IFLA_GRE_REMOTE]; | |
137 | if (rta && get_addr_rta(&daddr, rta, AF_INET)) | |
138 | goto get_failed; | |
139 | ||
72c771b2 | 140 | if (greinfo[IFLA_GRE_IKEY]) |
ff24746c | 141 | ikey = rta_getattr_u32(greinfo[IFLA_GRE_IKEY]); |
72c771b2 HX |
142 | |
143 | if (greinfo[IFLA_GRE_OKEY]) | |
ff24746c | 144 | okey = rta_getattr_u32(greinfo[IFLA_GRE_OKEY]); |
72c771b2 HX |
145 | |
146 | if (greinfo[IFLA_GRE_IFLAGS]) | |
ff24746c | 147 | iflags = rta_getattr_u16(greinfo[IFLA_GRE_IFLAGS]); |
72c771b2 HX |
148 | |
149 | if (greinfo[IFLA_GRE_OFLAGS]) | |
ff24746c | 150 | oflags = rta_getattr_u16(greinfo[IFLA_GRE_OFLAGS]); |
72c771b2 | 151 | |
72c771b2 | 152 | if (greinfo[IFLA_GRE_PMTUDISC]) |
ff24746c | 153 | pmtudisc = rta_getattr_u8( |
72c771b2 HX |
154 | greinfo[IFLA_GRE_PMTUDISC]); |
155 | ||
5bd93795 SP |
156 | if (greinfo[IFLA_GRE_IGNORE_DF]) |
157 | ignore_df = | |
158 | !!rta_getattr_u8(greinfo[IFLA_GRE_IGNORE_DF]); | |
72c771b2 HX |
159 | |
160 | if (greinfo[IFLA_GRE_TOS]) | |
ff24746c | 161 | tos = rta_getattr_u8(greinfo[IFLA_GRE_TOS]); |
72c771b2 | 162 | |
5bd93795 SP |
163 | if (greinfo[IFLA_GRE_TTL]) |
164 | ttl = rta_getattr_u8(greinfo[IFLA_GRE_TTL]); | |
165 | ||
72c771b2 | 166 | if (greinfo[IFLA_GRE_LINK]) |
a6addd5c | 167 | link = rta_getattr_u32(greinfo[IFLA_GRE_LINK]); |
80c24b09 TH |
168 | |
169 | if (greinfo[IFLA_GRE_ENCAP_TYPE]) | |
170 | encaptype = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_TYPE]); | |
5bd93795 | 171 | |
80c24b09 TH |
172 | if (greinfo[IFLA_GRE_ENCAP_FLAGS]) |
173 | encapflags = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_FLAGS]); | |
5bd93795 | 174 | |
80c24b09 TH |
175 | if (greinfo[IFLA_GRE_ENCAP_SPORT]) |
176 | encapsport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_SPORT]); | |
5bd93795 | 177 | |
80c24b09 TH |
178 | if (greinfo[IFLA_GRE_ENCAP_DPORT]) |
179 | encapdport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_DPORT]); | |
926b39e1 PA |
180 | |
181 | if (greinfo[IFLA_GRE_COLLECT_METADATA]) | |
182 | metadata = 1; | |
ad4b1425 CG |
183 | |
184 | if (greinfo[IFLA_GRE_FWMARK]) | |
185 | fwmark = rta_getattr_u32(greinfo[IFLA_GRE_FWMARK]); | |
9a1381d5 WT |
186 | |
187 | if (greinfo[IFLA_GRE_ERSPAN_INDEX]) | |
188 | erspan_idx = rta_getattr_u32(greinfo[IFLA_GRE_ERSPAN_INDEX]); | |
86bf43c7 | 189 | |
28976362 WT |
190 | if (greinfo[IFLA_GRE_ERSPAN_VER]) |
191 | erspan_ver = rta_getattr_u8(greinfo[IFLA_GRE_ERSPAN_VER]); | |
192 | ||
193 | if (greinfo[IFLA_GRE_ERSPAN_DIR]) | |
194 | erspan_dir = rta_getattr_u8(greinfo[IFLA_GRE_ERSPAN_DIR]); | |
195 | ||
196 | if (greinfo[IFLA_GRE_ERSPAN_HWID]) | |
197 | erspan_hwid = rta_getattr_u16(greinfo[IFLA_GRE_ERSPAN_HWID]); | |
198 | ||
86bf43c7 | 199 | free(answer); |
72c771b2 | 200 | } |
237d9e82 HX |
201 | |
202 | while (argc > 0) { | |
203 | if (!matches(*argv, "key")) { | |
237d9e82 HX |
204 | NEXT_ARG(); |
205 | iflags |= GRE_KEY; | |
206 | oflags |= GRE_KEY; | |
1f44b937 | 207 | ikey = okey = tnl_parse_key("key", *argv); |
7f520601 SD |
208 | } else if (!matches(*argv, "nokey")) { |
209 | iflags &= ~GRE_KEY; | |
210 | oflags &= ~GRE_KEY; | |
211 | ikey = okey = 0; | |
237d9e82 | 212 | } else if (!matches(*argv, "ikey")) { |
237d9e82 HX |
213 | NEXT_ARG(); |
214 | iflags |= GRE_KEY; | |
1f44b937 | 215 | ikey = tnl_parse_key("ikey", *argv); |
7f520601 SD |
216 | } else if (!matches(*argv, "noikey")) { |
217 | iflags &= ~GRE_KEY; | |
218 | ikey = 0; | |
237d9e82 | 219 | } else if (!matches(*argv, "okey")) { |
237d9e82 HX |
220 | NEXT_ARG(); |
221 | oflags |= GRE_KEY; | |
1f44b937 | 222 | okey = tnl_parse_key("okey", *argv); |
7f520601 SD |
223 | } else if (!matches(*argv, "nookey")) { |
224 | oflags &= ~GRE_KEY; | |
225 | okey = 0; | |
237d9e82 HX |
226 | } else if (!matches(*argv, "seq")) { |
227 | iflags |= GRE_SEQ; | |
228 | oflags |= GRE_SEQ; | |
7f520601 SD |
229 | } else if (!matches(*argv, "noseq")) { |
230 | iflags &= ~GRE_SEQ; | |
231 | oflags &= ~GRE_SEQ; | |
237d9e82 HX |
232 | } else if (!matches(*argv, "iseq")) { |
233 | iflags |= GRE_SEQ; | |
7f520601 SD |
234 | } else if (!matches(*argv, "noiseq")) { |
235 | iflags &= ~GRE_SEQ; | |
237d9e82 HX |
236 | } else if (!matches(*argv, "oseq")) { |
237 | oflags |= GRE_SEQ; | |
7f520601 SD |
238 | } else if (!matches(*argv, "nooseq")) { |
239 | oflags &= ~GRE_SEQ; | |
237d9e82 HX |
240 | } else if (!matches(*argv, "csum")) { |
241 | iflags |= GRE_CSUM; | |
242 | oflags |= GRE_CSUM; | |
7f520601 SD |
243 | } else if (!matches(*argv, "nocsum")) { |
244 | iflags &= ~GRE_CSUM; | |
245 | oflags &= ~GRE_CSUM; | |
237d9e82 HX |
246 | } else if (!matches(*argv, "icsum")) { |
247 | iflags |= GRE_CSUM; | |
7f520601 SD |
248 | } else if (!matches(*argv, "noicsum")) { |
249 | iflags &= ~GRE_CSUM; | |
237d9e82 HX |
250 | } else if (!matches(*argv, "ocsum")) { |
251 | oflags |= GRE_CSUM; | |
7f520601 SD |
252 | } else if (!matches(*argv, "noocsum")) { |
253 | oflags &= ~GRE_CSUM; | |
237d9e82 | 254 | } else if (!matches(*argv, "nopmtudisc")) { |
72c771b2 | 255 | pmtudisc = 0; |
237d9e82 | 256 | } else if (!matches(*argv, "pmtudisc")) { |
72c771b2 | 257 | pmtudisc = 1; |
237d9e82 HX |
258 | } else if (!matches(*argv, "remote")) { |
259 | NEXT_ARG(); | |
a066cc66 | 260 | get_addr(&daddr, *argv, AF_INET); |
237d9e82 HX |
261 | } else if (!matches(*argv, "local")) { |
262 | NEXT_ARG(); | |
a066cc66 | 263 | get_addr(&saddr, *argv, AF_INET); |
237d9e82 | 264 | } else if (!matches(*argv, "dev")) { |
237d9e82 | 265 | NEXT_ARG(); |
7a14358b | 266 | link = ll_name_to_index(*argv); |
fe99adbc SP |
267 | if (!link) |
268 | exit(nodev(*argv)); | |
237d9e82 | 269 | } else if (!matches(*argv, "ttl") || |
c4743c4d SP |
270 | !matches(*argv, "hoplimit") || |
271 | !matches(*argv, "hlim")) { | |
237d9e82 HX |
272 | NEXT_ARG(); |
273 | if (strcmp(*argv, "inherit") != 0) { | |
c4743c4d | 274 | if (get_u8(&ttl, *argv, 0)) |
237d9e82 | 275 | invarg("invalid TTL\n", *argv); |
079e6781 RS |
276 | } else |
277 | ttl = 0; | |
237d9e82 HX |
278 | } else if (!matches(*argv, "tos") || |
279 | !matches(*argv, "tclass") || | |
280 | !matches(*argv, "dsfield")) { | |
281 | __u32 uval; | |
237d9e82 HX |
282 | |
283 | NEXT_ARG(); | |
284 | if (strcmp(*argv, "inherit") != 0) { | |
285 | if (rtnl_dsfield_a2n(&uval, *argv)) | |
286 | invarg("bad TOS value", *argv); | |
287 | tos = uval; | |
288 | } else | |
289 | tos = 1; | |
80c24b09 TH |
290 | } else if (strcmp(*argv, "noencap") == 0) { |
291 | encaptype = TUNNEL_ENCAP_NONE; | |
292 | } else if (strcmp(*argv, "encap") == 0) { | |
293 | NEXT_ARG(); | |
294 | if (strcmp(*argv, "fou") == 0) | |
295 | encaptype = TUNNEL_ENCAP_FOU; | |
296 | else if (strcmp(*argv, "gue") == 0) | |
297 | encaptype = TUNNEL_ENCAP_GUE; | |
298 | else if (strcmp(*argv, "none") == 0) | |
299 | encaptype = TUNNEL_ENCAP_NONE; | |
300 | else | |
301 | invarg("Invalid encap type.", *argv); | |
302 | } else if (strcmp(*argv, "encap-sport") == 0) { | |
303 | NEXT_ARG(); | |
304 | if (strcmp(*argv, "auto") == 0) | |
305 | encapsport = 0; | |
306 | else if (get_u16(&encapsport, *argv, 0)) | |
307 | invarg("Invalid source port.", *argv); | |
308 | } else if (strcmp(*argv, "encap-dport") == 0) { | |
309 | NEXT_ARG(); | |
310 | if (get_u16(&encapdport, *argv, 0)) | |
311 | invarg("Invalid destination port.", *argv); | |
312 | } else if (strcmp(*argv, "encap-csum") == 0) { | |
313 | encapflags |= TUNNEL_ENCAP_FLAG_CSUM; | |
314 | } else if (strcmp(*argv, "noencap-csum") == 0) { | |
315 | encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM; | |
316 | } else if (strcmp(*argv, "encap-udp6-csum") == 0) { | |
317 | encapflags |= TUNNEL_ENCAP_FLAG_CSUM6; | |
318 | } else if (strcmp(*argv, "noencap-udp6-csum") == 0) { | |
147ade01 | 319 | encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM6; |
858dbb20 TH |
320 | } else if (strcmp(*argv, "encap-remcsum") == 0) { |
321 | encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM; | |
322 | } else if (strcmp(*argv, "noencap-remcsum") == 0) { | |
147ade01 | 323 | encapflags &= ~TUNNEL_ENCAP_FLAG_REMCSUM; |
926b39e1 PA |
324 | } else if (strcmp(*argv, "external") == 0) { |
325 | metadata = 1; | |
adbb2965 PP |
326 | } else if (strcmp(*argv, "ignore-df") == 0) { |
327 | ignore_df = 1; | |
328 | } else if (strcmp(*argv, "noignore-df") == 0) { | |
329 | /* | |
330 | *only the lsb is significant, use 2 for presence | |
331 | */ | |
332 | ignore_df = 2; | |
ad4b1425 CG |
333 | } else if (strcmp(*argv, "fwmark") == 0) { |
334 | NEXT_ARG(); | |
335 | if (get_u32(&fwmark, *argv, 0)) | |
336 | invarg("invalid fwmark\n", *argv); | |
9a1381d5 WT |
337 | } else if (strcmp(*argv, "erspan") == 0) { |
338 | NEXT_ARG(); | |
339 | if (get_u32(&erspan_idx, *argv, 0)) | |
340 | invarg("invalid erspan index\n", *argv); | |
341 | if (erspan_idx & ~((1<<20) - 1) || erspan_idx == 0) | |
342 | invarg("erspan index must be > 0 and <= 20-bit\n", *argv); | |
28976362 WT |
343 | } else if (strcmp(*argv, "erspan_ver") == 0) { |
344 | NEXT_ARG(); | |
345 | if (get_u8(&erspan_ver, *argv, 0)) | |
346 | invarg("invalid erspan version\n", *argv); | |
347 | if (erspan_ver != 1 && erspan_ver != 2) | |
348 | invarg("erspan version must be 1 or 2\n", *argv); | |
349 | } else if (strcmp(*argv, "erspan_dir") == 0) { | |
350 | NEXT_ARG(); | |
351 | if (matches(*argv, "ingress") == 0) | |
352 | erspan_dir = 0; | |
353 | else if (matches(*argv, "egress") == 0) | |
354 | erspan_dir = 1; | |
355 | else | |
356 | invarg("Invalid erspan direction.", *argv); | |
357 | } else if (strcmp(*argv, "erspan_hwid") == 0) { | |
358 | NEXT_ARG(); | |
359 | if (get_u16(&erspan_hwid, *argv, 0)) | |
360 | invarg("invalid erspan hwid\n", *argv); | |
ae91205c SP |
361 | } else { |
362 | gre_print_help(lu, argc, argv, stderr); | |
363 | return -1; | |
364 | } | |
237d9e82 HX |
365 | argc--; argv++; |
366 | } | |
367 | ||
a066cc66 SP |
368 | if (is_addrtype_inet_multi(&daddr)) { |
369 | if (!ikey) { | |
370 | ikey = daddr.data[0]; | |
371 | iflags |= GRE_KEY; | |
372 | } | |
373 | if (!okey) { | |
374 | okey = daddr.data[0]; | |
375 | oflags |= GRE_KEY; | |
376 | } | |
377 | if (!is_addrtype_inet_not_unspec(&saddr)) { | |
378 | fprintf(stderr, | |
379 | "A broadcast tunnel requires a source address.\n"); | |
380 | return -1; | |
381 | } | |
237d9e82 HX |
382 | } |
383 | ||
5bd93795 | 384 | if (metadata) { |
df217d5d | 385 | addattr_l(n, 1024, IFLA_GRE_COLLECT_METADATA, NULL, 0); |
5bd93795 | 386 | return 0; |
df217d5d | 387 | } |
237d9e82 | 388 | |
5bd93795 SP |
389 | addattr32(n, 1024, IFLA_GRE_IKEY, ikey); |
390 | addattr32(n, 1024, IFLA_GRE_OKEY, okey); | |
391 | addattr_l(n, 1024, IFLA_GRE_IFLAGS, &iflags, 2); | |
392 | addattr_l(n, 1024, IFLA_GRE_OFLAGS, &oflags, 2); | |
fa1e658e | 393 | if (is_addrtype_inet_not_unspec(&saddr)) |
a066cc66 | 394 | addattr_l(n, 1024, IFLA_GRE_LOCAL, saddr.data, saddr.bytelen); |
fa1e658e | 395 | if (is_addrtype_inet_not_unspec(&daddr)) |
a066cc66 | 396 | addattr_l(n, 1024, IFLA_GRE_REMOTE, daddr.data, daddr.bytelen); |
5bd93795 SP |
397 | addattr_l(n, 1024, IFLA_GRE_PMTUDISC, &pmtudisc, 1); |
398 | if (ignore_df) | |
399 | addattr8(n, 1024, IFLA_GRE_IGNORE_DF, ignore_df & 1); | |
400 | addattr_l(n, 1024, IFLA_GRE_TOS, &tos, 1); | |
401 | if (link) | |
402 | addattr32(n, 1024, IFLA_GRE_LINK, link); | |
403 | addattr_l(n, 1024, IFLA_GRE_TTL, &ttl, 1); | |
404 | addattr32(n, 1024, IFLA_GRE_FWMARK, fwmark); | |
405 | if (erspan_ver) { | |
406 | addattr8(n, 1024, IFLA_GRE_ERSPAN_VER, erspan_ver); | |
407 | if (erspan_ver == 1 && erspan_idx != 0) { | |
408 | addattr32(n, 1024, IFLA_GRE_ERSPAN_INDEX, erspan_idx); | |
409 | } else if (erspan_ver == 2) { | |
410 | addattr8(n, 1024, IFLA_GRE_ERSPAN_DIR, erspan_dir); | |
411 | addattr16(n, 1024, IFLA_GRE_ERSPAN_HWID, erspan_hwid); | |
412 | } | |
413 | } | |
414 | addattr16(n, 1024, IFLA_GRE_ENCAP_TYPE, encaptype); | |
415 | addattr16(n, 1024, IFLA_GRE_ENCAP_FLAGS, encapflags); | |
416 | addattr16(n, 1024, IFLA_GRE_ENCAP_SPORT, htons(encapsport)); | |
417 | addattr16(n, 1024, IFLA_GRE_ENCAP_DPORT, htons(encapdport)); | |
418 | ||
237d9e82 HX |
419 | return 0; |
420 | } | |
421 | ||
00ff4b8e | 422 | static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) |
237d9e82 | 423 | { |
237d9e82 | 424 | char s2[64]; |
5bd93795 SP |
425 | __u16 iflags = 0; |
426 | __u16 oflags = 0; | |
375560c4 | 427 | __u8 ttl = 0; |
3caa526c | 428 | __u8 tos = 0; |
237d9e82 | 429 | |
00ff4b8e SP |
430 | if (!tb) |
431 | return; | |
432 | ||
433 | if (tb[IFLA_GRE_COLLECT_METADATA]) { | |
d6abae5a | 434 | print_bool(PRINT_ANY, "external", "external ", true); |
00ff4b8e SP |
435 | return; |
436 | } | |
437 | ||
b761fc41 SP |
438 | tnl_print_endpoint("remote", tb[IFLA_GRE_REMOTE], AF_INET); |
439 | tnl_print_endpoint("local", tb[IFLA_GRE_LOCAL], AF_INET); | |
237d9e82 | 440 | |
45d3a6ef | 441 | if (tb[IFLA_GRE_LINK]) { |
5bd93795 | 442 | __u32 link = rta_getattr_u32(tb[IFLA_GRE_LINK]); |
237d9e82 | 443 | |
45d3a6ef SP |
444 | if (link) { |
445 | print_string(PRINT_ANY, "link", "dev %s ", | |
446 | ll_index_to_name(link)); | |
447 | } | |
237d9e82 HX |
448 | } |
449 | ||
375560c4 SP |
450 | if (tb[IFLA_GRE_TTL]) |
451 | ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]); | |
452 | if (is_json_context() || ttl) | |
453 | print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl); | |
454 | else | |
e2d45883 | 455 | print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); |
e2d45883 | 456 | |
3caa526c SP |
457 | if (tb[IFLA_GRE_TOS]) |
458 | tos = rta_getattr_u8(tb[IFLA_GRE_TOS]); | |
459 | if (tos) { | |
460 | if (is_json_context() || tos != 1) | |
90c5c969 | 461 | print_0xhex(PRINT_ANY, "tos", "tos %#llx ", tos); |
3caa526c SP |
462 | else |
463 | print_string(PRINT_FP, NULL, "tos %s ", "inherit"); | |
237d9e82 HX |
464 | } |
465 | ||
e2d45883 JF |
466 | if (tb[IFLA_GRE_PMTUDISC]) { |
467 | if (!rta_getattr_u8(tb[IFLA_GRE_PMTUDISC])) | |
468 | print_bool(PRINT_ANY, "pmtudisc", "nopmtudisc ", false); | |
469 | else | |
470 | print_bool(PRINT_JSON, "pmtudisc", NULL, true); | |
471 | } | |
237d9e82 | 472 | |
00ff4b8e SP |
473 | if (tb[IFLA_GRE_IGNORE_DF] && rta_getattr_u8(tb[IFLA_GRE_IGNORE_DF])) |
474 | print_bool(PRINT_ANY, "ignore_df", "ignore-df ", true); | |
475 | ||
237d9e82 | 476 | if (tb[IFLA_GRE_IFLAGS]) |
ff24746c | 477 | iflags = rta_getattr_u16(tb[IFLA_GRE_IFLAGS]); |
237d9e82 HX |
478 | |
479 | if (tb[IFLA_GRE_OFLAGS]) | |
ff24746c | 480 | oflags = rta_getattr_u16(tb[IFLA_GRE_OFLAGS]); |
237d9e82 | 481 | |
71816553 | 482 | if ((iflags & GRE_KEY) && tb[IFLA_GRE_IKEY]) { |
237d9e82 | 483 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_IKEY]), s2, sizeof(s2)); |
e2d45883 | 484 | print_string(PRINT_ANY, "ikey", "ikey %s ", s2); |
237d9e82 HX |
485 | } |
486 | ||
71816553 | 487 | if ((oflags & GRE_KEY) && tb[IFLA_GRE_OKEY]) { |
237d9e82 | 488 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_OKEY]), s2, sizeof(s2)); |
e2d45883 | 489 | print_string(PRINT_ANY, "okey", "okey %s ", s2); |
237d9e82 HX |
490 | } |
491 | ||
492 | if (iflags & GRE_SEQ) | |
e2d45883 | 493 | print_bool(PRINT_ANY, "iseq", "iseq ", true); |
237d9e82 | 494 | if (oflags & GRE_SEQ) |
e2d45883 | 495 | print_bool(PRINT_ANY, "oseq", "oseq ", true); |
237d9e82 | 496 | if (iflags & GRE_CSUM) |
e2d45883 | 497 | print_bool(PRINT_ANY, "icsum", "icsum ", true); |
237d9e82 | 498 | if (oflags & GRE_CSUM) |
e2d45883 JF |
499 | print_bool(PRINT_ANY, "ocsum", "ocsum ", true); |
500 | ||
501 | if (tb[IFLA_GRE_FWMARK]) { | |
502 | __u32 fwmark = rta_getattr_u32(tb[IFLA_GRE_FWMARK]); | |
503 | ||
504 | if (fwmark) { | |
e97ad3d2 | 505 | print_0xhex(PRINT_ANY, |
90c5c969 | 506 | "fwmark", "fwmark %#llx ", fwmark); |
e2d45883 | 507 | } |
ad4b1425 | 508 | } |
adbb2965 | 509 | |
9a1381d5 WT |
510 | if (tb[IFLA_GRE_ERSPAN_INDEX]) { |
511 | __u32 erspan_idx = rta_getattr_u32(tb[IFLA_GRE_ERSPAN_INDEX]); | |
512 | ||
3cb92eb9 SP |
513 | print_uint(PRINT_ANY, |
514 | "erspan_index", "erspan_index %u ", erspan_idx); | |
28976362 WT |
515 | } |
516 | ||
517 | if (tb[IFLA_GRE_ERSPAN_VER]) { | |
518 | __u8 erspan_ver = rta_getattr_u8(tb[IFLA_GRE_ERSPAN_VER]); | |
519 | ||
db2b8b6e SP |
520 | print_uint(PRINT_ANY, |
521 | "erspan_ver", "erspan_ver %u ", erspan_ver); | |
28976362 WT |
522 | } |
523 | ||
524 | if (tb[IFLA_GRE_ERSPAN_DIR]) { | |
525 | __u8 erspan_dir = rta_getattr_u8(tb[IFLA_GRE_ERSPAN_DIR]); | |
526 | ||
527 | if (erspan_dir == 0) | |
528 | print_string(PRINT_ANY, "erspan_dir", | |
529 | "erspan_dir ingress ", NULL); | |
530 | else | |
531 | print_string(PRINT_ANY, "erspan_dir", | |
532 | "erspan_dir egress ", NULL); | |
533 | } | |
534 | ||
535 | if (tb[IFLA_GRE_ERSPAN_HWID]) { | |
536 | __u16 erspan_hwid = rta_getattr_u16(tb[IFLA_GRE_ERSPAN_HWID]); | |
537 | ||
db2b8b6e | 538 | print_0xhex(PRINT_ANY, |
90c5c969 | 539 | "erspan_hwid", "erspan_hwid %#llx ", erspan_hwid); |
9a1381d5 WT |
540 | } |
541 | ||
bad76e6b SP |
542 | tnl_print_encap(tb, |
543 | IFLA_GRE_ENCAP_TYPE, | |
544 | IFLA_GRE_ENCAP_FLAGS, | |
545 | IFLA_GRE_ENCAP_SPORT, | |
546 | IFLA_GRE_ENCAP_DPORT); | |
237d9e82 HX |
547 | } |
548 | ||
549 | struct link_util gre_link_util = { | |
550 | .id = "gre", | |
551 | .maxattr = IFLA_GRE_MAX, | |
552 | .parse_opt = gre_parse_opt, | |
553 | .print_opt = gre_print_opt, | |
561e650e | 554 | .print_help = gre_print_help, |
237d9e82 HX |
555 | }; |
556 | ||
557 | struct link_util gretap_link_util = { | |
558 | .id = "gretap", | |
559 | .maxattr = IFLA_GRE_MAX, | |
560 | .parse_opt = gre_parse_opt, | |
561 | .print_opt = gre_print_opt, | |
561e650e | 562 | .print_help = gre_print_help, |
237d9e82 | 563 | }; |
9a1381d5 WT |
564 | |
565 | struct link_util erspan_link_util = { | |
566 | .id = "erspan", | |
567 | .maxattr = IFLA_GRE_MAX, | |
568 | .parse_opt = gre_parse_opt, | |
569 | .print_opt = gre_print_opt, | |
570 | .print_help = gre_print_help, | |
571 | }; |