Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * link_ip6tnl.c ip6tnl 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: Nicolas Dichtel <nicolas.dichtel@6wind.com> |
| 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 | #include "rt_names.h" |
| 23 | #include "utils.h" |
| 24 | #include "ip_common.h" |
| 25 | #include "tunnel.h" |
| 26 | |
| 27 | #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000) |
| 28 | #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF) |
| 29 | |
| 30 | #define DEFAULT_TNL_HOP_LIMIT (64) |
| 31 | |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 32 | static void print_usage(FILE *f) |
| 33 | { |
| 34 | fprintf(f, "Usage: ip link { add | set | change | replace | del } NAME\n"); |
| 35 | fprintf(f, " [ mode { ip6ip6 | ipip6 | any } ]\n"); |
| 36 | fprintf(f, " type ip6tnl [ remote ADDR ] [ local ADDR ]\n"); |
| 37 | fprintf(f, " [ dev PHYS_DEV ] [ encaplimit ELIM ]\n"); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 38 | fprintf(f, " [ hoplimit HLIM ] [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n"); |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 39 | fprintf(f, " [ dscp inherit ] [ fwmark inherit ]\n"); |
Tom Herbert | 73516e1 | 2016-08-04 13:34:55 -0700 | [diff] [blame] | 40 | fprintf(f, " [ noencap ] [ encap { fou | gue | none } ]\n"); |
| 41 | fprintf(f, " [ encap-sport PORT ] [ encap-dport PORT ]\n"); |
| 42 | fprintf(f, " [ [no]encap-csum ] [ [no]encap-csum6 ] [ [no]encap-remcsum ]\n"); |
Alexei Starovoitov | 4bfe682 | 2016-09-19 17:03:14 -0700 | [diff] [blame] | 43 | fprintf(f, " [ external ]\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, " ELIM := { none | 0..255 }(default=%d)\n", |
| 48 | IPV6_DEFAULT_TNL_ENCAP_LIMIT); |
| 49 | fprintf(f, " HLIM := 0..255 (default=%d)\n", |
| 50 | DEFAULT_TNL_HOP_LIMIT); |
| 51 | fprintf(f, " TCLASS := { 0x0..0xff | inherit }\n"); |
| 52 | fprintf(f, " FLOWLABEL := { 0x0..0xfffff | inherit }\n"); |
| 53 | } |
| 54 | |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 55 | static void usage(void) __attribute__((noreturn)); |
| 56 | static void usage(void) |
| 57 | { |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 58 | print_usage(stderr); |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 59 | exit(-1); |
| 60 | } |
| 61 | |
| 62 | static int ip6tunnel_parse_opt(struct link_util *lu, int argc, char **argv, |
| 63 | struct nlmsghdr *n) |
| 64 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 65 | struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1); |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 66 | struct { |
| 67 | struct nlmsghdr n; |
| 68 | struct ifinfomsg i; |
| 69 | char buf[2048]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 70 | } req = { |
| 71 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)), |
| 72 | .n.nlmsg_flags = NLM_F_REQUEST, |
| 73 | .n.nlmsg_type = RTM_GETLINK, |
| 74 | .i.ifi_family = preferred_family, |
| 75 | .i.ifi_index = ifi->ifi_index, |
| 76 | }; |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 77 | struct rtattr *tb[IFLA_MAX + 1]; |
| 78 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; |
| 79 | struct rtattr *iptuninfo[IFLA_IPTUN_MAX + 1]; |
| 80 | int len; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 81 | struct in6_addr laddr = {}; |
| 82 | struct in6_addr raddr = {}; |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 83 | __u8 hop_limit = DEFAULT_TNL_HOP_LIMIT; |
| 84 | __u8 encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT; |
| 85 | __u32 flowinfo = 0; |
| 86 | __u32 flags = 0; |
| 87 | __u32 link = 0; |
| 88 | __u8 proto = 0; |
Tom Herbert | 73516e1 | 2016-08-04 13:34:55 -0700 | [diff] [blame] | 89 | __u16 encaptype = 0; |
| 90 | __u16 encapflags = TUNNEL_ENCAP_FLAG_CSUM6; |
| 91 | __u16 encapsport = 0; |
| 92 | __u16 encapdport = 0; |
Alexei Starovoitov | 4bfe682 | 2016-09-19 17:03:14 -0700 | [diff] [blame] | 93 | __u8 metadata = 0; |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 94 | |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 95 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 96 | if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) { |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 97 | get_failed: |
| 98 | fprintf(stderr, |
| 99 | "Failed to get existing tunnel info.\n"); |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | len = req.n.nlmsg_len; |
| 104 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
| 105 | if (len < 0) |
| 106 | goto get_failed; |
| 107 | |
| 108 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len); |
| 109 | |
| 110 | if (!tb[IFLA_LINKINFO]) |
| 111 | goto get_failed; |
| 112 | |
| 113 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); |
| 114 | |
| 115 | if (!linkinfo[IFLA_INFO_DATA]) |
| 116 | goto get_failed; |
| 117 | |
| 118 | parse_rtattr_nested(iptuninfo, IFLA_IPTUN_MAX, |
| 119 | linkinfo[IFLA_INFO_DATA]); |
| 120 | |
| 121 | if (iptuninfo[IFLA_IPTUN_LOCAL]) |
| 122 | memcpy(&laddr, RTA_DATA(iptuninfo[IFLA_IPTUN_LOCAL]), |
| 123 | sizeof(laddr)); |
| 124 | |
| 125 | if (iptuninfo[IFLA_IPTUN_REMOTE]) |
| 126 | memcpy(&raddr, RTA_DATA(iptuninfo[IFLA_IPTUN_REMOTE]), |
| 127 | sizeof(raddr)); |
| 128 | |
| 129 | if (iptuninfo[IFLA_IPTUN_TTL]) |
| 130 | hop_limit = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TTL]); |
| 131 | |
| 132 | if (iptuninfo[IFLA_IPTUN_ENCAP_LIMIT]) |
| 133 | encap_limit = rta_getattr_u8(iptuninfo[IFLA_IPTUN_ENCAP_LIMIT]); |
| 134 | |
| 135 | if (iptuninfo[IFLA_IPTUN_FLOWINFO]) |
| 136 | flowinfo = rta_getattr_u32(iptuninfo[IFLA_IPTUN_FLOWINFO]); |
| 137 | |
| 138 | if (iptuninfo[IFLA_IPTUN_FLAGS]) |
| 139 | flags = rta_getattr_u32(iptuninfo[IFLA_IPTUN_FLAGS]); |
| 140 | |
| 141 | if (iptuninfo[IFLA_IPTUN_LINK]) |
| 142 | link = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LINK]); |
| 143 | |
| 144 | if (iptuninfo[IFLA_IPTUN_PROTO]) |
| 145 | proto = rta_getattr_u8(iptuninfo[IFLA_IPTUN_PROTO]); |
Alexei Starovoitov | 4bfe682 | 2016-09-19 17:03:14 -0700 | [diff] [blame] | 146 | if (iptuninfo[IFLA_IPTUN_COLLECT_METADATA]) |
| 147 | metadata = 1; |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | while (argc > 0) { |
| 151 | if (matches(*argv, "mode") == 0) { |
| 152 | NEXT_ARG(); |
| 153 | if (strcmp(*argv, "ipv6/ipv6") == 0 || |
| 154 | strcmp(*argv, "ip6ip6") == 0) |
| 155 | proto = IPPROTO_IPV6; |
| 156 | else if (strcmp(*argv, "ip/ipv6") == 0 || |
| 157 | strcmp(*argv, "ipv4/ipv6") == 0 || |
| 158 | strcmp(*argv, "ipip6") == 0 || |
| 159 | strcmp(*argv, "ip4ip6") == 0) |
| 160 | proto = IPPROTO_IPIP; |
| 161 | else if (strcmp(*argv, "any/ipv6") == 0 || |
| 162 | strcmp(*argv, "any") == 0) |
| 163 | proto = 0; |
| 164 | else |
| 165 | invarg("Cannot guess tunnel mode.", *argv); |
| 166 | } else if (strcmp(*argv, "remote") == 0) { |
| 167 | inet_prefix addr; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 168 | |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 169 | NEXT_ARG(); |
| 170 | get_prefix(&addr, *argv, preferred_family); |
| 171 | if (addr.family == AF_UNSPEC) |
| 172 | invarg("\"remote\" address family is AF_UNSPEC", *argv); |
| 173 | memcpy(&raddr, addr.data, addr.bytelen); |
| 174 | } else if (strcmp(*argv, "local") == 0) { |
| 175 | inet_prefix addr; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 176 | |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 177 | NEXT_ARG(); |
| 178 | get_prefix(&addr, *argv, preferred_family); |
| 179 | if (addr.family == AF_UNSPEC) |
| 180 | invarg("\"local\" address family is AF_UNSPEC", *argv); |
| 181 | memcpy(&laddr, addr.data, addr.bytelen); |
| 182 | } else if (matches(*argv, "dev") == 0) { |
| 183 | NEXT_ARG(); |
| 184 | link = if_nametoindex(*argv); |
| 185 | if (link == 0) |
| 186 | invarg("\"dev\" is invalid", *argv); |
| 187 | } else if (strcmp(*argv, "hoplimit") == 0 || |
| 188 | strcmp(*argv, "ttl") == 0 || |
| 189 | strcmp(*argv, "hlim") == 0) { |
| 190 | __u8 uval; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 191 | |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 192 | NEXT_ARG(); |
| 193 | if (get_u8(&uval, *argv, 0)) |
| 194 | invarg("invalid HLIM", *argv); |
| 195 | hop_limit = uval; |
Tom Herbert | 73516e1 | 2016-08-04 13:34:55 -0700 | [diff] [blame] | 196 | } else if (strcmp(*argv, "encaplimit") == 0) { |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 197 | NEXT_ARG(); |
| 198 | if (strcmp(*argv, "none") == 0) { |
| 199 | flags |= IP6_TNL_F_IGN_ENCAP_LIMIT; |
| 200 | } else { |
| 201 | __u8 uval; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 202 | |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 203 | if (get_u8(&uval, *argv, 0) < -1) |
| 204 | invarg("invalid ELIM", *argv); |
| 205 | encap_limit = uval; |
| 206 | flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT; |
| 207 | } |
| 208 | } else if (strcmp(*argv, "tclass") == 0 || |
| 209 | strcmp(*argv, "tc") == 0 || |
| 210 | strcmp(*argv, "tos") == 0 || |
| 211 | matches(*argv, "dsfield") == 0) { |
| 212 | __u8 uval; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 213 | |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 214 | NEXT_ARG(); |
| 215 | flowinfo &= ~IP6_FLOWINFO_TCLASS; |
| 216 | if (strcmp(*argv, "inherit") == 0) |
| 217 | flags |= IP6_TNL_F_USE_ORIG_TCLASS; |
| 218 | else { |
| 219 | if (get_u8(&uval, *argv, 16)) |
| 220 | invarg("invalid TClass", *argv); |
| 221 | flowinfo |= htonl((__u32)uval << 20) & IP6_FLOWINFO_TCLASS; |
| 222 | flags &= ~IP6_TNL_F_USE_ORIG_TCLASS; |
| 223 | } |
| 224 | } else if (strcmp(*argv, "flowlabel") == 0 || |
| 225 | strcmp(*argv, "fl") == 0) { |
| 226 | __u32 uval; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 227 | |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 228 | NEXT_ARG(); |
| 229 | flowinfo &= ~IP6_FLOWINFO_FLOWLABEL; |
| 230 | if (strcmp(*argv, "inherit") == 0) |
| 231 | flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL; |
| 232 | else { |
| 233 | if (get_u32(&uval, *argv, 16)) |
| 234 | invarg("invalid Flowlabel", *argv); |
| 235 | if (uval > 0xFFFFF) |
| 236 | invarg("invalid Flowlabel", *argv); |
| 237 | flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL; |
| 238 | flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL; |
| 239 | } |
| 240 | } else if (strcmp(*argv, "dscp") == 0) { |
| 241 | NEXT_ARG(); |
| 242 | if (strcmp(*argv, "inherit") != 0) |
| 243 | invarg("not inherit", *argv); |
| 244 | flags |= IP6_TNL_F_RCV_DSCP_COPY; |
| 245 | } else if (strcmp(*argv, "fwmark") == 0) { |
| 246 | NEXT_ARG(); |
| 247 | if (strcmp(*argv, "inherit") != 0) |
| 248 | invarg("not inherit", *argv); |
| 249 | flags |= IP6_TNL_F_USE_ORIG_FWMARK; |
Tom Herbert | 73516e1 | 2016-08-04 13:34:55 -0700 | [diff] [blame] | 250 | } else if (strcmp(*argv, "noencap") == 0) { |
| 251 | encaptype = TUNNEL_ENCAP_NONE; |
| 252 | } else if (strcmp(*argv, "encap") == 0) { |
| 253 | NEXT_ARG(); |
| 254 | if (strcmp(*argv, "fou") == 0) |
| 255 | encaptype = TUNNEL_ENCAP_FOU; |
| 256 | else if (strcmp(*argv, "gue") == 0) |
| 257 | encaptype = TUNNEL_ENCAP_GUE; |
| 258 | else if (strcmp(*argv, "none") == 0) |
| 259 | encaptype = TUNNEL_ENCAP_NONE; |
| 260 | else |
| 261 | invarg("Invalid encap type.", *argv); |
| 262 | } else if (strcmp(*argv, "encap-sport") == 0) { |
| 263 | NEXT_ARG(); |
| 264 | if (strcmp(*argv, "auto") == 0) |
| 265 | encapsport = 0; |
| 266 | else if (get_u16(&encapsport, *argv, 0)) |
| 267 | invarg("Invalid source port.", *argv); |
| 268 | } else if (strcmp(*argv, "encap-dport") == 0) { |
| 269 | NEXT_ARG(); |
| 270 | if (get_u16(&encapdport, *argv, 0)) |
| 271 | invarg("Invalid destination port.", *argv); |
| 272 | } else if (strcmp(*argv, "encap-csum") == 0) { |
| 273 | encapflags |= TUNNEL_ENCAP_FLAG_CSUM; |
| 274 | } else if (strcmp(*argv, "noencap-csum") == 0) { |
| 275 | encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM; |
| 276 | } else if (strcmp(*argv, "encap-udp6-csum") == 0) { |
| 277 | encapflags |= TUNNEL_ENCAP_FLAG_CSUM6; |
| 278 | } else if (strcmp(*argv, "noencap-udp6-csum") == 0) { |
| 279 | encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM6; |
| 280 | } else if (strcmp(*argv, "encap-remcsum") == 0) { |
| 281 | encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM; |
| 282 | } else if (strcmp(*argv, "noencap-remcsum") == 0) { |
| 283 | encapflags |= ~TUNNEL_ENCAP_FLAG_REMCSUM; |
Alexei Starovoitov | 4bfe682 | 2016-09-19 17:03:14 -0700 | [diff] [blame] | 284 | } else if (strcmp(*argv, "external") == 0) { |
| 285 | metadata = 1; |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 286 | } else |
| 287 | usage(); |
| 288 | argc--, argv++; |
| 289 | } |
| 290 | |
| 291 | addattr8(n, 1024, IFLA_IPTUN_PROTO, proto); |
Alexei Starovoitov | 4bfe682 | 2016-09-19 17:03:14 -0700 | [diff] [blame] | 292 | if (metadata) { |
| 293 | addattr_l(n, 1024, IFLA_IPTUN_COLLECT_METADATA, NULL, 0); |
| 294 | return 0; |
| 295 | } |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 296 | addattr_l(n, 1024, IFLA_IPTUN_LOCAL, &laddr, sizeof(laddr)); |
| 297 | addattr_l(n, 1024, IFLA_IPTUN_REMOTE, &raddr, sizeof(raddr)); |
| 298 | addattr8(n, 1024, IFLA_IPTUN_TTL, hop_limit); |
| 299 | addattr8(n, 1024, IFLA_IPTUN_ENCAP_LIMIT, encap_limit); |
| 300 | addattr32(n, 1024, IFLA_IPTUN_FLOWINFO, flowinfo); |
| 301 | addattr32(n, 1024, IFLA_IPTUN_FLAGS, flags); |
| 302 | addattr32(n, 1024, IFLA_IPTUN_LINK, link); |
| 303 | |
Tom Herbert | 73516e1 | 2016-08-04 13:34:55 -0700 | [diff] [blame] | 304 | addattr16(n, 1024, IFLA_IPTUN_ENCAP_TYPE, encaptype); |
| 305 | addattr16(n, 1024, IFLA_IPTUN_ENCAP_FLAGS, encapflags); |
| 306 | addattr16(n, 1024, IFLA_IPTUN_ENCAP_SPORT, htons(encapsport)); |
| 307 | addattr16(n, 1024, IFLA_IPTUN_ENCAP_DPORT, htons(encapdport)); |
| 308 | |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) |
| 313 | { |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 314 | char s2[64]; |
| 315 | int flags = 0; |
| 316 | __u32 flowinfo = 0; |
| 317 | |
| 318 | if (!tb) |
| 319 | return; |
| 320 | |
| 321 | if (tb[IFLA_IPTUN_FLAGS]) |
| 322 | flags = rta_getattr_u32(tb[IFLA_IPTUN_FLAGS]); |
| 323 | |
| 324 | if (tb[IFLA_IPTUN_FLOWINFO]) |
| 325 | flowinfo = rta_getattr_u32(tb[IFLA_IPTUN_FLOWINFO]); |
| 326 | |
| 327 | if (tb[IFLA_IPTUN_PROTO]) { |
| 328 | switch (rta_getattr_u8(tb[IFLA_IPTUN_PROTO])) { |
| 329 | case IPPROTO_IPIP: |
| 330 | fprintf(f, "ipip6 "); |
| 331 | break; |
| 332 | case IPPROTO_IPV6: |
| 333 | fprintf(f, "ip6ip6 "); |
| 334 | break; |
| 335 | case 0: |
| 336 | fprintf(f, "any "); |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | if (tb[IFLA_IPTUN_REMOTE]) { |
| 342 | fprintf(f, "remote %s ", |
Phil Sutter | 7faf158 | 2016-03-22 19:35:18 +0100 | [diff] [blame] | 343 | rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_REMOTE])); |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | if (tb[IFLA_IPTUN_LOCAL]) { |
| 347 | fprintf(f, "local %s ", |
Phil Sutter | 7faf158 | 2016-03-22 19:35:18 +0100 | [diff] [blame] | 348 | rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_LOCAL])); |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 352 | unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]); |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 353 | const char *n = if_indextoname(link, s2); |
| 354 | |
| 355 | if (n) |
| 356 | fprintf(f, "dev %s ", n); |
| 357 | else |
| 358 | fprintf(f, "dev %u ", link); |
| 359 | } |
| 360 | |
| 361 | if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT) |
| 362 | printf("encaplimit none "); |
| 363 | else if (tb[IFLA_IPTUN_ENCAP_LIMIT]) |
| 364 | fprintf(f, "encaplimit %u ", |
| 365 | rta_getattr_u8(tb[IFLA_IPTUN_ENCAP_LIMIT])); |
| 366 | |
| 367 | if (tb[IFLA_IPTUN_TTL]) |
| 368 | fprintf(f, "hoplimit %u ", rta_getattr_u8(tb[IFLA_IPTUN_TTL])); |
| 369 | |
| 370 | if (flags & IP6_TNL_F_USE_ORIG_TCLASS) |
| 371 | printf("tclass inherit "); |
| 372 | else if (tb[IFLA_IPTUN_FLOWINFO]) { |
| 373 | __u32 val = ntohl(flowinfo & IP6_FLOWINFO_TCLASS); |
| 374 | |
| 375 | printf("tclass 0x%02x ", (__u8)(val >> 20)); |
| 376 | } |
| 377 | |
| 378 | if (flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) |
| 379 | printf("flowlabel inherit "); |
| 380 | else |
| 381 | printf("flowlabel 0x%05x ", ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL)); |
| 382 | |
| 383 | printf("(flowinfo 0x%08x) ", ntohl(flowinfo)); |
| 384 | |
| 385 | if (flags & IP6_TNL_F_RCV_DSCP_COPY) |
| 386 | printf("dscp inherit "); |
| 387 | |
| 388 | if (flags & IP6_TNL_F_MIP6_DEV) |
| 389 | fprintf(f, "mip6 "); |
| 390 | |
| 391 | if (flags & IP6_TNL_F_USE_ORIG_FWMARK) |
| 392 | fprintf(f, "fwmark inherit "); |
Tom Herbert | 73516e1 | 2016-08-04 13:34:55 -0700 | [diff] [blame] | 393 | |
| 394 | if (tb[IFLA_IPTUN_ENCAP_TYPE] && |
| 395 | rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE]) != |
| 396 | TUNNEL_ENCAP_NONE) { |
| 397 | __u16 type = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE]); |
| 398 | __u16 flags = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_FLAGS]); |
| 399 | __u16 sport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_SPORT]); |
| 400 | __u16 dport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_DPORT]); |
| 401 | |
| 402 | fputs("encap ", f); |
| 403 | switch (type) { |
| 404 | case TUNNEL_ENCAP_FOU: |
| 405 | fputs("fou ", f); |
| 406 | break; |
| 407 | case TUNNEL_ENCAP_GUE: |
| 408 | fputs("gue ", f); |
| 409 | break; |
| 410 | default: |
| 411 | fputs("unknown ", f); |
| 412 | break; |
| 413 | } |
| 414 | |
| 415 | if (sport == 0) |
| 416 | fputs("encap-sport auto ", f); |
| 417 | else |
| 418 | fprintf(f, "encap-sport %u", ntohs(sport)); |
| 419 | |
| 420 | fprintf(f, "encap-dport %u ", ntohs(dport)); |
| 421 | |
| 422 | if (flags & TUNNEL_ENCAP_FLAG_CSUM) |
| 423 | fputs("encap-csum ", f); |
| 424 | else |
| 425 | fputs("noencap-csum ", f); |
| 426 | |
| 427 | if (flags & TUNNEL_ENCAP_FLAG_CSUM6) |
| 428 | fputs("encap-csum6 ", f); |
| 429 | else |
| 430 | fputs("noencap-csum6 ", f); |
| 431 | |
| 432 | if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) |
| 433 | fputs("encap-remcsum ", f); |
| 434 | else |
| 435 | fputs("noencap-remcsum ", f); |
| 436 | } |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 437 | } |
| 438 | |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 439 | static void ip6tunnel_print_help(struct link_util *lu, int argc, char **argv, |
| 440 | FILE *f) |
| 441 | { |
| 442 | print_usage(f); |
| 443 | } |
| 444 | |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 445 | struct link_util ip6tnl_link_util = { |
| 446 | .id = "ip6tnl", |
| 447 | .maxattr = IFLA_IPTUN_MAX, |
| 448 | .parse_opt = ip6tunnel_parse_opt, |
| 449 | .print_opt = ip6tunnel_print_opt, |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 450 | .print_help = ip6tunnel_print_help, |
Nicolas Dichtel | 9d0efc1 | 2012-12-12 09:09:23 -0800 | [diff] [blame] | 451 | }; |