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