xref: /dragonfly/sys/net/pf/if_pflog.c (revision 7b0266d8)
1 /*	$FreeBSD: src/sys/contrib/pf/net/if_pflog.c,v 1.9 2004/06/22 20:13:24 brooks Exp $	*/
2 /*	$OpenBSD: if_pflog.c,v 1.11 2003/12/31 11:18:25 cedric Exp $	*/
3 /*	$DragonFly: src/sys/net/pf/if_pflog.c,v 1.2 2004/11/14 17:27:31 joerg Exp $ */
4 
5 /*
6  * The authors of this code are John Ioannidis (ji@tla.org),
7  * Angelos D. Keromytis (kermit@csd.uch.gr) and
8  * Niels Provos (provos@physnet.uni-hamburg.de).
9  *
10  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
11  * in November 1995.
12  *
13  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
14  * by Angelos D. Keromytis.
15  *
16  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
17  * and Niels Provos.
18  *
19  * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
20  * and Niels Provos.
21  * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
22  *
23  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
24  *
25  * Permission to use, copy, and modify this software with or without fee
26  * is hereby granted, provided that this entire notice is included in
27  * all copies of any software which is or includes a copy or
28  * modification of this software.
29  * You may use this code under the GNU public license if you so wish. Please
30  * contribute changes back to the authors under this freer than GPL license
31  * so that we may further the use of strong encryption without limitations to
32  * all.
33  *
34  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
35  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
36  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
37  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
38  * PURPOSE.
39  */
40 
41 #include "opt_inet.h"
42 #include "opt_inet6.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/in_cksum.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/sockio.h>
53 #include <vm/vm_zone.h>
54 
55 #include <net/if.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
58 #include <net/bpf.h>
59 
60 #ifdef	INET
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #endif
66 
67 #ifdef INET6
68 #ifndef INET
69 #include <netinet/in.h>
70 #endif
71 #include <netinet6/nd6.h>
72 #endif /* INET6 */
73 
74 #include <net/pf/pfvar.h>
75 #include <net/pf/if_pflog.h>
76 
77 #define	PFLOGNAME	"pflog"
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 
88 static void	pflog_clone_destroy(struct ifnet *);
89 static int	pflog_clone_create(struct if_clone *, int);
90 int	pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
91 	    	       struct rtentry *);
92 int	pflogioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
93 void	pflogrtrequest(int, struct rtentry *, struct sockaddr *);
94 void	pflogstart(struct ifnet *);
95 
96 static MALLOC_DEFINE(M_PFLOG, PFLOGNAME, "Packet Filter Logging Interface");
97 static LIST_HEAD(pflog_list, pflog_softc) pflog_list;
98 struct if_clone pflog_cloner = IF_CLONE_INITIALIZER("pflog", pflog_clone_create,
99 	    pflog_clone_destroy, 1, 1);
100 
101 static void
102 pflog_clone_destroy(struct ifnet *ifp)
103 {
104 	struct pflog_softc *sc;
105 
106 	sc = ifp->if_softc;
107 
108 	/*
109 	 * Do we really need this?
110 	 */
111 	IF_DRAIN(&ifp->if_snd);
112 
113 	bpfdetach(ifp);
114 	if_detach(ifp);
115 	LIST_REMOVE(sc, sc_next);
116 	free(sc, M_PFLOG);
117 }
118 
119 static int
120 pflog_clone_create(struct if_clone *ifc, int unit)
121 {
122 	struct pflog_softc *sc;
123 
124 	MALLOC(sc, struct pflog_softc *, sizeof(*sc), M_PFLOG, M_WAITOK|M_ZERO);
125 
126 	if_initname(&sc->sc_if, ifc->ifc_name, unit);
127         sc->sc_if.if_mtu = PFLOGMTU;
128         sc->sc_if.if_ioctl = pflogioctl;
129         sc->sc_if.if_output = pflogoutput;
130         sc->sc_if.if_start = pflogstart;
131         sc->sc_if.if_type = IFT_PFLOG;
132         sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
133         sc->sc_if.if_hdrlen = PFLOG_HDRLEN;
134         sc->sc_if.if_softc = sc;
135         if_attach(&sc->sc_if);
136 
137         LIST_INSERT_HEAD(&pflog_list, sc, sc_next);
138 	bpfattach(&sc->sc_if, DLT_PFLOG, PFLOG_HDRLEN);
139 
140         return (0);
141 }
142 
143 /*
144  * Start output on the pflog interface.
145  */
146 void
147 pflogstart(struct ifnet *ifp)
148 {
149 	int s;
150 
151 	s = splimp();
152 	IF_DROP(&ifp->if_snd);
153 	IF_DRAIN(&ifp->if_snd);
154 	splx(s);
155 }
156 
157 int
158 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
159 	struct rtentry *rt)
160 {
161 	m_freem(m);
162 	return (0);
163 }
164 
165 /* ARGSUSED */
166 void
167 pflogrtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
168 {
169 	if (rt)
170 		rt->rt_rmx.rmx_mtu = PFLOGMTU;
171 }
172 
173 /* ARGSUSED */
174 int
175 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
176 {
177 	switch (cmd) {
178 	case SIOCSIFADDR:
179 	case SIOCAIFADDR:
180 	case SIOCSIFDSTADDR:
181 	case SIOCSIFFLAGS:
182 		if (ifp->if_flags & IFF_UP)
183 			ifp->if_flags |= IFF_RUNNING;
184 		else
185 			ifp->if_flags &= ~IFF_RUNNING;
186 		break;
187 	default:
188 		return (EINVAL);
189 	}
190 
191 	return (0);
192 }
193 
194 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)
198 {
199 	struct ifnet *ifn;
200 	struct pfloghdr hdr;
201 	struct mbuf m1;
202 
203 	if (kif == NULL || m == NULL || rm == NULL)
204 		return (-1);
205 
206 	bzero(&hdr, sizeof(hdr));
207 	hdr.length = PFLOG_REAL_HDRLEN;
208 	hdr.af = af;
209 	hdr.action = rm->action;
210 	hdr.reason = reason;
211 	memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
212 
213 	if (am == NULL) {
214 		hdr.rulenr = htonl(rm->nr);
215 		hdr.subrulenr = -1;
216 	} else {
217 		hdr.rulenr = htonl(am->nr);
218 		hdr.subrulenr = htonl(rm->nr);
219 		if (ruleset != NULL)
220 			memcpy(hdr.ruleset, ruleset->name,
221 			    sizeof(hdr.ruleset));
222 
223 
224 	}
225 	hdr.dir = dir;
226 
227 #ifdef INET
228 	if (af == AF_INET) {
229 		struct ip *ip = mtod(m, struct ip *);
230 
231 		ip->ip_len = htons(ip->ip_len);
232 		ip->ip_off = htons(ip->ip_off);
233 
234 		if (dir == PF_OUT) {
235 			ip->ip_sum = 0;
236 			ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
237 		}
238 	}
239 #endif /* INET */
240 
241 	m1.m_next = m;
242 	m1.m_len = PFLOG_HDRLEN;
243 	m1.m_data = (char *) &hdr;
244 
245 	KASSERT((!LIST_EMPTY(&pflog_list)), ("pflog: no interface"));
246 	ifn = &LIST_FIRST(&pflog_list)->sc_if;
247 
248 	BPF_MTAP(ifn, &m1);
249 
250 #ifdef INET
251 	if (af == AF_INET) {
252 		struct ip *ip = mtod(m, struct ip *);
253 
254 		ip->ip_len = ntohs(ip->ip_len);
255 		ip->ip_off = ntohs(ip->ip_off);
256 	}
257 #endif /* INET */
258 
259 	return (0);
260 }
261 
262 static int
263 pflog_modevent(module_t mod, int type, void *data)
264 {
265 	int error = 0;
266 
267 	switch (type) {
268 	case MOD_LOAD:
269 		LIST_INIT(&pflog_list);
270 		if_clone_attach(&pflog_cloner);
271 		break;
272 
273 	case MOD_UNLOAD:
274 		if_clone_detach(&pflog_cloner);
275 		while (!LIST_EMPTY(&pflog_list))
276 			pflog_clone_destroy(
277 				&LIST_FIRST(&pflog_list)->sc_if);
278 		break;
279 
280 	default:
281 		error = EINVAL;
282 		break;
283 	}
284 
285 	return error;
286 }
287 
288 static moduledata_t pflog_mod = {
289 	"pflog",
290 	pflog_modevent,
291 	0
292 };
293 
294 #define PFLOG_MODVER 1
295 
296 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
297 MODULE_VERSION(pflog, PFLOG_MODVER);
298