1 #ifndef _IPV6_NF_REJECT_H
2 #define _IPV6_NF_REJECT_H
4 #include <net/ipv6.h>
5 #include <net/ip6_route.h>
6 #include <net/ip6_fib.h>
7 #include <net/ip6_checksum.h>
8 #include <linux/netfilter_ipv6.h>
10 static inline void
11 nf_send_unreach6(struct net *net, struct sk_buff *skb_in, unsigned char code,
12 unsigned int hooknum)
13 {
14 if (hooknum == NF_INET_LOCAL_OUT && skb_in->dev == NULL)
15 skb_in->dev = net->loopback_dev;
17 icmpv6_send(skb_in, ICMPV6_DEST_UNREACH, code, 0);
18 #ifdef CONFIG_IP6_NF_TARGET_REJECT_SKERR
19 if (skb_in->sk) {
20 icmpv6_err_convert(ICMPV6_DEST_UNREACH, code,
21 &skb_in->sk->sk_err);
22 skb_in->sk->sk_error_report(skb_in->sk);
23 pr_debug("ip6t_REJECT: sk_err=%d for skb=%p sk=%p\n",
24 skb_in->sk->sk_err, skb_in, skb_in->sk);
25 }
26 #endif
27 }
29 /* Send RST reply */
30 static void nf_send_reset6(struct net *net, struct sk_buff *oldskb, int hook)
31 {
32 struct sk_buff *nskb;
33 struct tcphdr otcph, *tcph;
34 unsigned int otcplen, hh_len;
35 int tcphoff, needs_ack;
36 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
37 struct ipv6hdr *ip6h;
38 #define DEFAULT_TOS_VALUE 0x0U
39 const __u8 tclass = DEFAULT_TOS_VALUE;
40 struct dst_entry *dst = NULL;
41 u8 proto;
42 __be16 frag_off;
43 struct flowi6 fl6;
45 if ((!(ipv6_addr_type(&oip6h->saddr) & IPV6_ADDR_UNICAST)) ||
46 (!(ipv6_addr_type(&oip6h->daddr) & IPV6_ADDR_UNICAST))) {
47 pr_debug("addr is not unicast.\n");
48 return;
49 }
51 proto = oip6h->nexthdr;
52 tcphoff = ipv6_skip_exthdr(oldskb, ((u8*)(oip6h+1) - oldskb->data), &proto, &frag_off);
54 if ((tcphoff < 0) || (tcphoff > oldskb->len)) {
55 pr_debug("Cannot get TCP header.\n");
56 return;
57 }
59 otcplen = oldskb->len - tcphoff;
61 /* IP header checks: fragment, too short. */
62 if (proto != IPPROTO_TCP || otcplen < sizeof(struct tcphdr)) {
63 pr_debug("proto(%d) != IPPROTO_TCP, "
64 "or too short. otcplen = %d\n",
65 proto, otcplen);
66 return;
67 }
69 if (skb_copy_bits(oldskb, tcphoff, &otcph, sizeof(struct tcphdr)))
70 BUG();
72 /* No RST for RST. */
73 if (otcph.rst) {
74 pr_debug("RST is set\n");
75 return;
76 }
78 /* Check checksum. */
79 if (nf_ip6_checksum(oldskb, hook, tcphoff, IPPROTO_TCP)) {
80 pr_debug("TCP checksum is invalid\n");
81 return;
82 }
84 memset(&fl6, 0, sizeof(fl6));
85 fl6.flowi6_proto = IPPROTO_TCP;
86 fl6.saddr = oip6h->daddr;
87 fl6.daddr = oip6h->saddr;
88 fl6.fl6_sport = otcph.dest;
89 fl6.fl6_dport = otcph.source;
90 security_skb_classify_flow(oldskb, flowi6_to_flowi(&fl6));
91 dst = ip6_route_output(net, NULL, &fl6);
92 if (dst == NULL || dst->error) {
93 dst_release(dst);
94 return;
95 }
96 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
97 if (IS_ERR(dst))
98 return;
100 hh_len = (dst->dev->hard_header_len + 15)&~15;
101 nskb = alloc_skb(hh_len + 15 + dst->header_len + sizeof(struct ipv6hdr)
102 + sizeof(struct tcphdr) + dst->trailer_len,
103 GFP_ATOMIC);
105 if (!nskb) {
106 net_dbg_ratelimited("cannot alloc skb\n");
107 dst_release(dst);
108 return;
109 }
111 skb_dst_set(nskb, dst);
113 skb_reserve(nskb, hh_len + dst->header_len);
115 skb_put(nskb, sizeof(struct ipv6hdr));
116 skb_reset_network_header(nskb);
117 ip6h = ipv6_hdr(nskb);
118 ip6_flow_hdr(ip6h, tclass, 0);
119 ip6h->hop_limit = ip6_dst_hoplimit(dst);
120 ip6h->nexthdr = IPPROTO_TCP;
121 ip6h->saddr = oip6h->daddr;
122 ip6h->daddr = oip6h->saddr;
124 skb_reset_transport_header(nskb);
125 tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
126 /* Truncate to length (no data) */
127 tcph->doff = sizeof(struct tcphdr)/4;
128 tcph->source = otcph.dest;
129 tcph->dest = otcph.source;
131 if (otcph.ack) {
132 needs_ack = 0;
133 tcph->seq = otcph.ack_seq;
134 tcph->ack_seq = 0;
135 } else {
136 needs_ack = 1;
137 tcph->ack_seq = htonl(ntohl(otcph.seq) + otcph.syn + otcph.fin
138 + otcplen - (otcph.doff<<2));
139 tcph->seq = 0;
140 }
142 /* Reset flags */
143 ((u_int8_t *)tcph)[13] = 0;
144 tcph->rst = 1;
145 tcph->ack = needs_ack;
146 tcph->window = 0;
147 tcph->urg_ptr = 0;
148 tcph->check = 0;
150 /* Adjust TCP checksum */
151 tcph->check = csum_ipv6_magic(&ipv6_hdr(nskb)->saddr,
152 &ipv6_hdr(nskb)->daddr,
153 sizeof(struct tcphdr), IPPROTO_TCP,
154 csum_partial(tcph,
155 sizeof(struct tcphdr), 0));
157 nf_ct_attach(nskb, oldskb);
159 #ifdef CONFIG_BRIDGE_NETFILTER
160 /* If we use ip6_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.
165 */
166 if (oldskb->nf_bridge) {
167 struct ethhdr *oeth = eth_hdr(oldskb);
168 nskb->dev = oldskb->nf_bridge->physindev;
169 nskb->protocol = htons(ETH_P_IPV6);
170 ip6h->payload_len = htons(sizeof(struct tcphdr));
171 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
172 oeth->h_source, oeth->h_dest, nskb->len) < 0)
173 return;
174 dev_queue_xmit(nskb);
175 } else
176 #endif
177 ip6_local_out(nskb);
178 }
180 #endif /* _IPV6_NF_REJECT_H */