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>
4 */
5
6 #include <linux/module.h>
7 #include <net/ipv6.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>
14
nf_reject_ip6_tcphdr_get(struct sk_buff * oldskb,struct tcphdr * otcph,unsigned int * otcplen,int hook)15 const struct tcphdr *nf_reject_ip6_tcphdr_get(struct sk_buff *oldskb,
16 struct tcphdr *otcph,
17 unsigned int *otcplen, int hook)
18 {
19 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
20 u8 proto;
21 __be16 frag_off;
22 int tcphoff;
23
24 proto = oip6h->nexthdr;
25 tcphoff = ipv6_skip_exthdr(oldskb, ((u8 *)(oip6h + 1) - oldskb->data),
26 &proto, &frag_off);
27
28 if ((tcphoff < 0) || (tcphoff > oldskb->len)) {
29 pr_debug("Cannot get TCP header.\n");
30 return NULL;
31 }
32
33 *otcplen = oldskb->len - tcphoff;
34
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",
38 proto, *otcplen);
39 return NULL;
40 }
41
42 otcph = skb_header_pointer(oldskb, tcphoff, sizeof(struct tcphdr),
43 otcph);
44 if (otcph == NULL)
45 return NULL;
46
47 /* No RST for RST. */
48 if (otcph->rst) {
49 pr_debug("RST is set\n");
50 return NULL;
51 }
52
53 /* Check checksum. */
54 if (nf_ip6_checksum(oldskb, hook, tcphoff, IPPROTO_TCP)) {
55 pr_debug("TCP checksum is invalid\n");
56 return NULL;
57 }
58
59 return otcph;
60 }
61 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_get);
62
nf_reject_ip6hdr_put(struct sk_buff * nskb,const struct sk_buff * oldskb,__u8 protocol,int hoplimit)63 struct ipv6hdr *nf_reject_ip6hdr_put(struct sk_buff *nskb,
64 const struct sk_buff *oldskb,
65 __u8 protocol, int hoplimit)
66 {
67 struct ipv6hdr *ip6h;
68 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
69 #define DEFAULT_TOS_VALUE 0x0U
70 const __u8 tclass = DEFAULT_TOS_VALUE;
71
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;
80
81 nskb->protocol = htons(ETH_P_IPV6);
82
83 return ip6h;
84 }
85 EXPORT_SYMBOL_GPL(nf_reject_ip6hdr_put);
86
nf_reject_ip6_tcphdr_put(struct sk_buff * nskb,const struct sk_buff * oldskb,const struct tcphdr * oth,unsigned int otcplen)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)
90 {
91 struct tcphdr *tcph;
92 int needs_ack;
93
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;
100
101 if (oth->ack) {
102 needs_ack = 0;
103 tcph->seq = oth->ack_seq;
104 tcph->ack_seq = 0;
105 } else {
106 needs_ack = 1;
107 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
108 otcplen - (oth->doff<<2));
109 tcph->seq = 0;
110 }
111
112 /* Reset flags */
113 ((u_int8_t *)tcph)[13] = 0;
114 tcph->rst = 1;
115 tcph->ack = needs_ack;
116 tcph->window = 0;
117 tcph->urg_ptr = 0;
118 tcph->check = 0;
119
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,
124 csum_partial(tcph,
125 sizeof(struct tcphdr), 0));
126 }
127 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_put);
128
nf_reject6_fill_skb_dst(struct sk_buff * skb_in)129 static int nf_reject6_fill_skb_dst(struct sk_buff *skb_in)
130 {
131 struct dst_entry *dst = NULL;
132 struct flowi fl;
133
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);
137 if (!dst)
138 return -1;
139
140 skb_dst_set(skb_in, dst);
141 return 0;
142 }
143
nf_send_reset6(struct net * net,struct sock * sk,struct sk_buff * oldskb,int hook)144 void nf_send_reset6(struct net *net, struct sock *sk, struct sk_buff *oldskb,
145 int hook)
146 {
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;
155 struct flowi6 fl6;
156
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");
160 return;
161 }
162
163 otcph = nf_reject_ip6_tcphdr_get(oldskb, &_otcph, &otcplen, hook);
164 if (!otcph)
165 return;
166
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;
173
174 if (hook == NF_INET_PRE_ROUTING) {
175 nf_ip6_route(net, &dst, flowi6_to_flowi(&fl6), false);
176 if (!dst)
177 return;
178 skb_dst_set(oldskb, dst);
179 }
180
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);
185 if (dst->error) {
186 dst_release(dst);
187 return;
188 }
189 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
190 if (IS_ERR(dst))
191 return;
192
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,
196 GFP_ATOMIC);
197
198 if (!nskb) {
199 net_dbg_ratelimited("cannot alloc skb\n");
200 dst_release(dst);
201 return;
202 }
203
204 skb_dst_set(nskb, dst);
205
206 nskb->mark = fl6.flowi6_mark;
207
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);
212
213 nf_ct_attach(nskb, oldskb);
214
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.
221 */
222 br_indev = nf_bridge_get_physindev(oldskb);
223 if (br_indev) {
224 struct ethhdr *oeth = eth_hdr(oldskb);
225
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) {
231 kfree_skb(nskb);
232 return;
233 }
234 dev_queue_xmit(nskb);
235 } else
236 #endif
237 ip6_local_out(net, sk, nskb);
238 }
239 EXPORT_SYMBOL_GPL(nf_send_reset6);
240
reject6_csum_ok(struct sk_buff * skb,int hook)241 static bool reject6_csum_ok(struct sk_buff *skb, int hook)
242 {
243 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
244 int thoff;
245 __be16 fo;
246 u8 proto;
247
248 if (skb_csum_unnecessary(skb))
249 return true;
250
251 proto = ip6h->nexthdr;
252 thoff = ipv6_skip_exthdr(skb, ((u8 *)(ip6h + 1) - skb->data), &proto, &fo);
253
254 if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
255 return false;
256
257 if (!nf_reject_verify_csum(proto))
258 return true;
259
260 return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
261 }
262
nf_send_unreach6(struct net * net,struct sk_buff * skb_in,unsigned char code,unsigned int hooknum)263 void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
264 unsigned char code, unsigned int hooknum)
265 {
266 if (!reject6_csum_ok(skb_in, hooknum))
267 return;
268
269 if (hooknum == NF_INET_LOCAL_OUT && skb_in->dev == NULL)
270 skb_in->dev = net->loopback_dev;
271
272 if (hooknum == NF_INET_PRE_ROUTING && nf_reject6_fill_skb_dst(skb_in))
273 return;
274
275 icmpv6_send(skb_in, ICMPV6_DEST_UNREACH, code, 0);
276 }
277 EXPORT_SYMBOL_GPL(nf_send_unreach6);
278
279 MODULE_LICENSE("GPL");
280