[NET]: Dynamically allocate the loopback device, part 1.
[linux-2.6.git] / net / ipv6 / netfilter / ip6t_REJECT.c
1 /*
2  * IP6 tables REJECT target module
3  * Linux INET6 implementation
4  *
5  * Copyright (C)2003 USAGI/WIDE Project
6  *
7  * Authors:
8  *      Yasuyuki Kozakai        <yasuyuki.kozakai@toshiba.co.jp>
9  *
10  * Based on net/ipv4/netfilter/ipt_REJECT.c
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version
15  * 2 of the License, or (at your option) any later version.
16  */
17
18 #include <linux/module.h>
19 #include <linux/skbuff.h>
20 #include <linux/icmpv6.h>
21 #include <linux/netdevice.h>
22 #include <net/ipv6.h>
23 #include <net/tcp.h>
24 #include <net/icmp.h>
25 #include <net/ip6_checksum.h>
26 #include <net/ip6_fib.h>
27 #include <net/ip6_route.h>
28 #include <net/flow.h>
29 #include <linux/netfilter/x_tables.h>
30 #include <linux/netfilter_ipv6/ip6_tables.h>
31 #include <linux/netfilter_ipv6/ip6t_REJECT.h>
32
33 MODULE_AUTHOR("Yasuyuki KOZAKAI <yasuyuki.kozakai@toshiba.co.jp>");
34 MODULE_DESCRIPTION("IP6 tables REJECT target module");
35 MODULE_LICENSE("GPL");
36
37 /* Send RST reply */
38 static void send_reset(struct sk_buff *oldskb)
39 {
40         struct sk_buff *nskb;
41         struct tcphdr otcph, *tcph;
42         unsigned int otcplen, hh_len;
43         int tcphoff, needs_ack;
44         struct ipv6hdr *oip6h = ipv6_hdr(oldskb), *ip6h;
45         struct dst_entry *dst = NULL;
46         u8 proto;
47         struct flowi fl;
48
49         if ((!(ipv6_addr_type(&oip6h->saddr) & IPV6_ADDR_UNICAST)) ||
50             (!(ipv6_addr_type(&oip6h->daddr) & IPV6_ADDR_UNICAST))) {
51                 pr_debug("ip6t_REJECT: addr is not unicast.\n");
52                 return;
53         }
54
55         proto = oip6h->nexthdr;
56         tcphoff = ipv6_skip_exthdr(oldskb, ((u8*)(oip6h+1) - oldskb->data), &proto);
57
58         if ((tcphoff < 0) || (tcphoff > oldskb->len)) {
59                 pr_debug("ip6t_REJECT: Can't get TCP header.\n");
60                 return;
61         }
62
63         otcplen = oldskb->len - tcphoff;
64
65         /* IP header checks: fragment, too short. */
66         if (proto != IPPROTO_TCP || otcplen < sizeof(struct tcphdr)) {
67                 pr_debug("ip6t_REJECT: proto(%d) != IPPROTO_TCP, "
68                          "or too short. otcplen = %d\n",
69                          proto, otcplen);
70                 return;
71         }
72
73         if (skb_copy_bits(oldskb, tcphoff, &otcph, sizeof(struct tcphdr)))
74                 BUG();
75
76         /* No RST for RST. */
77         if (otcph.rst) {
78                 pr_debug("ip6t_REJECT: RST is set\n");
79                 return;
80         }
81
82         /* Check checksum. */
83         if (csum_ipv6_magic(&oip6h->saddr, &oip6h->daddr, otcplen, IPPROTO_TCP,
84                             skb_checksum(oldskb, tcphoff, otcplen, 0))) {
85                 pr_debug("ip6t_REJECT: TCP checksum is invalid\n");
86                 return;
87         }
88
89         memset(&fl, 0, sizeof(fl));
90         fl.proto = IPPROTO_TCP;
91         ipv6_addr_copy(&fl.fl6_src, &oip6h->daddr);
92         ipv6_addr_copy(&fl.fl6_dst, &oip6h->saddr);
93         fl.fl_ip_sport = otcph.dest;
94         fl.fl_ip_dport = otcph.source;
95         security_skb_classify_flow(oldskb, &fl);
96         dst = ip6_route_output(NULL, &fl);
97         if (dst == NULL)
98                 return;
99         if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0))
100                 return;
101
102         hh_len = (dst->dev->hard_header_len + 15)&~15;
103         nskb = alloc_skb(hh_len + 15 + dst->header_len + sizeof(struct ipv6hdr)
104                          + sizeof(struct tcphdr) + dst->trailer_len,
105                          GFP_ATOMIC);
106
107         if (!nskb) {
108                 if (net_ratelimit())
109                         printk("ip6t_REJECT: Can't alloc skb\n");
110                 dst_release(dst);
111                 return;
112         }
113
114         nskb->dst = dst;
115
116         skb_reserve(nskb, hh_len + dst->header_len);
117
118         skb_put(nskb, sizeof(struct ipv6hdr));
119         skb_reset_network_header(nskb);
120         ip6h = ipv6_hdr(nskb);
121         ip6h->version = 6;
122         ip6h->hop_limit = dst_metric(dst, RTAX_HOPLIMIT);
123         ip6h->nexthdr = IPPROTO_TCP;
124         ip6h->payload_len = htons(sizeof(struct tcphdr));
125         ipv6_addr_copy(&ip6h->saddr, &oip6h->daddr);
126         ipv6_addr_copy(&ip6h->daddr, &oip6h->saddr);
127
128         tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
129         /* Truncate to length (no data) */
130         tcph->doff = sizeof(struct tcphdr)/4;
131         tcph->source = otcph.dest;
132         tcph->dest = otcph.source;
133
134         if (otcph.ack) {
135                 needs_ack = 0;
136                 tcph->seq = otcph.ack_seq;
137                 tcph->ack_seq = 0;
138         } else {
139                 needs_ack = 1;
140                 tcph->ack_seq = htonl(ntohl(otcph.seq) + otcph.syn + otcph.fin
141                                       + otcplen - (otcph.doff<<2));
142                 tcph->seq = 0;
143         }
144
145         /* Reset flags */
146         ((u_int8_t *)tcph)[13] = 0;
147         tcph->rst = 1;
148         tcph->ack = needs_ack;
149         tcph->window = 0;
150         tcph->urg_ptr = 0;
151         tcph->check = 0;
152
153         /* Adjust TCP checksum */
154         tcph->check = csum_ipv6_magic(&ipv6_hdr(nskb)->saddr,
155                                       &ipv6_hdr(nskb)->daddr,
156                                       sizeof(struct tcphdr), IPPROTO_TCP,
157                                       csum_partial(tcph,
158                                                    sizeof(struct tcphdr), 0));
159
160         nf_ct_attach(nskb, oldskb);
161
162         NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, nskb, NULL, nskb->dst->dev,
163                 dst_output);
164 }
165
166 static inline void
167 send_unreach(struct sk_buff *skb_in, unsigned char code, unsigned int hooknum)
168 {
169         if (hooknum == NF_IP6_LOCAL_OUT && skb_in->dev == NULL)
170                 skb_in->dev = loopback_dev;
171
172         icmpv6_send(skb_in, ICMPV6_DEST_UNREACH, code, 0, NULL);
173 }
174
175 static unsigned int reject6_target(struct sk_buff **pskb,
176                            const struct net_device *in,
177                            const struct net_device *out,
178                            unsigned int hooknum,
179                            const struct xt_target *target,
180                            const void *targinfo)
181 {
182         const struct ip6t_reject_info *reject = targinfo;
183
184         pr_debug("%s: medium point\n", __FUNCTION__);
185         /* WARNING: This code causes reentry within ip6tables.
186            This means that the ip6tables jump stack is now crap.  We
187            must return an absolute verdict. --RR */
188         switch (reject->with) {
189         case IP6T_ICMP6_NO_ROUTE:
190                 send_unreach(*pskb, ICMPV6_NOROUTE, hooknum);
191                 break;
192         case IP6T_ICMP6_ADM_PROHIBITED:
193                 send_unreach(*pskb, ICMPV6_ADM_PROHIBITED, hooknum);
194                 break;
195         case IP6T_ICMP6_NOT_NEIGHBOUR:
196                 send_unreach(*pskb, ICMPV6_NOT_NEIGHBOUR, hooknum);
197                 break;
198         case IP6T_ICMP6_ADDR_UNREACH:
199                 send_unreach(*pskb, ICMPV6_ADDR_UNREACH, hooknum);
200                 break;
201         case IP6T_ICMP6_PORT_UNREACH:
202                 send_unreach(*pskb, ICMPV6_PORT_UNREACH, hooknum);
203                 break;
204         case IP6T_ICMP6_ECHOREPLY:
205                 /* Do nothing */
206                 break;
207         case IP6T_TCP_RESET:
208                 send_reset(*pskb);
209                 break;
210         default:
211                 if (net_ratelimit())
212                         printk(KERN_WARNING "ip6t_REJECT: case %u not handled yet\n", reject->with);
213                 break;
214         }
215
216         return NF_DROP;
217 }
218
219 static bool check(const char *tablename,
220                   const void *entry,
221                   const struct xt_target *target,
222                   void *targinfo,
223                   unsigned int hook_mask)
224 {
225         const struct ip6t_reject_info *rejinfo = targinfo;
226         const struct ip6t_entry *e = entry;
227
228         if (rejinfo->with == IP6T_ICMP6_ECHOREPLY) {
229                 printk("ip6t_REJECT: ECHOREPLY is not supported.\n");
230                 return false;
231         } else if (rejinfo->with == IP6T_TCP_RESET) {
232                 /* Must specify that it's a TCP packet */
233                 if (e->ipv6.proto != IPPROTO_TCP
234                     || (e->ipv6.invflags & XT_INV_PROTO)) {
235                         printk("ip6t_REJECT: TCP_RESET illegal for non-tcp\n");
236                         return false;
237                 }
238         }
239         return true;
240 }
241
242 static struct xt_target ip6t_reject_reg __read_mostly = {
243         .name           = "REJECT",
244         .family         = AF_INET6,
245         .target         = reject6_target,
246         .targetsize     = sizeof(struct ip6t_reject_info),
247         .table          = "filter",
248         .hooks          = (1 << NF_IP6_LOCAL_IN) | (1 << NF_IP6_FORWARD) |
249                           (1 << NF_IP6_LOCAL_OUT),
250         .checkentry     = check,
251         .me             = THIS_MODULE
252 };
253
254 static int __init ip6t_reject_init(void)
255 {
256         return xt_register_target(&ip6t_reject_reg);
257 }
258
259 static void __exit ip6t_reject_fini(void)
260 {
261         xt_unregister_target(&ip6t_reject_reg);
262 }
263
264 module_init(ip6t_reject_init);
265 module_exit(ip6t_reject_fini);