20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/module.h>
23 #include <linux/capability.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
28 #include <linux/net.h>
29 #include <linux/in6.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/icmp.h>
33 #include <linux/slab.h>
34 #include <asm/uaccess.h>
36 #include <linux/netfilter_ipv4.h>
37 #include <linux/if_ether.h>
66 #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
87 #define for_each_ip_tunnel_rcu(start) \
88 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
110 start = u64_stats_fetch_begin_bh(&tstats->
syncp);
115 }
while (u64_stats_fetch_retry_bh(&tstats->
syncp, start));
139 unsigned int h0 =
HASH(remote);
140 unsigned int h1 =
HASH(local);
145 if (local == t->
parms.iph.saddr &&
146 remote == t->
parms.iph.daddr &&
152 if (remote == t->
parms.iph.daddr &&
158 if (local == t->
parms.iph.saddr &&
191 return __ipip6_bucket(sitn, &t->
parms);
199 for (tp = ipip6_bucket(sitn, t);
219 #ifdef CONFIG_IPV6_SIT_6RD
223 ipv6_addr_set(&t->ip6rd.prefix,
htonl(0x20020000), 0, 0, 0);
224 t->ip6rd.relay_prefix = 0;
225 t->ip6rd.prefixlen = 16;
226 t->ip6rd.relay_prefixlen = 0;
229 memcpy(&t->ip6rd, &t0->ip6rd,
sizeof(t->ip6rd));
234 static struct ip_tunnel *ipip6_tunnel_locate(
struct net *net,
245 for (tp = __ipip6_bucket(sitn, parms);
248 if (local == t->
parms.iph.saddr &&
249 remote == t->
parms.iph.daddr &&
265 dev =
alloc_netdev(
sizeof(*t), name, ipip6_tunnel_setup);
269 dev_net_set(dev, net);
271 nt = netdev_priv(dev);
274 if (ipip6_tunnel_init(dev) < 0)
276 ipip6_tunnel_clone_6rd(dev, sitn);
288 ipip6_tunnel_link(sitn, nt);
297 #define for_each_prl_rcu(start) \
298 for (prl = rcu_dereference(start); \
300 prl = rcu_dereference(prl->next))
308 if (prl->
addr == addr)
314 static int ipip6_tunnel_get_prl(
struct ip_tunnel *t,
319 unsigned int cmax,
c = 0,
ca,
len;
324 cmax = kprl.datalen /
sizeof(kprl);
367 len =
sizeof(*kp) *
c;
471 p = __ipip6_tunnel_locate_prl(t, iph->
saddr);
478 const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
480 if (ipv6_addr_is_isatap(addr6) &&
481 (addr6->s6_addr32[3] == iph->
saddr) &&
491 static void ipip6_tunnel_uninit(
struct net_device *dev)
493 struct net *net = dev_net(dev);
499 ipip6_tunnel_unlink(sitn, netdev_priv(dev));
500 ipip6_tunnel_del_prl(netdev_priv(dev),
NULL);
514 const int type = icmp_hdr(skb)->type;
515 const int code = icmp_hdr(skb)->code;
548 t = ipip6_tunnel_lookup(dev_net(skb->
dev),
568 if (t->
parms.iph.daddr == 0)
584 static inline void ipip6_ecn_decapsulate(
const struct iphdr *iph,
struct sk_buff *skb)
586 if (INET_ECN_is_ce(iph->
tos))
587 IP6_ECN_set_ce(ipv6_hdr(skb));
590 static int ipip6_rcv(
struct sk_buff *skb)
592 const struct iphdr *iph;
595 if (!pskb_may_pull(skb,
sizeof(
struct ipv6hdr)))
600 tunnel = ipip6_tunnel_lookup(dev_net(skb->
dev), skb->
dev,
602 if (tunnel !=
NULL) {
607 skb_reset_network_header(skb);
608 IPCB(skb)->flags = 0;
613 !isatap_chksrc(skb, iph, tunnel)) {
614 tunnel->
dev->stats.rx_errors++;
623 __skb_tunnel_rx(skb, tunnel->
dev);
625 ipip6_ecn_decapsulate(iph, skb);
648 #ifdef CONFIG_IPV6_SIT_6RD
649 if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
650 tunnel->ip6rd.prefixlen)) {
651 unsigned int pbw0, pbi0;
655 pbw0 = tunnel->ip6rd.prefixlen >> 5;
656 pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
658 d = (
ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
659 tunnel->ip6rd.relay_prefixlen;
661 pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
663 d |=
ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
666 dst = tunnel->ip6rd.relay_prefix |
htonl(d);
669 if (v6dst->s6_addr16[0] ==
htons(0x2002)) {
671 memcpy(&dst, &v6dst->s6_addr16[1], 4);
685 struct ip_tunnel *tunnel = netdev_priv(dev);
687 const struct iphdr *tiph = &tunnel->
parms.iph;
688 const struct ipv6hdr *iph6 = ipv6_hdr(skb);
689 u8 tos = tunnel->
parms.iph.tos;
694 unsigned int max_headroom;
705 tos = ipv6_get_dsfield(iph6);
710 bool do_tx_error =
false;
713 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->
daddr);
721 addr_type = ipv6_addr_type(addr6);
724 ipv6_addr_is_isatap(addr6))
725 dst = addr6->s6_addr32[3];
729 neigh_release(neigh);
735 dst = try_6rd(&iph6->
daddr, tunnel);
739 bool do_tx_error =
false;
742 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->
daddr);
750 addr_type = ipv6_addr_type(addr6);
753 addr6 = &ipv6_hdr(skb)->daddr;
754 addr_type = ipv6_addr_type(addr6);
758 dst = addr6->s6_addr32[3];
762 neigh_release(neigh);
767 rt = ip_route_output_ports(dev_net(dev), &fl4,
NULL,
773 dev->
stats.tx_carrier_errors++;
778 dev->
stats.tx_carrier_errors++;
785 dev->
stats.collisions++;
790 mtu = dst_mtu(&rt->
dst) -
sizeof(
struct iphdr);
793 dev->
stats.collisions++;
803 if (tunnel->
parms.iph.daddr && skb_dst(skb))
804 skb_dst(skb)->ops->update_pmtu(skb_dst(skb),
NULL, skb, mtu);
806 if (skb->
len > mtu) {
817 dst_link_failure(skb);
827 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
828 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
832 dev->
stats.tx_dropped++;
837 skb_set_owner_w(new_skb, skb->
sk);
840 iph6 = ipv6_hdr(skb);
845 skb_reset_network_header(skb);
847 IPCB(skb)->flags = 0;
849 skb_dst_set(skb, &rt->
dst);
857 iph->ihl =
sizeof(
struct iphdr)>>2;
860 iph->
tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
861 iph->
daddr = fl4.daddr;
862 iph->
saddr = fl4.saddr;
864 if ((iph->
ttl = tiph->
ttl) == 0)
873 dst_link_failure(skb);
875 dev->
stats.tx_errors++;
880 static void ipip6_tunnel_bind_dev(
struct net_device *dev)
884 const struct iphdr *iph;
887 tunnel = netdev_priv(dev);
888 iph = &tunnel->parms.iph;
891 struct rtable *rt = ip_route_output_ports(dev_net(dev), &fl4,
NULL,
905 if (!tdev && tunnel->parms.link)
914 dev->
iflink = tunnel->parms.link;
924 struct net *net = dev_net(dev);
926 #ifdef CONFIG_IPV6_SIT_6RD
932 #ifdef CONFIG_IPV6_SIT_6RD
941 t = ipip6_tunnel_locate(net, &p, 0);
944 t = netdev_priv(dev);
952 #ifdef CONFIG_IPV6_SIT_6RD
954 ip6rd.prefix = t->ip6rd.prefix;
955 ip6rd.relay_prefix = t->ip6rd.relay_prefix;
956 ip6rd.prefixlen = t->ip6rd.prefixlen;
957 ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
977 if (p.iph.version != 4 || p.iph.protocol !=
IPPROTO_IPV6 ||
978 p.iph.ihl != 5 || (p.iph.frag_off&
htons(~
IP_DF)))
997 t = netdev_priv(dev);
998 ipip6_tunnel_unlink(sitn, t);
1000 t->
parms.iph.saddr = p.iph.saddr;
1001 t->
parms.iph.daddr = p.iph.daddr;
1004 ipip6_tunnel_link(sitn, t);
1012 t->
parms.iph.ttl = p.iph.ttl;
1013 t->
parms.iph.tos = p.iph.tos;
1014 if (t->
parms.link != p.link) {
1015 t->
parms.link = p.link;
1016 ipip6_tunnel_bind_dev(dev);
1036 if ((t = ipip6_tunnel_locate(net, &p, 0)) ==
NULL)
1043 unregister_netdevice(dev);
1052 if (!(t = netdev_priv(dev)))
1070 if (!(t = netdev_priv(dev)))
1075 err = ipip6_tunnel_del_prl(t, &prl);
1079 err = ipip6_tunnel_add_prl(t, &prl, cmd ==
SIOCCHGPRL);
1085 #ifdef CONFIG_IPV6_SIT_6RD
1098 t = netdev_priv(dev);
1105 if (ip6rd.relay_prefixlen > 32 ||
1106 ip6rd.prefixlen + (32 - ip6rd.relay_prefixlen) > 64)
1109 ipv6_addr_prefix(&
prefix, &ip6rd.prefix,
1111 if (!ipv6_addr_equal(&
prefix, &ip6rd.prefix))
1113 if (ip6rd.relay_prefixlen)
1114 relay_prefix = ip6rd.relay_prefix &
1115 htonl(0xffffffffUL <<
1116 (32 - ip6rd.relay_prefixlen));
1119 if (relay_prefix != ip6rd.relay_prefix)
1122 t->ip6rd.prefix =
prefix;
1123 t->ip6rd.relay_prefix = relay_prefix;
1124 t->ip6rd.prefixlen = ip6rd.prefixlen;
1125 t->ip6rd.relay_prefixlen = ip6rd.relay_prefixlen;
1127 ipip6_tunnel_clone_6rd(dev, sitn);
1141 static int ipip6_tunnel_change_mtu(
struct net_device *dev,
int new_mtu)
1143 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 -
sizeof(
struct iphdr))
1150 .ndo_uninit = ipip6_tunnel_uninit,
1151 .ndo_start_xmit = ipip6_tunnel_xmit,
1152 .ndo_do_ioctl = ipip6_tunnel_ioctl,
1153 .ndo_change_mtu = ipip6_tunnel_change_mtu,
1154 .ndo_get_stats64= ipip6_get_stats64,
1157 static void ipip6_dev_free(
struct net_device *dev)
1163 static void ipip6_tunnel_setup(
struct net_device *dev)
1179 static int ipip6_tunnel_init(
struct net_device *dev)
1181 struct ip_tunnel *tunnel = netdev_priv(dev);
1188 ipip6_tunnel_bind_dev(dev);
1198 struct ip_tunnel *tunnel = netdev_priv(dev);
1200 struct net *net = dev_net(dev);
1220 .handler = ipip6_rcv,
1221 .err_handler = ipip6_err,
1229 for (prio = 1; prio < 4; prio++) {
1243 static int __net_init sit_init_net(
struct net *net)
1255 ipip6_tunnel_setup);
1284 static void __net_exit sit_exit_net(
struct net *net)
1290 sit_destroy_tunnels(sitn, &
list);
1297 .init = sit_init_net,
1298 .exit = sit_exit_net,
1300 .size =
sizeof(
struct sit_net),
1303 static void __exit sit_cleanup(
void)
1311 static int __init sit_init(
void)
1315 pr_info(
"IPv6 over IPv4 tunneling driver\n");
1323 pr_info(
"%s: can't add protocol\n", __func__);