1 /* $OpenBSD: if_pflog.c,v 1.83 2019/06/13 21:12:52 mpi 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 * Copyright (c) 2002 - 2010 Henning Brauer 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 37 #include "bpfilter.h" 38 #include "pflog.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/mbuf.h> 43 #include <sys/proc.h> 44 #include <sys/socket.h> 45 #include <sys/stdint.h> 46 #include <sys/ioctl.h> 47 48 #include <net/if.h> 49 #include <net/if_var.h> 50 #include <net/if_types.h> 51 #include <net/bpf.h> 52 53 #include <netinet/in.h> 54 #include <netinet/ip.h> 55 #include <netinet/ip_icmp.h> 56 #include <netinet/tcp.h> 57 #include <netinet/udp.h> 58 59 #ifdef INET6 60 #include <netinet/ip6.h> 61 #include <netinet/icmp6.h> 62 #endif /* INET6 */ 63 64 #include <net/pfvar.h> 65 #include <net/pfvar_priv.h> 66 #include <net/if_pflog.h> 67 68 #define PFLOGMTU (32768 + MHLEN + MLEN) 69 70 #ifdef PFLOGDEBUG 71 #define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0) 72 #else 73 #define DPRINTF(x) 74 #endif 75 76 void pflogattach(int); 77 int pflogifs_resize(size_t); 78 int pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *, 79 struct rtentry *); 80 int pflogioctl(struct ifnet *, u_long, caddr_t); 81 void pflogstart(struct ifnet *); 82 int pflog_clone_create(struct if_clone *, int); 83 int pflog_clone_destroy(struct ifnet *); 84 void pflog_bpfcopy(const void *, void *, size_t); 85 86 struct if_clone pflog_cloner = 87 IF_CLONE_INITIALIZER("pflog", pflog_clone_create, pflog_clone_destroy); 88 89 int npflogifs = 0; 90 struct ifnet **pflogifs = NULL; /* for fast access */ 91 struct mbuf *pflog_mhdr = NULL, *pflog_mptr = NULL; 92 93 void 94 pflogattach(int npflog) 95 { 96 if (pflog_mhdr == NULL) 97 if ((pflog_mhdr = m_get(M_DONTWAIT, MT_HEADER)) == NULL) 98 panic("pflogattach: no mbuf"); 99 if (pflog_mptr == NULL) 100 if ((pflog_mptr = m_get(M_DONTWAIT, MT_DATA)) == NULL) 101 panic("pflogattach: no mbuf"); 102 if_clone_attach(&pflog_cloner); 103 } 104 105 int 106 pflogifs_resize(size_t n) 107 { 108 struct ifnet **p; 109 int i; 110 111 NET_ASSERT_LOCKED(); 112 113 if (n > SIZE_MAX / sizeof(*p)) 114 return (EINVAL); 115 if (n == 0) 116 p = NULL; 117 else 118 if ((p = mallocarray(n, sizeof(*p), M_DEVBUF, 119 M_NOWAIT|M_ZERO)) == NULL) 120 return (ENOMEM); 121 for (i = 0; i < n; i++) 122 if (i < npflogifs) 123 p[i] = pflogifs[i]; 124 else 125 p[i] = NULL; 126 127 free(pflogifs, M_DEVBUF, npflogifs * sizeof(*pflogifs)); 128 pflogifs = p; 129 npflogifs = n; 130 return (0); 131 } 132 133 int 134 pflog_clone_create(struct if_clone *ifc, int unit) 135 { 136 struct ifnet *ifp; 137 struct pflog_softc *pflogif; 138 139 pflogif = malloc(sizeof(*pflogif), M_DEVBUF, M_WAITOK|M_ZERO); 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_xflags = IFXF_CLONED; 149 ifp->if_type = IFT_PFLOG; 150 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 151 ifp->if_hdrlen = PFLOG_HDRLEN; 152 if_attach(ifp); 153 if_alloc_sadl(ifp); 154 155 #if NBPFILTER > 0 156 bpfattach(&pflogif->sc_if.if_bpf, ifp, DLT_PFLOG, PFLOG_HDRLEN); 157 #endif 158 159 NET_LOCK(); 160 if (unit + 1 > npflogifs && pflogifs_resize(unit + 1) != 0) { 161 NET_UNLOCK(); 162 return (ENOMEM); 163 } 164 pflogifs[unit] = ifp; 165 NET_UNLOCK(); 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 i; 175 176 NET_LOCK(); 177 pflogifs[pflogif->sc_unit] = NULL; 178 for (i = npflogifs; i > 0 && pflogifs[i - 1] == NULL; i--) 179 ; /* nothing */ 180 if (i < npflogifs) 181 pflogifs_resize(i); /* error harmless here */ 182 NET_UNLOCK(); 183 184 if_detach(ifp); 185 free(pflogif, M_DEVBUF, sizeof(*pflogif)); 186 return (0); 187 } 188 189 /* 190 * Start output on the pflog interface. 191 */ 192 void 193 pflogstart(struct ifnet *ifp) 194 { 195 IFQ_PURGE(&ifp->if_snd); 196 } 197 198 int 199 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 200 struct rtentry *rt) 201 { 202 m_freem(m); /* drop packet */ 203 return (EAFNOSUPPORT); 204 } 205 206 int 207 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 208 { 209 switch (cmd) { 210 case SIOCSIFFLAGS: 211 if (ifp->if_flags & IFF_UP) 212 ifp->if_flags |= IFF_RUNNING; 213 else 214 ifp->if_flags &= ~IFF_RUNNING; 215 break; 216 default: 217 return (ENOTTY); 218 } 219 220 return (0); 221 } 222 223 int 224 pflog_packet(struct pf_pdesc *pd, u_int8_t reason, struct pf_rule *rm, 225 struct pf_rule *am, struct pf_ruleset *ruleset, struct pf_rule *trigger) 226 { 227 #if NBPFILTER > 0 228 struct ifnet *ifn; 229 struct pfloghdr hdr; 230 231 if (rm == NULL || pd == NULL || pd->kif == NULL || pd->m == NULL) 232 return (-1); 233 if (trigger == NULL) 234 trigger = rm; 235 236 if (trigger->logif >= npflogifs || (ifn = pflogifs[trigger->logif]) == 237 NULL || !ifn->if_bpf) 238 return (0); 239 240 bzero(&hdr, sizeof(hdr)); 241 hdr.length = PFLOG_REAL_HDRLEN; 242 hdr.action = rm->action; 243 hdr.reason = reason; 244 memcpy(hdr.ifname, pd->kif->pfik_name, sizeof(hdr.ifname)); 245 246 if (am == NULL) { 247 hdr.rulenr = htonl(rm->nr); 248 hdr.subrulenr = -1; 249 } else { 250 hdr.rulenr = htonl(am->nr); 251 hdr.subrulenr = htonl(rm->nr); 252 if (ruleset != NULL && ruleset->anchor != NULL) 253 strlcpy(hdr.ruleset, ruleset->anchor->name, 254 sizeof(hdr.ruleset)); 255 } 256 if (trigger->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done) 257 pd->lookup.done = pf_socket_lookup(pd); 258 if (pd->lookup.done > 0) { 259 hdr.uid = pd->lookup.uid; 260 hdr.pid = pd->lookup.pid; 261 } else { 262 hdr.uid = UID_MAX; 263 hdr.pid = NO_PID; 264 } 265 hdr.rule_uid = rm->cuid; 266 hdr.rule_pid = rm->cpid; 267 hdr.dir = pd->dir; 268 269 pf_addrcpy(&hdr.saddr, &pd->nsaddr, pd->naf); 270 pf_addrcpy(&hdr.daddr, &pd->ndaddr, pd->naf); 271 hdr.af = pd->af; 272 hdr.naf = pd->naf; 273 hdr.sport = pd->nsport; 274 hdr.dport = pd->ndport; 275 276 ifn->if_opackets++; 277 ifn->if_obytes += pd->m->m_pkthdr.len; 278 279 bpf_mtap_hdr(ifn->if_bpf, (caddr_t)&hdr, PFLOG_HDRLEN, pd->m, 280 BPF_DIRECTION_OUT, pflog_bpfcopy); 281 #endif 282 283 return (0); 284 } 285 286 void 287 pflog_bpfcopy(const void *src_arg, void *dst_arg, size_t len) 288 { 289 struct mbuf *m, *mp, *mhdr, *mptr; 290 struct pfloghdr *pfloghdr; 291 u_int count; 292 u_char *dst, *mdst; 293 int afto, hlen, mlen, off; 294 295 struct pf_pdesc pd; 296 struct pf_addr osaddr, odaddr; 297 u_int16_t osport = 0, odport = 0; 298 u_int8_t proto = 0; 299 300 m = (struct mbuf *)src_arg; 301 dst = dst_arg; 302 303 mhdr = pflog_mhdr; 304 mptr = pflog_mptr; 305 306 if (m == NULL) 307 panic("pflog_bpfcopy got no mbuf"); 308 309 /* first mbuf holds struct pfloghdr */ 310 pfloghdr = mtod(m, struct pfloghdr *); 311 afto = pfloghdr->af != pfloghdr->naf; 312 count = min(m->m_len, len); 313 bcopy(pfloghdr, dst, count); 314 pfloghdr = (struct pfloghdr *)dst; 315 dst += count; 316 len -= count; 317 m = m->m_next; 318 319 if (len <= 0) 320 return; 321 322 /* second mbuf is pkthdr */ 323 if (m == NULL) 324 panic("no second mbuf"); 325 326 /* 327 * temporary mbuf will hold an ip/ip6 header and 8 bytes 328 * of the protocol header 329 */ 330 m_inithdr(mhdr); 331 mhdr->m_len = 0; /* XXX not done in m_inithdr() */ 332 333 #ifdef INET6 334 /* offset for a new header */ 335 if (afto && pfloghdr->af == AF_INET) 336 mhdr->m_data += sizeof(struct ip6_hdr) - 337 sizeof(struct ip); 338 #endif /* INET6 */ 339 340 mdst = mtod(mhdr, char *); 341 switch (pfloghdr->af) { 342 case AF_INET: { 343 struct ip *h; 344 345 if (m->m_pkthdr.len < sizeof(*h)) 346 goto copy; 347 m_copydata(m, 0, sizeof(*h), mdst); 348 h = (struct ip *)mdst; 349 hlen = h->ip_hl << 2; 350 if (hlen > sizeof(*h) && (m->m_pkthdr.len >= hlen)) 351 m_copydata(m, sizeof(*h), hlen - sizeof(*h), 352 mdst + sizeof(*h)); 353 break; 354 } 355 #ifdef INET6 356 case AF_INET6: { 357 struct ip6_hdr *h; 358 359 if (m->m_pkthdr.len < sizeof(*h)) 360 goto copy; 361 hlen = sizeof(struct ip6_hdr); 362 m_copydata(m, 0, hlen, mdst); 363 h = (struct ip6_hdr *)mdst; 364 proto = h->ip6_nxt; 365 break; 366 } 367 #endif /* INET6 */ 368 default: 369 /* shouldn't happen ever :-) */ 370 goto copy; 371 } 372 373 if (m->m_pkthdr.len < hlen + 8 && proto != IPPROTO_NONE) 374 goto copy; 375 else if (proto != IPPROTO_NONE) { 376 /* copy 8 bytes of the protocol header */ 377 m_copydata(m, hlen, 8, mdst + hlen); 378 hlen += 8; 379 } 380 381 mhdr->m_len += hlen; 382 mhdr->m_pkthdr.len = mhdr->m_len; 383 384 /* create a chain mhdr -> mptr, mptr->m_data = (m->m_data+hlen) */ 385 mp = m_getptr(m, hlen, &off); 386 if (mp != NULL) { 387 bcopy(mp, mptr, sizeof(*mptr)); 388 mptr->m_data += off; 389 mptr->m_len -= off; 390 mptr->m_flags &= ~M_PKTHDR; 391 mhdr->m_next = mptr; 392 mhdr->m_pkthdr.len += m->m_pkthdr.len - hlen; 393 } 394 395 /* 396 * Rewrite addresses if needed. Reason pointer must be NULL to avoid 397 * counting the packet here again. 398 */ 399 if (pf_setup_pdesc(&pd, pfloghdr->af, pfloghdr->dir, NULL, 400 mhdr, NULL) != PF_PASS) 401 goto copy; 402 pd.naf = pfloghdr->naf; 403 404 pf_addrcpy(&osaddr, pd.src, pd.af); 405 pf_addrcpy(&odaddr, pd.dst, pd.af); 406 if (pd.sport) 407 osport = *pd.sport; 408 if (pd.dport) 409 odport = *pd.dport; 410 411 if (pd.virtual_proto != PF_VPROTO_FRAGMENT && 412 (pfloghdr->rewritten = pf_translate(&pd, &pfloghdr->saddr, 413 pfloghdr->sport, &pfloghdr->daddr, pfloghdr->dport, 0, 414 pfloghdr->dir))) { 415 m_copyback(pd.m, pd.off, min(pd.m->m_len - pd.off, pd.hdrlen), 416 &pd.hdr, M_NOWAIT); 417 #ifdef INET6 418 if (afto) { 419 pf_addrcpy(&pd.nsaddr, &pfloghdr->saddr, pd.naf); 420 pf_addrcpy(&pd.ndaddr, &pfloghdr->daddr, pd.naf); 421 } 422 #endif /* INET6 */ 423 pf_addrcpy(&pfloghdr->saddr, &osaddr, pd.af); 424 pf_addrcpy(&pfloghdr->daddr, &odaddr, pd.af); 425 pfloghdr->sport = osport; 426 pfloghdr->dport = odport; 427 } 428 429 pd.tot_len = min(pd.tot_len, len); 430 pd.tot_len -= pd.m->m_data - pd.m->m_pktdat; 431 432 #ifdef INET6 433 if (afto && pfloghdr->rewritten) 434 pf_translate_af(&pd); 435 #endif /* INET6 */ 436 437 m = pd.m; 438 copy: 439 mlen = min(m->m_pkthdr.len, len); 440 m_copydata(m, 0, mlen, dst); 441 len -= mlen; 442 if (len > 0) 443 bzero(dst + mlen, len); 444 } 445