blob: 13b1a525b92c67ea31f82e35490a347cd35346cf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Packet matching code.
3 *
4 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
Harald Welte6b7d31f2005-10-26 09:34:24 +02005 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * 19 Jan 2002 Harald Welte <laforge@gnumonks.org>
12 * - increase module usage count as soon as we have rules inside
13 * a table
14 * 06 Jun 2002 Andras Kis-Szabo <kisza@sch.bme.hu>
15 * - new extension header parser code
16 */
17#include <linux/config.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020018#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/skbuff.h>
20#include <linux/kmod.h>
21#include <linux/vmalloc.h>
22#include <linux/netdevice.h>
23#include <linux/module.h>
24#include <linux/tcp.h>
25#include <linux/udp.h>
26#include <linux/icmpv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/ipv6.h>
28#include <asm/uaccess.h>
29#include <asm/semaphore.h>
30#include <linux/proc_fs.h>
David S. Millerc8923c62005-10-13 14:41:23 -070031#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/netfilter_ipv6/ip6_tables.h>
34
35MODULE_LICENSE("GPL");
36MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
37MODULE_DESCRIPTION("IPv6 packet filter");
38
39#define IPV6_HDR_LEN (sizeof(struct ipv6hdr))
40#define IPV6_OPTHDR_LEN (sizeof(struct ipv6_opt_hdr))
41
42/*#define DEBUG_IP_FIREWALL*/
43/*#define DEBUG_ALLOW_ALL*/ /* Useful for remote debugging */
44/*#define DEBUG_IP_FIREWALL_USER*/
45
46#ifdef DEBUG_IP_FIREWALL
47#define dprintf(format, args...) printk(format , ## args)
48#else
49#define dprintf(format, args...)
50#endif
51
52#ifdef DEBUG_IP_FIREWALL_USER
53#define duprintf(format, args...) printk(format , ## args)
54#else
55#define duprintf(format, args...)
56#endif
57
58#ifdef CONFIG_NETFILTER_DEBUG
59#define IP_NF_ASSERT(x) \
60do { \
61 if (!(x)) \
62 printk("IP_NF_ASSERT: %s:%s:%u\n", \
63 __FUNCTION__, __FILE__, __LINE__); \
64} while(0)
65#else
66#define IP_NF_ASSERT(x)
67#endif
68#define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
69
70static DECLARE_MUTEX(ip6t_mutex);
71
72/* Must have mutex */
73#define ASSERT_READ_LOCK(x) IP_NF_ASSERT(down_trylock(&ip6t_mutex) != 0)
74#define ASSERT_WRITE_LOCK(x) IP_NF_ASSERT(down_trylock(&ip6t_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/netfilter_ipv4/listhelp.h>
76
77#if 0
78/* All the better to debug you with... */
79#define static
80#define inline
81#endif
82
Harald Welte6b7d31f2005-10-26 09:34:24 +020083/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 We keep a set of rules for each CPU, so we can avoid write-locking
Harald Welte6b7d31f2005-10-26 09:34:24 +020085 them in the softirq when updating the counters and therefore
86 only need to read-lock in the softirq; doing a write_lock_bh() in user
87 context stops packets coming through and allows user context to read
88 the counters or update the rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 Hence the start of any table is given by get_table() below. */
91
92/* The table itself */
93struct ip6t_table_info
94{
95 /* Size per table */
96 unsigned int size;
97 /* Number of entries: FIXME. --RR */
98 unsigned int number;
99 /* Initial number of entries. Needed for module usage count */
100 unsigned int initial_entries;
101
102 /* Entry points and underflows */
103 unsigned int hook_entry[NF_IP6_NUMHOOKS];
104 unsigned int underflow[NF_IP6_NUMHOOKS];
105
106 /* ip6t_entry tables: one per CPU */
Eric Dumazet31836062005-12-13 23:13:48 -0800107 void *entries[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110static LIST_HEAD(ip6t_target);
111static LIST_HEAD(ip6t_match);
112static LIST_HEAD(ip6t_tables);
Eric Dumazet31836062005-12-13 23:13:48 -0800113#define SET_COUNTER(c,b,p) do { (c).bcnt = (b); (c).pcnt = (p); } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define ADD_COUNTER(c,b,p) do { (c).bcnt += (b); (c).pcnt += (p); } while(0)
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#if 0
117#define down(x) do { printk("DOWN:%u:" #x "\n", __LINE__); down(x); } while(0)
118#define down_interruptible(x) ({ int __r; printk("DOWNi:%u:" #x "\n", __LINE__); __r = down_interruptible(x); if (__r != 0) printk("ABORT-DOWNi:%u\n", __LINE__); __r; })
119#define up(x) do { printk("UP:%u:" #x "\n", __LINE__); up(x); } while(0)
120#endif
121
122static int ip6_masked_addrcmp(struct in6_addr addr1, struct in6_addr mask,
123 struct in6_addr addr2)
124{
125 int i;
126 for( i = 0; i < 16; i++){
127 if((addr1.s6_addr[i] & mask.s6_addr[i]) !=
128 (addr2.s6_addr[i] & mask.s6_addr[i]))
129 return 1;
130 }
131 return 0;
132}
133
134/* Check for an extension */
135int
136ip6t_ext_hdr(u8 nexthdr)
137{
138 return ( (nexthdr == IPPROTO_HOPOPTS) ||
139 (nexthdr == IPPROTO_ROUTING) ||
140 (nexthdr == IPPROTO_FRAGMENT) ||
141 (nexthdr == IPPROTO_ESP) ||
142 (nexthdr == IPPROTO_AH) ||
143 (nexthdr == IPPROTO_NONE) ||
144 (nexthdr == IPPROTO_DSTOPTS) );
145}
146
147/* Returns whether matches rule or not. */
148static inline int
149ip6_packet_match(const struct sk_buff *skb,
150 const char *indev,
151 const char *outdev,
152 const struct ip6t_ip6 *ip6info,
153 unsigned int *protoff,
154 int *fragoff)
155{
156 size_t i;
157 unsigned long ret;
158 const struct ipv6hdr *ipv6 = skb->nh.ipv6h;
159
160#define FWINV(bool,invflg) ((bool) ^ !!(ip6info->invflags & invflg))
161
162 if (FWINV(ip6_masked_addrcmp(ipv6->saddr,ip6info->smsk,ip6info->src),
163 IP6T_INV_SRCIP)
164 || FWINV(ip6_masked_addrcmp(ipv6->daddr,ip6info->dmsk,ip6info->dst),
165 IP6T_INV_DSTIP)) {
166 dprintf("Source or dest mismatch.\n");
167/*
168 dprintf("SRC: %u. Mask: %u. Target: %u.%s\n", ip->saddr,
169 ipinfo->smsk.s_addr, ipinfo->src.s_addr,
170 ipinfo->invflags & IP6T_INV_SRCIP ? " (INV)" : "");
171 dprintf("DST: %u. Mask: %u. Target: %u.%s\n", ip->daddr,
172 ipinfo->dmsk.s_addr, ipinfo->dst.s_addr,
173 ipinfo->invflags & IP6T_INV_DSTIP ? " (INV)" : "");*/
174 return 0;
175 }
176
177 /* Look for ifname matches; this should unroll nicely. */
178 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
179 ret |= (((const unsigned long *)indev)[i]
180 ^ ((const unsigned long *)ip6info->iniface)[i])
181 & ((const unsigned long *)ip6info->iniface_mask)[i];
182 }
183
184 if (FWINV(ret != 0, IP6T_INV_VIA_IN)) {
185 dprintf("VIA in mismatch (%s vs %s).%s\n",
186 indev, ip6info->iniface,
187 ip6info->invflags&IP6T_INV_VIA_IN ?" (INV)":"");
188 return 0;
189 }
190
191 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
192 ret |= (((const unsigned long *)outdev)[i]
193 ^ ((const unsigned long *)ip6info->outiface)[i])
194 & ((const unsigned long *)ip6info->outiface_mask)[i];
195 }
196
197 if (FWINV(ret != 0, IP6T_INV_VIA_OUT)) {
198 dprintf("VIA out mismatch (%s vs %s).%s\n",
199 outdev, ip6info->outiface,
200 ip6info->invflags&IP6T_INV_VIA_OUT ?" (INV)":"");
201 return 0;
202 }
203
204/* ... might want to do something with class and flowlabel here ... */
205
206 /* look for the desired protocol header */
207 if((ip6info->flags & IP6T_F_PROTO)) {
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800208 int protohdr;
209 unsigned short _frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800211 protohdr = ipv6_find_hdr(skb, protoff, -1, &_frag_off);
212 if (protohdr < 0)
213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800215 *fragoff = _frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 dprintf("Packet protocol %hi ?= %s%hi.\n",
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800218 protohdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 ip6info->invflags & IP6T_INV_PROTO ? "!":"",
220 ip6info->proto);
221
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800222 if (ip6info->proto == protohdr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if(ip6info->invflags & IP6T_INV_PROTO) {
224 return 0;
225 }
226 return 1;
227 }
228
229 /* We need match for the '-p all', too! */
230 if ((ip6info->proto != 0) &&
231 !(ip6info->invflags & IP6T_INV_PROTO))
232 return 0;
233 }
234 return 1;
235}
236
237/* should be ip6 safe */
238static inline int
239ip6_checkentry(const struct ip6t_ip6 *ipv6)
240{
241 if (ipv6->flags & ~IP6T_F_MASK) {
242 duprintf("Unknown flag bits set: %08X\n",
243 ipv6->flags & ~IP6T_F_MASK);
244 return 0;
245 }
246 if (ipv6->invflags & ~IP6T_INV_MASK) {
247 duprintf("Unknown invflag bits set: %08X\n",
248 ipv6->invflags & ~IP6T_INV_MASK);
249 return 0;
250 }
251 return 1;
252}
253
254static unsigned int
255ip6t_error(struct sk_buff **pskb,
256 const struct net_device *in,
257 const struct net_device *out,
258 unsigned int hooknum,
259 const void *targinfo,
260 void *userinfo)
261{
262 if (net_ratelimit())
263 printk("ip6_tables: error: `%s'\n", (char *)targinfo);
264
265 return NF_DROP;
266}
267
268static inline
269int do_match(struct ip6t_entry_match *m,
270 const struct sk_buff *skb,
271 const struct net_device *in,
272 const struct net_device *out,
273 int offset,
274 unsigned int protoff,
275 int *hotdrop)
276{
277 /* Stop iteration if it doesn't match */
278 if (!m->u.kernel.match->match(skb, in, out, m->data,
279 offset, protoff, hotdrop))
280 return 1;
281 else
282 return 0;
283}
284
285static inline struct ip6t_entry *
286get_entry(void *base, unsigned int offset)
287{
288 return (struct ip6t_entry *)(base + offset);
289}
290
291/* Returns one of the generic firewall policies, like NF_ACCEPT. */
292unsigned int
293ip6t_do_table(struct sk_buff **pskb,
294 unsigned int hook,
295 const struct net_device *in,
296 const struct net_device *out,
297 struct ip6t_table *table,
298 void *userdata)
299{
Harald Welte6b7d31f2005-10-26 09:34:24 +0200300 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int offset = 0;
302 unsigned int protoff = 0;
303 int hotdrop = 0;
304 /* Initializing verdict to NF_DROP keeps gcc happy. */
305 unsigned int verdict = NF_DROP;
306 const char *indev, *outdev;
307 void *table_base;
308 struct ip6t_entry *e, *back;
309
310 /* Initialization */
311 indev = in ? in->name : nulldevname;
312 outdev = out ? out->name : nulldevname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* We handle fragments by dealing with the first fragment as
314 * if it was a normal packet. All other fragments are treated
315 * normally, except that they will NEVER match rules that ask
316 * things we don't know, ie. tcp syn flag or ports). If the
317 * rule is also a fragment-specific rule, non-fragments won't
318 * match it. */
319
320 read_lock_bh(&table->lock);
321 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Eric Dumazet31836062005-12-13 23:13:48 -0800322 table_base = (void *)table->private->entries[smp_processor_id()];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 e = get_entry(table_base, table->private->hook_entry[hook]);
324
325#ifdef CONFIG_NETFILTER_DEBUG
326 /* Check noone else using our table */
327 if (((struct ip6t_entry *)table_base)->comefrom != 0xdead57ac
328 && ((struct ip6t_entry *)table_base)->comefrom != 0xeeeeeeec) {
329 printk("ASSERT: CPU #%u, %s comefrom(%p) = %X\n",
330 smp_processor_id(),
331 table->name,
332 &((struct ip6t_entry *)table_base)->comefrom,
333 ((struct ip6t_entry *)table_base)->comefrom);
334 }
335 ((struct ip6t_entry *)table_base)->comefrom = 0x57acc001;
336#endif
337
338 /* For return from builtin chain */
339 back = get_entry(table_base, table->private->underflow[hook]);
340
341 do {
342 IP_NF_ASSERT(e);
343 IP_NF_ASSERT(back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (ip6_packet_match(*pskb, indev, outdev, &e->ipv6,
345 &protoff, &offset)) {
346 struct ip6t_entry_target *t;
347
348 if (IP6T_MATCH_ITERATE(e, do_match,
349 *pskb, in, out,
350 offset, protoff, &hotdrop) != 0)
351 goto no_match;
352
353 ADD_COUNTER(e->counters,
354 ntohs((*pskb)->nh.ipv6h->payload_len)
355 + IPV6_HDR_LEN,
356 1);
357
358 t = ip6t_get_target(e);
359 IP_NF_ASSERT(t->u.kernel.target);
360 /* Standard target? */
361 if (!t->u.kernel.target->target) {
362 int v;
363
364 v = ((struct ip6t_standard_target *)t)->verdict;
365 if (v < 0) {
366 /* Pop from stack? */
367 if (v != IP6T_RETURN) {
368 verdict = (unsigned)(-v) - 1;
369 break;
370 }
371 e = back;
372 back = get_entry(table_base,
373 back->comefrom);
374 continue;
375 }
Patrick McHardy05465342005-08-21 23:31:43 -0700376 if (table_base + v != (void *)e + e->next_offset
377 && !(e->ipv6.flags & IP6T_F_GOTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* Save old back ptr in next entry */
379 struct ip6t_entry *next
380 = (void *)e + e->next_offset;
381 next->comefrom
382 = (void *)back - table_base;
383 /* set back pointer to next entry */
384 back = next;
385 }
386
387 e = get_entry(table_base, v);
388 } else {
389 /* Targets which reenter must return
390 abs. verdicts */
391#ifdef CONFIG_NETFILTER_DEBUG
392 ((struct ip6t_entry *)table_base)->comefrom
393 = 0xeeeeeeec;
394#endif
395 verdict = t->u.kernel.target->target(pskb,
396 in, out,
397 hook,
398 t->data,
399 userdata);
400
401#ifdef CONFIG_NETFILTER_DEBUG
402 if (((struct ip6t_entry *)table_base)->comefrom
403 != 0xeeeeeeec
404 && verdict == IP6T_CONTINUE) {
405 printk("Target %s reentered!\n",
406 t->u.kernel.target->name);
407 verdict = NF_DROP;
408 }
409 ((struct ip6t_entry *)table_base)->comefrom
410 = 0x57acc001;
411#endif
412 if (verdict == IP6T_CONTINUE)
413 e = (void *)e + e->next_offset;
414 else
415 /* Verdict */
416 break;
417 }
418 } else {
419
420 no_match:
421 e = (void *)e + e->next_offset;
422 }
423 } while (!hotdrop);
424
425#ifdef CONFIG_NETFILTER_DEBUG
426 ((struct ip6t_entry *)table_base)->comefrom = 0xdead57ac;
427#endif
428 read_unlock_bh(&table->lock);
429
430#ifdef DEBUG_ALLOW_ALL
431 return NF_ACCEPT;
432#else
433 if (hotdrop)
434 return NF_DROP;
435 else return verdict;
436#endif
437}
438
Harald Welte6b7d31f2005-10-26 09:34:24 +0200439/*
440 * These are weird, but module loading must not be done with mutex
441 * held (since they will register), and we have to have a single
442 * function to use try_then_request_module().
443 */
444
445/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
446static inline struct ip6t_table *find_table_lock(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Harald Welte6b7d31f2005-10-26 09:34:24 +0200448 struct ip6t_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Harald Welte6b7d31f2005-10-26 09:34:24 +0200450 if (down_interruptible(&ip6t_mutex) != 0)
451 return ERR_PTR(-EINTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Harald Welte6b7d31f2005-10-26 09:34:24 +0200453 list_for_each_entry(t, &ip6t_tables, list)
454 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
455 return t;
456 up(&ip6t_mutex);
457 return NULL;
458}
459
460/* Find match, grabs ref. Returns ERR_PTR() on error. */
461static inline struct ip6t_match *find_match(const char *name, u8 revision)
462{
463 struct ip6t_match *m;
464 int err = 0;
465
466 if (down_interruptible(&ip6t_mutex) != 0)
467 return ERR_PTR(-EINTR);
468
469 list_for_each_entry(m, &ip6t_match, list) {
470 if (strcmp(m->name, name) == 0) {
471 if (m->revision == revision) {
472 if (try_module_get(m->me)) {
473 up(&ip6t_mutex);
474 return m;
475 }
476 } else
477 err = -EPROTOTYPE; /* Found something. */
478 }
479 }
480 up(&ip6t_mutex);
481 return ERR_PTR(err);
482}
483
484/* Find target, grabs ref. Returns ERR_PTR() on error. */
485static inline struct ip6t_target *find_target(const char *name, u8 revision)
486{
487 struct ip6t_target *t;
488 int err = 0;
489
490 if (down_interruptible(&ip6t_mutex) != 0)
491 return ERR_PTR(-EINTR);
492
493 list_for_each_entry(t, &ip6t_target, list) {
494 if (strcmp(t->name, name) == 0) {
495 if (t->revision == revision) {
496 if (try_module_get(t->me)) {
497 up(&ip6t_mutex);
498 return t;
499 }
500 } else
501 err = -EPROTOTYPE; /* Found something. */
502 }
503 }
504 up(&ip6t_mutex);
505 return ERR_PTR(err);
506}
507
508struct ip6t_target *ip6t_find_target(const char *name, u8 revision)
509{
510 struct ip6t_target *target;
511
512 target = try_then_request_module(find_target(name, revision),
513 "ip6t_%s", name);
514 if (IS_ERR(target) || !target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return NULL;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200516 return target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Harald Welte6b7d31f2005-10-26 09:34:24 +0200519static int match_revfn(const char *name, u8 revision, int *bestp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Harald Welte6b7d31f2005-10-26 09:34:24 +0200521 struct ip6t_match *m;
522 int have_rev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Harald Welte6b7d31f2005-10-26 09:34:24 +0200524 list_for_each_entry(m, &ip6t_match, list) {
525 if (strcmp(m->name, name) == 0) {
526 if (m->revision > *bestp)
527 *bestp = m->revision;
528 if (m->revision == revision)
529 have_rev = 1;
530 }
531 }
532 return have_rev;
533}
534
535static int target_revfn(const char *name, u8 revision, int *bestp)
536{
537 struct ip6t_target *t;
538 int have_rev = 0;
539
540 list_for_each_entry(t, &ip6t_target, list) {
541 if (strcmp(t->name, name) == 0) {
542 if (t->revision > *bestp)
543 *bestp = t->revision;
544 if (t->revision == revision)
545 have_rev = 1;
546 }
547 }
548 return have_rev;
549}
550
551/* Returns true or fals (if no such extension at all) */
552static inline int find_revision(const char *name, u8 revision,
553 int (*revfn)(const char *, u8, int *),
554 int *err)
555{
556 int have_rev, best = -1;
557
558 if (down_interruptible(&ip6t_mutex) != 0) {
559 *err = -EINTR;
560 return 1;
561 }
562 have_rev = revfn(name, revision, &best);
563 up(&ip6t_mutex);
564
565 /* Nothing at all? Return 0 to try loading module. */
566 if (best == -1) {
567 *err = -ENOENT;
568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570
Harald Welte6b7d31f2005-10-26 09:34:24 +0200571 *err = best;
572 if (!have_rev)
573 *err = -EPROTONOSUPPORT;
574 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578/* All zeroes == unconditional rule. */
579static inline int
580unconditional(const struct ip6t_ip6 *ipv6)
581{
582 unsigned int i;
583
584 for (i = 0; i < sizeof(*ipv6); i++)
585 if (((char *)ipv6)[i])
586 break;
587
588 return (i == sizeof(*ipv6));
589}
590
591/* Figures out from what hook each rule can be called: returns 0 if
592 there are loops. Puts hook bitmask in comefrom. */
593static int
Eric Dumazet31836062005-12-13 23:13:48 -0800594mark_source_chains(struct ip6t_table_info *newinfo,
595 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 unsigned int hook;
598
599 /* No recursion; use packet counter to save back ptrs (reset
600 to 0 as we leave), and comefrom to save source hook bitmask */
601 for (hook = 0; hook < NF_IP6_NUMHOOKS; hook++) {
602 unsigned int pos = newinfo->hook_entry[hook];
603 struct ip6t_entry *e
Eric Dumazet31836062005-12-13 23:13:48 -0800604 = (struct ip6t_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (!(valid_hooks & (1 << hook)))
607 continue;
608
609 /* Set initial back pointer. */
610 e->counters.pcnt = pos;
611
612 for (;;) {
613 struct ip6t_standard_target *t
614 = (void *)ip6t_get_target(e);
615
616 if (e->comefrom & (1 << NF_IP6_NUMHOOKS)) {
617 printk("iptables: loop hook %u pos %u %08X.\n",
618 hook, pos, e->comefrom);
619 return 0;
620 }
621 e->comefrom
622 |= ((1 << hook) | (1 << NF_IP6_NUMHOOKS));
623
624 /* Unconditional return/END. */
625 if (e->target_offset == sizeof(struct ip6t_entry)
626 && (strcmp(t->target.u.user.name,
627 IP6T_STANDARD_TARGET) == 0)
628 && t->verdict < 0
629 && unconditional(&e->ipv6)) {
630 unsigned int oldpos, size;
631
632 /* Return: backtrack through the last
633 big jump. */
634 do {
635 e->comefrom ^= (1<<NF_IP6_NUMHOOKS);
636#ifdef DEBUG_IP_FIREWALL_USER
637 if (e->comefrom
638 & (1 << NF_IP6_NUMHOOKS)) {
639 duprintf("Back unset "
640 "on hook %u "
641 "rule %u\n",
642 hook, pos);
643 }
644#endif
645 oldpos = pos;
646 pos = e->counters.pcnt;
647 e->counters.pcnt = 0;
648
649 /* We're at the start. */
650 if (pos == oldpos)
651 goto next;
652
653 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800654 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 } while (oldpos == pos + e->next_offset);
656
657 /* Move along one */
658 size = e->next_offset;
659 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800660 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 e->counters.pcnt = pos;
662 pos += size;
663 } else {
664 int newpos = t->verdict;
665
666 if (strcmp(t->target.u.user.name,
667 IP6T_STANDARD_TARGET) == 0
668 && newpos >= 0) {
669 /* This a jump; chase it. */
670 duprintf("Jump rule %u -> %u\n",
671 pos, newpos);
672 } else {
673 /* ... this is a fallthru */
674 newpos = pos + e->next_offset;
675 }
676 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800677 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 e->counters.pcnt = pos;
679 pos = newpos;
680 }
681 }
682 next:
683 duprintf("Finished chain %u\n", hook);
684 }
685 return 1;
686}
687
688static inline int
689cleanup_match(struct ip6t_entry_match *m, unsigned int *i)
690{
691 if (i && (*i)-- == 0)
692 return 1;
693
694 if (m->u.kernel.match->destroy)
695 m->u.kernel.match->destroy(m->data,
696 m->u.match_size - sizeof(*m));
697 module_put(m->u.kernel.match->me);
698 return 0;
699}
700
701static inline int
702standard_check(const struct ip6t_entry_target *t,
703 unsigned int max_offset)
704{
705 struct ip6t_standard_target *targ = (void *)t;
706
707 /* Check standard info. */
708 if (t->u.target_size
709 != IP6T_ALIGN(sizeof(struct ip6t_standard_target))) {
710 duprintf("standard_check: target size %u != %u\n",
711 t->u.target_size,
712 IP6T_ALIGN(sizeof(struct ip6t_standard_target)));
713 return 0;
714 }
715
716 if (targ->verdict >= 0
717 && targ->verdict > max_offset - sizeof(struct ip6t_entry)) {
718 duprintf("ip6t_standard_check: bad verdict (%i)\n",
719 targ->verdict);
720 return 0;
721 }
722
723 if (targ->verdict < -NF_MAX_VERDICT - 1) {
724 duprintf("ip6t_standard_check: bad negative verdict (%i)\n",
725 targ->verdict);
726 return 0;
727 }
728 return 1;
729}
730
731static inline int
732check_match(struct ip6t_entry_match *m,
733 const char *name,
734 const struct ip6t_ip6 *ipv6,
735 unsigned int hookmask,
736 unsigned int *i)
737{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 struct ip6t_match *match;
739
Harald Welte6b7d31f2005-10-26 09:34:24 +0200740 match = try_then_request_module(find_match(m->u.user.name,
741 m->u.user.revision),
742 "ip6t_%s", m->u.user.name);
743 if (IS_ERR(match) || !match) {
744 duprintf("check_match: `%s' not found\n", m->u.user.name);
745 return match ? PTR_ERR(match) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747 m->u.kernel.match = match;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (m->u.kernel.match->checkentry
750 && !m->u.kernel.match->checkentry(name, ipv6, m->data,
751 m->u.match_size - sizeof(*m),
752 hookmask)) {
753 module_put(m->u.kernel.match->me);
754 duprintf("ip_tables: check failed for `%s'.\n",
755 m->u.kernel.match->name);
756 return -EINVAL;
757 }
758
759 (*i)++;
760 return 0;
761}
762
763static struct ip6t_target ip6t_standard_target;
764
765static inline int
766check_entry(struct ip6t_entry *e, const char *name, unsigned int size,
767 unsigned int *i)
768{
769 struct ip6t_entry_target *t;
770 struct ip6t_target *target;
771 int ret;
772 unsigned int j;
773
774 if (!ip6_checkentry(&e->ipv6)) {
775 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
776 return -EINVAL;
777 }
778
779 j = 0;
780 ret = IP6T_MATCH_ITERATE(e, check_match, name, &e->ipv6, e->comefrom, &j);
781 if (ret != 0)
782 goto cleanup_matches;
783
784 t = ip6t_get_target(e);
Harald Welte6b7d31f2005-10-26 09:34:24 +0200785 target = try_then_request_module(find_target(t->u.user.name,
786 t->u.user.revision),
787 "ip6t_%s", t->u.user.name);
788 if (IS_ERR(target) || !target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 duprintf("check_entry: `%s' not found\n", t->u.user.name);
Harald Welte6b7d31f2005-10-26 09:34:24 +0200790 ret = target ? PTR_ERR(target) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 goto cleanup_matches;
792 }
793 t->u.kernel.target = target;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (t->u.kernel.target == &ip6t_standard_target) {
796 if (!standard_check(t, size)) {
797 ret = -EINVAL;
798 goto cleanup_matches;
799 }
800 } else if (t->u.kernel.target->checkentry
801 && !t->u.kernel.target->checkentry(name, e, t->data,
802 t->u.target_size
803 - sizeof(*t),
804 e->comefrom)) {
805 module_put(t->u.kernel.target->me);
806 duprintf("ip_tables: check failed for `%s'.\n",
807 t->u.kernel.target->name);
808 ret = -EINVAL;
809 goto cleanup_matches;
810 }
811
812 (*i)++;
813 return 0;
814
815 cleanup_matches:
816 IP6T_MATCH_ITERATE(e, cleanup_match, &j);
817 return ret;
818}
819
820static inline int
821check_entry_size_and_hooks(struct ip6t_entry *e,
822 struct ip6t_table_info *newinfo,
823 unsigned char *base,
824 unsigned char *limit,
825 const unsigned int *hook_entries,
826 const unsigned int *underflows,
827 unsigned int *i)
828{
829 unsigned int h;
830
831 if ((unsigned long)e % __alignof__(struct ip6t_entry) != 0
832 || (unsigned char *)e + sizeof(struct ip6t_entry) >= limit) {
833 duprintf("Bad offset %p\n", e);
834 return -EINVAL;
835 }
836
837 if (e->next_offset
838 < sizeof(struct ip6t_entry) + sizeof(struct ip6t_entry_target)) {
839 duprintf("checking: element %p size %u\n",
840 e, e->next_offset);
841 return -EINVAL;
842 }
843
844 /* Check hooks & underflows */
845 for (h = 0; h < NF_IP6_NUMHOOKS; h++) {
846 if ((unsigned char *)e - base == hook_entries[h])
847 newinfo->hook_entry[h] = hook_entries[h];
848 if ((unsigned char *)e - base == underflows[h])
849 newinfo->underflow[h] = underflows[h];
850 }
851
852 /* FIXME: underflows must be unconditional, standard verdicts
853 < 0 (not IP6T_RETURN). --RR */
854
855 /* Clear counters and comefrom */
856 e->counters = ((struct ip6t_counters) { 0, 0 });
857 e->comefrom = 0;
858
859 (*i)++;
860 return 0;
861}
862
863static inline int
864cleanup_entry(struct ip6t_entry *e, unsigned int *i)
865{
866 struct ip6t_entry_target *t;
867
868 if (i && (*i)-- == 0)
869 return 1;
870
871 /* Cleanup all matches */
872 IP6T_MATCH_ITERATE(e, cleanup_match, NULL);
873 t = ip6t_get_target(e);
874 if (t->u.kernel.target->destroy)
875 t->u.kernel.target->destroy(t->data,
876 t->u.target_size - sizeof(*t));
877 module_put(t->u.kernel.target->me);
878 return 0;
879}
880
881/* Checks and translates the user-supplied table segment (held in
882 newinfo) */
883static int
884translate_table(const char *name,
885 unsigned int valid_hooks,
886 struct ip6t_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800887 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 unsigned int size,
889 unsigned int number,
890 const unsigned int *hook_entries,
891 const unsigned int *underflows)
892{
893 unsigned int i;
894 int ret;
895
896 newinfo->size = size;
897 newinfo->number = number;
898
899 /* Init all hooks to impossible value. */
900 for (i = 0; i < NF_IP6_NUMHOOKS; i++) {
901 newinfo->hook_entry[i] = 0xFFFFFFFF;
902 newinfo->underflow[i] = 0xFFFFFFFF;
903 }
904
905 duprintf("translate_table: size %u\n", newinfo->size);
906 i = 0;
907 /* Walk through entries, checking offsets. */
Eric Dumazet31836062005-12-13 23:13:48 -0800908 ret = IP6T_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 check_entry_size_and_hooks,
910 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800911 entry0,
912 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 hook_entries, underflows, &i);
914 if (ret != 0)
915 return ret;
916
917 if (i != number) {
918 duprintf("translate_table: %u not %u entries\n",
919 i, number);
920 return -EINVAL;
921 }
922
923 /* Check hooks all assigned */
924 for (i = 0; i < NF_IP6_NUMHOOKS; i++) {
925 /* Only hooks which are valid */
926 if (!(valid_hooks & (1 << i)))
927 continue;
928 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
929 duprintf("Invalid hook entry %u %u\n",
930 i, hook_entries[i]);
931 return -EINVAL;
932 }
933 if (newinfo->underflow[i] == 0xFFFFFFFF) {
934 duprintf("Invalid underflow %u %u\n",
935 i, underflows[i]);
936 return -EINVAL;
937 }
938 }
939
Eric Dumazet31836062005-12-13 23:13:48 -0800940 if (!mark_source_chains(newinfo, valid_hooks, entry0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return -ELOOP;
942
943 /* Finally, each sanity check must pass */
944 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800945 ret = IP6T_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 check_entry, name, size, &i);
947
948 if (ret != 0) {
Eric Dumazet31836062005-12-13 23:13:48 -0800949 IP6T_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 cleanup_entry, &i);
951 return ret;
952 }
953
954 /* And one copy for every other CPU */
David S. Millerc8923c62005-10-13 14:41:23 -0700955 for_each_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800956 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
957 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959
960 return ret;
961}
962
963static struct ip6t_table_info *
964replace_table(struct ip6t_table *table,
965 unsigned int num_counters,
966 struct ip6t_table_info *newinfo,
967 int *error)
968{
969 struct ip6t_table_info *oldinfo;
970
971#ifdef CONFIG_NETFILTER_DEBUG
972 {
Eric Dumazet31836062005-12-13 23:13:48 -0800973 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Eric Dumazet31836062005-12-13 23:13:48 -0800975 for_each_cpu(cpu) {
976 struct ip6t_entry *table_base = newinfo->entries[cpu];
977 if (table_base)
978 table_base->comefrom = 0xdead57ac;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980 }
981#endif
982
983 /* Do the substitution. */
984 write_lock_bh(&table->lock);
985 /* Check inside lock: is the old number correct? */
986 if (num_counters != table->private->number) {
987 duprintf("num_counters != table->private->number (%u/%u)\n",
988 num_counters, table->private->number);
989 write_unlock_bh(&table->lock);
990 *error = -EAGAIN;
991 return NULL;
992 }
993 oldinfo = table->private;
994 table->private = newinfo;
995 newinfo->initial_entries = oldinfo->initial_entries;
996 write_unlock_bh(&table->lock);
997
998 return oldinfo;
999}
1000
1001/* Gets counters. */
1002static inline int
1003add_entry_to_counter(const struct ip6t_entry *e,
1004 struct ip6t_counters total[],
1005 unsigned int *i)
1006{
1007 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
1008
1009 (*i)++;
1010 return 0;
1011}
1012
Eric Dumazet31836062005-12-13 23:13:48 -08001013static inline int
1014set_entry_to_counter(const struct ip6t_entry *e,
1015 struct ip6t_counters total[],
1016 unsigned int *i)
1017{
1018 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
1019
1020 (*i)++;
1021 return 0;
1022}
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024static void
1025get_counters(const struct ip6t_table_info *t,
1026 struct ip6t_counters counters[])
1027{
1028 unsigned int cpu;
1029 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -08001030 unsigned int curcpu;
1031
1032 /* Instead of clearing (by a previous call to memset())
1033 * the counters and using adds, we set the counters
1034 * with data used by 'current' CPU
1035 * We dont care about preemption here.
1036 */
1037 curcpu = raw_smp_processor_id();
1038
1039 i = 0;
1040 IP6T_ENTRY_ITERATE(t->entries[curcpu],
1041 t->size,
1042 set_entry_to_counter,
1043 counters,
1044 &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
David S. Millerc8923c62005-10-13 14:41:23 -07001046 for_each_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -08001047 if (cpu == curcpu)
1048 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001050 IP6T_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 t->size,
1052 add_entry_to_counter,
1053 counters,
1054 &i);
1055 }
1056}
1057
1058static int
1059copy_entries_to_user(unsigned int total_size,
1060 struct ip6t_table *table,
1061 void __user *userptr)
1062{
1063 unsigned int off, num, countersize;
1064 struct ip6t_entry *e;
1065 struct ip6t_counters *counters;
1066 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001067 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /* We need atomic snapshot of counters: rest doesn't change
1070 (other than comefrom, which userspace doesn't care
1071 about). */
1072 countersize = sizeof(struct ip6t_counters) * table->private->number;
1073 counters = vmalloc(countersize);
1074
1075 if (counters == NULL)
1076 return -ENOMEM;
1077
1078 /* First, sum counters... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 write_lock_bh(&table->lock);
1080 get_counters(table->private, counters);
1081 write_unlock_bh(&table->lock);
1082
Eric Dumazet31836062005-12-13 23:13:48 -08001083 /* choose the copy that is on ourc node/cpu */
1084 loc_cpu_entry = table->private->entries[raw_smp_processor_id()];
1085 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 ret = -EFAULT;
1087 goto free_counters;
1088 }
1089
1090 /* FIXME: use iterator macros --RR */
1091 /* ... then go back and fix counters and names */
1092 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
1093 unsigned int i;
1094 struct ip6t_entry_match *m;
1095 struct ip6t_entry_target *t;
1096
Eric Dumazet31836062005-12-13 23:13:48 -08001097 e = (struct ip6t_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (copy_to_user(userptr + off
1099 + offsetof(struct ip6t_entry, counters),
1100 &counters[num],
1101 sizeof(counters[num])) != 0) {
1102 ret = -EFAULT;
1103 goto free_counters;
1104 }
1105
1106 for (i = sizeof(struct ip6t_entry);
1107 i < e->target_offset;
1108 i += m->u.match_size) {
1109 m = (void *)e + i;
1110
1111 if (copy_to_user(userptr + off + i
1112 + offsetof(struct ip6t_entry_match,
1113 u.user.name),
1114 m->u.kernel.match->name,
1115 strlen(m->u.kernel.match->name)+1)
1116 != 0) {
1117 ret = -EFAULT;
1118 goto free_counters;
1119 }
1120 }
1121
1122 t = ip6t_get_target(e);
1123 if (copy_to_user(userptr + off + e->target_offset
1124 + offsetof(struct ip6t_entry_target,
1125 u.user.name),
1126 t->u.kernel.target->name,
1127 strlen(t->u.kernel.target->name)+1) != 0) {
1128 ret = -EFAULT;
1129 goto free_counters;
1130 }
1131 }
1132
1133 free_counters:
1134 vfree(counters);
1135 return ret;
1136}
1137
1138static int
1139get_entries(const struct ip6t_get_entries *entries,
1140 struct ip6t_get_entries __user *uptr)
1141{
1142 int ret;
1143 struct ip6t_table *t;
1144
Harald Welte6b7d31f2005-10-26 09:34:24 +02001145 t = find_table_lock(entries->name);
1146 if (t && !IS_ERR(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 duprintf("t->private->number = %u\n",
1148 t->private->number);
1149 if (entries->size == t->private->size)
1150 ret = copy_entries_to_user(t->private->size,
1151 t, uptr->entrytable);
1152 else {
1153 duprintf("get_entries: I've got %u not %u!\n",
1154 t->private->size,
1155 entries->size);
1156 ret = -EINVAL;
1157 }
Harald Welte6b7d31f2005-10-26 09:34:24 +02001158 module_put(t->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 up(&ip6t_mutex);
1160 } else
Harald Welte6b7d31f2005-10-26 09:34:24 +02001161 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 return ret;
1164}
1165
Eric Dumazet31836062005-12-13 23:13:48 -08001166static void free_table_info(struct ip6t_table_info *info)
1167{
1168 int cpu;
1169 for_each_cpu(cpu) {
1170 if (info->size <= PAGE_SIZE)
1171 kfree(info->entries[cpu]);
1172 else
1173 vfree(info->entries[cpu]);
1174 }
1175 kfree(info);
1176}
1177
1178static struct ip6t_table_info *alloc_table_info(unsigned int size)
1179{
1180 struct ip6t_table_info *newinfo;
1181 int cpu;
1182
1183 newinfo = kzalloc(sizeof(struct ip6t_table_info), GFP_KERNEL);
1184 if (!newinfo)
1185 return NULL;
1186
1187 newinfo->size = size;
1188
1189 for_each_cpu(cpu) {
1190 if (size <= PAGE_SIZE)
1191 newinfo->entries[cpu] = kmalloc_node(size,
1192 GFP_KERNEL,
1193 cpu_to_node(cpu));
1194 else
1195 newinfo->entries[cpu] = vmalloc_node(size,
1196 cpu_to_node(cpu));
1197 if (newinfo->entries[cpu] == NULL) {
1198 free_table_info(newinfo);
1199 return NULL;
1200 }
1201 }
1202
1203 return newinfo;
1204}
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206static int
1207do_replace(void __user *user, unsigned int len)
1208{
1209 int ret;
1210 struct ip6t_replace tmp;
1211 struct ip6t_table *t;
1212 struct ip6t_table_info *newinfo, *oldinfo;
1213 struct ip6t_counters *counters;
Eric Dumazet31836062005-12-13 23:13:48 -08001214 void *loc_cpu_entry, *loc_cpu_old_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1217 return -EFAULT;
1218
1219 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
1220 if ((SMP_ALIGN(tmp.size) >> PAGE_SHIFT) + 2 > num_physpages)
1221 return -ENOMEM;
1222
Eric Dumazet31836062005-12-13 23:13:48 -08001223 newinfo = alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (!newinfo)
1225 return -ENOMEM;
1226
Eric Dumazet31836062005-12-13 23:13:48 -08001227 /* choose the copy that is on our node/cpu */
1228 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1229 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 tmp.size) != 0) {
1231 ret = -EFAULT;
1232 goto free_newinfo;
1233 }
1234
1235 counters = vmalloc(tmp.num_counters * sizeof(struct ip6t_counters));
1236 if (!counters) {
1237 ret = -ENOMEM;
1238 goto free_newinfo;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001242 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 tmp.hook_entry, tmp.underflow);
1244 if (ret != 0)
1245 goto free_newinfo_counters;
1246
1247 duprintf("ip_tables: Translated table\n");
1248
Harald Welte6b7d31f2005-10-26 09:34:24 +02001249 t = try_then_request_module(find_table_lock(tmp.name),
1250 "ip6table_%s", tmp.name);
1251 if (!t || IS_ERR(t)) {
1252 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 goto free_newinfo_counters_untrans;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 /* You lied! */
1257 if (tmp.valid_hooks != t->valid_hooks) {
1258 duprintf("Valid hook crap: %08X vs %08X\n",
1259 tmp.valid_hooks, t->valid_hooks);
1260 ret = -EINVAL;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001261 goto put_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263
1264 oldinfo = replace_table(t, tmp.num_counters, newinfo, &ret);
1265 if (!oldinfo)
1266 goto put_module;
1267
1268 /* Update module usage count based on number of rules */
1269 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1270 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1271 if ((oldinfo->number > oldinfo->initial_entries) ||
1272 (newinfo->number <= oldinfo->initial_entries))
1273 module_put(t->me);
1274 if ((oldinfo->number > oldinfo->initial_entries) &&
1275 (newinfo->number <= oldinfo->initial_entries))
1276 module_put(t->me);
1277
1278 /* Get the old counters. */
1279 get_counters(oldinfo, counters);
1280 /* Decrease module usage counts and free resource */
Eric Dumazet31836062005-12-13 23:13:48 -08001281 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1282 IP6T_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL);
1283 free_table_info(oldinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (copy_to_user(tmp.counters, counters,
1285 sizeof(struct ip6t_counters) * tmp.num_counters) != 0)
1286 ret = -EFAULT;
1287 vfree(counters);
1288 up(&ip6t_mutex);
1289 return ret;
1290
1291 put_module:
1292 module_put(t->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 up(&ip6t_mutex);
1294 free_newinfo_counters_untrans:
Eric Dumazet31836062005-12-13 23:13:48 -08001295 IP6T_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 free_newinfo_counters:
1297 vfree(counters);
1298 free_newinfo:
Eric Dumazet31836062005-12-13 23:13:48 -08001299 free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return ret;
1301}
1302
1303/* We're lazy, and add to the first CPU; overflow works its fey magic
1304 * and everything is OK. */
1305static inline int
1306add_counter_to_entry(struct ip6t_entry *e,
1307 const struct ip6t_counters addme[],
1308 unsigned int *i)
1309{
1310#if 0
1311 duprintf("add_counter: Entry %u %lu/%lu + %lu/%lu\n",
1312 *i,
1313 (long unsigned int)e->counters.pcnt,
1314 (long unsigned int)e->counters.bcnt,
1315 (long unsigned int)addme[*i].pcnt,
1316 (long unsigned int)addme[*i].bcnt);
1317#endif
1318
1319 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1320
1321 (*i)++;
1322 return 0;
1323}
1324
1325static int
1326do_add_counters(void __user *user, unsigned int len)
1327{
1328 unsigned int i;
1329 struct ip6t_counters_info tmp, *paddc;
1330 struct ip6t_table *t;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001331 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001332 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1335 return -EFAULT;
1336
1337 if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct ip6t_counters))
1338 return -EINVAL;
1339
1340 paddc = vmalloc(len);
1341 if (!paddc)
1342 return -ENOMEM;
1343
1344 if (copy_from_user(paddc, user, len) != 0) {
1345 ret = -EFAULT;
1346 goto free;
1347 }
1348
Harald Welte6b7d31f2005-10-26 09:34:24 +02001349 t = find_table_lock(tmp.name);
1350 if (!t || IS_ERR(t)) {
1351 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 goto free;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 write_lock_bh(&t->lock);
1356 if (t->private->number != paddc->num_counters) {
1357 ret = -EINVAL;
1358 goto unlock_up_free;
1359 }
1360
1361 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001362 /* Choose the copy that is on our node */
1363 loc_cpu_entry = t->private->entries[smp_processor_id()];
1364 IP6T_ENTRY_ITERATE(loc_cpu_entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 t->private->size,
1366 add_counter_to_entry,
1367 paddc->counters,
1368 &i);
1369 unlock_up_free:
1370 write_unlock_bh(&t->lock);
1371 up(&ip6t_mutex);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001372 module_put(t->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 free:
1374 vfree(paddc);
1375
1376 return ret;
1377}
1378
1379static int
1380do_ip6t_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1381{
1382 int ret;
1383
1384 if (!capable(CAP_NET_ADMIN))
1385 return -EPERM;
1386
1387 switch (cmd) {
1388 case IP6T_SO_SET_REPLACE:
1389 ret = do_replace(user, len);
1390 break;
1391
1392 case IP6T_SO_SET_ADD_COUNTERS:
1393 ret = do_add_counters(user, len);
1394 break;
1395
1396 default:
1397 duprintf("do_ip6t_set_ctl: unknown request %i\n", cmd);
1398 ret = -EINVAL;
1399 }
1400
1401 return ret;
1402}
1403
1404static int
1405do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1406{
1407 int ret;
1408
1409 if (!capable(CAP_NET_ADMIN))
1410 return -EPERM;
1411
1412 switch (cmd) {
1413 case IP6T_SO_GET_INFO: {
1414 char name[IP6T_TABLE_MAXNAMELEN];
1415 struct ip6t_table *t;
1416
1417 if (*len != sizeof(struct ip6t_getinfo)) {
1418 duprintf("length %u != %u\n", *len,
1419 sizeof(struct ip6t_getinfo));
1420 ret = -EINVAL;
1421 break;
1422 }
1423
1424 if (copy_from_user(name, user, sizeof(name)) != 0) {
1425 ret = -EFAULT;
1426 break;
1427 }
1428 name[IP6T_TABLE_MAXNAMELEN-1] = '\0';
Harald Welte6b7d31f2005-10-26 09:34:24 +02001429
1430 t = try_then_request_module(find_table_lock(name),
1431 "ip6table_%s", name);
1432 if (t && !IS_ERR(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 struct ip6t_getinfo info;
1434
1435 info.valid_hooks = t->valid_hooks;
1436 memcpy(info.hook_entry, t->private->hook_entry,
1437 sizeof(info.hook_entry));
1438 memcpy(info.underflow, t->private->underflow,
1439 sizeof(info.underflow));
1440 info.num_entries = t->private->number;
1441 info.size = t->private->size;
1442 memcpy(info.name, name, sizeof(info.name));
1443
1444 if (copy_to_user(user, &info, *len) != 0)
1445 ret = -EFAULT;
1446 else
1447 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 up(&ip6t_mutex);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001449 module_put(t->me);
1450 } else
1451 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453 break;
1454
1455 case IP6T_SO_GET_ENTRIES: {
1456 struct ip6t_get_entries get;
1457
1458 if (*len < sizeof(get)) {
1459 duprintf("get_entries: %u < %u\n", *len, sizeof(get));
1460 ret = -EINVAL;
1461 } else if (copy_from_user(&get, user, sizeof(get)) != 0) {
1462 ret = -EFAULT;
1463 } else if (*len != sizeof(struct ip6t_get_entries) + get.size) {
1464 duprintf("get_entries: %u != %u\n", *len,
1465 sizeof(struct ip6t_get_entries) + get.size);
1466 ret = -EINVAL;
1467 } else
1468 ret = get_entries(&get, user);
1469 break;
1470 }
1471
Harald Welte6b7d31f2005-10-26 09:34:24 +02001472 case IP6T_SO_GET_REVISION_MATCH:
1473 case IP6T_SO_GET_REVISION_TARGET: {
1474 struct ip6t_get_revision rev;
1475 int (*revfn)(const char *, u8, int *);
1476
1477 if (*len != sizeof(rev)) {
1478 ret = -EINVAL;
1479 break;
1480 }
1481 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1482 ret = -EFAULT;
1483 break;
1484 }
1485
1486 if (cmd == IP6T_SO_GET_REVISION_TARGET)
1487 revfn = target_revfn;
1488 else
1489 revfn = match_revfn;
1490
1491 try_then_request_module(find_revision(rev.name, rev.revision,
1492 revfn, &ret),
1493 "ip6t_%s", rev.name);
1494 break;
1495 }
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 default:
1498 duprintf("do_ip6t_get_ctl: unknown request %i\n", cmd);
1499 ret = -EINVAL;
1500 }
1501
1502 return ret;
1503}
1504
1505/* Registration hooks for targets. */
1506int
1507ip6t_register_target(struct ip6t_target *target)
1508{
1509 int ret;
1510
1511 ret = down_interruptible(&ip6t_mutex);
1512 if (ret != 0)
1513 return ret;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001514 list_add(&target->list, &ip6t_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 up(&ip6t_mutex);
1516 return ret;
1517}
1518
1519void
1520ip6t_unregister_target(struct ip6t_target *target)
1521{
1522 down(&ip6t_mutex);
1523 LIST_DELETE(&ip6t_target, target);
1524 up(&ip6t_mutex);
1525}
1526
1527int
1528ip6t_register_match(struct ip6t_match *match)
1529{
1530 int ret;
1531
1532 ret = down_interruptible(&ip6t_mutex);
1533 if (ret != 0)
1534 return ret;
1535
Harald Welte6b7d31f2005-10-26 09:34:24 +02001536 list_add(&match->list, &ip6t_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 up(&ip6t_mutex);
1538
1539 return ret;
1540}
1541
1542void
1543ip6t_unregister_match(struct ip6t_match *match)
1544{
1545 down(&ip6t_mutex);
1546 LIST_DELETE(&ip6t_match, match);
1547 up(&ip6t_mutex);
1548}
1549
1550int ip6t_register_table(struct ip6t_table *table,
1551 const struct ip6t_replace *repl)
1552{
1553 int ret;
1554 struct ip6t_table_info *newinfo;
1555 static struct ip6t_table_info bootstrap
1556 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08001557 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Eric Dumazet31836062005-12-13 23:13:48 -08001559 newinfo = alloc_table_info(repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (!newinfo)
1561 return -ENOMEM;
1562
Eric Dumazet31836062005-12-13 23:13:48 -08001563 /* choose the copy on our node/cpu */
1564 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1565 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001568 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 repl->num_entries,
1570 repl->hook_entry,
1571 repl->underflow);
1572 if (ret != 0) {
Eric Dumazet31836062005-12-13 23:13:48 -08001573 free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 return ret;
1575 }
1576
1577 ret = down_interruptible(&ip6t_mutex);
1578 if (ret != 0) {
Eric Dumazet31836062005-12-13 23:13:48 -08001579 free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 return ret;
1581 }
1582
1583 /* Don't autoload: we'd eat our tail... */
1584 if (list_named_find(&ip6t_tables, table->name)) {
1585 ret = -EEXIST;
1586 goto free_unlock;
1587 }
1588
1589 /* Simplifies replace_table code. */
1590 table->private = &bootstrap;
1591 if (!replace_table(table, 0, newinfo, &ret))
1592 goto free_unlock;
1593
1594 duprintf("table->private->number = %u\n",
1595 table->private->number);
1596
1597 /* save number of initial entries */
1598 table->private->initial_entries = table->private->number;
1599
1600 rwlock_init(&table->lock);
1601 list_prepend(&ip6t_tables, table);
1602
1603 unlock:
1604 up(&ip6t_mutex);
1605 return ret;
1606
1607 free_unlock:
Eric Dumazet31836062005-12-13 23:13:48 -08001608 free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 goto unlock;
1610}
1611
1612void ip6t_unregister_table(struct ip6t_table *table)
1613{
Eric Dumazet31836062005-12-13 23:13:48 -08001614 void *loc_cpu_entry;
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 down(&ip6t_mutex);
1617 LIST_DELETE(&ip6t_tables, table);
1618 up(&ip6t_mutex);
1619
1620 /* Decrease module usage counts and free resources */
Eric Dumazet31836062005-12-13 23:13:48 -08001621 loc_cpu_entry = table->private->entries[raw_smp_processor_id()];
1622 IP6T_ENTRY_ITERATE(loc_cpu_entry, table->private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 cleanup_entry, NULL);
Eric Dumazet31836062005-12-13 23:13:48 -08001624 free_table_info(table->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
1627/* Returns 1 if the port is matched by the range, 0 otherwise */
1628static inline int
1629port_match(u_int16_t min, u_int16_t max, u_int16_t port, int invert)
1630{
1631 int ret;
1632
1633 ret = (port >= min && port <= max) ^ invert;
1634 return ret;
1635}
1636
1637static int
1638tcp_find_option(u_int8_t option,
1639 const struct sk_buff *skb,
1640 unsigned int tcpoff,
1641 unsigned int optlen,
1642 int invert,
1643 int *hotdrop)
1644{
1645 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
1646 u_int8_t _opt[60 - sizeof(struct tcphdr)], *op;
1647 unsigned int i;
1648
1649 duprintf("tcp_match: finding option\n");
1650 if (!optlen)
1651 return invert;
1652 /* If we don't have the whole header, drop packet. */
1653 op = skb_header_pointer(skb, tcpoff + sizeof(struct tcphdr), optlen,
1654 _opt);
1655 if (op == NULL) {
1656 *hotdrop = 1;
1657 return 0;
1658 }
1659
1660 for (i = 0; i < optlen; ) {
1661 if (op[i] == option) return !invert;
1662 if (op[i] < 2) i++;
1663 else i += op[i+1]?:1;
1664 }
1665
1666 return invert;
1667}
1668
1669static int
1670tcp_match(const struct sk_buff *skb,
1671 const struct net_device *in,
1672 const struct net_device *out,
1673 const void *matchinfo,
1674 int offset,
1675 unsigned int protoff,
1676 int *hotdrop)
1677{
1678 struct tcphdr _tcph, *th;
1679 const struct ip6t_tcp *tcpinfo = matchinfo;
1680
1681 if (offset) {
1682 /* To quote Alan:
1683
1684 Don't allow a fragment of TCP 8 bytes in. Nobody normal
1685 causes this. Its a cracker trying to break in by doing a
1686 flag overwrite to pass the direction checks.
1687 */
1688 if (offset == 1) {
1689 duprintf("Dropping evil TCP offset=1 frag.\n");
1690 *hotdrop = 1;
1691 }
1692 /* Must not be a fragment. */
1693 return 0;
1694 }
1695
1696#define FWINVTCP(bool,invflg) ((bool) ^ !!(tcpinfo->invflags & invflg))
1697
1698 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
1699 if (th == NULL) {
1700 /* We've been asked to examine this packet, and we
1701 can't. Hence, no choice but to drop. */
1702 duprintf("Dropping evil TCP offset=0 tinygram.\n");
1703 *hotdrop = 1;
1704 return 0;
1705 }
1706
1707 if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1],
1708 ntohs(th->source),
1709 !!(tcpinfo->invflags & IP6T_TCP_INV_SRCPT)))
1710 return 0;
1711 if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],
1712 ntohs(th->dest),
1713 !!(tcpinfo->invflags & IP6T_TCP_INV_DSTPT)))
1714 return 0;
1715 if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask)
1716 == tcpinfo->flg_cmp,
1717 IP6T_TCP_INV_FLAGS))
1718 return 0;
1719 if (tcpinfo->option) {
1720 if (th->doff * 4 < sizeof(_tcph)) {
1721 *hotdrop = 1;
1722 return 0;
1723 }
1724 if (!tcp_find_option(tcpinfo->option, skb, protoff,
1725 th->doff*4 - sizeof(*th),
1726 tcpinfo->invflags & IP6T_TCP_INV_OPTION,
1727 hotdrop))
1728 return 0;
1729 }
1730 return 1;
1731}
1732
1733/* Called when user tries to insert an entry of this type. */
1734static int
1735tcp_checkentry(const char *tablename,
1736 const struct ip6t_ip6 *ipv6,
1737 void *matchinfo,
1738 unsigned int matchsize,
1739 unsigned int hook_mask)
1740{
1741 const struct ip6t_tcp *tcpinfo = matchinfo;
1742
1743 /* Must specify proto == TCP, and no unknown invflags */
1744 return ipv6->proto == IPPROTO_TCP
1745 && !(ipv6->invflags & IP6T_INV_PROTO)
1746 && matchsize == IP6T_ALIGN(sizeof(struct ip6t_tcp))
1747 && !(tcpinfo->invflags & ~IP6T_TCP_INV_MASK);
1748}
1749
1750static int
1751udp_match(const struct sk_buff *skb,
1752 const struct net_device *in,
1753 const struct net_device *out,
1754 const void *matchinfo,
1755 int offset,
1756 unsigned int protoff,
1757 int *hotdrop)
1758{
1759 struct udphdr _udph, *uh;
1760 const struct ip6t_udp *udpinfo = matchinfo;
1761
1762 /* Must not be a fragment. */
1763 if (offset)
1764 return 0;
1765
1766 uh = skb_header_pointer(skb, protoff, sizeof(_udph), &_udph);
1767 if (uh == NULL) {
1768 /* We've been asked to examine this packet, and we
1769 can't. Hence, no choice but to drop. */
1770 duprintf("Dropping evil UDP tinygram.\n");
1771 *hotdrop = 1;
1772 return 0;
1773 }
1774
1775 return port_match(udpinfo->spts[0], udpinfo->spts[1],
1776 ntohs(uh->source),
1777 !!(udpinfo->invflags & IP6T_UDP_INV_SRCPT))
1778 && port_match(udpinfo->dpts[0], udpinfo->dpts[1],
1779 ntohs(uh->dest),
1780 !!(udpinfo->invflags & IP6T_UDP_INV_DSTPT));
1781}
1782
1783/* Called when user tries to insert an entry of this type. */
1784static int
1785udp_checkentry(const char *tablename,
1786 const struct ip6t_ip6 *ipv6,
1787 void *matchinfo,
1788 unsigned int matchinfosize,
1789 unsigned int hook_mask)
1790{
1791 const struct ip6t_udp *udpinfo = matchinfo;
1792
1793 /* Must specify proto == UDP, and no unknown invflags */
1794 if (ipv6->proto != IPPROTO_UDP || (ipv6->invflags & IP6T_INV_PROTO)) {
1795 duprintf("ip6t_udp: Protocol %u != %u\n", ipv6->proto,
1796 IPPROTO_UDP);
1797 return 0;
1798 }
1799 if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_udp))) {
1800 duprintf("ip6t_udp: matchsize %u != %u\n",
1801 matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_udp)));
1802 return 0;
1803 }
1804 if (udpinfo->invflags & ~IP6T_UDP_INV_MASK) {
1805 duprintf("ip6t_udp: unknown flags %X\n",
1806 udpinfo->invflags);
1807 return 0;
1808 }
1809
1810 return 1;
1811}
1812
1813/* Returns 1 if the type and code is matched by the range, 0 otherwise */
1814static inline int
1815icmp6_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
1816 u_int8_t type, u_int8_t code,
1817 int invert)
1818{
1819 return (type == test_type && code >= min_code && code <= max_code)
1820 ^ invert;
1821}
1822
1823static int
1824icmp6_match(const struct sk_buff *skb,
1825 const struct net_device *in,
1826 const struct net_device *out,
1827 const void *matchinfo,
1828 int offset,
1829 unsigned int protoff,
1830 int *hotdrop)
1831{
1832 struct icmp6hdr _icmp, *ic;
1833 const struct ip6t_icmp *icmpinfo = matchinfo;
1834
1835 /* Must not be a fragment. */
1836 if (offset)
1837 return 0;
1838
1839 ic = skb_header_pointer(skb, protoff, sizeof(_icmp), &_icmp);
1840 if (ic == NULL) {
1841 /* We've been asked to examine this packet, and we
1842 can't. Hence, no choice but to drop. */
1843 duprintf("Dropping evil ICMP tinygram.\n");
1844 *hotdrop = 1;
1845 return 0;
1846 }
1847
1848 return icmp6_type_code_match(icmpinfo->type,
1849 icmpinfo->code[0],
1850 icmpinfo->code[1],
1851 ic->icmp6_type, ic->icmp6_code,
1852 !!(icmpinfo->invflags&IP6T_ICMP_INV));
1853}
1854
1855/* Called when user tries to insert an entry of this type. */
1856static int
1857icmp6_checkentry(const char *tablename,
1858 const struct ip6t_ip6 *ipv6,
1859 void *matchinfo,
1860 unsigned int matchsize,
1861 unsigned int hook_mask)
1862{
1863 const struct ip6t_icmp *icmpinfo = matchinfo;
1864
1865 /* Must specify proto == ICMP, and no unknown invflags */
1866 return ipv6->proto == IPPROTO_ICMPV6
1867 && !(ipv6->invflags & IP6T_INV_PROTO)
1868 && matchsize == IP6T_ALIGN(sizeof(struct ip6t_icmp))
1869 && !(icmpinfo->invflags & ~IP6T_ICMP_INV);
1870}
1871
1872/* The built-in targets: standard (NULL) and error. */
1873static struct ip6t_target ip6t_standard_target = {
1874 .name = IP6T_STANDARD_TARGET,
1875};
1876
1877static struct ip6t_target ip6t_error_target = {
1878 .name = IP6T_ERROR_TARGET,
1879 .target = ip6t_error,
1880};
1881
1882static struct nf_sockopt_ops ip6t_sockopts = {
1883 .pf = PF_INET6,
1884 .set_optmin = IP6T_BASE_CTL,
1885 .set_optmax = IP6T_SO_SET_MAX+1,
1886 .set = do_ip6t_set_ctl,
1887 .get_optmin = IP6T_BASE_CTL,
1888 .get_optmax = IP6T_SO_GET_MAX+1,
1889 .get = do_ip6t_get_ctl,
1890};
1891
1892static struct ip6t_match tcp_matchstruct = {
1893 .name = "tcp",
1894 .match = &tcp_match,
1895 .checkentry = &tcp_checkentry,
1896};
1897
1898static struct ip6t_match udp_matchstruct = {
1899 .name = "udp",
1900 .match = &udp_match,
1901 .checkentry = &udp_checkentry,
1902};
1903
1904static struct ip6t_match icmp6_matchstruct = {
1905 .name = "icmp6",
1906 .match = &icmp6_match,
1907 .checkentry = &icmp6_checkentry,
1908};
1909
1910#ifdef CONFIG_PROC_FS
1911static inline int print_name(const char *i,
1912 off_t start_offset, char *buffer, int length,
1913 off_t *pos, unsigned int *count)
1914{
1915 if ((*count)++ >= start_offset) {
1916 unsigned int namelen;
1917
1918 namelen = sprintf(buffer + *pos, "%s\n",
1919 i + sizeof(struct list_head));
1920 if (*pos + namelen > length) {
1921 /* Stop iterating */
1922 return 1;
1923 }
1924 *pos += namelen;
1925 }
1926 return 0;
1927}
1928
1929static inline int print_target(const struct ip6t_target *t,
1930 off_t start_offset, char *buffer, int length,
1931 off_t *pos, unsigned int *count)
1932{
1933 if (t == &ip6t_standard_target || t == &ip6t_error_target)
1934 return 0;
1935 return print_name((char *)t, start_offset, buffer, length, pos, count);
1936}
1937
1938static int ip6t_get_tables(char *buffer, char **start, off_t offset, int length)
1939{
1940 off_t pos = 0;
1941 unsigned int count = 0;
1942
1943 if (down_interruptible(&ip6t_mutex) != 0)
1944 return 0;
1945
1946 LIST_FIND(&ip6t_tables, print_name, char *,
1947 offset, buffer, length, &pos, &count);
1948
1949 up(&ip6t_mutex);
1950
1951 /* `start' hack - see fs/proc/generic.c line ~105 */
1952 *start=(char *)((unsigned long)count-offset);
1953 return pos;
1954}
1955
1956static int ip6t_get_targets(char *buffer, char **start, off_t offset, int length)
1957{
1958 off_t pos = 0;
1959 unsigned int count = 0;
1960
1961 if (down_interruptible(&ip6t_mutex) != 0)
1962 return 0;
1963
1964 LIST_FIND(&ip6t_target, print_target, struct ip6t_target *,
1965 offset, buffer, length, &pos, &count);
1966
1967 up(&ip6t_mutex);
1968
1969 *start = (char *)((unsigned long)count - offset);
1970 return pos;
1971}
1972
1973static int ip6t_get_matches(char *buffer, char **start, off_t offset, int length)
1974{
1975 off_t pos = 0;
1976 unsigned int count = 0;
1977
1978 if (down_interruptible(&ip6t_mutex) != 0)
1979 return 0;
1980
1981 LIST_FIND(&ip6t_match, print_name, char *,
1982 offset, buffer, length, &pos, &count);
1983
1984 up(&ip6t_mutex);
1985
1986 *start = (char *)((unsigned long)count - offset);
1987 return pos;
1988}
1989
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001990static const struct { char *name; get_info_t *get_info; } ip6t_proc_entry[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{ { "ip6_tables_names", ip6t_get_tables },
1992 { "ip6_tables_targets", ip6t_get_targets },
1993 { "ip6_tables_matches", ip6t_get_matches },
1994 { NULL, NULL} };
1995#endif /*CONFIG_PROC_FS*/
1996
1997static int __init init(void)
1998{
1999 int ret;
2000
2001 /* Noone else will be downing sem now, so we won't sleep */
2002 down(&ip6t_mutex);
2003 list_append(&ip6t_target, &ip6t_standard_target);
2004 list_append(&ip6t_target, &ip6t_error_target);
2005 list_append(&ip6t_match, &tcp_matchstruct);
2006 list_append(&ip6t_match, &udp_matchstruct);
2007 list_append(&ip6t_match, &icmp6_matchstruct);
2008 up(&ip6t_mutex);
2009
2010 /* Register setsockopt */
2011 ret = nf_register_sockopt(&ip6t_sockopts);
2012 if (ret < 0) {
2013 duprintf("Unable to register sockopts.\n");
2014 return ret;
2015 }
2016
2017#ifdef CONFIG_PROC_FS
2018 {
2019 struct proc_dir_entry *proc;
2020 int i;
2021
2022 for (i = 0; ip6t_proc_entry[i].name; i++) {
2023 proc = proc_net_create(ip6t_proc_entry[i].name, 0,
2024 ip6t_proc_entry[i].get_info);
2025 if (!proc) {
2026 while (--i >= 0)
2027 proc_net_remove(ip6t_proc_entry[i].name);
2028 nf_unregister_sockopt(&ip6t_sockopts);
2029 return -ENOMEM;
2030 }
2031 proc->owner = THIS_MODULE;
2032 }
2033 }
2034#endif
2035
2036 printk("ip6_tables: (C) 2000-2002 Netfilter core team\n");
2037 return 0;
2038}
2039
2040static void __exit fini(void)
2041{
2042 nf_unregister_sockopt(&ip6t_sockopts);
2043#ifdef CONFIG_PROC_FS
2044 {
2045 int i;
2046 for (i = 0; ip6t_proc_entry[i].name; i++)
2047 proc_net_remove(ip6t_proc_entry[i].name);
2048 }
2049#endif
2050}
2051
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07002052/*
Patrick McHardyb777e0c2006-01-05 12:21:16 -08002053 * find the offset to specified header or the protocol number of last header
2054 * if target < 0. "last header" is transport protocol header, ESP, or
2055 * "No next header".
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07002056 *
Patrick McHardyb777e0c2006-01-05 12:21:16 -08002057 * If target header is found, its offset is set in *offset and return protocol
2058 * number. Otherwise, return -1.
2059 *
2060 * Note that non-1st fragment is special case that "the protocol number
2061 * of last header" is "next header" field in Fragment header. In this case,
2062 * *offset is meaningless and fragment offset is stored in *fragoff if fragoff
2063 * isn't NULL.
2064 *
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07002065 */
Patrick McHardyb777e0c2006-01-05 12:21:16 -08002066int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
2067 int target, unsigned short *fragoff)
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07002068{
2069 unsigned int start = (u8*)(skb->nh.ipv6h + 1) - skb->data;
2070 u8 nexthdr = skb->nh.ipv6h->nexthdr;
2071 unsigned int len = skb->len - start;
2072
Patrick McHardyb777e0c2006-01-05 12:21:16 -08002073 if (fragoff)
2074 *fragoff = 0;
2075
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07002076 while (nexthdr != target) {
2077 struct ipv6_opt_hdr _hdr, *hp;
2078 unsigned int hdrlen;
2079
Patrick McHardyb777e0c2006-01-05 12:21:16 -08002080 if ((!ipv6_ext_hdr(nexthdr)) || nexthdr == NEXTHDR_NONE) {
2081 if (target < 0)
2082 break;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07002083 return -1;
Patrick McHardyb777e0c2006-01-05 12:21:16 -08002084 }
2085
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07002086 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
2087 if (hp == NULL)
2088 return -1;
2089 if (nexthdr == NEXTHDR_FRAGMENT) {
2090 unsigned short _frag_off, *fp;
2091 fp = skb_header_pointer(skb,
2092 start+offsetof(struct frag_hdr,
2093 frag_off),
2094 sizeof(_frag_off),
2095 &_frag_off);
2096 if (fp == NULL)
2097 return -1;
2098
Patrick McHardyb777e0c2006-01-05 12:21:16 -08002099 _frag_off = ntohs(*fp) & ~0x7;
2100 if (_frag_off) {
2101 if (target < 0 &&
2102 ((!ipv6_ext_hdr(hp->nexthdr)) ||
2103 nexthdr == NEXTHDR_NONE)) {
2104 if (fragoff)
2105 *fragoff = _frag_off;
2106 return hp->nexthdr;
2107 }
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07002108 return -1;
Patrick McHardyb777e0c2006-01-05 12:21:16 -08002109 }
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07002110 hdrlen = 8;
2111 } else if (nexthdr == NEXTHDR_AUTH)
2112 hdrlen = (hp->hdrlen + 2) << 2;
2113 else
2114 hdrlen = ipv6_optlen(hp);
2115
2116 nexthdr = hp->nexthdr;
2117 len -= hdrlen;
2118 start += hdrlen;
2119 }
2120
2121 *offset = start;
Patrick McHardyb777e0c2006-01-05 12:21:16 -08002122 return nexthdr;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07002123}
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125EXPORT_SYMBOL(ip6t_register_table);
2126EXPORT_SYMBOL(ip6t_unregister_table);
2127EXPORT_SYMBOL(ip6t_do_table);
2128EXPORT_SYMBOL(ip6t_register_match);
2129EXPORT_SYMBOL(ip6t_unregister_match);
2130EXPORT_SYMBOL(ip6t_register_target);
2131EXPORT_SYMBOL(ip6t_unregister_target);
2132EXPORT_SYMBOL(ip6t_ext_hdr);
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07002133EXPORT_SYMBOL(ipv6_find_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135module_init(init);
2136module_exit(fini);