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