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