1/* 2 * link_iptnl.c ipip and sit 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 "rt_names.h" 22#include "utils.h" 23#include "ip_common.h" 24#include "tunnel.h" 25 26static void print_usage(FILE *f, int sit) 27{ 28 fprintf(f, "Usage: ip link { add | set | change | replace | del } NAME\n"); 29 fprintf(f, " type { ipip | sit } [ remote ADDR ] [ local ADDR ]\n"); 30 fprintf(f, " [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n"); 31 fprintf(f, " [ 6rd-prefix ADDR ] [ 6rd-relay_prefix ADDR ] [ 6rd-reset ]\n"); 32 fprintf(f, " [ noencap ] [ encap { fou | gue | none } ]\n"); 33 fprintf(f, " [ encap-sport PORT ] [ encap-dport PORT ]\n"); 34 fprintf(f, " [ [no]encap-csum ] [ [no]encap-csum6 ] [ [no]encap-remcsum ]\n"); 35 if (sit) { 36 fprintf(f, " [ mode { ip6ip | ipip | any } ]\n"); 37 fprintf(f, " [ isatap ]\n"); 38 } 39 fprintf(f, "\n"); 40 fprintf(f, "Where: NAME := STRING\n"); 41 fprintf(f, " ADDR := { IP_ADDRESS | any }\n"); 42 fprintf(f, " TOS := { NUMBER | inherit }\n"); 43 fprintf(f, " TTL := { 1..255 | inherit }\n"); 44} 45 46static void usage(int sit) __attribute__((noreturn)); 47static void usage(int sit) 48{ 49 print_usage(stderr, sit); 50 exit(-1); 51} 52 53static int iptunnel_parse_opt(struct link_util *lu, int argc, char **argv, 54 struct nlmsghdr *n) 55{ 56 struct { 57 struct nlmsghdr n; 58 struct ifinfomsg i; 59 char buf[2048]; 60 } req; 61 struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1); 62 struct rtattr *tb[IFLA_MAX + 1]; 63 struct rtattr *linkinfo[IFLA_INFO_MAX+1]; 64 struct rtattr *iptuninfo[IFLA_IPTUN_MAX + 1]; 65 int len; 66 __u32 link = 0; 67 __u32 laddr = 0; 68 __u32 raddr = 0; 69 __u8 ttl = 0; 70 __u8 tos = 0; 71 __u8 pmtudisc = 1; 72 __u16 iflags = 0; 73 __u8 proto = 0; 74 struct in6_addr ip6rdprefix; 75 __u16 ip6rdprefixlen = 0; 76 __u32 ip6rdrelayprefix = 0; 77 __u16 ip6rdrelayprefixlen = 0; 78 __u16 encaptype = 0; 79 __u16 encapflags = 0; 80 __u16 encapsport = 0; 81 __u16 encapdport = 0; 82 83 memset(&ip6rdprefix, 0, sizeof(ip6rdprefix)); 84 85 if (!(n->nlmsg_flags & NLM_F_CREATE)) { 86 memset(&req, 0, sizeof(req)); 87 88 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)); 89 req.n.nlmsg_flags = NLM_F_REQUEST; 90 req.n.nlmsg_type = RTM_GETLINK; 91 req.i.ifi_family = preferred_family; 92 req.i.ifi_index = ifi->ifi_index; 93 94 if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) { 95get_failed: 96 fprintf(stderr, 97 "Failed to get existing tunnel info.\n"); 98 return -1; 99 } 100 101 len = req.n.nlmsg_len; 102 len -= NLMSG_LENGTH(sizeof(*ifi)); 103 if (len < 0) 104 goto get_failed; 105 106 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len); 107 108 if (!tb[IFLA_LINKINFO]) 109 goto get_failed; 110 111 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); 112 113 if (!linkinfo[IFLA_INFO_DATA]) 114 goto get_failed; 115 116 parse_rtattr_nested(iptuninfo, IFLA_IPTUN_MAX, 117 linkinfo[IFLA_INFO_DATA]); 118 119 if (iptuninfo[IFLA_IPTUN_LOCAL]) 120 laddr = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LOCAL]); 121 122 if (iptuninfo[IFLA_IPTUN_REMOTE]) 123 raddr = rta_getattr_u32(iptuninfo[IFLA_IPTUN_REMOTE]); 124 125 if (iptuninfo[IFLA_IPTUN_TTL]) 126 ttl = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TTL]); 127 128 if (iptuninfo[IFLA_IPTUN_TOS]) 129 tos = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TOS]); 130 131 if (iptuninfo[IFLA_IPTUN_PMTUDISC]) 132 pmtudisc = 133 rta_getattr_u8(iptuninfo[IFLA_IPTUN_PMTUDISC]); 134 135 if (iptuninfo[IFLA_IPTUN_FLAGS]) 136 iflags = rta_getattr_u16(iptuninfo[IFLA_IPTUN_FLAGS]); 137 138 if (iptuninfo[IFLA_IPTUN_LINK]) 139 link = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LINK]); 140 141 if (iptuninfo[IFLA_IPTUN_PROTO]) 142 proto = rta_getattr_u8(iptuninfo[IFLA_IPTUN_PROTO]); 143 144 if (iptuninfo[IFLA_IPTUN_ENCAP_TYPE]) 145 encaptype = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_TYPE]); 146 if (iptuninfo[IFLA_IPTUN_ENCAP_FLAGS]) 147 encapflags = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_FLAGS]); 148 if (iptuninfo[IFLA_IPTUN_ENCAP_SPORT]) 149 encapsport = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_SPORT]); 150 if (iptuninfo[IFLA_IPTUN_ENCAP_DPORT]) 151 encapdport = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_DPORT]); 152 if (iptuninfo[IFLA_IPTUN_6RD_PREFIX]) 153 memcpy(&ip6rdprefix, 154 RTA_DATA(iptuninfo[IFLA_IPTUN_6RD_PREFIX]), 155 sizeof(laddr)); 156 157 if (iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN]) 158 ip6rdprefixlen = 159 rta_getattr_u16(iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN]); 160 161 if (iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX]) 162 ip6rdrelayprefix = 163 rta_getattr_u32(iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX]); 164 165 if (iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) 166 ip6rdrelayprefixlen = 167 rta_getattr_u16(iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]); 168 } 169 170 while (argc > 0) { 171 if (strcmp(*argv, "remote") == 0) { 172 NEXT_ARG(); 173 if (strcmp(*argv, "any")) 174 raddr = get_addr32(*argv); 175 else 176 raddr = 0; 177 } else if (strcmp(*argv, "local") == 0) { 178 NEXT_ARG(); 179 if (strcmp(*argv, "any")) 180 laddr = get_addr32(*argv); 181 else 182 laddr = 0; 183 } else if (matches(*argv, "dev") == 0) { 184 NEXT_ARG(); 185 link = if_nametoindex(*argv); 186 if (link == 0) 187 invarg("\"dev\" is invalid", *argv); 188 } else if (strcmp(*argv, "ttl") == 0 || 189 strcmp(*argv, "hoplimit") == 0) { 190 NEXT_ARG(); 191 if (strcmp(*argv, "inherit") != 0) { 192 if (get_u8(&ttl, *argv, 0)) 193 invarg("invalid TTL\n", *argv); 194 } else 195 ttl = 0; 196 } else if (strcmp(*argv, "tos") == 0 || 197 strcmp(*argv, "tclass") == 0 || 198 matches(*argv, "dsfield") == 0) { 199 __u32 uval; 200 NEXT_ARG(); 201 if (strcmp(*argv, "inherit") != 0) { 202 if (rtnl_dsfield_a2n(&uval, *argv)) 203 invarg("bad TOS value", *argv); 204 tos = uval; 205 } else 206 tos = 1; 207 } else if (strcmp(*argv, "nopmtudisc") == 0) { 208 pmtudisc = 0; 209 } else if (strcmp(*argv, "pmtudisc") == 0) { 210 pmtudisc = 1; 211 } else if (strcmp(lu->id, "sit") == 0 && 212 strcmp(*argv, "isatap") == 0) { 213 iflags |= SIT_ISATAP; 214 } else if (strcmp(lu->id, "sit") == 0 && 215 strcmp(*argv, "mode") == 0) { 216 NEXT_ARG(); 217 if (strcmp(*argv, "ipv6/ipv4") == 0 || 218 strcmp(*argv, "ip6ip") == 0) 219 proto = IPPROTO_IPV6; 220 else if (strcmp(*argv, "ipv4/ipv4") == 0 || 221 strcmp(*argv, "ipip") == 0 || 222 strcmp(*argv, "ip4ip4") == 0) 223 proto = IPPROTO_IPIP; 224 else if (strcmp(*argv, "any/ipv4") == 0 || 225 strcmp(*argv, "any") == 0) 226 proto = 0; 227 else 228 invarg("Cannot guess tunnel mode.", *argv); 229 } else if (strcmp(*argv, "noencap") == 0) { 230 encaptype = TUNNEL_ENCAP_NONE; 231 } else if (strcmp(*argv, "encap") == 0) { 232 NEXT_ARG(); 233 if (strcmp(*argv, "fou") == 0) 234 encaptype = TUNNEL_ENCAP_FOU; 235 else if (strcmp(*argv, "gue") == 0) 236 encaptype = TUNNEL_ENCAP_GUE; 237 else if (strcmp(*argv, "none") == 0) 238 encaptype = TUNNEL_ENCAP_NONE; 239 else 240 invarg("Invalid encap type.", *argv); 241 } else if (strcmp(*argv, "encap-sport") == 0) { 242 NEXT_ARG(); 243 if (strcmp(*argv, "auto") == 0) 244 encapsport = 0; 245 else if (get_u16(&encapsport, *argv, 0)) 246 invarg("Invalid source port.", *argv); 247 } else if (strcmp(*argv, "encap-dport") == 0) { 248 NEXT_ARG(); 249 if (get_u16(&encapdport, *argv, 0)) 250 invarg("Invalid destination port.", *argv); 251 } else if (strcmp(*argv, "encap-csum") == 0) { 252 encapflags |= TUNNEL_ENCAP_FLAG_CSUM; 253 } else if (strcmp(*argv, "noencap-csum") == 0) { 254 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM; 255 } else if (strcmp(*argv, "encap-udp6-csum") == 0) { 256 encapflags |= TUNNEL_ENCAP_FLAG_CSUM6; 257 } else if (strcmp(*argv, "noencap-udp6-csum") == 0) { 258 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM6; 259 } else if (strcmp(*argv, "encap-remcsum") == 0) { 260 encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM; 261 } else if (strcmp(*argv, "noencap-remcsum") == 0) { 262 encapflags &= ~TUNNEL_ENCAP_FLAG_REMCSUM; 263 } else if (strcmp(*argv, "6rd-prefix") == 0) { 264 inet_prefix prefix; 265 NEXT_ARG(); 266 if (get_prefix(&prefix, *argv, AF_INET6)) 267 invarg("invalid 6rd_prefix\n", *argv); 268 memcpy(&ip6rdprefix, prefix.data, 16); 269 ip6rdprefixlen = prefix.bitlen; 270 } else if (strcmp(*argv, "6rd-relay_prefix") == 0) { 271 inet_prefix prefix; 272 NEXT_ARG(); 273 if (get_prefix(&prefix, *argv, AF_INET)) 274 invarg("invalid 6rd-relay_prefix\n", *argv); 275 memcpy(&ip6rdrelayprefix, prefix.data, 4); 276 ip6rdrelayprefixlen = prefix.bitlen; 277 } else if (strcmp(*argv, "6rd-reset") == 0) { 278 inet_prefix prefix; 279 get_prefix(&prefix, "2002::", AF_INET6); 280 memcpy(&ip6rdprefix, prefix.data, 16); 281 ip6rdprefixlen = 16; 282 ip6rdrelayprefix = 0; 283 ip6rdrelayprefixlen = 0; 284 } else 285 usage(strcmp(lu->id, "sit") == 0); 286 argc--, argv++; 287 } 288 289 if (ttl && pmtudisc == 0) { 290 fprintf(stderr, "ttl != 0 and nopmtudisc are incompatible\n"); 291 exit(-1); 292 } 293 294 addattr32(n, 1024, IFLA_IPTUN_LINK, link); 295 addattr32(n, 1024, IFLA_IPTUN_LOCAL, laddr); 296 addattr32(n, 1024, IFLA_IPTUN_REMOTE, raddr); 297 addattr8(n, 1024, IFLA_IPTUN_TTL, ttl); 298 addattr8(n, 1024, IFLA_IPTUN_TOS, tos); 299 addattr8(n, 1024, IFLA_IPTUN_PMTUDISC, pmtudisc); 300 301 addattr16(n, 1024, IFLA_IPTUN_ENCAP_TYPE, encaptype); 302 addattr16(n, 1024, IFLA_IPTUN_ENCAP_FLAGS, encapflags); 303 addattr16(n, 1024, IFLA_IPTUN_ENCAP_SPORT, htons(encapsport)); 304 addattr16(n, 1024, IFLA_IPTUN_ENCAP_DPORT, htons(encapdport)); 305 306 if (strcmp(lu->id, "sit") == 0) { 307 addattr16(n, 1024, IFLA_IPTUN_FLAGS, iflags); 308 addattr8(n, 1024, IFLA_IPTUN_PROTO, proto); 309 if (ip6rdprefixlen) { 310 addattr_l(n, 1024, IFLA_IPTUN_6RD_PREFIX, 311 &ip6rdprefix, sizeof(ip6rdprefix)); 312 addattr16(n, 1024, IFLA_IPTUN_6RD_PREFIXLEN, 313 ip6rdprefixlen); 314 addattr32(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIX, 315 ip6rdrelayprefix); 316 addattr16(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIXLEN, 317 ip6rdrelayprefixlen); 318 } 319 } 320 321 return 0; 322} 323 324static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) 325{ 326 char s1[1024]; 327 char s2[64]; 328 const char *local = "any"; 329 const char *remote = "any"; 330 331 if (!tb) 332 return; 333 334 if (tb[IFLA_IPTUN_REMOTE]) { 335 unsigned addr = rta_getattr_u32(tb[IFLA_IPTUN_REMOTE]); 336 337 if (addr) 338 remote = format_host(AF_INET, 4, &addr, s1, sizeof(s1)); 339 } 340 341 fprintf(f, "remote %s ", remote); 342 343 if (tb[IFLA_IPTUN_LOCAL]) { 344 unsigned addr = rta_getattr_u32(tb[IFLA_IPTUN_LOCAL]); 345 346 if (addr) 347 local = format_host(AF_INET, 4, &addr, s1, sizeof(s1)); 348 } 349 350 fprintf(f, "local %s ", local); 351 352 if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) { 353 unsigned link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]); 354 const char *n = if_indextoname(link, s2); 355 356 if (n) 357 fprintf(f, "dev %s ", n); 358 else 359 fprintf(f, "dev %u ", link); 360 } 361 362 if (tb[IFLA_IPTUN_TTL] && rta_getattr_u8(tb[IFLA_IPTUN_TTL])) 363 fprintf(f, "ttl %d ", rta_getattr_u8(tb[IFLA_IPTUN_TTL])); 364 else 365 fprintf(f, "ttl inherit "); 366 367 if (tb[IFLA_IPTUN_TOS] && rta_getattr_u8(tb[IFLA_IPTUN_TOS])) { 368 int tos = rta_getattr_u8(tb[IFLA_IPTUN_TOS]); 369 370 fputs("tos ", f); 371 if (tos == 1) 372 fputs("inherit ", f); 373 else 374 fprintf(f, "0x%x ", tos); 375 } 376 377 if (tb[IFLA_IPTUN_PMTUDISC] && rta_getattr_u8(tb[IFLA_IPTUN_PMTUDISC])) 378 fprintf(f, "pmtudisc "); 379 else 380 fprintf(f, "nopmtudisc "); 381 382 if (tb[IFLA_IPTUN_FLAGS]) { 383 __u16 iflags = rta_getattr_u16(tb[IFLA_IPTUN_FLAGS]); 384 385 if (iflags & SIT_ISATAP) 386 fprintf(f, "isatap "); 387 } 388 389 if (tb[IFLA_IPTUN_6RD_PREFIXLEN] && 390 *(__u16 *)RTA_DATA(tb[IFLA_IPTUN_6RD_PREFIXLEN])) { 391 __u16 prefixlen = rta_getattr_u16(tb[IFLA_IPTUN_6RD_PREFIXLEN]); 392 __u16 relayprefixlen = 393 rta_getattr_u16(tb[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]); 394 __u32 relayprefix = 395 rta_getattr_u32(tb[IFLA_IPTUN_6RD_RELAY_PREFIX]); 396 397 printf("6rd-prefix %s/%u ", 398 inet_ntop(AF_INET6, RTA_DATA(tb[IFLA_IPTUN_6RD_PREFIX]), 399 s1, sizeof(s1)), 400 prefixlen); 401 if (relayprefix) { 402 printf("6rd-relay_prefix %s/%u ", 403 format_host(AF_INET, 4, &relayprefix, s1, 404 sizeof(s1)), 405 relayprefixlen); 406 } 407 } 408 409 if (tb[IFLA_IPTUN_ENCAP_TYPE] && 410 *(__u16 *)RTA_DATA(tb[IFLA_IPTUN_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) { 411 __u16 type = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE]); 412 __u16 flags = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_FLAGS]); 413 __u16 sport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_SPORT]); 414 __u16 dport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_DPORT]); 415 416 fputs("encap ", f); 417 switch (type) { 418 case TUNNEL_ENCAP_FOU: 419 fputs("fou ", f); 420 break; 421 case TUNNEL_ENCAP_GUE: 422 fputs("gue ", f); 423 break; 424 default: 425 fputs("unknown ", f); 426 break; 427 } 428 429 if (sport == 0) 430 fputs("encap-sport auto ", f); 431 else 432 fprintf(f, "encap-sport %u", ntohs(sport)); 433 434 fprintf(f, "encap-dport %u ", ntohs(dport)); 435 436 if (flags & TUNNEL_ENCAP_FLAG_CSUM) 437 fputs("encap-csum ", f); 438 else 439 fputs("noencap-csum ", f); 440 441 if (flags & TUNNEL_ENCAP_FLAG_CSUM6) 442 fputs("encap-csum6 ", f); 443 else 444 fputs("noencap-csum6 ", f); 445 446 if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) 447 fputs("encap-remcsum ", f); 448 else 449 fputs("noencap-remcsum ", f); 450 } 451} 452 453static void iptunnel_print_help(struct link_util *lu, int argc, char **argv, 454 FILE *f) 455{ 456 print_usage(f, strcmp(lu->id, "sit") == 0); 457} 458 459struct link_util ipip_link_util = { 460 .id = "ipip", 461 .maxattr = IFLA_IPTUN_MAX, 462 .parse_opt = iptunnel_parse_opt, 463 .print_opt = iptunnel_print_opt, 464 .print_help = iptunnel_print_help, 465}; 466 467struct link_util sit_link_util = { 468 .id = "sit", 469 .maxattr = IFLA_IPTUN_MAX, 470 .parse_opt = iptunnel_parse_opt, 471 .print_opt = iptunnel_print_opt, 472 .print_help = iptunnel_print_help, 473}; 474