if_pflog.c revision 1.2 1 /* $NetBSD: if_pflog.c,v 1.2 2004/06/22 14:17:07 itojun Exp $ */
2 /* $OpenBSD: if_pflog.c,v 1.11 2003/12/31 11:18:25 cedric Exp $ */
3 /*
4 * The authors of this code are John Ioannidis (ji (at) tla.org),
5 * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
6 * Niels Provos (provos (at) 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
37 #ifdef _KERNEL_OPT
38 #include "opt_inet.h"
39 #endif
40
41 #include "bpfilter.h"
42 #include "pflog.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/ioctl.h>
49
50 #include <net/if.h>
51 #include <net/if_types.h>
52 #include <net/route.h>
53 #include <net/bpf.h>
54
55 #ifdef INET
56 #include <netinet/in.h>
57 #include <netinet/in_var.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #endif
61
62 #ifdef INET6
63 #ifndef INET
64 #include <netinet/in.h>
65 #endif
66 #include <netinet6/nd6.h>
67 #endif /* INET6 */
68
69 #include <net/pfvar.h>
70 #include <net/if_pflog.h>
71
72 #define PFLOGMTU (32768 + MHLEN + MLEN)
73
74 #ifdef PFLOGDEBUG
75 #define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0)
76 #else
77 #define DPRINTF(x)
78 #endif
79
80 struct pflog_softc pflogif[NPFLOG];
81
82 void pflogattach(int);
83 int pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
84 struct rtentry *);
85 int pflogioctl(struct ifnet *, u_long, caddr_t);
86 void pflogrtrequest(int, struct rtentry *, struct sockaddr *);
87 void pflogstart(struct ifnet *);
88
89 extern int ifqmaxlen;
90
91 void
92 pflogattach(int npflog)
93 {
94 struct ifnet *ifp;
95 int i;
96
97 bzero(pflogif, sizeof(pflogif));
98
99 for (i = 0; i < NPFLOG; i++) {
100 ifp = &pflogif[i].sc_if;
101 snprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", i);
102 ifp->if_softc = &pflogif[i];
103 ifp->if_mtu = PFLOGMTU;
104 ifp->if_ioctl = pflogioctl;
105 ifp->if_output = pflogoutput;
106 ifp->if_start = pflogstart;
107 ifp->if_type = IFT_PFLOG;
108 ifp->if_snd.ifq_maxlen = ifqmaxlen;
109 ifp->if_hdrlen = PFLOG_HDRLEN;
110 if_attach(ifp);
111 if_alloc_sadl(ifp);
112
113 #if NBPFILTER > 0
114 #ifdef __OpenBSD__
115 bpfattach(&pflogif[i].sc_if.if_bpf, ifp, DLT_PFLOG,
116 PFLOG_HDRLEN);
117 #else
118 bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
119 #endif
120 #endif
121 }
122 }
123
124 /*
125 * Start output on the pflog interface.
126 */
127 void
128 pflogstart(struct ifnet *ifp)
129 {
130 struct mbuf *m;
131 int s;
132
133 for (;;) {
134 #ifdef __OpenBSD__
135 s = splimp();
136 #else
137 s = splnet();
138 #endif
139 IF_DROP(&ifp->if_snd);
140 IF_DEQUEUE(&ifp->if_snd, m);
141 splx(s);
142
143 if (m == NULL)
144 return;
145 else
146 m_freem(m);
147 }
148 }
149
150 int
151 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
152 struct rtentry *rt)
153 {
154 m_freem(m);
155 return (0);
156 }
157
158 /* ARGSUSED */
159 void
160 pflogrtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
161 {
162 if (rt)
163 rt->rt_rmx.rmx_mtu = PFLOGMTU;
164 }
165
166 /* ARGSUSED */
167 int
168 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
169 {
170 switch (cmd) {
171 case SIOCSIFADDR:
172 case SIOCAIFADDR:
173 case SIOCSIFDSTADDR:
174 case SIOCSIFFLAGS:
175 if (ifp->if_flags & IFF_UP)
176 ifp->if_flags |= IFF_RUNNING;
177 else
178 ifp->if_flags &= ~IFF_RUNNING;
179 break;
180 default:
181 return (EINVAL);
182 }
183
184 return (0);
185 }
186
187 int
188 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
189 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
190 struct pf_ruleset *ruleset)
191 {
192 #if NBPFILTER > 0
193 struct ifnet *ifn;
194 struct pfloghdr hdr;
195 struct mbuf m1;
196
197 if (kif == NULL || m == NULL || rm == NULL)
198 return (-1);
199
200 bzero(&hdr, sizeof(hdr));
201 hdr.length = PFLOG_REAL_HDRLEN;
202 hdr.af = af;
203 hdr.action = rm->action;
204 hdr.reason = reason;
205 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
206
207 if (am == NULL) {
208 hdr.rulenr = htonl(rm->nr);
209 hdr.subrulenr = -1;
210 } else {
211 hdr.rulenr = htonl(am->nr);
212 hdr.subrulenr = htonl(rm->nr);
213 if (ruleset != NULL)
214 memcpy(hdr.ruleset, ruleset->name,
215 sizeof(hdr.ruleset));
216
217
218 }
219 hdr.dir = dir;
220
221 #ifdef INET
222 if (af == AF_INET && dir == PF_OUT) {
223 struct ip *ip;
224
225 ip = mtod(m, struct ip *);
226 ip->ip_sum = 0;
227 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
228 }
229 #endif /* INET */
230
231 m1.m_next = m;
232 m1.m_len = PFLOG_HDRLEN;
233 m1.m_data = (char *) &hdr;
234
235 ifn = &(pflogif[0].sc_if);
236
237 if (ifn->if_bpf)
238 bpf_mtap(ifn->if_bpf, &m1);
239 #endif
240
241 return (0);
242 }
243