blob: e60c1b4b1ec8c6112d46583243d92abef1e54eb4 [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
Herbert Xu3db05fe2007-10-15 00:53:15 -0700208ip6t_error(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 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
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700244/* All zeroes == unconditional rule. */
245static inline int
246unconditional(const struct ip6t_ip6 *ipv6)
247{
248 unsigned int i;
249
250 for (i = 0; i < sizeof(*ipv6); i++)
251 if (((char *)ipv6)[i])
252 break;
253
254 return (i == sizeof(*ipv6));
255}
256
257#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
258 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
259/* This cries for unification! */
260static const char *hooknames[] = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800261 [NF_INET_PRE_ROUTING] = "PREROUTING",
262 [NF_INET_LOCAL_IN] = "INPUT",
263 [NF_INET_FORWARD] = "FORWARD",
264 [NF_INET_LOCAL_OUT] = "OUTPUT",
265 [NF_INET_POST_ROUTING] = "POSTROUTING",
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700266};
267
268enum nf_ip_trace_comments {
269 NF_IP6_TRACE_COMMENT_RULE,
270 NF_IP6_TRACE_COMMENT_RETURN,
271 NF_IP6_TRACE_COMMENT_POLICY,
272};
273
274static const char *comments[] = {
275 [NF_IP6_TRACE_COMMENT_RULE] = "rule",
276 [NF_IP6_TRACE_COMMENT_RETURN] = "return",
277 [NF_IP6_TRACE_COMMENT_POLICY] = "policy",
278};
279
280static struct nf_loginfo trace_loginfo = {
281 .type = NF_LOG_TYPE_LOG,
282 .u = {
283 .log = {
284 .level = 4,
285 .logflags = NF_LOG_MASK,
286 },
287 },
288};
289
290static inline int
291get_chainname_rulenum(struct ip6t_entry *s, struct ip6t_entry *e,
292 char *hookname, char **chainname,
293 char **comment, unsigned int *rulenum)
294{
295 struct ip6t_standard_target *t = (void *)ip6t_get_target(s);
296
297 if (strcmp(t->target.u.kernel.target->name, IP6T_ERROR_TARGET) == 0) {
298 /* Head of user chain: ERROR target with chainname */
299 *chainname = t->target.data;
300 (*rulenum) = 0;
301 } else if (s == e) {
302 (*rulenum)++;
303
304 if (s->target_offset == sizeof(struct ip6t_entry)
305 && strcmp(t->target.u.kernel.target->name,
306 IP6T_STANDARD_TARGET) == 0
307 && t->verdict < 0
308 && unconditional(&s->ipv6)) {
309 /* Tail of chains: STANDARD target (return/policy) */
310 *comment = *chainname == hookname
311 ? (char *)comments[NF_IP6_TRACE_COMMENT_POLICY]
312 : (char *)comments[NF_IP6_TRACE_COMMENT_RETURN];
313 }
314 return 1;
315 } else
316 (*rulenum)++;
317
318 return 0;
319}
320
321static void trace_packet(struct sk_buff *skb,
322 unsigned int hook,
323 const struct net_device *in,
324 const struct net_device *out,
325 char *tablename,
326 struct xt_table_info *private,
327 struct ip6t_entry *e)
328{
329 void *table_base;
330 struct ip6t_entry *root;
331 char *hookname, *chainname, *comment;
332 unsigned int rulenum = 0;
333
334 table_base = (void *)private->entries[smp_processor_id()];
335 root = get_entry(table_base, private->hook_entry[hook]);
336
337 hookname = chainname = (char *)hooknames[hook];
338 comment = (char *)comments[NF_IP6_TRACE_COMMENT_RULE];
339
340 IP6T_ENTRY_ITERATE(root,
341 private->size - private->hook_entry[hook],
342 get_chainname_rulenum,
343 e, hookname, &chainname, &comment, &rulenum);
344
345 nf_log_packet(AF_INET6, hook, skb, in, out, &trace_loginfo,
346 "TRACE: %s:%s:%s:%u ",
347 tablename, chainname, comment, rulenum);
348}
349#endif
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/* Returns one of the generic firewall policies, like NF_ACCEPT. */
352unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700353ip6t_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 unsigned int hook,
355 const struct net_device *in,
356 const struct net_device *out,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700357 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Harald Welte6b7d31f2005-10-26 09:34:24 +0200359 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int offset = 0;
361 unsigned int protoff = 0;
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700362 bool hotdrop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 /* Initializing verdict to NF_DROP keeps gcc happy. */
364 unsigned int verdict = NF_DROP;
365 const char *indev, *outdev;
366 void *table_base;
367 struct ip6t_entry *e, *back;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800368 struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /* Initialization */
371 indev = in ? in->name : nulldevname;
372 outdev = out ? out->name : nulldevname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* We handle fragments by dealing with the first fragment as
374 * if it was a normal packet. All other fragments are treated
375 * normally, except that they will NEVER match rules that ask
376 * things we don't know, ie. tcp syn flag or ports). If the
377 * rule is also a fragment-specific rule, non-fragments won't
378 * match it. */
379
380 read_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800381 private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800383 table_base = (void *)private->entries[smp_processor_id()];
384 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* For return from builtin chain */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800387 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 do {
390 IP_NF_ASSERT(e);
391 IP_NF_ASSERT(back);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700392 if (ip6_packet_match(skb, indev, outdev, &e->ipv6,
Patrick McHardy51d8b1a2006-10-24 16:14:04 -0700393 &protoff, &offset, &hotdrop)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct ip6t_entry_target *t;
395
396 if (IP6T_MATCH_ITERATE(e, do_match,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700397 skb, in, out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 offset, protoff, &hotdrop) != 0)
399 goto no_match;
400
401 ADD_COUNTER(e->counters,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700402 ntohs(ipv6_hdr(skb)->payload_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 + IPV6_HDR_LEN,
404 1);
405
406 t = ip6t_get_target(e);
407 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700408
409#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
410 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
411 /* The packet is traced: log it */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700412 if (unlikely(skb->nf_trace))
413 trace_packet(skb, hook, in, out,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700414 table->name, private, e);
415#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /* Standard target? */
417 if (!t->u.kernel.target->target) {
418 int v;
419
420 v = ((struct ip6t_standard_target *)t)->verdict;
421 if (v < 0) {
422 /* Pop from stack? */
423 if (v != IP6T_RETURN) {
424 verdict = (unsigned)(-v) - 1;
425 break;
426 }
427 e = back;
428 back = get_entry(table_base,
429 back->comefrom);
430 continue;
431 }
Patrick McHardy05465342005-08-21 23:31:43 -0700432 if (table_base + v != (void *)e + e->next_offset
433 && !(e->ipv6.flags & IP6T_F_GOTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* Save old back ptr in next entry */
435 struct ip6t_entry *next
436 = (void *)e + e->next_offset;
437 next->comefrom
438 = (void *)back - table_base;
439 /* set back pointer to next entry */
440 back = next;
441 }
442
443 e = get_entry(table_base, v);
444 } else {
445 /* Targets which reenter must return
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900446 abs. verdicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447#ifdef CONFIG_NETFILTER_DEBUG
448 ((struct ip6t_entry *)table_base)->comefrom
449 = 0xeeeeeeec;
450#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700451 verdict = t->u.kernel.target->target(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 in, out,
453 hook,
Patrick McHardy1c524832006-03-20 18:02:15 -0800454 t->u.kernel.target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700455 t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457#ifdef CONFIG_NETFILTER_DEBUG
458 if (((struct ip6t_entry *)table_base)->comefrom
459 != 0xeeeeeeec
460 && verdict == IP6T_CONTINUE) {
461 printk("Target %s reentered!\n",
462 t->u.kernel.target->name);
463 verdict = NF_DROP;
464 }
465 ((struct ip6t_entry *)table_base)->comefrom
466 = 0x57acc001;
467#endif
468 if (verdict == IP6T_CONTINUE)
469 e = (void *)e + e->next_offset;
470 else
471 /* Verdict */
472 break;
473 }
474 } else {
475
476 no_match:
477 e = (void *)e + e->next_offset;
478 }
479 } while (!hotdrop);
480
481#ifdef CONFIG_NETFILTER_DEBUG
Randy Dunlap4bdbf6c2006-07-03 19:47:27 -0700482 ((struct ip6t_entry *)table_base)->comefrom = NETFILTER_LINK_POISON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483#endif
484 read_unlock_bh(&table->lock);
485
486#ifdef DEBUG_ALLOW_ALL
487 return NF_ACCEPT;
488#else
489 if (hotdrop)
490 return NF_DROP;
491 else return verdict;
492#endif
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/* Figures out from what hook each rule can be called: returns 0 if
496 there are loops. Puts hook bitmask in comefrom. */
497static int
Harald Welte2e4e6a12006-01-12 13:30:04 -0800498mark_source_chains(struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800499 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 unsigned int hook;
502
503 /* No recursion; use packet counter to save back ptrs (reset
504 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800505 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 unsigned int pos = newinfo->hook_entry[hook];
507 struct ip6t_entry *e
Eric Dumazet31836062005-12-13 23:13:48 -0800508 = (struct ip6t_entry *)(entry0 + pos);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800509 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if (!(valid_hooks & (1 << hook)))
512 continue;
513
514 /* Set initial back pointer. */
515 e->counters.pcnt = pos;
516
517 for (;;) {
518 struct ip6t_standard_target *t
519 = (void *)ip6t_get_target(e);
520
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800521 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 printk("iptables: loop hook %u pos %u %08X.\n",
523 hook, pos, e->comefrom);
524 return 0;
525 }
526 e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800527 |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /* Unconditional return/END. */
Al Viroe1b4b9f2006-12-12 00:29:52 -0800530 if ((e->target_offset == sizeof(struct ip6t_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 && (strcmp(t->target.u.user.name,
532 IP6T_STANDARD_TARGET) == 0)
533 && t->verdict < 0
Al Viroe1b4b9f2006-12-12 00:29:52 -0800534 && unconditional(&e->ipv6)) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 unsigned int oldpos, size;
536
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800537 if (t->verdict < -NF_MAX_VERDICT - 1) {
538 duprintf("mark_source_chains: bad "
539 "negative verdict (%i)\n",
540 t->verdict);
541 return 0;
542 }
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* Return: backtrack through the last
545 big jump. */
546 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800547 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#ifdef DEBUG_IP_FIREWALL_USER
549 if (e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800550 & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 duprintf("Back unset "
552 "on hook %u "
553 "rule %u\n",
554 hook, pos);
555 }
556#endif
557 oldpos = pos;
558 pos = e->counters.pcnt;
559 e->counters.pcnt = 0;
560
561 /* We're at the start. */
562 if (pos == oldpos)
563 goto next;
564
565 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800566 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 } while (oldpos == pos + e->next_offset);
568
569 /* Move along one */
570 size = e->next_offset;
571 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800572 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 e->counters.pcnt = pos;
574 pos += size;
575 } else {
576 int newpos = t->verdict;
577
578 if (strcmp(t->target.u.user.name,
579 IP6T_STANDARD_TARGET) == 0
580 && newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800581 if (newpos > newinfo->size -
582 sizeof(struct ip6t_entry)) {
583 duprintf("mark_source_chains: "
584 "bad verdict (%i)\n",
585 newpos);
586 return 0;
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* This a jump; chase it. */
589 duprintf("Jump rule %u -> %u\n",
590 pos, newpos);
591 } else {
592 /* ... this is a fallthru */
593 newpos = pos + e->next_offset;
594 }
595 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800596 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 e->counters.pcnt = pos;
598 pos = newpos;
599 }
600 }
601 next:
602 duprintf("Finished chain %u\n", hook);
603 }
604 return 1;
605}
606
607static inline int
608cleanup_match(struct ip6t_entry_match *m, unsigned int *i)
609{
610 if (i && (*i)-- == 0)
611 return 1;
612
613 if (m->u.kernel.match->destroy)
Patrick McHardyefa74162006-08-22 00:36:37 -0700614 m->u.kernel.match->destroy(m->u.kernel.match, m->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 module_put(m->u.kernel.match->me);
616 return 0;
617}
618
619static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620check_match(struct ip6t_entry_match *m,
621 const char *name,
622 const struct ip6t_ip6 *ipv6,
623 unsigned int hookmask,
624 unsigned int *i)
625{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800626 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800627 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Harald Welte2e4e6a12006-01-12 13:30:04 -0800629 match = try_then_request_module(xt_find_match(AF_INET6, m->u.user.name,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900630 m->u.user.revision),
Harald Welte6b7d31f2005-10-26 09:34:24 +0200631 "ip6t_%s", m->u.user.name);
632 if (IS_ERR(match) || !match) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900633 duprintf("check_match: `%s' not found\n", m->u.user.name);
Harald Welte6b7d31f2005-10-26 09:34:24 +0200634 return match ? PTR_ERR(match) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 m->u.kernel.match = match;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800638 ret = xt_check_match(match, AF_INET6, m->u.match_size - sizeof(*m),
639 name, hookmask, ipv6->proto,
640 ipv6->invflags & IP6T_INV_PROTO);
641 if (ret)
642 goto err;
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (m->u.kernel.match->checkentry
Patrick McHardy1c524832006-03-20 18:02:15 -0800645 && !m->u.kernel.match->checkentry(name, ipv6, match, m->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 hookmask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 duprintf("ip_tables: check failed for `%s'.\n",
648 m->u.kernel.match->name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800649 ret = -EINVAL;
650 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
653 (*i)++;
654 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800655err:
656 module_put(m->u.kernel.match->me);
657 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800660static struct xt_target ip6t_standard_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662static inline int
663check_entry(struct ip6t_entry *e, const char *name, unsigned int size,
664 unsigned int *i)
665{
666 struct ip6t_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800667 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int ret;
669 unsigned int j;
670
671 if (!ip6_checkentry(&e->ipv6)) {
672 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
673 return -EINVAL;
674 }
675
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800676 if (e->target_offset + sizeof(struct ip6t_entry_target) >
677 e->next_offset)
678 return -EINVAL;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 j = 0;
681 ret = IP6T_MATCH_ITERATE(e, check_match, name, &e->ipv6, e->comefrom, &j);
682 if (ret != 0)
683 goto cleanup_matches;
684
685 t = ip6t_get_target(e);
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800686 ret = -EINVAL;
687 if (e->target_offset + t->u.target_size > e->next_offset)
688 goto cleanup_matches;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800689 target = try_then_request_module(xt_find_target(AF_INET6,
690 t->u.user.name,
691 t->u.user.revision),
Harald Welte6b7d31f2005-10-26 09:34:24 +0200692 "ip6t_%s", t->u.user.name);
693 if (IS_ERR(target) || !target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 duprintf("check_entry: `%s' not found\n", t->u.user.name);
Harald Welte6b7d31f2005-10-26 09:34:24 +0200695 ret = target ? PTR_ERR(target) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 goto cleanup_matches;
697 }
698 t->u.kernel.target = target;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200699
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800700 ret = xt_check_target(target, AF_INET6, t->u.target_size - sizeof(*t),
701 name, e->comefrom, e->ipv6.proto,
702 e->ipv6.invflags & IP6T_INV_PROTO);
703 if (ret)
704 goto err;
705
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800706 if (t->u.kernel.target->checkentry
Patrick McHardy1c524832006-03-20 18:02:15 -0800707 && !t->u.kernel.target->checkentry(name, e, target, t->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 e->comefrom)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 duprintf("ip_tables: check failed for `%s'.\n",
710 t->u.kernel.target->name);
711 ret = -EINVAL;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800712 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714
715 (*i)++;
716 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800717 err:
718 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 cleanup_matches:
720 IP6T_MATCH_ITERATE(e, cleanup_match, &j);
721 return ret;
722}
723
724static inline int
725check_entry_size_and_hooks(struct ip6t_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800726 struct xt_table_info *newinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 unsigned char *base,
728 unsigned char *limit,
729 const unsigned int *hook_entries,
730 const unsigned int *underflows,
731 unsigned int *i)
732{
733 unsigned int h;
734
735 if ((unsigned long)e % __alignof__(struct ip6t_entry) != 0
736 || (unsigned char *)e + sizeof(struct ip6t_entry) >= limit) {
737 duprintf("Bad offset %p\n", e);
738 return -EINVAL;
739 }
740
741 if (e->next_offset
742 < sizeof(struct ip6t_entry) + sizeof(struct ip6t_entry_target)) {
743 duprintf("checking: element %p size %u\n",
744 e, e->next_offset);
745 return -EINVAL;
746 }
747
748 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800749 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if ((unsigned char *)e - base == hook_entries[h])
751 newinfo->hook_entry[h] = hook_entries[h];
752 if ((unsigned char *)e - base == underflows[h])
753 newinfo->underflow[h] = underflows[h];
754 }
755
756 /* FIXME: underflows must be unconditional, standard verdicts
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900757 < 0 (not IP6T_RETURN). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800760 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 e->comefrom = 0;
762
763 (*i)++;
764 return 0;
765}
766
767static inline int
768cleanup_entry(struct ip6t_entry *e, unsigned int *i)
769{
770 struct ip6t_entry_target *t;
771
772 if (i && (*i)-- == 0)
773 return 1;
774
775 /* Cleanup all matches */
776 IP6T_MATCH_ITERATE(e, cleanup_match, NULL);
777 t = ip6t_get_target(e);
778 if (t->u.kernel.target->destroy)
Patrick McHardyefa74162006-08-22 00:36:37 -0700779 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 module_put(t->u.kernel.target->me);
781 return 0;
782}
783
784/* Checks and translates the user-supplied table segment (held in
785 newinfo) */
786static int
787translate_table(const char *name,
788 unsigned int valid_hooks,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800789 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800790 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 unsigned int size,
792 unsigned int number,
793 const unsigned int *hook_entries,
794 const unsigned int *underflows)
795{
796 unsigned int i;
797 int ret;
798
799 newinfo->size = size;
800 newinfo->number = number;
801
802 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800803 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 newinfo->hook_entry[i] = 0xFFFFFFFF;
805 newinfo->underflow[i] = 0xFFFFFFFF;
806 }
807
808 duprintf("translate_table: size %u\n", newinfo->size);
809 i = 0;
810 /* Walk through entries, checking offsets. */
Eric Dumazet31836062005-12-13 23:13:48 -0800811 ret = IP6T_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 check_entry_size_and_hooks,
813 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800814 entry0,
815 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 hook_entries, underflows, &i);
817 if (ret != 0)
818 return ret;
819
820 if (i != number) {
821 duprintf("translate_table: %u not %u entries\n",
822 i, number);
823 return -EINVAL;
824 }
825
826 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800827 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* Only hooks which are valid */
829 if (!(valid_hooks & (1 << i)))
830 continue;
831 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
832 duprintf("Invalid hook entry %u %u\n",
833 i, hook_entries[i]);
834 return -EINVAL;
835 }
836 if (newinfo->underflow[i] == 0xFFFFFFFF) {
837 duprintf("Invalid underflow %u %u\n",
838 i, underflows[i]);
839 return -EINVAL;
840 }
841 }
842
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800843 if (!mark_source_chains(newinfo, valid_hooks, entry0))
844 return -ELOOP;
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* Finally, each sanity check must pass */
847 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800848 ret = IP6T_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 check_entry, name, size, &i);
850
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800851 if (ret != 0) {
852 IP6T_ENTRY_ITERATE(entry0, newinfo->size,
853 cleanup_entry, &i);
854 return ret;
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700858 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800859 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
860 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800863 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/* Gets counters. */
867static inline int
868add_entry_to_counter(const struct ip6t_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800869 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 unsigned int *i)
871{
872 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
873
874 (*i)++;
875 return 0;
876}
877
Eric Dumazet31836062005-12-13 23:13:48 -0800878static inline int
879set_entry_to_counter(const struct ip6t_entry *e,
880 struct ip6t_counters total[],
881 unsigned int *i)
882{
883 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
884
885 (*i)++;
886 return 0;
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800890get_counters(const struct xt_table_info *t,
891 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 unsigned int cpu;
894 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800895 unsigned int curcpu;
896
897 /* Instead of clearing (by a previous call to memset())
898 * the counters and using adds, we set the counters
899 * with data used by 'current' CPU
900 * We dont care about preemption here.
901 */
902 curcpu = raw_smp_processor_id();
903
904 i = 0;
905 IP6T_ENTRY_ITERATE(t->entries[curcpu],
906 t->size,
907 set_entry_to_counter,
908 counters,
909 &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700911 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800912 if (cpu == curcpu)
913 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800915 IP6T_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 t->size,
917 add_entry_to_counter,
918 counters,
919 &i);
920 }
921}
922
923static int
924copy_entries_to_user(unsigned int total_size,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800925 struct xt_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 void __user *userptr)
927{
928 unsigned int off, num, countersize;
929 struct ip6t_entry *e;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800930 struct xt_counters *counters;
931 struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800933 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* We need atomic snapshot of counters: rest doesn't change
936 (other than comefrom, which userspace doesn't care
937 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800938 countersize = sizeof(struct xt_counters) * private->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 counters = vmalloc(countersize);
940
941 if (counters == NULL)
942 return -ENOMEM;
943
944 /* First, sum counters... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 write_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800946 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 write_unlock_bh(&table->lock);
948
Eric Dumazet31836062005-12-13 23:13:48 -0800949 /* choose the copy that is on ourc node/cpu */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800950 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800951 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 ret = -EFAULT;
953 goto free_counters;
954 }
955
956 /* FIXME: use iterator macros --RR */
957 /* ... then go back and fix counters and names */
958 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
959 unsigned int i;
960 struct ip6t_entry_match *m;
961 struct ip6t_entry_target *t;
962
Eric Dumazet31836062005-12-13 23:13:48 -0800963 e = (struct ip6t_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (copy_to_user(userptr + off
965 + offsetof(struct ip6t_entry, counters),
966 &counters[num],
967 sizeof(counters[num])) != 0) {
968 ret = -EFAULT;
969 goto free_counters;
970 }
971
972 for (i = sizeof(struct ip6t_entry);
973 i < e->target_offset;
974 i += m->u.match_size) {
975 m = (void *)e + i;
976
977 if (copy_to_user(userptr + off + i
978 + offsetof(struct ip6t_entry_match,
979 u.user.name),
980 m->u.kernel.match->name,
981 strlen(m->u.kernel.match->name)+1)
982 != 0) {
983 ret = -EFAULT;
984 goto free_counters;
985 }
986 }
987
988 t = ip6t_get_target(e);
989 if (copy_to_user(userptr + off + e->target_offset
990 + offsetof(struct ip6t_entry_target,
991 u.user.name),
992 t->u.kernel.target->name,
993 strlen(t->u.kernel.target->name)+1) != 0) {
994 ret = -EFAULT;
995 goto free_counters;
996 }
997 }
998
999 free_counters:
1000 vfree(counters);
1001 return ret;
1002}
1003
1004static int
1005get_entries(const struct ip6t_get_entries *entries,
1006 struct ip6t_get_entries __user *uptr)
1007{
1008 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001009 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Harald Welte2e4e6a12006-01-12 13:30:04 -08001011 t = xt_find_table_lock(AF_INET6, entries->name);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001012 if (t && !IS_ERR(t)) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001013 struct xt_table_info *private = t->private;
1014 duprintf("t->private->number = %u\n", private->number);
1015 if (entries->size == private->size)
1016 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 t, uptr->entrytable);
1018 else {
1019 duprintf("get_entries: I've got %u not %u!\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -08001020 private->size, entries->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 ret = -EINVAL;
1022 }
Harald Welte6b7d31f2005-10-26 09:34:24 +02001023 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001024 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 } else
Harald Welte6b7d31f2005-10-26 09:34:24 +02001026 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 return ret;
1029}
1030
1031static int
1032do_replace(void __user *user, unsigned int len)
1033{
1034 int ret;
1035 struct ip6t_replace tmp;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001036 struct xt_table *t;
1037 struct xt_table_info *newinfo, *oldinfo;
1038 struct xt_counters *counters;
Eric Dumazet31836062005-12-13 23:13:48 -08001039 void *loc_cpu_entry, *loc_cpu_old_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1042 return -EFAULT;
1043
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001044 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001045 if (tmp.size >= INT_MAX / num_possible_cpus())
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001046 return -ENOMEM;
1047 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1048 return -ENOMEM;
1049
Harald Welte2e4e6a12006-01-12 13:30:04 -08001050 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (!newinfo)
1052 return -ENOMEM;
1053
Eric Dumazet31836062005-12-13 23:13:48 -08001054 /* choose the copy that is on our node/cpu */
1055 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1056 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 tmp.size) != 0) {
1058 ret = -EFAULT;
1059 goto free_newinfo;
1060 }
1061
Harald Welte2e4e6a12006-01-12 13:30:04 -08001062 counters = vmalloc(tmp.num_counters * sizeof(struct xt_counters));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (!counters) {
1064 ret = -ENOMEM;
1065 goto free_newinfo;
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001069 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 tmp.hook_entry, tmp.underflow);
1071 if (ret != 0)
1072 goto free_newinfo_counters;
1073
1074 duprintf("ip_tables: Translated table\n");
1075
Harald Welte2e4e6a12006-01-12 13:30:04 -08001076 t = try_then_request_module(xt_find_table_lock(AF_INET6, tmp.name),
Harald Welte6b7d31f2005-10-26 09:34:24 +02001077 "ip6table_%s", tmp.name);
1078 if (!t || IS_ERR(t)) {
1079 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 goto free_newinfo_counters_untrans;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 /* You lied! */
1084 if (tmp.valid_hooks != t->valid_hooks) {
1085 duprintf("Valid hook crap: %08X vs %08X\n",
1086 tmp.valid_hooks, t->valid_hooks);
1087 ret = -EINVAL;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001088 goto put_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
Harald Welte2e4e6a12006-01-12 13:30:04 -08001091 oldinfo = xt_replace_table(t, tmp.num_counters, newinfo, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (!oldinfo)
1093 goto put_module;
1094
1095 /* Update module usage count based on number of rules */
1096 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1097 oldinfo->number, oldinfo->initial_entries, newinfo->number);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001098 if ((oldinfo->number > oldinfo->initial_entries) ||
1099 (newinfo->number <= oldinfo->initial_entries))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 module_put(t->me);
1101 if ((oldinfo->number > oldinfo->initial_entries) &&
1102 (newinfo->number <= oldinfo->initial_entries))
1103 module_put(t->me);
1104
1105 /* Get the old counters. */
1106 get_counters(oldinfo, counters);
1107 /* Decrease module usage counts and free resource */
Eric Dumazet31836062005-12-13 23:13:48 -08001108 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1109 IP6T_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001110 xt_free_table_info(oldinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (copy_to_user(tmp.counters, counters,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001112 sizeof(struct xt_counters) * tmp.num_counters) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 ret = -EFAULT;
1114 vfree(counters);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001115 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return ret;
1117
1118 put_module:
1119 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001120 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 free_newinfo_counters_untrans:
Eric Dumazet31836062005-12-13 23:13:48 -08001122 IP6T_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 free_newinfo_counters:
1124 vfree(counters);
1125 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001126 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return ret;
1128}
1129
1130/* We're lazy, and add to the first CPU; overflow works its fey magic
1131 * and everything is OK. */
1132static inline int
1133add_counter_to_entry(struct ip6t_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001134 const struct xt_counters addme[],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 unsigned int *i)
1136{
1137#if 0
1138 duprintf("add_counter: Entry %u %lu/%lu + %lu/%lu\n",
1139 *i,
1140 (long unsigned int)e->counters.pcnt,
1141 (long unsigned int)e->counters.bcnt,
1142 (long unsigned int)addme[*i].pcnt,
1143 (long unsigned int)addme[*i].bcnt);
1144#endif
1145
1146 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1147
1148 (*i)++;
1149 return 0;
1150}
1151
1152static int
1153do_add_counters(void __user *user, unsigned int len)
1154{
1155 unsigned int i;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001156 struct xt_counters_info tmp, *paddc;
1157 struct xt_table_info *private;
1158 struct xt_table *t;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001159 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001160 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1163 return -EFAULT;
1164
Harald Welte2e4e6a12006-01-12 13:30:04 -08001165 if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return -EINVAL;
1167
1168 paddc = vmalloc(len);
1169 if (!paddc)
1170 return -ENOMEM;
1171
1172 if (copy_from_user(paddc, user, len) != 0) {
1173 ret = -EFAULT;
1174 goto free;
1175 }
1176
Harald Welte2e4e6a12006-01-12 13:30:04 -08001177 t = xt_find_table_lock(AF_INET6, tmp.name);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001178 if (!t || IS_ERR(t)) {
1179 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 goto free;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 write_lock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001184 private = t->private;
Solar Designer2c8ac662006-05-19 02:16:52 -07001185 if (private->number != tmp.num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 ret = -EINVAL;
1187 goto unlock_up_free;
1188 }
1189
1190 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001191 /* Choose the copy that is on our node */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001192 loc_cpu_entry = private->entries[smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -08001193 IP6T_ENTRY_ITERATE(loc_cpu_entry,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001194 private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 add_counter_to_entry,
1196 paddc->counters,
1197 &i);
1198 unlock_up_free:
1199 write_unlock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001200 xt_table_unlock(t);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001201 module_put(t->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 free:
1203 vfree(paddc);
1204
1205 return ret;
1206}
1207
1208static int
1209do_ip6t_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1210{
1211 int ret;
1212
1213 if (!capable(CAP_NET_ADMIN))
1214 return -EPERM;
1215
1216 switch (cmd) {
1217 case IP6T_SO_SET_REPLACE:
1218 ret = do_replace(user, len);
1219 break;
1220
1221 case IP6T_SO_SET_ADD_COUNTERS:
1222 ret = do_add_counters(user, len);
1223 break;
1224
1225 default:
1226 duprintf("do_ip6t_set_ctl: unknown request %i\n", cmd);
1227 ret = -EINVAL;
1228 }
1229
1230 return ret;
1231}
1232
1233static int
1234do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1235{
1236 int ret;
1237
1238 if (!capable(CAP_NET_ADMIN))
1239 return -EPERM;
1240
1241 switch (cmd) {
1242 case IP6T_SO_GET_INFO: {
1243 char name[IP6T_TABLE_MAXNAMELEN];
Harald Welte2e4e6a12006-01-12 13:30:04 -08001244 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 if (*len != sizeof(struct ip6t_getinfo)) {
1247 duprintf("length %u != %u\n", *len,
1248 sizeof(struct ip6t_getinfo));
1249 ret = -EINVAL;
1250 break;
1251 }
1252
1253 if (copy_from_user(name, user, sizeof(name)) != 0) {
1254 ret = -EFAULT;
1255 break;
1256 }
1257 name[IP6T_TABLE_MAXNAMELEN-1] = '\0';
Harald Welte6b7d31f2005-10-26 09:34:24 +02001258
Harald Welte2e4e6a12006-01-12 13:30:04 -08001259 t = try_then_request_module(xt_find_table_lock(AF_INET6, name),
Harald Welte6b7d31f2005-10-26 09:34:24 +02001260 "ip6table_%s", name);
1261 if (t && !IS_ERR(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 struct ip6t_getinfo info;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001263 struct xt_table_info *private = t->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 info.valid_hooks = t->valid_hooks;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001266 memcpy(info.hook_entry, private->hook_entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 sizeof(info.hook_entry));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001268 memcpy(info.underflow, private->underflow,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 sizeof(info.underflow));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001270 info.num_entries = private->number;
1271 info.size = private->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 memcpy(info.name, name, sizeof(info.name));
1273
1274 if (copy_to_user(user, &info, *len) != 0)
1275 ret = -EFAULT;
1276 else
1277 ret = 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001278 xt_table_unlock(t);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001279 module_put(t->me);
1280 } else
1281 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 break;
1284
1285 case IP6T_SO_GET_ENTRIES: {
1286 struct ip6t_get_entries get;
1287
1288 if (*len < sizeof(get)) {
1289 duprintf("get_entries: %u < %u\n", *len, sizeof(get));
1290 ret = -EINVAL;
1291 } else if (copy_from_user(&get, user, sizeof(get)) != 0) {
1292 ret = -EFAULT;
1293 } else if (*len != sizeof(struct ip6t_get_entries) + get.size) {
1294 duprintf("get_entries: %u != %u\n", *len,
1295 sizeof(struct ip6t_get_entries) + get.size);
1296 ret = -EINVAL;
1297 } else
1298 ret = get_entries(&get, user);
1299 break;
1300 }
1301
Harald Welte6b7d31f2005-10-26 09:34:24 +02001302 case IP6T_SO_GET_REVISION_MATCH:
1303 case IP6T_SO_GET_REVISION_TARGET: {
1304 struct ip6t_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001305 int target;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001306
1307 if (*len != sizeof(rev)) {
1308 ret = -EINVAL;
1309 break;
1310 }
1311 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1312 ret = -EFAULT;
1313 break;
1314 }
1315
1316 if (cmd == IP6T_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001317 target = 1;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001318 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08001319 target = 0;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001320
Harald Welte2e4e6a12006-01-12 13:30:04 -08001321 try_then_request_module(xt_find_revision(AF_INET6, rev.name,
1322 rev.revision,
1323 target, &ret),
Harald Welte6b7d31f2005-10-26 09:34:24 +02001324 "ip6t_%s", rev.name);
1325 break;
1326 }
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 default:
1329 duprintf("do_ip6t_get_ctl: unknown request %i\n", cmd);
1330 ret = -EINVAL;
1331 }
1332
1333 return ret;
1334}
1335
Harald Welte2e4e6a12006-01-12 13:30:04 -08001336int ip6t_register_table(struct xt_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 const struct ip6t_replace *repl)
1338{
1339 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001340 struct xt_table_info *newinfo;
Eric Dumazet259d4e42007-12-04 23:24:56 -08001341 struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08001343 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Harald Welte2e4e6a12006-01-12 13:30:04 -08001345 newinfo = xt_alloc_table_info(repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (!newinfo)
1347 return -ENOMEM;
1348
Eric Dumazet31836062005-12-13 23:13:48 -08001349 /* choose the copy on our node/cpu */
1350 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1351 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001354 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 repl->num_entries,
1356 repl->hook_entry,
1357 repl->underflow);
1358 if (ret != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001359 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return ret;
1361 }
1362
Patrick McHardyda298d32006-06-27 03:00:09 -07001363 ret = xt_register_table(table, &bootstrap, newinfo);
1364 if (ret != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001365 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return ret;
1367 }
1368
Harald Welte2e4e6a12006-01-12 13:30:04 -08001369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
Harald Welte2e4e6a12006-01-12 13:30:04 -08001372void ip6t_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Harald Welte2e4e6a12006-01-12 13:30:04 -08001374 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08001375 void *loc_cpu_entry;
1376
Harald Welte2e4e6a12006-01-12 13:30:04 -08001377 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001380 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1381 IP6T_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
1382 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
1385/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07001386static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387icmp6_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
1388 u_int8_t type, u_int8_t code,
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07001389 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 return (type == test_type && code >= min_code && code <= max_code)
1392 ^ invert;
1393}
1394
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001395static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396icmp6_match(const struct sk_buff *skb,
1397 const struct net_device *in,
1398 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -08001399 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 const void *matchinfo,
1401 int offset,
1402 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -07001403 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct icmp6hdr _icmp, *ic;
1406 const struct ip6t_icmp *icmpinfo = matchinfo;
1407
1408 /* Must not be a fragment. */
1409 if (offset)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001410 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 ic = skb_header_pointer(skb, protoff, sizeof(_icmp), &_icmp);
1413 if (ic == NULL) {
1414 /* We've been asked to examine this packet, and we
1415 can't. Hence, no choice but to drop. */
1416 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -07001417 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001418 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420
1421 return icmp6_type_code_match(icmpinfo->type,
1422 icmpinfo->code[0],
1423 icmpinfo->code[1],
1424 ic->icmp6_type, ic->icmp6_code,
1425 !!(icmpinfo->invflags&IP6T_ICMP_INV));
1426}
1427
1428/* Called when user tries to insert an entry of this type. */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07001429static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430icmp6_checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001431 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -08001432 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 void *matchinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 unsigned int hook_mask)
1435{
1436 const struct ip6t_icmp *icmpinfo = matchinfo;
1437
Patrick McHardy7f939712006-03-20 18:01:43 -08001438 /* Must specify no unknown invflags */
1439 return !(icmpinfo->invflags & ~IP6T_ICMP_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
1442/* The built-in targets: standard (NULL) and error. */
Patrick McHardy9f15c532007-07-07 22:22:02 -07001443static struct xt_target ip6t_standard_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 .name = IP6T_STANDARD_TARGET,
Patrick McHardy7f939712006-03-20 18:01:43 -08001445 .targetsize = sizeof(int),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08001446 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447};
1448
Patrick McHardy9f15c532007-07-07 22:22:02 -07001449static struct xt_target ip6t_error_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 .name = IP6T_ERROR_TARGET,
1451 .target = ip6t_error,
Patrick McHardy7f939712006-03-20 18:01:43 -08001452 .targetsize = IP6T_FUNCTION_MAXNAMELEN,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08001453 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454};
1455
1456static struct nf_sockopt_ops ip6t_sockopts = {
1457 .pf = PF_INET6,
1458 .set_optmin = IP6T_BASE_CTL,
1459 .set_optmax = IP6T_SO_SET_MAX+1,
1460 .set = do_ip6t_set_ctl,
1461 .get_optmin = IP6T_BASE_CTL,
1462 .get_optmax = IP6T_SO_GET_MAX+1,
1463 .get = do_ip6t_get_ctl,
Neil Horman16fcec32007-09-11 11:28:26 +02001464 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465};
1466
Patrick McHardy9f15c532007-07-07 22:22:02 -07001467static struct xt_match icmp6_matchstruct __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 .name = "icmp6",
1469 .match = &icmp6_match,
Patrick McHardy7f939712006-03-20 18:01:43 -08001470 .matchsize = sizeof(struct ip6t_icmp),
1471 .checkentry = icmp6_checkentry,
1472 .proto = IPPROTO_ICMPV6,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08001473 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474};
1475
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001476static int __init ip6_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 int ret;
1479
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001480 ret = xt_proto_init(AF_INET6);
1481 if (ret < 0)
1482 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /* Noone else will be downing sem now, so we won't sleep */
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001485 ret = xt_register_target(&ip6t_standard_target);
1486 if (ret < 0)
1487 goto err2;
1488 ret = xt_register_target(&ip6t_error_target);
1489 if (ret < 0)
1490 goto err3;
1491 ret = xt_register_match(&icmp6_matchstruct);
1492 if (ret < 0)
1493 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 /* Register setsockopt */
1496 ret = nf_register_sockopt(&ip6t_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001497 if (ret < 0)
1498 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Patrick McHardya887c1c2007-07-14 20:46:15 -07001500 printk(KERN_INFO "ip6_tables: (C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001502
1503err5:
1504 xt_unregister_match(&icmp6_matchstruct);
1505err4:
1506 xt_unregister_target(&ip6t_error_target);
1507err3:
1508 xt_unregister_target(&ip6t_standard_target);
1509err2:
1510 xt_proto_fini(AF_INET6);
1511err1:
1512 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001515static void __exit ip6_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 nf_unregister_sockopt(&ip6t_sockopts);
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08001518 xt_unregister_match(&icmp6_matchstruct);
1519 xt_unregister_target(&ip6t_error_target);
1520 xt_unregister_target(&ip6t_standard_target);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001521 xt_proto_fini(AF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001524/*
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001525 * find the offset to specified header or the protocol number of last header
1526 * if target < 0. "last header" is transport protocol header, ESP, or
1527 * "No next header".
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001528 *
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001529 * If target header is found, its offset is set in *offset and return protocol
1530 * number. Otherwise, return -1.
1531 *
Patrick McHardy6d381632006-10-24 16:15:10 -07001532 * If the first fragment doesn't contain the final protocol header or
1533 * NEXTHDR_NONE it is considered invalid.
1534 *
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001535 * Note that non-1st fragment is special case that "the protocol number
1536 * of last header" is "next header" field in Fragment header. In this case,
1537 * *offset is meaningless and fragment offset is stored in *fragoff if fragoff
1538 * isn't NULL.
1539 *
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001540 */
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001541int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
1542 int target, unsigned short *fragoff)
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001543{
Arnaldo Carvalho de Melo6b88dd92007-03-19 22:29:03 -03001544 unsigned int start = skb_network_offset(skb) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001545 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001546 unsigned int len = skb->len - start;
1547
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001548 if (fragoff)
1549 *fragoff = 0;
1550
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001551 while (nexthdr != target) {
1552 struct ipv6_opt_hdr _hdr, *hp;
1553 unsigned int hdrlen;
1554
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001555 if ((!ipv6_ext_hdr(nexthdr)) || nexthdr == NEXTHDR_NONE) {
1556 if (target < 0)
1557 break;
Patrick McHardy6d381632006-10-24 16:15:10 -07001558 return -ENOENT;
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001559 }
1560
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001561 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
1562 if (hp == NULL)
Patrick McHardy6d381632006-10-24 16:15:10 -07001563 return -EBADMSG;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001564 if (nexthdr == NEXTHDR_FRAGMENT) {
Al Viroe69a4adc2006-11-14 20:56:00 -08001565 unsigned short _frag_off;
1566 __be16 *fp;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001567 fp = skb_header_pointer(skb,
1568 start+offsetof(struct frag_hdr,
1569 frag_off),
1570 sizeof(_frag_off),
1571 &_frag_off);
1572 if (fp == NULL)
Patrick McHardy6d381632006-10-24 16:15:10 -07001573 return -EBADMSG;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001574
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001575 _frag_off = ntohs(*fp) & ~0x7;
1576 if (_frag_off) {
1577 if (target < 0 &&
1578 ((!ipv6_ext_hdr(hp->nexthdr)) ||
Patrick McHardy337dde72006-11-14 19:49:13 -08001579 hp->nexthdr == NEXTHDR_NONE)) {
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001580 if (fragoff)
1581 *fragoff = _frag_off;
1582 return hp->nexthdr;
1583 }
Patrick McHardy6d381632006-10-24 16:15:10 -07001584 return -ENOENT;
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001585 }
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001586 hdrlen = 8;
1587 } else if (nexthdr == NEXTHDR_AUTH)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001588 hdrlen = (hp->hdrlen + 2) << 2;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001589 else
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001590 hdrlen = ipv6_optlen(hp);
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001591
1592 nexthdr = hp->nexthdr;
1593 len -= hdrlen;
1594 start += hdrlen;
1595 }
1596
1597 *offset = start;
Patrick McHardyb777e0c2006-01-05 12:21:16 -08001598 return nexthdr;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001599}
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601EXPORT_SYMBOL(ip6t_register_table);
1602EXPORT_SYMBOL(ip6t_unregister_table);
1603EXPORT_SYMBOL(ip6t_do_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604EXPORT_SYMBOL(ip6t_ext_hdr);
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -07001605EXPORT_SYMBOL(ipv6_find_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001607module_init(ip6_tables_init);
1608module_exit(ip6_tables_fini);