Home | History | Annotate | Line # | Download | only in net
if_pflog.c revision 1.11
      1  1.11     lukem /*	$NetBSD: if_pflog.c,v 1.11 2007/12/11 11:08:19 lukem 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.11     lukem #include <sys/cdefs.h>
     38  1.11     lukem __KERNEL_RCSID(0, "$NetBSD: if_pflog.c,v 1.11 2007/12/11 11:08:19 lukem Exp $");
     39  1.11     lukem 
     40   1.2    itojun #ifdef _KERNEL_OPT
     41   1.2    itojun #include "opt_inet.h"
     42   1.2    itojun #endif
     43   1.2    itojun 
     44   1.1    itojun #include "bpfilter.h"
     45   1.1    itojun #include "pflog.h"
     46   1.1    itojun 
     47   1.1    itojun #include <sys/param.h>
     48   1.1    itojun #include <sys/systm.h>
     49   1.1    itojun #include <sys/mbuf.h>
     50   1.1    itojun #include <sys/socket.h>
     51   1.1    itojun #include <sys/ioctl.h>
     52   1.1    itojun 
     53   1.1    itojun #include <net/if.h>
     54   1.1    itojun #include <net/if_types.h>
     55   1.1    itojun #include <net/route.h>
     56   1.1    itojun #include <net/bpf.h>
     57   1.1    itojun 
     58   1.1    itojun #ifdef	INET
     59   1.1    itojun #include <netinet/in.h>
     60   1.1    itojun #include <netinet/in_var.h>
     61   1.1    itojun #include <netinet/in_systm.h>
     62   1.1    itojun #include <netinet/ip.h>
     63   1.1    itojun #endif
     64   1.1    itojun 
     65   1.1    itojun #ifdef INET6
     66   1.1    itojun #ifndef INET
     67   1.1    itojun #include <netinet/in.h>
     68   1.1    itojun #endif
     69   1.1    itojun #include <netinet6/nd6.h>
     70   1.1    itojun #endif /* INET6 */
     71   1.1    itojun 
     72   1.1    itojun #include <net/pfvar.h>
     73   1.1    itojun #include <net/if_pflog.h>
     74   1.1    itojun 
     75   1.1    itojun #define PFLOGMTU	(32768 + MHLEN + MLEN)
     76   1.1    itojun 
     77   1.1    itojun #ifdef PFLOGDEBUG
     78   1.1    itojun #define DPRINTF(x)    do { if (pflogdebug) printf x ; } while (0)
     79   1.1    itojun #else
     80   1.1    itojun #define DPRINTF(x)
     81   1.1    itojun #endif
     82   1.1    itojun 
     83   1.1    itojun struct pflog_softc pflogif[NPFLOG];
     84   1.1    itojun 
     85   1.1    itojun void	pflogattach(int);
     86   1.3    itojun #ifdef _LKM
     87   1.3    itojun void	pflogdetach(void);
     88   1.3    itojun #endif
     89   1.9    dyoung int	pflogoutput(struct ifnet *, struct mbuf *, const struct sockaddr *,
     90   1.1    itojun 	    	       struct rtentry *);
     91  1.10  christos int	pflogioctl(struct ifnet *, u_long, void *);
     92   1.1    itojun void	pflogrtrequest(int, struct rtentry *, struct sockaddr *);
     93   1.1    itojun void	pflogstart(struct ifnet *);
     94   1.1    itojun 
     95   1.1    itojun extern int ifqmaxlen;
     96   1.1    itojun 
     97   1.1    itojun void
     98   1.8  christos pflogattach(int npflog)
     99   1.1    itojun {
    100   1.1    itojun 	struct ifnet *ifp;
    101   1.1    itojun 	int i;
    102   1.1    itojun 
    103   1.1    itojun 	bzero(pflogif, sizeof(pflogif));
    104   1.1    itojun 
    105   1.1    itojun 	for (i = 0; i < NPFLOG; i++) {
    106   1.1    itojun 		ifp = &pflogif[i].sc_if;
    107   1.1    itojun 		snprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", i);
    108   1.1    itojun 		ifp->if_softc = &pflogif[i];
    109   1.1    itojun 		ifp->if_mtu = PFLOGMTU;
    110   1.1    itojun 		ifp->if_ioctl = pflogioctl;
    111   1.1    itojun 		ifp->if_output = pflogoutput;
    112   1.1    itojun 		ifp->if_start = pflogstart;
    113   1.1    itojun 		ifp->if_type = IFT_PFLOG;
    114   1.1    itojun 		ifp->if_snd.ifq_maxlen = ifqmaxlen;
    115   1.1    itojun 		ifp->if_hdrlen = PFLOG_HDRLEN;
    116   1.1    itojun 		if_attach(ifp);
    117   1.1    itojun 		if_alloc_sadl(ifp);
    118   1.1    itojun 
    119   1.1    itojun #if NBPFILTER > 0
    120   1.2    itojun #ifdef __OpenBSD__
    121   1.1    itojun 		bpfattach(&pflogif[i].sc_if.if_bpf, ifp, DLT_PFLOG,
    122   1.1    itojun 			  PFLOG_HDRLEN);
    123   1.2    itojun #else
    124   1.2    itojun 		bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
    125   1.2    itojun #endif
    126   1.1    itojun #endif
    127   1.1    itojun 	}
    128   1.1    itojun }
    129   1.1    itojun 
    130   1.3    itojun #ifdef _LKM
    131   1.3    itojun void
    132   1.3    itojun pflogdetach(void)
    133   1.3    itojun {
    134   1.3    itojun 	struct ifnet *ifp;
    135   1.3    itojun 	int i;
    136   1.3    itojun 
    137   1.3    itojun 	for (i = 0; i < NPFLOG; i++) {
    138   1.3    itojun 		ifp = &pflogif[i].sc_if;
    139   1.3    itojun 		bpfdetach(ifp);
    140   1.3    itojun 		if_detach(ifp);
    141   1.3    itojun 	}
    142   1.3    itojun }
    143   1.3    itojun #endif
    144   1.3    itojun 
    145   1.1    itojun /*
    146   1.1    itojun  * Start output on the pflog interface.
    147   1.1    itojun  */
    148   1.1    itojun void
    149   1.1    itojun pflogstart(struct ifnet *ifp)
    150   1.1    itojun {
    151   1.1    itojun 	struct mbuf *m;
    152   1.1    itojun 	int s;
    153   1.1    itojun 
    154   1.1    itojun 	for (;;) {
    155   1.2    itojun #ifdef __OpenBSD__
    156   1.1    itojun 		s = splimp();
    157   1.2    itojun #else
    158   1.2    itojun 		s = splnet();
    159   1.2    itojun #endif
    160   1.1    itojun 		IF_DROP(&ifp->if_snd);
    161   1.1    itojun 		IF_DEQUEUE(&ifp->if_snd, m);
    162   1.1    itojun 		splx(s);
    163   1.1    itojun 
    164   1.1    itojun 		if (m == NULL)
    165   1.1    itojun 			return;
    166   1.1    itojun 		else
    167   1.1    itojun 			m_freem(m);
    168   1.1    itojun 	}
    169   1.1    itojun }
    170   1.1    itojun 
    171   1.1    itojun int
    172   1.8  christos pflogoutput(struct ifnet *ifp, struct mbuf *m,
    173   1.9    dyoung     const struct sockaddr *dst, struct rtentry *rt)
    174   1.1    itojun {
    175   1.1    itojun 	m_freem(m);
    176   1.1    itojun 	return (0);
    177   1.1    itojun }
    178   1.1    itojun 
    179   1.1    itojun /* ARGSUSED */
    180   1.1    itojun void
    181   1.8  christos pflogrtrequest(int cmd, struct rtentry *rt,
    182   1.8  christos     struct sockaddr *sa)
    183   1.1    itojun {
    184   1.1    itojun 	if (rt)
    185   1.1    itojun 		rt->rt_rmx.rmx_mtu = PFLOGMTU;
    186   1.1    itojun }
    187   1.1    itojun 
    188   1.1    itojun /* ARGSUSED */
    189   1.1    itojun int
    190  1.10  christos pflogioctl(struct ifnet *ifp, u_long cmd, void *data)
    191   1.1    itojun {
    192   1.1    itojun 	switch (cmd) {
    193   1.1    itojun 	case SIOCSIFADDR:
    194   1.1    itojun 	case SIOCAIFADDR:
    195   1.1    itojun 	case SIOCSIFDSTADDR:
    196   1.1    itojun 	case SIOCSIFFLAGS:
    197   1.1    itojun 		if (ifp->if_flags & IFF_UP)
    198   1.1    itojun 			ifp->if_flags |= IFF_RUNNING;
    199   1.1    itojun 		else
    200   1.1    itojun 			ifp->if_flags &= ~IFF_RUNNING;
    201   1.1    itojun 		break;
    202   1.1    itojun 	default:
    203   1.1    itojun 		return (EINVAL);
    204   1.1    itojun 	}
    205   1.1    itojun 
    206   1.1    itojun 	return (0);
    207   1.1    itojun }
    208   1.1    itojun 
    209   1.1    itojun int
    210   1.1    itojun pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
    211   1.1    itojun     u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
    212   1.1    itojun     struct pf_ruleset *ruleset)
    213   1.1    itojun {
    214   1.1    itojun #if NBPFILTER > 0
    215   1.1    itojun 	struct ifnet *ifn;
    216   1.1    itojun 	struct pfloghdr hdr;
    217   1.4      yamt #ifndef __NetBSD__
    218   1.1    itojun 	struct mbuf m1;
    219   1.4      yamt #endif
    220   1.1    itojun 
    221   1.1    itojun 	if (kif == NULL || m == NULL || rm == NULL)
    222   1.1    itojun 		return (-1);
    223   1.1    itojun 
    224   1.1    itojun 	bzero(&hdr, sizeof(hdr));
    225   1.1    itojun 	hdr.length = PFLOG_REAL_HDRLEN;
    226   1.1    itojun 	hdr.af = af;
    227   1.1    itojun 	hdr.action = rm->action;
    228   1.1    itojun 	hdr.reason = reason;
    229   1.1    itojun 	memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
    230   1.1    itojun 
    231   1.1    itojun 	if (am == NULL) {
    232   1.1    itojun 		hdr.rulenr = htonl(rm->nr);
    233   1.1    itojun 		hdr.subrulenr = -1;
    234   1.1    itojun 	} else {
    235   1.1    itojun 		hdr.rulenr = htonl(am->nr);
    236   1.1    itojun 		hdr.subrulenr = htonl(rm->nr);
    237   1.5      yamt 		if (ruleset != NULL && ruleset->anchor != NULL)
    238   1.5      yamt 			strlcpy(hdr.ruleset, ruleset->anchor->name,
    239   1.1    itojun 			    sizeof(hdr.ruleset));
    240   1.1    itojun 	}
    241   1.1    itojun 	hdr.dir = dir;
    242   1.1    itojun 
    243   1.1    itojun #ifdef INET
    244   1.1    itojun 	if (af == AF_INET && dir == PF_OUT) {
    245   1.1    itojun 		struct ip *ip;
    246   1.1    itojun 
    247   1.1    itojun 		ip = mtod(m, struct ip *);
    248   1.1    itojun 		ip->ip_sum = 0;
    249   1.1    itojun 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
    250   1.1    itojun 	}
    251   1.1    itojun #endif /* INET */
    252   1.1    itojun 
    253   1.4      yamt #ifndef __NetBSD__
    254   1.1    itojun 	m1.m_next = m;
    255   1.1    itojun 	m1.m_len = PFLOG_HDRLEN;
    256   1.1    itojun 	m1.m_data = (char *) &hdr;
    257   1.4      yamt #endif
    258   1.1    itojun 
    259   1.1    itojun 	ifn = &(pflogif[0].sc_if);
    260   1.1    itojun 
    261   1.1    itojun 	if (ifn->if_bpf)
    262   1.4      yamt #ifndef __NetBSD__
    263   1.1    itojun 		bpf_mtap(ifn->if_bpf, &m1);
    264   1.4      yamt #else
    265   1.4      yamt 		bpf_mtap2(ifn->if_bpf, &hdr, PFLOG_HDRLEN, m);
    266   1.4      yamt #endif
    267   1.1    itojun #endif
    268   1.1    itojun 
    269   1.1    itojun 	return (0);
    270   1.1    itojun }
    271