0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/module.h>
0015 #include <linux/capability.h>
0016 #include <linux/errno.h>
0017 #include <linux/types.h>
0018 #include <linux/sockios.h>
0019 #include <linux/icmp.h>
0020 #include <linux/if.h>
0021 #include <linux/in.h>
0022 #include <linux/ip.h>
0023 #include <linux/net.h>
0024 #include <linux/in6.h>
0025 #include <linux/netdevice.h>
0026 #include <linux/if_arp.h>
0027 #include <linux/icmpv6.h>
0028 #include <linux/init.h>
0029 #include <linux/route.h>
0030 #include <linux/rtnetlink.h>
0031 #include <linux/netfilter_ipv6.h>
0032 #include <linux/slab.h>
0033 #include <linux/hash.h>
0034
0035 #include <linux/uaccess.h>
0036 #include <linux/atomic.h>
0037
0038 #include <net/icmp.h>
0039 #include <net/ip.h>
0040 #include <net/ip_tunnels.h>
0041 #include <net/ipv6.h>
0042 #include <net/ip6_route.h>
0043 #include <net/addrconf.h>
0044 #include <net/ip6_tunnel.h>
0045 #include <net/xfrm.h>
0046 #include <net/net_namespace.h>
0047 #include <net/netns/generic.h>
0048 #include <linux/etherdevice.h>
0049
0050 #define IP6_VTI_HASH_SIZE_SHIFT 5
0051 #define IP6_VTI_HASH_SIZE (1 << IP6_VTI_HASH_SIZE_SHIFT)
0052
0053 static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
0054 {
0055 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
0056
0057 return hash_32(hash, IP6_VTI_HASH_SIZE_SHIFT);
0058 }
0059
0060 static int vti6_dev_init(struct net_device *dev);
0061 static void vti6_dev_setup(struct net_device *dev);
0062 static struct rtnl_link_ops vti6_link_ops __read_mostly;
0063
0064 static unsigned int vti6_net_id __read_mostly;
0065 struct vti6_net {
0066
0067 struct net_device *fb_tnl_dev;
0068
0069 struct ip6_tnl __rcu *tnls_r_l[IP6_VTI_HASH_SIZE];
0070 struct ip6_tnl __rcu *tnls_wc[1];
0071 struct ip6_tnl __rcu **tnls[2];
0072 };
0073
0074 #define for_each_vti6_tunnel_rcu(start) \
0075 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088 static struct ip6_tnl *
0089 vti6_tnl_lookup(struct net *net, const struct in6_addr *remote,
0090 const struct in6_addr *local)
0091 {
0092 unsigned int hash = HASH(remote, local);
0093 struct ip6_tnl *t;
0094 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
0095 struct in6_addr any;
0096
0097 for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
0098 if (ipv6_addr_equal(local, &t->parms.laddr) &&
0099 ipv6_addr_equal(remote, &t->parms.raddr) &&
0100 (t->dev->flags & IFF_UP))
0101 return t;
0102 }
0103
0104 memset(&any, 0, sizeof(any));
0105 hash = HASH(&any, local);
0106 for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
0107 if (ipv6_addr_equal(local, &t->parms.laddr) &&
0108 (t->dev->flags & IFF_UP))
0109 return t;
0110 }
0111
0112 hash = HASH(remote, &any);
0113 for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
0114 if (ipv6_addr_equal(remote, &t->parms.raddr) &&
0115 (t->dev->flags & IFF_UP))
0116 return t;
0117 }
0118
0119 t = rcu_dereference(ip6n->tnls_wc[0]);
0120 if (t && (t->dev->flags & IFF_UP))
0121 return t;
0122
0123 return NULL;
0124 }
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137 static struct ip6_tnl __rcu **
0138 vti6_tnl_bucket(struct vti6_net *ip6n, const struct __ip6_tnl_parm *p)
0139 {
0140 const struct in6_addr *remote = &p->raddr;
0141 const struct in6_addr *local = &p->laddr;
0142 unsigned int h = 0;
0143 int prio = 0;
0144
0145 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
0146 prio = 1;
0147 h = HASH(remote, local);
0148 }
0149 return &ip6n->tnls[prio][h];
0150 }
0151
0152 static void
0153 vti6_tnl_link(struct vti6_net *ip6n, struct ip6_tnl *t)
0154 {
0155 struct ip6_tnl __rcu **tp = vti6_tnl_bucket(ip6n, &t->parms);
0156
0157 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
0158 rcu_assign_pointer(*tp, t);
0159 }
0160
0161 static void
0162 vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t)
0163 {
0164 struct ip6_tnl __rcu **tp;
0165 struct ip6_tnl *iter;
0166
0167 for (tp = vti6_tnl_bucket(ip6n, &t->parms);
0168 (iter = rtnl_dereference(*tp)) != NULL;
0169 tp = &iter->next) {
0170 if (t == iter) {
0171 rcu_assign_pointer(*tp, t->next);
0172 break;
0173 }
0174 }
0175 }
0176
0177 static void vti6_dev_free(struct net_device *dev)
0178 {
0179 free_percpu(dev->tstats);
0180 }
0181
0182 static int vti6_tnl_create2(struct net_device *dev)
0183 {
0184 struct ip6_tnl *t = netdev_priv(dev);
0185 struct net *net = dev_net(dev);
0186 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
0187 int err;
0188
0189 dev->rtnl_link_ops = &vti6_link_ops;
0190 err = register_netdevice(dev);
0191 if (err < 0)
0192 goto out;
0193
0194 strcpy(t->parms.name, dev->name);
0195
0196 vti6_tnl_link(ip6n, t);
0197
0198 return 0;
0199
0200 out:
0201 return err;
0202 }
0203
0204 static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
0205 {
0206 struct net_device *dev;
0207 struct ip6_tnl *t;
0208 char name[IFNAMSIZ];
0209 int err;
0210
0211 if (p->name[0]) {
0212 if (!dev_valid_name(p->name))
0213 goto failed;
0214 strlcpy(name, p->name, IFNAMSIZ);
0215 } else {
0216 sprintf(name, "ip6_vti%%d");
0217 }
0218
0219 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, vti6_dev_setup);
0220 if (!dev)
0221 goto failed;
0222
0223 dev_net_set(dev, net);
0224
0225 t = netdev_priv(dev);
0226 t->parms = *p;
0227 t->net = dev_net(dev);
0228
0229 err = vti6_tnl_create2(dev);
0230 if (err < 0)
0231 goto failed_free;
0232
0233 return t;
0234
0235 failed_free:
0236 free_netdev(dev);
0237 failed:
0238 return NULL;
0239 }
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255 static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
0256 int create)
0257 {
0258 const struct in6_addr *remote = &p->raddr;
0259 const struct in6_addr *local = &p->laddr;
0260 struct ip6_tnl __rcu **tp;
0261 struct ip6_tnl *t;
0262 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
0263
0264 for (tp = vti6_tnl_bucket(ip6n, p);
0265 (t = rtnl_dereference(*tp)) != NULL;
0266 tp = &t->next) {
0267 if (ipv6_addr_equal(local, &t->parms.laddr) &&
0268 ipv6_addr_equal(remote, &t->parms.raddr)) {
0269 if (create)
0270 return NULL;
0271
0272 return t;
0273 }
0274 }
0275 if (!create)
0276 return NULL;
0277 return vti6_tnl_create(net, p);
0278 }
0279
0280
0281
0282
0283
0284
0285
0286
0287 static void vti6_dev_uninit(struct net_device *dev)
0288 {
0289 struct ip6_tnl *t = netdev_priv(dev);
0290 struct vti6_net *ip6n = net_generic(t->net, vti6_net_id);
0291
0292 if (dev == ip6n->fb_tnl_dev)
0293 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
0294 else
0295 vti6_tnl_unlink(ip6n, t);
0296 netdev_put(dev, &t->dev_tracker);
0297 }
0298
0299 static int vti6_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
0300 int encap_type)
0301 {
0302 struct ip6_tnl *t;
0303 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
0304
0305 rcu_read_lock();
0306 t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
0307 if (t) {
0308 if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
0309 rcu_read_unlock();
0310 goto discard;
0311 }
0312
0313 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
0314 rcu_read_unlock();
0315 goto discard;
0316 }
0317
0318 ipv6h = ipv6_hdr(skb);
0319 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
0320 t->dev->stats.rx_dropped++;
0321 rcu_read_unlock();
0322 goto discard;
0323 }
0324
0325 rcu_read_unlock();
0326
0327 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
0328 XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
0329 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
0330 return xfrm_input(skb, nexthdr, spi, encap_type);
0331 }
0332 rcu_read_unlock();
0333 return -EINVAL;
0334 discard:
0335 kfree_skb(skb);
0336 return 0;
0337 }
0338
0339 static int vti6_rcv(struct sk_buff *skb)
0340 {
0341 int nexthdr = skb_network_header(skb)[IP6CB(skb)->nhoff];
0342
0343 return vti6_input_proto(skb, nexthdr, 0, 0);
0344 }
0345
0346 static int vti6_rcv_cb(struct sk_buff *skb, int err)
0347 {
0348 unsigned short family;
0349 struct net_device *dev;
0350 struct xfrm_state *x;
0351 const struct xfrm_mode *inner_mode;
0352 struct ip6_tnl *t = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6;
0353 u32 orig_mark = skb->mark;
0354 int ret;
0355
0356 if (!t)
0357 return 1;
0358
0359 dev = t->dev;
0360
0361 if (err) {
0362 dev->stats.rx_errors++;
0363 dev->stats.rx_dropped++;
0364
0365 return 0;
0366 }
0367
0368 x = xfrm_input_state(skb);
0369
0370 inner_mode = &x->inner_mode;
0371
0372 if (x->sel.family == AF_UNSPEC) {
0373 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
0374 if (inner_mode == NULL) {
0375 XFRM_INC_STATS(dev_net(skb->dev),
0376 LINUX_MIB_XFRMINSTATEMODEERROR);
0377 return -EINVAL;
0378 }
0379 }
0380
0381 family = inner_mode->family;
0382
0383 skb->mark = be32_to_cpu(t->parms.i_key);
0384 ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
0385 skb->mark = orig_mark;
0386
0387 if (!ret)
0388 return -EPERM;
0389
0390 skb_scrub_packet(skb, !net_eq(t->net, dev_net(skb->dev)));
0391 skb->dev = dev;
0392 dev_sw_netstats_rx_add(dev, skb->len);
0393
0394 return 0;
0395 }
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410 static inline bool
0411 vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
0412 {
0413 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
0414 }
0415
0416 static bool vti6_state_check(const struct xfrm_state *x,
0417 const struct in6_addr *dst,
0418 const struct in6_addr *src)
0419 {
0420 xfrm_address_t *daddr = (xfrm_address_t *)dst;
0421 xfrm_address_t *saddr = (xfrm_address_t *)src;
0422
0423
0424
0425
0426 if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
0427 x->props.family != AF_INET6)
0428 return false;
0429
0430 if (ipv6_addr_any(dst))
0431 return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET6);
0432
0433 if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET6))
0434 return false;
0435
0436 return true;
0437 }
0438
0439
0440
0441
0442
0443
0444
0445 static int
0446 vti6_xmit(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
0447 {
0448 struct ip6_tnl *t = netdev_priv(dev);
0449 struct net_device_stats *stats = &t->dev->stats;
0450 struct dst_entry *dst = skb_dst(skb);
0451 struct net_device *tdev;
0452 struct xfrm_state *x;
0453 int pkt_len = skb->len;
0454 int err = -1;
0455 int mtu;
0456
0457 if (!dst) {
0458 switch (skb->protocol) {
0459 case htons(ETH_P_IP): {
0460 struct rtable *rt;
0461
0462 fl->u.ip4.flowi4_oif = dev->ifindex;
0463 fl->u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
0464 rt = __ip_route_output_key(dev_net(dev), &fl->u.ip4);
0465 if (IS_ERR(rt))
0466 goto tx_err_link_failure;
0467 dst = &rt->dst;
0468 skb_dst_set(skb, dst);
0469 break;
0470 }
0471 case htons(ETH_P_IPV6):
0472 fl->u.ip6.flowi6_oif = dev->ifindex;
0473 fl->u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
0474 dst = ip6_route_output(dev_net(dev), NULL, &fl->u.ip6);
0475 if (dst->error) {
0476 dst_release(dst);
0477 dst = NULL;
0478 goto tx_err_link_failure;
0479 }
0480 skb_dst_set(skb, dst);
0481 break;
0482 default:
0483 goto tx_err_link_failure;
0484 }
0485 }
0486
0487 dst_hold(dst);
0488 dst = xfrm_lookup_route(t->net, dst, fl, NULL, 0);
0489 if (IS_ERR(dst)) {
0490 err = PTR_ERR(dst);
0491 dst = NULL;
0492 goto tx_err_link_failure;
0493 }
0494
0495 if (dst->flags & DST_XFRM_QUEUE)
0496 goto xmit;
0497
0498 x = dst->xfrm;
0499 if (!vti6_state_check(x, &t->parms.raddr, &t->parms.laddr))
0500 goto tx_err_link_failure;
0501
0502 if (!ip6_tnl_xmit_ctl(t, (const struct in6_addr *)&x->props.saddr,
0503 (const struct in6_addr *)&x->id.daddr))
0504 goto tx_err_link_failure;
0505
0506 tdev = dst->dev;
0507
0508 if (tdev == dev) {
0509 stats->collisions++;
0510 net_warn_ratelimited("%s: Local routing loop detected!\n",
0511 t->parms.name);
0512 goto tx_err_dst_release;
0513 }
0514
0515 mtu = dst_mtu(dst);
0516 if (skb->len > mtu) {
0517 skb_dst_update_pmtu_no_confirm(skb, mtu);
0518
0519 if (skb->protocol == htons(ETH_P_IPV6)) {
0520 if (mtu < IPV6_MIN_MTU)
0521 mtu = IPV6_MIN_MTU;
0522
0523 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
0524 } else {
0525 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)))
0526 goto xmit;
0527 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
0528 htonl(mtu));
0529 }
0530
0531 err = -EMSGSIZE;
0532 goto tx_err_dst_release;
0533 }
0534
0535 xmit:
0536 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
0537 skb_dst_set(skb, dst);
0538 skb->dev = skb_dst(skb)->dev;
0539
0540 err = dst_output(t->net, skb->sk, skb);
0541 if (net_xmit_eval(err) == 0)
0542 err = pkt_len;
0543 iptunnel_xmit_stats(dev, err);
0544
0545 return 0;
0546 tx_err_link_failure:
0547 stats->tx_carrier_errors++;
0548 dst_link_failure(skb);
0549 tx_err_dst_release:
0550 dst_release(dst);
0551 return err;
0552 }
0553
0554 static netdev_tx_t
0555 vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
0556 {
0557 struct ip6_tnl *t = netdev_priv(dev);
0558 struct net_device_stats *stats = &t->dev->stats;
0559 struct flowi fl;
0560 int ret;
0561
0562 if (!pskb_inet_may_pull(skb))
0563 goto tx_err;
0564
0565 memset(&fl, 0, sizeof(fl));
0566
0567 switch (skb->protocol) {
0568 case htons(ETH_P_IPV6):
0569 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
0570 vti6_addr_conflict(t, ipv6_hdr(skb)))
0571 goto tx_err;
0572
0573 xfrm_decode_session(skb, &fl, AF_INET6);
0574 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
0575 break;
0576 case htons(ETH_P_IP):
0577 xfrm_decode_session(skb, &fl, AF_INET);
0578 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
0579 break;
0580 default:
0581 goto tx_err;
0582 }
0583
0584
0585 fl.flowi_mark = be32_to_cpu(t->parms.o_key);
0586
0587 ret = vti6_xmit(skb, dev, &fl);
0588 if (ret < 0)
0589 goto tx_err;
0590
0591 return NETDEV_TX_OK;
0592
0593 tx_err:
0594 stats->tx_errors++;
0595 stats->tx_dropped++;
0596 kfree_skb(skb);
0597 return NETDEV_TX_OK;
0598 }
0599
0600 static int vti6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
0601 u8 type, u8 code, int offset, __be32 info)
0602 {
0603 __be32 spi;
0604 __u32 mark;
0605 struct xfrm_state *x;
0606 struct ip6_tnl *t;
0607 struct ip_esp_hdr *esph;
0608 struct ip_auth_hdr *ah;
0609 struct ip_comp_hdr *ipch;
0610 struct net *net = dev_net(skb->dev);
0611 const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
0612 int protocol = iph->nexthdr;
0613
0614 t = vti6_tnl_lookup(dev_net(skb->dev), &iph->daddr, &iph->saddr);
0615 if (!t)
0616 return -1;
0617
0618 mark = be32_to_cpu(t->parms.o_key);
0619
0620 switch (protocol) {
0621 case IPPROTO_ESP:
0622 esph = (struct ip_esp_hdr *)(skb->data + offset);
0623 spi = esph->spi;
0624 break;
0625 case IPPROTO_AH:
0626 ah = (struct ip_auth_hdr *)(skb->data + offset);
0627 spi = ah->spi;
0628 break;
0629 case IPPROTO_COMP:
0630 ipch = (struct ip_comp_hdr *)(skb->data + offset);
0631 spi = htonl(ntohs(ipch->cpi));
0632 break;
0633 default:
0634 return 0;
0635 }
0636
0637 if (type != ICMPV6_PKT_TOOBIG &&
0638 type != NDISC_REDIRECT)
0639 return 0;
0640
0641 x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
0642 spi, protocol, AF_INET6);
0643 if (!x)
0644 return 0;
0645
0646 if (type == NDISC_REDIRECT)
0647 ip6_redirect(skb, net, skb->dev->ifindex, 0,
0648 sock_net_uid(net, NULL));
0649 else
0650 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
0651 xfrm_state_put(x);
0652
0653 return 0;
0654 }
0655
0656 static void vti6_link_config(struct ip6_tnl *t, bool keep_mtu)
0657 {
0658 struct net_device *dev = t->dev;
0659 struct __ip6_tnl_parm *p = &t->parms;
0660 struct net_device *tdev = NULL;
0661 int mtu;
0662
0663 __dev_addr_set(dev, &p->laddr, sizeof(struct in6_addr));
0664 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
0665
0666 p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
0667 IP6_TNL_F_CAP_PER_PACKET);
0668 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
0669
0670 if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
0671 dev->flags |= IFF_POINTOPOINT;
0672 else
0673 dev->flags &= ~IFF_POINTOPOINT;
0674
0675 if (keep_mtu && dev->mtu) {
0676 dev->mtu = clamp(dev->mtu, dev->min_mtu, dev->max_mtu);
0677 return;
0678 }
0679
0680 if (p->flags & IP6_TNL_F_CAP_XMIT) {
0681 int strict = (ipv6_addr_type(&p->raddr) &
0682 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL));
0683 struct rt6_info *rt = rt6_lookup(t->net,
0684 &p->raddr, &p->laddr,
0685 p->link, NULL, strict);
0686
0687 if (rt)
0688 tdev = rt->dst.dev;
0689 ip6_rt_put(rt);
0690 }
0691
0692 if (!tdev && p->link)
0693 tdev = __dev_get_by_index(t->net, p->link);
0694
0695 if (tdev)
0696 mtu = tdev->mtu - sizeof(struct ipv6hdr);
0697 else
0698 mtu = ETH_DATA_LEN - LL_MAX_HEADER - sizeof(struct ipv6hdr);
0699
0700 dev->mtu = max_t(int, mtu, IPV4_MIN_MTU);
0701 }
0702
0703
0704
0705
0706
0707
0708
0709
0710
0711
0712 static int
0713 vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
0714 bool keep_mtu)
0715 {
0716 t->parms.laddr = p->laddr;
0717 t->parms.raddr = p->raddr;
0718 t->parms.link = p->link;
0719 t->parms.i_key = p->i_key;
0720 t->parms.o_key = p->o_key;
0721 t->parms.proto = p->proto;
0722 t->parms.fwmark = p->fwmark;
0723 dst_cache_reset(&t->dst_cache);
0724 vti6_link_config(t, keep_mtu);
0725 return 0;
0726 }
0727
0728 static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p,
0729 bool keep_mtu)
0730 {
0731 struct net *net = dev_net(t->dev);
0732 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
0733 int err;
0734
0735 vti6_tnl_unlink(ip6n, t);
0736 synchronize_net();
0737 err = vti6_tnl_change(t, p, keep_mtu);
0738 vti6_tnl_link(ip6n, t);
0739 netdev_state_change(t->dev);
0740 return err;
0741 }
0742
0743 static void
0744 vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
0745 {
0746 p->laddr = u->laddr;
0747 p->raddr = u->raddr;
0748 p->link = u->link;
0749 p->i_key = u->i_key;
0750 p->o_key = u->o_key;
0751 p->proto = u->proto;
0752
0753 memcpy(p->name, u->name, sizeof(u->name));
0754 }
0755
0756 static void
0757 vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
0758 {
0759 u->laddr = p->laddr;
0760 u->raddr = p->raddr;
0761 u->link = p->link;
0762 u->i_key = p->i_key;
0763 u->o_key = p->o_key;
0764 if (u->i_key)
0765 u->i_flags |= GRE_KEY;
0766 if (u->o_key)
0767 u->o_flags |= GRE_KEY;
0768 u->proto = p->proto;
0769
0770 memcpy(u->name, p->name, sizeof(u->name));
0771 }
0772
0773
0774
0775
0776
0777
0778
0779
0780
0781
0782
0783
0784
0785
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798
0799
0800
0801 static int
0802 vti6_siocdevprivate(struct net_device *dev, struct ifreq *ifr, void __user *data, int cmd)
0803 {
0804 int err = 0;
0805 struct ip6_tnl_parm2 p;
0806 struct __ip6_tnl_parm p1;
0807 struct ip6_tnl *t = NULL;
0808 struct net *net = dev_net(dev);
0809 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
0810
0811 memset(&p1, 0, sizeof(p1));
0812
0813 switch (cmd) {
0814 case SIOCGETTUNNEL:
0815 if (dev == ip6n->fb_tnl_dev) {
0816 if (copy_from_user(&p, data, sizeof(p))) {
0817 err = -EFAULT;
0818 break;
0819 }
0820 vti6_parm_from_user(&p1, &p);
0821 t = vti6_locate(net, &p1, 0);
0822 } else {
0823 memset(&p, 0, sizeof(p));
0824 }
0825 if (!t)
0826 t = netdev_priv(dev);
0827 vti6_parm_to_user(&p, &t->parms);
0828 if (copy_to_user(data, &p, sizeof(p)))
0829 err = -EFAULT;
0830 break;
0831 case SIOCADDTUNNEL:
0832 case SIOCCHGTUNNEL:
0833 err = -EPERM;
0834 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
0835 break;
0836 err = -EFAULT;
0837 if (copy_from_user(&p, data, sizeof(p)))
0838 break;
0839 err = -EINVAL;
0840 if (p.proto != IPPROTO_IPV6 && p.proto != 0)
0841 break;
0842 vti6_parm_from_user(&p1, &p);
0843 t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
0844 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
0845 if (t) {
0846 if (t->dev != dev) {
0847 err = -EEXIST;
0848 break;
0849 }
0850 } else
0851 t = netdev_priv(dev);
0852
0853 err = vti6_update(t, &p1, false);
0854 }
0855 if (t) {
0856 err = 0;
0857 vti6_parm_to_user(&p, &t->parms);
0858 if (copy_to_user(data, &p, sizeof(p)))
0859 err = -EFAULT;
0860
0861 } else
0862 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
0863 break;
0864 case SIOCDELTUNNEL:
0865 err = -EPERM;
0866 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
0867 break;
0868
0869 if (dev == ip6n->fb_tnl_dev) {
0870 err = -EFAULT;
0871 if (copy_from_user(&p, data, sizeof(p)))
0872 break;
0873 err = -ENOENT;
0874 vti6_parm_from_user(&p1, &p);
0875 t = vti6_locate(net, &p1, 0);
0876 if (!t)
0877 break;
0878 err = -EPERM;
0879 if (t->dev == ip6n->fb_tnl_dev)
0880 break;
0881 dev = t->dev;
0882 }
0883 err = 0;
0884 unregister_netdevice(dev);
0885 break;
0886 default:
0887 err = -EINVAL;
0888 }
0889 return err;
0890 }
0891
0892 static const struct net_device_ops vti6_netdev_ops = {
0893 .ndo_init = vti6_dev_init,
0894 .ndo_uninit = vti6_dev_uninit,
0895 .ndo_start_xmit = vti6_tnl_xmit,
0896 .ndo_siocdevprivate = vti6_siocdevprivate,
0897 .ndo_get_stats64 = dev_get_tstats64,
0898 .ndo_get_iflink = ip6_tnl_get_iflink,
0899 };
0900
0901
0902
0903
0904
0905
0906
0907
0908 static void vti6_dev_setup(struct net_device *dev)
0909 {
0910 dev->netdev_ops = &vti6_netdev_ops;
0911 dev->header_ops = &ip_tunnel_header_ops;
0912 dev->needs_free_netdev = true;
0913 dev->priv_destructor = vti6_dev_free;
0914
0915 dev->type = ARPHRD_TUNNEL6;
0916 dev->min_mtu = IPV4_MIN_MTU;
0917 dev->max_mtu = IP_MAX_MTU - sizeof(struct ipv6hdr);
0918 dev->flags |= IFF_NOARP;
0919 dev->addr_len = sizeof(struct in6_addr);
0920 netif_keep_dst(dev);
0921
0922 dev->addr_assign_type = NET_ADDR_RANDOM;
0923 eth_random_addr(dev->perm_addr);
0924 }
0925
0926
0927
0928
0929
0930 static inline int vti6_dev_init_gen(struct net_device *dev)
0931 {
0932 struct ip6_tnl *t = netdev_priv(dev);
0933
0934 t->dev = dev;
0935 t->net = dev_net(dev);
0936 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
0937 if (!dev->tstats)
0938 return -ENOMEM;
0939 netdev_hold(dev, &t->dev_tracker, GFP_KERNEL);
0940 return 0;
0941 }
0942
0943
0944
0945
0946
0947 static int vti6_dev_init(struct net_device *dev)
0948 {
0949 struct ip6_tnl *t = netdev_priv(dev);
0950 int err = vti6_dev_init_gen(dev);
0951
0952 if (err)
0953 return err;
0954 vti6_link_config(t, true);
0955 return 0;
0956 }
0957
0958
0959
0960
0961
0962
0963
0964 static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
0965 {
0966 struct ip6_tnl *t = netdev_priv(dev);
0967 struct net *net = dev_net(dev);
0968 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
0969
0970 t->parms.proto = IPPROTO_IPV6;
0971
0972 rcu_assign_pointer(ip6n->tnls_wc[0], t);
0973 return 0;
0974 }
0975
0976 static int vti6_validate(struct nlattr *tb[], struct nlattr *data[],
0977 struct netlink_ext_ack *extack)
0978 {
0979 return 0;
0980 }
0981
0982 static void vti6_netlink_parms(struct nlattr *data[],
0983 struct __ip6_tnl_parm *parms)
0984 {
0985 memset(parms, 0, sizeof(*parms));
0986
0987 if (!data)
0988 return;
0989
0990 if (data[IFLA_VTI_LINK])
0991 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
0992
0993 if (data[IFLA_VTI_LOCAL])
0994 parms->laddr = nla_get_in6_addr(data[IFLA_VTI_LOCAL]);
0995
0996 if (data[IFLA_VTI_REMOTE])
0997 parms->raddr = nla_get_in6_addr(data[IFLA_VTI_REMOTE]);
0998
0999 if (data[IFLA_VTI_IKEY])
1000 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
1001
1002 if (data[IFLA_VTI_OKEY])
1003 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
1004
1005 if (data[IFLA_VTI_FWMARK])
1006 parms->fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]);
1007 }
1008
1009 static int vti6_newlink(struct net *src_net, struct net_device *dev,
1010 struct nlattr *tb[], struct nlattr *data[],
1011 struct netlink_ext_ack *extack)
1012 {
1013 struct net *net = dev_net(dev);
1014 struct ip6_tnl *nt;
1015
1016 nt = netdev_priv(dev);
1017 vti6_netlink_parms(data, &nt->parms);
1018
1019 nt->parms.proto = IPPROTO_IPV6;
1020
1021 if (vti6_locate(net, &nt->parms, 0))
1022 return -EEXIST;
1023
1024 return vti6_tnl_create2(dev);
1025 }
1026
1027 static void vti6_dellink(struct net_device *dev, struct list_head *head)
1028 {
1029 struct net *net = dev_net(dev);
1030 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
1031
1032 if (dev != ip6n->fb_tnl_dev)
1033 unregister_netdevice_queue(dev, head);
1034 }
1035
1036 static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
1037 struct nlattr *data[],
1038 struct netlink_ext_ack *extack)
1039 {
1040 struct ip6_tnl *t;
1041 struct __ip6_tnl_parm p;
1042 struct net *net = dev_net(dev);
1043 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
1044
1045 if (dev == ip6n->fb_tnl_dev)
1046 return -EINVAL;
1047
1048 vti6_netlink_parms(data, &p);
1049
1050 t = vti6_locate(net, &p, 0);
1051
1052 if (t) {
1053 if (t->dev != dev)
1054 return -EEXIST;
1055 } else
1056 t = netdev_priv(dev);
1057
1058 return vti6_update(t, &p, tb && tb[IFLA_MTU]);
1059 }
1060
1061 static size_t vti6_get_size(const struct net_device *dev)
1062 {
1063 return
1064
1065 nla_total_size(4) +
1066
1067 nla_total_size(sizeof(struct in6_addr)) +
1068
1069 nla_total_size(sizeof(struct in6_addr)) +
1070
1071 nla_total_size(4) +
1072
1073 nla_total_size(4) +
1074
1075 nla_total_size(4) +
1076 0;
1077 }
1078
1079 static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
1080 {
1081 struct ip6_tnl *tunnel = netdev_priv(dev);
1082 struct __ip6_tnl_parm *parm = &tunnel->parms;
1083
1084 if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
1085 nla_put_in6_addr(skb, IFLA_VTI_LOCAL, &parm->laddr) ||
1086 nla_put_in6_addr(skb, IFLA_VTI_REMOTE, &parm->raddr) ||
1087 nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
1088 nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key) ||
1089 nla_put_u32(skb, IFLA_VTI_FWMARK, parm->fwmark))
1090 goto nla_put_failure;
1091 return 0;
1092
1093 nla_put_failure:
1094 return -EMSGSIZE;
1095 }
1096
1097 static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
1098 [IFLA_VTI_LINK] = { .type = NLA_U32 },
1099 [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
1100 [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
1101 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
1102 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
1103 [IFLA_VTI_FWMARK] = { .type = NLA_U32 },
1104 };
1105
1106 static struct rtnl_link_ops vti6_link_ops __read_mostly = {
1107 .kind = "vti6",
1108 .maxtype = IFLA_VTI_MAX,
1109 .policy = vti6_policy,
1110 .priv_size = sizeof(struct ip6_tnl),
1111 .setup = vti6_dev_setup,
1112 .validate = vti6_validate,
1113 .newlink = vti6_newlink,
1114 .dellink = vti6_dellink,
1115 .changelink = vti6_changelink,
1116 .get_size = vti6_get_size,
1117 .fill_info = vti6_fill_info,
1118 .get_link_net = ip6_tnl_get_link_net,
1119 };
1120
1121 static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n,
1122 struct list_head *list)
1123 {
1124 int h;
1125 struct ip6_tnl *t;
1126
1127 for (h = 0; h < IP6_VTI_HASH_SIZE; h++) {
1128 t = rtnl_dereference(ip6n->tnls_r_l[h]);
1129 while (t) {
1130 unregister_netdevice_queue(t->dev, list);
1131 t = rtnl_dereference(t->next);
1132 }
1133 }
1134
1135 t = rtnl_dereference(ip6n->tnls_wc[0]);
1136 if (t)
1137 unregister_netdevice_queue(t->dev, list);
1138 }
1139
1140 static int __net_init vti6_init_net(struct net *net)
1141 {
1142 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
1143 struct ip6_tnl *t = NULL;
1144 int err;
1145
1146 ip6n->tnls[0] = ip6n->tnls_wc;
1147 ip6n->tnls[1] = ip6n->tnls_r_l;
1148
1149 if (!net_has_fallback_tunnels(net))
1150 return 0;
1151 err = -ENOMEM;
1152 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
1153 NET_NAME_UNKNOWN, vti6_dev_setup);
1154
1155 if (!ip6n->fb_tnl_dev)
1156 goto err_alloc_dev;
1157 dev_net_set(ip6n->fb_tnl_dev, net);
1158 ip6n->fb_tnl_dev->rtnl_link_ops = &vti6_link_ops;
1159
1160 err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1161 if (err < 0)
1162 goto err_register;
1163
1164 err = register_netdev(ip6n->fb_tnl_dev);
1165 if (err < 0)
1166 goto err_register;
1167
1168 t = netdev_priv(ip6n->fb_tnl_dev);
1169
1170 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
1171 return 0;
1172
1173 err_register:
1174 free_netdev(ip6n->fb_tnl_dev);
1175 err_alloc_dev:
1176 return err;
1177 }
1178
1179 static void __net_exit vti6_exit_batch_net(struct list_head *net_list)
1180 {
1181 struct vti6_net *ip6n;
1182 struct net *net;
1183 LIST_HEAD(list);
1184
1185 rtnl_lock();
1186 list_for_each_entry(net, net_list, exit_list) {
1187 ip6n = net_generic(net, vti6_net_id);
1188 vti6_destroy_tunnels(ip6n, &list);
1189 }
1190 unregister_netdevice_many(&list);
1191 rtnl_unlock();
1192 }
1193
1194 static struct pernet_operations vti6_net_ops = {
1195 .init = vti6_init_net,
1196 .exit_batch = vti6_exit_batch_net,
1197 .id = &vti6_net_id,
1198 .size = sizeof(struct vti6_net),
1199 };
1200
1201 static struct xfrm6_protocol vti_esp6_protocol __read_mostly = {
1202 .handler = vti6_rcv,
1203 .input_handler = vti6_input_proto,
1204 .cb_handler = vti6_rcv_cb,
1205 .err_handler = vti6_err,
1206 .priority = 100,
1207 };
1208
1209 static struct xfrm6_protocol vti_ah6_protocol __read_mostly = {
1210 .handler = vti6_rcv,
1211 .input_handler = vti6_input_proto,
1212 .cb_handler = vti6_rcv_cb,
1213 .err_handler = vti6_err,
1214 .priority = 100,
1215 };
1216
1217 static struct xfrm6_protocol vti_ipcomp6_protocol __read_mostly = {
1218 .handler = vti6_rcv,
1219 .input_handler = vti6_input_proto,
1220 .cb_handler = vti6_rcv_cb,
1221 .err_handler = vti6_err,
1222 .priority = 100,
1223 };
1224
1225 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
1226 static int vti6_rcv_tunnel(struct sk_buff *skb)
1227 {
1228 const xfrm_address_t *saddr;
1229 __be32 spi;
1230
1231 saddr = (const xfrm_address_t *)&ipv6_hdr(skb)->saddr;
1232 spi = xfrm6_tunnel_spi_lookup(dev_net(skb->dev), saddr);
1233
1234 return vti6_input_proto(skb, IPPROTO_IPV6, spi, 0);
1235 }
1236
1237 static struct xfrm6_tunnel vti_ipv6_handler __read_mostly = {
1238 .handler = vti6_rcv_tunnel,
1239 .cb_handler = vti6_rcv_cb,
1240 .err_handler = vti6_err,
1241 .priority = 0,
1242 };
1243
1244 static struct xfrm6_tunnel vti_ip6ip_handler __read_mostly = {
1245 .handler = vti6_rcv_tunnel,
1246 .cb_handler = vti6_rcv_cb,
1247 .err_handler = vti6_err,
1248 .priority = 0,
1249 };
1250 #endif
1251
1252
1253
1254
1255
1256
1257 static int __init vti6_tunnel_init(void)
1258 {
1259 const char *msg;
1260 int err;
1261
1262 msg = "tunnel device";
1263 err = register_pernet_device(&vti6_net_ops);
1264 if (err < 0)
1265 goto pernet_dev_failed;
1266
1267 msg = "tunnel protocols";
1268 err = xfrm6_protocol_register(&vti_esp6_protocol, IPPROTO_ESP);
1269 if (err < 0)
1270 goto xfrm_proto_esp_failed;
1271 err = xfrm6_protocol_register(&vti_ah6_protocol, IPPROTO_AH);
1272 if (err < 0)
1273 goto xfrm_proto_ah_failed;
1274 err = xfrm6_protocol_register(&vti_ipcomp6_protocol, IPPROTO_COMP);
1275 if (err < 0)
1276 goto xfrm_proto_comp_failed;
1277 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
1278 msg = "ipv6 tunnel";
1279 err = xfrm6_tunnel_register(&vti_ipv6_handler, AF_INET6);
1280 if (err < 0)
1281 goto vti_tunnel_ipv6_failed;
1282 err = xfrm6_tunnel_register(&vti_ip6ip_handler, AF_INET);
1283 if (err < 0)
1284 goto vti_tunnel_ip6ip_failed;
1285 #endif
1286
1287 msg = "netlink interface";
1288 err = rtnl_link_register(&vti6_link_ops);
1289 if (err < 0)
1290 goto rtnl_link_failed;
1291
1292 return 0;
1293
1294 rtnl_link_failed:
1295 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
1296 err = xfrm6_tunnel_deregister(&vti_ip6ip_handler, AF_INET);
1297 vti_tunnel_ip6ip_failed:
1298 err = xfrm6_tunnel_deregister(&vti_ipv6_handler, AF_INET6);
1299 vti_tunnel_ipv6_failed:
1300 #endif
1301 xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
1302 xfrm_proto_comp_failed:
1303 xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
1304 xfrm_proto_ah_failed:
1305 xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
1306 xfrm_proto_esp_failed:
1307 unregister_pernet_device(&vti6_net_ops);
1308 pernet_dev_failed:
1309 pr_err("vti6 init: failed to register %s\n", msg);
1310 return err;
1311 }
1312
1313
1314
1315
1316 static void __exit vti6_tunnel_cleanup(void)
1317 {
1318 rtnl_link_unregister(&vti6_link_ops);
1319 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
1320 xfrm6_tunnel_deregister(&vti_ip6ip_handler, AF_INET);
1321 xfrm6_tunnel_deregister(&vti_ipv6_handler, AF_INET6);
1322 #endif
1323 xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
1324 xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
1325 xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
1326 unregister_pernet_device(&vti6_net_ops);
1327 }
1328
1329 module_init(vti6_tunnel_init);
1330 module_exit(vti6_tunnel_cleanup);
1331 MODULE_LICENSE("GPL");
1332 MODULE_ALIAS_RTNL_LINK("vti6");
1333 MODULE_ALIAS_NETDEV("ip6_vti0");
1334 MODULE_AUTHOR("Steffen Klassert");
1335 MODULE_DESCRIPTION("IPv6 virtual tunnel interface");