blob: 7fe4d29708cb9050a8d6260100d55e1d81920269 [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
Randy Dunlap4fc268d2006-01-11 12:17:47 -080011
12#include <linux/capability.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020013#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/skbuff.h>
15#include <linux/kmod.h>
16#include <linux/vmalloc.h>
17#include <linux/netdevice.h>
18#include <linux/module.h>
Randy Dunlap4bdbf6c2006-07-03 19:47:27 -070019#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/icmpv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <net/ipv6.h>
22#include <asm/uaccess.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -080023#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/proc_fs.h>
David S. Millerc8923c62005-10-13 14:41:23 -070025#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <linux/netfilter_ipv6/ip6_tables.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080028#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
32MODULE_DESCRIPTION("IPv6 packet filter");
33
34#define IPV6_HDR_LEN (sizeof(struct ipv6hdr))
35#define IPV6_OPTHDR_LEN (sizeof(struct ipv6_opt_hdr))
36
37/*#define DEBUG_IP_FIREWALL*/
38/*#define DEBUG_ALLOW_ALL*/ /* Useful for remote debugging */
39/*#define DEBUG_IP_FIREWALL_USER*/
40
41#ifdef DEBUG_IP_FIREWALL
42#define dprintf(format, args...) printk(format , ## args)
43#else
44#define dprintf(format, args...)
45#endif
46
47#ifdef DEBUG_IP_FIREWALL_USER
48#define duprintf(format, args...) printk(format , ## args)
49#else
50#define duprintf(format, args...)
51#endif
52
53#ifdef CONFIG_NETFILTER_DEBUG
54#define IP_NF_ASSERT(x) \
55do { \
56 if (!(x)) \
57 printk("IP_NF_ASSERT: %s:%s:%u\n", \
58 __FUNCTION__, __FILE__, __LINE__); \
59} while(0)
60#else
61#define IP_NF_ASSERT(x)
62#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#if 0
65/* All the better to debug you with... */
66#define static
67#define inline
68#endif
69
Harald Welte6b7d31f2005-10-26 09:34:24 +020070/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 We keep a set of rules for each CPU, so we can avoid write-locking
Harald Welte6b7d31f2005-10-26 09:34:24 +020072 them in the softirq when updating the counters and therefore
73 only need to read-lock in the softirq; doing a write_lock_bh() in user
74 context stops packets coming through and allows user context to read
75 the counters or update the rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 Hence the start of any table is given by get_table() below. */
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#if 0
80#define down(x) do { printk("DOWN:%u:" #x "\n", __LINE__); down(x); } while(0)
81#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; })
82#define up(x) do { printk("UP:%u:" #x "\n", __LINE__); up(x); } while(0)
83#endif
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Check for an extension */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090086int
Linus Torvalds1da177e2005-04-16 15:20:36 -070087ip6t_ext_hdr(u8 nexthdr)
88{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090089 return ( (nexthdr == IPPROTO_HOPOPTS) ||
90 (nexthdr == IPPROTO_ROUTING) ||
91 (nexthdr == IPPROTO_FRAGMENT) ||
92 (nexthdr == IPPROTO_ESP) ||
93 (nexthdr == IPPROTO_AH) ||
94 (nexthdr == IPPROTO_NONE) ||
95 (nexthdr == IPPROTO_DSTOPTS) );
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98/* Returns whether matches rule or not. */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070099static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100ip6_packet_match(const struct sk_buff *skb,
101 const char *indev,
102 const char *outdev,
103 const struct ip6t_ip6 *ip6info,
104 unsigned int *protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700105 int *fragoff, bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 size_t i;
108 unsigned long ret;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700109 const struct ipv6hdr *ipv6 = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111#define FWINV(bool,invflg) ((bool) ^ !!(ip6info->invflags & invflg))
112
Patrick McHardyf2ffd9e2006-03-20 18:03:16 -0800113 if (FWINV(ipv6_masked_addr_cmp(&ipv6->saddr, &ip6info->smsk,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900114 &ip6info->src), IP6T_INV_SRCIP)
Patrick McHardyf2ffd9e2006-03-20 18:03:16 -0800115 || FWINV(ipv6_masked_addr_cmp(&ipv6->daddr, &ip6info->dmsk,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900116 &ip6info->dst), IP6T_INV_DSTIP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 dprintf("Source or dest mismatch.\n");
118/*
119 dprintf("SRC: %u. Mask: %u. Target: %u.%s\n", ip->saddr,
120 ipinfo->smsk.s_addr, ipinfo->src.s_addr,
121 ipinfo->invflags & IP6T_INV_SRCIP ? " (INV)" : "");
122 dprintf("DST: %u. Mask: %u. Target: %u.%s\n", ip->daddr,
123 ipinfo->dmsk.s_addr, ipinfo->dst.s_addr,
124 ipinfo->invflags & IP6T_INV_DSTIP ? " (INV)" : "");*/
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700125 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127
128 /* Look for ifname matches; this should unroll nicely. */
129 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
130 ret |= (((const unsigned long *)indev)[i]
131 ^ ((const unsigned long *)ip6info->iniface)[i])
132 & ((const unsigned long *)ip6info->iniface_mask)[i];
133 }
134
135 if (FWINV(ret != 0, IP6T_INV_VIA_IN)) {
136 dprintf("VIA in mismatch (%s vs %s).%s\n",
137 indev, ip6info->iniface,
138 ip6info->invflags&IP6T_INV_VIA_IN ?" (INV)":"");
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700139 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141
142 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
143 ret |= (((const unsigned long *)outdev)[i]
144 ^ ((const unsigned long *)ip6info->outiface)[i])
145 & ((const unsigned long *)ip6info->outiface_mask)[i];
146 }
147
148 if (FWINV(ret != 0, IP6T_INV_VIA_OUT)) {
149 dprintf("VIA out mismatch (%s vs %s).%s\n",
150 outdev, ip6info->outiface,
151 ip6info->invflags&IP6T_INV_VIA_OUT ?" (INV)":"");
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700152 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
155/* ... might want to do something with class and flowlabel here ... */
156
157 /* look for the desired protocol header */
158 if((ip6info->flags & IP6T_F_PROTO)) {
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800159 int protohdr;
160 unsigned short _frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800162 protohdr = ipv6_find_hdr(skb, protoff, -1, &_frag_off);
Patrick McHardy51d8b1a2006-10-24 16:14:04 -0700163 if (protohdr < 0) {
164 if (_frag_off == 0)
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700165 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700166 return false;
Patrick McHardy51d8b1a2006-10-24 16:14:04 -0700167 }
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800168 *fragoff = _frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 dprintf("Packet protocol %hi ?= %s%hi.\n",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900171 protohdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ip6info->invflags & IP6T_INV_PROTO ? "!":"",
173 ip6info->proto);
174
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800175 if (ip6info->proto == protohdr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if(ip6info->invflags & IP6T_INV_PROTO) {
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700177 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700179 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181
182 /* We need match for the '-p all', too! */
183 if ((ip6info->proto != 0) &&
184 !(ip6info->invflags & IP6T_INV_PROTO))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700185 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700187 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190/* should be ip6 safe */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700191static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192ip6_checkentry(const struct ip6t_ip6 *ipv6)
193{
194 if (ipv6->flags & ~IP6T_F_MASK) {
195 duprintf("Unknown flag bits set: %08X\n",
196 ipv6->flags & ~IP6T_F_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700197 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 if (ipv6->invflags & ~IP6T_INV_MASK) {
200 duprintf("Unknown invflag bits set: %08X\n",
201 ipv6->invflags & ~IP6T_INV_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700202 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700204 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207static unsigned int
208ip6t_error(struct sk_buff **pskb,
209 const struct net_device *in,
210 const struct net_device *out,
211 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -0800212 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700213 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 if (net_ratelimit())
216 printk("ip6_tables: error: `%s'\n", (char *)targinfo);
217
218 return NF_DROP;
219}
220
221static inline
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700222bool do_match(struct ip6t_entry_match *m,
223 const struct sk_buff *skb,
224 const struct net_device *in,
225 const struct net_device *out,
226 int offset,
227 unsigned int protoff,
228 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 /* Stop iteration if it doesn't match */
Patrick McHardy1c524832006-03-20 18:02:15 -0800231 if (!m->u.kernel.match->match(skb, in, out, m->u.kernel.match, m->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 offset, protoff, hotdrop))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700233 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 else
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700235 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238static inline struct ip6t_entry *
239get_entry(void *base, unsigned int offset)
240{
241 return (struct ip6t_entry *)(base + offset);
242}
243
244/* Returns one of the generic firewall policies, like NF_ACCEPT. */
245unsigned int
246ip6t_do_table(struct sk_buff **pskb,
247 unsigned int hook,
248 const struct net_device *in,
249 const struct net_device *out,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700250 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Harald Welte6b7d31f2005-10-26 09:34:24 +0200252 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int offset = 0;
254 unsigned int protoff = 0;
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700255 bool hotdrop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* Initializing verdict to NF_DROP keeps gcc happy. */
257 unsigned int verdict = NF_DROP;
258 const char *indev, *outdev;
259 void *table_base;
260 struct ip6t_entry *e, *back;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800261 struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /* Initialization */
264 indev = in ? in->name : nulldevname;
265 outdev = out ? out->name : nulldevname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /* We handle fragments by dealing with the first fragment as
267 * if it was a normal packet. All other fragments are treated
268 * normally, except that they will NEVER match rules that ask
269 * things we don't know, ie. tcp syn flag or ports). If the
270 * rule is also a fragment-specific rule, non-fragments won't
271 * match it. */
272
273 read_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800274 private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800276 table_base = (void *)private->entries[smp_processor_id()];
277 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* For return from builtin chain */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800280 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 do {
283 IP_NF_ASSERT(e);
284 IP_NF_ASSERT(back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (ip6_packet_match(*pskb, indev, outdev, &e->ipv6,
Patrick McHardy51d8b1a2006-10-24 16:14:04 -0700286 &protoff, &offset, &hotdrop)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 struct ip6t_entry_target *t;
288
289 if (IP6T_MATCH_ITERATE(e, do_match,
290 *pskb, in, out,
291 offset, protoff, &hotdrop) != 0)
292 goto no_match;
293
294 ADD_COUNTER(e->counters,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700295 ntohs(ipv6_hdr(*pskb)->payload_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 + IPV6_HDR_LEN,
297 1);
298
299 t = ip6t_get_target(e);
300 IP_NF_ASSERT(t->u.kernel.target);
301 /* Standard target? */
302 if (!t->u.kernel.target->target) {
303 int v;
304
305 v = ((struct ip6t_standard_target *)t)->verdict;
306 if (v < 0) {
307 /* Pop from stack? */
308 if (v != IP6T_RETURN) {
309 verdict = (unsigned)(-v) - 1;
310 break;
311 }
312 e = back;
313 back = get_entry(table_base,
314 back->comefrom);
315 continue;
316 }
Patrick McHardy05465342005-08-21 23:31:43 -0700317 if (table_base + v != (void *)e + e->next_offset
318 && !(e->ipv6.flags & IP6T_F_GOTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* Save old back ptr in next entry */
320 struct ip6t_entry *next
321 = (void *)e + e->next_offset;
322 next->comefrom
323 = (void *)back - table_base;
324 /* set back pointer to next entry */
325 back = next;
326 }
327
328 e = get_entry(table_base, v);
329 } else {
330 /* Targets which reenter must return
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900331 abs. verdicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332#ifdef CONFIG_NETFILTER_DEBUG
333 ((struct ip6t_entry *)table_base)->comefrom
334 = 0xeeeeeeec;
335#endif
336 verdict = t->u.kernel.target->target(pskb,
337 in, out,
338 hook,
Patrick McHardy1c524832006-03-20 18:02:15 -0800339 t->u.kernel.target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700340 t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342#ifdef CONFIG_NETFILTER_DEBUG
343 if (((struct ip6t_entry *)table_base)->comefrom
344 != 0xeeeeeeec
345 && verdict == IP6T_CONTINUE) {
346 printk("Target %s reentered!\n",
347 t->u.kernel.target->name);
348 verdict = NF_DROP;
349 }
350 ((struct ip6t_entry *)table_base)->comefrom
351 = 0x57acc001;
352#endif
353 if (verdict == IP6T_CONTINUE)
354 e = (void *)e + e->next_offset;
355 else
356 /* Verdict */
357 break;
358 }
359 } else {
360
361 no_match:
362 e = (void *)e + e->next_offset;
363 }
364 } while (!hotdrop);
365
366#ifdef CONFIG_NETFILTER_DEBUG
Randy Dunlap4bdbf6c2006-07-03 19:47:27 -0700367 ((struct ip6t_entry *)table_base)->comefrom = NETFILTER_LINK_POISON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368#endif
369 read_unlock_bh(&table->lock);
370
371#ifdef DEBUG_ALLOW_ALL
372 return NF_ACCEPT;
373#else
374 if (hotdrop)
375 return NF_DROP;
376 else return verdict;
377#endif
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/* All zeroes == unconditional rule. */
381static inline int
382unconditional(const struct ip6t_ip6 *ipv6)
383{
384 unsigned int i;
385
386 for (i = 0; i < sizeof(*ipv6); i++)
387 if (((char *)ipv6)[i])
388 break;
389
390 return (i == sizeof(*ipv6));
391}
392
393/* Figures out from what hook each rule can be called: returns 0 if
394 there are loops. Puts hook bitmask in comefrom. */
395static int
Harald Welte2e4e6a12006-01-12 13:30:04 -0800396mark_source_chains(struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800397 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 unsigned int hook;
400
401 /* No recursion; use packet counter to save back ptrs (reset
402 to 0 as we leave), and comefrom to save source hook bitmask */
403 for (hook = 0; hook < NF_IP6_NUMHOOKS; hook++) {
404 unsigned int pos = newinfo->hook_entry[hook];
405 struct ip6t_entry *e
Eric Dumazet31836062005-12-13 23:13:48 -0800406 = (struct ip6t_entry *)(entry0 + pos);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800407 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (!(valid_hooks & (1 << hook)))
410 continue;
411
412 /* Set initial back pointer. */
413 e->counters.pcnt = pos;
414
415 for (;;) {
416 struct ip6t_standard_target *t
417 = (void *)ip6t_get_target(e);
418
419 if (e->comefrom & (1 << NF_IP6_NUMHOOKS)) {
420 printk("iptables: loop hook %u pos %u %08X.\n",
421 hook, pos, e->comefrom);
422 return 0;
423 }
424 e->comefrom
425 |= ((1 << hook) | (1 << NF_IP6_NUMHOOKS));
426
427 /* Unconditional return/END. */
Al Viroe1b4b9f2006-12-12 00:29:52 -0800428 if ((e->target_offset == sizeof(struct ip6t_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 && (strcmp(t->target.u.user.name,
430 IP6T_STANDARD_TARGET) == 0)
431 && t->verdict < 0
Al Viroe1b4b9f2006-12-12 00:29:52 -0800432 && unconditional(&e->ipv6)) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 unsigned int oldpos, size;
434
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800435 if (t->verdict < -NF_MAX_VERDICT - 1) {
436 duprintf("mark_source_chains: bad "
437 "negative verdict (%i)\n",
438 t->verdict);
439 return 0;
440 }
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* Return: backtrack through the last
443 big jump. */
444 do {
445 e->comefrom ^= (1<<NF_IP6_NUMHOOKS);
446#ifdef DEBUG_IP_FIREWALL_USER
447 if (e->comefrom
448 & (1 << NF_IP6_NUMHOOKS)) {
449 duprintf("Back unset "
450 "on hook %u "
451 "rule %u\n",
452 hook, pos);
453 }
454#endif
455 oldpos = pos;
456 pos = e->counters.pcnt;
457 e->counters.pcnt = 0;
458
459 /* We're at the start. */
460 if (pos == oldpos)
461 goto next;
462
463 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800464 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 } while (oldpos == pos + e->next_offset);
466
467 /* Move along one */
468 size = e->next_offset;
469 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800470 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 e->counters.pcnt = pos;
472 pos += size;
473 } else {
474 int newpos = t->verdict;
475
476 if (strcmp(t->target.u.user.name,
477 IP6T_STANDARD_TARGET) == 0
478 && newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800479 if (newpos > newinfo->size -
480 sizeof(struct ip6t_entry)) {
481 duprintf("mark_source_chains: "
482 "bad verdict (%i)\n",
483 newpos);
484 return 0;
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /* This a jump; chase it. */
487 duprintf("Jump rule %u -> %u\n",
488 pos, newpos);
489 } else {
490 /* ... this is a fallthru */
491 newpos = pos + e->next_offset;
492 }
493 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800494 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 e->counters.pcnt = pos;
496 pos = newpos;
497 }
498 }
499 next:
500 duprintf("Finished chain %u\n", hook);
501 }
502 return 1;
503}
504
505static inline int
506cleanup_match(struct ip6t_entry_match *m, unsigned int *i)
507{
508 if (i && (*i)-- == 0)
509 return 1;
510
511 if (m->u.kernel.match->destroy)
Patrick McHardyefa74162006-08-22 00:36:37 -0700512 m->u.kernel.match->destroy(m->u.kernel.match, m->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 module_put(m->u.kernel.match->me);
514 return 0;
515}
516
517static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518check_match(struct ip6t_entry_match *m,
519 const char *name,
520 const struct ip6t_ip6 *ipv6,
521 unsigned int hookmask,
522 unsigned int *i)
523{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800524 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800525 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Harald Welte2e4e6a12006-01-12 13:30:04 -0800527 match = try_then_request_module(xt_find_match(AF_INET6, m->u.user.name,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900528 m->u.user.revision),
Harald Welte6b7d31f2005-10-26 09:34:24 +0200529 "ip6t_%s", m->u.user.name);
530 if (IS_ERR(match) || !match) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900531 duprintf("check_match: `%s' not found\n", m->u.user.name);
Harald Welte6b7d31f2005-10-26 09:34:24 +0200532 return match ? PTR_ERR(match) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 m->u.kernel.match = match;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800536 ret = xt_check_match(match, AF_INET6, m->u.match_size - sizeof(*m),
537 name, hookmask, ipv6->proto,
538 ipv6->invflags & IP6T_INV_PROTO);
539 if (ret)
540 goto err;
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (m->u.kernel.match->checkentry
Patrick McHardy1c524832006-03-20 18:02:15 -0800543 && !m->u.kernel.match->checkentry(name, ipv6, match, m->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 hookmask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 duprintf("ip_tables: check failed for `%s'.\n",
546 m->u.kernel.match->name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800547 ret = -EINVAL;
548 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550
551 (*i)++;
552 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800553err:
554 module_put(m->u.kernel.match->me);
555 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800558static struct xt_target ip6t_standard_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560static inline int
561check_entry(struct ip6t_entry *e, const char *name, unsigned int size,
562 unsigned int *i)
563{
564 struct ip6t_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800565 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int ret;
567 unsigned int j;
568
569 if (!ip6_checkentry(&e->ipv6)) {
570 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
571 return -EINVAL;
572 }
573
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800574 if (e->target_offset + sizeof(struct ip6t_entry_target) >
575 e->next_offset)
576 return -EINVAL;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 j = 0;
579 ret = IP6T_MATCH_ITERATE(e, check_match, name, &e->ipv6, e->comefrom, &j);
580 if (ret != 0)
581 goto cleanup_matches;
582
583 t = ip6t_get_target(e);
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800584 ret = -EINVAL;
585 if (e->target_offset + t->u.target_size > e->next_offset)
586 goto cleanup_matches;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800587 target = try_then_request_module(xt_find_target(AF_INET6,
588 t->u.user.name,
589 t->u.user.revision),
Harald Welte6b7d31f2005-10-26 09:34:24 +0200590 "ip6t_%s", t->u.user.name);
591 if (IS_ERR(target) || !target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 duprintf("check_entry: `%s' not found\n", t->u.user.name);
Harald Welte6b7d31f2005-10-26 09:34:24 +0200593 ret = target ? PTR_ERR(target) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 goto cleanup_matches;
595 }
596 t->u.kernel.target = target;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200597
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800598 ret = xt_check_target(target, AF_INET6, t->u.target_size - sizeof(*t),
599 name, e->comefrom, e->ipv6.proto,
600 e->ipv6.invflags & IP6T_INV_PROTO);
601 if (ret)
602 goto err;
603
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800604 if (t->u.kernel.target->checkentry
Patrick McHardy1c524832006-03-20 18:02:15 -0800605 && !t->u.kernel.target->checkentry(name, e, target, t->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 e->comefrom)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 duprintf("ip_tables: check failed for `%s'.\n",
608 t->u.kernel.target->name);
609 ret = -EINVAL;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800610 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
613 (*i)++;
614 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800615 err:
616 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 cleanup_matches:
618 IP6T_MATCH_ITERATE(e, cleanup_match, &j);
619 return ret;
620}
621
622static inline int
623check_entry_size_and_hooks(struct ip6t_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800624 struct xt_table_info *newinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 unsigned char *base,
626 unsigned char *limit,
627 const unsigned int *hook_entries,
628 const unsigned int *underflows,
629 unsigned int *i)
630{
631 unsigned int h;
632
633 if ((unsigned long)e % __alignof__(struct ip6t_entry) != 0
634 || (unsigned char *)e + sizeof(struct ip6t_entry) >= limit) {
635 duprintf("Bad offset %p\n", e);
636 return -EINVAL;
637 }
638
639 if (e->next_offset
640 < sizeof(struct ip6t_entry) + sizeof(struct ip6t_entry_target)) {
641 duprintf("checking: element %p size %u\n",
642 e, e->next_offset);
643 return -EINVAL;
644 }
645
646 /* Check hooks & underflows */
647 for (h = 0; h < NF_IP6_NUMHOOKS; h++) {
648 if ((unsigned char *)e - base == hook_entries[h])
649 newinfo->hook_entry[h] = hook_entries[h];
650 if ((unsigned char *)e - base == underflows[h])
651 newinfo->underflow[h] = underflows[h];
652 }
653
654 /* FIXME: underflows must be unconditional, standard verdicts
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900655 < 0 (not IP6T_RETURN). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800658 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 e->comefrom = 0;
660
661 (*i)++;
662 return 0;
663}
664
665static inline int
666cleanup_entry(struct ip6t_entry *e, unsigned int *i)
667{
668 struct ip6t_entry_target *t;
669
670 if (i && (*i)-- == 0)
671 return 1;
672
673 /* Cleanup all matches */
674 IP6T_MATCH_ITERATE(e, cleanup_match, NULL);
675 t = ip6t_get_target(e);
676 if (t->u.kernel.target->destroy)
Patrick McHardyefa74162006-08-22 00:36:37 -0700677 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 module_put(t->u.kernel.target->me);
679 return 0;
680}
681
682/* Checks and translates the user-supplied table segment (held in
683 newinfo) */
684static int
685translate_table(const char *name,
686 unsigned int valid_hooks,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800687 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800688 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 unsigned int size,
690 unsigned int number,
691 const unsigned int *hook_entries,
692 const unsigned int *underflows)
693{
694 unsigned int i;
695 int ret;
696
697 newinfo->size = size;
698 newinfo->number = number;
699
700 /* Init all hooks to impossible value. */
701 for (i = 0; i < NF_IP6_NUMHOOKS; i++) {
702 newinfo->hook_entry[i] = 0xFFFFFFFF;
703 newinfo->underflow[i] = 0xFFFFFFFF;
704 }
705
706 duprintf("translate_table: size %u\n", newinfo->size);
707 i = 0;
708 /* Walk through entries, checking offsets. */
Eric Dumazet31836062005-12-13 23:13:48 -0800709 ret = IP6T_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 check_entry_size_and_hooks,
711 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800712 entry0,
713 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 hook_entries, underflows, &i);
715 if (ret != 0)
716 return ret;
717
718 if (i != number) {
719 duprintf("translate_table: %u not %u entries\n",
720 i, number);
721 return -EINVAL;
722 }
723
724 /* Check hooks all assigned */
725 for (i = 0; i < NF_IP6_NUMHOOKS; i++) {
726 /* Only hooks which are valid */
727 if (!(valid_hooks & (1 << i)))
728 continue;
729 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
730 duprintf("Invalid hook entry %u %u\n",
731 i, hook_entries[i]);
732 return -EINVAL;
733 }
734 if (newinfo->underflow[i] == 0xFFFFFFFF) {
735 duprintf("Invalid underflow %u %u\n",
736 i, underflows[i]);
737 return -EINVAL;
738 }
739 }
740
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800741 if (!mark_source_chains(newinfo, valid_hooks, entry0))
742 return -ELOOP;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /* Finally, each sanity check must pass */
745 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800746 ret = IP6T_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 check_entry, name, size, &i);
748
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800749 if (ret != 0) {
750 IP6T_ENTRY_ITERATE(entry0, newinfo->size,
751 cleanup_entry, &i);
752 return ret;
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700756 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800757 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
758 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764/* Gets counters. */
765static inline int
766add_entry_to_counter(const struct ip6t_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800767 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 unsigned int *i)
769{
770 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
771
772 (*i)++;
773 return 0;
774}
775
Eric Dumazet31836062005-12-13 23:13:48 -0800776static inline int
777set_entry_to_counter(const struct ip6t_entry *e,
778 struct ip6t_counters total[],
779 unsigned int *i)
780{
781 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
782
783 (*i)++;
784 return 0;
785}
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800788get_counters(const struct xt_table_info *t,
789 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 unsigned int cpu;
792 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800793 unsigned int curcpu;
794
795 /* Instead of clearing (by a previous call to memset())
796 * the counters and using adds, we set the counters
797 * with data used by 'current' CPU
798 * We dont care about preemption here.
799 */
800 curcpu = raw_smp_processor_id();
801
802 i = 0;
803 IP6T_ENTRY_ITERATE(t->entries[curcpu],
804 t->size,
805 set_entry_to_counter,
806 counters,
807 &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700809 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800810 if (cpu == curcpu)
811 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800813 IP6T_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 t->size,
815 add_entry_to_counter,
816 counters,
817 &i);
818 }
819}
820
821static int
822copy_entries_to_user(unsigned int total_size,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800823 struct xt_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 void __user *userptr)
825{
826 unsigned int off, num, countersize;
827 struct ip6t_entry *e;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800828 struct xt_counters *counters;
829 struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800831 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* We need atomic snapshot of counters: rest doesn't change
834 (other than comefrom, which userspace doesn't care
835 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800836 countersize = sizeof(struct xt_counters) * private->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 counters = vmalloc(countersize);
838
839 if (counters == NULL)
840 return -ENOMEM;
841
842 /* First, sum counters... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 write_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800844 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 write_unlock_bh(&table->lock);
846
Eric Dumazet31836062005-12-13 23:13:48 -0800847 /* choose the copy that is on ourc node/cpu */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800848 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800849 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 ret = -EFAULT;
851 goto free_counters;
852 }
853
854 /* FIXME: use iterator macros --RR */
855 /* ... then go back and fix counters and names */
856 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
857 unsigned int i;
858 struct ip6t_entry_match *m;
859 struct ip6t_entry_target *t;
860
Eric Dumazet31836062005-12-13 23:13:48 -0800861 e = (struct ip6t_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (copy_to_user(userptr + off
863 + offsetof(struct ip6t_entry, counters),
864 &counters[num],
865 sizeof(counters[num])) != 0) {
866 ret = -EFAULT;
867 goto free_counters;
868 }
869
870 for (i = sizeof(struct ip6t_entry);
871 i < e->target_offset;
872 i += m->u.match_size) {
873 m = (void *)e + i;
874
875 if (copy_to_user(userptr + off + i
876 + offsetof(struct ip6t_entry_match,
877 u.user.name),
878 m->u.kernel.match->name,
879 strlen(m->u.kernel.match->name)+1)
880 != 0) {
881 ret = -EFAULT;
882 goto free_counters;
883 }
884 }
885
886 t = ip6t_get_target(e);
887 if (copy_to_user(userptr + off + e->target_offset
888 + offsetof(struct ip6t_entry_target,
889 u.user.name),
890 t->u.kernel.target->name,
891 strlen(t->u.kernel.target->name)+1) != 0) {
892 ret = -EFAULT;
893 goto free_counters;
894 }
895 }
896
897 free_counters:
898 vfree(counters);
899 return ret;
900}
901
902static int
903get_entries(const struct ip6t_get_entries *entries,
904 struct ip6t_get_entries __user *uptr)
905{
906 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800907 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Harald Welte2e4e6a12006-01-12 13:30:04 -0800909 t = xt_find_table_lock(AF_INET6, entries->name);
Harald Welte6b7d31f2005-10-26 09:34:24 +0200910 if (t && !IS_ERR(t)) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800911 struct xt_table_info *private = t->private;
912 duprintf("t->private->number = %u\n", private->number);
913 if (entries->size == private->size)
914 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 t, uptr->entrytable);
916 else {
917 duprintf("get_entries: I've got %u not %u!\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -0800918 private->size, entries->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 ret = -EINVAL;
920 }
Harald Welte6b7d31f2005-10-26 09:34:24 +0200921 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800922 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 } else
Harald Welte6b7d31f2005-10-26 09:34:24 +0200924 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 return ret;
927}
928
929static int
930do_replace(void __user *user, unsigned int len)
931{
932 int ret;
933 struct ip6t_replace tmp;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800934 struct xt_table *t;
935 struct xt_table_info *newinfo, *oldinfo;
936 struct xt_counters *counters;
Eric Dumazet31836062005-12-13 23:13:48 -0800937 void *loc_cpu_entry, *loc_cpu_old_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
940 return -EFAULT;
941
Kirill Korotaevee4bb812006-02-04 02:16:56 -0800942 /* overflow check */
943 if (tmp.size >= (INT_MAX - sizeof(struct xt_table_info)) / NR_CPUS -
944 SMP_CACHE_BYTES)
945 return -ENOMEM;
946 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
947 return -ENOMEM;
948
Harald Welte2e4e6a12006-01-12 13:30:04 -0800949 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (!newinfo)
951 return -ENOMEM;
952
Eric Dumazet31836062005-12-13 23:13:48 -0800953 /* choose the copy that is on our node/cpu */
954 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
955 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 tmp.size) != 0) {
957 ret = -EFAULT;
958 goto free_newinfo;
959 }
960
Harald Welte2e4e6a12006-01-12 13:30:04 -0800961 counters = vmalloc(tmp.num_counters * sizeof(struct xt_counters));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (!counters) {
963 ret = -ENOMEM;
964 goto free_newinfo;
965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -0800968 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 tmp.hook_entry, tmp.underflow);
970 if (ret != 0)
971 goto free_newinfo_counters;
972
973 duprintf("ip_tables: Translated table\n");
974
Harald Welte2e4e6a12006-01-12 13:30:04 -0800975 t = try_then_request_module(xt_find_table_lock(AF_INET6, tmp.name),
Harald Welte6b7d31f2005-10-26 09:34:24 +0200976 "ip6table_%s", tmp.name);
977 if (!t || IS_ERR(t)) {
978 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 goto free_newinfo_counters_untrans;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 /* You lied! */
983 if (tmp.valid_hooks != t->valid_hooks) {
984 duprintf("Valid hook crap: %08X vs %08X\n",
985 tmp.valid_hooks, t->valid_hooks);
986 ret = -EINVAL;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200987 goto put_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989
Harald Welte2e4e6a12006-01-12 13:30:04 -0800990 oldinfo = xt_replace_table(t, tmp.num_counters, newinfo, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (!oldinfo)
992 goto put_module;
993
994 /* Update module usage count based on number of rules */
995 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
996 oldinfo->number, oldinfo->initial_entries, newinfo->number);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900997 if ((oldinfo->number > oldinfo->initial_entries) ||
998 (newinfo->number <= oldinfo->initial_entries))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 module_put(t->me);
1000 if ((oldinfo->number > oldinfo->initial_entries) &&
1001 (newinfo->number <= oldinfo->initial_entries))
1002 module_put(t->me);
1003
1004 /* Get the old counters. */
1005 get_counters(oldinfo, counters);
1006 /* Decrease module usage counts and free resource */
Eric Dumazet31836062005-12-13 23:13:48 -08001007 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1008 IP6T_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001009 xt_free_table_info(oldinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (copy_to_user(tmp.counters, counters,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001011 sizeof(struct xt_counters) * tmp.num_counters) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 ret = -EFAULT;
1013 vfree(counters);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001014 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return ret;
1016
1017 put_module:
1018 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001019 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 free_newinfo_counters_untrans:
Eric Dumazet31836062005-12-13 23:13:48 -08001021 IP6T_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 free_newinfo_counters:
1023 vfree(counters);
1024 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001025 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return ret;
1027}
1028
1029/* We're lazy, and add to the first CPU; overflow works its fey magic
1030 * and everything is OK. */
1031static inline int
1032add_counter_to_entry(struct ip6t_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001033 const struct xt_counters addme[],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 unsigned int *i)
1035{
1036#if 0
1037 duprintf("add_counter: Entry %u %lu/%lu + %lu/%lu\n",
1038 *i,
1039 (long unsigned int)e->counters.pcnt,
1040 (long unsigned int)e->counters.bcnt,
1041 (long unsigned int)addme[*i].pcnt,
1042 (long unsigned int)addme[*i].bcnt);
1043#endif
1044
1045 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1046
1047 (*i)++;
1048 return 0;
1049}
1050
1051static int
1052do_add_counters(void __user *user, unsigned int len)
1053{
1054 unsigned int i;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001055 struct xt_counters_info tmp, *paddc;
1056 struct xt_table_info *private;
1057 struct xt_table *t;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001058 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001059 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1062 return -EFAULT;
1063
Harald Welte2e4e6a12006-01-12 13:30:04 -08001064 if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return -EINVAL;
1066
1067 paddc = vmalloc(len);
1068 if (!paddc)
1069 return -ENOMEM;
1070
1071 if (copy_from_user(paddc, user, len) != 0) {
1072 ret = -EFAULT;
1073 goto free;
1074 }
1075
Harald Welte2e4e6a12006-01-12 13:30:04 -08001076 t = xt_find_table_lock(AF_INET6, tmp.name);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001077 if (!t || IS_ERR(t)) {
1078 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 goto free;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 write_lock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001083 private = t->private;
Solar Designer2c8ac662006-05-19 02:16:52 -07001084 if (private->number != tmp.num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 ret = -EINVAL;
1086 goto unlock_up_free;
1087 }
1088
1089 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001090 /* Choose the copy that is on our node */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001091 loc_cpu_entry = private->entries[smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -08001092 IP6T_ENTRY_ITERATE(loc_cpu_entry,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001093 private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 add_counter_to_entry,
1095 paddc->counters,
1096 &i);
1097 unlock_up_free:
1098 write_unlock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001099 xt_table_unlock(t);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001100 module_put(t->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 free:
1102 vfree(paddc);
1103
1104 return ret;
1105}
1106
1107static int
1108do_ip6t_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1109{
1110 int ret;
1111
1112 if (!capable(CAP_NET_ADMIN))
1113 return -EPERM;
1114
1115 switch (cmd) {
1116 case IP6T_SO_SET_REPLACE:
1117 ret = do_replace(user, len);
1118 break;
1119
1120 case IP6T_SO_SET_ADD_COUNTERS:
1121 ret = do_add_counters(user, len);
1122 break;
1123
1124 default:
1125 duprintf("do_ip6t_set_ctl: unknown request %i\n", cmd);
1126 ret = -EINVAL;
1127 }
1128
1129 return ret;
1130}
1131
1132static int
1133do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1134{
1135 int ret;
1136
1137 if (!capable(CAP_NET_ADMIN))
1138 return -EPERM;
1139
1140 switch (cmd) {
1141 case IP6T_SO_GET_INFO: {
1142 char name[IP6T_TABLE_MAXNAMELEN];
Harald Welte2e4e6a12006-01-12 13:30:04 -08001143 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 if (*len != sizeof(struct ip6t_getinfo)) {
1146 duprintf("length %u != %u\n", *len,
1147 sizeof(struct ip6t_getinfo));
1148 ret = -EINVAL;
1149 break;
1150 }
1151
1152 if (copy_from_user(name, user, sizeof(name)) != 0) {
1153 ret = -EFAULT;
1154 break;
1155 }
1156 name[IP6T_TABLE_MAXNAMELEN-1] = '\0';
Harald Welte6b7d31f2005-10-26 09:34:24 +02001157
Harald Welte2e4e6a12006-01-12 13:30:04 -08001158 t = try_then_request_module(xt_find_table_lock(AF_INET6, name),
Harald Welte6b7d31f2005-10-26 09:34:24 +02001159 "ip6table_%s", name);
1160 if (t && !IS_ERR(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 struct ip6t_getinfo info;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001162 struct xt_table_info *private = t->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 info.valid_hooks = t->valid_hooks;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001165 memcpy(info.hook_entry, private->hook_entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 sizeof(info.hook_entry));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001167 memcpy(info.underflow, private->underflow,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 sizeof(info.underflow));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001169 info.num_entries = private->number;
1170 info.size = private->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 memcpy(info.name, name, sizeof(info.name));
1172
1173 if (copy_to_user(user, &info, *len) != 0)
1174 ret = -EFAULT;
1175 else
1176 ret = 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001177 xt_table_unlock(t);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001178 module_put(t->me);
1179 } else
1180 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 break;
1183
1184 case IP6T_SO_GET_ENTRIES: {
1185 struct ip6t_get_entries get;
1186
1187 if (*len < sizeof(get)) {
1188 duprintf("get_entries: %u < %u\n", *len, sizeof(get));
1189 ret = -EINVAL;
1190 } else if (copy_from_user(&get, user, sizeof(get)) != 0) {
1191 ret = -EFAULT;
1192 } else if (*len != sizeof(struct ip6t_get_entries) + get.size) {
1193 duprintf("get_entries: %u != %u\n", *len,
1194 sizeof(struct ip6t_get_entries) + get.size);
1195 ret = -EINVAL;
1196 } else
1197 ret = get_entries(&get, user);
1198 break;
1199 }
1200
Harald Welte6b7d31f2005-10-26 09:34:24 +02001201 case IP6T_SO_GET_REVISION_MATCH:
1202 case IP6T_SO_GET_REVISION_TARGET: {
1203 struct ip6t_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001204 int target;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001205
1206 if (*len != sizeof(rev)) {
1207 ret = -EINVAL;
1208 break;
1209 }
1210 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1211 ret = -EFAULT;
1212 break;
1213 }
1214
1215 if (cmd == IP6T_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001216 target = 1;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001217 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08001218 target = 0;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001219
Harald Welte2e4e6a12006-01-12 13:30:04 -08001220 try_then_request_module(xt_find_revision(AF_INET6, rev.name,
1221 rev.revision,
1222 target, &ret),
Harald Welte6b7d31f2005-10-26 09:34:24 +02001223 "ip6t_%s", rev.name);
1224 break;
1225 }
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 default:
1228 duprintf("do_ip6t_get_ctl: unknown request %i\n", cmd);
1229 ret = -EINVAL;
1230 }
1231
1232 return ret;
1233}
1234
Harald Welte2e4e6a12006-01-12 13:30:04 -08001235int ip6t_register_table(struct xt_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 const struct ip6t_replace *repl)
1237{
1238 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001239 struct xt_table_info *newinfo;
1240 static struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08001242 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Harald Welte2e4e6a12006-01-12 13:30:04 -08001244 newinfo = xt_alloc_table_info(repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (!newinfo)
1246 return -ENOMEM;
1247
Eric Dumazet31836062005-12-13 23:13:48 -08001248 /* choose the copy on our node/cpu */
1249 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1250 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001253 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 repl->num_entries,
1255 repl->hook_entry,
1256 repl->underflow);
1257 if (ret != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001258 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return ret;
1260 }
1261
Patrick McHardyda298d32006-06-27 03:00:09 -07001262 ret = xt_register_table(table, &bootstrap, newinfo);
1263 if (ret != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001264 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return ret;
1266 }
1267
Harald Welte2e4e6a12006-01-12 13:30:04 -08001268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Harald Welte2e4e6a12006-01-12 13:30:04 -08001271void ip6t_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Harald Welte2e4e6a12006-01-12 13:30:04 -08001273 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08001274 void *loc_cpu_entry;
1275
Harald Welte2e4e6a12006-01-12 13:30:04 -08001276 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001279 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1280 IP6T_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
1281 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
1284/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07001285static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286icmp6_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
1287 u_int8_t type, u_int8_t code,
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07001288 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 return (type == test_type && code >= min_code && code <= max_code)
1291 ^ invert;
1292}
1293
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001294static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295icmp6_match(const struct sk_buff *skb,
1296 const struct net_device *in,
1297 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -08001298 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 const void *matchinfo,
1300 int offset,
1301 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -07001302 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct icmp6hdr _icmp, *ic;
1305 const struct ip6t_icmp *icmpinfo = matchinfo;
1306
1307 /* Must not be a fragment. */
1308 if (offset)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001309 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 ic = skb_header_pointer(skb, protoff, sizeof(_icmp), &_icmp);
1312 if (ic == NULL) {
1313 /* We've been asked to examine this packet, and we
1314 can't. Hence, no choice but to drop. */
1315 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -07001316 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001317 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
1320 return icmp6_type_code_match(icmpinfo->type,
1321 icmpinfo->code[0],
1322 icmpinfo->code[1],
1323 ic->icmp6_type, ic->icmp6_code,
1324 !!(icmpinfo->invflags&IP6T_ICMP_INV));
1325}
1326
1327/* Called when user tries to insert an entry of this type. */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07001328static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329icmp6_checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001330 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -08001331 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 void *matchinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 unsigned int hook_mask)
1334{
1335 const struct ip6t_icmp *icmpinfo = matchinfo;
1336
Patrick McHardy7f939712006-03-20 18:01:43 -08001337 /* Must specify no unknown invflags */
1338 return !(icmpinfo->invflags & ~IP6T_ICMP_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
1341/* The built-in targets: standard (NULL) and error. */
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001342static struct xt_target ip6t_standard_target = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 .name = IP6T_STANDARD_TARGET,
Patrick McHardy7f939712006-03-20 18:01:43 -08001344 .targetsize = sizeof(int),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08001345 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346};
1347
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001348static struct xt_target ip6t_error_target = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 .name = IP6T_ERROR_TARGET,
1350 .target = ip6t_error,
Patrick McHardy7f939712006-03-20 18:01:43 -08001351 .targetsize = IP6T_FUNCTION_MAXNAMELEN,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08001352 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353};
1354
1355static struct nf_sockopt_ops ip6t_sockopts = {
1356 .pf = PF_INET6,
1357 .set_optmin = IP6T_BASE_CTL,
1358 .set_optmax = IP6T_SO_SET_MAX+1,
1359 .set = do_ip6t_set_ctl,
1360 .get_optmin = IP6T_BASE_CTL,
1361 .get_optmax = IP6T_SO_GET_MAX+1,
1362 .get = do_ip6t_get_ctl,
1363};
1364
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001365static struct xt_match icmp6_matchstruct = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 .name = "icmp6",
1367 .match = &icmp6_match,
Patrick McHardy7f939712006-03-20 18:01:43 -08001368 .matchsize = sizeof(struct ip6t_icmp),
1369 .checkentry = icmp6_checkentry,
1370 .proto = IPPROTO_ICMPV6,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08001371 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372};
1373
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001374static int __init ip6_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 int ret;
1377
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001378 ret = xt_proto_init(AF_INET6);
1379 if (ret < 0)
1380 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* Noone else will be downing sem now, so we won't sleep */
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001383 ret = xt_register_target(&ip6t_standard_target);
1384 if (ret < 0)
1385 goto err2;
1386 ret = xt_register_target(&ip6t_error_target);
1387 if (ret < 0)
1388 goto err3;
1389 ret = xt_register_match(&icmp6_matchstruct);
1390 if (ret < 0)
1391 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 /* Register setsockopt */
1394 ret = nf_register_sockopt(&ip6t_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001395 if (ret < 0)
1396 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Harald Welte2e4e6a12006-01-12 13:30:04 -08001398 printk("ip6_tables: (C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001400
1401err5:
1402 xt_unregister_match(&icmp6_matchstruct);
1403err4:
1404 xt_unregister_target(&ip6t_error_target);
1405err3:
1406 xt_unregister_target(&ip6t_standard_target);
1407err2:
1408 xt_proto_fini(AF_INET6);
1409err1:
1410 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001413static void __exit ip6_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 nf_unregister_sockopt(&ip6t_sockopts);
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08001416 xt_unregister_match(&icmp6_matchstruct);
1417 xt_unregister_target(&ip6t_error_target);
1418 xt_unregister_target(&ip6t_standard_target);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001419 xt_proto_fini(AF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001422/*
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001423 * find the offset to specified header or the protocol number of last header
1424 * if target < 0. "last header" is transport protocol header, ESP, or
1425 * "No next header".
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001426 *
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001427 * If target header is found, its offset is set in *offset and return protocol
1428 * number. Otherwise, return -1.
1429 *
Patrick McHardy6d381632006-10-24 16:15:10 -07001430 * If the first fragment doesn't contain the final protocol header or
1431 * NEXTHDR_NONE it is considered invalid.
1432 *
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001433 * Note that non-1st fragment is special case that "the protocol number
1434 * of last header" is "next header" field in Fragment header. In this case,
1435 * *offset is meaningless and fragment offset is stored in *fragoff if fragoff
1436 * isn't NULL.
1437 *
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001438 */
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001439int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
1440 int target, unsigned short *fragoff)
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001441{
Arnaldo Carvalho de Melo6b88dd92007-03-19 22:29:03 -03001442 unsigned int start = skb_network_offset(skb) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001443 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001444 unsigned int len = skb->len - start;
1445
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001446 if (fragoff)
1447 *fragoff = 0;
1448
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001449 while (nexthdr != target) {
1450 struct ipv6_opt_hdr _hdr, *hp;
1451 unsigned int hdrlen;
1452
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001453 if ((!ipv6_ext_hdr(nexthdr)) || nexthdr == NEXTHDR_NONE) {
1454 if (target < 0)
1455 break;
Patrick McHardy6d381632006-10-24 16:15:10 -07001456 return -ENOENT;
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001457 }
1458
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001459 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
1460 if (hp == NULL)
Patrick McHardy6d381632006-10-24 16:15:10 -07001461 return -EBADMSG;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001462 if (nexthdr == NEXTHDR_FRAGMENT) {
Al Viroe69a4adc2006-11-14 20:56:00 -08001463 unsigned short _frag_off;
1464 __be16 *fp;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001465 fp = skb_header_pointer(skb,
1466 start+offsetof(struct frag_hdr,
1467 frag_off),
1468 sizeof(_frag_off),
1469 &_frag_off);
1470 if (fp == NULL)
Patrick McHardy6d381632006-10-24 16:15:10 -07001471 return -EBADMSG;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001472
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001473 _frag_off = ntohs(*fp) & ~0x7;
1474 if (_frag_off) {
1475 if (target < 0 &&
1476 ((!ipv6_ext_hdr(hp->nexthdr)) ||
Patrick McHardy337dde72006-11-14 19:49:13 -08001477 hp->nexthdr == NEXTHDR_NONE)) {
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001478 if (fragoff)
1479 *fragoff = _frag_off;
1480 return hp->nexthdr;
1481 }
Patrick McHardy6d381632006-10-24 16:15:10 -07001482 return -ENOENT;
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001483 }
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001484 hdrlen = 8;
1485 } else if (nexthdr == NEXTHDR_AUTH)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001486 hdrlen = (hp->hdrlen + 2) << 2;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001487 else
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001488 hdrlen = ipv6_optlen(hp);
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001489
1490 nexthdr = hp->nexthdr;
1491 len -= hdrlen;
1492 start += hdrlen;
1493 }
1494
1495 *offset = start;
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001496 return nexthdr;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001497}
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499EXPORT_SYMBOL(ip6t_register_table);
1500EXPORT_SYMBOL(ip6t_unregister_table);
1501EXPORT_SYMBOL(ip6t_do_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502EXPORT_SYMBOL(ip6t_ext_hdr);
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001503EXPORT_SYMBOL(ipv6_find_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001505module_init(ip6_tables_init);
1506module_exit(ip6_tables_fini);