xref: /freebsd/sbin/ipf/libipf/printipfexpr.c (revision 61e21613)
1 #include "ipf.h"
2 
3 static void printport(int *);
4 static void printhosts(int *);
5 static void printsingle(int *);
6 #ifdef USE_INET6
7 static void printhostsv6(int *);
8 #endif
9 
10 void
11 printipfexpr(int *array)
12 {
13 	int i, nelems, j, not;
14 	ipfexp_t *ipfe;
15 
16 	nelems = array[0];
17 
18 	for (i = 1; i < nelems; ) {
19 		ipfe = (ipfexp_t *)(array + i);
20 		if (ipfe->ipfe_cmd == IPF_EXP_END)
21 			break;
22 
23 		not = ipfe->ipfe_not;
24 
25 		switch (ipfe->ipfe_cmd)
26 		{
27 		case IPF_EXP_IP_ADDR :
28 			PRINTF("ip.addr %s= ", not ? "!" : "");
29 			printhosts(array + i);
30 			break;
31 
32 		case IPF_EXP_IP_PR :
33 			PRINTF("ip.p %s= ", not ? "!" : "");
34 			printsingle(array + i);
35 			break;
36 
37 		case IPF_EXP_IP_SRCADDR :
38 			PRINTF("ip.src %s= ", not ? "!" : "");
39 			printhosts(array + i);
40 			break;
41 
42 		case IPF_EXP_IP_DSTADDR :
43 			PRINTF("ip.dst %s= ", not ? "!" : "");
44 			printhosts(array + i);
45 			break;
46 
47 		case IPF_EXP_TCP_PORT :
48 			PRINTF("tcp.port %s= ", not ? "!" : "");
49 			printport(array + i);
50 			break;
51 
52 		case IPF_EXP_TCP_DPORT :
53 			PRINTF("tcp.dport %s= ", not ? "!" : "");
54 			printport(array + i);
55 			break;
56 
57 		case IPF_EXP_TCP_SPORT :
58 			PRINTF("tcp.sport %s= ", not ? "!" : "");
59 			printport(array + i);
60 			break;
61 
62 		case IPF_EXP_TCP_FLAGS :
63 			PRINTF("tcp.flags %s= ", not ? "!" : "");
64 
65 			for (j = 0; j < ipfe->ipfe_narg; ) {
66 				printtcpflags(array[i + 4], array[i + 5]);
67 				j += 2;
68 				if (j < array[4])
69 					putchar(',');
70 			}
71 			break;
72 
73 		case IPF_EXP_UDP_PORT :
74 			PRINTF("udp.port %s= ", not ? "!" : "");
75 			printport(array + i);
76 			break;
77 
78 		case IPF_EXP_UDP_DPORT :
79 			PRINTF("udp.dport %s= ", not ? "!" : "");
80 			printport(array + i);
81 			break;
82 
83 		case IPF_EXP_UDP_SPORT :
84 			PRINTF("udp.sport %s= ", not ? "!" : "");
85 			printport(array + i);
86 			break;
87 
88 		case IPF_EXP_IDLE_GT :
89 			PRINTF("idle-gt %s= ", not ? "!" : "");
90 			printsingle(array + i);
91 			break;
92 
93 		case IPF_EXP_TCP_STATE :
94 			PRINTF("tcp-state %s= ", not ? "!" : "");
95 			printsingle(array + i);
96 			break;
97 
98 #ifdef USE_INET6
99 		case IPF_EXP_IP6_ADDR :
100 			PRINTF("ip6.addr %s= ", not ? "!" : "");
101 			printhostsv6(array + i);
102 			break;
103 
104 		case IPF_EXP_IP6_SRCADDR :
105 			PRINTF("ip6.src %s= ", not ? "!" : "");
106 			printhostsv6(array + i);
107 			break;
108 
109 		case IPF_EXP_IP6_DSTADDR :
110 			PRINTF("ip6.dst %s= ", not ? "!" : "");
111 			printhostsv6(array + i);
112 			break;
113 #endif
114 
115 		case IPF_EXP_END :
116 			break;
117 
118 		default :
119 			PRINTF("#%#x,len=%d;",
120 			       ipfe->ipfe_cmd, ipfe->ipfe_narg);
121 		}
122 
123 		if (array[i] != IPF_EXP_END)
124 			putchar(';');
125 
126 		i += ipfe->ipfe_size;
127 		if (array[i] != IPF_EXP_END)
128 			putchar(' ');
129 	}
130 }
131 
132 
133 static void
134 printsingle(int *array)
135 {
136 	ipfexp_t *ipfe = (ipfexp_t *)array;
137 	int i;
138 
139 	for (i = 0; i < ipfe->ipfe_narg; ) {
140 		PRINTF("%d", array[i + 4]);
141 		i++;
142 		if (i < ipfe->ipfe_narg)
143 			putchar(',');
144 	}
145 }
146 
147 
148 static void
149 printport(int *array)
150 {
151 	ipfexp_t *ipfe = (ipfexp_t *)array;
152 	int i;
153 
154 	for (i = 0; i < ipfe->ipfe_narg; ) {
155 		PRINTF("%d", ntohs(array[i + 4]));
156 		i++;
157 		if (i < ipfe->ipfe_narg)
158 			putchar(',');
159 	}
160 }
161 
162 
163 static void
164 printhosts(int *array)
165 {
166 	ipfexp_t *ipfe = (ipfexp_t *)array;
167 	int i, j;
168 
169 	for (i = 0, j = 0; i < ipfe->ipfe_narg; j++) {
170 		printhostmask(AF_INET, (u_32_t *)ipfe->ipfe_arg0 + j * 2,
171 			      (u_32_t *)ipfe->ipfe_arg0 + j * 2 + 1);
172 		i += 2;
173 		if (i < ipfe->ipfe_narg)
174 			putchar(',');
175 	}
176 }
177 
178 
179 #ifdef USE_INET6
180 static void
181 printhostsv6(int *array)
182 {
183 	ipfexp_t *ipfe = (ipfexp_t *)array;
184 	int i, j;
185 
186 	for (i = 4, j= 0; i < ipfe->ipfe_size; j++) {
187 		printhostmask(AF_INET6, (u_32_t *)ipfe->ipfe_arg0 + j * 8,
188 			      (u_32_t *)ipfe->ipfe_arg0 + j * 8 + 4);
189 		i += 8;
190 		if (i < ipfe->ipfe_size)
191 			putchar(',');
192 	}
193 }
194 #endif
195