1 // SPDX-License-Identifier: GPL-2.0-only
2 /* (C) 1999-2001 Paul `Rusty' Russell
3  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
4  */
5 
6 #include <linux/module.h>
7 #include <net/ipv6.h>
8 #include <net/ip6_route.h>
9 #include <net/ip6_fib.h>
10 #include <net/ip6_checksum.h>
11 #include <net/netfilter/ipv6/nf_reject.h>
12 #include <linux/netfilter_ipv6.h>
13 #include <linux/netfilter_bridge.h>
14 
nf_reject_v6_csum_ok(struct sk_buff * skb,int hook)15 static bool nf_reject_v6_csum_ok(struct sk_buff *skb, int hook)
16 {
17 	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
18 	int thoff;
19 	__be16 fo;
20 	u8 proto = ip6h->nexthdr;
21 
22 	if (skb_csum_unnecessary(skb))
23 		return true;
24 
25 	if (ip6h->payload_len &&
26 	    pskb_trim_rcsum(skb, ntohs(ip6h->payload_len) + sizeof(*ip6h)))
27 		return false;
28 
29 	ip6h = ipv6_hdr(skb);
30 	thoff = ipv6_skip_exthdr(skb, ((u8*)(ip6h+1) - skb->data), &proto, &fo);
31 	if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
32 		return false;
33 
34 	if (!nf_reject_verify_csum(skb, thoff, proto))
35 		return true;
36 
37 	return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
38 }
39 
nf_reject_ip6hdr_validate(struct sk_buff * skb)40 static int nf_reject_ip6hdr_validate(struct sk_buff *skb)
41 {
42 	struct ipv6hdr *hdr;
43 	u32 pkt_len;
44 
45 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
46 		return 0;
47 
48 	hdr = ipv6_hdr(skb);
49 	if (hdr->version != 6)
50 		return 0;
51 
52 	pkt_len = ntohs(hdr->payload_len);
53 	if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
54 		return 0;
55 
56 	return 1;
57 }
58 
nf_reject_skb_v6_tcp_reset(struct net * net,struct sk_buff * oldskb,const struct net_device * dev,int hook)59 struct sk_buff *nf_reject_skb_v6_tcp_reset(struct net *net,
60 					   struct sk_buff *oldskb,
61 					   const struct net_device *dev,
62 					   int hook)
63 {
64 	struct sk_buff *nskb;
65 	const struct tcphdr *oth;
66 	struct tcphdr _oth;
67 	unsigned int otcplen;
68 	struct ipv6hdr *nip6h;
69 
70 	if (!nf_reject_ip6hdr_validate(oldskb))
71 		return NULL;
72 
73 	oth = nf_reject_ip6_tcphdr_get(oldskb, &_oth, &otcplen, hook);
74 	if (!oth)
75 		return NULL;
76 
77 	nskb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(struct tcphdr) +
78 			 LL_MAX_HEADER, GFP_ATOMIC);
79 	if (!nskb)
80 		return NULL;
81 
82 	nskb->dev = (struct net_device *)dev;
83 
84 	skb_reserve(nskb, LL_MAX_HEADER);
85 	nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
86 				     READ_ONCE(net->ipv6.devconf_all->hop_limit));
87 	nf_reject_ip6_tcphdr_put(nskb, oldskb, oth, otcplen);
88 	nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
89 
90 	return nskb;
91 }
92 EXPORT_SYMBOL_GPL(nf_reject_skb_v6_tcp_reset);
93 
nf_reject_skb_v6_unreach(struct net * net,struct sk_buff * oldskb,const struct net_device * dev,int hook,u8 code)94 struct sk_buff *nf_reject_skb_v6_unreach(struct net *net,
95 					 struct sk_buff *oldskb,
96 					 const struct net_device *dev,
97 					 int hook, u8 code)
98 {
99 	struct sk_buff *nskb;
100 	struct ipv6hdr *nip6h;
101 	struct icmp6hdr *icmp6h;
102 	unsigned int len;
103 
104 	if (!nf_reject_ip6hdr_validate(oldskb))
105 		return NULL;
106 
107 	/* Include "As much of invoking packet as possible without the ICMPv6
108 	 * packet exceeding the minimum IPv6 MTU" in the ICMP payload.
109 	 */
110 	len = min_t(unsigned int, 1220, oldskb->len);
111 
112 	if (!pskb_may_pull(oldskb, len))
113 		return NULL;
114 
115 	if (!nf_reject_v6_csum_ok(oldskb, hook))
116 		return NULL;
117 
118 	nskb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr) +
119 			 LL_MAX_HEADER + len, GFP_ATOMIC);
120 	if (!nskb)
121 		return NULL;
122 
123 	nskb->dev = (struct net_device *)dev;
124 
125 	skb_reserve(nskb, LL_MAX_HEADER);
126 	nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_ICMPV6,
127 				     READ_ONCE(net->ipv6.devconf_all->hop_limit));
128 
129 	skb_reset_transport_header(nskb);
130 	icmp6h = skb_put_zero(nskb, sizeof(struct icmp6hdr));
131 	icmp6h->icmp6_type = ICMPV6_DEST_UNREACH;
132 	icmp6h->icmp6_code = code;
133 
134 	skb_put_data(nskb, skb_network_header(oldskb), len);
135 	nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
136 
137 	icmp6h->icmp6_cksum =
138 		csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr,
139 				nskb->len - sizeof(struct ipv6hdr),
140 				IPPROTO_ICMPV6,
141 				csum_partial(icmp6h,
142 					     nskb->len - sizeof(struct ipv6hdr),
143 					     0));
144 
145 	return nskb;
146 }
147 EXPORT_SYMBOL_GPL(nf_reject_skb_v6_unreach);
148 
nf_reject_ip6_tcphdr_get(struct sk_buff * oldskb,struct tcphdr * otcph,unsigned int * otcplen,int hook)149 const struct tcphdr *nf_reject_ip6_tcphdr_get(struct sk_buff *oldskb,
150 					      struct tcphdr *otcph,
151 					      unsigned int *otcplen, int hook)
152 {
153 	const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
154 	u8 proto;
155 	__be16 frag_off;
156 	int tcphoff;
157 
158 	proto = oip6h->nexthdr;
159 	tcphoff = ipv6_skip_exthdr(oldskb, ((u8 *)(oip6h + 1) - oldskb->data),
160 				   &proto, &frag_off);
161 
162 	if ((tcphoff < 0) || (tcphoff > oldskb->len)) {
163 		pr_debug("Cannot get TCP header.\n");
164 		return NULL;
165 	}
166 
167 	*otcplen = oldskb->len - tcphoff;
168 
169 	/* IP header checks: fragment, too short. */
170 	if (proto != IPPROTO_TCP || *otcplen < sizeof(struct tcphdr)) {
171 		pr_debug("proto(%d) != IPPROTO_TCP or too short (len = %d)\n",
172 			 proto, *otcplen);
173 		return NULL;
174 	}
175 
176 	otcph = skb_header_pointer(oldskb, tcphoff, sizeof(struct tcphdr),
177 				   otcph);
178 	if (otcph == NULL)
179 		return NULL;
180 
181 	/* No RST for RST. */
182 	if (otcph->rst) {
183 		pr_debug("RST is set\n");
184 		return NULL;
185 	}
186 
187 	/* Check checksum. */
188 	if (nf_ip6_checksum(oldskb, hook, tcphoff, IPPROTO_TCP)) {
189 		pr_debug("TCP checksum is invalid\n");
190 		return NULL;
191 	}
192 
193 	return otcph;
194 }
195 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_get);
196 
nf_reject_ip6hdr_put(struct sk_buff * nskb,const struct sk_buff * oldskb,__u8 protocol,int hoplimit)197 struct ipv6hdr *nf_reject_ip6hdr_put(struct sk_buff *nskb,
198 				     const struct sk_buff *oldskb,
199 				     __u8 protocol, int hoplimit)
200 {
201 	struct ipv6hdr *ip6h;
202 	const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
203 #define DEFAULT_TOS_VALUE	0x0U
204 	const __u8 tclass = DEFAULT_TOS_VALUE;
205 
206 	skb_put(nskb, sizeof(struct ipv6hdr));
207 	skb_reset_network_header(nskb);
208 	ip6h = ipv6_hdr(nskb);
209 	ip6_flow_hdr(ip6h, tclass, 0);
210 	ip6h->hop_limit = hoplimit;
211 	ip6h->nexthdr = protocol;
212 	ip6h->saddr = oip6h->daddr;
213 	ip6h->daddr = oip6h->saddr;
214 
215 	nskb->protocol = htons(ETH_P_IPV6);
216 
217 	return ip6h;
218 }
219 EXPORT_SYMBOL_GPL(nf_reject_ip6hdr_put);
220 
nf_reject_ip6_tcphdr_put(struct sk_buff * nskb,const struct sk_buff * oldskb,const struct tcphdr * oth,unsigned int otcplen)221 void nf_reject_ip6_tcphdr_put(struct sk_buff *nskb,
222 			      const struct sk_buff *oldskb,
223 			      const struct tcphdr *oth, unsigned int otcplen)
224 {
225 	struct tcphdr *tcph;
226 
227 	skb_reset_transport_header(nskb);
228 	tcph = skb_put_zero(nskb, sizeof(struct tcphdr));
229 	/* Truncate to length (no data) */
230 	tcph->doff = sizeof(struct tcphdr)/4;
231 	tcph->source = oth->dest;
232 	tcph->dest = oth->source;
233 
234 	if (oth->ack) {
235 		tcph->seq = oth->ack_seq;
236 	} else {
237 		tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
238 				      otcplen - (oth->doff<<2));
239 		tcph->ack = 1;
240 	}
241 
242 	tcph->rst = 1;
243 
244 	/* Adjust TCP checksum */
245 	tcph->check = csum_ipv6_magic(&ipv6_hdr(nskb)->saddr,
246 				      &ipv6_hdr(nskb)->daddr,
247 				      sizeof(struct tcphdr), IPPROTO_TCP,
248 				      csum_partial(tcph,
249 						   sizeof(struct tcphdr), 0));
250 }
251 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_put);
252 
nf_reject6_fill_skb_dst(struct sk_buff * skb_in)253 static int nf_reject6_fill_skb_dst(struct sk_buff *skb_in)
254 {
255 	struct dst_entry *dst = NULL;
256 	struct flowi fl;
257 
258 	memset(&fl, 0, sizeof(struct flowi));
259 	fl.u.ip6.daddr = ipv6_hdr(skb_in)->saddr;
260 	nf_ip6_route(dev_net(skb_in->dev), &dst, &fl, false);
261 	if (!dst)
262 		return -1;
263 
264 	skb_dst_set(skb_in, dst);
265 	return 0;
266 }
267 
nf_send_reset6(struct net * net,struct sock * sk,struct sk_buff * oldskb,int hook)268 void nf_send_reset6(struct net *net, struct sock *sk, struct sk_buff *oldskb,
269 		    int hook)
270 {
271 	const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
272 	struct dst_entry *dst = NULL;
273 	const struct tcphdr *otcph;
274 	struct sk_buff *nskb;
275 	struct tcphdr _otcph;
276 	unsigned int otcplen;
277 	struct flowi6 fl6;
278 
279 	if ((!(ipv6_addr_type(&oip6h->saddr) & IPV6_ADDR_UNICAST)) ||
280 	    (!(ipv6_addr_type(&oip6h->daddr) & IPV6_ADDR_UNICAST))) {
281 		pr_debug("addr is not unicast.\n");
282 		return;
283 	}
284 
285 	otcph = nf_reject_ip6_tcphdr_get(oldskb, &_otcph, &otcplen, hook);
286 	if (!otcph)
287 		return;
288 
289 	memset(&fl6, 0, sizeof(fl6));
290 	fl6.flowi6_proto = IPPROTO_TCP;
291 	fl6.saddr = oip6h->daddr;
292 	fl6.daddr = oip6h->saddr;
293 	fl6.fl6_sport = otcph->dest;
294 	fl6.fl6_dport = otcph->source;
295 
296 	if (hook == NF_INET_PRE_ROUTING || hook == NF_INET_INGRESS) {
297 		nf_ip6_route(net, &dst, flowi6_to_flowi(&fl6), false);
298 		if (!dst)
299 			return;
300 		skb_dst_set(oldskb, dst);
301 	}
302 
303 	fl6.flowi6_oif = l3mdev_master_ifindex(skb_dst(oldskb)->dev);
304 	fl6.flowi6_mark = IP6_REPLY_MARK(net, oldskb->mark);
305 	security_skb_classify_flow(oldskb, flowi6_to_flowi_common(&fl6));
306 	dst = ip6_route_output(net, NULL, &fl6);
307 	if (dst->error) {
308 		dst_release(dst);
309 		return;
310 	}
311 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
312 	if (IS_ERR(dst))
313 		return;
314 
315 	nskb = alloc_skb(LL_MAX_HEADER + sizeof(struct ipv6hdr) +
316 			 sizeof(struct tcphdr) + dst->trailer_len,
317 			 GFP_ATOMIC);
318 
319 	if (!nskb) {
320 		net_dbg_ratelimited("cannot alloc skb\n");
321 		dst_release(dst);
322 		return;
323 	}
324 
325 	skb_dst_set(nskb, dst);
326 
327 	nskb->mark = fl6.flowi6_mark;
328 
329 	skb_reserve(nskb, LL_MAX_HEADER);
330 	nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP, ip6_dst_hoplimit(dst));
331 	nf_reject_ip6_tcphdr_put(nskb, oldskb, otcph, otcplen);
332 
333 	nf_ct_attach(nskb, oldskb);
334 	nf_ct_set_closing(skb_nfct(oldskb));
335 
336 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
337 	/* If we use ip6_local_out for bridged traffic, the MAC source on
338 	 * the RST will be ours, instead of the destination's.  This confuses
339 	 * some routers/firewalls, and they drop the packet.  So we need to
340 	 * build the eth header using the original destination's MAC as the
341 	 * source, and send the RST packet directly.
342 	 */
343 	if (nf_bridge_info_exists(oldskb)) {
344 		struct ethhdr *oeth = eth_hdr(oldskb);
345 		struct ipv6hdr *ip6h = ipv6_hdr(nskb);
346 		struct net_device *br_indev;
347 
348 		br_indev = nf_bridge_get_physindev(oldskb, net);
349 		if (!br_indev) {
350 			kfree_skb(nskb);
351 			return;
352 		}
353 
354 		nskb->dev = br_indev;
355 		nskb->protocol = htons(ETH_P_IPV6);
356 		ip6h->payload_len = htons(sizeof(struct tcphdr));
357 		if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
358 				    oeth->h_source, oeth->h_dest, nskb->len) < 0) {
359 			kfree_skb(nskb);
360 			return;
361 		}
362 		dev_queue_xmit(nskb);
363 	} else
364 #endif
365 		ip6_local_out(net, sk, nskb);
366 }
367 EXPORT_SYMBOL_GPL(nf_send_reset6);
368 
reject6_csum_ok(struct sk_buff * skb,int hook)369 static bool reject6_csum_ok(struct sk_buff *skb, int hook)
370 {
371 	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
372 	int thoff;
373 	__be16 fo;
374 	u8 proto;
375 
376 	if (skb_csum_unnecessary(skb))
377 		return true;
378 
379 	proto = ip6h->nexthdr;
380 	thoff = ipv6_skip_exthdr(skb, ((u8 *)(ip6h + 1) - skb->data), &proto, &fo);
381 
382 	if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
383 		return false;
384 
385 	if (!nf_reject_verify_csum(skb, thoff, proto))
386 		return true;
387 
388 	return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
389 }
390 
nf_send_unreach6(struct net * net,struct sk_buff * skb_in,unsigned char code,unsigned int hooknum)391 void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
392 		      unsigned char code, unsigned int hooknum)
393 {
394 	if (!reject6_csum_ok(skb_in, hooknum))
395 		return;
396 
397 	if (hooknum == NF_INET_LOCAL_OUT && skb_in->dev == NULL)
398 		skb_in->dev = net->loopback_dev;
399 
400 	if ((hooknum == NF_INET_PRE_ROUTING || hooknum == NF_INET_INGRESS) &&
401 	    nf_reject6_fill_skb_dst(skb_in) < 0)
402 		return;
403 
404 	icmpv6_send(skb_in, ICMPV6_DEST_UNREACH, code, 0);
405 }
406 EXPORT_SYMBOL_GPL(nf_send_unreach6);
407 
408 MODULE_LICENSE("GPL");
409 MODULE_DESCRIPTION("IPv6 packet rejection core");
410