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