1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #if defined(__GLIBC__) && __GLIBC__ == 2
6 #include <net/ethernet.h>
7 #else
8 #include <linux/if_ether.h>
9 #endif
10 #include <xtables.h>
11 #include <linux/netfilter_ipv4/ipt_realm.h>
12
13 enum {
14 O_REALM = 0,
15 };
16
realm_help(void)17 static void realm_help(void)
18 {
19 printf(
20 "realm match options:\n"
21 "[!] --realm value[/mask]\n"
22 " Match realm\n");
23 }
24
25 static const struct xt_option_entry realm_opts[] = {
26 {.name = "realm", .id = O_REALM, .type = XTTYPE_STRING,
27 .flags = XTOPT_MAND | XTOPT_INVERT},
28 XTOPT_TABLEEND,
29 };
30
31 static const char f_realms[] = "/etc/iproute2/rt_realms";
32 /* array of realms from f_realms[] */
33 static struct xtables_lmap *realms;
34
realm_parse(struct xt_option_call * cb)35 static void realm_parse(struct xt_option_call *cb)
36 {
37 struct xt_realm_info *ri = cb->data;
38 unsigned int id, mask;
39
40 xtables_option_parse(cb);
41 xtables_parse_val_mask(cb, &id, &mask, realms);
42
43 ri->id = id;
44 ri->mask = mask;
45
46 if (cb->invert)
47 ri->invert = 1;
48 }
49
realm_print(const void * ip,const struct xt_entry_match * match,int numeric)50 static void realm_print(const void *ip, const struct xt_entry_match *match,
51 int numeric)
52 {
53 const struct xt_realm_info *ri = (const void *)match->data;
54
55 if (ri->invert)
56 printf(" !");
57
58 printf(" realm");
59 xtables_print_val_mask(ri->id, ri->mask, numeric ? NULL : realms);
60 }
61
realm_save(const void * ip,const struct xt_entry_match * match)62 static void realm_save(const void *ip, const struct xt_entry_match *match)
63 {
64 const struct xt_realm_info *ri = (const void *)match->data;
65
66 if (ri->invert)
67 printf(" !");
68
69 printf(" --realm");
70 xtables_print_val_mask(ri->id, ri->mask, realms);
71 }
72
73 static void
print_realm_xlate(unsigned long id,unsigned long mask,int numeric,struct xt_xlate * xl,uint32_t op)74 print_realm_xlate(unsigned long id, unsigned long mask,
75 int numeric, struct xt_xlate *xl, uint32_t op)
76 {
77 const char *name = NULL;
78
79 if (mask != 0xffffffff)
80 xt_xlate_add(xl, " and 0x%lx %s 0x%lx", mask,
81 op == XT_OP_EQ ? "==" : "!=", id);
82 else {
83 if (numeric == 0)
84 name = xtables_lmap_id2name(realms, id);
85 if (name)
86 xt_xlate_add(xl, " %s%s",
87 op == XT_OP_EQ ? "" : "!= ", name);
88 else
89 xt_xlate_add(xl, " %s0x%lx",
90 op == XT_OP_EQ ? "" : "!= ", id);
91 }
92 }
93
realm_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)94 static int realm_xlate(struct xt_xlate *xl,
95 const struct xt_xlate_mt_params *params)
96 {
97 const struct xt_realm_info *ri = (const void *)params->match->data;
98 enum xt_op op = XT_OP_EQ;
99
100 if (ri->invert)
101 op = XT_OP_NEQ;
102
103 xt_xlate_add(xl, "rtclassid");
104 print_realm_xlate(ri->id, ri->mask, 0, xl, op);
105
106 return 1;
107 }
108
109 static struct xtables_match realm_mt_reg = {
110 .name = "realm",
111 .version = XTABLES_VERSION,
112 .family = NFPROTO_IPV4,
113 .size = XT_ALIGN(sizeof(struct xt_realm_info)),
114 .userspacesize = XT_ALIGN(sizeof(struct xt_realm_info)),
115 .help = realm_help,
116 .print = realm_print,
117 .save = realm_save,
118 .x6_parse = realm_parse,
119 .x6_options = realm_opts,
120 .xlate = realm_xlate,
121 };
122
_init(void)123 void _init(void)
124 {
125 realms = xtables_lmap_init(f_realms);
126 if (realms == NULL && errno != ENOENT)
127 fprintf(stderr, "Warning: %s: %s\n", f_realms,
128 strerror(errno));
129
130 xtables_register_match(&realm_mt_reg);
131 }
132