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 | { |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 53 | struct { |
| 54 | struct nlmsghdr n; |
| 55 | struct ifinfomsg i; |
Jetchko Jekov | bde5baa | 2015-05-21 16:32:24 +0200 | [diff] [blame^] | 56 | char buf[16384]; |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 57 | } req; |
| 58 | struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1); |
| 59 | struct rtattr *tb[IFLA_MAX + 1]; |
| 60 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; |
| 61 | struct rtattr *greinfo[IFLA_GRE_MAX + 1]; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 62 | __u16 iflags = 0; |
| 63 | __u16 oflags = 0; |
| 64 | unsigned ikey = 0; |
| 65 | unsigned okey = 0; |
| 66 | unsigned saddr = 0; |
| 67 | unsigned daddr = 0; |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 68 | unsigned link = 0; |
| 69 | __u8 pmtudisc = 1; |
| 70 | __u8 ttl = 0; |
| 71 | __u8 tos = 0; |
| 72 | int len; |
Tom Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 73 | __u16 encaptype = 0; |
| 74 | __u16 encapflags = 0; |
| 75 | __u16 encapsport = 0; |
| 76 | __u16 encapdport = 0; |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 77 | |
| 78 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
| 79 | memset(&req, 0, sizeof(req)); |
| 80 | |
| 81 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)); |
| 82 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 83 | req.n.nlmsg_type = RTM_GETLINK; |
| 84 | req.i.ifi_family = preferred_family; |
| 85 | req.i.ifi_index = ifi->ifi_index; |
| 86 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 87 | if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0) { |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 88 | get_failed: |
| 89 | fprintf(stderr, |
| 90 | "Failed to get existing tunnel info.\n"); |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | len = req.n.nlmsg_len; |
| 95 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
| 96 | if (len < 0) |
| 97 | goto get_failed; |
| 98 | |
| 99 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len); |
| 100 | |
| 101 | if (!tb[IFLA_LINKINFO]) |
| 102 | goto get_failed; |
| 103 | |
| 104 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); |
| 105 | |
| 106 | if (!linkinfo[IFLA_INFO_DATA]) |
| 107 | goto get_failed; |
| 108 | |
| 109 | parse_rtattr_nested(greinfo, IFLA_GRE_MAX, |
| 110 | linkinfo[IFLA_INFO_DATA]); |
| 111 | |
| 112 | if (greinfo[IFLA_GRE_IKEY]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 113 | ikey = rta_getattr_u32(greinfo[IFLA_GRE_IKEY]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 114 | |
| 115 | if (greinfo[IFLA_GRE_OKEY]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 116 | okey = rta_getattr_u32(greinfo[IFLA_GRE_OKEY]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 117 | |
| 118 | if (greinfo[IFLA_GRE_IFLAGS]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 119 | iflags = rta_getattr_u16(greinfo[IFLA_GRE_IFLAGS]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 120 | |
| 121 | if (greinfo[IFLA_GRE_OFLAGS]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 122 | oflags = rta_getattr_u16(greinfo[IFLA_GRE_OFLAGS]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 123 | |
| 124 | if (greinfo[IFLA_GRE_LOCAL]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 125 | saddr = rta_getattr_u32(greinfo[IFLA_GRE_LOCAL]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 126 | |
| 127 | if (greinfo[IFLA_GRE_REMOTE]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 128 | daddr = rta_getattr_u32(greinfo[IFLA_GRE_REMOTE]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 129 | |
| 130 | if (greinfo[IFLA_GRE_PMTUDISC]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 131 | pmtudisc = rta_getattr_u8( |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 132 | greinfo[IFLA_GRE_PMTUDISC]); |
| 133 | |
| 134 | if (greinfo[IFLA_GRE_TTL]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 135 | ttl = rta_getattr_u8(greinfo[IFLA_GRE_TTL]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 136 | |
| 137 | if (greinfo[IFLA_GRE_TOS]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 138 | tos = rta_getattr_u8(greinfo[IFLA_GRE_TOS]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 139 | |
| 140 | if (greinfo[IFLA_GRE_LINK]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 141 | link = rta_getattr_u8(greinfo[IFLA_GRE_LINK]); |
Tom Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 142 | |
| 143 | if (greinfo[IFLA_GRE_ENCAP_TYPE]) |
| 144 | encaptype = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_TYPE]); |
| 145 | if (greinfo[IFLA_GRE_ENCAP_FLAGS]) |
| 146 | encapflags = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_FLAGS]); |
| 147 | if (greinfo[IFLA_GRE_ENCAP_SPORT]) |
| 148 | encapsport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_SPORT]); |
| 149 | if (greinfo[IFLA_GRE_ENCAP_DPORT]) |
| 150 | encapdport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_DPORT]); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 151 | } |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 152 | |
| 153 | while (argc > 0) { |
| 154 | if (!matches(*argv, "key")) { |
| 155 | unsigned uval; |
| 156 | |
| 157 | NEXT_ARG(); |
| 158 | iflags |= GRE_KEY; |
| 159 | oflags |= GRE_KEY; |
| 160 | if (strchr(*argv, '.')) |
| 161 | uval = get_addr32(*argv); |
| 162 | else { |
| 163 | if (get_unsigned(&uval, *argv, 0) < 0) { |
| 164 | fprintf(stderr, |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 165 | "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] | 166 | exit(-1); |
| 167 | } |
| 168 | uval = htonl(uval); |
| 169 | } |
| 170 | |
| 171 | ikey = okey = uval; |
| 172 | } else if (!matches(*argv, "ikey")) { |
| 173 | unsigned uval; |
| 174 | |
| 175 | NEXT_ARG(); |
| 176 | iflags |= GRE_KEY; |
| 177 | if (strchr(*argv, '.')) |
| 178 | uval = get_addr32(*argv); |
| 179 | else { |
| 180 | if (get_unsigned(&uval, *argv, 0)<0) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 181 | 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] | 182 | exit(-1); |
| 183 | } |
| 184 | uval = htonl(uval); |
| 185 | } |
| 186 | ikey = uval; |
| 187 | } else if (!matches(*argv, "okey")) { |
| 188 | unsigned uval; |
| 189 | |
| 190 | NEXT_ARG(); |
| 191 | oflags |= GRE_KEY; |
| 192 | if (strchr(*argv, '.')) |
| 193 | uval = get_addr32(*argv); |
| 194 | else { |
| 195 | if (get_unsigned(&uval, *argv, 0)<0) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 196 | 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] | 197 | exit(-1); |
| 198 | } |
| 199 | uval = htonl(uval); |
| 200 | } |
| 201 | okey = uval; |
| 202 | } else if (!matches(*argv, "seq")) { |
| 203 | iflags |= GRE_SEQ; |
| 204 | oflags |= GRE_SEQ; |
| 205 | } else if (!matches(*argv, "iseq")) { |
| 206 | iflags |= GRE_SEQ; |
| 207 | } else if (!matches(*argv, "oseq")) { |
| 208 | oflags |= GRE_SEQ; |
| 209 | } else if (!matches(*argv, "csum")) { |
| 210 | iflags |= GRE_CSUM; |
| 211 | oflags |= GRE_CSUM; |
| 212 | } else if (!matches(*argv, "icsum")) { |
| 213 | iflags |= GRE_CSUM; |
| 214 | } else if (!matches(*argv, "ocsum")) { |
| 215 | oflags |= GRE_CSUM; |
| 216 | } else if (!matches(*argv, "nopmtudisc")) { |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 217 | pmtudisc = 0; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 218 | } else if (!matches(*argv, "pmtudisc")) { |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 219 | pmtudisc = 1; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 220 | } else if (!matches(*argv, "remote")) { |
| 221 | NEXT_ARG(); |
| 222 | if (strcmp(*argv, "any")) |
| 223 | daddr = get_addr32(*argv); |
| 224 | } else if (!matches(*argv, "local")) { |
| 225 | NEXT_ARG(); |
| 226 | if (strcmp(*argv, "any")) |
| 227 | saddr = get_addr32(*argv); |
| 228 | } else if (!matches(*argv, "dev")) { |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 229 | NEXT_ARG(); |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 230 | link = if_nametoindex(*argv); |
Cong Wang | 0cb6bb5 | 2014-06-03 16:06:45 -0700 | [diff] [blame] | 231 | if (link == 0) { |
| 232 | fprintf(stderr, "Cannot find device \"%s\"\n", |
| 233 | *argv); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 234 | exit(-1); |
Cong Wang | 0cb6bb5 | 2014-06-03 16:06:45 -0700 | [diff] [blame] | 235 | } |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 236 | } else if (!matches(*argv, "ttl") || |
| 237 | !matches(*argv, "hoplimit")) { |
| 238 | unsigned uval; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 239 | |
| 240 | NEXT_ARG(); |
| 241 | if (strcmp(*argv, "inherit") != 0) { |
| 242 | if (get_unsigned(&uval, *argv, 0)) |
| 243 | invarg("invalid TTL\n", *argv); |
| 244 | if (uval > 255) |
| 245 | invarg("TTL must be <= 255\n", *argv); |
| 246 | ttl = uval; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 247 | } |
| 248 | } else if (!matches(*argv, "tos") || |
| 249 | !matches(*argv, "tclass") || |
| 250 | !matches(*argv, "dsfield")) { |
| 251 | __u32 uval; |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 252 | |
| 253 | NEXT_ARG(); |
| 254 | if (strcmp(*argv, "inherit") != 0) { |
| 255 | if (rtnl_dsfield_a2n(&uval, *argv)) |
| 256 | invarg("bad TOS value", *argv); |
| 257 | tos = uval; |
| 258 | } else |
| 259 | tos = 1; |
Tom Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 260 | } else if (strcmp(*argv, "noencap") == 0) { |
| 261 | encaptype = TUNNEL_ENCAP_NONE; |
| 262 | } else if (strcmp(*argv, "encap") == 0) { |
| 263 | NEXT_ARG(); |
| 264 | if (strcmp(*argv, "fou") == 0) |
| 265 | encaptype = TUNNEL_ENCAP_FOU; |
| 266 | else if (strcmp(*argv, "gue") == 0) |
| 267 | encaptype = TUNNEL_ENCAP_GUE; |
| 268 | else if (strcmp(*argv, "none") == 0) |
| 269 | encaptype = TUNNEL_ENCAP_NONE; |
| 270 | else |
| 271 | invarg("Invalid encap type.", *argv); |
| 272 | } else if (strcmp(*argv, "encap-sport") == 0) { |
| 273 | NEXT_ARG(); |
| 274 | if (strcmp(*argv, "auto") == 0) |
| 275 | encapsport = 0; |
| 276 | else if (get_u16(&encapsport, *argv, 0)) |
| 277 | invarg("Invalid source port.", *argv); |
| 278 | } else if (strcmp(*argv, "encap-dport") == 0) { |
| 279 | NEXT_ARG(); |
| 280 | if (get_u16(&encapdport, *argv, 0)) |
| 281 | invarg("Invalid destination port.", *argv); |
| 282 | } else if (strcmp(*argv, "encap-csum") == 0) { |
| 283 | encapflags |= TUNNEL_ENCAP_FLAG_CSUM; |
| 284 | } else if (strcmp(*argv, "noencap-csum") == 0) { |
| 285 | encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM; |
| 286 | } else if (strcmp(*argv, "encap-udp6-csum") == 0) { |
| 287 | encapflags |= TUNNEL_ENCAP_FLAG_CSUM6; |
| 288 | } else if (strcmp(*argv, "noencap-udp6-csum") == 0) { |
| 289 | encapflags |= ~TUNNEL_ENCAP_FLAG_CSUM6; |
Tom Herbert | 858dbb2 | 2015-01-29 08:51:58 -0800 | [diff] [blame] | 290 | } else if (strcmp(*argv, "encap-remcsum") == 0) { |
| 291 | encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM; |
| 292 | } else if (strcmp(*argv, "noencap-remcsum") == 0) { |
| 293 | encapflags |= ~TUNNEL_ENCAP_FLAG_REMCSUM; |
Stephen Hemminger | 0612519 | 2014-02-17 10:55:31 -0800 | [diff] [blame] | 294 | } else |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 295 | usage(); |
| 296 | argc--; argv++; |
| 297 | } |
| 298 | |
| 299 | if (!ikey && IN_MULTICAST(ntohl(daddr))) { |
| 300 | ikey = daddr; |
| 301 | iflags |= GRE_KEY; |
| 302 | } |
| 303 | if (!okey && IN_MULTICAST(ntohl(daddr))) { |
| 304 | okey = daddr; |
| 305 | oflags |= GRE_KEY; |
| 306 | } |
| 307 | if (IN_MULTICAST(ntohl(daddr)) && !saddr) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 308 | fprintf(stderr, "A broadcast tunnel requires a source address.\n"); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | addattr32(n, 1024, IFLA_GRE_IKEY, ikey); |
| 313 | addattr32(n, 1024, IFLA_GRE_OKEY, okey); |
| 314 | addattr_l(n, 1024, IFLA_GRE_IFLAGS, &iflags, 2); |
| 315 | addattr_l(n, 1024, IFLA_GRE_OFLAGS, &oflags, 2); |
| 316 | addattr_l(n, 1024, IFLA_GRE_LOCAL, &saddr, 4); |
| 317 | addattr_l(n, 1024, IFLA_GRE_REMOTE, &daddr, 4); |
Herbert Xu | 72c771b | 2009-01-06 18:27:52 -0800 | [diff] [blame] | 318 | addattr_l(n, 1024, IFLA_GRE_PMTUDISC, &pmtudisc, 1); |
| 319 | if (link) |
| 320 | addattr32(n, 1024, IFLA_GRE_LINK, link); |
| 321 | addattr_l(n, 1024, IFLA_GRE_TTL, &ttl, 1); |
| 322 | addattr_l(n, 1024, IFLA_GRE_TOS, &tos, 1); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 323 | |
Tom Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 324 | addattr16(n, 1024, IFLA_GRE_ENCAP_TYPE, encaptype); |
| 325 | addattr16(n, 1024, IFLA_GRE_ENCAP_FLAGS, encapflags); |
| 326 | addattr16(n, 1024, IFLA_GRE_ENCAP_SPORT, htons(encapsport)); |
| 327 | addattr16(n, 1024, IFLA_GRE_ENCAP_DPORT, htons(encapdport)); |
| 328 | |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) |
| 333 | { |
| 334 | char s1[1024]; |
| 335 | char s2[64]; |
| 336 | const char *local = "any"; |
| 337 | const char *remote = "any"; |
| 338 | unsigned iflags = 0; |
| 339 | unsigned oflags = 0; |
| 340 | |
| 341 | if (!tb) |
| 342 | return; |
| 343 | |
| 344 | if (tb[IFLA_GRE_REMOTE]) { |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 345 | unsigned addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 346 | |
| 347 | if (addr) |
| 348 | remote = format_host(AF_INET, 4, &addr, s1, sizeof(s1)); |
| 349 | } |
| 350 | |
| 351 | fprintf(f, "remote %s ", remote); |
| 352 | |
| 353 | if (tb[IFLA_GRE_LOCAL]) { |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 354 | unsigned addr = rta_getattr_u32(tb[IFLA_GRE_LOCAL]); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 355 | |
| 356 | if (addr) |
| 357 | local = format_host(AF_INET, 4, &addr, s1, sizeof(s1)); |
| 358 | } |
| 359 | |
| 360 | fprintf(f, "local %s ", local); |
| 361 | |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 362 | if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) { |
| 363 | unsigned link = rta_getattr_u32(tb[IFLA_GRE_LINK]); |
Stephen Hemminger | ea71bea | 2010-11-28 10:35:28 -0800 | [diff] [blame] | 364 | const char *n = if_indextoname(link, s2); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 365 | |
| 366 | if (n) |
| 367 | fprintf(f, "dev %s ", n); |
| 368 | else |
| 369 | fprintf(f, "dev %u ", link); |
| 370 | } |
| 371 | |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 372 | if (tb[IFLA_GRE_TTL] && rta_getattr_u8(tb[IFLA_GRE_TTL])) |
| 373 | fprintf(f, "ttl %d ", rta_getattr_u8(tb[IFLA_GRE_TTL])); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 374 | else |
| 375 | fprintf(f, "ttl inherit "); |
| 376 | |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 377 | if (tb[IFLA_GRE_TOS] && rta_getattr_u8(tb[IFLA_GRE_TOS])) { |
| 378 | int tos = rta_getattr_u8(tb[IFLA_GRE_TOS]); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 379 | |
| 380 | fputs("tos ", f); |
| 381 | if (tos == 1) |
| 382 | fputs("inherit ", f); |
| 383 | else |
| 384 | fprintf(f, "0x%x ", tos); |
| 385 | } |
| 386 | |
| 387 | if (tb[IFLA_GRE_PMTUDISC] && |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 388 | !rta_getattr_u8(tb[IFLA_GRE_PMTUDISC])) |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 389 | fputs("nopmtudisc ", f); |
| 390 | |
| 391 | if (tb[IFLA_GRE_IFLAGS]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 392 | iflags = rta_getattr_u16(tb[IFLA_GRE_IFLAGS]); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 393 | |
| 394 | if (tb[IFLA_GRE_OFLAGS]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 395 | oflags = rta_getattr_u16(tb[IFLA_GRE_OFLAGS]); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 396 | |
Stephen Hemminger | 7181655 | 2012-03-19 17:18:49 -0700 | [diff] [blame] | 397 | if ((iflags & GRE_KEY) && tb[IFLA_GRE_IKEY]) { |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 398 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_IKEY]), s2, sizeof(s2)); |
| 399 | fprintf(f, "ikey %s ", s2); |
| 400 | } |
| 401 | |
Stephen Hemminger | 7181655 | 2012-03-19 17:18:49 -0700 | [diff] [blame] | 402 | if ((oflags & GRE_KEY) && tb[IFLA_GRE_OKEY]) { |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 403 | 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] | 404 | fprintf(f, "okey %s ", s2); |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | if (iflags & GRE_SEQ) |
| 408 | fputs("iseq ", f); |
| 409 | if (oflags & GRE_SEQ) |
| 410 | fputs("oseq ", f); |
| 411 | if (iflags & GRE_CSUM) |
| 412 | fputs("icsum ", f); |
| 413 | if (oflags & GRE_CSUM) |
| 414 | fputs("ocsum ", f); |
Tom Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 415 | |
| 416 | if (tb[IFLA_GRE_ENCAP_TYPE] && |
| 417 | *(__u16 *)RTA_DATA(tb[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) { |
| 418 | __u16 type = rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]); |
| 419 | __u16 flags = rta_getattr_u16(tb[IFLA_GRE_ENCAP_FLAGS]); |
| 420 | __u16 sport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_SPORT]); |
| 421 | __u16 dport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_DPORT]); |
| 422 | |
| 423 | fputs("encap ", f); |
| 424 | switch (type) { |
| 425 | case TUNNEL_ENCAP_FOU: |
| 426 | fputs("fou ", f); |
| 427 | break; |
| 428 | case TUNNEL_ENCAP_GUE: |
| 429 | fputs("gue ", f); |
| 430 | break; |
| 431 | default: |
| 432 | fputs("unknown ", f); |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | if (sport == 0) |
| 437 | fputs("encap-sport auto ", f); |
| 438 | else |
| 439 | fprintf(f, "encap-sport %u", ntohs(sport)); |
| 440 | |
| 441 | fprintf(f, "encap-dport %u ", ntohs(dport)); |
| 442 | |
| 443 | if (flags & TUNNEL_ENCAP_FLAG_CSUM) |
| 444 | fputs("encap-csum ", f); |
| 445 | else |
| 446 | fputs("noencap-csum ", f); |
| 447 | |
| 448 | if (flags & TUNNEL_ENCAP_FLAG_CSUM6) |
| 449 | fputs("encap-csum6 ", f); |
| 450 | else |
| 451 | fputs("noencap-csum6 ", f); |
Tom Herbert | 858dbb2 | 2015-01-29 08:51:58 -0800 | [diff] [blame] | 452 | |
| 453 | if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) |
| 454 | fputs("encap-remcsum ", f); |
| 455 | else |
| 456 | fputs("noencap-remcsum ", f); |
Tom Herbert | 80c24b0 | 2014-11-05 10:06:26 -0800 | [diff] [blame] | 457 | } |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 458 | } |
| 459 | |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 460 | static void gre_print_help(struct link_util *lu, int argc, char **argv, |
| 461 | FILE *f) |
| 462 | { |
| 463 | print_usage(f); |
| 464 | } |
| 465 | |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 466 | struct link_util gre_link_util = { |
| 467 | .id = "gre", |
| 468 | .maxattr = IFLA_GRE_MAX, |
| 469 | .parse_opt = gre_parse_opt, |
| 470 | .print_opt = gre_print_opt, |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 471 | .print_help = gre_print_help, |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | struct link_util gretap_link_util = { |
| 475 | .id = "gretap", |
| 476 | .maxattr = IFLA_GRE_MAX, |
| 477 | .parse_opt = gre_parse_opt, |
| 478 | .print_opt = gre_print_opt, |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 479 | .print_help = gre_print_help, |
Herbert Xu | 237d9e8 | 2008-10-09 15:08:24 +0800 | [diff] [blame] | 480 | }; |