1 /* 2 * GRE over IPv6 protocol decoder. 3 * 4 * Authors: Dmitry Kozlov (xeb@mail.ru) 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 9 * 2 of the License, or (at your option) any later version. 10 * 11 */ 12 13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14 15 #include <linux/capability.h> 16 #include <linux/module.h> 17 #include <linux/types.h> 18 #include <linux/kernel.h> 19 #include <linux/slab.h> 20 #include <linux/uaccess.h> 21 #include <linux/skbuff.h> 22 #include <linux/netdevice.h> 23 #include <linux/in.h> 24 #include <linux/tcp.h> 25 #include <linux/udp.h> 26 #include <linux/if_arp.h> 27 #include <linux/init.h> 28 #include <linux/in6.h> 29 #include <linux/inetdevice.h> 30 #include <linux/igmp.h> 31 #include <linux/netfilter_ipv4.h> 32 #include <linux/etherdevice.h> 33 #include <linux/if_ether.h> 34 #include <linux/hash.h> 35 #include <linux/if_tunnel.h> 36 #include <linux/ip6_tunnel.h> 37 38 #include <net/sock.h> 39 #include <net/ip.h> 40 #include <net/ip_tunnels.h> 41 #include <net/icmp.h> 42 #include <net/protocol.h> 43 #include <net/addrconf.h> 44 #include <net/arp.h> 45 #include <net/checksum.h> 46 #include <net/dsfield.h> 47 #include <net/inet_ecn.h> 48 #include <net/xfrm.h> 49 #include <net/net_namespace.h> 50 #include <net/netns/generic.h> 51 #include <net/rtnetlink.h> 52 53 #include <net/ipv6.h> 54 #include <net/ip6_fib.h> 55 #include <net/ip6_route.h> 56 #include <net/ip6_tunnel.h> 57 #include <net/gre.h> 58 #include <net/erspan.h> 59 #include <net/dst_metadata.h> 60 61 62 static bool log_ecn_error = true; 63 module_param(log_ecn_error, bool, 0644); 64 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); 65 66 #define IP6_GRE_HASH_SIZE_SHIFT 5 67 #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT) 68 69 static unsigned int ip6gre_net_id __read_mostly; 70 struct ip6gre_net { 71 struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE]; 72 73 struct ip6_tnl __rcu *collect_md_tun; 74 struct net_device *fb_tunnel_dev; 75 }; 76 77 static struct rtnl_link_ops ip6gre_link_ops __read_mostly; 78 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly; 79 static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly; 80 static int ip6gre_tunnel_init(struct net_device *dev); 81 static void ip6gre_tunnel_setup(struct net_device *dev); 82 static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t); 83 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu); 84 85 /* Tunnel hash table */ 86 87 /* 88 4 hash tables: 89 90 3: (remote,local) 91 2: (remote,*) 92 1: (*,local) 93 0: (*,*) 94 95 We require exact key match i.e. if a key is present in packet 96 it will match only tunnel with the same key; if it is not present, 97 it will match only keyless tunnel. 98 99 All keysless packets, if not matched configured keyless tunnels 100 will match fallback tunnel. 101 */ 102 103 #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1)) 104 static u32 HASH_ADDR(const struct in6_addr *addr) 105 { 106 u32 hash = ipv6_addr_hash(addr); 107 108 return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT); 109 } 110 111 #define tunnels_r_l tunnels[3] 112 #define tunnels_r tunnels[2] 113 #define tunnels_l tunnels[1] 114 #define tunnels_wc tunnels[0] 115 116 /* Given src, dst and key, find appropriate for input tunnel. */ 117 118 static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev, 119 const struct in6_addr *remote, const struct in6_addr *local, 120 __be32 key, __be16 gre_proto) 121 { 122 struct net *net = dev_net(dev); 123 int link = dev->ifindex; 124 unsigned int h0 = HASH_ADDR(remote); 125 unsigned int h1 = HASH_KEY(key); 126 struct ip6_tnl *t, *cand = NULL; 127 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 128 int dev_type = (gre_proto == htons(ETH_P_TEB) || 129 gre_proto == htons(ETH_P_ERSPAN) || 130 gre_proto == htons(ETH_P_ERSPAN2)) ? 131 ARPHRD_ETHER : ARPHRD_IP6GRE; 132 int score, cand_score = 4; 133 134 for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) { 135 if (!ipv6_addr_equal(local, &t->parms.laddr) || 136 !ipv6_addr_equal(remote, &t->parms.raddr) || 137 key != t->parms.i_key || 138 !(t->dev->flags & IFF_UP)) 139 continue; 140 141 if (t->dev->type != ARPHRD_IP6GRE && 142 t->dev->type != dev_type) 143 continue; 144 145 score = 0; 146 if (t->parms.link != link) 147 score |= 1; 148 if (t->dev->type != dev_type) 149 score |= 2; 150 if (score == 0) 151 return t; 152 153 if (score < cand_score) { 154 cand = t; 155 cand_score = score; 156 } 157 } 158 159 for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) { 160 if (!ipv6_addr_equal(remote, &t->parms.raddr) || 161 key != t->parms.i_key || 162 !(t->dev->flags & IFF_UP)) 163 continue; 164 165 if (t->dev->type != ARPHRD_IP6GRE && 166 t->dev->type != dev_type) 167 continue; 168 169 score = 0; 170 if (t->parms.link != link) 171 score |= 1; 172 if (t->dev->type != dev_type) 173 score |= 2; 174 if (score == 0) 175 return t; 176 177 if (score < cand_score) { 178 cand = t; 179 cand_score = score; 180 } 181 } 182 183 for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) { 184 if ((!ipv6_addr_equal(local, &t->parms.laddr) && 185 (!ipv6_addr_equal(local, &t->parms.raddr) || 186 !ipv6_addr_is_multicast(local))) || 187 key != t->parms.i_key || 188 !(t->dev->flags & IFF_UP)) 189 continue; 190 191 if (t->dev->type != ARPHRD_IP6GRE && 192 t->dev->type != dev_type) 193 continue; 194 195 score = 0; 196 if (t->parms.link != link) 197 score |= 1; 198 if (t->dev->type != dev_type) 199 score |= 2; 200 if (score == 0) 201 return t; 202 203 if (score < cand_score) { 204 cand = t; 205 cand_score = score; 206 } 207 } 208 209 for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) { 210 if (t->parms.i_key != key || 211 !(t->dev->flags & IFF_UP)) 212 continue; 213 214 if (t->dev->type != ARPHRD_IP6GRE && 215 t->dev->type != dev_type) 216 continue; 217 218 score = 0; 219 if (t->parms.link != link) 220 score |= 1; 221 if (t->dev->type != dev_type) 222 score |= 2; 223 if (score == 0) 224 return t; 225 226 if (score < cand_score) { 227 cand = t; 228 cand_score = score; 229 } 230 } 231 232 if (cand) 233 return cand; 234 235 t = rcu_dereference(ign->collect_md_tun); 236 if (t && t->dev->flags & IFF_UP) 237 return t; 238 239 dev = ign->fb_tunnel_dev; 240 if (dev && dev->flags & IFF_UP) 241 return netdev_priv(dev); 242 243 return NULL; 244 } 245 246 static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign, 247 const struct __ip6_tnl_parm *p) 248 { 249 const struct in6_addr *remote = &p->raddr; 250 const struct in6_addr *local = &p->laddr; 251 unsigned int h = HASH_KEY(p->i_key); 252 int prio = 0; 253 254 if (!ipv6_addr_any(local)) 255 prio |= 1; 256 if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) { 257 prio |= 2; 258 h ^= HASH_ADDR(remote); 259 } 260 261 return &ign->tunnels[prio][h]; 262 } 263 264 static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign, 265 const struct ip6_tnl *t) 266 { 267 return __ip6gre_bucket(ign, &t->parms); 268 } 269 270 static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t) 271 { 272 struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t); 273 274 if (t->parms.collect_md) 275 rcu_assign_pointer(ign->collect_md_tun, t); 276 277 rcu_assign_pointer(t->next, rtnl_dereference(*tp)); 278 rcu_assign_pointer(*tp, t); 279 } 280 281 static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t) 282 { 283 struct ip6_tnl __rcu **tp; 284 struct ip6_tnl *iter; 285 286 if (t->parms.collect_md) 287 rcu_assign_pointer(ign->collect_md_tun, NULL); 288 289 for (tp = ip6gre_bucket(ign, t); 290 (iter = rtnl_dereference(*tp)) != NULL; 291 tp = &iter->next) { 292 if (t == iter) { 293 rcu_assign_pointer(*tp, t->next); 294 break; 295 } 296 } 297 } 298 299 static struct ip6_tnl *ip6gre_tunnel_find(struct net *net, 300 const struct __ip6_tnl_parm *parms, 301 int type) 302 { 303 const struct in6_addr *remote = &parms->raddr; 304 const struct in6_addr *local = &parms->laddr; 305 __be32 key = parms->i_key; 306 int link = parms->link; 307 struct ip6_tnl *t; 308 struct ip6_tnl __rcu **tp; 309 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 310 311 for (tp = __ip6gre_bucket(ign, parms); 312 (t = rtnl_dereference(*tp)) != NULL; 313 tp = &t->next) 314 if (ipv6_addr_equal(local, &t->parms.laddr) && 315 ipv6_addr_equal(remote, &t->parms.raddr) && 316 key == t->parms.i_key && 317 link == t->parms.link && 318 type == t->dev->type) 319 break; 320 321 return t; 322 } 323 324 static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net, 325 const struct __ip6_tnl_parm *parms, int create) 326 { 327 struct ip6_tnl *t, *nt; 328 struct net_device *dev; 329 char name[IFNAMSIZ]; 330 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 331 332 t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE); 333 if (t && create) 334 return NULL; 335 if (t || !create) 336 return t; 337 338 if (parms->name[0]) { 339 if (!dev_valid_name(parms->name)) 340 return NULL; 341 strlcpy(name, parms->name, IFNAMSIZ); 342 } else { 343 strcpy(name, "ip6gre%d"); 344 } 345 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, 346 ip6gre_tunnel_setup); 347 if (!dev) 348 return NULL; 349 350 dev_net_set(dev, net); 351 352 nt = netdev_priv(dev); 353 nt->parms = *parms; 354 dev->rtnl_link_ops = &ip6gre_link_ops; 355 356 nt->dev = dev; 357 nt->net = dev_net(dev); 358 359 if (register_netdevice(dev) < 0) 360 goto failed_free; 361 362 ip6gre_tnl_link_config(nt, 1); 363 364 /* Can use a lockless transmit, unless we generate output sequences */ 365 if (!(nt->parms.o_flags & TUNNEL_SEQ)) 366 dev->features |= NETIF_F_LLTX; 367 368 dev_hold(dev); 369 ip6gre_tunnel_link(ign, nt); 370 return nt; 371 372 failed_free: 373 free_netdev(dev); 374 return NULL; 375 } 376 377 static void ip6gre_tunnel_uninit(struct net_device *dev) 378 { 379 struct ip6_tnl *t = netdev_priv(dev); 380 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id); 381 382 ip6gre_tunnel_unlink(ign, t); 383 dst_cache_reset(&t->dst_cache); 384 dev_put(dev); 385 } 386 387 388 static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 389 u8 type, u8 code, int offset, __be32 info) 390 { 391 struct net *net = dev_net(skb->dev); 392 const struct gre_base_hdr *greh; 393 const struct ipv6hdr *ipv6h; 394 int grehlen = sizeof(*greh); 395 struct ip6_tnl *t; 396 int key_off = 0; 397 __be16 flags; 398 __be32 key; 399 400 if (!pskb_may_pull(skb, offset + grehlen)) 401 return; 402 greh = (const struct gre_base_hdr *)(skb->data + offset); 403 flags = greh->flags; 404 if (flags & (GRE_VERSION | GRE_ROUTING)) 405 return; 406 if (flags & GRE_CSUM) 407 grehlen += 4; 408 if (flags & GRE_KEY) { 409 key_off = grehlen + offset; 410 grehlen += 4; 411 } 412 413 if (!pskb_may_pull(skb, offset + grehlen)) 414 return; 415 ipv6h = (const struct ipv6hdr *)skb->data; 416 greh = (const struct gre_base_hdr *)(skb->data + offset); 417 key = key_off ? *(__be32 *)(skb->data + key_off) : 0; 418 419 t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr, 420 key, greh->protocol); 421 if (!t) 422 return; 423 424 switch (type) { 425 struct ipv6_tlv_tnl_enc_lim *tel; 426 __u32 teli; 427 case ICMPV6_DEST_UNREACH: 428 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n", 429 t->parms.name); 430 if (code != ICMPV6_PORT_UNREACH) 431 break; 432 return; 433 case ICMPV6_TIME_EXCEED: 434 if (code == ICMPV6_EXC_HOPLIMIT) { 435 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n", 436 t->parms.name); 437 break; 438 } 439 return; 440 case ICMPV6_PARAMPROB: 441 teli = 0; 442 if (code == ICMPV6_HDR_FIELD) 443 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); 444 445 if (teli && teli == be32_to_cpu(info) - 2) { 446 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; 447 if (tel->encap_limit == 0) { 448 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n", 449 t->parms.name); 450 } 451 } else { 452 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n", 453 t->parms.name); 454 } 455 return; 456 case ICMPV6_PKT_TOOBIG: 457 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL)); 458 return; 459 case NDISC_REDIRECT: 460 ip6_redirect(skb, net, skb->dev->ifindex, 0, 461 sock_net_uid(net, NULL)); 462 return; 463 } 464 465 if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO)) 466 t->err_count++; 467 else 468 t->err_count = 1; 469 t->err_time = jiffies; 470 } 471 472 static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi) 473 { 474 const struct ipv6hdr *ipv6h; 475 struct ip6_tnl *tunnel; 476 477 ipv6h = ipv6_hdr(skb); 478 tunnel = ip6gre_tunnel_lookup(skb->dev, 479 &ipv6h->saddr, &ipv6h->daddr, tpi->key, 480 tpi->proto); 481 if (tunnel) { 482 if (tunnel->parms.collect_md) { 483 struct metadata_dst *tun_dst; 484 __be64 tun_id; 485 __be16 flags; 486 487 flags = tpi->flags; 488 tun_id = key32_to_tunnel_id(tpi->key); 489 490 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0); 491 if (!tun_dst) 492 return PACKET_REJECT; 493 494 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); 495 } else { 496 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error); 497 } 498 499 return PACKET_RCVD; 500 } 501 502 return PACKET_REJECT; 503 } 504 505 static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len, 506 struct tnl_ptk_info *tpi) 507 { 508 struct erspan_base_hdr *ershdr; 509 struct erspan_metadata *pkt_md; 510 const struct ipv6hdr *ipv6h; 511 struct erspan_md2 *md2; 512 struct ip6_tnl *tunnel; 513 u8 ver; 514 515 if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr)))) 516 return PACKET_REJECT; 517 518 ipv6h = ipv6_hdr(skb); 519 ershdr = (struct erspan_base_hdr *)skb->data; 520 ver = ershdr->ver; 521 tpi->key = cpu_to_be32(get_session_id(ershdr)); 522 523 tunnel = ip6gre_tunnel_lookup(skb->dev, 524 &ipv6h->saddr, &ipv6h->daddr, tpi->key, 525 tpi->proto); 526 if (tunnel) { 527 int len = erspan_hdr_len(ver); 528 529 if (unlikely(!pskb_may_pull(skb, len))) 530 return PACKET_REJECT; 531 532 ershdr = (struct erspan_base_hdr *)skb->data; 533 pkt_md = (struct erspan_metadata *)(ershdr + 1); 534 535 if (__iptunnel_pull_header(skb, len, 536 htons(ETH_P_TEB), 537 false, false) < 0) 538 return PACKET_REJECT; 539 540 if (tunnel->parms.collect_md) { 541 struct metadata_dst *tun_dst; 542 struct ip_tunnel_info *info; 543 struct erspan_metadata *md; 544 __be64 tun_id; 545 __be16 flags; 546 547 tpi->flags |= TUNNEL_KEY; 548 flags = tpi->flags; 549 tun_id = key32_to_tunnel_id(tpi->key); 550 551 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 552 sizeof(*md)); 553 if (!tun_dst) 554 return PACKET_REJECT; 555 556 info = &tun_dst->u.tun_info; 557 md = ip_tunnel_info_opts(info); 558 md->version = ver; 559 md2 = &md->u.md2; 560 memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE : 561 ERSPAN_V2_MDSIZE); 562 info->key.tun_flags |= TUNNEL_ERSPAN_OPT; 563 info->options_len = sizeof(*md); 564 565 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); 566 567 } else { 568 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error); 569 } 570 571 return PACKET_RCVD; 572 } 573 574 return PACKET_REJECT; 575 } 576 577 static int gre_rcv(struct sk_buff *skb) 578 { 579 struct tnl_ptk_info tpi; 580 bool csum_err = false; 581 int hdr_len; 582 583 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0); 584 if (hdr_len < 0) 585 goto drop; 586 587 if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false)) 588 goto drop; 589 590 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) || 591 tpi.proto == htons(ETH_P_ERSPAN2))) { 592 if (ip6erspan_rcv(skb, hdr_len, &tpi) == PACKET_RCVD) 593 return 0; 594 goto out; 595 } 596 597 if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD) 598 return 0; 599 600 out: 601 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); 602 drop: 603 kfree_skb(skb); 604 return 0; 605 } 606 607 static int gre_handle_offloads(struct sk_buff *skb, bool csum) 608 { 609 return iptunnel_handle_offloads(skb, 610 csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE); 611 } 612 613 static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb, 614 struct net_device *dev, 615 struct flowi6 *fl6, __u8 *dsfield, 616 int *encap_limit) 617 { 618 const struct iphdr *iph = ip_hdr(skb); 619 struct ip6_tnl *t = netdev_priv(dev); 620 621 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 622 *encap_limit = t->parms.encap_limit; 623 624 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6)); 625 626 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) 627 *dsfield = ipv4_get_dsfield(iph); 628 else 629 *dsfield = ip6_tclass(t->parms.flowinfo); 630 631 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) 632 fl6->flowi6_mark = skb->mark; 633 else 634 fl6->flowi6_mark = t->parms.fwmark; 635 636 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL); 637 } 638 639 static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb, 640 struct net_device *dev, 641 struct flowi6 *fl6, __u8 *dsfield, 642 int *encap_limit) 643 { 644 struct ipv6hdr *ipv6h = ipv6_hdr(skb); 645 struct ip6_tnl *t = netdev_priv(dev); 646 __u16 offset; 647 648 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb)); 649 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */ 650 651 if (offset > 0) { 652 struct ipv6_tlv_tnl_enc_lim *tel; 653 654 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset]; 655 if (tel->encap_limit == 0) { 656 icmpv6_send(skb, ICMPV6_PARAMPROB, 657 ICMPV6_HDR_FIELD, offset + 2); 658 return -1; 659 } 660 *encap_limit = tel->encap_limit - 1; 661 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) { 662 *encap_limit = t->parms.encap_limit; 663 } 664 665 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6)); 666 667 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) 668 *dsfield = ipv6_get_dsfield(ipv6h); 669 else 670 *dsfield = ip6_tclass(t->parms.flowinfo); 671 672 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) 673 fl6->flowlabel |= ip6_flowlabel(ipv6h); 674 675 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) 676 fl6->flowi6_mark = skb->mark; 677 else 678 fl6->flowi6_mark = t->parms.fwmark; 679 680 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL); 681 682 return 0; 683 } 684 685 static netdev_tx_t __gre6_xmit(struct sk_buff *skb, 686 struct net_device *dev, __u8 dsfield, 687 struct flowi6 *fl6, int encap_limit, 688 __u32 *pmtu, __be16 proto) 689 { 690 struct ip6_tnl *tunnel = netdev_priv(dev); 691 __be16 protocol; 692 693 if (dev->type == ARPHRD_ETHER) 694 IPCB(skb)->flags = 0; 695 696 if (dev->header_ops && dev->type == ARPHRD_IP6GRE) 697 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr; 698 else 699 fl6->daddr = tunnel->parms.raddr; 700 701 /* Push GRE header. */ 702 protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto; 703 704 if (tunnel->parms.collect_md) { 705 struct ip_tunnel_info *tun_info; 706 const struct ip_tunnel_key *key; 707 __be16 flags; 708 709 tun_info = skb_tunnel_info(skb); 710 if (unlikely(!tun_info || 711 !(tun_info->mode & IP_TUNNEL_INFO_TX) || 712 ip_tunnel_info_af(tun_info) != AF_INET6)) 713 return -EINVAL; 714 715 key = &tun_info->key; 716 memset(fl6, 0, sizeof(*fl6)); 717 fl6->flowi6_proto = IPPROTO_GRE; 718 fl6->daddr = key->u.ipv6.dst; 719 fl6->flowlabel = key->label; 720 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL); 721 722 dsfield = key->tos; 723 flags = key->tun_flags & 724 (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ); 725 tunnel->tun_hlen = gre_calc_hlen(flags); 726 727 gre_build_header(skb, tunnel->tun_hlen, 728 flags, protocol, 729 tunnel_id_to_key32(tun_info->key.tun_id), 730 (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++) 731 : 0); 732 733 } else { 734 if (tunnel->parms.o_flags & TUNNEL_SEQ) 735 tunnel->o_seqno++; 736 737 gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags, 738 protocol, tunnel->parms.o_key, 739 htonl(tunnel->o_seqno)); 740 } 741 742 return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu, 743 NEXTHDR_GRE); 744 } 745 746 static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev) 747 { 748 struct ip6_tnl *t = netdev_priv(dev); 749 int encap_limit = -1; 750 struct flowi6 fl6; 751 __u8 dsfield = 0; 752 __u32 mtu; 753 int err; 754 755 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 756 757 if (!t->parms.collect_md) 758 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6, 759 &dsfield, &encap_limit); 760 761 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)); 762 if (err) 763 return -1; 764 765 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, 766 skb->protocol); 767 if (err != 0) { 768 /* XXX: send ICMP error even if DF is not set. */ 769 if (err == -EMSGSIZE) 770 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, 771 htonl(mtu)); 772 return -1; 773 } 774 775 return 0; 776 } 777 778 static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev) 779 { 780 struct ip6_tnl *t = netdev_priv(dev); 781 struct ipv6hdr *ipv6h = ipv6_hdr(skb); 782 int encap_limit = -1; 783 struct flowi6 fl6; 784 __u8 dsfield = 0; 785 __u32 mtu; 786 int err; 787 788 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr)) 789 return -1; 790 791 if (!t->parms.collect_md && 792 prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit)) 793 return -1; 794 795 if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM))) 796 return -1; 797 798 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, 799 &mtu, skb->protocol); 800 if (err != 0) { 801 if (err == -EMSGSIZE) 802 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 803 return -1; 804 } 805 806 return 0; 807 } 808 809 /** 810 * ip6gre_tnl_addr_conflict - compare packet addresses to tunnel's own 811 * @t: the outgoing tunnel device 812 * @hdr: IPv6 header from the incoming packet 813 * 814 * Description: 815 * Avoid trivial tunneling loop by checking that tunnel exit-point 816 * doesn't match source of incoming packet. 817 * 818 * Return: 819 * 1 if conflict, 820 * 0 else 821 **/ 822 823 static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t, 824 const struct ipv6hdr *hdr) 825 { 826 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); 827 } 828 829 static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev) 830 { 831 struct ip6_tnl *t = netdev_priv(dev); 832 int encap_limit = -1; 833 struct flowi6 fl6; 834 __u32 mtu; 835 int err; 836 837 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 838 encap_limit = t->parms.encap_limit; 839 840 if (!t->parms.collect_md) 841 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); 842 843 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)); 844 if (err) 845 return err; 846 847 err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol); 848 849 return err; 850 } 851 852 static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb, 853 struct net_device *dev) 854 { 855 struct ip6_tnl *t = netdev_priv(dev); 856 struct net_device_stats *stats = &t->dev->stats; 857 int ret; 858 859 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr)) 860 goto tx_err; 861 862 switch (skb->protocol) { 863 case htons(ETH_P_IP): 864 ret = ip6gre_xmit_ipv4(skb, dev); 865 break; 866 case htons(ETH_P_IPV6): 867 ret = ip6gre_xmit_ipv6(skb, dev); 868 break; 869 default: 870 ret = ip6gre_xmit_other(skb, dev); 871 break; 872 } 873 874 if (ret < 0) 875 goto tx_err; 876 877 return NETDEV_TX_OK; 878 879 tx_err: 880 stats->tx_errors++; 881 stats->tx_dropped++; 882 kfree_skb(skb); 883 return NETDEV_TX_OK; 884 } 885 886 static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb, 887 struct net_device *dev) 888 { 889 struct ipv6hdr *ipv6h = ipv6_hdr(skb); 890 struct ip6_tnl *t = netdev_priv(dev); 891 struct dst_entry *dst = skb_dst(skb); 892 struct net_device_stats *stats; 893 bool truncate = false; 894 int encap_limit = -1; 895 __u8 dsfield = false; 896 struct flowi6 fl6; 897 int err = -EINVAL; 898 __u32 mtu; 899 int nhoff; 900 int thoff; 901 902 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr)) 903 goto tx_err; 904 905 if (gre_handle_offloads(skb, false)) 906 goto tx_err; 907 908 if (skb->len > dev->mtu + dev->hard_header_len) { 909 pskb_trim(skb, dev->mtu + dev->hard_header_len); 910 truncate = true; 911 } 912 913 nhoff = skb_network_header(skb) - skb_mac_header(skb); 914 if (skb->protocol == htons(ETH_P_IP) && 915 (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff)) 916 truncate = true; 917 918 thoff = skb_transport_header(skb) - skb_mac_header(skb); 919 if (skb->protocol == htons(ETH_P_IPV6) && 920 (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff)) 921 truncate = true; 922 923 if (skb_cow_head(skb, dev->needed_headroom)) 924 goto tx_err; 925 926 t->parms.o_flags &= ~TUNNEL_KEY; 927 IPCB(skb)->flags = 0; 928 929 /* For collect_md mode, derive fl6 from the tunnel key, 930 * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}. 931 */ 932 if (t->parms.collect_md) { 933 struct ip_tunnel_info *tun_info; 934 const struct ip_tunnel_key *key; 935 struct erspan_metadata *md; 936 __be32 tun_id; 937 938 tun_info = skb_tunnel_info(skb); 939 if (unlikely(!tun_info || 940 !(tun_info->mode & IP_TUNNEL_INFO_TX) || 941 ip_tunnel_info_af(tun_info) != AF_INET6)) 942 return -EINVAL; 943 944 key = &tun_info->key; 945 memset(&fl6, 0, sizeof(fl6)); 946 fl6.flowi6_proto = IPPROTO_GRE; 947 fl6.daddr = key->u.ipv6.dst; 948 fl6.flowlabel = key->label; 949 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL); 950 951 dsfield = key->tos; 952 md = ip_tunnel_info_opts(tun_info); 953 if (!md) 954 goto tx_err; 955 956 tun_id = tunnel_id_to_key32(key->tun_id); 957 if (md->version == 1) { 958 erspan_build_header(skb, 959 ntohl(tun_id), 960 ntohl(md->u.index), truncate, 961 false); 962 } else if (md->version == 2) { 963 erspan_build_header_v2(skb, 964 ntohl(tun_id), 965 md->u.md2.dir, 966 get_hwid(&md->u.md2), 967 truncate, false); 968 } else { 969 goto tx_err; 970 } 971 } else { 972 switch (skb->protocol) { 973 case htons(ETH_P_IP): 974 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 975 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6, 976 &dsfield, &encap_limit); 977 break; 978 case htons(ETH_P_IPV6): 979 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr)) 980 goto tx_err; 981 if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, 982 &dsfield, &encap_limit)) 983 goto tx_err; 984 break; 985 default: 986 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); 987 break; 988 } 989 990 if (t->parms.erspan_ver == 1) 991 erspan_build_header(skb, ntohl(t->parms.o_key), 992 t->parms.index, 993 truncate, false); 994 else 995 erspan_build_header_v2(skb, ntohl(t->parms.o_key), 996 t->parms.dir, 997 t->parms.hwid, 998 truncate, false); 999 fl6.daddr = t->parms.raddr; 1000 } 1001 1002 /* Push GRE header. */ 1003 gre_build_header(skb, 8, TUNNEL_SEQ, 1004 htons(ETH_P_ERSPAN), 0, htonl(t->o_seqno++)); 1005 1006 /* TooBig packet may have updated dst->dev's mtu */ 1007 if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu) 1008 dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu); 1009 1010 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, 1011 NEXTHDR_GRE); 1012 if (err != 0) { 1013 /* XXX: send ICMP error even if DF is not set. */ 1014 if (err == -EMSGSIZE) { 1015 if (skb->protocol == htons(ETH_P_IP)) 1016 icmp_send(skb, ICMP_DEST_UNREACH, 1017 ICMP_FRAG_NEEDED, htonl(mtu)); 1018 else 1019 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 1020 } 1021 1022 goto tx_err; 1023 } 1024 return NETDEV_TX_OK; 1025 1026 tx_err: 1027 stats = &t->dev->stats; 1028 stats->tx_errors++; 1029 stats->tx_dropped++; 1030 kfree_skb(skb); 1031 return NETDEV_TX_OK; 1032 } 1033 1034 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu) 1035 { 1036 struct net_device *dev = t->dev; 1037 struct __ip6_tnl_parm *p = &t->parms; 1038 struct flowi6 *fl6 = &t->fl.u.ip6; 1039 int t_hlen; 1040 1041 if (dev->type != ARPHRD_ETHER) { 1042 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); 1043 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); 1044 } 1045 1046 /* Set up flowi template */ 1047 fl6->saddr = p->laddr; 1048 fl6->daddr = p->raddr; 1049 fl6->flowi6_oif = p->link; 1050 fl6->flowlabel = 0; 1051 fl6->flowi6_proto = IPPROTO_GRE; 1052 1053 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) 1054 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; 1055 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) 1056 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; 1057 1058 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); 1059 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); 1060 1061 if (p->flags&IP6_TNL_F_CAP_XMIT && 1062 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER) 1063 dev->flags |= IFF_POINTOPOINT; 1064 else 1065 dev->flags &= ~IFF_POINTOPOINT; 1066 1067 t->tun_hlen = gre_calc_hlen(t->parms.o_flags); 1068 1069 t->hlen = t->encap_hlen + t->tun_hlen; 1070 1071 t_hlen = t->hlen + sizeof(struct ipv6hdr); 1072 1073 if (p->flags & IP6_TNL_F_CAP_XMIT) { 1074 int strict = (ipv6_addr_type(&p->raddr) & 1075 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); 1076 1077 struct rt6_info *rt = rt6_lookup(t->net, 1078 &p->raddr, &p->laddr, 1079 p->link, NULL, strict); 1080 1081 if (!rt) 1082 return; 1083 1084 if (rt->dst.dev) { 1085 dev->hard_header_len = rt->dst.dev->hard_header_len + 1086 t_hlen; 1087 1088 if (set_mtu) { 1089 dev->mtu = rt->dst.dev->mtu - t_hlen; 1090 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1091 dev->mtu -= 8; 1092 if (dev->type == ARPHRD_ETHER) 1093 dev->mtu -= ETH_HLEN; 1094 1095 if (dev->mtu < IPV6_MIN_MTU) 1096 dev->mtu = IPV6_MIN_MTU; 1097 } 1098 } 1099 ip6_rt_put(rt); 1100 } 1101 } 1102 1103 static int ip6gre_tnl_change(struct ip6_tnl *t, 1104 const struct __ip6_tnl_parm *p, int set_mtu) 1105 { 1106 t->parms.laddr = p->laddr; 1107 t->parms.raddr = p->raddr; 1108 t->parms.flags = p->flags; 1109 t->parms.hop_limit = p->hop_limit; 1110 t->parms.encap_limit = p->encap_limit; 1111 t->parms.flowinfo = p->flowinfo; 1112 t->parms.link = p->link; 1113 t->parms.proto = p->proto; 1114 t->parms.i_key = p->i_key; 1115 t->parms.o_key = p->o_key; 1116 t->parms.i_flags = p->i_flags; 1117 t->parms.o_flags = p->o_flags; 1118 t->parms.fwmark = p->fwmark; 1119 dst_cache_reset(&t->dst_cache); 1120 ip6gre_tnl_link_config(t, set_mtu); 1121 return 0; 1122 } 1123 1124 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p, 1125 const struct ip6_tnl_parm2 *u) 1126 { 1127 p->laddr = u->laddr; 1128 p->raddr = u->raddr; 1129 p->flags = u->flags; 1130 p->hop_limit = u->hop_limit; 1131 p->encap_limit = u->encap_limit; 1132 p->flowinfo = u->flowinfo; 1133 p->link = u->link; 1134 p->i_key = u->i_key; 1135 p->o_key = u->o_key; 1136 p->i_flags = gre_flags_to_tnl_flags(u->i_flags); 1137 p->o_flags = gre_flags_to_tnl_flags(u->o_flags); 1138 memcpy(p->name, u->name, sizeof(u->name)); 1139 } 1140 1141 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u, 1142 const struct __ip6_tnl_parm *p) 1143 { 1144 u->proto = IPPROTO_GRE; 1145 u->laddr = p->laddr; 1146 u->raddr = p->raddr; 1147 u->flags = p->flags; 1148 u->hop_limit = p->hop_limit; 1149 u->encap_limit = p->encap_limit; 1150 u->flowinfo = p->flowinfo; 1151 u->link = p->link; 1152 u->i_key = p->i_key; 1153 u->o_key = p->o_key; 1154 u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags); 1155 u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags); 1156 memcpy(u->name, p->name, sizeof(u->name)); 1157 } 1158 1159 static int ip6gre_tunnel_ioctl(struct net_device *dev, 1160 struct ifreq *ifr, int cmd) 1161 { 1162 int err = 0; 1163 struct ip6_tnl_parm2 p; 1164 struct __ip6_tnl_parm p1; 1165 struct ip6_tnl *t = netdev_priv(dev); 1166 struct net *net = t->net; 1167 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 1168 1169 memset(&p1, 0, sizeof(p1)); 1170 1171 switch (cmd) { 1172 case SIOCGETTUNNEL: 1173 if (dev == ign->fb_tunnel_dev) { 1174 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { 1175 err = -EFAULT; 1176 break; 1177 } 1178 ip6gre_tnl_parm_from_user(&p1, &p); 1179 t = ip6gre_tunnel_locate(net, &p1, 0); 1180 if (!t) 1181 t = netdev_priv(dev); 1182 } 1183 memset(&p, 0, sizeof(p)); 1184 ip6gre_tnl_parm_to_user(&p, &t->parms); 1185 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) 1186 err = -EFAULT; 1187 break; 1188 1189 case SIOCADDTUNNEL: 1190 case SIOCCHGTUNNEL: 1191 err = -EPERM; 1192 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1193 goto done; 1194 1195 err = -EFAULT; 1196 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) 1197 goto done; 1198 1199 err = -EINVAL; 1200 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)) 1201 goto done; 1202 1203 if (!(p.i_flags&GRE_KEY)) 1204 p.i_key = 0; 1205 if (!(p.o_flags&GRE_KEY)) 1206 p.o_key = 0; 1207 1208 ip6gre_tnl_parm_from_user(&p1, &p); 1209 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL); 1210 1211 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { 1212 if (t) { 1213 if (t->dev != dev) { 1214 err = -EEXIST; 1215 break; 1216 } 1217 } else { 1218 t = netdev_priv(dev); 1219 1220 ip6gre_tunnel_unlink(ign, t); 1221 synchronize_net(); 1222 ip6gre_tnl_change(t, &p1, 1); 1223 ip6gre_tunnel_link(ign, t); 1224 netdev_state_change(dev); 1225 } 1226 } 1227 1228 if (t) { 1229 err = 0; 1230 1231 memset(&p, 0, sizeof(p)); 1232 ip6gre_tnl_parm_to_user(&p, &t->parms); 1233 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) 1234 err = -EFAULT; 1235 } else 1236 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); 1237 break; 1238 1239 case SIOCDELTUNNEL: 1240 err = -EPERM; 1241 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1242 goto done; 1243 1244 if (dev == ign->fb_tunnel_dev) { 1245 err = -EFAULT; 1246 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) 1247 goto done; 1248 err = -ENOENT; 1249 ip6gre_tnl_parm_from_user(&p1, &p); 1250 t = ip6gre_tunnel_locate(net, &p1, 0); 1251 if (!t) 1252 goto done; 1253 err = -EPERM; 1254 if (t == netdev_priv(ign->fb_tunnel_dev)) 1255 goto done; 1256 dev = t->dev; 1257 } 1258 unregister_netdevice(dev); 1259 err = 0; 1260 break; 1261 1262 default: 1263 err = -EINVAL; 1264 } 1265 1266 done: 1267 return err; 1268 } 1269 1270 static int ip6gre_header(struct sk_buff *skb, struct net_device *dev, 1271 unsigned short type, const void *daddr, 1272 const void *saddr, unsigned int len) 1273 { 1274 struct ip6_tnl *t = netdev_priv(dev); 1275 struct ipv6hdr *ipv6h; 1276 __be16 *p; 1277 1278 ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h)); 1279 ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb, 1280 t->fl.u.ip6.flowlabel, 1281 true, &t->fl.u.ip6)); 1282 ipv6h->hop_limit = t->parms.hop_limit; 1283 ipv6h->nexthdr = NEXTHDR_GRE; 1284 ipv6h->saddr = t->parms.laddr; 1285 ipv6h->daddr = t->parms.raddr; 1286 1287 p = (__be16 *)(ipv6h + 1); 1288 p[0] = t->parms.o_flags; 1289 p[1] = htons(type); 1290 1291 /* 1292 * Set the source hardware address. 1293 */ 1294 1295 if (saddr) 1296 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr)); 1297 if (daddr) 1298 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr)); 1299 if (!ipv6_addr_any(&ipv6h->daddr)) 1300 return t->hlen; 1301 1302 return -t->hlen; 1303 } 1304 1305 static const struct header_ops ip6gre_header_ops = { 1306 .create = ip6gre_header, 1307 }; 1308 1309 static const struct net_device_ops ip6gre_netdev_ops = { 1310 .ndo_init = ip6gre_tunnel_init, 1311 .ndo_uninit = ip6gre_tunnel_uninit, 1312 .ndo_start_xmit = ip6gre_tunnel_xmit, 1313 .ndo_do_ioctl = ip6gre_tunnel_ioctl, 1314 .ndo_change_mtu = ip6_tnl_change_mtu, 1315 .ndo_get_stats64 = ip_tunnel_get_stats64, 1316 .ndo_get_iflink = ip6_tnl_get_iflink, 1317 }; 1318 1319 static void ip6gre_dev_free(struct net_device *dev) 1320 { 1321 struct ip6_tnl *t = netdev_priv(dev); 1322 1323 gro_cells_destroy(&t->gro_cells); 1324 dst_cache_destroy(&t->dst_cache); 1325 free_percpu(dev->tstats); 1326 } 1327 1328 static void ip6gre_tunnel_setup(struct net_device *dev) 1329 { 1330 dev->netdev_ops = &ip6gre_netdev_ops; 1331 dev->needs_free_netdev = true; 1332 dev->priv_destructor = ip6gre_dev_free; 1333 1334 dev->type = ARPHRD_IP6GRE; 1335 1336 dev->flags |= IFF_NOARP; 1337 dev->addr_len = sizeof(struct in6_addr); 1338 netif_keep_dst(dev); 1339 /* This perm addr will be used as interface identifier by IPv6 */ 1340 dev->addr_assign_type = NET_ADDR_RANDOM; 1341 eth_random_addr(dev->perm_addr); 1342 } 1343 1344 #define GRE6_FEATURES (NETIF_F_SG | \ 1345 NETIF_F_FRAGLIST | \ 1346 NETIF_F_HIGHDMA | \ 1347 NETIF_F_HW_CSUM) 1348 1349 static void ip6gre_tnl_init_features(struct net_device *dev) 1350 { 1351 struct ip6_tnl *nt = netdev_priv(dev); 1352 1353 dev->features |= GRE6_FEATURES; 1354 dev->hw_features |= GRE6_FEATURES; 1355 1356 if (!(nt->parms.o_flags & TUNNEL_SEQ)) { 1357 /* TCP offload with GRE SEQ is not supported, nor 1358 * can we support 2 levels of outer headers requiring 1359 * an update. 1360 */ 1361 if (!(nt->parms.o_flags & TUNNEL_CSUM) || 1362 nt->encap.type == TUNNEL_ENCAP_NONE) { 1363 dev->features |= NETIF_F_GSO_SOFTWARE; 1364 dev->hw_features |= NETIF_F_GSO_SOFTWARE; 1365 } 1366 1367 /* Can use a lockless transmit, unless we generate 1368 * output sequences 1369 */ 1370 dev->features |= NETIF_F_LLTX; 1371 } 1372 } 1373 1374 static int ip6gre_tunnel_init_common(struct net_device *dev) 1375 { 1376 struct ip6_tnl *tunnel; 1377 int ret; 1378 int t_hlen; 1379 1380 tunnel = netdev_priv(dev); 1381 1382 tunnel->dev = dev; 1383 tunnel->net = dev_net(dev); 1384 strcpy(tunnel->parms.name, dev->name); 1385 1386 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 1387 if (!dev->tstats) 1388 return -ENOMEM; 1389 1390 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL); 1391 if (ret) 1392 goto cleanup_alloc_pcpu_stats; 1393 1394 ret = gro_cells_init(&tunnel->gro_cells, dev); 1395 if (ret) 1396 goto cleanup_dst_cache_init; 1397 1398 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags); 1399 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen; 1400 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr); 1401 1402 dev->hard_header_len = LL_MAX_HEADER + t_hlen; 1403 dev->mtu = ETH_DATA_LEN - t_hlen; 1404 if (dev->type == ARPHRD_ETHER) 1405 dev->mtu -= ETH_HLEN; 1406 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1407 dev->mtu -= 8; 1408 1409 if (tunnel->parms.collect_md) { 1410 dev->features |= NETIF_F_NETNS_LOCAL; 1411 netif_keep_dst(dev); 1412 } 1413 ip6gre_tnl_init_features(dev); 1414 1415 return 0; 1416 1417 cleanup_dst_cache_init: 1418 dst_cache_destroy(&tunnel->dst_cache); 1419 cleanup_alloc_pcpu_stats: 1420 free_percpu(dev->tstats); 1421 dev->tstats = NULL; 1422 return ret; 1423 } 1424 1425 static int ip6gre_tunnel_init(struct net_device *dev) 1426 { 1427 struct ip6_tnl *tunnel; 1428 int ret; 1429 1430 ret = ip6gre_tunnel_init_common(dev); 1431 if (ret) 1432 return ret; 1433 1434 tunnel = netdev_priv(dev); 1435 1436 if (tunnel->parms.collect_md) 1437 return 0; 1438 1439 memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr)); 1440 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr)); 1441 1442 if (ipv6_addr_any(&tunnel->parms.raddr)) 1443 dev->header_ops = &ip6gre_header_ops; 1444 1445 return 0; 1446 } 1447 1448 static void ip6gre_fb_tunnel_init(struct net_device *dev) 1449 { 1450 struct ip6_tnl *tunnel = netdev_priv(dev); 1451 1452 tunnel->dev = dev; 1453 tunnel->net = dev_net(dev); 1454 strcpy(tunnel->parms.name, dev->name); 1455 1456 tunnel->hlen = sizeof(struct ipv6hdr) + 4; 1457 1458 dev_hold(dev); 1459 } 1460 1461 static struct inet6_protocol ip6gre_protocol __read_mostly = { 1462 .handler = gre_rcv, 1463 .err_handler = ip6gre_err, 1464 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, 1465 }; 1466 1467 static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head) 1468 { 1469 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 1470 struct net_device *dev, *aux; 1471 int prio; 1472 1473 for_each_netdev_safe(net, dev, aux) 1474 if (dev->rtnl_link_ops == &ip6gre_link_ops || 1475 dev->rtnl_link_ops == &ip6gre_tap_ops || 1476 dev->rtnl_link_ops == &ip6erspan_tap_ops) 1477 unregister_netdevice_queue(dev, head); 1478 1479 for (prio = 0; prio < 4; prio++) { 1480 int h; 1481 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) { 1482 struct ip6_tnl *t; 1483 1484 t = rtnl_dereference(ign->tunnels[prio][h]); 1485 1486 while (t) { 1487 /* If dev is in the same netns, it has already 1488 * been added to the list by the previous loop. 1489 */ 1490 if (!net_eq(dev_net(t->dev), net)) 1491 unregister_netdevice_queue(t->dev, 1492 head); 1493 t = rtnl_dereference(t->next); 1494 } 1495 } 1496 } 1497 } 1498 1499 static int __net_init ip6gre_init_net(struct net *net) 1500 { 1501 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 1502 int err; 1503 1504 if (!net_has_fallback_tunnels(net)) 1505 return 0; 1506 ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0", 1507 NET_NAME_UNKNOWN, 1508 ip6gre_tunnel_setup); 1509 if (!ign->fb_tunnel_dev) { 1510 err = -ENOMEM; 1511 goto err_alloc_dev; 1512 } 1513 dev_net_set(ign->fb_tunnel_dev, net); 1514 /* FB netdevice is special: we have one, and only one per netns. 1515 * Allowing to move it to another netns is clearly unsafe. 1516 */ 1517 ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL; 1518 1519 1520 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev); 1521 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops; 1522 1523 err = register_netdev(ign->fb_tunnel_dev); 1524 if (err) 1525 goto err_reg_dev; 1526 1527 rcu_assign_pointer(ign->tunnels_wc[0], 1528 netdev_priv(ign->fb_tunnel_dev)); 1529 return 0; 1530 1531 err_reg_dev: 1532 free_netdev(ign->fb_tunnel_dev); 1533 err_alloc_dev: 1534 return err; 1535 } 1536 1537 static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list) 1538 { 1539 struct net *net; 1540 LIST_HEAD(list); 1541 1542 rtnl_lock(); 1543 list_for_each_entry(net, net_list, exit_list) 1544 ip6gre_destroy_tunnels(net, &list); 1545 unregister_netdevice_many(&list); 1546 rtnl_unlock(); 1547 } 1548 1549 static struct pernet_operations ip6gre_net_ops = { 1550 .init = ip6gre_init_net, 1551 .exit_batch = ip6gre_exit_batch_net, 1552 .id = &ip6gre_net_id, 1553 .size = sizeof(struct ip6gre_net), 1554 }; 1555 1556 static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[], 1557 struct netlink_ext_ack *extack) 1558 { 1559 __be16 flags; 1560 1561 if (!data) 1562 return 0; 1563 1564 flags = 0; 1565 if (data[IFLA_GRE_IFLAGS]) 1566 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); 1567 if (data[IFLA_GRE_OFLAGS]) 1568 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); 1569 if (flags & (GRE_VERSION|GRE_ROUTING)) 1570 return -EINVAL; 1571 1572 return 0; 1573 } 1574 1575 static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[], 1576 struct netlink_ext_ack *extack) 1577 { 1578 struct in6_addr daddr; 1579 1580 if (tb[IFLA_ADDRESS]) { 1581 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 1582 return -EINVAL; 1583 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 1584 return -EADDRNOTAVAIL; 1585 } 1586 1587 if (!data) 1588 goto out; 1589 1590 if (data[IFLA_GRE_REMOTE]) { 1591 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); 1592 if (ipv6_addr_any(&daddr)) 1593 return -EINVAL; 1594 } 1595 1596 out: 1597 return ip6gre_tunnel_validate(tb, data, extack); 1598 } 1599 1600 static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[], 1601 struct netlink_ext_ack *extack) 1602 { 1603 __be16 flags = 0; 1604 int ret, ver = 0; 1605 1606 if (!data) 1607 return 0; 1608 1609 ret = ip6gre_tap_validate(tb, data, extack); 1610 if (ret) 1611 return ret; 1612 1613 /* ERSPAN should only have GRE sequence and key flag */ 1614 if (data[IFLA_GRE_OFLAGS]) 1615 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); 1616 if (data[IFLA_GRE_IFLAGS]) 1617 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); 1618 if (!data[IFLA_GRE_COLLECT_METADATA] && 1619 flags != (GRE_SEQ | GRE_KEY)) 1620 return -EINVAL; 1621 1622 /* ERSPAN Session ID only has 10-bit. Since we reuse 1623 * 32-bit key field as ID, check it's range. 1624 */ 1625 if (data[IFLA_GRE_IKEY] && 1626 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK)) 1627 return -EINVAL; 1628 1629 if (data[IFLA_GRE_OKEY] && 1630 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK)) 1631 return -EINVAL; 1632 1633 if (data[IFLA_GRE_ERSPAN_VER]) { 1634 ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]); 1635 if (ver != 1 && ver != 2) 1636 return -EINVAL; 1637 } 1638 1639 if (ver == 1) { 1640 if (data[IFLA_GRE_ERSPAN_INDEX]) { 1641 u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]); 1642 1643 if (index & ~INDEX_MASK) 1644 return -EINVAL; 1645 } 1646 } else if (ver == 2) { 1647 if (data[IFLA_GRE_ERSPAN_DIR]) { 1648 u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]); 1649 1650 if (dir & ~(DIR_MASK >> DIR_OFFSET)) 1651 return -EINVAL; 1652 } 1653 1654 if (data[IFLA_GRE_ERSPAN_HWID]) { 1655 u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]); 1656 1657 if (hwid & ~(HWID_MASK >> HWID_OFFSET)) 1658 return -EINVAL; 1659 } 1660 } 1661 1662 return 0; 1663 } 1664 1665 static void ip6gre_netlink_parms(struct nlattr *data[], 1666 struct __ip6_tnl_parm *parms) 1667 { 1668 memset(parms, 0, sizeof(*parms)); 1669 1670 if (!data) 1671 return; 1672 1673 if (data[IFLA_GRE_LINK]) 1674 parms->link = nla_get_u32(data[IFLA_GRE_LINK]); 1675 1676 if (data[IFLA_GRE_IFLAGS]) 1677 parms->i_flags = gre_flags_to_tnl_flags( 1678 nla_get_be16(data[IFLA_GRE_IFLAGS])); 1679 1680 if (data[IFLA_GRE_OFLAGS]) 1681 parms->o_flags = gre_flags_to_tnl_flags( 1682 nla_get_be16(data[IFLA_GRE_OFLAGS])); 1683 1684 if (data[IFLA_GRE_IKEY]) 1685 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); 1686 1687 if (data[IFLA_GRE_OKEY]) 1688 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); 1689 1690 if (data[IFLA_GRE_LOCAL]) 1691 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]); 1692 1693 if (data[IFLA_GRE_REMOTE]) 1694 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); 1695 1696 if (data[IFLA_GRE_TTL]) 1697 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]); 1698 1699 if (data[IFLA_GRE_ENCAP_LIMIT]) 1700 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]); 1701 1702 if (data[IFLA_GRE_FLOWINFO]) 1703 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]); 1704 1705 if (data[IFLA_GRE_FLAGS]) 1706 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]); 1707 1708 if (data[IFLA_GRE_FWMARK]) 1709 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]); 1710 1711 if (data[IFLA_GRE_COLLECT_METADATA]) 1712 parms->collect_md = true; 1713 1714 if (data[IFLA_GRE_ERSPAN_VER]) 1715 parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]); 1716 1717 if (parms->erspan_ver == 1) { 1718 if (data[IFLA_GRE_ERSPAN_INDEX]) 1719 parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]); 1720 } else if (parms->erspan_ver == 2) { 1721 if (data[IFLA_GRE_ERSPAN_DIR]) 1722 parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]); 1723 if (data[IFLA_GRE_ERSPAN_HWID]) 1724 parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]); 1725 } 1726 } 1727 1728 static int ip6gre_tap_init(struct net_device *dev) 1729 { 1730 int ret; 1731 1732 ret = ip6gre_tunnel_init_common(dev); 1733 if (ret) 1734 return ret; 1735 1736 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 1737 1738 return 0; 1739 } 1740 1741 static const struct net_device_ops ip6gre_tap_netdev_ops = { 1742 .ndo_init = ip6gre_tap_init, 1743 .ndo_uninit = ip6gre_tunnel_uninit, 1744 .ndo_start_xmit = ip6gre_tunnel_xmit, 1745 .ndo_set_mac_address = eth_mac_addr, 1746 .ndo_validate_addr = eth_validate_addr, 1747 .ndo_change_mtu = ip6_tnl_change_mtu, 1748 .ndo_get_stats64 = ip_tunnel_get_stats64, 1749 .ndo_get_iflink = ip6_tnl_get_iflink, 1750 }; 1751 1752 static int ip6erspan_tap_init(struct net_device *dev) 1753 { 1754 struct ip6_tnl *tunnel; 1755 int t_hlen; 1756 int ret; 1757 1758 tunnel = netdev_priv(dev); 1759 1760 tunnel->dev = dev; 1761 tunnel->net = dev_net(dev); 1762 strcpy(tunnel->parms.name, dev->name); 1763 1764 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 1765 if (!dev->tstats) 1766 return -ENOMEM; 1767 1768 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL); 1769 if (ret) 1770 goto cleanup_alloc_pcpu_stats; 1771 1772 ret = gro_cells_init(&tunnel->gro_cells, dev); 1773 if (ret) 1774 goto cleanup_dst_cache_init; 1775 1776 tunnel->tun_hlen = 8; 1777 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen + 1778 erspan_hdr_len(tunnel->parms.erspan_ver); 1779 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr); 1780 1781 dev->hard_header_len = LL_MAX_HEADER + t_hlen; 1782 dev->mtu = ETH_DATA_LEN - t_hlen; 1783 if (dev->type == ARPHRD_ETHER) 1784 dev->mtu -= ETH_HLEN; 1785 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1786 dev->mtu -= 8; 1787 1788 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 1789 ip6gre_tnl_link_config(tunnel, 1); 1790 1791 return 0; 1792 1793 cleanup_dst_cache_init: 1794 dst_cache_destroy(&tunnel->dst_cache); 1795 cleanup_alloc_pcpu_stats: 1796 free_percpu(dev->tstats); 1797 dev->tstats = NULL; 1798 return ret; 1799 } 1800 1801 static const struct net_device_ops ip6erspan_netdev_ops = { 1802 .ndo_init = ip6erspan_tap_init, 1803 .ndo_uninit = ip6gre_tunnel_uninit, 1804 .ndo_start_xmit = ip6erspan_tunnel_xmit, 1805 .ndo_set_mac_address = eth_mac_addr, 1806 .ndo_validate_addr = eth_validate_addr, 1807 .ndo_change_mtu = ip6_tnl_change_mtu, 1808 .ndo_get_stats64 = ip_tunnel_get_stats64, 1809 .ndo_get_iflink = ip6_tnl_get_iflink, 1810 }; 1811 1812 static void ip6gre_tap_setup(struct net_device *dev) 1813 { 1814 1815 ether_setup(dev); 1816 1817 dev->max_mtu = 0; 1818 dev->netdev_ops = &ip6gre_tap_netdev_ops; 1819 dev->needs_free_netdev = true; 1820 dev->priv_destructor = ip6gre_dev_free; 1821 1822 dev->features |= NETIF_F_NETNS_LOCAL; 1823 dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1824 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 1825 netif_keep_dst(dev); 1826 } 1827 1828 bool is_ip6gretap_dev(const struct net_device *dev) 1829 { 1830 return dev->netdev_ops == &ip6gre_tap_netdev_ops; 1831 } 1832 EXPORT_SYMBOL_GPL(is_ip6gretap_dev); 1833 1834 static bool ip6gre_netlink_encap_parms(struct nlattr *data[], 1835 struct ip_tunnel_encap *ipencap) 1836 { 1837 bool ret = false; 1838 1839 memset(ipencap, 0, sizeof(*ipencap)); 1840 1841 if (!data) 1842 return ret; 1843 1844 if (data[IFLA_GRE_ENCAP_TYPE]) { 1845 ret = true; 1846 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]); 1847 } 1848 1849 if (data[IFLA_GRE_ENCAP_FLAGS]) { 1850 ret = true; 1851 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]); 1852 } 1853 1854 if (data[IFLA_GRE_ENCAP_SPORT]) { 1855 ret = true; 1856 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]); 1857 } 1858 1859 if (data[IFLA_GRE_ENCAP_DPORT]) { 1860 ret = true; 1861 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]); 1862 } 1863 1864 return ret; 1865 } 1866 1867 static int ip6gre_newlink(struct net *src_net, struct net_device *dev, 1868 struct nlattr *tb[], struct nlattr *data[], 1869 struct netlink_ext_ack *extack) 1870 { 1871 struct ip6_tnl *nt; 1872 struct net *net = dev_net(dev); 1873 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 1874 struct ip_tunnel_encap ipencap; 1875 int err; 1876 1877 nt = netdev_priv(dev); 1878 1879 if (ip6gre_netlink_encap_parms(data, &ipencap)) { 1880 int err = ip6_tnl_encap_setup(nt, &ipencap); 1881 1882 if (err < 0) 1883 return err; 1884 } 1885 1886 ip6gre_netlink_parms(data, &nt->parms); 1887 1888 if (nt->parms.collect_md) { 1889 if (rtnl_dereference(ign->collect_md_tun)) 1890 return -EEXIST; 1891 } else { 1892 if (ip6gre_tunnel_find(net, &nt->parms, dev->type)) 1893 return -EEXIST; 1894 } 1895 1896 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS]) 1897 eth_hw_addr_random(dev); 1898 1899 nt->dev = dev; 1900 nt->net = dev_net(dev); 1901 1902 err = register_netdevice(dev); 1903 if (err) 1904 goto out; 1905 1906 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]); 1907 1908 if (tb[IFLA_MTU]) 1909 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 1910 1911 dev_hold(dev); 1912 ip6gre_tunnel_link(ign, nt); 1913 1914 out: 1915 return err; 1916 } 1917 1918 static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[], 1919 struct nlattr *data[], 1920 struct netlink_ext_ack *extack) 1921 { 1922 struct ip6_tnl *t, *nt = netdev_priv(dev); 1923 struct net *net = nt->net; 1924 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 1925 struct __ip6_tnl_parm p; 1926 struct ip_tunnel_encap ipencap; 1927 1928 if (dev == ign->fb_tunnel_dev) 1929 return -EINVAL; 1930 1931 if (ip6gre_netlink_encap_parms(data, &ipencap)) { 1932 int err = ip6_tnl_encap_setup(nt, &ipencap); 1933 1934 if (err < 0) 1935 return err; 1936 } 1937 1938 ip6gre_netlink_parms(data, &p); 1939 1940 t = ip6gre_tunnel_locate(net, &p, 0); 1941 1942 if (t) { 1943 if (t->dev != dev) 1944 return -EEXIST; 1945 } else { 1946 t = nt; 1947 } 1948 1949 ip6gre_tunnel_unlink(ign, t); 1950 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]); 1951 ip6gre_tunnel_link(ign, t); 1952 return 0; 1953 } 1954 1955 static void ip6gre_dellink(struct net_device *dev, struct list_head *head) 1956 { 1957 struct net *net = dev_net(dev); 1958 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 1959 1960 if (dev != ign->fb_tunnel_dev) 1961 unregister_netdevice_queue(dev, head); 1962 } 1963 1964 static size_t ip6gre_get_size(const struct net_device *dev) 1965 { 1966 return 1967 /* IFLA_GRE_LINK */ 1968 nla_total_size(4) + 1969 /* IFLA_GRE_IFLAGS */ 1970 nla_total_size(2) + 1971 /* IFLA_GRE_OFLAGS */ 1972 nla_total_size(2) + 1973 /* IFLA_GRE_IKEY */ 1974 nla_total_size(4) + 1975 /* IFLA_GRE_OKEY */ 1976 nla_total_size(4) + 1977 /* IFLA_GRE_LOCAL */ 1978 nla_total_size(sizeof(struct in6_addr)) + 1979 /* IFLA_GRE_REMOTE */ 1980 nla_total_size(sizeof(struct in6_addr)) + 1981 /* IFLA_GRE_TTL */ 1982 nla_total_size(1) + 1983 /* IFLA_GRE_ENCAP_LIMIT */ 1984 nla_total_size(1) + 1985 /* IFLA_GRE_FLOWINFO */ 1986 nla_total_size(4) + 1987 /* IFLA_GRE_FLAGS */ 1988 nla_total_size(4) + 1989 /* IFLA_GRE_ENCAP_TYPE */ 1990 nla_total_size(2) + 1991 /* IFLA_GRE_ENCAP_FLAGS */ 1992 nla_total_size(2) + 1993 /* IFLA_GRE_ENCAP_SPORT */ 1994 nla_total_size(2) + 1995 /* IFLA_GRE_ENCAP_DPORT */ 1996 nla_total_size(2) + 1997 /* IFLA_GRE_COLLECT_METADATA */ 1998 nla_total_size(0) + 1999 /* IFLA_GRE_FWMARK */ 2000 nla_total_size(4) + 2001 /* IFLA_GRE_ERSPAN_INDEX */ 2002 nla_total_size(4) + 2003 0; 2004 } 2005 2006 static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev) 2007 { 2008 struct ip6_tnl *t = netdev_priv(dev); 2009 struct __ip6_tnl_parm *p = &t->parms; 2010 2011 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || 2012 nla_put_be16(skb, IFLA_GRE_IFLAGS, 2013 gre_tnl_flags_to_gre_flags(p->i_flags)) || 2014 nla_put_be16(skb, IFLA_GRE_OFLAGS, 2015 gre_tnl_flags_to_gre_flags(p->o_flags)) || 2016 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || 2017 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || 2018 nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) || 2019 nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) || 2020 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) || 2021 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) || 2022 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) || 2023 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) || 2024 nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) || 2025 nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index)) 2026 goto nla_put_failure; 2027 2028 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE, 2029 t->encap.type) || 2030 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT, 2031 t->encap.sport) || 2032 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT, 2033 t->encap.dport) || 2034 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS, 2035 t->encap.flags)) 2036 goto nla_put_failure; 2037 2038 if (p->collect_md) { 2039 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA)) 2040 goto nla_put_failure; 2041 } 2042 2043 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver)) 2044 goto nla_put_failure; 2045 2046 if (p->erspan_ver == 1) { 2047 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index)) 2048 goto nla_put_failure; 2049 } else if (p->erspan_ver == 2) { 2050 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir)) 2051 goto nla_put_failure; 2052 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid)) 2053 goto nla_put_failure; 2054 } 2055 2056 return 0; 2057 2058 nla_put_failure: 2059 return -EMSGSIZE; 2060 } 2061 2062 static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = { 2063 [IFLA_GRE_LINK] = { .type = NLA_U32 }, 2064 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, 2065 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, 2066 [IFLA_GRE_IKEY] = { .type = NLA_U32 }, 2067 [IFLA_GRE_OKEY] = { .type = NLA_U32 }, 2068 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) }, 2069 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) }, 2070 [IFLA_GRE_TTL] = { .type = NLA_U8 }, 2071 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 }, 2072 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 }, 2073 [IFLA_GRE_FLAGS] = { .type = NLA_U32 }, 2074 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 }, 2075 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 }, 2076 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 }, 2077 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 }, 2078 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG }, 2079 [IFLA_GRE_FWMARK] = { .type = NLA_U32 }, 2080 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 }, 2081 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 }, 2082 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 }, 2083 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 }, 2084 }; 2085 2086 static void ip6erspan_tap_setup(struct net_device *dev) 2087 { 2088 ether_setup(dev); 2089 2090 dev->netdev_ops = &ip6erspan_netdev_ops; 2091 dev->needs_free_netdev = true; 2092 dev->priv_destructor = ip6gre_dev_free; 2093 2094 dev->features |= NETIF_F_NETNS_LOCAL; 2095 dev->priv_flags &= ~IFF_TX_SKB_SHARING; 2096 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 2097 netif_keep_dst(dev); 2098 } 2099 2100 static struct rtnl_link_ops ip6gre_link_ops __read_mostly = { 2101 .kind = "ip6gre", 2102 .maxtype = IFLA_GRE_MAX, 2103 .policy = ip6gre_policy, 2104 .priv_size = sizeof(struct ip6_tnl), 2105 .setup = ip6gre_tunnel_setup, 2106 .validate = ip6gre_tunnel_validate, 2107 .newlink = ip6gre_newlink, 2108 .changelink = ip6gre_changelink, 2109 .dellink = ip6gre_dellink, 2110 .get_size = ip6gre_get_size, 2111 .fill_info = ip6gre_fill_info, 2112 .get_link_net = ip6_tnl_get_link_net, 2113 }; 2114 2115 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = { 2116 .kind = "ip6gretap", 2117 .maxtype = IFLA_GRE_MAX, 2118 .policy = ip6gre_policy, 2119 .priv_size = sizeof(struct ip6_tnl), 2120 .setup = ip6gre_tap_setup, 2121 .validate = ip6gre_tap_validate, 2122 .newlink = ip6gre_newlink, 2123 .changelink = ip6gre_changelink, 2124 .get_size = ip6gre_get_size, 2125 .fill_info = ip6gre_fill_info, 2126 .get_link_net = ip6_tnl_get_link_net, 2127 }; 2128 2129 static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = { 2130 .kind = "ip6erspan", 2131 .maxtype = IFLA_GRE_MAX, 2132 .policy = ip6gre_policy, 2133 .priv_size = sizeof(struct ip6_tnl), 2134 .setup = ip6erspan_tap_setup, 2135 .validate = ip6erspan_tap_validate, 2136 .newlink = ip6gre_newlink, 2137 .changelink = ip6gre_changelink, 2138 .get_size = ip6gre_get_size, 2139 .fill_info = ip6gre_fill_info, 2140 .get_link_net = ip6_tnl_get_link_net, 2141 }; 2142 2143 /* 2144 * And now the modules code and kernel interface. 2145 */ 2146 2147 static int __init ip6gre_init(void) 2148 { 2149 int err; 2150 2151 pr_info("GRE over IPv6 tunneling driver\n"); 2152 2153 err = register_pernet_device(&ip6gre_net_ops); 2154 if (err < 0) 2155 return err; 2156 2157 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE); 2158 if (err < 0) { 2159 pr_info("%s: can't add protocol\n", __func__); 2160 goto add_proto_failed; 2161 } 2162 2163 err = rtnl_link_register(&ip6gre_link_ops); 2164 if (err < 0) 2165 goto rtnl_link_failed; 2166 2167 err = rtnl_link_register(&ip6gre_tap_ops); 2168 if (err < 0) 2169 goto tap_ops_failed; 2170 2171 err = rtnl_link_register(&ip6erspan_tap_ops); 2172 if (err < 0) 2173 goto erspan_link_failed; 2174 2175 out: 2176 return err; 2177 2178 erspan_link_failed: 2179 rtnl_link_unregister(&ip6gre_tap_ops); 2180 tap_ops_failed: 2181 rtnl_link_unregister(&ip6gre_link_ops); 2182 rtnl_link_failed: 2183 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); 2184 add_proto_failed: 2185 unregister_pernet_device(&ip6gre_net_ops); 2186 goto out; 2187 } 2188 2189 static void __exit ip6gre_fini(void) 2190 { 2191 rtnl_link_unregister(&ip6gre_tap_ops); 2192 rtnl_link_unregister(&ip6gre_link_ops); 2193 rtnl_link_unregister(&ip6erspan_tap_ops); 2194 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); 2195 unregister_pernet_device(&ip6gre_net_ops); 2196 } 2197 2198 module_init(ip6gre_init); 2199 module_exit(ip6gre_fini); 2200 MODULE_LICENSE("GPL"); 2201 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)"); 2202 MODULE_DESCRIPTION("GRE over IPv6 tunneling device"); 2203 MODULE_ALIAS_RTNL_LINK("ip6gre"); 2204 MODULE_ALIAS_RTNL_LINK("ip6gretap"); 2205 MODULE_ALIAS_RTNL_LINK("ip6erspan"); 2206 MODULE_ALIAS_NETDEV("ip6gre0"); 2207