1 /* $NetBSD: if_pflog.c,v 1.22 2020/01/29 05:52:27 thorpej Exp $ */ 2 /* $OpenBSD: if_pflog.c,v 1.24 2007/05/26 17:13:30 jason Exp $ */ 3 4 /* 5 * The authors of this code are John Ioannidis (ji@tla.org), 6 * Angelos D. Keromytis (kermit@csd.uch.gr) and 7 * Niels Provos (provos@physnet.uni-hamburg.de). 8 * 9 * This code was written by John Ioannidis for BSD/OS in Athens, Greece, 10 * in November 1995. 11 * 12 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996, 13 * by Angelos D. Keromytis. 14 * 15 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis 16 * and Niels Provos. 17 * 18 * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis 19 * and Niels Provos. 20 * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos. 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 <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: if_pflog.c,v 1.22 2020/01/29 05:52:27 thorpej Exp $"); 40 41 #ifdef _KERNEL_OPT 42 #include "opt_inet.h" 43 #endif 44 45 #include "pflog.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/mbuf.h> 50 #include <sys/proc.h> 51 #include <sys/socket.h> 52 #include <sys/ioctl.h> 53 54 #include <net/if.h> 55 #include <net/if_types.h> 56 #include <net/route.h> 57 #include <net/bpf.h> 58 59 #ifdef INET 60 #include <netinet/in.h> 61 #include <netinet/in_var.h> 62 #include <netinet/in_systm.h> 63 #include <netinet/ip.h> 64 #endif 65 66 #ifdef INET6 67 #ifndef INET 68 #include <netinet/in.h> 69 #endif 70 #include <netinet6/nd6.h> 71 #endif /* INET6 */ 72 73 #include <net/pfvar.h> 74 #include <net/if_pflog.h> 75 76 #include "ioconf.h" 77 78 #define PFLOGMTU (32768 + MHLEN + MLEN) 79 80 #ifdef PFLOGDEBUG 81 #define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0) 82 #else 83 #define DPRINTF(x) 84 #endif 85 86 #ifdef _MODULE 87 void pflogdetach(void); 88 #endif /* _MODULE */ 89 int pflogoutput(struct ifnet *, struct mbuf *, const struct sockaddr *, 90 const struct rtentry *); 91 int pflogioctl(struct ifnet *, u_long, void *); 92 void pflogstart(struct ifnet *); 93 int pflog_clone_create(struct if_clone *, int); 94 int pflog_clone_destroy(struct ifnet *); 95 96 LIST_HEAD(, pflog_softc) pflogif_list; 97 struct if_clone pflog_cloner = 98 IF_CLONE_INITIALIZER("pflog", pflog_clone_create, pflog_clone_destroy); 99 100 struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */ 101 102 void 103 pflogattach(int npflog) 104 { 105 int i; 106 107 LIST_INIT(&pflogif_list); 108 for (i = 0; i < PFLOGIFS_MAX; i++) 109 pflogifs[i] = NULL; 110 if_clone_attach(&pflog_cloner); 111 } 112 113 #ifdef _MODULE 114 void 115 pflogdetach(void) 116 { 117 int i; 118 119 for (i = 0; i < PFLOGIFS_MAX; i++) { 120 if (pflogifs[i] != NULL) 121 pflog_clone_destroy(pflogifs[i]); 122 } 123 if_clone_detach(&pflog_cloner); 124 } 125 #endif /* _MODULE */ 126 127 int 128 pflog_clone_create(struct if_clone *ifc, int unit) 129 { 130 struct ifnet *ifp; 131 struct pflog_softc *pflogif; 132 int s; 133 134 if (unit >= PFLOGIFS_MAX) 135 return (EINVAL); 136 137 if ((pflogif = malloc(sizeof(*pflogif), M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) 138 return (ENOMEM); 139 140 pflogif->sc_unit = unit; 141 ifp = &pflogif->sc_if; 142 snprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", unit); 143 ifp->if_softc = pflogif; 144 ifp->if_mtu = PFLOGMTU; 145 ifp->if_ioctl = pflogioctl; 146 ifp->if_output = pflogoutput; 147 ifp->if_start = pflogstart; 148 ifp->if_type = IFT_PFLOG; 149 #ifndef __NetBSD__ 150 ifp->if_snd.ifq_maxlen = ifqmaxlen; 151 #endif /* !__NetBSD__ */ 152 ifp->if_hdrlen = PFLOG_HDRLEN; 153 if_attach(ifp); 154 if_alloc_sadl(ifp); 155 156 #ifdef __NetBSD__ 157 bpf_attach(ifp, DLT_PFLOG, PFLOG_HDRLEN); 158 #else 159 bpfattach(&pflogif->sc_if.if_bpf, ifp, DLT_PFLOG, PFLOG_HDRLEN); 160 #endif /* !__NetBSD__ */ 161 162 s = splnet(); 163 LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list); 164 pflogifs[unit] = ifp; 165 splx(s); 166 167 return (0); 168 } 169 170 int 171 pflog_clone_destroy(struct ifnet *ifp) 172 { 173 struct pflog_softc *pflogif = ifp->if_softc; 174 int s; 175 176 s = splnet(); 177 pflogifs[pflogif->sc_unit] = NULL; 178 LIST_REMOVE(pflogif, sc_list); 179 splx(s); 180 181 bpf_detach(ifp); 182 if_detach(ifp); 183 free(pflogif, M_DEVBUF); 184 return (0); 185 } 186 187 /* 188 * Start output on the pflog interface. 189 */ 190 void 191 pflogstart(struct ifnet *ifp) 192 { 193 struct mbuf *m; 194 int s; 195 196 for (;;) { 197 s = splnet(); 198 IF_DROP(&ifp->if_snd); 199 IF_DEQUEUE(&ifp->if_snd, m); 200 splx(s); 201 202 if (m == NULL) 203 return; 204 else 205 m_freem(m); 206 } 207 } 208 209 int 210 pflogoutput(struct ifnet *ifp, struct mbuf *m, 211 const struct sockaddr *dst, const struct rtentry *rt) 212 { 213 m_freem(m); 214 return (0); 215 } 216 217 /* ARGSUSED */ 218 int 219 pflogioctl(struct ifnet *ifp, u_long cmd, void *data) 220 { 221 int error = 0; 222 223 switch (cmd) { 224 case SIOCSIFFLAGS: 225 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 226 break; 227 /*FALLTHROUGH*/ 228 case SIOCINITIFADDR: 229 case SIOCAIFADDR: 230 case SIOCSIFDSTADDR: 231 if (ifp->if_flags & IFF_UP) 232 ifp->if_flags |= IFF_RUNNING; 233 else 234 ifp->if_flags &= ~IFF_RUNNING; 235 break; 236 default: 237 error = ifioctl_common(ifp, cmd, data); 238 } 239 240 return error; 241 } 242 243 int 244 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir, 245 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am, 246 struct pf_ruleset *ruleset, struct pf_pdesc *pd) 247 { 248 struct ifnet *ifn; 249 struct pfloghdr hdr; 250 251 if (kif == NULL || m == NULL || rm == NULL || pd == NULL) 252 return (-1); 253 254 if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf) 255 return (0); 256 257 bzero(&hdr, sizeof(hdr)); 258 hdr.length = PFLOG_REAL_HDRLEN; 259 hdr.af = af; 260 hdr.action = rm->action; 261 hdr.reason = reason; 262 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname)); 263 264 if (am == NULL) { 265 hdr.rulenr = htonl(rm->nr); 266 hdr.subrulenr = -1; 267 } else { 268 hdr.rulenr = htonl(am->nr); 269 hdr.subrulenr = htonl(rm->nr); 270 if (ruleset != NULL && ruleset->anchor != NULL) 271 strlcpy(hdr.ruleset, ruleset->anchor->name, 272 sizeof(hdr.ruleset)); 273 } 274 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done) 275 pd->lookup.done = pf_socket_lookup(dir, pd); 276 if (pd->lookup.done > 0) { 277 hdr.uid = pd->lookup.uid; 278 hdr.pid = pd->lookup.pid; 279 } else { 280 hdr.uid = UID_MAX; 281 hdr.pid = NO_PID; 282 } 283 hdr.rule_uid = rm->cuid; 284 hdr.rule_pid = rm->cpid; 285 hdr.dir = dir; 286 287 #ifdef INET 288 if (af == AF_INET && dir == PF_OUT) { 289 struct ip *ip; 290 291 ip = mtod(m, struct ip *); 292 ip->ip_sum = 0; 293 ip->ip_sum = in_cksum(m, ip->ip_hl << 2); 294 } 295 #endif /* INET */ 296 297 #ifdef __NetBSD__ 298 if_statadd2(ifn, if_opackets, 1, if_obytes, m->m_pkthdr.len); 299 #else 300 ifn->if_opackets++; 301 ifn->if_obytes += m->m_pkthdr.len; 302 #endif /* __NetBSD__ */ 303 304 #ifdef __NetBSD__ 305 bpf_mtap2(ifn->if_bpf, &hdr, PFLOG_HDRLEN, m, BPF_D_OUT); 306 #else 307 bpf_mtap_hdr(ifn->if_bpf, (char *)&hdr, PFLOG_HDRLEN, m, 308 BPF_DIRECTION_OUT); 309 #endif /* !__NetBSD__ */ 310 311 312 return (0); 313 } 314