if_pflog.c revision 310094
11541Srgrimes/*- 2109524Sphk * The authors of this code are John Ioannidis (ji@tla.org), 3109524Sphk * Angelos D. Keromytis (kermit@csd.uch.gr) and 4109524Sphk * Niels Provos (provos@physnet.uni-hamburg.de). 5109524Sphk * 6109524Sphk * This code was written by John Ioannidis for BSD/OS in Athens, Greece, 7109524Sphk * in November 1995. 8109524Sphk * 9109524Sphk * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996, 10109524Sphk * by Angelos D. Keromytis. 11109524Sphk * 12109524Sphk * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis 13109524Sphk * and Niels Provos. 14109524Sphk * 15109524Sphk * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis 16109524Sphk * and Niels Provos. 17109524Sphk * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos. 18109524Sphk * 19109524Sphk * Permission to use, copy, and modify this software with or without fee 20109524Sphk * is hereby granted, provided that this entire notice is included in 21109524Sphk * all copies of any software which is or includes a copy or 22109524Sphk * modification of this software. 23109524Sphk * You may use this code under the GNU public license if you so wish. Please 24109524Sphk * contribute changes back to the authors under this freer than GPL license 25109524Sphk * so that we may further the use of strong encryption without limitations to 26116182Sobrien * all. 27116182Sobrien * 28116182Sobrien * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 29109524Sphk * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 30109524Sphk * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 31109524Sphk * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 32109524Sphk * PURPOSE. 33109524Sphk * 34171186Skib * $OpenBSD: if_pflog.c,v 1.26 2007/10/18 21:58:18 mpf Exp $ 35109524Sphk */ 36109524Sphk 37171186Skib#include <sys/cdefs.h> 38171186Skib__FBSDID("$FreeBSD: stable/10/sys/netpfil/pf/if_pflog.c 310094 2016-12-14 21:30:35Z kp $"); 39171186Skib 40109524Sphk#include "opt_inet.h" 41109524Sphk#include "opt_inet6.h" 42109524Sphk#include "opt_bpf.h" 43126080Sphk#include "opt_pf.h" 44111815Sphk 45111815Sphk#include <sys/param.h> 46109524Sphk#include <sys/kernel.h> 47109524Sphk#include <sys/mbuf.h> 48130585Sphk#include <sys/module.h> 49109524Sphk#include <sys/proc.h> 50109524Sphk#include <sys/socket.h> 51130585Sphk#include <sys/sockio.h> 52109524Sphk 53109524Sphk#include <net/bpf.h> 54109524Sphk#include <net/if.h> 55109524Sphk#include <net/if_clone.h> 56109524Sphk#include <net/if_pflog.h> 57109524Sphk#include <net/if_types.h> 58148868Srwatson#include <net/pfvar.h> 59148868Srwatson 60109524Sphk#if defined(INET) || defined(INET6) 61109524Sphk#include <netinet/in.h> 62130640Sphk#endif 63109524Sphk#ifdef INET 64109524Sphk#include <netinet/in_var.h> 65109524Sphk#include <netinet/ip.h> 66171186Skib#endif 67171186Skib 68171186Skib#ifdef INET6 69171186Skib#include <netinet6/in6_var.h> 70109940Sphk#include <netinet6/nd6.h> 71109940Sphk#endif /* INET6 */ 72109940Sphk 73109940Sphk#ifdef INET 74162573Smbr#include <machine/in_cksum.h> 75162573Smbr#endif /* INET */ 76162573Smbr 77162705Smbr#define PFLOGMTU (32768 + MHLEN + MLEN) 78162573Smbr 79109524Sphk#ifdef PFLOGDEBUG 80171186Skib#define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0) 81171186Skib#else 82171186Skib#define DPRINTF(x) 83109524Sphk#endif 84109524Sphk 85109524Sphkstatic int pflogoutput(struct ifnet *, struct mbuf *, 86109524Sphk const struct sockaddr *, struct route *); 87109524Sphkstatic void pflogattach(int); 88109524Sphkstatic int pflogioctl(struct ifnet *, u_long, caddr_t); 89109524Sphkstatic void pflogstart(struct ifnet *); 90216952Skibstatic int pflog_clone_create(struct if_clone *, int, caddr_t); 91216952Skibstatic void pflog_clone_destroy(struct ifnet *); 92109524Sphkstatic struct if_clone *pflog_cloner; 93109524Sphk 94177253Srwatsonstatic const char pflogname[] = "pflog"; 95 96struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */ 97 98static void 99pflogattach(int npflog) 100{ 101 int i; 102 for (i = 0; i < PFLOGIFS_MAX; i++) 103 pflogifs[i] = NULL; 104 pflog_cloner = if_clone_simple(pflogname, pflog_clone_create, 105 pflog_clone_destroy, 1); 106} 107 108static int 109pflog_clone_create(struct if_clone *ifc, int unit, caddr_t param) 110{ 111 struct ifnet *ifp; 112 113 if (unit >= PFLOGIFS_MAX) 114 return (EINVAL); 115 116 ifp = if_alloc(IFT_PFLOG); 117 if (ifp == NULL) { 118 return (ENOSPC); 119 } 120 if_initname(ifp, pflogname, unit); 121 ifp->if_mtu = PFLOGMTU; 122 ifp->if_ioctl = pflogioctl; 123 ifp->if_output = pflogoutput; 124 ifp->if_start = pflogstart; 125 ifp->if_snd.ifq_maxlen = ifqmaxlen; 126 ifp->if_hdrlen = PFLOG_HDRLEN; 127 if_attach(ifp); 128 129 bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN); 130 131 pflogifs[unit] = ifp; 132 133 return (0); 134} 135 136static void 137pflog_clone_destroy(struct ifnet *ifp) 138{ 139 int i; 140 141 for (i = 0; i < PFLOGIFS_MAX; i++) 142 if (pflogifs[i] == ifp) 143 pflogifs[i] = NULL; 144 145 bpfdetach(ifp); 146 if_detach(ifp); 147 if_free(ifp); 148} 149 150/* 151 * Start output on the pflog interface. 152 */ 153static void 154pflogstart(struct ifnet *ifp) 155{ 156 struct mbuf *m; 157 158 for (;;) { 159 IF_LOCK(&ifp->if_snd); 160 _IF_DROP(&ifp->if_snd); 161 _IF_DEQUEUE(&ifp->if_snd, m); 162 IF_UNLOCK(&ifp->if_snd); 163 164 if (m == NULL) 165 return; 166 else 167 m_freem(m); 168 } 169} 170 171static int 172pflogoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 173 struct route *rt) 174{ 175 m_freem(m); 176 return (0); 177} 178 179/* ARGSUSED */ 180static int 181pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 182{ 183 switch (cmd) { 184 case SIOCSIFFLAGS: 185 if (ifp->if_flags & IFF_UP) 186 ifp->if_drv_flags |= IFF_DRV_RUNNING; 187 else 188 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 189 break; 190 default: 191 return (ENOTTY); 192 } 193 194 return (0); 195} 196 197static int 198pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir, 199 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am, 200 struct pf_ruleset *ruleset, struct pf_pdesc *pd, int lookupsafe) 201{ 202 struct ifnet *ifn; 203 struct pfloghdr hdr; 204 205 if (kif == NULL || m == NULL || rm == NULL || pd == NULL) 206 return ( 1); 207 208 if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf) 209 return (0); 210 211 bzero(&hdr, sizeof(hdr)); 212 hdr.length = PFLOG_REAL_HDRLEN; 213 hdr.af = af; 214 hdr.action = rm->action; 215 hdr.reason = reason; 216 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname)); 217 218 if (am == NULL) { 219 hdr.rulenr = htonl(rm->nr); 220 hdr.subrulenr = -1; 221 } else { 222 hdr.rulenr = htonl(am->nr); 223 hdr.subrulenr = htonl(rm->nr); 224 if (ruleset != NULL && ruleset->anchor != NULL) 225 strlcpy(hdr.ruleset, ruleset->anchor->name, 226 sizeof(hdr.ruleset)); 227 } 228 /* 229 * XXXGL: we avoid pf_socket_lookup() when we are holding 230 * state lock, since this leads to unsafe LOR. 231 * These conditions are very very rare, however. 232 */ 233 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done && lookupsafe) 234 pd->lookup.done = pf_socket_lookup(dir, pd, m); 235 if (pd->lookup.done > 0) 236 hdr.uid = pd->lookup.uid; 237 else 238 hdr.uid = UID_MAX; 239 hdr.pid = NO_PID; 240 hdr.rule_uid = rm->cuid; 241 hdr.rule_pid = rm->cpid; 242 hdr.dir = dir; 243 244#ifdef INET 245 if (af == AF_INET && dir == PF_OUT) { 246 struct ip *ip; 247 248 ip = mtod(m, struct ip *); 249 ip->ip_sum = 0; 250 ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 251 } 252#endif /* INET */ 253 254 ifn->if_opackets++; 255 ifn->if_obytes += m->m_pkthdr.len; 256 BPF_MTAP2(ifn, &hdr, PFLOG_HDRLEN, m); 257 258 return (0); 259} 260 261static int 262pflog_modevent(module_t mod, int type, void *data) 263{ 264 int error = 0; 265 266 switch (type) { 267 case MOD_LOAD: 268 pflogattach(1); 269 PF_RULES_WLOCK(); 270 pflog_packet_ptr = pflog_packet; 271 PF_RULES_WUNLOCK(); 272 break; 273 case MOD_UNLOAD: 274 PF_RULES_WLOCK(); 275 pflog_packet_ptr = NULL; 276 PF_RULES_WUNLOCK(); 277 if_clone_detach(pflog_cloner); 278 break; 279 default: 280 error = EINVAL; 281 break; 282 } 283 284 return error; 285} 286 287static moduledata_t pflog_mod = { pflogname, pflog_modevent, 0 }; 288 289#define PFLOG_MODVER 1 290 291DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 292MODULE_VERSION(pflog, PFLOG_MODVER); 293MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER); 294