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 ip6_tnl *t = netdev_priv(dev); 882 __be16 payload_protocol; 883 int ret; 884 885 if (!pskb_inet_may_pull(skb)) 886 goto tx_err; 887 888 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr)) 889 goto tx_err; 890 891 payload_protocol = skb_protocol(skb, true); 892 switch (payload_protocol) { 893 case htons(ETH_P_IP): 894 ret = ip6gre_xmit_ipv4(skb, dev); 895 break; 896 case htons(ETH_P_IPV6): 897 ret = ip6gre_xmit_ipv6(skb, dev); 898 break; 899 default: 900 ret = ip6gre_xmit_other(skb, dev); 901 break; 902 } 903 904 if (ret < 0) 905 goto tx_err; 906 907 return NETDEV_TX_OK; 908 909 tx_err: 910 if (!t->parms.collect_md || !IS_ERR(skb_tunnel_info_txcheck(skb))) 911 DEV_STATS_INC(dev, tx_errors); 912 DEV_STATS_INC(dev, tx_dropped); 913 kfree_skb(skb); 914 return NETDEV_TX_OK; 915 } 916 917 static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb, 918 struct net_device *dev) 919 { 920 struct ip_tunnel_info *tun_info = NULL; 921 struct ip6_tnl *t = netdev_priv(dev); 922 struct dst_entry *dst = skb_dst(skb); 923 IP_TUNNEL_DECLARE_FLAGS(flags) = { }; 924 bool truncate = false; 925 int encap_limit = -1; 926 __u8 dsfield = false; 927 struct flowi6 fl6; 928 int err = -EINVAL; 929 __be16 proto; 930 __u32 mtu; 931 int nhoff; 932 933 if (!pskb_inet_may_pull(skb)) 934 goto tx_err; 935 936 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr)) 937 goto tx_err; 938 939 if (gre_handle_offloads(skb, false)) 940 goto tx_err; 941 942 if (skb->len > dev->mtu + dev->hard_header_len) { 943 if (pskb_trim(skb, dev->mtu + dev->hard_header_len)) 944 goto tx_err; 945 truncate = true; 946 } 947 948 nhoff = skb_network_offset(skb); 949 if (skb->protocol == htons(ETH_P_IP) && 950 (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff)) 951 truncate = true; 952 953 if (skb->protocol == htons(ETH_P_IPV6)) { 954 int thoff; 955 956 if (skb_transport_header_was_set(skb)) 957 thoff = skb_transport_offset(skb); 958 else 959 thoff = nhoff + sizeof(struct ipv6hdr); 960 if (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff) 961 truncate = true; 962 } 963 964 if (skb_cow_head(skb, dev->needed_headroom ?: t->hlen)) 965 goto tx_err; 966 967 IPCB(skb)->flags = 0; 968 969 /* For collect_md mode, derive fl6 from the tunnel key, 970 * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}. 971 */ 972 if (t->parms.collect_md) { 973 const struct ip_tunnel_key *key; 974 struct erspan_metadata *md; 975 __be32 tun_id; 976 977 tun_info = skb_tunnel_info_txcheck(skb); 978 if (IS_ERR(tun_info) || 979 unlikely(ip_tunnel_info_af(tun_info) != AF_INET6)) 980 goto tx_err; 981 982 key = &tun_info->key; 983 memset(&fl6, 0, sizeof(fl6)); 984 fl6.flowi6_proto = IPPROTO_GRE; 985 fl6.daddr = key->u.ipv6.dst; 986 fl6.flowlabel = key->label; 987 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL); 988 fl6.fl6_gre_key = tunnel_id_to_key32(key->tun_id); 989 990 dsfield = key->tos; 991 if (!test_bit(IP_TUNNEL_ERSPAN_OPT_BIT, 992 tun_info->key.tun_flags)) 993 goto tx_err; 994 if (tun_info->options_len < sizeof(*md)) 995 goto tx_err; 996 md = ip_tunnel_info_opts(tun_info); 997 998 tun_id = tunnel_id_to_key32(key->tun_id); 999 if (md->version == 1) { 1000 erspan_build_header(skb, 1001 ntohl(tun_id), 1002 ntohl(md->u.index), truncate, 1003 false); 1004 proto = htons(ETH_P_ERSPAN); 1005 } else if (md->version == 2) { 1006 erspan_build_header_v2(skb, 1007 ntohl(tun_id), 1008 md->u.md2.dir, 1009 get_hwid(&md->u.md2), 1010 truncate, false); 1011 proto = htons(ETH_P_ERSPAN2); 1012 } else { 1013 goto tx_err; 1014 } 1015 } else { 1016 switch (skb->protocol) { 1017 case htons(ETH_P_IP): 1018 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1019 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6, 1020 &dsfield, &encap_limit); 1021 break; 1022 case htons(ETH_P_IPV6): 1023 if (ipv6_addr_equal(&t->parms.raddr, &ipv6_hdr(skb)->saddr)) 1024 goto tx_err; 1025 if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, 1026 &dsfield, &encap_limit)) 1027 goto tx_err; 1028 break; 1029 default: 1030 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); 1031 break; 1032 } 1033 1034 if (t->parms.erspan_ver == 1) { 1035 erspan_build_header(skb, ntohl(t->parms.o_key), 1036 t->parms.index, 1037 truncate, false); 1038 proto = htons(ETH_P_ERSPAN); 1039 } else if (t->parms.erspan_ver == 2) { 1040 erspan_build_header_v2(skb, ntohl(t->parms.o_key), 1041 t->parms.dir, 1042 t->parms.hwid, 1043 truncate, false); 1044 proto = htons(ETH_P_ERSPAN2); 1045 } else { 1046 goto tx_err; 1047 } 1048 1049 fl6.daddr = t->parms.raddr; 1050 } 1051 1052 /* Push GRE header. */ 1053 __set_bit(IP_TUNNEL_SEQ_BIT, flags); 1054 gre_build_header(skb, 8, flags, proto, 0, 1055 htonl(atomic_fetch_inc(&t->o_seqno))); 1056 1057 /* TooBig packet may have updated dst->dev's mtu */ 1058 if (!t->parms.collect_md && dst) { 1059 mtu = READ_ONCE(dst_dev(dst)->mtu); 1060 if (dst_mtu(dst) > mtu) 1061 dst->ops->update_pmtu(dst, NULL, skb, mtu, false); 1062 } 1063 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, 1064 NEXTHDR_GRE); 1065 if (err != 0) { 1066 /* XXX: send ICMP error even if DF is not set. */ 1067 if (err == -EMSGSIZE) { 1068 if (skb->protocol == htons(ETH_P_IP)) 1069 icmp_ndo_send(skb, ICMP_DEST_UNREACH, 1070 ICMP_FRAG_NEEDED, htonl(mtu)); 1071 else 1072 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 1073 } 1074 1075 goto tx_err; 1076 } 1077 return NETDEV_TX_OK; 1078 1079 tx_err: 1080 if (!IS_ERR(tun_info)) 1081 DEV_STATS_INC(dev, tx_errors); 1082 DEV_STATS_INC(dev, tx_dropped); 1083 kfree_skb(skb); 1084 return NETDEV_TX_OK; 1085 } 1086 1087 static void ip6gre_tnl_link_config_common(struct ip6_tnl *t) 1088 { 1089 struct net_device *dev = t->dev; 1090 struct __ip6_tnl_parm *p = &t->parms; 1091 struct flowi6 *fl6 = &t->fl.u.ip6; 1092 1093 if (dev->type != ARPHRD_ETHER) { 1094 __dev_addr_set(dev, &p->laddr, sizeof(struct in6_addr)); 1095 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); 1096 } 1097 1098 /* Set up flowi template */ 1099 fl6->saddr = p->laddr; 1100 fl6->daddr = p->raddr; 1101 fl6->flowi6_oif = p->link; 1102 fl6->flowlabel = 0; 1103 fl6->flowi6_proto = IPPROTO_GRE; 1104 fl6->fl6_gre_key = t->parms.o_key; 1105 1106 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) 1107 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; 1108 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) 1109 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; 1110 1111 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); 1112 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); 1113 1114 if (p->flags&IP6_TNL_F_CAP_XMIT && 1115 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER) 1116 dev->flags |= IFF_POINTOPOINT; 1117 else 1118 dev->flags &= ~IFF_POINTOPOINT; 1119 } 1120 1121 static void ip6gre_tnl_link_config_route(struct ip6_tnl *t, int set_mtu, 1122 int t_hlen) 1123 { 1124 const struct __ip6_tnl_parm *p = &t->parms; 1125 struct net_device *dev = t->dev; 1126 1127 if (p->flags & IP6_TNL_F_CAP_XMIT) { 1128 int strict = (ipv6_addr_type(&p->raddr) & 1129 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); 1130 1131 struct rt6_info *rt = rt6_lookup(t->net, 1132 &p->raddr, &p->laddr, 1133 p->link, NULL, strict); 1134 1135 if (!rt) 1136 return; 1137 1138 if (rt->dst.dev) { 1139 unsigned int headroom; 1140 1141 headroom = rt->dst.dev->hard_header_len + t_hlen; 1142 headroom = ip_tunnel_limit_headroom(headroom); 1143 dev->needed_headroom = headroom; 1144 1145 if (set_mtu) { 1146 int mtu = rt->dst.dev->mtu - t_hlen; 1147 1148 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1149 mtu -= 8; 1150 if (dev->type == ARPHRD_ETHER) 1151 mtu -= ETH_HLEN; 1152 1153 if (mtu < IPV6_MIN_MTU) 1154 mtu = IPV6_MIN_MTU; 1155 WRITE_ONCE(dev->mtu, mtu); 1156 } 1157 } 1158 ip6_rt_put(rt); 1159 } 1160 } 1161 1162 static int ip6gre_calc_hlen(struct ip6_tnl *tunnel) 1163 { 1164 int t_hlen; 1165 1166 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags); 1167 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen; 1168 1169 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr); 1170 1171 if (tunnel->dev->header_ops && tunnel->dev->type == ARPHRD_IP6GRE) 1172 tunnel->dev->hard_header_len = t_hlen; 1173 else 1174 tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen; 1175 1176 return t_hlen; 1177 } 1178 1179 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu) 1180 { 1181 ip6gre_tnl_link_config_common(t); 1182 ip6gre_tnl_link_config_route(t, set_mtu, ip6gre_calc_hlen(t)); 1183 } 1184 1185 static void ip6gre_tnl_copy_tnl_parm(struct ip6_tnl *t, 1186 const struct __ip6_tnl_parm *p) 1187 { 1188 t->parms.laddr = p->laddr; 1189 t->parms.raddr = p->raddr; 1190 t->parms.flags = p->flags; 1191 t->parms.hop_limit = p->hop_limit; 1192 t->parms.encap_limit = p->encap_limit; 1193 t->parms.flowinfo = p->flowinfo; 1194 t->parms.link = p->link; 1195 t->parms.proto = p->proto; 1196 t->parms.i_key = p->i_key; 1197 t->parms.o_key = p->o_key; 1198 ip_tunnel_flags_copy(t->parms.i_flags, p->i_flags); 1199 ip_tunnel_flags_copy(t->parms.o_flags, p->o_flags); 1200 t->parms.fwmark = p->fwmark; 1201 t->parms.erspan_ver = p->erspan_ver; 1202 t->parms.index = p->index; 1203 t->parms.dir = p->dir; 1204 t->parms.hwid = p->hwid; 1205 dst_cache_reset(&t->dst_cache); 1206 } 1207 1208 static int ip6gre_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p, 1209 int set_mtu) 1210 { 1211 ip6gre_tnl_copy_tnl_parm(t, p); 1212 ip6gre_tnl_link_config(t, set_mtu); 1213 return 0; 1214 } 1215 1216 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p, 1217 const struct ip6_tnl_parm2 *u) 1218 { 1219 p->laddr = u->laddr; 1220 p->raddr = u->raddr; 1221 p->flags = u->flags; 1222 p->hop_limit = u->hop_limit; 1223 p->encap_limit = u->encap_limit; 1224 p->flowinfo = u->flowinfo; 1225 p->link = u->link; 1226 p->i_key = u->i_key; 1227 p->o_key = u->o_key; 1228 gre_flags_to_tnl_flags(p->i_flags, u->i_flags); 1229 gre_flags_to_tnl_flags(p->o_flags, u->o_flags); 1230 memcpy(p->name, u->name, sizeof(u->name)); 1231 } 1232 1233 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u, 1234 const struct __ip6_tnl_parm *p) 1235 { 1236 u->proto = IPPROTO_GRE; 1237 u->laddr = p->laddr; 1238 u->raddr = p->raddr; 1239 u->flags = p->flags; 1240 u->hop_limit = p->hop_limit; 1241 u->encap_limit = p->encap_limit; 1242 u->flowinfo = p->flowinfo; 1243 u->link = p->link; 1244 u->i_key = p->i_key; 1245 u->o_key = p->o_key; 1246 u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags); 1247 u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags); 1248 memcpy(u->name, p->name, sizeof(u->name)); 1249 } 1250 1251 static int ip6gre_tunnel_siocdevprivate(struct net_device *dev, 1252 struct ifreq *ifr, void __user *data, 1253 int cmd) 1254 { 1255 int err = 0; 1256 struct ip6_tnl_parm2 p; 1257 struct __ip6_tnl_parm p1; 1258 struct ip6_tnl *t = netdev_priv(dev); 1259 struct net *net = t->net; 1260 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 1261 1262 memset(&p1, 0, sizeof(p1)); 1263 1264 switch (cmd) { 1265 case SIOCGETTUNNEL: 1266 if (dev == ign->fb_tunnel_dev) { 1267 if (copy_from_user(&p, data, sizeof(p))) { 1268 err = -EFAULT; 1269 break; 1270 } 1271 ip6gre_tnl_parm_from_user(&p1, &p); 1272 t = ip6gre_tunnel_locate(net, &p1, 0); 1273 if (!t) 1274 t = netdev_priv(dev); 1275 } 1276 memset(&p, 0, sizeof(p)); 1277 ip6gre_tnl_parm_to_user(&p, &t->parms); 1278 if (copy_to_user(data, &p, sizeof(p))) 1279 err = -EFAULT; 1280 break; 1281 1282 case SIOCADDTUNNEL: 1283 case SIOCCHGTUNNEL: 1284 err = -EPERM; 1285 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1286 goto done; 1287 1288 err = -EFAULT; 1289 if (copy_from_user(&p, data, sizeof(p))) 1290 goto done; 1291 1292 err = -EINVAL; 1293 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)) 1294 goto done; 1295 1296 if (!(p.i_flags&GRE_KEY)) 1297 p.i_key = 0; 1298 if (!(p.o_flags&GRE_KEY)) 1299 p.o_key = 0; 1300 1301 ip6gre_tnl_parm_from_user(&p1, &p); 1302 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL); 1303 1304 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { 1305 if (t) { 1306 if (t->dev != dev) { 1307 err = -EEXIST; 1308 break; 1309 } 1310 } else { 1311 t = netdev_priv(dev); 1312 1313 ip6gre_tunnel_unlink(ign, t); 1314 synchronize_net(); 1315 ip6gre_tnl_change(t, &p1, 1); 1316 ip6gre_tunnel_link(ign, t); 1317 netdev_state_change(dev); 1318 } 1319 } 1320 1321 if (t) { 1322 err = 0; 1323 1324 memset(&p, 0, sizeof(p)); 1325 ip6gre_tnl_parm_to_user(&p, &t->parms); 1326 if (copy_to_user(data, &p, sizeof(p))) 1327 err = -EFAULT; 1328 } else 1329 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); 1330 break; 1331 1332 case SIOCDELTUNNEL: 1333 err = -EPERM; 1334 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1335 goto done; 1336 1337 if (dev == ign->fb_tunnel_dev) { 1338 err = -EFAULT; 1339 if (copy_from_user(&p, data, sizeof(p))) 1340 goto done; 1341 err = -ENOENT; 1342 ip6gre_tnl_parm_from_user(&p1, &p); 1343 t = ip6gre_tunnel_locate(net, &p1, 0); 1344 if (!t) 1345 goto done; 1346 err = -EPERM; 1347 if (t == netdev_priv(ign->fb_tunnel_dev)) 1348 goto done; 1349 dev = t->dev; 1350 } 1351 unregister_netdevice(dev); 1352 err = 0; 1353 break; 1354 1355 default: 1356 err = -EINVAL; 1357 } 1358 1359 done: 1360 return err; 1361 } 1362 1363 static int ip6gre_header(struct sk_buff *skb, struct net_device *dev, 1364 unsigned short type, const void *daddr, 1365 const void *saddr, unsigned int len) 1366 { 1367 struct ip6_tnl *t = netdev_priv(dev); 1368 struct ipv6hdr *ipv6h; 1369 int needed; 1370 __be16 *p; 1371 1372 needed = t->hlen + sizeof(*ipv6h); 1373 if (skb_headroom(skb) < needed && 1374 pskb_expand_head(skb, HH_DATA_ALIGN(needed - skb_headroom(skb)), 1375 0, GFP_ATOMIC)) 1376 return -needed; 1377 1378 ipv6h = skb_push(skb, needed); 1379 ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb, 1380 t->fl.u.ip6.flowlabel, 1381 true, &t->fl.u.ip6)); 1382 ipv6h->hop_limit = t->parms.hop_limit; 1383 ipv6h->nexthdr = NEXTHDR_GRE; 1384 ipv6h->saddr = t->parms.laddr; 1385 ipv6h->daddr = t->parms.raddr; 1386 1387 p = (__be16 *)(ipv6h + 1); 1388 p[0] = ip_tunnel_flags_to_be16(t->parms.o_flags); 1389 p[1] = htons(type); 1390 1391 /* 1392 * Set the source hardware address. 1393 */ 1394 1395 if (saddr) 1396 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr)); 1397 if (daddr) 1398 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr)); 1399 if (!ipv6_addr_any(&ipv6h->daddr)) 1400 return t->hlen; 1401 1402 return -t->hlen; 1403 } 1404 1405 static const struct header_ops ip6gre_header_ops = { 1406 .create = ip6gre_header, 1407 }; 1408 1409 static const struct net_device_ops ip6gre_netdev_ops = { 1410 .ndo_init = ip6gre_tunnel_init, 1411 .ndo_uninit = ip6gre_tunnel_uninit, 1412 .ndo_start_xmit = ip6gre_tunnel_xmit, 1413 .ndo_siocdevprivate = ip6gre_tunnel_siocdevprivate, 1414 .ndo_change_mtu = ip6_tnl_change_mtu, 1415 .ndo_get_iflink = ip6_tnl_get_iflink, 1416 }; 1417 1418 static void ip6gre_dev_free(struct net_device *dev) 1419 { 1420 struct ip6_tnl *t = netdev_priv(dev); 1421 1422 gro_cells_destroy(&t->gro_cells); 1423 dst_cache_destroy(&t->dst_cache); 1424 } 1425 1426 static void ip6gre_tunnel_setup(struct net_device *dev) 1427 { 1428 dev->netdev_ops = &ip6gre_netdev_ops; 1429 dev->needs_free_netdev = true; 1430 dev->priv_destructor = ip6gre_dev_free; 1431 1432 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS; 1433 dev->type = ARPHRD_IP6GRE; 1434 1435 dev->flags |= IFF_NOARP; 1436 dev->addr_len = sizeof(struct in6_addr); 1437 netif_keep_dst(dev); 1438 /* This perm addr will be used as interface identifier by IPv6 */ 1439 dev->addr_assign_type = NET_ADDR_RANDOM; 1440 eth_random_addr(dev->perm_addr); 1441 } 1442 1443 #define GRE6_FEATURES (NETIF_F_SG | \ 1444 NETIF_F_FRAGLIST | \ 1445 NETIF_F_HIGHDMA | \ 1446 NETIF_F_HW_CSUM) 1447 1448 static void ip6gre_tnl_init_features(struct net_device *dev) 1449 { 1450 struct ip6_tnl *nt = netdev_priv(dev); 1451 1452 dev->features |= GRE6_FEATURES; 1453 dev->hw_features |= GRE6_FEATURES; 1454 1455 dev->lltx = true; 1456 1457 /* TCP offload with GRE SEQ is not supported, nor can we support 2 1458 * levels of outer headers requiring an update. 1459 */ 1460 if (test_bit(IP_TUNNEL_SEQ_BIT, nt->parms.o_flags)) 1461 return; 1462 if (test_bit(IP_TUNNEL_CSUM_BIT, nt->parms.o_flags) && 1463 nt->encap.type != TUNNEL_ENCAP_NONE) 1464 return; 1465 1466 dev->features |= NETIF_F_GSO_SOFTWARE; 1467 dev->hw_features |= NETIF_F_GSO_SOFTWARE; 1468 } 1469 1470 static int ip6gre_tunnel_init_common(struct net_device *dev) 1471 { 1472 struct ip6_tnl *tunnel; 1473 int ret; 1474 int t_hlen; 1475 1476 tunnel = netdev_priv(dev); 1477 1478 tunnel->dev = dev; 1479 strscpy(tunnel->parms.name, dev->name); 1480 1481 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL); 1482 if (ret) 1483 return ret; 1484 1485 ret = gro_cells_init(&tunnel->gro_cells, dev); 1486 if (ret) 1487 goto cleanup_dst_cache_init; 1488 1489 t_hlen = ip6gre_calc_hlen(tunnel); 1490 dev->mtu = ETH_DATA_LEN - t_hlen; 1491 if (dev->type == ARPHRD_ETHER) 1492 dev->mtu -= ETH_HLEN; 1493 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1494 dev->mtu -= 8; 1495 1496 if (tunnel->parms.collect_md) { 1497 netif_keep_dst(dev); 1498 } 1499 ip6gre_tnl_init_features(dev); 1500 1501 netdev_hold(dev, &tunnel->dev_tracker, GFP_KERNEL); 1502 netdev_lockdep_set_classes(dev); 1503 return 0; 1504 1505 cleanup_dst_cache_init: 1506 dst_cache_destroy(&tunnel->dst_cache); 1507 return ret; 1508 } 1509 1510 static int ip6gre_tunnel_init(struct net_device *dev) 1511 { 1512 struct ip6_tnl *tunnel; 1513 int ret; 1514 1515 ret = ip6gre_tunnel_init_common(dev); 1516 if (ret) 1517 return ret; 1518 1519 tunnel = netdev_priv(dev); 1520 1521 if (tunnel->parms.collect_md) 1522 return 0; 1523 1524 __dev_addr_set(dev, &tunnel->parms.laddr, sizeof(struct in6_addr)); 1525 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr)); 1526 1527 if (ipv6_addr_any(&tunnel->parms.raddr)) 1528 dev->header_ops = &ip6gre_header_ops; 1529 1530 return 0; 1531 } 1532 1533 static void ip6gre_fb_tunnel_init(struct net_device *dev) 1534 { 1535 struct ip6_tnl *tunnel = netdev_priv(dev); 1536 1537 tunnel->dev = dev; 1538 tunnel->net = dev_net(dev); 1539 strscpy(tunnel->parms.name, dev->name); 1540 1541 tunnel->hlen = sizeof(struct ipv6hdr) + 4; 1542 } 1543 1544 static struct inet6_protocol ip6gre_protocol __read_mostly = { 1545 .handler = gre_rcv, 1546 .err_handler = ip6gre_err, 1547 .flags = INET6_PROTO_FINAL, 1548 }; 1549 1550 static void __net_exit ip6gre_exit_rtnl_net(struct net *net, struct list_head *head) 1551 { 1552 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 1553 struct net_device *dev, *aux; 1554 int prio; 1555 1556 for_each_netdev_safe(net, dev, aux) 1557 if (dev->rtnl_link_ops == &ip6gre_link_ops || 1558 dev->rtnl_link_ops == &ip6gre_tap_ops || 1559 dev->rtnl_link_ops == &ip6erspan_tap_ops) 1560 unregister_netdevice_queue(dev, head); 1561 1562 for (prio = 0; prio < 4; prio++) { 1563 int h; 1564 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) { 1565 struct ip6_tnl *t; 1566 1567 t = rtnl_net_dereference(net, ign->tunnels[prio][h]); 1568 1569 while (t) { 1570 /* If dev is in the same netns, it has already 1571 * been added to the list by the previous loop. 1572 */ 1573 if (!net_eq(dev_net(t->dev), net)) 1574 unregister_netdevice_queue(t->dev, head); 1575 1576 t = rtnl_net_dereference(net, t->next); 1577 } 1578 } 1579 } 1580 } 1581 1582 static int __net_init ip6gre_init_net(struct net *net) 1583 { 1584 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 1585 struct net_device *ndev; 1586 int err; 1587 1588 if (!net_has_fallback_tunnels(net)) 1589 return 0; 1590 ndev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0", 1591 NET_NAME_UNKNOWN, ip6gre_tunnel_setup); 1592 if (!ndev) { 1593 err = -ENOMEM; 1594 goto err_alloc_dev; 1595 } 1596 ign->fb_tunnel_dev = ndev; 1597 dev_net_set(ign->fb_tunnel_dev, net); 1598 /* FB netdevice is special: we have one, and only one per netns. 1599 * Allowing to move it to another netns is clearly unsafe. 1600 */ 1601 ign->fb_tunnel_dev->netns_immutable = true; 1602 1603 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev); 1604 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops; 1605 1606 err = register_netdev(ign->fb_tunnel_dev); 1607 if (err) 1608 goto err_reg_dev; 1609 1610 rcu_assign_pointer(ign->tunnels_wc[0], 1611 netdev_priv(ign->fb_tunnel_dev)); 1612 return 0; 1613 1614 err_reg_dev: 1615 free_netdev(ndev); 1616 err_alloc_dev: 1617 return err; 1618 } 1619 1620 static struct pernet_operations ip6gre_net_ops = { 1621 .init = ip6gre_init_net, 1622 .exit_rtnl = ip6gre_exit_rtnl_net, 1623 .id = &ip6gre_net_id, 1624 .size = sizeof(struct ip6gre_net), 1625 }; 1626 1627 static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[], 1628 struct netlink_ext_ack *extack) 1629 { 1630 __be16 flags; 1631 1632 if (!data) 1633 return 0; 1634 1635 flags = 0; 1636 if (data[IFLA_GRE_IFLAGS]) 1637 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); 1638 if (data[IFLA_GRE_OFLAGS]) 1639 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); 1640 if (flags & (GRE_VERSION|GRE_ROUTING)) 1641 return -EINVAL; 1642 1643 return 0; 1644 } 1645 1646 static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[], 1647 struct netlink_ext_ack *extack) 1648 { 1649 struct in6_addr daddr; 1650 1651 if (tb[IFLA_ADDRESS]) { 1652 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 1653 return -EINVAL; 1654 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 1655 return -EADDRNOTAVAIL; 1656 } 1657 1658 if (!data) 1659 goto out; 1660 1661 if (data[IFLA_GRE_REMOTE]) { 1662 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); 1663 if (ipv6_addr_any(&daddr)) 1664 return -EINVAL; 1665 } 1666 1667 out: 1668 return ip6gre_tunnel_validate(tb, data, extack); 1669 } 1670 1671 static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[], 1672 struct netlink_ext_ack *extack) 1673 { 1674 __be16 flags = 0; 1675 int ret, ver = 0; 1676 1677 if (!data) 1678 return 0; 1679 1680 ret = ip6gre_tap_validate(tb, data, extack); 1681 if (ret) 1682 return ret; 1683 1684 /* ERSPAN should only have GRE sequence and key flag */ 1685 if (data[IFLA_GRE_OFLAGS]) 1686 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); 1687 if (data[IFLA_GRE_IFLAGS]) 1688 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); 1689 if (!data[IFLA_GRE_COLLECT_METADATA] && 1690 flags != (GRE_SEQ | GRE_KEY)) 1691 return -EINVAL; 1692 1693 /* ERSPAN Session ID only has 10-bit. Since we reuse 1694 * 32-bit key field as ID, check it's range. 1695 */ 1696 if (data[IFLA_GRE_IKEY] && 1697 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK)) 1698 return -EINVAL; 1699 1700 if (data[IFLA_GRE_OKEY] && 1701 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK)) 1702 return -EINVAL; 1703 1704 if (data[IFLA_GRE_ERSPAN_VER]) { 1705 ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]); 1706 if (ver != 1 && ver != 2) 1707 return -EINVAL; 1708 } 1709 1710 if (ver == 1) { 1711 if (data[IFLA_GRE_ERSPAN_INDEX]) { 1712 u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]); 1713 1714 if (index & ~INDEX_MASK) 1715 return -EINVAL; 1716 } 1717 } else if (ver == 2) { 1718 if (data[IFLA_GRE_ERSPAN_DIR]) { 1719 u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]); 1720 1721 if (dir & ~(DIR_MASK >> DIR_OFFSET)) 1722 return -EINVAL; 1723 } 1724 1725 if (data[IFLA_GRE_ERSPAN_HWID]) { 1726 u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]); 1727 1728 if (hwid & ~(HWID_MASK >> HWID_OFFSET)) 1729 return -EINVAL; 1730 } 1731 } 1732 1733 return 0; 1734 } 1735 1736 static void ip6erspan_set_version(struct nlattr *data[], 1737 struct __ip6_tnl_parm *parms) 1738 { 1739 if (!data) 1740 return; 1741 1742 parms->erspan_ver = 1; 1743 if (data[IFLA_GRE_ERSPAN_VER]) 1744 parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]); 1745 1746 if (parms->erspan_ver == 1) { 1747 if (data[IFLA_GRE_ERSPAN_INDEX]) 1748 parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]); 1749 } else if (parms->erspan_ver == 2) { 1750 if (data[IFLA_GRE_ERSPAN_DIR]) 1751 parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]); 1752 if (data[IFLA_GRE_ERSPAN_HWID]) 1753 parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]); 1754 } 1755 } 1756 1757 static void ip6gre_netlink_parms(struct nlattr *data[], 1758 struct __ip6_tnl_parm *parms) 1759 { 1760 memset(parms, 0, sizeof(*parms)); 1761 1762 if (!data) 1763 return; 1764 1765 if (data[IFLA_GRE_LINK]) 1766 parms->link = nla_get_u32(data[IFLA_GRE_LINK]); 1767 1768 if (data[IFLA_GRE_IFLAGS]) 1769 gre_flags_to_tnl_flags(parms->i_flags, 1770 nla_get_be16(data[IFLA_GRE_IFLAGS])); 1771 1772 if (data[IFLA_GRE_OFLAGS]) 1773 gre_flags_to_tnl_flags(parms->o_flags, 1774 nla_get_be16(data[IFLA_GRE_OFLAGS])); 1775 1776 if (data[IFLA_GRE_IKEY]) 1777 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); 1778 1779 if (data[IFLA_GRE_OKEY]) 1780 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); 1781 1782 if (data[IFLA_GRE_LOCAL]) 1783 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]); 1784 1785 if (data[IFLA_GRE_REMOTE]) 1786 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); 1787 1788 if (data[IFLA_GRE_TTL]) 1789 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]); 1790 1791 if (data[IFLA_GRE_ENCAP_LIMIT]) 1792 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]); 1793 1794 if (data[IFLA_GRE_FLOWINFO]) 1795 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]); 1796 1797 if (data[IFLA_GRE_FLAGS]) 1798 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]); 1799 1800 if (data[IFLA_GRE_FWMARK]) 1801 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]); 1802 1803 if (data[IFLA_GRE_COLLECT_METADATA]) 1804 parms->collect_md = true; 1805 } 1806 1807 static int ip6gre_tap_init(struct net_device *dev) 1808 { 1809 int ret; 1810 1811 ret = ip6gre_tunnel_init_common(dev); 1812 if (ret) 1813 return ret; 1814 1815 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 1816 1817 return 0; 1818 } 1819 1820 static const struct net_device_ops ip6gre_tap_netdev_ops = { 1821 .ndo_init = ip6gre_tap_init, 1822 .ndo_uninit = ip6gre_tunnel_uninit, 1823 .ndo_start_xmit = ip6gre_tunnel_xmit, 1824 .ndo_set_mac_address = eth_mac_addr, 1825 .ndo_validate_addr = eth_validate_addr, 1826 .ndo_change_mtu = ip6_tnl_change_mtu, 1827 .ndo_get_iflink = ip6_tnl_get_iflink, 1828 }; 1829 1830 static int ip6erspan_calc_hlen(struct ip6_tnl *tunnel) 1831 { 1832 int t_hlen; 1833 1834 tunnel->tun_hlen = 8; 1835 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen + 1836 erspan_hdr_len(tunnel->parms.erspan_ver); 1837 1838 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr); 1839 tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen; 1840 return t_hlen; 1841 } 1842 1843 static int ip6erspan_tap_init(struct net_device *dev) 1844 { 1845 struct ip6_tnl *tunnel; 1846 int t_hlen; 1847 int ret; 1848 1849 tunnel = netdev_priv(dev); 1850 1851 tunnel->dev = dev; 1852 strscpy(tunnel->parms.name, dev->name); 1853 1854 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL); 1855 if (ret) 1856 return ret; 1857 1858 ret = gro_cells_init(&tunnel->gro_cells, dev); 1859 if (ret) 1860 goto cleanup_dst_cache_init; 1861 1862 t_hlen = ip6erspan_calc_hlen(tunnel); 1863 dev->mtu = ETH_DATA_LEN - t_hlen; 1864 if (dev->type == ARPHRD_ETHER) 1865 dev->mtu -= ETH_HLEN; 1866 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1867 dev->mtu -= 8; 1868 1869 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 1870 ip6erspan_tnl_link_config(tunnel, 1); 1871 1872 netdev_hold(dev, &tunnel->dev_tracker, GFP_KERNEL); 1873 netdev_lockdep_set_classes(dev); 1874 return 0; 1875 1876 cleanup_dst_cache_init: 1877 dst_cache_destroy(&tunnel->dst_cache); 1878 return ret; 1879 } 1880 1881 static const struct net_device_ops ip6erspan_netdev_ops = { 1882 .ndo_init = ip6erspan_tap_init, 1883 .ndo_uninit = ip6erspan_tunnel_uninit, 1884 .ndo_start_xmit = ip6erspan_tunnel_xmit, 1885 .ndo_set_mac_address = eth_mac_addr, 1886 .ndo_validate_addr = eth_validate_addr, 1887 .ndo_change_mtu = ip6_tnl_change_mtu, 1888 .ndo_get_iflink = ip6_tnl_get_iflink, 1889 }; 1890 1891 static void ip6gre_tap_setup(struct net_device *dev) 1892 { 1893 1894 ether_setup(dev); 1895 1896 dev->max_mtu = 0; 1897 dev->netdev_ops = &ip6gre_tap_netdev_ops; 1898 dev->needs_free_netdev = true; 1899 dev->priv_destructor = ip6gre_dev_free; 1900 1901 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS; 1902 dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1903 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 1904 netif_keep_dst(dev); 1905 } 1906 1907 static bool ip6gre_netlink_encap_parms(struct nlattr *data[], 1908 struct ip_tunnel_encap *ipencap) 1909 { 1910 bool ret = false; 1911 1912 memset(ipencap, 0, sizeof(*ipencap)); 1913 1914 if (!data) 1915 return ret; 1916 1917 if (data[IFLA_GRE_ENCAP_TYPE]) { 1918 ret = true; 1919 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]); 1920 } 1921 1922 if (data[IFLA_GRE_ENCAP_FLAGS]) { 1923 ret = true; 1924 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]); 1925 } 1926 1927 if (data[IFLA_GRE_ENCAP_SPORT]) { 1928 ret = true; 1929 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]); 1930 } 1931 1932 if (data[IFLA_GRE_ENCAP_DPORT]) { 1933 ret = true; 1934 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]); 1935 } 1936 1937 return ret; 1938 } 1939 1940 static int ip6gre_newlink_common(struct net *link_net, struct net_device *dev, 1941 struct nlattr *tb[], struct nlattr *data[], 1942 struct netlink_ext_ack *extack) 1943 { 1944 struct ip6_tnl *nt; 1945 struct ip_tunnel_encap ipencap; 1946 int err; 1947 1948 nt = netdev_priv(dev); 1949 1950 if (ip6gre_netlink_encap_parms(data, &ipencap)) { 1951 int err = ip6_tnl_encap_setup(nt, &ipencap); 1952 1953 if (err < 0) 1954 return err; 1955 } 1956 1957 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS]) 1958 eth_hw_addr_random(dev); 1959 1960 nt->dev = dev; 1961 nt->net = link_net; 1962 1963 err = register_netdevice(dev); 1964 if (err) 1965 goto out; 1966 1967 if (tb[IFLA_MTU]) 1968 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 1969 1970 out: 1971 return err; 1972 } 1973 1974 static int ip6gre_newlink(struct net_device *dev, 1975 struct rtnl_newlink_params *params, 1976 struct netlink_ext_ack *extack) 1977 { 1978 struct net *net = params->link_net ? : dev_net(dev); 1979 struct ip6_tnl *nt = netdev_priv(dev); 1980 struct nlattr **data = params->data; 1981 struct nlattr **tb = params->tb; 1982 struct ip6gre_net *ign; 1983 int err; 1984 1985 ip6gre_netlink_parms(data, &nt->parms); 1986 ign = net_generic(net, ip6gre_net_id); 1987 1988 if (nt->parms.collect_md) { 1989 if (rtnl_dereference(ign->collect_md_tun)) 1990 return -EEXIST; 1991 } else { 1992 if (ip6gre_tunnel_find(net, &nt->parms, dev->type)) 1993 return -EEXIST; 1994 } 1995 1996 err = ip6gre_newlink_common(net, dev, tb, data, extack); 1997 if (!err) { 1998 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]); 1999 ip6gre_tunnel_link_md(ign, nt); 2000 ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt); 2001 } 2002 return err; 2003 } 2004 2005 static struct ip6_tnl * 2006 ip6gre_changelink_common(struct net_device *dev, struct nlattr *tb[], 2007 struct nlattr *data[], struct __ip6_tnl_parm *p_p, 2008 struct netlink_ext_ack *extack) 2009 { 2010 struct ip6_tnl *t, *nt = netdev_priv(dev); 2011 struct net *net = nt->net; 2012 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 2013 struct ip_tunnel_encap ipencap; 2014 2015 if (dev == ign->fb_tunnel_dev) 2016 return ERR_PTR(-EINVAL); 2017 2018 if (ip6gre_netlink_encap_parms(data, &ipencap)) { 2019 int err = ip6_tnl_encap_setup(nt, &ipencap); 2020 2021 if (err < 0) 2022 return ERR_PTR(err); 2023 } 2024 2025 ip6gre_netlink_parms(data, p_p); 2026 2027 t = ip6gre_tunnel_locate(net, p_p, 0); 2028 2029 if (t) { 2030 if (t->dev != dev) 2031 return ERR_PTR(-EEXIST); 2032 } else { 2033 t = nt; 2034 } 2035 2036 return t; 2037 } 2038 2039 static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[], 2040 struct nlattr *data[], 2041 struct netlink_ext_ack *extack) 2042 { 2043 struct ip6_tnl *t = netdev_priv(dev); 2044 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id); 2045 struct __ip6_tnl_parm p; 2046 2047 if (!rtnl_dev_link_net_capable(dev, t->net)) 2048 return -EPERM; 2049 2050 t = ip6gre_changelink_common(dev, tb, data, &p, extack); 2051 if (IS_ERR(t)) 2052 return PTR_ERR(t); 2053 2054 ip6gre_tunnel_unlink_md(ign, t); 2055 ip6gre_tunnel_unlink(ign, t); 2056 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]); 2057 ip6gre_tunnel_link_md(ign, t); 2058 ip6gre_tunnel_link(ign, t); 2059 return 0; 2060 } 2061 2062 static void ip6gre_dellink(struct net_device *dev, struct list_head *head) 2063 { 2064 struct net *net = dev_net(dev); 2065 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); 2066 2067 if (dev != ign->fb_tunnel_dev) 2068 unregister_netdevice_queue(dev, head); 2069 } 2070 2071 static size_t ip6gre_get_size(const struct net_device *dev) 2072 { 2073 return 2074 /* IFLA_GRE_LINK */ 2075 nla_total_size(4) + 2076 /* IFLA_GRE_IFLAGS */ 2077 nla_total_size(2) + 2078 /* IFLA_GRE_OFLAGS */ 2079 nla_total_size(2) + 2080 /* IFLA_GRE_IKEY */ 2081 nla_total_size(4) + 2082 /* IFLA_GRE_OKEY */ 2083 nla_total_size(4) + 2084 /* IFLA_GRE_LOCAL */ 2085 nla_total_size(sizeof(struct in6_addr)) + 2086 /* IFLA_GRE_REMOTE */ 2087 nla_total_size(sizeof(struct in6_addr)) + 2088 /* IFLA_GRE_TTL */ 2089 nla_total_size(1) + 2090 /* IFLA_GRE_ENCAP_LIMIT */ 2091 nla_total_size(1) + 2092 /* IFLA_GRE_FLOWINFO */ 2093 nla_total_size(4) + 2094 /* IFLA_GRE_FLAGS */ 2095 nla_total_size(4) + 2096 /* IFLA_GRE_ENCAP_TYPE */ 2097 nla_total_size(2) + 2098 /* IFLA_GRE_ENCAP_FLAGS */ 2099 nla_total_size(2) + 2100 /* IFLA_GRE_ENCAP_SPORT */ 2101 nla_total_size(2) + 2102 /* IFLA_GRE_ENCAP_DPORT */ 2103 nla_total_size(2) + 2104 /* IFLA_GRE_COLLECT_METADATA */ 2105 nla_total_size(0) + 2106 /* IFLA_GRE_FWMARK */ 2107 nla_total_size(4) + 2108 /* IFLA_GRE_ERSPAN_INDEX */ 2109 nla_total_size(4) + 2110 0; 2111 } 2112 2113 static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev) 2114 { 2115 const struct ip6_tnl *t = netdev_priv(dev); 2116 const struct __ip6_tnl_parm *p = &t->parms; 2117 IP_TUNNEL_DECLARE_FLAGS(o_flags); 2118 2119 ip_tunnel_flags_copy(o_flags, p->o_flags); 2120 2121 if (p->erspan_ver == 1 || p->erspan_ver == 2) { 2122 if (!p->collect_md) 2123 __set_bit(IP_TUNNEL_KEY_BIT, o_flags); 2124 2125 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver)) 2126 goto nla_put_failure; 2127 2128 if (p->erspan_ver == 1) { 2129 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index)) 2130 goto nla_put_failure; 2131 } else { 2132 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir)) 2133 goto nla_put_failure; 2134 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid)) 2135 goto nla_put_failure; 2136 } 2137 } 2138 2139 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || 2140 nla_put_be16(skb, IFLA_GRE_IFLAGS, 2141 gre_tnl_flags_to_gre_flags(p->i_flags)) || 2142 nla_put_be16(skb, IFLA_GRE_OFLAGS, 2143 gre_tnl_flags_to_gre_flags(o_flags)) || 2144 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || 2145 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || 2146 nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) || 2147 nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) || 2148 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) || 2149 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) || 2150 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) || 2151 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) || 2152 nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark)) 2153 goto nla_put_failure; 2154 2155 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE, 2156 t->encap.type) || 2157 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT, 2158 t->encap.sport) || 2159 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT, 2160 t->encap.dport) || 2161 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS, 2162 t->encap.flags)) 2163 goto nla_put_failure; 2164 2165 if (p->collect_md) { 2166 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA)) 2167 goto nla_put_failure; 2168 } 2169 2170 return 0; 2171 2172 nla_put_failure: 2173 return -EMSGSIZE; 2174 } 2175 2176 static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = { 2177 [IFLA_GRE_LINK] = { .type = NLA_U32 }, 2178 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, 2179 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, 2180 [IFLA_GRE_IKEY] = { .type = NLA_U32 }, 2181 [IFLA_GRE_OKEY] = { .type = NLA_U32 }, 2182 [IFLA_GRE_LOCAL] = { .len = sizeof_field(struct ipv6hdr, saddr) }, 2183 [IFLA_GRE_REMOTE] = { .len = sizeof_field(struct ipv6hdr, daddr) }, 2184 [IFLA_GRE_TTL] = { .type = NLA_U8 }, 2185 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 }, 2186 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 }, 2187 [IFLA_GRE_FLAGS] = { .type = NLA_U32 }, 2188 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 }, 2189 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 }, 2190 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 }, 2191 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 }, 2192 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG }, 2193 [IFLA_GRE_FWMARK] = { .type = NLA_U32 }, 2194 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 }, 2195 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 }, 2196 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 }, 2197 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 }, 2198 }; 2199 2200 static void ip6erspan_tap_setup(struct net_device *dev) 2201 { 2202 ether_setup(dev); 2203 2204 dev->max_mtu = 0; 2205 dev->netdev_ops = &ip6erspan_netdev_ops; 2206 dev->needs_free_netdev = true; 2207 dev->priv_destructor = ip6gre_dev_free; 2208 2209 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS; 2210 dev->priv_flags &= ~IFF_TX_SKB_SHARING; 2211 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 2212 netif_keep_dst(dev); 2213 } 2214 2215 static int ip6erspan_newlink(struct net_device *dev, 2216 struct rtnl_newlink_params *params, 2217 struct netlink_ext_ack *extack) 2218 { 2219 struct net *net = params->link_net ? : dev_net(dev); 2220 struct ip6_tnl *nt = netdev_priv(dev); 2221 struct nlattr **data = params->data; 2222 struct nlattr **tb = params->tb; 2223 struct ip6gre_net *ign; 2224 int err; 2225 2226 ip6gre_netlink_parms(data, &nt->parms); 2227 ip6erspan_set_version(data, &nt->parms); 2228 ign = net_generic(net, ip6gre_net_id); 2229 2230 if (nt->parms.collect_md) { 2231 if (rtnl_dereference(ign->collect_md_tun_erspan)) 2232 return -EEXIST; 2233 } else { 2234 if (ip6gre_tunnel_find(net, &nt->parms, dev->type)) 2235 return -EEXIST; 2236 } 2237 2238 err = ip6gre_newlink_common(net, dev, tb, data, extack); 2239 if (!err) { 2240 ip6erspan_tnl_link_config(nt, !tb[IFLA_MTU]); 2241 ip6erspan_tunnel_link_md(ign, nt); 2242 ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt); 2243 } 2244 return err; 2245 } 2246 2247 static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu) 2248 { 2249 ip6gre_tnl_link_config_common(t); 2250 ip6gre_tnl_link_config_route(t, set_mtu, ip6erspan_calc_hlen(t)); 2251 } 2252 2253 static int ip6erspan_tnl_change(struct ip6_tnl *t, 2254 const struct __ip6_tnl_parm *p, int set_mtu) 2255 { 2256 ip6gre_tnl_copy_tnl_parm(t, p); 2257 ip6erspan_tnl_link_config(t, set_mtu); 2258 return 0; 2259 } 2260 2261 static int ip6erspan_changelink(struct net_device *dev, struct nlattr *tb[], 2262 struct nlattr *data[], 2263 struct netlink_ext_ack *extack) 2264 { 2265 struct ip6_tnl *t = netdev_priv(dev); 2266 struct __ip6_tnl_parm p; 2267 struct ip6gre_net *ign; 2268 2269 if (!rtnl_dev_link_net_capable(dev, t->net)) 2270 return -EPERM; 2271 2272 ign = net_generic(t->net, ip6gre_net_id); 2273 t = ip6gre_changelink_common(dev, tb, data, &p, extack); 2274 if (IS_ERR(t)) 2275 return PTR_ERR(t); 2276 2277 ip6erspan_set_version(data, &p); 2278 ip6gre_tunnel_unlink_md(ign, t); 2279 ip6gre_tunnel_unlink(ign, t); 2280 ip6erspan_tnl_change(t, &p, !tb[IFLA_MTU]); 2281 ip6erspan_tunnel_link_md(ign, t); 2282 ip6gre_tunnel_link(ign, t); 2283 return 0; 2284 } 2285 2286 static struct rtnl_link_ops ip6gre_link_ops __read_mostly = { 2287 .kind = "ip6gre", 2288 .maxtype = IFLA_GRE_MAX, 2289 .policy = ip6gre_policy, 2290 .priv_size = sizeof(struct ip6_tnl), 2291 .setup = ip6gre_tunnel_setup, 2292 .validate = ip6gre_tunnel_validate, 2293 .newlink = ip6gre_newlink, 2294 .changelink = ip6gre_changelink, 2295 .dellink = ip6gre_dellink, 2296 .get_size = ip6gre_get_size, 2297 .fill_info = ip6gre_fill_info, 2298 .get_link_net = ip6_tnl_get_link_net, 2299 }; 2300 2301 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = { 2302 .kind = "ip6gretap", 2303 .maxtype = IFLA_GRE_MAX, 2304 .policy = ip6gre_policy, 2305 .priv_size = sizeof(struct ip6_tnl), 2306 .setup = ip6gre_tap_setup, 2307 .validate = ip6gre_tap_validate, 2308 .newlink = ip6gre_newlink, 2309 .changelink = ip6gre_changelink, 2310 .get_size = ip6gre_get_size, 2311 .fill_info = ip6gre_fill_info, 2312 .get_link_net = ip6_tnl_get_link_net, 2313 }; 2314 2315 static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = { 2316 .kind = "ip6erspan", 2317 .maxtype = IFLA_GRE_MAX, 2318 .policy = ip6gre_policy, 2319 .priv_size = sizeof(struct ip6_tnl), 2320 .setup = ip6erspan_tap_setup, 2321 .validate = ip6erspan_tap_validate, 2322 .newlink = ip6erspan_newlink, 2323 .changelink = ip6erspan_changelink, 2324 .get_size = ip6gre_get_size, 2325 .fill_info = ip6gre_fill_info, 2326 .get_link_net = ip6_tnl_get_link_net, 2327 }; 2328 2329 /* 2330 * And now the modules code and kernel interface. 2331 */ 2332 2333 static int __init ip6gre_init(void) 2334 { 2335 int err; 2336 2337 pr_info("GRE over IPv6 tunneling driver\n"); 2338 2339 err = register_pernet_device(&ip6gre_net_ops); 2340 if (err < 0) 2341 return err; 2342 2343 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE); 2344 if (err < 0) { 2345 pr_info("%s: can't add protocol\n", __func__); 2346 goto add_proto_failed; 2347 } 2348 2349 err = rtnl_link_register(&ip6gre_link_ops); 2350 if (err < 0) 2351 goto rtnl_link_failed; 2352 2353 err = rtnl_link_register(&ip6gre_tap_ops); 2354 if (err < 0) 2355 goto tap_ops_failed; 2356 2357 err = rtnl_link_register(&ip6erspan_tap_ops); 2358 if (err < 0) 2359 goto erspan_link_failed; 2360 2361 out: 2362 return err; 2363 2364 erspan_link_failed: 2365 rtnl_link_unregister(&ip6gre_tap_ops); 2366 tap_ops_failed: 2367 rtnl_link_unregister(&ip6gre_link_ops); 2368 rtnl_link_failed: 2369 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); 2370 add_proto_failed: 2371 unregister_pernet_device(&ip6gre_net_ops); 2372 goto out; 2373 } 2374 2375 static void __exit ip6gre_fini(void) 2376 { 2377 rtnl_link_unregister(&ip6gre_tap_ops); 2378 rtnl_link_unregister(&ip6gre_link_ops); 2379 rtnl_link_unregister(&ip6erspan_tap_ops); 2380 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); 2381 unregister_pernet_device(&ip6gre_net_ops); 2382 } 2383 2384 module_init(ip6gre_init); 2385 module_exit(ip6gre_fini); 2386 MODULE_LICENSE("GPL"); 2387 MODULE_AUTHOR("D. Kozlov <xeb@mail.ru>"); 2388 MODULE_DESCRIPTION("GRE over IPv6 tunneling device"); 2389 MODULE_ALIAS_RTNL_LINK("ip6gre"); 2390 MODULE_ALIAS_RTNL_LINK("ip6gretap"); 2391 MODULE_ALIAS_RTNL_LINK("ip6erspan"); 2392 MODULE_ALIAS_NETDEV("ip6gre0"); 2393