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