blob: 3b99e56f40748ee439f0386d78e5b0c741d229af [file] [log] [blame]
Herbert Xu237d9e82008-10-09 15:08:24 +08001/*
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>
Herbert Xu237d9e82008-10-09 15:08:24 +080015#include <sys/types.h>
16#include <sys/socket.h>
17#include <arpa/inet.h>
18
Stephen Hemminger1957a322009-03-24 15:40:54 -070019#include <linux/ip.h>
20#include <linux/if_tunnel.h>
Herbert Xu237d9e82008-10-09 15:08:24 +080021#include "rt_names.h"
22#include "utils.h"
23#include "ip_common.h"
24#include "tunnel.h"
25
vadimk561e6502014-09-30 08:17:31 +030026static void print_usage(FILE *f)
27{
28 fprintf(f, "Usage: ip link { add | set | change | replace | del } NAME\n");
29 fprintf(f, " type { gre | gretap } [ remote ADDR ] [ local ADDR ]\n");
30 fprintf(f, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
31 fprintf(f, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
Tom Herbert80c24b02014-11-05 10:06:26 -080032 fprintf(f, " [ noencap ] [ encap { fou | gue | none } ]\n");
33 fprintf(f, " [ encap-sport PORT ] [ encap-dport PORT ]\n");
Tom Herbert858dbb22015-01-29 08:51:58 -080034 fprintf(f, " [ [no]encap-csum ] [ [no]encap-csum6 ] [ [no]encap-remcsum ]\n");
vadimk561e6502014-09-30 08:17:31 +030035 fprintf(f, "\n");
36 fprintf(f, "Where: NAME := STRING\n");
37 fprintf(f, " ADDR := { IP_ADDRESS | any }\n");
38 fprintf(f, " TOS := { NUMBER | inherit }\n");
39 fprintf(f, " TTL := { 1..255 | inherit }\n");
40 fprintf(f, " KEY := { DOTTED_QUAD | NUMBER }\n");
41}
42
Herbert Xu237d9e82008-10-09 15:08:24 +080043static void usage(void) __attribute__((noreturn));
44static void usage(void)
45{
vadimk561e6502014-09-30 08:17:31 +030046 print_usage(stderr);
Herbert Xu237d9e82008-10-09 15:08:24 +080047 exit(-1);
48}
49
50static int gre_parse_opt(struct link_util *lu, int argc, char **argv,
51 struct nlmsghdr *n)
52{
Phil Sutterd17b1362016-07-18 16:48:42 +020053 struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
Herbert Xu72c771b2009-01-06 18:27:52 -080054 struct {
55 struct nlmsghdr n;
56 struct ifinfomsg i;
Jetchko Jekovbde5baa2015-05-21 16:32:24 +020057 char buf[16384];
Phil Sutterd17b1362016-07-18 16:48:42 +020058 } req = {
59 .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)),
60 .n.nlmsg_flags = NLM_F_REQUEST,
61 .n.nlmsg_type = RTM_GETLINK,
62 .i.ifi_family = preferred_family,
63 .i.ifi_index = ifi->ifi_index,
64 };
Herbert Xu72c771b2009-01-06 18:27:52 -080065 struct rtattr *tb[IFLA_MAX + 1];
66 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
67 struct rtattr *greinfo[IFLA_GRE_MAX + 1];
Herbert Xu237d9e82008-10-09 15:08:24 +080068 __u16 iflags = 0;
69 __u16 oflags = 0;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070070 unsigned int ikey = 0;
71 unsigned int okey = 0;
72 unsigned int saddr = 0;
73 unsigned int daddr = 0;
74 unsigned int link = 0;
Herbert Xu72c771b2009-01-06 18:27:52 -080075 __u8 pmtudisc = 1;
76 __u8 ttl = 0;
77 __u8 tos = 0;
78 int len;
Tom Herbert80c24b02014-11-05 10:06:26 -080079 __u16 encaptype = 0;
80 __u16 encapflags = 0;
81 __u16 encapsport = 0;
82 __u16 encapdport = 0;
Paolo Abeni926b39e2015-12-18 10:50:37 +010083 __u8 metadata = 0;
Herbert Xu72c771b2009-01-06 18:27:52 -080084
85 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Stephen Hemmingerc079e122015-05-27 12:26:14 -070086 if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) {
Herbert Xu72c771b2009-01-06 18:27:52 -080087get_failed:
88 fprintf(stderr,
89 "Failed to get existing tunnel info.\n");
90 return -1;
91 }
92
93 len = req.n.nlmsg_len;
94 len -= NLMSG_LENGTH(sizeof(*ifi));
95 if (len < 0)
96 goto get_failed;
97
98 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len);
99
100 if (!tb[IFLA_LINKINFO])
101 goto get_failed;
102
103 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
104
105 if (!linkinfo[IFLA_INFO_DATA])
106 goto get_failed;
107
108 parse_rtattr_nested(greinfo, IFLA_GRE_MAX,
109 linkinfo[IFLA_INFO_DATA]);
110
111 if (greinfo[IFLA_GRE_IKEY])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700112 ikey = rta_getattr_u32(greinfo[IFLA_GRE_IKEY]);
Herbert Xu72c771b2009-01-06 18:27:52 -0800113
114 if (greinfo[IFLA_GRE_OKEY])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700115 okey = rta_getattr_u32(greinfo[IFLA_GRE_OKEY]);
Herbert Xu72c771b2009-01-06 18:27:52 -0800116
117 if (greinfo[IFLA_GRE_IFLAGS])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700118 iflags = rta_getattr_u16(greinfo[IFLA_GRE_IFLAGS]);
Herbert Xu72c771b2009-01-06 18:27:52 -0800119
120 if (greinfo[IFLA_GRE_OFLAGS])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700121 oflags = rta_getattr_u16(greinfo[IFLA_GRE_OFLAGS]);
Herbert Xu72c771b2009-01-06 18:27:52 -0800122
123 if (greinfo[IFLA_GRE_LOCAL])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700124 saddr = rta_getattr_u32(greinfo[IFLA_GRE_LOCAL]);
Herbert Xu72c771b2009-01-06 18:27:52 -0800125
126 if (greinfo[IFLA_GRE_REMOTE])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700127 daddr = rta_getattr_u32(greinfo[IFLA_GRE_REMOTE]);
Herbert Xu72c771b2009-01-06 18:27:52 -0800128
129 if (greinfo[IFLA_GRE_PMTUDISC])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700130 pmtudisc = rta_getattr_u8(
Herbert Xu72c771b2009-01-06 18:27:52 -0800131 greinfo[IFLA_GRE_PMTUDISC]);
132
133 if (greinfo[IFLA_GRE_TTL])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700134 ttl = rta_getattr_u8(greinfo[IFLA_GRE_TTL]);
Herbert Xu72c771b2009-01-06 18:27:52 -0800135
136 if (greinfo[IFLA_GRE_TOS])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700137 tos = rta_getattr_u8(greinfo[IFLA_GRE_TOS]);
Herbert Xu72c771b2009-01-06 18:27:52 -0800138
139 if (greinfo[IFLA_GRE_LINK])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700140 link = rta_getattr_u8(greinfo[IFLA_GRE_LINK]);
Tom Herbert80c24b02014-11-05 10:06:26 -0800141
142 if (greinfo[IFLA_GRE_ENCAP_TYPE])
143 encaptype = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_TYPE]);
144 if (greinfo[IFLA_GRE_ENCAP_FLAGS])
145 encapflags = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_FLAGS]);
146 if (greinfo[IFLA_GRE_ENCAP_SPORT])
147 encapsport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_SPORT]);
148 if (greinfo[IFLA_GRE_ENCAP_DPORT])
149 encapdport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_DPORT]);
Paolo Abeni926b39e2015-12-18 10:50:37 +0100150
151 if (greinfo[IFLA_GRE_COLLECT_METADATA])
152 metadata = 1;
Herbert Xu72c771b2009-01-06 18:27:52 -0800153 }
Herbert Xu237d9e82008-10-09 15:08:24 +0800154
155 while (argc > 0) {
156 if (!matches(*argv, "key")) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700157 unsigned int uval;
Herbert Xu237d9e82008-10-09 15:08:24 +0800158
159 NEXT_ARG();
160 iflags |= GRE_KEY;
161 oflags |= GRE_KEY;
162 if (strchr(*argv, '.'))
163 uval = get_addr32(*argv);
164 else {
165 if (get_unsigned(&uval, *argv, 0) < 0) {
166 fprintf(stderr,
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000167 "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv);
Herbert Xu237d9e82008-10-09 15:08:24 +0800168 exit(-1);
169 }
170 uval = htonl(uval);
171 }
172
173 ikey = okey = uval;
174 } else if (!matches(*argv, "ikey")) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700175 unsigned int uval;
Herbert Xu237d9e82008-10-09 15:08:24 +0800176
177 NEXT_ARG();
178 iflags |= GRE_KEY;
179 if (strchr(*argv, '.'))
180 uval = get_addr32(*argv);
181 else {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700182 if (get_unsigned(&uval, *argv, 0) < 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000183 fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv);
Herbert Xu237d9e82008-10-09 15:08:24 +0800184 exit(-1);
185 }
186 uval = htonl(uval);
187 }
188 ikey = uval;
189 } else if (!matches(*argv, "okey")) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700190 unsigned int uval;
Herbert Xu237d9e82008-10-09 15:08:24 +0800191
192 NEXT_ARG();
193 oflags |= GRE_KEY;
194 if (strchr(*argv, '.'))
195 uval = get_addr32(*argv);
196 else {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700197 if (get_unsigned(&uval, *argv, 0) < 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000198 fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv);
Herbert Xu237d9e82008-10-09 15:08:24 +0800199 exit(-1);
200 }
201 uval = htonl(uval);
202 }
203 okey = uval;
204 } else if (!matches(*argv, "seq")) {
205 iflags |= GRE_SEQ;
206 oflags |= GRE_SEQ;
207 } else if (!matches(*argv, "iseq")) {
208 iflags |= GRE_SEQ;
209 } else if (!matches(*argv, "oseq")) {
210 oflags |= GRE_SEQ;
211 } else if (!matches(*argv, "csum")) {
212 iflags |= GRE_CSUM;
213 oflags |= GRE_CSUM;
214 } else if (!matches(*argv, "icsum")) {
215 iflags |= GRE_CSUM;
216 } else if (!matches(*argv, "ocsum")) {
217 oflags |= GRE_CSUM;
218 } else if (!matches(*argv, "nopmtudisc")) {
Herbert Xu72c771b2009-01-06 18:27:52 -0800219 pmtudisc = 0;
Herbert Xu237d9e82008-10-09 15:08:24 +0800220 } else if (!matches(*argv, "pmtudisc")) {
Herbert Xu72c771b2009-01-06 18:27:52 -0800221 pmtudisc = 1;
Herbert Xu237d9e82008-10-09 15:08:24 +0800222 } else if (!matches(*argv, "remote")) {
223 NEXT_ARG();
224 if (strcmp(*argv, "any"))
225 daddr = get_addr32(*argv);
226 } else if (!matches(*argv, "local")) {
227 NEXT_ARG();
228 if (strcmp(*argv, "any"))
229 saddr = get_addr32(*argv);
230 } else if (!matches(*argv, "dev")) {
Herbert Xu237d9e82008-10-09 15:08:24 +0800231 NEXT_ARG();
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800232 link = if_nametoindex(*argv);
Cong Wang0cb6bb52014-06-03 16:06:45 -0700233 if (link == 0) {
234 fprintf(stderr, "Cannot find device \"%s\"\n",
235 *argv);
Herbert Xu237d9e82008-10-09 15:08:24 +0800236 exit(-1);
Cong Wang0cb6bb52014-06-03 16:06:45 -0700237 }
Herbert Xu237d9e82008-10-09 15:08:24 +0800238 } else if (!matches(*argv, "ttl") ||
239 !matches(*argv, "hoplimit")) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700240 unsigned int uval;
Herbert Xu237d9e82008-10-09 15:08:24 +0800241
242 NEXT_ARG();
243 if (strcmp(*argv, "inherit") != 0) {
244 if (get_unsigned(&uval, *argv, 0))
245 invarg("invalid TTL\n", *argv);
246 if (uval > 255)
247 invarg("TTL must be <= 255\n", *argv);
248 ttl = uval;
Herbert Xu237d9e82008-10-09 15:08:24 +0800249 }
250 } else if (!matches(*argv, "tos") ||
251 !matches(*argv, "tclass") ||
252 !matches(*argv, "dsfield")) {
253 __u32 uval;
Herbert Xu237d9e82008-10-09 15:08:24 +0800254
255 NEXT_ARG();
256 if (strcmp(*argv, "inherit") != 0) {
257 if (rtnl_dsfield_a2n(&uval, *argv))
258 invarg("bad TOS value", *argv);
259 tos = uval;
260 } else
261 tos = 1;
Tom Herbert80c24b02014-11-05 10:06:26 -0800262 } else if (strcmp(*argv, "noencap") == 0) {
263 encaptype = TUNNEL_ENCAP_NONE;
264 } else if (strcmp(*argv, "encap") == 0) {
265 NEXT_ARG();
266 if (strcmp(*argv, "fou") == 0)
267 encaptype = TUNNEL_ENCAP_FOU;
268 else if (strcmp(*argv, "gue") == 0)
269 encaptype = TUNNEL_ENCAP_GUE;
270 else if (strcmp(*argv, "none") == 0)
271 encaptype = TUNNEL_ENCAP_NONE;
272 else
273 invarg("Invalid encap type.", *argv);
274 } else if (strcmp(*argv, "encap-sport") == 0) {
275 NEXT_ARG();
276 if (strcmp(*argv, "auto") == 0)
277 encapsport = 0;
278 else if (get_u16(&encapsport, *argv, 0))
279 invarg("Invalid source port.", *argv);
280 } else if (strcmp(*argv, "encap-dport") == 0) {
281 NEXT_ARG();
282 if (get_u16(&encapdport, *argv, 0))
283 invarg("Invalid destination port.", *argv);
284 } else if (strcmp(*argv, "encap-csum") == 0) {
285 encapflags |= TUNNEL_ENCAP_FLAG_CSUM;
286 } else if (strcmp(*argv, "noencap-csum") == 0) {
287 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM;
288 } else if (strcmp(*argv, "encap-udp6-csum") == 0) {
289 encapflags |= TUNNEL_ENCAP_FLAG_CSUM6;
290 } else if (strcmp(*argv, "noencap-udp6-csum") == 0) {
291 encapflags |= ~TUNNEL_ENCAP_FLAG_CSUM6;
Tom Herbert858dbb22015-01-29 08:51:58 -0800292 } else if (strcmp(*argv, "encap-remcsum") == 0) {
293 encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM;
294 } else if (strcmp(*argv, "noencap-remcsum") == 0) {
295 encapflags |= ~TUNNEL_ENCAP_FLAG_REMCSUM;
Paolo Abeni926b39e2015-12-18 10:50:37 +0100296 } else if (strcmp(*argv, "external") == 0) {
297 metadata = 1;
Stephen Hemminger06125192014-02-17 10:55:31 -0800298 } else
Herbert Xu237d9e82008-10-09 15:08:24 +0800299 usage();
300 argc--; argv++;
301 }
302
303 if (!ikey && IN_MULTICAST(ntohl(daddr))) {
304 ikey = daddr;
305 iflags |= GRE_KEY;
306 }
307 if (!okey && IN_MULTICAST(ntohl(daddr))) {
308 okey = daddr;
309 oflags |= GRE_KEY;
310 }
311 if (IN_MULTICAST(ntohl(daddr)) && !saddr) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000312 fprintf(stderr, "A broadcast tunnel requires a source address.\n");
Herbert Xu237d9e82008-10-09 15:08:24 +0800313 return -1;
314 }
315
Jiri Bencdf217d52016-04-27 16:11:13 +0200316 if (!metadata) {
317 addattr32(n, 1024, IFLA_GRE_IKEY, ikey);
318 addattr32(n, 1024, IFLA_GRE_OKEY, okey);
319 addattr_l(n, 1024, IFLA_GRE_IFLAGS, &iflags, 2);
320 addattr_l(n, 1024, IFLA_GRE_OFLAGS, &oflags, 2);
321 addattr_l(n, 1024, IFLA_GRE_LOCAL, &saddr, 4);
322 addattr_l(n, 1024, IFLA_GRE_REMOTE, &daddr, 4);
323 addattr_l(n, 1024, IFLA_GRE_PMTUDISC, &pmtudisc, 1);
324 if (link)
325 addattr32(n, 1024, IFLA_GRE_LINK, link);
326 addattr_l(n, 1024, IFLA_GRE_TTL, &ttl, 1);
327 addattr_l(n, 1024, IFLA_GRE_TOS, &tos, 1);
328 } else {
329 addattr_l(n, 1024, IFLA_GRE_COLLECT_METADATA, NULL, 0);
330 }
Herbert Xu237d9e82008-10-09 15:08:24 +0800331
Tom Herbert80c24b02014-11-05 10:06:26 -0800332 addattr16(n, 1024, IFLA_GRE_ENCAP_TYPE, encaptype);
333 addattr16(n, 1024, IFLA_GRE_ENCAP_FLAGS, encapflags);
334 addattr16(n, 1024, IFLA_GRE_ENCAP_SPORT, htons(encapsport));
335 addattr16(n, 1024, IFLA_GRE_ENCAP_DPORT, htons(encapdport));
336
Herbert Xu237d9e82008-10-09 15:08:24 +0800337 return 0;
338}
339
Jiri Benc7c337e22016-04-27 16:11:14 +0200340static void gre_print_direct_opt(FILE *f, struct rtattr *tb[])
Herbert Xu237d9e82008-10-09 15:08:24 +0800341{
Herbert Xu237d9e82008-10-09 15:08:24 +0800342 char s2[64];
343 const char *local = "any";
344 const char *remote = "any";
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700345 unsigned int iflags = 0;
346 unsigned int oflags = 0;
Herbert Xu237d9e82008-10-09 15:08:24 +0800347
Herbert Xu237d9e82008-10-09 15:08:24 +0800348 if (tb[IFLA_GRE_REMOTE]) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700349 unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]);
Herbert Xu237d9e82008-10-09 15:08:24 +0800350
351 if (addr)
Phil Suttera418e452016-03-22 19:35:15 +0100352 remote = format_host(AF_INET, 4, &addr);
Herbert Xu237d9e82008-10-09 15:08:24 +0800353 }
354
355 fprintf(f, "remote %s ", remote);
356
357 if (tb[IFLA_GRE_LOCAL]) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700358 unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_LOCAL]);
Herbert Xu237d9e82008-10-09 15:08:24 +0800359
360 if (addr)
Phil Suttera418e452016-03-22 19:35:15 +0100361 local = format_host(AF_INET, 4, &addr);
Herbert Xu237d9e82008-10-09 15:08:24 +0800362 }
363
364 fprintf(f, "local %s ", local);
365
Stephen Hemmingerff247462012-04-10 08:47:55 -0700366 if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700367 unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]);
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800368 const char *n = if_indextoname(link, s2);
Herbert Xu237d9e82008-10-09 15:08:24 +0800369
370 if (n)
371 fprintf(f, "dev %s ", n);
372 else
373 fprintf(f, "dev %u ", link);
374 }
375
Stephen Hemmingerff247462012-04-10 08:47:55 -0700376 if (tb[IFLA_GRE_TTL] && rta_getattr_u8(tb[IFLA_GRE_TTL]))
377 fprintf(f, "ttl %d ", rta_getattr_u8(tb[IFLA_GRE_TTL]));
Herbert Xu237d9e82008-10-09 15:08:24 +0800378 else
379 fprintf(f, "ttl inherit ");
380
Stephen Hemmingerff247462012-04-10 08:47:55 -0700381 if (tb[IFLA_GRE_TOS] && rta_getattr_u8(tb[IFLA_GRE_TOS])) {
382 int tos = rta_getattr_u8(tb[IFLA_GRE_TOS]);
Herbert Xu237d9e82008-10-09 15:08:24 +0800383
384 fputs("tos ", f);
385 if (tos == 1)
386 fputs("inherit ", f);
387 else
388 fprintf(f, "0x%x ", tos);
389 }
390
391 if (tb[IFLA_GRE_PMTUDISC] &&
Stephen Hemmingerff247462012-04-10 08:47:55 -0700392 !rta_getattr_u8(tb[IFLA_GRE_PMTUDISC]))
Herbert Xu237d9e82008-10-09 15:08:24 +0800393 fputs("nopmtudisc ", f);
394
395 if (tb[IFLA_GRE_IFLAGS])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700396 iflags = rta_getattr_u16(tb[IFLA_GRE_IFLAGS]);
Herbert Xu237d9e82008-10-09 15:08:24 +0800397
398 if (tb[IFLA_GRE_OFLAGS])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700399 oflags = rta_getattr_u16(tb[IFLA_GRE_OFLAGS]);
Herbert Xu237d9e82008-10-09 15:08:24 +0800400
Stephen Hemminger71816552012-03-19 17:18:49 -0700401 if ((iflags & GRE_KEY) && tb[IFLA_GRE_IKEY]) {
Herbert Xu237d9e82008-10-09 15:08:24 +0800402 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_IKEY]), s2, sizeof(s2));
403 fprintf(f, "ikey %s ", s2);
404 }
405
Stephen Hemminger71816552012-03-19 17:18:49 -0700406 if ((oflags & GRE_KEY) && tb[IFLA_GRE_OKEY]) {
Herbert Xu237d9e82008-10-09 15:08:24 +0800407 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_OKEY]), s2, sizeof(s2));
Stephen Hemminger71816552012-03-19 17:18:49 -0700408 fprintf(f, "okey %s ", s2);
Herbert Xu237d9e82008-10-09 15:08:24 +0800409 }
410
411 if (iflags & GRE_SEQ)
412 fputs("iseq ", f);
413 if (oflags & GRE_SEQ)
414 fputs("oseq ", f);
415 if (iflags & GRE_CSUM)
416 fputs("icsum ", f);
417 if (oflags & GRE_CSUM)
418 fputs("ocsum ", f);
Jiri Benc7c337e22016-04-27 16:11:14 +0200419}
Tom Herbert80c24b02014-11-05 10:06:26 -0800420
Jiri Benc7c337e22016-04-27 16:11:14 +0200421static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
422{
423 if (!tb)
424 return;
425
426 if (!tb[IFLA_GRE_COLLECT_METADATA])
427 gre_print_direct_opt(f, tb);
428 else
Paolo Abeni926b39e2015-12-18 10:50:37 +0100429 fputs("external ", f);
430
Tom Herbert80c24b02014-11-05 10:06:26 -0800431 if (tb[IFLA_GRE_ENCAP_TYPE] &&
Tom Herbert0b2fbb72016-08-04 13:34:56 -0700432 rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) {
Tom Herbert80c24b02014-11-05 10:06:26 -0800433 __u16 type = rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]);
434 __u16 flags = rta_getattr_u16(tb[IFLA_GRE_ENCAP_FLAGS]);
435 __u16 sport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_SPORT]);
436 __u16 dport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_DPORT]);
437
438 fputs("encap ", f);
439 switch (type) {
440 case TUNNEL_ENCAP_FOU:
441 fputs("fou ", f);
442 break;
443 case TUNNEL_ENCAP_GUE:
444 fputs("gue ", f);
445 break;
446 default:
447 fputs("unknown ", f);
448 break;
449 }
450
451 if (sport == 0)
452 fputs("encap-sport auto ", f);
453 else
454 fprintf(f, "encap-sport %u", ntohs(sport));
455
456 fprintf(f, "encap-dport %u ", ntohs(dport));
457
458 if (flags & TUNNEL_ENCAP_FLAG_CSUM)
459 fputs("encap-csum ", f);
460 else
461 fputs("noencap-csum ", f);
462
463 if (flags & TUNNEL_ENCAP_FLAG_CSUM6)
464 fputs("encap-csum6 ", f);
465 else
466 fputs("noencap-csum6 ", f);
Tom Herbert858dbb22015-01-29 08:51:58 -0800467
468 if (flags & TUNNEL_ENCAP_FLAG_REMCSUM)
469 fputs("encap-remcsum ", f);
470 else
471 fputs("noencap-remcsum ", f);
Tom Herbert80c24b02014-11-05 10:06:26 -0800472 }
Herbert Xu237d9e82008-10-09 15:08:24 +0800473}
474
vadimk561e6502014-09-30 08:17:31 +0300475static void gre_print_help(struct link_util *lu, int argc, char **argv,
476 FILE *f)
477{
478 print_usage(f);
479}
480
Herbert Xu237d9e82008-10-09 15:08:24 +0800481struct link_util gre_link_util = {
482 .id = "gre",
483 .maxattr = IFLA_GRE_MAX,
484 .parse_opt = gre_parse_opt,
485 .print_opt = gre_print_opt,
vadimk561e6502014-09-30 08:17:31 +0300486 .print_help = gre_print_help,
Herbert Xu237d9e82008-10-09 15:08:24 +0800487};
488
489struct link_util gretap_link_util = {
490 .id = "gretap",
491 .maxattr = IFLA_GRE_MAX,
492 .parse_opt = gre_parse_opt,
493 .print_opt = gre_print_opt,
vadimk561e6502014-09-30 08:17:31 +0300494 .print_help = gre_print_help,
Herbert Xu237d9e82008-10-09 15:08:24 +0800495};