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>
11 #include <net/netfilter/ipv4/nf_reject.h>
12 #include <linux/netfilter_ipv4.h>
13 #include <linux/netfilter_bridge.h>
15 const struct tcphdr *nf_reject_ip_tcphdr_get(struct sk_buff *oldskb,
16 struct tcphdr *_oth, int hook)
18 const struct tcphdr *oth;
20 /* IP header checks: fragment. */
21 if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
24 if (ip_hdr(oldskb)->protocol != IPPROTO_TCP)
27 oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
28 sizeof(struct tcphdr), _oth);
37 if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
42 EXPORT_SYMBOL_GPL(nf_reject_ip_tcphdr_get);
44 struct iphdr *nf_reject_iphdr_put(struct sk_buff *nskb,
45 const struct sk_buff *oldskb,
46 __u8 protocol, int ttl)
48 struct iphdr *niph, *oiph = ip_hdr(oldskb);
50 skb_reset_network_header(nskb);
51 niph = skb_put(nskb, sizeof(struct iphdr));
53 niph->ihl = sizeof(struct iphdr) / 4;
56 niph->frag_off = htons(IP_DF);
57 niph->protocol = protocol;
59 niph->saddr = oiph->daddr;
60 niph->daddr = oiph->saddr;
63 nskb->protocol = htons(ETH_P_IP);
67 EXPORT_SYMBOL_GPL(nf_reject_iphdr_put);
69 void nf_reject_ip_tcphdr_put(struct sk_buff *nskb, const struct sk_buff *oldskb,
70 const struct tcphdr *oth)
72 struct iphdr *niph = ip_hdr(nskb);
75 skb_reset_transport_header(nskb);
76 tcph = skb_put_zero(nskb, sizeof(struct tcphdr));
77 tcph->source = oth->dest;
78 tcph->dest = oth->source;
79 tcph->doff = sizeof(struct tcphdr) / 4;
82 tcph->seq = oth->ack_seq;
84 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
85 oldskb->len - ip_hdrlen(oldskb) -
91 tcph->check = ~tcp_v4_check(sizeof(struct tcphdr), niph->saddr,
93 nskb->ip_summed = CHECKSUM_PARTIAL;
94 nskb->csum_start = (unsigned char *)tcph - nskb->head;
95 nskb->csum_offset = offsetof(struct tcphdr, check);
97 EXPORT_SYMBOL_GPL(nf_reject_ip_tcphdr_put);
99 static int nf_reject_fill_skb_dst(struct sk_buff *skb_in)
101 struct dst_entry *dst = NULL;
104 memset(&fl, 0, sizeof(struct flowi));
105 fl.u.ip4.daddr = ip_hdr(skb_in)->saddr;
106 nf_ip_route(dev_net(skb_in->dev), &dst, &fl, false);
110 skb_dst_set(skb_in, dst);
115 void nf_send_reset(struct net *net, struct sk_buff *oldskb, int hook)
117 struct net_device *br_indev __maybe_unused;
118 struct sk_buff *nskb;
120 const struct tcphdr *oth;
123 oth = nf_reject_ip_tcphdr_get(oldskb, &_oth, hook);
127 if (hook == NF_INET_PRE_ROUTING && nf_reject_fill_skb_dst(oldskb))
130 if (skb_rtable(oldskb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
133 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
134 LL_MAX_HEADER, GFP_ATOMIC);
138 /* ip_route_me_harder expects skb->dst to be set */
139 skb_dst_set_noref(nskb, skb_dst(oldskb));
141 nskb->mark = IP4_REPLY_MARK(net, oldskb->mark);
143 skb_reserve(nskb, LL_MAX_HEADER);
144 niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_TCP,
145 ip4_dst_hoplimit(skb_dst(nskb)));
146 nf_reject_ip_tcphdr_put(nskb, oldskb, oth);
148 if (ip_route_me_harder(net, nskb->sk, nskb, RTN_UNSPEC))
153 /* "Never happens" */
154 if (nskb->len > dst_mtu(skb_dst(nskb)))
157 nf_ct_attach(nskb, oldskb);
159 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
160 /* If we use ip_local_out for bridged traffic, the MAC source on
161 * the RST will be ours, instead of the destination's. This confuses
162 * some routers/firewalls, and they drop the packet. So we need to
163 * build the eth header using the original destination's MAC as the
164 * source, and send the RST packet directly.
166 br_indev = nf_bridge_get_physindev(oldskb);
168 struct ethhdr *oeth = eth_hdr(oldskb);
170 nskb->dev = br_indev;
171 niph->tot_len = htons(nskb->len);
173 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
174 oeth->h_source, oeth->h_dest, nskb->len) < 0)
176 dev_queue_xmit(nskb);
179 ip_local_out(net, nskb->sk, nskb);
186 EXPORT_SYMBOL_GPL(nf_send_reset);
188 void nf_send_unreach(struct sk_buff *skb_in, int code, int hook)
190 struct iphdr *iph = ip_hdr(skb_in);
191 u8 proto = iph->protocol;
193 if (iph->frag_off & htons(IP_OFFSET))
196 if (hook == NF_INET_PRE_ROUTING && nf_reject_fill_skb_dst(skb_in))
199 if (skb_csum_unnecessary(skb_in) || !nf_reject_verify_csum(proto)) {
200 icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
204 if (nf_ip_checksum(skb_in, hook, ip_hdrlen(skb_in), proto) == 0)
205 icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
207 EXPORT_SYMBOL_GPL(nf_send_unreach);
209 MODULE_LICENSE("GPL");