15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/socket.h>
19 #include <linux/net.h>
22 #include <linux/string.h>
26 #include <linux/slab.h>
27 #include <linux/audit.h>
30 #include <linux/netfilter/x_tables.h>
32 #include <linux/netfilter_ipv4/ip_tables.h>
33 #include <linux/netfilter_ipv6/ip6_tables.h>
34 #include <linux/netfilter_arp/arp_tables.h>
40 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
52 struct mutex compat_mutex;
59 static struct xt_af *xt;
70 static const unsigned int xt_jumpstack_multiplier = 2;
105 for (i = 0; i <
n; i++) {
161 for (i = 0; i <
n; i++) {
197 return ERR_PTR(-
EINTR);
202 if (try_module_get(m->
me)) {
227 request_module(
"%st_%s", xt_prefix[nfproto], name);
242 return ERR_PTR(-
EINTR);
247 if (try_module_get(t->
me)) {
270 if (IS_ERR(target)) {
271 request_module(
"%st_%s", xt_prefix[af], name);
299 static int target_revfn(
u8 af,
const char *name,
u8 revision,
int *bestp)
323 int have_rev, best = -1;
330 have_rev = target_revfn(af, name, revision, &best);
332 have_rev = match_revfn(af, name, revision, &best);
348 static char *textify_hooks(
char *
buf,
size_t size,
unsigned int mask)
350 static const char *
const names[] = {
351 "PREROUTING",
"INPUT",
"FORWARD",
352 "OUTPUT",
"POSTROUTING",
"BROUTING",
361 if (!(mask & (1 << i)))
363 res =
snprintf(p, size,
"%s%s", np ?
"/" :
"", names[i]);
380 par->
match->matchsize != -1) {
385 pr_err(
"%s_tables: %s.%u match: invalid size "
386 "%u (kernel) != (user) %u\n",
388 par->
match->revision,
394 pr_err(
"%s_tables: %s match: only valid in %s table, not %s\n",
400 char used[64], allow[64];
402 pr_err(
"%s_tables: %s match: used from hooks %s, but only "
405 textify_hooks(used,
sizeof(used), par->
hook_mask),
406 textify_hooks(allow,
sizeof(allow), par->
match->hooks));
409 if (par->
match->proto && (par->
match->proto != proto || inv_proto)) {
410 pr_err(
"%s_tables: %s match: only valid for protocol %u\n",
416 ret = par->
match->checkentry(par);
432 if (!xp->compat_tab) {
441 if (xp->cur >= xp->number)
445 delta += xp->compat_tab[xp->cur - 1].delta;
446 xp->compat_tab[xp->cur].offset =
offset;
447 xp->compat_tab[xp->cur].delta =
delta;
453 void xt_compat_flush_offsets(
u_int8_t af)
455 if (xt[af].compat_tab) {
456 vfree(xt[af].compat_tab);
457 xt[af].compat_tab =
NULL;
464 int xt_compat_calc_jump(
u_int8_t af,
unsigned int offset)
469 while (left <=
right) {
470 mid = (left +
right) >> 1;
471 if (offset > tmp[mid].offset)
473 else if (offset < tmp[mid].offset)
476 return mid ? tmp[mid - 1].
delta : 0;
478 return left ? tmp[left - 1].
delta : 0;
482 void xt_compat_init_offsets(
u_int8_t af,
unsigned int number)
484 xt[af].number = number;
489 int xt_compat_match_offset(
const struct xt_match *match)
496 int xt_compat_match_from_user(
struct xt_entry_match *m,
void **dstptr,
500 struct compat_xt_entry_match *cm = (
struct compat_xt_entry_match *)m;
501 int pad, off = xt_compat_match_offset(match);
505 memcpy(m, cm,
sizeof(*cm));
506 if (match->compat_from_user)
507 match->compat_from_user(m->
data, cm->data);
509 memcpy(m->
data, cm->data, msize -
sizeof(*cm));
515 m->
u.
user.match_size = msize;
524 void __user **dstptr,
unsigned int *size)
527 struct compat_xt_entry_match
__user *cm = *dstptr;
528 int off = xt_compat_match_offset(match);
532 put_user(msize, &cm->u.user.match_size) ||
537 if (match->compat_to_user) {
538 if (match->compat_to_user((
void __user *)cm->data, m->
data))
558 pr_err(
"%s_tables: %s.%u target: invalid size "
559 "%u (kernel) != (user) %u\n",
567 pr_err(
"%s_tables: %s target: only valid in %s table, not %s\n",
573 char used[64], allow[64];
575 pr_err(
"%s_tables: %s target: used from hooks %s, but only "
578 textify_hooks(used,
sizeof(used), par->
hook_mask),
579 textify_hooks(allow,
sizeof(allow), par->
target->hooks));
582 if (par->
target->proto && (par->
target->proto != proto || inv_proto)) {
583 pr_err(
"%s_tables: %s target: only valid for protocol %u\n",
589 ret = par->
target->checkentry(par);
601 int xt_compat_target_offset(
const struct xt_target *target)
608 void xt_compat_target_from_user(
struct xt_entry_target *t,
void **dstptr,
612 struct compat_xt_entry_target *
ct = (
struct compat_xt_entry_target *)t;
613 int pad, off = xt_compat_target_offset(target);
614 u_int16_t tsize = ct->u.user.target_size;
617 memcpy(t, ct,
sizeof(*ct));
618 if (target->compat_from_user)
619 target->compat_from_user(t->
data, ct->data);
621 memcpy(t->
data, ct->data, tsize -
sizeof(*ct));
627 t->
u.
user.target_size = tsize;
635 void __user **dstptr,
unsigned int *size)
638 struct compat_xt_entry_target
__user *ct = *dstptr;
639 int off = xt_compat_target_offset(target);
643 put_user(tsize, &ct->u.user.target_size) ||
648 if (target->compat_to_user) {
649 if (target->compat_to_user((
void __user *)ct->data, t->
data))
736 return ERR_PTR(-
EINTR);
739 if (
strcmp(t->
name, name) == 0 && try_module_get(t->
me))
778 size =
sizeof(
void **) * nr_cpu_ids;
809 unsigned int num_counters,
816 ret = xt_jumpstack_alloc(newinfo);
827 if (num_counters != private->number) {
828 pr_debug(
"num_counters != table->private->number (%u/%u)\n",
829 num_counters, private->number);
903 private->initial_entries =
private->number;
905 list_add(&table->
list, &net->xt.tables[table->
af]);
932 #ifdef CONFIG_PROC_FS
933 struct xt_names_priv {
937 static void *xt_table_seq_start(
struct seq_file *seq, loff_t *
pos)
940 struct net *
net = seq_file_net(seq);
947 static void *xt_table_seq_next(
struct seq_file *seq,
void *
v, loff_t *
pos)
950 struct net *
net = seq_file_net(seq);
956 static void xt_table_seq_stop(
struct seq_file *seq,
void *
v)
964 static int xt_table_seq_show(
struct seq_file *seq,
void *
v)
975 .
start = xt_table_seq_start,
976 .next = xt_table_seq_next,
977 .stop = xt_table_seq_stop,
978 .show = xt_table_seq_show,
984 struct xt_names_priv *
priv;
987 sizeof(
struct xt_names_priv));
990 priv->af = (
unsigned long)PDE(inode)->data;
997 .open = xt_table_open,
1007 struct nf_mttg_trav {
1014 MTTG_TRAV_NFP_UNSPEC,
1019 static void *xt_mttg_seq_next(
struct seq_file *seq,
void *v, loff_t *ppos,
1022 static const uint8_t next_class[] = {
1023 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1024 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1026 struct nf_mttg_trav *trav = seq->
private;
1028 switch (trav->class) {
1029 case MTTG_TRAV_INIT:
1030 trav->class = MTTG_TRAV_NFP_UNSPEC;
1032 trav->head = trav->curr = is_target ?
1035 case MTTG_TRAV_NFP_UNSPEC:
1036 trav->curr = trav->curr->next;
1037 if (trav->curr != trav->head)
1041 trav->head = trav->curr = is_target ?
1042 &xt[trav->nfproto].
target : &xt[trav->nfproto].
match;
1043 trav->class = next_class[trav->class];
1045 case MTTG_TRAV_NFP_SPEC:
1046 trav->curr = trav->curr->next;
1047 if (trav->curr != trav->head)
1059 static void *xt_mttg_seq_start(
struct seq_file *seq, loff_t *
pos,
1062 struct nf_mttg_trav *trav = seq->
private;
1065 trav->class = MTTG_TRAV_INIT;
1066 for (j = 0; j < *
pos; ++
j)
1067 if (xt_mttg_seq_next(seq,
NULL,
NULL, is_target) ==
NULL)
1072 static void xt_mttg_seq_stop(
struct seq_file *seq,
void *v)
1074 struct nf_mttg_trav *trav = seq->
private;
1076 switch (trav->class) {
1077 case MTTG_TRAV_NFP_UNSPEC:
1080 case MTTG_TRAV_NFP_SPEC:
1086 static void *xt_match_seq_start(
struct seq_file *seq, loff_t *pos)
1088 return xt_mttg_seq_start(seq, pos,
false);
1091 static void *xt_match_seq_next(
struct seq_file *seq,
void *v, loff_t *ppos)
1093 return xt_mttg_seq_next(seq, v, ppos,
false);
1096 static int xt_match_seq_show(
struct seq_file *seq,
void *v)
1098 const struct nf_mttg_trav *trav = seq->
private;
1101 switch (trav->class) {
1102 case MTTG_TRAV_NFP_UNSPEC:
1103 case MTTG_TRAV_NFP_SPEC:
1104 if (trav->curr == trav->head)
1107 return (*match->
name ==
'\0') ? 0 :
1114 .
start = xt_match_seq_start,
1115 .next = xt_match_seq_next,
1116 .stop = xt_mttg_seq_stop,
1117 .show = xt_match_seq_show,
1120 static int xt_match_open(
struct inode *inode,
struct file *file)
1123 struct nf_mttg_trav *trav;
1130 ret =
seq_open(file, &xt_match_seq_ops);
1138 trav->nfproto = (
unsigned long)PDE(inode)->data;
1144 .open = xt_match_open,
1150 static void *xt_target_seq_start(
struct seq_file *seq, loff_t *pos)
1152 return xt_mttg_seq_start(seq, pos,
true);
1155 static void *xt_target_seq_next(
struct seq_file *seq,
void *v, loff_t *ppos)
1157 return xt_mttg_seq_next(seq, v, ppos,
true);
1160 static int xt_target_seq_show(
struct seq_file *seq,
void *v)
1162 const struct nf_mttg_trav *trav = seq->
private;
1165 switch (trav->class) {
1166 case MTTG_TRAV_NFP_UNSPEC:
1167 case MTTG_TRAV_NFP_SPEC:
1168 if (trav->curr == trav->head)
1171 return (*target->
name ==
'\0') ? 0 :
1178 .
start = xt_target_seq_start,
1179 .next = xt_target_seq_next,
1180 .stop = xt_mttg_seq_stop,
1181 .show = xt_target_seq_show,
1184 static int xt_target_open(
struct inode *inode,
struct file *file)
1187 struct nf_mttg_trav *trav;
1194 ret =
seq_open(file, &xt_target_seq_ops);
1202 trav->nfproto = (
unsigned long)PDE(inode)->data;
1208 .open = xt_target_open,
1214 #define FORMAT_TABLES "_tables_names"
1215 #define FORMAT_MATCHES "_tables_matches"
1216 #define FORMAT_TARGETS "_tables_targets"
1233 struct nf_hook_ops *
ops;
1240 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1242 if (!(hook_mask & 1))
1245 ops[
i].owner = table->
me;
1246 ops[
i].pf = table->
af;
1255 return ERR_PTR(ret);
1276 #ifdef CONFIG_PROC_FS
1285 #ifdef CONFIG_PROC_FS
1286 strlcpy(buf, xt_prefix[af],
sizeof(buf));
1287 strlcat(buf, FORMAT_TABLES,
sizeof(buf));
1289 (
void *)(
unsigned long)af);
1293 strlcpy(buf, xt_prefix[af],
sizeof(buf));
1294 strlcat(buf, FORMAT_MATCHES,
sizeof(buf));
1296 (
void *)(
unsigned long)af);
1298 goto out_remove_tables;
1300 strlcpy(buf, xt_prefix[af],
sizeof(buf));
1301 strlcat(buf, FORMAT_TARGETS,
sizeof(buf));
1303 (
void *)(
unsigned long)af);
1305 goto out_remove_matches;
1310 #ifdef CONFIG_PROC_FS
1312 strlcpy(buf, xt_prefix[af],
sizeof(buf));
1313 strlcat(buf, FORMAT_MATCHES,
sizeof(buf));
1317 strlcpy(buf, xt_prefix[af],
sizeof(buf));
1318 strlcat(buf, FORMAT_TABLES,
sizeof(buf));
1328 #ifdef CONFIG_PROC_FS
1331 strlcpy(buf, xt_prefix[af],
sizeof(buf));
1332 strlcat(buf, FORMAT_TABLES,
sizeof(buf));
1335 strlcpy(buf, xt_prefix[af],
sizeof(buf));
1336 strlcat(buf, FORMAT_TARGETS,
sizeof(buf));
1339 strlcpy(buf, xt_prefix[af],
sizeof(buf));
1340 strlcat(buf, FORMAT_MATCHES,
sizeof(buf));
1351 INIT_LIST_HEAD(&net->xt.tables[i]);
1356 .init = xt_net_init,
1359 static int __init xt_init(
void)
1374 #ifdef CONFIG_COMPAT
1376 xt[
i].compat_tab =
NULL;
1378 INIT_LIST_HEAD(&xt[i].target);
1379 INIT_LIST_HEAD(&xt[i].match);
1387 static void __exit xt_fini(
void)