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