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