1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
3*8b26181fSAndroid Build Coastguard Worker * The Regents of the University of California. All rights reserved.
4*8b26181fSAndroid Build Coastguard Worker *
5*8b26181fSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*8b26181fSAndroid Build Coastguard Worker * modification, are permitted provided that: (1) source code distributions
7*8b26181fSAndroid Build Coastguard Worker * retain the above copyright notice and this paragraph in its entirety, (2)
8*8b26181fSAndroid Build Coastguard Worker * distributions including binary code include the above copyright notice and
9*8b26181fSAndroid Build Coastguard Worker * this paragraph in its entirety in the documentation or other materials
10*8b26181fSAndroid Build Coastguard Worker * provided with the distribution, and (3) all advertising materials mentioning
11*8b26181fSAndroid Build Coastguard Worker * features or use of this software display the following acknowledgement:
12*8b26181fSAndroid Build Coastguard Worker * ``This product includes software developed by the University of California,
13*8b26181fSAndroid Build Coastguard Worker * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*8b26181fSAndroid Build Coastguard Worker * the University nor the names of its contributors may be used to endorse
15*8b26181fSAndroid Build Coastguard Worker * or promote products derived from this software without specific prior
16*8b26181fSAndroid Build Coastguard Worker * written permission.
17*8b26181fSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*8b26181fSAndroid Build Coastguard Worker * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*8b26181fSAndroid Build Coastguard Worker * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*8b26181fSAndroid Build Coastguard Worker *
21*8b26181fSAndroid Build Coastguard Worker * Modifications made to accommodate the new SunOS4.0 NIT facility by
22*8b26181fSAndroid Build Coastguard Worker * Micky Liu, [email protected], Columbia University in May, 1989.
23*8b26181fSAndroid Build Coastguard Worker * This module now handles the STREAMS based NIT.
24*8b26181fSAndroid Build Coastguard Worker */
25*8b26181fSAndroid Build Coastguard Worker
26*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
27*8b26181fSAndroid Build Coastguard Worker #include <config.h>
28*8b26181fSAndroid Build Coastguard Worker #endif
29*8b26181fSAndroid Build Coastguard Worker
30*8b26181fSAndroid Build Coastguard Worker #include <sys/types.h>
31*8b26181fSAndroid Build Coastguard Worker #include <sys/time.h>
32*8b26181fSAndroid Build Coastguard Worker #include <sys/timeb.h>
33*8b26181fSAndroid Build Coastguard Worker #include <sys/dir.h>
34*8b26181fSAndroid Build Coastguard Worker #include <sys/fcntlcom.h>
35*8b26181fSAndroid Build Coastguard Worker #include <sys/file.h>
36*8b26181fSAndroid Build Coastguard Worker #include <sys/ioctl.h>
37*8b26181fSAndroid Build Coastguard Worker #include <sys/socket.h>
38*8b26181fSAndroid Build Coastguard Worker #include <sys/stropts.h>
39*8b26181fSAndroid Build Coastguard Worker
40*8b26181fSAndroid Build Coastguard Worker #include <net/if.h>
41*8b26181fSAndroid Build Coastguard Worker #include <net/nit.h>
42*8b26181fSAndroid Build Coastguard Worker #include <net/nit_if.h>
43*8b26181fSAndroid Build Coastguard Worker #include <net/nit_pf.h>
44*8b26181fSAndroid Build Coastguard Worker #include <net/nit_buf.h>
45*8b26181fSAndroid Build Coastguard Worker
46*8b26181fSAndroid Build Coastguard Worker #include <netinet/in.h>
47*8b26181fSAndroid Build Coastguard Worker #include <netinet/in_systm.h>
48*8b26181fSAndroid Build Coastguard Worker #include <netinet/ip.h>
49*8b26181fSAndroid Build Coastguard Worker #include <netinet/if_ether.h>
50*8b26181fSAndroid Build Coastguard Worker #include <netinet/ip_var.h>
51*8b26181fSAndroid Build Coastguard Worker #include <netinet/udp.h>
52*8b26181fSAndroid Build Coastguard Worker #include <netinet/udp_var.h>
53*8b26181fSAndroid Build Coastguard Worker #include <netinet/tcp.h>
54*8b26181fSAndroid Build Coastguard Worker #include <netinet/tcpip.h>
55*8b26181fSAndroid Build Coastguard Worker
56*8b26181fSAndroid Build Coastguard Worker #include <errno.h>
57*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
58*8b26181fSAndroid Build Coastguard Worker #include <string.h>
59*8b26181fSAndroid Build Coastguard Worker #include <unistd.h>
60*8b26181fSAndroid Build Coastguard Worker
61*8b26181fSAndroid Build Coastguard Worker #include "pcap-int.h"
62*8b26181fSAndroid Build Coastguard Worker
63*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OS_PROTO_H
64*8b26181fSAndroid Build Coastguard Worker #include "os-proto.h"
65*8b26181fSAndroid Build Coastguard Worker #endif
66*8b26181fSAndroid Build Coastguard Worker
67*8b26181fSAndroid Build Coastguard Worker /*
68*8b26181fSAndroid Build Coastguard Worker * The chunk size for NIT. This is the amount of buffering
69*8b26181fSAndroid Build Coastguard Worker * done for read calls.
70*8b26181fSAndroid Build Coastguard Worker */
71*8b26181fSAndroid Build Coastguard Worker #define CHUNKSIZE (2*1024)
72*8b26181fSAndroid Build Coastguard Worker
73*8b26181fSAndroid Build Coastguard Worker /*
74*8b26181fSAndroid Build Coastguard Worker * The total buffer space used by NIT.
75*8b26181fSAndroid Build Coastguard Worker */
76*8b26181fSAndroid Build Coastguard Worker #define BUFSPACE (4*CHUNKSIZE)
77*8b26181fSAndroid Build Coastguard Worker
78*8b26181fSAndroid Build Coastguard Worker /* Forwards */
79*8b26181fSAndroid Build Coastguard Worker static int nit_setflags(int, int, int, char *);
80*8b26181fSAndroid Build Coastguard Worker
81*8b26181fSAndroid Build Coastguard Worker /*
82*8b26181fSAndroid Build Coastguard Worker * Private data for capturing on STREAMS NIT devices.
83*8b26181fSAndroid Build Coastguard Worker */
84*8b26181fSAndroid Build Coastguard Worker struct pcap_snit {
85*8b26181fSAndroid Build Coastguard Worker struct pcap_stat stat;
86*8b26181fSAndroid Build Coastguard Worker };
87*8b26181fSAndroid Build Coastguard Worker
88*8b26181fSAndroid Build Coastguard Worker static int
pcap_stats_snit(pcap_t * p,struct pcap_stat * ps)89*8b26181fSAndroid Build Coastguard Worker pcap_stats_snit(pcap_t *p, struct pcap_stat *ps)
90*8b26181fSAndroid Build Coastguard Worker {
91*8b26181fSAndroid Build Coastguard Worker struct pcap_snit *psn = p->priv;
92*8b26181fSAndroid Build Coastguard Worker
93*8b26181fSAndroid Build Coastguard Worker /*
94*8b26181fSAndroid Build Coastguard Worker * "ps_recv" counts packets handed to the filter, not packets
95*8b26181fSAndroid Build Coastguard Worker * that passed the filter. As filtering is done in userland,
96*8b26181fSAndroid Build Coastguard Worker * this does not include packets dropped because we ran out
97*8b26181fSAndroid Build Coastguard Worker * of buffer space.
98*8b26181fSAndroid Build Coastguard Worker *
99*8b26181fSAndroid Build Coastguard Worker * "ps_drop" counts packets dropped inside the "/dev/nit"
100*8b26181fSAndroid Build Coastguard Worker * device because of flow control requirements or resource
101*8b26181fSAndroid Build Coastguard Worker * exhaustion; it doesn't count packets dropped by the
102*8b26181fSAndroid Build Coastguard Worker * interface driver, or packets dropped upstream. As filtering
103*8b26181fSAndroid Build Coastguard Worker * is done in userland, it counts packets regardless of whether
104*8b26181fSAndroid Build Coastguard Worker * they would've passed the filter.
105*8b26181fSAndroid Build Coastguard Worker *
106*8b26181fSAndroid Build Coastguard Worker * These statistics don't include packets not yet read from the
107*8b26181fSAndroid Build Coastguard Worker * kernel by libpcap or packets not yet read from libpcap by the
108*8b26181fSAndroid Build Coastguard Worker * application.
109*8b26181fSAndroid Build Coastguard Worker */
110*8b26181fSAndroid Build Coastguard Worker *ps = psn->stat;
111*8b26181fSAndroid Build Coastguard Worker return (0);
112*8b26181fSAndroid Build Coastguard Worker }
113*8b26181fSAndroid Build Coastguard Worker
114*8b26181fSAndroid Build Coastguard Worker static int
pcap_read_snit(pcap_t * p,int cnt,pcap_handler callback,u_char * user)115*8b26181fSAndroid Build Coastguard Worker pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
116*8b26181fSAndroid Build Coastguard Worker {
117*8b26181fSAndroid Build Coastguard Worker struct pcap_snit *psn = p->priv;
118*8b26181fSAndroid Build Coastguard Worker register int cc, n;
119*8b26181fSAndroid Build Coastguard Worker register u_char *bp, *cp, *ep;
120*8b26181fSAndroid Build Coastguard Worker register struct nit_bufhdr *hdrp;
121*8b26181fSAndroid Build Coastguard Worker register struct nit_iftime *ntp;
122*8b26181fSAndroid Build Coastguard Worker register struct nit_iflen *nlp;
123*8b26181fSAndroid Build Coastguard Worker register struct nit_ifdrops *ndp;
124*8b26181fSAndroid Build Coastguard Worker register int caplen;
125*8b26181fSAndroid Build Coastguard Worker
126*8b26181fSAndroid Build Coastguard Worker cc = p->cc;
127*8b26181fSAndroid Build Coastguard Worker if (cc == 0) {
128*8b26181fSAndroid Build Coastguard Worker cc = read(p->fd, (char *)p->buffer, p->bufsize);
129*8b26181fSAndroid Build Coastguard Worker if (cc < 0) {
130*8b26181fSAndroid Build Coastguard Worker if (errno == EWOULDBLOCK)
131*8b26181fSAndroid Build Coastguard Worker return (0);
132*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
133*8b26181fSAndroid Build Coastguard Worker errno, "pcap_read");
134*8b26181fSAndroid Build Coastguard Worker return (-1);
135*8b26181fSAndroid Build Coastguard Worker }
136*8b26181fSAndroid Build Coastguard Worker bp = (u_char *)p->buffer;
137*8b26181fSAndroid Build Coastguard Worker } else
138*8b26181fSAndroid Build Coastguard Worker bp = p->bp;
139*8b26181fSAndroid Build Coastguard Worker
140*8b26181fSAndroid Build Coastguard Worker /*
141*8b26181fSAndroid Build Coastguard Worker * loop through each snapshot in the chunk
142*8b26181fSAndroid Build Coastguard Worker *
143*8b26181fSAndroid Build Coastguard Worker * This assumes that a single buffer of packets will have
144*8b26181fSAndroid Build Coastguard Worker * <= INT_MAX packets, so the packet count doesn't overflow.
145*8b26181fSAndroid Build Coastguard Worker */
146*8b26181fSAndroid Build Coastguard Worker n = 0;
147*8b26181fSAndroid Build Coastguard Worker ep = bp + cc;
148*8b26181fSAndroid Build Coastguard Worker while (bp < ep) {
149*8b26181fSAndroid Build Coastguard Worker /*
150*8b26181fSAndroid Build Coastguard Worker * Has "pcap_breakloop()" been called?
151*8b26181fSAndroid Build Coastguard Worker * If so, return immediately - if we haven't read any
152*8b26181fSAndroid Build Coastguard Worker * packets, clear the flag and return -2 to indicate
153*8b26181fSAndroid Build Coastguard Worker * that we were told to break out of the loop, otherwise
154*8b26181fSAndroid Build Coastguard Worker * leave the flag set, so that the *next* call will break
155*8b26181fSAndroid Build Coastguard Worker * out of the loop without having read any packets, and
156*8b26181fSAndroid Build Coastguard Worker * return the number of packets we've processed so far.
157*8b26181fSAndroid Build Coastguard Worker */
158*8b26181fSAndroid Build Coastguard Worker if (p->break_loop) {
159*8b26181fSAndroid Build Coastguard Worker if (n == 0) {
160*8b26181fSAndroid Build Coastguard Worker p->break_loop = 0;
161*8b26181fSAndroid Build Coastguard Worker return (-2);
162*8b26181fSAndroid Build Coastguard Worker } else {
163*8b26181fSAndroid Build Coastguard Worker p->bp = bp;
164*8b26181fSAndroid Build Coastguard Worker p->cc = ep - bp;
165*8b26181fSAndroid Build Coastguard Worker return (n);
166*8b26181fSAndroid Build Coastguard Worker }
167*8b26181fSAndroid Build Coastguard Worker }
168*8b26181fSAndroid Build Coastguard Worker
169*8b26181fSAndroid Build Coastguard Worker ++psn->stat.ps_recv;
170*8b26181fSAndroid Build Coastguard Worker cp = bp;
171*8b26181fSAndroid Build Coastguard Worker
172*8b26181fSAndroid Build Coastguard Worker /* get past NIT buffer */
173*8b26181fSAndroid Build Coastguard Worker hdrp = (struct nit_bufhdr *)cp;
174*8b26181fSAndroid Build Coastguard Worker cp += sizeof(*hdrp);
175*8b26181fSAndroid Build Coastguard Worker
176*8b26181fSAndroid Build Coastguard Worker /* get past NIT timer */
177*8b26181fSAndroid Build Coastguard Worker ntp = (struct nit_iftime *)cp;
178*8b26181fSAndroid Build Coastguard Worker cp += sizeof(*ntp);
179*8b26181fSAndroid Build Coastguard Worker
180*8b26181fSAndroid Build Coastguard Worker ndp = (struct nit_ifdrops *)cp;
181*8b26181fSAndroid Build Coastguard Worker psn->stat.ps_drop = ndp->nh_drops;
182*8b26181fSAndroid Build Coastguard Worker cp += sizeof *ndp;
183*8b26181fSAndroid Build Coastguard Worker
184*8b26181fSAndroid Build Coastguard Worker /* get past packet len */
185*8b26181fSAndroid Build Coastguard Worker nlp = (struct nit_iflen *)cp;
186*8b26181fSAndroid Build Coastguard Worker cp += sizeof(*nlp);
187*8b26181fSAndroid Build Coastguard Worker
188*8b26181fSAndroid Build Coastguard Worker /* next snapshot */
189*8b26181fSAndroid Build Coastguard Worker bp += hdrp->nhb_totlen;
190*8b26181fSAndroid Build Coastguard Worker
191*8b26181fSAndroid Build Coastguard Worker caplen = nlp->nh_pktlen;
192*8b26181fSAndroid Build Coastguard Worker if (caplen > p->snapshot)
193*8b26181fSAndroid Build Coastguard Worker caplen = p->snapshot;
194*8b26181fSAndroid Build Coastguard Worker
195*8b26181fSAndroid Build Coastguard Worker if (pcap_filter(p->fcode.bf_insns, cp, nlp->nh_pktlen, caplen)) {
196*8b26181fSAndroid Build Coastguard Worker struct pcap_pkthdr h;
197*8b26181fSAndroid Build Coastguard Worker h.ts = ntp->nh_timestamp;
198*8b26181fSAndroid Build Coastguard Worker h.len = nlp->nh_pktlen;
199*8b26181fSAndroid Build Coastguard Worker h.caplen = caplen;
200*8b26181fSAndroid Build Coastguard Worker (*callback)(user, &h, cp);
201*8b26181fSAndroid Build Coastguard Worker if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
202*8b26181fSAndroid Build Coastguard Worker p->cc = ep - bp;
203*8b26181fSAndroid Build Coastguard Worker p->bp = bp;
204*8b26181fSAndroid Build Coastguard Worker return (n);
205*8b26181fSAndroid Build Coastguard Worker }
206*8b26181fSAndroid Build Coastguard Worker }
207*8b26181fSAndroid Build Coastguard Worker }
208*8b26181fSAndroid Build Coastguard Worker p->cc = 0;
209*8b26181fSAndroid Build Coastguard Worker return (n);
210*8b26181fSAndroid Build Coastguard Worker }
211*8b26181fSAndroid Build Coastguard Worker
212*8b26181fSAndroid Build Coastguard Worker static int
pcap_inject_snit(pcap_t * p,const void * buf,int size)213*8b26181fSAndroid Build Coastguard Worker pcap_inject_snit(pcap_t *p, const void *buf, int size)
214*8b26181fSAndroid Build Coastguard Worker {
215*8b26181fSAndroid Build Coastguard Worker struct strbuf ctl, data;
216*8b26181fSAndroid Build Coastguard Worker
217*8b26181fSAndroid Build Coastguard Worker /*
218*8b26181fSAndroid Build Coastguard Worker * XXX - can we just do
219*8b26181fSAndroid Build Coastguard Worker *
220*8b26181fSAndroid Build Coastguard Worker ret = write(pd->f, buf, size);
221*8b26181fSAndroid Build Coastguard Worker */
222*8b26181fSAndroid Build Coastguard Worker ctl.len = sizeof(*sa); /* XXX - what was this? */
223*8b26181fSAndroid Build Coastguard Worker ctl.buf = (char *)sa;
224*8b26181fSAndroid Build Coastguard Worker data.buf = buf;
225*8b26181fSAndroid Build Coastguard Worker data.len = size;
226*8b26181fSAndroid Build Coastguard Worker ret = putmsg(p->fd, &ctl, &data);
227*8b26181fSAndroid Build Coastguard Worker if (ret == -1) {
228*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
229*8b26181fSAndroid Build Coastguard Worker errno, "send");
230*8b26181fSAndroid Build Coastguard Worker return (-1);
231*8b26181fSAndroid Build Coastguard Worker }
232*8b26181fSAndroid Build Coastguard Worker return (ret);
233*8b26181fSAndroid Build Coastguard Worker }
234*8b26181fSAndroid Build Coastguard Worker
235*8b26181fSAndroid Build Coastguard Worker static int
nit_setflags(pcap_t * p)236*8b26181fSAndroid Build Coastguard Worker nit_setflags(pcap_t *p)
237*8b26181fSAndroid Build Coastguard Worker {
238*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 flags;
239*8b26181fSAndroid Build Coastguard Worker struct strioctl si;
240*8b26181fSAndroid Build Coastguard Worker u_int zero = 0;
241*8b26181fSAndroid Build Coastguard Worker struct timeval timeout;
242*8b26181fSAndroid Build Coastguard Worker
243*8b26181fSAndroid Build Coastguard Worker if (p->opt.immediate) {
244*8b26181fSAndroid Build Coastguard Worker /*
245*8b26181fSAndroid Build Coastguard Worker * Set the chunk size to zero, so that chunks get sent
246*8b26181fSAndroid Build Coastguard Worker * up immediately.
247*8b26181fSAndroid Build Coastguard Worker */
248*8b26181fSAndroid Build Coastguard Worker si.ic_cmd = NIOCSCHUNK;
249*8b26181fSAndroid Build Coastguard Worker si.ic_len = sizeof(zero);
250*8b26181fSAndroid Build Coastguard Worker si.ic_dp = (char *)&zero;
251*8b26181fSAndroid Build Coastguard Worker if (ioctl(p->fd, I_STR, (char *)&si) < 0) {
252*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
253*8b26181fSAndroid Build Coastguard Worker errno, "NIOCSCHUNK");
254*8b26181fSAndroid Build Coastguard Worker return (-1);
255*8b26181fSAndroid Build Coastguard Worker }
256*8b26181fSAndroid Build Coastguard Worker }
257*8b26181fSAndroid Build Coastguard Worker si.ic_timout = INFTIM;
258*8b26181fSAndroid Build Coastguard Worker if (p->opt.timeout != 0) {
259*8b26181fSAndroid Build Coastguard Worker timeout.tv_sec = p->opt.timeout / 1000;
260*8b26181fSAndroid Build Coastguard Worker timeout.tv_usec = (p->opt.timeout * 1000) % 1000000;
261*8b26181fSAndroid Build Coastguard Worker si.ic_cmd = NIOCSTIME;
262*8b26181fSAndroid Build Coastguard Worker si.ic_len = sizeof(timeout);
263*8b26181fSAndroid Build Coastguard Worker si.ic_dp = (char *)&timeout;
264*8b26181fSAndroid Build Coastguard Worker if (ioctl(p->fd, I_STR, (char *)&si) < 0) {
265*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
266*8b26181fSAndroid Build Coastguard Worker errno, "NIOCSTIME");
267*8b26181fSAndroid Build Coastguard Worker return (-1);
268*8b26181fSAndroid Build Coastguard Worker }
269*8b26181fSAndroid Build Coastguard Worker }
270*8b26181fSAndroid Build Coastguard Worker flags = NI_TIMESTAMP | NI_LEN | NI_DROPS;
271*8b26181fSAndroid Build Coastguard Worker if (p->opt.promisc)
272*8b26181fSAndroid Build Coastguard Worker flags |= NI_PROMISC;
273*8b26181fSAndroid Build Coastguard Worker si.ic_cmd = NIOCSFLAGS;
274*8b26181fSAndroid Build Coastguard Worker si.ic_len = sizeof(flags);
275*8b26181fSAndroid Build Coastguard Worker si.ic_dp = (char *)&flags;
276*8b26181fSAndroid Build Coastguard Worker if (ioctl(p->fd, I_STR, (char *)&si) < 0) {
277*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
278*8b26181fSAndroid Build Coastguard Worker errno, "NIOCSFLAGS");
279*8b26181fSAndroid Build Coastguard Worker return (-1);
280*8b26181fSAndroid Build Coastguard Worker }
281*8b26181fSAndroid Build Coastguard Worker return (0);
282*8b26181fSAndroid Build Coastguard Worker }
283*8b26181fSAndroid Build Coastguard Worker
284*8b26181fSAndroid Build Coastguard Worker static int
pcap_activate_snit(pcap_t * p)285*8b26181fSAndroid Build Coastguard Worker pcap_activate_snit(pcap_t *p)
286*8b26181fSAndroid Build Coastguard Worker {
287*8b26181fSAndroid Build Coastguard Worker struct strioctl si; /* struct for ioctl() */
288*8b26181fSAndroid Build Coastguard Worker struct ifreq ifr; /* interface request struct */
289*8b26181fSAndroid Build Coastguard Worker int chunksize = CHUNKSIZE;
290*8b26181fSAndroid Build Coastguard Worker int fd;
291*8b26181fSAndroid Build Coastguard Worker static const char dev[] = "/dev/nit";
292*8b26181fSAndroid Build Coastguard Worker int err;
293*8b26181fSAndroid Build Coastguard Worker
294*8b26181fSAndroid Build Coastguard Worker if (p->opt.rfmon) {
295*8b26181fSAndroid Build Coastguard Worker /*
296*8b26181fSAndroid Build Coastguard Worker * No monitor mode on SunOS 4.x (no Wi-Fi devices on
297*8b26181fSAndroid Build Coastguard Worker * hardware supported by SunOS 4.x).
298*8b26181fSAndroid Build Coastguard Worker */
299*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_RFMON_NOTSUP);
300*8b26181fSAndroid Build Coastguard Worker }
301*8b26181fSAndroid Build Coastguard Worker
302*8b26181fSAndroid Build Coastguard Worker /*
303*8b26181fSAndroid Build Coastguard Worker * Turn a negative snapshot value (invalid), a snapshot value of
304*8b26181fSAndroid Build Coastguard Worker * 0 (unspecified), or a value bigger than the normal maximum
305*8b26181fSAndroid Build Coastguard Worker * value, into the maximum allowed value.
306*8b26181fSAndroid Build Coastguard Worker *
307*8b26181fSAndroid Build Coastguard Worker * If some application really *needs* a bigger snapshot
308*8b26181fSAndroid Build Coastguard Worker * length, we should just increase MAXIMUM_SNAPLEN.
309*8b26181fSAndroid Build Coastguard Worker */
310*8b26181fSAndroid Build Coastguard Worker if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
311*8b26181fSAndroid Build Coastguard Worker p->snapshot = MAXIMUM_SNAPLEN;
312*8b26181fSAndroid Build Coastguard Worker
313*8b26181fSAndroid Build Coastguard Worker if (p->snapshot < 96)
314*8b26181fSAndroid Build Coastguard Worker /*
315*8b26181fSAndroid Build Coastguard Worker * NIT requires a snapshot length of at least 96.
316*8b26181fSAndroid Build Coastguard Worker */
317*8b26181fSAndroid Build Coastguard Worker p->snapshot = 96;
318*8b26181fSAndroid Build Coastguard Worker
319*8b26181fSAndroid Build Coastguard Worker /*
320*8b26181fSAndroid Build Coastguard Worker * Initially try a read/write open (to allow the inject
321*8b26181fSAndroid Build Coastguard Worker * method to work). If that fails due to permission
322*8b26181fSAndroid Build Coastguard Worker * issues, fall back to read-only. This allows a
323*8b26181fSAndroid Build Coastguard Worker * non-root user to be granted specific access to pcap
324*8b26181fSAndroid Build Coastguard Worker * capabilities via file permissions.
325*8b26181fSAndroid Build Coastguard Worker *
326*8b26181fSAndroid Build Coastguard Worker * XXX - we should have an API that has a flag that
327*8b26181fSAndroid Build Coastguard Worker * controls whether to open read-only or read-write,
328*8b26181fSAndroid Build Coastguard Worker * so that denial of permission to send (or inability
329*8b26181fSAndroid Build Coastguard Worker * to send, if sending packets isn't supported on
330*8b26181fSAndroid Build Coastguard Worker * the device in question) can be indicated at open
331*8b26181fSAndroid Build Coastguard Worker * time.
332*8b26181fSAndroid Build Coastguard Worker */
333*8b26181fSAndroid Build Coastguard Worker p->fd = fd = open(dev, O_RDWR);
334*8b26181fSAndroid Build Coastguard Worker if (fd < 0 && errno == EACCES)
335*8b26181fSAndroid Build Coastguard Worker p->fd = fd = open(dev, O_RDONLY);
336*8b26181fSAndroid Build Coastguard Worker if (fd < 0) {
337*8b26181fSAndroid Build Coastguard Worker if (errno == EACCES) {
338*8b26181fSAndroid Build Coastguard Worker err = PCAP_ERROR_PERM_DENIED;
339*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
340*8b26181fSAndroid Build Coastguard Worker "Attempt to open %s failed with EACCES - root privileges may be required",
341*8b26181fSAndroid Build Coastguard Worker dev);
342*8b26181fSAndroid Build Coastguard Worker } else {
343*8b26181fSAndroid Build Coastguard Worker err = PCAP_ERROR;
344*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
345*8b26181fSAndroid Build Coastguard Worker errno, "%s", dev);
346*8b26181fSAndroid Build Coastguard Worker }
347*8b26181fSAndroid Build Coastguard Worker goto bad;
348*8b26181fSAndroid Build Coastguard Worker }
349*8b26181fSAndroid Build Coastguard Worker
350*8b26181fSAndroid Build Coastguard Worker /* arrange to get discrete messages from the STREAM and use NIT_BUF */
351*8b26181fSAndroid Build Coastguard Worker if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) {
352*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
353*8b26181fSAndroid Build Coastguard Worker errno, "I_SRDOPT");
354*8b26181fSAndroid Build Coastguard Worker err = PCAP_ERROR;
355*8b26181fSAndroid Build Coastguard Worker goto bad;
356*8b26181fSAndroid Build Coastguard Worker }
357*8b26181fSAndroid Build Coastguard Worker if (ioctl(fd, I_PUSH, "nbuf") < 0) {
358*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
359*8b26181fSAndroid Build Coastguard Worker errno, "push nbuf");
360*8b26181fSAndroid Build Coastguard Worker err = PCAP_ERROR;
361*8b26181fSAndroid Build Coastguard Worker goto bad;
362*8b26181fSAndroid Build Coastguard Worker }
363*8b26181fSAndroid Build Coastguard Worker /* set the chunksize */
364*8b26181fSAndroid Build Coastguard Worker si.ic_cmd = NIOCSCHUNK;
365*8b26181fSAndroid Build Coastguard Worker si.ic_timout = INFTIM;
366*8b26181fSAndroid Build Coastguard Worker si.ic_len = sizeof(chunksize);
367*8b26181fSAndroid Build Coastguard Worker si.ic_dp = (char *)&chunksize;
368*8b26181fSAndroid Build Coastguard Worker if (ioctl(fd, I_STR, (char *)&si) < 0) {
369*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
370*8b26181fSAndroid Build Coastguard Worker errno, "NIOCSCHUNK");
371*8b26181fSAndroid Build Coastguard Worker err = PCAP_ERROR;
372*8b26181fSAndroid Build Coastguard Worker goto bad;
373*8b26181fSAndroid Build Coastguard Worker }
374*8b26181fSAndroid Build Coastguard Worker
375*8b26181fSAndroid Build Coastguard Worker /* request the interface */
376*8b26181fSAndroid Build Coastguard Worker strncpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name));
377*8b26181fSAndroid Build Coastguard Worker ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
378*8b26181fSAndroid Build Coastguard Worker si.ic_cmd = NIOCBIND;
379*8b26181fSAndroid Build Coastguard Worker si.ic_len = sizeof(ifr);
380*8b26181fSAndroid Build Coastguard Worker si.ic_dp = (char *)𝔦
381*8b26181fSAndroid Build Coastguard Worker if (ioctl(fd, I_STR, (char *)&si) < 0) {
382*8b26181fSAndroid Build Coastguard Worker /*
383*8b26181fSAndroid Build Coastguard Worker * XXX - is there an error that means "no such device"?
384*8b26181fSAndroid Build Coastguard Worker * Is there one that means "that device doesn't support
385*8b26181fSAndroid Build Coastguard Worker * STREAMS NIT"?
386*8b26181fSAndroid Build Coastguard Worker */
387*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
388*8b26181fSAndroid Build Coastguard Worker errno, "NIOCBIND: %s", ifr.ifr_name);
389*8b26181fSAndroid Build Coastguard Worker err = PCAP_ERROR;
390*8b26181fSAndroid Build Coastguard Worker goto bad;
391*8b26181fSAndroid Build Coastguard Worker }
392*8b26181fSAndroid Build Coastguard Worker
393*8b26181fSAndroid Build Coastguard Worker /* set the snapshot length */
394*8b26181fSAndroid Build Coastguard Worker si.ic_cmd = NIOCSSNAP;
395*8b26181fSAndroid Build Coastguard Worker si.ic_len = sizeof(p->snapshot);
396*8b26181fSAndroid Build Coastguard Worker si.ic_dp = (char *)&p->snapshot;
397*8b26181fSAndroid Build Coastguard Worker if (ioctl(fd, I_STR, (char *)&si) < 0) {
398*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
399*8b26181fSAndroid Build Coastguard Worker errno, "NIOCSSNAP");
400*8b26181fSAndroid Build Coastguard Worker err = PCAP_ERROR;
401*8b26181fSAndroid Build Coastguard Worker goto bad;
402*8b26181fSAndroid Build Coastguard Worker }
403*8b26181fSAndroid Build Coastguard Worker if (nit_setflags(p) < 0) {
404*8b26181fSAndroid Build Coastguard Worker err = PCAP_ERROR;
405*8b26181fSAndroid Build Coastguard Worker goto bad;
406*8b26181fSAndroid Build Coastguard Worker }
407*8b26181fSAndroid Build Coastguard Worker
408*8b26181fSAndroid Build Coastguard Worker (void)ioctl(fd, I_FLUSH, (char *)FLUSHR);
409*8b26181fSAndroid Build Coastguard Worker /*
410*8b26181fSAndroid Build Coastguard Worker * NIT supports only ethernets.
411*8b26181fSAndroid Build Coastguard Worker */
412*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_EN10MB;
413*8b26181fSAndroid Build Coastguard Worker
414*8b26181fSAndroid Build Coastguard Worker p->bufsize = BUFSPACE;
415*8b26181fSAndroid Build Coastguard Worker p->buffer = malloc(p->bufsize);
416*8b26181fSAndroid Build Coastguard Worker if (p->buffer == NULL) {
417*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
418*8b26181fSAndroid Build Coastguard Worker errno, "malloc");
419*8b26181fSAndroid Build Coastguard Worker err = PCAP_ERROR;
420*8b26181fSAndroid Build Coastguard Worker goto bad;
421*8b26181fSAndroid Build Coastguard Worker }
422*8b26181fSAndroid Build Coastguard Worker
423*8b26181fSAndroid Build Coastguard Worker /*
424*8b26181fSAndroid Build Coastguard Worker * "p->fd" is an FD for a STREAMS device, so "select()" and
425*8b26181fSAndroid Build Coastguard Worker * "poll()" should work on it.
426*8b26181fSAndroid Build Coastguard Worker */
427*8b26181fSAndroid Build Coastguard Worker p->selectable_fd = p->fd;
428*8b26181fSAndroid Build Coastguard Worker
429*8b26181fSAndroid Build Coastguard Worker /*
430*8b26181fSAndroid Build Coastguard Worker * This is (presumably) a real Ethernet capture; give it a
431*8b26181fSAndroid Build Coastguard Worker * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
432*8b26181fSAndroid Build Coastguard Worker * that an application can let you choose it, in case you're
433*8b26181fSAndroid Build Coastguard Worker * capturing DOCSIS traffic that a Cisco Cable Modem
434*8b26181fSAndroid Build Coastguard Worker * Termination System is putting out onto an Ethernet (it
435*8b26181fSAndroid Build Coastguard Worker * doesn't put an Ethernet header onto the wire, it puts raw
436*8b26181fSAndroid Build Coastguard Worker * DOCSIS frames out on the wire inside the low-level
437*8b26181fSAndroid Build Coastguard Worker * Ethernet framing).
438*8b26181fSAndroid Build Coastguard Worker */
439*8b26181fSAndroid Build Coastguard Worker p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
440*8b26181fSAndroid Build Coastguard Worker /*
441*8b26181fSAndroid Build Coastguard Worker * If that fails, just leave the list empty.
442*8b26181fSAndroid Build Coastguard Worker */
443*8b26181fSAndroid Build Coastguard Worker if (p->dlt_list != NULL) {
444*8b26181fSAndroid Build Coastguard Worker p->dlt_list[0] = DLT_EN10MB;
445*8b26181fSAndroid Build Coastguard Worker p->dlt_list[1] = DLT_DOCSIS;
446*8b26181fSAndroid Build Coastguard Worker p->dlt_count = 2;
447*8b26181fSAndroid Build Coastguard Worker }
448*8b26181fSAndroid Build Coastguard Worker
449*8b26181fSAndroid Build Coastguard Worker p->read_op = pcap_read_snit;
450*8b26181fSAndroid Build Coastguard Worker p->inject_op = pcap_inject_snit;
451*8b26181fSAndroid Build Coastguard Worker p->setfilter_op = install_bpf_program; /* no kernel filtering */
452*8b26181fSAndroid Build Coastguard Worker p->setdirection_op = NULL; /* Not implemented. */
453*8b26181fSAndroid Build Coastguard Worker p->set_datalink_op = NULL; /* can't change data link type */
454*8b26181fSAndroid Build Coastguard Worker p->getnonblock_op = pcap_getnonblock_fd;
455*8b26181fSAndroid Build Coastguard Worker p->setnonblock_op = pcap_setnonblock_fd;
456*8b26181fSAndroid Build Coastguard Worker p->stats_op = pcap_stats_snit;
457*8b26181fSAndroid Build Coastguard Worker
458*8b26181fSAndroid Build Coastguard Worker return (0);
459*8b26181fSAndroid Build Coastguard Worker bad:
460*8b26181fSAndroid Build Coastguard Worker pcap_cleanup_live_common(p);
461*8b26181fSAndroid Build Coastguard Worker return (err);
462*8b26181fSAndroid Build Coastguard Worker }
463*8b26181fSAndroid Build Coastguard Worker
464*8b26181fSAndroid Build Coastguard Worker pcap_t *
pcap_create_interface(const char * device _U_,char * ebuf)465*8b26181fSAndroid Build Coastguard Worker pcap_create_interface(const char *device _U_, char *ebuf)
466*8b26181fSAndroid Build Coastguard Worker {
467*8b26181fSAndroid Build Coastguard Worker pcap_t *p;
468*8b26181fSAndroid Build Coastguard Worker
469*8b26181fSAndroid Build Coastguard Worker p = PCAP_CREATE_COMMON(ebuf, struct pcap_snit);
470*8b26181fSAndroid Build Coastguard Worker if (p == NULL)
471*8b26181fSAndroid Build Coastguard Worker return (NULL);
472*8b26181fSAndroid Build Coastguard Worker
473*8b26181fSAndroid Build Coastguard Worker p->activate_op = pcap_activate_snit;
474*8b26181fSAndroid Build Coastguard Worker return (p);
475*8b26181fSAndroid Build Coastguard Worker }
476*8b26181fSAndroid Build Coastguard Worker
477*8b26181fSAndroid Build Coastguard Worker /*
478*8b26181fSAndroid Build Coastguard Worker * XXX - there's probably a NIOCBIND error that means "that device
479*8b26181fSAndroid Build Coastguard Worker * doesn't support NIT"; if so, we should try an NIOCBIND and use that.
480*8b26181fSAndroid Build Coastguard Worker */
481*8b26181fSAndroid Build Coastguard Worker static int
can_be_bound(const char * name _U_)482*8b26181fSAndroid Build Coastguard Worker can_be_bound(const char *name _U_)
483*8b26181fSAndroid Build Coastguard Worker {
484*8b26181fSAndroid Build Coastguard Worker return (1);
485*8b26181fSAndroid Build Coastguard Worker }
486*8b26181fSAndroid Build Coastguard Worker
487*8b26181fSAndroid Build Coastguard Worker static int
get_if_flags(const char * name _U_,bpf_u_int32 * flags _U_,char * errbuf _U_)488*8b26181fSAndroid Build Coastguard Worker get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
489*8b26181fSAndroid Build Coastguard Worker {
490*8b26181fSAndroid Build Coastguard Worker /*
491*8b26181fSAndroid Build Coastguard Worker * Nothing we can do.
492*8b26181fSAndroid Build Coastguard Worker * XXX - is there a way to find out whether an adapter has
493*8b26181fSAndroid Build Coastguard Worker * something plugged into it?
494*8b26181fSAndroid Build Coastguard Worker */
495*8b26181fSAndroid Build Coastguard Worker return (0);
496*8b26181fSAndroid Build Coastguard Worker }
497*8b26181fSAndroid Build Coastguard Worker
498*8b26181fSAndroid Build Coastguard Worker int
pcap_platform_finddevs(pcap_if_list_t * devlistp,char * errbuf)499*8b26181fSAndroid Build Coastguard Worker pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
500*8b26181fSAndroid Build Coastguard Worker {
501*8b26181fSAndroid Build Coastguard Worker return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound,
502*8b26181fSAndroid Build Coastguard Worker get_if_flags));
503*8b26181fSAndroid Build Coastguard Worker }
504*8b26181fSAndroid Build Coastguard Worker
505*8b26181fSAndroid Build Coastguard Worker /*
506*8b26181fSAndroid Build Coastguard Worker * Libpcap version string.
507*8b26181fSAndroid Build Coastguard Worker */
508*8b26181fSAndroid Build Coastguard Worker const char *
pcap_lib_version(void)509*8b26181fSAndroid Build Coastguard Worker pcap_lib_version(void)
510*8b26181fSAndroid Build Coastguard Worker {
511*8b26181fSAndroid Build Coastguard Worker return (PCAP_VERSION_STRING);
512*8b26181fSAndroid Build Coastguard Worker }
513