Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf
[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 #include <net/netfilter/ipv6/nf_reject.h>
16
17 const struct tcphdr *nf_reject_ip6_tcphdr_get(struct sk_buff *oldskb,
18                                               struct tcphdr *otcph,
19                                               unsigned int *otcplen, int hook)
20 {
21         const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
22         u8 proto;
23         __be16 frag_off;
24         int tcphoff;
25
26         proto = oip6h->nexthdr;
27         tcphoff = ipv6_skip_exthdr(oldskb, ((u8*)(oip6h+1) - oldskb->data),
28                                    &proto, &frag_off);
29
30         if ((tcphoff < 0) || (tcphoff > oldskb->len)) {
31                 pr_debug("Cannot get TCP header.\n");
32                 return NULL;
33         }
34
35         *otcplen = oldskb->len - tcphoff;
36
37         /* IP header checks: fragment, too short. */
38         if (proto != IPPROTO_TCP || *otcplen < sizeof(struct tcphdr)) {
39                 pr_debug("proto(%d) != IPPROTO_TCP or too short (len = %d)\n",
40                          proto, *otcplen);
41                 return NULL;
42         }
43
44         otcph = skb_header_pointer(oldskb, tcphoff, sizeof(struct tcphdr),
45                                    otcph);
46         if (otcph == NULL)
47                 return NULL;
48
49         /* No RST for RST. */
50         if (otcph->rst) {
51                 pr_debug("RST is set\n");
52                 return NULL;
53         }
54
55         /* Check checksum. */
56         if (nf_ip6_checksum(oldskb, hook, tcphoff, IPPROTO_TCP)) {
57                 pr_debug("TCP checksum is invalid\n");
58                 return NULL;
59         }
60
61         return otcph;
62 }
63 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_get);
64
65 struct ipv6hdr *nf_reject_ip6hdr_put(struct sk_buff *nskb,
66                                      const struct sk_buff *oldskb,
67                                      __be16 protocol, int hoplimit)
68 {
69         struct ipv6hdr *ip6h;
70         const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
71 #define DEFAULT_TOS_VALUE       0x0U
72         const __u8 tclass = DEFAULT_TOS_VALUE;
73
74         skb_put(nskb, sizeof(struct ipv6hdr));
75         skb_reset_network_header(nskb);
76         ip6h = ipv6_hdr(nskb);
77         ip6_flow_hdr(ip6h, tclass, 0);
78         ip6h->hop_limit = hoplimit;
79         ip6h->nexthdr = protocol;
80         ip6h->saddr = oip6h->daddr;
81         ip6h->daddr = oip6h->saddr;
82
83         nskb->protocol = htons(ETH_P_IPV6);
84
85         return ip6h;
86 }
87 EXPORT_SYMBOL_GPL(nf_reject_ip6hdr_put);
88
89 void nf_reject_ip6_tcphdr_put(struct sk_buff *nskb,
90                               const struct sk_buff *oldskb,
91                               const struct tcphdr *oth, unsigned int otcplen)
92 {
93         struct tcphdr *tcph;
94         int needs_ack;
95
96         skb_reset_transport_header(nskb);
97         tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
98         /* Truncate to length (no data) */
99         tcph->doff = sizeof(struct tcphdr)/4;
100         tcph->source = oth->dest;
101         tcph->dest = oth->source;
102
103         if (oth->ack) {
104                 needs_ack = 0;
105                 tcph->seq = oth->ack_seq;
106                 tcph->ack_seq = 0;
107         } else {
108                 needs_ack = 1;
109                 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
110                                       otcplen - (oth->doff<<2));
111                 tcph->seq = 0;
112         }
113
114         /* Reset flags */
115         ((u_int8_t *)tcph)[13] = 0;
116         tcph->rst = 1;
117         tcph->ack = needs_ack;
118         tcph->window = 0;
119         tcph->urg_ptr = 0;
120         tcph->check = 0;
121
122         /* Adjust TCP checksum */
123         tcph->check = csum_ipv6_magic(&ipv6_hdr(nskb)->saddr,
124                                       &ipv6_hdr(nskb)->daddr,
125                                       sizeof(struct tcphdr), IPPROTO_TCP,
126                                       csum_partial(tcph,
127                                                    sizeof(struct tcphdr), 0));
128 }
129 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_put);
130
131 void nf_send_reset6(struct net *net, struct sk_buff *oldskb, int hook)
132 {
133         struct sk_buff *nskb;
134         struct tcphdr _otcph;
135         const struct tcphdr *otcph;
136         unsigned int otcplen, hh_len;
137         const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
138         struct ipv6hdr *ip6h;
139         struct dst_entry *dst = NULL;
140         struct flowi6 fl6;
141
142         if ((!(ipv6_addr_type(&oip6h->saddr) & IPV6_ADDR_UNICAST)) ||
143             (!(ipv6_addr_type(&oip6h->daddr) & IPV6_ADDR_UNICAST))) {
144                 pr_debug("addr is not unicast.\n");
145                 return;
146         }
147
148         otcph = nf_reject_ip6_tcphdr_get(oldskb, &_otcph, &otcplen, hook);
149         if (!otcph)
150                 return;
151
152         memset(&fl6, 0, sizeof(fl6));
153         fl6.flowi6_proto = IPPROTO_TCP;
154         fl6.saddr = oip6h->daddr;
155         fl6.daddr = oip6h->saddr;
156         fl6.fl6_sport = otcph->dest;
157         fl6.fl6_dport = otcph->source;
158         security_skb_classify_flow(oldskb, flowi6_to_flowi(&fl6));
159         dst = ip6_route_output(net, NULL, &fl6);
160         if (dst == NULL || dst->error) {
161                 dst_release(dst);
162                 return;
163         }
164         dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
165         if (IS_ERR(dst))
166                 return;
167
168         hh_len = (dst->dev->hard_header_len + 15)&~15;
169         nskb = alloc_skb(hh_len + 15 + dst->header_len + sizeof(struct ipv6hdr)
170                          + sizeof(struct tcphdr) + dst->trailer_len,
171                          GFP_ATOMIC);
172
173         if (!nskb) {
174                 net_dbg_ratelimited("cannot alloc skb\n");
175                 dst_release(dst);
176                 return;
177         }
178
179         skb_dst_set(nskb, dst);
180
181         skb_reserve(nskb, hh_len + dst->header_len);
182         ip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
183                                     ip6_dst_hoplimit(dst));
184         nf_reject_ip6_tcphdr_put(nskb, oldskb, otcph, otcplen);
185
186         nf_ct_attach(nskb, oldskb);
187
188 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
189         /* If we use ip6_local_out for bridged traffic, the MAC source on
190          * the RST will be ours, instead of the destination's.  This confuses
191          * some routers/firewalls, and they drop the packet.  So we need to
192          * build the eth header using the original destination's MAC as the
193          * source, and send the RST packet directly.
194          */
195         if (oldskb->nf_bridge) {
196                 struct ethhdr *oeth = eth_hdr(oldskb);
197                 nskb->dev = oldskb->nf_bridge->physindev;
198                 nskb->protocol = htons(ETH_P_IPV6);
199                 ip6h->payload_len = htons(sizeof(struct tcphdr));
200                 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
201                                     oeth->h_source, oeth->h_dest, nskb->len) < 0)
202                         return;
203                 dev_queue_xmit(nskb);
204         } else
205 #endif
206                 ip6_local_out(nskb);
207 }
208 EXPORT_SYMBOL_GPL(nf_send_reset6);
209
210 MODULE_LICENSE("GPL");