4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
10 # ifndef __FreeBSD_cc_version
11 # include <osreldate.h>
13 # if __FreeBSD_cc_version < 430000
14 # include <osreldate.h>
23 #if !defined(__SVR4) && !defined(__GNUC__)
26 #include <sys/types.h>
27 #include <sys/param.h>
31 #include <sys/socket.h>
32 #include <sys/ioctl.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
38 #if __FreeBSD_version >= 300000
39 # include <net/if_var.h>
42 #include <arpa/nameser.h>
45 #include "netinet/ipl.h"
50 extern void yyerror __P((char *));
51 extern int yyparse __P((void));
52 extern int yylex __P((void));
57 static ipnat_t *nattop = NULL;
58 static ipnat_t *nat = NULL;
59 static int natfd = -1;
60 static ioctlfunc_t natioctlfunc = NULL;
61 static addfunc_t nataddfunc = NULL;
62 static int suggest_port = 0;
63 static proxyrule_t *prules = NULL;
64 static int parser_error = 0;
66 static void newnatrule __P((void));
67 static void setnatproto __P((int));
68 static void setmapifnames __P((void));
69 static void setrdrifnames __P((void));
70 static void proxy_setconfig __P((int));
71 static void proxy_unsetconfig __P((void));
72 static namelist_t *proxy_dns_add_pass __P((char *, char *));
73 static namelist_t *proxy_dns_add_block __P((char *, char *));
74 static void proxy_addconfig __P((char *, int, char *, namelist_t *));
75 static void proxy_loadconfig __P((int, ioctlfunc_t, char *, int,
76 char *, namelist_t *));
77 static void proxy_loadrules __P((int, ioctlfunc_t, proxyrule_t *));
78 static void setmapifnames __P((void));
79 static void setrdrifnames __P((void));
80 static void setifname __P((ipnat_t **, int, char *));
81 static int addname __P((ipnat_t **, char *));
101 int t; /* Address type */
104 int v; /* IP version */
105 int s; /* 0 = number, 1 = text */
112 %token <num> YY_NUMBER YY_HEX
115 %token YY_CMP_EQ YY_CMP_NE YY_CMP_LE YY_CMP_GE YY_CMP_LT YY_CMP_GT
116 %token YY_RANGE_OUT YY_RANGE_IN
119 %token IPNY_MAPBLOCK IPNY_RDR IPNY_PORT IPNY_PORTS IPNY_AUTO IPNY_RANGE
120 %token IPNY_MAP IPNY_BIMAP IPNY_FROM IPNY_TO IPNY_MASK IPNY_PORTMAP IPNY_ANY
121 %token IPNY_ROUNDROBIN IPNY_FRAG IPNY_AGE IPNY_ICMPIDMAP IPNY_PROXY
122 %token IPNY_TCP IPNY_UDP IPNY_TCPUDP IPNY_STICKY IPNY_MSSCLAMP IPNY_TAG
123 %token IPNY_TLATE IPNY_POOL IPNY_HASH IPNY_NO IPNY_REWRITE IPNY_PROTO
124 %token IPNY_ON IPNY_SRC IPNY_DST IPNY_IN IPNY_OUT IPNY_DIVERT
125 %token IPNY_CONFIG IPNY_ALLOW IPNY_DENY IPNY_DNS IPNY_INET IPNY_INET6
126 %token IPNY_SEQUENTIAL IPNY_DSTLIST IPNY_PURGE
127 %type <port> portspec
128 %type <num> hexnumber compare range proto
129 %type <num> saddr daddr sobject dobject mapfrom rdrfrom dip
130 %type <ipa> hostname ipv4 ipaddr
131 %type <ipp> addr rhsaddr rhdaddr erhdaddr
132 %type <pc> portstuff portpair comaports srcports dstports
133 %type <names> dnslines dnsline
142 line: xx rule { int err;
143 while ((nat = nattop) != NULL) {
144 if (nat->in_v[0] == 0)
146 if (nat->in_v[1] == 0)
147 nat->in_v[1] = nat->in_v[0];
148 nattop = nat->in_next;
149 err = (*nataddfunc)(natfd, natioctlfunc, nat);
156 if (parser_error == 0 && prules != NULL) {
157 proxy_loadrules(natfd, natioctlfunc, prules);
165 assign: YY_STR assigning YY_STR ';' { set_variable($1, $3);
174 '=' { yyvarnext = 1; }
177 xx: { newnatrule(); }
187 no: IPNY_NO { nat->in_flags |= IPN_NO; }
193 map: mapit ifnames addr tlate rhsaddr proxy mapoptions
194 { if ($3.f != 0 && $3.f != $5.f && $5.f != 0)
195 yyerror("3.address family mismatch");
196 if (nat->in_v[0] == 0 && $5.v != 0)
198 else if (nat->in_v[0] == 0 && $3.v != 0)
200 if (nat->in_v[1] == 0 && $5.v != 0)
202 else if (nat->in_v[1] == 0 && $3.v != 0)
204 nat->in_osrcatype = $3.t;
205 bcopy(&$3.a, &nat->in_osrc.na_addr[0],
207 bcopy(&$3.m, &nat->in_osrc.na_addr[1],
209 nat->in_nsrcatype = $5.t;
210 nat->in_nsrcafunc = $5.u;
211 bcopy(&$5.a, &nat->in_nsrc.na_addr[0],
213 bcopy(&$5.m, &nat->in_nsrc.na_addr[1],
218 | mapit ifnames addr tlate rhsaddr mapport mapoptions
219 { if ($3.f != $5.f && $3.f != 0 && $5.f != 0)
220 yyerror("4.address family mismatch");
221 if (nat->in_v[1] == 0 && $5.v != 0)
223 else if (nat->in_v[0] == 0 && $3.v != 0)
225 if (nat->in_v[0] == 0 && $5.v != 0)
227 else if (nat->in_v[1] == 0 && $3.v != 0)
229 nat->in_osrcatype = $3.t;
230 bcopy(&$3.a, &nat->in_osrc.na_addr[0],
232 bcopy(&$3.m, &nat->in_osrc.na_addr[1],
234 nat->in_nsrcatype = $5.t;
235 nat->in_nsrcafunc = $5.u;
236 bcopy(&$5.a, &nat->in_nsrc.na_addr[0],
238 bcopy(&$5.m, &nat->in_nsrc.na_addr[1],
243 | no mapit ifnames addr setproto ';'
244 { if (nat->in_v[0] == 0)
246 nat->in_osrcatype = $4.t;
247 bcopy(&$4.a, &nat->in_osrc.na_addr[0],
249 bcopy(&$4.m, &nat->in_osrc.na_addr[1],
254 | mapit ifnames mapfrom tlate rhsaddr proxy mapoptions
255 { if ($3 != 0 && $5.f != 0 && $3 != $5.f)
256 yyerror("5.address family mismatch");
257 if (nat->in_v[0] == 0 && $5.v != 0)
259 else if (nat->in_v[0] == 0 && $3 != 0)
260 nat->in_v[0] = ftov($3);
261 if (nat->in_v[1] == 0 && $5.v != 0)
263 else if (nat->in_v[1] == 0 && $3 != 0)
264 nat->in_v[1] = ftov($3);
265 nat->in_nsrcatype = $5.t;
266 nat->in_nsrcafunc = $5.u;
267 bcopy(&$5.a, &nat->in_nsrc.na_addr[0],
269 bcopy(&$5.m, &nat->in_nsrc.na_addr[1],
274 | no mapit ifnames mapfrom setproto ';'
275 { nat->in_v[0] = ftov($4);
278 | mapit ifnames mapfrom tlate rhsaddr mapport mapoptions
279 { if ($3 != 0 && $5.f != 0 && $3 != $5.f)
280 yyerror("6.address family mismatch");
281 if (nat->in_v[0] == 0 && $5.v != 0)
283 else if (nat->in_v[0] == 0 && $3 != 0)
284 nat->in_v[0] = ftov($3);
285 if (nat->in_v[1] == 0 && $5.v != 0)
287 else if (nat->in_v[1] == 0 && $3 != 0)
288 nat->in_v[1] = ftov($3);
289 nat->in_nsrcatype = $5.t;
290 nat->in_nsrcafunc = $5.u;
291 bcopy(&$5.a, &nat->in_nsrc.na_addr[0],
293 bcopy(&$5.m, &nat->in_nsrc.na_addr[1],
301 mapblockit ifnames addr tlate addr ports mapoptions
302 { if ($3.f != 0 && $5.f != 0 && $3.f != $5.f)
303 yyerror("7.address family mismatch");
304 if (nat->in_v[0] == 0 && $5.v != 0)
306 else if (nat->in_v[0] == 0 && $3.v != 0)
308 if (nat->in_v[1] == 0 && $5.v != 0)
310 else if (nat->in_v[1] == 0 && $3.v != 0)
312 nat->in_osrcatype = $3.t;
313 bcopy(&$3.a, &nat->in_osrc.na_addr[0],
315 bcopy(&$3.m, &nat->in_osrc.na_addr[1],
317 nat->in_nsrcatype = $5.t;
318 nat->in_nsrcafunc = $5.u;
319 bcopy(&$5.a, &nat->in_nsrc.na_addr[0],
321 bcopy(&$5.m, &nat->in_nsrc.na_addr[1],
326 | no mapblockit ifnames { yyexpectaddr = 1; } addr setproto ';'
327 { if (nat->in_v[0] == 0)
329 if (nat->in_v[1] == 0)
331 nat->in_osrcatype = $5.t;
332 bcopy(&$5.a, &nat->in_osrc.na_addr[0],
334 bcopy(&$5.m, &nat->in_osrc.na_addr[1],
341 redir: rdrit ifnames addr dport tlate dip nport setproto rdroptions
342 { if ($6 != 0 && $3.f != 0 && $6 != $3.f)
343 yyerror("21.address family mismatch");
344 if (nat->in_v[0] == 0) {
345 if ($3.v != AF_UNSPEC)
346 nat->in_v[0] = ftov($3.f);
348 nat->in_v[0] = ftov($6);
350 nat->in_odstatype = $3.t;
351 bcopy(&$3.a, &nat->in_odst.na_addr[0],
353 bcopy(&$3.m, &nat->in_odst.na_addr[1],
358 | no rdrit ifnames addr dport setproto ';'
359 { if (nat->in_v[0] == 0)
360 nat->in_v[0] = ftov($4.f);
361 nat->in_odstatype = $4.t;
362 bcopy(&$4.a, &nat->in_odst.na_addr[0],
364 bcopy(&$4.m, &nat->in_odst.na_addr[1],
369 | rdrit ifnames rdrfrom tlate dip nport setproto rdroptions
370 { if ($5 != 0 && $3 != 0 && $5 != $3)
371 yyerror("20.address family mismatch");
372 if (nat->in_v[0] == 0) {
374 nat->in_v[0] = ftov($3);
376 nat->in_v[0] = ftov($5);
380 | no rdrit ifnames rdrfrom setproto ';'
381 { nat->in_v[0] = ftov($4);
388 IPNY_REWRITE oninout rwrproto mapfrom tlate newdst newopts
389 { if (nat->in_v[0] == 0)
390 nat->in_v[0] = ftov($4);
391 if (nat->in_redir & NAT_MAP)
395 nat->in_redir |= NAT_REWRITE;
399 divert: IPNY_DIVERT oninout rwrproto mapfrom tlate divdst newopts
400 { if (nat->in_v[0] == 0)
401 nat->in_v[0] = ftov($4);
402 if (nat->in_redir & NAT_MAP) {
404 nat->in_pr[0] = IPPROTO_UDP;
407 nat->in_pr[1] = IPPROTO_UDP;
409 nat->in_flags &= ~IPN_TCP;
413 tlate: IPNY_TLATE { yyexpectaddr = 1; }
416 pconf: IPNY_PROXY { yysetdict(proxies); }
417 IPNY_DNS '/' proto IPNY_CONFIG YY_STR '{'
418 { proxy_setconfig(IPNY_DNS); }
420 { proxy_addconfig("dns", $5, $7, $10);
427 | dnslines ';' dnsline { $$ = $1; $1->na_next = $3; }
431 IPNY_ALLOW YY_STR { $$ = proxy_dns_add_pass(NULL, $2); }
432 | IPNY_DENY YY_STR { $$ = proxy_dns_add_block(NULL, $2); }
433 | IPNY_ALLOW '.' YY_STR { $$ = proxy_dns_add_pass(".", $3); }
434 | IPNY_DENY '.' YY_STR { $$ = proxy_dns_add_block(".", $3); }
438 inout IPNY_ON ifnames { ; }
441 inout: IPNY_IN { nat->in_redir = NAT_REDIRECT; }
442 | IPNY_OUT { nat->in_redir = NAT_MAP; }
446 | IPNY_PROTO setproto
449 newdst: src rhsaddr srcports dst erhdaddr dstports
450 { nat->in_nsrc.na_addr[0] = $2.a;
451 nat->in_nsrc.na_addr[1] = $2.m;
452 nat->in_nsrc.na_atype = $2.t;
453 if ($2.t == FRI_LOOKUP) {
454 nat->in_nsrc.na_type = $2.u;
455 nat->in_nsrc.na_subtype = $2.s;
456 nat->in_nsrc.na_num = $2.n;
458 nat->in_nsports[0] = $3.p1;
459 nat->in_nsports[1] = $3.p2;
460 nat->in_ndst.na_addr[0] = $5.a;
461 nat->in_ndst.na_addr[1] = $5.m;
462 nat->in_ndst.na_atype = $5.t;
463 if ($5.t == FRI_LOOKUP) {
464 nat->in_ndst.na_type = $5.u;
465 nat->in_ndst.na_subtype = $5.s;
466 nat->in_ndst.na_num = $5.n;
468 nat->in_ndports[0] = $6.p1;
469 nat->in_ndports[1] = $6.p2;
473 divdst: src addr ',' portspec dst addr ',' portspec IPNY_UDP
474 { nat->in_nsrc.na_addr[0] = $2.a;
475 if ($2.m.in4.s_addr != 0xffffffff)
476 yyerror("divert must have /32 dest");
477 nat->in_nsrc.na_addr[1] = $2.m;
478 nat->in_nsports[0] = $4;
479 nat->in_nsports[1] = $4;
481 nat->in_ndst.na_addr[0] = $6.a;
482 nat->in_ndst.na_addr[1] = $6.m;
483 if ($6.m.in4.s_addr != 0xffffffff)
484 yyerror("divert must have /32 dest");
485 nat->in_ndports[0] = $8;
486 nat->in_ndports[1] = $8;
488 nat->in_redir |= NAT_DIVERTUDP;
492 src: IPNY_SRC { yyexpectaddr = 1; }
495 dst: IPNY_DST { yyexpectaddr = 1; }
499 comaports { $$.p1 = $1.p1;
502 | IPNY_PORT '=' portspec
505 nat->in_flags |= IPN_FIXEDSPORT;
510 comaports { $$.p1 = $1.p1;
513 | IPNY_PORT '=' portspec
516 nat->in_flags |= IPN_FIXEDDPORT;
524 | ',' { if (!(nat->in_flags & IPN_TCPUDP))
525 yyerror("must be TCP/UDP for ports");
527 portpair { $$.p1 = $3.p1;
532 proxy: | IPNY_PROXY port portspec YY_STR '/' proto
534 pos = addname(&nat, $4);
535 nat->in_plabel = pos;
536 if (nat->in_dcmp == 0) {
538 } else if ($3 != nat->in_odport) {
539 yyerror("proxy port numbers not consistant");
545 | IPNY_PROXY port YY_STR YY_STR '/' proto
547 pos = addname(&nat, $4);
548 nat->in_plabel = pos;
549 pnum = getportproto($3, $6);
551 yyerror("invalid port number");
552 nat->in_odport = ntohs(pnum);
553 nat->in_ndport = ntohs(pnum);
558 | IPNY_PROXY port portspec YY_STR '/' proto IPNY_CONFIG YY_STR
560 pos = addname(&nat, $4);
561 nat->in_plabel = pos;
562 if (nat->in_dcmp == 0) {
564 } else if ($3 != nat->in_odport) {
565 yyerror("proxy port numbers not consistant");
569 nat->in_pconfig = addname(&nat, $8);
573 | IPNY_PROXY port YY_STR YY_STR '/' proto IPNY_CONFIG YY_STR
575 pos = addname(&nat, $4);
576 nat->in_plabel = pos;
577 pnum = getportproto($3, $6);
579 yyerror("invalid port number");
580 nat->in_odport = ntohs(pnum);
581 nat->in_ndport = ntohs(pnum);
583 pos = addname(&nat, $8);
584 nat->in_pconfig = pos;
591 | proto { if (nat->in_pr[0] != 0 ||
592 nat->in_pr[1] != 0 ||
593 nat->in_flags & IPN_TCPUDP)
594 yyerror("protocol set twice");
597 | IPNY_TCPUDP { if (nat->in_pr[0] != 0 ||
598 nat->in_pr[1] != 0 ||
599 nat->in_flags & IPN_TCPUDP)
600 yyerror("protocol set twice");
601 nat->in_flags |= IPN_TCPUDP;
605 | IPNY_TCP '/' IPNY_UDP { if (nat->in_pr[0] != 0 ||
606 nat->in_pr[1] != 0 ||
607 nat->in_flags & IPN_TCPUDP)
608 yyerror("protocol set twice");
609 nat->in_flags |= IPN_TCPUDP;
619 | hostname '-' { yyexpectaddr = 1; } hostname
622 yyerror("8.address family "
628 nat->in_flags |= IPN_SIPRANGE;
631 | IPNY_RANGE hostname '-' { yyexpectaddr = 1; } hostname
634 yyerror("9.address family "
640 nat->in_flags |= IPN_SIPRANGE;
646 hostname ',' { yyexpectaddr = 1; } hostname
647 { nat->in_flags |= IPN_SPLIT;
649 yyerror("10.address family "
652 nat->in_ndstip6 = $1.a;
653 nat->in_ndstmsk6 = $4.a;
654 nat->in_ndstatype = FRI_SPLIT;
657 | rhdaddr { int bits;
658 nat->in_ndstip6 = $1.a;
659 nat->in_ndstmsk6 = $1.m;
660 nat->in_ndst.na_atype = $1.t;
663 bits = count4bits($1.m.in4.s_addr);
665 bits = count6bits($1.m.i6);
666 if (($1.f == AF_INET) && (bits != 0) &&
668 yyerror("dest ip bitmask not /32");
669 } else if (($1.f == AF_INET6) &&
670 (bits != 0) && (bits != 128)) {
671 yyerror("dest ip bitmask not /128");
681 | hostname '-' hostname { bzero(&$$, sizeof($$));
683 if ($1.f != 0 && $3.f != 0 &&
685 yyerror("11.address family "
689 nat->in_flags |= IPN_DIPRANGE;
692 | IPNY_RANGE hostname '-' hostname
693 { bzero(&$$, sizeof($$));
695 if ($2.f != 0 && $4.f != 0 &&
697 yyerror("12.address family "
701 nat->in_flags |= IPN_DIPRANGE;
708 | IPNY_DSTLIST '/' YY_NUMBER { $$.t = FRI_LOOKUP;
713 | IPNY_DSTLIST '/' YY_STR { $$.t = FRI_LOOKUP;
716 $$.n = addname(&nat, $3);
720 port: IPNY_PORT { suggest_port = 1; }
724 YY_NUMBER { if ($1 > 65535) /* Unsigned */
725 yyerror("invalid port number");
729 | YY_STR { if (getport(NULL, $1,
731 yyerror("invalid port number");
737 portspec { $$.p1 = $1; $$.p2 = $1; }
738 | portspec '-' portspec { $$.p1 = $1; $$.p2 = $3; }
739 | portspec ':' portspec { $$.p1 = $1; $$.p2 = $3; }
742 dport: | port portpair { nat->in_odport = $2.p1;
744 nat->in_dtop = $2.p1;
746 nat->in_dtop = $2.p2;
750 nport: | port portpair { nat->in_dpmin = $2.p1;
751 nat->in_dpnext = $2.p1;
752 nat->in_dpmax = $2.p2;
753 nat->in_ndport = $2.p1;
754 if (nat->in_dtop == 0)
755 nat->in_dtop = $2.p2;
757 | port '=' portspec { nat->in_dpmin = $3;
760 if (nat->in_dtop == 0)
761 nat->in_dtop = nat->in_odport;
762 nat->in_flags |= IPN_FIXEDDPORT;
766 ports: | IPNY_PORTS YY_NUMBER { nat->in_spmin = $2; }
767 | IPNY_PORTS IPNY_AUTO { nat->in_flags |= IPN_AUTOPORTMAP; }
770 mapit: IPNY_MAP { nat->in_redir = NAT_MAP; }
771 | IPNY_BIMAP { nat->in_redir = NAT_BIMAP; }
774 rdrit: IPNY_RDR { nat->in_redir = NAT_REDIRECT; }
778 IPNY_MAPBLOCK { nat->in_redir = NAT_MAPBLK; }
782 from sobject to dobject { if ($2 != 0 && $4 != 0 && $2 != $4)
783 yyerror("13.address family "
787 | from sobject '!' to dobject
788 { if ($2 != 0 && $5 != 0 && $2 != $5)
789 yyerror("14.address family "
791 nat->in_flags |= IPN_NOTDST;
794 | from sobject to '!' dobject
795 { if ($2 != 0 && $5 != 0 && $2 != $5)
796 yyerror("15.address family "
798 nat->in_flags |= IPN_NOTDST;
804 from sobject to dobject { if ($2 != 0 && $4 != 0 && $2 != $4)
805 yyerror("16.address family "
809 | '!' from sobject to dobject
810 { if ($3 != 0 && $5 != 0 && $3 != $5)
811 yyerror("17.address family "
813 nat->in_flags |= IPN_NOTSRC;
816 | from '!' sobject to dobject
817 { if ($3 != 0 && $5 != 0 && $3 != $5)
818 yyerror("18.address family "
820 nat->in_flags |= IPN_NOTSRC;
825 from: IPNY_FROM { nat->in_flags |= IPN_FILTER;
830 to: IPNY_TO { yyexpectaddr = 1; }
834 ifname family { yyexpectaddr = 1; }
835 | ifname ',' otherifname family { yyexpectaddr = 1; }
838 ifname: YY_STR { setifname(&nat, 0, $1);
843 family: | IPNY_INET { nat->in_v[0] = 4; nat->in_v[1] = 4; }
844 | IPNY_INET6 { nat->in_v[0] = 6; nat->in_v[1] = 6; }
848 YY_STR { setifname(&nat, 1, $1);
854 IPNY_PORTMAP tcpudp portpair sequential
855 { nat->in_spmin = $3.p1;
856 nat->in_spmax = $3.p2;
858 | IPNY_PORTMAP portpair tcpudp sequential
859 { nat->in_spmin = $2.p1;
860 nat->in_spmax = $2.p2;
862 | IPNY_PORTMAP tcpudp IPNY_AUTO sequential
863 { nat->in_flags |= IPN_AUTOPORTMAP;
864 nat->in_spmin = 1024;
865 nat->in_spmax = 65535;
867 | IPNY_ICMPIDMAP YY_STR portpair sequential
868 { if (strcmp($2, "icmp") != 0 &&
869 strcmp($2, "ipv6-icmp") != 0) {
870 yyerror("icmpidmap not followed by icmp");
873 if ($3.p1 < 0 || $3.p1 > 65535)
874 yyerror("invalid ICMP Id number");
875 if ($3.p2 < 0 || $3.p2 > 65535)
876 yyerror("invalid ICMP Id number");
877 if (strcmp($2, "ipv6-icmp") == 0) {
878 nat->in_pr[0] = IPPROTO_ICMPV6;
879 nat->in_pr[1] = IPPROTO_ICMPV6;
881 nat->in_pr[0] = IPPROTO_ICMP;
882 nat->in_pr[1] = IPPROTO_ICMP;
884 nat->in_flags = IPN_ICMPQUERY;
885 nat->in_spmin = $3.p1;
886 nat->in_spmax = $3.p2;
892 | saddr port portstuff { nat->in_osport = $3.p1;
893 nat->in_stop = $3.p2;
894 nat->in_scmp = $3.pc;
899 saddr: addr { nat->in_osrcatype = $1.t;
901 &nat->in_osrc.na_addr[0],
904 &nat->in_osrc.na_addr[1],
912 | daddr port portstuff { nat->in_odport = $3.p1;
913 nat->in_dtop = $3.p2;
914 nat->in_dcmp = $3.pc;
919 daddr: addr { nat->in_odstatype = $1.t;
921 &nat->in_odst.na_addr[0],
924 &nat->in_odst.na_addr[1],
930 addr: IPNY_ANY { yyexpectaddr = 0;
931 bzero(&$$, sizeof($$));
934 | hostname { bzero(&$$, sizeof($$));
939 if ($$.f == AF_INET) {
940 $$.m.in4.s_addr = 0xffffffff;
941 } else if ($$.f == AF_INET6) {
942 $$.m.i6[0] = 0xffffffff;
943 $$.m.i6[1] = 0xffffffff;
944 $$.m.i6[2] = 0xffffffff;
945 $$.m.i6[3] = 0xffffffff;
949 | hostname slash YY_NUMBER
950 { bzero(&$$, sizeof($$));
955 ntomask($$.f, $3, (u_32_t *)&$$.m);
956 $$.a.i6[0] &= $$.m.i6[0];
957 $$.a.i6[1] &= $$.m.i6[1];
958 $$.a.i6[2] &= $$.m.i6[2];
959 $$.a.i6[3] &= $$.m.i6[3];
962 | hostname slash ipaddr { bzero(&$$, sizeof($$));
964 yyerror("1.address family "
970 $$.a.i6[0] &= $$.m.i6[0];
971 $$.a.i6[1] &= $$.m.i6[1];
972 $$.a.i6[2] &= $$.m.i6[2];
973 $$.a.i6[3] &= $$.m.i6[3];
978 | hostname slash hexnumber { bzero(&$$, sizeof($$));
980 $$.m.in4.s_addr = htonl($3);
982 $$.a.in4.s_addr &= $$.m.in4.s_addr;
985 if ($$.f == AF_INET6)
986 yyerror("incorrect inet6 mask");
988 | hostname mask ipaddr { bzero(&$$, sizeof($$));
990 yyerror("2.address family "
996 $$.a.i6[0] &= $$.m.i6[0];
997 $$.a.i6[1] &= $$.m.i6[1];
998 $$.a.i6[2] &= $$.m.i6[2];
999 $$.a.i6[3] &= $$.m.i6[3];
1004 | hostname mask hexnumber { bzero(&$$, sizeof($$));
1006 $$.m.in4.s_addr = htonl($3);
1008 $$.a.in4.s_addr &= $$.m.in4.s_addr;
1012 | pool slash YY_NUMBER { bzero(&$$, sizeof($$));
1013 $$.a.iplookupnum = $3;
1014 $$.a.iplookuptype = IPLT_POOL;
1015 $$.a.iplookupsubtype = 0;
1018 | pool slash YY_STR { bzero(&$$, sizeof($$));
1019 $$.a.iplookupname = addname(&nat,$3);
1020 $$.a.iplookuptype = IPLT_POOL;
1021 $$.a.iplookupsubtype = 1;
1024 | hash slash YY_NUMBER { bzero(&$$, sizeof($$));
1025 $$.a.iplookupnum = $3;
1026 $$.a.iplookuptype = IPLT_HASH;
1027 $$.a.iplookupsubtype = 0;
1030 | hash slash YY_STR { bzero(&$$, sizeof($$));
1031 $$.a.iplookupname = addname(&nat,$3);
1032 $$.a.iplookuptype = IPLT_HASH;
1033 $$.a.iplookupsubtype = 1;
1038 slash: '/' { yyexpectaddr = 0; }
1041 mask: IPNY_MASK { yyexpectaddr = 0; }
1044 pool: IPNY_POOL { if (!(nat->in_flags & IPN_FILTER)) {
1045 yyerror("Can only use pool with from/to rules\n");
1052 hash: IPNY_HASH { if (!(nat->in_flags & IPN_FILTER)) {
1053 yyerror("Can only use hash with from/to rules\n");
1061 compare portspec { $$.pc = $1; $$.p1 = $2; }
1062 | portspec range portspec { $$.pc = $2; $$.p1 = $1; $$.p2 = $3; }
1066 rr frag age mssclamp nattag setproto purge
1070 rr frag age sticky mssclamp rdrproxy nattag purge
1073 nattag: | IPNY_TAG YY_STR { strncpy(nat->in_tag.ipt_tag, $2,
1074 sizeof(nat->in_tag.ipt_tag));
1076 rr: | IPNY_ROUNDROBIN { nat->in_flags |= IPN_ROUNDR; }
1079 frag: | IPNY_FRAG { nat->in_flags |= IPN_FRAG; }
1082 age: | IPNY_AGE YY_NUMBER { nat->in_age[0] = $2;
1083 nat->in_age[1] = $2; }
1084 | IPNY_AGE YY_NUMBER '/' YY_NUMBER { nat->in_age[0] = $2;
1085 nat->in_age[1] = $4; }
1088 sticky: | IPNY_STICKY { if (!(nat->in_flags & IPN_ROUNDR) &&
1089 !(nat->in_flags & IPN_SPLIT)) {
1091 "'sticky' for use with round-robin/IP splitting only\n");
1093 nat->in_flags |= IPN_STICKY;
1098 | IPNY_MSSCLAMP YY_NUMBER { nat->in_mssclamp = $2; }
1101 tcpudp: IPNY_TCP { setnatproto(IPPROTO_TCP); }
1102 | IPNY_UDP { setnatproto(IPPROTO_UDP); }
1103 | IPNY_TCPUDP { nat->in_flags |= IPN_TCPUDP;
1107 | IPNY_TCP '/' IPNY_UDP { nat->in_flags |= IPN_TCPUDP;
1114 | IPNY_SEQUENTIAL { nat->in_flags |= IPN_SEQUENTIAL; }
1118 | IPNY_PURGE { nat->in_flags |= IPN_PURGE; }
1124 pos = addname(&nat, $2);
1125 nat->in_plabel = pos;
1126 nat->in_odport = nat->in_dpnext;
1127 nat->in_dtop = nat->in_odport;
1130 | proxy { if (nat->in_plabel != -1) {
1131 nat->in_ndport = nat->in_odport;
1132 nat->in_dpmin = nat->in_odport;
1133 nat->in_dpmax = nat->in_dpmin;
1134 nat->in_dtop = nat->in_dpmin;
1135 nat->in_dpnext = nat->in_dpmin;
1141 | IPNY_PURGE { nat->in_flags |= IPN_PURGE; }
1144 proto: YY_NUMBER { $$ = $1;
1145 if ($$ != IPPROTO_TCP &&
1149 | IPNY_TCP { $$ = IPPROTO_TCP; }
1150 | IPNY_UDP { $$ = IPPROTO_UDP; }
1151 | YY_STR { $$ = getproto($1);
1154 yyerror("unknwon protocol");
1155 if ($$ != IPPROTO_TCP &&
1166 YY_STR { i6addr_t addr;
1168 bzero(&$$, sizeof($$));
1169 if (gethost(AF_INET, $1,
1174 if (gethost(AF_INET6, $1,
1180 "Unknown host '%s'\n",
1185 | YY_NUMBER { bzero(&$$, sizeof($$));
1186 $$.a.in4.s_addr = htonl($1);
1187 if ($$.a.in4.s_addr != 0)
1191 | YY_IPV6 { bzero(&$$, sizeof($$));
1195 | YY_NUMBER YY_IPV6 { bzero(&$$, sizeof($$));
1202 '=' { $$ = FR_EQUAL; }
1203 | YY_CMP_EQ { $$ = FR_EQUAL; }
1204 | YY_CMP_NE { $$ = FR_NEQUAL; }
1205 | YY_CMP_LT { $$ = FR_LESST; }
1206 | YY_CMP_LE { $$ = FR_LESSTE; }
1207 | YY_CMP_GT { $$ = FR_GREATERT; }
1208 | YY_CMP_GE { $$ = FR_GREATERTE; }
1211 YY_RANGE_OUT { $$ = FR_OUTRANGE; }
1212 | YY_RANGE_IN { $$ = FR_INRANGE; }
1213 | ':' { $$ = FR_INCRANGE; }
1216 ipaddr: ipv4 { $$ = $1; }
1217 | YY_IPV6 { $$.a = $1;
1222 ipv4: YY_NUMBER '.' YY_NUMBER '.' YY_NUMBER '.' YY_NUMBER
1223 { if ($1 > 255 || $3 > 255 || $5 > 255 || $7 > 255) {
1224 yyerror("Invalid octet string for IP address");
1227 bzero((char *)&$$, sizeof($$));
1228 $$.a.in4.s_addr = ($1 << 24) | ($3 << 16) | ($5 << 8) | $7;
1229 $$.a.in4.s_addr = htonl($$.a.in4.s_addr);
1237 static wordtab_t proxies[] = {
1241 static wordtab_t dnswords[] = {
1242 { "allow", IPNY_ALLOW },
1243 { "block", IPNY_DENY },
1244 { "deny", IPNY_DENY },
1245 { "drop", IPNY_DENY },
1246 { "pass", IPNY_ALLOW },
1250 static wordtab_t yywords[] = {
1251 { "age", IPNY_AGE },
1252 { "any", IPNY_ANY },
1253 { "auto", IPNY_AUTO },
1254 { "bimap", IPNY_BIMAP },
1255 { "config", IPNY_CONFIG },
1256 { "divert", IPNY_DIVERT },
1257 { "dst", IPNY_DST },
1258 { "dstlist", IPNY_DSTLIST },
1259 { "frag", IPNY_FRAG },
1260 { "from", IPNY_FROM },
1261 { "hash", IPNY_HASH },
1262 { "icmpidmap", IPNY_ICMPIDMAP },
1264 { "inet", IPNY_INET },
1265 { "inet6", IPNY_INET6 },
1266 { "mask", IPNY_MASK },
1267 { "map", IPNY_MAP },
1268 { "map-block", IPNY_MAPBLOCK },
1269 { "mssclamp", IPNY_MSSCLAMP },
1270 { "netmask", IPNY_MASK },
1273 { "out", IPNY_OUT },
1274 { "pool", IPNY_POOL },
1275 { "port", IPNY_PORT },
1276 { "portmap", IPNY_PORTMAP },
1277 { "ports", IPNY_PORTS },
1278 { "proto", IPNY_PROTO },
1279 { "proxy", IPNY_PROXY },
1280 { "purge", IPNY_PURGE },
1281 { "range", IPNY_RANGE },
1282 { "rewrite", IPNY_REWRITE },
1283 { "rdr", IPNY_RDR },
1284 { "round-robin",IPNY_ROUNDROBIN },
1285 { "sequential", IPNY_SEQUENTIAL },
1286 { "src", IPNY_SRC },
1287 { "sticky", IPNY_STICKY },
1288 { "tag", IPNY_TAG },
1289 { "tcp", IPNY_TCP },
1290 { "tcpudp", IPNY_TCPUDP },
1292 { "udp", IPNY_UDP },
1294 { "->", IPNY_TLATE },
1295 { "eq", YY_CMP_EQ },
1296 { "ne", YY_CMP_NE },
1297 { "lt", YY_CMP_LT },
1298 { "gt", YY_CMP_GT },
1299 { "le", YY_CMP_LE },
1300 { "ge", YY_CMP_GE },
1306 ipnat_parsefile(fd, addfunc, ioctlfunc, filename)
1309 ioctlfunc_t ioctlfunc;
1318 (void) yysettab(yywords);
1320 s = getenv("YYDEBUG");
1326 if (strcmp(filename, "-")) {
1327 fp = fopen(filename, "r");
1329 FPRINTF(stderr, "fopen(%s) failed: %s\n", filename,
1336 while ((rval = ipnat_parsesome(fd, addfunc, ioctlfunc, fp)) == 0)
1349 ipnat_parsesome(fd, addfunc, ioctlfunc, fp)
1352 ioctlfunc_t ioctlfunc;
1360 nataddfunc = addfunc;
1361 natioctlfunc = ioctlfunc;
1368 if (ungetc(i, fp) == EOF)
1372 s = getenv("YYDEBUG");
1380 return parser_error;
1389 n = calloc(1, sizeof(*n));
1395 n->in_pnext = &nattop;
1398 n->in_pnext = &nat->in_next;
1402 n->in_flineno = yylineNum;
1403 n->in_ifnames[0] = -1;
1404 n->in_ifnames[1] = -1;
1407 n->in_size = sizeof(*n);
1423 nat->in_flags |= IPN_TCP;
1424 nat->in_flags &= ~IPN_UDP;
1427 nat->in_flags |= IPN_UDP;
1428 nat->in_flags &= ~IPN_TCP;
1431 nat->in_flags &= ~IPN_TCPUDP;
1432 if (!(nat->in_flags & IPN_ICMPQUERY) &&
1433 !(nat->in_redir & NAT_DIVERTUDP)) {
1445 if ((nat->in_redir & NAT_MAPBLK) == 0) {
1446 nat->in_flags &= ~IPN_TCPUDP;
1459 if ((nat->in_flags & (IPN_TCP|IPN_UDP)) == 0) {
1469 if ((nat->in_flags & (IPN_TCPUDP|IPN_FIXEDDPORT)) == IPN_FIXEDDPORT)
1470 nat->in_flags &= ~IPN_FIXEDDPORT;
1475 ipnat_addrule(fd, ioctlfunc, ptr)
1477 ioctlfunc_t ioctlfunc;
1480 ioctlcmd_t add, del;
1485 bzero((char *)&obj, sizeof(obj));
1486 obj.ipfo_rev = IPFILTER_VERSION;
1487 obj.ipfo_size = ipn->in_size;
1488 obj.ipfo_type = IPFOBJ_IPNAT;
1491 if ((opts & OPT_DONOTHING) != 0)
1494 if (opts & OPT_ZERORULEST) {
1497 } else if (opts & OPT_PURGE) {
1505 if ((opts & OPT_VERBOSE) != 0)
1506 printnat(ipn, opts);
1508 if (opts & OPT_DEBUG)
1509 binprint(ipn, sizeof(*ipn));
1511 if ((opts & OPT_ZERORULEST) != 0) {
1512 if ((*ioctlfunc)(fd, add, (void *)&obj) == -1) {
1513 if ((opts & OPT_DONOTHING) == 0) {
1516 sprintf(msg, "%d:ioctl(zero nat rule)",
1518 return ipf_perror_fd(fd, ioctlfunc, msg);
1521 PRINTF("hits %lu ", ipn->in_hits);
1523 PRINTF("bytes %"PRIu64" ",
1524 ipn->in_bytes[0] + ipn->in_bytes[1]);
1526 PRINTF("bytes %lu ",
1527 ipn->in_bytes[0] + ipn->in_bytes[1]);
1529 printnat(ipn, opts);
1531 } else if ((opts & OPT_REMOVE) != 0) {
1532 if ((*ioctlfunc)(fd, del, (void *)&obj) == -1) {
1533 if ((opts & OPT_DONOTHING) == 0) {
1536 sprintf(msg, "%d:ioctl(delete nat rule)",
1538 return ipf_perror_fd(fd, ioctlfunc, msg);
1542 if ((*ioctlfunc)(fd, add, (void *)&obj) == -1) {
1543 if ((opts & OPT_DONOTHING) == 0) {
1546 sprintf(msg, "%d:ioctl(add/insert nat rule)",
1548 if (errno == EEXIST) {
1549 sprintf(msg + strlen(msg), "(line %d)",
1552 return ipf_perror_fd(fd, ioctlfunc, msg);
1563 if (nat->in_ifnames[1] == -1)
1564 nat->in_ifnames[1] = nat->in_ifnames[0];
1566 if ((suggest_port == 1) && (nat->in_flags & IPN_TCPUDP) == 0)
1567 nat->in_flags |= IPN_TCPUDP;
1569 if ((nat->in_flags & IPN_TCPUDP) == 0)
1570 setnatproto(nat->in_pr[1]);
1572 if (((nat->in_redir & NAT_MAPBLK) != 0) ||
1573 ((nat->in_flags & IPN_AUTOPORTMAP) != 0))
1574 nat_setgroupmap(nat);
1581 if ((suggest_port == 1) && (nat->in_flags & IPN_TCPUDP) == 0)
1582 nat->in_flags |= IPN_TCPUDP;
1584 if ((nat->in_pr[0] == 0) && ((nat->in_flags & IPN_TCPUDP) == 0) &&
1585 (nat->in_dpmin != 0 || nat->in_dpmax != 0 || nat->in_dpnext != 0))
1586 setnatproto(IPPROTO_TCP);
1588 if (nat->in_ifnames[1] == -1)
1589 nat->in_ifnames[1] = nat->in_ifnames[0];
1594 proxy_setconfig(proxy)
1597 if (proxy == IPNY_DNS) {
1598 yysetfixeddict(dnswords);
1611 proxy_dns_add_pass(prefix, name)
1612 char *prefix, *name;
1616 n = calloc(1, sizeof(*n));
1618 if (prefix == NULL || *prefix == '\0') {
1619 n->na_name = strdup(name);
1621 n->na_name = malloc(strlen(name) + strlen(prefix) + 1);
1622 strcpy(n->na_name, prefix);
1623 strcat(n->na_name, name);
1631 proxy_dns_add_block(prefix, name)
1632 char *prefix, *name;
1636 n = calloc(1, sizeof(*n));
1638 if (prefix == NULL || *prefix == '\0') {
1639 n->na_name = strdup(name);
1641 n->na_name = malloc(strlen(name) + strlen(prefix) + 1);
1642 strcpy(n->na_name, prefix);
1643 strcat(n->na_name, name);
1652 proxy_addconfig(proxy, proto, conf, list)
1659 pr = calloc(1, sizeof(*pr));
1661 pr->pr_proto = proto;
1662 pr->pr_proxy = proxy;
1664 pr->pr_names = list;
1665 pr->pr_next = prules;
1672 proxy_loadrules(fd, ioctlfunc, rules)
1674 ioctlfunc_t ioctlfunc;
1679 while ((pr = rules) != NULL) {
1680 proxy_loadconfig(fd, ioctlfunc, pr->pr_proxy, pr->pr_proto,
1681 pr->pr_conf, pr->pr_names);
1682 rules = pr->pr_next;
1690 proxy_loadconfig(fd, ioctlfunc, proxy, proto, conf, list)
1692 ioctlfunc_t ioctlfunc;
1701 obj.ipfo_rev = IPFILTER_VERSION;
1702 obj.ipfo_type = IPFOBJ_PROXYCTL;
1703 obj.ipfo_size = sizeof(pcmd);
1704 obj.ipfo_ptr = &pcmd;
1706 while ((na = list) != NULL) {
1707 if ((opts & OPT_REMOVE) != 0)
1708 pcmd.apc_cmd = APC_CMD_DEL;
1710 pcmd.apc_cmd = APC_CMD_ADD;
1711 pcmd.apc_dsize = strlen(na->na_name) + 1;
1712 pcmd.apc_data = na->na_name;
1713 pcmd.apc_arg = na->na_value;
1716 strncpy(pcmd.apc_label, proxy, APR_LABELLEN);
1717 pcmd.apc_label[APR_LABELLEN - 1] = '\0';
1719 strncpy(pcmd.apc_config, conf, APR_LABELLEN);
1720 pcmd.apc_config[APR_LABELLEN - 1] = '\0';
1722 if ((*ioctlfunc)(fd, SIOCPROXY, (void *)&obj) == -1) {
1723 if ((opts & OPT_DONOTHING) == 0) {
1726 sprintf(msg, "%d:ioctl(add/remove proxy rule)",
1728 ipf_perror_fd(fd, ioctlfunc, msg);
1741 setifname(np, idx, name)
1748 pos = addname(np, name);
1751 (*np)->in_ifnames[idx] = pos;
1764 nlen = strlen(name) + 1;
1765 n = realloc(*np, (*np)->in_size + nlen);
1771 if (n->in_pnext != NULL)
1774 pos = n->in_namelen;
1775 n->in_namelen += nlen;
1776 strcpy(n->in_names + pos, name);
1777 n->in_names[n->in_namelen] = '\0';