xref: /freebsd/sys/netpfil/pf/if_pflog.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * The authors of this code are John Ioannidis (ji@tla.org),
5  * Angelos D. Keromytis (kermit@csd.uch.gr) and
6  * Niels Provos (provos@physnet.uni-hamburg.de).
7  *
8  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
9  * in November 1995.
10  *
11  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12  * by Angelos D. Keromytis.
13  *
14  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15  * and Niels Provos.
16  *
17  * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
18  * and Niels Provos.
19  * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
20  *
21  * Permission to use, copy, and modify this software with or without fee
22  * is hereby granted, provided that this entire notice is included in
23  * all copies of any software which is or includes a copy or
24  * modification of this software.
25  * You may use this code under the GNU public license if you so wish. Please
26  * contribute changes back to the authors under this freer than GPL license
27  * so that we may further the use of strong encryption without limitations to
28  * all.
29  *
30  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
31  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
32  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
33  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
34  * PURPOSE.
35  *
36  *	$OpenBSD: if_pflog.c,v 1.26 2007/10/18 21:58:18 mpf Exp $
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
44 #include "opt_bpf.h"
45 #include "opt_pf.h"
46 
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/mbuf.h>
50 #include <sys/module.h>
51 #include <sys/proc.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 
55 #include <net/bpf.h>
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_clone.h>
59 #include <net/if_pflog.h>
60 #include <net/if_private.h>
61 #include <net/if_types.h>
62 #include <net/vnet.h>
63 #include <net/pfvar.h>
64 
65 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
67 #endif
68 #ifdef	INET
69 #include <netinet/in_var.h>
70 #include <netinet/ip.h>
71 #endif
72 
73 #ifdef INET6
74 #include <netinet6/in6_var.h>
75 #include <netinet6/nd6.h>
76 #endif /* INET6 */
77 
78 #ifdef INET
79 #include <machine/in_cksum.h>
80 #endif /* INET */
81 
82 #define PFLOGMTU	(32768 + MHLEN + MLEN)
83 
84 #ifdef PFLOGDEBUG
85 #define DPRINTF(x)    do { if (pflogdebug) printf x ; } while (0)
86 #else
87 #define DPRINTF(x)
88 #endif
89 
90 static int	pflogoutput(struct ifnet *, struct mbuf *,
91 		    const struct sockaddr *, struct route *);
92 static void	pflogattach(int);
93 static int	pflogioctl(struct ifnet *, u_long, caddr_t);
94 static void	pflogstart(struct ifnet *);
95 static int	pflog_clone_create(struct if_clone *, char *, size_t,
96 		    struct ifc_data *, struct ifnet **);
97 static int	pflog_clone_destroy(struct if_clone *, struct ifnet *, uint32_t);
98 
99 static const char pflogname[] = "pflog";
100 
101 VNET_DEFINE_STATIC(struct if_clone *, pflog_cloner);
102 #define	V_pflog_cloner		VNET(pflog_cloner)
103 
104 VNET_DEFINE(struct ifnet *, pflogifs[PFLOGIFS_MAX]);	/* for fast access */
105 #define	V_pflogifs		VNET(pflogifs)
106 
107 static void
108 pflogattach(int npflog __unused)
109 {
110 	int	i;
111 	for (i = 0; i < PFLOGIFS_MAX; i++)
112 		V_pflogifs[i] = NULL;
113 
114 	struct if_clone_addreq req = {
115 		.create_f = pflog_clone_create,
116 		.destroy_f = pflog_clone_destroy,
117 		.flags = IFC_F_AUTOUNIT,
118 	};
119 	V_pflog_cloner = ifc_attach_cloner(pflogname, &req);
120 	struct ifc_data ifd = { .unit = 0 };
121 	ifc_create_ifp(pflogname, &ifd, NULL);
122 }
123 
124 static int
125 pflog_clone_create(struct if_clone *ifc, char *name, size_t maxlen,
126     struct ifc_data *ifd, struct ifnet **ifpp)
127 {
128 	struct ifnet *ifp;
129 
130 	if (ifd->unit >= PFLOGIFS_MAX)
131 		return (EINVAL);
132 
133 	ifp = if_alloc(IFT_PFLOG);
134 	if (ifp == NULL) {
135 		return (ENOSPC);
136 	}
137 	if_initname(ifp, pflogname, ifd->unit);
138 	ifp->if_mtu = PFLOGMTU;
139 	ifp->if_ioctl = pflogioctl;
140 	ifp->if_output = pflogoutput;
141 	ifp->if_start = pflogstart;
142 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
143 	ifp->if_hdrlen = PFLOG_HDRLEN;
144 	if_attach(ifp);
145 
146 	bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
147 
148 	V_pflogifs[ifd->unit] = ifp;
149 	*ifpp = ifp;
150 
151 	return (0);
152 }
153 
154 static int
155 pflog_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
156 {
157 	int i;
158 
159 	if (ifp->if_dunit == 0 && (flags & IFC_F_FORCE) == 0)
160 		return (EINVAL);
161 
162 	for (i = 0; i < PFLOGIFS_MAX; i++)
163 		if (V_pflogifs[i] == ifp)
164 			V_pflogifs[i] = NULL;
165 
166 	bpfdetach(ifp);
167 	if_detach(ifp);
168 	if_free(ifp);
169 
170 	return (0);
171 }
172 
173 /*
174  * Start output on the pflog interface.
175  */
176 static void
177 pflogstart(struct ifnet *ifp)
178 {
179 	struct mbuf *m;
180 
181 	for (;;) {
182 		IF_LOCK(&ifp->if_snd);
183 		_IF_DEQUEUE(&ifp->if_snd, m);
184 		IF_UNLOCK(&ifp->if_snd);
185 
186 		if (m == NULL)
187 			return;
188 		else
189 			m_freem(m);
190 	}
191 }
192 
193 static int
194 pflogoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
195 	struct route *rt)
196 {
197 	m_freem(m);
198 	return (0);
199 }
200 
201 /* ARGSUSED */
202 static int
203 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
204 {
205 	switch (cmd) {
206 	case SIOCSIFFLAGS:
207 		if (ifp->if_flags & IFF_UP)
208 			ifp->if_drv_flags |= IFF_DRV_RUNNING;
209 		else
210 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
211 		break;
212 	default:
213 		return (ENOTTY);
214 	}
215 
216 	return (0);
217 }
218 
219 static int
220 pflog_packet(struct pfi_kkif *kif, struct mbuf *m, sa_family_t af,
221     u_int8_t reason, struct pf_krule *rm, struct pf_krule *am,
222     struct pf_kruleset *ruleset, struct pf_pdesc *pd, int lookupsafe)
223 {
224 	struct ifnet *ifn;
225 	struct pfloghdr hdr;
226 
227 	if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
228 		return ( 1);
229 
230 	if ((ifn = V_pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
231 		return (0);
232 
233 	bzero(&hdr, sizeof(hdr));
234 	hdr.length = PFLOG_REAL_HDRLEN;
235 	hdr.af = af;
236 	hdr.action = rm->action;
237 	hdr.reason = reason;
238 	memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
239 
240 	if (am == NULL) {
241 		hdr.rulenr = htonl(rm->nr);
242 		hdr.subrulenr = -1;
243 	} else {
244 		hdr.rulenr = htonl(am->nr);
245 		hdr.subrulenr = htonl(rm->nr);
246 		if (ruleset != NULL && ruleset->anchor != NULL)
247 			strlcpy(hdr.ruleset, ruleset->anchor->name,
248 			    sizeof(hdr.ruleset));
249 	}
250 	hdr.ridentifier = htonl(rm->ridentifier);
251 	/*
252 	 * XXXGL: we avoid pf_socket_lookup() when we are holding
253 	 * state lock, since this leads to unsafe LOR.
254 	 * These conditions are very very rare, however.
255 	 */
256 	if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done && lookupsafe)
257 		pd->lookup.done = pf_socket_lookup(pd, m);
258 	if (pd->lookup.done > 0)
259 		hdr.uid = pd->lookup.uid;
260 	else
261 		hdr.uid = UID_MAX;
262 	hdr.pid = NO_PID;
263 	hdr.rule_uid = rm->cuid;
264 	hdr.rule_pid = rm->cpid;
265 	hdr.dir = pd->dir;
266 
267 #ifdef INET
268 	if (af == AF_INET && pd->dir == PF_OUT) {
269 		struct ip *ip;
270 
271 		ip = mtod(m, struct ip *);
272 		ip->ip_sum = 0;
273 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
274 	}
275 #endif /* INET */
276 
277 	if_inc_counter(ifn, IFCOUNTER_OPACKETS, 1);
278 	if_inc_counter(ifn, IFCOUNTER_OBYTES, m->m_pkthdr.len);
279 	BPF_MTAP2(ifn, &hdr, PFLOG_HDRLEN, m);
280 
281 	return (0);
282 }
283 
284 static void
285 vnet_pflog_init(const void *unused __unused)
286 {
287 
288 	pflogattach(1);
289 }
290 VNET_SYSINIT(vnet_pflog_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY,
291     vnet_pflog_init, NULL);
292 
293 static void
294 vnet_pflog_uninit(const void *unused __unused)
295 {
296 
297 	ifc_detach_cloner(V_pflog_cloner);
298 }
299 /*
300  * Detach after pf is gone; otherwise we might touch pflog memory
301  * from within pf after freeing pflog.
302  */
303 VNET_SYSUNINIT(vnet_pflog_uninit, SI_SUB_INIT_IF, SI_ORDER_SECOND,
304     vnet_pflog_uninit, NULL);
305 
306 static int
307 pflog_modevent(module_t mod, int type, void *data)
308 {
309 	int error = 0;
310 
311 	switch (type) {
312 	case MOD_LOAD:
313 		PF_RULES_WLOCK();
314 		pflog_packet_ptr = pflog_packet;
315 		PF_RULES_WUNLOCK();
316 		break;
317 	case MOD_UNLOAD:
318 		PF_RULES_WLOCK();
319 		pflog_packet_ptr = NULL;
320 		PF_RULES_WUNLOCK();
321 		break;
322 	default:
323 		error = EOPNOTSUPP;
324 		break;
325 	}
326 
327 	return error;
328 }
329 
330 static moduledata_t pflog_mod = { pflogname, pflog_modevent, 0 };
331 
332 #define PFLOG_MODVER 1
333 
334 /* Do not run before pf is initialized as we depend on its locks. */
335 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY);
336 MODULE_VERSION(pflog, PFLOG_MODVER);
337 MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER);
338