Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 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> |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 15 | #include <sys/types.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <arpa/inet.h> |
| 18 | |
Stephen Hemminger | 1957a32 | 2009-03-24 15:40:54 -0700 | [diff] [blame] | 19 | #include <linux/ip.h> |
| 20 | #include <linux/if_tunnel.h> |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 21 | #include "rt_names.h" |
| 22 | #include "utils.h" |
| 23 | #include "ip_common.h" |
| 24 | #include "tunnel.h" |
| 25 | |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 26 | static 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 Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 32 | fprintf(f, " [ noencap ] [ encap { fou | gue | none } ]\n"); |
| 33 | fprintf(f, " [ encap-sport PORT ] [ encap-dport PORT ]\n"); |
Tom Herbert | 858dbb2 | 2015-01-29 08:51:58 -0800 | [diff] [blame] | 34 | fprintf(f, " [ [no]encap-csum ] [ [no]encap-csum6 ] [ [no]encap-remcsum ]\n"); |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 35 | 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 Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 43 | static void usage(void) __attribute__((noreturn)); |
| 44 | static void usage(void) |
| 45 | { |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 46 | print_usage(stderr); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 47 | exit(-1); |
| 48 | } |
| 49 | |
| 50 | static int gre_parse_opt(struct link_util *lu, int argc, char **argv, |
| 51 | struct nlmsghdr *n) |
| 52 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 53 | struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 54 | struct { |
| 55 | struct nlmsghdr n; |
| 56 | struct ifinfomsg i; |
Jetchko Jekov | bde5baa | 2015-05-21 16:32:24 +0200 | [diff] [blame] | 57 | char buf[16384]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 58 | } 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 Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 65 | struct rtattr *tb[IFLA_MAX + 1]; |
| 66 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; |
| 67 | struct rtattr *greinfo[IFLA_GRE_MAX + 1]; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 68 | __u16 iflags = 0; |
| 69 | __u16 oflags = 0; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 70 | 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 Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 75 | __u8 pmtudisc = 1; |
| 76 | __u8 ttl = 0; |
| 77 | __u8 tos = 0; |
| 78 | int len; |
Tom Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 79 | __u16 encaptype = 0; |
| 80 | __u16 encapflags = 0; |
| 81 | __u16 encapsport = 0; |
| 82 | __u16 encapdport = 0; |
Paolo Abeni | 926b39e | 2015-12-18 10:50:37 +0100 | [diff] [blame] | 83 | __u8 metadata = 0; |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 84 | |
| 85 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 86 | if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) { |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 87 | get_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 Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 112 | ikey = rta_getattr_u32(greinfo[IFLA_GRE_IKEY]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 113 | |
| 114 | if (greinfo[IFLA_GRE_OKEY]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 115 | okey = rta_getattr_u32(greinfo[IFLA_GRE_OKEY]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 116 | |
| 117 | if (greinfo[IFLA_GRE_IFLAGS]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 118 | iflags = rta_getattr_u16(greinfo[IFLA_GRE_IFLAGS]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 119 | |
| 120 | if (greinfo[IFLA_GRE_OFLAGS]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 121 | oflags = rta_getattr_u16(greinfo[IFLA_GRE_OFLAGS]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 122 | |
| 123 | if (greinfo[IFLA_GRE_LOCAL]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 124 | saddr = rta_getattr_u32(greinfo[IFLA_GRE_LOCAL]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 125 | |
| 126 | if (greinfo[IFLA_GRE_REMOTE]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 127 | daddr = rta_getattr_u32(greinfo[IFLA_GRE_REMOTE]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 128 | |
| 129 | if (greinfo[IFLA_GRE_PMTUDISC]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 130 | pmtudisc = rta_getattr_u8( |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 131 | greinfo[IFLA_GRE_PMTUDISC]); |
| 132 | |
| 133 | if (greinfo[IFLA_GRE_TTL]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 134 | ttl = rta_getattr_u8(greinfo[IFLA_GRE_TTL]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 135 | |
| 136 | if (greinfo[IFLA_GRE_TOS]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 137 | tos = rta_getattr_u8(greinfo[IFLA_GRE_TOS]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 138 | |
| 139 | if (greinfo[IFLA_GRE_LINK]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 140 | link = rta_getattr_u8(greinfo[IFLA_GRE_LINK]); |
Tom Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 141 | |
| 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 Abeni | 926b39e | 2015-12-18 10:50:37 +0100 | [diff] [blame] | 150 | |
| 151 | if (greinfo[IFLA_GRE_COLLECT_METADATA]) |
| 152 | metadata = 1; |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 153 | } |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 154 | |
| 155 | while (argc > 0) { |
| 156 | if (!matches(*argv, "key")) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 157 | unsigned int uval; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 158 | |
| 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 Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 167 | "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 168 | exit(-1); |
| 169 | } |
| 170 | uval = htonl(uval); |
| 171 | } |
| 172 | |
| 173 | ikey = okey = uval; |
| 174 | } else if (!matches(*argv, "ikey")) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 175 | unsigned int uval; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 176 | |
| 177 | NEXT_ARG(); |
| 178 | iflags |= GRE_KEY; |
| 179 | if (strchr(*argv, '.')) |
| 180 | uval = get_addr32(*argv); |
| 181 | else { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 182 | if (get_unsigned(&uval, *argv, 0) < 0) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 183 | fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 184 | exit(-1); |
| 185 | } |
| 186 | uval = htonl(uval); |
| 187 | } |
| 188 | ikey = uval; |
| 189 | } else if (!matches(*argv, "okey")) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 190 | unsigned int uval; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 191 | |
| 192 | NEXT_ARG(); |
| 193 | oflags |= GRE_KEY; |
| 194 | if (strchr(*argv, '.')) |
| 195 | uval = get_addr32(*argv); |
| 196 | else { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 197 | if (get_unsigned(&uval, *argv, 0) < 0) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 198 | fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 199 | 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 Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 219 | pmtudisc = 0; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 220 | } else if (!matches(*argv, "pmtudisc")) { |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 221 | pmtudisc = 1; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 222 | } 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 Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 231 | NEXT_ARG(); |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 232 | link = if_nametoindex(*argv); |
Cong Wang | 0cb6bb5 | 2014-06-03 16:06:45 -0700 | [diff] [blame] | 233 | if (link == 0) { |
| 234 | fprintf(stderr, "Cannot find device \"%s\"\n", |
| 235 | *argv); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 236 | exit(-1); |
Cong Wang | 0cb6bb5 | 2014-06-03 16:06:45 -0700 | [diff] [blame] | 237 | } |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 238 | } else if (!matches(*argv, "ttl") || |
| 239 | !matches(*argv, "hoplimit")) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 240 | unsigned int uval; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 241 | |
| 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 Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 249 | } |
| 250 | } else if (!matches(*argv, "tos") || |
| 251 | !matches(*argv, "tclass") || |
| 252 | !matches(*argv, "dsfield")) { |
| 253 | __u32 uval; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 254 | |
| 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 Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 262 | } 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 Herbert | 858dbb2 | 2015-01-29 08:51:58 -0800 | [diff] [blame] | 292 | } 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 Abeni | 926b39e | 2015-12-18 10:50:37 +0100 | [diff] [blame] | 296 | } else if (strcmp(*argv, "external") == 0) { |
| 297 | metadata = 1; |
Stephen Hemminger | 0612519 | 2014-02-17 10:55:31 -0800 | [diff] [blame] | 298 | } else |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 299 | 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 Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 312 | fprintf(stderr, "A broadcast tunnel requires a source address.\n"); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 313 | return -1; |
| 314 | } |
| 315 | |
Jiri Benc | df217d5 | 2016-04-27 16:11:13 +0200 | [diff] [blame] | 316 | 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 Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 331 | |
Tom Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 332 | 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 Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
Jiri Benc | 7c337e2 | 2016-04-27 16:11:14 +0200 | [diff] [blame] | 340 | static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 341 | { |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 342 | char s2[64]; |
| 343 | const char *local = "any"; |
| 344 | const char *remote = "any"; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 345 | unsigned int iflags = 0; |
| 346 | unsigned int oflags = 0; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 347 | |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 348 | if (tb[IFLA_GRE_REMOTE]) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 349 | unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 350 | |
| 351 | if (addr) |
Phil Sutter | a418e45 | 2016-03-22 19:35:15 +0100 | [diff] [blame] | 352 | remote = format_host(AF_INET, 4, &addr); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | fprintf(f, "remote %s ", remote); |
| 356 | |
| 357 | if (tb[IFLA_GRE_LOCAL]) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 358 | unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_LOCAL]); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 359 | |
| 360 | if (addr) |
Phil Sutter | a418e45 | 2016-03-22 19:35:15 +0100 | [diff] [blame] | 361 | local = format_host(AF_INET, 4, &addr); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | fprintf(f, "local %s ", local); |
| 365 | |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 366 | if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 367 | unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]); |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 368 | const char *n = if_indextoname(link, s2); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 369 | |
| 370 | if (n) |
| 371 | fprintf(f, "dev %s ", n); |
| 372 | else |
| 373 | fprintf(f, "dev %u ", link); |
| 374 | } |
| 375 | |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 376 | 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 Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 378 | else |
| 379 | fprintf(f, "ttl inherit "); |
| 380 | |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 381 | if (tb[IFLA_GRE_TOS] && rta_getattr_u8(tb[IFLA_GRE_TOS])) { |
| 382 | int tos = rta_getattr_u8(tb[IFLA_GRE_TOS]); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 383 | |
| 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 Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 392 | !rta_getattr_u8(tb[IFLA_GRE_PMTUDISC])) |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 393 | fputs("nopmtudisc ", f); |
| 394 | |
| 395 | if (tb[IFLA_GRE_IFLAGS]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 396 | iflags = rta_getattr_u16(tb[IFLA_GRE_IFLAGS]); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 397 | |
| 398 | if (tb[IFLA_GRE_OFLAGS]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 399 | oflags = rta_getattr_u16(tb[IFLA_GRE_OFLAGS]); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 400 | |
Stephen Hemminger | 7181655 | 2012-03-19 17:18:49 -0700 | [diff] [blame] | 401 | if ((iflags & GRE_KEY) && tb[IFLA_GRE_IKEY]) { |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 402 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_IKEY]), s2, sizeof(s2)); |
| 403 | fprintf(f, "ikey %s ", s2); |
| 404 | } |
| 405 | |
Stephen Hemminger | 7181655 | 2012-03-19 17:18:49 -0700 | [diff] [blame] | 406 | if ((oflags & GRE_KEY) && tb[IFLA_GRE_OKEY]) { |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 407 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_OKEY]), s2, sizeof(s2)); |
Stephen Hemminger | 7181655 | 2012-03-19 17:18:49 -0700 | [diff] [blame] | 408 | fprintf(f, "okey %s ", s2); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 409 | } |
| 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 Benc | 7c337e2 | 2016-04-27 16:11:14 +0200 | [diff] [blame] | 419 | } |
Tom Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 420 | |
Jiri Benc | 7c337e2 | 2016-04-27 16:11:14 +0200 | [diff] [blame] | 421 | static 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 Abeni | 926b39e | 2015-12-18 10:50:37 +0100 | [diff] [blame] | 429 | fputs("external ", f); |
| 430 | |
Tom Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 431 | if (tb[IFLA_GRE_ENCAP_TYPE] && |
Tom Herbert | 0b2fbb7 | 2016-08-04 13:34:56 -0700 | [diff] [blame] | 432 | rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) { |
Tom Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 433 | __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 Herbert | 858dbb2 | 2015-01-29 08:51:58 -0800 | [diff] [blame] | 467 | |
| 468 | if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) |
| 469 | fputs("encap-remcsum ", f); |
| 470 | else |
| 471 | fputs("noencap-remcsum ", f); |
Tom Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 472 | } |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 473 | } |
| 474 | |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 475 | static void gre_print_help(struct link_util *lu, int argc, char **argv, |
| 476 | FILE *f) |
| 477 | { |
| 478 | print_usage(f); |
| 479 | } |
| 480 | |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 481 | struct 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, |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 486 | .print_help = gre_print_help, |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | struct 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, |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 494 | .print_help = gre_print_help, |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 495 | }; |