1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/module.h>
11 #include <net/ip6_route.h>
12 #include <net/ip6_fib.h>
13 #include <net/ip6_checksum.h>
14 #include <net/netfilter/ipv6/nf_reject.h>
15 #include <linux/netfilter_ipv6.h>
16 #include <linux/netfilter_bridge.h>
18 const struct tcphdr *nf_reject_ip6_tcphdr_get(struct sk_buff *oldskb,
20 unsigned int *otcplen, int hook)
22 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
27 proto = oip6h->nexthdr;
28 tcphoff = ipv6_skip_exthdr(oldskb, ((u8 *)(oip6h + 1) - oldskb->data),
31 if ((tcphoff < 0) || (tcphoff > oldskb->len)) {
32 pr_debug("Cannot get TCP header.\n");
36 *otcplen = oldskb->len - tcphoff;
38 /* IP header checks: fragment, too short. */
39 if (proto != IPPROTO_TCP || *otcplen < sizeof(struct tcphdr)) {
40 pr_debug("proto(%d) != IPPROTO_TCP or too short (len = %d)\n",
45 otcph = skb_header_pointer(oldskb, tcphoff, sizeof(struct tcphdr),
52 pr_debug("RST is set\n");
57 if (nf_ip6_checksum(oldskb, hook, tcphoff, IPPROTO_TCP)) {
58 pr_debug("TCP checksum is invalid\n");
64 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_get);
66 struct ipv6hdr *nf_reject_ip6hdr_put(struct sk_buff *nskb,
67 const struct sk_buff *oldskb,
68 __u8 protocol, int hoplimit)
71 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
72 #define DEFAULT_TOS_VALUE 0x0U
73 const __u8 tclass = DEFAULT_TOS_VALUE;
75 skb_put(nskb, sizeof(struct ipv6hdr));
76 skb_reset_network_header(nskb);
77 ip6h = ipv6_hdr(nskb);
78 ip6_flow_hdr(ip6h, tclass, 0);
79 ip6h->hop_limit = hoplimit;
80 ip6h->nexthdr = protocol;
81 ip6h->saddr = oip6h->daddr;
82 ip6h->daddr = oip6h->saddr;
84 nskb->protocol = htons(ETH_P_IPV6);
88 EXPORT_SYMBOL_GPL(nf_reject_ip6hdr_put);
90 void nf_reject_ip6_tcphdr_put(struct sk_buff *nskb,
91 const struct sk_buff *oldskb,
92 const struct tcphdr *oth, unsigned int otcplen)
97 skb_reset_transport_header(nskb);
98 tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
99 /* Truncate to length (no data) */
100 tcph->doff = sizeof(struct tcphdr)/4;
101 tcph->source = oth->dest;
102 tcph->dest = oth->source;
106 tcph->seq = oth->ack_seq;
110 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
111 otcplen - (oth->doff<<2));
116 ((u_int8_t *)tcph)[13] = 0;
118 tcph->ack = needs_ack;
123 /* Adjust TCP checksum */
124 tcph->check = csum_ipv6_magic(&ipv6_hdr(nskb)->saddr,
125 &ipv6_hdr(nskb)->daddr,
126 sizeof(struct tcphdr), IPPROTO_TCP,
128 sizeof(struct tcphdr), 0));
130 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_put);
132 void nf_send_reset6(struct net *net, struct sk_buff *oldskb, int hook)
134 struct sk_buff *nskb;
135 struct tcphdr _otcph;
136 const struct tcphdr *otcph;
137 unsigned int otcplen, hh_len;
138 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
139 struct ipv6hdr *ip6h;
140 struct dst_entry *dst = NULL;
143 if ((!(ipv6_addr_type(&oip6h->saddr) & IPV6_ADDR_UNICAST)) ||
144 (!(ipv6_addr_type(&oip6h->daddr) & IPV6_ADDR_UNICAST))) {
145 pr_debug("addr is not unicast.\n");
149 otcph = nf_reject_ip6_tcphdr_get(oldskb, &_otcph, &otcplen, hook);
153 memset(&fl6, 0, sizeof(fl6));
154 fl6.flowi6_proto = IPPROTO_TCP;
155 fl6.saddr = oip6h->daddr;
156 fl6.daddr = oip6h->saddr;
157 fl6.fl6_sport = otcph->dest;
158 fl6.fl6_dport = otcph->source;
159 fl6.flowi6_oif = l3mdev_master_ifindex(skb_dst(oldskb)->dev);
160 fl6.flowi6_mark = IP6_REPLY_MARK(net, oldskb->mark);
161 security_skb_classify_flow(oldskb, flowi6_to_flowi(&fl6));
162 dst = ip6_route_output(net, NULL, &fl6);
167 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
171 hh_len = (dst->dev->hard_header_len + 15)&~15;
172 nskb = alloc_skb(hh_len + 15 + dst->header_len + sizeof(struct ipv6hdr)
173 + sizeof(struct tcphdr) + dst->trailer_len,
177 net_dbg_ratelimited("cannot alloc skb\n");
182 skb_dst_set(nskb, dst);
184 nskb->mark = fl6.flowi6_mark;
186 skb_reserve(nskb, hh_len + dst->header_len);
187 ip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
188 ip6_dst_hoplimit(dst));
189 nf_reject_ip6_tcphdr_put(nskb, oldskb, otcph, otcplen);
191 nf_ct_attach(nskb, oldskb);
193 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
194 /* If we use ip6_local_out for bridged traffic, the MAC source on
195 * the RST will be ours, instead of the destination's. This confuses
196 * some routers/firewalls, and they drop the packet. So we need to
197 * build the eth header using the original destination's MAC as the
198 * source, and send the RST packet directly.
200 if (oldskb->nf_bridge) {
201 struct ethhdr *oeth = eth_hdr(oldskb);
203 nskb->dev = nf_bridge_get_physindev(oldskb);
204 nskb->protocol = htons(ETH_P_IPV6);
205 ip6h->payload_len = htons(sizeof(struct tcphdr));
206 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
207 oeth->h_source, oeth->h_dest, nskb->len) < 0)
209 dev_queue_xmit(nskb);
212 ip6_local_out(net, nskb->sk, nskb);
214 EXPORT_SYMBOL_GPL(nf_send_reset6);
216 static bool reject6_csum_ok(struct sk_buff *skb, int hook)
218 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
226 if (skb_csum_unnecessary(skb))
229 proto = ip6h->nexthdr;
230 thoff = ipv6_skip_exthdr(skb, ((u8 *)(ip6h + 1) - skb->data), &proto, &fo);
232 if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
235 return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
238 void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
239 unsigned char code, unsigned int hooknum)
241 if (!reject6_csum_ok(skb_in, hooknum))
244 if (hooknum == NF_INET_LOCAL_OUT && skb_in->dev == NULL)
245 skb_in->dev = net->loopback_dev;
247 icmpv6_send(skb_in, ICMPV6_DEST_UNREACH, code, 0);
249 EXPORT_SYMBOL_GPL(nf_send_unreach6);
251 MODULE_LICENSE("GPL");