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