xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 1 | /* |
| 2 | * link_gre6.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: Dmitry Kozlov <xeb@mail.ru> |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <string.h> |
| 14 | #include <net/if.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <arpa/inet.h> |
| 18 | |
| 19 | #include <linux/ip.h> |
| 20 | #include <linux/if_tunnel.h> |
| 21 | #include <linux/ip6_tunnel.h> |
| 22 | |
| 23 | #include "rt_names.h" |
| 24 | #include "utils.h" |
| 25 | #include "ip_common.h" |
| 26 | #include "tunnel.h" |
| 27 | |
| 28 | #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000) |
| 29 | #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF) |
| 30 | |
| 31 | #define DEFAULT_TNL_HOP_LIMIT (64) |
| 32 | |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 33 | static void print_usage(FILE *f) |
| 34 | { |
| 35 | fprintf(f, "Usage: ip link { add | set | change | replace | del } NAME\n"); |
| 36 | fprintf(f, " type { ip6gre | ip6gretap } [ remote ADDR ] [ local ADDR ]\n"); |
| 37 | fprintf(f, " [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n"); |
| 38 | fprintf(f, " [ hoplimit TTL ] [ encaplimit ELIM ]\n"); |
| 39 | fprintf(f, " [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n"); |
| 40 | fprintf(f, " [ dscp inherit ] [ dev PHYS_DEV ]\n"); |
Tom Herbert | 0b2fbb7 | 2016-08-04 13:34:56 -0700 | [diff] [blame] | 41 | fprintf(f, " [ noencap ] [ encap { fou | gue | none } ]\n"); |
| 42 | fprintf(f, " [ encap-sport PORT ] [ encap-dport PORT ]\n"); |
| 43 | fprintf(f, " [ [no]encap-csum ] [ [no]encap-csum6 ] [ [no]encap-remcsum ]\n"); |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 44 | fprintf(f, "\n"); |
| 45 | fprintf(f, "Where: NAME := STRING\n"); |
| 46 | fprintf(f, " ADDR := IPV6_ADDRESS\n"); |
| 47 | fprintf(f, " TTL := { 0..255 } (default=%d)\n", |
| 48 | DEFAULT_TNL_HOP_LIMIT); |
| 49 | fprintf(f, " KEY := { DOTTED_QUAD | NUMBER }\n"); |
| 50 | fprintf(f, " ELIM := { none | 0..255 }(default=%d)\n", |
| 51 | IPV6_DEFAULT_TNL_ENCAP_LIMIT); |
| 52 | fprintf(f, " TCLASS := { 0x0..0xff | inherit }\n"); |
| 53 | fprintf(f, " FLOWLABEL := { 0x0..0xfffff | inherit }\n"); |
| 54 | } |
| 55 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 56 | static void usage(void) __attribute__((noreturn)); |
| 57 | static void usage(void) |
| 58 | { |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 59 | print_usage(stderr); |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 60 | exit(-1); |
| 61 | } |
| 62 | |
| 63 | static int gre_parse_opt(struct link_util *lu, int argc, char **argv, |
| 64 | struct nlmsghdr *n) |
| 65 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 66 | struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1); |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 67 | struct { |
| 68 | struct nlmsghdr n; |
| 69 | struct ifinfomsg i; |
| 70 | char buf[1024]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 71 | } req = { |
| 72 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)), |
| 73 | .n.nlmsg_flags = NLM_F_REQUEST, |
| 74 | .n.nlmsg_type = RTM_GETLINK, |
| 75 | .i.ifi_family = preferred_family, |
| 76 | .i.ifi_index = ifi->ifi_index, |
| 77 | }; |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 78 | struct rtattr *tb[IFLA_MAX + 1]; |
| 79 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; |
| 80 | struct rtattr *greinfo[IFLA_GRE_MAX + 1]; |
| 81 | __u16 iflags = 0; |
| 82 | __u16 oflags = 0; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 83 | unsigned int ikey = 0; |
| 84 | unsigned int okey = 0; |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 85 | struct in6_addr raddr = IN6ADDR_ANY_INIT; |
| 86 | struct in6_addr laddr = IN6ADDR_ANY_INIT; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 87 | unsigned int link = 0; |
| 88 | unsigned int flowinfo = 0; |
| 89 | unsigned int flags = 0; |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 90 | __u8 hop_limit = DEFAULT_TNL_HOP_LIMIT; |
| 91 | __u8 encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT; |
Tom Herbert | 0b2fbb7 | 2016-08-04 13:34:56 -0700 | [diff] [blame] | 92 | __u16 encaptype = 0; |
| 93 | __u16 encapflags = TUNNEL_ENCAP_FLAG_CSUM6; |
| 94 | __u16 encapsport = 0; |
| 95 | __u16 encapdport = 0; |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 96 | int len; |
| 97 | |
| 98 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 99 | if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) { |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 100 | get_failed: |
| 101 | fprintf(stderr, |
| 102 | "Failed to get existing tunnel info.\n"); |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | len = req.n.nlmsg_len; |
| 107 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
| 108 | if (len < 0) |
| 109 | goto get_failed; |
| 110 | |
| 111 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len); |
| 112 | |
| 113 | if (!tb[IFLA_LINKINFO]) |
| 114 | goto get_failed; |
| 115 | |
| 116 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); |
| 117 | |
| 118 | if (!linkinfo[IFLA_INFO_DATA]) |
| 119 | goto get_failed; |
| 120 | |
| 121 | parse_rtattr_nested(greinfo, IFLA_GRE_MAX, |
| 122 | linkinfo[IFLA_INFO_DATA]); |
| 123 | |
| 124 | if (greinfo[IFLA_GRE_IKEY]) |
| 125 | ikey = rta_getattr_u32(greinfo[IFLA_GRE_IKEY]); |
| 126 | |
| 127 | if (greinfo[IFLA_GRE_OKEY]) |
| 128 | okey = rta_getattr_u32(greinfo[IFLA_GRE_OKEY]); |
| 129 | |
| 130 | if (greinfo[IFLA_GRE_IFLAGS]) |
| 131 | iflags = rta_getattr_u16(greinfo[IFLA_GRE_IFLAGS]); |
| 132 | |
| 133 | if (greinfo[IFLA_GRE_OFLAGS]) |
| 134 | oflags = rta_getattr_u16(greinfo[IFLA_GRE_OFLAGS]); |
| 135 | |
| 136 | if (greinfo[IFLA_GRE_LOCAL]) |
| 137 | memcpy(&laddr, RTA_DATA(greinfo[IFLA_GRE_LOCAL]), sizeof(laddr)); |
| 138 | |
| 139 | if (greinfo[IFLA_GRE_REMOTE]) |
| 140 | memcpy(&raddr, RTA_DATA(greinfo[IFLA_GRE_REMOTE]), sizeof(raddr)); |
| 141 | |
| 142 | if (greinfo[IFLA_GRE_TTL]) |
| 143 | hop_limit = rta_getattr_u8(greinfo[IFLA_GRE_TTL]); |
| 144 | |
| 145 | if (greinfo[IFLA_GRE_LINK]) |
| 146 | link = rta_getattr_u32(greinfo[IFLA_GRE_LINK]); |
| 147 | |
| 148 | if (greinfo[IFLA_GRE_ENCAP_LIMIT]) |
| 149 | encap_limit = rta_getattr_u8(greinfo[IFLA_GRE_ENCAP_LIMIT]); |
| 150 | |
| 151 | if (greinfo[IFLA_GRE_FLOWINFO]) |
| 152 | flowinfo = rta_getattr_u32(greinfo[IFLA_GRE_FLOWINFO]); |
| 153 | |
| 154 | if (greinfo[IFLA_GRE_FLAGS]) |
| 155 | flags = rta_getattr_u32(greinfo[IFLA_GRE_FLAGS]); |
Tom Herbert | 0b2fbb7 | 2016-08-04 13:34:56 -0700 | [diff] [blame] | 156 | |
| 157 | if (greinfo[IFLA_GRE_ENCAP_TYPE]) |
| 158 | encaptype = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_TYPE]); |
| 159 | |
| 160 | if (greinfo[IFLA_GRE_ENCAP_FLAGS]) |
| 161 | encapflags = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_FLAGS]); |
| 162 | |
| 163 | if (greinfo[IFLA_GRE_ENCAP_SPORT]) |
| 164 | encapsport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_SPORT]); |
| 165 | |
| 166 | if (greinfo[IFLA_GRE_ENCAP_DPORT]) |
| 167 | encapdport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_DPORT]); |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | while (argc > 0) { |
| 171 | if (!matches(*argv, "key")) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 172 | unsigned int uval; |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 173 | |
| 174 | NEXT_ARG(); |
| 175 | iflags |= GRE_KEY; |
| 176 | oflags |= GRE_KEY; |
| 177 | if (strchr(*argv, '.')) |
| 178 | uval = get_addr32(*argv); |
| 179 | else { |
| 180 | if (get_unsigned(&uval, *argv, 0) < 0) { |
| 181 | fprintf(stderr, |
| 182 | "Invalid value for \"key\"\n"); |
| 183 | exit(-1); |
| 184 | } |
| 185 | uval = htonl(uval); |
| 186 | } |
| 187 | |
| 188 | ikey = okey = uval; |
| 189 | } else if (!matches(*argv, "ikey")) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 190 | unsigned int uval; |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 191 | |
| 192 | NEXT_ARG(); |
| 193 | iflags |= 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) { |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 198 | fprintf(stderr, "invalid value of \"ikey\"\n"); |
| 199 | exit(-1); |
| 200 | } |
| 201 | uval = htonl(uval); |
| 202 | } |
| 203 | ikey = uval; |
| 204 | } else if (!matches(*argv, "okey")) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 205 | unsigned int uval; |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 206 | |
| 207 | NEXT_ARG(); |
| 208 | oflags |= GRE_KEY; |
| 209 | if (strchr(*argv, '.')) |
| 210 | uval = get_addr32(*argv); |
| 211 | else { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 212 | if (get_unsigned(&uval, *argv, 0) < 0) { |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 213 | fprintf(stderr, "invalid value of \"okey\"\n"); |
| 214 | exit(-1); |
| 215 | } |
| 216 | uval = htonl(uval); |
| 217 | } |
| 218 | okey = uval; |
| 219 | } else if (!matches(*argv, "seq")) { |
| 220 | iflags |= GRE_SEQ; |
| 221 | oflags |= GRE_SEQ; |
| 222 | } else if (!matches(*argv, "iseq")) { |
| 223 | iflags |= GRE_SEQ; |
| 224 | } else if (!matches(*argv, "oseq")) { |
| 225 | oflags |= GRE_SEQ; |
| 226 | } else if (!matches(*argv, "csum")) { |
| 227 | iflags |= GRE_CSUM; |
| 228 | oflags |= GRE_CSUM; |
| 229 | } else if (!matches(*argv, "icsum")) { |
| 230 | iflags |= GRE_CSUM; |
| 231 | } else if (!matches(*argv, "ocsum")) { |
| 232 | oflags |= GRE_CSUM; |
| 233 | } else if (!matches(*argv, "remote")) { |
| 234 | inet_prefix addr; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 235 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 236 | NEXT_ARG(); |
| 237 | get_prefix(&addr, *argv, preferred_family); |
| 238 | if (addr.family == AF_UNSPEC) |
| 239 | invarg("\"remote\" address family is AF_UNSPEC", *argv); |
| 240 | memcpy(&raddr, &addr.data, sizeof(raddr)); |
| 241 | } else if (!matches(*argv, "local")) { |
| 242 | inet_prefix addr; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 243 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 244 | NEXT_ARG(); |
| 245 | get_prefix(&addr, *argv, preferred_family); |
| 246 | if (addr.family == AF_UNSPEC) |
| 247 | invarg("\"local\" address family is AF_UNSPEC", *argv); |
| 248 | memcpy(&laddr, &addr.data, sizeof(laddr)); |
| 249 | } else if (!matches(*argv, "dev")) { |
| 250 | NEXT_ARG(); |
| 251 | link = if_nametoindex(*argv); |
Cong Wang | 0cb6bb5 | 2014-06-03 16:06:45 -0700 | [diff] [blame] | 252 | if (link == 0) { |
| 253 | fprintf(stderr, "Cannot find device \"%s\"\n", |
| 254 | *argv); |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 255 | exit(-1); |
Cong Wang | 0cb6bb5 | 2014-06-03 16:06:45 -0700 | [diff] [blame] | 256 | } |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 257 | } else if (!matches(*argv, "ttl") || |
| 258 | !matches(*argv, "hoplimit")) { |
| 259 | __u8 uval; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 260 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 261 | NEXT_ARG(); |
| 262 | if (get_u8(&uval, *argv, 0)) |
| 263 | invarg("invalid TTL", *argv); |
| 264 | hop_limit = uval; |
| 265 | } else if (!matches(*argv, "tos") || |
| 266 | !matches(*argv, "tclass") || |
| 267 | !matches(*argv, "dsfield")) { |
| 268 | __u8 uval; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 269 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 270 | NEXT_ARG(); |
| 271 | if (strcmp(*argv, "inherit") == 0) |
| 272 | flags |= IP6_TNL_F_USE_ORIG_TCLASS; |
| 273 | else { |
| 274 | if (get_u8(&uval, *argv, 16)) |
| 275 | invarg("invalid TClass", *argv); |
| 276 | flowinfo |= htonl((__u32)uval << 20) & IP6_FLOWINFO_TCLASS; |
| 277 | flags &= ~IP6_TNL_F_USE_ORIG_TCLASS; |
| 278 | } |
| 279 | } else if (strcmp(*argv, "flowlabel") == 0 || |
| 280 | strcmp(*argv, "fl") == 0) { |
| 281 | __u32 uval; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 282 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 283 | NEXT_ARG(); |
| 284 | if (strcmp(*argv, "inherit") == 0) |
| 285 | flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL; |
| 286 | else { |
| 287 | if (get_u32(&uval, *argv, 16)) |
| 288 | invarg("invalid Flowlabel", *argv); |
| 289 | if (uval > 0xFFFFF) |
| 290 | invarg("invalid Flowlabel", *argv); |
| 291 | flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL; |
| 292 | flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL; |
| 293 | } |
| 294 | } else if (strcmp(*argv, "dscp") == 0) { |
| 295 | NEXT_ARG(); |
| 296 | if (strcmp(*argv, "inherit") != 0) |
| 297 | invarg("not inherit", *argv); |
| 298 | flags |= IP6_TNL_F_RCV_DSCP_COPY; |
Tom Herbert | 0b2fbb7 | 2016-08-04 13:34:56 -0700 | [diff] [blame] | 299 | } else if (strcmp(*argv, "noencap") == 0) { |
| 300 | encaptype = TUNNEL_ENCAP_NONE; |
| 301 | } else if (strcmp(*argv, "encap") == 0) { |
| 302 | NEXT_ARG(); |
| 303 | if (strcmp(*argv, "fou") == 0) |
| 304 | encaptype = TUNNEL_ENCAP_FOU; |
| 305 | else if (strcmp(*argv, "gue") == 0) |
| 306 | encaptype = TUNNEL_ENCAP_GUE; |
| 307 | else if (strcmp(*argv, "none") == 0) |
| 308 | encaptype = TUNNEL_ENCAP_NONE; |
| 309 | else |
| 310 | invarg("Invalid encap type.", *argv); |
| 311 | } else if (strcmp(*argv, "encap-sport") == 0) { |
| 312 | NEXT_ARG(); |
| 313 | if (strcmp(*argv, "auto") == 0) |
| 314 | encapsport = 0; |
| 315 | else if (get_u16(&encapsport, *argv, 0)) |
| 316 | invarg("Invalid source port.", *argv); |
| 317 | } else if (strcmp(*argv, "encap-dport") == 0) { |
| 318 | NEXT_ARG(); |
| 319 | if (get_u16(&encapdport, *argv, 0)) |
| 320 | invarg("Invalid destination port.", *argv); |
| 321 | } else if (strcmp(*argv, "encap-csum") == 0) { |
| 322 | encapflags |= TUNNEL_ENCAP_FLAG_CSUM; |
| 323 | } else if (strcmp(*argv, "noencap-csum") == 0) { |
| 324 | encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM; |
| 325 | } else if (strcmp(*argv, "encap-udp6-csum") == 0) { |
| 326 | encapflags |= TUNNEL_ENCAP_FLAG_CSUM6; |
| 327 | } else if (strcmp(*argv, "noencap-udp6-csum") == 0) { |
| 328 | encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM6; |
| 329 | } else if (strcmp(*argv, "encap-remcsum") == 0) { |
| 330 | encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM; |
| 331 | } else if (strcmp(*argv, "noencap-remcsum") == 0) { |
| 332 | encapflags &= ~TUNNEL_ENCAP_FLAG_REMCSUM; |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 333 | } else |
| 334 | usage(); |
| 335 | argc--; argv++; |
| 336 | } |
| 337 | |
| 338 | addattr32(n, 1024, IFLA_GRE_IKEY, ikey); |
| 339 | addattr32(n, 1024, IFLA_GRE_OKEY, okey); |
| 340 | addattr_l(n, 1024, IFLA_GRE_IFLAGS, &iflags, 2); |
| 341 | addattr_l(n, 1024, IFLA_GRE_OFLAGS, &oflags, 2); |
| 342 | addattr_l(n, 1024, IFLA_GRE_LOCAL, &laddr, sizeof(laddr)); |
| 343 | addattr_l(n, 1024, IFLA_GRE_REMOTE, &raddr, sizeof(raddr)); |
| 344 | if (link) |
| 345 | addattr32(n, 1024, IFLA_GRE_LINK, link); |
| 346 | addattr_l(n, 1024, IFLA_GRE_TTL, &hop_limit, 1); |
| 347 | addattr_l(n, 1024, IFLA_GRE_ENCAP_LIMIT, &encap_limit, 1); |
| 348 | addattr_l(n, 1024, IFLA_GRE_FLOWINFO, &flowinfo, 4); |
| 349 | addattr_l(n, 1024, IFLA_GRE_FLAGS, &flowinfo, 4); |
| 350 | |
Tom Herbert | 0b2fbb7 | 2016-08-04 13:34:56 -0700 | [diff] [blame] | 351 | addattr16(n, 1024, IFLA_GRE_ENCAP_TYPE, encaptype); |
| 352 | addattr16(n, 1024, IFLA_GRE_ENCAP_FLAGS, encapflags); |
| 353 | addattr16(n, 1024, IFLA_GRE_ENCAP_SPORT, htons(encapsport)); |
| 354 | addattr16(n, 1024, IFLA_GRE_ENCAP_DPORT, htons(encapdport)); |
| 355 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) |
| 360 | { |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 361 | char s2[64]; |
| 362 | const char *local = "any"; |
| 363 | const char *remote = "any"; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 364 | unsigned int iflags = 0; |
| 365 | unsigned int oflags = 0; |
| 366 | unsigned int flags = 0; |
| 367 | unsigned int flowinfo = 0; |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 368 | struct in6_addr in6_addr_any = IN6ADDR_ANY_INIT; |
| 369 | |
| 370 | if (!tb) |
| 371 | return; |
| 372 | |
| 373 | if (tb[IFLA_GRE_FLAGS]) |
| 374 | flags = rta_getattr_u32(tb[IFLA_GRE_FLAGS]); |
| 375 | |
| 376 | if (tb[IFLA_GRE_FLOWINFO]) |
| 377 | flags = rta_getattr_u32(tb[IFLA_GRE_FLOWINFO]); |
| 378 | |
| 379 | if (tb[IFLA_GRE_REMOTE]) { |
| 380 | struct in6_addr addr; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 381 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 382 | memcpy(&addr, RTA_DATA(tb[IFLA_GRE_REMOTE]), sizeof(addr)); |
| 383 | |
| 384 | if (memcmp(&addr, &in6_addr_any, sizeof(addr))) |
Phil Sutter | a418e45 | 2016-03-22 19:35:15 +0100 | [diff] [blame] | 385 | remote = format_host(AF_INET6, sizeof(addr), &addr); |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | fprintf(f, "remote %s ", remote); |
| 389 | |
| 390 | if (tb[IFLA_GRE_LOCAL]) { |
| 391 | struct in6_addr addr; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 392 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 393 | memcpy(&addr, RTA_DATA(tb[IFLA_GRE_LOCAL]), sizeof(addr)); |
| 394 | |
| 395 | if (memcmp(&addr, &in6_addr_any, sizeof(addr))) |
Phil Sutter | a418e45 | 2016-03-22 19:35:15 +0100 | [diff] [blame] | 396 | local = format_host(AF_INET6, sizeof(addr), &addr); |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | fprintf(f, "local %s ", local); |
| 400 | |
| 401 | if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 402 | unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]); |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 403 | const char *n = if_indextoname(link, s2); |
| 404 | |
| 405 | if (n) |
| 406 | fprintf(f, "dev %s ", n); |
| 407 | else |
| 408 | fprintf(f, "dev %u ", link); |
| 409 | } |
| 410 | |
| 411 | if (tb[IFLA_GRE_TTL] && rta_getattr_u8(tb[IFLA_GRE_TTL])) |
| 412 | fprintf(f, "hoplimit %d ", rta_getattr_u8(tb[IFLA_GRE_TTL])); |
| 413 | |
| 414 | if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT) |
| 415 | fprintf(f, "encaplimit none "); |
| 416 | else if (tb[IFLA_GRE_ENCAP_LIMIT]) { |
| 417 | int encap_limit = rta_getattr_u8(tb[IFLA_GRE_ENCAP_LIMIT]); |
| 418 | |
| 419 | fprintf(f, "encaplimit %d ", encap_limit); |
| 420 | } |
| 421 | |
| 422 | if (flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) |
| 423 | fprintf(f, "flowlabel inherit "); |
| 424 | else |
| 425 | fprintf(f, "flowlabel 0x%05x ", ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL)); |
| 426 | |
| 427 | if (flags & IP6_TNL_F_RCV_DSCP_COPY) |
| 428 | fprintf(f, "dscp inherit "); |
| 429 | |
| 430 | if (tb[IFLA_GRE_IFLAGS]) |
| 431 | iflags = rta_getattr_u16(tb[IFLA_GRE_IFLAGS]); |
| 432 | |
| 433 | if (tb[IFLA_GRE_OFLAGS]) |
| 434 | oflags = rta_getattr_u16(tb[IFLA_GRE_OFLAGS]); |
| 435 | |
| 436 | if ((iflags & GRE_KEY) && tb[IFLA_GRE_IKEY]) { |
| 437 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_IKEY]), s2, sizeof(s2)); |
| 438 | fprintf(f, "ikey %s ", s2); |
| 439 | } |
| 440 | |
| 441 | if ((oflags & GRE_KEY) && tb[IFLA_GRE_OKEY]) { |
| 442 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_OKEY]), s2, sizeof(s2)); |
| 443 | fprintf(f, "okey %s ", s2); |
| 444 | } |
| 445 | |
| 446 | if (iflags & GRE_SEQ) |
| 447 | fputs("iseq ", f); |
| 448 | if (oflags & GRE_SEQ) |
| 449 | fputs("oseq ", f); |
| 450 | if (iflags & GRE_CSUM) |
| 451 | fputs("icsum ", f); |
| 452 | if (oflags & GRE_CSUM) |
| 453 | fputs("ocsum ", f); |
Tom Herbert | 0b2fbb7 | 2016-08-04 13:34:56 -0700 | [diff] [blame] | 454 | |
| 455 | if (tb[IFLA_GRE_ENCAP_TYPE] && |
| 456 | rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) { |
| 457 | __u16 type = rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]); |
| 458 | __u16 flags = rta_getattr_u16(tb[IFLA_GRE_ENCAP_FLAGS]); |
| 459 | __u16 sport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_SPORT]); |
| 460 | __u16 dport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_DPORT]); |
| 461 | |
| 462 | fputs("encap ", f); |
| 463 | switch (type) { |
| 464 | case TUNNEL_ENCAP_FOU: |
| 465 | fputs("fou ", f); |
| 466 | break; |
| 467 | case TUNNEL_ENCAP_GUE: |
| 468 | fputs("gue ", f); |
| 469 | break; |
| 470 | default: |
| 471 | fputs("unknown ", f); |
| 472 | break; |
| 473 | } |
| 474 | |
| 475 | if (sport == 0) |
| 476 | fputs("encap-sport auto ", f); |
| 477 | else |
| 478 | fprintf(f, "encap-sport %u", ntohs(sport)); |
| 479 | |
| 480 | fprintf(f, "encap-dport %u ", ntohs(dport)); |
| 481 | |
| 482 | if (flags & TUNNEL_ENCAP_FLAG_CSUM) |
| 483 | fputs("encap-csum ", f); |
| 484 | else |
| 485 | fputs("noencap-csum ", f); |
| 486 | |
| 487 | if (flags & TUNNEL_ENCAP_FLAG_CSUM6) |
| 488 | fputs("encap-csum6 ", f); |
| 489 | else |
| 490 | fputs("noencap-csum6 ", f); |
| 491 | |
| 492 | if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) |
| 493 | fputs("encap-remcsum ", f); |
| 494 | else |
| 495 | fputs("noencap-remcsum ", f); |
| 496 | } |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 497 | } |
| 498 | |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 499 | static void gre_print_help(struct link_util *lu, int argc, char **argv, |
| 500 | FILE *f) |
| 501 | { |
| 502 | print_usage(f); |
| 503 | } |
| 504 | |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 505 | struct link_util ip6gre_link_util = { |
| 506 | .id = "ip6gre", |
| 507 | .maxattr = IFLA_GRE_MAX, |
| 508 | .parse_opt = gre_parse_opt, |
| 509 | .print_opt = gre_print_opt, |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 510 | .print_help = gre_print_help, |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 511 | }; |
| 512 | |
| 513 | struct link_util ip6gretap_link_util = { |
| 514 | .id = "ip6gretap", |
| 515 | .maxattr = IFLA_GRE_MAX, |
| 516 | .parse_opt = gre_parse_opt, |
| 517 | .print_opt = gre_print_opt, |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 518 | .print_help = gre_print_help, |
xeb@mail.ru | af89576 | 2013-09-28 11:32:51 +0400 | [diff] [blame] | 519 | }; |