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 const struct tcphdr *nf_reject_ip6_tcphdr_get(struct sk_buff *oldskb,
17 unsigned int *otcplen, int hook)
19 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
24 proto = oip6h->nexthdr;
25 tcphoff = ipv6_skip_exthdr(oldskb, ((u8 *)(oip6h + 1) - oldskb->data),
28 if ((tcphoff < 0) || (tcphoff > oldskb->len)) {
29 pr_debug("Cannot get TCP header.\n");
33 *otcplen = oldskb->len - tcphoff;
35 /* IP header checks: fragment, too short. */
36 if (proto != IPPROTO_TCP || *otcplen < sizeof(struct tcphdr)) {
37 pr_debug("proto(%d) != IPPROTO_TCP or too short (len = %d)\n",
42 otcph = skb_header_pointer(oldskb, tcphoff, sizeof(struct tcphdr),
49 pr_debug("RST is set\n");
54 if (nf_ip6_checksum(oldskb, hook, tcphoff, IPPROTO_TCP)) {
55 pr_debug("TCP checksum is invalid\n");
61 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_get);
63 struct ipv6hdr *nf_reject_ip6hdr_put(struct sk_buff *nskb,
64 const struct sk_buff *oldskb,
65 __u8 protocol, int hoplimit)
68 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
69 #define DEFAULT_TOS_VALUE 0x0U
70 const __u8 tclass = DEFAULT_TOS_VALUE;
72 skb_put(nskb, sizeof(struct ipv6hdr));
73 skb_reset_network_header(nskb);
74 ip6h = ipv6_hdr(nskb);
75 ip6_flow_hdr(ip6h, tclass, 0);
76 ip6h->hop_limit = hoplimit;
77 ip6h->nexthdr = protocol;
78 ip6h->saddr = oip6h->daddr;
79 ip6h->daddr = oip6h->saddr;
81 nskb->protocol = htons(ETH_P_IPV6);
85 EXPORT_SYMBOL_GPL(nf_reject_ip6hdr_put);
87 void nf_reject_ip6_tcphdr_put(struct sk_buff *nskb,
88 const struct sk_buff *oldskb,
89 const struct tcphdr *oth, unsigned int otcplen)
94 skb_reset_transport_header(nskb);
95 tcph = skb_put(nskb, sizeof(struct tcphdr));
96 /* Truncate to length (no data) */
97 tcph->doff = sizeof(struct tcphdr)/4;
98 tcph->source = oth->dest;
99 tcph->dest = oth->source;
103 tcph->seq = oth->ack_seq;
107 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
108 otcplen - (oth->doff<<2));
113 ((u_int8_t *)tcph)[13] = 0;
115 tcph->ack = needs_ack;
120 /* Adjust TCP checksum */
121 tcph->check = csum_ipv6_magic(&ipv6_hdr(nskb)->saddr,
122 &ipv6_hdr(nskb)->daddr,
123 sizeof(struct tcphdr), IPPROTO_TCP,
125 sizeof(struct tcphdr), 0));
127 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_put);
129 static int nf_reject6_fill_skb_dst(struct sk_buff *skb_in)
131 struct dst_entry *dst = NULL;
134 memset(&fl, 0, sizeof(struct flowi));
135 fl.u.ip6.daddr = ipv6_hdr(skb_in)->saddr;
136 nf_ip6_route(dev_net(skb_in->dev), &dst, &fl, false);
140 skb_dst_set(skb_in, dst);
144 void nf_send_reset6(struct net *net, struct sock *sk, struct sk_buff *oldskb,
147 struct net_device *br_indev __maybe_unused;
148 struct sk_buff *nskb;
149 struct tcphdr _otcph;
150 const struct tcphdr *otcph;
151 unsigned int otcplen, hh_len;
152 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
153 struct ipv6hdr *ip6h;
154 struct dst_entry *dst = NULL;
157 if ((!(ipv6_addr_type(&oip6h->saddr) & IPV6_ADDR_UNICAST)) ||
158 (!(ipv6_addr_type(&oip6h->daddr) & IPV6_ADDR_UNICAST))) {
159 pr_debug("addr is not unicast.\n");
163 otcph = nf_reject_ip6_tcphdr_get(oldskb, &_otcph, &otcplen, hook);
167 memset(&fl6, 0, sizeof(fl6));
168 fl6.flowi6_proto = IPPROTO_TCP;
169 fl6.saddr = oip6h->daddr;
170 fl6.daddr = oip6h->saddr;
171 fl6.fl6_sport = otcph->dest;
172 fl6.fl6_dport = otcph->source;
174 if (hook == NF_INET_PRE_ROUTING) {
175 nf_ip6_route(net, &dst, flowi6_to_flowi(&fl6), false);
178 skb_dst_set(oldskb, dst);
181 fl6.flowi6_oif = l3mdev_master_ifindex(skb_dst(oldskb)->dev);
182 fl6.flowi6_mark = IP6_REPLY_MARK(net, oldskb->mark);
183 security_skb_classify_flow(oldskb, flowi6_to_flowi_common(&fl6));
184 dst = ip6_route_output(net, NULL, &fl6);
189 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
193 hh_len = (dst->dev->hard_header_len + 15)&~15;
194 nskb = alloc_skb(hh_len + 15 + dst->header_len + sizeof(struct ipv6hdr)
195 + sizeof(struct tcphdr) + dst->trailer_len,
199 net_dbg_ratelimited("cannot alloc skb\n");
204 skb_dst_set(nskb, dst);
206 nskb->mark = fl6.flowi6_mark;
208 skb_reserve(nskb, hh_len + dst->header_len);
209 ip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
210 ip6_dst_hoplimit(dst));
211 nf_reject_ip6_tcphdr_put(nskb, oldskb, otcph, otcplen);
213 nf_ct_attach(nskb, oldskb);
215 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
216 /* If we use ip6_local_out for bridged traffic, the MAC source on
217 * the RST will be ours, instead of the destination's. This confuses
218 * some routers/firewalls, and they drop the packet. So we need to
219 * build the eth header using the original destination's MAC as the
220 * source, and send the RST packet directly.
222 br_indev = nf_bridge_get_physindev(oldskb);
224 struct ethhdr *oeth = eth_hdr(oldskb);
226 nskb->dev = br_indev;
227 nskb->protocol = htons(ETH_P_IPV6);
228 ip6h->payload_len = htons(sizeof(struct tcphdr));
229 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
230 oeth->h_source, oeth->h_dest, nskb->len) < 0) {
234 dev_queue_xmit(nskb);
237 ip6_local_out(net, sk, nskb);
239 EXPORT_SYMBOL_GPL(nf_send_reset6);
241 static bool reject6_csum_ok(struct sk_buff *skb, int hook)
243 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
248 if (skb_csum_unnecessary(skb))
251 proto = ip6h->nexthdr;
252 thoff = ipv6_skip_exthdr(skb, ((u8 *)(ip6h + 1) - skb->data), &proto, &fo);
254 if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
257 if (!nf_reject_verify_csum(proto))
260 return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
263 void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
264 unsigned char code, unsigned int hooknum)
266 if (!reject6_csum_ok(skb_in, hooknum))
269 if (hooknum == NF_INET_LOCAL_OUT && skb_in->dev == NULL)
270 skb_in->dev = net->loopback_dev;
272 if (hooknum == NF_INET_PRE_ROUTING && nf_reject6_fill_skb_dst(skb_in))
275 icmpv6_send(skb_in, ICMPV6_DEST_UNREACH, code, 0);
277 EXPORT_SYMBOL_GPL(nf_send_unreach6);
279 MODULE_LICENSE("GPL");