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 sk_buff *oldskb, int hook)
146 struct net_device *br_indev __maybe_unused;
147 struct sk_buff *nskb;
148 struct tcphdr _otcph;
149 const struct tcphdr *otcph;
150 unsigned int otcplen, hh_len;
151 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
152 struct ipv6hdr *ip6h;
153 struct dst_entry *dst = NULL;
156 if ((!(ipv6_addr_type(&oip6h->saddr) & IPV6_ADDR_UNICAST)) ||
157 (!(ipv6_addr_type(&oip6h->daddr) & IPV6_ADDR_UNICAST))) {
158 pr_debug("addr is not unicast.\n");
162 otcph = nf_reject_ip6_tcphdr_get(oldskb, &_otcph, &otcplen, hook);
166 memset(&fl6, 0, sizeof(fl6));
167 fl6.flowi6_proto = IPPROTO_TCP;
168 fl6.saddr = oip6h->daddr;
169 fl6.daddr = oip6h->saddr;
170 fl6.fl6_sport = otcph->dest;
171 fl6.fl6_dport = otcph->source;
173 if (hook == NF_INET_PRE_ROUTING) {
174 nf_ip6_route(net, &dst, flowi6_to_flowi(&fl6), false);
177 skb_dst_set(oldskb, dst);
180 fl6.flowi6_oif = l3mdev_master_ifindex(skb_dst(oldskb)->dev);
181 fl6.flowi6_mark = IP6_REPLY_MARK(net, oldskb->mark);
182 security_skb_classify_flow(oldskb, flowi6_to_flowi(&fl6));
183 dst = ip6_route_output(net, NULL, &fl6);
188 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
192 hh_len = (dst->dev->hard_header_len + 15)&~15;
193 nskb = alloc_skb(hh_len + 15 + dst->header_len + sizeof(struct ipv6hdr)
194 + sizeof(struct tcphdr) + dst->trailer_len,
198 net_dbg_ratelimited("cannot alloc skb\n");
203 skb_dst_set(nskb, dst);
205 nskb->mark = fl6.flowi6_mark;
207 skb_reserve(nskb, hh_len + dst->header_len);
208 ip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
209 ip6_dst_hoplimit(dst));
210 nf_reject_ip6_tcphdr_put(nskb, oldskb, otcph, otcplen);
212 nf_ct_attach(nskb, oldskb);
214 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
215 /* If we use ip6_local_out for bridged traffic, the MAC source on
216 * the RST will be ours, instead of the destination's. This confuses
217 * some routers/firewalls, and they drop the packet. So we need to
218 * build the eth header using the original destination's MAC as the
219 * source, and send the RST packet directly.
221 br_indev = nf_bridge_get_physindev(oldskb);
223 struct ethhdr *oeth = eth_hdr(oldskb);
225 nskb->dev = br_indev;
226 nskb->protocol = htons(ETH_P_IPV6);
227 ip6h->payload_len = htons(sizeof(struct tcphdr));
228 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
229 oeth->h_source, oeth->h_dest, nskb->len) < 0) {
233 dev_queue_xmit(nskb);
236 ip6_local_out(net, nskb->sk, nskb);
238 EXPORT_SYMBOL_GPL(nf_send_reset6);
240 static bool reject6_csum_ok(struct sk_buff *skb, int hook)
242 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
247 if (skb_csum_unnecessary(skb))
250 proto = ip6h->nexthdr;
251 thoff = ipv6_skip_exthdr(skb, ((u8 *)(ip6h + 1) - skb->data), &proto, &fo);
253 if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
256 if (!nf_reject_verify_csum(proto))
259 return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
262 void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
263 unsigned char code, unsigned int hooknum)
265 if (!reject6_csum_ok(skb_in, hooknum))
268 if (hooknum == NF_INET_LOCAL_OUT && skb_in->dev == NULL)
269 skb_in->dev = net->loopback_dev;
271 if (hooknum == NF_INET_PRE_ROUTING && nf_reject6_fill_skb_dst(skb_in))
274 icmpv6_send(skb_in, ICMPV6_DEST_UNREACH, code, 0);
276 EXPORT_SYMBOL_GPL(nf_send_unreach6);
278 MODULE_LICENSE("GPL");