Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sbin/ipfw/tables.c
104185 views
1
/*
2
* Copyright (c) 2014 Yandex LLC
3
* Copyright (c) 2014 Alexander V. Chernikov
4
*
5
* Redistribution and use in source forms, with and without modification,
6
* are permitted provided that this entire comment appears intact.
7
*
8
* Redistribution in binary form may occur without any restrictions.
9
* Obviously, it would be nice if you gave credit where credit is due
10
* but requiring it would be too onerous.
11
*
12
* This software is provided ``AS IS'' without any warranties of any kind.
13
*
14
* in-kernel ipfw tables support.
15
*/
16
17
18
#include <sys/types.h>
19
#include <sys/param.h>
20
#include <sys/socket.h>
21
#include <sys/sysctl.h>
22
23
#include <ctype.h>
24
#include <err.h>
25
#include <errno.h>
26
#include <netdb.h>
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <sysexits.h>
31
32
#include <net/ethernet.h>
33
#include <net/if.h>
34
#include <netinet/in.h>
35
#include <netinet/ip_fw.h>
36
#include <arpa/inet.h>
37
#include <netdb.h>
38
39
#include "ipfw2.h"
40
41
static void table_modify_record(ipfw_obj_header *oh, int ac, char *av[],
42
int add, int quiet, int update, int atomic);
43
static int table_flush(ipfw_obj_header *oh);
44
static int table_destroy(ipfw_obj_header *oh);
45
static int table_do_create(ipfw_obj_header *oh, ipfw_xtable_info *i);
46
static int table_do_modify(ipfw_obj_header *oh, ipfw_xtable_info *i);
47
static int table_do_swap(ipfw_obj_header *oh, char *second);
48
static void table_create(ipfw_obj_header *oh, int ac, char *av[]);
49
static void table_modify(ipfw_obj_header *oh, int ac, char *av[]);
50
static void table_lookup(ipfw_obj_header *oh, int ac, char *av[]);
51
static void table_lock(ipfw_obj_header *oh, int lock);
52
static int table_swap(ipfw_obj_header *oh, char *second);
53
static int table_get_info(ipfw_obj_header *oh, ipfw_xtable_info *i);
54
static int table_show_info(ipfw_xtable_info *i, void *arg);
55
56
static int table_destroy_one(ipfw_xtable_info *i, void *arg);
57
static int table_flush_one(ipfw_xtable_info *i, void *arg);
58
static int table_show_one(ipfw_xtable_info *i, void *arg);
59
static int table_do_get_list(ipfw_xtable_info *i, ipfw_obj_header **poh);
60
static void table_show_list(ipfw_obj_header *oh, int need_header);
61
static void table_show_entry(ipfw_xtable_info *i, ipfw_obj_tentry *tent);
62
63
static void tentry_fill_key(ipfw_obj_header *oh, ipfw_obj_tentry *tent,
64
char *key, int add, uint8_t *ptype, uint32_t *pvmask, ipfw_xtable_info *xi);
65
static void tentry_fill_value(ipfw_obj_header *oh, ipfw_obj_tentry *tent,
66
char *arg, uint8_t type, uint32_t vmask);
67
static void table_show_value(char *buf, size_t bufsize, ipfw_table_value *v,
68
uint32_t vmask, int print_ip);
69
70
typedef int (table_cb_t)(ipfw_xtable_info *i, void *arg);
71
static int tables_foreach(table_cb_t *f, void *arg, int sort);
72
73
#ifndef s6_addr32
74
#define s6_addr32 __u6_addr.__u6_addr32
75
#endif
76
77
static struct _s_x tabletypes[] = {
78
{ "addr", IPFW_TABLE_ADDR },
79
{ "mac", IPFW_TABLE_MAC },
80
{ "iface", IPFW_TABLE_INTERFACE },
81
{ "number", IPFW_TABLE_NUMBER },
82
{ "flow", IPFW_TABLE_FLOW },
83
{ NULL, 0 }
84
};
85
86
/* Default algorithms for various table types */
87
static struct _s_x tablealgos[] = {
88
{ "addr:radix", IPFW_TABLE_ADDR },
89
{ "flow:hash", IPFW_TABLE_FLOW },
90
{ "iface:array", IPFW_TABLE_INTERFACE },
91
{ "number:array", IPFW_TABLE_NUMBER },
92
{ NULL, 0 }
93
};
94
95
static struct _s_x tablevaltypes[] = {
96
{ "skipto", IPFW_VTYPE_SKIPTO },
97
{ "pipe", IPFW_VTYPE_PIPE },
98
{ "fib", IPFW_VTYPE_FIB },
99
{ "nat", IPFW_VTYPE_NAT },
100
{ "dscp", IPFW_VTYPE_DSCP },
101
{ "tag", IPFW_VTYPE_TAG },
102
{ "divert", IPFW_VTYPE_DIVERT },
103
{ "netgraph", IPFW_VTYPE_NETGRAPH },
104
{ "limit", IPFW_VTYPE_LIMIT },
105
{ "ipv4", IPFW_VTYPE_NH4 },
106
{ "ipv6", IPFW_VTYPE_NH6 },
107
{ "mark", IPFW_VTYPE_MARK },
108
{ NULL, 0 }
109
};
110
111
static struct _s_x tablecmds[] = {
112
{ "add", TOK_ADD },
113
{ "delete", TOK_DEL },
114
{ "create", TOK_CREATE },
115
{ "destroy", TOK_DESTROY },
116
{ "flush", TOK_FLUSH },
117
{ "modify", TOK_MODIFY },
118
{ "swap", TOK_SWAP },
119
{ "info", TOK_INFO },
120
{ "detail", TOK_DETAIL },
121
{ "list", TOK_LIST },
122
{ "lookup", TOK_LOOKUP },
123
{ "atomic", TOK_ATOMIC },
124
{ "lock", TOK_LOCK },
125
{ "unlock", TOK_UNLOCK },
126
{ NULL, 0 }
127
};
128
129
static int
130
lookup_host (char *host, struct in_addr *ipaddr)
131
{
132
struct hostent *he;
133
134
if (!inet_aton(host, ipaddr)) {
135
if ((he = gethostbyname(host)) == NULL)
136
return(-1);
137
*ipaddr = *(struct in_addr *)he->h_addr_list[0];
138
}
139
return(0);
140
}
141
142
/*
143
* This one handles all table-related commands
144
* ipfw table NAME create ...
145
* ipfw table NAME modify ...
146
* ipfw table {NAME | all} destroy
147
* ipfw table NAME swap NAME
148
* ipfw table NAME lock
149
* ipfw table NAME unlock
150
* ipfw table NAME add addr[/masklen] [value]
151
* ipfw table NAME add [addr[/masklen] value] [addr[/masklen] value] ..
152
* ipfw table NAME delete addr[/masklen] [addr[/masklen]] ..
153
* ipfw table NAME lookup addr
154
* ipfw table {NAME | all} flush
155
* ipfw table {NAME | all} list
156
* ipfw table {NAME | all} info
157
* ipfw table {NAME | all} detail
158
*/
159
void
160
ipfw_table_handler(int ac, char *av[])
161
{
162
int do_add, is_all;
163
int atomic, error, tcmd;
164
ipfw_xtable_info i;
165
ipfw_obj_header oh;
166
char *tablename;
167
uint8_t set;
168
void *arg;
169
170
memset(&oh, 0, sizeof(oh));
171
is_all = 0;
172
if (g_co.use_set != 0)
173
set = g_co.use_set - 1;
174
else
175
set = 0;
176
177
ac--; av++;
178
NEED1("table needs name");
179
tablename = *av;
180
181
if (table_check_name(tablename) == 0) {
182
table_fill_ntlv(&oh.ntlv, *av, set, 1);
183
oh.idx = 1;
184
} else {
185
if (strcmp(tablename, "all") == 0)
186
is_all = 1;
187
else
188
errx(EX_USAGE, "table name %s is invalid", tablename);
189
}
190
ac--; av++;
191
NEED1("table needs command");
192
193
tcmd = get_token(tablecmds, *av, "table command");
194
/* Check if atomic operation was requested */
195
atomic = 0;
196
if (tcmd == TOK_ATOMIC) {
197
ac--; av++;
198
NEED1("atomic needs command");
199
tcmd = get_token(tablecmds, *av, "table command");
200
switch (tcmd) {
201
case TOK_ADD:
202
break;
203
default:
204
errx(EX_USAGE, "atomic is not compatible with %s", *av);
205
}
206
atomic = 1;
207
}
208
209
switch (tcmd) {
210
case TOK_LIST:
211
case TOK_INFO:
212
case TOK_DETAIL:
213
case TOK_FLUSH:
214
case TOK_DESTROY:
215
break;
216
default:
217
if (is_all != 0)
218
errx(EX_USAGE, "table name required");
219
}
220
221
switch (tcmd) {
222
case TOK_ADD:
223
case TOK_DEL:
224
do_add = **av == 'a';
225
ac--; av++;
226
table_modify_record(&oh, ac, av, do_add, g_co.do_quiet,
227
g_co.do_quiet, atomic);
228
break;
229
case TOK_CREATE:
230
ac--; av++;
231
table_create(&oh, ac, av);
232
break;
233
case TOK_MODIFY:
234
ac--; av++;
235
table_modify(&oh, ac, av);
236
break;
237
case TOK_DESTROY:
238
if (is_all == 0) {
239
if (table_destroy(&oh) == 0)
240
break;
241
if (errno != ESRCH)
242
err(EX_OSERR, "failed to destroy table %s",
243
tablename);
244
/* ESRCH isn't fatal, warn if not quiet mode */
245
if (g_co.do_quiet == 0)
246
warn("failed to destroy table %s", tablename);
247
} else {
248
error = tables_foreach(table_destroy_one, &oh, 1);
249
if (error != 0)
250
err(EX_OSERR,
251
"failed to destroy tables list");
252
}
253
break;
254
case TOK_FLUSH:
255
if (is_all == 0) {
256
if ((error = table_flush(&oh)) == 0)
257
break;
258
if (errno != ESRCH)
259
err(EX_OSERR, "failed to flush table %s info",
260
tablename);
261
/* ESRCH isn't fatal, warn if not quiet mode */
262
if (g_co.do_quiet == 0)
263
warn("failed to flush table %s info",
264
tablename);
265
} else {
266
error = tables_foreach(table_flush_one, &oh, 1);
267
if (error != 0)
268
err(EX_OSERR, "failed to flush tables list");
269
/* XXX: we ignore errors here */
270
}
271
break;
272
case TOK_SWAP:
273
ac--; av++;
274
NEED1("second table name required");
275
table_swap(&oh, *av);
276
break;
277
case TOK_LOCK:
278
case TOK_UNLOCK:
279
table_lock(&oh, (tcmd == TOK_LOCK));
280
break;
281
case TOK_DETAIL:
282
case TOK_INFO:
283
arg = (tcmd == TOK_DETAIL) ? (void *)1 : NULL;
284
if (is_all == 0) {
285
if ((error = table_get_info(&oh, &i)) != 0)
286
err(EX_OSERR, "failed to request table info");
287
table_show_info(&i, arg);
288
} else {
289
error = tables_foreach(table_show_info, arg, 1);
290
if (error != 0)
291
err(EX_OSERR, "failed to request tables list");
292
}
293
break;
294
case TOK_LIST:
295
arg = is_all ? (void*)1 : NULL;
296
if (is_all == 0) {
297
if ((error = table_get_info(&oh, &i)) != 0)
298
err(EX_OSERR, "failed to request table info");
299
table_show_one(&i, arg);
300
} else {
301
error = tables_foreach(table_show_one, arg, 1);
302
if (error != 0)
303
err(EX_OSERR, "failed to request tables list");
304
}
305
break;
306
case TOK_LOOKUP:
307
ac--; av++;
308
table_lookup(&oh, ac, av);
309
break;
310
}
311
}
312
313
void
314
table_fill_ntlv(ipfw_obj_ntlv *ntlv, const char *name, uint8_t set,
315
uint32_t uidx)
316
{
317
318
ntlv->head.type = IPFW_TLV_TBL_NAME;
319
ntlv->head.length = sizeof(ipfw_obj_ntlv);
320
ntlv->idx = uidx;
321
ntlv->set = set;
322
strlcpy(ntlv->name, name, sizeof(ntlv->name));
323
}
324
325
static void
326
table_fill_objheader(ipfw_obj_header *oh, ipfw_xtable_info *i)
327
{
328
329
oh->idx = 1;
330
table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
331
}
332
333
static struct _s_x tablenewcmds[] = {
334
{ "type", TOK_TYPE },
335
{ "valtype", TOK_VALTYPE },
336
{ "algo", TOK_ALGO },
337
{ "limit", TOK_LIMIT },
338
{ "locked", TOK_LOCK },
339
{ "missing", TOK_MISSING },
340
{ "or-flush", TOK_ORFLUSH },
341
{ NULL, 0 }
342
};
343
344
static struct _s_x flowtypecmds[] = {
345
{ "src-ip", IPFW_TFFLAG_SRCIP },
346
{ "proto", IPFW_TFFLAG_PROTO },
347
{ "src-port", IPFW_TFFLAG_SRCPORT },
348
{ "dst-ip", IPFW_TFFLAG_DSTIP },
349
{ "dst-port", IPFW_TFFLAG_DSTPORT },
350
{ NULL, 0 }
351
};
352
353
static int
354
table_parse_type(uint8_t ttype, char *p, uint8_t *tflags)
355
{
356
uint32_t fset, fclear;
357
char *e;
358
359
/* Parse type options */
360
switch(ttype) {
361
case IPFW_TABLE_FLOW:
362
fset = fclear = 0;
363
if (fill_flags(flowtypecmds, p, &e, &fset, &fclear) != 0)
364
errx(EX_USAGE,
365
"unable to parse flow option %s", e);
366
*tflags = fset;
367
break;
368
default:
369
return (EX_USAGE);
370
}
371
372
return (0);
373
}
374
375
static void
376
table_print_type(char *tbuf, size_t size, uint8_t type, uint8_t tflags)
377
{
378
const char *tname;
379
int l;
380
381
if ((tname = match_value(tabletypes, type)) == NULL)
382
tname = "unknown";
383
384
l = snprintf(tbuf, size, "%s", tname);
385
tbuf += l;
386
size -= l;
387
388
switch(type) {
389
case IPFW_TABLE_FLOW:
390
if (tflags != 0) {
391
*tbuf++ = ':';
392
l--;
393
print_flags_buffer(tbuf, size, flowtypecmds, tflags);
394
}
395
break;
396
}
397
}
398
399
/*
400
* Creates new table
401
*
402
* ipfw table NAME create [ type { addr | iface | number | flow } ]
403
* [ algo algoname ] [missing] [or-flush]
404
*/
405
static void
406
table_create(ipfw_obj_header *oh, int ac, char *av[])
407
{
408
ipfw_xtable_info xi, xie;
409
int error, missing, orflush, tcmd, val;
410
uint32_t fset, fclear;
411
char *e, *p;
412
char tbuf[128];
413
414
missing = orflush = 0;
415
memset(&xi, 0, sizeof(xi));
416
while (ac > 0) {
417
tcmd = get_token(tablenewcmds, *av, "option");
418
ac--; av++;
419
420
switch (tcmd) {
421
case TOK_LIMIT:
422
NEED1("limit value required");
423
xi.limit = strtol(*av, NULL, 10);
424
ac--; av++;
425
break;
426
case TOK_TYPE:
427
NEED1("table type required");
428
/* Type may have suboptions after ':' */
429
if ((p = strchr(*av, ':')) != NULL)
430
*p++ = '\0';
431
val = match_token(tabletypes, *av);
432
if (val == -1) {
433
concat_tokens(tbuf, sizeof(tbuf), tabletypes,
434
", ");
435
errx(EX_USAGE,
436
"Unknown tabletype: %s. Supported: %s",
437
*av, tbuf);
438
}
439
xi.type = val;
440
if (p != NULL) {
441
error = table_parse_type(val, p, &xi.tflags);
442
if (error != 0)
443
errx(EX_USAGE,
444
"Unsupported suboptions: %s", p);
445
}
446
ac--; av++;
447
break;
448
case TOK_VALTYPE:
449
NEED1("table value type required");
450
fset = fclear = 0;
451
val = fill_flags(tablevaltypes, *av, &e, &fset, &fclear);
452
if (val != -1) {
453
xi.vmask = fset;
454
ac--; av++;
455
break;
456
}
457
concat_tokens(tbuf, sizeof(tbuf), tablevaltypes, ", ");
458
errx(EX_USAGE, "Unknown value type: %s. Supported: %s",
459
e, tbuf);
460
break;
461
case TOK_ALGO:
462
NEED1("table algorithm name required");
463
if (strlen(*av) > sizeof(xi.algoname))
464
errx(EX_USAGE, "algorithm name too long");
465
strlcpy(xi.algoname, *av, sizeof(xi.algoname));
466
ac--; av++;
467
break;
468
case TOK_LOCK:
469
xi.flags |= IPFW_TGFLAGS_LOCKED;
470
break;
471
case TOK_ORFLUSH:
472
orflush = 1;
473
/* FALLTHROUGH */
474
case TOK_MISSING:
475
missing = 1;
476
break;
477
}
478
}
479
480
/* Set some defaults to preserve compatibility. */
481
if (xi.algoname[0] == '\0') {
482
const char *algo;
483
484
if (xi.type == 0)
485
xi.type = IPFW_TABLE_ADDR;
486
algo = match_value(tablealgos, xi.type);
487
if (algo != NULL)
488
strlcpy(xi.algoname, algo, sizeof(xi.algoname));
489
}
490
if (xi.vmask == 0)
491
xi.vmask = IPFW_VTYPE_LEGACY;
492
493
error = table_do_create(oh, &xi);
494
495
if (error == 0)
496
return;
497
498
if (errno != EEXIST || missing == 0)
499
err(EX_OSERR, "Table creation failed");
500
501
/* Check that existing table is the same we are trying to create */
502
if (table_get_info(oh, &xie) != 0)
503
err(EX_OSERR, "Existing table check failed");
504
505
if (xi.limit != xie.limit || xi.type != xie.type ||
506
xi.tflags != xie.tflags || xi.vmask != xie.vmask || (
507
xi.algoname[0] != '\0' && strcmp(xi.algoname,
508
xie.algoname) != 0) || xi.flags != xie.flags)
509
errx(EX_DATAERR, "The existing table is not compatible "
510
"with one you are creating.");
511
512
/* Flush existing table if instructed to do so */
513
if (orflush != 0 && table_flush(oh) != 0)
514
err(EX_OSERR, "Table flush on creation failed");
515
}
516
517
/*
518
* Creates new table
519
*
520
* Request: [ ipfw_obj_header ipfw_xtable_info ]
521
*
522
* Returns 0 on success.
523
*/
524
static int
525
table_do_create(ipfw_obj_header *oh, ipfw_xtable_info *i)
526
{
527
char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
528
int error;
529
530
memcpy(tbuf, oh, sizeof(*oh));
531
memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
532
oh = (ipfw_obj_header *)tbuf;
533
534
error = do_set3(IP_FW_TABLE_XCREATE, &oh->opheader, sizeof(tbuf));
535
536
return (error);
537
}
538
539
/*
540
* Modifies existing table
541
*
542
* ipfw table NAME modify [ limit number ]
543
*/
544
static void
545
table_modify(ipfw_obj_header *oh, int ac, char *av[])
546
{
547
ipfw_xtable_info xi;
548
int tcmd;
549
550
memset(&xi, 0, sizeof(xi));
551
552
while (ac > 0) {
553
tcmd = get_token(tablenewcmds, *av, "option");
554
ac--; av++;
555
556
switch (tcmd) {
557
case TOK_LIMIT:
558
NEED1("limit value required");
559
xi.limit = strtol(*av, NULL, 10);
560
xi.mflags |= IPFW_TMFLAGS_LIMIT;
561
ac--; av++;
562
break;
563
default:
564
errx(EX_USAGE, "cmd is not supported for modification");
565
}
566
}
567
568
if (table_do_modify(oh, &xi) != 0)
569
err(EX_OSERR, "Table modification failed");
570
}
571
572
/*
573
* Modifies existing table.
574
*
575
* Request: [ ipfw_obj_header ipfw_xtable_info ]
576
*
577
* Returns 0 on success.
578
*/
579
static int
580
table_do_modify(ipfw_obj_header *oh, ipfw_xtable_info *i)
581
{
582
char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
583
int error;
584
585
memcpy(tbuf, oh, sizeof(*oh));
586
memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
587
oh = (ipfw_obj_header *)tbuf;
588
589
error = do_set3(IP_FW_TABLE_XMODIFY, &oh->opheader, sizeof(tbuf));
590
591
return (error);
592
}
593
594
/*
595
* Locks or unlocks given table
596
*/
597
static void
598
table_lock(ipfw_obj_header *oh, int lock)
599
{
600
ipfw_xtable_info xi;
601
602
memset(&xi, 0, sizeof(xi));
603
604
xi.mflags |= IPFW_TMFLAGS_LOCK;
605
xi.flags |= (lock != 0) ? IPFW_TGFLAGS_LOCKED : 0;
606
607
if (table_do_modify(oh, &xi) != 0)
608
err(EX_OSERR, "Table %s failed", lock != 0 ? "lock" : "unlock");
609
}
610
611
/*
612
* Destroys given table specified by @oh->ntlv.
613
* Returns 0 on success.
614
*/
615
static int
616
table_destroy(ipfw_obj_header *oh)
617
{
618
619
if (do_set3(IP_FW_TABLE_XDESTROY, &oh->opheader, sizeof(*oh)) != 0)
620
return (-1);
621
622
return (0);
623
}
624
625
static int
626
table_destroy_one(ipfw_xtable_info *i, void *arg)
627
{
628
ipfw_obj_header *oh;
629
630
oh = (ipfw_obj_header *)arg;
631
table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
632
if (table_destroy(oh) != 0) {
633
if (g_co.do_quiet == 0)
634
warn("failed to destroy table(%s) in set %u",
635
i->tablename, i->set);
636
return (-1);
637
}
638
return (0);
639
}
640
641
/*
642
* Flushes given table specified by @oh->ntlv.
643
* Returns 0 on success.
644
*/
645
static int
646
table_flush(ipfw_obj_header *oh)
647
{
648
649
if (do_set3(IP_FW_TABLE_XFLUSH, &oh->opheader, sizeof(*oh)) != 0)
650
return (-1);
651
652
return (0);
653
}
654
655
static int
656
table_do_swap(ipfw_obj_header *oh, char *second)
657
{
658
char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_ntlv)];
659
int error;
660
661
memset(tbuf, 0, sizeof(tbuf));
662
memcpy(tbuf, oh, sizeof(*oh));
663
oh = (ipfw_obj_header *)tbuf;
664
table_fill_ntlv((ipfw_obj_ntlv *)(oh + 1), second, oh->ntlv.set, 1);
665
666
error = do_set3(IP_FW_TABLE_XSWAP, &oh->opheader, sizeof(tbuf));
667
668
return (error);
669
}
670
671
/*
672
* Swaps given table with @second one.
673
*/
674
static int
675
table_swap(ipfw_obj_header *oh, char *second)
676
{
677
678
if (table_check_name(second) != 0)
679
errx(EX_USAGE, "table name %s is invalid", second);
680
681
if (table_do_swap(oh, second) == 0)
682
return (0);
683
684
switch (errno) {
685
case EINVAL:
686
errx(EX_USAGE, "Unable to swap table: check types");
687
case EFBIG:
688
errx(EX_USAGE, "Unable to swap table: check limits");
689
}
690
691
return (0);
692
}
693
694
695
/*
696
* Retrieves table in given table specified by @oh->ntlv.
697
* it inside @i.
698
* Returns 0 on success.
699
*/
700
static int
701
table_get_info(ipfw_obj_header *oh, ipfw_xtable_info *i)
702
{
703
char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
704
size_t sz;
705
706
sz = sizeof(tbuf);
707
memset(tbuf, 0, sizeof(tbuf));
708
memcpy(tbuf, oh, sizeof(*oh));
709
oh = (ipfw_obj_header *)tbuf;
710
711
if (do_get3(IP_FW_TABLE_XINFO, &oh->opheader, &sz) != 0)
712
return (errno);
713
714
if (sz < sizeof(tbuf))
715
return (EINVAL);
716
717
*i = *(ipfw_xtable_info *)(oh + 1);
718
719
return (0);
720
}
721
722
static struct _s_x tablealgoclass[] = {
723
{ "hash", IPFW_TACLASS_HASH },
724
{ "array", IPFW_TACLASS_ARRAY },
725
{ "radix", IPFW_TACLASS_RADIX },
726
{ NULL, 0 }
727
};
728
729
struct ta_cldata {
730
uint8_t taclass;
731
uint8_t spare4;
732
uint16_t itemsize;
733
uint16_t itemsize6;
734
uint32_t size;
735
uint32_t count;
736
};
737
738
/*
739
* Print global/per-AF table @i algorithm info.
740
*/
741
static void
742
table_show_tainfo(ipfw_xtable_info *i __unused, struct ta_cldata *d,
743
const char *af, const char *taclass)
744
{
745
746
switch (d->taclass) {
747
case IPFW_TACLASS_HASH:
748
case IPFW_TACLASS_ARRAY:
749
printf(" %salgorithm %s info\n", af, taclass);
750
if (d->itemsize == d->itemsize6)
751
printf(" size: %u items: %u itemsize: %u\n",
752
d->size, d->count, d->itemsize);
753
else
754
printf(" size: %u items: %u "
755
"itemsize4: %u itemsize6: %u\n",
756
d->size, d->count,
757
d->itemsize, d->itemsize6);
758
break;
759
case IPFW_TACLASS_RADIX:
760
printf(" %salgorithm %s info\n", af, taclass);
761
if (d->itemsize == d->itemsize6)
762
printf(" items: %u itemsize: %u\n",
763
d->count, d->itemsize);
764
else
765
printf(" items: %u "
766
"itemsize4: %u itemsize6: %u\n",
767
d->count, d->itemsize, d->itemsize6);
768
break;
769
default:
770
printf(" algo class: %s\n", taclass);
771
}
772
}
773
774
static void
775
table_print_valheader(char *buf, size_t bufsize, uint32_t vmask)
776
{
777
778
if (vmask == IPFW_VTYPE_LEGACY) {
779
snprintf(buf, bufsize, "legacy");
780
return;
781
}
782
783
memset(buf, 0, bufsize);
784
print_flags_buffer(buf, bufsize, tablevaltypes, vmask);
785
}
786
787
/*
788
* Prints table info struct @i in human-readable form.
789
*/
790
static int
791
table_show_info(ipfw_xtable_info *i, void *arg)
792
{
793
const char *vtype;
794
ipfw_ta_tinfo *tainfo;
795
int afdata, afitem;
796
struct ta_cldata d;
797
char ttype[64], tvtype[64];
798
799
table_print_type(ttype, sizeof(ttype), i->type, i->tflags);
800
table_print_valheader(tvtype, sizeof(tvtype), i->vmask);
801
802
printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
803
if ((i->flags & IPFW_TGFLAGS_LOCKED) != 0)
804
printf(" kindex: %d, type: %s, locked\n", i->kidx, ttype);
805
else
806
printf(" kindex: %d, type: %s\n", i->kidx, ttype);
807
printf(" references: %u, valtype: %s\n", i->refcnt, tvtype);
808
printf(" algorithm: %s\n", i->algoname);
809
printf(" items: %u, size: %u\n", i->count, i->size);
810
if (i->limit > 0)
811
printf(" limit: %u\n", i->limit);
812
813
/* Print algo-specific info if requested & set */
814
if (arg == NULL)
815
return (0);
816
817
if ((i->ta_info.flags & IPFW_TATFLAGS_DATA) == 0)
818
return (0);
819
tainfo = &i->ta_info;
820
821
afdata = 0;
822
afitem = 0;
823
if (tainfo->flags & IPFW_TATFLAGS_AFDATA)
824
afdata = 1;
825
if (tainfo->flags & IPFW_TATFLAGS_AFITEM)
826
afitem = 1;
827
828
memset(&d, 0, sizeof(d));
829
d.taclass = tainfo->taclass4;
830
d.size = tainfo->size4;
831
d.count = tainfo->count4;
832
d.itemsize = tainfo->itemsize4;
833
if (afdata == 0 && afitem != 0)
834
d.itemsize6 = tainfo->itemsize6;
835
else
836
d.itemsize6 = d.itemsize;
837
if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
838
vtype = "unknown";
839
840
if (afdata == 0) {
841
table_show_tainfo(i, &d, "", vtype);
842
} else {
843
table_show_tainfo(i, &d, "IPv4 ", vtype);
844
memset(&d, 0, sizeof(d));
845
d.taclass = tainfo->taclass6;
846
if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
847
vtype = "unknown";
848
d.size = tainfo->size6;
849
d.count = tainfo->count6;
850
d.itemsize = tainfo->itemsize6;
851
d.itemsize6 = d.itemsize;
852
table_show_tainfo(i, &d, "IPv6 ", vtype);
853
}
854
855
return (0);
856
}
857
858
859
/*
860
* Function wrappers which can be used either
861
* as is or as foreach function parameter.
862
*/
863
864
static int
865
table_show_one(ipfw_xtable_info *i, void *arg)
866
{
867
ipfw_obj_header *oh = NULL;
868
int error;
869
int is_all;
870
871
is_all = arg == NULL ? 0 : 1;
872
873
if ((error = table_do_get_list(i, &oh)) != 0) {
874
err(EX_OSERR, "Error requesting table %s list", i->tablename);
875
return (error);
876
}
877
878
table_show_list(oh, is_all);
879
880
free(oh);
881
return (0);
882
}
883
884
static int
885
table_flush_one(ipfw_xtable_info *i, void *arg)
886
{
887
ipfw_obj_header *oh;
888
889
oh = (ipfw_obj_header *)arg;
890
891
table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
892
893
return (table_flush(oh));
894
}
895
896
static int
897
table_do_modify_record(int cmd, ipfw_obj_header *oh,
898
ipfw_obj_tentry *tent, int count, int atomic)
899
{
900
ipfw_obj_ctlv *ctlv;
901
ipfw_obj_tentry *tent_base;
902
caddr_t pbuf;
903
char xbuf[sizeof(*oh) + sizeof(ipfw_obj_ctlv) + sizeof(*tent)];
904
int error, i;
905
size_t sz;
906
907
sz = sizeof(*ctlv) + sizeof(*tent) * count;
908
if (count == 1) {
909
memset(xbuf, 0, sizeof(xbuf));
910
pbuf = xbuf;
911
} else {
912
if ((pbuf = calloc(1, sizeof(*oh) + sz)) == NULL)
913
return (ENOMEM);
914
}
915
916
memcpy(pbuf, oh, sizeof(*oh));
917
oh = (ipfw_obj_header *)pbuf;
918
oh->opheader.version = 1; /* Current version */
919
920
ctlv = (ipfw_obj_ctlv *)(oh + 1);
921
ctlv->count = count;
922
ctlv->head.length = sz;
923
if (atomic != 0)
924
ctlv->flags |= IPFW_CTF_ATOMIC;
925
926
tent_base = tent;
927
memcpy(ctlv + 1, tent, sizeof(*tent) * count);
928
tent = (ipfw_obj_tentry *)(ctlv + 1);
929
for (i = 0; i < count; i++, tent++) {
930
tent->head.length = sizeof(ipfw_obj_tentry);
931
tent->idx = oh->idx;
932
}
933
934
sz += sizeof(*oh);
935
error = do_get3(cmd, &oh->opheader, &sz);
936
if (error != 0)
937
error = errno;
938
tent = (ipfw_obj_tentry *)(ctlv + 1);
939
/* Copy result back to provided buffer */
940
memcpy(tent_base, ctlv + 1, sizeof(*tent) * count);
941
942
if (pbuf != xbuf)
943
free(pbuf);
944
945
return (error);
946
}
947
948
static void
949
table_modify_record(ipfw_obj_header *oh, int ac, char *av[], int add,
950
int quiet, int update, int atomic)
951
{
952
ipfw_obj_tentry *ptent, tent, *tent_buf;
953
ipfw_xtable_info xi;
954
const char *etxt, *px, *texterr;
955
uint8_t type;
956
uint32_t vmask;
957
int cmd, count, error, i, ignored;
958
959
if (ac == 0)
960
errx(EX_USAGE, "address required");
961
962
if (add != 0) {
963
cmd = IP_FW_TABLE_XADD;
964
texterr = "Adding record failed";
965
} else {
966
cmd = IP_FW_TABLE_XDEL;
967
texterr = "Deleting record failed";
968
}
969
970
/*
971
* Calculate number of entries:
972
* Assume [key val] x N for add
973
* and
974
* key x N for delete
975
*/
976
count = (add != 0) ? ac / 2 + 1 : ac;
977
978
if (count <= 1) {
979
/* Adding single entry with/without value */
980
memset(&tent, 0, sizeof(tent));
981
tent_buf = &tent;
982
} else {
983
984
if ((tent_buf = calloc(count, sizeof(tent))) == NULL)
985
errx(EX_OSERR,
986
"Unable to allocate memory for all entries");
987
}
988
ptent = tent_buf;
989
990
memset(&xi, 0, sizeof(xi));
991
count = 0;
992
while (ac > 0) {
993
tentry_fill_key(oh, ptent, *av, add, &type, &vmask, &xi);
994
995
/*
996
* Compatibility layer: auto-create table if not exists.
997
*/
998
if (xi.tablename[0] == '\0') {
999
xi.type = type;
1000
xi.vmask = vmask;
1001
strlcpy(xi.tablename, oh->ntlv.name,
1002
sizeof(xi.tablename));
1003
if (quiet == 0)
1004
warnx("DEPRECATED: inserting data into "
1005
"non-existent table %s. (auto-created)",
1006
xi.tablename);
1007
table_do_create(oh, &xi);
1008
}
1009
1010
oh->ntlv.type = type;
1011
ac--; av++;
1012
1013
if (add != 0 && ac > 0) {
1014
tentry_fill_value(oh, ptent, *av, type, vmask);
1015
ac--; av++;
1016
}
1017
1018
if (update != 0)
1019
ptent->head.flags |= IPFW_TF_UPDATE;
1020
1021
count++;
1022
ptent++;
1023
}
1024
1025
error = table_do_modify_record(cmd, oh, tent_buf, count, atomic);
1026
1027
/*
1028
* Compatibility stuff: do not yell on duplicate keys or
1029
* failed deletions.
1030
*/
1031
if (error == 0 || (error == EEXIST && add != 0) ||
1032
(error == ENOENT && add == 0)) {
1033
if (quiet != 0) {
1034
if (tent_buf != &tent)
1035
free(tent_buf);
1036
return;
1037
}
1038
}
1039
1040
/* Report results back */
1041
ptent = tent_buf;
1042
for (i = 0; i < count; ptent++, i++) {
1043
ignored = 0;
1044
switch (ptent->result) {
1045
case IPFW_TR_ADDED:
1046
px = "added";
1047
break;
1048
case IPFW_TR_DELETED:
1049
px = "deleted";
1050
break;
1051
case IPFW_TR_UPDATED:
1052
px = "updated";
1053
break;
1054
case IPFW_TR_LIMIT:
1055
px = "limit";
1056
ignored = 1;
1057
break;
1058
case IPFW_TR_ERROR:
1059
px = "error";
1060
ignored = 1;
1061
break;
1062
case IPFW_TR_NOTFOUND:
1063
px = "notfound";
1064
ignored = 1;
1065
break;
1066
case IPFW_TR_EXISTS:
1067
px = "exists";
1068
ignored = 1;
1069
break;
1070
case IPFW_TR_IGNORED:
1071
px = "ignored";
1072
ignored = 1;
1073
break;
1074
default:
1075
px = "unknown";
1076
ignored = 1;
1077
}
1078
1079
if (error != 0 && atomic != 0 && ignored == 0)
1080
printf("%s(reverted): ", px);
1081
else
1082
printf("%s: ", px);
1083
1084
table_show_entry(&xi, ptent);
1085
}
1086
1087
if (tent_buf != &tent)
1088
free(tent_buf);
1089
1090
if (error == 0)
1091
return;
1092
1093
/* Try to provide more human-readable error */
1094
switch (error) {
1095
case EEXIST:
1096
etxt = "record already exists";
1097
break;
1098
case EFBIG:
1099
etxt = "limit hit";
1100
break;
1101
case ESRCH:
1102
etxt = "table not found";
1103
break;
1104
case ENOENT:
1105
etxt = "record not found";
1106
break;
1107
case EACCES:
1108
etxt = "table is locked";
1109
break;
1110
default:
1111
etxt = strerror(error);
1112
}
1113
1114
errx(EX_OSERR, "%s: %s", texterr, etxt);
1115
}
1116
1117
static int
1118
table_do_lookup(ipfw_obj_header *oh, char *key, ipfw_xtable_info *xi,
1119
ipfw_obj_tentry *xtent)
1120
{
1121
char xbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_tentry)];
1122
ipfw_obj_tentry *tent;
1123
uint8_t type;
1124
uint32_t vmask;
1125
size_t sz;
1126
1127
memcpy(xbuf, oh, sizeof(*oh));
1128
oh = (ipfw_obj_header *)xbuf;
1129
tent = (ipfw_obj_tentry *)(oh + 1);
1130
1131
memset(tent, 0, sizeof(*tent));
1132
tent->head.length = sizeof(*tent);
1133
tent->idx = 1;
1134
1135
tentry_fill_key(oh, tent, key, 0, &type, &vmask, xi);
1136
oh->ntlv.type = type;
1137
1138
sz = sizeof(xbuf);
1139
if (do_get3(IP_FW_TABLE_XFIND, &oh->opheader, &sz) != 0)
1140
return (errno);
1141
1142
if (sz < sizeof(xbuf))
1143
return (EINVAL);
1144
1145
*xtent = *tent;
1146
1147
return (0);
1148
}
1149
1150
static void
1151
table_lookup(ipfw_obj_header *oh, int ac, char *av[])
1152
{
1153
ipfw_obj_tentry xtent;
1154
ipfw_xtable_info xi;
1155
char key[64];
1156
int error;
1157
1158
if (ac == 0)
1159
errx(EX_USAGE, "address required");
1160
1161
strlcpy(key, *av, sizeof(key));
1162
1163
memset(&xi, 0, sizeof(xi));
1164
error = table_do_lookup(oh, key, &xi, &xtent);
1165
1166
switch (error) {
1167
case 0:
1168
break;
1169
case ESRCH:
1170
errx(EX_UNAVAILABLE, "Table %s not found", oh->ntlv.name);
1171
case ENOENT:
1172
errx(EX_UNAVAILABLE, "Entry %s not found", *av);
1173
case ENOTSUP:
1174
errx(EX_UNAVAILABLE, "Table %s algo does not support "
1175
"\"lookup\" method", oh->ntlv.name);
1176
default:
1177
err(EX_OSERR, "getsockopt(IP_FW_TABLE_XFIND)");
1178
}
1179
1180
table_show_entry(&xi, &xtent);
1181
}
1182
1183
static void
1184
tentry_fill_key_type(char *arg, ipfw_obj_tentry *tentry, uint8_t type,
1185
uint8_t tflags)
1186
{
1187
char *p, *pp;
1188
int mask, af;
1189
struct in6_addr *paddr, tmp;
1190
struct ether_addr *mac;
1191
struct tflow_entry *tfe;
1192
uint32_t key, *pkey;
1193
uint16_t port;
1194
struct protoent *pent;
1195
struct servent *sent;
1196
int masklen;
1197
1198
mask = masklen = 0;
1199
af = 0;
1200
paddr = (struct in6_addr *)&tentry->k;
1201
1202
switch (type) {
1203
case IPFW_TABLE_ADDR:
1204
/* Remove / if exists */
1205
if ((p = strchr(arg, '/')) != NULL) {
1206
*p = '\0';
1207
mask = atoi(p + 1);
1208
}
1209
1210
if (inet_pton(AF_INET, arg, paddr) == 1) {
1211
if (p != NULL && mask > 32)
1212
errx(EX_DATAERR, "bad IPv4 mask width: %s",
1213
p + 1);
1214
1215
masklen = p ? mask : 32;
1216
af = AF_INET;
1217
} else if (inet_pton(AF_INET6, arg, paddr) == 1) {
1218
if (IN6_IS_ADDR_V4COMPAT(paddr))
1219
errx(EX_DATAERR,
1220
"Use IPv4 instead of v4-compatible");
1221
if (p != NULL && mask > 128)
1222
errx(EX_DATAERR, "bad IPv6 mask width: %s",
1223
p + 1);
1224
1225
masklen = p ? mask : 128;
1226
af = AF_INET6;
1227
} else {
1228
/* Assume FQDN */
1229
if (lookup_host(arg, (struct in_addr *)paddr) != 0)
1230
errx(EX_NOHOST, "hostname ``%s'' unknown", arg);
1231
1232
masklen = 32;
1233
type = IPFW_TABLE_ADDR;
1234
af = AF_INET;
1235
}
1236
break;
1237
case IPFW_TABLE_MAC:
1238
/* Remove / if exists */
1239
if ((p = strchr(arg, '/')) != NULL) {
1240
*p = '\0';
1241
mask = atoi(p + 1);
1242
}
1243
1244
if (p != NULL && mask > 8 * ETHER_ADDR_LEN)
1245
errx(EX_DATAERR, "bad MAC mask width: %s",
1246
p + 1);
1247
1248
if ((mac = ether_aton(arg)) == NULL)
1249
errx(EX_DATAERR, "Incorrect MAC address");
1250
1251
memcpy(tentry->k.mac, mac->octet, ETHER_ADDR_LEN);
1252
masklen = p ? mask : 8 * ETHER_ADDR_LEN;
1253
af = AF_LINK;
1254
break;
1255
case IPFW_TABLE_INTERFACE:
1256
/* Assume interface name. Copy significant data only */
1257
mask = MIN(strlen(arg), IF_NAMESIZE - 1);
1258
memcpy(paddr, arg, mask);
1259
/* Set mask to exact match */
1260
masklen = 8 * IF_NAMESIZE;
1261
break;
1262
case IPFW_TABLE_NUMBER:
1263
/* Port or any other key */
1264
key = strtol(arg, &p, 10);
1265
if (*p != '\0')
1266
errx(EX_DATAERR, "Invalid number: %s", arg);
1267
1268
pkey = (uint32_t *)paddr;
1269
*pkey = key;
1270
masklen = 32;
1271
break;
1272
case IPFW_TABLE_FLOW:
1273
/* Assume [src-ip][,proto][,src-port][,dst-ip][,dst-port] */
1274
tfe = &tentry->k.flow;
1275
af = 0;
1276
1277
/* Handle <ipv4|ipv6> */
1278
if ((tflags & IPFW_TFFLAG_SRCIP) != 0) {
1279
if ((p = strchr(arg, ',')) != NULL)
1280
*p++ = '\0';
1281
/* Determine family using temporary storage */
1282
if (inet_pton(AF_INET, arg, &tmp) == 1) {
1283
if (af != 0 && af != AF_INET)
1284
errx(EX_DATAERR,
1285
"Inconsistent address family\n");
1286
af = AF_INET;
1287
memcpy(&tfe->a.a4.sip, &tmp, 4);
1288
} else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1289
if (af != 0 && af != AF_INET6)
1290
errx(EX_DATAERR,
1291
"Inconsistent address family\n");
1292
af = AF_INET6;
1293
memcpy(&tfe->a.a6.sip6, &tmp, 16);
1294
}
1295
1296
arg = p;
1297
}
1298
1299
/* Handle <proto-num|proto-name> */
1300
if ((tflags & IPFW_TFFLAG_PROTO) != 0) {
1301
if (arg == NULL)
1302
errx(EX_DATAERR, "invalid key: proto missing");
1303
if ((p = strchr(arg, ',')) != NULL)
1304
*p++ = '\0';
1305
1306
key = strtol(arg, &pp, 10);
1307
if (*pp != '\0') {
1308
if ((pent = getprotobyname(arg)) == NULL)
1309
errx(EX_DATAERR, "Unknown proto: %s",
1310
arg);
1311
else
1312
key = pent->p_proto;
1313
}
1314
1315
if (key > 255)
1316
errx(EX_DATAERR, "Bad protocol number: %u",key);
1317
1318
tfe->proto = key;
1319
1320
arg = p;
1321
}
1322
1323
/* Handle <port-num|service-name> */
1324
if ((tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1325
if (arg == NULL)
1326
errx(EX_DATAERR, "invalid key: src port missing");
1327
if ((p = strchr(arg, ',')) != NULL)
1328
*p++ = '\0';
1329
1330
port = htons(strtol(arg, &pp, 10));
1331
if (*pp != '\0') {
1332
if ((sent = getservbyname(arg, NULL)) == NULL)
1333
errx(EX_DATAERR, "Unknown service: %s",
1334
arg);
1335
port = sent->s_port;
1336
}
1337
tfe->sport = port;
1338
arg = p;
1339
}
1340
1341
/* Handle <ipv4|ipv6>*/
1342
if ((tflags & IPFW_TFFLAG_DSTIP) != 0) {
1343
if (arg == NULL)
1344
errx(EX_DATAERR, "invalid key: dst ip missing");
1345
if ((p = strchr(arg, ',')) != NULL)
1346
*p++ = '\0';
1347
/* Determine family using temporary storage */
1348
if (inet_pton(AF_INET, arg, &tmp) == 1) {
1349
if (af != 0 && af != AF_INET)
1350
errx(EX_DATAERR,
1351
"Inconsistent address family");
1352
af = AF_INET;
1353
memcpy(&tfe->a.a4.dip, &tmp, 4);
1354
} else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1355
if (af != 0 && af != AF_INET6)
1356
errx(EX_DATAERR,
1357
"Inconsistent address family");
1358
af = AF_INET6;
1359
memcpy(&tfe->a.a6.dip6, &tmp, 16);
1360
}
1361
1362
arg = p;
1363
}
1364
1365
/* Handle <port-num|service-name> */
1366
if ((tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1367
if (arg == NULL)
1368
errx(EX_DATAERR, "invalid key: dst port missing");
1369
if ((p = strchr(arg, ',')) != NULL)
1370
*p++ = '\0';
1371
1372
port = htons(strtol(arg, &pp, 10));
1373
if (*pp != '\0') {
1374
if ((sent = getservbyname(arg, NULL)) == NULL)
1375
errx(EX_DATAERR, "Unknown service: %s",
1376
arg);
1377
port = sent->s_port;
1378
}
1379
tfe->dport = port;
1380
arg = p;
1381
}
1382
1383
tfe->af = af;
1384
1385
break;
1386
1387
default:
1388
errx(EX_DATAERR, "Unsupported table type: %d", type);
1389
}
1390
1391
tentry->subtype = af;
1392
tentry->masklen = masklen;
1393
}
1394
1395
/*
1396
* Tries to guess table key type.
1397
* This procedure is used in legacy table auto-create
1398
* code AND in `ipfw -n` ruleset checking.
1399
*
1400
* Imported from old table_fill_xentry() parse code.
1401
*/
1402
static int
1403
guess_key_type(char *key, uint8_t *ptype)
1404
{
1405
char *p;
1406
struct in6_addr addr;
1407
1408
if (ishexnumber(*key) != 0 || *key == ':') {
1409
/* Remove / if exists */
1410
if ((p = strchr(key, '/')) != NULL)
1411
*p = '\0';
1412
1413
if ((inet_pton(AF_INET, key, &addr) == 1) ||
1414
(inet_pton(AF_INET6, key, &addr) == 1)) {
1415
*ptype = IPFW_TABLE_CIDR;
1416
if (p != NULL)
1417
*p = '/';
1418
return (0);
1419
} else {
1420
/* Port or any other key */
1421
/* Skip non-base 10 entries like 'fa1' */
1422
(void)strtol(key, &p, 10);
1423
if (*p == '\0') {
1424
*ptype = IPFW_TABLE_NUMBER;
1425
return (0);
1426
} else if ((p != key) && (*p == '.')) {
1427
/*
1428
* Warn on IPv4 address strings
1429
* which are "valid" for inet_aton() but not
1430
* in inet_pton().
1431
*
1432
* Typical examples: '10.5' or '10.0.0.05'
1433
*/
1434
return (1);
1435
}
1436
}
1437
}
1438
1439
if (strchr(key, '.') == NULL) {
1440
*ptype = IPFW_TABLE_INTERFACE;
1441
return (0);
1442
}
1443
1444
if (lookup_host(key, (struct in_addr *)&addr) != 0)
1445
return (1);
1446
1447
*ptype = IPFW_TABLE_CIDR;
1448
return (0);
1449
}
1450
1451
static void
1452
tentry_fill_key(ipfw_obj_header *oh, ipfw_obj_tentry *tent, char *key,
1453
int add, uint8_t *ptype, uint32_t *pvmask, ipfw_xtable_info *xi)
1454
{
1455
uint8_t type, tflags;
1456
uint32_t vmask;
1457
int error;
1458
1459
type = 0;
1460
tflags = 0;
1461
vmask = 0;
1462
1463
if (xi->tablename[0] == '\0')
1464
error = table_get_info(oh, xi);
1465
else
1466
error = 0;
1467
1468
if (error == 0) {
1469
if (g_co.test_only == 0) {
1470
/* Table found */
1471
type = xi->type;
1472
tflags = xi->tflags;
1473
vmask = xi->vmask;
1474
} else {
1475
/*
1476
* We're running `ipfw -n`
1477
* Compatibility layer: try to guess key type
1478
* before failing.
1479
*/
1480
if (guess_key_type(key, &type) != 0) {
1481
/* Inknown key */
1482
errx(EX_USAGE, "Cannot guess "
1483
"key '%s' type", key);
1484
}
1485
vmask = IPFW_VTYPE_LEGACY;
1486
}
1487
} else {
1488
if (error != ESRCH)
1489
errx(EX_OSERR, "Error requesting table %s info",
1490
oh->ntlv.name);
1491
if (add == 0)
1492
errx(EX_DATAERR, "Table %s does not exist",
1493
oh->ntlv.name);
1494
/*
1495
* Table does not exist
1496
* Compatibility layer: try to guess key type before failing.
1497
*/
1498
if (guess_key_type(key, &type) != 0) {
1499
/* Inknown key */
1500
errx(EX_USAGE, "Table %s does not exist, cannot guess "
1501
"key '%s' type", oh->ntlv.name, key);
1502
}
1503
1504
vmask = IPFW_VTYPE_LEGACY;
1505
}
1506
1507
tentry_fill_key_type(key, tent, type, tflags);
1508
1509
*ptype = type;
1510
*pvmask = vmask;
1511
}
1512
1513
static void
1514
set_legacy_value(uint32_t val, ipfw_table_value *v)
1515
{
1516
v->tag = val;
1517
v->pipe = val;
1518
v->divert = val;
1519
v->skipto = val;
1520
v->netgraph = val;
1521
v->fib = val;
1522
v->nat = val;
1523
v->nh4 = val;
1524
v->dscp = (uint8_t)val;
1525
v->limit = val;
1526
}
1527
1528
static void
1529
tentry_fill_value(ipfw_obj_header *oh __unused, ipfw_obj_tentry *tent,
1530
char *arg, uint8_t type __unused, uint32_t vmask)
1531
{
1532
struct addrinfo hints, *res;
1533
struct in_addr ipaddr;
1534
const char *etype;
1535
char *comma, *e, *n, *p;
1536
uint32_t a4, flag, val;
1537
ipfw_table_value *v;
1538
uint32_t i;
1539
int dval;
1540
1541
v = &tent->v.value;
1542
1543
/* Compat layer: keep old behavior for legacy value types */
1544
if (vmask == IPFW_VTYPE_LEGACY) {
1545
/* Try to interpret as number first */
1546
val = strtoul(arg, &p, 0);
1547
if (*p == '\0') {
1548
set_legacy_value(val, v);
1549
return;
1550
}
1551
if (inet_pton(AF_INET, arg, &val) == 1) {
1552
set_legacy_value(ntohl(val), v);
1553
return;
1554
}
1555
/* Try hostname */
1556
if (lookup_host(arg, &ipaddr) == 0) {
1557
set_legacy_value(ntohl(ipaddr.s_addr), v);
1558
return;
1559
}
1560
errx(EX_OSERR, "Unable to parse value %s", arg);
1561
}
1562
1563
/*
1564
* Shorthands: handle single value if vmask consists
1565
* of numbers only. e.g.:
1566
* vmask = "fib,skipto" -> treat input "1" as "1,1"
1567
*/
1568
1569
n = arg;
1570
etype = NULL;
1571
for (i = 1; i < (1u << 31); i *= 2) {
1572
if ((flag = (vmask & i)) == 0)
1573
continue;
1574
vmask &= ~flag;
1575
1576
if ((comma = strchr(n, ',')) != NULL)
1577
*comma = '\0';
1578
1579
switch (flag) {
1580
case IPFW_VTYPE_TAG:
1581
v->tag = strtol(n, &e, 10);
1582
if (*e != '\0')
1583
etype = "tag";
1584
break;
1585
case IPFW_VTYPE_PIPE:
1586
v->pipe = strtol(n, &e, 10);
1587
if (*e != '\0')
1588
etype = "pipe";
1589
break;
1590
case IPFW_VTYPE_DIVERT:
1591
v->divert = strtol(n, &e, 10);
1592
if (*e != '\0')
1593
etype = "divert";
1594
break;
1595
case IPFW_VTYPE_SKIPTO:
1596
v->skipto = strtol(n, &e, 10);
1597
if (*e != '\0')
1598
etype = "skipto";
1599
break;
1600
case IPFW_VTYPE_NETGRAPH:
1601
v->netgraph = strtol(n, &e, 10);
1602
if (*e != '\0')
1603
etype = "netgraph";
1604
break;
1605
case IPFW_VTYPE_FIB:
1606
v->fib = strtol(n, &e, 10);
1607
if (*e != '\0')
1608
etype = "fib";
1609
break;
1610
case IPFW_VTYPE_NAT:
1611
v->nat = strtol(n, &e, 10);
1612
if (*e != '\0')
1613
etype = "nat";
1614
break;
1615
case IPFW_VTYPE_LIMIT:
1616
v->limit = strtol(n, &e, 10);
1617
if (*e != '\0')
1618
etype = "limit";
1619
break;
1620
case IPFW_VTYPE_NH4:
1621
if (strchr(n, '.') != NULL &&
1622
inet_pton(AF_INET, n, &a4) == 1) {
1623
v->nh4 = ntohl(a4);
1624
break;
1625
}
1626
if (lookup_host(n, &ipaddr) == 0) {
1627
v->nh4 = ntohl(ipaddr.s_addr);
1628
break;
1629
}
1630
etype = "ipv4";
1631
break;
1632
case IPFW_VTYPE_DSCP:
1633
if (isalpha(*n)) {
1634
if ((dval = match_token(f_ipdscp, n)) != -1) {
1635
v->dscp = dval;
1636
break;
1637
} else
1638
etype = "DSCP code";
1639
} else {
1640
v->dscp = strtol(n, &e, 10);
1641
if (v->dscp > 63 || *e != '\0')
1642
etype = "DSCP value";
1643
}
1644
break;
1645
case IPFW_VTYPE_NH6:
1646
if (strchr(n, ':') != NULL) {
1647
memset(&hints, 0, sizeof(hints));
1648
hints.ai_family = AF_INET6;
1649
hints.ai_flags = AI_NUMERICHOST;
1650
if (getaddrinfo(n, NULL, &hints, &res) == 0) {
1651
v->nh6 = ((struct sockaddr_in6 *)
1652
res->ai_addr)->sin6_addr;
1653
v->zoneid = ((struct sockaddr_in6 *)
1654
res->ai_addr)->sin6_scope_id;
1655
freeaddrinfo(res);
1656
break;
1657
}
1658
}
1659
etype = "ipv6";
1660
break;
1661
case IPFW_VTYPE_MARK:
1662
v->mark = strtol(n, &e, 16);
1663
if (*e != '\0')
1664
etype = "mark";
1665
break;
1666
}
1667
1668
if (etype != NULL)
1669
errx(EX_USAGE, "Unable to parse %s as %s", n, etype);
1670
1671
if (comma != NULL)
1672
*comma++ = ',';
1673
1674
if ((n = comma) != NULL)
1675
continue;
1676
1677
/* End of input. */
1678
if (vmask != 0)
1679
errx(EX_USAGE, "Not enough fields inside value");
1680
}
1681
}
1682
1683
/*
1684
* Compare table names.
1685
* Honor number comparison.
1686
*/
1687
static int
1688
tablename_cmp(const void *a, const void *b)
1689
{
1690
const ipfw_xtable_info *ia, *ib;
1691
1692
ia = (const ipfw_xtable_info *)a;
1693
ib = (const ipfw_xtable_info *)b;
1694
1695
return (stringnum_cmp(ia->tablename, ib->tablename));
1696
}
1697
1698
/*
1699
* Retrieves table list from kernel,
1700
* optionally sorts it and calls requested function for each table.
1701
* Returns 0 on success.
1702
*/
1703
static int
1704
tables_foreach(table_cb_t *f, void *arg, int sort)
1705
{
1706
ipfw_obj_lheader *olh;
1707
ipfw_xtable_info *info;
1708
size_t sz;
1709
uint32_t i;
1710
1711
/* Start with reasonable default */
1712
sz = sizeof(*olh) + 16 * sizeof(ipfw_xtable_info);
1713
1714
for (;;) {
1715
if ((olh = calloc(1, sz)) == NULL)
1716
return (ENOMEM);
1717
1718
olh->size = sz;
1719
if (do_get3(IP_FW_TABLES_XLIST, &olh->opheader, &sz) != 0) {
1720
sz = olh->size;
1721
free(olh);
1722
if (errno != ENOMEM)
1723
return (errno);
1724
continue;
1725
}
1726
1727
if (sort != 0)
1728
qsort(olh + 1, olh->count, olh->objsize,
1729
tablename_cmp);
1730
1731
info = (ipfw_xtable_info *)(olh + 1);
1732
for (i = 0; i < olh->count; i++) {
1733
if (g_co.use_set == 0 || info->set == g_co.use_set - 1)
1734
(void)f(info, arg);
1735
info = (ipfw_xtable_info *)((caddr_t)info +
1736
olh->objsize);
1737
}
1738
free(olh);
1739
break;
1740
}
1741
return (0);
1742
}
1743
1744
1745
/*
1746
* Retrieves all entries for given table @i in
1747
* eXtended format. Allocate buffer large enough
1748
* to store result. Called needs to free it later.
1749
*
1750
* Returns 0 on success.
1751
*/
1752
static int
1753
table_do_get_list(ipfw_xtable_info *i, ipfw_obj_header **poh)
1754
{
1755
ipfw_obj_header *oh;
1756
size_t sz;
1757
int c;
1758
1759
sz = 0;
1760
oh = NULL;
1761
for (c = 0; c < 8; c++) {
1762
if (sz < i->size)
1763
sz = i->size + 44;
1764
if (oh != NULL)
1765
free(oh);
1766
if ((oh = calloc(1, sz)) == NULL)
1767
continue;
1768
table_fill_objheader(oh, i);
1769
oh->opheader.version = 1; /* Current version */
1770
if (do_get3(IP_FW_TABLE_XLIST, &oh->opheader, &sz) == 0) {
1771
*poh = oh;
1772
return (0);
1773
}
1774
1775
if (errno != ENOMEM)
1776
break;
1777
}
1778
free(oh);
1779
1780
return (errno);
1781
}
1782
1783
/*
1784
* Shows all entries from @oh in human-readable format
1785
*/
1786
static void
1787
table_show_list(ipfw_obj_header *oh, int need_header)
1788
{
1789
ipfw_obj_tentry *tent;
1790
uint32_t count;
1791
ipfw_xtable_info *i;
1792
1793
i = (ipfw_xtable_info *)(oh + 1);
1794
tent = (ipfw_obj_tentry *)(i + 1);
1795
1796
if (need_header)
1797
printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
1798
1799
count = i->count;
1800
while (count > 0) {
1801
table_show_entry(i, tent);
1802
tent = (ipfw_obj_tentry *)((caddr_t)tent + tent->head.length);
1803
count--;
1804
}
1805
}
1806
1807
static void
1808
table_show_value(char *buf, size_t bufsize, ipfw_table_value *v,
1809
uint32_t vmask, int print_ip)
1810
{
1811
char abuf[INET6_ADDRSTRLEN + IF_NAMESIZE + 2];
1812
struct sockaddr_in6 sa6;
1813
uint32_t flag, i, l;
1814
size_t sz;
1815
struct in_addr a4;
1816
1817
sz = bufsize;
1818
1819
/*
1820
* Some shorthands for printing values:
1821
* legacy assumes all values are equal, so keep the first one.
1822
*/
1823
if (vmask == IPFW_VTYPE_LEGACY) {
1824
if (print_ip != 0) {
1825
flag = htonl(v->tag);
1826
inet_ntop(AF_INET, &flag, buf, sz);
1827
} else
1828
snprintf(buf, sz, "%u", v->tag);
1829
return;
1830
}
1831
1832
for (i = 1; i < (1u << 31); i *= 2) {
1833
if ((flag = (vmask & i)) == 0)
1834
continue;
1835
l = 0;
1836
1837
switch (flag) {
1838
case IPFW_VTYPE_TAG:
1839
l = snprintf(buf, sz, "%u,", v->tag);
1840
break;
1841
case IPFW_VTYPE_PIPE:
1842
l = snprintf(buf, sz, "%u,", v->pipe);
1843
break;
1844
case IPFW_VTYPE_DIVERT:
1845
l = snprintf(buf, sz, "%d,", v->divert);
1846
break;
1847
case IPFW_VTYPE_SKIPTO:
1848
l = snprintf(buf, sz, "%d,", v->skipto);
1849
break;
1850
case IPFW_VTYPE_NETGRAPH:
1851
l = snprintf(buf, sz, "%u,", v->netgraph);
1852
break;
1853
case IPFW_VTYPE_FIB:
1854
l = snprintf(buf, sz, "%u,", v->fib);
1855
break;
1856
case IPFW_VTYPE_NAT:
1857
l = snprintf(buf, sz, "%u,", v->nat);
1858
break;
1859
case IPFW_VTYPE_LIMIT:
1860
l = snprintf(buf, sz, "%u,", v->limit);
1861
break;
1862
case IPFW_VTYPE_NH4:
1863
a4.s_addr = htonl(v->nh4);
1864
inet_ntop(AF_INET, &a4, abuf, sizeof(abuf));
1865
l = snprintf(buf, sz, "%s,", abuf);
1866
break;
1867
case IPFW_VTYPE_DSCP:
1868
l = snprintf(buf, sz, "%d,", v->dscp);
1869
break;
1870
case IPFW_VTYPE_NH6:
1871
sa6.sin6_family = AF_INET6;
1872
sa6.sin6_len = sizeof(sa6);
1873
sa6.sin6_addr = v->nh6;
1874
sa6.sin6_port = 0;
1875
sa6.sin6_scope_id = v->zoneid;
1876
if (getnameinfo((const struct sockaddr *)&sa6,
1877
sa6.sin6_len, abuf, sizeof(abuf), NULL, 0,
1878
NI_NUMERICHOST) == 0)
1879
l = snprintf(buf, sz, "%s,", abuf);
1880
break;
1881
case IPFW_VTYPE_MARK:
1882
l = snprintf(buf, sz, "%#x,", v->mark);
1883
break;
1884
}
1885
1886
buf += l;
1887
sz -= l;
1888
}
1889
1890
if (sz != bufsize)
1891
*(buf - 1) = '\0';
1892
}
1893
1894
static void
1895
table_show_entry(ipfw_xtable_info *i, ipfw_obj_tentry *tent)
1896
{
1897
char tbuf[128], pval[128];
1898
const char *comma;
1899
const u_char *mac;
1900
void *paddr;
1901
struct tflow_entry *tfe;
1902
1903
table_show_value(pval, sizeof(pval), &tent->v.value, i->vmask,
1904
g_co.do_value_as_ip);
1905
1906
switch (i->type) {
1907
case IPFW_TABLE_ADDR:
1908
/* IPv4 or IPv6 prefixes */
1909
inet_ntop(tent->subtype, &tent->k, tbuf, sizeof(tbuf));
1910
printf("%s/%u %s\n", tbuf, tent->masklen, pval);
1911
break;
1912
case IPFW_TABLE_MAC:
1913
/* MAC prefixes */
1914
mac = tent->k.mac;
1915
printf("%02x:%02x:%02x:%02x:%02x:%02x/%u %s\n",
1916
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
1917
tent->masklen, pval);
1918
break;
1919
case IPFW_TABLE_INTERFACE:
1920
/* Interface names */
1921
printf("%s %s\n", tent->k.iface, pval);
1922
break;
1923
case IPFW_TABLE_NUMBER:
1924
/* numbers */
1925
printf("%u %s\n", tent->k.key, pval);
1926
break;
1927
case IPFW_TABLE_FLOW:
1928
/* flows */
1929
tfe = &tent->k.flow;
1930
comma = "";
1931
1932
if ((i->tflags & IPFW_TFFLAG_SRCIP) != 0) {
1933
if (tfe->af == AF_INET)
1934
paddr = &tfe->a.a4.sip;
1935
else
1936
paddr = &tfe->a.a6.sip6;
1937
1938
inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1939
printf("%s%s", comma, tbuf);
1940
comma = ",";
1941
}
1942
1943
if ((i->tflags & IPFW_TFFLAG_PROTO) != 0) {
1944
printf("%s%d", comma, tfe->proto);
1945
comma = ",";
1946
}
1947
1948
if ((i->tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1949
printf("%s%d", comma, ntohs(tfe->sport));
1950
comma = ",";
1951
}
1952
if ((i->tflags & IPFW_TFFLAG_DSTIP) != 0) {
1953
if (tfe->af == AF_INET)
1954
paddr = &tfe->a.a4.dip;
1955
else
1956
paddr = &tfe->a.a6.dip6;
1957
1958
inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1959
printf("%s%s", comma, tbuf);
1960
comma = ",";
1961
}
1962
1963
if ((i->tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1964
printf("%s%d", comma, ntohs(tfe->dport));
1965
comma = ",";
1966
}
1967
1968
printf(" %s\n", pval);
1969
}
1970
}
1971
1972
static int
1973
table_do_get_stdlist(uint16_t opcode, ipfw_obj_lheader **polh)
1974
{
1975
ipfw_obj_lheader req, *olh;
1976
size_t sz;
1977
1978
memset(&req, 0, sizeof(req));
1979
sz = sizeof(req);
1980
1981
if (do_get3(opcode, &req.opheader, &sz) != 0)
1982
if (errno != ENOMEM)
1983
return (errno);
1984
1985
sz = req.size;
1986
if ((olh = calloc(1, sz)) == NULL)
1987
return (ENOMEM);
1988
1989
olh->size = sz;
1990
if (do_get3(opcode, &olh->opheader, &sz) != 0) {
1991
free(olh);
1992
return (errno);
1993
}
1994
1995
*polh = olh;
1996
return (0);
1997
}
1998
1999
static int
2000
table_do_get_algolist(ipfw_obj_lheader **polh)
2001
{
2002
2003
return (table_do_get_stdlist(IP_FW_TABLES_ALIST, polh));
2004
}
2005
2006
static int
2007
table_do_get_vlist(ipfw_obj_lheader **polh)
2008
{
2009
2010
return (table_do_get_stdlist(IP_FW_TABLE_VLIST, polh));
2011
}
2012
2013
void
2014
ipfw_list_ta(int ac __unused, char *av[] __unused)
2015
{
2016
ipfw_obj_lheader *olh;
2017
ipfw_ta_info *info;
2018
const char *atype;
2019
uint32_t i;
2020
int error;
2021
2022
error = table_do_get_algolist(&olh);
2023
if (error != 0)
2024
err(EX_OSERR, "Unable to request algorithm list");
2025
2026
info = (ipfw_ta_info *)(olh + 1);
2027
for (i = 0; i < olh->count; i++) {
2028
if ((atype = match_value(tabletypes, info->type)) == NULL)
2029
atype = "unknown";
2030
printf("--- %s ---\n", info->algoname);
2031
printf(" type: %s\n refcount: %u\n", atype, info->refcnt);
2032
2033
info = (ipfw_ta_info *)((caddr_t)info + olh->objsize);
2034
}
2035
2036
free(olh);
2037
}
2038
2039
2040
static int
2041
compare_values(const void *_a, const void *_b)
2042
{
2043
const ipfw_table_value *a, *b;
2044
2045
a = (const ipfw_table_value *)_a;
2046
b = (const ipfw_table_value *)_b;
2047
2048
if (a->kidx < b->kidx)
2049
return (-1);
2050
else if (a->kidx > b->kidx)
2051
return (1);
2052
2053
return (0);
2054
}
2055
2056
void
2057
ipfw_list_values(int ac __unused, char *av[] __unused)
2058
{
2059
char buf[128];
2060
ipfw_obj_lheader *olh;
2061
ipfw_table_value *v;
2062
uint32_t i, vmask;
2063
int error;
2064
2065
error = table_do_get_vlist(&olh);
2066
if (error != 0)
2067
err(EX_OSERR, "Unable to request value list");
2068
2069
vmask = 0x7FFFFFFF; /* Similar to IPFW_VTYPE_LEGACY */
2070
2071
table_print_valheader(buf, sizeof(buf), vmask);
2072
printf("HEADER: %s\n", buf);
2073
v = (ipfw_table_value *)(olh + 1);
2074
qsort(v, olh->count, olh->objsize, compare_values);
2075
for (i = 0; i < olh->count; i++) {
2076
table_show_value(buf, sizeof(buf), (ipfw_table_value *)v,
2077
vmask, 0);
2078
printf("[%u] refs=%lu %s\n", v->kidx, (u_long)v->refcnt, buf);
2079
v = (ipfw_table_value *)((caddr_t)v + olh->objsize);
2080
}
2081
2082
free(olh);
2083
}
2084
2085
int
2086
table_check_name(const char *tablename)
2087
{
2088
2089
if (ipfw_check_object_name(tablename) != 0)
2090
return (EINVAL);
2091
/* Restrict some 'special' names */
2092
if (strcmp(tablename, "all") == 0)
2093
return (EINVAL);
2094
return (0);
2095
}
2096
2097
2098