blob: 3dd63d3eb33f0f0e4e236788824fb7d607d4a04b [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Shared library add-on to iptables to add realm matching support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <errno.h>
7#include <ctype.h>
8#include <getopt.h>
9#if defined(__GLIBC__) && __GLIBC__ == 2
10#include <net/ethernet.h>
11#else
12#include <linux/if_ether.h>
13#endif
14#include <xtables.h>
15#include <linux/netfilter_ipv4/ipt_realm.h>
16
17static void realm_help(void)
18{
19 printf(
20"realm match options:\n"
21"[!] --realm value[/mask]\n"
22" Match realm\n");
23}
24
25static const struct option realm_opts[] = {
26 { "realm", 1, NULL, '1' },
27 { .name = NULL }
28};
29
30struct realmname {
31 int id;
32 char* name;
33 int len;
34 struct realmname* next;
35};
36
37/* array of realms from /etc/iproute2/rt_realms */
38static struct realmname *realms = NULL;
39/* 1 if loading failed */
40static int rdberr = 0;
41
42
43static void load_realms(void)
44{
45 const char* rfnm = "/etc/iproute2/rt_realms";
46 char buf[512];
47 FILE *fil;
48 char *cur, *nxt;
49 int id;
50 struct realmname *oldnm = NULL, *newnm = NULL;
51
52 fil = fopen(rfnm, "r");
53 if (!fil) {
54 rdberr = 1;
55 return;
56 }
57
58 while (fgets(buf, sizeof(buf), fil)) {
59 cur = buf;
60 while ((*cur == ' ') || (*cur == '\t'))
61 cur++;
62 if ((*cur == '#') || (*cur == '\n') || (*cur == 0))
63 continue;
64
65 /* iproute2 allows hex and dec format */
66 errno = 0;
67 id = strtoul(cur, &nxt, strncmp(cur, "0x", 2) ? 10 : 16);
68 if ((nxt == cur) || errno)
69 continue;
70
71 /* same boundaries as in iproute2 */
72 if (id < 0 || id > 255)
73 continue;
74 cur = nxt;
75
76 if (!isspace(*cur))
77 continue;
78 while ((*cur == ' ') || (*cur == '\t'))
79 cur++;
80 if ((*cur == '#') || (*cur == '\n') || (*cur == 0))
81 continue;
82 nxt = cur;
83 while ((*nxt != 0) && !isspace(*nxt))
84 nxt++;
85 if (nxt == cur)
86 continue;
87
88 /* found valid data */
89 newnm = (struct realmname*)malloc(sizeof(struct realmname));
90 if (newnm == NULL) {
91 perror("libipt_realm: malloc failed");
92 exit(1);
93 }
94 newnm->id = id;
95 newnm->len = nxt - cur;
96 newnm->name = (char*)malloc(newnm->len + 1);
97 if (newnm->name == NULL) {
98 perror("libipt_realm: malloc failed");
99 exit(1);
100 }
101 strncpy(newnm->name, cur, newnm->len);
102 newnm->name[newnm->len] = 0;
103 newnm->next = NULL;
104
105 if (oldnm)
106 oldnm->next = newnm;
107 else
108 realms = newnm;
109 oldnm = newnm;
110 }
111
112 fclose(fil);
113}
114
115/* get realm id for name, -1 if error/not found */
116static int realm_name2id(const char* name)
117{
118 struct realmname* cur;
119
120 if ((realms == NULL) && (rdberr == 0))
121 load_realms();
122 cur = realms;
123 if (cur == NULL)
124 return -1;
125 while (cur) {
126 if (!strncmp(name, cur->name, cur->len + 1))
127 return cur->id;
128 cur = cur->next;
129 }
130 return -1;
131}
132
133/* get realm name for id, NULL if error/not found */
134static const char *realm_id2name(int id)
135{
136 struct realmname* cur;
137
138 if ((realms == NULL) && (rdberr == 0))
139 load_realms();
140 cur = realms;
141 if (cur == NULL)
142 return NULL;
143 while (cur) {
144 if (id == cur->id)
145 return cur->name;
146 cur = cur->next;
147 }
148 return NULL;
149}
150
151static int realm_parse(int c, char **argv, int invert, unsigned int *flags,
152 const void *entry, struct xt_entry_match **match)
153{
154 struct ipt_realm_info *realminfo = (struct ipt_realm_info *)(*match)->data;
155 int id;
156
157 switch (c) {
158 char *end;
159 case '1':
160 xtables_check_inverse(argv[optind-1], &invert, &optind, 0);
161 end = optarg = argv[optind-1];
162 realminfo->id = strtoul(optarg, &end, 0);
163 if (end != optarg && (*end == '/' || *end == '\0')) {
164 if (*end == '/')
165 realminfo->mask = strtoul(end+1, &end, 0);
166 else
167 realminfo->mask = 0xffffffff;
168 if (*end != '\0' || end == optarg)
169 xtables_error(PARAMETER_PROBLEM,
170 "Bad realm value `%s'", optarg);
171 } else {
172 id = realm_name2id(optarg);
173 if (id == -1)
174 xtables_error(PARAMETER_PROBLEM,
175 "Realm `%s' not found", optarg);
176 realminfo->id = id;
177 realminfo->mask = 0xffffffff;
178 }
179 if (invert)
180 realminfo->invert = 1;
181 *flags = 1;
182 break;
183
184 default:
185 return 0;
186 }
187 return 1;
188}
189
190static void
191print_realm(unsigned long id, unsigned long mask, int numeric)
192{
193 const char* name = NULL;
194
195 if (mask != 0xffffffff)
196 printf("0x%lx/0x%lx ", id, mask);
197 else {
198 if (numeric == 0)
199 name = realm_id2name(id);
200 if (name)
201 printf("%s ", name);
202 else
203 printf("0x%lx ", id);
204 }
205}
206
207static void realm_print(const void *ip, const struct xt_entry_match *match,
208 int numeric)
209{
210 struct ipt_realm_info *ri = (struct ipt_realm_info *) match->data;
211
212 if (ri->invert)
213 printf("! ");
214
215 printf("realm ");
216 print_realm(ri->id, ri->mask, numeric);
217}
218
219static void realm_save(const void *ip, const struct xt_entry_match *match)
220{
221 struct ipt_realm_info *ri = (struct ipt_realm_info *) match->data;
222
223 if (ri->invert)
224 printf("! ");
225
226 printf("--realm ");
227 print_realm(ri->id, ri->mask, 0);
228}
229
230static void realm_check(unsigned int flags)
231{
232 if (!flags)
233 xtables_error(PARAMETER_PROBLEM,
234 "realm match: You must specify `--realm'");
235}
236
237static struct xtables_match realm_mt_reg = {
238 .name = "realm",
239 .version = XTABLES_VERSION,
240 .family = NFPROTO_IPV4,
241 .size = XT_ALIGN(sizeof(struct ipt_realm_info)),
242 .userspacesize = XT_ALIGN(sizeof(struct ipt_realm_info)),
243 .help = realm_help,
244 .parse = realm_parse,
245 .final_check = realm_check,
246 .print = realm_print,
247 .save = realm_save,
248 .extra_opts = realm_opts,
249};
250
251void _init(void)
252{
253 xtables_register_match(&realm_mt_reg);
254}