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: releng/11.0/sys/netpfil/pf/if_pflog.c 302159 2016-06-23 22:31:10Z bz $");
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_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 static VNET_DEFINE(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
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
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 (ifp == NULL) {
123 return (ENOSPC);
124 }
125 if_initname(ifp, pflogname, unit);
126 ifp->if_mtu = PFLOGMTU;
127 ifp->if_ioctl = pflogioctl;
128 ifp->if_output = pflogoutput;
129 ifp->if_start = pflogstart;
130 ifp->if_snd.ifq_maxlen = ifqmaxlen;
131 ifp->if_hdrlen = PFLOG_HDRLEN;
132 if_attach(ifp);
133
134 bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
135
136 V_pflogifs[unit] = ifp;
137
138 return (0);
139 }
140
141 static void
142 pflog_clone_destroy(struct ifnet *ifp)
143 {
144 int i;
145
146 for (i = 0; i < PFLOGIFS_MAX; i++)
147 if (V_pflogifs[i] == ifp)
148 V_pflogifs[i] = NULL;
149
150 bpfdetach(ifp);
151 if_detach(ifp);
152 if_free(ifp);
153 }
154
155 /*
156 * Start output on the pflog interface.
157 */
158 static void
159 pflogstart(struct ifnet *ifp)
160 {
161 struct mbuf *m;
162
163 for (;;) {
164 IF_LOCK(&ifp->if_snd);
165 _IF_DEQUEUE(&ifp->if_snd, m);
166 IF_UNLOCK(&ifp->if_snd);
167
168 if (m == NULL)
169 return;
170 else
171 m_freem(m);
172 }
173 }
174
175 static int
176 pflogoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
177 struct route *rt)
178 {
179 m_freem(m);
180 return (0);
181 }
182
183 /* ARGSUSED */
184 static int
185 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
186 {
187 switch (cmd) {
188 case SIOCSIFFLAGS:
189 if (ifp->if_flags & IFF_UP)
190 ifp->if_drv_flags |= IFF_DRV_RUNNING;
191 else
192 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
193 break;
194 default:
195 return (ENOTTY);
196 }
197
198 return (0);
199 }
200
201 static int
202 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
203 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
204 struct pf_ruleset *ruleset, struct pf_pdesc *pd, int lookupsafe)
205 {
206 struct ifnet *ifn;
207 struct pfloghdr hdr;
208
209 if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
210 return ( 1);
211
212 if ((ifn = V_pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
213 return (0);
214
215 bzero(&hdr, sizeof(hdr));
216 hdr.length = PFLOG_REAL_HDRLEN;
217 hdr.af = af;
218 hdr.action = rm->action;
219 hdr.reason = reason;
220 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
221
222 if (am == NULL) {
223 hdr.rulenr = htonl(rm->nr);
224 hdr.subrulenr = 1;
225 } else {
226 hdr.rulenr = htonl(am->nr);
227 hdr.subrulenr = htonl(rm->nr);
228 if (ruleset != NULL && ruleset->anchor != NULL)
229 strlcpy(hdr.ruleset, ruleset->anchor->name,
230 sizeof(hdr.ruleset));
231 }
232 /*
233 * XXXGL: we avoid pf_socket_lookup() when we are holding
234 * state lock, since this leads to unsafe LOR.
235 * These conditions are very very rare, however.
236 */
237 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done && lookupsafe)
238 pd->lookup.done = pf_socket_lookup(dir, pd, m);
239 if (pd->lookup.done > 0)
240 hdr.uid = pd->lookup.uid;
241 else
242 hdr.uid = UID_MAX;
243 hdr.pid = NO_PID;
244 hdr.rule_uid = rm->cuid;
245 hdr.rule_pid = rm->cpid;
246 hdr.dir = dir;
247
248 #ifdef INET
249 if (af == AF_INET && dir == PF_OUT) {
250 struct ip *ip;
251
252 ip = mtod(m, struct ip *);
253 ip->ip_sum = 0;
254 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
255 }
256 #endif /* INET */
257
258 if_inc_counter(ifn, IFCOUNTER_OPACKETS, 1);
259 if_inc_counter(ifn, IFCOUNTER_OBYTES, m->m_pkthdr.len);
260 BPF_MTAP2(ifn, &hdr, PFLOG_HDRLEN, m);
261
262 return (0);
263 }
264
265 static void
266 vnet_pflog_init(const void *unused __unused)
267 {
268
269 pflogattach(1);
270 }
271 VNET_SYSINIT(vnet_pflog_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY,
272 vnet_pflog_init, NULL);
273
274 static void
275 vnet_pflog_uninit(const void *unused __unused)
276 {
277
278 if_clone_detach(V_pflog_cloner);
279 }
280 /*
281 * Detach after pf is gone; otherwise we might touch pflog memory
282 * from within pf after freeing pflog.
283 */
284 VNET_SYSUNINIT(vnet_pflog_uninit, SI_SUB_INIT_IF, SI_ORDER_SECOND,
285 vnet_pflog_uninit, NULL);
286
287 static int
288 pflog_modevent(module_t mod, int type, void *data)
289 {
290 int error = 0;
291
292 switch (type) {
293 case MOD_LOAD:
294 PF_RULES_WLOCK();
295 pflog_packet_ptr = pflog_packet;
296 PF_RULES_WUNLOCK();
297 break;
298 case MOD_UNLOAD:
299 PF_RULES_WLOCK();
300 pflog_packet_ptr = NULL;
301 PF_RULES_WUNLOCK();
302 break;
303 default:
304 error = EOPNOTSUPP;
305 break;
306 }
307
308 return error;
309 }
310
311 static moduledata_t pflog_mod = { pflogname, pflog_modevent, 0 };
312
313 #define PFLOG_MODVER 1
314
315 /* Do not run before pf is initialized as we depend on its locks. */
316 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY);
317 MODULE_VERSION(pflog, PFLOG_MODVER);
318 MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER);
Cache object: edb0f83e4828c5051400319bf181e7f6
|