blob: fc5fbd7f67af5d702ea69899a4093b910e564366 [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);
Patrick McHardy0ad352c2012-08-26 19:14:08 +020018 unsigned int hh_len;
Harald Welte020b4c12005-08-09 19:39:00 -070019 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -050020 struct flowi6 fl6 = {
21 .flowi6_oif = skb->sk ? skb->sk->sk_bound_dev_if : 0,
22 .flowi6_mark = skb->mark,
23 .daddr = iph->daddr,
24 .saddr = iph->saddr,
Harald Welte020b4c12005-08-09 19:39:00 -070025 };
26
David S. Miller4c9483b2011-03-12 16:22:43 -050027 dst = ip6_route_output(net, skb->sk, &fl6);
Harald Welte020b4c12005-08-09 19:39:00 -070028 if (dst->error) {
Alexey Dobriyaneef9d902008-10-14 22:55:21 -070029 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
Patrick McHardy64ce2072005-08-09 20:50:53 -070030 LIMIT_NETDEBUG(KERN_DEBUG "ip6_route_me_harder: No more route.\n");
Harald Welte020b4c12005-08-09 19:39:00 -070031 dst_release(dst);
Patrick McHardy58e35d12013-04-05 06:41:11 +000032 return dst->error;
Harald Welte020b4c12005-08-09 19:39:00 -070033 }
34
35 /* Drop old route. */
Eric Dumazetadf30902009-06-02 05:19:30 +000036 skb_dst_drop(skb);
Harald Welte020b4c12005-08-09 19:39:00 -070037
Eric Dumazetadf30902009-06-02 05:19:30 +000038 skb_dst_set(skb, dst);
Ulrich Weber90348e02010-04-15 12:37:18 +020039
40#ifdef CONFIG_XFRM
41 if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
David S. Miller4c9483b2011-03-12 16:22:43 -050042 xfrm_decode_session(skb, flowi6_to_flowi(&fl6), AF_INET6) == 0) {
Ulrich Weber90348e02010-04-15 12:37:18 +020043 skb_dst_set(skb, NULL);
David S. Miller4c9483b2011-03-12 16:22:43 -050044 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), skb->sk, 0);
David S. Miller452edd52011-03-02 13:27:41 -080045 if (IS_ERR(dst))
Patrick McHardy58e35d12013-04-05 06:41:11 +000046 return PTR_ERR(dst);
Ulrich Weber90348e02010-04-15 12:37:18 +020047 skb_dst_set(skb, dst);
48 }
49#endif
50
Patrick McHardy0ad352c2012-08-26 19:14:08 +020051 /* Change in oif may mean change in hh_len. */
52 hh_len = skb_dst(skb)->dev->hard_header_len;
53 if (skb_headroom(skb) < hh_len &&
54 pskb_expand_head(skb, HH_DATA_ALIGN(hh_len - skb_headroom(skb)),
55 0, GFP_ATOMIC))
Patrick McHardy58e35d12013-04-05 06:41:11 +000056 return -ENOMEM;
Patrick McHardy0ad352c2012-08-26 19:14:08 +020057
Harald Welte020b4c12005-08-09 19:39:00 -070058 return 0;
59}
60EXPORT_SYMBOL(ip6_route_me_harder);
61
Harald Welte2cc7d572005-08-09 19:42:34 -070062/*
63 * Extra routing may needed on local out, as the QUEUE target never
64 * returns control to the table.
65 */
66
67struct ip6_rt_info {
68 struct in6_addr daddr;
69 struct in6_addr saddr;
Eric Leblond9f40ac72008-11-25 12:18:11 +010070 u_int32_t mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070071};
72
Patrick McHardy02f014d2007-12-05 01:26:33 -080073static void nf_ip6_saveroute(const struct sk_buff *skb,
74 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -070075{
Patrick McHardy02f014d2007-12-05 01:26:33 -080076 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -070077
Patrick McHardy02f014d2007-12-05 01:26:33 -080078 if (entry->hook == NF_INET_LOCAL_OUT) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +000079 const struct ipv6hdr *iph = ipv6_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070080
81 rt_info->daddr = iph->daddr;
82 rt_info->saddr = iph->saddr;
Eric Leblond9f40ac72008-11-25 12:18:11 +010083 rt_info->mark = skb->mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070084 }
85}
86
Patrick McHardy02f014d2007-12-05 01:26:33 -080087static int nf_ip6_reroute(struct sk_buff *skb,
88 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -070089{
Patrick McHardy02f014d2007-12-05 01:26:33 -080090 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -070091
Patrick McHardy02f014d2007-12-05 01:26:33 -080092 if (entry->hook == NF_INET_LOCAL_OUT) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +000093 const struct ipv6hdr *iph = ipv6_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070094 if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) ||
Eric Leblond9f40ac72008-11-25 12:18:11 +010095 !ipv6_addr_equal(&iph->saddr, &rt_info->saddr) ||
96 skb->mark != rt_info->mark)
Herbert Xu3db05fe2007-10-15 00:53:15 -070097 return ip6_route_me_harder(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070098 }
99 return 0;
100}
101
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200102static int nf_ip6_route(struct net *net, struct dst_entry **dst,
Florian Westphal0fae2e72011-04-04 17:00:54 +0200103 struct flowi *fl, bool strict)
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800104{
Florian Westphal0fae2e72011-04-04 17:00:54 +0200105 static const struct ipv6_pinfo fake_pinfo;
106 static const struct inet_sock fake_sk = {
107 /* makes ip6_route_output set RT6_LOOKUP_F_IFACE: */
108 .sk.sk_bound_dev_if = 1,
109 .pinet6 = (struct ipv6_pinfo *) &fake_pinfo,
110 };
111 const void *sk = strict ? &fake_sk : NULL;
Florian Westphal2dad81a2011-10-19 13:23:06 +0200112 struct dst_entry *result;
113 int err;
Florian Westphal0fae2e72011-04-04 17:00:54 +0200114
Florian Westphal2dad81a2011-10-19 13:23:06 +0200115 result = ip6_route_output(net, sk, &fl->u.ip6);
116 err = result->error;
117 if (err)
118 dst_release(result);
119 else
120 *dst = result;
121 return err;
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800122}
123
Al Virob51655b2006-11-14 21:40:42 -0800124__sum16 nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700125 unsigned int dataoff, u_int8_t protocol)
126{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000127 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800128 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700129
130 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700131 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800132 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700133 break;
134 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900135 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700136 csum_sub(skb->csum,
137 skb_checksum(skb, 0,
138 dataoff, 0)))) {
139 skb->ip_summed = CHECKSUM_UNNECESSARY;
140 break;
141 }
142 /* fall through */
143 case CHECKSUM_NONE:
Al Viro868c86b2006-11-14 21:35:48 -0800144 skb->csum = ~csum_unfold(
145 csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
Patrick McHardy422c3462006-04-06 14:18:43 -0700146 skb->len - dataoff,
147 protocol,
148 csum_sub(0,
149 skb_checksum(skb, 0,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900150 dataoff, 0))));
Patrick McHardy422c3462006-04-06 14:18:43 -0700151 csum = __skb_checksum_complete(skb);
152 }
153 return csum;
154}
Patrick McHardy422c3462006-04-06 14:18:43 -0700155EXPORT_SYMBOL(nf_ip6_checksum);
156
Patrick McHardyd63a6502008-03-20 15:15:53 +0100157static __sum16 nf_ip6_checksum_partial(struct sk_buff *skb, unsigned int hook,
158 unsigned int dataoff, unsigned int len,
159 u_int8_t protocol)
160{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000161 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100162 __wsum hsum;
163 __sum16 csum = 0;
164
165 switch (skb->ip_summed) {
166 case CHECKSUM_COMPLETE:
167 if (len == skb->len - dataoff)
168 return nf_ip6_checksum(skb, hook, dataoff, protocol);
169 /* fall through */
170 case CHECKSUM_NONE:
171 hsum = skb_checksum(skb, 0, dataoff, 0);
172 skb->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
173 &ip6h->daddr,
174 skb->len - dataoff,
175 protocol,
176 csum_sub(0, hsum)));
177 skb->ip_summed = CHECKSUM_NONE;
Shan Weic86ee672010-06-14 16:20:02 +0200178 return __skb_checksum_complete_head(skb, dataoff + len);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100179 }
180 return csum;
181};
182
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800183static const struct nf_afinfo nf_ip6_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100184 .family = AF_INET6,
185 .checksum = nf_ip6_checksum,
186 .checksum_partial = nf_ip6_checksum_partial,
187 .route = nf_ip6_route,
188 .saveroute = nf_ip6_saveroute,
189 .reroute = nf_ip6_reroute,
190 .route_key_size = sizeof(struct ip6_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700191};
192
193int __init ipv6_netfilter_init(void)
194{
Patrick McHardybce80322006-04-06 14:18:09 -0700195 return nf_register_afinfo(&nf_ip6_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700196}
197
David S. Miller5bf887f2006-01-10 21:02:21 -0800198/* This can be called from inet6_init() on errors, so it cannot
199 * be marked __exit. -DaveM
200 */
201void ipv6_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700202{
Patrick McHardybce80322006-04-06 14:18:09 -0700203 nf_unregister_afinfo(&nf_ip6_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700204}