xref: /freebsd/sys/netpfil/pf/if_pflog.c (revision a10cee30c94cf5944826d2a495e9cdf339dfbcc8)
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 
92 IFC_SIMPLE_DECLARE(pflog, 1);
93 
94 struct ifnet	*pflogifs[PFLOGIFS_MAX];	/* for fast access */
95 
96 static void
97 pflogattach(int npflog)
98 {
99 	int	i;
100 	for (i = 0; i < PFLOGIFS_MAX; i++)
101 		pflogifs[i] = NULL;
102 	if_clone_attach(&pflog_cloner);
103 }
104 
105 static int
106 pflog_clone_create(struct if_clone *ifc, int unit, caddr_t param)
107 {
108 	struct ifnet *ifp;
109 
110 	if (unit >= PFLOGIFS_MAX)
111 		return (EINVAL);
112 
113 	ifp = if_alloc(IFT_PFLOG);
114 	if (ifp == NULL) {
115 		return (ENOSPC);
116 	}
117 	if_initname(ifp, ifc->ifc_name, unit);
118 	ifp->if_mtu = PFLOGMTU;
119 	ifp->if_ioctl = pflogioctl;
120 	ifp->if_output = pflogoutput;
121 	ifp->if_start = pflogstart;
122 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
123 	ifp->if_hdrlen = PFLOG_HDRLEN;
124 	if_attach(ifp);
125 
126 	bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
127 
128 	pflogifs[unit] = ifp;
129 
130 	return (0);
131 }
132 
133 static void
134 pflog_clone_destroy(struct ifnet *ifp)
135 {
136 	int i;
137 
138 	for (i = 0; i < PFLOGIFS_MAX; i++)
139 		if (pflogifs[i] == ifp)
140 			pflogifs[i] = NULL;
141 
142 	bpfdetach(ifp);
143 	if_detach(ifp);
144 	if_free(ifp);
145 }
146 
147 /*
148  * Start output on the pflog interface.
149  */
150 static void
151 pflogstart(struct ifnet *ifp)
152 {
153 	struct mbuf *m;
154 
155 	for (;;) {
156 		IF_LOCK(&ifp->if_snd);
157 		_IF_DROP(&ifp->if_snd);
158 		_IF_DEQUEUE(&ifp->if_snd, m);
159 		IF_UNLOCK(&ifp->if_snd);
160 
161 		if (m == NULL)
162 			return;
163 		else
164 			m_freem(m);
165 	}
166 }
167 
168 static int
169 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
170 	struct route *rt)
171 {
172 	m_freem(m);
173 	return (0);
174 }
175 
176 /* ARGSUSED */
177 static int
178 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
179 {
180 	switch (cmd) {
181 	case SIOCSIFFLAGS:
182 		if (ifp->if_flags & IFF_UP)
183 			ifp->if_drv_flags |= IFF_DRV_RUNNING;
184 		else
185 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
186 		break;
187 	default:
188 		return (ENOTTY);
189 	}
190 
191 	return (0);
192 }
193 
194 static int
195 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
196     u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
197     struct pf_ruleset *ruleset, struct pf_pdesc *pd, int lookupsafe)
198 {
199 	struct ifnet *ifn;
200 	struct pfloghdr hdr;
201 
202 	if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
203 		return ( 1);
204 
205 	if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
206 		return (0);
207 
208 	bzero(&hdr, sizeof(hdr));
209 	hdr.length = PFLOG_REAL_HDRLEN;
210 	hdr.af = af;
211 	hdr.action = rm->action;
212 	hdr.reason = reason;
213 	memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
214 
215 	if (am == NULL) {
216 		hdr.rulenr = htonl(rm->nr);
217 		hdr.subrulenr =  1;
218 	} else {
219 		hdr.rulenr = htonl(am->nr);
220 		hdr.subrulenr = htonl(rm->nr);
221 		if (ruleset != NULL && ruleset->anchor != NULL)
222 			strlcpy(hdr.ruleset, ruleset->anchor->name,
223 			    sizeof(hdr.ruleset));
224 	}
225 	/*
226 	 * XXXGL: we avoid pf_socket_lookup() when we are holding
227 	 * state lock, since this leads to unsafe LOR.
228 	 * These conditions are very very rare, however.
229 	 */
230 	if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done && lookupsafe)
231 		pd->lookup.done = pf_socket_lookup(dir, pd, m);
232 	if (pd->lookup.done > 0)
233 		hdr.uid = pd->lookup.uid;
234 	else
235 		hdr.uid = UID_MAX;
236 	hdr.pid = NO_PID;
237 	hdr.rule_uid = rm->cuid;
238 	hdr.rule_pid = rm->cpid;
239 	hdr.dir = dir;
240 
241 #ifdef INET
242 	if (af == AF_INET && dir == PF_OUT) {
243 		struct ip *ip;
244 
245 		ip = mtod(m, struct ip *);
246 		ip->ip_sum = 0;
247 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
248 	}
249 #endif /* INET */
250 
251 	ifn->if_opackets++;
252 	ifn->if_obytes += m->m_pkthdr.len;
253 	BPF_MTAP2(ifn, &hdr, PFLOG_HDRLEN, m);
254 
255 	return (0);
256 }
257 
258 static int
259 pflog_modevent(module_t mod, int type, void *data)
260 {
261 	int error = 0;
262 
263 	switch (type) {
264 	case MOD_LOAD:
265 		pflogattach(1);
266 		PF_RULES_WLOCK();
267 		pflog_packet_ptr = pflog_packet;
268 		PF_RULES_WUNLOCK();
269 		break;
270 	case MOD_UNLOAD:
271 		PF_RULES_WLOCK();
272 		pflog_packet_ptr = NULL;
273 		PF_RULES_WUNLOCK();
274 		if_clone_detach(&pflog_cloner);
275 		break;
276 	default:
277 		error = EINVAL;
278 		break;
279 	}
280 
281 	return error;
282 }
283 
284 static moduledata_t pflog_mod = { "pflog", pflog_modevent, NULL };
285 
286 #define PFLOG_MODVER 1
287 
288 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
289 MODULE_VERSION(pflog, PFLOG_MODVER);
290 MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER);
291