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#include "opt_inet.h"
41#include "opt_inet6.h"
42#include "opt_bpf.h"
43#include "opt_pf.h"
44
45#include <sys/param.h>
46#include <sys/kernel.h>
47#include <sys/mbuf.h>
48#include <sys/module.h>
49#include <sys/proc.h>
50#include <sys/socket.h>
51#include <sys/sockio.h>
52
53#include <net/bpf.h>
54#include <net/if.h>
55#include <net/if_var.h>
56#include <net/if_clone.h>
57#include <net/if_pflog.h>
58#include <net/if_private.h>
59#include <net/if_types.h>
60#include <net/vnet.h>
61#include <net/pfvar.h>
62
63#if defined(INET) || defined(INET6)
64#include <netinet/in.h>
65#endif
66#ifdef	INET
67#include <netinet/in_var.h>
68#include <netinet/ip.h>
69#endif
70
71#ifdef INET6
72#include <netinet6/in6_var.h>
73#include <netinet6/nd6.h>
74#endif /* INET6 */
75
76#ifdef INET
77#include <machine/in_cksum.h>
78#endif /* INET */
79
80#define PFLOGMTU	(32768 + MHLEN + MLEN)
81
82#ifdef PFLOGDEBUG
83#define DPRINTF(x)    do { if (pflogdebug) printf x ; } while (0)
84#else
85#define DPRINTF(x)
86#endif
87
88static int	pflogoutput(struct ifnet *, struct mbuf *,
89		    const struct sockaddr *, struct route *);
90static void	pflogattach(int);
91static int	pflogioctl(struct ifnet *, u_long, caddr_t);
92static void	pflogstart(struct ifnet *);
93static int	pflog_clone_create(struct if_clone *, char *, size_t,
94		    struct ifc_data *, struct ifnet **);
95static int	pflog_clone_destroy(struct if_clone *, struct ifnet *, uint32_t);
96
97static const char pflogname[] = "pflog";
98
99VNET_DEFINE_STATIC(struct if_clone *, pflog_cloner);
100#define	V_pflog_cloner		VNET(pflog_cloner)
101
102VNET_DEFINE(struct ifnet *, pflogifs[PFLOGIFS_MAX]);	/* for fast access */
103#define	V_pflogifs		VNET(pflogifs)
104
105static void
106pflogattach(int npflog __unused)
107{
108	int	i;
109	for (i = 0; i < PFLOGIFS_MAX; i++)
110		V_pflogifs[i] = NULL;
111
112	struct if_clone_addreq req = {
113		.create_f = pflog_clone_create,
114		.destroy_f = pflog_clone_destroy,
115		.flags = IFC_F_AUTOUNIT,
116	};
117	V_pflog_cloner = ifc_attach_cloner(pflogname, &req);
118	struct ifc_data ifd = { .unit = 0 };
119	ifc_create_ifp(pflogname, &ifd, NULL);
120}
121
122static int
123pflog_clone_create(struct if_clone *ifc, char *name, size_t maxlen,
124    struct ifc_data *ifd, struct ifnet **ifpp)
125{
126	struct ifnet *ifp;
127
128	if (ifd->unit >= PFLOGIFS_MAX)
129		return (EINVAL);
130
131	ifp = if_alloc(IFT_PFLOG);
132	if (ifp == NULL) {
133		return (ENOSPC);
134	}
135	if_initname(ifp, pflogname, ifd->unit);
136	ifp->if_mtu = PFLOGMTU;
137	ifp->if_ioctl = pflogioctl;
138	ifp->if_output = pflogoutput;
139	ifp->if_start = pflogstart;
140	ifp->if_snd.ifq_maxlen = ifqmaxlen;
141	ifp->if_hdrlen = PFLOG_HDRLEN;
142	if_attach(ifp);
143
144	bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
145
146	V_pflogifs[ifd->unit] = ifp;
147	*ifpp = ifp;
148
149	return (0);
150}
151
152static int
153pflog_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
154{
155	int i;
156
157	if (ifp->if_dunit == 0 && (flags & IFC_F_FORCE) == 0)
158		return (EINVAL);
159
160	for (i = 0; i < PFLOGIFS_MAX; i++)
161		if (V_pflogifs[i] == ifp)
162			V_pflogifs[i] = NULL;
163
164	bpfdetach(ifp);
165	if_detach(ifp);
166	if_free(ifp);
167
168	return (0);
169}
170
171/*
172 * Start output on the pflog interface.
173 */
174static void
175pflogstart(struct ifnet *ifp)
176{
177	struct mbuf *m;
178
179	for (;;) {
180		IF_LOCK(&ifp->if_snd);
181		_IF_DEQUEUE(&ifp->if_snd, m);
182		IF_UNLOCK(&ifp->if_snd);
183
184		if (m == NULL)
185			return;
186		else
187			m_freem(m);
188	}
189}
190
191static int
192pflogoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
193	struct route *rt)
194{
195	m_freem(m);
196	return (0);
197}
198
199/* ARGSUSED */
200static int
201pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
202{
203	switch (cmd) {
204	case SIOCSIFFLAGS:
205		if (ifp->if_flags & IFF_UP)
206			ifp->if_drv_flags |= IFF_DRV_RUNNING;
207		else
208			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
209		break;
210	default:
211		return (ENOTTY);
212	}
213
214	return (0);
215}
216
217static int
218pflog_packet(struct pfi_kkif *kif, struct mbuf *m, sa_family_t af,
219    uint8_t action, u_int8_t reason, struct pf_krule *rm, struct pf_krule *am,
220    struct pf_kruleset *ruleset, struct pf_pdesc *pd, int lookupsafe)
221{
222	struct ifnet *ifn;
223	struct pfloghdr hdr;
224
225	if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
226		return ( 1);
227
228	if ((ifn = V_pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
229		return (0);
230
231	bzero(&hdr, sizeof(hdr));
232	hdr.length = PFLOG_REAL_HDRLEN;
233	hdr.af = af;
234	hdr.action = action;
235	hdr.reason = reason;
236	memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
237
238	if (am == NULL) {
239		hdr.rulenr = htonl(rm->nr);
240		hdr.subrulenr = -1;
241	} else {
242		hdr.rulenr = htonl(am->nr);
243		hdr.subrulenr = htonl(rm->nr);
244		if (ruleset != NULL && ruleset->anchor != NULL)
245			strlcpy(hdr.ruleset, ruleset->anchor->name,
246			    sizeof(hdr.ruleset));
247	}
248	hdr.ridentifier = htonl(rm->ridentifier);
249	/*
250	 * XXXGL: we avoid pf_socket_lookup() when we are holding
251	 * state lock, since this leads to unsafe LOR.
252	 * These conditions are very very rare, however.
253	 */
254	if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done && lookupsafe)
255		pd->lookup.done = pf_socket_lookup(pd, m);
256	if (pd->lookup.done > 0)
257		hdr.uid = pd->lookup.uid;
258	else
259		hdr.uid = UID_MAX;
260	hdr.pid = NO_PID;
261	hdr.rule_uid = rm->cuid;
262	hdr.rule_pid = rm->cpid;
263	hdr.dir = pd->dir;
264
265#ifdef INET
266	if (af == AF_INET && pd->dir == PF_OUT) {
267		struct ip *ip;
268
269		ip = mtod(m, struct ip *);
270		ip->ip_sum = 0;
271		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
272	}
273#endif /* INET */
274
275	if_inc_counter(ifn, IFCOUNTER_OPACKETS, 1);
276	if_inc_counter(ifn, IFCOUNTER_OBYTES, m->m_pkthdr.len);
277	BPF_MTAP2(ifn, &hdr, PFLOG_HDRLEN, m);
278
279	return (0);
280}
281
282static void
283vnet_pflog_init(const void *unused __unused)
284{
285
286	pflogattach(1);
287}
288VNET_SYSINIT(vnet_pflog_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY,
289    vnet_pflog_init, NULL);
290
291static void
292vnet_pflog_uninit(const void *unused __unused)
293{
294
295	ifc_detach_cloner(V_pflog_cloner);
296}
297/*
298 * Detach after pf is gone; otherwise we might touch pflog memory
299 * from within pf after freeing pflog.
300 */
301VNET_SYSUNINIT(vnet_pflog_uninit, SI_SUB_INIT_IF, SI_ORDER_SECOND,
302    vnet_pflog_uninit, NULL);
303
304static int
305pflog_modevent(module_t mod, int type, void *data)
306{
307	int error = 0;
308
309	switch (type) {
310	case MOD_LOAD:
311		PF_RULES_WLOCK();
312		pflog_packet_ptr = pflog_packet;
313		PF_RULES_WUNLOCK();
314		break;
315	case MOD_UNLOAD:
316		PF_RULES_WLOCK();
317		pflog_packet_ptr = NULL;
318		PF_RULES_WUNLOCK();
319		break;
320	default:
321		error = EOPNOTSUPP;
322		break;
323	}
324
325	return error;
326}
327
328static moduledata_t pflog_mod = { pflogname, pflog_modevent, 0 };
329
330#define PFLOG_MODVER 1
331
332/* Do not run before pf is initialized as we depend on its locks. */
333DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY);
334MODULE_VERSION(pflog, PFLOG_MODVER);
335MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER);
336