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