xref: /openbsd-src/sys/net/if_pflog.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: if_pflog.c,v 1.74 2016/04/29 08:55:03 krw Exp $	*/
2 /*
3  * The authors of this code are John Ioannidis (ji@tla.org),
4  * Angelos D. Keromytis (kermit@csd.uch.gr) and
5  * Niels Provos (provos@physnet.uni-hamburg.de).
6  *
7  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
8  * in November 1995.
9  *
10  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
11  * by Angelos D. Keromytis.
12  *
13  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
14  * and Niels Provos.
15  *
16  * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
17  * and Niels Provos.
18  * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
19  * Copyright (c) 2002 - 2010 Henning Brauer
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 
37 #include "bpfilter.h"
38 #include "pflog.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/proc.h>
44 #include <sys/socket.h>
45 #include <sys/stdint.h>
46 #include <sys/ioctl.h>
47 
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/if_types.h>
51 #include <net/bpf.h>
52 
53 #include <netinet/in.h>
54 #include <netinet/ip.h>
55 #include <netinet/tcp.h>
56 #include <netinet/udp.h>
57 #include <netinet/ip_icmp.h>
58 
59 #ifdef INET6
60 #include <netinet/ip6.h>
61 #include <netinet/icmp6.h>
62 #endif /* INET6 */
63 
64 #include <net/pfvar.h>
65 #include <net/if_pflog.h>
66 
67 #define PFLOGMTU	(32768 + MHLEN + MLEN)
68 
69 #ifdef PFLOGDEBUG
70 #define DPRINTF(x)    do { if (pflogdebug) printf x ; } while (0)
71 #else
72 #define DPRINTF(x)
73 #endif
74 
75 void	pflogattach(int);
76 int	pflogifs_resize(size_t);
77 int	pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
78 	    	       struct rtentry *);
79 int	pflogioctl(struct ifnet *, u_long, caddr_t);
80 void	pflogstart(struct ifnet *);
81 int	pflog_clone_create(struct if_clone *, int);
82 int	pflog_clone_destroy(struct ifnet *);
83 void	pflog_bpfcopy(const void *, void *, size_t);
84 
85 LIST_HEAD(, pflog_softc)	pflogif_list;
86 struct if_clone	pflog_cloner =
87     IF_CLONE_INITIALIZER("pflog", pflog_clone_create, pflog_clone_destroy);
88 
89 int		  npflogifs = 0;
90 struct ifnet	**pflogifs = NULL;	/* for fast access */
91 struct mbuf	 *pflog_mhdr = NULL, *pflog_mptr = NULL;
92 
93 void
94 pflogattach(int npflog)
95 {
96 	LIST_INIT(&pflogif_list);
97 	if (pflog_mhdr == NULL)
98 		if ((pflog_mhdr = m_get(M_DONTWAIT, MT_HEADER)) == NULL)
99 			panic("pflogattach: no mbuf");
100 	if (pflog_mptr == NULL)
101 		if ((pflog_mptr = m_get(M_DONTWAIT, MT_DATA)) == NULL)
102 			panic("pflogattach: no mbuf");
103 	if_clone_attach(&pflog_cloner);
104 }
105 
106 int
107 pflogifs_resize(size_t n)
108 {
109 	struct ifnet	**p;
110 	int		  i;
111 
112 	if (n > SIZE_MAX / sizeof(*p))
113 		return (EINVAL);
114 	if (n == 0)
115 		p = NULL;
116 	else
117 		if ((p = mallocarray(n, sizeof(*p), M_DEVBUF,
118 		    M_NOWAIT|M_ZERO)) == NULL)
119 			return (ENOMEM);
120 	for (i = 0; i < n; i++)
121 		if (i < npflogifs)
122 			p[i] = pflogifs[i];
123 		else
124 			p[i] = NULL;
125 
126 	if (pflogifs)
127 		free(pflogifs, M_DEVBUF, 0);
128 	pflogifs = p;
129 	npflogifs = n;
130 	return (0);
131 }
132 
133 int
134 pflog_clone_create(struct if_clone *ifc, int unit)
135 {
136 	struct ifnet *ifp;
137 	struct pflog_softc *pflogif;
138 	int s;
139 
140 	if ((pflogif = malloc(sizeof(*pflogif),
141 	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
142 		return (ENOMEM);
143 
144 	pflogif->sc_unit = unit;
145 	ifp = &pflogif->sc_if;
146 	snprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", unit);
147 	ifp->if_softc = pflogif;
148 	ifp->if_mtu = PFLOGMTU;
149 	ifp->if_ioctl = pflogioctl;
150 	ifp->if_output = pflogoutput;
151 	ifp->if_start = pflogstart;
152 	ifp->if_type = IFT_PFLOG;
153 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
154 	ifp->if_hdrlen = PFLOG_HDRLEN;
155 	if_attach(ifp);
156 	if_alloc_sadl(ifp);
157 
158 #if NBPFILTER > 0
159 	bpfattach(&pflogif->sc_if.if_bpf, ifp, DLT_PFLOG, PFLOG_HDRLEN);
160 #endif
161 
162 	s = splnet();
163 	LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list);
164 	if (unit + 1 > npflogifs && pflogifs_resize(unit + 1) != 0) {
165 		splx(s);
166 		return (ENOMEM);
167 	}
168 	pflogifs[unit] = ifp;
169 	splx(s);
170 
171 	return (0);
172 }
173 
174 int
175 pflog_clone_destroy(struct ifnet *ifp)
176 {
177 	struct pflog_softc	*pflogif = ifp->if_softc;
178 	int			 s, i;
179 
180 	s = splnet();
181 	pflogifs[pflogif->sc_unit] = NULL;
182 	LIST_REMOVE(pflogif, sc_list);
183 
184 	for (i = npflogifs; i > 0 && pflogifs[i - 1] == NULL; i--)
185 		; /* nothing */
186 	if (i < npflogifs)
187 		pflogifs_resize(i);	/* error harmless here */
188 	splx(s);
189 
190 	if_detach(ifp);
191 	free(pflogif, M_DEVBUF, 0);
192 	return (0);
193 }
194 
195 /*
196  * Start output on the pflog interface.
197  */
198 void
199 pflogstart(struct ifnet *ifp)
200 {
201 	IFQ_PURGE(&ifp->if_snd);
202 }
203 
204 int
205 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
206 	struct rtentry *rt)
207 {
208 	m_freem(m);	/* drop packet */
209 	return (EAFNOSUPPORT);
210 }
211 
212 int
213 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
214 {
215 	switch (cmd) {
216 	case SIOCSIFFLAGS:
217 		if (ifp->if_flags & IFF_UP)
218 			ifp->if_flags |= IFF_RUNNING;
219 		else
220 			ifp->if_flags &= ~IFF_RUNNING;
221 		break;
222 	default:
223 		return (ENOTTY);
224 	}
225 
226 	return (0);
227 }
228 
229 int
230 pflog_packet(struct pf_pdesc *pd, u_int8_t reason, struct pf_rule *rm,
231     struct pf_rule *am, struct pf_ruleset *ruleset, struct pf_rule *trigger)
232 {
233 #if NBPFILTER > 0
234 	struct ifnet *ifn;
235 	struct pfloghdr hdr;
236 
237 	if (rm == NULL || pd == NULL || pd->kif == NULL || pd->m == NULL)
238 		return (-1);
239 	if (trigger == NULL)
240 		trigger = rm;
241 
242 	if (trigger->logif >= npflogifs || (ifn = pflogifs[trigger->logif]) ==
243 	    NULL || !ifn->if_bpf)
244 		return (0);
245 
246 	bzero(&hdr, sizeof(hdr));
247 	hdr.length = PFLOG_REAL_HDRLEN;
248 	hdr.action = rm->action;
249 	hdr.reason = reason;
250 	memcpy(hdr.ifname, pd->kif->pfik_name, sizeof(hdr.ifname));
251 
252 	if (am == NULL) {
253 		hdr.rulenr = htonl(rm->nr);
254 		hdr.subrulenr = -1;
255 	} else {
256 		hdr.rulenr = htonl(am->nr);
257 		hdr.subrulenr = htonl(rm->nr);
258 		if (ruleset != NULL && ruleset->anchor != NULL)
259 			strlcpy(hdr.ruleset, ruleset->anchor->name,
260 			    sizeof(hdr.ruleset));
261 	}
262 	if (trigger->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done)
263 		pd->lookup.done = pf_socket_lookup(pd);
264 	if (pd->lookup.done > 0) {
265 		hdr.uid = pd->lookup.uid;
266 		hdr.pid = pd->lookup.pid;
267 	} else {
268 		hdr.uid = UID_MAX;
269 		hdr.pid = NO_PID;
270 	}
271 	hdr.rule_uid = rm->cuid;
272 	hdr.rule_pid = rm->cpid;
273 	hdr.dir = pd->dir;
274 
275 	PF_ACPY(&hdr.saddr, &pd->nsaddr, pd->naf);
276 	PF_ACPY(&hdr.daddr, &pd->ndaddr, pd->naf);
277 	hdr.af = pd->af;
278 	hdr.naf = pd->naf;
279 	hdr.sport = pd->nsport;
280 	hdr.dport = pd->ndport;
281 
282 	ifn->if_opackets++;
283 	ifn->if_obytes += pd->m->m_pkthdr.len;
284 
285 	bpf_mtap_hdr(ifn->if_bpf, (caddr_t)&hdr, PFLOG_HDRLEN, pd->m,
286 	    BPF_DIRECTION_OUT, pflog_bpfcopy);
287 #endif
288 
289 	return (0);
290 }
291 
292 void
293 pflog_bpfcopy(const void *src_arg, void *dst_arg, size_t len)
294 {
295 	struct mbuf		*m, *mp, *mhdr, *mptr;
296 	struct pfloghdr		*pfloghdr;
297 	u_int			 count;
298 	u_char			*dst, *mdst;
299 	int			 afto, hlen, mlen, off;
300 	union pf_headers {
301 		struct tcphdr		tcp;
302 		struct udphdr		udp;
303 		struct icmp		icmp;
304 #ifdef INET6
305 		struct icmp6_hdr	icmp6;
306 		struct mld_hdr		mld;
307 		struct nd_neighbor_solicit nd_ns;
308 #endif /* INET6 */
309 	} pdhdrs;
310 
311 	struct pf_pdesc		 pd;
312 	struct pf_addr		 osaddr, odaddr;
313 	u_int16_t		 osport = 0, odport = 0;
314 	u_int8_t		 proto = 0;
315 
316 	m = (struct mbuf *)src_arg;
317 	dst = dst_arg;
318 
319 	mhdr = pflog_mhdr;
320 	mptr = pflog_mptr;
321 
322 	if (m == NULL)
323 		panic("pflog_bpfcopy got no mbuf");
324 
325 	/* first mbuf holds struct pfloghdr */
326 	pfloghdr = mtod(m, struct pfloghdr *);
327 	afto = pfloghdr->af != pfloghdr->naf;
328 	count = min(m->m_len, len);
329 	bcopy(pfloghdr, dst, count);
330 	pfloghdr = (struct pfloghdr *)dst;
331 	dst += count;
332 	len -= count;
333 	m = m->m_next;
334 
335 	if (len <= 0)
336 		return;
337 
338 	/* second mbuf is pkthdr */
339 	if (m == NULL)
340 		panic("no second mbuf");
341 
342 	/*
343 	 * temporary mbuf will hold an ip/ip6 header and 8 bytes
344 	 * of the protocol header
345 	 */
346 	m_inithdr(mhdr);
347 	mhdr->m_len = 0;	/* XXX not done in m_inithdr() */
348 
349 #ifdef INET6
350 	/* offset for a new header */
351 	if (afto && pfloghdr->af == AF_INET)
352 		mhdr->m_data += sizeof(struct ip6_hdr) -
353 		    sizeof(struct ip);
354 #endif /* INET6 */
355 
356 	mdst = mtod(mhdr, char *);
357 	switch (pfloghdr->af) {
358 	case AF_INET: {
359 		struct ip	*h;
360 
361 		if (m->m_pkthdr.len < sizeof(*h))
362 			goto copy;
363 		m_copydata(m, 0, sizeof(*h), mdst);
364 		h = (struct ip *)mdst;
365 		hlen = h->ip_hl << 2;
366 		if (hlen > sizeof(*h) && (m->m_pkthdr.len >= hlen))
367 			m_copydata(m, sizeof(*h), hlen - sizeof(*h),
368 			    mdst + sizeof(*h));
369 		break;
370 	    }
371 #ifdef INET6
372 	case AF_INET6: {
373 		struct ip6_hdr	*h;
374 
375 		if (m->m_pkthdr.len < sizeof(*h))
376 			goto copy;
377 		hlen = sizeof(struct ip6_hdr);
378 		m_copydata(m, 0, hlen, mdst);
379 		h = (struct ip6_hdr *)mdst;
380 		proto = h->ip6_nxt;
381 		break;
382 	    }
383 #endif /* INET6 */
384 	default:
385 		/* shouldn't happen ever :-) */
386 		goto copy;
387 	}
388 
389 	if (m->m_pkthdr.len < hlen + 8 && proto != IPPROTO_NONE)
390 		goto copy;
391 	else if (proto != IPPROTO_NONE) {
392 		/* copy 8 bytes of the protocol header */
393 		m_copydata(m, hlen, 8, mdst + hlen);
394 		hlen += 8;
395 	}
396 
397 	mhdr->m_len += hlen;
398 	mhdr->m_pkthdr.len = mhdr->m_len;
399 
400 	/* create a chain mhdr -> mptr, mptr->m_data = (m->m_data+hlen) */
401 	mp = m_getptr(m, hlen, &off);
402 	if (mp != NULL) {
403 		bcopy(mp, mptr, sizeof(*mptr));
404 		mptr->m_data += off;
405 		mptr->m_len -= off;
406 		mptr->m_flags &= ~M_PKTHDR;
407 		mhdr->m_next = mptr;
408 		mhdr->m_pkthdr.len += m->m_pkthdr.len - hlen;
409 	}
410 
411 	/*
412 	 * Rewrite addresses if needed. Reason pointer must be NULL to avoid
413 	 * counting the packet here again.
414 	 */
415 	if (pf_setup_pdesc(&pd, &pdhdrs, pfloghdr->af, pfloghdr->dir, NULL,
416 	    mhdr, NULL) != PF_PASS)
417 		goto copy;
418 	pd.naf = pfloghdr->naf;
419 
420 	PF_ACPY(&osaddr, pd.src, pd.af);
421 	PF_ACPY(&odaddr, pd.dst, pd.af);
422 	if (pd.sport)
423 		osport = *pd.sport;
424 	if (pd.dport)
425 		odport = *pd.dport;
426 
427 	if (pd.virtual_proto != PF_VPROTO_FRAGMENT &&
428 	    (pfloghdr->rewritten = pf_translate(&pd, &pfloghdr->saddr,
429 	    pfloghdr->sport, &pfloghdr->daddr, pfloghdr->dport, 0,
430 	    pfloghdr->dir))) {
431 		m_copyback(pd.m, pd.off, min(pd.m->m_len - pd.off, pd.hdrlen),
432 		    pd.hdr.any, M_NOWAIT);
433 #ifdef INET6
434 		if (afto) {
435 			PF_ACPY(&pd.nsaddr, &pfloghdr->saddr, pd.naf);
436 			PF_ACPY(&pd.ndaddr, &pfloghdr->daddr, pd.naf);
437 		}
438 #endif /* INET6 */
439 		PF_ACPY(&pfloghdr->saddr, &osaddr, pd.af);
440 		PF_ACPY(&pfloghdr->daddr, &odaddr, pd.af);
441 		pfloghdr->sport = osport;
442 		pfloghdr->dport = odport;
443 	}
444 
445 	pd.tot_len = min(pd.tot_len, len);
446 	pd.tot_len -= pd.m->m_data - pd.m->m_pktdat;
447 
448 #ifdef INET6
449 	if (afto && pfloghdr->rewritten)
450 		pf_translate_af(&pd);
451 #endif /* INET6 */
452 
453 	m = pd.m;
454  copy:
455 	mlen = min(m->m_pkthdr.len, len);
456 	m_copydata(m, 0, mlen, dst);
457 	len -= mlen;
458 	if (len > 0)
459 		bzero(dst + mlen, len);
460 }
461