2 * GRE over IPv6 protocol decoder.
4 * Authors: Dmitry Kozlov (xeb@mail.ru)
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.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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>
42 #include <net/protocol.h>
43 #include <net/addrconf.h>
45 #include <net/checksum.h>
46 #include <net/dsfield.h>
47 #include <net/inet_ecn.h>
49 #include <net/net_namespace.h>
50 #include <net/netns/generic.h>
51 #include <net/rtnetlink.h>
54 #include <net/ip6_fib.h>
55 #include <net/ip6_route.h>
56 #include <net/ip6_tunnel.h>
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");
63 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
64 #define IPV6_TCLASS_SHIFT 20
66 #define HASH_SIZE_SHIFT 5
67 #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
69 static int ip6gre_net_id __read_mostly
;
71 struct ip6_tnl __rcu
*tunnels
[4][HASH_SIZE
];
73 struct net_device
*fb_tunnel_dev
;
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
);
82 /* Tunnel hash table */
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.
96 All keysless packets, if not matched configured keyless tunnels
97 will match fallback tunnel.
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
)
103 u32 hash
= ipv6_addr_hash(addr
);
105 return hash_32(hash
, HASH_SIZE_SHIFT
);
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]
113 static struct rtnl_link_stats64
*ip6gre_get_stats64(struct net_device
*dev
,
114 struct rtnl_link_stats64
*tot
)
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
;
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
));
131 tot
->rx_packets
+= rx_packets
;
132 tot
->tx_packets
+= tx_packets
;
133 tot
->rx_bytes
+= rx_bytes
;
134 tot
->tx_bytes
+= tx_bytes
;
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
;
141 tot
->rx_frame_errors
= dev
->stats
.rx_frame_errors
;
142 tot
->rx_errors
= dev
->stats
.rx_errors
;
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
;
153 /* Given src, dst and key, find appropriate for input tunnel. */
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
)
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;
169 for_each_ip_tunnel_rcu(t
, ign
->tunnels_r_l
[h0
^ h1
]) {
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
))
176 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
177 t
->dev
->type
!= dev_type
)
181 if (t
->parms
.link
!= link
)
183 if (t
->dev
->type
!= dev_type
)
188 if (score
< cand_score
) {
194 for_each_ip_tunnel_rcu(t
, ign
->tunnels_r
[h0
^ h1
]) {
195 if (!ipv6_addr_equal(remote
, &t
->parms
.raddr
) ||
196 key
!= t
->parms
.i_key
||
197 !(t
->dev
->flags
& IFF_UP
))
200 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
201 t
->dev
->type
!= dev_type
)
205 if (t
->parms
.link
!= link
)
207 if (t
->dev
->type
!= dev_type
)
212 if (score
< cand_score
) {
218 for_each_ip_tunnel_rcu(t
, ign
->tunnels_l
[h1
]) {
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
))
226 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
227 t
->dev
->type
!= dev_type
)
231 if (t
->parms
.link
!= link
)
233 if (t
->dev
->type
!= dev_type
)
238 if (score
< cand_score
) {
244 for_each_ip_tunnel_rcu(t
, ign
->tunnels_wc
[h1
]) {
245 if (t
->parms
.i_key
!= key
||
246 !(t
->dev
->flags
& IFF_UP
))
249 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
250 t
->dev
->type
!= dev_type
)
254 if (t
->parms
.link
!= link
)
256 if (t
->dev
->type
!= dev_type
)
261 if (score
< cand_score
) {
270 dev
= ign
->fb_tunnel_dev
;
271 if (dev
->flags
& IFF_UP
)
272 return netdev_priv(dev
);
277 static struct ip6_tnl __rcu
**__ip6gre_bucket(struct ip6gre_net
*ign
,
278 const struct __ip6_tnl_parm
*p
)
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
);
285 if (!ipv6_addr_any(local
))
287 if (!ipv6_addr_any(remote
) && !ipv6_addr_is_multicast(remote
)) {
289 h
^= HASH_ADDR(remote
);
292 return &ign
->tunnels
[prio
][h
];
295 static inline struct ip6_tnl __rcu
**ip6gre_bucket(struct ip6gre_net
*ign
,
296 const struct ip6_tnl
*t
)
298 return __ip6gre_bucket(ign
, &t
->parms
);
301 static void ip6gre_tunnel_link(struct ip6gre_net
*ign
, struct ip6_tnl
*t
)
303 struct ip6_tnl __rcu
**tp
= ip6gre_bucket(ign
, t
);
305 rcu_assign_pointer(t
->next
, rtnl_dereference(*tp
));
306 rcu_assign_pointer(*tp
, t
);
309 static void ip6gre_tunnel_unlink(struct ip6gre_net
*ign
, struct ip6_tnl
*t
)
311 struct ip6_tnl __rcu
**tp
;
312 struct ip6_tnl
*iter
;
314 for (tp
= ip6gre_bucket(ign
, t
);
315 (iter
= rtnl_dereference(*tp
)) != NULL
;
318 rcu_assign_pointer(*tp
, t
->next
);
324 static struct ip6_tnl
*ip6gre_tunnel_find(struct net
*net
,
325 const struct __ip6_tnl_parm
*parms
,
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
;
333 struct ip6_tnl __rcu
**tp
;
334 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
336 for (tp
= __ip6gre_bucket(ign
, parms
);
337 (t
= rtnl_dereference(*tp
)) != NULL
;
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
)
349 static struct ip6_tnl
*ip6gre_tunnel_locate(struct net
*net
,
350 const struct __ip6_tnl_parm
*parms
, int create
)
352 struct ip6_tnl
*t
, *nt
;
353 struct net_device
*dev
;
355 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
357 t
= ip6gre_tunnel_find(net
, parms
, ARPHRD_IP6GRE
);
362 strlcpy(name
, parms
->name
, IFNAMSIZ
);
364 strcpy(name
, "ip6gre%d");
366 dev
= alloc_netdev(sizeof(*t
), name
, ip6gre_tunnel_setup
);
370 dev_net_set(dev
, net
);
372 nt
= netdev_priv(dev
);
374 dev
->rtnl_link_ops
= &ip6gre_link_ops
;
377 ip6gre_tnl_link_config(nt
, 1);
379 if (register_netdevice(dev
) < 0)
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
;
387 ip6gre_tunnel_link(ign
, nt
);
395 static void ip6gre_tunnel_uninit(struct net_device
*dev
)
397 struct net
*net
= dev_net(dev
);
398 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
400 ip6gre_tunnel_unlink(ign
, netdev_priv(dev
));
405 static void ip6gre_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
406 u8 type
, u8 code
, int offset
, __be32 info
)
408 const struct ipv6hdr
*ipv6h
= (const struct ipv6hdr
*)skb
->data
;
409 __be16
*p
= (__be16
*)(skb
->data
+ offset
);
410 int grehlen
= offset
+ 4;
415 if (flags
&(GRE_CSUM
|GRE_KEY
|GRE_SEQ
|GRE_ROUTING
|GRE_VERSION
)) {
416 if (flags
&(GRE_VERSION
|GRE_ROUTING
))
425 /* If only 8 bytes returned, keyed message will be dropped here */
426 if (!pskb_may_pull(skb
, grehlen
))
428 ipv6h
= (const struct ipv6hdr
*)skb
->data
;
429 p
= (__be16
*)(skb
->data
+ offset
);
431 t
= ip6gre_tunnel_lookup(skb
->dev
, &ipv6h
->daddr
, &ipv6h
->saddr
,
433 *(((__be32
*)p
) + (grehlen
/ 4) - 1) : 0,
440 struct ipv6_tlv_tnl_enc_lim
*tel
;
442 case ICMPV6_DEST_UNREACH
:
443 net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
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",
452 case ICMPV6_PARAMPROB
:
454 if (code
== ICMPV6_HDR_FIELD
)
455 teli
= ip6_tnl_parse_tlv_enc_lim(skb
, skb
->data
);
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",
464 net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
468 case ICMPV6_PKT_TOOBIG
:
470 if (mtu
< IPV6_MIN_MTU
)
476 if (time_before(jiffies
, t
->err_time
+ IP6TUNNEL_ERR_TIMEO
))
480 t
->err_time
= jiffies
;
483 static int ip6gre_rcv(struct sk_buff
*skb
)
485 const struct ipv6hdr
*ipv6h
;
491 struct ip6_tnl
*tunnel
;
496 if (!pskb_may_pull(skb
, sizeof(struct in6_addr
)))
499 ipv6h
= ipv6_hdr(skb
);
501 flags
= *(__be16
*)h
;
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.
507 if (flags
&(GRE_VERSION
|GRE_ROUTING
))
510 if (flags
&GRE_CSUM
) {
511 switch (skb
->ip_summed
) {
512 case CHECKSUM_COMPLETE
:
513 csum
= csum_fold(skb
->csum
);
519 csum
= __skb_checksum_complete(skb
);
520 skb
->ip_summed
= CHECKSUM_COMPLETE
;
525 key
= *(__be32
*)(h
+ offset
);
529 seqno
= ntohl(*(__be32
*)(h
+ offset
));
534 gre_proto
= *(__be16
*)(h
+ 2);
536 tunnel
= ip6gre_tunnel_lookup(skb
->dev
,
537 &ipv6h
->saddr
, &ipv6h
->daddr
, key
,
540 struct pcpu_tstats
*tstats
;
542 if (!xfrm6_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
545 if (!ip6_tnl_rcv_ctl(tunnel
, &ipv6h
->daddr
, &ipv6h
->saddr
)) {
546 tunnel
->dev
->stats
.rx_dropped
++;
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
557 if (flags
== 0 && gre_proto
== htons(ETH_P_WCCP
)) {
558 skb
->protocol
= htons(ETH_P_IP
);
559 if ((*(h
+ offset
) & 0xF0) != 0x40)
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
;
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
++;
574 if (tunnel
->parms
.i_flags
&GRE_SEQ
) {
575 if (!(flags
&GRE_SEQ
) ||
577 (s32
)(seqno
- tunnel
->i_seqno
) < 0)) {
578 tunnel
->dev
->stats
.rx_fifo_errors
++;
579 tunnel
->dev
->stats
.rx_errors
++;
582 tunnel
->i_seqno
= seqno
+ 1;
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
++;
593 ipv6h
= ipv6_hdr(skb
);
594 skb
->protocol
= eth_type_trans(skb
, tunnel
->dev
);
595 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
598 __skb_tunnel_rx(skb
, tunnel
->dev
);
600 skb_reset_network_header(skb
);
602 err
= IP6_ECN_decapsulate(ipv6h
, skb
);
605 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
607 ipv6_get_dsfield(ipv6h
));
609 ++tunnel
->dev
->stats
.rx_frame_errors
;
610 ++tunnel
->dev
->stats
.rx_errors
;
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
);
625 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
632 struct ipv6_tel_txoption
{
633 struct ipv6_txoptions ops
;
637 static void init_tel_txopt(struct ipv6_tel_txoption
*opt
, __u8 encap_limit
)
639 memset(opt
, 0, sizeof(struct ipv6_tel_txoption
));
641 opt
->dst_opt
[2] = IPV6_TLV_TNL_ENCAP_LIMIT
;
643 opt
->dst_opt
[4] = encap_limit
;
644 opt
->dst_opt
[5] = IPV6_TLV_PADN
;
647 opt
->ops
.dst0opt
= (struct ipv6_opt_hdr
*) opt
->dst_opt
;
648 opt
->ops
.opt_nflen
= 8;
651 static netdev_tx_t
ip6gre_xmit2(struct sk_buff
*skb
,
652 struct net_device
*dev
,
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 */
664 struct ipv6_tel_txoption opt
;
666 struct dst_entry
*dst
= NULL
, *ndst
= NULL
;
667 struct net_device_stats
*stats
= &tunnel
->dev
->stats
;
671 struct sk_buff
*new_skb
;
673 if (dev
->type
== ARPHRD_ETHER
)
674 IPCB(skb
)->flags
= 0;
676 if (dev
->header_ops
&& dev
->type
== ARPHRD_IP6GRE
) {
678 ipv6h
= (struct ipv6hdr
*)skb
->data
;
679 fl6
->daddr
= ipv6h
->daddr
;
681 gre_hlen
= tunnel
->hlen
;
682 fl6
->daddr
= tunnel
->parms
.raddr
;
685 if (!fl6
->flowi6_mark
)
686 dst
= ip6_tnl_dst_check(tunnel
);
689 ndst
= ip6_route_output(net
, NULL
, fl6
);
692 goto tx_err_link_failure
;
693 ndst
= xfrm_lookup(net
, ndst
, flowi6_to_flowi(fl6
), NULL
, 0);
697 goto tx_err_link_failure
;
706 net_warn_ratelimited("%s: Local routing loop detected!\n",
708 goto tx_err_dst_release
;
711 mtu
= dst_mtu(dst
) - sizeof(*ipv6h
);
712 if (encap_limit
>= 0) {
716 if (mtu
< IPV6_MIN_MTU
)
719 skb_dst(skb
)->ops
->update_pmtu(skb_dst(skb
), NULL
, skb
, mtu
);
720 if (skb
->len
> mtu
) {
723 goto tx_err_dst_release
;
726 if (tunnel
->err_count
> 0) {
727 if (time_before(jiffies
,
728 tunnel
->err_time
+ IP6TUNNEL_ERR_TIMEO
)) {
731 dst_link_failure(skb
);
733 tunnel
->err_count
= 0;
736 max_headroom
= LL_RESERVED_SPACE(tdev
) + gre_hlen
+ dst
->header_len
;
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
;
744 goto tx_err_dst_release
;
747 skb_set_owner_w(new_skb
, skb
->sk
);
754 if (fl6
->flowi6_mark
) {
755 skb_dst_set(skb
, dst
);
758 skb_dst_set_noref(skb
, dst
);
762 if (encap_limit
>= 0) {
763 init_tel_txopt(&opt
, encap_limit
);
764 ipv6_push_nfrag_opts(skb
, &opt
.ops
, &proto
, NULL
);
767 skb_push(skb
, gre_hlen
);
768 skb_reset_network_header(skb
);
769 skb_set_transport_header(skb
, sizeof(*ipv6h
));
772 * Push down and install the IP header.
774 ipv6h
= ipv6_hdr(skb
);
775 *(__be32
*)ipv6h
= fl6
->flowlabel
| htonl(0x60000000);
776 dsfield
= INET_ECN_encapsulate(0, dsfield
);
777 ipv6_change_dsfield(ipv6h
, ~INET_ECN_MASK
, dsfield
);
778 ipv6h
->hop_limit
= tunnel
->parms
.hop_limit
;
779 ipv6h
->nexthdr
= proto
;
780 ipv6h
->saddr
= fl6
->saddr
;
781 ipv6h
->daddr
= fl6
->daddr
;
783 ((__be16
*)(ipv6h
+ 1))[0] = tunnel
->parms
.o_flags
;
784 ((__be16
*)(ipv6h
+ 1))[1] = (dev
->type
== ARPHRD_ETHER
) ?
785 htons(ETH_P_TEB
) : skb
->protocol
;
787 if (tunnel
->parms
.o_flags
&(GRE_KEY
|GRE_CSUM
|GRE_SEQ
)) {
788 __be32
*ptr
= (__be32
*)(((u8
*)ipv6h
) + tunnel
->hlen
- 4);
790 if (tunnel
->parms
.o_flags
&GRE_SEQ
) {
792 *ptr
= htonl(tunnel
->o_seqno
);
795 if (tunnel
->parms
.o_flags
&GRE_KEY
) {
796 *ptr
= tunnel
->parms
.o_key
;
799 if (tunnel
->parms
.o_flags
&GRE_CSUM
) {
801 *(__sum16
*)ptr
= ip_compute_csum((void *)(ipv6h
+1),
802 skb
->len
- sizeof(struct ipv6hdr
));
808 err
= ip6_local_out(skb
);
810 if (net_xmit_eval(err
) == 0) {
811 struct pcpu_tstats
*tstats
= this_cpu_ptr(tunnel
->dev
->tstats
);
813 tstats
->tx_bytes
+= pkt_len
;
814 tstats
->tx_packets
++;
817 stats
->tx_aborted_errors
++;
821 ip6_tnl_dst_store(tunnel
, ndst
);
825 stats
->tx_carrier_errors
++;
826 dst_link_failure(skb
);
832 static inline int ip6gre_xmit_ipv4(struct sk_buff
*skb
, struct net_device
*dev
)
834 struct ip6_tnl
*t
= netdev_priv(dev
);
835 const struct iphdr
*iph
= ip_hdr(skb
);
836 int encap_limit
= -1;
842 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
843 encap_limit
= t
->parms
.encap_limit
;
845 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
846 fl6
.flowi6_proto
= IPPROTO_IPIP
;
848 dsfield
= ipv4_get_dsfield(iph
);
850 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
851 fl6
.flowlabel
|= htonl((__u32
)iph
->tos
<< IPV6_TCLASS_SHIFT
)
853 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
854 fl6
.flowi6_mark
= skb
->mark
;
856 err
= ip6gre_xmit2(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
);
858 /* XXX: send ICMP error even if DF is not set. */
859 if (err
== -EMSGSIZE
)
860 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
,
868 static inline int ip6gre_xmit_ipv6(struct sk_buff
*skb
, struct net_device
*dev
)
870 struct ip6_tnl
*t
= netdev_priv(dev
);
871 struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
872 int encap_limit
= -1;
879 if (ipv6_addr_equal(&t
->parms
.raddr
, &ipv6h
->saddr
))
882 offset
= ip6_tnl_parse_tlv_enc_lim(skb
, skb_network_header(skb
));
884 struct ipv6_tlv_tnl_enc_lim
*tel
;
885 tel
= (struct ipv6_tlv_tnl_enc_lim
*)&skb_network_header(skb
)[offset
];
886 if (tel
->encap_limit
== 0) {
887 icmpv6_send(skb
, ICMPV6_PARAMPROB
,
888 ICMPV6_HDR_FIELD
, offset
+ 2);
891 encap_limit
= tel
->encap_limit
- 1;
892 } else if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
893 encap_limit
= t
->parms
.encap_limit
;
895 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
896 fl6
.flowi6_proto
= IPPROTO_IPV6
;
898 dsfield
= ipv6_get_dsfield(ipv6h
);
899 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
900 fl6
.flowlabel
|= (*(__be32
*) ipv6h
& IPV6_TCLASS_MASK
);
901 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
)
902 fl6
.flowlabel
|= (*(__be32
*) ipv6h
& IPV6_FLOWLABEL_MASK
);
903 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
904 fl6
.flowi6_mark
= skb
->mark
;
906 err
= ip6gre_xmit2(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
);
908 if (err
== -EMSGSIZE
)
909 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
917 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
918 * @t: the outgoing tunnel device
919 * @hdr: IPv6 header from the incoming packet
922 * Avoid trivial tunneling loop by checking that tunnel exit-point
923 * doesn't match source of incoming packet.
930 static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl
*t
,
931 const struct ipv6hdr
*hdr
)
933 return ipv6_addr_equal(&t
->parms
.raddr
, &hdr
->saddr
);
936 static int ip6gre_xmit_other(struct sk_buff
*skb
, struct net_device
*dev
)
938 struct ip6_tnl
*t
= netdev_priv(dev
);
939 int encap_limit
= -1;
944 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
945 encap_limit
= t
->parms
.encap_limit
;
947 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
948 fl6
.flowi6_proto
= skb
->protocol
;
950 err
= ip6gre_xmit2(skb
, dev
, 0, &fl6
, encap_limit
, &mtu
);
955 static netdev_tx_t
ip6gre_tunnel_xmit(struct sk_buff
*skb
,
956 struct net_device
*dev
)
958 struct ip6_tnl
*t
= netdev_priv(dev
);
959 struct net_device_stats
*stats
= &t
->dev
->stats
;
962 if (!ip6_tnl_xmit_ctl(t
))
965 switch (skb
->protocol
) {
966 case htons(ETH_P_IP
):
967 ret
= ip6gre_xmit_ipv4(skb
, dev
);
969 case htons(ETH_P_IPV6
):
970 ret
= ip6gre_xmit_ipv6(skb
, dev
);
973 ret
= ip6gre_xmit_other(skb
, dev
);
989 static void ip6gre_tnl_link_config(struct ip6_tnl
*t
, int set_mtu
)
991 struct net_device
*dev
= t
->dev
;
992 struct __ip6_tnl_parm
*p
= &t
->parms
;
993 struct flowi6
*fl6
= &t
->fl
.u
.ip6
;
994 int addend
= sizeof(struct ipv6hdr
) + 4;
996 if (dev
->type
!= ARPHRD_ETHER
) {
997 memcpy(dev
->dev_addr
, &p
->laddr
, sizeof(struct in6_addr
));
998 memcpy(dev
->broadcast
, &p
->raddr
, sizeof(struct in6_addr
));
1001 /* Set up flowi template */
1002 fl6
->saddr
= p
->laddr
;
1003 fl6
->daddr
= p
->raddr
;
1004 fl6
->flowi6_oif
= p
->link
;
1007 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_TCLASS
))
1008 fl6
->flowlabel
|= IPV6_TCLASS_MASK
& p
->flowinfo
;
1009 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_FLOWLABEL
))
1010 fl6
->flowlabel
|= IPV6_FLOWLABEL_MASK
& p
->flowinfo
;
1012 p
->flags
&= ~(IP6_TNL_F_CAP_XMIT
|IP6_TNL_F_CAP_RCV
|IP6_TNL_F_CAP_PER_PACKET
);
1013 p
->flags
|= ip6_tnl_get_cap(t
, &p
->laddr
, &p
->raddr
);
1015 if (p
->flags
&IP6_TNL_F_CAP_XMIT
&&
1016 p
->flags
&IP6_TNL_F_CAP_RCV
&& dev
->type
!= ARPHRD_ETHER
)
1017 dev
->flags
|= IFF_POINTOPOINT
;
1019 dev
->flags
&= ~IFF_POINTOPOINT
;
1021 dev
->iflink
= p
->link
;
1023 /* Precalculate GRE options length */
1024 if (t
->parms
.o_flags
&(GRE_CSUM
|GRE_KEY
|GRE_SEQ
)) {
1025 if (t
->parms
.o_flags
&GRE_CSUM
)
1027 if (t
->parms
.o_flags
&GRE_KEY
)
1029 if (t
->parms
.o_flags
&GRE_SEQ
)
1033 if (p
->flags
& IP6_TNL_F_CAP_XMIT
) {
1034 int strict
= (ipv6_addr_type(&p
->raddr
) &
1035 (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
));
1037 struct rt6_info
*rt
= rt6_lookup(dev_net(dev
),
1038 &p
->raddr
, &p
->laddr
,
1045 dev
->hard_header_len
= rt
->dst
.dev
->hard_header_len
+ addend
;
1048 dev
->mtu
= rt
->dst
.dev
->mtu
- addend
;
1049 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1052 if (dev
->mtu
< IPV6_MIN_MTU
)
1053 dev
->mtu
= IPV6_MIN_MTU
;
1062 static int ip6gre_tnl_change(struct ip6_tnl
*t
,
1063 const struct __ip6_tnl_parm
*p
, int set_mtu
)
1065 t
->parms
.laddr
= p
->laddr
;
1066 t
->parms
.raddr
= p
->raddr
;
1067 t
->parms
.flags
= p
->flags
;
1068 t
->parms
.hop_limit
= p
->hop_limit
;
1069 t
->parms
.encap_limit
= p
->encap_limit
;
1070 t
->parms
.flowinfo
= p
->flowinfo
;
1071 t
->parms
.link
= p
->link
;
1072 t
->parms
.proto
= p
->proto
;
1073 t
->parms
.i_key
= p
->i_key
;
1074 t
->parms
.o_key
= p
->o_key
;
1075 t
->parms
.i_flags
= p
->i_flags
;
1076 t
->parms
.o_flags
= p
->o_flags
;
1077 ip6_tnl_dst_reset(t
);
1078 ip6gre_tnl_link_config(t
, set_mtu
);
1082 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm
*p
,
1083 const struct ip6_tnl_parm2
*u
)
1085 p
->laddr
= u
->laddr
;
1086 p
->raddr
= u
->raddr
;
1087 p
->flags
= u
->flags
;
1088 p
->hop_limit
= u
->hop_limit
;
1089 p
->encap_limit
= u
->encap_limit
;
1090 p
->flowinfo
= u
->flowinfo
;
1092 p
->i_key
= u
->i_key
;
1093 p
->o_key
= u
->o_key
;
1094 p
->i_flags
= u
->i_flags
;
1095 p
->o_flags
= u
->o_flags
;
1096 memcpy(p
->name
, u
->name
, sizeof(u
->name
));
1099 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2
*u
,
1100 const struct __ip6_tnl_parm
*p
)
1102 u
->proto
= IPPROTO_GRE
;
1103 u
->laddr
= p
->laddr
;
1104 u
->raddr
= p
->raddr
;
1105 u
->flags
= p
->flags
;
1106 u
->hop_limit
= p
->hop_limit
;
1107 u
->encap_limit
= p
->encap_limit
;
1108 u
->flowinfo
= p
->flowinfo
;
1110 u
->i_key
= p
->i_key
;
1111 u
->o_key
= p
->o_key
;
1112 u
->i_flags
= p
->i_flags
;
1113 u
->o_flags
= p
->o_flags
;
1114 memcpy(u
->name
, p
->name
, sizeof(u
->name
));
1117 static int ip6gre_tunnel_ioctl(struct net_device
*dev
,
1118 struct ifreq
*ifr
, int cmd
)
1121 struct ip6_tnl_parm2 p
;
1122 struct __ip6_tnl_parm p1
;
1124 struct net
*net
= dev_net(dev
);
1125 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1130 if (dev
== ign
->fb_tunnel_dev
) {
1131 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
))) {
1135 ip6gre_tnl_parm_from_user(&p1
, &p
);
1136 t
= ip6gre_tunnel_locate(net
, &p1
, 0);
1139 t
= netdev_priv(dev
);
1140 ip6gre_tnl_parm_to_user(&p
, &t
->parms
);
1141 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof(p
)))
1148 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1152 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
1156 if ((p
.i_flags
|p
.o_flags
)&(GRE_VERSION
|GRE_ROUTING
))
1159 if (!(p
.i_flags
&GRE_KEY
))
1161 if (!(p
.o_flags
&GRE_KEY
))
1164 ip6gre_tnl_parm_from_user(&p1
, &p
);
1165 t
= ip6gre_tunnel_locate(net
, &p1
, cmd
== SIOCADDTUNNEL
);
1167 if (dev
!= ign
->fb_tunnel_dev
&& cmd
== SIOCCHGTUNNEL
) {
1169 if (t
->dev
!= dev
) {
1174 t
= netdev_priv(dev
);
1176 ip6gre_tunnel_unlink(ign
, t
);
1178 ip6gre_tnl_change(t
, &p1
, 1);
1179 ip6gre_tunnel_link(ign
, t
);
1180 netdev_state_change(dev
);
1187 ip6gre_tnl_parm_to_user(&p
, &t
->parms
);
1188 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof(p
)))
1191 err
= (cmd
== SIOCADDTUNNEL
? -ENOBUFS
: -ENOENT
);
1196 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1199 if (dev
== ign
->fb_tunnel_dev
) {
1201 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
1204 ip6gre_tnl_parm_from_user(&p1
, &p
);
1205 t
= ip6gre_tunnel_locate(net
, &p1
, 0);
1209 if (t
== netdev_priv(ign
->fb_tunnel_dev
))
1213 unregister_netdevice(dev
);
1225 static int ip6gre_tunnel_change_mtu(struct net_device
*dev
, int new_mtu
)
1227 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
1229 new_mtu
> 0xFFF8 - dev
->hard_header_len
- tunnel
->hlen
)
1235 static int ip6gre_header(struct sk_buff
*skb
, struct net_device
*dev
,
1236 unsigned short type
,
1237 const void *daddr
, const void *saddr
, unsigned int len
)
1239 struct ip6_tnl
*t
= netdev_priv(dev
);
1240 struct ipv6hdr
*ipv6h
= (struct ipv6hdr
*)skb_push(skb
, t
->hlen
);
1241 __be16
*p
= (__be16
*)(ipv6h
+1);
1243 *(__be32
*)ipv6h
= t
->fl
.u
.ip6
.flowlabel
| htonl(0x60000000);
1244 ipv6h
->hop_limit
= t
->parms
.hop_limit
;
1245 ipv6h
->nexthdr
= NEXTHDR_GRE
;
1246 ipv6h
->saddr
= t
->parms
.laddr
;
1247 ipv6h
->daddr
= t
->parms
.raddr
;
1249 p
[0] = t
->parms
.o_flags
;
1253 * Set the source hardware address.
1257 memcpy(&ipv6h
->saddr
, saddr
, sizeof(struct in6_addr
));
1259 memcpy(&ipv6h
->daddr
, daddr
, sizeof(struct in6_addr
));
1260 if (!ipv6_addr_any(&ipv6h
->daddr
))
1266 static const struct header_ops ip6gre_header_ops
= {
1267 .create
= ip6gre_header
,
1270 static const struct net_device_ops ip6gre_netdev_ops
= {
1271 .ndo_init
= ip6gre_tunnel_init
,
1272 .ndo_uninit
= ip6gre_tunnel_uninit
,
1273 .ndo_start_xmit
= ip6gre_tunnel_xmit
,
1274 .ndo_do_ioctl
= ip6gre_tunnel_ioctl
,
1275 .ndo_change_mtu
= ip6gre_tunnel_change_mtu
,
1276 .ndo_get_stats64
= ip6gre_get_stats64
,
1279 static void ip6gre_dev_free(struct net_device
*dev
)
1281 free_percpu(dev
->tstats
);
1285 static void ip6gre_tunnel_setup(struct net_device
*dev
)
1289 dev
->netdev_ops
= &ip6gre_netdev_ops
;
1290 dev
->destructor
= ip6gre_dev_free
;
1292 dev
->type
= ARPHRD_IP6GRE
;
1293 dev
->hard_header_len
= LL_MAX_HEADER
+ sizeof(struct ipv6hdr
) + 4;
1294 dev
->mtu
= ETH_DATA_LEN
- sizeof(struct ipv6hdr
) - 4;
1295 t
= netdev_priv(dev
);
1296 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1298 dev
->flags
|= IFF_NOARP
;
1300 dev
->addr_len
= sizeof(struct in6_addr
);
1301 dev
->features
|= NETIF_F_NETNS_LOCAL
;
1302 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
1305 static int ip6gre_tunnel_init(struct net_device
*dev
)
1307 struct ip6_tnl
*tunnel
;
1309 tunnel
= netdev_priv(dev
);
1312 strcpy(tunnel
->parms
.name
, dev
->name
);
1314 memcpy(dev
->dev_addr
, &tunnel
->parms
.laddr
, sizeof(struct in6_addr
));
1315 memcpy(dev
->broadcast
, &tunnel
->parms
.raddr
, sizeof(struct in6_addr
));
1317 if (ipv6_addr_any(&tunnel
->parms
.raddr
))
1318 dev
->header_ops
= &ip6gre_header_ops
;
1320 dev
->tstats
= alloc_percpu(struct pcpu_tstats
);
1327 static void ip6gre_fb_tunnel_init(struct net_device
*dev
)
1329 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
1332 strcpy(tunnel
->parms
.name
, dev
->name
);
1334 tunnel
->hlen
= sizeof(struct ipv6hdr
) + 4;
1340 static struct inet6_protocol ip6gre_protocol __read_mostly
= {
1341 .handler
= ip6gre_rcv
,
1342 .err_handler
= ip6gre_err
,
1343 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
1346 static void ip6gre_destroy_tunnels(struct ip6gre_net
*ign
,
1347 struct list_head
*head
)
1351 for (prio
= 0; prio
< 4; prio
++) {
1353 for (h
= 0; h
< HASH_SIZE
; h
++) {
1356 t
= rtnl_dereference(ign
->tunnels
[prio
][h
]);
1359 unregister_netdevice_queue(t
->dev
, head
);
1360 t
= rtnl_dereference(t
->next
);
1366 static int __net_init
ip6gre_init_net(struct net
*net
)
1368 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1371 ign
->fb_tunnel_dev
= alloc_netdev(sizeof(struct ip6_tnl
), "ip6gre0",
1372 ip6gre_tunnel_setup
);
1373 if (!ign
->fb_tunnel_dev
) {
1377 dev_net_set(ign
->fb_tunnel_dev
, net
);
1379 ip6gre_fb_tunnel_init(ign
->fb_tunnel_dev
);
1380 ign
->fb_tunnel_dev
->rtnl_link_ops
= &ip6gre_link_ops
;
1382 err
= register_netdev(ign
->fb_tunnel_dev
);
1386 rcu_assign_pointer(ign
->tunnels_wc
[0],
1387 netdev_priv(ign
->fb_tunnel_dev
));
1391 ip6gre_dev_free(ign
->fb_tunnel_dev
);
1396 static void __net_exit
ip6gre_exit_net(struct net
*net
)
1398 struct ip6gre_net
*ign
;
1401 ign
= net_generic(net
, ip6gre_net_id
);
1403 ip6gre_destroy_tunnels(ign
, &list
);
1404 unregister_netdevice_many(&list
);
1408 static struct pernet_operations ip6gre_net_ops
= {
1409 .init
= ip6gre_init_net
,
1410 .exit
= ip6gre_exit_net
,
1411 .id
= &ip6gre_net_id
,
1412 .size
= sizeof(struct ip6gre_net
),
1415 static int ip6gre_tunnel_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1423 if (data
[IFLA_GRE_IFLAGS
])
1424 flags
|= nla_get_be16(data
[IFLA_GRE_IFLAGS
]);
1425 if (data
[IFLA_GRE_OFLAGS
])
1426 flags
|= nla_get_be16(data
[IFLA_GRE_OFLAGS
]);
1427 if (flags
& (GRE_VERSION
|GRE_ROUTING
))
1433 static int ip6gre_tap_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1435 struct in6_addr daddr
;
1437 if (tb
[IFLA_ADDRESS
]) {
1438 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
1440 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
1441 return -EADDRNOTAVAIL
;
1447 if (data
[IFLA_GRE_REMOTE
]) {
1448 nla_memcpy(&daddr
, data
[IFLA_GRE_REMOTE
], sizeof(struct in6_addr
));
1449 if (ipv6_addr_any(&daddr
))
1454 return ip6gre_tunnel_validate(tb
, data
);
1458 static void ip6gre_netlink_parms(struct nlattr
*data
[],
1459 struct __ip6_tnl_parm
*parms
)
1461 memset(parms
, 0, sizeof(*parms
));
1466 if (data
[IFLA_GRE_LINK
])
1467 parms
->link
= nla_get_u32(data
[IFLA_GRE_LINK
]);
1469 if (data
[IFLA_GRE_IFLAGS
])
1470 parms
->i_flags
= nla_get_be16(data
[IFLA_GRE_IFLAGS
]);
1472 if (data
[IFLA_GRE_OFLAGS
])
1473 parms
->o_flags
= nla_get_be16(data
[IFLA_GRE_OFLAGS
]);
1475 if (data
[IFLA_GRE_IKEY
])
1476 parms
->i_key
= nla_get_be32(data
[IFLA_GRE_IKEY
]);
1478 if (data
[IFLA_GRE_OKEY
])
1479 parms
->o_key
= nla_get_be32(data
[IFLA_GRE_OKEY
]);
1481 if (data
[IFLA_GRE_LOCAL
])
1482 nla_memcpy(&parms
->laddr
, data
[IFLA_GRE_LOCAL
], sizeof(struct in6_addr
));
1484 if (data
[IFLA_GRE_REMOTE
])
1485 nla_memcpy(&parms
->raddr
, data
[IFLA_GRE_REMOTE
], sizeof(struct in6_addr
));
1487 if (data
[IFLA_GRE_TTL
])
1488 parms
->hop_limit
= nla_get_u8(data
[IFLA_GRE_TTL
]);
1490 if (data
[IFLA_GRE_ENCAP_LIMIT
])
1491 parms
->encap_limit
= nla_get_u8(data
[IFLA_GRE_ENCAP_LIMIT
]);
1493 if (data
[IFLA_GRE_FLOWINFO
])
1494 parms
->flowinfo
= nla_get_u32(data
[IFLA_GRE_FLOWINFO
]);
1496 if (data
[IFLA_GRE_FLAGS
])
1497 parms
->flags
= nla_get_u32(data
[IFLA_GRE_FLAGS
]);
1500 static int ip6gre_tap_init(struct net_device
*dev
)
1502 struct ip6_tnl
*tunnel
;
1504 tunnel
= netdev_priv(dev
);
1507 strcpy(tunnel
->parms
.name
, dev
->name
);
1509 ip6gre_tnl_link_config(tunnel
, 1);
1511 dev
->tstats
= alloc_percpu(struct pcpu_tstats
);
1518 static const struct net_device_ops ip6gre_tap_netdev_ops
= {
1519 .ndo_init
= ip6gre_tap_init
,
1520 .ndo_uninit
= ip6gre_tunnel_uninit
,
1521 .ndo_start_xmit
= ip6gre_tunnel_xmit
,
1522 .ndo_set_mac_address
= eth_mac_addr
,
1523 .ndo_validate_addr
= eth_validate_addr
,
1524 .ndo_change_mtu
= ip6gre_tunnel_change_mtu
,
1525 .ndo_get_stats64
= ip6gre_get_stats64
,
1528 static void ip6gre_tap_setup(struct net_device
*dev
)
1533 dev
->netdev_ops
= &ip6gre_tap_netdev_ops
;
1534 dev
->destructor
= ip6gre_dev_free
;
1537 dev
->features
|= NETIF_F_NETNS_LOCAL
;
1540 static int ip6gre_newlink(struct net
*src_net
, struct net_device
*dev
,
1541 struct nlattr
*tb
[], struct nlattr
*data
[])
1544 struct net
*net
= dev_net(dev
);
1545 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1548 nt
= netdev_priv(dev
);
1549 ip6gre_netlink_parms(data
, &nt
->parms
);
1551 if (ip6gre_tunnel_find(net
, &nt
->parms
, dev
->type
))
1554 if (dev
->type
== ARPHRD_ETHER
&& !tb
[IFLA_ADDRESS
])
1555 eth_hw_addr_random(dev
);
1558 ip6gre_tnl_link_config(nt
, !tb
[IFLA_MTU
]);
1560 /* Can use a lockless transmit, unless we generate output sequences */
1561 if (!(nt
->parms
.o_flags
& GRE_SEQ
))
1562 dev
->features
|= NETIF_F_LLTX
;
1564 err
= register_netdevice(dev
);
1569 ip6gre_tunnel_link(ign
, nt
);
1575 static int ip6gre_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
1576 struct nlattr
*data
[])
1578 struct ip6_tnl
*t
, *nt
;
1579 struct net
*net
= dev_net(dev
);
1580 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1581 struct __ip6_tnl_parm p
;
1583 if (dev
== ign
->fb_tunnel_dev
)
1586 nt
= netdev_priv(dev
);
1587 ip6gre_netlink_parms(data
, &p
);
1589 t
= ip6gre_tunnel_locate(net
, &p
, 0);
1597 ip6gre_tunnel_unlink(ign
, t
);
1598 ip6gre_tnl_change(t
, &p
, !tb
[IFLA_MTU
]);
1599 ip6gre_tunnel_link(ign
, t
);
1600 netdev_state_change(dev
);
1606 static size_t ip6gre_get_size(const struct net_device
*dev
)
1611 /* IFLA_GRE_IFLAGS */
1613 /* IFLA_GRE_OFLAGS */
1619 /* IFLA_GRE_LOCAL */
1620 nla_total_size(sizeof(struct in6_addr
)) +
1621 /* IFLA_GRE_REMOTE */
1622 nla_total_size(sizeof(struct in6_addr
)) +
1627 /* IFLA_GRE_ENCAP_LIMIT */
1629 /* IFLA_GRE_FLOWINFO */
1631 /* IFLA_GRE_FLAGS */
1636 static int ip6gre_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1638 struct ip6_tnl
*t
= netdev_priv(dev
);
1639 struct __ip6_tnl_parm
*p
= &t
->parms
;
1641 if (nla_put_u32(skb
, IFLA_GRE_LINK
, p
->link
) ||
1642 nla_put_be16(skb
, IFLA_GRE_IFLAGS
, p
->i_flags
) ||
1643 nla_put_be16(skb
, IFLA_GRE_OFLAGS
, p
->o_flags
) ||
1644 nla_put_be32(skb
, IFLA_GRE_IKEY
, p
->i_key
) ||
1645 nla_put_be32(skb
, IFLA_GRE_OKEY
, p
->o_key
) ||
1646 nla_put(skb
, IFLA_GRE_LOCAL
, sizeof(struct in6_addr
), &p
->laddr
) ||
1647 nla_put(skb
, IFLA_GRE_REMOTE
, sizeof(struct in6_addr
), &p
->raddr
) ||
1648 nla_put_u8(skb
, IFLA_GRE_TTL
, p
->hop_limit
) ||
1649 /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/
1650 nla_put_u8(skb
, IFLA_GRE_ENCAP_LIMIT
, p
->encap_limit
) ||
1651 nla_put_be32(skb
, IFLA_GRE_FLOWINFO
, p
->flowinfo
) ||
1652 nla_put_u32(skb
, IFLA_GRE_FLAGS
, p
->flags
))
1653 goto nla_put_failure
;
1660 static const struct nla_policy ip6gre_policy
[IFLA_GRE_MAX
+ 1] = {
1661 [IFLA_GRE_LINK
] = { .type
= NLA_U32
},
1662 [IFLA_GRE_IFLAGS
] = { .type
= NLA_U16
},
1663 [IFLA_GRE_OFLAGS
] = { .type
= NLA_U16
},
1664 [IFLA_GRE_IKEY
] = { .type
= NLA_U32
},
1665 [IFLA_GRE_OKEY
] = { .type
= NLA_U32
},
1666 [IFLA_GRE_LOCAL
] = { .len
= FIELD_SIZEOF(struct ipv6hdr
, saddr
) },
1667 [IFLA_GRE_REMOTE
] = { .len
= FIELD_SIZEOF(struct ipv6hdr
, daddr
) },
1668 [IFLA_GRE_TTL
] = { .type
= NLA_U8
},
1669 [IFLA_GRE_ENCAP_LIMIT
] = { .type
= NLA_U8
},
1670 [IFLA_GRE_FLOWINFO
] = { .type
= NLA_U32
},
1671 [IFLA_GRE_FLAGS
] = { .type
= NLA_U32
},
1674 static struct rtnl_link_ops ip6gre_link_ops __read_mostly
= {
1676 .maxtype
= IFLA_GRE_MAX
,
1677 .policy
= ip6gre_policy
,
1678 .priv_size
= sizeof(struct ip6_tnl
),
1679 .setup
= ip6gre_tunnel_setup
,
1680 .validate
= ip6gre_tunnel_validate
,
1681 .newlink
= ip6gre_newlink
,
1682 .changelink
= ip6gre_changelink
,
1683 .get_size
= ip6gre_get_size
,
1684 .fill_info
= ip6gre_fill_info
,
1687 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly
= {
1688 .kind
= "ip6gretap",
1689 .maxtype
= IFLA_GRE_MAX
,
1690 .policy
= ip6gre_policy
,
1691 .priv_size
= sizeof(struct ip6_tnl
),
1692 .setup
= ip6gre_tap_setup
,
1693 .validate
= ip6gre_tap_validate
,
1694 .newlink
= ip6gre_newlink
,
1695 .changelink
= ip6gre_changelink
,
1696 .get_size
= ip6gre_get_size
,
1697 .fill_info
= ip6gre_fill_info
,
1701 * And now the modules code and kernel interface.
1704 static int __init
ip6gre_init(void)
1708 pr_info("GRE over IPv6 tunneling driver\n");
1710 err
= register_pernet_device(&ip6gre_net_ops
);
1714 err
= inet6_add_protocol(&ip6gre_protocol
, IPPROTO_GRE
);
1716 pr_info("%s: can't add protocol\n", __func__
);
1717 goto add_proto_failed
;
1720 err
= rtnl_link_register(&ip6gre_link_ops
);
1722 goto rtnl_link_failed
;
1724 err
= rtnl_link_register(&ip6gre_tap_ops
);
1726 goto tap_ops_failed
;
1732 rtnl_link_unregister(&ip6gre_link_ops
);
1734 inet6_del_protocol(&ip6gre_protocol
, IPPROTO_GRE
);
1736 unregister_pernet_device(&ip6gre_net_ops
);
1740 static void __exit
ip6gre_fini(void)
1742 rtnl_link_unregister(&ip6gre_tap_ops
);
1743 rtnl_link_unregister(&ip6gre_link_ops
);
1744 inet6_del_protocol(&ip6gre_protocol
, IPPROTO_GRE
);
1745 unregister_pernet_device(&ip6gre_net_ops
);
1748 module_init(ip6gre_init
);
1749 module_exit(ip6gre_fini
);
1750 MODULE_LICENSE("GPL");
1751 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
1752 MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
1753 MODULE_ALIAS_RTNL_LINK("ip6gre");
1754 MODULE_ALIAS_NETDEV("ip6gre0");