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 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
60 #define IPV6_TCLASS_SHIFT 20
62 #define HASH_SIZE_SHIFT 5
63 #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
65 static int ip6gre_net_id __read_mostly
;
67 struct ip6_tnl __rcu
*tunnels
[4][HASH_SIZE
];
69 struct net_device
*fb_tunnel_dev
;
72 static struct rtnl_link_ops ip6gre_link_ops __read_mostly
;
73 static int ip6gre_tunnel_init(struct net_device
*dev
);
74 static void ip6gre_tunnel_setup(struct net_device
*dev
);
75 static void ip6gre_tunnel_link(struct ip6gre_net
*ign
, struct ip6_tnl
*t
);
76 static void ip6gre_tnl_link_config(struct ip6_tnl
*t
, int set_mtu
);
78 /* Tunnel hash table */
88 We require exact key match i.e. if a key is present in packet
89 it will match only tunnel with the same key; if it is not present,
90 it will match only keyless tunnel.
92 All keysless packets, if not matched configured keyless tunnels
93 will match fallback tunnel.
96 #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(HASH_SIZE - 1))
97 static u32
HASH_ADDR(const struct in6_addr
*addr
)
99 u32 hash
= ipv6_addr_hash(addr
);
101 return hash_32(hash
, HASH_SIZE_SHIFT
);
104 #define tunnels_r_l tunnels[3]
105 #define tunnels_r tunnels[2]
106 #define tunnels_l tunnels[1]
107 #define tunnels_wc tunnels[0]
109 * Locking : hash tables are protected by RCU and RTNL
112 #define for_each_ip_tunnel_rcu(start) \
113 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
115 /* often modified stats are per cpu, other are shared (netdev->stats) */
121 struct u64_stats_sync syncp
;
124 static struct rtnl_link_stats64
*ip6gre_get_stats64(struct net_device
*dev
,
125 struct rtnl_link_stats64
*tot
)
129 for_each_possible_cpu(i
) {
130 const struct pcpu_tstats
*tstats
= per_cpu_ptr(dev
->tstats
, i
);
131 u64 rx_packets
, rx_bytes
, tx_packets
, tx_bytes
;
135 start
= u64_stats_fetch_begin_bh(&tstats
->syncp
);
136 rx_packets
= tstats
->rx_packets
;
137 tx_packets
= tstats
->tx_packets
;
138 rx_bytes
= tstats
->rx_bytes
;
139 tx_bytes
= tstats
->tx_bytes
;
140 } while (u64_stats_fetch_retry_bh(&tstats
->syncp
, start
));
142 tot
->rx_packets
+= rx_packets
;
143 tot
->tx_packets
+= tx_packets
;
144 tot
->rx_bytes
+= rx_bytes
;
145 tot
->tx_bytes
+= tx_bytes
;
148 tot
->multicast
= dev
->stats
.multicast
;
149 tot
->rx_crc_errors
= dev
->stats
.rx_crc_errors
;
150 tot
->rx_fifo_errors
= dev
->stats
.rx_fifo_errors
;
151 tot
->rx_length_errors
= dev
->stats
.rx_length_errors
;
152 tot
->rx_errors
= dev
->stats
.rx_errors
;
153 tot
->tx_fifo_errors
= dev
->stats
.tx_fifo_errors
;
154 tot
->tx_carrier_errors
= dev
->stats
.tx_carrier_errors
;
155 tot
->tx_dropped
= dev
->stats
.tx_dropped
;
156 tot
->tx_aborted_errors
= dev
->stats
.tx_aborted_errors
;
157 tot
->tx_errors
= dev
->stats
.tx_errors
;
162 /* Given src, dst and key, find appropriate for input tunnel. */
164 static struct ip6_tnl
*ip6gre_tunnel_lookup(struct net_device
*dev
,
165 const struct in6_addr
*remote
, const struct in6_addr
*local
,
166 __be32 key
, __be16 gre_proto
)
168 struct net
*net
= dev_net(dev
);
169 int link
= dev
->ifindex
;
170 unsigned int h0
= HASH_ADDR(remote
);
171 unsigned int h1
= HASH_KEY(key
);
172 struct ip6_tnl
*t
, *cand
= NULL
;
173 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
174 int dev_type
= (gre_proto
== htons(ETH_P_TEB
)) ?
175 ARPHRD_ETHER
: ARPHRD_IP6GRE
;
176 int score
, cand_score
= 4;
178 for_each_ip_tunnel_rcu(ign
->tunnels_r_l
[h0
^ h1
]) {
179 if (!ipv6_addr_equal(local
, &t
->parms
.laddr
) ||
180 !ipv6_addr_equal(remote
, &t
->parms
.raddr
) ||
181 key
!= t
->parms
.i_key
||
182 !(t
->dev
->flags
& IFF_UP
))
185 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
186 t
->dev
->type
!= dev_type
)
190 if (t
->parms
.link
!= link
)
192 if (t
->dev
->type
!= dev_type
)
197 if (score
< cand_score
) {
203 for_each_ip_tunnel_rcu(ign
->tunnels_r
[h0
^ h1
]) {
204 if (!ipv6_addr_equal(remote
, &t
->parms
.raddr
) ||
205 key
!= t
->parms
.i_key
||
206 !(t
->dev
->flags
& IFF_UP
))
209 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
210 t
->dev
->type
!= dev_type
)
214 if (t
->parms
.link
!= link
)
216 if (t
->dev
->type
!= dev_type
)
221 if (score
< cand_score
) {
227 for_each_ip_tunnel_rcu(ign
->tunnels_l
[h1
]) {
228 if ((!ipv6_addr_equal(local
, &t
->parms
.laddr
) &&
229 (!ipv6_addr_equal(local
, &t
->parms
.raddr
) ||
230 !ipv6_addr_is_multicast(local
))) ||
231 key
!= t
->parms
.i_key
||
232 !(t
->dev
->flags
& IFF_UP
))
235 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
236 t
->dev
->type
!= dev_type
)
240 if (t
->parms
.link
!= link
)
242 if (t
->dev
->type
!= dev_type
)
247 if (score
< cand_score
) {
253 for_each_ip_tunnel_rcu(ign
->tunnels_wc
[h1
]) {
254 if (t
->parms
.i_key
!= key
||
255 !(t
->dev
->flags
& IFF_UP
))
258 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
259 t
->dev
->type
!= dev_type
)
263 if (t
->parms
.link
!= link
)
265 if (t
->dev
->type
!= dev_type
)
270 if (score
< cand_score
) {
279 dev
= ign
->fb_tunnel_dev
;
280 if (dev
->flags
& IFF_UP
)
281 return netdev_priv(dev
);
286 static struct ip6_tnl __rcu
**__ip6gre_bucket(struct ip6gre_net
*ign
,
287 const struct __ip6_tnl_parm
*p
)
289 const struct in6_addr
*remote
= &p
->raddr
;
290 const struct in6_addr
*local
= &p
->laddr
;
291 unsigned int h
= HASH_KEY(p
->i_key
);
294 if (!ipv6_addr_any(local
))
296 if (!ipv6_addr_any(remote
) && !ipv6_addr_is_multicast(remote
)) {
298 h
^= HASH_ADDR(remote
);
301 return &ign
->tunnels
[prio
][h
];
304 static inline struct ip6_tnl __rcu
**ip6gre_bucket(struct ip6gre_net
*ign
,
305 const struct ip6_tnl
*t
)
307 return __ip6gre_bucket(ign
, &t
->parms
);
310 static void ip6gre_tunnel_link(struct ip6gre_net
*ign
, struct ip6_tnl
*t
)
312 struct ip6_tnl __rcu
**tp
= ip6gre_bucket(ign
, t
);
314 rcu_assign_pointer(t
->next
, rtnl_dereference(*tp
));
315 rcu_assign_pointer(*tp
, t
);
318 static void ip6gre_tunnel_unlink(struct ip6gre_net
*ign
, struct ip6_tnl
*t
)
320 struct ip6_tnl __rcu
**tp
;
321 struct ip6_tnl
*iter
;
323 for (tp
= ip6gre_bucket(ign
, t
);
324 (iter
= rtnl_dereference(*tp
)) != NULL
;
327 rcu_assign_pointer(*tp
, t
->next
);
333 static struct ip6_tnl
*ip6gre_tunnel_find(struct net
*net
,
334 const struct __ip6_tnl_parm
*parms
,
337 const struct in6_addr
*remote
= &parms
->raddr
;
338 const struct in6_addr
*local
= &parms
->laddr
;
339 __be32 key
= parms
->i_key
;
340 int link
= parms
->link
;
342 struct ip6_tnl __rcu
**tp
;
343 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
345 for (tp
= __ip6gre_bucket(ign
, parms
);
346 (t
= rtnl_dereference(*tp
)) != NULL
;
348 if (ipv6_addr_equal(local
, &t
->parms
.laddr
) &&
349 ipv6_addr_equal(remote
, &t
->parms
.raddr
) &&
350 key
== t
->parms
.i_key
&&
351 link
== t
->parms
.link
&&
352 type
== t
->dev
->type
)
358 static struct ip6_tnl
*ip6gre_tunnel_locate(struct net
*net
,
359 const struct __ip6_tnl_parm
*parms
, int create
)
361 struct ip6_tnl
*t
, *nt
;
362 struct net_device
*dev
;
364 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
366 t
= ip6gre_tunnel_find(net
, parms
, ARPHRD_IP6GRE
);
371 strlcpy(name
, parms
->name
, IFNAMSIZ
);
373 strcpy(name
, "ip6gre%d");
375 dev
= alloc_netdev(sizeof(*t
), name
, ip6gre_tunnel_setup
);
379 dev_net_set(dev
, net
);
381 nt
= netdev_priv(dev
);
383 dev
->rtnl_link_ops
= &ip6gre_link_ops
;
386 ip6gre_tnl_link_config(nt
, 1);
388 if (register_netdevice(dev
) < 0)
391 /* Can use a lockless transmit, unless we generate output sequences */
392 if (!(nt
->parms
.o_flags
& GRE_SEQ
))
393 dev
->features
|= NETIF_F_LLTX
;
396 ip6gre_tunnel_link(ign
, nt
);
404 static void ip6gre_tunnel_uninit(struct net_device
*dev
)
406 struct net
*net
= dev_net(dev
);
407 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
409 ip6gre_tunnel_unlink(ign
, netdev_priv(dev
));
414 static void ip6gre_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
415 u8 type
, u8 code
, int offset
, __be32 info
)
417 const struct ipv6hdr
*ipv6h
= (const struct ipv6hdr
*)skb
->data
;
418 __be16
*p
= (__be16
*)(ipv6h
+ 1);
419 int grehlen
= sizeof(ipv6h
) + 4;
424 if (flags
&(GRE_CSUM
|GRE_KEY
|GRE_SEQ
|GRE_ROUTING
|GRE_VERSION
)) {
425 if (flags
&(GRE_VERSION
|GRE_ROUTING
))
434 /* If only 8 bytes returned, keyed message will be dropped here */
435 if (skb_headlen(skb
) < grehlen
)
440 t
= ip6gre_tunnel_lookup(skb
->dev
, &ipv6h
->daddr
, &ipv6h
->saddr
,
442 *(((__be32
*)p
) + (grehlen
/ 4) - 1) : 0,
449 struct ipv6_tlv_tnl_enc_lim
*tel
;
451 case ICMPV6_DEST_UNREACH
:
452 net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
455 case ICMPV6_TIME_EXCEED
:
456 if (code
== ICMPV6_EXC_HOPLIMIT
) {
457 net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
461 case ICMPV6_PARAMPROB
:
463 if (code
== ICMPV6_HDR_FIELD
)
464 teli
= ip6_tnl_parse_tlv_enc_lim(skb
, skb
->data
);
466 if (teli
&& teli
== info
- 2) {
467 tel
= (struct ipv6_tlv_tnl_enc_lim
*) &skb
->data
[teli
];
468 if (tel
->encap_limit
== 0) {
469 net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
473 net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
477 case ICMPV6_PKT_TOOBIG
:
479 if (mtu
< IPV6_MIN_MTU
)
485 if (time_before(jiffies
, t
->err_time
+ IP6TUNNEL_ERR_TIMEO
))
489 t
->err_time
= jiffies
;
494 static inline void ip6gre_ecn_decapsulate_ipv4(const struct ip6_tnl
*t
,
495 const struct ipv6hdr
*ipv6h
, struct sk_buff
*skb
)
497 __u8 dsfield
= ipv6_get_dsfield(ipv6h
) & ~INET_ECN_MASK
;
499 if (t
->parms
.flags
& IP6_TNL_F_RCV_DSCP_COPY
)
500 ipv4_change_dsfield(ip_hdr(skb
), INET_ECN_MASK
, dsfield
);
502 if (INET_ECN_is_ce(dsfield
))
503 IP_ECN_set_ce(ip_hdr(skb
));
506 static inline void ip6gre_ecn_decapsulate_ipv6(const struct ip6_tnl
*t
,
507 const struct ipv6hdr
*ipv6h
, struct sk_buff
*skb
)
509 if (t
->parms
.flags
& IP6_TNL_F_RCV_DSCP_COPY
)
510 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h
), ipv6_hdr(skb
));
512 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h
)))
513 IP6_ECN_set_ce(ipv6_hdr(skb
));
516 static int ip6gre_rcv(struct sk_buff
*skb
)
518 const struct ipv6hdr
*ipv6h
;
524 struct ip6_tnl
*tunnel
;
528 if (!pskb_may_pull(skb
, sizeof(struct in6_addr
)))
531 ipv6h
= ipv6_hdr(skb
);
533 flags
= *(__be16
*)h
;
535 if (flags
&(GRE_CSUM
|GRE_KEY
|GRE_ROUTING
|GRE_SEQ
|GRE_VERSION
)) {
536 /* - Version must be 0.
537 - We do not support routing headers.
539 if (flags
&(GRE_VERSION
|GRE_ROUTING
))
542 if (flags
&GRE_CSUM
) {
543 switch (skb
->ip_summed
) {
544 case CHECKSUM_COMPLETE
:
545 csum
= csum_fold(skb
->csum
);
551 csum
= __skb_checksum_complete(skb
);
552 skb
->ip_summed
= CHECKSUM_COMPLETE
;
557 key
= *(__be32
*)(h
+ offset
);
561 seqno
= ntohl(*(__be32
*)(h
+ offset
));
566 gre_proto
= *(__be16
*)(h
+ 2);
569 tunnel
= ip6gre_tunnel_lookup(skb
->dev
,
570 &ipv6h
->saddr
, &ipv6h
->daddr
, key
,
573 struct pcpu_tstats
*tstats
;
575 if (!xfrm6_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
578 if (!ip6_tnl_rcv_ctl(tunnel
, &ipv6h
->daddr
, &ipv6h
->saddr
)) {
579 tunnel
->dev
->stats
.rx_dropped
++;
585 skb
->protocol
= gre_proto
;
586 /* WCCP version 1 and 2 protocol decoding.
587 * - Change protocol to IP
588 * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
590 if (flags
== 0 && gre_proto
== htons(ETH_P_WCCP
)) {
591 skb
->protocol
= htons(ETH_P_IP
);
592 if ((*(h
+ offset
) & 0xF0) != 0x40)
596 skb
->mac_header
= skb
->network_header
;
597 __pskb_pull(skb
, offset
);
598 skb_postpull_rcsum(skb
, skb_transport_header(skb
), offset
);
599 skb
->pkt_type
= PACKET_HOST
;
601 if (((flags
&GRE_CSUM
) && csum
) ||
602 (!(flags
&GRE_CSUM
) && tunnel
->parms
.i_flags
&GRE_CSUM
)) {
603 tunnel
->dev
->stats
.rx_crc_errors
++;
604 tunnel
->dev
->stats
.rx_errors
++;
607 if (tunnel
->parms
.i_flags
&GRE_SEQ
) {
608 if (!(flags
&GRE_SEQ
) ||
610 (s32
)(seqno
- tunnel
->i_seqno
) < 0)) {
611 tunnel
->dev
->stats
.rx_fifo_errors
++;
612 tunnel
->dev
->stats
.rx_errors
++;
615 tunnel
->i_seqno
= seqno
+ 1;
618 /* Warning: All skb pointers will be invalidated! */
619 if (tunnel
->dev
->type
== ARPHRD_ETHER
) {
620 if (!pskb_may_pull(skb
, ETH_HLEN
)) {
621 tunnel
->dev
->stats
.rx_length_errors
++;
622 tunnel
->dev
->stats
.rx_errors
++;
626 ipv6h
= ipv6_hdr(skb
);
627 skb
->protocol
= eth_type_trans(skb
, tunnel
->dev
);
628 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
631 tstats
= this_cpu_ptr(tunnel
->dev
->tstats
);
632 u64_stats_update_begin(&tstats
->syncp
);
633 tstats
->rx_packets
++;
634 tstats
->rx_bytes
+= skb
->len
;
635 u64_stats_update_end(&tstats
->syncp
);
637 __skb_tunnel_rx(skb
, tunnel
->dev
);
639 skb_reset_network_header(skb
);
640 if (skb
->protocol
== htons(ETH_P_IP
))
641 ip6gre_ecn_decapsulate_ipv4(tunnel
, ipv6h
, skb
);
642 else if (skb
->protocol
== htons(ETH_P_IPV6
))
643 ip6gre_ecn_decapsulate_ipv6(tunnel
, ipv6h
, skb
);
650 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
659 struct ipv6_tel_txoption
{
660 struct ipv6_txoptions ops
;
664 static void init_tel_txopt(struct ipv6_tel_txoption
*opt
, __u8 encap_limit
)
666 memset(opt
, 0, sizeof(struct ipv6_tel_txoption
));
668 opt
->dst_opt
[2] = IPV6_TLV_TNL_ENCAP_LIMIT
;
670 opt
->dst_opt
[4] = encap_limit
;
671 opt
->dst_opt
[5] = IPV6_TLV_PADN
;
674 opt
->ops
.dst0opt
= (struct ipv6_opt_hdr
*) opt
->dst_opt
;
675 opt
->ops
.opt_nflen
= 8;
678 static netdev_tx_t
ip6gre_xmit2(struct sk_buff
*skb
,
679 struct net_device
*dev
,
685 struct net
*net
= dev_net(dev
);
686 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
687 struct net_device
*tdev
; /* Device to other host */
688 struct ipv6hdr
*ipv6h
; /* Our new IP header */
689 unsigned int max_headroom
; /* The extra header space needed */
691 struct ipv6_tel_txoption opt
;
693 struct dst_entry
*dst
= NULL
, *ndst
= NULL
;
694 struct net_device_stats
*stats
= &tunnel
->dev
->stats
;
698 struct sk_buff
*new_skb
;
700 if (dev
->type
== ARPHRD_ETHER
)
701 IPCB(skb
)->flags
= 0;
703 if (dev
->header_ops
&& dev
->type
== ARPHRD_IP6GRE
) {
705 ipv6h
= (struct ipv6hdr
*)skb
->data
;
706 fl6
->daddr
= ipv6h
->daddr
;
708 gre_hlen
= tunnel
->hlen
;
709 fl6
->daddr
= tunnel
->parms
.raddr
;
712 if (!fl6
->flowi6_mark
)
713 dst
= ip6_tnl_dst_check(tunnel
);
716 ndst
= ip6_route_output(net
, NULL
, fl6
);
719 goto tx_err_link_failure
;
720 ndst
= xfrm_lookup(net
, ndst
, flowi6_to_flowi(fl6
), NULL
, 0);
724 goto tx_err_link_failure
;
733 net_warn_ratelimited("%s: Local routing loop detected!\n",
735 goto tx_err_dst_release
;
738 mtu
= dst_mtu(dst
) - sizeof(*ipv6h
);
739 if (encap_limit
>= 0) {
743 if (mtu
< IPV6_MIN_MTU
)
746 skb_dst(skb
)->ops
->update_pmtu(skb_dst(skb
), NULL
, skb
, mtu
);
747 if (skb
->len
> mtu
) {
750 goto tx_err_dst_release
;
753 if (tunnel
->err_count
> 0) {
754 if (time_before(jiffies
,
755 tunnel
->err_time
+ IP6TUNNEL_ERR_TIMEO
)) {
758 dst_link_failure(skb
);
760 tunnel
->err_count
= 0;
763 max_headroom
= LL_RESERVED_SPACE(tdev
) + gre_hlen
+ dst
->header_len
;
765 if (skb_headroom(skb
) < max_headroom
|| skb_shared(skb
) ||
766 (skb_cloned(skb
) && !skb_clone_writable(skb
, 0))) {
767 new_skb
= skb_realloc_headroom(skb
, max_headroom
);
768 if (max_headroom
> dev
->needed_headroom
)
769 dev
->needed_headroom
= max_headroom
;
771 goto tx_err_dst_release
;
774 skb_set_owner_w(new_skb
, skb
->sk
);
781 if (fl6
->flowi6_mark
) {
782 skb_dst_set(skb
, dst
);
785 skb_dst_set_noref(skb
, dst
);
788 skb
->transport_header
= skb
->network_header
;
791 if (encap_limit
>= 0) {
792 init_tel_txopt(&opt
, encap_limit
);
793 ipv6_push_nfrag_opts(skb
, &opt
.ops
, &proto
, NULL
);
796 skb_push(skb
, gre_hlen
);
797 skb_reset_network_header(skb
);
800 * Push down and install the IP header.
802 ipv6h
= ipv6_hdr(skb
);
803 *(__be32
*)ipv6h
= fl6
->flowlabel
| htonl(0x60000000);
804 dsfield
= INET_ECN_encapsulate(0, dsfield
);
805 ipv6_change_dsfield(ipv6h
, ~INET_ECN_MASK
, dsfield
);
806 ipv6h
->hop_limit
= tunnel
->parms
.hop_limit
;
807 ipv6h
->nexthdr
= proto
;
808 ipv6h
->saddr
= fl6
->saddr
;
809 ipv6h
->daddr
= fl6
->daddr
;
811 ((__be16
*)(ipv6h
+ 1))[0] = tunnel
->parms
.o_flags
;
812 ((__be16
*)(ipv6h
+ 1))[1] = (dev
->type
== ARPHRD_ETHER
) ?
813 htons(ETH_P_TEB
) : skb
->protocol
;
815 if (tunnel
->parms
.o_flags
&(GRE_KEY
|GRE_CSUM
|GRE_SEQ
)) {
816 __be32
*ptr
= (__be32
*)(((u8
*)ipv6h
) + tunnel
->hlen
- 4);
818 if (tunnel
->parms
.o_flags
&GRE_SEQ
) {
820 *ptr
= htonl(tunnel
->o_seqno
);
823 if (tunnel
->parms
.o_flags
&GRE_KEY
) {
824 *ptr
= tunnel
->parms
.o_key
;
827 if (tunnel
->parms
.o_flags
&GRE_CSUM
) {
829 *(__sum16
*)ptr
= ip_compute_csum((void *)(ipv6h
+1),
830 skb
->len
- sizeof(struct ipv6hdr
));
836 err
= ip6_local_out(skb
);
838 if (net_xmit_eval(err
) == 0) {
839 struct pcpu_tstats
*tstats
= this_cpu_ptr(tunnel
->dev
->tstats
);
841 tstats
->tx_bytes
+= pkt_len
;
842 tstats
->tx_packets
++;
845 stats
->tx_aborted_errors
++;
849 ip6_tnl_dst_store(tunnel
, ndst
);
853 stats
->tx_carrier_errors
++;
854 dst_link_failure(skb
);
860 static inline int ip6gre_xmit_ipv4(struct sk_buff
*skb
, struct net_device
*dev
)
862 struct ip6_tnl
*t
= netdev_priv(dev
);
863 const struct iphdr
*iph
= ip_hdr(skb
);
864 int encap_limit
= -1;
870 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
871 encap_limit
= t
->parms
.encap_limit
;
873 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
874 fl6
.flowi6_proto
= IPPROTO_IPIP
;
876 dsfield
= ipv4_get_dsfield(iph
);
878 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
879 fl6
.flowlabel
|= htonl((__u32
)iph
->tos
<< IPV6_TCLASS_SHIFT
)
881 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
882 fl6
.flowi6_mark
= skb
->mark
;
884 err
= ip6gre_xmit2(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
);
886 /* XXX: send ICMP error even if DF is not set. */
887 if (err
== -EMSGSIZE
)
888 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
,
896 static inline int ip6gre_xmit_ipv6(struct sk_buff
*skb
, struct net_device
*dev
)
898 struct ip6_tnl
*t
= netdev_priv(dev
);
899 struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
900 int encap_limit
= -1;
907 if (ipv6_addr_equal(&t
->parms
.raddr
, &ipv6h
->saddr
))
910 offset
= ip6_tnl_parse_tlv_enc_lim(skb
, skb_network_header(skb
));
912 struct ipv6_tlv_tnl_enc_lim
*tel
;
913 tel
= (struct ipv6_tlv_tnl_enc_lim
*)&skb_network_header(skb
)[offset
];
914 if (tel
->encap_limit
== 0) {
915 icmpv6_send(skb
, ICMPV6_PARAMPROB
,
916 ICMPV6_HDR_FIELD
, offset
+ 2);
919 encap_limit
= tel
->encap_limit
- 1;
920 } else if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
921 encap_limit
= t
->parms
.encap_limit
;
923 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
924 fl6
.flowi6_proto
= IPPROTO_IPV6
;
926 dsfield
= ipv6_get_dsfield(ipv6h
);
927 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
928 fl6
.flowlabel
|= (*(__be32
*) ipv6h
& IPV6_TCLASS_MASK
);
929 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
)
930 fl6
.flowlabel
|= (*(__be32
*) ipv6h
& IPV6_FLOWLABEL_MASK
);
931 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
932 fl6
.flowi6_mark
= skb
->mark
;
934 err
= ip6gre_xmit2(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
);
936 if (err
== -EMSGSIZE
)
937 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
945 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
946 * @t: the outgoing tunnel device
947 * @hdr: IPv6 header from the incoming packet
950 * Avoid trivial tunneling loop by checking that tunnel exit-point
951 * doesn't match source of incoming packet.
958 static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl
*t
,
959 const struct ipv6hdr
*hdr
)
961 return ipv6_addr_equal(&t
->parms
.raddr
, &hdr
->saddr
);
964 static int ip6gre_xmit_other(struct sk_buff
*skb
, struct net_device
*dev
)
966 struct ip6_tnl
*t
= netdev_priv(dev
);
967 int encap_limit
= -1;
972 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
973 encap_limit
= t
->parms
.encap_limit
;
975 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
976 fl6
.flowi6_proto
= skb
->protocol
;
978 err
= ip6gre_xmit2(skb
, dev
, 0, &fl6
, encap_limit
, &mtu
);
983 static netdev_tx_t
ip6gre_tunnel_xmit(struct sk_buff
*skb
,
984 struct net_device
*dev
)
986 struct ip6_tnl
*t
= netdev_priv(dev
);
987 struct net_device_stats
*stats
= &t
->dev
->stats
;
990 if (!ip6_tnl_xmit_ctl(t
))
993 switch (skb
->protocol
) {
994 case htons(ETH_P_IP
):
995 ret
= ip6gre_xmit_ipv4(skb
, dev
);
997 case htons(ETH_P_IPV6
):
998 ret
= ip6gre_xmit_ipv6(skb
, dev
);
1001 ret
= ip6gre_xmit_other(skb
, dev
);
1008 return NETDEV_TX_OK
;
1012 stats
->tx_dropped
++;
1014 return NETDEV_TX_OK
;
1017 static void ip6gre_tnl_link_config(struct ip6_tnl
*t
, int set_mtu
)
1019 struct net_device
*dev
= t
->dev
;
1020 struct __ip6_tnl_parm
*p
= &t
->parms
;
1021 struct flowi6
*fl6
= &t
->fl
.u
.ip6
;
1022 int addend
= sizeof(struct ipv6hdr
) + 4;
1024 if (dev
->type
!= ARPHRD_ETHER
) {
1025 memcpy(dev
->dev_addr
, &p
->laddr
, sizeof(struct in6_addr
));
1026 memcpy(dev
->broadcast
, &p
->raddr
, sizeof(struct in6_addr
));
1029 /* Set up flowi template */
1030 fl6
->saddr
= p
->laddr
;
1031 fl6
->daddr
= p
->raddr
;
1032 fl6
->flowi6_oif
= p
->link
;
1035 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_TCLASS
))
1036 fl6
->flowlabel
|= IPV6_TCLASS_MASK
& p
->flowinfo
;
1037 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_FLOWLABEL
))
1038 fl6
->flowlabel
|= IPV6_FLOWLABEL_MASK
& p
->flowinfo
;
1040 p
->flags
&= ~(IP6_TNL_F_CAP_XMIT
|IP6_TNL_F_CAP_RCV
|IP6_TNL_F_CAP_PER_PACKET
);
1041 p
->flags
|= ip6_tnl_get_cap(t
, &p
->laddr
, &p
->raddr
);
1043 if (p
->flags
&IP6_TNL_F_CAP_XMIT
&&
1044 p
->flags
&IP6_TNL_F_CAP_RCV
&& dev
->type
!= ARPHRD_ETHER
)
1045 dev
->flags
|= IFF_POINTOPOINT
;
1047 dev
->flags
&= ~IFF_POINTOPOINT
;
1049 dev
->iflink
= p
->link
;
1051 /* Precalculate GRE options length */
1052 if (t
->parms
.o_flags
&(GRE_CSUM
|GRE_KEY
|GRE_SEQ
)) {
1053 if (t
->parms
.o_flags
&GRE_CSUM
)
1055 if (t
->parms
.o_flags
&GRE_KEY
)
1057 if (t
->parms
.o_flags
&GRE_SEQ
)
1061 if (p
->flags
& IP6_TNL_F_CAP_XMIT
) {
1062 int strict
= (ipv6_addr_type(&p
->raddr
) &
1063 (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
));
1065 struct rt6_info
*rt
= rt6_lookup(dev_net(dev
),
1066 &p
->raddr
, &p
->laddr
,
1073 dev
->hard_header_len
= rt
->dst
.dev
->hard_header_len
+ addend
;
1076 dev
->mtu
= rt
->dst
.dev
->mtu
- addend
;
1077 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1080 if (dev
->mtu
< IPV6_MIN_MTU
)
1081 dev
->mtu
= IPV6_MIN_MTU
;
1084 dst_release(&rt
->dst
);
1090 static int ip6gre_tnl_change(struct ip6_tnl
*t
,
1091 const struct __ip6_tnl_parm
*p
, int set_mtu
)
1093 t
->parms
.laddr
= p
->laddr
;
1094 t
->parms
.raddr
= p
->raddr
;
1095 t
->parms
.flags
= p
->flags
;
1096 t
->parms
.hop_limit
= p
->hop_limit
;
1097 t
->parms
.encap_limit
= p
->encap_limit
;
1098 t
->parms
.flowinfo
= p
->flowinfo
;
1099 t
->parms
.link
= p
->link
;
1100 t
->parms
.proto
= p
->proto
;
1101 t
->parms
.i_key
= p
->i_key
;
1102 t
->parms
.o_key
= p
->o_key
;
1103 t
->parms
.i_flags
= p
->i_flags
;
1104 t
->parms
.o_flags
= p
->o_flags
;
1105 ip6_tnl_dst_reset(t
);
1106 ip6gre_tnl_link_config(t
, set_mtu
);
1110 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm
*p
,
1111 const struct ip6_tnl_parm2
*u
)
1113 p
->laddr
= u
->laddr
;
1114 p
->raddr
= u
->raddr
;
1115 p
->flags
= u
->flags
;
1116 p
->hop_limit
= u
->hop_limit
;
1117 p
->encap_limit
= u
->encap_limit
;
1118 p
->flowinfo
= u
->flowinfo
;
1120 p
->i_key
= u
->i_key
;
1121 p
->o_key
= u
->o_key
;
1122 p
->i_flags
= u
->i_flags
;
1123 p
->o_flags
= u
->o_flags
;
1124 memcpy(p
->name
, u
->name
, sizeof(u
->name
));
1127 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2
*u
,
1128 const struct __ip6_tnl_parm
*p
)
1130 u
->proto
= IPPROTO_GRE
;
1131 u
->laddr
= p
->laddr
;
1132 u
->raddr
= p
->raddr
;
1133 u
->flags
= p
->flags
;
1134 u
->hop_limit
= p
->hop_limit
;
1135 u
->encap_limit
= p
->encap_limit
;
1136 u
->flowinfo
= p
->flowinfo
;
1138 u
->i_key
= p
->i_key
;
1139 u
->o_key
= p
->o_key
;
1140 u
->i_flags
= p
->i_flags
;
1141 u
->o_flags
= p
->o_flags
;
1142 memcpy(u
->name
, p
->name
, sizeof(u
->name
));
1145 static int ip6gre_tunnel_ioctl(struct net_device
*dev
,
1146 struct ifreq
*ifr
, int cmd
)
1149 struct ip6_tnl_parm2 p
;
1150 struct __ip6_tnl_parm p1
;
1152 struct net
*net
= dev_net(dev
);
1153 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1158 if (dev
== ign
->fb_tunnel_dev
) {
1159 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
))) {
1163 ip6gre_tnl_parm_from_user(&p1
, &p
);
1164 t
= ip6gre_tunnel_locate(net
, &p1
, 0);
1167 t
= netdev_priv(dev
);
1168 ip6gre_tnl_parm_to_user(&p
, &t
->parms
);
1169 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof(p
)))
1176 if (!capable(CAP_NET_ADMIN
))
1180 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
1184 if ((p
.i_flags
|p
.o_flags
)&(GRE_VERSION
|GRE_ROUTING
))
1187 if (!(p
.i_flags
&GRE_KEY
))
1189 if (!(p
.o_flags
&GRE_KEY
))
1192 ip6gre_tnl_parm_from_user(&p1
, &p
);
1193 t
= ip6gre_tunnel_locate(net
, &p1
, cmd
== SIOCADDTUNNEL
);
1195 if (dev
!= ign
->fb_tunnel_dev
&& cmd
== SIOCCHGTUNNEL
) {
1197 if (t
->dev
!= dev
) {
1202 t
= netdev_priv(dev
);
1204 ip6gre_tunnel_unlink(ign
, t
);
1206 ip6gre_tnl_change(t
, &p1
, 1);
1207 ip6gre_tunnel_link(ign
, t
);
1208 netdev_state_change(dev
);
1215 ip6gre_tnl_parm_to_user(&p
, &t
->parms
);
1216 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof(p
)))
1219 err
= (cmd
== SIOCADDTUNNEL
? -ENOBUFS
: -ENOENT
);
1224 if (!capable(CAP_NET_ADMIN
))
1227 if (dev
== ign
->fb_tunnel_dev
) {
1229 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
1232 ip6gre_tnl_parm_from_user(&p1
, &p
);
1233 t
= ip6gre_tunnel_locate(net
, &p1
, 0);
1237 if (t
== netdev_priv(ign
->fb_tunnel_dev
))
1241 unregister_netdevice(dev
);
1253 static int ip6gre_tunnel_change_mtu(struct net_device
*dev
, int new_mtu
)
1255 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
1257 new_mtu
> 0xFFF8 - dev
->hard_header_len
- tunnel
->hlen
)
1263 static int ip6gre_header(struct sk_buff
*skb
, struct net_device
*dev
,
1264 unsigned short type
,
1265 const void *daddr
, const void *saddr
, unsigned int len
)
1267 struct ip6_tnl
*t
= netdev_priv(dev
);
1268 struct ipv6hdr
*ipv6h
= (struct ipv6hdr
*)skb_push(skb
, t
->hlen
);
1269 __be16
*p
= (__be16
*)(ipv6h
+1);
1271 *(__be32
*)ipv6h
= t
->fl
.u
.ip6
.flowlabel
| htonl(0x60000000);
1272 ipv6h
->hop_limit
= t
->parms
.hop_limit
;
1273 ipv6h
->nexthdr
= NEXTHDR_GRE
;
1274 ipv6h
->saddr
= t
->parms
.laddr
;
1275 ipv6h
->daddr
= t
->parms
.raddr
;
1277 p
[0] = t
->parms
.o_flags
;
1281 * Set the source hardware address.
1285 memcpy(&ipv6h
->saddr
, saddr
, sizeof(struct in6_addr
));
1287 memcpy(&ipv6h
->daddr
, daddr
, sizeof(struct in6_addr
));
1288 if (!ipv6_addr_any(&ipv6h
->daddr
))
1294 static int ip6gre_header_parse(const struct sk_buff
*skb
, unsigned char *haddr
)
1296 const struct ipv6hdr
*ipv6h
= (const struct ipv6hdr
*)skb_mac_header(skb
);
1297 memcpy(haddr
, &ipv6h
->saddr
, sizeof(struct in6_addr
));
1298 return sizeof(struct in6_addr
);
1301 static const struct header_ops ip6gre_header_ops
= {
1302 .create
= ip6gre_header
,
1303 .parse
= ip6gre_header_parse
,
1306 static const struct net_device_ops ip6gre_netdev_ops
= {
1307 .ndo_init
= ip6gre_tunnel_init
,
1308 .ndo_uninit
= ip6gre_tunnel_uninit
,
1309 .ndo_start_xmit
= ip6gre_tunnel_xmit
,
1310 .ndo_do_ioctl
= ip6gre_tunnel_ioctl
,
1311 .ndo_change_mtu
= ip6gre_tunnel_change_mtu
,
1312 .ndo_get_stats64
= ip6gre_get_stats64
,
1315 static void ip6gre_dev_free(struct net_device
*dev
)
1317 free_percpu(dev
->tstats
);
1321 static void ip6gre_tunnel_setup(struct net_device
*dev
)
1325 dev
->netdev_ops
= &ip6gre_netdev_ops
;
1326 dev
->destructor
= ip6gre_dev_free
;
1328 dev
->type
= ARPHRD_IP6GRE
;
1329 dev
->hard_header_len
= LL_MAX_HEADER
+ sizeof(struct ipv6hdr
) + 4;
1330 dev
->mtu
= ETH_DATA_LEN
- sizeof(struct ipv6hdr
) - 4;
1331 t
= netdev_priv(dev
);
1332 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1334 dev
->flags
|= IFF_NOARP
;
1336 dev
->addr_len
= sizeof(struct in6_addr
);
1337 dev
->features
|= NETIF_F_NETNS_LOCAL
;
1338 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
1341 static int ip6gre_tunnel_init(struct net_device
*dev
)
1343 struct ip6_tnl
*tunnel
;
1345 tunnel
= netdev_priv(dev
);
1348 strcpy(tunnel
->parms
.name
, dev
->name
);
1350 memcpy(dev
->dev_addr
, &tunnel
->parms
.laddr
, sizeof(struct in6_addr
));
1351 memcpy(dev
->broadcast
, &tunnel
->parms
.raddr
, sizeof(struct in6_addr
));
1353 if (ipv6_addr_any(&tunnel
->parms
.raddr
))
1354 dev
->header_ops
= &ip6gre_header_ops
;
1356 dev
->tstats
= alloc_percpu(struct pcpu_tstats
);
1363 static void ip6gre_fb_tunnel_init(struct net_device
*dev
)
1365 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
1368 strcpy(tunnel
->parms
.name
, dev
->name
);
1370 tunnel
->hlen
= sizeof(struct ipv6hdr
) + 4;
1376 static struct inet6_protocol ip6gre_protocol __read_mostly
= {
1377 .handler
= ip6gre_rcv
,
1378 .err_handler
= ip6gre_err
,
1379 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
1382 static void ip6gre_destroy_tunnels(struct ip6gre_net
*ign
,
1383 struct list_head
*head
)
1387 for (prio
= 0; prio
< 4; prio
++) {
1389 for (h
= 0; h
< HASH_SIZE
; h
++) {
1392 t
= rtnl_dereference(ign
->tunnels
[prio
][h
]);
1395 unregister_netdevice_queue(t
->dev
, head
);
1396 t
= rtnl_dereference(t
->next
);
1402 static int __net_init
ip6gre_init_net(struct net
*net
)
1404 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1407 ign
->fb_tunnel_dev
= alloc_netdev(sizeof(struct ip6_tnl
), "ip6gre0",
1408 ip6gre_tunnel_setup
);
1409 if (!ign
->fb_tunnel_dev
) {
1413 dev_net_set(ign
->fb_tunnel_dev
, net
);
1415 ip6gre_fb_tunnel_init(ign
->fb_tunnel_dev
);
1416 ign
->fb_tunnel_dev
->rtnl_link_ops
= &ip6gre_link_ops
;
1418 err
= register_netdev(ign
->fb_tunnel_dev
);
1422 rcu_assign_pointer(ign
->tunnels_wc
[0],
1423 netdev_priv(ign
->fb_tunnel_dev
));
1427 ip6gre_dev_free(ign
->fb_tunnel_dev
);
1432 static void __net_exit
ip6gre_exit_net(struct net
*net
)
1434 struct ip6gre_net
*ign
;
1437 ign
= net_generic(net
, ip6gre_net_id
);
1439 ip6gre_destroy_tunnels(ign
, &list
);
1440 unregister_netdevice_many(&list
);
1444 static struct pernet_operations ip6gre_net_ops
= {
1445 .init
= ip6gre_init_net
,
1446 .exit
= ip6gre_exit_net
,
1447 .id
= &ip6gre_net_id
,
1448 .size
= sizeof(struct ip6gre_net
),
1451 static int ip6gre_tunnel_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1459 if (data
[IFLA_GRE_IFLAGS
])
1460 flags
|= nla_get_be16(data
[IFLA_GRE_IFLAGS
]);
1461 if (data
[IFLA_GRE_OFLAGS
])
1462 flags
|= nla_get_be16(data
[IFLA_GRE_OFLAGS
]);
1463 if (flags
& (GRE_VERSION
|GRE_ROUTING
))
1469 static int ip6gre_tap_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1471 struct in6_addr daddr
;
1473 if (tb
[IFLA_ADDRESS
]) {
1474 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
1476 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
1477 return -EADDRNOTAVAIL
;
1483 if (data
[IFLA_GRE_REMOTE
]) {
1484 nla_memcpy(&daddr
, data
[IFLA_GRE_REMOTE
], sizeof(struct in6_addr
));
1485 if (ipv6_addr_any(&daddr
))
1490 return ip6gre_tunnel_validate(tb
, data
);
1494 static void ip6gre_netlink_parms(struct nlattr
*data
[],
1495 struct __ip6_tnl_parm
*parms
)
1497 memset(parms
, 0, sizeof(*parms
));
1502 if (data
[IFLA_GRE_LINK
])
1503 parms
->link
= nla_get_u32(data
[IFLA_GRE_LINK
]);
1505 if (data
[IFLA_GRE_IFLAGS
])
1506 parms
->i_flags
= nla_get_be16(data
[IFLA_GRE_IFLAGS
]);
1508 if (data
[IFLA_GRE_OFLAGS
])
1509 parms
->o_flags
= nla_get_be16(data
[IFLA_GRE_OFLAGS
]);
1511 if (data
[IFLA_GRE_IKEY
])
1512 parms
->i_key
= nla_get_be32(data
[IFLA_GRE_IKEY
]);
1514 if (data
[IFLA_GRE_OKEY
])
1515 parms
->o_key
= nla_get_be32(data
[IFLA_GRE_OKEY
]);
1517 if (data
[IFLA_GRE_LOCAL
])
1518 nla_memcpy(&parms
->laddr
, data
[IFLA_GRE_LOCAL
], sizeof(struct in6_addr
));
1520 if (data
[IFLA_GRE_REMOTE
])
1521 nla_memcpy(&parms
->raddr
, data
[IFLA_GRE_REMOTE
], sizeof(struct in6_addr
));
1523 if (data
[IFLA_GRE_TTL
])
1524 parms
->hop_limit
= nla_get_u8(data
[IFLA_GRE_TTL
]);
1526 if (data
[IFLA_GRE_ENCAP_LIMIT
])
1527 parms
->encap_limit
= nla_get_u8(data
[IFLA_GRE_ENCAP_LIMIT
]);
1529 if (data
[IFLA_GRE_FLOWINFO
])
1530 parms
->flowinfo
= nla_get_u32(data
[IFLA_GRE_FLOWINFO
]);
1532 if (data
[IFLA_GRE_FLAGS
])
1533 parms
->flags
= nla_get_u32(data
[IFLA_GRE_FLAGS
]);
1536 static int ip6gre_tap_init(struct net_device
*dev
)
1538 struct ip6_tnl
*tunnel
;
1540 tunnel
= netdev_priv(dev
);
1543 strcpy(tunnel
->parms
.name
, dev
->name
);
1545 ip6gre_tnl_link_config(tunnel
, 1);
1547 dev
->tstats
= alloc_percpu(struct pcpu_tstats
);
1554 static const struct net_device_ops ip6gre_tap_netdev_ops
= {
1555 .ndo_init
= ip6gre_tap_init
,
1556 .ndo_uninit
= ip6gre_tunnel_uninit
,
1557 .ndo_start_xmit
= ip6gre_tunnel_xmit
,
1558 .ndo_set_mac_address
= eth_mac_addr
,
1559 .ndo_validate_addr
= eth_validate_addr
,
1560 .ndo_change_mtu
= ip6gre_tunnel_change_mtu
,
1561 .ndo_get_stats64
= ip6gre_get_stats64
,
1564 static void ip6gre_tap_setup(struct net_device
*dev
)
1569 dev
->netdev_ops
= &ip6gre_tap_netdev_ops
;
1570 dev
->destructor
= ip6gre_dev_free
;
1573 dev
->features
|= NETIF_F_NETNS_LOCAL
;
1576 static int ip6gre_newlink(struct net
*src_net
, struct net_device
*dev
,
1577 struct nlattr
*tb
[], struct nlattr
*data
[])
1580 struct net
*net
= dev_net(dev
);
1581 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1584 nt
= netdev_priv(dev
);
1585 ip6gre_netlink_parms(data
, &nt
->parms
);
1587 if (ip6gre_tunnel_find(net
, &nt
->parms
, dev
->type
))
1590 if (dev
->type
== ARPHRD_ETHER
&& !tb
[IFLA_ADDRESS
])
1591 eth_hw_addr_random(dev
);
1594 ip6gre_tnl_link_config(nt
, !tb
[IFLA_MTU
]);
1596 /* Can use a lockless transmit, unless we generate output sequences */
1597 if (!(nt
->parms
.o_flags
& GRE_SEQ
))
1598 dev
->features
|= NETIF_F_LLTX
;
1600 err
= register_netdevice(dev
);
1605 ip6gre_tunnel_link(ign
, nt
);
1611 static int ip6gre_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
1612 struct nlattr
*data
[])
1614 struct ip6_tnl
*t
, *nt
;
1615 struct net
*net
= dev_net(dev
);
1616 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1617 struct __ip6_tnl_parm p
;
1619 if (dev
== ign
->fb_tunnel_dev
)
1622 nt
= netdev_priv(dev
);
1623 ip6gre_netlink_parms(data
, &p
);
1625 t
= ip6gre_tunnel_locate(net
, &p
, 0);
1633 ip6gre_tunnel_unlink(ign
, t
);
1634 ip6gre_tnl_change(t
, &p
, !tb
[IFLA_MTU
]);
1635 ip6gre_tunnel_link(ign
, t
);
1636 netdev_state_change(dev
);
1642 static size_t ip6gre_get_size(const struct net_device
*dev
)
1647 /* IFLA_GRE_IFLAGS */
1649 /* IFLA_GRE_OFLAGS */
1655 /* IFLA_GRE_LOCAL */
1657 /* IFLA_GRE_REMOTE */
1663 /* IFLA_GRE_ENCAP_LIMIT */
1665 /* IFLA_GRE_FLOWINFO */
1667 /* IFLA_GRE_FLAGS */
1672 static int ip6gre_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1674 struct ip6_tnl
*t
= netdev_priv(dev
);
1675 struct __ip6_tnl_parm
*p
= &t
->parms
;
1677 if (nla_put_u32(skb
, IFLA_GRE_LINK
, p
->link
) ||
1678 nla_put_be16(skb
, IFLA_GRE_IFLAGS
, p
->i_flags
) ||
1679 nla_put_be16(skb
, IFLA_GRE_OFLAGS
, p
->o_flags
) ||
1680 nla_put_be32(skb
, IFLA_GRE_IKEY
, p
->i_key
) ||
1681 nla_put_be32(skb
, IFLA_GRE_OKEY
, p
->o_key
) ||
1682 nla_put(skb
, IFLA_GRE_LOCAL
, sizeof(struct in6_addr
), &p
->raddr
) ||
1683 nla_put(skb
, IFLA_GRE_REMOTE
, sizeof(struct in6_addr
), &p
->laddr
) ||
1684 nla_put_u8(skb
, IFLA_GRE_TTL
, p
->hop_limit
) ||
1685 /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/
1686 nla_put_u8(skb
, IFLA_GRE_ENCAP_LIMIT
, p
->encap_limit
) ||
1687 nla_put_be32(skb
, IFLA_GRE_FLOWINFO
, p
->flowinfo
) ||
1688 nla_put_u32(skb
, IFLA_GRE_FLAGS
, p
->flags
))
1689 goto nla_put_failure
;
1696 static const struct nla_policy ip6gre_policy
[IFLA_GRE_MAX
+ 1] = {
1697 [IFLA_GRE_LINK
] = { .type
= NLA_U32
},
1698 [IFLA_GRE_IFLAGS
] = { .type
= NLA_U16
},
1699 [IFLA_GRE_OFLAGS
] = { .type
= NLA_U16
},
1700 [IFLA_GRE_IKEY
] = { .type
= NLA_U32
},
1701 [IFLA_GRE_OKEY
] = { .type
= NLA_U32
},
1702 [IFLA_GRE_LOCAL
] = { .len
= FIELD_SIZEOF(struct ipv6hdr
, saddr
) },
1703 [IFLA_GRE_REMOTE
] = { .len
= FIELD_SIZEOF(struct ipv6hdr
, daddr
) },
1704 [IFLA_GRE_TTL
] = { .type
= NLA_U8
},
1705 [IFLA_GRE_ENCAP_LIMIT
] = { .type
= NLA_U8
},
1706 [IFLA_GRE_FLOWINFO
] = { .type
= NLA_U32
},
1707 [IFLA_GRE_FLAGS
] = { .type
= NLA_U32
},
1710 static struct rtnl_link_ops ip6gre_link_ops __read_mostly
= {
1712 .maxtype
= IFLA_GRE_MAX
,
1713 .policy
= ip6gre_policy
,
1714 .priv_size
= sizeof(struct ip6_tnl
),
1715 .setup
= ip6gre_tunnel_setup
,
1716 .validate
= ip6gre_tunnel_validate
,
1717 .newlink
= ip6gre_newlink
,
1718 .changelink
= ip6gre_changelink
,
1719 .get_size
= ip6gre_get_size
,
1720 .fill_info
= ip6gre_fill_info
,
1723 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly
= {
1724 .kind
= "ip6gretap",
1725 .maxtype
= IFLA_GRE_MAX
,
1726 .policy
= ip6gre_policy
,
1727 .priv_size
= sizeof(struct ip6_tnl
),
1728 .setup
= ip6gre_tap_setup
,
1729 .validate
= ip6gre_tap_validate
,
1730 .newlink
= ip6gre_newlink
,
1731 .changelink
= ip6gre_changelink
,
1732 .get_size
= ip6gre_get_size
,
1733 .fill_info
= ip6gre_fill_info
,
1737 * And now the modules code and kernel interface.
1740 static int __init
ip6gre_init(void)
1744 pr_info("GRE over IPv6 tunneling driver\n");
1746 err
= register_pernet_device(&ip6gre_net_ops
);
1750 err
= inet6_add_protocol(&ip6gre_protocol
, IPPROTO_GRE
);
1752 pr_info("%s: can't add protocol\n", __func__
);
1753 goto add_proto_failed
;
1756 err
= rtnl_link_register(&ip6gre_link_ops
);
1758 goto rtnl_link_failed
;
1760 err
= rtnl_link_register(&ip6gre_tap_ops
);
1762 goto tap_ops_failed
;
1768 rtnl_link_unregister(&ip6gre_link_ops
);
1770 inet6_del_protocol(&ip6gre_protocol
, IPPROTO_GRE
);
1772 unregister_pernet_device(&ip6gre_net_ops
);
1776 static void __exit
ip6gre_fini(void)
1778 rtnl_link_unregister(&ip6gre_tap_ops
);
1779 rtnl_link_unregister(&ip6gre_link_ops
);
1780 inet6_del_protocol(&ip6gre_protocol
, IPPROTO_GRE
);
1781 unregister_pernet_device(&ip6gre_net_ops
);
1784 module_init(ip6gre_init
);
1785 module_exit(ip6gre_fini
);
1786 MODULE_LICENSE("GPL");
1787 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
1788 MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
1789 MODULE_ALIAS_RTNL_LINK("ip6gre");
1790 MODULE_ALIAS_NETDEV("ip6gre0");