xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * GRE over IPv6 protocol decoder. |
| 3 | * |
| 4 | * Authors: Dmitry Kozlov (xeb@mail.ru) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
| 15 | #include <linux/capability.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/uaccess.h> |
| 21 | #include <linux/skbuff.h> |
| 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/in.h> |
| 24 | #include <linux/tcp.h> |
| 25 | #include <linux/udp.h> |
| 26 | #include <linux/if_arp.h> |
| 27 | #include <linux/mroute.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/in6.h> |
| 30 | #include <linux/inetdevice.h> |
| 31 | #include <linux/igmp.h> |
| 32 | #include <linux/netfilter_ipv4.h> |
| 33 | #include <linux/etherdevice.h> |
| 34 | #include <linux/if_ether.h> |
| 35 | #include <linux/hash.h> |
| 36 | #include <linux/if_tunnel.h> |
| 37 | #include <linux/ip6_tunnel.h> |
| 38 | |
| 39 | #include <net/sock.h> |
| 40 | #include <net/ip.h> |
| 41 | #include <net/icmp.h> |
| 42 | #include <net/protocol.h> |
| 43 | #include <net/addrconf.h> |
| 44 | #include <net/arp.h> |
| 45 | #include <net/checksum.h> |
| 46 | #include <net/dsfield.h> |
| 47 | #include <net/inet_ecn.h> |
| 48 | #include <net/xfrm.h> |
| 49 | #include <net/net_namespace.h> |
| 50 | #include <net/netns/generic.h> |
| 51 | #include <net/rtnetlink.h> |
| 52 | |
| 53 | #include <net/ipv6.h> |
| 54 | #include <net/ip6_fib.h> |
| 55 | #include <net/ip6_route.h> |
| 56 | #include <net/ip6_tunnel.h> |
| 57 | |
| 58 | |
| 59 | #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK) |
| 60 | #define IPV6_TCLASS_SHIFT 20 |
| 61 | |
| 62 | #define HASH_SIZE_SHIFT 5 |
| 63 | #define HASH_SIZE (1 << HASH_SIZE_SHIFT) |
| 64 | |
| 65 | static int ip6gre_net_id __read_mostly; |
| 66 | struct ip6gre_net { |
| 67 | struct ip6_tnl __rcu *tunnels[4][HASH_SIZE]; |
| 68 | |
| 69 | struct net_device *fb_tunnel_dev; |
| 70 | }; |
| 71 | |
| 72 | static struct rtnl_link_ops ip6gre_link_ops __read_mostly; |
| 73 | static int ip6gre_tunnel_init(struct net_device *dev); |
| 74 | static void ip6gre_tunnel_setup(struct net_device *dev); |
| 75 | static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t); |
| 76 | static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu); |
| 77 | |
| 78 | /* Tunnel hash table */ |
| 79 | |
| 80 | /* |
| 81 | 4 hash tables: |
| 82 | |
| 83 | 3: (remote,local) |
| 84 | 2: (remote,*) |
| 85 | 1: (*,local) |
| 86 | 0: (*,*) |
| 87 | |
| 88 | We require exact key match i.e. if a key is present in packet |
| 89 | it will match only tunnel with the same key; if it is not present, |
| 90 | it will match only keyless tunnel. |
| 91 | |
| 92 | All keysless packets, if not matched configured keyless tunnels |
| 93 | will match fallback tunnel. |
| 94 | */ |
| 95 | |
| 96 | #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(HASH_SIZE - 1)) |
| 97 | static u32 HASH_ADDR(const struct in6_addr *addr) |
| 98 | { |
| 99 | u32 hash = ipv6_addr_hash(addr); |
| 100 | |
| 101 | return hash_32(hash, HASH_SIZE_SHIFT); |
| 102 | } |
| 103 | |
| 104 | #define tunnels_r_l tunnels[3] |
| 105 | #define tunnels_r tunnels[2] |
| 106 | #define tunnels_l tunnels[1] |
| 107 | #define tunnels_wc tunnels[0] |
| 108 | /* |
| 109 | * Locking : hash tables are protected by RCU and RTNL |
| 110 | */ |
| 111 | |
| 112 | #define for_each_ip_tunnel_rcu(start) \ |
| 113 | for (t = rcu_dereference(start); t; t = rcu_dereference(t->next)) |
| 114 | |
| 115 | /* often modified stats are per cpu, other are shared (netdev->stats) */ |
| 116 | struct pcpu_tstats { |
| 117 | u64 rx_packets; |
| 118 | u64 rx_bytes; |
| 119 | u64 tx_packets; |
| 120 | u64 tx_bytes; |
| 121 | struct u64_stats_sync syncp; |
| 122 | }; |
| 123 | |
| 124 | static struct rtnl_link_stats64 *ip6gre_get_stats64(struct net_device *dev, |
| 125 | struct rtnl_link_stats64 *tot) |
| 126 | { |
| 127 | int i; |
| 128 | |
| 129 | for_each_possible_cpu(i) { |
| 130 | const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i); |
| 131 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; |
| 132 | unsigned int start; |
| 133 | |
| 134 | do { |
| 135 | start = u64_stats_fetch_begin_bh(&tstats->syncp); |
| 136 | rx_packets = tstats->rx_packets; |
| 137 | tx_packets = tstats->tx_packets; |
| 138 | rx_bytes = tstats->rx_bytes; |
| 139 | tx_bytes = tstats->tx_bytes; |
| 140 | } while (u64_stats_fetch_retry_bh(&tstats->syncp, start)); |
| 141 | |
| 142 | tot->rx_packets += rx_packets; |
| 143 | tot->tx_packets += tx_packets; |
| 144 | tot->rx_bytes += rx_bytes; |
| 145 | tot->tx_bytes += tx_bytes; |
| 146 | } |
| 147 | |
| 148 | tot->multicast = dev->stats.multicast; |
| 149 | tot->rx_crc_errors = dev->stats.rx_crc_errors; |
| 150 | tot->rx_fifo_errors = dev->stats.rx_fifo_errors; |
| 151 | tot->rx_length_errors = dev->stats.rx_length_errors; |
| 152 | tot->rx_errors = dev->stats.rx_errors; |
| 153 | tot->tx_fifo_errors = dev->stats.tx_fifo_errors; |
| 154 | tot->tx_carrier_errors = dev->stats.tx_carrier_errors; |
| 155 | tot->tx_dropped = dev->stats.tx_dropped; |
| 156 | tot->tx_aborted_errors = dev->stats.tx_aborted_errors; |
| 157 | tot->tx_errors = dev->stats.tx_errors; |
| 158 | |
| 159 | return tot; |
| 160 | } |
| 161 | |
| 162 | /* Given src, dst and key, find appropriate for input tunnel. */ |
| 163 | |
| 164 | static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev, |
| 165 | const struct in6_addr *remote, const struct in6_addr *local, |
| 166 | __be32 key, __be16 gre_proto) |
| 167 | { |
| 168 | struct net *net = dev_net(dev); |
| 169 | int link = dev->ifindex; |
| 170 | unsigned int h0 = HASH_ADDR(remote); |
| 171 | unsigned int h1 = HASH_KEY(key); |
| 172 | struct ip6_tnl *t, *cand = NULL; |
| 173 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 174 | int dev_type = (gre_proto == htons(ETH_P_TEB)) ? |
| 175 | ARPHRD_ETHER : ARPHRD_IP6GRE; |
| 176 | int score, cand_score = 4; |
| 177 | |
| 178 | for_each_ip_tunnel_rcu(ign->tunnels_r_l[h0 ^ h1]) { |
| 179 | if (!ipv6_addr_equal(local, &t->parms.laddr) || |
| 180 | !ipv6_addr_equal(remote, &t->parms.raddr) || |
| 181 | key != t->parms.i_key || |
| 182 | !(t->dev->flags & IFF_UP)) |
| 183 | continue; |
| 184 | |
| 185 | if (t->dev->type != ARPHRD_IP6GRE && |
| 186 | t->dev->type != dev_type) |
| 187 | continue; |
| 188 | |
| 189 | score = 0; |
| 190 | if (t->parms.link != link) |
| 191 | score |= 1; |
| 192 | if (t->dev->type != dev_type) |
| 193 | score |= 2; |
| 194 | if (score == 0) |
| 195 | return t; |
| 196 | |
| 197 | if (score < cand_score) { |
| 198 | cand = t; |
| 199 | cand_score = score; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | for_each_ip_tunnel_rcu(ign->tunnels_r[h0 ^ h1]) { |
| 204 | if (!ipv6_addr_equal(remote, &t->parms.raddr) || |
| 205 | key != t->parms.i_key || |
| 206 | !(t->dev->flags & IFF_UP)) |
| 207 | continue; |
| 208 | |
| 209 | if (t->dev->type != ARPHRD_IP6GRE && |
| 210 | t->dev->type != dev_type) |
| 211 | continue; |
| 212 | |
| 213 | score = 0; |
| 214 | if (t->parms.link != link) |
| 215 | score |= 1; |
| 216 | if (t->dev->type != dev_type) |
| 217 | score |= 2; |
| 218 | if (score == 0) |
| 219 | return t; |
| 220 | |
| 221 | if (score < cand_score) { |
| 222 | cand = t; |
| 223 | cand_score = score; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | for_each_ip_tunnel_rcu(ign->tunnels_l[h1]) { |
| 228 | if ((!ipv6_addr_equal(local, &t->parms.laddr) && |
| 229 | (!ipv6_addr_equal(local, &t->parms.raddr) || |
| 230 | !ipv6_addr_is_multicast(local))) || |
| 231 | key != t->parms.i_key || |
| 232 | !(t->dev->flags & IFF_UP)) |
| 233 | continue; |
| 234 | |
| 235 | if (t->dev->type != ARPHRD_IP6GRE && |
| 236 | t->dev->type != dev_type) |
| 237 | continue; |
| 238 | |
| 239 | score = 0; |
| 240 | if (t->parms.link != link) |
| 241 | score |= 1; |
| 242 | if (t->dev->type != dev_type) |
| 243 | score |= 2; |
| 244 | if (score == 0) |
| 245 | return t; |
| 246 | |
| 247 | if (score < cand_score) { |
| 248 | cand = t; |
| 249 | cand_score = score; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | for_each_ip_tunnel_rcu(ign->tunnels_wc[h1]) { |
| 254 | if (t->parms.i_key != key || |
| 255 | !(t->dev->flags & IFF_UP)) |
| 256 | continue; |
| 257 | |
| 258 | if (t->dev->type != ARPHRD_IP6GRE && |
| 259 | t->dev->type != dev_type) |
| 260 | continue; |
| 261 | |
| 262 | score = 0; |
| 263 | if (t->parms.link != link) |
| 264 | score |= 1; |
| 265 | if (t->dev->type != dev_type) |
| 266 | score |= 2; |
| 267 | if (score == 0) |
| 268 | return t; |
| 269 | |
| 270 | if (score < cand_score) { |
| 271 | cand = t; |
| 272 | cand_score = score; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | if (cand != NULL) |
| 277 | return cand; |
| 278 | |
| 279 | dev = ign->fb_tunnel_dev; |
| 280 | if (dev->flags & IFF_UP) |
| 281 | return netdev_priv(dev); |
| 282 | |
| 283 | return NULL; |
| 284 | } |
| 285 | |
| 286 | static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign, |
| 287 | const struct __ip6_tnl_parm *p) |
| 288 | { |
| 289 | const struct in6_addr *remote = &p->raddr; |
| 290 | const struct in6_addr *local = &p->laddr; |
| 291 | unsigned int h = HASH_KEY(p->i_key); |
| 292 | int prio = 0; |
| 293 | |
| 294 | if (!ipv6_addr_any(local)) |
| 295 | prio |= 1; |
| 296 | if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) { |
| 297 | prio |= 2; |
| 298 | h ^= HASH_ADDR(remote); |
| 299 | } |
| 300 | |
| 301 | return &ign->tunnels[prio][h]; |
| 302 | } |
| 303 | |
| 304 | static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign, |
| 305 | const struct ip6_tnl *t) |
| 306 | { |
| 307 | return __ip6gre_bucket(ign, &t->parms); |
| 308 | } |
| 309 | |
| 310 | static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t) |
| 311 | { |
| 312 | struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t); |
| 313 | |
| 314 | rcu_assign_pointer(t->next, rtnl_dereference(*tp)); |
| 315 | rcu_assign_pointer(*tp, t); |
| 316 | } |
| 317 | |
| 318 | static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t) |
| 319 | { |
| 320 | struct ip6_tnl __rcu **tp; |
| 321 | struct ip6_tnl *iter; |
| 322 | |
| 323 | for (tp = ip6gre_bucket(ign, t); |
| 324 | (iter = rtnl_dereference(*tp)) != NULL; |
| 325 | tp = &iter->next) { |
| 326 | if (t == iter) { |
| 327 | rcu_assign_pointer(*tp, t->next); |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | static struct ip6_tnl *ip6gre_tunnel_find(struct net *net, |
| 334 | const struct __ip6_tnl_parm *parms, |
| 335 | int type) |
| 336 | { |
| 337 | const struct in6_addr *remote = &parms->raddr; |
| 338 | const struct in6_addr *local = &parms->laddr; |
| 339 | __be32 key = parms->i_key; |
| 340 | int link = parms->link; |
| 341 | struct ip6_tnl *t; |
| 342 | struct ip6_tnl __rcu **tp; |
| 343 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 344 | |
| 345 | for (tp = __ip6gre_bucket(ign, parms); |
| 346 | (t = rtnl_dereference(*tp)) != NULL; |
| 347 | tp = &t->next) |
| 348 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
| 349 | ipv6_addr_equal(remote, &t->parms.raddr) && |
| 350 | key == t->parms.i_key && |
| 351 | link == t->parms.link && |
| 352 | type == t->dev->type) |
| 353 | break; |
| 354 | |
| 355 | return t; |
| 356 | } |
| 357 | |
| 358 | static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net, |
| 359 | const struct __ip6_tnl_parm *parms, int create) |
| 360 | { |
| 361 | struct ip6_tnl *t, *nt; |
| 362 | struct net_device *dev; |
| 363 | char name[IFNAMSIZ]; |
| 364 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 365 | |
| 366 | t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE); |
| 367 | if (t || !create) |
| 368 | return t; |
| 369 | |
| 370 | if (parms->name[0]) |
| 371 | strlcpy(name, parms->name, IFNAMSIZ); |
| 372 | else |
| 373 | strcpy(name, "ip6gre%d"); |
| 374 | |
| 375 | dev = alloc_netdev(sizeof(*t), name, ip6gre_tunnel_setup); |
| 376 | if (!dev) |
| 377 | return NULL; |
| 378 | |
| 379 | dev_net_set(dev, net); |
| 380 | |
| 381 | nt = netdev_priv(dev); |
| 382 | nt->parms = *parms; |
| 383 | dev->rtnl_link_ops = &ip6gre_link_ops; |
| 384 | |
| 385 | nt->dev = dev; |
| 386 | ip6gre_tnl_link_config(nt, 1); |
| 387 | |
| 388 | if (register_netdevice(dev) < 0) |
| 389 | goto failed_free; |
| 390 | |
| 391 | /* Can use a lockless transmit, unless we generate output sequences */ |
| 392 | if (!(nt->parms.o_flags & GRE_SEQ)) |
| 393 | dev->features |= NETIF_F_LLTX; |
| 394 | |
| 395 | dev_hold(dev); |
| 396 | ip6gre_tunnel_link(ign, nt); |
| 397 | return nt; |
| 398 | |
| 399 | failed_free: |
| 400 | free_netdev(dev); |
| 401 | return NULL; |
| 402 | } |
| 403 | |
| 404 | static void ip6gre_tunnel_uninit(struct net_device *dev) |
| 405 | { |
| 406 | struct net *net = dev_net(dev); |
| 407 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 408 | |
| 409 | ip6gre_tunnel_unlink(ign, netdev_priv(dev)); |
| 410 | dev_put(dev); |
| 411 | } |
| 412 | |
| 413 | |
| 414 | static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
| 415 | u8 type, u8 code, int offset, __be32 info) |
| 416 | { |
| 417 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data; |
Eric Dumazet | b87fb39 | 2012-08-19 03:47:30 +0000 | [diff] [blame] | 418 | __be16 *p = (__be16 *)(skb->data + offset); |
| 419 | int grehlen = offset + 4; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 420 | struct ip6_tnl *t; |
| 421 | __be16 flags; |
| 422 | |
| 423 | flags = p[0]; |
| 424 | if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) { |
| 425 | if (flags&(GRE_VERSION|GRE_ROUTING)) |
| 426 | return; |
| 427 | if (flags&GRE_KEY) { |
| 428 | grehlen += 4; |
| 429 | if (flags&GRE_CSUM) |
| 430 | grehlen += 4; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* If only 8 bytes returned, keyed message will be dropped here */ |
Eric Dumazet | b87fb39 | 2012-08-19 03:47:30 +0000 | [diff] [blame] | 435 | if (!pskb_may_pull(skb, grehlen)) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 436 | return; |
Eric Dumazet | b87fb39 | 2012-08-19 03:47:30 +0000 | [diff] [blame] | 437 | ipv6h = (const struct ipv6hdr *)skb->data; |
| 438 | p = (__be16 *)(skb->data + offset); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 439 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 440 | t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr, |
| 441 | flags & GRE_KEY ? |
| 442 | *(((__be32 *)p) + (grehlen / 4) - 1) : 0, |
| 443 | p[1]); |
| 444 | if (t == NULL) |
stephen hemminger | 0c5794a | 2012-09-24 18:12:24 +0000 | [diff] [blame^] | 445 | return; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 446 | |
| 447 | switch (type) { |
| 448 | __u32 teli; |
| 449 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 450 | __u32 mtu; |
| 451 | case ICMPV6_DEST_UNREACH: |
| 452 | net_warn_ratelimited("%s: Path to destination invalid or inactive!\n", |
| 453 | t->parms.name); |
| 454 | break; |
| 455 | case ICMPV6_TIME_EXCEED: |
| 456 | if (code == ICMPV6_EXC_HOPLIMIT) { |
| 457 | net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n", |
| 458 | t->parms.name); |
| 459 | } |
| 460 | break; |
| 461 | case ICMPV6_PARAMPROB: |
| 462 | teli = 0; |
| 463 | if (code == ICMPV6_HDR_FIELD) |
| 464 | teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); |
| 465 | |
| 466 | if (teli && teli == info - 2) { |
| 467 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; |
| 468 | if (tel->encap_limit == 0) { |
| 469 | net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n", |
| 470 | t->parms.name); |
| 471 | } |
| 472 | } else { |
| 473 | net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n", |
| 474 | t->parms.name); |
| 475 | } |
| 476 | break; |
| 477 | case ICMPV6_PKT_TOOBIG: |
| 478 | mtu = info - offset; |
| 479 | if (mtu < IPV6_MIN_MTU) |
| 480 | mtu = IPV6_MIN_MTU; |
| 481 | t->dev->mtu = mtu; |
| 482 | break; |
| 483 | } |
| 484 | |
| 485 | if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO)) |
| 486 | t->err_count++; |
| 487 | else |
| 488 | t->err_count = 1; |
| 489 | t->err_time = jiffies; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static inline void ip6gre_ecn_decapsulate_ipv4(const struct ip6_tnl *t, |
| 493 | const struct ipv6hdr *ipv6h, struct sk_buff *skb) |
| 494 | { |
| 495 | __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK; |
| 496 | |
| 497 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) |
| 498 | ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield); |
| 499 | |
| 500 | if (INET_ECN_is_ce(dsfield)) |
| 501 | IP_ECN_set_ce(ip_hdr(skb)); |
| 502 | } |
| 503 | |
| 504 | static inline void ip6gre_ecn_decapsulate_ipv6(const struct ip6_tnl *t, |
| 505 | const struct ipv6hdr *ipv6h, struct sk_buff *skb) |
| 506 | { |
| 507 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) |
| 508 | ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb)); |
| 509 | |
| 510 | if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h))) |
| 511 | IP6_ECN_set_ce(ipv6_hdr(skb)); |
| 512 | } |
| 513 | |
| 514 | static int ip6gre_rcv(struct sk_buff *skb) |
| 515 | { |
| 516 | const struct ipv6hdr *ipv6h; |
| 517 | u8 *h; |
| 518 | __be16 flags; |
| 519 | __sum16 csum = 0; |
| 520 | __be32 key = 0; |
| 521 | u32 seqno = 0; |
| 522 | struct ip6_tnl *tunnel; |
| 523 | int offset = 4; |
| 524 | __be16 gre_proto; |
| 525 | |
| 526 | if (!pskb_may_pull(skb, sizeof(struct in6_addr))) |
stephen hemminger | 0c5794a | 2012-09-24 18:12:24 +0000 | [diff] [blame^] | 527 | goto drop; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 528 | |
| 529 | ipv6h = ipv6_hdr(skb); |
| 530 | h = skb->data; |
| 531 | flags = *(__be16 *)h; |
| 532 | |
| 533 | if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) { |
| 534 | /* - Version must be 0. |
| 535 | - We do not support routing headers. |
| 536 | */ |
| 537 | if (flags&(GRE_VERSION|GRE_ROUTING)) |
stephen hemminger | 0c5794a | 2012-09-24 18:12:24 +0000 | [diff] [blame^] | 538 | goto drop; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 539 | |
| 540 | if (flags&GRE_CSUM) { |
| 541 | switch (skb->ip_summed) { |
| 542 | case CHECKSUM_COMPLETE: |
| 543 | csum = csum_fold(skb->csum); |
| 544 | if (!csum) |
| 545 | break; |
| 546 | /* fall through */ |
| 547 | case CHECKSUM_NONE: |
| 548 | skb->csum = 0; |
| 549 | csum = __skb_checksum_complete(skb); |
| 550 | skb->ip_summed = CHECKSUM_COMPLETE; |
| 551 | } |
| 552 | offset += 4; |
| 553 | } |
| 554 | if (flags&GRE_KEY) { |
| 555 | key = *(__be32 *)(h + offset); |
| 556 | offset += 4; |
| 557 | } |
| 558 | if (flags&GRE_SEQ) { |
| 559 | seqno = ntohl(*(__be32 *)(h + offset)); |
| 560 | offset += 4; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | gre_proto = *(__be16 *)(h + 2); |
| 565 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 566 | tunnel = ip6gre_tunnel_lookup(skb->dev, |
| 567 | &ipv6h->saddr, &ipv6h->daddr, key, |
| 568 | gre_proto); |
| 569 | if (tunnel) { |
| 570 | struct pcpu_tstats *tstats; |
| 571 | |
| 572 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) |
| 573 | goto drop; |
| 574 | |
| 575 | if (!ip6_tnl_rcv_ctl(tunnel, &ipv6h->daddr, &ipv6h->saddr)) { |
| 576 | tunnel->dev->stats.rx_dropped++; |
| 577 | goto drop; |
| 578 | } |
| 579 | |
| 580 | secpath_reset(skb); |
| 581 | |
| 582 | skb->protocol = gre_proto; |
| 583 | /* WCCP version 1 and 2 protocol decoding. |
| 584 | * - Change protocol to IP |
| 585 | * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header |
| 586 | */ |
| 587 | if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) { |
| 588 | skb->protocol = htons(ETH_P_IP); |
| 589 | if ((*(h + offset) & 0xF0) != 0x40) |
| 590 | offset += 4; |
| 591 | } |
| 592 | |
| 593 | skb->mac_header = skb->network_header; |
| 594 | __pskb_pull(skb, offset); |
| 595 | skb_postpull_rcsum(skb, skb_transport_header(skb), offset); |
| 596 | skb->pkt_type = PACKET_HOST; |
| 597 | |
| 598 | if (((flags&GRE_CSUM) && csum) || |
| 599 | (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) { |
| 600 | tunnel->dev->stats.rx_crc_errors++; |
| 601 | tunnel->dev->stats.rx_errors++; |
| 602 | goto drop; |
| 603 | } |
| 604 | if (tunnel->parms.i_flags&GRE_SEQ) { |
| 605 | if (!(flags&GRE_SEQ) || |
| 606 | (tunnel->i_seqno && |
| 607 | (s32)(seqno - tunnel->i_seqno) < 0)) { |
| 608 | tunnel->dev->stats.rx_fifo_errors++; |
| 609 | tunnel->dev->stats.rx_errors++; |
| 610 | goto drop; |
| 611 | } |
| 612 | tunnel->i_seqno = seqno + 1; |
| 613 | } |
| 614 | |
| 615 | /* Warning: All skb pointers will be invalidated! */ |
| 616 | if (tunnel->dev->type == ARPHRD_ETHER) { |
| 617 | if (!pskb_may_pull(skb, ETH_HLEN)) { |
| 618 | tunnel->dev->stats.rx_length_errors++; |
| 619 | tunnel->dev->stats.rx_errors++; |
| 620 | goto drop; |
| 621 | } |
| 622 | |
| 623 | ipv6h = ipv6_hdr(skb); |
| 624 | skb->protocol = eth_type_trans(skb, tunnel->dev); |
| 625 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); |
| 626 | } |
| 627 | |
| 628 | tstats = this_cpu_ptr(tunnel->dev->tstats); |
| 629 | u64_stats_update_begin(&tstats->syncp); |
| 630 | tstats->rx_packets++; |
| 631 | tstats->rx_bytes += skb->len; |
| 632 | u64_stats_update_end(&tstats->syncp); |
| 633 | |
| 634 | __skb_tunnel_rx(skb, tunnel->dev); |
| 635 | |
| 636 | skb_reset_network_header(skb); |
| 637 | if (skb->protocol == htons(ETH_P_IP)) |
| 638 | ip6gre_ecn_decapsulate_ipv4(tunnel, ipv6h, skb); |
| 639 | else if (skb->protocol == htons(ETH_P_IPV6)) |
| 640 | ip6gre_ecn_decapsulate_ipv6(tunnel, ipv6h, skb); |
| 641 | |
| 642 | netif_rx(skb); |
| 643 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 644 | return 0; |
| 645 | } |
| 646 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
| 647 | |
| 648 | drop: |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 649 | kfree_skb(skb); |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | struct ipv6_tel_txoption { |
| 654 | struct ipv6_txoptions ops; |
| 655 | __u8 dst_opt[8]; |
| 656 | }; |
| 657 | |
| 658 | static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit) |
| 659 | { |
| 660 | memset(opt, 0, sizeof(struct ipv6_tel_txoption)); |
| 661 | |
| 662 | opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT; |
| 663 | opt->dst_opt[3] = 1; |
| 664 | opt->dst_opt[4] = encap_limit; |
| 665 | opt->dst_opt[5] = IPV6_TLV_PADN; |
| 666 | opt->dst_opt[6] = 1; |
| 667 | |
| 668 | opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt; |
| 669 | opt->ops.opt_nflen = 8; |
| 670 | } |
| 671 | |
| 672 | static netdev_tx_t ip6gre_xmit2(struct sk_buff *skb, |
| 673 | struct net_device *dev, |
| 674 | __u8 dsfield, |
| 675 | struct flowi6 *fl6, |
| 676 | int encap_limit, |
| 677 | __u32 *pmtu) |
| 678 | { |
| 679 | struct net *net = dev_net(dev); |
| 680 | struct ip6_tnl *tunnel = netdev_priv(dev); |
| 681 | struct net_device *tdev; /* Device to other host */ |
| 682 | struct ipv6hdr *ipv6h; /* Our new IP header */ |
| 683 | unsigned int max_headroom; /* The extra header space needed */ |
| 684 | int gre_hlen; |
| 685 | struct ipv6_tel_txoption opt; |
| 686 | int mtu; |
| 687 | struct dst_entry *dst = NULL, *ndst = NULL; |
| 688 | struct net_device_stats *stats = &tunnel->dev->stats; |
| 689 | int err = -1; |
| 690 | u8 proto; |
| 691 | int pkt_len; |
| 692 | struct sk_buff *new_skb; |
| 693 | |
| 694 | if (dev->type == ARPHRD_ETHER) |
| 695 | IPCB(skb)->flags = 0; |
| 696 | |
| 697 | if (dev->header_ops && dev->type == ARPHRD_IP6GRE) { |
| 698 | gre_hlen = 0; |
| 699 | ipv6h = (struct ipv6hdr *)skb->data; |
| 700 | fl6->daddr = ipv6h->daddr; |
| 701 | } else { |
| 702 | gre_hlen = tunnel->hlen; |
| 703 | fl6->daddr = tunnel->parms.raddr; |
| 704 | } |
| 705 | |
| 706 | if (!fl6->flowi6_mark) |
| 707 | dst = ip6_tnl_dst_check(tunnel); |
| 708 | |
| 709 | if (!dst) { |
| 710 | ndst = ip6_route_output(net, NULL, fl6); |
| 711 | |
| 712 | if (ndst->error) |
| 713 | goto tx_err_link_failure; |
| 714 | ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0); |
| 715 | if (IS_ERR(ndst)) { |
| 716 | err = PTR_ERR(ndst); |
| 717 | ndst = NULL; |
| 718 | goto tx_err_link_failure; |
| 719 | } |
| 720 | dst = ndst; |
| 721 | } |
| 722 | |
| 723 | tdev = dst->dev; |
| 724 | |
| 725 | if (tdev == dev) { |
| 726 | stats->collisions++; |
| 727 | net_warn_ratelimited("%s: Local routing loop detected!\n", |
| 728 | tunnel->parms.name); |
| 729 | goto tx_err_dst_release; |
| 730 | } |
| 731 | |
| 732 | mtu = dst_mtu(dst) - sizeof(*ipv6h); |
| 733 | if (encap_limit >= 0) { |
| 734 | max_headroom += 8; |
| 735 | mtu -= 8; |
| 736 | } |
| 737 | if (mtu < IPV6_MIN_MTU) |
| 738 | mtu = IPV6_MIN_MTU; |
| 739 | if (skb_dst(skb)) |
| 740 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu); |
| 741 | if (skb->len > mtu) { |
| 742 | *pmtu = mtu; |
| 743 | err = -EMSGSIZE; |
| 744 | goto tx_err_dst_release; |
| 745 | } |
| 746 | |
| 747 | if (tunnel->err_count > 0) { |
| 748 | if (time_before(jiffies, |
| 749 | tunnel->err_time + IP6TUNNEL_ERR_TIMEO)) { |
| 750 | tunnel->err_count--; |
| 751 | |
| 752 | dst_link_failure(skb); |
| 753 | } else |
| 754 | tunnel->err_count = 0; |
| 755 | } |
| 756 | |
| 757 | max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen + dst->header_len; |
| 758 | |
| 759 | if (skb_headroom(skb) < max_headroom || skb_shared(skb) || |
| 760 | (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { |
| 761 | new_skb = skb_realloc_headroom(skb, max_headroom); |
| 762 | if (max_headroom > dev->needed_headroom) |
| 763 | dev->needed_headroom = max_headroom; |
| 764 | if (!new_skb) |
| 765 | goto tx_err_dst_release; |
| 766 | |
| 767 | if (skb->sk) |
| 768 | skb_set_owner_w(new_skb, skb->sk); |
| 769 | consume_skb(skb); |
| 770 | skb = new_skb; |
| 771 | } |
| 772 | |
| 773 | skb_dst_drop(skb); |
| 774 | |
| 775 | if (fl6->flowi6_mark) { |
| 776 | skb_dst_set(skb, dst); |
| 777 | ndst = NULL; |
| 778 | } else { |
| 779 | skb_dst_set_noref(skb, dst); |
| 780 | } |
| 781 | |
| 782 | skb->transport_header = skb->network_header; |
| 783 | |
| 784 | proto = NEXTHDR_GRE; |
| 785 | if (encap_limit >= 0) { |
| 786 | init_tel_txopt(&opt, encap_limit); |
| 787 | ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL); |
| 788 | } |
| 789 | |
| 790 | skb_push(skb, gre_hlen); |
| 791 | skb_reset_network_header(skb); |
| 792 | |
| 793 | /* |
| 794 | * Push down and install the IP header. |
| 795 | */ |
| 796 | ipv6h = ipv6_hdr(skb); |
| 797 | *(__be32 *)ipv6h = fl6->flowlabel | htonl(0x60000000); |
| 798 | dsfield = INET_ECN_encapsulate(0, dsfield); |
| 799 | ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield); |
| 800 | ipv6h->hop_limit = tunnel->parms.hop_limit; |
| 801 | ipv6h->nexthdr = proto; |
| 802 | ipv6h->saddr = fl6->saddr; |
| 803 | ipv6h->daddr = fl6->daddr; |
| 804 | |
| 805 | ((__be16 *)(ipv6h + 1))[0] = tunnel->parms.o_flags; |
| 806 | ((__be16 *)(ipv6h + 1))[1] = (dev->type == ARPHRD_ETHER) ? |
| 807 | htons(ETH_P_TEB) : skb->protocol; |
| 808 | |
| 809 | if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) { |
| 810 | __be32 *ptr = (__be32 *)(((u8 *)ipv6h) + tunnel->hlen - 4); |
| 811 | |
| 812 | if (tunnel->parms.o_flags&GRE_SEQ) { |
| 813 | ++tunnel->o_seqno; |
| 814 | *ptr = htonl(tunnel->o_seqno); |
| 815 | ptr--; |
| 816 | } |
| 817 | if (tunnel->parms.o_flags&GRE_KEY) { |
| 818 | *ptr = tunnel->parms.o_key; |
| 819 | ptr--; |
| 820 | } |
| 821 | if (tunnel->parms.o_flags&GRE_CSUM) { |
| 822 | *ptr = 0; |
| 823 | *(__sum16 *)ptr = ip_compute_csum((void *)(ipv6h+1), |
| 824 | skb->len - sizeof(struct ipv6hdr)); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | nf_reset(skb); |
| 829 | pkt_len = skb->len; |
| 830 | err = ip6_local_out(skb); |
| 831 | |
| 832 | if (net_xmit_eval(err) == 0) { |
| 833 | struct pcpu_tstats *tstats = this_cpu_ptr(tunnel->dev->tstats); |
| 834 | |
| 835 | tstats->tx_bytes += pkt_len; |
| 836 | tstats->tx_packets++; |
| 837 | } else { |
| 838 | stats->tx_errors++; |
| 839 | stats->tx_aborted_errors++; |
| 840 | } |
| 841 | |
| 842 | if (ndst) |
| 843 | ip6_tnl_dst_store(tunnel, ndst); |
| 844 | |
| 845 | return 0; |
| 846 | tx_err_link_failure: |
| 847 | stats->tx_carrier_errors++; |
| 848 | dst_link_failure(skb); |
| 849 | tx_err_dst_release: |
| 850 | dst_release(ndst); |
| 851 | return err; |
| 852 | } |
| 853 | |
| 854 | static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev) |
| 855 | { |
| 856 | struct ip6_tnl *t = netdev_priv(dev); |
| 857 | const struct iphdr *iph = ip_hdr(skb); |
| 858 | int encap_limit = -1; |
| 859 | struct flowi6 fl6; |
| 860 | __u8 dsfield; |
| 861 | __u32 mtu; |
| 862 | int err; |
| 863 | |
| 864 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 865 | encap_limit = t->parms.encap_limit; |
| 866 | |
| 867 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
| 868 | fl6.flowi6_proto = IPPROTO_IPIP; |
| 869 | |
| 870 | dsfield = ipv4_get_dsfield(iph); |
| 871 | |
| 872 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
| 873 | fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT) |
| 874 | & IPV6_TCLASS_MASK; |
| 875 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
| 876 | fl6.flowi6_mark = skb->mark; |
| 877 | |
| 878 | err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu); |
| 879 | if (err != 0) { |
| 880 | /* XXX: send ICMP error even if DF is not set. */ |
| 881 | if (err == -EMSGSIZE) |
| 882 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, |
| 883 | htonl(mtu)); |
| 884 | return -1; |
| 885 | } |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev) |
| 891 | { |
| 892 | struct ip6_tnl *t = netdev_priv(dev); |
| 893 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
| 894 | int encap_limit = -1; |
| 895 | __u16 offset; |
| 896 | struct flowi6 fl6; |
| 897 | __u8 dsfield; |
| 898 | __u32 mtu; |
| 899 | int err; |
| 900 | |
| 901 | if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr)) |
| 902 | return -1; |
| 903 | |
| 904 | offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb)); |
| 905 | if (offset > 0) { |
| 906 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 907 | tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset]; |
| 908 | if (tel->encap_limit == 0) { |
| 909 | icmpv6_send(skb, ICMPV6_PARAMPROB, |
| 910 | ICMPV6_HDR_FIELD, offset + 2); |
| 911 | return -1; |
| 912 | } |
| 913 | encap_limit = tel->encap_limit - 1; |
| 914 | } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 915 | encap_limit = t->parms.encap_limit; |
| 916 | |
| 917 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
| 918 | fl6.flowi6_proto = IPPROTO_IPV6; |
| 919 | |
| 920 | dsfield = ipv6_get_dsfield(ipv6h); |
| 921 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
| 922 | fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK); |
| 923 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) |
| 924 | fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK); |
| 925 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
| 926 | fl6.flowi6_mark = skb->mark; |
| 927 | |
| 928 | err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu); |
| 929 | if (err != 0) { |
| 930 | if (err == -EMSGSIZE) |
| 931 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
| 932 | return -1; |
| 933 | } |
| 934 | |
| 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | /** |
| 939 | * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own |
| 940 | * @t: the outgoing tunnel device |
| 941 | * @hdr: IPv6 header from the incoming packet |
| 942 | * |
| 943 | * Description: |
| 944 | * Avoid trivial tunneling loop by checking that tunnel exit-point |
| 945 | * doesn't match source of incoming packet. |
| 946 | * |
| 947 | * Return: |
| 948 | * 1 if conflict, |
| 949 | * 0 else |
| 950 | **/ |
| 951 | |
| 952 | static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t, |
| 953 | const struct ipv6hdr *hdr) |
| 954 | { |
| 955 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); |
| 956 | } |
| 957 | |
| 958 | static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev) |
| 959 | { |
| 960 | struct ip6_tnl *t = netdev_priv(dev); |
| 961 | int encap_limit = -1; |
| 962 | struct flowi6 fl6; |
| 963 | __u32 mtu; |
| 964 | int err; |
| 965 | |
| 966 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 967 | encap_limit = t->parms.encap_limit; |
| 968 | |
| 969 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
| 970 | fl6.flowi6_proto = skb->protocol; |
| 971 | |
| 972 | err = ip6gre_xmit2(skb, dev, 0, &fl6, encap_limit, &mtu); |
| 973 | |
| 974 | return err; |
| 975 | } |
| 976 | |
| 977 | static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb, |
| 978 | struct net_device *dev) |
| 979 | { |
| 980 | struct ip6_tnl *t = netdev_priv(dev); |
| 981 | struct net_device_stats *stats = &t->dev->stats; |
| 982 | int ret; |
| 983 | |
| 984 | if (!ip6_tnl_xmit_ctl(t)) |
| 985 | return -1; |
| 986 | |
| 987 | switch (skb->protocol) { |
| 988 | case htons(ETH_P_IP): |
| 989 | ret = ip6gre_xmit_ipv4(skb, dev); |
| 990 | break; |
| 991 | case htons(ETH_P_IPV6): |
| 992 | ret = ip6gre_xmit_ipv6(skb, dev); |
| 993 | break; |
| 994 | default: |
| 995 | ret = ip6gre_xmit_other(skb, dev); |
| 996 | break; |
| 997 | } |
| 998 | |
| 999 | if (ret < 0) |
| 1000 | goto tx_err; |
| 1001 | |
| 1002 | return NETDEV_TX_OK; |
| 1003 | |
| 1004 | tx_err: |
| 1005 | stats->tx_errors++; |
| 1006 | stats->tx_dropped++; |
| 1007 | kfree_skb(skb); |
| 1008 | return NETDEV_TX_OK; |
| 1009 | } |
| 1010 | |
| 1011 | static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu) |
| 1012 | { |
| 1013 | struct net_device *dev = t->dev; |
| 1014 | struct __ip6_tnl_parm *p = &t->parms; |
| 1015 | struct flowi6 *fl6 = &t->fl.u.ip6; |
| 1016 | int addend = sizeof(struct ipv6hdr) + 4; |
| 1017 | |
| 1018 | if (dev->type != ARPHRD_ETHER) { |
| 1019 | memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); |
| 1020 | memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); |
| 1021 | } |
| 1022 | |
| 1023 | /* Set up flowi template */ |
| 1024 | fl6->saddr = p->laddr; |
| 1025 | fl6->daddr = p->raddr; |
| 1026 | fl6->flowi6_oif = p->link; |
| 1027 | fl6->flowlabel = 0; |
| 1028 | |
| 1029 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) |
| 1030 | fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; |
| 1031 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) |
| 1032 | fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; |
| 1033 | |
| 1034 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); |
| 1035 | p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); |
| 1036 | |
| 1037 | if (p->flags&IP6_TNL_F_CAP_XMIT && |
| 1038 | p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER) |
| 1039 | dev->flags |= IFF_POINTOPOINT; |
| 1040 | else |
| 1041 | dev->flags &= ~IFF_POINTOPOINT; |
| 1042 | |
| 1043 | dev->iflink = p->link; |
| 1044 | |
| 1045 | /* Precalculate GRE options length */ |
| 1046 | if (t->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) { |
| 1047 | if (t->parms.o_flags&GRE_CSUM) |
| 1048 | addend += 4; |
| 1049 | if (t->parms.o_flags&GRE_KEY) |
| 1050 | addend += 4; |
| 1051 | if (t->parms.o_flags&GRE_SEQ) |
| 1052 | addend += 4; |
| 1053 | } |
| 1054 | |
| 1055 | if (p->flags & IP6_TNL_F_CAP_XMIT) { |
| 1056 | int strict = (ipv6_addr_type(&p->raddr) & |
| 1057 | (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); |
| 1058 | |
| 1059 | struct rt6_info *rt = rt6_lookup(dev_net(dev), |
| 1060 | &p->raddr, &p->laddr, |
| 1061 | p->link, strict); |
| 1062 | |
| 1063 | if (rt == NULL) |
| 1064 | return; |
| 1065 | |
| 1066 | if (rt->dst.dev) { |
| 1067 | dev->hard_header_len = rt->dst.dev->hard_header_len + addend; |
| 1068 | |
| 1069 | if (set_mtu) { |
| 1070 | dev->mtu = rt->dst.dev->mtu - addend; |
| 1071 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1072 | dev->mtu -= 8; |
| 1073 | |
| 1074 | if (dev->mtu < IPV6_MIN_MTU) |
| 1075 | dev->mtu = IPV6_MIN_MTU; |
| 1076 | } |
| 1077 | } |
| 1078 | dst_release(&rt->dst); |
| 1079 | } |
| 1080 | |
| 1081 | t->hlen = addend; |
| 1082 | } |
| 1083 | |
| 1084 | static int ip6gre_tnl_change(struct ip6_tnl *t, |
| 1085 | const struct __ip6_tnl_parm *p, int set_mtu) |
| 1086 | { |
| 1087 | t->parms.laddr = p->laddr; |
| 1088 | t->parms.raddr = p->raddr; |
| 1089 | t->parms.flags = p->flags; |
| 1090 | t->parms.hop_limit = p->hop_limit; |
| 1091 | t->parms.encap_limit = p->encap_limit; |
| 1092 | t->parms.flowinfo = p->flowinfo; |
| 1093 | t->parms.link = p->link; |
| 1094 | t->parms.proto = p->proto; |
| 1095 | t->parms.i_key = p->i_key; |
| 1096 | t->parms.o_key = p->o_key; |
| 1097 | t->parms.i_flags = p->i_flags; |
| 1098 | t->parms.o_flags = p->o_flags; |
| 1099 | ip6_tnl_dst_reset(t); |
| 1100 | ip6gre_tnl_link_config(t, set_mtu); |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p, |
| 1105 | const struct ip6_tnl_parm2 *u) |
| 1106 | { |
| 1107 | p->laddr = u->laddr; |
| 1108 | p->raddr = u->raddr; |
| 1109 | p->flags = u->flags; |
| 1110 | p->hop_limit = u->hop_limit; |
| 1111 | p->encap_limit = u->encap_limit; |
| 1112 | p->flowinfo = u->flowinfo; |
| 1113 | p->link = u->link; |
| 1114 | p->i_key = u->i_key; |
| 1115 | p->o_key = u->o_key; |
| 1116 | p->i_flags = u->i_flags; |
| 1117 | p->o_flags = u->o_flags; |
| 1118 | memcpy(p->name, u->name, sizeof(u->name)); |
| 1119 | } |
| 1120 | |
| 1121 | static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u, |
| 1122 | const struct __ip6_tnl_parm *p) |
| 1123 | { |
| 1124 | u->proto = IPPROTO_GRE; |
| 1125 | u->laddr = p->laddr; |
| 1126 | u->raddr = p->raddr; |
| 1127 | u->flags = p->flags; |
| 1128 | u->hop_limit = p->hop_limit; |
| 1129 | u->encap_limit = p->encap_limit; |
| 1130 | u->flowinfo = p->flowinfo; |
| 1131 | u->link = p->link; |
| 1132 | u->i_key = p->i_key; |
| 1133 | u->o_key = p->o_key; |
| 1134 | u->i_flags = p->i_flags; |
| 1135 | u->o_flags = p->o_flags; |
| 1136 | memcpy(u->name, p->name, sizeof(u->name)); |
| 1137 | } |
| 1138 | |
| 1139 | static int ip6gre_tunnel_ioctl(struct net_device *dev, |
| 1140 | struct ifreq *ifr, int cmd) |
| 1141 | { |
| 1142 | int err = 0; |
| 1143 | struct ip6_tnl_parm2 p; |
| 1144 | struct __ip6_tnl_parm p1; |
| 1145 | struct ip6_tnl *t; |
| 1146 | struct net *net = dev_net(dev); |
| 1147 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1148 | |
| 1149 | switch (cmd) { |
| 1150 | case SIOCGETTUNNEL: |
| 1151 | t = NULL; |
| 1152 | if (dev == ign->fb_tunnel_dev) { |
| 1153 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { |
| 1154 | err = -EFAULT; |
| 1155 | break; |
| 1156 | } |
| 1157 | ip6gre_tnl_parm_from_user(&p1, &p); |
| 1158 | t = ip6gre_tunnel_locate(net, &p1, 0); |
| 1159 | } |
| 1160 | if (t == NULL) |
| 1161 | t = netdev_priv(dev); |
| 1162 | ip6gre_tnl_parm_to_user(&p, &t->parms); |
| 1163 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) |
| 1164 | err = -EFAULT; |
| 1165 | break; |
| 1166 | |
| 1167 | case SIOCADDTUNNEL: |
| 1168 | case SIOCCHGTUNNEL: |
| 1169 | err = -EPERM; |
| 1170 | if (!capable(CAP_NET_ADMIN)) |
| 1171 | goto done; |
| 1172 | |
| 1173 | err = -EFAULT; |
| 1174 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
| 1175 | goto done; |
| 1176 | |
| 1177 | err = -EINVAL; |
| 1178 | if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)) |
| 1179 | goto done; |
| 1180 | |
| 1181 | if (!(p.i_flags&GRE_KEY)) |
| 1182 | p.i_key = 0; |
| 1183 | if (!(p.o_flags&GRE_KEY)) |
| 1184 | p.o_key = 0; |
| 1185 | |
| 1186 | ip6gre_tnl_parm_from_user(&p1, &p); |
| 1187 | t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL); |
| 1188 | |
| 1189 | if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { |
| 1190 | if (t != NULL) { |
| 1191 | if (t->dev != dev) { |
| 1192 | err = -EEXIST; |
| 1193 | break; |
| 1194 | } |
| 1195 | } else { |
| 1196 | t = netdev_priv(dev); |
| 1197 | |
| 1198 | ip6gre_tunnel_unlink(ign, t); |
| 1199 | synchronize_net(); |
| 1200 | ip6gre_tnl_change(t, &p1, 1); |
| 1201 | ip6gre_tunnel_link(ign, t); |
| 1202 | netdev_state_change(dev); |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | if (t) { |
| 1207 | err = 0; |
| 1208 | |
| 1209 | ip6gre_tnl_parm_to_user(&p, &t->parms); |
| 1210 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) |
| 1211 | err = -EFAULT; |
| 1212 | } else |
| 1213 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); |
| 1214 | break; |
| 1215 | |
| 1216 | case SIOCDELTUNNEL: |
| 1217 | err = -EPERM; |
| 1218 | if (!capable(CAP_NET_ADMIN)) |
| 1219 | goto done; |
| 1220 | |
| 1221 | if (dev == ign->fb_tunnel_dev) { |
| 1222 | err = -EFAULT; |
| 1223 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
| 1224 | goto done; |
| 1225 | err = -ENOENT; |
| 1226 | ip6gre_tnl_parm_from_user(&p1, &p); |
| 1227 | t = ip6gre_tunnel_locate(net, &p1, 0); |
| 1228 | if (t == NULL) |
| 1229 | goto done; |
| 1230 | err = -EPERM; |
| 1231 | if (t == netdev_priv(ign->fb_tunnel_dev)) |
| 1232 | goto done; |
| 1233 | dev = t->dev; |
| 1234 | } |
| 1235 | unregister_netdevice(dev); |
| 1236 | err = 0; |
| 1237 | break; |
| 1238 | |
| 1239 | default: |
| 1240 | err = -EINVAL; |
| 1241 | } |
| 1242 | |
| 1243 | done: |
| 1244 | return err; |
| 1245 | } |
| 1246 | |
| 1247 | static int ip6gre_tunnel_change_mtu(struct net_device *dev, int new_mtu) |
| 1248 | { |
| 1249 | struct ip6_tnl *tunnel = netdev_priv(dev); |
| 1250 | if (new_mtu < 68 || |
| 1251 | new_mtu > 0xFFF8 - dev->hard_header_len - tunnel->hlen) |
| 1252 | return -EINVAL; |
| 1253 | dev->mtu = new_mtu; |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | static int ip6gre_header(struct sk_buff *skb, struct net_device *dev, |
| 1258 | unsigned short type, |
| 1259 | const void *daddr, const void *saddr, unsigned int len) |
| 1260 | { |
| 1261 | struct ip6_tnl *t = netdev_priv(dev); |
| 1262 | struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen); |
| 1263 | __be16 *p = (__be16 *)(ipv6h+1); |
| 1264 | |
| 1265 | *(__be32 *)ipv6h = t->fl.u.ip6.flowlabel | htonl(0x60000000); |
| 1266 | ipv6h->hop_limit = t->parms.hop_limit; |
| 1267 | ipv6h->nexthdr = NEXTHDR_GRE; |
| 1268 | ipv6h->saddr = t->parms.laddr; |
| 1269 | ipv6h->daddr = t->parms.raddr; |
| 1270 | |
| 1271 | p[0] = t->parms.o_flags; |
| 1272 | p[1] = htons(type); |
| 1273 | |
| 1274 | /* |
| 1275 | * Set the source hardware address. |
| 1276 | */ |
| 1277 | |
| 1278 | if (saddr) |
| 1279 | memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr)); |
| 1280 | if (daddr) |
| 1281 | memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr)); |
| 1282 | if (!ipv6_addr_any(&ipv6h->daddr)) |
| 1283 | return t->hlen; |
| 1284 | |
| 1285 | return -t->hlen; |
| 1286 | } |
| 1287 | |
| 1288 | static int ip6gre_header_parse(const struct sk_buff *skb, unsigned char *haddr) |
| 1289 | { |
| 1290 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb_mac_header(skb); |
| 1291 | memcpy(haddr, &ipv6h->saddr, sizeof(struct in6_addr)); |
| 1292 | return sizeof(struct in6_addr); |
| 1293 | } |
| 1294 | |
| 1295 | static const struct header_ops ip6gre_header_ops = { |
| 1296 | .create = ip6gre_header, |
| 1297 | .parse = ip6gre_header_parse, |
| 1298 | }; |
| 1299 | |
| 1300 | static const struct net_device_ops ip6gre_netdev_ops = { |
| 1301 | .ndo_init = ip6gre_tunnel_init, |
| 1302 | .ndo_uninit = ip6gre_tunnel_uninit, |
| 1303 | .ndo_start_xmit = ip6gre_tunnel_xmit, |
| 1304 | .ndo_do_ioctl = ip6gre_tunnel_ioctl, |
| 1305 | .ndo_change_mtu = ip6gre_tunnel_change_mtu, |
| 1306 | .ndo_get_stats64 = ip6gre_get_stats64, |
| 1307 | }; |
| 1308 | |
| 1309 | static void ip6gre_dev_free(struct net_device *dev) |
| 1310 | { |
| 1311 | free_percpu(dev->tstats); |
| 1312 | free_netdev(dev); |
| 1313 | } |
| 1314 | |
| 1315 | static void ip6gre_tunnel_setup(struct net_device *dev) |
| 1316 | { |
| 1317 | struct ip6_tnl *t; |
| 1318 | |
| 1319 | dev->netdev_ops = &ip6gre_netdev_ops; |
| 1320 | dev->destructor = ip6gre_dev_free; |
| 1321 | |
| 1322 | dev->type = ARPHRD_IP6GRE; |
| 1323 | dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr) + 4; |
| 1324 | dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr) - 4; |
| 1325 | t = netdev_priv(dev); |
| 1326 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1327 | dev->mtu -= 8; |
| 1328 | dev->flags |= IFF_NOARP; |
| 1329 | dev->iflink = 0; |
| 1330 | dev->addr_len = sizeof(struct in6_addr); |
| 1331 | dev->features |= NETIF_F_NETNS_LOCAL; |
| 1332 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
| 1333 | } |
| 1334 | |
| 1335 | static int ip6gre_tunnel_init(struct net_device *dev) |
| 1336 | { |
| 1337 | struct ip6_tnl *tunnel; |
| 1338 | |
| 1339 | tunnel = netdev_priv(dev); |
| 1340 | |
| 1341 | tunnel->dev = dev; |
| 1342 | strcpy(tunnel->parms.name, dev->name); |
| 1343 | |
| 1344 | memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr)); |
| 1345 | memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr)); |
| 1346 | |
| 1347 | if (ipv6_addr_any(&tunnel->parms.raddr)) |
| 1348 | dev->header_ops = &ip6gre_header_ops; |
| 1349 | |
| 1350 | dev->tstats = alloc_percpu(struct pcpu_tstats); |
| 1351 | if (!dev->tstats) |
| 1352 | return -ENOMEM; |
| 1353 | |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | static void ip6gre_fb_tunnel_init(struct net_device *dev) |
| 1358 | { |
| 1359 | struct ip6_tnl *tunnel = netdev_priv(dev); |
| 1360 | |
| 1361 | tunnel->dev = dev; |
| 1362 | strcpy(tunnel->parms.name, dev->name); |
| 1363 | |
| 1364 | tunnel->hlen = sizeof(struct ipv6hdr) + 4; |
| 1365 | |
| 1366 | dev_hold(dev); |
| 1367 | } |
| 1368 | |
| 1369 | |
| 1370 | static struct inet6_protocol ip6gre_protocol __read_mostly = { |
| 1371 | .handler = ip6gre_rcv, |
| 1372 | .err_handler = ip6gre_err, |
| 1373 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, |
| 1374 | }; |
| 1375 | |
| 1376 | static void ip6gre_destroy_tunnels(struct ip6gre_net *ign, |
| 1377 | struct list_head *head) |
| 1378 | { |
| 1379 | int prio; |
| 1380 | |
| 1381 | for (prio = 0; prio < 4; prio++) { |
| 1382 | int h; |
| 1383 | for (h = 0; h < HASH_SIZE; h++) { |
| 1384 | struct ip6_tnl *t; |
| 1385 | |
| 1386 | t = rtnl_dereference(ign->tunnels[prio][h]); |
| 1387 | |
| 1388 | while (t != NULL) { |
| 1389 | unregister_netdevice_queue(t->dev, head); |
| 1390 | t = rtnl_dereference(t->next); |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | static int __net_init ip6gre_init_net(struct net *net) |
| 1397 | { |
| 1398 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1399 | int err; |
| 1400 | |
| 1401 | ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0", |
| 1402 | ip6gre_tunnel_setup); |
| 1403 | if (!ign->fb_tunnel_dev) { |
| 1404 | err = -ENOMEM; |
| 1405 | goto err_alloc_dev; |
| 1406 | } |
| 1407 | dev_net_set(ign->fb_tunnel_dev, net); |
| 1408 | |
| 1409 | ip6gre_fb_tunnel_init(ign->fb_tunnel_dev); |
| 1410 | ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops; |
| 1411 | |
| 1412 | err = register_netdev(ign->fb_tunnel_dev); |
| 1413 | if (err) |
| 1414 | goto err_reg_dev; |
| 1415 | |
| 1416 | rcu_assign_pointer(ign->tunnels_wc[0], |
| 1417 | netdev_priv(ign->fb_tunnel_dev)); |
| 1418 | return 0; |
| 1419 | |
| 1420 | err_reg_dev: |
| 1421 | ip6gre_dev_free(ign->fb_tunnel_dev); |
| 1422 | err_alloc_dev: |
| 1423 | return err; |
| 1424 | } |
| 1425 | |
| 1426 | static void __net_exit ip6gre_exit_net(struct net *net) |
| 1427 | { |
| 1428 | struct ip6gre_net *ign; |
| 1429 | LIST_HEAD(list); |
| 1430 | |
| 1431 | ign = net_generic(net, ip6gre_net_id); |
| 1432 | rtnl_lock(); |
| 1433 | ip6gre_destroy_tunnels(ign, &list); |
| 1434 | unregister_netdevice_many(&list); |
| 1435 | rtnl_unlock(); |
| 1436 | } |
| 1437 | |
| 1438 | static struct pernet_operations ip6gre_net_ops = { |
| 1439 | .init = ip6gre_init_net, |
| 1440 | .exit = ip6gre_exit_net, |
| 1441 | .id = &ip6gre_net_id, |
| 1442 | .size = sizeof(struct ip6gre_net), |
| 1443 | }; |
| 1444 | |
| 1445 | static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 1446 | { |
| 1447 | __be16 flags; |
| 1448 | |
| 1449 | if (!data) |
| 1450 | return 0; |
| 1451 | |
| 1452 | flags = 0; |
| 1453 | if (data[IFLA_GRE_IFLAGS]) |
| 1454 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); |
| 1455 | if (data[IFLA_GRE_OFLAGS]) |
| 1456 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); |
| 1457 | if (flags & (GRE_VERSION|GRE_ROUTING)) |
| 1458 | return -EINVAL; |
| 1459 | |
| 1460 | return 0; |
| 1461 | } |
| 1462 | |
| 1463 | static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 1464 | { |
| 1465 | struct in6_addr daddr; |
| 1466 | |
| 1467 | if (tb[IFLA_ADDRESS]) { |
| 1468 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 1469 | return -EINVAL; |
| 1470 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 1471 | return -EADDRNOTAVAIL; |
| 1472 | } |
| 1473 | |
| 1474 | if (!data) |
| 1475 | goto out; |
| 1476 | |
| 1477 | if (data[IFLA_GRE_REMOTE]) { |
| 1478 | nla_memcpy(&daddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr)); |
| 1479 | if (ipv6_addr_any(&daddr)) |
| 1480 | return -EINVAL; |
| 1481 | } |
| 1482 | |
| 1483 | out: |
| 1484 | return ip6gre_tunnel_validate(tb, data); |
| 1485 | } |
| 1486 | |
| 1487 | |
| 1488 | static void ip6gre_netlink_parms(struct nlattr *data[], |
| 1489 | struct __ip6_tnl_parm *parms) |
| 1490 | { |
| 1491 | memset(parms, 0, sizeof(*parms)); |
| 1492 | |
| 1493 | if (!data) |
| 1494 | return; |
| 1495 | |
| 1496 | if (data[IFLA_GRE_LINK]) |
| 1497 | parms->link = nla_get_u32(data[IFLA_GRE_LINK]); |
| 1498 | |
| 1499 | if (data[IFLA_GRE_IFLAGS]) |
| 1500 | parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]); |
| 1501 | |
| 1502 | if (data[IFLA_GRE_OFLAGS]) |
| 1503 | parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]); |
| 1504 | |
| 1505 | if (data[IFLA_GRE_IKEY]) |
| 1506 | parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); |
| 1507 | |
| 1508 | if (data[IFLA_GRE_OKEY]) |
| 1509 | parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); |
| 1510 | |
| 1511 | if (data[IFLA_GRE_LOCAL]) |
| 1512 | nla_memcpy(&parms->laddr, data[IFLA_GRE_LOCAL], sizeof(struct in6_addr)); |
| 1513 | |
| 1514 | if (data[IFLA_GRE_REMOTE]) |
| 1515 | nla_memcpy(&parms->raddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr)); |
| 1516 | |
| 1517 | if (data[IFLA_GRE_TTL]) |
| 1518 | parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]); |
| 1519 | |
| 1520 | if (data[IFLA_GRE_ENCAP_LIMIT]) |
| 1521 | parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]); |
| 1522 | |
| 1523 | if (data[IFLA_GRE_FLOWINFO]) |
| 1524 | parms->flowinfo = nla_get_u32(data[IFLA_GRE_FLOWINFO]); |
| 1525 | |
| 1526 | if (data[IFLA_GRE_FLAGS]) |
| 1527 | parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]); |
| 1528 | } |
| 1529 | |
| 1530 | static int ip6gre_tap_init(struct net_device *dev) |
| 1531 | { |
| 1532 | struct ip6_tnl *tunnel; |
| 1533 | |
| 1534 | tunnel = netdev_priv(dev); |
| 1535 | |
| 1536 | tunnel->dev = dev; |
| 1537 | strcpy(tunnel->parms.name, dev->name); |
| 1538 | |
| 1539 | ip6gre_tnl_link_config(tunnel, 1); |
| 1540 | |
| 1541 | dev->tstats = alloc_percpu(struct pcpu_tstats); |
| 1542 | if (!dev->tstats) |
| 1543 | return -ENOMEM; |
| 1544 | |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
| 1548 | static const struct net_device_ops ip6gre_tap_netdev_ops = { |
| 1549 | .ndo_init = ip6gre_tap_init, |
| 1550 | .ndo_uninit = ip6gre_tunnel_uninit, |
| 1551 | .ndo_start_xmit = ip6gre_tunnel_xmit, |
| 1552 | .ndo_set_mac_address = eth_mac_addr, |
| 1553 | .ndo_validate_addr = eth_validate_addr, |
| 1554 | .ndo_change_mtu = ip6gre_tunnel_change_mtu, |
| 1555 | .ndo_get_stats64 = ip6gre_get_stats64, |
| 1556 | }; |
| 1557 | |
| 1558 | static void ip6gre_tap_setup(struct net_device *dev) |
| 1559 | { |
| 1560 | |
| 1561 | ether_setup(dev); |
| 1562 | |
| 1563 | dev->netdev_ops = &ip6gre_tap_netdev_ops; |
| 1564 | dev->destructor = ip6gre_dev_free; |
| 1565 | |
| 1566 | dev->iflink = 0; |
| 1567 | dev->features |= NETIF_F_NETNS_LOCAL; |
| 1568 | } |
| 1569 | |
| 1570 | static int ip6gre_newlink(struct net *src_net, struct net_device *dev, |
| 1571 | struct nlattr *tb[], struct nlattr *data[]) |
| 1572 | { |
| 1573 | struct ip6_tnl *nt; |
| 1574 | struct net *net = dev_net(dev); |
| 1575 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1576 | int err; |
| 1577 | |
| 1578 | nt = netdev_priv(dev); |
| 1579 | ip6gre_netlink_parms(data, &nt->parms); |
| 1580 | |
| 1581 | if (ip6gre_tunnel_find(net, &nt->parms, dev->type)) |
| 1582 | return -EEXIST; |
| 1583 | |
| 1584 | if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS]) |
| 1585 | eth_hw_addr_random(dev); |
| 1586 | |
| 1587 | nt->dev = dev; |
| 1588 | ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]); |
| 1589 | |
| 1590 | /* Can use a lockless transmit, unless we generate output sequences */ |
| 1591 | if (!(nt->parms.o_flags & GRE_SEQ)) |
| 1592 | dev->features |= NETIF_F_LLTX; |
| 1593 | |
| 1594 | err = register_netdevice(dev); |
| 1595 | if (err) |
| 1596 | goto out; |
| 1597 | |
| 1598 | dev_hold(dev); |
| 1599 | ip6gre_tunnel_link(ign, nt); |
| 1600 | |
| 1601 | out: |
| 1602 | return err; |
| 1603 | } |
| 1604 | |
| 1605 | static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[], |
| 1606 | struct nlattr *data[]) |
| 1607 | { |
| 1608 | struct ip6_tnl *t, *nt; |
| 1609 | struct net *net = dev_net(dev); |
| 1610 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1611 | struct __ip6_tnl_parm p; |
| 1612 | |
| 1613 | if (dev == ign->fb_tunnel_dev) |
| 1614 | return -EINVAL; |
| 1615 | |
| 1616 | nt = netdev_priv(dev); |
| 1617 | ip6gre_netlink_parms(data, &p); |
| 1618 | |
| 1619 | t = ip6gre_tunnel_locate(net, &p, 0); |
| 1620 | |
| 1621 | if (t) { |
| 1622 | if (t->dev != dev) |
| 1623 | return -EEXIST; |
| 1624 | } else { |
| 1625 | t = nt; |
| 1626 | |
| 1627 | ip6gre_tunnel_unlink(ign, t); |
| 1628 | ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]); |
| 1629 | ip6gre_tunnel_link(ign, t); |
| 1630 | netdev_state_change(dev); |
| 1631 | } |
| 1632 | |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | static size_t ip6gre_get_size(const struct net_device *dev) |
| 1637 | { |
| 1638 | return |
| 1639 | /* IFLA_GRE_LINK */ |
| 1640 | nla_total_size(4) + |
| 1641 | /* IFLA_GRE_IFLAGS */ |
| 1642 | nla_total_size(2) + |
| 1643 | /* IFLA_GRE_OFLAGS */ |
| 1644 | nla_total_size(2) + |
| 1645 | /* IFLA_GRE_IKEY */ |
| 1646 | nla_total_size(4) + |
| 1647 | /* IFLA_GRE_OKEY */ |
| 1648 | nla_total_size(4) + |
| 1649 | /* IFLA_GRE_LOCAL */ |
| 1650 | nla_total_size(4) + |
| 1651 | /* IFLA_GRE_REMOTE */ |
| 1652 | nla_total_size(4) + |
| 1653 | /* IFLA_GRE_TTL */ |
| 1654 | nla_total_size(1) + |
| 1655 | /* IFLA_GRE_TOS */ |
| 1656 | nla_total_size(1) + |
| 1657 | /* IFLA_GRE_ENCAP_LIMIT */ |
| 1658 | nla_total_size(1) + |
| 1659 | /* IFLA_GRE_FLOWINFO */ |
| 1660 | nla_total_size(4) + |
| 1661 | /* IFLA_GRE_FLAGS */ |
| 1662 | nla_total_size(4) + |
| 1663 | 0; |
| 1664 | } |
| 1665 | |
| 1666 | static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 1667 | { |
| 1668 | struct ip6_tnl *t = netdev_priv(dev); |
| 1669 | struct __ip6_tnl_parm *p = &t->parms; |
| 1670 | |
| 1671 | if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || |
| 1672 | nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) || |
| 1673 | nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) || |
| 1674 | nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || |
| 1675 | nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || |
| 1676 | nla_put(skb, IFLA_GRE_LOCAL, sizeof(struct in6_addr), &p->raddr) || |
| 1677 | nla_put(skb, IFLA_GRE_REMOTE, sizeof(struct in6_addr), &p->laddr) || |
| 1678 | nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) || |
| 1679 | /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/ |
| 1680 | nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) || |
| 1681 | nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) || |
| 1682 | nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags)) |
| 1683 | goto nla_put_failure; |
| 1684 | return 0; |
| 1685 | |
| 1686 | nla_put_failure: |
| 1687 | return -EMSGSIZE; |
| 1688 | } |
| 1689 | |
| 1690 | static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = { |
| 1691 | [IFLA_GRE_LINK] = { .type = NLA_U32 }, |
| 1692 | [IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, |
| 1693 | [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, |
| 1694 | [IFLA_GRE_IKEY] = { .type = NLA_U32 }, |
| 1695 | [IFLA_GRE_OKEY] = { .type = NLA_U32 }, |
| 1696 | [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) }, |
| 1697 | [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) }, |
| 1698 | [IFLA_GRE_TTL] = { .type = NLA_U8 }, |
| 1699 | [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 }, |
| 1700 | [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 }, |
| 1701 | [IFLA_GRE_FLAGS] = { .type = NLA_U32 }, |
| 1702 | }; |
| 1703 | |
| 1704 | static struct rtnl_link_ops ip6gre_link_ops __read_mostly = { |
| 1705 | .kind = "ip6gre", |
| 1706 | .maxtype = IFLA_GRE_MAX, |
| 1707 | .policy = ip6gre_policy, |
| 1708 | .priv_size = sizeof(struct ip6_tnl), |
| 1709 | .setup = ip6gre_tunnel_setup, |
| 1710 | .validate = ip6gre_tunnel_validate, |
| 1711 | .newlink = ip6gre_newlink, |
| 1712 | .changelink = ip6gre_changelink, |
| 1713 | .get_size = ip6gre_get_size, |
| 1714 | .fill_info = ip6gre_fill_info, |
| 1715 | }; |
| 1716 | |
| 1717 | static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = { |
| 1718 | .kind = "ip6gretap", |
| 1719 | .maxtype = IFLA_GRE_MAX, |
| 1720 | .policy = ip6gre_policy, |
| 1721 | .priv_size = sizeof(struct ip6_tnl), |
| 1722 | .setup = ip6gre_tap_setup, |
| 1723 | .validate = ip6gre_tap_validate, |
| 1724 | .newlink = ip6gre_newlink, |
| 1725 | .changelink = ip6gre_changelink, |
| 1726 | .get_size = ip6gre_get_size, |
| 1727 | .fill_info = ip6gre_fill_info, |
| 1728 | }; |
| 1729 | |
| 1730 | /* |
| 1731 | * And now the modules code and kernel interface. |
| 1732 | */ |
| 1733 | |
| 1734 | static int __init ip6gre_init(void) |
| 1735 | { |
| 1736 | int err; |
| 1737 | |
| 1738 | pr_info("GRE over IPv6 tunneling driver\n"); |
| 1739 | |
| 1740 | err = register_pernet_device(&ip6gre_net_ops); |
| 1741 | if (err < 0) |
| 1742 | return err; |
| 1743 | |
| 1744 | err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE); |
| 1745 | if (err < 0) { |
| 1746 | pr_info("%s: can't add protocol\n", __func__); |
| 1747 | goto add_proto_failed; |
| 1748 | } |
| 1749 | |
| 1750 | err = rtnl_link_register(&ip6gre_link_ops); |
| 1751 | if (err < 0) |
| 1752 | goto rtnl_link_failed; |
| 1753 | |
| 1754 | err = rtnl_link_register(&ip6gre_tap_ops); |
| 1755 | if (err < 0) |
| 1756 | goto tap_ops_failed; |
| 1757 | |
| 1758 | out: |
| 1759 | return err; |
| 1760 | |
| 1761 | tap_ops_failed: |
| 1762 | rtnl_link_unregister(&ip6gre_link_ops); |
| 1763 | rtnl_link_failed: |
| 1764 | inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); |
| 1765 | add_proto_failed: |
| 1766 | unregister_pernet_device(&ip6gre_net_ops); |
| 1767 | goto out; |
| 1768 | } |
| 1769 | |
| 1770 | static void __exit ip6gre_fini(void) |
| 1771 | { |
| 1772 | rtnl_link_unregister(&ip6gre_tap_ops); |
| 1773 | rtnl_link_unregister(&ip6gre_link_ops); |
| 1774 | inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); |
| 1775 | unregister_pernet_device(&ip6gre_net_ops); |
| 1776 | } |
| 1777 | |
| 1778 | module_init(ip6gre_init); |
| 1779 | module_exit(ip6gre_fini); |
| 1780 | MODULE_LICENSE("GPL"); |
| 1781 | MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)"); |
| 1782 | MODULE_DESCRIPTION("GRE over IPv6 tunneling device"); |
| 1783 | MODULE_ALIAS_RTNL_LINK("ip6gre"); |
| 1784 | MODULE_ALIAS_NETDEV("ip6gre0"); |