2 * Copyright (C) 2012 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * $Id: ip_dns_pxy.c,v 1.1.2.10 2012/07/22 08:04:23 darren_r Exp $
12 * map ... proxy port dns/udp 53 { block .cnn.com; }
14 typedef struct ipf_dns_filter {
15 struct ipf_dns_filter *idns_next;
22 typedef struct ipf_dns_softc_s {
23 ipf_dns_filter_t *ipf_p_dns_list;
24 ipfrwlock_t ipf_p_dns_rwlock;
25 u_long ipf_p_dns_compress;
26 u_long ipf_p_dns_toolong;
27 u_long ipf_p_dns_nospace;
30 int ipf_p_dns_allow_query __P((ipf_dns_softc_t *, dnsinfo_t *));
31 int ipf_p_dns_ctl __P((ipf_main_softc_t *, void *, ap_ctl_t *));
32 int ipf_p_dns_del __P((ipf_main_softc_t *, ap_session_t *));
33 int ipf_p_dns_get_name __P((ipf_dns_softc_t *, char *, int, char *, int));
34 int ipf_p_dns_inout __P((void *, fr_info_t *, ap_session_t *, nat_t *));
35 int ipf_p_dns_match __P((fr_info_t *, ap_session_t *, nat_t *));
36 int ipf_p_dns_match_names __P((ipf_dns_filter_t *, char *, int));
37 int ipf_p_dns_new __P((void *, fr_info_t *, ap_session_t *, nat_t *));
38 void *ipf_p_dns_soft_create __P((ipf_main_softc_t *));
39 void ipf_p_dns_soft_destroy __P((ipf_main_softc_t *, void *));
50 #define DNS_QR(x) ((ntohs(x) & 0x8000) >> 15)
51 #define DNS_OPCODE(x) ((ntohs(x) & 0x7800) >> 11)
52 #define DNS_AA(x) ((ntohs(x) & 0x0400) >> 10)
53 #define DNS_TC(x) ((ntohs(x) & 0x0200) >> 9)
54 #define DNS_RD(x) ((ntohs(x) & 0x0100) >> 8)
55 #define DNS_RA(x) ((ntohs(x) & 0x0080) >> 7)
56 #define DNS_Z(x) ((ntohs(x) & 0x0070) >> 4)
57 #define DNS_RCODE(x) ((ntohs(x) & 0x000f) >> 0)
61 ipf_p_dns_soft_create(softc)
62 ipf_main_softc_t *softc;
64 ipf_dns_softc_t *softd;
66 KMALLOC(softd, ipf_dns_softc_t *);
70 bzero((char *)softd, sizeof(*softd));
71 RWLOCK_INIT(&softd->ipf_p_dns_rwlock, "ipf dns rwlock");
78 ipf_p_dns_soft_destroy(softc, arg)
79 ipf_main_softc_t *softc;
82 ipf_dns_softc_t *softd = arg;
83 ipf_dns_filter_t *idns;
85 while ((idns = softd->ipf_p_dns_list) != NULL) {
86 KFREES(idns->idns_name, idns->idns_namelen);
87 idns->idns_name = NULL;
88 idns->idns_namelen = 0;
89 softd->ipf_p_dns_list = idns->idns_next;
92 RW_DESTROY(&softd->ipf_p_dns_rwlock);
99 ipf_p_dns_ctl(softc, arg, ctl)
100 ipf_main_softc_t *softc;
104 ipf_dns_softc_t *softd = arg;
105 ipf_dns_filter_t *tmp, *idns, **idnsp;
109 * To make locking easier.
111 KMALLOC(tmp, ipf_dns_filter_t *);
113 WRITE_ENTER(&softd->ipf_p_dns_rwlock);
114 for (idnsp = &softd->ipf_p_dns_list; (idns = *idnsp) != NULL;
115 idnsp = &idns->idns_next) {
116 if (idns->idns_namelen != ctl->apc_dsize)
118 if (!strncmp(ctl->apc_data, idns->idns_name,
123 switch (ctl->apc_cmd)
131 *idnsp = idns->idns_next;
132 idns->idns_next = NULL;
133 KFREES(idns->idns_name, idns->idns_namelen);
134 idns->idns_name = NULL;
135 idns->idns_namelen = 0;
151 idns->idns_namelen = ctl->apc_dsize;
152 idns->idns_name = ctl->apc_data;
153 idns->idns_pass = ctl->apc_arg;
154 idns->idns_next = NULL;
156 ctl->apc_data = NULL;
164 RWLOCK_EXIT(&softd->ipf_p_dns_rwlock);
177 ipf_p_dns_new(arg, fin, aps, nat)
189 dlen = fin->fin_dlen - sizeof(udphdr_t);
190 if (dlen < sizeof(ipf_dns_hdr_t)) {
192 * No real DNS packet is smaller than that.
197 aps->aps_psiz = sizeof(dnsinfo_t);
198 KMALLOCS(di, dnsinfo_t *, sizeof(dnsinfo_t));
200 printf("ipf_dns_new:KMALLOCS(%d) failed\n", sizeof(*di));
204 MUTEX_INIT(&di->dnsi_lock, "dns lock");
208 dlen = fin->fin_dlen - sizeof(udphdr_t);
209 COPYDATA(fin->fin_m, fin->fin_hlen + sizeof(udphdr_t),
210 MIN(dlen, sizeof(di->dnsi_buffer)), di->dnsi_buffer);
211 di->dnsi_id = (di->dnsi_buffer[0] << 8) | di->dnsi_buffer[1];
218 ipf_p_dns_del(softc, aps)
219 ipf_main_softc_t *softc;
223 dnsinfo_t *di = aps->aps_data;
225 MUTEX_DESTROY(&di->dnsi_lock);
227 KFREES(aps->aps_data, aps->aps_psiz);
228 aps->aps_data = NULL;
235 * Tries to match the base string (in our ACL) with the query from a packet.
238 ipf_p_dns_match_names(idns, query, qlen)
239 ipf_dns_filter_t *idns;
246 blen = idns->idns_namelen;
247 base = idns->idns_name;
253 return strncasecmp(base, query, qlen);
256 * If the base string string is shorter than the query, allow the
257 * tail of the base to match the same length tail of the query *if*:
258 * - the base string starts with a '*' (*cnn.com)
259 * - the base string represents a domain (.cnn.com)
260 * as otherwise it would not be possible to block just "cnn.com"
261 * without also impacting "foocnn.com", etc.
266 } else if (*base != '.')
269 return strncasecmp(base, query + qlen - blen, blen);
274 ipf_p_dns_get_name(softd, start, len, buffer, buflen)
275 ipf_dns_softc_t *softd;
287 blen = buflen - 1; /* Always make room for trailing \0 */
291 if ((clen & 0xc0) == 0xc0) { /* Doesn't do compression */
292 softd->ipf_p_dns_compress++;
296 softd->ipf_p_dns_toolong++;
297 return 0; /* Does the name run off the end? */
299 if ((clen + 1) > blen) {
300 softd->ipf_p_dns_nospace++;
301 return 0; /* Enough room for name+.? */
318 ipf_p_dns_allow_query(softd, dnsi)
319 ipf_dns_softc_t *softd;
322 ipf_dns_filter_t *idns;
325 len = strlen(dnsi->dnsi_buffer);
327 for (idns = softd->ipf_p_dns_list; idns != NULL; idns = idns->idns_next)
328 if (ipf_p_dns_match_names(idns, dnsi->dnsi_buffer, len) == 0)
329 return idns->idns_pass;
336 ipf_p_dns_inout(arg, fin, aps, nat)
342 ipf_dns_softc_t *softd = arg;
348 if (fin->fin_dlen < sizeof(*dns))
351 dns = (ipf_dns_hdr_t *)((char *)fin->fin_dp + sizeof(udphdr_t));
353 q = dns->dns_qdcount;
355 data = (char *)(dns + 1);
356 dlen = fin->fin_dlen - sizeof(*dns) - sizeof(udphdr_t);
360 READ_ENTER(&softd->ipf_p_dns_rwlock);
361 MUTEX_ENTER(&di->dnsi_lock);
363 for (; (dlen > 0) && (q > 0); q--) {
366 len = ipf_p_dns_get_name(softd, data, dlen, di->dnsi_buffer,
367 sizeof(di->dnsi_buffer));
372 rc = ipf_p_dns_allow_query(softd, di);
378 MUTEX_EXIT(&di->dnsi_lock);
379 RWLOCK_EXIT(&softd->ipf_p_dns_rwlock);
387 ipf_p_dns_match(fin, aps, nat)
392 dnsinfo_t *di = aps->aps_data;
395 if ((fin->fin_dlen < sizeof(u_short)) || (fin->fin_flx & FI_FRAG))
398 dnh = (ipf_dns_hdr_t *)((char *)fin->fin_dp + sizeof(udphdr_t));
399 if (((dnh->dns_id[0] << 8) | dnh->dns_id[1]) != di->dnsi_id)