blob: 39aaca2b4fd2db98d7463110107ecc0ec70b2097 [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>
Harald Welte020b4c12005-08-09 19:39:00 -07006#include <net/dst.h>
7#include <net/ipv6.h>
8#include <net/ip6_route.h>
Patrick McHardy3e3850e2006-01-06 23:04:54 -08009#include <net/xfrm.h>
Brian Haley503e4fa2006-04-07 15:00:06 -070010#include <net/ip6_checksum.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080011#include <net/netfilter/nf_queue.h>
Harald Welte020b4c12005-08-09 19:39:00 -070012
13int ip6_route_me_harder(struct sk_buff *skb)
14{
Eric Dumazetadf30902009-06-02 05:19:30 +000015 struct net *net = dev_net(skb_dst(skb)->dev);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070016 struct ipv6hdr *iph = ipv6_hdr(skb);
Harald Welte020b4c12005-08-09 19:39:00 -070017 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -050018 struct flowi6 fl6 = {
19 .flowi6_oif = skb->sk ? skb->sk->sk_bound_dev_if : 0,
20 .flowi6_mark = skb->mark,
21 .daddr = iph->daddr,
22 .saddr = iph->saddr,
Harald Welte020b4c12005-08-09 19:39:00 -070023 };
24
David S. Miller4c9483b2011-03-12 16:22:43 -050025 dst = ip6_route_output(net, skb->sk, &fl6);
Harald Welte020b4c12005-08-09 19:39:00 -070026 if (dst->error) {
Alexey Dobriyaneef9d902008-10-14 22:55:21 -070027 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
Patrick McHardy64ce2072005-08-09 20:50:53 -070028 LIMIT_NETDEBUG(KERN_DEBUG "ip6_route_me_harder: No more route.\n");
Harald Welte020b4c12005-08-09 19:39:00 -070029 dst_release(dst);
30 return -EINVAL;
31 }
32
33 /* Drop old route. */
Eric Dumazetadf30902009-06-02 05:19:30 +000034 skb_dst_drop(skb);
Harald Welte020b4c12005-08-09 19:39:00 -070035
Eric Dumazetadf30902009-06-02 05:19:30 +000036 skb_dst_set(skb, dst);
Ulrich Weber90348e02010-04-15 12:37:18 +020037
38#ifdef CONFIG_XFRM
39 if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
David S. Miller4c9483b2011-03-12 16:22:43 -050040 xfrm_decode_session(skb, flowi6_to_flowi(&fl6), AF_INET6) == 0) {
Ulrich Weber90348e02010-04-15 12:37:18 +020041 skb_dst_set(skb, NULL);
David S. Miller4c9483b2011-03-12 16:22:43 -050042 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), skb->sk, 0);
David S. Miller452edd52011-03-02 13:27:41 -080043 if (IS_ERR(dst))
Ulrich Weber90348e02010-04-15 12:37:18 +020044 return -1;
45 skb_dst_set(skb, dst);
46 }
47#endif
48
Harald Welte020b4c12005-08-09 19:39:00 -070049 return 0;
50}
51EXPORT_SYMBOL(ip6_route_me_harder);
52
Harald Welte2cc7d572005-08-09 19:42:34 -070053/*
54 * Extra routing may needed on local out, as the QUEUE target never
55 * returns control to the table.
56 */
57
58struct ip6_rt_info {
59 struct in6_addr daddr;
60 struct in6_addr saddr;
Eric Leblond9f40ac72008-11-25 12:18:11 +010061 u_int32_t mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070062};
63
Patrick McHardy02f014d2007-12-05 01:26:33 -080064static void nf_ip6_saveroute(const struct sk_buff *skb,
65 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -070066{
Patrick McHardy02f014d2007-12-05 01:26:33 -080067 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -070068
Patrick McHardy02f014d2007-12-05 01:26:33 -080069 if (entry->hook == NF_INET_LOCAL_OUT) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070070 struct ipv6hdr *iph = ipv6_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070071
72 rt_info->daddr = iph->daddr;
73 rt_info->saddr = iph->saddr;
Eric Leblond9f40ac72008-11-25 12:18:11 +010074 rt_info->mark = skb->mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070075 }
76}
77
Patrick McHardy02f014d2007-12-05 01:26:33 -080078static int nf_ip6_reroute(struct sk_buff *skb,
79 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -070080{
Patrick McHardy02f014d2007-12-05 01:26:33 -080081 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -070082
Patrick McHardy02f014d2007-12-05 01:26:33 -080083 if (entry->hook == NF_INET_LOCAL_OUT) {
Herbert Xu3db05fe2007-10-15 00:53:15 -070084 struct ipv6hdr *iph = ipv6_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070085 if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) ||
Eric Leblond9f40ac72008-11-25 12:18:11 +010086 !ipv6_addr_equal(&iph->saddr, &rt_info->saddr) ||
87 skb->mark != rt_info->mark)
Herbert Xu3db05fe2007-10-15 00:53:15 -070088 return ip6_route_me_harder(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070089 }
90 return 0;
91}
92
Patrick McHardy1841a4c2007-12-05 01:22:05 -080093static int nf_ip6_route(struct dst_entry **dst, struct flowi *fl)
94{
David S. Miller4c9483b2011-03-12 16:22:43 -050095 *dst = ip6_route_output(&init_net, NULL, &fl->u.ip6);
Patrick McHardy1841a4c2007-12-05 01:22:05 -080096 return (*dst)->error;
97}
98
Al Virob51655b2006-11-14 21:40:42 -080099__sum16 nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700100 unsigned int dataoff, u_int8_t protocol)
101{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700102 struct ipv6hdr *ip6h = ipv6_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800103 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700104
105 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700106 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800107 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700108 break;
109 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900110 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700111 csum_sub(skb->csum,
112 skb_checksum(skb, 0,
113 dataoff, 0)))) {
114 skb->ip_summed = CHECKSUM_UNNECESSARY;
115 break;
116 }
117 /* fall through */
118 case CHECKSUM_NONE:
Al Viro868c86b2006-11-14 21:35:48 -0800119 skb->csum = ~csum_unfold(
120 csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
Patrick McHardy422c3462006-04-06 14:18:43 -0700121 skb->len - dataoff,
122 protocol,
123 csum_sub(0,
124 skb_checksum(skb, 0,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900125 dataoff, 0))));
Patrick McHardy422c3462006-04-06 14:18:43 -0700126 csum = __skb_checksum_complete(skb);
127 }
128 return csum;
129}
Patrick McHardy422c3462006-04-06 14:18:43 -0700130EXPORT_SYMBOL(nf_ip6_checksum);
131
Patrick McHardyd63a6502008-03-20 15:15:53 +0100132static __sum16 nf_ip6_checksum_partial(struct sk_buff *skb, unsigned int hook,
133 unsigned int dataoff, unsigned int len,
134 u_int8_t protocol)
135{
136 struct ipv6hdr *ip6h = ipv6_hdr(skb);
137 __wsum hsum;
138 __sum16 csum = 0;
139
140 switch (skb->ip_summed) {
141 case CHECKSUM_COMPLETE:
142 if (len == skb->len - dataoff)
143 return nf_ip6_checksum(skb, hook, dataoff, protocol);
144 /* fall through */
145 case CHECKSUM_NONE:
146 hsum = skb_checksum(skb, 0, dataoff, 0);
147 skb->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
148 &ip6h->daddr,
149 skb->len - dataoff,
150 protocol,
151 csum_sub(0, hsum)));
152 skb->ip_summed = CHECKSUM_NONE;
Shan Weic86ee672010-06-14 16:20:02 +0200153 return __skb_checksum_complete_head(skb, dataoff + len);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100154 }
155 return csum;
156};
157
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800158static const struct nf_afinfo nf_ip6_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100159 .family = AF_INET6,
160 .checksum = nf_ip6_checksum,
161 .checksum_partial = nf_ip6_checksum_partial,
162 .route = nf_ip6_route,
163 .saveroute = nf_ip6_saveroute,
164 .reroute = nf_ip6_reroute,
165 .route_key_size = sizeof(struct ip6_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700166};
167
168int __init ipv6_netfilter_init(void)
169{
Patrick McHardybce80322006-04-06 14:18:09 -0700170 return nf_register_afinfo(&nf_ip6_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700171}
172
David S. Miller5bf887f2006-01-10 21:02:21 -0800173/* This can be called from inet6_init() on errors, so it cannot
174 * be marked __exit. -DaveM
175 */
176void ipv6_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700177{
Patrick McHardybce80322006-04-06 14:18:09 -0700178 nf_unregister_afinfo(&nf_ip6_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700179}