blob: 95f3f1da0d7f2ff20c3afa3eeda315dd9e2e6b5f [file] [log] [blame]
Patrick McHardyf229f6c2013-04-06 15:24:29 +02001/*
2 * IPv6 specific functions of netfilter core
3 *
4 * Rusty Russell (C) 2000 -- This code is GPL.
5 * Patrick McHardy (C) 2006-2012
6 */
Harald Welte020b4c12005-08-09 19:39:00 -07007#include <linux/kernel.h>
Patrick McHardybb94aa12006-01-09 16:43:13 -08008#include <linux/init.h>
Harald Welte020b4c12005-08-09 19:39:00 -07009#include <linux/ipv6.h>
Harald Welte2cc7d572005-08-09 19:42:34 -070010#include <linux/netfilter.h>
11#include <linux/netfilter_ipv6.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040012#include <linux/export.h>
Florian Westphal2a7851b2013-05-17 03:56:10 +000013#include <net/addrconf.h>
Harald Welte020b4c12005-08-09 19:39:00 -070014#include <net/dst.h>
15#include <net/ipv6.h>
16#include <net/ip6_route.h>
Patrick McHardy3e3850e2006-01-06 23:04:54 -080017#include <net/xfrm.h>
Brian Haley503e4fa2006-04-07 15:00:06 -070018#include <net/ip6_checksum.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080019#include <net/netfilter/nf_queue.h>
Harald Welte020b4c12005-08-09 19:39:00 -070020
21int ip6_route_me_harder(struct sk_buff *skb)
22{
Eric Dumazetadf30902009-06-02 05:19:30 +000023 struct net *net = dev_net(skb_dst(skb)->dev);
Eric Dumazetb71d1d42011-04-22 04:53:02 +000024 const struct ipv6hdr *iph = ipv6_hdr(skb);
Patrick McHardy0ad352c2012-08-26 19:14:08 +020025 unsigned int hh_len;
Harald Welte020b4c12005-08-09 19:39:00 -070026 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -050027 struct flowi6 fl6 = {
28 .flowi6_oif = skb->sk ? skb->sk->sk_bound_dev_if : 0,
29 .flowi6_mark = skb->mark,
30 .daddr = iph->daddr,
31 .saddr = iph->saddr,
Harald Welte020b4c12005-08-09 19:39:00 -070032 };
33
David S. Miller4c9483b2011-03-12 16:22:43 -050034 dst = ip6_route_output(net, skb->sk, &fl6);
Harald Welte020b4c12005-08-09 19:39:00 -070035 if (dst->error) {
Alexey Dobriyaneef9d902008-10-14 22:55:21 -070036 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
Patrick McHardy64ce2072005-08-09 20:50:53 -070037 LIMIT_NETDEBUG(KERN_DEBUG "ip6_route_me_harder: No more route.\n");
Harald Welte020b4c12005-08-09 19:39:00 -070038 dst_release(dst);
Patrick McHardy58e35d12013-04-05 06:41:11 +000039 return dst->error;
Harald Welte020b4c12005-08-09 19:39:00 -070040 }
41
42 /* Drop old route. */
Eric Dumazetadf30902009-06-02 05:19:30 +000043 skb_dst_drop(skb);
Harald Welte020b4c12005-08-09 19:39:00 -070044
Eric Dumazetadf30902009-06-02 05:19:30 +000045 skb_dst_set(skb, dst);
Ulrich Weber90348e02010-04-15 12:37:18 +020046
47#ifdef CONFIG_XFRM
48 if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
David S. Miller4c9483b2011-03-12 16:22:43 -050049 xfrm_decode_session(skb, flowi6_to_flowi(&fl6), AF_INET6) == 0) {
Ulrich Weber90348e02010-04-15 12:37:18 +020050 skb_dst_set(skb, NULL);
David S. Miller4c9483b2011-03-12 16:22:43 -050051 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), skb->sk, 0);
David S. Miller452edd52011-03-02 13:27:41 -080052 if (IS_ERR(dst))
Patrick McHardy58e35d12013-04-05 06:41:11 +000053 return PTR_ERR(dst);
Ulrich Weber90348e02010-04-15 12:37:18 +020054 skb_dst_set(skb, dst);
55 }
56#endif
57
Patrick McHardy0ad352c2012-08-26 19:14:08 +020058 /* Change in oif may mean change in hh_len. */
59 hh_len = skb_dst(skb)->dev->hard_header_len;
60 if (skb_headroom(skb) < hh_len &&
61 pskb_expand_head(skb, HH_DATA_ALIGN(hh_len - skb_headroom(skb)),
62 0, GFP_ATOMIC))
Patrick McHardy58e35d12013-04-05 06:41:11 +000063 return -ENOMEM;
Patrick McHardy0ad352c2012-08-26 19:14:08 +020064
Harald Welte020b4c12005-08-09 19:39:00 -070065 return 0;
66}
67EXPORT_SYMBOL(ip6_route_me_harder);
68
Harald Welte2cc7d572005-08-09 19:42:34 -070069/*
70 * Extra routing may needed on local out, as the QUEUE target never
71 * returns control to the table.
72 */
73
74struct ip6_rt_info {
75 struct in6_addr daddr;
76 struct in6_addr saddr;
Eric Leblond9f40ac72008-11-25 12:18:11 +010077 u_int32_t mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070078};
79
Patrick McHardy02f014d2007-12-05 01:26:33 -080080static void nf_ip6_saveroute(const struct sk_buff *skb,
81 struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -070082{
Patrick McHardy02f014d2007-12-05 01:26:33 -080083 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -070084
Patrick McHardy02f014d2007-12-05 01:26:33 -080085 if (entry->hook == NF_INET_LOCAL_OUT) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +000086 const struct ipv6hdr *iph = ipv6_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -070087
88 rt_info->daddr = iph->daddr;
89 rt_info->saddr = iph->saddr;
Eric Leblond9f40ac72008-11-25 12:18:11 +010090 rt_info->mark = skb->mark;
Harald Welte2cc7d572005-08-09 19:42:34 -070091 }
92}
93
Patrick McHardy02f014d2007-12-05 01:26:33 -080094static int nf_ip6_reroute(struct sk_buff *skb,
95 const struct nf_queue_entry *entry)
Harald Welte2cc7d572005-08-09 19:42:34 -070096{
Patrick McHardy02f014d2007-12-05 01:26:33 -080097 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
Harald Welte2cc7d572005-08-09 19:42:34 -070098
Patrick McHardy02f014d2007-12-05 01:26:33 -080099 if (entry->hook == NF_INET_LOCAL_OUT) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000100 const struct ipv6hdr *iph = ipv6_hdr(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700101 if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) ||
Eric Leblond9f40ac72008-11-25 12:18:11 +0100102 !ipv6_addr_equal(&iph->saddr, &rt_info->saddr) ||
103 skb->mark != rt_info->mark)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700104 return ip6_route_me_harder(skb);
Harald Welte2cc7d572005-08-09 19:42:34 -0700105 }
106 return 0;
107}
108
Florian Westphal31ad3dd2011-04-04 16:56:29 +0200109static int nf_ip6_route(struct net *net, struct dst_entry **dst,
Florian Westphal0fae2e72011-04-04 17:00:54 +0200110 struct flowi *fl, bool strict)
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800111{
Florian Westphal0fae2e72011-04-04 17:00:54 +0200112 static const struct ipv6_pinfo fake_pinfo;
113 static const struct inet_sock fake_sk = {
114 /* makes ip6_route_output set RT6_LOOKUP_F_IFACE: */
115 .sk.sk_bound_dev_if = 1,
116 .pinet6 = (struct ipv6_pinfo *) &fake_pinfo,
117 };
118 const void *sk = strict ? &fake_sk : NULL;
Florian Westphal2dad81a2011-10-19 13:23:06 +0200119 struct dst_entry *result;
120 int err;
Florian Westphal0fae2e72011-04-04 17:00:54 +0200121
Florian Westphal2dad81a2011-10-19 13:23:06 +0200122 result = ip6_route_output(net, sk, &fl->u.ip6);
123 err = result->error;
124 if (err)
125 dst_release(result);
126 else
127 *dst = result;
128 return err;
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800129}
130
Al Virob51655b2006-11-14 21:40:42 -0800131__sum16 nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700132 unsigned int dataoff, u_int8_t protocol)
133{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000134 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
Al Virob51655b2006-11-14 21:40:42 -0800135 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700136
137 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700138 case CHECKSUM_COMPLETE:
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800139 if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
Patrick McHardy422c3462006-04-06 14:18:43 -0700140 break;
141 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900142 skb->len - dataoff, protocol,
Patrick McHardy422c3462006-04-06 14:18:43 -0700143 csum_sub(skb->csum,
144 skb_checksum(skb, 0,
145 dataoff, 0)))) {
146 skb->ip_summed = CHECKSUM_UNNECESSARY;
147 break;
148 }
149 /* fall through */
150 case CHECKSUM_NONE:
Al Viro868c86b2006-11-14 21:35:48 -0800151 skb->csum = ~csum_unfold(
152 csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
Patrick McHardy422c3462006-04-06 14:18:43 -0700153 skb->len - dataoff,
154 protocol,
155 csum_sub(0,
156 skb_checksum(skb, 0,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900157 dataoff, 0))));
Patrick McHardy422c3462006-04-06 14:18:43 -0700158 csum = __skb_checksum_complete(skb);
159 }
160 return csum;
161}
Patrick McHardy422c3462006-04-06 14:18:43 -0700162EXPORT_SYMBOL(nf_ip6_checksum);
163
Patrick McHardyd63a6502008-03-20 15:15:53 +0100164static __sum16 nf_ip6_checksum_partial(struct sk_buff *skb, unsigned int hook,
165 unsigned int dataoff, unsigned int len,
166 u_int8_t protocol)
167{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000168 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100169 __wsum hsum;
170 __sum16 csum = 0;
171
172 switch (skb->ip_summed) {
173 case CHECKSUM_COMPLETE:
174 if (len == skb->len - dataoff)
175 return nf_ip6_checksum(skb, hook, dataoff, protocol);
176 /* fall through */
177 case CHECKSUM_NONE:
178 hsum = skb_checksum(skb, 0, dataoff, 0);
179 skb->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
180 &ip6h->daddr,
181 skb->len - dataoff,
182 protocol,
183 csum_sub(0, hsum)));
184 skb->ip_summed = CHECKSUM_NONE;
Shan Weic86ee672010-06-14 16:20:02 +0200185 return __skb_checksum_complete_head(skb, dataoff + len);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100186 }
187 return csum;
188};
189
Florian Westphal2a7851b2013-05-17 03:56:10 +0000190static const struct nf_ipv6_ops ipv6ops = {
191 .chk_addr = ipv6_chk_addr,
192};
193
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800194static const struct nf_afinfo nf_ip6_afinfo = {
Patrick McHardyd63a6502008-03-20 15:15:53 +0100195 .family = AF_INET6,
196 .checksum = nf_ip6_checksum,
197 .checksum_partial = nf_ip6_checksum_partial,
198 .route = nf_ip6_route,
199 .saveroute = nf_ip6_saveroute,
200 .reroute = nf_ip6_reroute,
201 .route_key_size = sizeof(struct ip6_rt_info),
Harald Welte2cc7d572005-08-09 19:42:34 -0700202};
203
204int __init ipv6_netfilter_init(void)
205{
Florian Westphal2a7851b2013-05-17 03:56:10 +0000206 RCU_INIT_POINTER(nf_ipv6_ops, &ipv6ops);
Patrick McHardybce80322006-04-06 14:18:09 -0700207 return nf_register_afinfo(&nf_ip6_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700208}
209
David S. Miller5bf887f2006-01-10 21:02:21 -0800210/* This can be called from inet6_init() on errors, so it cannot
211 * be marked __exit. -DaveM
212 */
213void ipv6_netfilter_fini(void)
Harald Welte2cc7d572005-08-09 19:42:34 -0700214{
Florian Westphal2a7851b2013-05-17 03:56:10 +0000215 RCU_INIT_POINTER(nf_ipv6_ops, NULL);
Patrick McHardybce80322006-04-06 14:18:09 -0700216 nf_unregister_afinfo(&nf_ip6_afinfo);
Harald Welte2cc7d572005-08-09 19:42:34 -0700217}