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>
6 #include <linux/module.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>
15 static bool nf_reject_v6_csum_ok(struct sk_buff *skb, int hook)
17 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
20 u8 proto = ip6h->nexthdr;
22 if (skb_csum_unnecessary(skb))
25 if (ip6h->payload_len &&
26 pskb_trim_rcsum(skb, ntohs(ip6h->payload_len) + sizeof(*ip6h)))
30 thoff = ipv6_skip_exthdr(skb, ((u8*)(ip6h+1) - skb->data), &proto, &fo);
31 if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
34 if (!nf_reject_verify_csum(proto))
37 return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
40 static int nf_reject_ip6hdr_validate(struct sk_buff *skb)
45 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
49 if (hdr->version != 6)
52 pkt_len = ntohs(hdr->payload_len);
53 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
59 struct sk_buff *nf_reject_skb_v6_tcp_reset(struct net *net,
60 struct sk_buff *oldskb,
61 const struct net_device *dev,
65 const struct tcphdr *oth;
68 struct ipv6hdr *nip6h;
70 if (!nf_reject_ip6hdr_validate(oldskb))
73 oth = nf_reject_ip6_tcphdr_get(oldskb, &_oth, &otcplen, hook);
77 nskb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(struct tcphdr) +
78 LL_MAX_HEADER, GFP_ATOMIC);
82 nskb->dev = (struct net_device *)dev;
84 skb_reserve(nskb, LL_MAX_HEADER);
85 nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
86 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));
92 EXPORT_SYMBOL_GPL(nf_reject_skb_v6_tcp_reset);
94 struct sk_buff *nf_reject_skb_v6_unreach(struct net *net,
95 struct sk_buff *oldskb,
96 const struct net_device *dev,
100 struct ipv6hdr *nip6h;
101 struct icmp6hdr *icmp6h;
104 if (!nf_reject_ip6hdr_validate(oldskb))
107 /* Include "As much of invoking packet as possible without the ICMPv6
108 * packet exceeding the minimum IPv6 MTU" in the ICMP payload.
110 len = min_t(unsigned int, 1220, oldskb->len);
112 if (!pskb_may_pull(oldskb, len))
115 if (!nf_reject_v6_csum_ok(oldskb, hook))
118 nskb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr) +
119 LL_MAX_HEADER + len, GFP_ATOMIC);
123 nskb->dev = (struct net_device *)dev;
125 skb_reserve(nskb, LL_MAX_HEADER);
126 nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_ICMPV6,
127 net->ipv6.devconf_all->hop_limit);
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;
134 skb_put_data(nskb, skb_network_header(oldskb), len);
135 nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
137 icmp6h->icmp6_cksum =
138 csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr,
139 nskb->len - sizeof(struct ipv6hdr),
142 nskb->len - sizeof(struct ipv6hdr),
147 EXPORT_SYMBOL_GPL(nf_reject_skb_v6_unreach);
149 const struct tcphdr *nf_reject_ip6_tcphdr_get(struct sk_buff *oldskb,
150 struct tcphdr *otcph,
151 unsigned int *otcplen, int hook)
153 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
158 proto = oip6h->nexthdr;
159 tcphoff = ipv6_skip_exthdr(oldskb, ((u8 *)(oip6h + 1) - oldskb->data),
162 if ((tcphoff < 0) || (tcphoff > oldskb->len)) {
163 pr_debug("Cannot get TCP header.\n");
167 *otcplen = oldskb->len - tcphoff;
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",
176 otcph = skb_header_pointer(oldskb, tcphoff, sizeof(struct tcphdr),
181 /* No RST for RST. */
183 pr_debug("RST is set\n");
187 /* Check checksum. */
188 if (nf_ip6_checksum(oldskb, hook, tcphoff, IPPROTO_TCP)) {
189 pr_debug("TCP checksum is invalid\n");
195 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_get);
197 struct ipv6hdr *nf_reject_ip6hdr_put(struct sk_buff *nskb,
198 const struct sk_buff *oldskb,
199 __u8 protocol, int hoplimit)
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;
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;
215 nskb->protocol = htons(ETH_P_IPV6);
219 EXPORT_SYMBOL_GPL(nf_reject_ip6hdr_put);
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)
228 skb_reset_transport_header(nskb);
229 tcph = skb_put(nskb, sizeof(struct tcphdr));
230 /* Truncate to length (no data) */
231 tcph->doff = sizeof(struct tcphdr)/4;
232 tcph->source = oth->dest;
233 tcph->dest = oth->source;
237 tcph->seq = oth->ack_seq;
241 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
242 otcplen - (oth->doff<<2));
247 ((u_int8_t *)tcph)[13] = 0;
249 tcph->ack = needs_ack;
254 /* Adjust TCP checksum */
255 tcph->check = csum_ipv6_magic(&ipv6_hdr(nskb)->saddr,
256 &ipv6_hdr(nskb)->daddr,
257 sizeof(struct tcphdr), IPPROTO_TCP,
259 sizeof(struct tcphdr), 0));
261 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_put);
263 static int nf_reject6_fill_skb_dst(struct sk_buff *skb_in)
265 struct dst_entry *dst = NULL;
268 memset(&fl, 0, sizeof(struct flowi));
269 fl.u.ip6.daddr = ipv6_hdr(skb_in)->saddr;
270 nf_ip6_route(dev_net(skb_in->dev), &dst, &fl, false);
274 skb_dst_set(skb_in, dst);
278 void nf_send_reset6(struct net *net, struct sock *sk, struct sk_buff *oldskb,
281 struct net_device *br_indev __maybe_unused;
282 struct sk_buff *nskb;
283 struct tcphdr _otcph;
284 const struct tcphdr *otcph;
285 unsigned int otcplen, hh_len;
286 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
287 struct ipv6hdr *ip6h;
288 struct dst_entry *dst = NULL;
291 if ((!(ipv6_addr_type(&oip6h->saddr) & IPV6_ADDR_UNICAST)) ||
292 (!(ipv6_addr_type(&oip6h->daddr) & IPV6_ADDR_UNICAST))) {
293 pr_debug("addr is not unicast.\n");
297 otcph = nf_reject_ip6_tcphdr_get(oldskb, &_otcph, &otcplen, hook);
301 memset(&fl6, 0, sizeof(fl6));
302 fl6.flowi6_proto = IPPROTO_TCP;
303 fl6.saddr = oip6h->daddr;
304 fl6.daddr = oip6h->saddr;
305 fl6.fl6_sport = otcph->dest;
306 fl6.fl6_dport = otcph->source;
308 if (hook == NF_INET_PRE_ROUTING || hook == NF_INET_INGRESS) {
309 nf_ip6_route(net, &dst, flowi6_to_flowi(&fl6), false);
312 skb_dst_set(oldskb, dst);
315 fl6.flowi6_oif = l3mdev_master_ifindex(skb_dst(oldskb)->dev);
316 fl6.flowi6_mark = IP6_REPLY_MARK(net, oldskb->mark);
317 security_skb_classify_flow(oldskb, flowi6_to_flowi(&fl6));
318 dst = ip6_route_output(net, NULL, &fl6);
323 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
327 hh_len = (dst->dev->hard_header_len + 15)&~15;
328 nskb = alloc_skb(hh_len + 15 + dst->header_len + sizeof(struct ipv6hdr)
329 + sizeof(struct tcphdr) + dst->trailer_len,
333 net_dbg_ratelimited("cannot alloc skb\n");
338 skb_dst_set(nskb, dst);
340 nskb->mark = fl6.flowi6_mark;
342 skb_reserve(nskb, hh_len + dst->header_len);
343 ip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
344 ip6_dst_hoplimit(dst));
345 nf_reject_ip6_tcphdr_put(nskb, oldskb, otcph, otcplen);
347 nf_ct_attach(nskb, oldskb);
349 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
350 /* If we use ip6_local_out for bridged traffic, the MAC source on
351 * the RST will be ours, instead of the destination's. This confuses
352 * some routers/firewalls, and they drop the packet. So we need to
353 * build the eth header using the original destination's MAC as the
354 * source, and send the RST packet directly.
356 br_indev = nf_bridge_get_physindev(oldskb);
358 struct ethhdr *oeth = eth_hdr(oldskb);
360 nskb->dev = br_indev;
361 nskb->protocol = htons(ETH_P_IPV6);
362 ip6h->payload_len = htons(sizeof(struct tcphdr));
363 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
364 oeth->h_source, oeth->h_dest, nskb->len) < 0) {
368 dev_queue_xmit(nskb);
371 ip6_local_out(net, sk, nskb);
373 EXPORT_SYMBOL_GPL(nf_send_reset6);
375 static bool reject6_csum_ok(struct sk_buff *skb, int hook)
377 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
382 if (skb_csum_unnecessary(skb))
385 proto = ip6h->nexthdr;
386 thoff = ipv6_skip_exthdr(skb, ((u8 *)(ip6h + 1) - skb->data), &proto, &fo);
388 if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
391 if (!nf_reject_verify_csum(proto))
394 return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
397 void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
398 unsigned char code, unsigned int hooknum)
400 if (!reject6_csum_ok(skb_in, hooknum))
403 if (hooknum == NF_INET_LOCAL_OUT && skb_in->dev == NULL)
404 skb_in->dev = net->loopback_dev;
406 if ((hooknum == NF_INET_PRE_ROUTING || hooknum == NF_INET_INGRESS) &&
407 nf_reject6_fill_skb_dst(skb_in) < 0)
410 icmpv6_send(skb_in, ICMPV6_DEST_UNREACH, code, 0);
412 EXPORT_SYMBOL_GPL(nf_send_unreach6);
414 MODULE_LICENSE("GPL");