drivers/net: macvtap and tun depend on INET
[sfrench/cifs-2.6.git] / net / ipv6 / netfilter / nf_reject_ipv6.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  *
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.
7  */
8
9 #include <linux/module.h>
10 #include <net/ipv6.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>
15
16 void nf_send_reset6(struct net *net, struct sk_buff *oldskb, int hook)
17 {
18         struct sk_buff *nskb;
19         struct tcphdr otcph, *tcph;
20         unsigned int otcplen, hh_len;
21         int tcphoff, needs_ack;
22         const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
23         struct ipv6hdr *ip6h;
24 #define DEFAULT_TOS_VALUE       0x0U
25         const __u8 tclass = DEFAULT_TOS_VALUE;
26         struct dst_entry *dst = NULL;
27         u8 proto;
28         __be16 frag_off;
29         struct flowi6 fl6;
30
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");
34                 return;
35         }
36
37         proto = oip6h->nexthdr;
38         tcphoff = ipv6_skip_exthdr(oldskb, ((u8*)(oip6h+1) - oldskb->data), &proto, &frag_off);
39
40         if ((tcphoff < 0) || (tcphoff > oldskb->len)) {
41                 pr_debug("Cannot get TCP header.\n");
42                 return;
43         }
44
45         otcplen = oldskb->len - tcphoff;
46
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",
51                          proto, otcplen);
52                 return;
53         }
54
55         if (skb_copy_bits(oldskb, tcphoff, &otcph, sizeof(struct tcphdr)))
56                 BUG();
57
58         /* No RST for RST. */
59         if (otcph.rst) {
60                 pr_debug("RST is set\n");
61                 return;
62         }
63
64         /* Check checksum. */
65         if (nf_ip6_checksum(oldskb, hook, tcphoff, IPPROTO_TCP)) {
66                 pr_debug("TCP checksum is invalid\n");
67                 return;
68         }
69
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) {
79                 dst_release(dst);
80                 return;
81         }
82         dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
83         if (IS_ERR(dst))
84                 return;
85
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,
89                          GFP_ATOMIC);
90
91         if (!nskb) {
92                 net_dbg_ratelimited("cannot alloc skb\n");
93                 dst_release(dst);
94                 return;
95         }
96
97         skb_dst_set(nskb, dst);
98
99         skb_reserve(nskb, hh_len + dst->header_len);
100
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;
109
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;
116
117         if (otcph.ack) {
118                 needs_ack = 0;
119                 tcph->seq = otcph.ack_seq;
120                 tcph->ack_seq = 0;
121         } else {
122                 needs_ack = 1;
123                 tcph->ack_seq = htonl(ntohl(otcph.seq) + otcph.syn + otcph.fin
124                                       + otcplen - (otcph.doff<<2));
125                 tcph->seq = 0;
126         }
127
128         /* Reset flags */
129         ((u_int8_t *)tcph)[13] = 0;
130         tcph->rst = 1;
131         tcph->ack = needs_ack;
132         tcph->window = 0;
133         tcph->urg_ptr = 0;
134         tcph->check = 0;
135
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,
140                                       csum_partial(tcph,
141                                                    sizeof(struct tcphdr), 0));
142
143         nf_ct_attach(nskb, oldskb);
144
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.
151          */
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)
159                         return;
160                 dev_queue_xmit(nskb);
161         } else
162 #endif
163                 ip6_local_out(nskb);
164 }
165 EXPORT_SYMBOL_GPL(nf_send_reset6);
166
167 MODULE_LICENSE("GPL");