if_pflog.c revision 1.1 1 1.1 itojun /* $OpenBSD: if_pflog.c,v 1.11 2003/12/31 11:18:25 cedric Exp $ */
2 1.1 itojun /*
3 1.1 itojun * The authors of this code are John Ioannidis (ji (at) tla.org),
4 1.1 itojun * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
5 1.1 itojun * Niels Provos (provos (at) physnet.uni-hamburg.de).
6 1.1 itojun *
7 1.1 itojun * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
8 1.1 itojun * in November 1995.
9 1.1 itojun *
10 1.1 itojun * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
11 1.1 itojun * by Angelos D. Keromytis.
12 1.1 itojun *
13 1.1 itojun * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
14 1.1 itojun * and Niels Provos.
15 1.1 itojun *
16 1.1 itojun * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
17 1.1 itojun * and Niels Provos.
18 1.1 itojun * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
19 1.1 itojun *
20 1.1 itojun * Permission to use, copy, and modify this software with or without fee
21 1.1 itojun * is hereby granted, provided that this entire notice is included in
22 1.1 itojun * all copies of any software which is or includes a copy or
23 1.1 itojun * modification of this software.
24 1.1 itojun * You may use this code under the GNU public license if you so wish. Please
25 1.1 itojun * contribute changes back to the authors under this freer than GPL license
26 1.1 itojun * so that we may further the use of strong encryption without limitations to
27 1.1 itojun * all.
28 1.1 itojun *
29 1.1 itojun * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
30 1.1 itojun * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
31 1.1 itojun * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
32 1.1 itojun * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
33 1.1 itojun * PURPOSE.
34 1.1 itojun */
35 1.1 itojun
36 1.1 itojun #include "bpfilter.h"
37 1.1 itojun #include "pflog.h"
38 1.1 itojun
39 1.1 itojun #include <sys/param.h>
40 1.1 itojun #include <sys/systm.h>
41 1.1 itojun #include <sys/mbuf.h>
42 1.1 itojun #include <sys/socket.h>
43 1.1 itojun #include <sys/ioctl.h>
44 1.1 itojun
45 1.1 itojun #include <net/if.h>
46 1.1 itojun #include <net/if_types.h>
47 1.1 itojun #include <net/route.h>
48 1.1 itojun #include <net/bpf.h>
49 1.1 itojun
50 1.1 itojun #ifdef INET
51 1.1 itojun #include <netinet/in.h>
52 1.1 itojun #include <netinet/in_var.h>
53 1.1 itojun #include <netinet/in_systm.h>
54 1.1 itojun #include <netinet/ip.h>
55 1.1 itojun #endif
56 1.1 itojun
57 1.1 itojun #ifdef INET6
58 1.1 itojun #ifndef INET
59 1.1 itojun #include <netinet/in.h>
60 1.1 itojun #endif
61 1.1 itojun #include <netinet6/nd6.h>
62 1.1 itojun #endif /* INET6 */
63 1.1 itojun
64 1.1 itojun #include <net/pfvar.h>
65 1.1 itojun #include <net/if_pflog.h>
66 1.1 itojun
67 1.1 itojun #define PFLOGMTU (32768 + MHLEN + MLEN)
68 1.1 itojun
69 1.1 itojun #ifdef PFLOGDEBUG
70 1.1 itojun #define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0)
71 1.1 itojun #else
72 1.1 itojun #define DPRINTF(x)
73 1.1 itojun #endif
74 1.1 itojun
75 1.1 itojun struct pflog_softc pflogif[NPFLOG];
76 1.1 itojun
77 1.1 itojun void pflogattach(int);
78 1.1 itojun int pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
79 1.1 itojun struct rtentry *);
80 1.1 itojun int pflogioctl(struct ifnet *, u_long, caddr_t);
81 1.1 itojun void pflogrtrequest(int, struct rtentry *, struct sockaddr *);
82 1.1 itojun void pflogstart(struct ifnet *);
83 1.1 itojun
84 1.1 itojun extern int ifqmaxlen;
85 1.1 itojun
86 1.1 itojun void
87 1.1 itojun pflogattach(int npflog)
88 1.1 itojun {
89 1.1 itojun struct ifnet *ifp;
90 1.1 itojun int i;
91 1.1 itojun
92 1.1 itojun bzero(pflogif, sizeof(pflogif));
93 1.1 itojun
94 1.1 itojun for (i = 0; i < NPFLOG; i++) {
95 1.1 itojun ifp = &pflogif[i].sc_if;
96 1.1 itojun snprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", i);
97 1.1 itojun ifp->if_softc = &pflogif[i];
98 1.1 itojun ifp->if_mtu = PFLOGMTU;
99 1.1 itojun ifp->if_ioctl = pflogioctl;
100 1.1 itojun ifp->if_output = pflogoutput;
101 1.1 itojun ifp->if_start = pflogstart;
102 1.1 itojun ifp->if_type = IFT_PFLOG;
103 1.1 itojun ifp->if_snd.ifq_maxlen = ifqmaxlen;
104 1.1 itojun ifp->if_hdrlen = PFLOG_HDRLEN;
105 1.1 itojun if_attach(ifp);
106 1.1 itojun if_alloc_sadl(ifp);
107 1.1 itojun
108 1.1 itojun #if NBPFILTER > 0
109 1.1 itojun bpfattach(&pflogif[i].sc_if.if_bpf, ifp, DLT_PFLOG,
110 1.1 itojun PFLOG_HDRLEN);
111 1.1 itojun #endif
112 1.1 itojun }
113 1.1 itojun }
114 1.1 itojun
115 1.1 itojun /*
116 1.1 itojun * Start output on the pflog interface.
117 1.1 itojun */
118 1.1 itojun void
119 1.1 itojun pflogstart(struct ifnet *ifp)
120 1.1 itojun {
121 1.1 itojun struct mbuf *m;
122 1.1 itojun int s;
123 1.1 itojun
124 1.1 itojun for (;;) {
125 1.1 itojun s = splimp();
126 1.1 itojun IF_DROP(&ifp->if_snd);
127 1.1 itojun IF_DEQUEUE(&ifp->if_snd, m);
128 1.1 itojun splx(s);
129 1.1 itojun
130 1.1 itojun if (m == NULL)
131 1.1 itojun return;
132 1.1 itojun else
133 1.1 itojun m_freem(m);
134 1.1 itojun }
135 1.1 itojun }
136 1.1 itojun
137 1.1 itojun int
138 1.1 itojun pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
139 1.1 itojun struct rtentry *rt)
140 1.1 itojun {
141 1.1 itojun m_freem(m);
142 1.1 itojun return (0);
143 1.1 itojun }
144 1.1 itojun
145 1.1 itojun /* ARGSUSED */
146 1.1 itojun void
147 1.1 itojun pflogrtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
148 1.1 itojun {
149 1.1 itojun if (rt)
150 1.1 itojun rt->rt_rmx.rmx_mtu = PFLOGMTU;
151 1.1 itojun }
152 1.1 itojun
153 1.1 itojun /* ARGSUSED */
154 1.1 itojun int
155 1.1 itojun pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
156 1.1 itojun {
157 1.1 itojun switch (cmd) {
158 1.1 itojun case SIOCSIFADDR:
159 1.1 itojun case SIOCAIFADDR:
160 1.1 itojun case SIOCSIFDSTADDR:
161 1.1 itojun case SIOCSIFFLAGS:
162 1.1 itojun if (ifp->if_flags & IFF_UP)
163 1.1 itojun ifp->if_flags |= IFF_RUNNING;
164 1.1 itojun else
165 1.1 itojun ifp->if_flags &= ~IFF_RUNNING;
166 1.1 itojun break;
167 1.1 itojun default:
168 1.1 itojun return (EINVAL);
169 1.1 itojun }
170 1.1 itojun
171 1.1 itojun return (0);
172 1.1 itojun }
173 1.1 itojun
174 1.1 itojun int
175 1.1 itojun pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
176 1.1 itojun u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
177 1.1 itojun struct pf_ruleset *ruleset)
178 1.1 itojun {
179 1.1 itojun #if NBPFILTER > 0
180 1.1 itojun struct ifnet *ifn;
181 1.1 itojun struct pfloghdr hdr;
182 1.1 itojun struct mbuf m1;
183 1.1 itojun
184 1.1 itojun if (kif == NULL || m == NULL || rm == NULL)
185 1.1 itojun return (-1);
186 1.1 itojun
187 1.1 itojun bzero(&hdr, sizeof(hdr));
188 1.1 itojun hdr.length = PFLOG_REAL_HDRLEN;
189 1.1 itojun hdr.af = af;
190 1.1 itojun hdr.action = rm->action;
191 1.1 itojun hdr.reason = reason;
192 1.1 itojun memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
193 1.1 itojun
194 1.1 itojun if (am == NULL) {
195 1.1 itojun hdr.rulenr = htonl(rm->nr);
196 1.1 itojun hdr.subrulenr = -1;
197 1.1 itojun } else {
198 1.1 itojun hdr.rulenr = htonl(am->nr);
199 1.1 itojun hdr.subrulenr = htonl(rm->nr);
200 1.1 itojun if (ruleset != NULL)
201 1.1 itojun memcpy(hdr.ruleset, ruleset->name,
202 1.1 itojun sizeof(hdr.ruleset));
203 1.1 itojun
204 1.1 itojun
205 1.1 itojun }
206 1.1 itojun hdr.dir = dir;
207 1.1 itojun
208 1.1 itojun #ifdef INET
209 1.1 itojun if (af == AF_INET && dir == PF_OUT) {
210 1.1 itojun struct ip *ip;
211 1.1 itojun
212 1.1 itojun ip = mtod(m, struct ip *);
213 1.1 itojun ip->ip_sum = 0;
214 1.1 itojun ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
215 1.1 itojun }
216 1.1 itojun #endif /* INET */
217 1.1 itojun
218 1.1 itojun m1.m_next = m;
219 1.1 itojun m1.m_len = PFLOG_HDRLEN;
220 1.1 itojun m1.m_data = (char *) &hdr;
221 1.1 itojun
222 1.1 itojun ifn = &(pflogif[0].sc_if);
223 1.1 itojun
224 1.1 itojun if (ifn->if_bpf)
225 1.1 itojun bpf_mtap(ifn->if_bpf, &m1);
226 1.1 itojun #endif
227 1.1 itojun
228 1.1 itojun return (0);
229 1.1 itojun }
230