1 /*- 2 * SPDX-License-Identifier: ISC 3 * 4 * The authors of this code are John Ioannidis (ji@tla.org), 5 * Angelos D. Keromytis (kermit@csd.uch.gr) and 6 * Niels Provos (provos@physnet.uni-hamburg.de). 7 * 8 * This code was written by John Ioannidis for BSD/OS in Athens, Greece, 9 * in November 1995. 10 * 11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996, 12 * by Angelos D. Keromytis. 13 * 14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis 15 * and Niels Provos. 16 * 17 * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis 18 * and Niels Provos. 19 * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos. 20 * 21 * Permission to use, copy, and modify this software with or without fee 22 * is hereby granted, provided that this entire notice is included in 23 * all copies of any software which is or includes a copy or 24 * modification of this software. 25 * You may use this code under the GNU public license if you so wish. Please 26 * contribute changes back to the authors under this freer than GPL license 27 * so that we may further the use of strong encryption without limitations to 28 * all. 29 * 30 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 31 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 32 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 33 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 34 * PURPOSE. 35 * 36 * $OpenBSD: if_pflog.c,v 1.26 2007/10/18 21:58:18 mpf Exp $ 37 */ 38 39 #include <sys/cdefs.h> 40 #include "opt_inet.h" 41 #include "opt_inet6.h" 42 #include "opt_bpf.h" 43 #include "opt_pf.h" 44 45 #include <sys/param.h> 46 #include <sys/kernel.h> 47 #include <sys/mbuf.h> 48 #include <sys/module.h> 49 #include <sys/proc.h> 50 #include <sys/socket.h> 51 #include <sys/sockio.h> 52 53 #include <net/bpf.h> 54 #include <net/if.h> 55 #include <net/if_var.h> 56 #include <net/if_clone.h> 57 #include <net/if_pflog.h> 58 #include <net/if_private.h> 59 #include <net/if_types.h> 60 #include <net/vnet.h> 61 #include <net/pfvar.h> 62 63 #if defined(INET) || defined(INET6) 64 #include <netinet/in.h> 65 #endif 66 #ifdef INET 67 #include <netinet/in_var.h> 68 #include <netinet/ip.h> 69 #endif 70 71 #ifdef INET6 72 #include <netinet6/in6_var.h> 73 #include <netinet6/nd6.h> 74 #endif /* INET6 */ 75 76 #ifdef INET 77 #include <machine/in_cksum.h> 78 #endif /* INET */ 79 80 #define PFLOGMTU (32768 + MHLEN + MLEN) 81 82 #ifdef PFLOGDEBUG 83 #define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0) 84 #else 85 #define DPRINTF(x) 86 #endif 87 88 static int pflogoutput(struct ifnet *, struct mbuf *, 89 const struct sockaddr *, struct route *); 90 static void pflogattach(int); 91 static int pflogioctl(struct ifnet *, u_long, caddr_t); 92 static void pflogstart(struct ifnet *); 93 static int pflog_clone_create(struct if_clone *, char *, size_t, 94 struct ifc_data *, struct ifnet **); 95 static int pflog_clone_destroy(struct if_clone *, struct ifnet *, uint32_t); 96 97 static const char pflogname[] = "pflog"; 98 99 VNET_DEFINE_STATIC(struct if_clone *, pflog_cloner); 100 #define V_pflog_cloner VNET(pflog_cloner) 101 102 VNET_DEFINE(struct ifnet *, pflogifs[PFLOGIFS_MAX]); /* for fast access */ 103 #define V_pflogifs VNET(pflogifs) 104 105 static void 106 pflogattach(int npflog __unused) 107 { 108 int i; 109 for (i = 0; i < PFLOGIFS_MAX; i++) 110 V_pflogifs[i] = NULL; 111 112 struct if_clone_addreq req = { 113 .create_f = pflog_clone_create, 114 .destroy_f = pflog_clone_destroy, 115 .flags = IFC_F_AUTOUNIT, 116 }; 117 V_pflog_cloner = ifc_attach_cloner(pflogname, &req); 118 struct ifc_data ifd = { .unit = 0 }; 119 ifc_create_ifp(pflogname, &ifd, NULL); 120 } 121 122 static int 123 pflog_clone_create(struct if_clone *ifc, char *name, size_t maxlen, 124 struct ifc_data *ifd, struct ifnet **ifpp) 125 { 126 struct ifnet *ifp; 127 128 if (ifd->unit >= PFLOGIFS_MAX) 129 return (EINVAL); 130 131 ifp = if_alloc(IFT_PFLOG); 132 if (ifp == NULL) { 133 return (ENOSPC); 134 } 135 if_initname(ifp, pflogname, ifd->unit); 136 ifp->if_mtu = PFLOGMTU; 137 ifp->if_ioctl = pflogioctl; 138 ifp->if_output = pflogoutput; 139 ifp->if_start = pflogstart; 140 ifp->if_snd.ifq_maxlen = ifqmaxlen; 141 ifp->if_hdrlen = PFLOG_HDRLEN; 142 if_attach(ifp); 143 144 bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN); 145 146 V_pflogifs[ifd->unit] = ifp; 147 *ifpp = ifp; 148 149 return (0); 150 } 151 152 static int 153 pflog_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 154 { 155 int i; 156 157 if (ifp->if_dunit == 0 && (flags & IFC_F_FORCE) == 0) 158 return (EINVAL); 159 160 for (i = 0; i < PFLOGIFS_MAX; i++) 161 if (V_pflogifs[i] == ifp) 162 V_pflogifs[i] = NULL; 163 164 bpfdetach(ifp); 165 if_detach(ifp); 166 if_free(ifp); 167 168 return (0); 169 } 170 171 /* 172 * Start output on the pflog interface. 173 */ 174 static void 175 pflogstart(struct ifnet *ifp) 176 { 177 struct mbuf *m; 178 179 for (;;) { 180 IF_LOCK(&ifp->if_snd); 181 _IF_DEQUEUE(&ifp->if_snd, m); 182 IF_UNLOCK(&ifp->if_snd); 183 184 if (m == NULL) 185 return; 186 else 187 m_freem(m); 188 } 189 } 190 191 static int 192 pflogoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 193 struct route *rt) 194 { 195 m_freem(m); 196 return (0); 197 } 198 199 /* ARGSUSED */ 200 static int 201 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 202 { 203 switch (cmd) { 204 case SIOCSIFFLAGS: 205 if (ifp->if_flags & IFF_UP) 206 ifp->if_drv_flags |= IFF_DRV_RUNNING; 207 else 208 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 209 break; 210 default: 211 return (ENOTTY); 212 } 213 214 return (0); 215 } 216 217 static int 218 pflog_packet(struct pfi_kkif *kif, struct mbuf *m, sa_family_t af, 219 uint8_t action, u_int8_t reason, struct pf_krule *rm, struct pf_krule *am, 220 struct pf_kruleset *ruleset, struct pf_pdesc *pd, int lookupsafe) 221 { 222 struct ifnet *ifn; 223 struct pfloghdr hdr; 224 225 if (kif == NULL || m == NULL || rm == NULL || pd == NULL) 226 return (1); 227 228 ifn = V_pflogifs[rm->logif]; 229 if (ifn == NULL || !bpf_peers_present(ifn->if_bpf)) 230 return (0); 231 232 bzero(&hdr, sizeof(hdr)); 233 hdr.length = PFLOG_REAL_HDRLEN; 234 hdr.af = af; 235 hdr.action = action; 236 hdr.reason = reason; 237 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname)); 238 239 if (am == NULL) { 240 hdr.rulenr = htonl(rm->nr); 241 hdr.subrulenr = -1; 242 } else { 243 hdr.rulenr = htonl(am->nr); 244 hdr.subrulenr = htonl(rm->nr); 245 if (ruleset != NULL && ruleset->anchor != NULL) 246 strlcpy(hdr.ruleset, ruleset->anchor->name, 247 sizeof(hdr.ruleset)); 248 } 249 hdr.ridentifier = htonl(rm->ridentifier); 250 /* 251 * XXXGL: we avoid pf_socket_lookup() when we are holding 252 * state lock, since this leads to unsafe LOR. 253 * These conditions are very very rare, however. 254 */ 255 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done && lookupsafe) 256 pd->lookup.done = pf_socket_lookup(pd, m); 257 if (pd->lookup.done > 0) 258 hdr.uid = pd->lookup.uid; 259 else 260 hdr.uid = UID_MAX; 261 hdr.pid = NO_PID; 262 hdr.rule_uid = rm->cuid; 263 hdr.rule_pid = rm->cpid; 264 hdr.dir = pd->dir; 265 266 #ifdef INET 267 if (af == AF_INET && pd->dir == PF_OUT) { 268 struct ip *ip; 269 270 ip = mtod(m, struct ip *); 271 ip->ip_sum = 0; 272 ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 273 } 274 #endif /* INET */ 275 276 if_inc_counter(ifn, IFCOUNTER_OPACKETS, 1); 277 if_inc_counter(ifn, IFCOUNTER_OBYTES, m->m_pkthdr.len); 278 bpf_mtap2(ifn->if_bpf, &hdr, PFLOG_HDRLEN, m); 279 280 return (0); 281 } 282 283 static void 284 vnet_pflog_init(const void *unused __unused) 285 { 286 287 pflogattach(1); 288 } 289 VNET_SYSINIT(vnet_pflog_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY, 290 vnet_pflog_init, NULL); 291 292 static void 293 vnet_pflog_uninit(const void *unused __unused) 294 { 295 296 ifc_detach_cloner(V_pflog_cloner); 297 } 298 /* 299 * Detach after pf is gone; otherwise we might touch pflog memory 300 * from within pf after freeing pflog. 301 */ 302 VNET_SYSUNINIT(vnet_pflog_uninit, SI_SUB_INIT_IF, SI_ORDER_SECOND, 303 vnet_pflog_uninit, NULL); 304 305 static int 306 pflog_modevent(module_t mod, int type, void *data) 307 { 308 int error = 0; 309 310 switch (type) { 311 case MOD_LOAD: 312 PF_RULES_WLOCK(); 313 pflog_packet_ptr = pflog_packet; 314 PF_RULES_WUNLOCK(); 315 break; 316 case MOD_UNLOAD: 317 PF_RULES_WLOCK(); 318 pflog_packet_ptr = NULL; 319 PF_RULES_WUNLOCK(); 320 break; 321 default: 322 error = EOPNOTSUPP; 323 break; 324 } 325 326 return error; 327 } 328 329 static moduledata_t pflog_mod = { pflogname, pflog_modevent, 0 }; 330 331 #define PFLOG_MODVER 1 332 333 /* Do not run before pf is initialized as we depend on its locks. */ 334 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY); 335 MODULE_VERSION(pflog, PFLOG_MODVER); 336 MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER); 337