xref: /freebsd/sys/netinet/accf_dns.c (revision 95ee2897)
1fe267a55SPedro F. Giffuni /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
4744eaff7SDavid Malone  * Copyright (C) 2007 David Malone <dwmalone@FreeBSD.org>
5744eaff7SDavid Malone  * All rights reserved.
6744eaff7SDavid Malone  *
7744eaff7SDavid Malone  * Redistribution and use in source and binary forms, with or without
8744eaff7SDavid Malone  * modification, are permitted provided that the following conditions
9744eaff7SDavid Malone  * are met:
10744eaff7SDavid Malone  * 1. Redistributions of source code must retain the above copyright
11744eaff7SDavid Malone  *    notice, this list of conditions and the following disclaimer.
12744eaff7SDavid Malone  * 2. Redistributions in binary form must reproduce the above copyright
13744eaff7SDavid Malone  *    notice, this list of conditions and the following disclaimer in the
14744eaff7SDavid Malone  *    documentation and/or other materials provided with the distribution.
15744eaff7SDavid Malone  *
16744eaff7SDavid Malone  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17744eaff7SDavid Malone  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18744eaff7SDavid Malone  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19744eaff7SDavid Malone  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20744eaff7SDavid Malone  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21744eaff7SDavid Malone  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22744eaff7SDavid Malone  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23744eaff7SDavid Malone  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24744eaff7SDavid Malone  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25744eaff7SDavid Malone  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26744eaff7SDavid Malone  * SUCH DAMAGE.
27744eaff7SDavid Malone  */
28744eaff7SDavid Malone 
29744eaff7SDavid Malone #define ACCEPT_FILTER_MOD
30744eaff7SDavid Malone 
31744eaff7SDavid Malone #include <sys/param.h>
32744eaff7SDavid Malone #include <sys/kernel.h>
33744eaff7SDavid Malone #include <sys/mbuf.h>
34744eaff7SDavid Malone #include <sys/module.h>
35744eaff7SDavid Malone #include <sys/signalvar.h>
36744eaff7SDavid Malone #include <sys/sysctl.h>
37744eaff7SDavid Malone #include <sys/socketvar.h>
38744eaff7SDavid Malone 
39744eaff7SDavid Malone /* check for full DNS request */
4074fb0ba7SJohn Baldwin static int sohasdns(struct socket *so, void *arg, int waitflag);
41744eaff7SDavid Malone 
42591b09b4SMark Johnston ACCEPT_FILTER_DEFINE(accf_dns, "dnsready", sohasdns, NULL, NULL, 1);
43591b09b4SMark Johnston 
44744eaff7SDavid Malone struct packet {
45744eaff7SDavid Malone 	struct mbuf *m;		/* Current mbuf. */
46744eaff7SDavid Malone 	struct mbuf *n;		/* nextpkt mbuf. */
47744eaff7SDavid Malone 	unsigned long moff;	/* Offset of the beginning of m. */
48744eaff7SDavid Malone 	unsigned long offset;	/* Which offset we are working at. */
49744eaff7SDavid Malone 	unsigned long len;	/* The number of bytes we have to play with. */
50744eaff7SDavid Malone };
51744eaff7SDavid Malone 
52744eaff7SDavid Malone #define DNS_OK 0
53744eaff7SDavid Malone #define DNS_WAIT -1
54744eaff7SDavid Malone #define DNS_RUN -2
55744eaff7SDavid Malone 
56744eaff7SDavid Malone /* check we can skip over various parts of DNS request */
57744eaff7SDavid Malone static int skippacket(struct sockbuf *sb);
58744eaff7SDavid Malone 
5974fb0ba7SJohn Baldwin static int
sohasdns(struct socket * so,void * arg,int waitflag)60744eaff7SDavid Malone sohasdns(struct socket *so, void *arg, int waitflag)
61744eaff7SDavid Malone {
62744eaff7SDavid Malone 	struct sockbuf *sb = &so->so_rcv;
63744eaff7SDavid Malone 
64744eaff7SDavid Malone 	/* If the socket is full, we're ready. */
65cfa6009eSGleb Smirnoff 	if (sbused(sb) >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax)
66744eaff7SDavid Malone 		goto ready;
67744eaff7SDavid Malone 
68256d7a8aSJohn Baldwin 	/* Check to see if we have a request. */
69744eaff7SDavid Malone 	if (skippacket(sb) == DNS_WAIT)
7074fb0ba7SJohn Baldwin 		return (SU_OK);
71744eaff7SDavid Malone 
72744eaff7SDavid Malone ready:
7374fb0ba7SJohn Baldwin 	return (SU_ISCONNECTED);
74744eaff7SDavid Malone }
75744eaff7SDavid Malone 
76744eaff7SDavid Malone #define GET8(p, val) do { \
77744eaff7SDavid Malone 	if (p->offset < p->moff) \
78744eaff7SDavid Malone 		return DNS_RUN; \
79744eaff7SDavid Malone 	while (p->offset >= p->moff + p->m->m_len) { \
80744eaff7SDavid Malone 		p->moff += p->m->m_len; \
81744eaff7SDavid Malone 		p->m = p->m->m_next; \
82744eaff7SDavid Malone 		if (p->m == NULL) { \
83744eaff7SDavid Malone 			p->m = p->n; \
84744eaff7SDavid Malone 			p->n = p->m->m_nextpkt; \
85744eaff7SDavid Malone 		} \
86744eaff7SDavid Malone 		if (p->m == NULL) \
87744eaff7SDavid Malone 			return DNS_WAIT; \
88744eaff7SDavid Malone 	} \
89744eaff7SDavid Malone 	val = *(mtod(p->m, unsigned char *) + (p->offset - p->moff)); \
90744eaff7SDavid Malone 	p->offset++; \
91744eaff7SDavid Malone 	} while (0)
92744eaff7SDavid Malone 
93744eaff7SDavid Malone #define GET16(p, val) do { \
94744eaff7SDavid Malone 	unsigned int v0, v1; \
95744eaff7SDavid Malone 	GET8(p, v0); \
96744eaff7SDavid Malone 	GET8(p, v1); \
97744eaff7SDavid Malone 	val = v0 * 0x100 + v1; \
98744eaff7SDavid Malone 	} while (0)
99744eaff7SDavid Malone 
100744eaff7SDavid Malone static int
skippacket(struct sockbuf * sb)101744eaff7SDavid Malone skippacket(struct sockbuf *sb) {
102744eaff7SDavid Malone 	unsigned long packlen;
103744eaff7SDavid Malone 	struct packet q, *p = &q;
104744eaff7SDavid Malone 
105cfa6009eSGleb Smirnoff 	if (sbavail(sb) < 2)
106744eaff7SDavid Malone 		return DNS_WAIT;
107744eaff7SDavid Malone 
108744eaff7SDavid Malone 	q.m = sb->sb_mb;
109744eaff7SDavid Malone 	q.n = q.m->m_nextpkt;
110744eaff7SDavid Malone 	q.moff = 0;
111744eaff7SDavid Malone 	q.offset = 0;
112cfa6009eSGleb Smirnoff 	q.len = sbavail(sb);
113744eaff7SDavid Malone 
114744eaff7SDavid Malone 	GET16(p, packlen);
115256d7a8aSJohn Baldwin 	if (packlen + 2 > q.len)
116744eaff7SDavid Malone 		return DNS_WAIT;
117744eaff7SDavid Malone 
118744eaff7SDavid Malone 	return DNS_OK;
119744eaff7SDavid Malone }
120