blob: 3e9ecfaf67e203095197660801fab536919acc83 [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>
Harald Welte020b4c12005-08-09 19:39:00 -070010
11int ip6_route_me_harder(struct sk_buff *skb)
12{
13 struct ipv6hdr *iph = skb->nh.ipv6h;
14 struct dst_entry *dst;
15 struct flowi fl = {
16 .oif = skb->sk ? skb->sk->sk_bound_dev_if : 0,
17 .nl_u =
18 { .ip6_u =
19 { .daddr = iph->daddr,
20 .saddr = iph->saddr, } },
Harald Welte020b4c12005-08-09 19:39:00 -070021 };
22
23 dst = ip6_route_output(skb->sk, &fl);
24
Patrick McHardy3e3850e2006-01-06 23:04:54 -080025#ifdef CONFIG_XFRM
26 if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
27 xfrm_decode_session(skb, &fl, AF_INET6) == 0)
28 if (xfrm_lookup(&skb->dst, &fl, skb->sk, 0))
29 return -1;
30#endif
31
Harald Welte020b4c12005-08-09 19:39:00 -070032 if (dst->error) {
33 IP6_INC_STATS(IPSTATS_MIB_OUTNOROUTES);
Patrick McHardy64ce2072005-08-09 20:50:53 -070034 LIMIT_NETDEBUG(KERN_DEBUG "ip6_route_me_harder: No more route.\n");
Harald Welte020b4c12005-08-09 19:39:00 -070035 dst_release(dst);
36 return -EINVAL;
37 }
38
39 /* Drop old route. */
40 dst_release(skb->dst);
41
42 skb->dst = dst;
43 return 0;
44}
45EXPORT_SYMBOL(ip6_route_me_harder);
46
Harald Welte2cc7d572005-08-09 19:42:34 -070047/*
48 * Extra routing may needed on local out, as the QUEUE target never
49 * returns control to the table.
50 */
51
52struct ip6_rt_info {
53 struct in6_addr daddr;
54 struct in6_addr saddr;
55};
56
Patrick McHardybce80322006-04-06 14:18:09 -070057static void nf_ip6_saveroute(const struct sk_buff *skb, struct nf_info *info)
Harald Welte2cc7d572005-08-09 19:42:34 -070058{
59 struct ip6_rt_info *rt_info = nf_info_reroute(info);
60
61 if (info->hook == NF_IP6_LOCAL_OUT) {
62 struct ipv6hdr *iph = skb->nh.ipv6h;
63
64 rt_info->daddr = iph->daddr;
65 rt_info->saddr = iph->saddr;
66 }
67}
68
Patrick McHardybce80322006-04-06 14:18:09 -070069static int nf_ip6_reroute(struct sk_buff **pskb, const struct nf_info *info)
Harald Welte2cc7d572005-08-09 19:42:34 -070070{
71 struct ip6_rt_info *rt_info = nf_info_reroute(info);
72
73 if (info->hook == NF_IP6_LOCAL_OUT) {
74 struct ipv6hdr *iph = (*pskb)->nh.ipv6h;
75 if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) ||
76 !ipv6_addr_equal(&iph->saddr, &rt_info->saddr))
77 return ip6_route_me_harder(*pskb);
78 }
79 return 0;
80}
81
Patrick McHardy422c3462006-04-06 14:18:43 -070082unsigned int nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,
83 unsigned int dataoff, u_int8_t protocol)
84{
85 struct ipv6hdr *ip6h = skb->nh.ipv6h;
86 unsigned int csum = 0;
87
88 switch (skb->ip_summed) {
89 case CHECKSUM_HW:
90 if (hook != NF_IP6_PRE_ROUTING && hook != NF_IP6_LOCAL_IN)
91 break;
92 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
93 skb->len - dataoff, protocol,
94 csum_sub(skb->csum,
95 skb_checksum(skb, 0,
96 dataoff, 0)))) {
97 skb->ip_summed = CHECKSUM_UNNECESSARY;
98 break;
99 }
100 /* fall through */
101 case CHECKSUM_NONE:
102 skb->csum = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
103 skb->len - dataoff,
104 protocol,
105 csum_sub(0,
106 skb_checksum(skb, 0,
107 dataoff, 0)));
108 csum = __skb_checksum_complete(skb);
109 }
110 return csum;
111}
112
113EXPORT_SYMBOL(nf_ip6_checksum);
114
Patrick McHardybce80322006-04-06 14:18:09 -0700115static struct nf_afinfo nf_ip6_afinfo = {
116 .family = AF_INET6,
Patrick McHardy422c3462006-04-06 14:18:43 -0700117 .checksum = nf_ip6_checksum,
Patrick McHardybce80322006-04-06 14:18:09 -0700118 .saveroute = nf_ip6_saveroute,
119 .reroute = nf_ip6_reroute,
120 .route_key_size = sizeof(struct ip6_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700121};
122
123int __init ipv6_netfilter_init(void)
124{
Patrick McHardybce80322006-04-06 14:18:09 -0700125 return nf_register_afinfo(&nf_ip6_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700126}
127
David S. Miller5bf887f2006-01-10 21:02:21 -0800128/* This can be called from inet6_init() on errors, so it cannot
129 * be marked __exit. -DaveM
130 */
131void ipv6_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700132{
Patrick McHardybce80322006-04-06 14:18:09 -0700133 nf_unregister_afinfo(&nf_ip6_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700134}