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