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 <net/ip6_route.h>
10 #include <net/ip6_fib.h>
11 #include <net/ip6_checksum.h>
12 #include <linux/netfilter_ipv6.h>
14 void nf_send_reset6(struct net
*net
, struct sk_buff
*oldskb
, int hook
)
17 struct tcphdr otcph
, *tcph
;
18 unsigned int otcplen
, hh_len
;
19 int tcphoff
, needs_ack
;
20 const struct ipv6hdr
*oip6h
= ipv6_hdr(oldskb
);
22 #define DEFAULT_TOS_VALUE 0x0U
23 const __u8 tclass
= DEFAULT_TOS_VALUE
;
24 struct dst_entry
*dst
= NULL
;
29 if ((!(ipv6_addr_type(&oip6h
->saddr
) & IPV6_ADDR_UNICAST
)) ||
30 (!(ipv6_addr_type(&oip6h
->daddr
) & IPV6_ADDR_UNICAST
))) {
31 pr_debug("addr is not unicast.\n");
35 proto
= oip6h
->nexthdr
;
36 tcphoff
= ipv6_skip_exthdr(oldskb
, ((u8
*)(oip6h
+1) - oldskb
->data
), &proto
, &frag_off
);
38 if ((tcphoff
< 0) || (tcphoff
> oldskb
->len
)) {
39 pr_debug("Cannot get TCP header.\n");
43 otcplen
= oldskb
->len
- tcphoff
;
45 /* IP header checks: fragment, too short. */
46 if (proto
!= IPPROTO_TCP
|| otcplen
< sizeof(struct tcphdr
)) {
47 pr_debug("proto(%d) != IPPROTO_TCP, "
48 "or too short. otcplen = %d\n",
53 if (skb_copy_bits(oldskb
, tcphoff
, &otcph
, sizeof(struct tcphdr
)))
58 pr_debug("RST is set\n");
63 if (nf_ip6_checksum(oldskb
, hook
, tcphoff
, IPPROTO_TCP
)) {
64 pr_debug("TCP checksum is invalid\n");
68 memset(&fl6
, 0, sizeof(fl6
));
69 fl6
.flowi6_proto
= IPPROTO_TCP
;
70 fl6
.saddr
= oip6h
->daddr
;
71 fl6
.daddr
= oip6h
->saddr
;
72 fl6
.fl6_sport
= otcph
.dest
;
73 fl6
.fl6_dport
= otcph
.source
;
74 security_skb_classify_flow(oldskb
, flowi6_to_flowi(&fl6
));
75 dst
= ip6_route_output(net
, NULL
, &fl6
);
76 if (dst
== NULL
|| dst
->error
) {
80 dst
= xfrm_lookup(net
, dst
, flowi6_to_flowi(&fl6
), NULL
, 0);
84 hh_len
= (dst
->dev
->hard_header_len
+ 15)&~15;
85 nskb
= alloc_skb(hh_len
+ 15 + dst
->header_len
+ sizeof(struct ipv6hdr
)
86 + sizeof(struct tcphdr
) + dst
->trailer_len
,
90 net_dbg_ratelimited("cannot alloc skb\n");
95 skb_dst_set(nskb
, dst
);
97 skb_reserve(nskb
, hh_len
+ dst
->header_len
);
99 skb_put(nskb
, sizeof(struct ipv6hdr
));
100 skb_reset_network_header(nskb
);
101 ip6h
= ipv6_hdr(nskb
);
102 ip6_flow_hdr(ip6h
, tclass
, 0);
103 ip6h
->hop_limit
= ip6_dst_hoplimit(dst
);
104 ip6h
->nexthdr
= IPPROTO_TCP
;
105 ip6h
->saddr
= oip6h
->daddr
;
106 ip6h
->daddr
= oip6h
->saddr
;
108 skb_reset_transport_header(nskb
);
109 tcph
= (struct tcphdr
*)skb_put(nskb
, sizeof(struct tcphdr
));
110 /* Truncate to length (no data) */
111 tcph
->doff
= sizeof(struct tcphdr
)/4;
112 tcph
->source
= otcph
.dest
;
113 tcph
->dest
= otcph
.source
;
117 tcph
->seq
= otcph
.ack_seq
;
121 tcph
->ack_seq
= htonl(ntohl(otcph
.seq
) + otcph
.syn
+ otcph
.fin
122 + otcplen
- (otcph
.doff
<<2));
127 ((u_int8_t
*)tcph
)[13] = 0;
129 tcph
->ack
= needs_ack
;
134 /* Adjust TCP checksum */
135 tcph
->check
= csum_ipv6_magic(&ipv6_hdr(nskb
)->saddr
,
136 &ipv6_hdr(nskb
)->daddr
,
137 sizeof(struct tcphdr
), IPPROTO_TCP
,
139 sizeof(struct tcphdr
), 0));
141 nf_ct_attach(nskb
, oldskb
);
143 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
144 /* If we use ip6_local_out for bridged traffic, the MAC source on
145 * the RST will be ours, instead of the destination's. This confuses
146 * some routers/firewalls, and they drop the packet. So we need to
147 * build the eth header using the original destination's MAC as the
148 * source, and send the RST packet directly.
150 if (oldskb
->nf_bridge
) {
151 struct ethhdr
*oeth
= eth_hdr(oldskb
);
152 nskb
->dev
= oldskb
->nf_bridge
->physindev
;
153 nskb
->protocol
= htons(ETH_P_IPV6
);
154 ip6h
->payload_len
= htons(sizeof(struct tcphdr
));
155 if (dev_hard_header(nskb
, nskb
->dev
, ntohs(nskb
->protocol
),
156 oeth
->h_source
, oeth
->h_dest
, nskb
->len
) < 0)
158 dev_queue_xmit(nskb
);
163 EXPORT_SYMBOL_GPL(nf_send_reset6
);
This page took 0.036387 seconds and 5 git commands to generate.