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