| 1 | /* |
| 2 | * IPv6 tunneling device |
| 3 | * Linux INET6 implementation |
| 4 | * |
| 5 | * Authors: |
| 6 | * Ville Nuorvala <vnuorval@tcs.hut.fi> |
| 7 | * Yasuyuki Kozakai <kozakai@linux-ipv6.org> |
| 8 | * |
| 9 | * Based on: |
| 10 | * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c |
| 11 | * |
| 12 | * RFC 2473 |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License |
| 16 | * as published by the Free Software Foundation; either version |
| 17 | * 2 of the License, or (at your option) any later version. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/capability.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/sockios.h> |
| 28 | #include <linux/icmp.h> |
| 29 | #include <linux/if.h> |
| 30 | #include <linux/in.h> |
| 31 | #include <linux/ip.h> |
| 32 | #include <linux/if_tunnel.h> |
| 33 | #include <linux/net.h> |
| 34 | #include <linux/in6.h> |
| 35 | #include <linux/netdevice.h> |
| 36 | #include <linux/if_arp.h> |
| 37 | #include <linux/icmpv6.h> |
| 38 | #include <linux/init.h> |
| 39 | #include <linux/route.h> |
| 40 | #include <linux/rtnetlink.h> |
| 41 | #include <linux/netfilter_ipv6.h> |
| 42 | #include <linux/slab.h> |
| 43 | #include <linux/hash.h> |
| 44 | |
| 45 | #include <asm/uaccess.h> |
| 46 | #include <linux/atomic.h> |
| 47 | |
| 48 | #include <net/icmp.h> |
| 49 | #include <net/ip.h> |
| 50 | #include <net/ip_tunnels.h> |
| 51 | #include <net/ipv6.h> |
| 52 | #include <net/ip6_route.h> |
| 53 | #include <net/addrconf.h> |
| 54 | #include <net/ip6_tunnel.h> |
| 55 | #include <net/xfrm.h> |
| 56 | #include <net/dsfield.h> |
| 57 | #include <net/inet_ecn.h> |
| 58 | #include <net/net_namespace.h> |
| 59 | #include <net/netns/generic.h> |
| 60 | |
| 61 | MODULE_AUTHOR("Ville Nuorvala"); |
| 62 | MODULE_DESCRIPTION("IPv6 tunneling device"); |
| 63 | MODULE_LICENSE("GPL"); |
| 64 | MODULE_ALIAS_RTNL_LINK("ip6tnl"); |
| 65 | MODULE_ALIAS_NETDEV("ip6tnl0"); |
| 66 | |
| 67 | #ifdef IP6_TNL_DEBUG |
| 68 | #define IP6_TNL_TRACE(x...) pr_debug("%s:" x "\n", __func__) |
| 69 | #else |
| 70 | #define IP6_TNL_TRACE(x...) do {;} while(0) |
| 71 | #endif |
| 72 | |
| 73 | #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK) |
| 74 | #define IPV6_TCLASS_SHIFT 20 |
| 75 | |
| 76 | #define HASH_SIZE_SHIFT 5 |
| 77 | #define HASH_SIZE (1 << HASH_SIZE_SHIFT) |
| 78 | |
| 79 | static bool log_ecn_error = true; |
| 80 | module_param(log_ecn_error, bool, 0644); |
| 81 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); |
| 82 | |
| 83 | static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2) |
| 84 | { |
| 85 | u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2); |
| 86 | |
| 87 | return hash_32(hash, HASH_SIZE_SHIFT); |
| 88 | } |
| 89 | |
| 90 | static int ip6_tnl_dev_init(struct net_device *dev); |
| 91 | static void ip6_tnl_dev_setup(struct net_device *dev); |
| 92 | static struct rtnl_link_ops ip6_link_ops __read_mostly; |
| 93 | |
| 94 | static int ip6_tnl_net_id __read_mostly; |
| 95 | struct ip6_tnl_net { |
| 96 | /* the IPv6 tunnel fallback device */ |
| 97 | struct net_device *fb_tnl_dev; |
| 98 | /* lists for storing tunnels in use */ |
| 99 | struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE]; |
| 100 | struct ip6_tnl __rcu *tnls_wc[1]; |
| 101 | struct ip6_tnl __rcu **tnls[2]; |
| 102 | }; |
| 103 | |
| 104 | static struct net_device_stats *ip6_get_stats(struct net_device *dev) |
| 105 | { |
| 106 | struct pcpu_tstats tmp, sum = { 0 }; |
| 107 | int i; |
| 108 | |
| 109 | for_each_possible_cpu(i) { |
| 110 | unsigned int start; |
| 111 | const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i); |
| 112 | |
| 113 | do { |
| 114 | start = u64_stats_fetch_begin_bh(&tstats->syncp); |
| 115 | tmp.rx_packets = tstats->rx_packets; |
| 116 | tmp.rx_bytes = tstats->rx_bytes; |
| 117 | tmp.tx_packets = tstats->tx_packets; |
| 118 | tmp.tx_bytes = tstats->tx_bytes; |
| 119 | } while (u64_stats_fetch_retry_bh(&tstats->syncp, start)); |
| 120 | |
| 121 | sum.rx_packets += tmp.rx_packets; |
| 122 | sum.rx_bytes += tmp.rx_bytes; |
| 123 | sum.tx_packets += tmp.tx_packets; |
| 124 | sum.tx_bytes += tmp.tx_bytes; |
| 125 | } |
| 126 | dev->stats.rx_packets = sum.rx_packets; |
| 127 | dev->stats.rx_bytes = sum.rx_bytes; |
| 128 | dev->stats.tx_packets = sum.tx_packets; |
| 129 | dev->stats.tx_bytes = sum.tx_bytes; |
| 130 | return &dev->stats; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Locking : hash tables are protected by RCU and RTNL |
| 135 | */ |
| 136 | |
| 137 | struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t) |
| 138 | { |
| 139 | struct dst_entry *dst = t->dst_cache; |
| 140 | |
| 141 | if (dst && dst->obsolete && |
| 142 | dst->ops->check(dst, t->dst_cookie) == NULL) { |
| 143 | t->dst_cache = NULL; |
| 144 | dst_release(dst); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | return dst; |
| 149 | } |
| 150 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_check); |
| 151 | |
| 152 | void ip6_tnl_dst_reset(struct ip6_tnl *t) |
| 153 | { |
| 154 | dst_release(t->dst_cache); |
| 155 | t->dst_cache = NULL; |
| 156 | } |
| 157 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset); |
| 158 | |
| 159 | void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst) |
| 160 | { |
| 161 | struct rt6_info *rt = (struct rt6_info *) dst; |
| 162 | t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0; |
| 163 | dst_release(t->dst_cache); |
| 164 | t->dst_cache = dst; |
| 165 | } |
| 166 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_store); |
| 167 | |
| 168 | /** |
| 169 | * ip6_tnl_lookup - fetch tunnel matching the end-point addresses |
| 170 | * @remote: the address of the tunnel exit-point |
| 171 | * @local: the address of the tunnel entry-point |
| 172 | * |
| 173 | * Return: |
| 174 | * tunnel matching given end-points if found, |
| 175 | * else fallback tunnel if its device is up, |
| 176 | * else %NULL |
| 177 | **/ |
| 178 | |
| 179 | #define for_each_ip6_tunnel_rcu(start) \ |
| 180 | for (t = rcu_dereference(start); t; t = rcu_dereference(t->next)) |
| 181 | |
| 182 | static struct ip6_tnl * |
| 183 | ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local) |
| 184 | { |
| 185 | unsigned int hash = HASH(remote, local); |
| 186 | struct ip6_tnl *t; |
| 187 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 188 | |
| 189 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { |
| 190 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
| 191 | ipv6_addr_equal(remote, &t->parms.raddr) && |
| 192 | (t->dev->flags & IFF_UP)) |
| 193 | return t; |
| 194 | } |
| 195 | t = rcu_dereference(ip6n->tnls_wc[0]); |
| 196 | if (t && (t->dev->flags & IFF_UP)) |
| 197 | return t; |
| 198 | |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * ip6_tnl_bucket - get head of list matching given tunnel parameters |
| 204 | * @p: parameters containing tunnel end-points |
| 205 | * |
| 206 | * Description: |
| 207 | * ip6_tnl_bucket() returns the head of the list matching the |
| 208 | * &struct in6_addr entries laddr and raddr in @p. |
| 209 | * |
| 210 | * Return: head of IPv6 tunnel list |
| 211 | **/ |
| 212 | |
| 213 | static struct ip6_tnl __rcu ** |
| 214 | ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p) |
| 215 | { |
| 216 | const struct in6_addr *remote = &p->raddr; |
| 217 | const struct in6_addr *local = &p->laddr; |
| 218 | unsigned int h = 0; |
| 219 | int prio = 0; |
| 220 | |
| 221 | if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) { |
| 222 | prio = 1; |
| 223 | h = HASH(remote, local); |
| 224 | } |
| 225 | return &ip6n->tnls[prio][h]; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * ip6_tnl_link - add tunnel to hash table |
| 230 | * @t: tunnel to be added |
| 231 | **/ |
| 232 | |
| 233 | static void |
| 234 | ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t) |
| 235 | { |
| 236 | struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms); |
| 237 | |
| 238 | rcu_assign_pointer(t->next , rtnl_dereference(*tp)); |
| 239 | rcu_assign_pointer(*tp, t); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * ip6_tnl_unlink - remove tunnel from hash table |
| 244 | * @t: tunnel to be removed |
| 245 | **/ |
| 246 | |
| 247 | static void |
| 248 | ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t) |
| 249 | { |
| 250 | struct ip6_tnl __rcu **tp; |
| 251 | struct ip6_tnl *iter; |
| 252 | |
| 253 | for (tp = ip6_tnl_bucket(ip6n, &t->parms); |
| 254 | (iter = rtnl_dereference(*tp)) != NULL; |
| 255 | tp = &iter->next) { |
| 256 | if (t == iter) { |
| 257 | rcu_assign_pointer(*tp, t->next); |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | static void ip6_dev_free(struct net_device *dev) |
| 264 | { |
| 265 | free_percpu(dev->tstats); |
| 266 | free_netdev(dev); |
| 267 | } |
| 268 | |
| 269 | static int ip6_tnl_create2(struct net_device *dev) |
| 270 | { |
| 271 | struct ip6_tnl *t = netdev_priv(dev); |
| 272 | struct net *net = dev_net(dev); |
| 273 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 274 | int err; |
| 275 | |
| 276 | t = netdev_priv(dev); |
| 277 | |
| 278 | dev->rtnl_link_ops = &ip6_link_ops; |
| 279 | err = register_netdevice(dev); |
| 280 | if (err < 0) |
| 281 | goto out; |
| 282 | |
| 283 | strcpy(t->parms.name, dev->name); |
| 284 | |
| 285 | dev_hold(dev); |
| 286 | ip6_tnl_link(ip6n, t); |
| 287 | return 0; |
| 288 | |
| 289 | out: |
| 290 | return err; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * ip6_tnl_create - create a new tunnel |
| 295 | * @p: tunnel parameters |
| 296 | * @pt: pointer to new tunnel |
| 297 | * |
| 298 | * Description: |
| 299 | * Create tunnel matching given parameters. |
| 300 | * |
| 301 | * Return: |
| 302 | * created tunnel or NULL |
| 303 | **/ |
| 304 | |
| 305 | static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p) |
| 306 | { |
| 307 | struct net_device *dev; |
| 308 | struct ip6_tnl *t; |
| 309 | char name[IFNAMSIZ]; |
| 310 | int err; |
| 311 | |
| 312 | if (p->name[0]) |
| 313 | strlcpy(name, p->name, IFNAMSIZ); |
| 314 | else |
| 315 | sprintf(name, "ip6tnl%%d"); |
| 316 | |
| 317 | dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup); |
| 318 | if (dev == NULL) |
| 319 | goto failed; |
| 320 | |
| 321 | dev_net_set(dev, net); |
| 322 | |
| 323 | t = netdev_priv(dev); |
| 324 | t->parms = *p; |
| 325 | err = ip6_tnl_create2(dev); |
| 326 | if (err < 0) |
| 327 | goto failed_free; |
| 328 | |
| 329 | return t; |
| 330 | |
| 331 | failed_free: |
| 332 | ip6_dev_free(dev); |
| 333 | failed: |
| 334 | return NULL; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * ip6_tnl_locate - find or create tunnel matching given parameters |
| 339 | * @p: tunnel parameters |
| 340 | * @create: != 0 if allowed to create new tunnel if no match found |
| 341 | * |
| 342 | * Description: |
| 343 | * ip6_tnl_locate() first tries to locate an existing tunnel |
| 344 | * based on @parms. If this is unsuccessful, but @create is set a new |
| 345 | * tunnel device is created and registered for use. |
| 346 | * |
| 347 | * Return: |
| 348 | * matching tunnel or NULL |
| 349 | **/ |
| 350 | |
| 351 | static struct ip6_tnl *ip6_tnl_locate(struct net *net, |
| 352 | struct __ip6_tnl_parm *p, int create) |
| 353 | { |
| 354 | const struct in6_addr *remote = &p->raddr; |
| 355 | const struct in6_addr *local = &p->laddr; |
| 356 | struct ip6_tnl __rcu **tp; |
| 357 | struct ip6_tnl *t; |
| 358 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 359 | |
| 360 | for (tp = ip6_tnl_bucket(ip6n, p); |
| 361 | (t = rtnl_dereference(*tp)) != NULL; |
| 362 | tp = &t->next) { |
| 363 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
| 364 | ipv6_addr_equal(remote, &t->parms.raddr)) |
| 365 | return t; |
| 366 | } |
| 367 | if (!create) |
| 368 | return NULL; |
| 369 | return ip6_tnl_create(net, p); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * ip6_tnl_dev_uninit - tunnel device uninitializer |
| 374 | * @dev: the device to be destroyed |
| 375 | * |
| 376 | * Description: |
| 377 | * ip6_tnl_dev_uninit() removes tunnel from its list |
| 378 | **/ |
| 379 | |
| 380 | static void |
| 381 | ip6_tnl_dev_uninit(struct net_device *dev) |
| 382 | { |
| 383 | struct ip6_tnl *t = netdev_priv(dev); |
| 384 | struct net *net = dev_net(dev); |
| 385 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 386 | |
| 387 | if (dev == ip6n->fb_tnl_dev) |
| 388 | RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL); |
| 389 | else |
| 390 | ip6_tnl_unlink(ip6n, t); |
| 391 | ip6_tnl_dst_reset(t); |
| 392 | dev_put(dev); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * parse_tvl_tnl_enc_lim - handle encapsulation limit option |
| 397 | * @skb: received socket buffer |
| 398 | * |
| 399 | * Return: |
| 400 | * 0 if none was found, |
| 401 | * else index to encapsulation limit |
| 402 | **/ |
| 403 | |
| 404 | __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw) |
| 405 | { |
| 406 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw; |
| 407 | unsigned int nhoff = raw - skb->data; |
| 408 | unsigned int off = nhoff + sizeof(*ipv6h); |
| 409 | u8 next, nexthdr = ipv6h->nexthdr; |
| 410 | |
| 411 | while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) { |
| 412 | struct ipv6_opt_hdr *hdr; |
| 413 | u16 optlen; |
| 414 | |
| 415 | if (!pskb_may_pull(skb, off + sizeof(*hdr))) |
| 416 | break; |
| 417 | |
| 418 | hdr = (struct ipv6_opt_hdr *)(skb->data + off); |
| 419 | if (nexthdr == NEXTHDR_FRAGMENT) { |
| 420 | struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr; |
| 421 | if (frag_hdr->frag_off) |
| 422 | break; |
| 423 | optlen = 8; |
| 424 | } else if (nexthdr == NEXTHDR_AUTH) { |
| 425 | optlen = (hdr->hdrlen + 2) << 2; |
| 426 | } else { |
| 427 | optlen = ipv6_optlen(hdr); |
| 428 | } |
| 429 | /* cache hdr->nexthdr, since pskb_may_pull() might |
| 430 | * invalidate hdr |
| 431 | */ |
| 432 | next = hdr->nexthdr; |
| 433 | if (nexthdr == NEXTHDR_DEST) { |
| 434 | u16 i = 2; |
| 435 | |
| 436 | /* Remember : hdr is no longer valid at this point. */ |
| 437 | if (!pskb_may_pull(skb, off + optlen)) |
| 438 | break; |
| 439 | |
| 440 | while (1) { |
| 441 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 442 | |
| 443 | /* No more room for encapsulation limit */ |
| 444 | if (i + sizeof(*tel) > optlen) |
| 445 | break; |
| 446 | |
| 447 | tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i); |
| 448 | /* return index of option if found and valid */ |
| 449 | if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT && |
| 450 | tel->length == 1) |
| 451 | return i + off - nhoff; |
| 452 | /* else jump to next option */ |
| 453 | if (tel->type) |
| 454 | i += tel->length + 2; |
| 455 | else |
| 456 | i++; |
| 457 | } |
| 458 | } |
| 459 | nexthdr = next; |
| 460 | off += optlen; |
| 461 | } |
| 462 | return 0; |
| 463 | } |
| 464 | EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim); |
| 465 | |
| 466 | /** |
| 467 | * ip6_tnl_err - tunnel error handler |
| 468 | * |
| 469 | * Description: |
| 470 | * ip6_tnl_err() should handle errors in the tunnel according |
| 471 | * to the specifications in RFC 2473. |
| 472 | **/ |
| 473 | |
| 474 | static int |
| 475 | ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt, |
| 476 | u8 *type, u8 *code, int *msg, __u32 *info, int offset) |
| 477 | { |
| 478 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data; |
| 479 | struct ip6_tnl *t; |
| 480 | int rel_msg = 0; |
| 481 | u8 rel_type = ICMPV6_DEST_UNREACH; |
| 482 | u8 rel_code = ICMPV6_ADDR_UNREACH; |
| 483 | __u32 rel_info = 0; |
| 484 | __u16 len; |
| 485 | int err = -ENOENT; |
| 486 | |
| 487 | /* If the packet doesn't contain the original IPv6 header we are |
| 488 | in trouble since we might need the source address for further |
| 489 | processing of the error. */ |
| 490 | |
| 491 | rcu_read_lock(); |
| 492 | if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, |
| 493 | &ipv6h->saddr)) == NULL) |
| 494 | goto out; |
| 495 | |
| 496 | if (t->parms.proto != ipproto && t->parms.proto != 0) |
| 497 | goto out; |
| 498 | |
| 499 | err = 0; |
| 500 | |
| 501 | switch (*type) { |
| 502 | __u32 teli; |
| 503 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 504 | __u32 mtu; |
| 505 | case ICMPV6_DEST_UNREACH: |
| 506 | net_warn_ratelimited("%s: Path to destination invalid or inactive!\n", |
| 507 | t->parms.name); |
| 508 | rel_msg = 1; |
| 509 | break; |
| 510 | case ICMPV6_TIME_EXCEED: |
| 511 | if ((*code) == ICMPV6_EXC_HOPLIMIT) { |
| 512 | net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n", |
| 513 | t->parms.name); |
| 514 | rel_msg = 1; |
| 515 | } |
| 516 | break; |
| 517 | case ICMPV6_PARAMPROB: |
| 518 | teli = 0; |
| 519 | if ((*code) == ICMPV6_HDR_FIELD) |
| 520 | teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); |
| 521 | |
| 522 | if (teli && teli == *info - 2) { |
| 523 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; |
| 524 | if (tel->encap_limit == 0) { |
| 525 | net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n", |
| 526 | t->parms.name); |
| 527 | rel_msg = 1; |
| 528 | } |
| 529 | } else { |
| 530 | net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n", |
| 531 | t->parms.name); |
| 532 | } |
| 533 | break; |
| 534 | case ICMPV6_PKT_TOOBIG: |
| 535 | mtu = *info - offset; |
| 536 | if (mtu < IPV6_MIN_MTU) |
| 537 | mtu = IPV6_MIN_MTU; |
| 538 | t->dev->mtu = mtu; |
| 539 | |
| 540 | if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) { |
| 541 | rel_type = ICMPV6_PKT_TOOBIG; |
| 542 | rel_code = 0; |
| 543 | rel_info = mtu; |
| 544 | rel_msg = 1; |
| 545 | } |
| 546 | break; |
| 547 | } |
| 548 | |
| 549 | *type = rel_type; |
| 550 | *code = rel_code; |
| 551 | *info = rel_info; |
| 552 | *msg = rel_msg; |
| 553 | |
| 554 | out: |
| 555 | rcu_read_unlock(); |
| 556 | return err; |
| 557 | } |
| 558 | |
| 559 | static int |
| 560 | ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
| 561 | u8 type, u8 code, int offset, __be32 info) |
| 562 | { |
| 563 | int rel_msg = 0; |
| 564 | u8 rel_type = type; |
| 565 | u8 rel_code = code; |
| 566 | __u32 rel_info = ntohl(info); |
| 567 | int err; |
| 568 | struct sk_buff *skb2; |
| 569 | const struct iphdr *eiph; |
| 570 | struct rtable *rt; |
| 571 | struct flowi4 fl4; |
| 572 | |
| 573 | err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code, |
| 574 | &rel_msg, &rel_info, offset); |
| 575 | if (err < 0) |
| 576 | return err; |
| 577 | |
| 578 | if (rel_msg == 0) |
| 579 | return 0; |
| 580 | |
| 581 | switch (rel_type) { |
| 582 | case ICMPV6_DEST_UNREACH: |
| 583 | if (rel_code != ICMPV6_ADDR_UNREACH) |
| 584 | return 0; |
| 585 | rel_type = ICMP_DEST_UNREACH; |
| 586 | rel_code = ICMP_HOST_UNREACH; |
| 587 | break; |
| 588 | case ICMPV6_PKT_TOOBIG: |
| 589 | if (rel_code != 0) |
| 590 | return 0; |
| 591 | rel_type = ICMP_DEST_UNREACH; |
| 592 | rel_code = ICMP_FRAG_NEEDED; |
| 593 | break; |
| 594 | case NDISC_REDIRECT: |
| 595 | rel_type = ICMP_REDIRECT; |
| 596 | rel_code = ICMP_REDIR_HOST; |
| 597 | default: |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | if (!pskb_may_pull(skb, offset + sizeof(struct iphdr))) |
| 602 | return 0; |
| 603 | |
| 604 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 605 | if (!skb2) |
| 606 | return 0; |
| 607 | |
| 608 | skb_dst_drop(skb2); |
| 609 | |
| 610 | skb_pull(skb2, offset); |
| 611 | skb_reset_network_header(skb2); |
| 612 | eiph = ip_hdr(skb2); |
| 613 | |
| 614 | /* Try to guess incoming interface */ |
| 615 | rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, |
| 616 | eiph->saddr, 0, |
| 617 | 0, 0, |
| 618 | IPPROTO_IPIP, RT_TOS(eiph->tos), 0); |
| 619 | if (IS_ERR(rt)) |
| 620 | goto out; |
| 621 | |
| 622 | skb2->dev = rt->dst.dev; |
| 623 | |
| 624 | /* route "incoming" packet */ |
| 625 | if (rt->rt_flags & RTCF_LOCAL) { |
| 626 | ip_rt_put(rt); |
| 627 | rt = NULL; |
| 628 | rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, |
| 629 | eiph->daddr, eiph->saddr, |
| 630 | 0, 0, |
| 631 | IPPROTO_IPIP, |
| 632 | RT_TOS(eiph->tos), 0); |
| 633 | if (IS_ERR(rt) || |
| 634 | rt->dst.dev->type != ARPHRD_TUNNEL) { |
| 635 | if (!IS_ERR(rt)) |
| 636 | ip_rt_put(rt); |
| 637 | goto out; |
| 638 | } |
| 639 | skb_dst_set(skb2, &rt->dst); |
| 640 | } else { |
| 641 | ip_rt_put(rt); |
| 642 | if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, |
| 643 | skb2->dev) || |
| 644 | skb_dst(skb2)->dev->type != ARPHRD_TUNNEL) |
| 645 | goto out; |
| 646 | } |
| 647 | |
| 648 | /* change mtu on this route */ |
| 649 | if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) { |
| 650 | if (rel_info > dst_mtu(skb_dst(skb2))) |
| 651 | goto out; |
| 652 | |
| 653 | skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info); |
| 654 | } |
| 655 | if (rel_type == ICMP_REDIRECT) |
| 656 | skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2); |
| 657 | |
| 658 | icmp_send(skb2, rel_type, rel_code, htonl(rel_info)); |
| 659 | |
| 660 | out: |
| 661 | kfree_skb(skb2); |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static int |
| 666 | ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
| 667 | u8 type, u8 code, int offset, __be32 info) |
| 668 | { |
| 669 | int rel_msg = 0; |
| 670 | u8 rel_type = type; |
| 671 | u8 rel_code = code; |
| 672 | __u32 rel_info = ntohl(info); |
| 673 | int err; |
| 674 | |
| 675 | err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code, |
| 676 | &rel_msg, &rel_info, offset); |
| 677 | if (err < 0) |
| 678 | return err; |
| 679 | |
| 680 | if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) { |
| 681 | struct rt6_info *rt; |
| 682 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); |
| 683 | |
| 684 | if (!skb2) |
| 685 | return 0; |
| 686 | |
| 687 | skb_dst_drop(skb2); |
| 688 | skb_pull(skb2, offset); |
| 689 | skb_reset_network_header(skb2); |
| 690 | |
| 691 | /* Try to guess incoming interface */ |
| 692 | rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, |
| 693 | NULL, 0, 0); |
| 694 | |
| 695 | if (rt && rt->dst.dev) |
| 696 | skb2->dev = rt->dst.dev; |
| 697 | |
| 698 | icmpv6_send(skb2, rel_type, rel_code, rel_info); |
| 699 | |
| 700 | ip6_rt_put(rt); |
| 701 | |
| 702 | kfree_skb(skb2); |
| 703 | } |
| 704 | |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, |
| 709 | const struct ipv6hdr *ipv6h, |
| 710 | struct sk_buff *skb) |
| 711 | { |
| 712 | __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK; |
| 713 | |
| 714 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) |
| 715 | ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield); |
| 716 | |
| 717 | return IP6_ECN_decapsulate(ipv6h, skb); |
| 718 | } |
| 719 | |
| 720 | static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, |
| 721 | const struct ipv6hdr *ipv6h, |
| 722 | struct sk_buff *skb) |
| 723 | { |
| 724 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) |
| 725 | ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb)); |
| 726 | |
| 727 | return IP6_ECN_decapsulate(ipv6h, skb); |
| 728 | } |
| 729 | |
| 730 | __u32 ip6_tnl_get_cap(struct ip6_tnl *t, |
| 731 | const struct in6_addr *laddr, |
| 732 | const struct in6_addr *raddr) |
| 733 | { |
| 734 | struct __ip6_tnl_parm *p = &t->parms; |
| 735 | int ltype = ipv6_addr_type(laddr); |
| 736 | int rtype = ipv6_addr_type(raddr); |
| 737 | __u32 flags = 0; |
| 738 | |
| 739 | if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) { |
| 740 | flags = IP6_TNL_F_CAP_PER_PACKET; |
| 741 | } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && |
| 742 | rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && |
| 743 | !((ltype|rtype) & IPV6_ADDR_LOOPBACK) && |
| 744 | (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) { |
| 745 | if (ltype&IPV6_ADDR_UNICAST) |
| 746 | flags |= IP6_TNL_F_CAP_XMIT; |
| 747 | if (rtype&IPV6_ADDR_UNICAST) |
| 748 | flags |= IP6_TNL_F_CAP_RCV; |
| 749 | } |
| 750 | return flags; |
| 751 | } |
| 752 | EXPORT_SYMBOL(ip6_tnl_get_cap); |
| 753 | |
| 754 | /* called with rcu_read_lock() */ |
| 755 | int ip6_tnl_rcv_ctl(struct ip6_tnl *t, |
| 756 | const struct in6_addr *laddr, |
| 757 | const struct in6_addr *raddr) |
| 758 | { |
| 759 | struct __ip6_tnl_parm *p = &t->parms; |
| 760 | int ret = 0; |
| 761 | struct net *net = dev_net(t->dev); |
| 762 | |
| 763 | if ((p->flags & IP6_TNL_F_CAP_RCV) || |
| 764 | ((p->flags & IP6_TNL_F_CAP_PER_PACKET) && |
| 765 | (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) { |
| 766 | struct net_device *ldev = NULL; |
| 767 | |
| 768 | if (p->link) |
| 769 | ldev = dev_get_by_index_rcu(net, p->link); |
| 770 | |
| 771 | if ((ipv6_addr_is_multicast(laddr) || |
| 772 | likely(ipv6_chk_addr(net, laddr, ldev, 0))) && |
| 773 | likely(!ipv6_chk_addr(net, raddr, NULL, 0))) |
| 774 | ret = 1; |
| 775 | } |
| 776 | return ret; |
| 777 | } |
| 778 | EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl); |
| 779 | |
| 780 | /** |
| 781 | * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally |
| 782 | * @skb: received socket buffer |
| 783 | * @protocol: ethernet protocol ID |
| 784 | * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN |
| 785 | * |
| 786 | * Return: 0 |
| 787 | **/ |
| 788 | |
| 789 | static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol, |
| 790 | __u8 ipproto, |
| 791 | int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t, |
| 792 | const struct ipv6hdr *ipv6h, |
| 793 | struct sk_buff *skb)) |
| 794 | { |
| 795 | struct ip6_tnl *t; |
| 796 | const struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
| 797 | int err; |
| 798 | |
| 799 | rcu_read_lock(); |
| 800 | |
| 801 | if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, |
| 802 | &ipv6h->daddr)) != NULL) { |
| 803 | struct pcpu_tstats *tstats; |
| 804 | |
| 805 | if (t->parms.proto != ipproto && t->parms.proto != 0) { |
| 806 | rcu_read_unlock(); |
| 807 | goto discard; |
| 808 | } |
| 809 | |
| 810 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { |
| 811 | rcu_read_unlock(); |
| 812 | goto discard; |
| 813 | } |
| 814 | |
| 815 | if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) { |
| 816 | t->dev->stats.rx_dropped++; |
| 817 | rcu_read_unlock(); |
| 818 | goto discard; |
| 819 | } |
| 820 | secpath_reset(skb); |
| 821 | skb->mac_header = skb->network_header; |
| 822 | skb_reset_network_header(skb); |
| 823 | skb->protocol = htons(protocol); |
| 824 | skb->pkt_type = PACKET_HOST; |
| 825 | memset(skb->cb, 0, sizeof(struct inet6_skb_parm)); |
| 826 | |
| 827 | __skb_tunnel_rx(skb, t->dev); |
| 828 | |
| 829 | err = dscp_ecn_decapsulate(t, ipv6h, skb); |
| 830 | if (unlikely(err)) { |
| 831 | if (log_ecn_error) |
| 832 | net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n", |
| 833 | &ipv6h->saddr, |
| 834 | ipv6_get_dsfield(ipv6h)); |
| 835 | if (err > 1) { |
| 836 | ++t->dev->stats.rx_frame_errors; |
| 837 | ++t->dev->stats.rx_errors; |
| 838 | rcu_read_unlock(); |
| 839 | goto discard; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | tstats = this_cpu_ptr(t->dev->tstats); |
| 844 | u64_stats_update_begin(&tstats->syncp); |
| 845 | tstats->rx_packets++; |
| 846 | tstats->rx_bytes += skb->len; |
| 847 | u64_stats_update_end(&tstats->syncp); |
| 848 | |
| 849 | netif_rx(skb); |
| 850 | |
| 851 | rcu_read_unlock(); |
| 852 | return 0; |
| 853 | } |
| 854 | rcu_read_unlock(); |
| 855 | return 1; |
| 856 | |
| 857 | discard: |
| 858 | kfree_skb(skb); |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static int ip4ip6_rcv(struct sk_buff *skb) |
| 863 | { |
| 864 | return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP, |
| 865 | ip4ip6_dscp_ecn_decapsulate); |
| 866 | } |
| 867 | |
| 868 | static int ip6ip6_rcv(struct sk_buff *skb) |
| 869 | { |
| 870 | return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6, |
| 871 | ip6ip6_dscp_ecn_decapsulate); |
| 872 | } |
| 873 | |
| 874 | struct ipv6_tel_txoption { |
| 875 | struct ipv6_txoptions ops; |
| 876 | __u8 dst_opt[8]; |
| 877 | }; |
| 878 | |
| 879 | static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit) |
| 880 | { |
| 881 | memset(opt, 0, sizeof(struct ipv6_tel_txoption)); |
| 882 | |
| 883 | opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT; |
| 884 | opt->dst_opt[3] = 1; |
| 885 | opt->dst_opt[4] = encap_limit; |
| 886 | opt->dst_opt[5] = IPV6_TLV_PADN; |
| 887 | opt->dst_opt[6] = 1; |
| 888 | |
| 889 | opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt; |
| 890 | opt->ops.opt_nflen = 8; |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own |
| 895 | * @t: the outgoing tunnel device |
| 896 | * @hdr: IPv6 header from the incoming packet |
| 897 | * |
| 898 | * Description: |
| 899 | * Avoid trivial tunneling loop by checking that tunnel exit-point |
| 900 | * doesn't match source of incoming packet. |
| 901 | * |
| 902 | * Return: |
| 903 | * 1 if conflict, |
| 904 | * 0 else |
| 905 | **/ |
| 906 | |
| 907 | static inline bool |
| 908 | ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr) |
| 909 | { |
| 910 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); |
| 911 | } |
| 912 | |
| 913 | int ip6_tnl_xmit_ctl(struct ip6_tnl *t) |
| 914 | { |
| 915 | struct __ip6_tnl_parm *p = &t->parms; |
| 916 | int ret = 0; |
| 917 | struct net *net = dev_net(t->dev); |
| 918 | |
| 919 | if (p->flags & IP6_TNL_F_CAP_XMIT) { |
| 920 | struct net_device *ldev = NULL; |
| 921 | |
| 922 | rcu_read_lock(); |
| 923 | if (p->link) |
| 924 | ldev = dev_get_by_index_rcu(net, p->link); |
| 925 | |
| 926 | if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0))) |
| 927 | pr_warn("%s xmit: Local address not yet configured!\n", |
| 928 | p->name); |
| 929 | else if (!ipv6_addr_is_multicast(&p->raddr) && |
| 930 | unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0))) |
| 931 | pr_warn("%s xmit: Routing loop! Remote address found on this node!\n", |
| 932 | p->name); |
| 933 | else |
| 934 | ret = 1; |
| 935 | rcu_read_unlock(); |
| 936 | } |
| 937 | return ret; |
| 938 | } |
| 939 | EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl); |
| 940 | |
| 941 | /** |
| 942 | * ip6_tnl_xmit2 - encapsulate packet and send |
| 943 | * @skb: the outgoing socket buffer |
| 944 | * @dev: the outgoing tunnel device |
| 945 | * @dsfield: dscp code for outer header |
| 946 | * @fl: flow of tunneled packet |
| 947 | * @encap_limit: encapsulation limit |
| 948 | * @pmtu: Path MTU is stored if packet is too big |
| 949 | * |
| 950 | * Description: |
| 951 | * Build new header and do some sanity checks on the packet before sending |
| 952 | * it. |
| 953 | * |
| 954 | * Return: |
| 955 | * 0 on success |
| 956 | * -1 fail |
| 957 | * %-EMSGSIZE message too big. return mtu in this case. |
| 958 | **/ |
| 959 | |
| 960 | static int ip6_tnl_xmit2(struct sk_buff *skb, |
| 961 | struct net_device *dev, |
| 962 | __u8 dsfield, |
| 963 | struct flowi6 *fl6, |
| 964 | int encap_limit, |
| 965 | __u32 *pmtu) |
| 966 | { |
| 967 | struct net *net = dev_net(dev); |
| 968 | struct ip6_tnl *t = netdev_priv(dev); |
| 969 | struct net_device_stats *stats = &t->dev->stats; |
| 970 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
| 971 | struct ipv6_tel_txoption opt; |
| 972 | struct dst_entry *dst = NULL, *ndst = NULL; |
| 973 | struct net_device *tdev; |
| 974 | bool use_cache = false; |
| 975 | int mtu; |
| 976 | unsigned int max_headroom = sizeof(struct ipv6hdr); |
| 977 | u8 proto; |
| 978 | int err = -1; |
| 979 | |
| 980 | if (!(t->parms.flags & |
| 981 | (IP6_TNL_F_USE_ORIG_TCLASS | IP6_TNL_F_USE_ORIG_FWMARK))) { |
| 982 | /* enable the cache only only if the routing decision does |
| 983 | * not depend on the current inner header value |
| 984 | */ |
| 985 | use_cache = true; |
| 986 | } |
| 987 | |
| 988 | if (use_cache) |
| 989 | dst = ip6_tnl_dst_check(t); |
| 990 | if (!dst) { |
| 991 | ndst = ip6_route_output(net, NULL, fl6); |
| 992 | |
| 993 | if (ndst->error) |
| 994 | goto tx_err_link_failure; |
| 995 | ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0); |
| 996 | if (IS_ERR(ndst)) { |
| 997 | err = PTR_ERR(ndst); |
| 998 | ndst = NULL; |
| 999 | goto tx_err_link_failure; |
| 1000 | } |
| 1001 | dst = ndst; |
| 1002 | } |
| 1003 | |
| 1004 | tdev = dst->dev; |
| 1005 | |
| 1006 | if (tdev == dev) { |
| 1007 | stats->collisions++; |
| 1008 | net_warn_ratelimited("%s: Local routing loop detected!\n", |
| 1009 | t->parms.name); |
| 1010 | goto tx_err_dst_release; |
| 1011 | } |
| 1012 | mtu = dst_mtu(dst) - sizeof (*ipv6h); |
| 1013 | if (encap_limit >= 0) { |
| 1014 | max_headroom += 8; |
| 1015 | mtu -= 8; |
| 1016 | } |
| 1017 | if (mtu < IPV6_MIN_MTU) |
| 1018 | mtu = IPV6_MIN_MTU; |
| 1019 | if (skb_dst(skb)) |
| 1020 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu); |
| 1021 | if (skb->len > mtu) { |
| 1022 | *pmtu = mtu; |
| 1023 | err = -EMSGSIZE; |
| 1024 | goto tx_err_dst_release; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * Okay, now see if we can stuff it in the buffer as-is. |
| 1029 | */ |
| 1030 | max_headroom += LL_RESERVED_SPACE(tdev); |
| 1031 | |
| 1032 | if (skb_headroom(skb) < max_headroom || skb_shared(skb) || |
| 1033 | (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { |
| 1034 | struct sk_buff *new_skb; |
| 1035 | |
| 1036 | if (!(new_skb = skb_realloc_headroom(skb, max_headroom))) |
| 1037 | goto tx_err_dst_release; |
| 1038 | |
| 1039 | if (skb->sk) |
| 1040 | skb_set_owner_w(new_skb, skb->sk); |
| 1041 | consume_skb(skb); |
| 1042 | skb = new_skb; |
| 1043 | } |
| 1044 | skb_dst_drop(skb); |
| 1045 | if (!use_cache) { |
| 1046 | skb_dst_set(skb, dst); |
| 1047 | ndst = NULL; |
| 1048 | } else { |
| 1049 | skb_dst_set_noref(skb, dst); |
| 1050 | } |
| 1051 | skb->transport_header = skb->network_header; |
| 1052 | |
| 1053 | proto = fl6->flowi6_proto; |
| 1054 | if (encap_limit >= 0) { |
| 1055 | init_tel_txopt(&opt, encap_limit); |
| 1056 | ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL); |
| 1057 | } |
| 1058 | skb_push(skb, sizeof(struct ipv6hdr)); |
| 1059 | skb_reset_network_header(skb); |
| 1060 | ipv6h = ipv6_hdr(skb); |
| 1061 | ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield), fl6->flowlabel); |
| 1062 | ipv6h->hop_limit = t->parms.hop_limit; |
| 1063 | ipv6h->nexthdr = proto; |
| 1064 | ipv6h->saddr = fl6->saddr; |
| 1065 | ipv6h->daddr = fl6->daddr; |
| 1066 | ip6tunnel_xmit(skb, dev); |
| 1067 | if (ndst) |
| 1068 | ip6_tnl_dst_store(t, ndst); |
| 1069 | return 0; |
| 1070 | tx_err_link_failure: |
| 1071 | stats->tx_carrier_errors++; |
| 1072 | dst_link_failure(skb); |
| 1073 | tx_err_dst_release: |
| 1074 | dst_release(ndst); |
| 1075 | return err; |
| 1076 | } |
| 1077 | |
| 1078 | static inline int |
| 1079 | ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1080 | { |
| 1081 | struct ip6_tnl *t = netdev_priv(dev); |
| 1082 | const struct iphdr *iph = ip_hdr(skb); |
| 1083 | int encap_limit = -1; |
| 1084 | struct flowi6 fl6; |
| 1085 | __u8 dsfield; |
| 1086 | __u32 mtu; |
| 1087 | int err; |
| 1088 | |
| 1089 | if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) || |
| 1090 | !ip6_tnl_xmit_ctl(t)) |
| 1091 | return -1; |
| 1092 | |
| 1093 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1094 | encap_limit = t->parms.encap_limit; |
| 1095 | |
| 1096 | memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6)); |
| 1097 | fl6.flowi6_proto = IPPROTO_IPIP; |
| 1098 | |
| 1099 | dsfield = ipv4_get_dsfield(iph); |
| 1100 | |
| 1101 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
| 1102 | fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT) |
| 1103 | & IPV6_TCLASS_MASK; |
| 1104 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
| 1105 | fl6.flowi6_mark = skb->mark; |
| 1106 | |
| 1107 | err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu); |
| 1108 | if (err != 0) { |
| 1109 | /* XXX: send ICMP error even if DF is not set. */ |
| 1110 | if (err == -EMSGSIZE) |
| 1111 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, |
| 1112 | htonl(mtu)); |
| 1113 | return -1; |
| 1114 | } |
| 1115 | |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | static inline int |
| 1120 | ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1121 | { |
| 1122 | struct ip6_tnl *t = netdev_priv(dev); |
| 1123 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
| 1124 | int encap_limit = -1; |
| 1125 | __u16 offset; |
| 1126 | struct flowi6 fl6; |
| 1127 | __u8 dsfield; |
| 1128 | __u32 mtu; |
| 1129 | int err; |
| 1130 | |
| 1131 | if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) || |
| 1132 | !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h)) |
| 1133 | return -1; |
| 1134 | |
| 1135 | offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb)); |
| 1136 | if (offset > 0) { |
| 1137 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 1138 | tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset]; |
| 1139 | if (tel->encap_limit == 0) { |
| 1140 | icmpv6_send(skb, ICMPV6_PARAMPROB, |
| 1141 | ICMPV6_HDR_FIELD, offset + 2); |
| 1142 | return -1; |
| 1143 | } |
| 1144 | encap_limit = tel->encap_limit - 1; |
| 1145 | } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1146 | encap_limit = t->parms.encap_limit; |
| 1147 | |
| 1148 | memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6)); |
| 1149 | fl6.flowi6_proto = IPPROTO_IPV6; |
| 1150 | |
| 1151 | dsfield = ipv6_get_dsfield(ipv6h); |
| 1152 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
| 1153 | fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK); |
| 1154 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) |
| 1155 | fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK); |
| 1156 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
| 1157 | fl6.flowi6_mark = skb->mark; |
| 1158 | |
| 1159 | err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu); |
| 1160 | if (err != 0) { |
| 1161 | if (err == -EMSGSIZE) |
| 1162 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
| 1163 | return -1; |
| 1164 | } |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | static netdev_tx_t |
| 1170 | ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1171 | { |
| 1172 | struct ip6_tnl *t = netdev_priv(dev); |
| 1173 | struct net_device_stats *stats = &t->dev->stats; |
| 1174 | int ret; |
| 1175 | |
| 1176 | switch (skb->protocol) { |
| 1177 | case htons(ETH_P_IP): |
| 1178 | ret = ip4ip6_tnl_xmit(skb, dev); |
| 1179 | break; |
| 1180 | case htons(ETH_P_IPV6): |
| 1181 | ret = ip6ip6_tnl_xmit(skb, dev); |
| 1182 | break; |
| 1183 | default: |
| 1184 | goto tx_err; |
| 1185 | } |
| 1186 | |
| 1187 | if (ret < 0) |
| 1188 | goto tx_err; |
| 1189 | |
| 1190 | return NETDEV_TX_OK; |
| 1191 | |
| 1192 | tx_err: |
| 1193 | stats->tx_errors++; |
| 1194 | stats->tx_dropped++; |
| 1195 | kfree_skb(skb); |
| 1196 | return NETDEV_TX_OK; |
| 1197 | } |
| 1198 | |
| 1199 | static void ip6_tnl_link_config(struct ip6_tnl *t) |
| 1200 | { |
| 1201 | struct net_device *dev = t->dev; |
| 1202 | struct __ip6_tnl_parm *p = &t->parms; |
| 1203 | struct flowi6 *fl6 = &t->fl.u.ip6; |
| 1204 | |
| 1205 | memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); |
| 1206 | memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); |
| 1207 | |
| 1208 | /* Set up flowi template */ |
| 1209 | fl6->saddr = p->laddr; |
| 1210 | fl6->daddr = p->raddr; |
| 1211 | fl6->flowi6_oif = p->link; |
| 1212 | fl6->flowlabel = 0; |
| 1213 | |
| 1214 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) |
| 1215 | fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; |
| 1216 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) |
| 1217 | fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; |
| 1218 | |
| 1219 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); |
| 1220 | p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); |
| 1221 | |
| 1222 | if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV) |
| 1223 | dev->flags |= IFF_POINTOPOINT; |
| 1224 | else |
| 1225 | dev->flags &= ~IFF_POINTOPOINT; |
| 1226 | |
| 1227 | dev->iflink = p->link; |
| 1228 | |
| 1229 | if (p->flags & IP6_TNL_F_CAP_XMIT) { |
| 1230 | int strict = (ipv6_addr_type(&p->raddr) & |
| 1231 | (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); |
| 1232 | |
| 1233 | struct rt6_info *rt = rt6_lookup(dev_net(dev), |
| 1234 | &p->raddr, &p->laddr, |
| 1235 | p->link, strict); |
| 1236 | |
| 1237 | if (rt == NULL) |
| 1238 | return; |
| 1239 | |
| 1240 | if (rt->dst.dev) { |
| 1241 | dev->hard_header_len = rt->dst.dev->hard_header_len + |
| 1242 | sizeof (struct ipv6hdr); |
| 1243 | |
| 1244 | dev->mtu = rt->dst.dev->mtu - sizeof (struct ipv6hdr); |
| 1245 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1246 | dev->mtu-=8; |
| 1247 | |
| 1248 | if (dev->mtu < IPV6_MIN_MTU) |
| 1249 | dev->mtu = IPV6_MIN_MTU; |
| 1250 | } |
| 1251 | ip6_rt_put(rt); |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * ip6_tnl_change - update the tunnel parameters |
| 1257 | * @t: tunnel to be changed |
| 1258 | * @p: tunnel configuration parameters |
| 1259 | * |
| 1260 | * Description: |
| 1261 | * ip6_tnl_change() updates the tunnel parameters |
| 1262 | **/ |
| 1263 | |
| 1264 | static int |
| 1265 | ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p) |
| 1266 | { |
| 1267 | t->parms.laddr = p->laddr; |
| 1268 | t->parms.raddr = p->raddr; |
| 1269 | t->parms.flags = p->flags; |
| 1270 | t->parms.hop_limit = p->hop_limit; |
| 1271 | t->parms.encap_limit = p->encap_limit; |
| 1272 | t->parms.flowinfo = p->flowinfo; |
| 1273 | t->parms.link = p->link; |
| 1274 | t->parms.proto = p->proto; |
| 1275 | ip6_tnl_dst_reset(t); |
| 1276 | ip6_tnl_link_config(t); |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
| 1280 | static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p) |
| 1281 | { |
| 1282 | struct net *net = dev_net(t->dev); |
| 1283 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 1284 | int err; |
| 1285 | |
| 1286 | ip6_tnl_unlink(ip6n, t); |
| 1287 | synchronize_net(); |
| 1288 | err = ip6_tnl_change(t, p); |
| 1289 | ip6_tnl_link(ip6n, t); |
| 1290 | netdev_state_change(t->dev); |
| 1291 | return err; |
| 1292 | } |
| 1293 | |
| 1294 | static void |
| 1295 | ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u) |
| 1296 | { |
| 1297 | p->laddr = u->laddr; |
| 1298 | p->raddr = u->raddr; |
| 1299 | p->flags = u->flags; |
| 1300 | p->hop_limit = u->hop_limit; |
| 1301 | p->encap_limit = u->encap_limit; |
| 1302 | p->flowinfo = u->flowinfo; |
| 1303 | p->link = u->link; |
| 1304 | p->proto = u->proto; |
| 1305 | memcpy(p->name, u->name, sizeof(u->name)); |
| 1306 | } |
| 1307 | |
| 1308 | static void |
| 1309 | ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p) |
| 1310 | { |
| 1311 | u->laddr = p->laddr; |
| 1312 | u->raddr = p->raddr; |
| 1313 | u->flags = p->flags; |
| 1314 | u->hop_limit = p->hop_limit; |
| 1315 | u->encap_limit = p->encap_limit; |
| 1316 | u->flowinfo = p->flowinfo; |
| 1317 | u->link = p->link; |
| 1318 | u->proto = p->proto; |
| 1319 | memcpy(u->name, p->name, sizeof(u->name)); |
| 1320 | } |
| 1321 | |
| 1322 | /** |
| 1323 | * ip6_tnl_ioctl - configure ipv6 tunnels from userspace |
| 1324 | * @dev: virtual device associated with tunnel |
| 1325 | * @ifr: parameters passed from userspace |
| 1326 | * @cmd: command to be performed |
| 1327 | * |
| 1328 | * Description: |
| 1329 | * ip6_tnl_ioctl() is used for managing IPv6 tunnels |
| 1330 | * from userspace. |
| 1331 | * |
| 1332 | * The possible commands are the following: |
| 1333 | * %SIOCGETTUNNEL: get tunnel parameters for device |
| 1334 | * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters |
| 1335 | * %SIOCCHGTUNNEL: change tunnel parameters to those given |
| 1336 | * %SIOCDELTUNNEL: delete tunnel |
| 1337 | * |
| 1338 | * The fallback device "ip6tnl0", created during module |
| 1339 | * initialization, can be used for creating other tunnel devices. |
| 1340 | * |
| 1341 | * Return: |
| 1342 | * 0 on success, |
| 1343 | * %-EFAULT if unable to copy data to or from userspace, |
| 1344 | * %-EPERM if current process hasn't %CAP_NET_ADMIN set |
| 1345 | * %-EINVAL if passed tunnel parameters are invalid, |
| 1346 | * %-EEXIST if changing a tunnel's parameters would cause a conflict |
| 1347 | * %-ENODEV if attempting to change or delete a nonexisting device |
| 1348 | **/ |
| 1349 | |
| 1350 | static int |
| 1351 | ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 1352 | { |
| 1353 | int err = 0; |
| 1354 | struct ip6_tnl_parm p; |
| 1355 | struct __ip6_tnl_parm p1; |
| 1356 | struct ip6_tnl *t = NULL; |
| 1357 | struct net *net = dev_net(dev); |
| 1358 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 1359 | |
| 1360 | switch (cmd) { |
| 1361 | case SIOCGETTUNNEL: |
| 1362 | if (dev == ip6n->fb_tnl_dev) { |
| 1363 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) { |
| 1364 | err = -EFAULT; |
| 1365 | break; |
| 1366 | } |
| 1367 | ip6_tnl_parm_from_user(&p1, &p); |
| 1368 | t = ip6_tnl_locate(net, &p1, 0); |
| 1369 | } else { |
| 1370 | memset(&p, 0, sizeof(p)); |
| 1371 | } |
| 1372 | if (t == NULL) |
| 1373 | t = netdev_priv(dev); |
| 1374 | ip6_tnl_parm_to_user(&p, &t->parms); |
| 1375 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) { |
| 1376 | err = -EFAULT; |
| 1377 | } |
| 1378 | break; |
| 1379 | case SIOCADDTUNNEL: |
| 1380 | case SIOCCHGTUNNEL: |
| 1381 | err = -EPERM; |
| 1382 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
| 1383 | break; |
| 1384 | err = -EFAULT; |
| 1385 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) |
| 1386 | break; |
| 1387 | err = -EINVAL; |
| 1388 | if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP && |
| 1389 | p.proto != 0) |
| 1390 | break; |
| 1391 | ip6_tnl_parm_from_user(&p1, &p); |
| 1392 | t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL); |
| 1393 | if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) { |
| 1394 | if (t != NULL) { |
| 1395 | if (t->dev != dev) { |
| 1396 | err = -EEXIST; |
| 1397 | break; |
| 1398 | } |
| 1399 | } else |
| 1400 | t = netdev_priv(dev); |
| 1401 | |
| 1402 | err = ip6_tnl_update(t, &p1); |
| 1403 | } |
| 1404 | if (t) { |
| 1405 | err = 0; |
| 1406 | ip6_tnl_parm_to_user(&p, &t->parms); |
| 1407 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) |
| 1408 | err = -EFAULT; |
| 1409 | |
| 1410 | } else |
| 1411 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); |
| 1412 | break; |
| 1413 | case SIOCDELTUNNEL: |
| 1414 | err = -EPERM; |
| 1415 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
| 1416 | break; |
| 1417 | |
| 1418 | if (dev == ip6n->fb_tnl_dev) { |
| 1419 | err = -EFAULT; |
| 1420 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) |
| 1421 | break; |
| 1422 | err = -ENOENT; |
| 1423 | ip6_tnl_parm_from_user(&p1, &p); |
| 1424 | t = ip6_tnl_locate(net, &p1, 0); |
| 1425 | if (t == NULL) |
| 1426 | break; |
| 1427 | err = -EPERM; |
| 1428 | if (t->dev == ip6n->fb_tnl_dev) |
| 1429 | break; |
| 1430 | dev = t->dev; |
| 1431 | } |
| 1432 | err = 0; |
| 1433 | unregister_netdevice(dev); |
| 1434 | break; |
| 1435 | default: |
| 1436 | err = -EINVAL; |
| 1437 | } |
| 1438 | return err; |
| 1439 | } |
| 1440 | |
| 1441 | /** |
| 1442 | * ip6_tnl_change_mtu - change mtu manually for tunnel device |
| 1443 | * @dev: virtual device associated with tunnel |
| 1444 | * @new_mtu: the new mtu |
| 1445 | * |
| 1446 | * Return: |
| 1447 | * 0 on success, |
| 1448 | * %-EINVAL if mtu too small |
| 1449 | **/ |
| 1450 | |
| 1451 | static int |
| 1452 | ip6_tnl_change_mtu(struct net_device *dev, int new_mtu) |
| 1453 | { |
| 1454 | if (new_mtu < IPV6_MIN_MTU) { |
| 1455 | return -EINVAL; |
| 1456 | } |
| 1457 | dev->mtu = new_mtu; |
| 1458 | return 0; |
| 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | static const struct net_device_ops ip6_tnl_netdev_ops = { |
| 1463 | .ndo_init = ip6_tnl_dev_init, |
| 1464 | .ndo_uninit = ip6_tnl_dev_uninit, |
| 1465 | .ndo_start_xmit = ip6_tnl_xmit, |
| 1466 | .ndo_do_ioctl = ip6_tnl_ioctl, |
| 1467 | .ndo_change_mtu = ip6_tnl_change_mtu, |
| 1468 | .ndo_get_stats = ip6_get_stats, |
| 1469 | }; |
| 1470 | |
| 1471 | |
| 1472 | /** |
| 1473 | * ip6_tnl_dev_setup - setup virtual tunnel device |
| 1474 | * @dev: virtual device associated with tunnel |
| 1475 | * |
| 1476 | * Description: |
| 1477 | * Initialize function pointers and device parameters |
| 1478 | **/ |
| 1479 | |
| 1480 | static void ip6_tnl_dev_setup(struct net_device *dev) |
| 1481 | { |
| 1482 | struct ip6_tnl *t; |
| 1483 | |
| 1484 | dev->netdev_ops = &ip6_tnl_netdev_ops; |
| 1485 | dev->destructor = ip6_dev_free; |
| 1486 | |
| 1487 | dev->type = ARPHRD_TUNNEL6; |
| 1488 | dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr); |
| 1489 | dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr); |
| 1490 | t = netdev_priv(dev); |
| 1491 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1492 | dev->mtu-=8; |
| 1493 | dev->flags |= IFF_NOARP; |
| 1494 | dev->addr_len = sizeof(struct in6_addr); |
| 1495 | dev->features |= NETIF_F_NETNS_LOCAL; |
| 1496 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
| 1497 | } |
| 1498 | |
| 1499 | |
| 1500 | /** |
| 1501 | * ip6_tnl_dev_init_gen - general initializer for all tunnel devices |
| 1502 | * @dev: virtual device associated with tunnel |
| 1503 | **/ |
| 1504 | |
| 1505 | static inline int |
| 1506 | ip6_tnl_dev_init_gen(struct net_device *dev) |
| 1507 | { |
| 1508 | struct ip6_tnl *t = netdev_priv(dev); |
| 1509 | |
| 1510 | t->dev = dev; |
| 1511 | dev->tstats = alloc_percpu(struct pcpu_tstats); |
| 1512 | if (!dev->tstats) |
| 1513 | return -ENOMEM; |
| 1514 | return 0; |
| 1515 | } |
| 1516 | |
| 1517 | /** |
| 1518 | * ip6_tnl_dev_init - initializer for all non fallback tunnel devices |
| 1519 | * @dev: virtual device associated with tunnel |
| 1520 | **/ |
| 1521 | |
| 1522 | static int ip6_tnl_dev_init(struct net_device *dev) |
| 1523 | { |
| 1524 | struct ip6_tnl *t = netdev_priv(dev); |
| 1525 | int err = ip6_tnl_dev_init_gen(dev); |
| 1526 | |
| 1527 | if (err) |
| 1528 | return err; |
| 1529 | ip6_tnl_link_config(t); |
| 1530 | return 0; |
| 1531 | } |
| 1532 | |
| 1533 | /** |
| 1534 | * ip6_fb_tnl_dev_init - initializer for fallback tunnel device |
| 1535 | * @dev: fallback device |
| 1536 | * |
| 1537 | * Return: 0 |
| 1538 | **/ |
| 1539 | |
| 1540 | static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev) |
| 1541 | { |
| 1542 | struct ip6_tnl *t = netdev_priv(dev); |
| 1543 | struct net *net = dev_net(dev); |
| 1544 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 1545 | |
| 1546 | t->parms.proto = IPPROTO_IPV6; |
| 1547 | dev_hold(dev); |
| 1548 | |
| 1549 | rcu_assign_pointer(ip6n->tnls_wc[0], t); |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
| 1553 | static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 1554 | { |
| 1555 | u8 proto; |
| 1556 | |
| 1557 | if (!data || !data[IFLA_IPTUN_PROTO]) |
| 1558 | return 0; |
| 1559 | |
| 1560 | proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); |
| 1561 | if (proto != IPPROTO_IPV6 && |
| 1562 | proto != IPPROTO_IPIP && |
| 1563 | proto != 0) |
| 1564 | return -EINVAL; |
| 1565 | |
| 1566 | return 0; |
| 1567 | } |
| 1568 | |
| 1569 | static void ip6_tnl_netlink_parms(struct nlattr *data[], |
| 1570 | struct __ip6_tnl_parm *parms) |
| 1571 | { |
| 1572 | memset(parms, 0, sizeof(*parms)); |
| 1573 | |
| 1574 | if (!data) |
| 1575 | return; |
| 1576 | |
| 1577 | if (data[IFLA_IPTUN_LINK]) |
| 1578 | parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]); |
| 1579 | |
| 1580 | if (data[IFLA_IPTUN_LOCAL]) |
| 1581 | nla_memcpy(&parms->laddr, data[IFLA_IPTUN_LOCAL], |
| 1582 | sizeof(struct in6_addr)); |
| 1583 | |
| 1584 | if (data[IFLA_IPTUN_REMOTE]) |
| 1585 | nla_memcpy(&parms->raddr, data[IFLA_IPTUN_REMOTE], |
| 1586 | sizeof(struct in6_addr)); |
| 1587 | |
| 1588 | if (data[IFLA_IPTUN_TTL]) |
| 1589 | parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]); |
| 1590 | |
| 1591 | if (data[IFLA_IPTUN_ENCAP_LIMIT]) |
| 1592 | parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]); |
| 1593 | |
| 1594 | if (data[IFLA_IPTUN_FLOWINFO]) |
| 1595 | parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]); |
| 1596 | |
| 1597 | if (data[IFLA_IPTUN_FLAGS]) |
| 1598 | parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]); |
| 1599 | |
| 1600 | if (data[IFLA_IPTUN_PROTO]) |
| 1601 | parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); |
| 1602 | } |
| 1603 | |
| 1604 | static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev, |
| 1605 | struct nlattr *tb[], struct nlattr *data[]) |
| 1606 | { |
| 1607 | struct net *net = dev_net(dev); |
| 1608 | struct ip6_tnl *nt; |
| 1609 | |
| 1610 | nt = netdev_priv(dev); |
| 1611 | ip6_tnl_netlink_parms(data, &nt->parms); |
| 1612 | |
| 1613 | if (ip6_tnl_locate(net, &nt->parms, 0)) |
| 1614 | return -EEXIST; |
| 1615 | |
| 1616 | return ip6_tnl_create2(dev); |
| 1617 | } |
| 1618 | |
| 1619 | static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[], |
| 1620 | struct nlattr *data[]) |
| 1621 | { |
| 1622 | struct ip6_tnl *t; |
| 1623 | struct __ip6_tnl_parm p; |
| 1624 | struct net *net = dev_net(dev); |
| 1625 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 1626 | |
| 1627 | if (dev == ip6n->fb_tnl_dev) |
| 1628 | return -EINVAL; |
| 1629 | |
| 1630 | ip6_tnl_netlink_parms(data, &p); |
| 1631 | |
| 1632 | t = ip6_tnl_locate(net, &p, 0); |
| 1633 | |
| 1634 | if (t) { |
| 1635 | if (t->dev != dev) |
| 1636 | return -EEXIST; |
| 1637 | } else |
| 1638 | t = netdev_priv(dev); |
| 1639 | |
| 1640 | return ip6_tnl_update(t, &p); |
| 1641 | } |
| 1642 | |
| 1643 | static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head) |
| 1644 | { |
| 1645 | struct net *net = dev_net(dev); |
| 1646 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 1647 | |
| 1648 | if (dev != ip6n->fb_tnl_dev) |
| 1649 | unregister_netdevice_queue(dev, head); |
| 1650 | } |
| 1651 | |
| 1652 | static size_t ip6_tnl_get_size(const struct net_device *dev) |
| 1653 | { |
| 1654 | return |
| 1655 | /* IFLA_IPTUN_LINK */ |
| 1656 | nla_total_size(4) + |
| 1657 | /* IFLA_IPTUN_LOCAL */ |
| 1658 | nla_total_size(sizeof(struct in6_addr)) + |
| 1659 | /* IFLA_IPTUN_REMOTE */ |
| 1660 | nla_total_size(sizeof(struct in6_addr)) + |
| 1661 | /* IFLA_IPTUN_TTL */ |
| 1662 | nla_total_size(1) + |
| 1663 | /* IFLA_IPTUN_ENCAP_LIMIT */ |
| 1664 | nla_total_size(1) + |
| 1665 | /* IFLA_IPTUN_FLOWINFO */ |
| 1666 | nla_total_size(4) + |
| 1667 | /* IFLA_IPTUN_FLAGS */ |
| 1668 | nla_total_size(4) + |
| 1669 | /* IFLA_IPTUN_PROTO */ |
| 1670 | nla_total_size(1) + |
| 1671 | 0; |
| 1672 | } |
| 1673 | |
| 1674 | static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 1675 | { |
| 1676 | struct ip6_tnl *tunnel = netdev_priv(dev); |
| 1677 | struct __ip6_tnl_parm *parm = &tunnel->parms; |
| 1678 | |
| 1679 | if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) || |
| 1680 | nla_put(skb, IFLA_IPTUN_LOCAL, sizeof(struct in6_addr), |
| 1681 | &parm->laddr) || |
| 1682 | nla_put(skb, IFLA_IPTUN_REMOTE, sizeof(struct in6_addr), |
| 1683 | &parm->raddr) || |
| 1684 | nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) || |
| 1685 | nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) || |
| 1686 | nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) || |
| 1687 | nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) || |
| 1688 | nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto)) |
| 1689 | goto nla_put_failure; |
| 1690 | return 0; |
| 1691 | |
| 1692 | nla_put_failure: |
| 1693 | return -EMSGSIZE; |
| 1694 | } |
| 1695 | |
| 1696 | static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = { |
| 1697 | [IFLA_IPTUN_LINK] = { .type = NLA_U32 }, |
| 1698 | [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) }, |
| 1699 | [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) }, |
| 1700 | [IFLA_IPTUN_TTL] = { .type = NLA_U8 }, |
| 1701 | [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 }, |
| 1702 | [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 }, |
| 1703 | [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 }, |
| 1704 | [IFLA_IPTUN_PROTO] = { .type = NLA_U8 }, |
| 1705 | }; |
| 1706 | |
| 1707 | static struct rtnl_link_ops ip6_link_ops __read_mostly = { |
| 1708 | .kind = "ip6tnl", |
| 1709 | .maxtype = IFLA_IPTUN_MAX, |
| 1710 | .policy = ip6_tnl_policy, |
| 1711 | .priv_size = sizeof(struct ip6_tnl), |
| 1712 | .setup = ip6_tnl_dev_setup, |
| 1713 | .validate = ip6_tnl_validate, |
| 1714 | .newlink = ip6_tnl_newlink, |
| 1715 | .changelink = ip6_tnl_changelink, |
| 1716 | .dellink = ip6_tnl_dellink, |
| 1717 | .get_size = ip6_tnl_get_size, |
| 1718 | .fill_info = ip6_tnl_fill_info, |
| 1719 | }; |
| 1720 | |
| 1721 | static struct xfrm6_tunnel ip4ip6_handler __read_mostly = { |
| 1722 | .handler = ip4ip6_rcv, |
| 1723 | .err_handler = ip4ip6_err, |
| 1724 | .priority = 1, |
| 1725 | }; |
| 1726 | |
| 1727 | static struct xfrm6_tunnel ip6ip6_handler __read_mostly = { |
| 1728 | .handler = ip6ip6_rcv, |
| 1729 | .err_handler = ip6ip6_err, |
| 1730 | .priority = 1, |
| 1731 | }; |
| 1732 | |
| 1733 | static void __net_exit ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n) |
| 1734 | { |
| 1735 | int h; |
| 1736 | struct ip6_tnl *t; |
| 1737 | LIST_HEAD(list); |
| 1738 | |
| 1739 | for (h = 0; h < HASH_SIZE; h++) { |
| 1740 | t = rtnl_dereference(ip6n->tnls_r_l[h]); |
| 1741 | while (t != NULL) { |
| 1742 | unregister_netdevice_queue(t->dev, &list); |
| 1743 | t = rtnl_dereference(t->next); |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | t = rtnl_dereference(ip6n->tnls_wc[0]); |
| 1748 | unregister_netdevice_queue(t->dev, &list); |
| 1749 | unregister_netdevice_many(&list); |
| 1750 | } |
| 1751 | |
| 1752 | static int __net_init ip6_tnl_init_net(struct net *net) |
| 1753 | { |
| 1754 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 1755 | struct ip6_tnl *t = NULL; |
| 1756 | int err; |
| 1757 | |
| 1758 | ip6n->tnls[0] = ip6n->tnls_wc; |
| 1759 | ip6n->tnls[1] = ip6n->tnls_r_l; |
| 1760 | |
| 1761 | err = -ENOMEM; |
| 1762 | ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0", |
| 1763 | ip6_tnl_dev_setup); |
| 1764 | |
| 1765 | if (!ip6n->fb_tnl_dev) |
| 1766 | goto err_alloc_dev; |
| 1767 | dev_net_set(ip6n->fb_tnl_dev, net); |
| 1768 | ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops; |
| 1769 | |
| 1770 | err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev); |
| 1771 | if (err < 0) |
| 1772 | goto err_register; |
| 1773 | |
| 1774 | err = register_netdev(ip6n->fb_tnl_dev); |
| 1775 | if (err < 0) |
| 1776 | goto err_register; |
| 1777 | |
| 1778 | t = netdev_priv(ip6n->fb_tnl_dev); |
| 1779 | |
| 1780 | strcpy(t->parms.name, ip6n->fb_tnl_dev->name); |
| 1781 | return 0; |
| 1782 | |
| 1783 | err_register: |
| 1784 | ip6_dev_free(ip6n->fb_tnl_dev); |
| 1785 | err_alloc_dev: |
| 1786 | return err; |
| 1787 | } |
| 1788 | |
| 1789 | static void __net_exit ip6_tnl_exit_net(struct net *net) |
| 1790 | { |
| 1791 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 1792 | |
| 1793 | rtnl_lock(); |
| 1794 | ip6_tnl_destroy_tunnels(ip6n); |
| 1795 | rtnl_unlock(); |
| 1796 | } |
| 1797 | |
| 1798 | static struct pernet_operations ip6_tnl_net_ops = { |
| 1799 | .init = ip6_tnl_init_net, |
| 1800 | .exit = ip6_tnl_exit_net, |
| 1801 | .id = &ip6_tnl_net_id, |
| 1802 | .size = sizeof(struct ip6_tnl_net), |
| 1803 | }; |
| 1804 | |
| 1805 | /** |
| 1806 | * ip6_tunnel_init - register protocol and reserve needed resources |
| 1807 | * |
| 1808 | * Return: 0 on success |
| 1809 | **/ |
| 1810 | |
| 1811 | static int __init ip6_tunnel_init(void) |
| 1812 | { |
| 1813 | int err; |
| 1814 | |
| 1815 | err = register_pernet_device(&ip6_tnl_net_ops); |
| 1816 | if (err < 0) |
| 1817 | goto out_pernet; |
| 1818 | |
| 1819 | err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET); |
| 1820 | if (err < 0) { |
| 1821 | pr_err("%s: can't register ip4ip6\n", __func__); |
| 1822 | goto out_ip4ip6; |
| 1823 | } |
| 1824 | |
| 1825 | err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6); |
| 1826 | if (err < 0) { |
| 1827 | pr_err("%s: can't register ip6ip6\n", __func__); |
| 1828 | goto out_ip6ip6; |
| 1829 | } |
| 1830 | err = rtnl_link_register(&ip6_link_ops); |
| 1831 | if (err < 0) |
| 1832 | goto rtnl_link_failed; |
| 1833 | |
| 1834 | return 0; |
| 1835 | |
| 1836 | rtnl_link_failed: |
| 1837 | xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6); |
| 1838 | out_ip6ip6: |
| 1839 | xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET); |
| 1840 | out_ip4ip6: |
| 1841 | unregister_pernet_device(&ip6_tnl_net_ops); |
| 1842 | out_pernet: |
| 1843 | return err; |
| 1844 | } |
| 1845 | |
| 1846 | /** |
| 1847 | * ip6_tunnel_cleanup - free resources and unregister protocol |
| 1848 | **/ |
| 1849 | |
| 1850 | static void __exit ip6_tunnel_cleanup(void) |
| 1851 | { |
| 1852 | rtnl_link_unregister(&ip6_link_ops); |
| 1853 | if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET)) |
| 1854 | pr_info("%s: can't deregister ip4ip6\n", __func__); |
| 1855 | |
| 1856 | if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6)) |
| 1857 | pr_info("%s: can't deregister ip6ip6\n", __func__); |
| 1858 | |
| 1859 | unregister_pernet_device(&ip6_tnl_net_ops); |
| 1860 | } |
| 1861 | |
| 1862 | module_init(ip6_tunnel_init); |
| 1863 | module_exit(ip6_tunnel_cleanup); |