blob: d5ed92b143469488f475816a1ebb08a5c1803558 [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;
18 struct flowi fl = {
19 .oif = skb->sk ? skb->sk->sk_bound_dev_if : 0,
Yasuyuki Kozakaibc5f7742007-03-04 16:00:32 -080020 .mark = skb->mark,
Harald Welte020b4c12005-08-09 19:39:00 -070021 .nl_u =
22 { .ip6_u =
23 { .daddr = iph->daddr,
24 .saddr = iph->saddr, } },
Harald Welte020b4c12005-08-09 19:39:00 -070025 };
26
Alexey Dobriyaneef9d902008-10-14 22:55:21 -070027 dst = ip6_route_output(net, skb->sk, &fl);
Harald Welte020b4c12005-08-09 19:39:00 -070028
Patrick McHardy3e3850e2006-01-06 23:04:54 -080029#ifdef CONFIG_XFRM
30 if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
Eric Dumazetadf30902009-06-02 05:19:30 +000031 xfrm_decode_session(skb, &fl, AF_INET6) == 0) {
32 struct dst_entry *dst2 = skb_dst(skb);
33
34 if (xfrm_lookup(net, &dst2, &fl, skb->sk, 0)) {
35 skb_dst_set(skb, NULL);
Patrick McHardy3e3850e2006-01-06 23:04:54 -080036 return -1;
Eric Dumazetadf30902009-06-02 05:19:30 +000037 }
38 skb_dst_set(skb, dst2);
39 }
Patrick McHardy3e3850e2006-01-06 23:04:54 -080040#endif
41
Harald Welte020b4c12005-08-09 19:39:00 -070042 if (dst->error) {
Alexey Dobriyaneef9d902008-10-14 22:55:21 -070043 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
Patrick McHardy64ce2072005-08-09 20:50:53 -070044 LIMIT_NETDEBUG(KERN_DEBUG "ip6_route_me_harder: No more route.\n");
Harald Welte020b4c12005-08-09 19:39:00 -070045 dst_release(dst);
46 return -EINVAL;
47 }
48
49 /* Drop old route. */
Eric Dumazetadf30902009-06-02 05:19:30 +000050 skb_dst_drop(skb);
Harald Welte020b4c12005-08-09 19:39:00 -070051
Eric Dumazetadf30902009-06-02 05:19:30 +000052 skb_dst_set(skb, dst);
Harald Welte020b4c12005-08-09 19:39:00 -070053 return 0;
54}
55EXPORT_SYMBOL(ip6_route_me_harder);
56
Harald Welte2cc7d572005-08-09 19:42:34 -070057/*
58 * Extra routing may needed on local out, as the QUEUE target never
59 * returns control to the table.
60 */
61
62struct ip6_rt_info {
63 struct in6_addr daddr;
64 struct in6_addr saddr;
Eric Leblond9f40ac72008-11-25 12:18:11 +010065 u_int32_t mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070066};
67
Patrick McHardy02f014d2007-12-05 01:26:33 -080068static void nf_ip6_saveroute(const struct sk_buff *skb,
69 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -070070{
Patrick McHardy02f014d2007-12-05 01:26:33 -080071 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -070072
Patrick McHardy02f014d2007-12-05 01:26:33 -080073 if (entry->hook == NF_INET_LOCAL_OUT) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070074 struct ipv6hdr *iph = ipv6_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070075
76 rt_info->daddr = iph->daddr;
77 rt_info->saddr = iph->saddr;
Eric Leblond9f40ac72008-11-25 12:18:11 +010078 rt_info->mark = skb->mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070079 }
80}
81
Patrick McHardy02f014d2007-12-05 01:26:33 -080082static int nf_ip6_reroute(struct sk_buff *skb,
83 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -070084{
Patrick McHardy02f014d2007-12-05 01:26:33 -080085 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -070086
Patrick McHardy02f014d2007-12-05 01:26:33 -080087 if (entry->hook == NF_INET_LOCAL_OUT) {
Herbert Xu3db05fe2007-10-15 00:53:15 -070088 struct ipv6hdr *iph = ipv6_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070089 if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) ||
Eric Leblond9f40ac72008-11-25 12:18:11 +010090 !ipv6_addr_equal(&iph->saddr, &rt_info->saddr) ||
91 skb->mark != rt_info->mark)
Herbert Xu3db05fe2007-10-15 00:53:15 -070092 return ip6_route_me_harder(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070093 }
94 return 0;
95}
96
Patrick McHardy1841a4c2007-12-05 01:22:05 -080097static int nf_ip6_route(struct dst_entry **dst, struct flowi *fl)
98{
Daniel Lezcano4591db42008-03-05 10:48:10 -080099 *dst = ip6_route_output(&init_net, NULL, fl);
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800100 return (*dst)->error;
101}
102
Al Virob51655b2006-11-14 21:40:42 -0800103__sum16 nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700104 unsigned int dataoff, u_int8_t protocol)
105{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700106 struct ipv6hdr *ip6h = ipv6_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800107 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700108
109 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700110 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800111 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700112 break;
113 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900114 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700115 csum_sub(skb->csum,
116 skb_checksum(skb, 0,
117 dataoff, 0)))) {
118 skb->ip_summed = CHECKSUM_UNNECESSARY;
119 break;
120 }
121 /* fall through */
122 case CHECKSUM_NONE:
Al Viro868c86b2006-11-14 21:35:48 -0800123 skb->csum = ~csum_unfold(
124 csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
Patrick McHardy422c3462006-04-06 14:18:43 -0700125 skb->len - dataoff,
126 protocol,
127 csum_sub(0,
128 skb_checksum(skb, 0,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900129 dataoff, 0))));
Patrick McHardy422c3462006-04-06 14:18:43 -0700130 csum = __skb_checksum_complete(skb);
131 }
132 return csum;
133}
Patrick McHardy422c3462006-04-06 14:18:43 -0700134EXPORT_SYMBOL(nf_ip6_checksum);
135
Patrick McHardyd63a6502008-03-20 15:15:53 +0100136static __sum16 nf_ip6_checksum_partial(struct sk_buff *skb, unsigned int hook,
137 unsigned int dataoff, unsigned int len,
138 u_int8_t protocol)
139{
140 struct ipv6hdr *ip6h = ipv6_hdr(skb);
141 __wsum hsum;
142 __sum16 csum = 0;
143
144 switch (skb->ip_summed) {
145 case CHECKSUM_COMPLETE:
146 if (len == skb->len - dataoff)
147 return nf_ip6_checksum(skb, hook, dataoff, protocol);
148 /* fall through */
149 case CHECKSUM_NONE:
150 hsum = skb_checksum(skb, 0, dataoff, 0);
151 skb->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
152 &ip6h->daddr,
153 skb->len - dataoff,
154 protocol,
155 csum_sub(0, hsum)));
156 skb->ip_summed = CHECKSUM_NONE;
157 csum = __skb_checksum_complete_head(skb, dataoff + len);
158 if (!csum)
159 skb->ip_summed = CHECKSUM_UNNECESSARY;
160 }
161 return csum;
162};
163
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800164static const struct nf_afinfo nf_ip6_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100165 .family = AF_INET6,
166 .checksum = nf_ip6_checksum,
167 .checksum_partial = nf_ip6_checksum_partial,
168 .route = nf_ip6_route,
169 .saveroute = nf_ip6_saveroute,
170 .reroute = nf_ip6_reroute,
171 .route_key_size = sizeof(struct ip6_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700172};
173
174int __init ipv6_netfilter_init(void)
175{
Patrick McHardybce80322006-04-06 14:18:09 -0700176 return nf_register_afinfo(&nf_ip6_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700177}
178
David S. Miller5bf887f2006-01-10 21:02:21 -0800179/* This can be called from inet6_init() on errors, so it cannot
180 * be marked __exit. -DaveM
181 */
182void ipv6_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700183{
Patrick McHardybce80322006-04-06 14:18:09 -0700184 nf_unregister_afinfo(&nf_ip6_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700185}