if_pflog.c revision 1.11.12.2 1 1.11.12.2 yamt /* $NetBSD: if_pflog.c,v 1.11.12.2 2009/08/19 18:47:33 yamt Exp $ */
2 1.11.12.1 yamt /* $OpenBSD: if_pflog.c,v 1.24 2007/05/26 17:13:30 jason Exp $ */
3 1.11.12.1 yamt
4 1.1 itojun /*
5 1.1 itojun * The authors of this code are John Ioannidis (ji (at) tla.org),
6 1.1 itojun * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
7 1.1 itojun * Niels Provos (provos (at) physnet.uni-hamburg.de).
8 1.1 itojun *
9 1.1 itojun * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
10 1.1 itojun * in November 1995.
11 1.1 itojun *
12 1.1 itojun * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
13 1.1 itojun * by Angelos D. Keromytis.
14 1.1 itojun *
15 1.1 itojun * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
16 1.1 itojun * and Niels Provos.
17 1.1 itojun *
18 1.1 itojun * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
19 1.1 itojun * and Niels Provos.
20 1.1 itojun * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
21 1.1 itojun *
22 1.1 itojun * Permission to use, copy, and modify this software with or without fee
23 1.1 itojun * is hereby granted, provided that this entire notice is included in
24 1.1 itojun * all copies of any software which is or includes a copy or
25 1.1 itojun * modification of this software.
26 1.1 itojun * You may use this code under the GNU public license if you so wish. Please
27 1.1 itojun * contribute changes back to the authors under this freer than GPL license
28 1.1 itojun * so that we may further the use of strong encryption without limitations to
29 1.1 itojun * all.
30 1.1 itojun *
31 1.1 itojun * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
32 1.1 itojun * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
33 1.1 itojun * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
34 1.1 itojun * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
35 1.1 itojun * PURPOSE.
36 1.1 itojun */
37 1.1 itojun
38 1.11 lukem #include <sys/cdefs.h>
39 1.11.12.2 yamt __KERNEL_RCSID(0, "$NetBSD: if_pflog.c,v 1.11.12.2 2009/08/19 18:47:33 yamt Exp $");
40 1.11 lukem
41 1.2 itojun #ifdef _KERNEL_OPT
42 1.2 itojun #include "opt_inet.h"
43 1.2 itojun #endif
44 1.2 itojun
45 1.1 itojun #include "bpfilter.h"
46 1.1 itojun #include "pflog.h"
47 1.1 itojun
48 1.1 itojun #include <sys/param.h>
49 1.1 itojun #include <sys/systm.h>
50 1.1 itojun #include <sys/mbuf.h>
51 1.11.12.1 yamt #include <sys/proc.h>
52 1.1 itojun #include <sys/socket.h>
53 1.1 itojun #include <sys/ioctl.h>
54 1.1 itojun
55 1.1 itojun #include <net/if.h>
56 1.1 itojun #include <net/if_types.h>
57 1.1 itojun #include <net/route.h>
58 1.1 itojun #include <net/bpf.h>
59 1.1 itojun
60 1.1 itojun #ifdef INET
61 1.1 itojun #include <netinet/in.h>
62 1.1 itojun #include <netinet/in_var.h>
63 1.1 itojun #include <netinet/in_systm.h>
64 1.1 itojun #include <netinet/ip.h>
65 1.1 itojun #endif
66 1.1 itojun
67 1.1 itojun #ifdef INET6
68 1.1 itojun #ifndef INET
69 1.1 itojun #include <netinet/in.h>
70 1.1 itojun #endif
71 1.1 itojun #include <netinet6/nd6.h>
72 1.1 itojun #endif /* INET6 */
73 1.1 itojun
74 1.1 itojun #include <net/pfvar.h>
75 1.1 itojun #include <net/if_pflog.h>
76 1.1 itojun
77 1.1 itojun #define PFLOGMTU (32768 + MHLEN + MLEN)
78 1.1 itojun
79 1.1 itojun #ifdef PFLOGDEBUG
80 1.1 itojun #define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0)
81 1.1 itojun #else
82 1.1 itojun #define DPRINTF(x)
83 1.1 itojun #endif
84 1.1 itojun
85 1.1 itojun void pflogattach(int);
86 1.9 dyoung int pflogoutput(struct ifnet *, struct mbuf *, const struct sockaddr *,
87 1.1 itojun struct rtentry *);
88 1.10 christos int pflogioctl(struct ifnet *, u_long, void *);
89 1.1 itojun void pflogstart(struct ifnet *);
90 1.11.12.1 yamt int pflog_clone_create(struct if_clone *, int);
91 1.11.12.1 yamt int pflog_clone_destroy(struct ifnet *);
92 1.11.12.1 yamt
93 1.11.12.1 yamt LIST_HEAD(, pflog_softc) pflogif_list;
94 1.11.12.1 yamt struct if_clone pflog_cloner =
95 1.11.12.1 yamt IF_CLONE_INITIALIZER("pflog", pflog_clone_create, pflog_clone_destroy);
96 1.1 itojun
97 1.11.12.1 yamt struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */
98 1.1 itojun
99 1.1 itojun void
100 1.8 christos pflogattach(int npflog)
101 1.1 itojun {
102 1.1 itojun int i;
103 1.1 itojun
104 1.11.12.1 yamt LIST_INIT(&pflogif_list);
105 1.11.12.1 yamt for (i = 0; i < PFLOGIFS_MAX; i++)
106 1.11.12.1 yamt pflogifs[i] = NULL;
107 1.11.12.1 yamt if_clone_attach(&pflog_cloner);
108 1.1 itojun }
109 1.1 itojun
110 1.11.12.1 yamt int
111 1.11.12.1 yamt pflog_clone_create(struct if_clone *ifc, int unit)
112 1.11.12.1 yamt {
113 1.11.12.1 yamt struct ifnet *ifp;
114 1.11.12.1 yamt struct pflog_softc *pflogif;
115 1.11.12.1 yamt int s;
116 1.11.12.1 yamt
117 1.11.12.1 yamt if (unit >= PFLOGIFS_MAX)
118 1.11.12.1 yamt return (EINVAL);
119 1.11.12.1 yamt
120 1.11.12.1 yamt if ((pflogif = malloc(sizeof(*pflogif), M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
121 1.11.12.1 yamt return (ENOMEM);
122 1.11.12.1 yamt
123 1.11.12.1 yamt pflogif->sc_unit = unit;
124 1.11.12.1 yamt ifp = &pflogif->sc_if;
125 1.11.12.1 yamt snprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", unit);
126 1.11.12.1 yamt ifp->if_softc = pflogif;
127 1.11.12.1 yamt ifp->if_mtu = PFLOGMTU;
128 1.11.12.1 yamt ifp->if_ioctl = pflogioctl;
129 1.11.12.1 yamt ifp->if_output = pflogoutput;
130 1.11.12.1 yamt ifp->if_start = pflogstart;
131 1.11.12.1 yamt ifp->if_type = IFT_PFLOG;
132 1.11.12.1 yamt #ifndef __NetBSD__
133 1.11.12.1 yamt ifp->if_snd.ifq_maxlen = ifqmaxlen;
134 1.11.12.1 yamt #endif /* !__NetBSD__ */
135 1.11.12.1 yamt ifp->if_hdrlen = PFLOG_HDRLEN;
136 1.11.12.1 yamt if_attach(ifp);
137 1.11.12.1 yamt if_alloc_sadl(ifp);
138 1.11.12.1 yamt
139 1.11.12.1 yamt #if NBPFILTER > 0
140 1.11.12.1 yamt #ifdef __NetBSD__
141 1.11.12.1 yamt bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
142 1.11.12.1 yamt #else
143 1.11.12.1 yamt bpfattach(&pflogif->sc_if.if_bpf, ifp, DLT_PFLOG, PFLOG_HDRLEN);
144 1.11.12.1 yamt #endif /* !__NetBSD__ */
145 1.11.12.1 yamt #endif
146 1.11.12.1 yamt
147 1.11.12.1 yamt s = splnet();
148 1.11.12.1 yamt LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list);
149 1.11.12.1 yamt pflogifs[unit] = ifp;
150 1.11.12.1 yamt splx(s);
151 1.11.12.1 yamt
152 1.11.12.1 yamt return (0);
153 1.3 itojun }
154 1.11.12.1 yamt
155 1.11.12.1 yamt int
156 1.11.12.1 yamt pflog_clone_destroy(struct ifnet *ifp)
157 1.11.12.1 yamt {
158 1.11.12.1 yamt struct pflog_softc *pflogif = ifp->if_softc;
159 1.11.12.1 yamt int s;
160 1.11.12.1 yamt
161 1.11.12.1 yamt s = splnet();
162 1.11.12.1 yamt pflogifs[pflogif->sc_unit] = NULL;
163 1.11.12.1 yamt LIST_REMOVE(pflogif, sc_list);
164 1.11.12.1 yamt splx(s);
165 1.11.12.1 yamt
166 1.11.12.1 yamt #if NBPFILTER > 0
167 1.11.12.1 yamt bpfdetach(ifp);
168 1.3 itojun #endif
169 1.11.12.1 yamt if_detach(ifp);
170 1.11.12.1 yamt free(pflogif, M_DEVBUF);
171 1.11.12.1 yamt return (0);
172 1.11.12.1 yamt }
173 1.3 itojun
174 1.1 itojun /*
175 1.1 itojun * Start output on the pflog interface.
176 1.1 itojun */
177 1.1 itojun void
178 1.1 itojun pflogstart(struct ifnet *ifp)
179 1.1 itojun {
180 1.1 itojun struct mbuf *m;
181 1.1 itojun int s;
182 1.1 itojun
183 1.1 itojun for (;;) {
184 1.2 itojun s = splnet();
185 1.1 itojun IF_DROP(&ifp->if_snd);
186 1.1 itojun IF_DEQUEUE(&ifp->if_snd, m);
187 1.1 itojun splx(s);
188 1.1 itojun
189 1.1 itojun if (m == NULL)
190 1.1 itojun return;
191 1.1 itojun else
192 1.1 itojun m_freem(m);
193 1.1 itojun }
194 1.1 itojun }
195 1.1 itojun
196 1.1 itojun int
197 1.8 christos pflogoutput(struct ifnet *ifp, struct mbuf *m,
198 1.9 dyoung const struct sockaddr *dst, struct rtentry *rt)
199 1.1 itojun {
200 1.1 itojun m_freem(m);
201 1.1 itojun return (0);
202 1.1 itojun }
203 1.1 itojun
204 1.1 itojun /* ARGSUSED */
205 1.1 itojun int
206 1.10 christos pflogioctl(struct ifnet *ifp, u_long cmd, void *data)
207 1.1 itojun {
208 1.11.12.1 yamt int error = 0;
209 1.11.12.1 yamt
210 1.1 itojun switch (cmd) {
211 1.11.12.1 yamt case SIOCSIFFLAGS:
212 1.11.12.1 yamt if ((error = ifioctl_common(ifp, cmd, data)) != 0)
213 1.11.12.1 yamt break;
214 1.11.12.1 yamt /*FALLTHROUGH*/
215 1.11.12.1 yamt case SIOCINITIFADDR:
216 1.1 itojun case SIOCAIFADDR:
217 1.1 itojun case SIOCSIFDSTADDR:
218 1.1 itojun if (ifp->if_flags & IFF_UP)
219 1.1 itojun ifp->if_flags |= IFF_RUNNING;
220 1.1 itojun else
221 1.1 itojun ifp->if_flags &= ~IFF_RUNNING;
222 1.1 itojun break;
223 1.1 itojun default:
224 1.11.12.1 yamt error = ifioctl_common(ifp, cmd, data);
225 1.1 itojun }
226 1.1 itojun
227 1.11.12.1 yamt return error;
228 1.1 itojun }
229 1.1 itojun
230 1.1 itojun int
231 1.1 itojun pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
232 1.1 itojun u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
233 1.11.12.1 yamt struct pf_ruleset *ruleset, struct pf_pdesc *pd)
234 1.1 itojun {
235 1.1 itojun #if NBPFILTER > 0
236 1.1 itojun struct ifnet *ifn;
237 1.1 itojun struct pfloghdr hdr;
238 1.1 itojun
239 1.11.12.1 yamt if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
240 1.1 itojun return (-1);
241 1.1 itojun
242 1.11.12.1 yamt if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
243 1.11.12.1 yamt return (0);
244 1.11.12.1 yamt
245 1.1 itojun bzero(&hdr, sizeof(hdr));
246 1.1 itojun hdr.length = PFLOG_REAL_HDRLEN;
247 1.1 itojun hdr.af = af;
248 1.1 itojun hdr.action = rm->action;
249 1.1 itojun hdr.reason = reason;
250 1.1 itojun memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
251 1.1 itojun
252 1.1 itojun if (am == NULL) {
253 1.1 itojun hdr.rulenr = htonl(rm->nr);
254 1.1 itojun hdr.subrulenr = -1;
255 1.1 itojun } else {
256 1.1 itojun hdr.rulenr = htonl(am->nr);
257 1.1 itojun hdr.subrulenr = htonl(rm->nr);
258 1.5 yamt if (ruleset != NULL && ruleset->anchor != NULL)
259 1.5 yamt strlcpy(hdr.ruleset, ruleset->anchor->name,
260 1.1 itojun sizeof(hdr.ruleset));
261 1.1 itojun }
262 1.11.12.1 yamt if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done)
263 1.11.12.1 yamt pd->lookup.done = pf_socket_lookup(dir, pd);
264 1.11.12.1 yamt if (pd->lookup.done > 0) {
265 1.11.12.1 yamt hdr.uid = pd->lookup.uid;
266 1.11.12.1 yamt hdr.pid = pd->lookup.pid;
267 1.11.12.1 yamt } else {
268 1.11.12.1 yamt hdr.uid = UID_MAX;
269 1.11.12.1 yamt hdr.pid = NO_PID;
270 1.11.12.1 yamt }
271 1.11.12.1 yamt hdr.rule_uid = rm->cuid;
272 1.11.12.1 yamt hdr.rule_pid = rm->cpid;
273 1.1 itojun hdr.dir = dir;
274 1.1 itojun
275 1.1 itojun #ifdef INET
276 1.1 itojun if (af == AF_INET && dir == PF_OUT) {
277 1.1 itojun struct ip *ip;
278 1.1 itojun
279 1.1 itojun ip = mtod(m, struct ip *);
280 1.1 itojun ip->ip_sum = 0;
281 1.1 itojun ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
282 1.1 itojun }
283 1.1 itojun #endif /* INET */
284 1.1 itojun
285 1.11.12.1 yamt ifn->if_opackets++;
286 1.11.12.1 yamt ifn->if_obytes += m->m_pkthdr.len;
287 1.1 itojun
288 1.11.12.1 yamt #ifdef __NetBSD__
289 1.11.12.1 yamt bpf_mtap2(ifn->if_bpf, &hdr, PFLOG_HDRLEN, m);
290 1.4 yamt #else
291 1.11.12.1 yamt bpf_mtap_hdr(ifn->if_bpf, (char *)&hdr, PFLOG_HDRLEN, m,
292 1.11.12.1 yamt BPF_DIRECTION_OUT);
293 1.11.12.1 yamt #endif /* !__NetBSD__ */
294 1.11.12.1 yamt
295 1.1 itojun #endif
296 1.1 itojun
297 1.1 itojun return (0);
298 1.1 itojun }
299