Home | History | Annotate | Line # | Download | only in net
if_pflog.c revision 1.2
      1  1.2  itojun /*	$NetBSD: if_pflog.c,v 1.2 2004/06/22 14:17:07 itojun Exp $	*/
      2  1.1  itojun /*	$OpenBSD: if_pflog.c,v 1.11 2003/12/31 11:18:25 cedric 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.1  itojun int	pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
     84  1.1  itojun 	    	       struct rtentry *);
     85  1.1  itojun int	pflogioctl(struct ifnet *, u_long, caddr_t);
     86  1.1  itojun void	pflogrtrequest(int, struct rtentry *, struct sockaddr *);
     87  1.1  itojun void	pflogstart(struct ifnet *);
     88  1.1  itojun 
     89  1.1  itojun extern int ifqmaxlen;
     90  1.1  itojun 
     91  1.1  itojun void
     92  1.1  itojun pflogattach(int npflog)
     93  1.1  itojun {
     94  1.1  itojun 	struct ifnet *ifp;
     95  1.1  itojun 	int i;
     96  1.1  itojun 
     97  1.1  itojun 	bzero(pflogif, sizeof(pflogif));
     98  1.1  itojun 
     99  1.1  itojun 	for (i = 0; i < NPFLOG; i++) {
    100  1.1  itojun 		ifp = &pflogif[i].sc_if;
    101  1.1  itojun 		snprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", i);
    102  1.1  itojun 		ifp->if_softc = &pflogif[i];
    103  1.1  itojun 		ifp->if_mtu = PFLOGMTU;
    104  1.1  itojun 		ifp->if_ioctl = pflogioctl;
    105  1.1  itojun 		ifp->if_output = pflogoutput;
    106  1.1  itojun 		ifp->if_start = pflogstart;
    107  1.1  itojun 		ifp->if_type = IFT_PFLOG;
    108  1.1  itojun 		ifp->if_snd.ifq_maxlen = ifqmaxlen;
    109  1.1  itojun 		ifp->if_hdrlen = PFLOG_HDRLEN;
    110  1.1  itojun 		if_attach(ifp);
    111  1.1  itojun 		if_alloc_sadl(ifp);
    112  1.1  itojun 
    113  1.1  itojun #if NBPFILTER > 0
    114  1.2  itojun #ifdef __OpenBSD__
    115  1.1  itojun 		bpfattach(&pflogif[i].sc_if.if_bpf, ifp, DLT_PFLOG,
    116  1.1  itojun 			  PFLOG_HDRLEN);
    117  1.2  itojun #else
    118  1.2  itojun 		bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
    119  1.2  itojun #endif
    120  1.1  itojun #endif
    121  1.1  itojun 	}
    122  1.1  itojun }
    123  1.1  itojun 
    124  1.1  itojun /*
    125  1.1  itojun  * Start output on the pflog interface.
    126  1.1  itojun  */
    127  1.1  itojun void
    128  1.1  itojun pflogstart(struct ifnet *ifp)
    129  1.1  itojun {
    130  1.1  itojun 	struct mbuf *m;
    131  1.1  itojun 	int s;
    132  1.1  itojun 
    133  1.1  itojun 	for (;;) {
    134  1.2  itojun #ifdef __OpenBSD__
    135  1.1  itojun 		s = splimp();
    136  1.2  itojun #else
    137  1.2  itojun 		s = splnet();
    138  1.2  itojun #endif
    139  1.1  itojun 		IF_DROP(&ifp->if_snd);
    140  1.1  itojun 		IF_DEQUEUE(&ifp->if_snd, m);
    141  1.1  itojun 		splx(s);
    142  1.1  itojun 
    143  1.1  itojun 		if (m == NULL)
    144  1.1  itojun 			return;
    145  1.1  itojun 		else
    146  1.1  itojun 			m_freem(m);
    147  1.1  itojun 	}
    148  1.1  itojun }
    149  1.1  itojun 
    150  1.1  itojun int
    151  1.1  itojun pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
    152  1.1  itojun 	struct rtentry *rt)
    153  1.1  itojun {
    154  1.1  itojun 	m_freem(m);
    155  1.1  itojun 	return (0);
    156  1.1  itojun }
    157  1.1  itojun 
    158  1.1  itojun /* ARGSUSED */
    159  1.1  itojun void
    160  1.1  itojun pflogrtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
    161  1.1  itojun {
    162  1.1  itojun 	if (rt)
    163  1.1  itojun 		rt->rt_rmx.rmx_mtu = PFLOGMTU;
    164  1.1  itojun }
    165  1.1  itojun 
    166  1.1  itojun /* ARGSUSED */
    167  1.1  itojun int
    168  1.1  itojun pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    169  1.1  itojun {
    170  1.1  itojun 	switch (cmd) {
    171  1.1  itojun 	case SIOCSIFADDR:
    172  1.1  itojun 	case SIOCAIFADDR:
    173  1.1  itojun 	case SIOCSIFDSTADDR:
    174  1.1  itojun 	case SIOCSIFFLAGS:
    175  1.1  itojun 		if (ifp->if_flags & IFF_UP)
    176  1.1  itojun 			ifp->if_flags |= IFF_RUNNING;
    177  1.1  itojun 		else
    178  1.1  itojun 			ifp->if_flags &= ~IFF_RUNNING;
    179  1.1  itojun 		break;
    180  1.1  itojun 	default:
    181  1.1  itojun 		return (EINVAL);
    182  1.1  itojun 	}
    183  1.1  itojun 
    184  1.1  itojun 	return (0);
    185  1.1  itojun }
    186  1.1  itojun 
    187  1.1  itojun int
    188  1.1  itojun pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
    189  1.1  itojun     u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
    190  1.1  itojun     struct pf_ruleset *ruleset)
    191  1.1  itojun {
    192  1.1  itojun #if NBPFILTER > 0
    193  1.1  itojun 	struct ifnet *ifn;
    194  1.1  itojun 	struct pfloghdr hdr;
    195  1.1  itojun 	struct mbuf m1;
    196  1.1  itojun 
    197  1.1  itojun 	if (kif == NULL || m == NULL || rm == NULL)
    198  1.1  itojun 		return (-1);
    199  1.1  itojun 
    200  1.1  itojun 	bzero(&hdr, sizeof(hdr));
    201  1.1  itojun 	hdr.length = PFLOG_REAL_HDRLEN;
    202  1.1  itojun 	hdr.af = af;
    203  1.1  itojun 	hdr.action = rm->action;
    204  1.1  itojun 	hdr.reason = reason;
    205  1.1  itojun 	memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
    206  1.1  itojun 
    207  1.1  itojun 	if (am == NULL) {
    208  1.1  itojun 		hdr.rulenr = htonl(rm->nr);
    209  1.1  itojun 		hdr.subrulenr = -1;
    210  1.1  itojun 	} else {
    211  1.1  itojun 		hdr.rulenr = htonl(am->nr);
    212  1.1  itojun 		hdr.subrulenr = htonl(rm->nr);
    213  1.1  itojun 		if (ruleset != NULL)
    214  1.1  itojun 			memcpy(hdr.ruleset, ruleset->name,
    215  1.1  itojun 			    sizeof(hdr.ruleset));
    216  1.1  itojun 
    217  1.1  itojun 
    218  1.1  itojun 	}
    219  1.1  itojun 	hdr.dir = dir;
    220  1.1  itojun 
    221  1.1  itojun #ifdef INET
    222  1.1  itojun 	if (af == AF_INET && dir == PF_OUT) {
    223  1.1  itojun 		struct ip *ip;
    224  1.1  itojun 
    225  1.1  itojun 		ip = mtod(m, struct ip *);
    226  1.1  itojun 		ip->ip_sum = 0;
    227  1.1  itojun 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
    228  1.1  itojun 	}
    229  1.1  itojun #endif /* INET */
    230  1.1  itojun 
    231  1.1  itojun 	m1.m_next = m;
    232  1.1  itojun 	m1.m_len = PFLOG_HDRLEN;
    233  1.1  itojun 	m1.m_data = (char *) &hdr;
    234  1.1  itojun 
    235  1.1  itojun 	ifn = &(pflogif[0].sc_if);
    236  1.1  itojun 
    237  1.1  itojun 	if (ifn->if_bpf)
    238  1.1  itojun 		bpf_mtap(ifn->if_bpf, &m1);
    239  1.1  itojun #endif
    240  1.1  itojun 
    241  1.1  itojun 	return (0);
    242  1.1  itojun }
    243