xref: /linux/net/ipv6/netfilter.c (revision c0c914eca7f251c70facc37dfebeaf176601918d)
1 /*
2  * IPv6 specific functions of netfilter core
3  *
4  * Rusty Russell (C) 2000 -- This code is GPL.
5  * Patrick McHardy (C) 2006-2012
6  */
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/ipv6.h>
10 #include <linux/netfilter.h>
11 #include <linux/netfilter_ipv6.h>
12 #include <linux/export.h>
13 #include <net/addrconf.h>
14 #include <net/dst.h>
15 #include <net/ipv6.h>
16 #include <net/ip6_route.h>
17 #include <net/xfrm.h>
18 #include <net/ip6_checksum.h>
19 #include <net/netfilter/nf_queue.h>
20 
21 int ip6_route_me_harder(struct net *net, struct sk_buff *skb)
22 {
23 	const struct ipv6hdr *iph = ipv6_hdr(skb);
24 	unsigned int hh_len;
25 	struct dst_entry *dst;
26 	struct flowi6 fl6 = {
27 		.flowi6_oif = skb->sk ? skb->sk->sk_bound_dev_if : 0,
28 		.flowi6_mark = skb->mark,
29 		.daddr = iph->daddr,
30 		.saddr = iph->saddr,
31 	};
32 	int err;
33 
34 	dst = ip6_route_output(net, skb->sk, &fl6);
35 	err = dst->error;
36 	if (err) {
37 		IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
38 		net_dbg_ratelimited("ip6_route_me_harder: No more route\n");
39 		dst_release(dst);
40 		return err;
41 	}
42 
43 	/* Drop old route. */
44 	skb_dst_drop(skb);
45 
46 	skb_dst_set(skb, dst);
47 
48 #ifdef CONFIG_XFRM
49 	if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
50 	    xfrm_decode_session(skb, flowi6_to_flowi(&fl6), AF_INET6) == 0) {
51 		skb_dst_set(skb, NULL);
52 		dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), skb->sk, 0);
53 		if (IS_ERR(dst))
54 			return PTR_ERR(dst);
55 		skb_dst_set(skb, dst);
56 	}
57 #endif
58 
59 	/* Change in oif may mean change in hh_len. */
60 	hh_len = skb_dst(skb)->dev->hard_header_len;
61 	if (skb_headroom(skb) < hh_len &&
62 	    pskb_expand_head(skb, HH_DATA_ALIGN(hh_len - skb_headroom(skb)),
63 			     0, GFP_ATOMIC))
64 		return -ENOMEM;
65 
66 	return 0;
67 }
68 EXPORT_SYMBOL(ip6_route_me_harder);
69 
70 /*
71  * Extra routing may needed on local out, as the QUEUE target never
72  * returns control to the table.
73  */
74 
75 struct ip6_rt_info {
76 	struct in6_addr daddr;
77 	struct in6_addr saddr;
78 	u_int32_t mark;
79 };
80 
81 static void nf_ip6_saveroute(const struct sk_buff *skb,
82 			     struct nf_queue_entry *entry)
83 {
84 	struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
85 
86 	if (entry->state.hook == NF_INET_LOCAL_OUT) {
87 		const struct ipv6hdr *iph = ipv6_hdr(skb);
88 
89 		rt_info->daddr = iph->daddr;
90 		rt_info->saddr = iph->saddr;
91 		rt_info->mark = skb->mark;
92 	}
93 }
94 
95 static int nf_ip6_reroute(struct net *net, struct sk_buff *skb,
96 			  const struct nf_queue_entry *entry)
97 {
98 	struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
99 
100 	if (entry->state.hook == NF_INET_LOCAL_OUT) {
101 		const struct ipv6hdr *iph = ipv6_hdr(skb);
102 		if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) ||
103 		    !ipv6_addr_equal(&iph->saddr, &rt_info->saddr) ||
104 		    skb->mark != rt_info->mark)
105 			return ip6_route_me_harder(net, skb);
106 	}
107 	return 0;
108 }
109 
110 static int nf_ip6_route(struct net *net, struct dst_entry **dst,
111 			struct flowi *fl, bool strict)
112 {
113 	static const struct ipv6_pinfo fake_pinfo;
114 	static const struct inet_sock fake_sk = {
115 		/* makes ip6_route_output set RT6_LOOKUP_F_IFACE: */
116 		.sk.sk_bound_dev_if = 1,
117 		.pinet6 = (struct ipv6_pinfo *) &fake_pinfo,
118 	};
119 	const void *sk = strict ? &fake_sk : NULL;
120 	struct dst_entry *result;
121 	int err;
122 
123 	result = ip6_route_output(net, sk, &fl->u.ip6);
124 	err = result->error;
125 	if (err)
126 		dst_release(result);
127 	else
128 		*dst = result;
129 	return err;
130 }
131 
132 __sum16 nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,
133 			     unsigned int dataoff, u_int8_t protocol)
134 {
135 	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
136 	__sum16 csum = 0;
137 
138 	switch (skb->ip_summed) {
139 	case CHECKSUM_COMPLETE:
140 		if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
141 			break;
142 		if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
143 				     skb->len - dataoff, protocol,
144 				     csum_sub(skb->csum,
145 					      skb_checksum(skb, 0,
146 							   dataoff, 0)))) {
147 			skb->ip_summed = CHECKSUM_UNNECESSARY;
148 			break;
149 		}
150 		/* fall through */
151 	case CHECKSUM_NONE:
152 		skb->csum = ~csum_unfold(
153 				csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
154 					     skb->len - dataoff,
155 					     protocol,
156 					     csum_sub(0,
157 						      skb_checksum(skb, 0,
158 								   dataoff, 0))));
159 		csum = __skb_checksum_complete(skb);
160 	}
161 	return csum;
162 }
163 EXPORT_SYMBOL(nf_ip6_checksum);
164 
165 static __sum16 nf_ip6_checksum_partial(struct sk_buff *skb, unsigned int hook,
166 				       unsigned int dataoff, unsigned int len,
167 				       u_int8_t protocol)
168 {
169 	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
170 	__wsum hsum;
171 	__sum16 csum = 0;
172 
173 	switch (skb->ip_summed) {
174 	case CHECKSUM_COMPLETE:
175 		if (len == skb->len - dataoff)
176 			return nf_ip6_checksum(skb, hook, dataoff, protocol);
177 		/* fall through */
178 	case CHECKSUM_NONE:
179 		hsum = skb_checksum(skb, 0, dataoff, 0);
180 		skb->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
181 							 &ip6h->daddr,
182 							 skb->len - dataoff,
183 							 protocol,
184 							 csum_sub(0, hsum)));
185 		skb->ip_summed = CHECKSUM_NONE;
186 		return __skb_checksum_complete_head(skb, dataoff + len);
187 	}
188 	return csum;
189 };
190 
191 static const struct nf_ipv6_ops ipv6ops = {
192 	.chk_addr	= ipv6_chk_addr,
193 	.route_input    = ip6_route_input,
194 	.fragment	= ip6_fragment
195 };
196 
197 static const struct nf_afinfo nf_ip6_afinfo = {
198 	.family			= AF_INET6,
199 	.checksum		= nf_ip6_checksum,
200 	.checksum_partial	= nf_ip6_checksum_partial,
201 	.route			= nf_ip6_route,
202 	.saveroute		= nf_ip6_saveroute,
203 	.reroute		= nf_ip6_reroute,
204 	.route_key_size		= sizeof(struct ip6_rt_info),
205 };
206 
207 int __init ipv6_netfilter_init(void)
208 {
209 	RCU_INIT_POINTER(nf_ipv6_ops, &ipv6ops);
210 	return nf_register_afinfo(&nf_ip6_afinfo);
211 }
212 
213 /* This can be called from inet6_init() on errors, so it cannot
214  * be marked __exit. -DaveM
215  */
216 void ipv6_netfilter_fini(void)
217 {
218 	RCU_INIT_POINTER(nf_ipv6_ops, NULL);
219 	nf_unregister_afinfo(&nf_ip6_afinfo);
220 }
221