1 /* $OpenBSD: if_pflog.c,v 1.27 2007/12/20 02:53:02 brad 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 * 20 * Copyright (c) 2010 The DragonFly Project. All rights reserved. 21 * 22 * Permission to use, copy, and modify this software with or without fee 23 * is hereby granted, provided that this entire notice is included in 24 * all copies of any software which is or includes a copy or 25 * modification of this software. 26 * You may use this code under the GNU public license if you so wish. Please 27 * contribute changes back to the authors under this freer than GPL license 28 * so that we may further the use of strong encryption without limitations to 29 * all. 30 * 31 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 32 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 33 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 34 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 35 * PURPOSE. 36 */ 37 38 #include "opt_inet.h" 39 #include "opt_inet6.h" 40 #include "use_bpf.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/in_cksum.h> 45 #include <sys/mbuf.h> 46 #include <sys/proc.h> 47 #include <sys/socket.h> 48 #include <sys/kernel.h> 49 #include <sys/malloc.h> 50 #include <sys/module.h> 51 #include <sys/sockio.h> 52 #include <sys/thread2.h> 53 54 #include <net/if.h> 55 #include <net/if_types.h> 56 #include <net/ifq_var.h> 57 #include <net/route.h> 58 #include <net/bpf.h> 59 60 #ifdef INET 61 #include <netinet/in.h> 62 #include <netinet/in_var.h> 63 #include <netinet/in_systm.h> 64 #include <netinet/ip.h> 65 #endif 66 67 #ifdef INET6 68 #ifndef INET 69 #include <netinet/in.h> 70 #endif 71 #include <netinet6/nd6.h> 72 #endif /* INET6 */ 73 74 #include <net/pf/pfvar.h> 75 #include <net/pf/if_pflog.h> 76 77 #define PFLOGNAME "pflog" 78 79 #define PFLOGMTU (32768 + MHLEN + MLEN) 80 81 #ifdef PFLOGDEBUG 82 #define DPRINTF(x) do { if (pflogdebug) kprintf x ; } while (0) 83 #else 84 #define DPRINTF(x) 85 #endif 86 87 static void pflogattach(void); 88 static int pflogoutput(struct ifnet *, struct mbuf *, 89 struct sockaddr *, struct rtentry *); 90 static int pflogioctl(struct ifnet *, u_long, caddr_t __unused, 91 struct ucred * __unused); 92 static void pflogstart(struct ifnet *, struct ifaltq_subque *); 93 static int pflog_clone_create(struct if_clone *, int, caddr_t __unused); 94 static int pflog_clone_destroy(struct ifnet *); 95 96 static struct if_clone pflog_cloner = IF_CLONE_INITIALIZER( 97 PFLOGNAME, pflog_clone_create, pflog_clone_destroy, 1, 1); 98 99 static LIST_HEAD(, pflog_softc) pflogif_list; 100 static struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */ 101 102 static void 103 pflogattach(void) 104 { 105 int i; 106 LIST_INIT(&pflogif_list); 107 for (i = 0; i < PFLOGIFS_MAX; i++) 108 pflogifs[i] = NULL; 109 if_clone_attach(&pflog_cloner); 110 } 111 112 static int 113 pflog_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused) 114 { 115 struct ifnet *ifp; 116 struct pflog_softc *pflogif; 117 118 lwkt_gettoken(&pf_token); 119 120 if (unit >= PFLOGIFS_MAX) { 121 lwkt_reltoken(&pf_token); 122 return (EINVAL); 123 } 124 125 pflogif = kmalloc(sizeof(*pflogif), M_DEVBUF, M_WAITOK | M_ZERO); 126 pflogif->sc_unit = unit; 127 lwkt_reltoken(&pf_token); 128 129 ifp = &pflogif->sc_if; 130 if_initname(ifp, ifc->ifc_name, unit); 131 ifp->if_softc = pflogif; 132 ifp->if_mtu = PFLOGMTU; 133 ifp->if_ioctl = pflogioctl; 134 ifp->if_output = pflogoutput; 135 ifp->if_start = pflogstart; 136 ifp->if_type = IFT_PFLOG; 137 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen); 138 ifp->if_hdrlen = PFLOG_HDRLEN; 139 140 if_attach(ifp, NULL); 141 #if NBPF > 0 142 bpfattach(&pflogif->sc_if, DLT_PFLOG, PFLOG_HDRLEN); 143 #endif 144 145 lwkt_gettoken(&pf_token); 146 crit_enter(); 147 LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list); 148 pflogifs[unit] = ifp; 149 crit_exit(); 150 lwkt_reltoken(&pf_token); 151 152 return (0); 153 } 154 155 static int 156 pflog_clone_destroy(struct ifnet *ifp) 157 { 158 struct pflog_softc *pflogif = ifp->if_softc; 159 160 lwkt_gettoken(&pf_token); 161 162 crit_enter(); 163 pflogifs[pflogif->sc_unit] = NULL; 164 LIST_REMOVE(pflogif, sc_list); 165 crit_exit(); 166 167 lwkt_reltoken(&pf_token); 168 #if NBPF > 0 169 bpfdetach(ifp); 170 #endif 171 if_detach(ifp); 172 lwkt_gettoken(&pf_token); 173 kfree(pflogif, M_DEVBUF); 174 lwkt_reltoken(&pf_token); 175 176 return 0; 177 } 178 179 /* 180 * Start output on the pflog interface. 181 */ 182 static void 183 pflogstart(struct ifnet *ifp, struct ifaltq_subque *ifsq) 184 { 185 struct mbuf *m; 186 187 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq); 188 ASSERT_LWKT_TOKEN_HELD(&pf_token); 189 190 for (;;) { 191 m = ifsq_dequeue(ifsq); 192 if (m == NULL) 193 return; 194 else 195 m_freem(m); 196 } 197 } 198 199 static int 200 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 201 struct rtentry *rt) 202 { 203 m_freem(m); 204 return (0); 205 } 206 207 static int 208 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data __unused, 209 struct ucred *cr __unused) 210 { 211 212 lwkt_gettoken(&pf_token); 213 214 switch (cmd) { 215 case SIOCSIFFLAGS: 216 lwkt_reltoken(&pf_token); 217 if (ifp->if_flags & IFF_UP) 218 ifp->if_flags |= IFF_RUNNING; 219 else 220 ifp->if_flags &= ~IFF_RUNNING; 221 lwkt_gettoken(&pf_token); 222 break; 223 default: 224 lwkt_reltoken(&pf_token); 225 return (EINVAL); 226 } 227 228 lwkt_reltoken(&pf_token); 229 return (0); 230 } 231 232 int 233 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir, 234 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am, 235 struct pf_ruleset *ruleset, struct pf_pdesc *pd) 236 { 237 struct ifnet *ifn = NULL; 238 struct pfloghdr hdr; 239 240 ASSERT_LWKT_TOKEN_HELD(&pf_token); 241 242 if (kif == NULL || m == NULL || rm == NULL) 243 return (-1); 244 245 if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf) 246 return (0); 247 248 bzero(&hdr, sizeof(hdr)); 249 hdr.length = PFLOG_REAL_HDRLEN; 250 hdr.af = af; 251 hdr.action = rm->action; 252 hdr.reason = reason; 253 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname)); 254 255 if (am == NULL) { 256 hdr.rulenr = htonl(rm->nr); 257 hdr.subrulenr = -1; 258 } else { 259 hdr.rulenr = htonl(am->nr); 260 hdr.subrulenr = htonl(rm->nr); 261 if (ruleset != NULL && ruleset->anchor != NULL) 262 strlcpy(hdr.ruleset, ruleset->anchor->name, 263 sizeof(hdr.ruleset)); 264 } 265 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done) 266 pd->lookup.done = pf_socket_lookup(dir, pd); 267 if (pd->lookup.done > 0) { 268 hdr.uid = pd->lookup.uid; 269 hdr.pid = pd->lookup.pid; 270 } else { 271 hdr.uid = UID_MAX; 272 hdr.pid = NO_PID; 273 } 274 hdr.rule_uid = rm->cuid; 275 hdr.rule_pid = rm->cpid; 276 hdr.dir = dir; 277 278 #ifdef INET 279 if (af == AF_INET) { 280 struct ip *ip; 281 ip = mtod(m, struct ip *); 282 ip->ip_len = htons(ip->ip_len); 283 ip->ip_off = htons(ip->ip_off); 284 285 if (dir == PF_OUT) { 286 ip->ip_sum = 0; 287 ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 288 } 289 } 290 #endif /* INET */ 291 292 IFNET_STAT_INC(ifn, opackets, 1); 293 IFNET_STAT_INC(ifn, obytes, m->m_pkthdr.len); 294 if (ifn->if_bpf) { 295 bpf_gettoken(); 296 if (ifn->if_bpf) { 297 bpf_mtap_hdr(ifn->if_bpf, (char *)&hdr, PFLOG_HDRLEN, m, 298 BPF_DIRECTION_OUT); 299 } 300 bpf_reltoken(); 301 } 302 303 #ifdef INET 304 if (af == AF_INET) { 305 struct ip *ip = mtod(m, struct ip *); 306 307 ip->ip_len = ntohs(ip->ip_len); 308 ip->ip_off = ntohs(ip->ip_off); 309 } 310 #endif /* INET */ 311 312 return (0); 313 } 314 315 static int 316 pflog_modevent(module_t mod, int type, void *data) 317 { 318 int error = 0; 319 320 struct pflog_softc *pflogif, *tmp; 321 322 lwkt_gettoken(&pf_token); 323 324 switch (type) { 325 case MOD_LOAD: 326 lwkt_reltoken(&pf_token); 327 pflogattach(); 328 lwkt_gettoken(&pf_token); 329 break; 330 331 case MOD_UNLOAD: 332 lwkt_reltoken(&pf_token); 333 if_clone_detach(&pflog_cloner); 334 LIST_FOREACH_MUTABLE(pflogif, &pflogif_list, sc_list, tmp) 335 pflog_clone_destroy(&pflogif->sc_if); 336 lwkt_gettoken(&pf_token); 337 break; 338 339 default: 340 error = EINVAL; 341 break; 342 } 343 lwkt_reltoken(&pf_token); 344 return error; 345 } 346 347 static moduledata_t pflog_mod = { 348 "pflog", 349 pflog_modevent, 350 0 351 }; 352 353 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 354 MODULE_VERSION(pflog, PFLOG_MODVER); 355 /* Do not run before pf is initialized. */ 356 MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER); 357