xref: /dragonfly/sys/net/pf/if_pflog.c (revision c70d4562)
1 /*	$OpenBSD: if_pflog.c,v 1.27 2007/12/20 02:53:02 brad Exp $	*/
2 /*
3  * The authors of this code are John Ioannidis (ji@tla.org),
4  * Angelos D. Keromytis (kermit@csd.uch.gr) and
5  * Niels Provos (provos@physnet.uni-hamburg.de).
6  *
7  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
8  * in November 1995.
9  *
10  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
11  * by Angelos D. Keromytis.
12  *
13  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
14  * and Niels Provos.
15  *
16  * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
17  * and Niels Provos.
18  * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
19  *
20  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
21  *
22  * Permission to use, copy, and modify this software with or without fee
23  * is hereby granted, provided that this entire notice is included in
24  * all copies of any software which is or includes a copy or
25  * modification of this software.
26  * You may use this code under the GNU public license if you so wish. Please
27  * contribute changes back to the authors under this freer than GPL license
28  * so that we may further the use of strong encryption without limitations to
29  * all.
30  *
31  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
32  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
33  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
34  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
35  * PURPOSE.
36  */
37 
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/in_cksum.h>
44 #include <sys/mbuf.h>
45 #include <sys/proc.h>
46 #include <sys/socket.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/sockio.h>
51 #include <sys/thread2.h>
52 
53 #include <net/if.h>
54 #include <net/if_types.h>
55 #include <net/ifq_var.h>
56 #include <net/route.h>
57 #include <net/bpf.h>
58 
59 #ifdef	INET
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #endif
65 
66 #ifdef INET6
67 #ifndef INET
68 #include <netinet/in.h>
69 #endif
70 #include <netinet6/nd6.h>
71 #endif /* INET6 */
72 
73 #include <net/pf/pfvar.h>
74 #include <net/pf/if_pflog.h>
75 
76 #define	PFLOGNAME	"pflog"
77 
78 #define PFLOGMTU	(32768 + MHLEN + MLEN)
79 
80 #ifdef PFLOGDEBUG
81 #define DPRINTF(x)	do { if (pflogdebug) kprintf x ; } while (0)
82 #else
83 #define DPRINTF(x)
84 #endif
85 
86 static void	pflogattach(void);
87 static int	pflogoutput(struct ifnet *, struct mbuf *,
88 		    struct sockaddr *, struct rtentry *);
89 static int	pflogioctl(struct ifnet *, u_long, caddr_t __unused,
90 		    struct ucred * __unused);
91 static void	pflogstart(struct ifnet *, struct ifaltq_subque *);
92 static int	pflog_clone_create(struct if_clone *, int, caddr_t __unused);
93 static int	pflog_clone_destroy(struct ifnet *);
94 
95 static struct if_clone pflog_cloner = IF_CLONE_INITIALIZER(
96     PFLOGNAME, pflog_clone_create, pflog_clone_destroy, 1, 1);
97 
98 static LIST_HEAD(, pflog_softc) pflogif_list;
99 static struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */
100 
101 static void
102 pflogattach(void)
103 {
104 	int	i;
105 	LIST_INIT(&pflogif_list);
106 	for (i = 0; i < PFLOGIFS_MAX; i++)
107 		pflogifs[i] = NULL;
108 	if_clone_attach(&pflog_cloner);
109 }
110 
111 static int
112 pflog_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
113 {
114 	struct ifnet *ifp;
115 	struct pflog_softc *pflogif;
116 
117 	lwkt_gettoken(&pf_token);
118 
119 	if (unit >= PFLOGIFS_MAX) {
120 		lwkt_reltoken(&pf_token);
121 		return (EINVAL);
122 	}
123 
124 	pflogif = kmalloc(sizeof(*pflogif), M_DEVBUF, M_WAITOK | M_ZERO);
125 	pflogif->sc_unit = unit;
126 	lwkt_reltoken(&pf_token);
127 
128 	ifp = &pflogif->sc_if;
129 	if_initname(ifp, ifc->ifc_name, unit);
130 	ifp->if_softc = pflogif;
131 	ifp->if_mtu = PFLOGMTU;
132 	ifp->if_ioctl = pflogioctl;
133 	ifp->if_output = pflogoutput;
134 	ifp->if_start = pflogstart;
135 	ifp->if_type = IFT_PFLOG;
136 	ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
137 	ifp->if_hdrlen = PFLOG_HDRLEN;
138 
139 	if_attach(ifp, NULL);
140 #if NBPFILTER > 0
141 	bpfattach(&pflogif->sc_if, DLT_PFLOG, PFLOG_HDRLEN);
142 #endif
143 
144 	lwkt_gettoken(&pf_token);
145 	crit_enter();
146 	LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list);
147 	pflogifs[unit] = ifp;
148 	crit_exit();
149 	lwkt_reltoken(&pf_token);
150 
151 	return (0);
152 }
153 
154 static int
155 pflog_clone_destroy(struct ifnet *ifp)
156 {
157 	struct pflog_softc	*pflogif = ifp->if_softc;
158 
159 	lwkt_gettoken(&pf_token);
160 
161 	crit_enter();
162 	pflogifs[pflogif->sc_unit] = NULL;
163 	LIST_REMOVE(pflogif, sc_list);
164 	crit_exit();
165 
166 	lwkt_reltoken(&pf_token);
167 #if NBPFILTER > 0
168 	bpfdetach(ifp);
169 #endif
170 	if_detach(ifp);
171 	lwkt_gettoken(&pf_token);
172 	kfree(pflogif, M_DEVBUF);
173 	lwkt_reltoken(&pf_token);
174 
175 	return 0;
176 }
177 
178 /*
179  * Start output on the pflog interface.
180  */
181 static void
182 pflogstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
183 {
184 	struct mbuf *m;
185 
186 	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
187 	ASSERT_LWKT_TOKEN_HELD(&pf_token);
188 
189 	for (;;) {
190 		m = ifsq_dequeue(ifsq);
191 		if (m == NULL)
192 			return;
193 		else
194 			m_freem(m);
195 	}
196 }
197 
198 static int
199 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
200 	    struct rtentry *rt)
201 {
202 	m_freem(m);
203 	return (0);
204 }
205 
206 static int
207 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data __unused,
208 	   struct ucred *cr __unused)
209 {
210 
211 	lwkt_gettoken(&pf_token);
212 
213 	switch (cmd) {
214 	case SIOCSIFFLAGS:
215 		lwkt_reltoken(&pf_token);
216 		if (ifp->if_flags & IFF_UP)
217 			ifp->if_flags |= IFF_RUNNING;
218 		else
219 			ifp->if_flags &= ~IFF_RUNNING;
220 		lwkt_gettoken(&pf_token);
221 		break;
222 	default:
223 		lwkt_reltoken(&pf_token);
224 		return (EINVAL);
225 	}
226 
227 	lwkt_reltoken(&pf_token);
228 	return (0);
229 }
230 
231 int
232 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
233 	     u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
234 	     struct pf_ruleset *ruleset, struct pf_pdesc *pd)
235 {
236 	struct ifnet *ifn = NULL;
237 	struct pfloghdr hdr;
238 
239 	ASSERT_LWKT_TOKEN_HELD(&pf_token);
240 
241 	if (kif == NULL || m == NULL || rm == NULL)
242 		return (-1);
243 
244 	if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
245 		return (0);
246 
247 	bzero(&hdr, sizeof(hdr));
248 	hdr.length = PFLOG_REAL_HDRLEN;
249 	hdr.af = af;
250 	hdr.action = rm->action;
251 	hdr.reason = reason;
252 	memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
253 
254 	if (am == NULL) {
255 		hdr.rulenr = htonl(rm->nr);
256 		hdr.subrulenr = -1;
257 	} else {
258 		hdr.rulenr = htonl(am->nr);
259 		hdr.subrulenr = htonl(rm->nr);
260 		if (ruleset != NULL && ruleset->anchor != NULL)
261 			strlcpy(hdr.ruleset, ruleset->anchor->name,
262 			    sizeof(hdr.ruleset));
263 	}
264 	if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done)
265 		pd->lookup.done = pf_socket_lookup(dir, pd);
266 	if (pd->lookup.done > 0) {
267 		hdr.uid = pd->lookup.uid;
268 		hdr.pid = pd->lookup.pid;
269 	} else {
270 		hdr.uid = UID_MAX;
271 		hdr.pid = NO_PID;
272 	}
273 	hdr.rule_uid = rm->cuid;
274 	hdr.rule_pid = rm->cpid;
275 	hdr.dir = dir;
276 
277 #ifdef INET
278 	if (af == AF_INET) {
279 		struct ip *ip;
280 		ip = mtod(m, struct ip *);
281 		ip->ip_len = htons(ip->ip_len);
282 		ip->ip_off = htons(ip->ip_off);
283 
284 		if (dir == PF_OUT) {
285 			ip->ip_sum = 0;
286 			ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
287 		}
288 	}
289 #endif /* INET */
290 
291 	IFNET_STAT_INC(ifn, opackets, 1);
292 	IFNET_STAT_INC(ifn, obytes, m->m_pkthdr.len);
293 	if (ifn->if_bpf) {
294 		bpf_gettoken();
295 		if (ifn->if_bpf) {
296 			bpf_mtap_hdr(ifn->if_bpf, (char *)&hdr, PFLOG_HDRLEN, m,
297 			    BPF_DIRECTION_OUT);
298 		}
299 		bpf_reltoken();
300 	}
301 
302 #ifdef INET
303 	if (af == AF_INET) {
304 		struct ip *ip = mtod(m, struct ip *);
305 
306 		ip->ip_len = ntohs(ip->ip_len);
307 		ip->ip_off = ntohs(ip->ip_off);
308 	}
309 #endif /* INET */
310 
311 	return (0);
312 }
313 
314 static int
315 pflog_modevent(module_t mod, int type, void *data)
316 {
317 	int error = 0;
318 
319 	struct pflog_softc *pflogif, *tmp;
320 
321 	lwkt_gettoken(&pf_token);
322 
323 	switch (type) {
324 	case MOD_LOAD:
325 		lwkt_reltoken(&pf_token);
326 		pflogattach();
327 		lwkt_gettoken(&pf_token);
328 		break;
329 
330 	case MOD_UNLOAD:
331 		lwkt_reltoken(&pf_token);
332 		if_clone_detach(&pflog_cloner);
333 		LIST_FOREACH_MUTABLE(pflogif, &pflogif_list, sc_list, tmp)
334 			pflog_clone_destroy(&pflogif->sc_if);
335 		lwkt_gettoken(&pf_token);
336 		break;
337 
338 	default:
339 		error = EINVAL;
340 		break;
341 	}
342 	lwkt_reltoken(&pf_token);
343 	return error;
344 }
345 
346 static moduledata_t pflog_mod = {
347 	"pflog",
348 	pflog_modevent,
349 	0
350 };
351 
352 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
353 MODULE_VERSION(pflog, PFLOG_MODVER);
354 /* Do not run before pf is initialized. */
355 MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER);
356