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 void nf_send_reset6(struct net *net, struct sk_buff *oldskb, int hook)
131 struct net_device *br_indev __maybe_unused;
132 struct sk_buff *nskb;
133 struct tcphdr _otcph;
134 const struct tcphdr *otcph;
135 unsigned int otcplen, hh_len;
136 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
137 struct ipv6hdr *ip6h;
138 struct dst_entry *dst = NULL;
141 if ((!(ipv6_addr_type(&oip6h->saddr) & IPV6_ADDR_UNICAST)) ||
142 (!(ipv6_addr_type(&oip6h->daddr) & IPV6_ADDR_UNICAST))) {
143 pr_debug("addr is not unicast.\n");
147 otcph = nf_reject_ip6_tcphdr_get(oldskb, &_otcph, &otcplen, hook);
151 memset(&fl6, 0, sizeof(fl6));
152 fl6.flowi6_proto = IPPROTO_TCP;
153 fl6.saddr = oip6h->daddr;
154 fl6.daddr = oip6h->saddr;
155 fl6.fl6_sport = otcph->dest;
156 fl6.fl6_dport = otcph->source;
157 fl6.flowi6_oif = l3mdev_master_ifindex(skb_dst(oldskb)->dev);
158 fl6.flowi6_mark = IP6_REPLY_MARK(net, oldskb->mark);
159 security_skb_classify_flow(oldskb, flowi6_to_flowi(&fl6));
160 dst = ip6_route_output(net, NULL, &fl6);
165 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
169 hh_len = (dst->dev->hard_header_len + 15)&~15;
170 nskb = alloc_skb(hh_len + 15 + dst->header_len + sizeof(struct ipv6hdr)
171 + sizeof(struct tcphdr) + dst->trailer_len,
175 net_dbg_ratelimited("cannot alloc skb\n");
180 skb_dst_set(nskb, dst);
182 nskb->mark = fl6.flowi6_mark;
184 skb_reserve(nskb, hh_len + dst->header_len);
185 ip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
186 ip6_dst_hoplimit(dst));
187 nf_reject_ip6_tcphdr_put(nskb, oldskb, otcph, otcplen);
189 nf_ct_attach(nskb, oldskb);
191 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
192 /* If we use ip6_local_out for bridged traffic, the MAC source on
193 * the RST will be ours, instead of the destination's. This confuses
194 * some routers/firewalls, and they drop the packet. So we need to
195 * build the eth header using the original destination's MAC as the
196 * source, and send the RST packet directly.
198 br_indev = nf_bridge_get_physindev(oldskb);
200 struct ethhdr *oeth = eth_hdr(oldskb);
202 nskb->dev = br_indev;
203 nskb->protocol = htons(ETH_P_IPV6);
204 ip6h->payload_len = htons(sizeof(struct tcphdr));
205 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
206 oeth->h_source, oeth->h_dest, nskb->len) < 0) {
210 dev_queue_xmit(nskb);
213 ip6_local_out(net, nskb->sk, nskb);
215 EXPORT_SYMBOL_GPL(nf_send_reset6);
217 static bool reject6_csum_ok(struct sk_buff *skb, int hook)
219 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
224 if (skb_csum_unnecessary(skb))
227 proto = ip6h->nexthdr;
228 thoff = ipv6_skip_exthdr(skb, ((u8 *)(ip6h + 1) - skb->data), &proto, &fo);
230 if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
233 if (!nf_reject_verify_csum(proto))
236 return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
239 void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
240 unsigned char code, unsigned int hooknum)
242 if (!reject6_csum_ok(skb_in, hooknum))
245 if (hooknum == NF_INET_LOCAL_OUT && skb_in->dev == NULL)
246 skb_in->dev = net->loopback_dev;
248 icmpv6_send(skb_in, ICMPV6_DEST_UNREACH, code, 0);
250 EXPORT_SYMBOL_GPL(nf_send_unreach6);
252 MODULE_LICENSE("GPL");