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 * Locking : hash tables are protected by RCU and RTNL
116 #define for_each_ip_tunnel_rcu(start) \
117 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
119 static struct rtnl_link_stats64
*ip6gre_get_stats64(struct net_device
*dev
,
120 struct rtnl_link_stats64
*tot
)
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
;
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
));
137 tot
->rx_packets
+= rx_packets
;
138 tot
->tx_packets
+= tx_packets
;
139 tot
->rx_bytes
+= rx_bytes
;
140 tot
->tx_bytes
+= tx_bytes
;
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
;
147 tot
->rx_frame_errors
= dev
->stats
.rx_frame_errors
;
148 tot
->rx_errors
= dev
->stats
.rx_errors
;
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
;
159 /* Given src, dst and key, find appropriate for input tunnel. */
161 static 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
)
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;
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
))
182 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
183 t
->dev
->type
!= dev_type
)
187 if (t
->parms
.link
!= link
)
189 if (t
->dev
->type
!= dev_type
)
194 if (score
< cand_score
) {
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
))
206 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
207 t
->dev
->type
!= dev_type
)
211 if (t
->parms
.link
!= link
)
213 if (t
->dev
->type
!= dev_type
)
218 if (score
< cand_score
) {
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
))
232 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
233 t
->dev
->type
!= dev_type
)
237 if (t
->parms
.link
!= link
)
239 if (t
->dev
->type
!= dev_type
)
244 if (score
< cand_score
) {
250 for_each_ip_tunnel_rcu(ign
->tunnels_wc
[h1
]) {
251 if (t
->parms
.i_key
!= key
||
252 !(t
->dev
->flags
& IFF_UP
))
255 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
256 t
->dev
->type
!= dev_type
)
260 if (t
->parms
.link
!= link
)
262 if (t
->dev
->type
!= dev_type
)
267 if (score
< cand_score
) {
276 dev
= ign
->fb_tunnel_dev
;
277 if (dev
->flags
& IFF_UP
)
278 return netdev_priv(dev
);
283 static struct ip6_tnl __rcu
**__ip6gre_bucket(struct ip6gre_net
*ign
,
284 const struct __ip6_tnl_parm
*p
)
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
);
291 if (!ipv6_addr_any(local
))
293 if (!ipv6_addr_any(remote
) && !ipv6_addr_is_multicast(remote
)) {
295 h
^= HASH_ADDR(remote
);
298 return &ign
->tunnels
[prio
][h
];
301 static inline struct ip6_tnl __rcu
**ip6gre_bucket(struct ip6gre_net
*ign
,
302 const struct ip6_tnl
*t
)
304 return __ip6gre_bucket(ign
, &t
->parms
);
307 static void ip6gre_tunnel_link(struct ip6gre_net
*ign
, struct ip6_tnl
*t
)
309 struct ip6_tnl __rcu
**tp
= ip6gre_bucket(ign
, t
);
311 rcu_assign_pointer(t
->next
, rtnl_dereference(*tp
));
312 rcu_assign_pointer(*tp
, t
);
315 static void ip6gre_tunnel_unlink(struct ip6gre_net
*ign
, struct ip6_tnl
*t
)
317 struct ip6_tnl __rcu
**tp
;
318 struct ip6_tnl
*iter
;
320 for (tp
= ip6gre_bucket(ign
, t
);
321 (iter
= rtnl_dereference(*tp
)) != NULL
;
324 rcu_assign_pointer(*tp
, t
->next
);
330 static struct ip6_tnl
*ip6gre_tunnel_find(struct net
*net
,
331 const struct __ip6_tnl_parm
*parms
,
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
;
339 struct ip6_tnl __rcu
**tp
;
340 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
342 for (tp
= __ip6gre_bucket(ign
, parms
);
343 (t
= rtnl_dereference(*tp
)) != NULL
;
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
)
355 static struct ip6_tnl
*ip6gre_tunnel_locate(struct net
*net
,
356 const struct __ip6_tnl_parm
*parms
, int create
)
358 struct ip6_tnl
*t
, *nt
;
359 struct net_device
*dev
;
361 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
363 t
= ip6gre_tunnel_find(net
, parms
, ARPHRD_IP6GRE
);
368 strlcpy(name
, parms
->name
, IFNAMSIZ
);
370 strcpy(name
, "ip6gre%d");
372 dev
= alloc_netdev(sizeof(*t
), name
, ip6gre_tunnel_setup
);
376 dev_net_set(dev
, net
);
378 nt
= netdev_priv(dev
);
380 dev
->rtnl_link_ops
= &ip6gre_link_ops
;
383 ip6gre_tnl_link_config(nt
, 1);
385 if (register_netdevice(dev
) < 0)
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
;
393 ip6gre_tunnel_link(ign
, nt
);
401 static void ip6gre_tunnel_uninit(struct net_device
*dev
)
403 struct net
*net
= dev_net(dev
);
404 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
406 ip6gre_tunnel_unlink(ign
, netdev_priv(dev
));
411 static void ip6gre_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
412 u8 type
, u8 code
, int offset
, __be32 info
)
414 const struct ipv6hdr
*ipv6h
= (const struct ipv6hdr
*)skb
->data
;
415 __be16
*p
= (__be16
*)(skb
->data
+ offset
);
416 int grehlen
= offset
+ 4;
421 if (flags
&(GRE_CSUM
|GRE_KEY
|GRE_SEQ
|GRE_ROUTING
|GRE_VERSION
)) {
422 if (flags
&(GRE_VERSION
|GRE_ROUTING
))
431 /* If only 8 bytes returned, keyed message will be dropped here */
432 if (!pskb_may_pull(skb
, grehlen
))
434 ipv6h
= (const struct ipv6hdr
*)skb
->data
;
435 p
= (__be16
*)(skb
->data
+ offset
);
437 t
= ip6gre_tunnel_lookup(skb
->dev
, &ipv6h
->daddr
, &ipv6h
->saddr
,
439 *(((__be32
*)p
) + (grehlen
/ 4) - 1) : 0,
446 struct ipv6_tlv_tnl_enc_lim
*tel
;
448 case ICMPV6_DEST_UNREACH
:
449 net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
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",
458 case ICMPV6_PARAMPROB
:
460 if (code
== ICMPV6_HDR_FIELD
)
461 teli
= ip6_tnl_parse_tlv_enc_lim(skb
, skb
->data
);
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",
470 net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
474 case ICMPV6_PKT_TOOBIG
:
476 if (mtu
< IPV6_MIN_MTU
)
482 if (time_before(jiffies
, t
->err_time
+ IP6TUNNEL_ERR_TIMEO
))
486 t
->err_time
= jiffies
;
489 static int ip6gre_rcv(struct sk_buff
*skb
)
491 const struct ipv6hdr
*ipv6h
;
497 struct ip6_tnl
*tunnel
;
502 if (!pskb_may_pull(skb
, sizeof(struct in6_addr
)))
505 ipv6h
= ipv6_hdr(skb
);
507 flags
= *(__be16
*)h
;
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.
513 if (flags
&(GRE_VERSION
|GRE_ROUTING
))
516 if (flags
&GRE_CSUM
) {
517 switch (skb
->ip_summed
) {
518 case CHECKSUM_COMPLETE
:
519 csum
= csum_fold(skb
->csum
);
525 csum
= __skb_checksum_complete(skb
);
526 skb
->ip_summed
= CHECKSUM_COMPLETE
;
531 key
= *(__be32
*)(h
+ offset
);
535 seqno
= ntohl(*(__be32
*)(h
+ offset
));
540 gre_proto
= *(__be16
*)(h
+ 2);
542 tunnel
= ip6gre_tunnel_lookup(skb
->dev
,
543 &ipv6h
->saddr
, &ipv6h
->daddr
, key
,
546 struct pcpu_tstats
*tstats
;
548 if (!xfrm6_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
551 if (!ip6_tnl_rcv_ctl(tunnel
, &ipv6h
->daddr
, &ipv6h
->saddr
)) {
552 tunnel
->dev
->stats
.rx_dropped
++;
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
563 if (flags
== 0 && gre_proto
== htons(ETH_P_WCCP
)) {
564 skb
->protocol
= htons(ETH_P_IP
);
565 if ((*(h
+ offset
) & 0xF0) != 0x40)
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
;
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
++;
580 if (tunnel
->parms
.i_flags
&GRE_SEQ
) {
581 if (!(flags
&GRE_SEQ
) ||
583 (s32
)(seqno
- tunnel
->i_seqno
) < 0)) {
584 tunnel
->dev
->stats
.rx_fifo_errors
++;
585 tunnel
->dev
->stats
.rx_errors
++;
588 tunnel
->i_seqno
= seqno
+ 1;
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
++;
599 ipv6h
= ipv6_hdr(skb
);
600 skb
->protocol
= eth_type_trans(skb
, tunnel
->dev
);
601 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
604 __skb_tunnel_rx(skb
, tunnel
->dev
);
606 skb_reset_network_header(skb
);
608 err
= IP6_ECN_decapsulate(ipv6h
, skb
);
611 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
613 ipv6_get_dsfield(ipv6h
));
615 ++tunnel
->dev
->stats
.rx_frame_errors
;
616 ++tunnel
->dev
->stats
.rx_errors
;
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
);
631 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
638 struct ipv6_tel_txoption
{
639 struct ipv6_txoptions ops
;
643 static void init_tel_txopt(struct ipv6_tel_txoption
*opt
, __u8 encap_limit
)
645 memset(opt
, 0, sizeof(struct ipv6_tel_txoption
));
647 opt
->dst_opt
[2] = IPV6_TLV_TNL_ENCAP_LIMIT
;
649 opt
->dst_opt
[4] = encap_limit
;
650 opt
->dst_opt
[5] = IPV6_TLV_PADN
;
653 opt
->ops
.dst0opt
= (struct ipv6_opt_hdr
*) opt
->dst_opt
;
654 opt
->ops
.opt_nflen
= 8;
657 static netdev_tx_t
ip6gre_xmit2(struct sk_buff
*skb
,
658 struct net_device
*dev
,
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 */
670 struct ipv6_tel_txoption opt
;
672 struct dst_entry
*dst
= NULL
, *ndst
= NULL
;
673 struct net_device_stats
*stats
= &tunnel
->dev
->stats
;
677 struct sk_buff
*new_skb
;
679 if (dev
->type
== ARPHRD_ETHER
)
680 IPCB(skb
)->flags
= 0;
682 if (dev
->header_ops
&& dev
->type
== ARPHRD_IP6GRE
) {
684 ipv6h
= (struct ipv6hdr
*)skb
->data
;
685 fl6
->daddr
= ipv6h
->daddr
;
687 gre_hlen
= tunnel
->hlen
;
688 fl6
->daddr
= tunnel
->parms
.raddr
;
691 if (!fl6
->flowi6_mark
)
692 dst
= ip6_tnl_dst_check(tunnel
);
695 ndst
= ip6_route_output(net
, NULL
, fl6
);
698 goto tx_err_link_failure
;
699 ndst
= xfrm_lookup(net
, ndst
, flowi6_to_flowi(fl6
), NULL
, 0);
703 goto tx_err_link_failure
;
712 net_warn_ratelimited("%s: Local routing loop detected!\n",
714 goto tx_err_dst_release
;
717 mtu
= dst_mtu(dst
) - sizeof(*ipv6h
);
718 if (encap_limit
>= 0) {
722 if (mtu
< IPV6_MIN_MTU
)
725 skb_dst(skb
)->ops
->update_pmtu(skb_dst(skb
), NULL
, skb
, mtu
);
726 if (skb
->len
> mtu
) {
729 goto tx_err_dst_release
;
732 if (tunnel
->err_count
> 0) {
733 if (time_before(jiffies
,
734 tunnel
->err_time
+ IP6TUNNEL_ERR_TIMEO
)) {
737 dst_link_failure(skb
);
739 tunnel
->err_count
= 0;
742 max_headroom
= LL_RESERVED_SPACE(tdev
) + gre_hlen
+ dst
->header_len
;
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
;
750 goto tx_err_dst_release
;
753 skb_set_owner_w(new_skb
, skb
->sk
);
760 if (fl6
->flowi6_mark
) {
761 skb_dst_set(skb
, dst
);
764 skb_dst_set_noref(skb
, dst
);
767 skb
->transport_header
= skb
->network_header
;
770 if (encap_limit
>= 0) {
771 init_tel_txopt(&opt
, encap_limit
);
772 ipv6_push_nfrag_opts(skb
, &opt
.ops
, &proto
, NULL
);
775 skb_push(skb
, gre_hlen
);
776 skb_reset_network_header(skb
);
779 * Push down and install the IP header.
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
;
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
;
794 if (tunnel
->parms
.o_flags
&(GRE_KEY
|GRE_CSUM
|GRE_SEQ
)) {
795 __be32
*ptr
= (__be32
*)(((u8
*)ipv6h
) + tunnel
->hlen
- 4);
797 if (tunnel
->parms
.o_flags
&GRE_SEQ
) {
799 *ptr
= htonl(tunnel
->o_seqno
);
802 if (tunnel
->parms
.o_flags
&GRE_KEY
) {
803 *ptr
= tunnel
->parms
.o_key
;
806 if (tunnel
->parms
.o_flags
&GRE_CSUM
) {
808 *(__sum16
*)ptr
= ip_compute_csum((void *)(ipv6h
+1),
809 skb
->len
- sizeof(struct ipv6hdr
));
815 err
= ip6_local_out(skb
);
817 if (net_xmit_eval(err
) == 0) {
818 struct pcpu_tstats
*tstats
= this_cpu_ptr(tunnel
->dev
->tstats
);
820 tstats
->tx_bytes
+= pkt_len
;
821 tstats
->tx_packets
++;
824 stats
->tx_aborted_errors
++;
828 ip6_tnl_dst_store(tunnel
, ndst
);
832 stats
->tx_carrier_errors
++;
833 dst_link_failure(skb
);
839 static inline int ip6gre_xmit_ipv4(struct sk_buff
*skb
, struct net_device
*dev
)
841 struct ip6_tnl
*t
= netdev_priv(dev
);
842 const struct iphdr
*iph
= ip_hdr(skb
);
843 int encap_limit
= -1;
849 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
850 encap_limit
= t
->parms
.encap_limit
;
852 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
853 fl6
.flowi6_proto
= IPPROTO_IPIP
;
855 dsfield
= ipv4_get_dsfield(iph
);
857 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
858 fl6
.flowlabel
|= htonl((__u32
)iph
->tos
<< IPV6_TCLASS_SHIFT
)
860 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
861 fl6
.flowi6_mark
= skb
->mark
;
863 err
= ip6gre_xmit2(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
);
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
,
875 static inline int ip6gre_xmit_ipv6(struct sk_buff
*skb
, struct net_device
*dev
)
877 struct ip6_tnl
*t
= netdev_priv(dev
);
878 struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
879 int encap_limit
= -1;
886 if (ipv6_addr_equal(&t
->parms
.raddr
, &ipv6h
->saddr
))
889 offset
= ip6_tnl_parse_tlv_enc_lim(skb
, skb_network_header(skb
));
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);
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
;
902 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
903 fl6
.flowi6_proto
= IPPROTO_IPV6
;
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
;
913 err
= ip6gre_xmit2(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
);
915 if (err
== -EMSGSIZE
)
916 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
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
929 * Avoid trivial tunneling loop by checking that tunnel exit-point
930 * doesn't match source of incoming packet.
937 static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl
*t
,
938 const struct ipv6hdr
*hdr
)
940 return ipv6_addr_equal(&t
->parms
.raddr
, &hdr
->saddr
);
943 static int ip6gre_xmit_other(struct sk_buff
*skb
, struct net_device
*dev
)
945 struct ip6_tnl
*t
= netdev_priv(dev
);
946 int encap_limit
= -1;
951 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
952 encap_limit
= t
->parms
.encap_limit
;
954 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
955 fl6
.flowi6_proto
= skb
->protocol
;
957 err
= ip6gre_xmit2(skb
, dev
, 0, &fl6
, encap_limit
, &mtu
);
962 static netdev_tx_t
ip6gre_tunnel_xmit(struct sk_buff
*skb
,
963 struct net_device
*dev
)
965 struct ip6_tnl
*t
= netdev_priv(dev
);
966 struct net_device_stats
*stats
= &t
->dev
->stats
;
969 if (!ip6_tnl_xmit_ctl(t
))
972 switch (skb
->protocol
) {
973 case htons(ETH_P_IP
):
974 ret
= ip6gre_xmit_ipv4(skb
, dev
);
976 case htons(ETH_P_IPV6
):
977 ret
= ip6gre_xmit_ipv6(skb
, dev
);
980 ret
= ip6gre_xmit_other(skb
, dev
);
996 static void ip6gre_tnl_link_config(struct ip6_tnl
*t
, int set_mtu
)
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;
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
));
1008 /* Set up flowi template */
1009 fl6
->saddr
= p
->laddr
;
1010 fl6
->daddr
= p
->raddr
;
1011 fl6
->flowi6_oif
= p
->link
;
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
;
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
);
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
;
1026 dev
->flags
&= ~IFF_POINTOPOINT
;
1028 dev
->iflink
= p
->link
;
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
)
1034 if (t
->parms
.o_flags
&GRE_KEY
)
1036 if (t
->parms
.o_flags
&GRE_SEQ
)
1040 if (p
->flags
& IP6_TNL_F_CAP_XMIT
) {
1041 int strict
= (ipv6_addr_type(&p
->raddr
) &
1042 (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
));
1044 struct rt6_info
*rt
= rt6_lookup(dev_net(dev
),
1045 &p
->raddr
, &p
->laddr
,
1052 dev
->hard_header_len
= rt
->dst
.dev
->hard_header_len
+ addend
;
1055 dev
->mtu
= rt
->dst
.dev
->mtu
- addend
;
1056 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1059 if (dev
->mtu
< IPV6_MIN_MTU
)
1060 dev
->mtu
= IPV6_MIN_MTU
;
1069 static int ip6gre_tnl_change(struct ip6_tnl
*t
,
1070 const struct __ip6_tnl_parm
*p
, int set_mtu
)
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
);
1089 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm
*p
,
1090 const struct ip6_tnl_parm2
*u
)
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
;
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
));
1106 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2
*u
,
1107 const struct __ip6_tnl_parm
*p
)
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
;
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
));
1124 static int ip6gre_tunnel_ioctl(struct net_device
*dev
,
1125 struct ifreq
*ifr
, int cmd
)
1128 struct ip6_tnl_parm2 p
;
1129 struct __ip6_tnl_parm p1
;
1131 struct net
*net
= dev_net(dev
);
1132 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1137 if (dev
== ign
->fb_tunnel_dev
) {
1138 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
))) {
1142 ip6gre_tnl_parm_from_user(&p1
, &p
);
1143 t
= ip6gre_tunnel_locate(net
, &p1
, 0);
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
)))
1155 if (!capable(CAP_NET_ADMIN
))
1159 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
1163 if ((p
.i_flags
|p
.o_flags
)&(GRE_VERSION
|GRE_ROUTING
))
1166 if (!(p
.i_flags
&GRE_KEY
))
1168 if (!(p
.o_flags
&GRE_KEY
))
1171 ip6gre_tnl_parm_from_user(&p1
, &p
);
1172 t
= ip6gre_tunnel_locate(net
, &p1
, cmd
== SIOCADDTUNNEL
);
1174 if (dev
!= ign
->fb_tunnel_dev
&& cmd
== SIOCCHGTUNNEL
) {
1176 if (t
->dev
!= dev
) {
1181 t
= netdev_priv(dev
);
1183 ip6gre_tunnel_unlink(ign
, t
);
1185 ip6gre_tnl_change(t
, &p1
, 1);
1186 ip6gre_tunnel_link(ign
, t
);
1187 netdev_state_change(dev
);
1194 ip6gre_tnl_parm_to_user(&p
, &t
->parms
);
1195 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof(p
)))
1198 err
= (cmd
== SIOCADDTUNNEL
? -ENOBUFS
: -ENOENT
);
1203 if (!capable(CAP_NET_ADMIN
))
1206 if (dev
== ign
->fb_tunnel_dev
) {
1208 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
1211 ip6gre_tnl_parm_from_user(&p1
, &p
);
1212 t
= ip6gre_tunnel_locate(net
, &p1
, 0);
1216 if (t
== netdev_priv(ign
->fb_tunnel_dev
))
1220 unregister_netdevice(dev
);
1232 static int ip6gre_tunnel_change_mtu(struct net_device
*dev
, int new_mtu
)
1234 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
1236 new_mtu
> 0xFFF8 - dev
->hard_header_len
- tunnel
->hlen
)
1242 static 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
)
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);
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
;
1256 p
[0] = t
->parms
.o_flags
;
1260 * Set the source hardware address.
1264 memcpy(&ipv6h
->saddr
, saddr
, sizeof(struct in6_addr
));
1266 memcpy(&ipv6h
->daddr
, daddr
, sizeof(struct in6_addr
));
1267 if (!ipv6_addr_any(&ipv6h
->daddr
))
1273 static const struct header_ops ip6gre_header_ops
= {
1274 .create
= ip6gre_header
,
1277 static 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
,
1286 static void ip6gre_dev_free(struct net_device
*dev
)
1288 free_percpu(dev
->tstats
);
1292 static void ip6gre_tunnel_setup(struct net_device
*dev
)
1296 dev
->netdev_ops
= &ip6gre_netdev_ops
;
1297 dev
->destructor
= ip6gre_dev_free
;
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
))
1305 dev
->flags
|= IFF_NOARP
;
1307 dev
->addr_len
= sizeof(struct in6_addr
);
1308 dev
->features
|= NETIF_F_NETNS_LOCAL
;
1309 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
1312 static int ip6gre_tunnel_init(struct net_device
*dev
)
1314 struct ip6_tnl
*tunnel
;
1316 tunnel
= netdev_priv(dev
);
1319 strcpy(tunnel
->parms
.name
, dev
->name
);
1321 memcpy(dev
->dev_addr
, &tunnel
->parms
.laddr
, sizeof(struct in6_addr
));
1322 memcpy(dev
->broadcast
, &tunnel
->parms
.raddr
, sizeof(struct in6_addr
));
1324 if (ipv6_addr_any(&tunnel
->parms
.raddr
))
1325 dev
->header_ops
= &ip6gre_header_ops
;
1327 dev
->tstats
= alloc_percpu(struct pcpu_tstats
);
1334 static void ip6gre_fb_tunnel_init(struct net_device
*dev
)
1336 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
1339 strcpy(tunnel
->parms
.name
, dev
->name
);
1341 tunnel
->hlen
= sizeof(struct ipv6hdr
) + 4;
1347 static struct inet6_protocol ip6gre_protocol __read_mostly
= {
1348 .handler
= ip6gre_rcv
,
1349 .err_handler
= ip6gre_err
,
1350 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
1353 static void ip6gre_destroy_tunnels(struct ip6gre_net
*ign
,
1354 struct list_head
*head
)
1358 for (prio
= 0; prio
< 4; prio
++) {
1360 for (h
= 0; h
< HASH_SIZE
; h
++) {
1363 t
= rtnl_dereference(ign
->tunnels
[prio
][h
]);
1366 unregister_netdevice_queue(t
->dev
, head
);
1367 t
= rtnl_dereference(t
->next
);
1373 static int __net_init
ip6gre_init_net(struct net
*net
)
1375 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1378 ign
->fb_tunnel_dev
= alloc_netdev(sizeof(struct ip6_tnl
), "ip6gre0",
1379 ip6gre_tunnel_setup
);
1380 if (!ign
->fb_tunnel_dev
) {
1384 dev_net_set(ign
->fb_tunnel_dev
, net
);
1386 ip6gre_fb_tunnel_init(ign
->fb_tunnel_dev
);
1387 ign
->fb_tunnel_dev
->rtnl_link_ops
= &ip6gre_link_ops
;
1389 err
= register_netdev(ign
->fb_tunnel_dev
);
1393 rcu_assign_pointer(ign
->tunnels_wc
[0],
1394 netdev_priv(ign
->fb_tunnel_dev
));
1398 ip6gre_dev_free(ign
->fb_tunnel_dev
);
1403 static void __net_exit
ip6gre_exit_net(struct net
*net
)
1405 struct ip6gre_net
*ign
;
1408 ign
= net_generic(net
, ip6gre_net_id
);
1410 ip6gre_destroy_tunnels(ign
, &list
);
1411 unregister_netdevice_many(&list
);
1415 static 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
),
1422 static int ip6gre_tunnel_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
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
))
1440 static int ip6gre_tap_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1442 struct in6_addr daddr
;
1444 if (tb
[IFLA_ADDRESS
]) {
1445 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
1447 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
1448 return -EADDRNOTAVAIL
;
1454 if (data
[IFLA_GRE_REMOTE
]) {
1455 nla_memcpy(&daddr
, data
[IFLA_GRE_REMOTE
], sizeof(struct in6_addr
));
1456 if (ipv6_addr_any(&daddr
))
1461 return ip6gre_tunnel_validate(tb
, data
);
1465 static void ip6gre_netlink_parms(struct nlattr
*data
[],
1466 struct __ip6_tnl_parm
*parms
)
1468 memset(parms
, 0, sizeof(*parms
));
1473 if (data
[IFLA_GRE_LINK
])
1474 parms
->link
= nla_get_u32(data
[IFLA_GRE_LINK
]);
1476 if (data
[IFLA_GRE_IFLAGS
])
1477 parms
->i_flags
= nla_get_be16(data
[IFLA_GRE_IFLAGS
]);
1479 if (data
[IFLA_GRE_OFLAGS
])
1480 parms
->o_flags
= nla_get_be16(data
[IFLA_GRE_OFLAGS
]);
1482 if (data
[IFLA_GRE_IKEY
])
1483 parms
->i_key
= nla_get_be32(data
[IFLA_GRE_IKEY
]);
1485 if (data
[IFLA_GRE_OKEY
])
1486 parms
->o_key
= nla_get_be32(data
[IFLA_GRE_OKEY
]);
1488 if (data
[IFLA_GRE_LOCAL
])
1489 nla_memcpy(&parms
->laddr
, data
[IFLA_GRE_LOCAL
], sizeof(struct in6_addr
));
1491 if (data
[IFLA_GRE_REMOTE
])
1492 nla_memcpy(&parms
->raddr
, data
[IFLA_GRE_REMOTE
], sizeof(struct in6_addr
));
1494 if (data
[IFLA_GRE_TTL
])
1495 parms
->hop_limit
= nla_get_u8(data
[IFLA_GRE_TTL
]);
1497 if (data
[IFLA_GRE_ENCAP_LIMIT
])
1498 parms
->encap_limit
= nla_get_u8(data
[IFLA_GRE_ENCAP_LIMIT
]);
1500 if (data
[IFLA_GRE_FLOWINFO
])
1501 parms
->flowinfo
= nla_get_u32(data
[IFLA_GRE_FLOWINFO
]);
1503 if (data
[IFLA_GRE_FLAGS
])
1504 parms
->flags
= nla_get_u32(data
[IFLA_GRE_FLAGS
]);
1507 static int ip6gre_tap_init(struct net_device
*dev
)
1509 struct ip6_tnl
*tunnel
;
1511 tunnel
= netdev_priv(dev
);
1514 strcpy(tunnel
->parms
.name
, dev
->name
);
1516 ip6gre_tnl_link_config(tunnel
, 1);
1518 dev
->tstats
= alloc_percpu(struct pcpu_tstats
);
1525 static 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
,
1535 static void ip6gre_tap_setup(struct net_device
*dev
)
1540 dev
->netdev_ops
= &ip6gre_tap_netdev_ops
;
1541 dev
->destructor
= ip6gre_dev_free
;
1544 dev
->features
|= NETIF_F_NETNS_LOCAL
;
1547 static int ip6gre_newlink(struct net
*src_net
, struct net_device
*dev
,
1548 struct nlattr
*tb
[], struct nlattr
*data
[])
1551 struct net
*net
= dev_net(dev
);
1552 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1555 nt
= netdev_priv(dev
);
1556 ip6gre_netlink_parms(data
, &nt
->parms
);
1558 if (ip6gre_tunnel_find(net
, &nt
->parms
, dev
->type
))
1561 if (dev
->type
== ARPHRD_ETHER
&& !tb
[IFLA_ADDRESS
])
1562 eth_hw_addr_random(dev
);
1565 ip6gre_tnl_link_config(nt
, !tb
[IFLA_MTU
]);
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
;
1571 err
= register_netdevice(dev
);
1576 ip6gre_tunnel_link(ign
, nt
);
1582 static int ip6gre_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
1583 struct nlattr
*data
[])
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
;
1590 if (dev
== ign
->fb_tunnel_dev
)
1593 nt
= netdev_priv(dev
);
1594 ip6gre_netlink_parms(data
, &p
);
1596 t
= ip6gre_tunnel_locate(net
, &p
, 0);
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
);
1613 static size_t ip6gre_get_size(const struct net_device
*dev
)
1618 /* IFLA_GRE_IFLAGS */
1620 /* IFLA_GRE_OFLAGS */
1626 /* IFLA_GRE_LOCAL */
1627 nla_total_size(sizeof(struct in6_addr
)) +
1628 /* IFLA_GRE_REMOTE */
1629 nla_total_size(sizeof(struct in6_addr
)) +
1634 /* IFLA_GRE_ENCAP_LIMIT */
1636 /* IFLA_GRE_FLOWINFO */
1638 /* IFLA_GRE_FLAGS */
1643 static int ip6gre_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1645 struct ip6_tnl
*t
= netdev_priv(dev
);
1646 struct __ip6_tnl_parm
*p
= &t
->parms
;
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
) ||
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
) ||
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
;
1667 static 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
},
1681 static struct rtnl_link_ops ip6gre_link_ops __read_mostly
= {
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
,
1694 static 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
,
1708 * And now the modules code and kernel interface.
1711 static int __init
ip6gre_init(void)
1715 pr_info("GRE over IPv6 tunneling driver\n");
1717 err
= register_pernet_device(&ip6gre_net_ops
);
1721 err
= inet6_add_protocol(&ip6gre_protocol
, IPPROTO_GRE
);
1723 pr_info("%s: can't add protocol\n", __func__
);
1724 goto add_proto_failed
;
1727 err
= rtnl_link_register(&ip6gre_link_ops
);
1729 goto rtnl_link_failed
;
1731 err
= rtnl_link_register(&ip6gre_tap_ops
);
1733 goto tap_ops_failed
;
1739 rtnl_link_unregister(&ip6gre_link_ops
);
1741 inet6_del_protocol(&ip6gre_protocol
, IPPROTO_GRE
);
1743 unregister_pernet_device(&ip6gre_net_ops
);
1747 static void __exit
ip6gre_fini(void)
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
);
1755 module_init(ip6gre_init
);
1756 module_exit(ip6gre_fini
);
1757 MODULE_LICENSE("GPL");
1758 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
1759 MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
1760 MODULE_ALIAS_RTNL_LINK("ip6gre");
1761 MODULE_ALIAS_NETDEV("ip6gre0");