ip_dns_pxy.c revision 1.2.2.2 1 1.2.2.2 joerg /* $NetBSD: ip_dns_pxy.c,v 1.2.2.2 2012/04/17 19:25:18 joerg Exp $ */
2 1.2.2.2 joerg
3 1.2.2.2 joerg /*
4 1.2.2.2 joerg * Copyright (C) 2010 by Darren Reed.
5 1.2.2.2 joerg *
6 1.2.2.2 joerg * See the IPFILTER.LICENCE file for details on licencing.
7 1.2.2.2 joerg *
8 1.2.2.2 joerg * Id: ip_dns_pxy.c,v 2.2.2.6 2012/01/29 05:30:35 darrenr Exp
9 1.2.2.2 joerg */
10 1.2.2.2 joerg
11 1.2.2.2 joerg #define IPF_DNS_PROXY
12 1.2.2.2 joerg
13 1.2.2.2 joerg /*
14 1.2.2.2 joerg * map ... proxy port dns/udp 53 { block .cnn.com; }
15 1.2.2.2 joerg */
16 1.2.2.2 joerg typedef struct ipf_dns_filter {
17 1.2.2.2 joerg struct ipf_dns_filter *idns_next;
18 1.2.2.2 joerg char *idns_name;
19 1.2.2.2 joerg int idns_namelen;
20 1.2.2.2 joerg int idns_pass;
21 1.2.2.2 joerg } ipf_dns_filter_t;
22 1.2.2.2 joerg
23 1.2.2.2 joerg
24 1.2.2.2 joerg typedef struct ipf_dns_softc_s {
25 1.2.2.2 joerg ipf_dns_filter_t *ipf_p_dns_list;
26 1.2.2.2 joerg ipfrwlock_t ipf_p_dns_rwlock;
27 1.2.2.2 joerg u_long ipf_p_dns_compress;
28 1.2.2.2 joerg u_long ipf_p_dns_toolong;
29 1.2.2.2 joerg u_long ipf_p_dns_nospace;
30 1.2.2.2 joerg } ipf_dns_softc_t;
31 1.2.2.2 joerg
32 1.2.2.2 joerg int ipf_p_dns_allow_query(ipf_dns_softc_t *, dnsinfo_t *);
33 1.2.2.2 joerg int ipf_p_dns_ctl(ipf_main_softc_t *, void *, ap_ctl_t *);
34 1.2.2.2 joerg int ipf_p_dns_del(ipf_main_softc_t *, ap_session_t *);
35 1.2.2.2 joerg int ipf_p_dns_get_name(ipf_dns_softc_t *, char *, int, char *, int);
36 1.2.2.2 joerg int ipf_p_dns_inout(void *, fr_info_t *, ap_session_t *, nat_t *);
37 1.2.2.2 joerg int ipf_p_dns_match(fr_info_t *, ap_session_t *, nat_t *);
38 1.2.2.2 joerg int ipf_p_dns_match_names(ipf_dns_filter_t *, char *, int);
39 1.2.2.2 joerg int ipf_p_dns_new(void *, fr_info_t *, ap_session_t *, nat_t *);
40 1.2.2.2 joerg void *ipf_p_dns_soft_create(ipf_main_softc_t *);
41 1.2.2.2 joerg void ipf_p_dns_soft_destroy(ipf_main_softc_t *, void *);
42 1.2.2.2 joerg
43 1.2.2.2 joerg typedef struct {
44 1.2.2.2 joerg u_char dns_id[2];
45 1.2.2.2 joerg u_short dns_ctlword;
46 1.2.2.2 joerg u_short dns_qdcount;
47 1.2.2.2 joerg u_short dns_ancount;
48 1.2.2.2 joerg u_short dns_nscount;
49 1.2.2.2 joerg u_short dns_arcount;
50 1.2.2.2 joerg } ipf_dns_hdr_t;
51 1.2.2.2 joerg
52 1.2.2.2 joerg #define DNS_QR(x) ((ntohs(x) & 0x8000) >> 15)
53 1.2.2.2 joerg #define DNS_OPCODE(x) ((ntohs(x) & 0x7800) >> 11)
54 1.2.2.2 joerg #define DNS_AA(x) ((ntohs(x) & 0x0400) >> 10)
55 1.2.2.2 joerg #define DNS_TC(x) ((ntohs(x) & 0x0200) >> 9)
56 1.2.2.2 joerg #define DNS_RD(x) ((ntohs(x) & 0x0100) >> 8)
57 1.2.2.2 joerg #define DNS_RA(x) ((ntohs(x) & 0x0080) >> 7)
58 1.2.2.2 joerg #define DNS_Z(x) ((ntohs(x) & 0x0070) >> 4)
59 1.2.2.2 joerg #define DNS_RCODE(x) ((ntohs(x) & 0x000f) >> 0)
60 1.2.2.2 joerg
61 1.2.2.2 joerg
62 1.2.2.2 joerg void *
63 1.2.2.2 joerg ipf_p_dns_soft_create(ipf_main_softc_t *softc)
64 1.2.2.2 joerg {
65 1.2.2.2 joerg ipf_dns_softc_t *softd;
66 1.2.2.2 joerg
67 1.2.2.2 joerg KMALLOC(softd, ipf_dns_softc_t *);
68 1.2.2.2 joerg if (softd == NULL)
69 1.2.2.2 joerg return NULL;
70 1.2.2.2 joerg
71 1.2.2.2 joerg bzero((char *)softd, sizeof(*softd));
72 1.2.2.2 joerg RWLOCK_INIT(&softd->ipf_p_dns_rwlock, "ipf dns rwlock");
73 1.2.2.2 joerg
74 1.2.2.2 joerg return softd;
75 1.2.2.2 joerg }
76 1.2.2.2 joerg
77 1.2.2.2 joerg
78 1.2.2.2 joerg void
79 1.2.2.2 joerg ipf_p_dns_soft_destroy(ipf_main_softc_t *softc, void *arg)
80 1.2.2.2 joerg {
81 1.2.2.2 joerg ipf_dns_softc_t *softd = arg;
82 1.2.2.2 joerg ipf_dns_filter_t *idns;
83 1.2.2.2 joerg
84 1.2.2.2 joerg while ((idns = softd->ipf_p_dns_list) != NULL) {
85 1.2.2.2 joerg KFREES(idns->idns_name, idns->idns_namelen);
86 1.2.2.2 joerg idns->idns_name = NULL;
87 1.2.2.2 joerg idns->idns_namelen = 0;
88 1.2.2.2 joerg softd->ipf_p_dns_list = idns->idns_next;
89 1.2.2.2 joerg KFREE(idns);
90 1.2.2.2 joerg }
91 1.2.2.2 joerg RW_DESTROY(&softd->ipf_p_dns_rwlock);
92 1.2.2.2 joerg
93 1.2.2.2 joerg KFREE(softd);
94 1.2.2.2 joerg }
95 1.2.2.2 joerg
96 1.2.2.2 joerg
97 1.2.2.2 joerg int
98 1.2.2.2 joerg ipf_p_dns_ctl(ipf_main_softc_t *softc, void *arg, ap_ctl_t *ctl)
99 1.2.2.2 joerg {
100 1.2.2.2 joerg ipf_dns_softc_t *softd = arg;
101 1.2.2.2 joerg ipf_dns_filter_t *tmp, *idns, **idnsp;
102 1.2.2.2 joerg int error = 0;
103 1.2.2.2 joerg
104 1.2.2.2 joerg /*
105 1.2.2.2 joerg * To make locking easier.
106 1.2.2.2 joerg */
107 1.2.2.2 joerg KMALLOC(tmp, ipf_dns_filter_t *);
108 1.2.2.2 joerg
109 1.2.2.2 joerg WRITE_ENTER(&softd->ipf_p_dns_rwlock);
110 1.2.2.2 joerg for (idnsp = &softd->ipf_p_dns_list; (idns = *idnsp) != NULL;
111 1.2.2.2 joerg idnsp = &idns->idns_next) {
112 1.2.2.2 joerg if (idns->idns_namelen != ctl->apc_dsize)
113 1.2.2.2 joerg continue;
114 1.2.2.2 joerg if (!strncmp(ctl->apc_data, idns->idns_name,
115 1.2.2.2 joerg idns->idns_namelen))
116 1.2.2.2 joerg break;
117 1.2.2.2 joerg }
118 1.2.2.2 joerg
119 1.2.2.2 joerg switch (ctl->apc_cmd)
120 1.2.2.2 joerg {
121 1.2.2.2 joerg case APC_CMD_DEL :
122 1.2.2.2 joerg if (idns == NULL) {
123 1.2.2.2 joerg IPFERROR(80006);
124 1.2.2.2 joerg error = ESRCH;
125 1.2.2.2 joerg break;
126 1.2.2.2 joerg }
127 1.2.2.2 joerg *idnsp = idns->idns_next;
128 1.2.2.2 joerg idns->idns_next = NULL;
129 1.2.2.2 joerg KFREES(idns->idns_name, idns->idns_namelen);
130 1.2.2.2 joerg idns->idns_name = NULL;
131 1.2.2.2 joerg idns->idns_namelen = 0;
132 1.2.2.2 joerg KFREE(idns);
133 1.2.2.2 joerg break;
134 1.2.2.2 joerg case APC_CMD_ADD :
135 1.2.2.2 joerg if (idns != NULL) {
136 1.2.2.2 joerg IPFERROR(80007);
137 1.2.2.2 joerg error = EEXIST;
138 1.2.2.2 joerg break;
139 1.2.2.2 joerg }
140 1.2.2.2 joerg if (tmp == NULL) {
141 1.2.2.2 joerg IPFERROR(80008);
142 1.2.2.2 joerg error = ENOMEM;
143 1.2.2.2 joerg break;
144 1.2.2.2 joerg }
145 1.2.2.2 joerg idns = tmp;
146 1.2.2.2 joerg tmp = NULL;
147 1.2.2.2 joerg idns->idns_namelen = ctl->apc_dsize;
148 1.2.2.2 joerg idns->idns_name = ctl->apc_data;
149 1.2.2.2 joerg idns->idns_pass = ctl->apc_arg;
150 1.2.2.2 joerg idns->idns_next = NULL;
151 1.2.2.2 joerg *idnsp = idns;
152 1.2.2.2 joerg ctl->apc_data = NULL;
153 1.2.2.2 joerg ctl->apc_dsize = 0;
154 1.2.2.2 joerg break;
155 1.2.2.2 joerg default :
156 1.2.2.2 joerg IPFERROR(80009);
157 1.2.2.2 joerg error = EINVAL;
158 1.2.2.2 joerg break;
159 1.2.2.2 joerg }
160 1.2.2.2 joerg RWLOCK_EXIT(&softd->ipf_p_dns_rwlock);
161 1.2.2.2 joerg
162 1.2.2.2 joerg if (tmp != NULL) {
163 1.2.2.2 joerg KFREE(tmp);
164 1.2.2.2 joerg tmp = NULL;
165 1.2.2.2 joerg }
166 1.2.2.2 joerg
167 1.2.2.2 joerg return error;
168 1.2.2.2 joerg }
169 1.2.2.2 joerg
170 1.2.2.2 joerg
171 1.2.2.2 joerg /* ARGSUSED */
172 1.2.2.2 joerg int
173 1.2.2.2 joerg ipf_p_dns_new(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
174 1.2.2.2 joerg {
175 1.2.2.2 joerg dnsinfo_t *di;
176 1.2.2.2 joerg int dlen;
177 1.2.2.2 joerg
178 1.2.2.2 joerg dlen = fin->fin_dlen - sizeof(udphdr_t);
179 1.2.2.2 joerg if (dlen < sizeof(ipf_dns_hdr_t)) {
180 1.2.2.2 joerg /*
181 1.2.2.2 joerg * No real DNS packet is smaller than that.
182 1.2.2.2 joerg */
183 1.2.2.2 joerg return -1;
184 1.2.2.2 joerg }
185 1.2.2.2 joerg
186 1.2.2.2 joerg aps->aps_psiz = sizeof(dnsinfo_t);
187 1.2.2.2 joerg KMALLOCS(di, dnsinfo_t *, sizeof(dnsinfo_t));
188 1.2.2.2 joerg if (di == NULL) {
189 1.2.2.2 joerg printf("ipf_dns_new:KMALLOCS(%zu) failed\n", sizeof(*di));
190 1.2.2.2 joerg return -1;
191 1.2.2.2 joerg }
192 1.2.2.2 joerg
193 1.2.2.2 joerg MUTEX_INIT(&di->dnsi_lock, "dns lock");
194 1.2.2.2 joerg
195 1.2.2.2 joerg aps->aps_data = di;
196 1.2.2.2 joerg
197 1.2.2.2 joerg dlen = fin->fin_dlen - sizeof(udphdr_t);
198 1.2.2.2 joerg COPYDATA(fin->fin_m, fin->fin_hlen + sizeof(udphdr_t),
199 1.2.2.2 joerg MIN(dlen, sizeof(di->dnsi_buffer)), di->dnsi_buffer);
200 1.2.2.2 joerg di->dnsi_id = (di->dnsi_buffer[0] << 8) | di->dnsi_buffer[1];
201 1.2.2.2 joerg return 0;
202 1.2.2.2 joerg }
203 1.2.2.2 joerg
204 1.2.2.2 joerg
205 1.2.2.2 joerg /* ARGSUSED */
206 1.2.2.2 joerg int
207 1.2.2.2 joerg ipf_p_dns_del(ipf_main_softc_t *softc, ap_session_t *aps)
208 1.2.2.2 joerg {
209 1.2.2.2 joerg #ifdef USE_MUTEXES
210 1.2.2.2 joerg dnsinfo_t *di = aps->aps_data;
211 1.2.2.2 joerg
212 1.2.2.2 joerg MUTEX_DESTROY(&di->dnsi_lock);
213 1.2.2.2 joerg #endif
214 1.2.2.2 joerg KFREES(aps->aps_data, aps->aps_psiz);
215 1.2.2.2 joerg aps->aps_data = NULL;
216 1.2.2.2 joerg aps->aps_psiz = 0;
217 1.2.2.2 joerg return 0;
218 1.2.2.2 joerg }
219 1.2.2.2 joerg
220 1.2.2.2 joerg
221 1.2.2.2 joerg /*
222 1.2.2.2 joerg * Tries to match the base string (in our ACL) with the query from a packet.
223 1.2.2.2 joerg */
224 1.2.2.2 joerg int
225 1.2.2.2 joerg ipf_p_dns_match_names(ipf_dns_filter_t *idns, char *query, int qlen)
226 1.2.2.2 joerg {
227 1.2.2.2 joerg int blen;
228 1.2.2.2 joerg char *base;
229 1.2.2.2 joerg
230 1.2.2.2 joerg blen = idns->idns_namelen;
231 1.2.2.2 joerg base = idns->idns_name;
232 1.2.2.2 joerg
233 1.2.2.2 joerg if (blen > qlen)
234 1.2.2.2 joerg return 1;
235 1.2.2.2 joerg
236 1.2.2.2 joerg if (blen == qlen)
237 1.2.2.2 joerg return strncasecmp(base, query, qlen);
238 1.2.2.2 joerg
239 1.2.2.2 joerg /*
240 1.2.2.2 joerg * If the base string string is shorter than the query, allow the
241 1.2.2.2 joerg * tail of the base to match the same length tail of the query *if*:
242 1.2.2.2 joerg * - the base string starts with a '*' (*cnn.com)
243 1.2.2.2 joerg * - the base string represents a domain (.cnn.com)
244 1.2.2.2 joerg * as otherwise it would not be possible to block just "cnn.com"
245 1.2.2.2 joerg * without also impacting "foocnn.com", etc.
246 1.2.2.2 joerg */
247 1.2.2.2 joerg if (*base == '*') {
248 1.2.2.2 joerg base++;
249 1.2.2.2 joerg blen--;
250 1.2.2.2 joerg } else if (*base != '.')
251 1.2.2.2 joerg return 1;
252 1.2.2.2 joerg
253 1.2.2.2 joerg return strncasecmp(base, query + qlen - blen, blen);
254 1.2.2.2 joerg }
255 1.2.2.2 joerg
256 1.2.2.2 joerg
257 1.2.2.2 joerg int
258 1.2.2.2 joerg ipf_p_dns_get_name(ipf_dns_softc_t *softd, char *start, int len, char *buffer,
259 1.2.2.2 joerg int buflen)
260 1.2.2.2 joerg {
261 1.2.2.2 joerg char *s, *t, clen;
262 1.2.2.2 joerg int slen, blen;
263 1.2.2.2 joerg
264 1.2.2.2 joerg s = start;
265 1.2.2.2 joerg t = buffer;
266 1.2.2.2 joerg slen = len;
267 1.2.2.2 joerg blen = buflen - 1; /* Always make room for trailing \0 */
268 1.2.2.2 joerg
269 1.2.2.2 joerg while (*s != '\0') {
270 1.2.2.2 joerg clen = *s;
271 1.2.2.2 joerg if ((clen & 0xc0) == 0xc0) { /* Doesn't do compression */
272 1.2.2.2 joerg softd->ipf_p_dns_compress++;
273 1.2.2.2 joerg return 0;
274 1.2.2.2 joerg }
275 1.2.2.2 joerg if (clen > slen) {
276 1.2.2.2 joerg softd->ipf_p_dns_toolong++;
277 1.2.2.2 joerg return 0; /* Does the name run off the end? */
278 1.2.2.2 joerg }
279 1.2.2.2 joerg if ((clen + 1) > blen) {
280 1.2.2.2 joerg softd->ipf_p_dns_nospace++;
281 1.2.2.2 joerg return 0; /* Enough room for name+.? */
282 1.2.2.2 joerg }
283 1.2.2.2 joerg s++;
284 1.2.2.2 joerg bcopy(s, t, clen);
285 1.2.2.2 joerg t += clen;
286 1.2.2.2 joerg s += clen;
287 1.2.2.2 joerg *t++ = '.';
288 1.2.2.2 joerg slen -= clen;
289 1.2.2.2 joerg blen -= (clen + 1);
290 1.2.2.2 joerg }
291 1.2.2.2 joerg
292 1.2.2.2 joerg *(t - 1) = '\0';
293 1.2.2.2 joerg return s - start;
294 1.2.2.2 joerg }
295 1.2.2.2 joerg
296 1.2.2.2 joerg
297 1.2.2.2 joerg int
298 1.2.2.2 joerg ipf_p_dns_allow_query(ipf_dns_softc_t *softd, dnsinfo_t *dnsi)
299 1.2.2.2 joerg {
300 1.2.2.2 joerg ipf_dns_filter_t *idns;
301 1.2.2.2 joerg int len;
302 1.2.2.2 joerg
303 1.2.2.2 joerg len = strlen(dnsi->dnsi_buffer);
304 1.2.2.2 joerg
305 1.2.2.2 joerg for (idns = softd->ipf_p_dns_list; idns != NULL; idns = idns->idns_next)
306 1.2.2.2 joerg if (ipf_p_dns_match_names(idns, dnsi->dnsi_buffer, len) == 0)
307 1.2.2.2 joerg return idns->idns_pass;
308 1.2.2.2 joerg return 0;
309 1.2.2.2 joerg }
310 1.2.2.2 joerg
311 1.2.2.2 joerg
312 1.2.2.2 joerg /* ARGSUSED */
313 1.2.2.2 joerg int
314 1.2.2.2 joerg ipf_p_dns_inout(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
315 1.2.2.2 joerg {
316 1.2.2.2 joerg ipf_dns_softc_t *softd = arg;
317 1.2.2.2 joerg ipf_dns_hdr_t *dns;
318 1.2.2.2 joerg dnsinfo_t *di;
319 1.2.2.2 joerg char *data;
320 1.2.2.2 joerg int dlen, q, rc = 0;
321 1.2.2.2 joerg
322 1.2.2.2 joerg if (fin->fin_dlen < sizeof(*dns))
323 1.2.2.2 joerg return APR_ERR(1);
324 1.2.2.2 joerg
325 1.2.2.2 joerg dns = (ipf_dns_hdr_t *)((char *)fin->fin_dp + sizeof(udphdr_t));
326 1.2.2.2 joerg
327 1.2.2.2 joerg q = dns->dns_qdcount;
328 1.2.2.2 joerg
329 1.2.2.2 joerg data = (char *)(dns + 1);
330 1.2.2.2 joerg dlen = fin->fin_dlen - sizeof(*dns) - sizeof(udphdr_t);
331 1.2.2.2 joerg
332 1.2.2.2 joerg di = aps->aps_data;
333 1.2.2.2 joerg
334 1.2.2.2 joerg READ_ENTER(&softd->ipf_p_dns_rwlock);
335 1.2.2.2 joerg MUTEX_ENTER(&di->dnsi_lock);
336 1.2.2.2 joerg
337 1.2.2.2 joerg for (; (dlen > 0) && (q > 0); q--) {
338 1.2.2.2 joerg int len;
339 1.2.2.2 joerg
340 1.2.2.2 joerg len = ipf_p_dns_get_name(softd, data, dlen, di->dnsi_buffer,
341 1.2.2.2 joerg sizeof(di->dnsi_buffer));
342 1.2.2.2 joerg if (len == 0) {
343 1.2.2.2 joerg rc = 1;
344 1.2.2.2 joerg break;
345 1.2.2.2 joerg }
346 1.2.2.2 joerg rc = ipf_p_dns_allow_query(softd, di);
347 1.2.2.2 joerg if (rc != 0)
348 1.2.2.2 joerg break;
349 1.2.2.2 joerg data += len;
350 1.2.2.2 joerg dlen -= len;
351 1.2.2.2 joerg }
352 1.2.2.2 joerg MUTEX_EXIT(&di->dnsi_lock);
353 1.2.2.2 joerg RWLOCK_EXIT(&softd->ipf_p_dns_rwlock);
354 1.2.2.2 joerg
355 1.2.2.2 joerg return APR_ERR(rc);
356 1.2.2.2 joerg }
357 1.2.2.2 joerg
358 1.2.2.2 joerg
359 1.2.2.2 joerg /* ARGSUSED */
360 1.2.2.2 joerg int
361 1.2.2.2 joerg ipf_p_dns_match(fr_info_t *fin, ap_session_t *aps, nat_t *nat)
362 1.2.2.2 joerg {
363 1.2.2.2 joerg dnsinfo_t *di = aps->aps_data;
364 1.2.2.2 joerg ipf_dns_hdr_t *dnh;
365 1.2.2.2 joerg
366 1.2.2.2 joerg if ((fin->fin_dlen < sizeof(u_short)) || (fin->fin_flx & FI_FRAG))
367 1.2.2.2 joerg return -1;
368 1.2.2.2 joerg
369 1.2.2.2 joerg dnh = (ipf_dns_hdr_t *)((char *)fin->fin_dp + sizeof(udphdr_t));
370 1.2.2.2 joerg if (((dnh->dns_id[0] << 8) | dnh->dns_id[1]) != di->dnsi_id)
371 1.2.2.2 joerg return -1;
372 1.2.2.2 joerg return 0;
373 1.2.2.2 joerg }
374