xref: /dragonfly/sys/net/pf/if_pflog.c (revision 6e278935)
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/route.h>
56 #include <net/bpf.h>
57 
58 #ifdef	INET
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #endif
64 
65 #ifdef INET6
66 #ifndef INET
67 #include <netinet/in.h>
68 #endif
69 #include <netinet6/nd6.h>
70 #endif /* INET6 */
71 
72 #include <net/pf/pfvar.h>
73 #include <net/pf/if_pflog.h>
74 
75 #define	PFLOGNAME	"pflog"
76 
77 #define PFLOGMTU	(32768 + MHLEN + MLEN)
78 
79 #ifdef PFLOGDEBUG
80 #define DPRINTF(x)    do { if (pflogdebug) kprintf x ; } while (0)
81 #else
82 #define DPRINTF(x)
83 #endif
84 
85 void	pflogattach(int);
86 int	pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
87 	    	       struct rtentry *);
88 int	pflogioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
89 void	pflogrtrequest(int, struct rtentry *, struct sockaddr *);
90 void	pflogstart(struct ifnet *);
91 int	pflog_clone_create(struct if_clone *, int, caddr_t);
92 int	pflog_clone_destroy(struct ifnet *);
93 
94 LIST_HEAD(, pflog_softc)	pflogif_list;
95 struct if_clone	pflog_cloner =
96     IF_CLONE_INITIALIZER("pflog", pflog_clone_create, pflog_clone_destroy, 1, 1);
97 
98 struct ifnet	*pflogifs[PFLOGIFS_MAX];	/* for fast access */
99 
100 void
101 pflogattach(int npflog)
102 {
103 	int	i;
104 	LIST_INIT(&pflogif_list);
105 	for (i = 0; i < PFLOGIFS_MAX; i++)
106 		pflogifs[i] = NULL;
107 	if_clone_attach(&pflog_cloner);
108 }
109 
110 int
111 pflog_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
112 {
113 	struct ifnet *ifp;
114 	struct pflog_softc *pflogif;
115 
116 	lwkt_gettoken(&pf_token);
117 
118 	if (unit >= PFLOGIFS_MAX) {
119 		lwkt_reltoken(&pf_token);
120 		return (EINVAL);
121 	}
122 
123 	if ((pflogif = kmalloc(sizeof(*pflogif),
124 		M_DEVBUF, M_WAITOK|M_ZERO)) == NULL) {
125 		lwkt_reltoken(&pf_token);
126 		return (ENOMEM);
127 	}
128 
129 	pflogif->sc_unit = unit;
130 	lwkt_reltoken(&pf_token);
131 	ifp = &pflogif->sc_if;
132 	ksnprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", unit);
133 	ifp->if_softc = pflogif;
134 	ifp->if_mtu = PFLOGMTU;
135 	ifp->if_ioctl = pflogioctl;
136 	ifp->if_output = pflogoutput;
137 	ifp->if_start = pflogstart;
138 	ifp->if_type = IFT_PFLOG;
139 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
140 	ifp->if_hdrlen = PFLOG_HDRLEN;
141 	if_attach(ifp, NULL);
142 
143 	bpfattach(&pflogif->sc_if, DLT_PFLOG, PFLOG_HDRLEN);
144 	lwkt_gettoken(&pf_token);
145 
146 	crit_enter();
147 	LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list);
148 	pflogifs[unit] = ifp;
149 	crit_exit();
150 
151 	lwkt_reltoken(&pf_token);
152 	return (0);
153 }
154 
155 int
156 pflog_clone_destroy(struct ifnet *ifp)
157 {
158 	struct pflog_softc	*pflogif = ifp->if_softc;
159 
160 	lwkt_gettoken(&pf_token);
161 
162 	crit_enter();
163 	pflogifs[pflogif->sc_unit] = NULL;
164 	LIST_REMOVE(pflogif, sc_list);
165 	crit_exit();
166 
167 	lwkt_reltoken(&pf_token);
168 #if NBPFILTER > 0
169 	bpfdetach(ifp);
170 #endif
171 	if_detach(ifp);
172 	lwkt_gettoken(&pf_token);
173 	kfree(pflogif, M_DEVBUF);
174 	lwkt_reltoken(&pf_token);
175 
176 	return 0;
177 }
178 
179 /*
180  * Start output on the pflog interface.
181  */
182 void
183 pflogstart(struct ifnet *ifp)
184 {
185 	struct mbuf *m;
186 
187 	ASSERT_LWKT_TOKEN_HELD(&pf_token);
188 
189 	for (;;) {
190 		lwkt_reltoken(&pf_token);
191 		crit_enter();
192 		IF_DROP(&ifp->if_snd);
193 		IF_DEQUEUE(&ifp->if_snd, m);
194 		crit_exit();
195 		lwkt_gettoken(&pf_token);
196 
197 		if (m == NULL)
198 			return;
199 		else
200 			m_freem(m);
201 	}
202 }
203 
204 int
205 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
206 	struct rtentry *rt)
207 {
208 	m_freem(m);
209 	return (0);
210 }
211 
212 /* ARGSUSED */
213 void
214 pflogrtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
215 {
216 	if (rt)
217 		rt->rt_rmx.rmx_mtu = PFLOGMTU;
218 }
219 
220 /* ARGSUSED */
221 int
222 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
223 {
224 
225 	lwkt_gettoken(&pf_token);
226 
227 	switch (cmd) {
228 	case SIOCSIFFLAGS:
229 		lwkt_reltoken(&pf_token);
230 		if (ifp->if_flags & IFF_UP)
231 			ifp->if_flags |= IFF_RUNNING;
232 		else
233 			ifp->if_flags &= ~IFF_RUNNING;
234 		lwkt_gettoken(&pf_token);
235 		break;
236 	default:
237 		lwkt_reltoken(&pf_token);
238 		return (EINVAL);
239 	}
240 
241 	lwkt_reltoken(&pf_token);
242 	return (0);
243 }
244 
245 int
246 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
247     u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
248     struct pf_ruleset *ruleset, struct pf_pdesc *pd)
249 {
250 	struct ifnet *ifn = NULL;
251 	struct pfloghdr hdr;
252 
253 	ASSERT_LWKT_TOKEN_HELD(&pf_token);
254 
255 	if (kif == NULL || m == NULL || rm == NULL)
256 		return (-1);
257 
258 	if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
259 		return (0);
260 
261 	bzero(&hdr, sizeof(hdr));
262 	hdr.length = PFLOG_REAL_HDRLEN;
263 	hdr.af = af;
264 	hdr.action = rm->action;
265 	hdr.reason = reason;
266 	memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
267 
268 	if (am == NULL) {
269 		hdr.rulenr = htonl(rm->nr);
270 		hdr.subrulenr = -1;
271 	} else {
272 		hdr.rulenr = htonl(am->nr);
273 		hdr.subrulenr = htonl(rm->nr);
274 		if (ruleset != NULL && ruleset->anchor != NULL)
275 			strlcpy(hdr.ruleset, ruleset->anchor->name,
276 			    sizeof(hdr.ruleset));
277 	}
278 	if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done)
279 		pd->lookup.done = pf_socket_lookup(dir, pd);
280 	if (pd->lookup.done > 0) {
281 		hdr.uid = pd->lookup.uid;
282 		hdr.pid = pd->lookup.pid;
283 	} else {
284 		hdr.uid = UID_MAX;
285 		hdr.pid = NO_PID;
286 	}
287 	hdr.rule_uid = rm->cuid;
288 	hdr.rule_pid = rm->cpid;
289 	hdr.dir = dir;
290 
291 #ifdef INET
292 	if (af == AF_INET) {
293 		struct ip *ip;
294 		ip = mtod(m, struct ip *);
295 		ip->ip_len = htons(ip->ip_len);
296 		ip->ip_off = htons(ip->ip_off);
297 
298 		if (dir == PF_OUT) {
299 			ip->ip_sum = 0;
300 			ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
301 		}
302 	}
303 #endif /* INET */
304 
305 	ifn->if_opackets++;
306 	ifn->if_obytes += m->m_pkthdr.len;
307 	lwkt_reltoken(&pf_token);
308 	bpf_mtap_hdr(ifn->if_bpf, (char *)&hdr, PFLOG_HDRLEN, m,
309 	    BPF_DIRECTION_OUT);
310 	lwkt_gettoken(&pf_token);
311 
312 #ifdef INET
313 	if (af == AF_INET) {
314 		struct ip *ip = mtod(m, struct ip *);
315 
316 		ip->ip_len = ntohs(ip->ip_len);
317 		ip->ip_off = ntohs(ip->ip_off);
318 	}
319 #endif /* INET */
320 
321 	return (0);
322 }
323 
324 static int
325 pflog_modevent(module_t mod, int type, void *data)
326 {
327 	int error = 0;
328 
329 	struct pflog_softc *pflogif, *tmp;
330 
331 	lwkt_gettoken(&pf_token);
332 
333 	switch (type) {
334 	case MOD_LOAD:
335 		LIST_INIT(&pflogif_list);
336 		lwkt_reltoken(&pf_token);
337 		if_clone_attach(&pflog_cloner);
338 		lwkt_gettoken(&pf_token);
339 		break;
340 
341 	case MOD_UNLOAD:
342 		lwkt_reltoken(&pf_token);
343 		if_clone_detach(&pflog_cloner);
344 		LIST_FOREACH_MUTABLE(pflogif, &pflogif_list, sc_list, tmp) {
345 			pflog_clone_destroy(&pflogif->sc_if);
346 		}
347 		lwkt_gettoken(&pf_token);
348 		break;
349 
350 	default:
351 		error = EINVAL;
352 		break;
353 	}
354 	lwkt_reltoken(&pf_token);
355 	return error;
356 }
357 
358 static moduledata_t pflog_mod = {
359 	"pflog",
360 	pflog_modevent,
361 	0
362 };
363 
364 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
365 MODULE_VERSION(pflog, PFLOG_MODVER);
366