]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0-only | |
2 | /* | |
3 | * Packet matching code. | |
4 | * | |
5 | * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling | |
6 | * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org> | |
7 | * Copyright (c) 2006-2010 Patrick McHardy <kaber@trash.net> | |
8 | */ | |
9 | ||
10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
11 | ||
12 | #include <linux/kernel.h> | |
13 | #include <linux/capability.h> | |
14 | #include <linux/in.h> | |
15 | #include <linux/skbuff.h> | |
16 | #include <linux/kmod.h> | |
17 | #include <linux/vmalloc.h> | |
18 | #include <linux/netdevice.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/poison.h> | |
21 | #include <linux/icmpv6.h> | |
22 | #include <net/ipv6.h> | |
23 | #include <net/compat.h> | |
24 | #include <linux/uaccess.h> | |
25 | #include <linux/mutex.h> | |
26 | #include <linux/proc_fs.h> | |
27 | #include <linux/err.h> | |
28 | #include <linux/cpumask.h> | |
29 | ||
30 | #include <linux/netfilter_ipv6/ip6_tables.h> | |
31 | #include <linux/netfilter/x_tables.h> | |
32 | #include <net/netfilter/nf_log.h> | |
33 | #include "../../netfilter/xt_repldata.h" | |
34 | ||
35 | MODULE_LICENSE("GPL"); | |
36 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); | |
37 | MODULE_DESCRIPTION("IPv6 packet filter"); | |
38 | MODULE_ALIAS("ip6t_icmp6"); | |
39 | ||
40 | void *ip6t_alloc_initial_table(const struct xt_table *info) | |
41 | { | |
42 | return xt_alloc_initial_table(ip6t, IP6T); | |
43 | } | |
44 | EXPORT_SYMBOL_GPL(ip6t_alloc_initial_table); | |
45 | ||
46 | /* Returns whether matches rule or not. */ | |
47 | /* Performance critical - called for every packet */ | |
48 | static inline bool | |
49 | ip6_packet_match(const struct sk_buff *skb, | |
50 | const char *indev, | |
51 | const char *outdev, | |
52 | const struct ip6t_ip6 *ip6info, | |
53 | unsigned int *protoff, | |
54 | u16 *fragoff, bool *hotdrop) | |
55 | { | |
56 | unsigned long ret; | |
57 | const struct ipv6hdr *ipv6 = ipv6_hdr(skb); | |
58 | ||
59 | if (NF_INVF(ip6info, IP6T_INV_SRCIP, | |
60 | ipv6_masked_addr_cmp(&ipv6->saddr, &ip6info->smsk, | |
61 | &ip6info->src)) || | |
62 | NF_INVF(ip6info, IP6T_INV_DSTIP, | |
63 | ipv6_masked_addr_cmp(&ipv6->daddr, &ip6info->dmsk, | |
64 | &ip6info->dst))) | |
65 | return false; | |
66 | ||
67 | ret = ifname_compare_aligned(indev, ip6info->iniface, ip6info->iniface_mask); | |
68 | ||
69 | if (NF_INVF(ip6info, IP6T_INV_VIA_IN, ret != 0)) | |
70 | return false; | |
71 | ||
72 | ret = ifname_compare_aligned(outdev, ip6info->outiface, ip6info->outiface_mask); | |
73 | ||
74 | if (NF_INVF(ip6info, IP6T_INV_VIA_OUT, ret != 0)) | |
75 | return false; | |
76 | ||
77 | /* ... might want to do something with class and flowlabel here ... */ | |
78 | ||
79 | /* look for the desired protocol header */ | |
80 | if (ip6info->flags & IP6T_F_PROTO) { | |
81 | int protohdr; | |
82 | unsigned short _frag_off; | |
83 | ||
84 | protohdr = ipv6_find_hdr(skb, protoff, -1, &_frag_off, NULL); | |
85 | if (protohdr < 0) { | |
86 | if (_frag_off == 0) | |
87 | *hotdrop = true; | |
88 | return false; | |
89 | } | |
90 | *fragoff = _frag_off; | |
91 | ||
92 | if (ip6info->proto == protohdr) { | |
93 | if (ip6info->invflags & IP6T_INV_PROTO) | |
94 | return false; | |
95 | ||
96 | return true; | |
97 | } | |
98 | ||
99 | /* We need match for the '-p all', too! */ | |
100 | if ((ip6info->proto != 0) && | |
101 | !(ip6info->invflags & IP6T_INV_PROTO)) | |
102 | return false; | |
103 | } | |
104 | return true; | |
105 | } | |
106 | ||
107 | /* should be ip6 safe */ | |
108 | static bool | |
109 | ip6_checkentry(const struct ip6t_ip6 *ipv6) | |
110 | { | |
111 | if (ipv6->flags & ~IP6T_F_MASK) | |
112 | return false; | |
113 | if (ipv6->invflags & ~IP6T_INV_MASK) | |
114 | return false; | |
115 | ||
116 | return true; | |
117 | } | |
118 | ||
119 | static unsigned int | |
120 | ip6t_error(struct sk_buff *skb, const struct xt_action_param *par) | |
121 | { | |
122 | net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo); | |
123 | ||
124 | return NF_DROP; | |
125 | } | |
126 | ||
127 | static inline struct ip6t_entry * | |
128 | get_entry(const void *base, unsigned int offset) | |
129 | { | |
130 | return (struct ip6t_entry *)(base + offset); | |
131 | } | |
132 | ||
133 | /* All zeroes == unconditional rule. */ | |
134 | /* Mildly perf critical (only if packet tracing is on) */ | |
135 | static inline bool unconditional(const struct ip6t_entry *e) | |
136 | { | |
137 | static const struct ip6t_ip6 uncond; | |
138 | ||
139 | return e->target_offset == sizeof(struct ip6t_entry) && | |
140 | memcmp(&e->ipv6, &uncond, sizeof(uncond)) == 0; | |
141 | } | |
142 | ||
143 | static inline const struct xt_entry_target * | |
144 | ip6t_get_target_c(const struct ip6t_entry *e) | |
145 | { | |
146 | return ip6t_get_target((struct ip6t_entry *)e); | |
147 | } | |
148 | ||
149 | #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) | |
150 | /* This cries for unification! */ | |
151 | static const char *const hooknames[] = { | |
152 | [NF_INET_PRE_ROUTING] = "PREROUTING", | |
153 | [NF_INET_LOCAL_IN] = "INPUT", | |
154 | [NF_INET_FORWARD] = "FORWARD", | |
155 | [NF_INET_LOCAL_OUT] = "OUTPUT", | |
156 | [NF_INET_POST_ROUTING] = "POSTROUTING", | |
157 | }; | |
158 | ||
159 | enum nf_ip_trace_comments { | |
160 | NF_IP6_TRACE_COMMENT_RULE, | |
161 | NF_IP6_TRACE_COMMENT_RETURN, | |
162 | NF_IP6_TRACE_COMMENT_POLICY, | |
163 | }; | |
164 | ||
165 | static const char *const comments[] = { | |
166 | [NF_IP6_TRACE_COMMENT_RULE] = "rule", | |
167 | [NF_IP6_TRACE_COMMENT_RETURN] = "return", | |
168 | [NF_IP6_TRACE_COMMENT_POLICY] = "policy", | |
169 | }; | |
170 | ||
171 | static const struct nf_loginfo trace_loginfo = { | |
172 | .type = NF_LOG_TYPE_LOG, | |
173 | .u = { | |
174 | .log = { | |
175 | .level = LOGLEVEL_WARNING, | |
176 | .logflags = NF_LOG_DEFAULT_MASK, | |
177 | }, | |
178 | }, | |
179 | }; | |
180 | ||
181 | /* Mildly perf critical (only if packet tracing is on) */ | |
182 | static inline int | |
183 | get_chainname_rulenum(const struct ip6t_entry *s, const struct ip6t_entry *e, | |
184 | const char *hookname, const char **chainname, | |
185 | const char **comment, unsigned int *rulenum) | |
186 | { | |
187 | const struct xt_standard_target *t = (void *)ip6t_get_target_c(s); | |
188 | ||
189 | if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) { | |
190 | /* Head of user chain: ERROR target with chainname */ | |
191 | *chainname = t->target.data; | |
192 | (*rulenum) = 0; | |
193 | } else if (s == e) { | |
194 | (*rulenum)++; | |
195 | ||
196 | if (unconditional(s) && | |
197 | strcmp(t->target.u.kernel.target->name, | |
198 | XT_STANDARD_TARGET) == 0 && | |
199 | t->verdict < 0) { | |
200 | /* Tail of chains: STANDARD target (return/policy) */ | |
201 | *comment = *chainname == hookname | |
202 | ? comments[NF_IP6_TRACE_COMMENT_POLICY] | |
203 | : comments[NF_IP6_TRACE_COMMENT_RETURN]; | |
204 | } | |
205 | return 1; | |
206 | } else | |
207 | (*rulenum)++; | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
212 | static void trace_packet(struct net *net, | |
213 | const struct sk_buff *skb, | |
214 | unsigned int hook, | |
215 | const struct net_device *in, | |
216 | const struct net_device *out, | |
217 | const char *tablename, | |
218 | const struct xt_table_info *private, | |
219 | const struct ip6t_entry *e) | |
220 | { | |
221 | const struct ip6t_entry *root; | |
222 | const char *hookname, *chainname, *comment; | |
223 | const struct ip6t_entry *iter; | |
224 | unsigned int rulenum = 0; | |
225 | ||
226 | root = get_entry(private->entries, private->hook_entry[hook]); | |
227 | ||
228 | hookname = chainname = hooknames[hook]; | |
229 | comment = comments[NF_IP6_TRACE_COMMENT_RULE]; | |
230 | ||
231 | xt_entry_foreach(iter, root, private->size - private->hook_entry[hook]) | |
232 | if (get_chainname_rulenum(iter, e, hookname, | |
233 | &chainname, &comment, &rulenum) != 0) | |
234 | break; | |
235 | ||
236 | nf_log_trace(net, AF_INET6, hook, skb, in, out, &trace_loginfo, | |
237 | "TRACE: %s:%s:%s:%u ", | |
238 | tablename, chainname, comment, rulenum); | |
239 | } | |
240 | #endif | |
241 | ||
242 | static inline struct ip6t_entry * | |
243 | ip6t_next_entry(const struct ip6t_entry *entry) | |
244 | { | |
245 | return (void *)entry + entry->next_offset; | |
246 | } | |
247 | ||
248 | /* Returns one of the generic firewall policies, like NF_ACCEPT. */ | |
249 | unsigned int | |
250 | ip6t_do_table(struct sk_buff *skb, | |
251 | const struct nf_hook_state *state, | |
252 | struct xt_table *table) | |
253 | { | |
254 | unsigned int hook = state->hook; | |
255 | static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long)))); | |
256 | /* Initializing verdict to NF_DROP keeps gcc happy. */ | |
257 | unsigned int verdict = NF_DROP; | |
258 | const char *indev, *outdev; | |
259 | const void *table_base; | |
260 | struct ip6t_entry *e, **jumpstack; | |
261 | unsigned int stackidx, cpu; | |
262 | const struct xt_table_info *private; | |
263 | struct xt_action_param acpar; | |
264 | unsigned int addend; | |
265 | ||
266 | /* Initialization */ | |
267 | stackidx = 0; | |
268 | indev = state->in ? state->in->name : nulldevname; | |
269 | outdev = state->out ? state->out->name : nulldevname; | |
270 | /* We handle fragments by dealing with the first fragment as | |
271 | * if it was a normal packet. All other fragments are treated | |
272 | * normally, except that they will NEVER match rules that ask | |
273 | * things we don't know, ie. tcp syn flag or ports). If the | |
274 | * rule is also a fragment-specific rule, non-fragments won't | |
275 | * match it. */ | |
276 | acpar.fragoff = 0; | |
277 | acpar.hotdrop = false; | |
278 | acpar.state = state; | |
279 | ||
280 | WARN_ON(!(table->valid_hooks & (1 << hook))); | |
281 | ||
282 | local_bh_disable(); | |
283 | addend = xt_write_recseq_begin(); | |
284 | private = READ_ONCE(table->private); /* Address dependency. */ | |
285 | cpu = smp_processor_id(); | |
286 | table_base = private->entries; | |
287 | jumpstack = (struct ip6t_entry **)private->jumpstack[cpu]; | |
288 | ||
289 | /* Switch to alternate jumpstack if we're being invoked via TEE. | |
290 | * TEE issues XT_CONTINUE verdict on original skb so we must not | |
291 | * clobber the jumpstack. | |
292 | * | |
293 | * For recursion via REJECT or SYNPROXY the stack will be clobbered | |
294 | * but it is no problem since absolute verdict is issued by these. | |
295 | */ | |
296 | if (static_key_false(&xt_tee_enabled)) | |
297 | jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated); | |
298 | ||
299 | e = get_entry(table_base, private->hook_entry[hook]); | |
300 | ||
301 | do { | |
302 | const struct xt_entry_target *t; | |
303 | const struct xt_entry_match *ematch; | |
304 | struct xt_counters *counter; | |
305 | ||
306 | WARN_ON(!e); | |
307 | acpar.thoff = 0; | |
308 | if (!ip6_packet_match(skb, indev, outdev, &e->ipv6, | |
309 | &acpar.thoff, &acpar.fragoff, &acpar.hotdrop)) { | |
310 | no_match: | |
311 | e = ip6t_next_entry(e); | |
312 | continue; | |
313 | } | |
314 | ||
315 | xt_ematch_foreach(ematch, e) { | |
316 | acpar.match = ematch->u.kernel.match; | |
317 | acpar.matchinfo = ematch->data; | |
318 | if (!acpar.match->match(skb, &acpar)) | |
319 | goto no_match; | |
320 | } | |
321 | ||
322 | counter = xt_get_this_cpu_counter(&e->counters); | |
323 | ADD_COUNTER(*counter, skb->len, 1); | |
324 | ||
325 | t = ip6t_get_target_c(e); | |
326 | WARN_ON(!t->u.kernel.target); | |
327 | ||
328 | #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) | |
329 | /* The packet is traced: log it */ | |
330 | if (unlikely(skb->nf_trace)) | |
331 | trace_packet(state->net, skb, hook, state->in, | |
332 | state->out, table->name, private, e); | |
333 | #endif | |
334 | /* Standard target? */ | |
335 | if (!t->u.kernel.target->target) { | |
336 | int v; | |
337 | ||
338 | v = ((struct xt_standard_target *)t)->verdict; | |
339 | if (v < 0) { | |
340 | /* Pop from stack? */ | |
341 | if (v != XT_RETURN) { | |
342 | verdict = (unsigned int)(-v) - 1; | |
343 | break; | |
344 | } | |
345 | if (stackidx == 0) | |
346 | e = get_entry(table_base, | |
347 | private->underflow[hook]); | |
348 | else | |
349 | e = ip6t_next_entry(jumpstack[--stackidx]); | |
350 | continue; | |
351 | } | |
352 | if (table_base + v != ip6t_next_entry(e) && | |
353 | !(e->ipv6.flags & IP6T_F_GOTO)) { | |
354 | if (unlikely(stackidx >= private->stacksize)) { | |
355 | verdict = NF_DROP; | |
356 | break; | |
357 | } | |
358 | jumpstack[stackidx++] = e; | |
359 | } | |
360 | ||
361 | e = get_entry(table_base, v); | |
362 | continue; | |
363 | } | |
364 | ||
365 | acpar.target = t->u.kernel.target; | |
366 | acpar.targinfo = t->data; | |
367 | ||
368 | verdict = t->u.kernel.target->target(skb, &acpar); | |
369 | if (verdict == XT_CONTINUE) | |
370 | e = ip6t_next_entry(e); | |
371 | else | |
372 | /* Verdict */ | |
373 | break; | |
374 | } while (!acpar.hotdrop); | |
375 | ||
376 | xt_write_recseq_end(addend); | |
377 | local_bh_enable(); | |
378 | ||
379 | if (acpar.hotdrop) | |
380 | return NF_DROP; | |
381 | else return verdict; | |
382 | } | |
383 | ||
384 | static bool next_offset_ok(const struct xt_table_info *t, unsigned int newpos) | |
385 | { | |
386 | if (newpos > t->size - sizeof(struct ip6t_entry)) | |
387 | return false; | |
388 | ||
389 | if (newpos % __alignof__(struct ip6t_entry) != 0) | |
390 | return false; | |
391 | ||
392 | return true; | |
393 | } | |
394 | ||
395 | /* Figures out from what hook each rule can be called: returns 0 if | |
396 | there are loops. Puts hook bitmask in comefrom. */ | |
397 | static int | |
398 | mark_source_chains(const struct xt_table_info *newinfo, | |
399 | unsigned int valid_hooks, void *entry0, | |
400 | unsigned int *offsets) | |
401 | { | |
402 | unsigned int hook; | |
403 | ||
404 | /* No recursion; use packet counter to save back ptrs (reset | |
405 | to 0 as we leave), and comefrom to save source hook bitmask */ | |
406 | for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) { | |
407 | unsigned int pos = newinfo->hook_entry[hook]; | |
408 | struct ip6t_entry *e = entry0 + pos; | |
409 | ||
410 | if (!(valid_hooks & (1 << hook))) | |
411 | continue; | |
412 | ||
413 | /* Set initial back pointer. */ | |
414 | e->counters.pcnt = pos; | |
415 | ||
416 | for (;;) { | |
417 | const struct xt_standard_target *t | |
418 | = (void *)ip6t_get_target_c(e); | |
419 | int visited = e->comefrom & (1 << hook); | |
420 | ||
421 | if (e->comefrom & (1 << NF_INET_NUMHOOKS)) | |
422 | return 0; | |
423 | ||
424 | e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS)); | |
425 | ||
426 | /* Unconditional return/END. */ | |
427 | if ((unconditional(e) && | |
428 | (strcmp(t->target.u.user.name, | |
429 | XT_STANDARD_TARGET) == 0) && | |
430 | t->verdict < 0) || visited) { | |
431 | unsigned int oldpos, size; | |
432 | ||
433 | /* Return: backtrack through the last | |
434 | big jump. */ | |
435 | do { | |
436 | e->comefrom ^= (1<<NF_INET_NUMHOOKS); | |
437 | oldpos = pos; | |
438 | pos = e->counters.pcnt; | |
439 | e->counters.pcnt = 0; | |
440 | ||
441 | /* We're at the start. */ | |
442 | if (pos == oldpos) | |
443 | goto next; | |
444 | ||
445 | e = entry0 + pos; | |
446 | } while (oldpos == pos + e->next_offset); | |
447 | ||
448 | /* Move along one */ | |
449 | size = e->next_offset; | |
450 | if (!next_offset_ok(newinfo, pos + size)) | |
451 | return 0; | |
452 | e = entry0 + pos + size; | |
453 | if (pos + size >= newinfo->size) | |
454 | return 0; | |
455 | e->counters.pcnt = pos; | |
456 | pos += size; | |
457 | } else { | |
458 | int newpos = t->verdict; | |
459 | ||
460 | if (strcmp(t->target.u.user.name, | |
461 | XT_STANDARD_TARGET) == 0 && | |
462 | newpos >= 0) { | |
463 | /* This a jump; chase it. */ | |
464 | if (!xt_find_jump_offset(offsets, newpos, | |
465 | newinfo->number)) | |
466 | return 0; | |
467 | } else { | |
468 | /* ... this is a fallthru */ | |
469 | newpos = pos + e->next_offset; | |
470 | if (newpos >= newinfo->size) | |
471 | return 0; | |
472 | } | |
473 | ||
474 | if (!next_offset_ok(newinfo, newpos)) | |
475 | return 0; | |
476 | ||
477 | e = entry0 + newpos; | |
478 | e->counters.pcnt = pos; | |
479 | pos = newpos; | |
480 | } | |
481 | } | |
482 | next: ; | |
483 | } | |
484 | return 1; | |
485 | } | |
486 | ||
487 | static void cleanup_match(struct xt_entry_match *m, struct net *net) | |
488 | { | |
489 | struct xt_mtdtor_param par; | |
490 | ||
491 | par.net = net; | |
492 | par.match = m->u.kernel.match; | |
493 | par.matchinfo = m->data; | |
494 | par.family = NFPROTO_IPV6; | |
495 | if (par.match->destroy != NULL) | |
496 | par.match->destroy(&par); | |
497 | module_put(par.match->me); | |
498 | } | |
499 | ||
500 | static int check_match(struct xt_entry_match *m, struct xt_mtchk_param *par) | |
501 | { | |
502 | const struct ip6t_ip6 *ipv6 = par->entryinfo; | |
503 | ||
504 | par->match = m->u.kernel.match; | |
505 | par->matchinfo = m->data; | |
506 | ||
507 | return xt_check_match(par, m->u.match_size - sizeof(*m), | |
508 | ipv6->proto, ipv6->invflags & IP6T_INV_PROTO); | |
509 | } | |
510 | ||
511 | static int | |
512 | find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par) | |
513 | { | |
514 | struct xt_match *match; | |
515 | int ret; | |
516 | ||
517 | match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name, | |
518 | m->u.user.revision); | |
519 | if (IS_ERR(match)) | |
520 | return PTR_ERR(match); | |
521 | ||
522 | m->u.kernel.match = match; | |
523 | ||
524 | ret = check_match(m, par); | |
525 | if (ret) | |
526 | goto err; | |
527 | ||
528 | return 0; | |
529 | err: | |
530 | module_put(m->u.kernel.match->me); | |
531 | return ret; | |
532 | } | |
533 | ||
534 | static int check_target(struct ip6t_entry *e, struct net *net, const char *name) | |
535 | { | |
536 | struct xt_entry_target *t = ip6t_get_target(e); | |
537 | struct xt_tgchk_param par = { | |
538 | .net = net, | |
539 | .table = name, | |
540 | .entryinfo = e, | |
541 | .target = t->u.kernel.target, | |
542 | .targinfo = t->data, | |
543 | .hook_mask = e->comefrom, | |
544 | .family = NFPROTO_IPV6, | |
545 | }; | |
546 | ||
547 | return xt_check_target(&par, t->u.target_size - sizeof(*t), | |
548 | e->ipv6.proto, | |
549 | e->ipv6.invflags & IP6T_INV_PROTO); | |
550 | } | |
551 | ||
552 | static int | |
553 | find_check_entry(struct ip6t_entry *e, struct net *net, const char *name, | |
554 | unsigned int size, | |
555 | struct xt_percpu_counter_alloc_state *alloc_state) | |
556 | { | |
557 | struct xt_entry_target *t; | |
558 | struct xt_target *target; | |
559 | int ret; | |
560 | unsigned int j; | |
561 | struct xt_mtchk_param mtpar; | |
562 | struct xt_entry_match *ematch; | |
563 | ||
564 | if (!xt_percpu_counter_alloc(alloc_state, &e->counters)) | |
565 | return -ENOMEM; | |
566 | ||
567 | j = 0; | |
568 | memset(&mtpar, 0, sizeof(mtpar)); | |
569 | mtpar.net = net; | |
570 | mtpar.table = name; | |
571 | mtpar.entryinfo = &e->ipv6; | |
572 | mtpar.hook_mask = e->comefrom; | |
573 | mtpar.family = NFPROTO_IPV6; | |
574 | xt_ematch_foreach(ematch, e) { | |
575 | ret = find_check_match(ematch, &mtpar); | |
576 | if (ret != 0) | |
577 | goto cleanup_matches; | |
578 | ++j; | |
579 | } | |
580 | ||
581 | t = ip6t_get_target(e); | |
582 | target = xt_request_find_target(NFPROTO_IPV6, t->u.user.name, | |
583 | t->u.user.revision); | |
584 | if (IS_ERR(target)) { | |
585 | ret = PTR_ERR(target); | |
586 | goto cleanup_matches; | |
587 | } | |
588 | t->u.kernel.target = target; | |
589 | ||
590 | ret = check_target(e, net, name); | |
591 | if (ret) | |
592 | goto err; | |
593 | return 0; | |
594 | err: | |
595 | module_put(t->u.kernel.target->me); | |
596 | cleanup_matches: | |
597 | xt_ematch_foreach(ematch, e) { | |
598 | if (j-- == 0) | |
599 | break; | |
600 | cleanup_match(ematch, net); | |
601 | } | |
602 | ||
603 | xt_percpu_counter_free(&e->counters); | |
604 | ||
605 | return ret; | |
606 | } | |
607 | ||
608 | static bool check_underflow(const struct ip6t_entry *e) | |
609 | { | |
610 | const struct xt_entry_target *t; | |
611 | unsigned int verdict; | |
612 | ||
613 | if (!unconditional(e)) | |
614 | return false; | |
615 | t = ip6t_get_target_c(e); | |
616 | if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0) | |
617 | return false; | |
618 | verdict = ((struct xt_standard_target *)t)->verdict; | |
619 | verdict = -verdict - 1; | |
620 | return verdict == NF_DROP || verdict == NF_ACCEPT; | |
621 | } | |
622 | ||
623 | static int | |
624 | check_entry_size_and_hooks(struct ip6t_entry *e, | |
625 | struct xt_table_info *newinfo, | |
626 | const unsigned char *base, | |
627 | const unsigned char *limit, | |
628 | const unsigned int *hook_entries, | |
629 | const unsigned int *underflows, | |
630 | unsigned int valid_hooks) | |
631 | { | |
632 | unsigned int h; | |
633 | int err; | |
634 | ||
635 | if ((unsigned long)e % __alignof__(struct ip6t_entry) != 0 || | |
636 | (unsigned char *)e + sizeof(struct ip6t_entry) >= limit || | |
637 | (unsigned char *)e + e->next_offset > limit) | |
638 | return -EINVAL; | |
639 | ||
640 | if (e->next_offset | |
641 | < sizeof(struct ip6t_entry) + sizeof(struct xt_entry_target)) | |
642 | return -EINVAL; | |
643 | ||
644 | if (!ip6_checkentry(&e->ipv6)) | |
645 | return -EINVAL; | |
646 | ||
647 | err = xt_check_entry_offsets(e, e->elems, e->target_offset, | |
648 | e->next_offset); | |
649 | if (err) | |
650 | return err; | |
651 | ||
652 | /* Check hooks & underflows */ | |
653 | for (h = 0; h < NF_INET_NUMHOOKS; h++) { | |
654 | if (!(valid_hooks & (1 << h))) | |
655 | continue; | |
656 | if ((unsigned char *)e - base == hook_entries[h]) | |
657 | newinfo->hook_entry[h] = hook_entries[h]; | |
658 | if ((unsigned char *)e - base == underflows[h]) { | |
659 | if (!check_underflow(e)) | |
660 | return -EINVAL; | |
661 | ||
662 | newinfo->underflow[h] = underflows[h]; | |
663 | } | |
664 | } | |
665 | ||
666 | /* Clear counters and comefrom */ | |
667 | e->counters = ((struct xt_counters) { 0, 0 }); | |
668 | e->comefrom = 0; | |
669 | return 0; | |
670 | } | |
671 | ||
672 | static void cleanup_entry(struct ip6t_entry *e, struct net *net) | |
673 | { | |
674 | struct xt_tgdtor_param par; | |
675 | struct xt_entry_target *t; | |
676 | struct xt_entry_match *ematch; | |
677 | ||
678 | /* Cleanup all matches */ | |
679 | xt_ematch_foreach(ematch, e) | |
680 | cleanup_match(ematch, net); | |
681 | t = ip6t_get_target(e); | |
682 | ||
683 | par.net = net; | |
684 | par.target = t->u.kernel.target; | |
685 | par.targinfo = t->data; | |
686 | par.family = NFPROTO_IPV6; | |
687 | if (par.target->destroy != NULL) | |
688 | par.target->destroy(&par); | |
689 | module_put(par.target->me); | |
690 | xt_percpu_counter_free(&e->counters); | |
691 | } | |
692 | ||
693 | /* Checks and translates the user-supplied table segment (held in | |
694 | newinfo) */ | |
695 | static int | |
696 | translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0, | |
697 | const struct ip6t_replace *repl) | |
698 | { | |
699 | struct xt_percpu_counter_alloc_state alloc_state = { 0 }; | |
700 | struct ip6t_entry *iter; | |
701 | unsigned int *offsets; | |
702 | unsigned int i; | |
703 | int ret = 0; | |
704 | ||
705 | newinfo->size = repl->size; | |
706 | newinfo->number = repl->num_entries; | |
707 | ||
708 | /* Init all hooks to impossible value. */ | |
709 | for (i = 0; i < NF_INET_NUMHOOKS; i++) { | |
710 | newinfo->hook_entry[i] = 0xFFFFFFFF; | |
711 | newinfo->underflow[i] = 0xFFFFFFFF; | |
712 | } | |
713 | ||
714 | offsets = xt_alloc_entry_offsets(newinfo->number); | |
715 | if (!offsets) | |
716 | return -ENOMEM; | |
717 | i = 0; | |
718 | /* Walk through entries, checking offsets. */ | |
719 | xt_entry_foreach(iter, entry0, newinfo->size) { | |
720 | ret = check_entry_size_and_hooks(iter, newinfo, entry0, | |
721 | entry0 + repl->size, | |
722 | repl->hook_entry, | |
723 | repl->underflow, | |
724 | repl->valid_hooks); | |
725 | if (ret != 0) | |
726 | goto out_free; | |
727 | if (i < repl->num_entries) | |
728 | offsets[i] = (void *)iter - entry0; | |
729 | ++i; | |
730 | if (strcmp(ip6t_get_target(iter)->u.user.name, | |
731 | XT_ERROR_TARGET) == 0) | |
732 | ++newinfo->stacksize; | |
733 | } | |
734 | ||
735 | ret = -EINVAL; | |
736 | if (i != repl->num_entries) | |
737 | goto out_free; | |
738 | ||
739 | ret = xt_check_table_hooks(newinfo, repl->valid_hooks); | |
740 | if (ret) | |
741 | goto out_free; | |
742 | ||
743 | if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) { | |
744 | ret = -ELOOP; | |
745 | goto out_free; | |
746 | } | |
747 | kvfree(offsets); | |
748 | ||
749 | /* Finally, each sanity check must pass */ | |
750 | i = 0; | |
751 | xt_entry_foreach(iter, entry0, newinfo->size) { | |
752 | ret = find_check_entry(iter, net, repl->name, repl->size, | |
753 | &alloc_state); | |
754 | if (ret != 0) | |
755 | break; | |
756 | ++i; | |
757 | } | |
758 | ||
759 | if (ret != 0) { | |
760 | xt_entry_foreach(iter, entry0, newinfo->size) { | |
761 | if (i-- == 0) | |
762 | break; | |
763 | cleanup_entry(iter, net); | |
764 | } | |
765 | return ret; | |
766 | } | |
767 | ||
768 | return ret; | |
769 | out_free: | |
770 | kvfree(offsets); | |
771 | return ret; | |
772 | } | |
773 | ||
774 | static void | |
775 | get_counters(const struct xt_table_info *t, | |
776 | struct xt_counters counters[]) | |
777 | { | |
778 | struct ip6t_entry *iter; | |
779 | unsigned int cpu; | |
780 | unsigned int i; | |
781 | ||
782 | for_each_possible_cpu(cpu) { | |
783 | seqcount_t *s = &per_cpu(xt_recseq, cpu); | |
784 | ||
785 | i = 0; | |
786 | xt_entry_foreach(iter, t->entries, t->size) { | |
787 | struct xt_counters *tmp; | |
788 | u64 bcnt, pcnt; | |
789 | unsigned int start; | |
790 | ||
791 | tmp = xt_get_per_cpu_counter(&iter->counters, cpu); | |
792 | do { | |
793 | start = read_seqcount_begin(s); | |
794 | bcnt = tmp->bcnt; | |
795 | pcnt = tmp->pcnt; | |
796 | } while (read_seqcount_retry(s, start)); | |
797 | ||
798 | ADD_COUNTER(counters[i], bcnt, pcnt); | |
799 | ++i; | |
800 | cond_resched(); | |
801 | } | |
802 | } | |
803 | } | |
804 | ||
805 | static void get_old_counters(const struct xt_table_info *t, | |
806 | struct xt_counters counters[]) | |
807 | { | |
808 | struct ip6t_entry *iter; | |
809 | unsigned int cpu, i; | |
810 | ||
811 | for_each_possible_cpu(cpu) { | |
812 | i = 0; | |
813 | xt_entry_foreach(iter, t->entries, t->size) { | |
814 | const struct xt_counters *tmp; | |
815 | ||
816 | tmp = xt_get_per_cpu_counter(&iter->counters, cpu); | |
817 | ADD_COUNTER(counters[i], tmp->bcnt, tmp->pcnt); | |
818 | ++i; | |
819 | } | |
820 | cond_resched(); | |
821 | } | |
822 | } | |
823 | ||
824 | static struct xt_counters *alloc_counters(const struct xt_table *table) | |
825 | { | |
826 | unsigned int countersize; | |
827 | struct xt_counters *counters; | |
828 | const struct xt_table_info *private = table->private; | |
829 | ||
830 | /* We need atomic snapshot of counters: rest doesn't change | |
831 | (other than comefrom, which userspace doesn't care | |
832 | about). */ | |
833 | countersize = sizeof(struct xt_counters) * private->number; | |
834 | counters = vzalloc(countersize); | |
835 | ||
836 | if (counters == NULL) | |
837 | return ERR_PTR(-ENOMEM); | |
838 | ||
839 | get_counters(private, counters); | |
840 | ||
841 | return counters; | |
842 | } | |
843 | ||
844 | static int | |
845 | copy_entries_to_user(unsigned int total_size, | |
846 | const struct xt_table *table, | |
847 | void __user *userptr) | |
848 | { | |
849 | unsigned int off, num; | |
850 | const struct ip6t_entry *e; | |
851 | struct xt_counters *counters; | |
852 | const struct xt_table_info *private = table->private; | |
853 | int ret = 0; | |
854 | const void *loc_cpu_entry; | |
855 | ||
856 | counters = alloc_counters(table); | |
857 | if (IS_ERR(counters)) | |
858 | return PTR_ERR(counters); | |
859 | ||
860 | loc_cpu_entry = private->entries; | |
861 | ||
862 | /* FIXME: use iterator macros --RR */ | |
863 | /* ... then go back and fix counters and names */ | |
864 | for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){ | |
865 | unsigned int i; | |
866 | const struct xt_entry_match *m; | |
867 | const struct xt_entry_target *t; | |
868 | ||
869 | e = loc_cpu_entry + off; | |
870 | if (copy_to_user(userptr + off, e, sizeof(*e))) { | |
871 | ret = -EFAULT; | |
872 | goto free_counters; | |
873 | } | |
874 | if (copy_to_user(userptr + off | |
875 | + offsetof(struct ip6t_entry, counters), | |
876 | &counters[num], | |
877 | sizeof(counters[num])) != 0) { | |
878 | ret = -EFAULT; | |
879 | goto free_counters; | |
880 | } | |
881 | ||
882 | for (i = sizeof(struct ip6t_entry); | |
883 | i < e->target_offset; | |
884 | i += m->u.match_size) { | |
885 | m = (void *)e + i; | |
886 | ||
887 | if (xt_match_to_user(m, userptr + off + i)) { | |
888 | ret = -EFAULT; | |
889 | goto free_counters; | |
890 | } | |
891 | } | |
892 | ||
893 | t = ip6t_get_target_c(e); | |
894 | if (xt_target_to_user(t, userptr + off + e->target_offset)) { | |
895 | ret = -EFAULT; | |
896 | goto free_counters; | |
897 | } | |
898 | } | |
899 | ||
900 | free_counters: | |
901 | vfree(counters); | |
902 | return ret; | |
903 | } | |
904 | ||
905 | #ifdef CONFIG_NETFILTER_XTABLES_COMPAT | |
906 | static void compat_standard_from_user(void *dst, const void *src) | |
907 | { | |
908 | int v = *(compat_int_t *)src; | |
909 | ||
910 | if (v > 0) | |
911 | v += xt_compat_calc_jump(AF_INET6, v); | |
912 | memcpy(dst, &v, sizeof(v)); | |
913 | } | |
914 | ||
915 | static int compat_standard_to_user(void __user *dst, const void *src) | |
916 | { | |
917 | compat_int_t cv = *(int *)src; | |
918 | ||
919 | if (cv > 0) | |
920 | cv -= xt_compat_calc_jump(AF_INET6, cv); | |
921 | return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0; | |
922 | } | |
923 | ||
924 | static int compat_calc_entry(const struct ip6t_entry *e, | |
925 | const struct xt_table_info *info, | |
926 | const void *base, struct xt_table_info *newinfo) | |
927 | { | |
928 | const struct xt_entry_match *ematch; | |
929 | const struct xt_entry_target *t; | |
930 | unsigned int entry_offset; | |
931 | int off, i, ret; | |
932 | ||
933 | off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry); | |
934 | entry_offset = (void *)e - base; | |
935 | xt_ematch_foreach(ematch, e) | |
936 | off += xt_compat_match_offset(ematch->u.kernel.match); | |
937 | t = ip6t_get_target_c(e); | |
938 | off += xt_compat_target_offset(t->u.kernel.target); | |
939 | newinfo->size -= off; | |
940 | ret = xt_compat_add_offset(AF_INET6, entry_offset, off); | |
941 | if (ret) | |
942 | return ret; | |
943 | ||
944 | for (i = 0; i < NF_INET_NUMHOOKS; i++) { | |
945 | if (info->hook_entry[i] && | |
946 | (e < (struct ip6t_entry *)(base + info->hook_entry[i]))) | |
947 | newinfo->hook_entry[i] -= off; | |
948 | if (info->underflow[i] && | |
949 | (e < (struct ip6t_entry *)(base + info->underflow[i]))) | |
950 | newinfo->underflow[i] -= off; | |
951 | } | |
952 | return 0; | |
953 | } | |
954 | ||
955 | static int compat_table_info(const struct xt_table_info *info, | |
956 | struct xt_table_info *newinfo) | |
957 | { | |
958 | struct ip6t_entry *iter; | |
959 | const void *loc_cpu_entry; | |
960 | int ret; | |
961 | ||
962 | if (!newinfo || !info) | |
963 | return -EINVAL; | |
964 | ||
965 | /* we dont care about newinfo->entries */ | |
966 | memcpy(newinfo, info, offsetof(struct xt_table_info, entries)); | |
967 | newinfo->initial_entries = 0; | |
968 | loc_cpu_entry = info->entries; | |
969 | ret = xt_compat_init_offsets(AF_INET6, info->number); | |
970 | if (ret) | |
971 | return ret; | |
972 | xt_entry_foreach(iter, loc_cpu_entry, info->size) { | |
973 | ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo); | |
974 | if (ret != 0) | |
975 | return ret; | |
976 | } | |
977 | return 0; | |
978 | } | |
979 | #endif | |
980 | ||
981 | static int get_info(struct net *net, void __user *user, const int *len) | |
982 | { | |
983 | char name[XT_TABLE_MAXNAMELEN]; | |
984 | struct xt_table *t; | |
985 | int ret; | |
986 | ||
987 | if (*len != sizeof(struct ip6t_getinfo)) | |
988 | return -EINVAL; | |
989 | ||
990 | if (copy_from_user(name, user, sizeof(name)) != 0) | |
991 | return -EFAULT; | |
992 | ||
993 | name[XT_TABLE_MAXNAMELEN-1] = '\0'; | |
994 | #ifdef CONFIG_NETFILTER_XTABLES_COMPAT | |
995 | if (in_compat_syscall()) | |
996 | xt_compat_lock(AF_INET6); | |
997 | #endif | |
998 | t = xt_request_find_table_lock(net, AF_INET6, name); | |
999 | if (!IS_ERR(t)) { | |
1000 | struct ip6t_getinfo info; | |
1001 | const struct xt_table_info *private = t->private; | |
1002 | #ifdef CONFIG_NETFILTER_XTABLES_COMPAT | |
1003 | struct xt_table_info tmp; | |
1004 | ||
1005 | if (in_compat_syscall()) { | |
1006 | ret = compat_table_info(private, &tmp); | |
1007 | xt_compat_flush_offsets(AF_INET6); | |
1008 | private = &tmp; | |
1009 | } | |
1010 | #endif | |
1011 | memset(&info, 0, sizeof(info)); | |
1012 | info.valid_hooks = t->valid_hooks; | |
1013 | memcpy(info.hook_entry, private->hook_entry, | |
1014 | sizeof(info.hook_entry)); | |
1015 | memcpy(info.underflow, private->underflow, | |
1016 | sizeof(info.underflow)); | |
1017 | info.num_entries = private->number; | |
1018 | info.size = private->size; | |
1019 | strcpy(info.name, name); | |
1020 | ||
1021 | if (copy_to_user(user, &info, *len) != 0) | |
1022 | ret = -EFAULT; | |
1023 | else | |
1024 | ret = 0; | |
1025 | ||
1026 | xt_table_unlock(t); | |
1027 | module_put(t->me); | |
1028 | } else | |
1029 | ret = PTR_ERR(t); | |
1030 | #ifdef CONFIG_NETFILTER_XTABLES_COMPAT | |
1031 | if (in_compat_syscall()) | |
1032 | xt_compat_unlock(AF_INET6); | |
1033 | #endif | |
1034 | return ret; | |
1035 | } | |
1036 | ||
1037 | static int | |
1038 | get_entries(struct net *net, struct ip6t_get_entries __user *uptr, | |
1039 | const int *len) | |
1040 | { | |
1041 | int ret; | |
1042 | struct ip6t_get_entries get; | |
1043 | struct xt_table *t; | |
1044 | ||
1045 | if (*len < sizeof(get)) | |
1046 | return -EINVAL; | |
1047 | if (copy_from_user(&get, uptr, sizeof(get)) != 0) | |
1048 | return -EFAULT; | |
1049 | if (*len != sizeof(struct ip6t_get_entries) + get.size) | |
1050 | return -EINVAL; | |
1051 | ||
1052 | get.name[sizeof(get.name) - 1] = '\0'; | |
1053 | ||
1054 | t = xt_find_table_lock(net, AF_INET6, get.name); | |
1055 | if (!IS_ERR(t)) { | |
1056 | struct xt_table_info *private = t->private; | |
1057 | if (get.size == private->size) | |
1058 | ret = copy_entries_to_user(private->size, | |
1059 | t, uptr->entrytable); | |
1060 | else | |
1061 | ret = -EAGAIN; | |
1062 | ||
1063 | module_put(t->me); | |
1064 | xt_table_unlock(t); | |
1065 | } else | |
1066 | ret = PTR_ERR(t); | |
1067 | ||
1068 | return ret; | |
1069 | } | |
1070 | ||
1071 | static int | |
1072 | __do_replace(struct net *net, const char *name, unsigned int valid_hooks, | |
1073 | struct xt_table_info *newinfo, unsigned int num_counters, | |
1074 | void __user *counters_ptr) | |
1075 | { | |
1076 | int ret; | |
1077 | struct xt_table *t; | |
1078 | struct xt_table_info *oldinfo; | |
1079 | struct xt_counters *counters; | |
1080 | struct ip6t_entry *iter; | |
1081 | ||
1082 | ret = 0; | |
1083 | counters = xt_counters_alloc(num_counters); | |
1084 | if (!counters) { | |
1085 | ret = -ENOMEM; | |
1086 | goto out; | |
1087 | } | |
1088 | ||
1089 | t = xt_request_find_table_lock(net, AF_INET6, name); | |
1090 | if (IS_ERR(t)) { | |
1091 | ret = PTR_ERR(t); | |
1092 | goto free_newinfo_counters_untrans; | |
1093 | } | |
1094 | ||
1095 | /* You lied! */ | |
1096 | if (valid_hooks != t->valid_hooks) { | |
1097 | ret = -EINVAL; | |
1098 | goto put_module; | |
1099 | } | |
1100 | ||
1101 | oldinfo = xt_replace_table(t, num_counters, newinfo, &ret); | |
1102 | if (!oldinfo) | |
1103 | goto put_module; | |
1104 | ||
1105 | /* Update module usage count based on number of rules */ | |
1106 | if ((oldinfo->number > oldinfo->initial_entries) || | |
1107 | (newinfo->number <= oldinfo->initial_entries)) | |
1108 | module_put(t->me); | |
1109 | if ((oldinfo->number > oldinfo->initial_entries) && | |
1110 | (newinfo->number <= oldinfo->initial_entries)) | |
1111 | module_put(t->me); | |
1112 | ||
1113 | xt_table_unlock(t); | |
1114 | ||
1115 | get_old_counters(oldinfo, counters); | |
1116 | ||
1117 | /* Decrease module usage counts and free resource */ | |
1118 | xt_entry_foreach(iter, oldinfo->entries, oldinfo->size) | |
1119 | cleanup_entry(iter, net); | |
1120 | ||
1121 | xt_free_table_info(oldinfo); | |
1122 | if (copy_to_user(counters_ptr, counters, | |
1123 | sizeof(struct xt_counters) * num_counters) != 0) { | |
1124 | /* Silent error, can't fail, new table is already in place */ | |
1125 | net_warn_ratelimited("ip6tables: counters copy to user failed while replacing table\n"); | |
1126 | } | |
1127 | vfree(counters); | |
1128 | return ret; | |
1129 | ||
1130 | put_module: | |
1131 | module_put(t->me); | |
1132 | xt_table_unlock(t); | |
1133 | free_newinfo_counters_untrans: | |
1134 | vfree(counters); | |
1135 | out: | |
1136 | return ret; | |
1137 | } | |
1138 | ||
1139 | static int | |
1140 | do_replace(struct net *net, sockptr_t arg, unsigned int len) | |
1141 | { | |
1142 | int ret; | |
1143 | struct ip6t_replace tmp; | |
1144 | struct xt_table_info *newinfo; | |
1145 | void *loc_cpu_entry; | |
1146 | struct ip6t_entry *iter; | |
1147 | ||
1148 | if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0) | |
1149 | return -EFAULT; | |
1150 | ||
1151 | /* overflow check */ | |
1152 | if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters)) | |
1153 | return -ENOMEM; | |
1154 | if (tmp.num_counters == 0) | |
1155 | return -EINVAL; | |
1156 | ||
1157 | tmp.name[sizeof(tmp.name)-1] = 0; | |
1158 | ||
1159 | newinfo = xt_alloc_table_info(tmp.size); | |
1160 | if (!newinfo) | |
1161 | return -ENOMEM; | |
1162 | ||
1163 | loc_cpu_entry = newinfo->entries; | |
1164 | if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp), | |
1165 | tmp.size) != 0) { | |
1166 | ret = -EFAULT; | |
1167 | goto free_newinfo; | |
1168 | } | |
1169 | ||
1170 | ret = translate_table(net, newinfo, loc_cpu_entry, &tmp); | |
1171 | if (ret != 0) | |
1172 | goto free_newinfo; | |
1173 | ||
1174 | ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo, | |
1175 | tmp.num_counters, tmp.counters); | |
1176 | if (ret) | |
1177 | goto free_newinfo_untrans; | |
1178 | return 0; | |
1179 | ||
1180 | free_newinfo_untrans: | |
1181 | xt_entry_foreach(iter, loc_cpu_entry, newinfo->size) | |
1182 | cleanup_entry(iter, net); | |
1183 | free_newinfo: | |
1184 | xt_free_table_info(newinfo); | |
1185 | return ret; | |
1186 | } | |
1187 | ||
1188 | static int | |
1189 | do_add_counters(struct net *net, sockptr_t arg, unsigned int len) | |
1190 | { | |
1191 | unsigned int i; | |
1192 | struct xt_counters_info tmp; | |
1193 | struct xt_counters *paddc; | |
1194 | struct xt_table *t; | |
1195 | const struct xt_table_info *private; | |
1196 | int ret = 0; | |
1197 | struct ip6t_entry *iter; | |
1198 | unsigned int addend; | |
1199 | ||
1200 | paddc = xt_copy_counters(arg, len, &tmp); | |
1201 | if (IS_ERR(paddc)) | |
1202 | return PTR_ERR(paddc); | |
1203 | t = xt_find_table_lock(net, AF_INET6, tmp.name); | |
1204 | if (IS_ERR(t)) { | |
1205 | ret = PTR_ERR(t); | |
1206 | goto free; | |
1207 | } | |
1208 | ||
1209 | local_bh_disable(); | |
1210 | private = t->private; | |
1211 | if (private->number != tmp.num_counters) { | |
1212 | ret = -EINVAL; | |
1213 | goto unlock_up_free; | |
1214 | } | |
1215 | ||
1216 | i = 0; | |
1217 | addend = xt_write_recseq_begin(); | |
1218 | xt_entry_foreach(iter, private->entries, private->size) { | |
1219 | struct xt_counters *tmp; | |
1220 | ||
1221 | tmp = xt_get_this_cpu_counter(&iter->counters); | |
1222 | ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt); | |
1223 | ++i; | |
1224 | } | |
1225 | xt_write_recseq_end(addend); | |
1226 | unlock_up_free: | |
1227 | local_bh_enable(); | |
1228 | xt_table_unlock(t); | |
1229 | module_put(t->me); | |
1230 | free: | |
1231 | vfree(paddc); | |
1232 | ||
1233 | return ret; | |
1234 | } | |
1235 | ||
1236 | #ifdef CONFIG_NETFILTER_XTABLES_COMPAT | |
1237 | struct compat_ip6t_replace { | |
1238 | char name[XT_TABLE_MAXNAMELEN]; | |
1239 | u32 valid_hooks; | |
1240 | u32 num_entries; | |
1241 | u32 size; | |
1242 | u32 hook_entry[NF_INET_NUMHOOKS]; | |
1243 | u32 underflow[NF_INET_NUMHOOKS]; | |
1244 | u32 num_counters; | |
1245 | compat_uptr_t counters; /* struct xt_counters * */ | |
1246 | struct compat_ip6t_entry entries[]; | |
1247 | }; | |
1248 | ||
1249 | static int | |
1250 | compat_copy_entry_to_user(struct ip6t_entry *e, void __user **dstptr, | |
1251 | unsigned int *size, struct xt_counters *counters, | |
1252 | unsigned int i) | |
1253 | { | |
1254 | struct xt_entry_target *t; | |
1255 | struct compat_ip6t_entry __user *ce; | |
1256 | u_int16_t target_offset, next_offset; | |
1257 | compat_uint_t origsize; | |
1258 | const struct xt_entry_match *ematch; | |
1259 | int ret = 0; | |
1260 | ||
1261 | origsize = *size; | |
1262 | ce = *dstptr; | |
1263 | if (copy_to_user(ce, e, sizeof(struct ip6t_entry)) != 0 || | |
1264 | copy_to_user(&ce->counters, &counters[i], | |
1265 | sizeof(counters[i])) != 0) | |
1266 | return -EFAULT; | |
1267 | ||
1268 | *dstptr += sizeof(struct compat_ip6t_entry); | |
1269 | *size -= sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry); | |
1270 | ||
1271 | xt_ematch_foreach(ematch, e) { | |
1272 | ret = xt_compat_match_to_user(ematch, dstptr, size); | |
1273 | if (ret != 0) | |
1274 | return ret; | |
1275 | } | |
1276 | target_offset = e->target_offset - (origsize - *size); | |
1277 | t = ip6t_get_target(e); | |
1278 | ret = xt_compat_target_to_user(t, dstptr, size); | |
1279 | if (ret) | |
1280 | return ret; | |
1281 | next_offset = e->next_offset - (origsize - *size); | |
1282 | if (put_user(target_offset, &ce->target_offset) != 0 || | |
1283 | put_user(next_offset, &ce->next_offset) != 0) | |
1284 | return -EFAULT; | |
1285 | return 0; | |
1286 | } | |
1287 | ||
1288 | static int | |
1289 | compat_find_calc_match(struct xt_entry_match *m, | |
1290 | const struct ip6t_ip6 *ipv6, | |
1291 | int *size) | |
1292 | { | |
1293 | struct xt_match *match; | |
1294 | ||
1295 | match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name, | |
1296 | m->u.user.revision); | |
1297 | if (IS_ERR(match)) | |
1298 | return PTR_ERR(match); | |
1299 | ||
1300 | m->u.kernel.match = match; | |
1301 | *size += xt_compat_match_offset(match); | |
1302 | return 0; | |
1303 | } | |
1304 | ||
1305 | static void compat_release_entry(struct compat_ip6t_entry *e) | |
1306 | { | |
1307 | struct xt_entry_target *t; | |
1308 | struct xt_entry_match *ematch; | |
1309 | ||
1310 | /* Cleanup all matches */ | |
1311 | xt_ematch_foreach(ematch, e) | |
1312 | module_put(ematch->u.kernel.match->me); | |
1313 | t = compat_ip6t_get_target(e); | |
1314 | module_put(t->u.kernel.target->me); | |
1315 | } | |
1316 | ||
1317 | static int | |
1318 | check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e, | |
1319 | struct xt_table_info *newinfo, | |
1320 | unsigned int *size, | |
1321 | const unsigned char *base, | |
1322 | const unsigned char *limit) | |
1323 | { | |
1324 | struct xt_entry_match *ematch; | |
1325 | struct xt_entry_target *t; | |
1326 | struct xt_target *target; | |
1327 | unsigned int entry_offset; | |
1328 | unsigned int j; | |
1329 | int ret, off; | |
1330 | ||
1331 | if ((unsigned long)e % __alignof__(struct compat_ip6t_entry) != 0 || | |
1332 | (unsigned char *)e + sizeof(struct compat_ip6t_entry) >= limit || | |
1333 | (unsigned char *)e + e->next_offset > limit) | |
1334 | return -EINVAL; | |
1335 | ||
1336 | if (e->next_offset < sizeof(struct compat_ip6t_entry) + | |
1337 | sizeof(struct compat_xt_entry_target)) | |
1338 | return -EINVAL; | |
1339 | ||
1340 | if (!ip6_checkentry(&e->ipv6)) | |
1341 | return -EINVAL; | |
1342 | ||
1343 | ret = xt_compat_check_entry_offsets(e, e->elems, | |
1344 | e->target_offset, e->next_offset); | |
1345 | if (ret) | |
1346 | return ret; | |
1347 | ||
1348 | off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry); | |
1349 | entry_offset = (void *)e - (void *)base; | |
1350 | j = 0; | |
1351 | xt_ematch_foreach(ematch, e) { | |
1352 | ret = compat_find_calc_match(ematch, &e->ipv6, &off); | |
1353 | if (ret != 0) | |
1354 | goto release_matches; | |
1355 | ++j; | |
1356 | } | |
1357 | ||
1358 | t = compat_ip6t_get_target(e); | |
1359 | target = xt_request_find_target(NFPROTO_IPV6, t->u.user.name, | |
1360 | t->u.user.revision); | |
1361 | if (IS_ERR(target)) { | |
1362 | ret = PTR_ERR(target); | |
1363 | goto release_matches; | |
1364 | } | |
1365 | t->u.kernel.target = target; | |
1366 | ||
1367 | off += xt_compat_target_offset(target); | |
1368 | *size += off; | |
1369 | ret = xt_compat_add_offset(AF_INET6, entry_offset, off); | |
1370 | if (ret) | |
1371 | goto out; | |
1372 | ||
1373 | return 0; | |
1374 | ||
1375 | out: | |
1376 | module_put(t->u.kernel.target->me); | |
1377 | release_matches: | |
1378 | xt_ematch_foreach(ematch, e) { | |
1379 | if (j-- == 0) | |
1380 | break; | |
1381 | module_put(ematch->u.kernel.match->me); | |
1382 | } | |
1383 | return ret; | |
1384 | } | |
1385 | ||
1386 | static void | |
1387 | compat_copy_entry_from_user(struct compat_ip6t_entry *e, void **dstptr, | |
1388 | unsigned int *size, | |
1389 | struct xt_table_info *newinfo, unsigned char *base) | |
1390 | { | |
1391 | struct xt_entry_target *t; | |
1392 | struct ip6t_entry *de; | |
1393 | unsigned int origsize; | |
1394 | int h; | |
1395 | struct xt_entry_match *ematch; | |
1396 | ||
1397 | origsize = *size; | |
1398 | de = *dstptr; | |
1399 | memcpy(de, e, sizeof(struct ip6t_entry)); | |
1400 | memcpy(&de->counters, &e->counters, sizeof(e->counters)); | |
1401 | ||
1402 | *dstptr += sizeof(struct ip6t_entry); | |
1403 | *size += sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry); | |
1404 | ||
1405 | xt_ematch_foreach(ematch, e) | |
1406 | xt_compat_match_from_user(ematch, dstptr, size); | |
1407 | ||
1408 | de->target_offset = e->target_offset - (origsize - *size); | |
1409 | t = compat_ip6t_get_target(e); | |
1410 | xt_compat_target_from_user(t, dstptr, size); | |
1411 | ||
1412 | de->next_offset = e->next_offset - (origsize - *size); | |
1413 | for (h = 0; h < NF_INET_NUMHOOKS; h++) { | |
1414 | if ((unsigned char *)de - base < newinfo->hook_entry[h]) | |
1415 | newinfo->hook_entry[h] -= origsize - *size; | |
1416 | if ((unsigned char *)de - base < newinfo->underflow[h]) | |
1417 | newinfo->underflow[h] -= origsize - *size; | |
1418 | } | |
1419 | } | |
1420 | ||
1421 | static int | |
1422 | translate_compat_table(struct net *net, | |
1423 | struct xt_table_info **pinfo, | |
1424 | void **pentry0, | |
1425 | const struct compat_ip6t_replace *compatr) | |
1426 | { | |
1427 | unsigned int i, j; | |
1428 | struct xt_table_info *newinfo, *info; | |
1429 | void *pos, *entry0, *entry1; | |
1430 | struct compat_ip6t_entry *iter0; | |
1431 | struct ip6t_replace repl; | |
1432 | unsigned int size; | |
1433 | int ret; | |
1434 | ||
1435 | info = *pinfo; | |
1436 | entry0 = *pentry0; | |
1437 | size = compatr->size; | |
1438 | info->number = compatr->num_entries; | |
1439 | ||
1440 | j = 0; | |
1441 | xt_compat_lock(AF_INET6); | |
1442 | ret = xt_compat_init_offsets(AF_INET6, compatr->num_entries); | |
1443 | if (ret) | |
1444 | goto out_unlock; | |
1445 | /* Walk through entries, checking offsets. */ | |
1446 | xt_entry_foreach(iter0, entry0, compatr->size) { | |
1447 | ret = check_compat_entry_size_and_hooks(iter0, info, &size, | |
1448 | entry0, | |
1449 | entry0 + compatr->size); | |
1450 | if (ret != 0) | |
1451 | goto out_unlock; | |
1452 | ++j; | |
1453 | } | |
1454 | ||
1455 | ret = -EINVAL; | |
1456 | if (j != compatr->num_entries) | |
1457 | goto out_unlock; | |
1458 | ||
1459 | ret = -ENOMEM; | |
1460 | newinfo = xt_alloc_table_info(size); | |
1461 | if (!newinfo) | |
1462 | goto out_unlock; | |
1463 | ||
1464 | memset(newinfo->entries, 0, size); | |
1465 | ||
1466 | newinfo->number = compatr->num_entries; | |
1467 | for (i = 0; i < NF_INET_NUMHOOKS; i++) { | |
1468 | newinfo->hook_entry[i] = compatr->hook_entry[i]; | |
1469 | newinfo->underflow[i] = compatr->underflow[i]; | |
1470 | } | |
1471 | entry1 = newinfo->entries; | |
1472 | pos = entry1; | |
1473 | size = compatr->size; | |
1474 | xt_entry_foreach(iter0, entry0, compatr->size) | |
1475 | compat_copy_entry_from_user(iter0, &pos, &size, | |
1476 | newinfo, entry1); | |
1477 | ||
1478 | /* all module references in entry0 are now gone. */ | |
1479 | xt_compat_flush_offsets(AF_INET6); | |
1480 | xt_compat_unlock(AF_INET6); | |
1481 | ||
1482 | memcpy(&repl, compatr, sizeof(*compatr)); | |
1483 | ||
1484 | for (i = 0; i < NF_INET_NUMHOOKS; i++) { | |
1485 | repl.hook_entry[i] = newinfo->hook_entry[i]; | |
1486 | repl.underflow[i] = newinfo->underflow[i]; | |
1487 | } | |
1488 | ||
1489 | repl.num_counters = 0; | |
1490 | repl.counters = NULL; | |
1491 | repl.size = newinfo->size; | |
1492 | ret = translate_table(net, newinfo, entry1, &repl); | |
1493 | if (ret) | |
1494 | goto free_newinfo; | |
1495 | ||
1496 | *pinfo = newinfo; | |
1497 | *pentry0 = entry1; | |
1498 | xt_free_table_info(info); | |
1499 | return 0; | |
1500 | ||
1501 | free_newinfo: | |
1502 | xt_free_table_info(newinfo); | |
1503 | return ret; | |
1504 | out_unlock: | |
1505 | xt_compat_flush_offsets(AF_INET6); | |
1506 | xt_compat_unlock(AF_INET6); | |
1507 | xt_entry_foreach(iter0, entry0, compatr->size) { | |
1508 | if (j-- == 0) | |
1509 | break; | |
1510 | compat_release_entry(iter0); | |
1511 | } | |
1512 | return ret; | |
1513 | } | |
1514 | ||
1515 | static int | |
1516 | compat_do_replace(struct net *net, sockptr_t arg, unsigned int len) | |
1517 | { | |
1518 | int ret; | |
1519 | struct compat_ip6t_replace tmp; | |
1520 | struct xt_table_info *newinfo; | |
1521 | void *loc_cpu_entry; | |
1522 | struct ip6t_entry *iter; | |
1523 | ||
1524 | if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0) | |
1525 | return -EFAULT; | |
1526 | ||
1527 | /* overflow check */ | |
1528 | if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters)) | |
1529 | return -ENOMEM; | |
1530 | if (tmp.num_counters == 0) | |
1531 | return -EINVAL; | |
1532 | ||
1533 | tmp.name[sizeof(tmp.name)-1] = 0; | |
1534 | ||
1535 | newinfo = xt_alloc_table_info(tmp.size); | |
1536 | if (!newinfo) | |
1537 | return -ENOMEM; | |
1538 | ||
1539 | loc_cpu_entry = newinfo->entries; | |
1540 | if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp), | |
1541 | tmp.size) != 0) { | |
1542 | ret = -EFAULT; | |
1543 | goto free_newinfo; | |
1544 | } | |
1545 | ||
1546 | ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp); | |
1547 | if (ret != 0) | |
1548 | goto free_newinfo; | |
1549 | ||
1550 | ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo, | |
1551 | tmp.num_counters, compat_ptr(tmp.counters)); | |
1552 | if (ret) | |
1553 | goto free_newinfo_untrans; | |
1554 | return 0; | |
1555 | ||
1556 | free_newinfo_untrans: | |
1557 | xt_entry_foreach(iter, loc_cpu_entry, newinfo->size) | |
1558 | cleanup_entry(iter, net); | |
1559 | free_newinfo: | |
1560 | xt_free_table_info(newinfo); | |
1561 | return ret; | |
1562 | } | |
1563 | ||
1564 | struct compat_ip6t_get_entries { | |
1565 | char name[XT_TABLE_MAXNAMELEN]; | |
1566 | compat_uint_t size; | |
1567 | struct compat_ip6t_entry entrytable[]; | |
1568 | }; | |
1569 | ||
1570 | static int | |
1571 | compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table, | |
1572 | void __user *userptr) | |
1573 | { | |
1574 | struct xt_counters *counters; | |
1575 | const struct xt_table_info *private = table->private; | |
1576 | void __user *pos; | |
1577 | unsigned int size; | |
1578 | int ret = 0; | |
1579 | unsigned int i = 0; | |
1580 | struct ip6t_entry *iter; | |
1581 | ||
1582 | counters = alloc_counters(table); | |
1583 | if (IS_ERR(counters)) | |
1584 | return PTR_ERR(counters); | |
1585 | ||
1586 | pos = userptr; | |
1587 | size = total_size; | |
1588 | xt_entry_foreach(iter, private->entries, total_size) { | |
1589 | ret = compat_copy_entry_to_user(iter, &pos, | |
1590 | &size, counters, i++); | |
1591 | if (ret != 0) | |
1592 | break; | |
1593 | } | |
1594 | ||
1595 | vfree(counters); | |
1596 | return ret; | |
1597 | } | |
1598 | ||
1599 | static int | |
1600 | compat_get_entries(struct net *net, struct compat_ip6t_get_entries __user *uptr, | |
1601 | int *len) | |
1602 | { | |
1603 | int ret; | |
1604 | struct compat_ip6t_get_entries get; | |
1605 | struct xt_table *t; | |
1606 | ||
1607 | if (*len < sizeof(get)) | |
1608 | return -EINVAL; | |
1609 | ||
1610 | if (copy_from_user(&get, uptr, sizeof(get)) != 0) | |
1611 | return -EFAULT; | |
1612 | ||
1613 | if (*len != sizeof(struct compat_ip6t_get_entries) + get.size) | |
1614 | return -EINVAL; | |
1615 | ||
1616 | get.name[sizeof(get.name) - 1] = '\0'; | |
1617 | ||
1618 | xt_compat_lock(AF_INET6); | |
1619 | t = xt_find_table_lock(net, AF_INET6, get.name); | |
1620 | if (!IS_ERR(t)) { | |
1621 | const struct xt_table_info *private = t->private; | |
1622 | struct xt_table_info info; | |
1623 | ret = compat_table_info(private, &info); | |
1624 | if (!ret && get.size == info.size) | |
1625 | ret = compat_copy_entries_to_user(private->size, | |
1626 | t, uptr->entrytable); | |
1627 | else if (!ret) | |
1628 | ret = -EAGAIN; | |
1629 | ||
1630 | xt_compat_flush_offsets(AF_INET6); | |
1631 | module_put(t->me); | |
1632 | xt_table_unlock(t); | |
1633 | } else | |
1634 | ret = PTR_ERR(t); | |
1635 | ||
1636 | xt_compat_unlock(AF_INET6); | |
1637 | return ret; | |
1638 | } | |
1639 | #endif | |
1640 | ||
1641 | static int | |
1642 | do_ip6t_set_ctl(struct sock *sk, int cmd, sockptr_t arg, unsigned int len) | |
1643 | { | |
1644 | int ret; | |
1645 | ||
1646 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) | |
1647 | return -EPERM; | |
1648 | ||
1649 | switch (cmd) { | |
1650 | case IP6T_SO_SET_REPLACE: | |
1651 | #ifdef CONFIG_NETFILTER_XTABLES_COMPAT | |
1652 | if (in_compat_syscall()) | |
1653 | ret = compat_do_replace(sock_net(sk), arg, len); | |
1654 | else | |
1655 | #endif | |
1656 | ret = do_replace(sock_net(sk), arg, len); | |
1657 | break; | |
1658 | ||
1659 | case IP6T_SO_SET_ADD_COUNTERS: | |
1660 | ret = do_add_counters(sock_net(sk), arg, len); | |
1661 | break; | |
1662 | ||
1663 | default: | |
1664 | ret = -EINVAL; | |
1665 | } | |
1666 | ||
1667 | return ret; | |
1668 | } | |
1669 | ||
1670 | static int | |
1671 | do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len) | |
1672 | { | |
1673 | int ret; | |
1674 | ||
1675 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) | |
1676 | return -EPERM; | |
1677 | ||
1678 | switch (cmd) { | |
1679 | case IP6T_SO_GET_INFO: | |
1680 | ret = get_info(sock_net(sk), user, len); | |
1681 | break; | |
1682 | ||
1683 | case IP6T_SO_GET_ENTRIES: | |
1684 | #ifdef CONFIG_NETFILTER_XTABLES_COMPAT | |
1685 | if (in_compat_syscall()) | |
1686 | ret = compat_get_entries(sock_net(sk), user, len); | |
1687 | else | |
1688 | #endif | |
1689 | ret = get_entries(sock_net(sk), user, len); | |
1690 | break; | |
1691 | ||
1692 | case IP6T_SO_GET_REVISION_MATCH: | |
1693 | case IP6T_SO_GET_REVISION_TARGET: { | |
1694 | struct xt_get_revision rev; | |
1695 | int target; | |
1696 | ||
1697 | if (*len != sizeof(rev)) { | |
1698 | ret = -EINVAL; | |
1699 | break; | |
1700 | } | |
1701 | if (copy_from_user(&rev, user, sizeof(rev)) != 0) { | |
1702 | ret = -EFAULT; | |
1703 | break; | |
1704 | } | |
1705 | rev.name[sizeof(rev.name)-1] = 0; | |
1706 | ||
1707 | if (cmd == IP6T_SO_GET_REVISION_TARGET) | |
1708 | target = 1; | |
1709 | else | |
1710 | target = 0; | |
1711 | ||
1712 | try_then_request_module(xt_find_revision(AF_INET6, rev.name, | |
1713 | rev.revision, | |
1714 | target, &ret), | |
1715 | "ip6t_%s", rev.name); | |
1716 | break; | |
1717 | } | |
1718 | ||
1719 | default: | |
1720 | ret = -EINVAL; | |
1721 | } | |
1722 | ||
1723 | return ret; | |
1724 | } | |
1725 | ||
1726 | static void __ip6t_unregister_table(struct net *net, struct xt_table *table) | |
1727 | { | |
1728 | struct xt_table_info *private; | |
1729 | void *loc_cpu_entry; | |
1730 | struct module *table_owner = table->me; | |
1731 | struct ip6t_entry *iter; | |
1732 | ||
1733 | private = xt_unregister_table(table); | |
1734 | ||
1735 | /* Decrease module usage counts and free resources */ | |
1736 | loc_cpu_entry = private->entries; | |
1737 | xt_entry_foreach(iter, loc_cpu_entry, private->size) | |
1738 | cleanup_entry(iter, net); | |
1739 | if (private->number > private->initial_entries) | |
1740 | module_put(table_owner); | |
1741 | xt_free_table_info(private); | |
1742 | } | |
1743 | ||
1744 | int ip6t_register_table(struct net *net, const struct xt_table *table, | |
1745 | const struct ip6t_replace *repl, | |
1746 | const struct nf_hook_ops *template_ops) | |
1747 | { | |
1748 | struct nf_hook_ops *ops; | |
1749 | unsigned int num_ops; | |
1750 | int ret, i; | |
1751 | struct xt_table_info *newinfo; | |
1752 | struct xt_table_info bootstrap = {0}; | |
1753 | void *loc_cpu_entry; | |
1754 | struct xt_table *new_table; | |
1755 | ||
1756 | newinfo = xt_alloc_table_info(repl->size); | |
1757 | if (!newinfo) | |
1758 | return -ENOMEM; | |
1759 | ||
1760 | loc_cpu_entry = newinfo->entries; | |
1761 | memcpy(loc_cpu_entry, repl->entries, repl->size); | |
1762 | ||
1763 | ret = translate_table(net, newinfo, loc_cpu_entry, repl); | |
1764 | if (ret != 0) { | |
1765 | xt_free_table_info(newinfo); | |
1766 | return ret; | |
1767 | } | |
1768 | ||
1769 | new_table = xt_register_table(net, table, &bootstrap, newinfo); | |
1770 | if (IS_ERR(new_table)) { | |
1771 | xt_free_table_info(newinfo); | |
1772 | return PTR_ERR(new_table); | |
1773 | } | |
1774 | ||
1775 | if (!template_ops) | |
1776 | return 0; | |
1777 | ||
1778 | num_ops = hweight32(table->valid_hooks); | |
1779 | if (num_ops == 0) { | |
1780 | ret = -EINVAL; | |
1781 | goto out_free; | |
1782 | } | |
1783 | ||
1784 | ops = kmemdup(template_ops, sizeof(*ops) * num_ops, GFP_KERNEL); | |
1785 | if (!ops) { | |
1786 | ret = -ENOMEM; | |
1787 | goto out_free; | |
1788 | } | |
1789 | ||
1790 | for (i = 0; i < num_ops; i++) | |
1791 | ops[i].priv = new_table; | |
1792 | ||
1793 | new_table->ops = ops; | |
1794 | ||
1795 | ret = nf_register_net_hooks(net, ops, num_ops); | |
1796 | if (ret != 0) | |
1797 | goto out_free; | |
1798 | ||
1799 | return ret; | |
1800 | ||
1801 | out_free: | |
1802 | __ip6t_unregister_table(net, new_table); | |
1803 | return ret; | |
1804 | } | |
1805 | ||
1806 | void ip6t_unregister_table_pre_exit(struct net *net, const char *name) | |
1807 | { | |
1808 | struct xt_table *table = xt_find_table(net, NFPROTO_IPV6, name); | |
1809 | ||
1810 | if (table) | |
1811 | nf_unregister_net_hooks(net, table->ops, hweight32(table->valid_hooks)); | |
1812 | } | |
1813 | ||
1814 | void ip6t_unregister_table_exit(struct net *net, const char *name) | |
1815 | { | |
1816 | struct xt_table *table = xt_find_table(net, NFPROTO_IPV6, name); | |
1817 | ||
1818 | if (table) | |
1819 | __ip6t_unregister_table(net, table); | |
1820 | } | |
1821 | ||
1822 | /* Returns 1 if the type and code is matched by the range, 0 otherwise */ | |
1823 | static inline bool | |
1824 | icmp6_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code, | |
1825 | u_int8_t type, u_int8_t code, | |
1826 | bool invert) | |
1827 | { | |
1828 | return (type == test_type && code >= min_code && code <= max_code) | |
1829 | ^ invert; | |
1830 | } | |
1831 | ||
1832 | static bool | |
1833 | icmp6_match(const struct sk_buff *skb, struct xt_action_param *par) | |
1834 | { | |
1835 | const struct icmp6hdr *ic; | |
1836 | struct icmp6hdr _icmph; | |
1837 | const struct ip6t_icmp *icmpinfo = par->matchinfo; | |
1838 | ||
1839 | /* Must not be a fragment. */ | |
1840 | if (par->fragoff != 0) | |
1841 | return false; | |
1842 | ||
1843 | ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph); | |
1844 | if (ic == NULL) { | |
1845 | /* We've been asked to examine this packet, and we | |
1846 | * can't. Hence, no choice but to drop. | |
1847 | */ | |
1848 | par->hotdrop = true; | |
1849 | return false; | |
1850 | } | |
1851 | ||
1852 | return icmp6_type_code_match(icmpinfo->type, | |
1853 | icmpinfo->code[0], | |
1854 | icmpinfo->code[1], | |
1855 | ic->icmp6_type, ic->icmp6_code, | |
1856 | !!(icmpinfo->invflags&IP6T_ICMP_INV)); | |
1857 | } | |
1858 | ||
1859 | /* Called when user tries to insert an entry of this type. */ | |
1860 | static int icmp6_checkentry(const struct xt_mtchk_param *par) | |
1861 | { | |
1862 | const struct ip6t_icmp *icmpinfo = par->matchinfo; | |
1863 | ||
1864 | /* Must specify no unknown invflags */ | |
1865 | return (icmpinfo->invflags & ~IP6T_ICMP_INV) ? -EINVAL : 0; | |
1866 | } | |
1867 | ||
1868 | /* The built-in targets: standard (NULL) and error. */ | |
1869 | static struct xt_target ip6t_builtin_tg[] __read_mostly = { | |
1870 | { | |
1871 | .name = XT_STANDARD_TARGET, | |
1872 | .targetsize = sizeof(int), | |
1873 | .family = NFPROTO_IPV6, | |
1874 | #ifdef CONFIG_NETFILTER_XTABLES_COMPAT | |
1875 | .compatsize = sizeof(compat_int_t), | |
1876 | .compat_from_user = compat_standard_from_user, | |
1877 | .compat_to_user = compat_standard_to_user, | |
1878 | #endif | |
1879 | }, | |
1880 | { | |
1881 | .name = XT_ERROR_TARGET, | |
1882 | .target = ip6t_error, | |
1883 | .targetsize = XT_FUNCTION_MAXNAMELEN, | |
1884 | .family = NFPROTO_IPV6, | |
1885 | }, | |
1886 | }; | |
1887 | ||
1888 | static struct nf_sockopt_ops ip6t_sockopts = { | |
1889 | .pf = PF_INET6, | |
1890 | .set_optmin = IP6T_BASE_CTL, | |
1891 | .set_optmax = IP6T_SO_SET_MAX+1, | |
1892 | .set = do_ip6t_set_ctl, | |
1893 | .get_optmin = IP6T_BASE_CTL, | |
1894 | .get_optmax = IP6T_SO_GET_MAX+1, | |
1895 | .get = do_ip6t_get_ctl, | |
1896 | .owner = THIS_MODULE, | |
1897 | }; | |
1898 | ||
1899 | static struct xt_match ip6t_builtin_mt[] __read_mostly = { | |
1900 | { | |
1901 | .name = "icmp6", | |
1902 | .match = icmp6_match, | |
1903 | .matchsize = sizeof(struct ip6t_icmp), | |
1904 | .checkentry = icmp6_checkentry, | |
1905 | .proto = IPPROTO_ICMPV6, | |
1906 | .family = NFPROTO_IPV6, | |
1907 | .me = THIS_MODULE, | |
1908 | }, | |
1909 | }; | |
1910 | ||
1911 | static int __net_init ip6_tables_net_init(struct net *net) | |
1912 | { | |
1913 | return xt_proto_init(net, NFPROTO_IPV6); | |
1914 | } | |
1915 | ||
1916 | static void __net_exit ip6_tables_net_exit(struct net *net) | |
1917 | { | |
1918 | xt_proto_fini(net, NFPROTO_IPV6); | |
1919 | } | |
1920 | ||
1921 | static struct pernet_operations ip6_tables_net_ops = { | |
1922 | .init = ip6_tables_net_init, | |
1923 | .exit = ip6_tables_net_exit, | |
1924 | }; | |
1925 | ||
1926 | static int __init ip6_tables_init(void) | |
1927 | { | |
1928 | int ret; | |
1929 | ||
1930 | ret = register_pernet_subsys(&ip6_tables_net_ops); | |
1931 | if (ret < 0) | |
1932 | goto err1; | |
1933 | ||
1934 | /* No one else will be downing sem now, so we won't sleep */ | |
1935 | ret = xt_register_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg)); | |
1936 | if (ret < 0) | |
1937 | goto err2; | |
1938 | ret = xt_register_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt)); | |
1939 | if (ret < 0) | |
1940 | goto err4; | |
1941 | ||
1942 | /* Register setsockopt */ | |
1943 | ret = nf_register_sockopt(&ip6t_sockopts); | |
1944 | if (ret < 0) | |
1945 | goto err5; | |
1946 | ||
1947 | return 0; | |
1948 | ||
1949 | err5: | |
1950 | xt_unregister_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt)); | |
1951 | err4: | |
1952 | xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg)); | |
1953 | err2: | |
1954 | unregister_pernet_subsys(&ip6_tables_net_ops); | |
1955 | err1: | |
1956 | return ret; | |
1957 | } | |
1958 | ||
1959 | static void __exit ip6_tables_fini(void) | |
1960 | { | |
1961 | nf_unregister_sockopt(&ip6t_sockopts); | |
1962 | ||
1963 | xt_unregister_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt)); | |
1964 | xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg)); | |
1965 | unregister_pernet_subsys(&ip6_tables_net_ops); | |
1966 | } | |
1967 | ||
1968 | EXPORT_SYMBOL(ip6t_register_table); | |
1969 | EXPORT_SYMBOL(ip6t_unregister_table_pre_exit); | |
1970 | EXPORT_SYMBOL(ip6t_unregister_table_exit); | |
1971 | EXPORT_SYMBOL(ip6t_do_table); | |
1972 | ||
1973 | module_init(ip6_tables_init); | |
1974 | module_exit(ip6_tables_fini); |