blob: db31561cc8df31afbf7f18fd4dd849811be5a8dd [file] [log] [blame]
Harald Welte020b4c12005-08-09 19:39:00 -07001#include <linux/kernel.h>
Patrick McHardybb94aa12006-01-09 16:43:13 -08002#include <linux/init.h>
Harald Welte020b4c12005-08-09 19:39:00 -07003#include <linux/ipv6.h>
Harald Welte2cc7d572005-08-09 19:42:34 -07004#include <linux/netfilter.h>
5#include <linux/netfilter_ipv6.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -04006#include <linux/export.h>
Harald Welte020b4c12005-08-09 19:39:00 -07007#include <net/dst.h>
8#include <net/ipv6.h>
9#include <net/ip6_route.h>
Patrick McHardy3e3850e2006-01-06 23:04:54 -080010#include <net/xfrm.h>
Brian Haley503e4fa2006-04-07 15:00:06 -070011#include <net/ip6_checksum.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080012#include <net/netfilter/nf_queue.h>
Harald Welte020b4c12005-08-09 19:39:00 -070013
14int ip6_route_me_harder(struct sk_buff *skb)
15{
Eric Dumazetadf30902009-06-02 05:19:30 +000016 struct net *net = dev_net(skb_dst(skb)->dev);
Eric Dumazetb71d1d42011-04-22 04:53:02 +000017 const struct ipv6hdr *iph = ipv6_hdr(skb);
Harald Welte020b4c12005-08-09 19:39:00 -070018 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -050019 struct flowi6 fl6 = {
20 .flowi6_oif = skb->sk ? skb->sk->sk_bound_dev_if : 0,
21 .flowi6_mark = skb->mark,
22 .daddr = iph->daddr,
23 .saddr = iph->saddr,
Harald Welte020b4c12005-08-09 19:39:00 -070024 };
25
David S. Miller4c9483b2011-03-12 16:22:43 -050026 dst = ip6_route_output(net, skb->sk, &fl6);
Harald Welte020b4c12005-08-09 19:39:00 -070027 if (dst->error) {
Alexey Dobriyaneef9d902008-10-14 22:55:21 -070028 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
Patrick McHardy64ce2072005-08-09 20:50:53 -070029 LIMIT_NETDEBUG(KERN_DEBUG "ip6_route_me_harder: No more route.\n");
Harald Welte020b4c12005-08-09 19:39:00 -070030 dst_release(dst);
31 return -EINVAL;
32 }
33
34 /* Drop old route. */
Eric Dumazetadf30902009-06-02 05:19:30 +000035 skb_dst_drop(skb);
Harald Welte020b4c12005-08-09 19:39:00 -070036
Eric Dumazetadf30902009-06-02 05:19:30 +000037 skb_dst_set(skb, dst);
Ulrich Weber90348e02010-04-15 12:37:18 +020038
39#ifdef CONFIG_XFRM
40 if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
David S. Miller4c9483b2011-03-12 16:22:43 -050041 xfrm_decode_session(skb, flowi6_to_flowi(&fl6), AF_INET6) == 0) {
Ulrich Weber90348e02010-04-15 12:37:18 +020042 skb_dst_set(skb, NULL);
David S. Miller4c9483b2011-03-12 16:22:43 -050043 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), skb->sk, 0);
David S. Miller452edd52011-03-02 13:27:41 -080044 if (IS_ERR(dst))
Ulrich Weber90348e02010-04-15 12:37:18 +020045 return -1;
46 skb_dst_set(skb, dst);
47 }
48#endif
49
Harald Welte020b4c12005-08-09 19:39:00 -070050 return 0;
51}
52EXPORT_SYMBOL(ip6_route_me_harder);
53
Harald Welte2cc7d572005-08-09 19:42:34 -070054/*
55 * Extra routing may needed on local out, as the QUEUE target never
56 * returns control to the table.
57 */
58
59struct ip6_rt_info {
60 struct in6_addr daddr;
61 struct in6_addr saddr;
Eric Leblond9f40ac72008-11-25 12:18:11 +010062 u_int32_t mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070063};
64
Patrick McHardy02f014d2007-12-05 01:26:33 -080065static void nf_ip6_saveroute(const struct sk_buff *skb,
66 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -070067{
Patrick McHardy02f014d2007-12-05 01:26:33 -080068 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -070069
Patrick McHardy02f014d2007-12-05 01:26:33 -080070 if (entry->hook == NF_INET_LOCAL_OUT) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +000071 const struct ipv6hdr *iph = ipv6_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070072
73 rt_info->daddr = iph->daddr;
74 rt_info->saddr = iph->saddr;
Eric Leblond9f40ac72008-11-25 12:18:11 +010075 rt_info->mark = skb->mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070076 }
77}
78
Patrick McHardy02f014d2007-12-05 01:26:33 -080079static int nf_ip6_reroute(struct sk_buff *skb,
80 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -070081{
Patrick McHardy02f014d2007-12-05 01:26:33 -080082 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -070083
Patrick McHardy02f014d2007-12-05 01:26:33 -080084 if (entry->hook == NF_INET_LOCAL_OUT) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +000085 const struct ipv6hdr *iph = ipv6_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070086 if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) ||
Eric Leblond9f40ac72008-11-25 12:18:11 +010087 !ipv6_addr_equal(&iph->saddr, &rt_info->saddr) ||
88 skb->mark != rt_info->mark)
Herbert Xu3db05fe2007-10-15 00:53:15 -070089 return ip6_route_me_harder(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070090 }
91 return 0;
92}
93
Florian Westphal31ad3dd2011-04-04 16:56:29 +020094static int nf_ip6_route(struct net *net, struct dst_entry **dst,
Florian Westphal0fae2e72011-04-04 17:00:54 +020095 struct flowi *fl, bool strict)
Patrick McHardy1841a4c2007-12-05 01:22:05 -080096{
Florian Westphal0fae2e72011-04-04 17:00:54 +020097 static const struct ipv6_pinfo fake_pinfo;
98 static const struct inet_sock fake_sk = {
99 /* makes ip6_route_output set RT6_LOOKUP_F_IFACE: */
100 .sk.sk_bound_dev_if = 1,
101 .pinet6 = (struct ipv6_pinfo *) &fake_pinfo,
102 };
103 const void *sk = strict ? &fake_sk : NULL;
Florian Westphal2dad81a2011-10-19 13:23:06 +0200104 struct dst_entry *result;
105 int err;
Florian Westphal0fae2e72011-04-04 17:00:54 +0200106
Florian Westphal2dad81a2011-10-19 13:23:06 +0200107 result = ip6_route_output(net, sk, &fl->u.ip6);
108 err = result->error;
109 if (err)
110 dst_release(result);
111 else
112 *dst = result;
113 return err;
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800114}
115
Al Virob51655b2006-11-14 21:40:42 -0800116__sum16 nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700117 unsigned int dataoff, u_int8_t protocol)
118{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000119 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800120 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700121
122 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700123 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800124 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700125 break;
126 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900127 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700128 csum_sub(skb->csum,
129 skb_checksum(skb, 0,
130 dataoff, 0)))) {
131 skb->ip_summed = CHECKSUM_UNNECESSARY;
132 break;
133 }
134 /* fall through */
135 case CHECKSUM_NONE:
Al Viro868c86b2006-11-14 21:35:48 -0800136 skb->csum = ~csum_unfold(
137 csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
Patrick McHardy422c3462006-04-06 14:18:43 -0700138 skb->len - dataoff,
139 protocol,
140 csum_sub(0,
141 skb_checksum(skb, 0,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900142 dataoff, 0))));
Patrick McHardy422c3462006-04-06 14:18:43 -0700143 csum = __skb_checksum_complete(skb);
144 }
145 return csum;
146}
Patrick McHardy422c3462006-04-06 14:18:43 -0700147EXPORT_SYMBOL(nf_ip6_checksum);
148
Patrick McHardyd63a6502008-03-20 15:15:53 +0100149static __sum16 nf_ip6_checksum_partial(struct sk_buff *skb, unsigned int hook,
150 unsigned int dataoff, unsigned int len,
151 u_int8_t protocol)
152{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000153 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100154 __wsum hsum;
155 __sum16 csum = 0;
156
157 switch (skb->ip_summed) {
158 case CHECKSUM_COMPLETE:
159 if (len == skb->len - dataoff)
160 return nf_ip6_checksum(skb, hook, dataoff, protocol);
161 /* fall through */
162 case CHECKSUM_NONE:
163 hsum = skb_checksum(skb, 0, dataoff, 0);
164 skb->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
165 &ip6h->daddr,
166 skb->len - dataoff,
167 protocol,
168 csum_sub(0, hsum)));
169 skb->ip_summed = CHECKSUM_NONE;
Shan Weic86ee672010-06-14 16:20:02 +0200170 return __skb_checksum_complete_head(skb, dataoff + len);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100171 }
172 return csum;
173};
174
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800175static const struct nf_afinfo nf_ip6_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100176 .family = AF_INET6,
177 .checksum = nf_ip6_checksum,
178 .checksum_partial = nf_ip6_checksum_partial,
179 .route = nf_ip6_route,
180 .saveroute = nf_ip6_saveroute,
181 .reroute = nf_ip6_reroute,
182 .route_key_size = sizeof(struct ip6_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700183};
184
185int __init ipv6_netfilter_init(void)
186{
Patrick McHardybce80322006-04-06 14:18:09 -0700187 return nf_register_afinfo(&nf_ip6_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700188}
189
David S. Miller5bf887f2006-01-10 21:02:21 -0800190/* This can be called from inet6_init() on errors, so it cannot
191 * be marked __exit. -DaveM
192 */
193void ipv6_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700194{
Patrick McHardybce80322006-04-06 14:18:09 -0700195 nf_unregister_afinfo(&nf_ip6_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700196}