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 <linux/netfilter_ipv6.h>
16 void nf_send_reset6(struct net *net, struct sk_buff *oldskb, int hook)
19 struct tcphdr otcph, *tcph;
20 unsigned int otcplen, hh_len;
21 int tcphoff, needs_ack;
22 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
24 #define DEFAULT_TOS_VALUE 0x0U
25 const __u8 tclass = DEFAULT_TOS_VALUE;
26 struct dst_entry *dst = NULL;
31 if ((!(ipv6_addr_type(&oip6h->saddr) & IPV6_ADDR_UNICAST)) ||
32 (!(ipv6_addr_type(&oip6h->daddr) & IPV6_ADDR_UNICAST))) {
33 pr_debug("addr is not unicast.\n");
37 proto = oip6h->nexthdr;
38 tcphoff = ipv6_skip_exthdr(oldskb, ((u8*)(oip6h+1) - oldskb->data), &proto, &frag_off);
40 if ((tcphoff < 0) || (tcphoff > oldskb->len)) {
41 pr_debug("Cannot get TCP header.\n");
45 otcplen = oldskb->len - tcphoff;
47 /* IP header checks: fragment, too short. */
48 if (proto != IPPROTO_TCP || otcplen < sizeof(struct tcphdr)) {
49 pr_debug("proto(%d) != IPPROTO_TCP, "
50 "or too short. otcplen = %d\n",
55 if (skb_copy_bits(oldskb, tcphoff, &otcph, sizeof(struct tcphdr)))
60 pr_debug("RST is set\n");
65 if (nf_ip6_checksum(oldskb, hook, tcphoff, IPPROTO_TCP)) {
66 pr_debug("TCP checksum is invalid\n");
70 memset(&fl6, 0, sizeof(fl6));
71 fl6.flowi6_proto = IPPROTO_TCP;
72 fl6.saddr = oip6h->daddr;
73 fl6.daddr = oip6h->saddr;
74 fl6.fl6_sport = otcph.dest;
75 fl6.fl6_dport = otcph.source;
76 security_skb_classify_flow(oldskb, flowi6_to_flowi(&fl6));
77 dst = ip6_route_output(net, NULL, &fl6);
78 if (dst == NULL || dst->error) {
82 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
86 hh_len = (dst->dev->hard_header_len + 15)&~15;
87 nskb = alloc_skb(hh_len + 15 + dst->header_len + sizeof(struct ipv6hdr)
88 + sizeof(struct tcphdr) + dst->trailer_len,
92 net_dbg_ratelimited("cannot alloc skb\n");
97 skb_dst_set(nskb, dst);
99 skb_reserve(nskb, hh_len + dst->header_len);
101 skb_put(nskb, sizeof(struct ipv6hdr));
102 skb_reset_network_header(nskb);
103 ip6h = ipv6_hdr(nskb);
104 ip6_flow_hdr(ip6h, tclass, 0);
105 ip6h->hop_limit = ip6_dst_hoplimit(dst);
106 ip6h->nexthdr = IPPROTO_TCP;
107 ip6h->saddr = oip6h->daddr;
108 ip6h->daddr = oip6h->saddr;
110 skb_reset_transport_header(nskb);
111 tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
112 /* Truncate to length (no data) */
113 tcph->doff = sizeof(struct tcphdr)/4;
114 tcph->source = otcph.dest;
115 tcph->dest = otcph.source;
119 tcph->seq = otcph.ack_seq;
123 tcph->ack_seq = htonl(ntohl(otcph.seq) + otcph.syn + otcph.fin
124 + otcplen - (otcph.doff<<2));
129 ((u_int8_t *)tcph)[13] = 0;
131 tcph->ack = needs_ack;
136 /* Adjust TCP checksum */
137 tcph->check = csum_ipv6_magic(&ipv6_hdr(nskb)->saddr,
138 &ipv6_hdr(nskb)->daddr,
139 sizeof(struct tcphdr), IPPROTO_TCP,
141 sizeof(struct tcphdr), 0));
143 nf_ct_attach(nskb, oldskb);
145 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
146 /* If we use ip6_local_out for bridged traffic, the MAC source on
147 * the RST will be ours, instead of the destination's. This confuses
148 * some routers/firewalls, and they drop the packet. So we need to
149 * build the eth header using the original destination's MAC as the
150 * source, and send the RST packet directly.
152 if (oldskb->nf_bridge) {
153 struct ethhdr *oeth = eth_hdr(oldskb);
154 nskb->dev = oldskb->nf_bridge->physindev;
155 nskb->protocol = htons(ETH_P_IPV6);
156 ip6h->payload_len = htons(sizeof(struct tcphdr));
157 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
158 oeth->h_source, oeth->h_dest, nskb->len) < 0)
160 dev_queue_xmit(nskb);
165 EXPORT_SYMBOL_GPL(nf_send_reset6);
167 MODULE_LICENSE("GPL");