1 /* $FreeBSD$ */
2
3 /*
4 * Copyright (C) 2012 by Darren Reed.
5 *
6 * See the IPFILTER.LICENCE file for details on licencing.
7 *
8 * Added redirect stuff and a variety of bug fixes. (mcn@EnGarde.com)
9 */
10 #include <stdio.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #include <errno.h>
14 #include <sys/types.h>
15 #if !defined(__SVR4) && !defined(__svr4__)
16 #include <strings.h>
17 #else
18 #include <sys/byteorder.h>
19 #endif
20 #include <sys/time.h>
21 #include <sys/param.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stddef.h>
25 #include <sys/file.h>
26 #define _KERNEL
27 #include <sys/uio.h>
28 #undef _KERNEL
29 #include <sys/socket.h>
30 #include <sys/ioctl.h>
31 #if defined(sun) && (defined(__svr4__) || defined(__SVR4))
32 # include <sys/ioccom.h>
33 # include <sys/sysmacros.h>
34 #endif
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/tcp.h>
39 #include <net/if.h>
40 #if __FreeBSD_version >= 300000
41 # include <net/if_var.h>
42 #endif
43 #include <netdb.h>
44 #include <arpa/nameser.h>
45 #include <arpa/inet.h>
46 #include <resolv.h>
47 #include <ctype.h>
48 #if defined(linux)
49 # include <linux/a.out.h>
50 #else
51 # include <nlist.h>
52 #endif
53 #include "ipf.h"
54 #include "netinet/ipl.h"
55 #include "kmem.h"
56
57 #ifdef __hpux
58 # define nlist nlist64
59 #endif
60
61 #if defined(sun) && !SOLARIS2
62 # define STRERROR(x) sys_errlist[x]
63 extern char *sys_errlist[];
64 #else
65 # define STRERROR(x) strerror(x)
66 #endif
67
68 #if !defined(lint)
69 static const char sccsid[] ="@(#)ipnat.c 1.9 6/5/96 (C) 1993 Darren Reed";
70 static const char rcsid[] = "@(#)$Id$";
71 #endif
72
73
74 #if SOLARIS
75 #define bzero(a,b) memset(a,0,b)
76 #endif
77 int use_inet6 = 0;
78 char thishost[MAXHOSTNAMELEN];
79
80 extern char *optarg;
81
82 void dostats __P((int, natstat_t *, int, int, int *));
83 void dotable __P((natstat_t *, int, int, int, char *));
84 void flushtable __P((int, int, int *));
85 void usage __P((char *));
86 int main __P((int, char*[]));
87 void showhostmap __P((natstat_t *nsp));
88 void natstat_dead __P((natstat_t *, char *));
89 void dostats_live __P((int, natstat_t *, int, int *));
90 void showhostmap_dead __P((natstat_t *));
91 void showhostmap_live __P((int, natstat_t *));
92 void dostats_dead __P((natstat_t *, int, int *));
93 int nat_matcharray __P((nat_t *, int *));
94
95 int opts;
96 int nohdrfields = 0;
97 wordtab_t *nat_fields = NULL;
98
usage(name)99 void usage(name)
100 char *name;
101 {
102 fprintf(stderr, "Usage: %s [-CFhlnrRsv] [-f filename]\n", name);
103 exit(1);
104 }
105
106
main(argc,argv)107 int main(argc, argv)
108 int argc;
109 char *argv[];
110 {
111 int fd, c, mode, *natfilter;
112 char *file, *core, *kernel;
113 natstat_t ns, *nsp;
114 ipfobj_t obj;
115
116 fd = -1;
117 opts = 0;
118 nsp = &ns;
119 file = NULL;
120 core = NULL;
121 kernel = NULL;
122 mode = O_RDWR;
123 natfilter = NULL;
124
125 assigndefined(getenv("IPNAT_PREDEFINED"));
126
127 while ((c = getopt(argc, argv, "CdFf:hlm:M:N:nO:prRsv")) != -1)
128 switch (c)
129 {
130 case 'C' :
131 opts |= OPT_CLEAR;
132 break;
133 case 'd' :
134 opts |= OPT_DEBUG;
135 break;
136 case 'f' :
137 file = optarg;
138 break;
139 case 'F' :
140 opts |= OPT_FLUSH;
141 break;
142 case 'h' :
143 opts |=OPT_HITS;
144 break;
145 case 'l' :
146 opts |= OPT_LIST;
147 mode = O_RDONLY;
148 break;
149 case 'm' :
150 natfilter = parseipfexpr(optarg, NULL);
151 break;
152 case 'M' :
153 core = optarg;
154 break;
155 case 'N' :
156 kernel = optarg;
157 break;
158 case 'n' :
159 opts |= OPT_DONOTHING|OPT_DONTOPEN;
160 mode = O_RDONLY;
161 break;
162 case 'O' :
163 nat_fields = parsefields(natfields, optarg);
164 break;
165 case 'p' :
166 opts |= OPT_PURGE;
167 break;
168 case 'R' :
169 opts |= OPT_NORESOLVE;
170 break;
171 case 'r' :
172 opts |= OPT_REMOVE;
173 break;
174 case 's' :
175 opts |= OPT_STAT;
176 mode = O_RDONLY;
177 break;
178 case 'v' :
179 opts |= OPT_VERBOSE;
180 break;
181 default :
182 usage(argv[0]);
183 }
184
185 if (((opts & OPT_PURGE) != 0) && ((opts & OPT_REMOVE) == 0)) {
186 (void) fprintf(stderr, "%s: -p must be used with -r\n",
187 argv[0]);
188 exit(1);
189 }
190
191 initparse();
192
193 if ((kernel != NULL) || (core != NULL)) {
194 (void) setgid(getgid());
195 (void) setuid(getuid());
196 }
197
198 if (!(opts & OPT_DONOTHING)) {
199 if (((fd = open(IPNAT_NAME, mode)) == -1) &&
200 ((fd = open(IPNAT_NAME, O_RDONLY)) == -1)) {
201 (void) fprintf(stderr, "%s: open: %s\n", IPNAT_NAME,
202 STRERROR(errno));
203 exit(1);
204 }
205 }
206
207 bzero((char *)&ns, sizeof(ns));
208
209 if ((opts & OPT_DONOTHING) == 0) {
210 if (checkrev(IPL_NAME) == -1) {
211 fprintf(stderr, "User/kernel version check failed\n");
212 exit(1);
213 }
214 }
215
216 if (!(opts & OPT_DONOTHING) && (kernel == NULL) && (core == NULL)) {
217 bzero((char *)&obj, sizeof(obj));
218 obj.ipfo_rev = IPFILTER_VERSION;
219 obj.ipfo_type = IPFOBJ_NATSTAT;
220 obj.ipfo_size = sizeof(*nsp);
221 obj.ipfo_ptr = (void *)nsp;
222 if (ioctl(fd, SIOCGNATS, &obj) == -1) {
223 ipferror(fd, "ioctl(SIOCGNATS)");
224 exit(1);
225 }
226 (void) setgid(getgid());
227 (void) setuid(getuid());
228 } else if ((kernel != NULL) || (core != NULL)) {
229 if (openkmem(kernel, core) == -1)
230 exit(1);
231
232 natstat_dead(nsp, kernel);
233 if (opts & (OPT_LIST|OPT_STAT))
234 dostats(fd, nsp, opts, 0, natfilter);
235 exit(0);
236 }
237
238 if (opts & (OPT_FLUSH|OPT_CLEAR))
239 flushtable(fd, opts, natfilter);
240 if (file) {
241 return ipnat_parsefile(fd, ipnat_addrule, ioctl, file);
242 }
243 if (opts & (OPT_LIST|OPT_STAT))
244 dostats(fd, nsp, opts, 1, natfilter);
245 return 0;
246 }
247
248
249 /*
250 * Read NAT statistic information in using a symbol table and memory file
251 * rather than doing ioctl's.
252 */
natstat_dead(nsp,kernel)253 void natstat_dead(nsp, kernel)
254 natstat_t *nsp;
255 char *kernel;
256 {
257 struct nlist nat_nlist[10] = {
258 { "nat_table" }, /* 0 */
259 { "nat_list" },
260 { "maptable" },
261 { "ipf_nattable_sz" },
262 { "ipf_natrules_sz" },
263 { "ipf_rdrrules_sz" }, /* 5 */
264 { "ipf_hostmap_sz" },
265 { "nat_instances" },
266 { NULL }
267 };
268 void *tables[2];
269
270 if (nlist(kernel, nat_nlist) == -1) {
271 fprintf(stderr, "nlist error\n");
272 return;
273 }
274
275 /*
276 * Normally the ioctl copies all of these values into the structure
277 * for us, before returning it to userland, so here we must copy each
278 * one in individually.
279 */
280 kmemcpy((char *)&tables, nat_nlist[0].n_value, sizeof(tables));
281 nsp->ns_side[0].ns_table = tables[0];
282 nsp->ns_side[1].ns_table = tables[1];
283
284 kmemcpy((char *)&nsp->ns_list, nat_nlist[1].n_value,
285 sizeof(nsp->ns_list));
286 kmemcpy((char *)&nsp->ns_maptable, nat_nlist[2].n_value,
287 sizeof(nsp->ns_maptable));
288 kmemcpy((char *)&nsp->ns_nattab_sz, nat_nlist[3].n_value,
289 sizeof(nsp->ns_nattab_sz));
290 kmemcpy((char *)&nsp->ns_rultab_sz, nat_nlist[4].n_value,
291 sizeof(nsp->ns_rultab_sz));
292 kmemcpy((char *)&nsp->ns_rdrtab_sz, nat_nlist[5].n_value,
293 sizeof(nsp->ns_rdrtab_sz));
294 kmemcpy((char *)&nsp->ns_hostmap_sz, nat_nlist[6].n_value,
295 sizeof(nsp->ns_hostmap_sz));
296 kmemcpy((char *)&nsp->ns_instances, nat_nlist[7].n_value,
297 sizeof(nsp->ns_instances));
298 }
299
300
301 /*
302 * Issue an ioctl to flush either the NAT rules table or the active mapping
303 * table or both.
304 */
flushtable(fd,opts,match)305 void flushtable(fd, opts, match)
306 int fd, opts, *match;
307 {
308 int n = 0;
309
310 if (opts & OPT_FLUSH) {
311 n = 0;
312 if (!(opts & OPT_DONOTHING)) {
313 if (match != NULL) {
314 ipfobj_t obj;
315
316 obj.ipfo_rev = IPFILTER_VERSION;
317 obj.ipfo_size = match[0] * sizeof(int);
318 obj.ipfo_type = IPFOBJ_IPFEXPR;
319 obj.ipfo_ptr = match;
320 if (ioctl(fd, SIOCMATCHFLUSH, &obj) == -1) {
321 ipferror(fd, "ioctl(SIOCMATCHFLUSH)");
322 n = -1;
323 } else {
324 n = obj.ipfo_retval;
325 }
326 } else if (ioctl(fd, SIOCIPFFL, &n) == -1) {
327 ipferror(fd, "ioctl(SIOCIPFFL)");
328 n = -1;
329 }
330 }
331 if (n >= 0)
332 printf("%d entries flushed from NAT table\n", n);
333 }
334
335 if (opts & OPT_CLEAR) {
336 n = 1;
337 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
338 ipferror(fd, "ioctl(SIOCCNATL)");
339 else
340 printf("%d entries flushed from NAT list\n", n);
341 }
342 }
343
344
345 /*
346 * Display NAT statistics.
347 */
dostats_dead(nsp,opts,filter)348 void dostats_dead(nsp, opts, filter)
349 natstat_t *nsp;
350 int opts, *filter;
351 {
352 nat_t *np, nat;
353 ipnat_t ipn;
354 int i;
355
356 if (nat_fields == NULL) {
357 printf("List of active MAP/Redirect filters:\n");
358 while (nsp->ns_list) {
359 if (kmemcpy((char *)&ipn, (long)nsp->ns_list,
360 sizeof(ipn))) {
361 perror("kmemcpy");
362 break;
363 }
364 if (opts & OPT_HITS)
365 printf("%lu ", ipn.in_hits);
366 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
367 nsp->ns_list = ipn.in_next;
368 }
369 }
370
371 if (nat_fields == NULL) {
372 printf("\nList of active sessions:\n");
373
374 } else if (nohdrfields == 0) {
375 for (i = 0; nat_fields[i].w_value != 0; i++) {
376 printfieldhdr(natfields, nat_fields + i);
377 if (nat_fields[i + 1].w_value != 0)
378 printf("\t");
379 }
380 printf("\n");
381 }
382
383 for (np = nsp->ns_instances; np; np = nat.nat_next) {
384 if (kmemcpy((char *)&nat, (long)np, sizeof(nat)))
385 break;
386 if ((filter != NULL) && (nat_matcharray(&nat, filter) == 0))
387 continue;
388 if (nat_fields != NULL) {
389 for (i = 0; nat_fields[i].w_value != 0; i++) {
390 printnatfield(&nat, nat_fields[i].w_value);
391 if (nat_fields[i + 1].w_value != 0)
392 printf("\t");
393 }
394 printf("\n");
395 } else {
396 printactivenat(&nat, opts, nsp->ns_ticks);
397 if (nat.nat_aps) {
398 int proto;
399
400 if (nat.nat_dir & NAT_OUTBOUND)
401 proto = nat.nat_pr[1];
402 else
403 proto = nat.nat_pr[0];
404 printaps(nat.nat_aps, opts, proto);
405 }
406 }
407 }
408
409 if (opts & OPT_VERBOSE)
410 showhostmap_dead(nsp);
411 }
412
413
dotable(nsp,fd,alive,which,side)414 void dotable(nsp, fd, alive, which, side)
415 natstat_t *nsp;
416 int fd, alive, which;
417 char *side;
418 {
419 int sz, i, used, maxlen, minlen, totallen;
420 ipftable_t table;
421 u_int *buckets;
422 ipfobj_t obj;
423
424 sz = sizeof(*buckets) * nsp->ns_nattab_sz;
425 buckets = (u_int *)malloc(sz);
426 if (buckets == NULL) {
427 fprintf(stderr,
428 "cannot allocate memory (%d) for buckets\n", sz);
429 return;
430 }
431
432 obj.ipfo_rev = IPFILTER_VERSION;
433 obj.ipfo_type = IPFOBJ_GTABLE;
434 obj.ipfo_size = sizeof(table);
435 obj.ipfo_ptr = &table;
436
437 if (which == 0) {
438 table.ita_type = IPFTABLE_BUCKETS_NATIN;
439 } else if (which == 1) {
440 table.ita_type = IPFTABLE_BUCKETS_NATOUT;
441 }
442 table.ita_table = buckets;
443
444 if (alive) {
445 if (ioctl(fd, SIOCGTABL, &obj) != 0) {
446 ipferror(fd, "SIOCFTABL");
447 free(buckets);
448 return;
449 }
450 } else {
451 if (kmemcpy((char *)buckets, (u_long)nsp->ns_nattab_sz, sz)) {
452 free(buckets);
453 return;
454 }
455 }
456
457 minlen = nsp->ns_side[which].ns_inuse;
458 totallen = 0;
459 maxlen = 0;
460 used = 0;
461
462 for (i = 0; i < nsp->ns_nattab_sz; i++) {
463 if (buckets[i] > maxlen)
464 maxlen = buckets[i];
465 if (buckets[i] < minlen)
466 minlen = buckets[i];
467 if (buckets[i] != 0)
468 used++;
469 totallen += buckets[i];
470 }
471
472 printf("%d%%\thash efficiency %s\n",
473 totallen ? used * 100 / totallen : 0, side);
474 printf("%2.2f%%\tbucket usage %s\n",
475 ((float)used / nsp->ns_nattab_sz) * 100.0, side);
476 printf("%d\tminimal length %s\n", minlen, side);
477 printf("%d\tmaximal length %s\n", maxlen, side);
478 printf("%.3f\taverage length %s\n",
479 used ? ((float)totallen / used) : 0.0, side);
480
481 free(buckets);
482 }
483
484
dostats(fd,nsp,opts,alive,filter)485 void dostats(fd, nsp, opts, alive, filter)
486 natstat_t *nsp;
487 int fd, opts, alive, *filter;
488 {
489 /*
490 * Show statistics ?
491 */
492 if (opts & OPT_STAT) {
493 printnatside("in", &nsp->ns_side[0]);
494 dotable(nsp, fd, alive, 0, "in");
495
496 printnatside("out", &nsp->ns_side[1]);
497 dotable(nsp, fd, alive, 1, "out");
498
499 printf("%lu\tlog successes\n", nsp->ns_side[0].ns_log);
500 printf("%lu\tlog failures\n", nsp->ns_side[1].ns_log);
501 printf("%lu\tadded in\n%lu\tadded out\n",
502 nsp->ns_side[0].ns_added,
503 nsp->ns_side[1].ns_added);
504 printf("%u\tactive\n", nsp->ns_active);
505 printf("%lu\ttransparent adds\n", nsp->ns_addtrpnt);
506 printf("%lu\tdivert build\n", nsp->ns_divert_build);
507 printf("%lu\texpired\n", nsp->ns_expire);
508 printf("%lu\tflush all\n", nsp->ns_flush_all);
509 printf("%lu\tflush closing\n", nsp->ns_flush_closing);
510 printf("%lu\tflush queue\n", nsp->ns_flush_queue);
511 printf("%lu\tflush state\n", nsp->ns_flush_state);
512 printf("%lu\tflush timeout\n", nsp->ns_flush_timeout);
513 printf("%lu\thostmap new\n", nsp->ns_hm_new);
514 printf("%lu\thostmap fails\n", nsp->ns_hm_newfail);
515 printf("%lu\thostmap add\n", nsp->ns_hm_addref);
516 printf("%lu\thostmap NULL rule\n", nsp->ns_hm_nullnp);
517 printf("%lu\tlog ok\n", nsp->ns_log_ok);
518 printf("%lu\tlog fail\n", nsp->ns_log_fail);
519 printf("%u\torphan count\n", nsp->ns_orphans);
520 printf("%u\trule count\n", nsp->ns_rules);
521 printf("%u\tmap rules\n", nsp->ns_rules_map);
522 printf("%u\trdr rules\n", nsp->ns_rules_rdr);
523 printf("%u\twilds\n", nsp->ns_wilds);
524 if (opts & OPT_VERBOSE)
525 printf("list %p\n", nsp->ns_list);
526 }
527
528 if (opts & OPT_LIST) {
529 if (alive)
530 dostats_live(fd, nsp, opts, filter);
531 else
532 dostats_dead(nsp, opts, filter);
533 }
534 }
535
536
537 /*
538 * Display NAT statistics.
539 */
dostats_live(fd,nsp,opts,filter)540 void dostats_live(fd, nsp, opts, filter)
541 natstat_t *nsp;
542 int fd, opts, *filter;
543 {
544 ipfgeniter_t iter;
545 char buffer[2000];
546 ipfobj_t obj;
547 ipnat_t *ipn;
548 nat_t nat;
549 int i;
550
551 bzero((char *)&obj, sizeof(obj));
552 obj.ipfo_rev = IPFILTER_VERSION;
553 obj.ipfo_type = IPFOBJ_GENITER;
554 obj.ipfo_size = sizeof(iter);
555 obj.ipfo_ptr = &iter;
556
557 iter.igi_type = IPFGENITER_IPNAT;
558 iter.igi_nitems = 1;
559 iter.igi_data = buffer;
560 ipn = (ipnat_t *)buffer;
561
562 /*
563 * Show list of NAT rules and NAT sessions ?
564 */
565 if (nat_fields == NULL) {
566 printf("List of active MAP/Redirect filters:\n");
567 while (nsp->ns_list) {
568 if (ioctl(fd, SIOCGENITER, &obj) == -1)
569 break;
570 if (opts & OPT_HITS)
571 printf("%lu ", ipn->in_hits);
572 printnat(ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
573 nsp->ns_list = ipn->in_next;
574 }
575 }
576
577 if (nat_fields == NULL) {
578 printf("\nList of active sessions:\n");
579
580 } else if (nohdrfields == 0) {
581 for (i = 0; nat_fields[i].w_value != 0; i++) {
582 printfieldhdr(natfields, nat_fields + i);
583 if (nat_fields[i + 1].w_value != 0)
584 printf("\t");
585 }
586 printf("\n");
587 }
588
589 i = IPFGENITER_IPNAT;
590 (void) ioctl(fd,SIOCIPFDELTOK, &i);
591
592
593 iter.igi_type = IPFGENITER_NAT;
594 iter.igi_nitems = 1;
595 iter.igi_data = &nat;
596
597 while (nsp->ns_instances != NULL) {
598 if (ioctl(fd, SIOCGENITER, &obj) == -1)
599 break;
600 if ((filter != NULL) && (nat_matcharray(&nat, filter) == 0))
601 continue;
602 if (nat_fields != NULL) {
603 for (i = 0; nat_fields[i].w_value != 0; i++) {
604 printnatfield(&nat, nat_fields[i].w_value);
605 if (nat_fields[i + 1].w_value != 0)
606 printf("\t");
607 }
608 printf("\n");
609 } else {
610 printactivenat(&nat, opts, nsp->ns_ticks);
611 if (nat.nat_aps) {
612 int proto;
613
614 if (nat.nat_dir & NAT_OUTBOUND)
615 proto = nat.nat_pr[1];
616 else
617 proto = nat.nat_pr[0];
618 printaps(nat.nat_aps, opts, proto);
619 }
620 }
621 nsp->ns_instances = nat.nat_next;
622 }
623
624 if (opts & OPT_VERBOSE)
625 showhostmap_live(fd, nsp);
626
627 i = IPFGENITER_NAT;
628 (void) ioctl(fd,SIOCIPFDELTOK, &i);
629 }
630
631
632 /*
633 * Display the active host mapping table.
634 */
showhostmap_dead(nsp)635 void showhostmap_dead(nsp)
636 natstat_t *nsp;
637 {
638 hostmap_t hm, *hmp, **maptable;
639 u_int hv;
640
641 printf("\nList of active host mappings:\n");
642
643 maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) *
644 nsp->ns_hostmap_sz);
645 if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable,
646 sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) {
647 perror("kmemcpy (maptable)");
648 return;
649 }
650
651 for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) {
652 hmp = maptable[hv];
653
654 while (hmp) {
655 if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) {
656 perror("kmemcpy (hostmap)");
657 return;
658 }
659
660 printhostmap(&hm, hv);
661 hmp = hm.hm_next;
662 }
663 }
664 free(maptable);
665 }
666
667
668 /*
669 * Display the active host mapping table.
670 */
showhostmap_live(fd,nsp)671 void showhostmap_live(fd, nsp)
672 int fd;
673 natstat_t *nsp;
674 {
675 ipfgeniter_t iter;
676 hostmap_t hm;
677 ipfobj_t obj;
678 int i;
679
680 bzero((char *)&obj, sizeof(obj));
681 obj.ipfo_rev = IPFILTER_VERSION;
682 obj.ipfo_type = IPFOBJ_GENITER;
683 obj.ipfo_size = sizeof(iter);
684 obj.ipfo_ptr = &iter;
685
686 iter.igi_type = IPFGENITER_HOSTMAP;
687 iter.igi_nitems = 1;
688 iter.igi_data = &hm;
689
690 printf("\nList of active host mappings:\n");
691
692 while (nsp->ns_maplist != NULL) {
693 if (ioctl(fd, SIOCGENITER, &obj) == -1)
694 break;
695 printhostmap(&hm, hm.hm_hv);
696 nsp->ns_maplist = hm.hm_next;
697 }
698
699 i = IPFGENITER_HOSTMAP;
700 (void) ioctl(fd,SIOCIPFDELTOK, &i);
701 }
702
703
nat_matcharray(nat,array)704 int nat_matcharray(nat, array)
705 nat_t *nat;
706 int *array;
707 {
708 int i, n, *x, rv, p;
709 ipfexp_t *e;
710
711 rv = 0;
712 n = array[0];
713 x = array + 1;
714
715 for (; n > 0; x += 3 + x[3], rv = 0) {
716 e = (ipfexp_t *)x;
717 if (e->ipfe_cmd == IPF_EXP_END)
718 break;
719 n -= e->ipfe_size;
720
721 p = e->ipfe_cmd >> 16;
722 if ((p != 0) && (p != nat->nat_pr[1]))
723 break;
724
725 switch (e->ipfe_cmd)
726 {
727 case IPF_EXP_IP_PR :
728 for (i = 0; !rv && i < e->ipfe_narg; i++) {
729 rv |= (nat->nat_pr[1] == e->ipfe_arg0[i]);
730 }
731 break;
732
733 case IPF_EXP_IP_SRCADDR :
734 if (nat->nat_v[0] != 4)
735 break;
736 for (i = 0; !rv && i < e->ipfe_narg; i++) {
737 rv |= ((nat->nat_osrcaddr &
738 e->ipfe_arg0[i * 2 + 1]) ==
739 e->ipfe_arg0[i * 2]) ||
740 ((nat->nat_nsrcaddr &
741 e->ipfe_arg0[i * 2 + 1]) ==
742 e->ipfe_arg0[i * 2]);
743 }
744 break;
745
746 case IPF_EXP_IP_DSTADDR :
747 if (nat->nat_v[0] != 4)
748 break;
749 for (i = 0; !rv && i < e->ipfe_narg; i++) {
750 rv |= ((nat->nat_odstaddr &
751 e->ipfe_arg0[i * 2 + 1]) ==
752 e->ipfe_arg0[i * 2]) ||
753 ((nat->nat_ndstaddr &
754 e->ipfe_arg0[i * 2 + 1]) ==
755 e->ipfe_arg0[i * 2]);
756 }
757 break;
758
759 case IPF_EXP_IP_ADDR :
760 if (nat->nat_v[0] != 4)
761 break;
762 for (i = 0; !rv && i < e->ipfe_narg; i++) {
763 rv |= ((nat->nat_osrcaddr &
764 e->ipfe_arg0[i * 2 + 1]) ==
765 e->ipfe_arg0[i * 2]) ||
766 ((nat->nat_nsrcaddr &
767 e->ipfe_arg0[i * 2 + 1]) ==
768 e->ipfe_arg0[i * 2]) ||
769 ((nat->nat_odstaddr &
770 e->ipfe_arg0[i * 2 + 1]) ==
771 e->ipfe_arg0[i * 2]) ||
772 ((nat->nat_ndstaddr &
773 e->ipfe_arg0[i * 2 + 1]) ==
774 e->ipfe_arg0[i * 2]);
775 }
776 break;
777
778 #ifdef USE_INET6
779 case IPF_EXP_IP6_SRCADDR :
780 if (nat->nat_v[0] != 6)
781 break;
782 for (i = 0; !rv && i < e->ipfe_narg; i++) {
783 rv |= IP6_MASKEQ(&nat->nat_osrc6,
784 &e->ipfe_arg0[i * 8 + 4],
785 &e->ipfe_arg0[i * 8]) ||
786 IP6_MASKEQ(&nat->nat_nsrc6,
787 &e->ipfe_arg0[i * 8 + 4],
788 &e->ipfe_arg0[i * 8]);
789 }
790 break;
791
792 case IPF_EXP_IP6_DSTADDR :
793 if (nat->nat_v[0] != 6)
794 break;
795 for (i = 0; !rv && i < e->ipfe_narg; i++) {
796 rv |= IP6_MASKEQ(&nat->nat_odst6,
797 &e->ipfe_arg0[i * 8 + 4],
798 &e->ipfe_arg0[i * 8]) ||
799 IP6_MASKEQ(&nat->nat_ndst6,
800 &e->ipfe_arg0[i * 8 + 4],
801 &e->ipfe_arg0[i * 8]);
802 }
803 break;
804
805 case IPF_EXP_IP6_ADDR :
806 if (nat->nat_v[0] != 6)
807 break;
808 for (i = 0; !rv && i < e->ipfe_narg; i++) {
809 rv |= IP6_MASKEQ(&nat->nat_osrc6,
810 &e->ipfe_arg0[i * 8 + 4],
811 &e->ipfe_arg0[i * 8]) ||
812 IP6_MASKEQ(&nat->nat_nsrc6,
813 &e->ipfe_arg0[i * 8 + 4],
814 &e->ipfe_arg0[i * 8]) ||
815 IP6_MASKEQ(&nat->nat_odst6,
816 &e->ipfe_arg0[i * 8 + 4],
817 &e->ipfe_arg0[i * 8]) ||
818 IP6_MASKEQ(&nat->nat_ndst6,
819 &e->ipfe_arg0[i * 8 + 4],
820 &e->ipfe_arg0[i * 8]);
821 }
822 break;
823 #endif
824
825 case IPF_EXP_UDP_PORT :
826 case IPF_EXP_TCP_PORT :
827 for (i = 0; !rv && i < e->ipfe_narg; i++) {
828 rv |= (nat->nat_osport == e->ipfe_arg0[i]) ||
829 (nat->nat_nsport == e->ipfe_arg0[i]) ||
830 (nat->nat_odport == e->ipfe_arg0[i]) ||
831 (nat->nat_ndport == e->ipfe_arg0[i]);
832 }
833 break;
834
835 case IPF_EXP_UDP_SPORT :
836 case IPF_EXP_TCP_SPORT :
837 for (i = 0; !rv && i < e->ipfe_narg; i++) {
838 rv |= (nat->nat_osport == e->ipfe_arg0[i]) ||
839 (nat->nat_nsport == e->ipfe_arg0[i]);
840 }
841 break;
842
843 case IPF_EXP_UDP_DPORT :
844 case IPF_EXP_TCP_DPORT :
845 for (i = 0; !rv && i < e->ipfe_narg; i++) {
846 rv |= (nat->nat_odport == e->ipfe_arg0[i]) ||
847 (nat->nat_ndport == e->ipfe_arg0[i]);
848 }
849 break;
850 }
851 rv ^= e->ipfe_not;
852
853 if (rv == 0)
854 break;
855 }
856
857 return rv;
858 }
859