Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Nicira, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 16 | * 02110-1301, USA |
| 17 | */ |
| 18 | |
| 19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 20 | |
| 21 | #include <linux/capability.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | #include <linux/skbuff.h> |
| 28 | #include <linux/netdevice.h> |
| 29 | #include <linux/in.h> |
| 30 | #include <linux/tcp.h> |
| 31 | #include <linux/udp.h> |
| 32 | #include <linux/if_arp.h> |
| 33 | #include <linux/mroute.h> |
| 34 | #include <linux/init.h> |
| 35 | #include <linux/in6.h> |
| 36 | #include <linux/inetdevice.h> |
| 37 | #include <linux/igmp.h> |
| 38 | #include <linux/netfilter_ipv4.h> |
| 39 | #include <linux/etherdevice.h> |
| 40 | #include <linux/if_ether.h> |
| 41 | #include <linux/if_vlan.h> |
| 42 | #include <linux/rculist.h> |
| 43 | |
| 44 | #include <net/sock.h> |
| 45 | #include <net/ip.h> |
| 46 | #include <net/icmp.h> |
| 47 | #include <net/protocol.h> |
| 48 | #include <net/ip_tunnels.h> |
| 49 | #include <net/arp.h> |
| 50 | #include <net/checksum.h> |
| 51 | #include <net/dsfield.h> |
| 52 | #include <net/inet_ecn.h> |
| 53 | #include <net/xfrm.h> |
| 54 | #include <net/net_namespace.h> |
| 55 | #include <net/netns/generic.h> |
| 56 | #include <net/rtnetlink.h> |
| 57 | |
| 58 | #if IS_ENABLED(CONFIG_IPV6) |
| 59 | #include <net/ipv6.h> |
| 60 | #include <net/ip6_fib.h> |
| 61 | #include <net/ip6_route.h> |
| 62 | #endif |
| 63 | |
| 64 | static unsigned int ip_tunnel_hash(struct ip_tunnel_net *itn, |
| 65 | __be32 key, __be32 remote) |
| 66 | { |
| 67 | return hash_32((__force u32)key ^ (__force u32)remote, |
| 68 | IP_TNL_HASH_BITS); |
| 69 | } |
| 70 | |
| 71 | /* Often modified stats are per cpu, other are shared (netdev->stats) */ |
| 72 | struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev, |
| 73 | struct rtnl_link_stats64 *tot) |
| 74 | { |
| 75 | int i; |
| 76 | |
| 77 | for_each_possible_cpu(i) { |
| 78 | const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i); |
| 79 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; |
| 80 | unsigned int start; |
| 81 | |
| 82 | do { |
| 83 | start = u64_stats_fetch_begin_bh(&tstats->syncp); |
| 84 | rx_packets = tstats->rx_packets; |
| 85 | tx_packets = tstats->tx_packets; |
| 86 | rx_bytes = tstats->rx_bytes; |
| 87 | tx_bytes = tstats->tx_bytes; |
| 88 | } while (u64_stats_fetch_retry_bh(&tstats->syncp, start)); |
| 89 | |
| 90 | tot->rx_packets += rx_packets; |
| 91 | tot->tx_packets += tx_packets; |
| 92 | tot->rx_bytes += rx_bytes; |
| 93 | tot->tx_bytes += tx_bytes; |
| 94 | } |
| 95 | |
| 96 | tot->multicast = dev->stats.multicast; |
| 97 | |
| 98 | tot->rx_crc_errors = dev->stats.rx_crc_errors; |
| 99 | tot->rx_fifo_errors = dev->stats.rx_fifo_errors; |
| 100 | tot->rx_length_errors = dev->stats.rx_length_errors; |
| 101 | tot->rx_frame_errors = dev->stats.rx_frame_errors; |
| 102 | tot->rx_errors = dev->stats.rx_errors; |
| 103 | |
| 104 | tot->tx_fifo_errors = dev->stats.tx_fifo_errors; |
| 105 | tot->tx_carrier_errors = dev->stats.tx_carrier_errors; |
| 106 | tot->tx_dropped = dev->stats.tx_dropped; |
| 107 | tot->tx_aborted_errors = dev->stats.tx_aborted_errors; |
| 108 | tot->tx_errors = dev->stats.tx_errors; |
| 109 | |
| 110 | tot->collisions = dev->stats.collisions; |
| 111 | |
| 112 | return tot; |
| 113 | } |
| 114 | EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64); |
| 115 | |
| 116 | static bool ip_tunnel_key_match(const struct ip_tunnel_parm *p, |
| 117 | __be16 flags, __be32 key) |
| 118 | { |
| 119 | if (p->i_flags & TUNNEL_KEY) { |
| 120 | if (flags & TUNNEL_KEY) |
| 121 | return key == p->i_key; |
| 122 | else |
| 123 | /* key expected, none present */ |
| 124 | return false; |
| 125 | } else |
| 126 | return !(flags & TUNNEL_KEY); |
| 127 | } |
| 128 | |
| 129 | /* Fallback tunnel: no source, no destination, no key, no options |
| 130 | |
| 131 | Tunnel hash table: |
| 132 | We require exact key match i.e. if a key is present in packet |
| 133 | it will match only tunnel with the same key; if it is not present, |
| 134 | it will match only keyless tunnel. |
| 135 | |
| 136 | All keysless packets, if not matched configured keyless tunnels |
| 137 | will match fallback tunnel. |
| 138 | Given src, dst and key, find appropriate for input tunnel. |
| 139 | */ |
| 140 | struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn, |
| 141 | int link, __be16 flags, |
| 142 | __be32 remote, __be32 local, |
| 143 | __be32 key) |
| 144 | { |
| 145 | unsigned int hash; |
| 146 | struct ip_tunnel *t, *cand = NULL; |
| 147 | struct hlist_head *head; |
| 148 | |
| 149 | hash = ip_tunnel_hash(itn, key, remote); |
| 150 | head = &itn->tunnels[hash]; |
| 151 | |
| 152 | hlist_for_each_entry_rcu(t, head, hash_node) { |
| 153 | if (local != t->parms.iph.saddr || |
| 154 | remote != t->parms.iph.daddr || |
| 155 | !(t->dev->flags & IFF_UP)) |
| 156 | continue; |
| 157 | |
| 158 | if (!ip_tunnel_key_match(&t->parms, flags, key)) |
| 159 | continue; |
| 160 | |
| 161 | if (t->parms.link == link) |
| 162 | return t; |
| 163 | else |
| 164 | cand = t; |
| 165 | } |
| 166 | |
| 167 | hlist_for_each_entry_rcu(t, head, hash_node) { |
| 168 | if (remote != t->parms.iph.daddr || |
| 169 | !(t->dev->flags & IFF_UP)) |
| 170 | continue; |
| 171 | |
| 172 | if (!ip_tunnel_key_match(&t->parms, flags, key)) |
| 173 | continue; |
| 174 | |
| 175 | if (t->parms.link == link) |
| 176 | return t; |
| 177 | else if (!cand) |
| 178 | cand = t; |
| 179 | } |
| 180 | |
| 181 | hash = ip_tunnel_hash(itn, key, 0); |
| 182 | head = &itn->tunnels[hash]; |
| 183 | |
| 184 | hlist_for_each_entry_rcu(t, head, hash_node) { |
| 185 | if ((local != t->parms.iph.saddr && |
| 186 | (local != t->parms.iph.daddr || |
| 187 | !ipv4_is_multicast(local))) || |
| 188 | !(t->dev->flags & IFF_UP)) |
| 189 | continue; |
| 190 | |
| 191 | if (!ip_tunnel_key_match(&t->parms, flags, key)) |
| 192 | continue; |
| 193 | |
| 194 | if (t->parms.link == link) |
| 195 | return t; |
| 196 | else if (!cand) |
| 197 | cand = t; |
| 198 | } |
| 199 | |
| 200 | if (flags & TUNNEL_NO_KEY) |
| 201 | goto skip_key_lookup; |
| 202 | |
| 203 | hlist_for_each_entry_rcu(t, head, hash_node) { |
| 204 | if (t->parms.i_key != key || |
| 205 | !(t->dev->flags & IFF_UP)) |
| 206 | continue; |
| 207 | |
| 208 | if (t->parms.link == link) |
| 209 | return t; |
| 210 | else if (!cand) |
| 211 | cand = t; |
| 212 | } |
| 213 | |
| 214 | skip_key_lookup: |
| 215 | if (cand) |
| 216 | return cand; |
| 217 | |
| 218 | if (itn->fb_tunnel_dev && itn->fb_tunnel_dev->flags & IFF_UP) |
| 219 | return netdev_priv(itn->fb_tunnel_dev); |
| 220 | |
| 221 | |
| 222 | return NULL; |
| 223 | } |
| 224 | EXPORT_SYMBOL_GPL(ip_tunnel_lookup); |
| 225 | |
| 226 | static struct hlist_head *ip_bucket(struct ip_tunnel_net *itn, |
| 227 | struct ip_tunnel_parm *parms) |
| 228 | { |
| 229 | unsigned int h; |
| 230 | __be32 remote; |
| 231 | |
| 232 | if (parms->iph.daddr && !ipv4_is_multicast(parms->iph.daddr)) |
| 233 | remote = parms->iph.daddr; |
| 234 | else |
| 235 | remote = 0; |
| 236 | |
| 237 | h = ip_tunnel_hash(itn, parms->i_key, remote); |
| 238 | return &itn->tunnels[h]; |
| 239 | } |
| 240 | |
| 241 | static void ip_tunnel_add(struct ip_tunnel_net *itn, struct ip_tunnel *t) |
| 242 | { |
| 243 | struct hlist_head *head = ip_bucket(itn, &t->parms); |
| 244 | |
| 245 | hlist_add_head_rcu(&t->hash_node, head); |
| 246 | } |
| 247 | |
| 248 | static void ip_tunnel_del(struct ip_tunnel *t) |
| 249 | { |
| 250 | hlist_del_init_rcu(&t->hash_node); |
| 251 | } |
| 252 | |
| 253 | static struct ip_tunnel *ip_tunnel_find(struct ip_tunnel_net *itn, |
| 254 | struct ip_tunnel_parm *parms, |
| 255 | int type) |
| 256 | { |
| 257 | __be32 remote = parms->iph.daddr; |
| 258 | __be32 local = parms->iph.saddr; |
| 259 | __be32 key = parms->i_key; |
| 260 | int link = parms->link; |
| 261 | struct ip_tunnel *t = NULL; |
| 262 | struct hlist_head *head = ip_bucket(itn, parms); |
| 263 | |
| 264 | hlist_for_each_entry_rcu(t, head, hash_node) { |
| 265 | if (local == t->parms.iph.saddr && |
| 266 | remote == t->parms.iph.daddr && |
| 267 | key == t->parms.i_key && |
| 268 | link == t->parms.link && |
| 269 | type == t->dev->type) |
| 270 | break; |
| 271 | } |
| 272 | return t; |
| 273 | } |
| 274 | |
| 275 | static struct net_device *__ip_tunnel_create(struct net *net, |
| 276 | const struct rtnl_link_ops *ops, |
| 277 | struct ip_tunnel_parm *parms) |
| 278 | { |
| 279 | int err; |
| 280 | struct ip_tunnel *tunnel; |
| 281 | struct net_device *dev; |
| 282 | char name[IFNAMSIZ]; |
| 283 | |
| 284 | if (parms->name[0]) |
| 285 | strlcpy(name, parms->name, IFNAMSIZ); |
| 286 | else { |
Pravin B Shelar | 54a5d38 | 2013-03-28 08:21:46 +0000 | [diff] [blame] | 287 | if (strlen(ops->kind) > (IFNAMSIZ - 3)) { |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 288 | err = -E2BIG; |
| 289 | goto failed; |
| 290 | } |
| 291 | strlcpy(name, ops->kind, IFNAMSIZ); |
| 292 | strncat(name, "%d", 2); |
| 293 | } |
| 294 | |
| 295 | ASSERT_RTNL(); |
| 296 | dev = alloc_netdev(ops->priv_size, name, ops->setup); |
| 297 | if (!dev) { |
| 298 | err = -ENOMEM; |
| 299 | goto failed; |
| 300 | } |
| 301 | dev_net_set(dev, net); |
| 302 | |
| 303 | dev->rtnl_link_ops = ops; |
| 304 | |
| 305 | tunnel = netdev_priv(dev); |
| 306 | tunnel->parms = *parms; |
| 307 | |
| 308 | err = register_netdevice(dev); |
| 309 | if (err) |
| 310 | goto failed_free; |
| 311 | |
| 312 | return dev; |
| 313 | |
| 314 | failed_free: |
| 315 | free_netdev(dev); |
| 316 | failed: |
| 317 | return ERR_PTR(err); |
| 318 | } |
| 319 | |
| 320 | static inline struct rtable *ip_route_output_tunnel(struct net *net, |
| 321 | struct flowi4 *fl4, |
| 322 | int proto, |
| 323 | __be32 daddr, __be32 saddr, |
| 324 | __be32 key, __u8 tos, int oif) |
| 325 | { |
| 326 | memset(fl4, 0, sizeof(*fl4)); |
| 327 | fl4->flowi4_oif = oif; |
| 328 | fl4->daddr = daddr; |
| 329 | fl4->saddr = saddr; |
| 330 | fl4->flowi4_tos = tos; |
| 331 | fl4->flowi4_proto = proto; |
| 332 | fl4->fl4_gre_key = key; |
| 333 | return ip_route_output_key(net, fl4); |
| 334 | } |
| 335 | |
| 336 | static int ip_tunnel_bind_dev(struct net_device *dev) |
| 337 | { |
| 338 | struct net_device *tdev = NULL; |
| 339 | struct ip_tunnel *tunnel = netdev_priv(dev); |
| 340 | const struct iphdr *iph; |
| 341 | int hlen = LL_MAX_HEADER; |
| 342 | int mtu = ETH_DATA_LEN; |
| 343 | int t_hlen = tunnel->hlen + sizeof(struct iphdr); |
| 344 | |
| 345 | iph = &tunnel->parms.iph; |
| 346 | |
| 347 | /* Guess output device to choose reasonable mtu and needed_headroom */ |
| 348 | if (iph->daddr) { |
| 349 | struct flowi4 fl4; |
| 350 | struct rtable *rt; |
| 351 | |
| 352 | rt = ip_route_output_tunnel(dev_net(dev), &fl4, |
| 353 | tunnel->parms.iph.protocol, |
| 354 | iph->daddr, iph->saddr, |
| 355 | tunnel->parms.o_key, |
| 356 | RT_TOS(iph->tos), |
| 357 | tunnel->parms.link); |
| 358 | if (!IS_ERR(rt)) { |
| 359 | tdev = rt->dst.dev; |
| 360 | ip_rt_put(rt); |
| 361 | } |
| 362 | if (dev->type != ARPHRD_ETHER) |
| 363 | dev->flags |= IFF_POINTOPOINT; |
| 364 | } |
| 365 | |
| 366 | if (!tdev && tunnel->parms.link) |
| 367 | tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link); |
| 368 | |
| 369 | if (tdev) { |
| 370 | hlen = tdev->hard_header_len + tdev->needed_headroom; |
| 371 | mtu = tdev->mtu; |
| 372 | } |
| 373 | dev->iflink = tunnel->parms.link; |
| 374 | |
| 375 | dev->needed_headroom = t_hlen + hlen; |
| 376 | mtu -= (dev->hard_header_len + t_hlen); |
| 377 | |
| 378 | if (mtu < 68) |
| 379 | mtu = 68; |
| 380 | |
| 381 | return mtu; |
| 382 | } |
| 383 | |
| 384 | static struct ip_tunnel *ip_tunnel_create(struct net *net, |
| 385 | struct ip_tunnel_net *itn, |
| 386 | struct ip_tunnel_parm *parms) |
| 387 | { |
| 388 | struct ip_tunnel *nt, *fbt; |
| 389 | struct net_device *dev; |
| 390 | |
| 391 | BUG_ON(!itn->fb_tunnel_dev); |
| 392 | fbt = netdev_priv(itn->fb_tunnel_dev); |
| 393 | dev = __ip_tunnel_create(net, itn->fb_tunnel_dev->rtnl_link_ops, parms); |
| 394 | if (IS_ERR(dev)) |
| 395 | return NULL; |
| 396 | |
| 397 | dev->mtu = ip_tunnel_bind_dev(dev); |
| 398 | |
| 399 | nt = netdev_priv(dev); |
| 400 | ip_tunnel_add(itn, nt); |
| 401 | return nt; |
| 402 | } |
| 403 | |
| 404 | int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb, |
| 405 | const struct tnl_ptk_info *tpi, bool log_ecn_error) |
| 406 | { |
| 407 | struct pcpu_tstats *tstats; |
| 408 | const struct iphdr *iph = ip_hdr(skb); |
| 409 | int err; |
| 410 | |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 411 | #ifdef CONFIG_NET_IPGRE_BROADCAST |
| 412 | if (ipv4_is_multicast(iph->daddr)) { |
| 413 | /* Looped back packet, drop it! */ |
| 414 | if (rt_is_output_route(skb_rtable(skb))) |
| 415 | goto drop; |
| 416 | tunnel->dev->stats.multicast++; |
| 417 | skb->pkt_type = PACKET_BROADCAST; |
| 418 | } |
| 419 | #endif |
| 420 | |
| 421 | if ((!(tpi->flags&TUNNEL_CSUM) && (tunnel->parms.i_flags&TUNNEL_CSUM)) || |
| 422 | ((tpi->flags&TUNNEL_CSUM) && !(tunnel->parms.i_flags&TUNNEL_CSUM))) { |
| 423 | tunnel->dev->stats.rx_crc_errors++; |
| 424 | tunnel->dev->stats.rx_errors++; |
| 425 | goto drop; |
| 426 | } |
| 427 | |
| 428 | if (tunnel->parms.i_flags&TUNNEL_SEQ) { |
| 429 | if (!(tpi->flags&TUNNEL_SEQ) || |
| 430 | (tunnel->i_seqno && (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) { |
| 431 | tunnel->dev->stats.rx_fifo_errors++; |
| 432 | tunnel->dev->stats.rx_errors++; |
| 433 | goto drop; |
| 434 | } |
| 435 | tunnel->i_seqno = ntohl(tpi->seq) + 1; |
| 436 | } |
| 437 | |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 438 | err = IP_ECN_decapsulate(iph, skb); |
| 439 | if (unlikely(err)) { |
| 440 | if (log_ecn_error) |
| 441 | net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", |
| 442 | &iph->saddr, iph->tos); |
| 443 | if (err > 1) { |
| 444 | ++tunnel->dev->stats.rx_frame_errors; |
| 445 | ++tunnel->dev->stats.rx_errors; |
| 446 | goto drop; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | tstats = this_cpu_ptr(tunnel->dev->tstats); |
| 451 | u64_stats_update_begin(&tstats->syncp); |
| 452 | tstats->rx_packets++; |
| 453 | tstats->rx_bytes += skb->len; |
| 454 | u64_stats_update_end(&tstats->syncp); |
| 455 | |
Pravin B Shelar | 3d7b46c | 2013-06-17 17:50:02 -0700 | [diff] [blame] | 456 | if (tunnel->dev->type == ARPHRD_ETHER) { |
| 457 | skb->protocol = eth_type_trans(skb, tunnel->dev); |
| 458 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); |
| 459 | } else { |
| 460 | skb->dev = tunnel->dev; |
| 461 | } |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 462 | gro_cells_receive(&tunnel->gro_cells, skb); |
| 463 | return 0; |
| 464 | |
| 465 | drop: |
| 466 | kfree_skb(skb); |
| 467 | return 0; |
| 468 | } |
| 469 | EXPORT_SYMBOL_GPL(ip_tunnel_rcv); |
| 470 | |
| 471 | void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev, |
Nicolas Dichtel | bf3d6a8 | 2013-05-27 23:48:15 +0000 | [diff] [blame] | 472 | const struct iphdr *tnl_params, const u8 protocol) |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 473 | { |
| 474 | struct ip_tunnel *tunnel = netdev_priv(dev); |
| 475 | const struct iphdr *inner_iph; |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 476 | struct flowi4 fl4; |
| 477 | u8 tos, ttl; |
| 478 | __be16 df; |
| 479 | struct rtable *rt; /* Route to the other host */ |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 480 | unsigned int max_headroom; /* The extra header space needed */ |
| 481 | __be32 dst; |
| 482 | int mtu; |
Pravin B Shelar | 0e6fbc5 | 2013-06-17 17:49:56 -0700 | [diff] [blame] | 483 | int err; |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 484 | |
| 485 | inner_iph = (const struct iphdr *)skb_inner_network_header(skb); |
| 486 | |
| 487 | dst = tnl_params->daddr; |
| 488 | if (dst == 0) { |
| 489 | /* NBMA tunnel */ |
| 490 | |
| 491 | if (skb_dst(skb) == NULL) { |
| 492 | dev->stats.tx_fifo_errors++; |
| 493 | goto tx_error; |
| 494 | } |
| 495 | |
| 496 | if (skb->protocol == htons(ETH_P_IP)) { |
| 497 | rt = skb_rtable(skb); |
| 498 | dst = rt_nexthop(rt, inner_iph->daddr); |
| 499 | } |
| 500 | #if IS_ENABLED(CONFIG_IPV6) |
| 501 | else if (skb->protocol == htons(ETH_P_IPV6)) { |
| 502 | const struct in6_addr *addr6; |
| 503 | struct neighbour *neigh; |
| 504 | bool do_tx_error_icmp; |
| 505 | int addr_type; |
| 506 | |
| 507 | neigh = dst_neigh_lookup(skb_dst(skb), |
| 508 | &ipv6_hdr(skb)->daddr); |
| 509 | if (neigh == NULL) |
| 510 | goto tx_error; |
| 511 | |
| 512 | addr6 = (const struct in6_addr *)&neigh->primary_key; |
| 513 | addr_type = ipv6_addr_type(addr6); |
| 514 | |
| 515 | if (addr_type == IPV6_ADDR_ANY) { |
| 516 | addr6 = &ipv6_hdr(skb)->daddr; |
| 517 | addr_type = ipv6_addr_type(addr6); |
| 518 | } |
| 519 | |
| 520 | if ((addr_type & IPV6_ADDR_COMPATv4) == 0) |
| 521 | do_tx_error_icmp = true; |
| 522 | else { |
| 523 | do_tx_error_icmp = false; |
| 524 | dst = addr6->s6_addr32[3]; |
| 525 | } |
| 526 | neigh_release(neigh); |
| 527 | if (do_tx_error_icmp) |
| 528 | goto tx_error_icmp; |
| 529 | } |
| 530 | #endif |
| 531 | else |
| 532 | goto tx_error; |
| 533 | } |
| 534 | |
| 535 | tos = tnl_params->tos; |
| 536 | if (tos & 0x1) { |
| 537 | tos &= ~0x1; |
| 538 | if (skb->protocol == htons(ETH_P_IP)) |
| 539 | tos = inner_iph->tos; |
| 540 | else if (skb->protocol == htons(ETH_P_IPV6)) |
| 541 | tos = ipv6_get_dsfield((const struct ipv6hdr *)inner_iph); |
| 542 | } |
| 543 | |
| 544 | rt = ip_route_output_tunnel(dev_net(dev), &fl4, |
| 545 | tunnel->parms.iph.protocol, |
| 546 | dst, tnl_params->saddr, |
| 547 | tunnel->parms.o_key, |
| 548 | RT_TOS(tos), |
| 549 | tunnel->parms.link); |
| 550 | if (IS_ERR(rt)) { |
| 551 | dev->stats.tx_carrier_errors++; |
| 552 | goto tx_error; |
| 553 | } |
Pravin B Shelar | 0e6fbc5 | 2013-06-17 17:49:56 -0700 | [diff] [blame] | 554 | if (rt->dst.dev == dev) { |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 555 | ip_rt_put(rt); |
| 556 | dev->stats.collisions++; |
| 557 | goto tx_error; |
| 558 | } |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 559 | df = tnl_params->frag_off; |
| 560 | |
| 561 | if (df) |
| 562 | mtu = dst_mtu(&rt->dst) - dev->hard_header_len |
| 563 | - sizeof(struct iphdr); |
| 564 | else |
| 565 | mtu = skb_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu; |
| 566 | |
| 567 | if (skb_dst(skb)) |
| 568 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu); |
| 569 | |
| 570 | if (skb->protocol == htons(ETH_P_IP)) { |
| 571 | df |= (inner_iph->frag_off&htons(IP_DF)); |
| 572 | |
| 573 | if (!skb_is_gso(skb) && |
| 574 | (inner_iph->frag_off&htons(IP_DF)) && |
| 575 | mtu < ntohs(inner_iph->tot_len)) { |
Pravin B Shelar | 0e6fbc5 | 2013-06-17 17:49:56 -0700 | [diff] [blame] | 576 | memset(IPCB(skb), 0, sizeof(*IPCB(skb))); |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 577 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu)); |
| 578 | ip_rt_put(rt); |
| 579 | goto tx_error; |
| 580 | } |
| 581 | } |
| 582 | #if IS_ENABLED(CONFIG_IPV6) |
| 583 | else if (skb->protocol == htons(ETH_P_IPV6)) { |
| 584 | struct rt6_info *rt6 = (struct rt6_info *)skb_dst(skb); |
| 585 | |
| 586 | if (rt6 && mtu < dst_mtu(skb_dst(skb)) && |
| 587 | mtu >= IPV6_MIN_MTU) { |
| 588 | if ((tunnel->parms.iph.daddr && |
| 589 | !ipv4_is_multicast(tunnel->parms.iph.daddr)) || |
| 590 | rt6->rt6i_dst.plen == 128) { |
| 591 | rt6->rt6i_flags |= RTF_MODIFIED; |
| 592 | dst_metric_set(skb_dst(skb), RTAX_MTU, mtu); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | if (!skb_is_gso(skb) && mtu >= IPV6_MIN_MTU && |
| 597 | mtu < skb->len) { |
| 598 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
| 599 | ip_rt_put(rt); |
| 600 | goto tx_error; |
| 601 | } |
| 602 | } |
| 603 | #endif |
| 604 | |
| 605 | if (tunnel->err_count > 0) { |
| 606 | if (time_before(jiffies, |
| 607 | tunnel->err_time + IPTUNNEL_ERR_TIMEO)) { |
| 608 | tunnel->err_count--; |
| 609 | |
| 610 | dst_link_failure(skb); |
| 611 | } else |
| 612 | tunnel->err_count = 0; |
| 613 | } |
| 614 | |
| 615 | ttl = tnl_params->ttl; |
| 616 | if (ttl == 0) { |
| 617 | if (skb->protocol == htons(ETH_P_IP)) |
| 618 | ttl = inner_iph->ttl; |
| 619 | #if IS_ENABLED(CONFIG_IPV6) |
| 620 | else if (skb->protocol == htons(ETH_P_IPV6)) |
| 621 | ttl = ((const struct ipv6hdr *)inner_iph)->hop_limit; |
| 622 | #endif |
| 623 | else |
| 624 | ttl = ip4_dst_hoplimit(&rt->dst); |
| 625 | } |
| 626 | |
Pravin B Shelar | 0e6fbc5 | 2013-06-17 17:49:56 -0700 | [diff] [blame] | 627 | max_headroom = LL_RESERVED_SPACE(rt->dst.dev) + sizeof(struct iphdr) |
| 628 | + rt->dst.header_len; |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 629 | if (max_headroom > dev->needed_headroom) { |
| 630 | dev->needed_headroom = max_headroom; |
| 631 | if (skb_cow_head(skb, dev->needed_headroom)) { |
| 632 | dev->stats.tx_dropped++; |
| 633 | dev_kfree_skb(skb); |
| 634 | return; |
| 635 | } |
| 636 | } |
| 637 | |
Pravin B Shelar | 0e6fbc5 | 2013-06-17 17:49:56 -0700 | [diff] [blame] | 638 | err = iptunnel_xmit(dev_net(dev), rt, skb, |
| 639 | fl4.saddr, fl4.daddr, protocol, |
| 640 | ip_tunnel_ecn_encap(tos, inner_iph, skb), ttl, df); |
| 641 | iptunnel_xmit_stats(err, &dev->stats, dev->tstats); |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 642 | |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 643 | return; |
| 644 | |
| 645 | #if IS_ENABLED(CONFIG_IPV6) |
| 646 | tx_error_icmp: |
| 647 | dst_link_failure(skb); |
| 648 | #endif |
| 649 | tx_error: |
| 650 | dev->stats.tx_errors++; |
| 651 | dev_kfree_skb(skb); |
| 652 | } |
| 653 | EXPORT_SYMBOL_GPL(ip_tunnel_xmit); |
| 654 | |
| 655 | static void ip_tunnel_update(struct ip_tunnel_net *itn, |
| 656 | struct ip_tunnel *t, |
| 657 | struct net_device *dev, |
| 658 | struct ip_tunnel_parm *p, |
| 659 | bool set_mtu) |
| 660 | { |
| 661 | ip_tunnel_del(t); |
| 662 | t->parms.iph.saddr = p->iph.saddr; |
| 663 | t->parms.iph.daddr = p->iph.daddr; |
| 664 | t->parms.i_key = p->i_key; |
| 665 | t->parms.o_key = p->o_key; |
| 666 | if (dev->type != ARPHRD_ETHER) { |
| 667 | memcpy(dev->dev_addr, &p->iph.saddr, 4); |
| 668 | memcpy(dev->broadcast, &p->iph.daddr, 4); |
| 669 | } |
| 670 | ip_tunnel_add(itn, t); |
| 671 | |
| 672 | t->parms.iph.ttl = p->iph.ttl; |
| 673 | t->parms.iph.tos = p->iph.tos; |
| 674 | t->parms.iph.frag_off = p->iph.frag_off; |
| 675 | |
| 676 | if (t->parms.link != p->link) { |
| 677 | int mtu; |
| 678 | |
| 679 | t->parms.link = p->link; |
| 680 | mtu = ip_tunnel_bind_dev(dev); |
| 681 | if (set_mtu) |
| 682 | dev->mtu = mtu; |
| 683 | } |
| 684 | netdev_state_change(dev); |
| 685 | } |
| 686 | |
| 687 | int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd) |
| 688 | { |
| 689 | int err = 0; |
| 690 | struct ip_tunnel *t; |
| 691 | struct net *net = dev_net(dev); |
| 692 | struct ip_tunnel *tunnel = netdev_priv(dev); |
| 693 | struct ip_tunnel_net *itn = net_generic(net, tunnel->ip_tnl_net_id); |
| 694 | |
| 695 | BUG_ON(!itn->fb_tunnel_dev); |
| 696 | switch (cmd) { |
| 697 | case SIOCGETTUNNEL: |
| 698 | t = NULL; |
| 699 | if (dev == itn->fb_tunnel_dev) |
| 700 | t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type); |
| 701 | if (t == NULL) |
| 702 | t = netdev_priv(dev); |
| 703 | memcpy(p, &t->parms, sizeof(*p)); |
| 704 | break; |
| 705 | |
| 706 | case SIOCADDTUNNEL: |
| 707 | case SIOCCHGTUNNEL: |
| 708 | err = -EPERM; |
| 709 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
| 710 | goto done; |
| 711 | if (p->iph.ttl) |
| 712 | p->iph.frag_off |= htons(IP_DF); |
| 713 | if (!(p->i_flags&TUNNEL_KEY)) |
| 714 | p->i_key = 0; |
| 715 | if (!(p->o_flags&TUNNEL_KEY)) |
| 716 | p->o_key = 0; |
| 717 | |
| 718 | t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type); |
| 719 | |
| 720 | if (!t && (cmd == SIOCADDTUNNEL)) |
| 721 | t = ip_tunnel_create(net, itn, p); |
| 722 | |
| 723 | if (dev != itn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { |
| 724 | if (t != NULL) { |
| 725 | if (t->dev != dev) { |
| 726 | err = -EEXIST; |
| 727 | break; |
| 728 | } |
| 729 | } else { |
| 730 | unsigned int nflags = 0; |
| 731 | |
| 732 | if (ipv4_is_multicast(p->iph.daddr)) |
| 733 | nflags = IFF_BROADCAST; |
| 734 | else if (p->iph.daddr) |
| 735 | nflags = IFF_POINTOPOINT; |
| 736 | |
| 737 | if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) { |
| 738 | err = -EINVAL; |
| 739 | break; |
| 740 | } |
| 741 | |
| 742 | t = netdev_priv(dev); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | if (t) { |
| 747 | err = 0; |
| 748 | ip_tunnel_update(itn, t, dev, p, true); |
| 749 | } else |
| 750 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); |
| 751 | break; |
| 752 | |
| 753 | case SIOCDELTUNNEL: |
| 754 | err = -EPERM; |
| 755 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
| 756 | goto done; |
| 757 | |
| 758 | if (dev == itn->fb_tunnel_dev) { |
| 759 | err = -ENOENT; |
| 760 | t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type); |
| 761 | if (t == NULL) |
| 762 | goto done; |
| 763 | err = -EPERM; |
| 764 | if (t == netdev_priv(itn->fb_tunnel_dev)) |
| 765 | goto done; |
| 766 | dev = t->dev; |
| 767 | } |
| 768 | unregister_netdevice(dev); |
| 769 | err = 0; |
| 770 | break; |
| 771 | |
| 772 | default: |
| 773 | err = -EINVAL; |
| 774 | } |
| 775 | |
| 776 | done: |
| 777 | return err; |
| 778 | } |
| 779 | EXPORT_SYMBOL_GPL(ip_tunnel_ioctl); |
| 780 | |
| 781 | int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu) |
| 782 | { |
| 783 | struct ip_tunnel *tunnel = netdev_priv(dev); |
| 784 | int t_hlen = tunnel->hlen + sizeof(struct iphdr); |
| 785 | |
| 786 | if (new_mtu < 68 || |
| 787 | new_mtu > 0xFFF8 - dev->hard_header_len - t_hlen) |
| 788 | return -EINVAL; |
| 789 | dev->mtu = new_mtu; |
| 790 | return 0; |
| 791 | } |
| 792 | EXPORT_SYMBOL_GPL(ip_tunnel_change_mtu); |
| 793 | |
| 794 | static void ip_tunnel_dev_free(struct net_device *dev) |
| 795 | { |
| 796 | struct ip_tunnel *tunnel = netdev_priv(dev); |
| 797 | |
| 798 | gro_cells_destroy(&tunnel->gro_cells); |
| 799 | free_percpu(dev->tstats); |
| 800 | free_netdev(dev); |
| 801 | } |
| 802 | |
| 803 | void ip_tunnel_dellink(struct net_device *dev, struct list_head *head) |
| 804 | { |
| 805 | struct net *net = dev_net(dev); |
| 806 | struct ip_tunnel *tunnel = netdev_priv(dev); |
| 807 | struct ip_tunnel_net *itn; |
| 808 | |
| 809 | itn = net_generic(net, tunnel->ip_tnl_net_id); |
| 810 | |
| 811 | if (itn->fb_tunnel_dev != dev) { |
| 812 | ip_tunnel_del(netdev_priv(dev)); |
| 813 | unregister_netdevice_queue(dev, head); |
| 814 | } |
| 815 | } |
| 816 | EXPORT_SYMBOL_GPL(ip_tunnel_dellink); |
| 817 | |
Eric Dumazet | d3b6f61 | 2013-06-07 13:26:05 -0700 | [diff] [blame] | 818 | int ip_tunnel_init_net(struct net *net, int ip_tnl_net_id, |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 819 | struct rtnl_link_ops *ops, char *devname) |
| 820 | { |
| 821 | struct ip_tunnel_net *itn = net_generic(net, ip_tnl_net_id); |
| 822 | struct ip_tunnel_parm parms; |
| 823 | |
| 824 | itn->tunnels = kzalloc(IP_TNL_HASH_SIZE * sizeof(struct hlist_head), GFP_KERNEL); |
| 825 | if (!itn->tunnels) |
| 826 | return -ENOMEM; |
| 827 | |
| 828 | if (!ops) { |
| 829 | itn->fb_tunnel_dev = NULL; |
| 830 | return 0; |
| 831 | } |
| 832 | memset(&parms, 0, sizeof(parms)); |
| 833 | if (devname) |
| 834 | strlcpy(parms.name, devname, IFNAMSIZ); |
| 835 | |
| 836 | rtnl_lock(); |
| 837 | itn->fb_tunnel_dev = __ip_tunnel_create(net, ops, &parms); |
| 838 | rtnl_unlock(); |
| 839 | if (IS_ERR(itn->fb_tunnel_dev)) { |
| 840 | kfree(itn->tunnels); |
| 841 | return PTR_ERR(itn->fb_tunnel_dev); |
| 842 | } |
| 843 | |
| 844 | return 0; |
| 845 | } |
| 846 | EXPORT_SYMBOL_GPL(ip_tunnel_init_net); |
| 847 | |
| 848 | static void ip_tunnel_destroy(struct ip_tunnel_net *itn, struct list_head *head) |
| 849 | { |
| 850 | int h; |
| 851 | |
| 852 | for (h = 0; h < IP_TNL_HASH_SIZE; h++) { |
| 853 | struct ip_tunnel *t; |
| 854 | struct hlist_node *n; |
| 855 | struct hlist_head *thead = &itn->tunnels[h]; |
| 856 | |
| 857 | hlist_for_each_entry_safe(t, n, thead, hash_node) |
| 858 | unregister_netdevice_queue(t->dev, head); |
| 859 | } |
| 860 | if (itn->fb_tunnel_dev) |
| 861 | unregister_netdevice_queue(itn->fb_tunnel_dev, head); |
| 862 | } |
| 863 | |
Eric Dumazet | d3b6f61 | 2013-06-07 13:26:05 -0700 | [diff] [blame] | 864 | void ip_tunnel_delete_net(struct ip_tunnel_net *itn) |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 865 | { |
| 866 | LIST_HEAD(list); |
| 867 | |
| 868 | rtnl_lock(); |
| 869 | ip_tunnel_destroy(itn, &list); |
| 870 | unregister_netdevice_many(&list); |
| 871 | rtnl_unlock(); |
| 872 | kfree(itn->tunnels); |
| 873 | } |
| 874 | EXPORT_SYMBOL_GPL(ip_tunnel_delete_net); |
| 875 | |
| 876 | int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[], |
| 877 | struct ip_tunnel_parm *p) |
| 878 | { |
| 879 | struct ip_tunnel *nt; |
| 880 | struct net *net = dev_net(dev); |
| 881 | struct ip_tunnel_net *itn; |
| 882 | int mtu; |
| 883 | int err; |
| 884 | |
| 885 | nt = netdev_priv(dev); |
| 886 | itn = net_generic(net, nt->ip_tnl_net_id); |
| 887 | |
| 888 | if (ip_tunnel_find(itn, p, dev->type)) |
| 889 | return -EEXIST; |
| 890 | |
| 891 | nt->parms = *p; |
| 892 | err = register_netdevice(dev); |
| 893 | if (err) |
| 894 | goto out; |
| 895 | |
| 896 | if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS]) |
| 897 | eth_hw_addr_random(dev); |
| 898 | |
| 899 | mtu = ip_tunnel_bind_dev(dev); |
| 900 | if (!tb[IFLA_MTU]) |
| 901 | dev->mtu = mtu; |
| 902 | |
| 903 | ip_tunnel_add(itn, nt); |
| 904 | |
| 905 | out: |
| 906 | return err; |
| 907 | } |
| 908 | EXPORT_SYMBOL_GPL(ip_tunnel_newlink); |
| 909 | |
| 910 | int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[], |
| 911 | struct ip_tunnel_parm *p) |
| 912 | { |
| 913 | struct ip_tunnel *t, *nt; |
| 914 | struct net *net = dev_net(dev); |
| 915 | struct ip_tunnel *tunnel = netdev_priv(dev); |
| 916 | struct ip_tunnel_net *itn = net_generic(net, tunnel->ip_tnl_net_id); |
| 917 | |
| 918 | if (dev == itn->fb_tunnel_dev) |
| 919 | return -EINVAL; |
| 920 | |
| 921 | nt = netdev_priv(dev); |
| 922 | |
| 923 | t = ip_tunnel_find(itn, p, dev->type); |
| 924 | |
| 925 | if (t) { |
| 926 | if (t->dev != dev) |
| 927 | return -EEXIST; |
| 928 | } else { |
| 929 | t = nt; |
| 930 | |
| 931 | if (dev->type != ARPHRD_ETHER) { |
| 932 | unsigned int nflags = 0; |
| 933 | |
| 934 | if (ipv4_is_multicast(p->iph.daddr)) |
| 935 | nflags = IFF_BROADCAST; |
| 936 | else if (p->iph.daddr) |
| 937 | nflags = IFF_POINTOPOINT; |
| 938 | |
| 939 | if ((dev->flags ^ nflags) & |
| 940 | (IFF_POINTOPOINT | IFF_BROADCAST)) |
| 941 | return -EINVAL; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | ip_tunnel_update(itn, t, dev, p, !tb[IFLA_MTU]); |
| 946 | return 0; |
| 947 | } |
| 948 | EXPORT_SYMBOL_GPL(ip_tunnel_changelink); |
| 949 | |
| 950 | int ip_tunnel_init(struct net_device *dev) |
| 951 | { |
| 952 | struct ip_tunnel *tunnel = netdev_priv(dev); |
| 953 | struct iphdr *iph = &tunnel->parms.iph; |
| 954 | int err; |
| 955 | |
| 956 | dev->destructor = ip_tunnel_dev_free; |
| 957 | dev->tstats = alloc_percpu(struct pcpu_tstats); |
| 958 | if (!dev->tstats) |
| 959 | return -ENOMEM; |
| 960 | |
| 961 | err = gro_cells_init(&tunnel->gro_cells, dev); |
| 962 | if (err) { |
| 963 | free_percpu(dev->tstats); |
| 964 | return err; |
| 965 | } |
| 966 | |
| 967 | tunnel->dev = dev; |
| 968 | strcpy(tunnel->parms.name, dev->name); |
| 969 | iph->version = 4; |
| 970 | iph->ihl = 5; |
| 971 | |
| 972 | return 0; |
| 973 | } |
| 974 | EXPORT_SYMBOL_GPL(ip_tunnel_init); |
| 975 | |
| 976 | void ip_tunnel_uninit(struct net_device *dev) |
| 977 | { |
| 978 | struct net *net = dev_net(dev); |
| 979 | struct ip_tunnel *tunnel = netdev_priv(dev); |
| 980 | struct ip_tunnel_net *itn; |
| 981 | |
| 982 | itn = net_generic(net, tunnel->ip_tnl_net_id); |
| 983 | /* fb_tunnel_dev will be unregisted in net-exit call. */ |
| 984 | if (itn->fb_tunnel_dev != dev) |
| 985 | ip_tunnel_del(netdev_priv(dev)); |
| 986 | } |
| 987 | EXPORT_SYMBOL_GPL(ip_tunnel_uninit); |
| 988 | |
| 989 | /* Do least required initialization, rest of init is done in tunnel_init call */ |
| 990 | void ip_tunnel_setup(struct net_device *dev, int net_id) |
| 991 | { |
| 992 | struct ip_tunnel *tunnel = netdev_priv(dev); |
| 993 | tunnel->ip_tnl_net_id = net_id; |
| 994 | } |
| 995 | EXPORT_SYMBOL_GPL(ip_tunnel_setup); |
| 996 | |
| 997 | MODULE_LICENSE("GPL"); |