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
22*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
23*8b26181fSAndroid Build Coastguard Worker #include <config.h>
24*8b26181fSAndroid Build Coastguard Worker #endif
25*8b26181fSAndroid Build Coastguard Worker
26*8b26181fSAndroid Build Coastguard Worker #include <sys/types.h>
27*8b26181fSAndroid Build Coastguard Worker #include <sys/time.h>
28*8b26181fSAndroid Build Coastguard Worker #include <sys/timeb.h>
29*8b26181fSAndroid Build Coastguard Worker #include <sys/file.h>
30*8b26181fSAndroid Build Coastguard Worker #include <sys/ioctl.h>
31*8b26181fSAndroid Build Coastguard Worker #include <sys/socket.h>
32*8b26181fSAndroid Build Coastguard Worker
33*8b26181fSAndroid Build Coastguard Worker #include <net/if.h>
34*8b26181fSAndroid Build Coastguard Worker #include <net/nit.h>
35*8b26181fSAndroid Build Coastguard Worker
36*8b26181fSAndroid Build Coastguard Worker #include <netinet/in.h>
37*8b26181fSAndroid Build Coastguard Worker #include <netinet/in_systm.h>
38*8b26181fSAndroid Build Coastguard Worker #include <netinet/ip.h>
39*8b26181fSAndroid Build Coastguard Worker #include <netinet/if_ether.h>
40*8b26181fSAndroid Build Coastguard Worker #include <netinet/ip_var.h>
41*8b26181fSAndroid Build Coastguard Worker #include <netinet/udp.h>
42*8b26181fSAndroid Build Coastguard Worker #include <netinet/udp_var.h>
43*8b26181fSAndroid Build Coastguard Worker #include <netinet/tcp.h>
44*8b26181fSAndroid Build Coastguard Worker #include <netinet/tcpip.h>
45*8b26181fSAndroid Build Coastguard Worker
46*8b26181fSAndroid Build Coastguard Worker #include <errno.h>
47*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
48*8b26181fSAndroid Build Coastguard Worker
49*8b26181fSAndroid Build Coastguard Worker #include "pcap-int.h"
50*8b26181fSAndroid Build Coastguard Worker
51*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OS_PROTO_H
52*8b26181fSAndroid Build Coastguard Worker #include "os-proto.h"
53*8b26181fSAndroid Build Coastguard Worker #endif
54*8b26181fSAndroid Build Coastguard Worker
55*8b26181fSAndroid Build Coastguard Worker /*
56*8b26181fSAndroid Build Coastguard Worker * The chunk size for NIT. This is the amount of buffering
57*8b26181fSAndroid Build Coastguard Worker * done for read calls.
58*8b26181fSAndroid Build Coastguard Worker */
59*8b26181fSAndroid Build Coastguard Worker #define CHUNKSIZE (2*1024)
60*8b26181fSAndroid Build Coastguard Worker
61*8b26181fSAndroid Build Coastguard Worker /*
62*8b26181fSAndroid Build Coastguard Worker * The total buffer space used by NIT.
63*8b26181fSAndroid Build Coastguard Worker */
64*8b26181fSAndroid Build Coastguard Worker #define BUFSPACE (4*CHUNKSIZE)
65*8b26181fSAndroid Build Coastguard Worker
66*8b26181fSAndroid Build Coastguard Worker /* Forwards */
67*8b26181fSAndroid Build Coastguard Worker static int nit_setflags(int, int, int, char *);
68*8b26181fSAndroid Build Coastguard Worker
69*8b26181fSAndroid Build Coastguard Worker /*
70*8b26181fSAndroid Build Coastguard Worker * Private data for capturing on NIT devices.
71*8b26181fSAndroid Build Coastguard Worker */
72*8b26181fSAndroid Build Coastguard Worker struct pcap_nit {
73*8b26181fSAndroid Build Coastguard Worker struct pcap_stat stat;
74*8b26181fSAndroid Build Coastguard Worker };
75*8b26181fSAndroid Build Coastguard Worker
76*8b26181fSAndroid Build Coastguard Worker static int
pcap_stats_nit(pcap_t * p,struct pcap_stat * ps)77*8b26181fSAndroid Build Coastguard Worker pcap_stats_nit(pcap_t *p, struct pcap_stat *ps)
78*8b26181fSAndroid Build Coastguard Worker {
79*8b26181fSAndroid Build Coastguard Worker struct pcap_nit *pn = p->priv;
80*8b26181fSAndroid Build Coastguard Worker
81*8b26181fSAndroid Build Coastguard Worker /*
82*8b26181fSAndroid Build Coastguard Worker * "ps_recv" counts packets handed to the filter, not packets
83*8b26181fSAndroid Build Coastguard Worker * that passed the filter. As filtering is done in userland,
84*8b26181fSAndroid Build Coastguard Worker * this does not include packets dropped because we ran out
85*8b26181fSAndroid Build Coastguard Worker * of buffer space.
86*8b26181fSAndroid Build Coastguard Worker *
87*8b26181fSAndroid Build Coastguard Worker * "ps_drop" presumably counts packets dropped by the socket
88*8b26181fSAndroid Build Coastguard Worker * because of flow control requirements or resource exhaustion;
89*8b26181fSAndroid Build Coastguard Worker * it doesn't count packets dropped by the interface driver.
90*8b26181fSAndroid Build Coastguard Worker * As filtering is done in userland, it counts packets regardless
91*8b26181fSAndroid Build Coastguard Worker * of whether they would've passed the filter.
92*8b26181fSAndroid Build Coastguard Worker *
93*8b26181fSAndroid Build Coastguard Worker * These statistics don't include packets not yet read from the
94*8b26181fSAndroid Build Coastguard Worker * kernel by libpcap or packets not yet read from libpcap by the
95*8b26181fSAndroid Build Coastguard Worker * application.
96*8b26181fSAndroid Build Coastguard Worker */
97*8b26181fSAndroid Build Coastguard Worker *ps = pn->stat;
98*8b26181fSAndroid Build Coastguard Worker return (0);
99*8b26181fSAndroid Build Coastguard Worker }
100*8b26181fSAndroid Build Coastguard Worker
101*8b26181fSAndroid Build Coastguard Worker static int
pcap_read_nit(pcap_t * p,int cnt,pcap_handler callback,u_char * user)102*8b26181fSAndroid Build Coastguard Worker pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
103*8b26181fSAndroid Build Coastguard Worker {
104*8b26181fSAndroid Build Coastguard Worker struct pcap_nit *pn = p->priv;
105*8b26181fSAndroid Build Coastguard Worker register int cc, n;
106*8b26181fSAndroid Build Coastguard Worker register u_char *bp, *cp, *ep;
107*8b26181fSAndroid Build Coastguard Worker register struct nit_hdr *nh;
108*8b26181fSAndroid Build Coastguard Worker register int caplen;
109*8b26181fSAndroid Build Coastguard Worker
110*8b26181fSAndroid Build Coastguard Worker cc = p->cc;
111*8b26181fSAndroid Build Coastguard Worker if (cc == 0) {
112*8b26181fSAndroid Build Coastguard Worker cc = read(p->fd, (char *)p->buffer, p->bufsize);
113*8b26181fSAndroid Build Coastguard Worker if (cc < 0) {
114*8b26181fSAndroid Build Coastguard Worker if (errno == EWOULDBLOCK)
115*8b26181fSAndroid Build Coastguard Worker return (0);
116*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
117*8b26181fSAndroid Build Coastguard Worker errno, "pcap_read");
118*8b26181fSAndroid Build Coastguard Worker return (-1);
119*8b26181fSAndroid Build Coastguard Worker }
120*8b26181fSAndroid Build Coastguard Worker bp = (u_char *)p->buffer;
121*8b26181fSAndroid Build Coastguard Worker } else
122*8b26181fSAndroid Build Coastguard Worker bp = p->bp;
123*8b26181fSAndroid Build Coastguard Worker
124*8b26181fSAndroid Build Coastguard Worker /*
125*8b26181fSAndroid Build Coastguard Worker * Loop through each packet. The increment expression
126*8b26181fSAndroid Build Coastguard Worker * rounds up to the next int boundary past the end of
127*8b26181fSAndroid Build Coastguard Worker * the previous packet.
128*8b26181fSAndroid Build Coastguard Worker *
129*8b26181fSAndroid Build Coastguard Worker * This assumes that a single buffer of packets will have
130*8b26181fSAndroid Build Coastguard Worker * <= INT_MAX packets, so the packet count doesn't overflow.
131*8b26181fSAndroid Build Coastguard Worker */
132*8b26181fSAndroid Build Coastguard Worker n = 0;
133*8b26181fSAndroid Build Coastguard Worker ep = bp + cc;
134*8b26181fSAndroid Build Coastguard Worker while (bp < ep) {
135*8b26181fSAndroid Build Coastguard Worker /*
136*8b26181fSAndroid Build Coastguard Worker * Has "pcap_breakloop()" been called?
137*8b26181fSAndroid Build Coastguard Worker * If so, return immediately - if we haven't read any
138*8b26181fSAndroid Build Coastguard Worker * packets, clear the flag and return -2 to indicate
139*8b26181fSAndroid Build Coastguard Worker * that we were told to break out of the loop, otherwise
140*8b26181fSAndroid Build Coastguard Worker * leave the flag set, so that the *next* call will break
141*8b26181fSAndroid Build Coastguard Worker * out of the loop without having read any packets, and
142*8b26181fSAndroid Build Coastguard Worker * return the number of packets we've processed so far.
143*8b26181fSAndroid Build Coastguard Worker */
144*8b26181fSAndroid Build Coastguard Worker if (p->break_loop) {
145*8b26181fSAndroid Build Coastguard Worker if (n == 0) {
146*8b26181fSAndroid Build Coastguard Worker p->break_loop = 0;
147*8b26181fSAndroid Build Coastguard Worker return (-2);
148*8b26181fSAndroid Build Coastguard Worker } else {
149*8b26181fSAndroid Build Coastguard Worker p->cc = ep - bp;
150*8b26181fSAndroid Build Coastguard Worker p->bp = bp;
151*8b26181fSAndroid Build Coastguard Worker return (n);
152*8b26181fSAndroid Build Coastguard Worker }
153*8b26181fSAndroid Build Coastguard Worker }
154*8b26181fSAndroid Build Coastguard Worker
155*8b26181fSAndroid Build Coastguard Worker nh = (struct nit_hdr *)bp;
156*8b26181fSAndroid Build Coastguard Worker cp = bp + sizeof(*nh);
157*8b26181fSAndroid Build Coastguard Worker
158*8b26181fSAndroid Build Coastguard Worker switch (nh->nh_state) {
159*8b26181fSAndroid Build Coastguard Worker
160*8b26181fSAndroid Build Coastguard Worker case NIT_CATCH:
161*8b26181fSAndroid Build Coastguard Worker break;
162*8b26181fSAndroid Build Coastguard Worker
163*8b26181fSAndroid Build Coastguard Worker case NIT_NOMBUF:
164*8b26181fSAndroid Build Coastguard Worker case NIT_NOCLUSTER:
165*8b26181fSAndroid Build Coastguard Worker case NIT_NOSPACE:
166*8b26181fSAndroid Build Coastguard Worker pn->stat.ps_drop = nh->nh_dropped;
167*8b26181fSAndroid Build Coastguard Worker continue;
168*8b26181fSAndroid Build Coastguard Worker
169*8b26181fSAndroid Build Coastguard Worker case NIT_SEQNO:
170*8b26181fSAndroid Build Coastguard Worker continue;
171*8b26181fSAndroid Build Coastguard Worker
172*8b26181fSAndroid Build Coastguard Worker default:
173*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, sizeof(p->errbuf),
174*8b26181fSAndroid Build Coastguard Worker "bad nit state %d", nh->nh_state);
175*8b26181fSAndroid Build Coastguard Worker return (-1);
176*8b26181fSAndroid Build Coastguard Worker }
177*8b26181fSAndroid Build Coastguard Worker ++pn->stat.ps_recv;
178*8b26181fSAndroid Build Coastguard Worker bp += ((sizeof(struct nit_hdr) + nh->nh_datalen +
179*8b26181fSAndroid Build Coastguard Worker sizeof(int) - 1) & ~(sizeof(int) - 1));
180*8b26181fSAndroid Build Coastguard Worker
181*8b26181fSAndroid Build Coastguard Worker caplen = nh->nh_wirelen;
182*8b26181fSAndroid Build Coastguard Worker if (caplen > p->snapshot)
183*8b26181fSAndroid Build Coastguard Worker caplen = p->snapshot;
184*8b26181fSAndroid Build Coastguard Worker if (pcap_filter(p->fcode.bf_insns, cp, nh->nh_wirelen, caplen)) {
185*8b26181fSAndroid Build Coastguard Worker struct pcap_pkthdr h;
186*8b26181fSAndroid Build Coastguard Worker h.ts = nh->nh_timestamp;
187*8b26181fSAndroid Build Coastguard Worker h.len = nh->nh_wirelen;
188*8b26181fSAndroid Build Coastguard Worker h.caplen = caplen;
189*8b26181fSAndroid Build Coastguard Worker (*callback)(user, &h, cp);
190*8b26181fSAndroid Build Coastguard Worker if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
191*8b26181fSAndroid Build Coastguard Worker p->cc = ep - bp;
192*8b26181fSAndroid Build Coastguard Worker p->bp = bp;
193*8b26181fSAndroid Build Coastguard Worker return (n);
194*8b26181fSAndroid Build Coastguard Worker }
195*8b26181fSAndroid Build Coastguard Worker }
196*8b26181fSAndroid Build Coastguard Worker }
197*8b26181fSAndroid Build Coastguard Worker p->cc = 0;
198*8b26181fSAndroid Build Coastguard Worker return (n);
199*8b26181fSAndroid Build Coastguard Worker }
200*8b26181fSAndroid Build Coastguard Worker
201*8b26181fSAndroid Build Coastguard Worker static int
pcap_inject_nit(pcap_t * p,const void * buf,int size)202*8b26181fSAndroid Build Coastguard Worker pcap_inject_nit(pcap_t *p, const void *buf, int size)
203*8b26181fSAndroid Build Coastguard Worker {
204*8b26181fSAndroid Build Coastguard Worker struct sockaddr sa;
205*8b26181fSAndroid Build Coastguard Worker int ret;
206*8b26181fSAndroid Build Coastguard Worker
207*8b26181fSAndroid Build Coastguard Worker memset(&sa, 0, sizeof(sa));
208*8b26181fSAndroid Build Coastguard Worker strncpy(sa.sa_data, device, sizeof(sa.sa_data));
209*8b26181fSAndroid Build Coastguard Worker ret = sendto(p->fd, buf, size, 0, &sa, sizeof(sa));
210*8b26181fSAndroid Build Coastguard Worker if (ret == -1) {
211*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
212*8b26181fSAndroid Build Coastguard Worker errno, "send");
213*8b26181fSAndroid Build Coastguard Worker return (-1);
214*8b26181fSAndroid Build Coastguard Worker }
215*8b26181fSAndroid Build Coastguard Worker return (ret);
216*8b26181fSAndroid Build Coastguard Worker }
217*8b26181fSAndroid Build Coastguard Worker
218*8b26181fSAndroid Build Coastguard Worker static int
nit_setflags(pcap_t * p)219*8b26181fSAndroid Build Coastguard Worker nit_setflags(pcap_t *p)
220*8b26181fSAndroid Build Coastguard Worker {
221*8b26181fSAndroid Build Coastguard Worker struct nit_ioc nioc;
222*8b26181fSAndroid Build Coastguard Worker
223*8b26181fSAndroid Build Coastguard Worker memset(&nioc, 0, sizeof(nioc));
224*8b26181fSAndroid Build Coastguard Worker nioc.nioc_typetomatch = NT_ALLTYPES;
225*8b26181fSAndroid Build Coastguard Worker nioc.nioc_snaplen = p->snapshot;
226*8b26181fSAndroid Build Coastguard Worker nioc.nioc_bufalign = sizeof(int);
227*8b26181fSAndroid Build Coastguard Worker nioc.nioc_bufoffset = 0;
228*8b26181fSAndroid Build Coastguard Worker
229*8b26181fSAndroid Build Coastguard Worker if (p->opt.buffer_size != 0)
230*8b26181fSAndroid Build Coastguard Worker nioc.nioc_bufspace = p->opt.buffer_size;
231*8b26181fSAndroid Build Coastguard Worker else {
232*8b26181fSAndroid Build Coastguard Worker /* Default buffer size */
233*8b26181fSAndroid Build Coastguard Worker nioc.nioc_bufspace = BUFSPACE;
234*8b26181fSAndroid Build Coastguard Worker }
235*8b26181fSAndroid Build Coastguard Worker
236*8b26181fSAndroid Build Coastguard Worker if (p->opt.immediate) {
237*8b26181fSAndroid Build Coastguard Worker /*
238*8b26181fSAndroid Build Coastguard Worker * XXX - will this cause packets to be delivered immediately?
239*8b26181fSAndroid Build Coastguard Worker * XXX - given that this is for SunOS prior to 4.0, do
240*8b26181fSAndroid Build Coastguard Worker * we care?
241*8b26181fSAndroid Build Coastguard Worker */
242*8b26181fSAndroid Build Coastguard Worker nioc.nioc_chunksize = 0;
243*8b26181fSAndroid Build Coastguard Worker } else
244*8b26181fSAndroid Build Coastguard Worker nioc.nioc_chunksize = CHUNKSIZE;
245*8b26181fSAndroid Build Coastguard Worker if (p->opt.timeout != 0) {
246*8b26181fSAndroid Build Coastguard Worker nioc.nioc_flags |= NF_TIMEOUT;
247*8b26181fSAndroid Build Coastguard Worker nioc.nioc_timeout.tv_sec = p->opt.timeout / 1000;
248*8b26181fSAndroid Build Coastguard Worker nioc.nioc_timeout.tv_usec = (p->opt.timeout * 1000) % 1000000;
249*8b26181fSAndroid Build Coastguard Worker }
250*8b26181fSAndroid Build Coastguard Worker if (p->opt.promisc)
251*8b26181fSAndroid Build Coastguard Worker nioc.nioc_flags |= NF_PROMISC;
252*8b26181fSAndroid Build Coastguard Worker
253*8b26181fSAndroid Build Coastguard Worker if (ioctl(p->fd, SIOCSNIT, &nioc) < 0) {
254*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
255*8b26181fSAndroid Build Coastguard Worker errno, "SIOCSNIT");
256*8b26181fSAndroid Build Coastguard Worker return (-1);
257*8b26181fSAndroid Build Coastguard Worker }
258*8b26181fSAndroid Build Coastguard Worker return (0);
259*8b26181fSAndroid Build Coastguard Worker }
260*8b26181fSAndroid Build Coastguard Worker
261*8b26181fSAndroid Build Coastguard Worker static int
pcap_activate_nit(pcap_t * p)262*8b26181fSAndroid Build Coastguard Worker pcap_activate_nit(pcap_t *p)
263*8b26181fSAndroid Build Coastguard Worker {
264*8b26181fSAndroid Build Coastguard Worker int fd;
265*8b26181fSAndroid Build Coastguard Worker struct sockaddr_nit snit;
266*8b26181fSAndroid Build Coastguard Worker
267*8b26181fSAndroid Build Coastguard Worker if (p->opt.rfmon) {
268*8b26181fSAndroid Build Coastguard Worker /*
269*8b26181fSAndroid Build Coastguard Worker * No monitor mode on SunOS 3.x or earlier (no
270*8b26181fSAndroid Build Coastguard Worker * Wi-Fi *devices* for the hardware that supported
271*8b26181fSAndroid Build Coastguard Worker * them!).
272*8b26181fSAndroid Build Coastguard Worker */
273*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_RFMON_NOTSUP);
274*8b26181fSAndroid Build Coastguard Worker }
275*8b26181fSAndroid Build Coastguard Worker
276*8b26181fSAndroid Build Coastguard Worker /*
277*8b26181fSAndroid Build Coastguard Worker * Turn a negative snapshot value (invalid), a snapshot value of
278*8b26181fSAndroid Build Coastguard Worker * 0 (unspecified), or a value bigger than the normal maximum
279*8b26181fSAndroid Build Coastguard Worker * value, into the maximum allowed value.
280*8b26181fSAndroid Build Coastguard Worker *
281*8b26181fSAndroid Build Coastguard Worker * If some application really *needs* a bigger snapshot
282*8b26181fSAndroid Build Coastguard Worker * length, we should just increase MAXIMUM_SNAPLEN.
283*8b26181fSAndroid Build Coastguard Worker */
284*8b26181fSAndroid Build Coastguard Worker if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
285*8b26181fSAndroid Build Coastguard Worker p->snapshot = MAXIMUM_SNAPLEN;
286*8b26181fSAndroid Build Coastguard Worker
287*8b26181fSAndroid Build Coastguard Worker if (p->snapshot < 96)
288*8b26181fSAndroid Build Coastguard Worker /*
289*8b26181fSAndroid Build Coastguard Worker * NIT requires a snapshot length of at least 96.
290*8b26181fSAndroid Build Coastguard Worker */
291*8b26181fSAndroid Build Coastguard Worker p->snapshot = 96;
292*8b26181fSAndroid Build Coastguard Worker
293*8b26181fSAndroid Build Coastguard Worker memset(p, 0, sizeof(*p));
294*8b26181fSAndroid Build Coastguard Worker p->fd = fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW);
295*8b26181fSAndroid Build Coastguard Worker if (fd < 0) {
296*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
297*8b26181fSAndroid Build Coastguard Worker errno, "socket");
298*8b26181fSAndroid Build Coastguard Worker goto bad;
299*8b26181fSAndroid Build Coastguard Worker }
300*8b26181fSAndroid Build Coastguard Worker snit.snit_family = AF_NIT;
301*8b26181fSAndroid Build Coastguard Worker (void)strncpy(snit.snit_ifname, p->opt.device, NITIFSIZ);
302*8b26181fSAndroid Build Coastguard Worker
303*8b26181fSAndroid Build Coastguard Worker if (bind(fd, (struct sockaddr *)&snit, sizeof(snit))) {
304*8b26181fSAndroid Build Coastguard Worker /*
305*8b26181fSAndroid Build Coastguard Worker * XXX - there's probably a particular bind error that
306*8b26181fSAndroid Build Coastguard Worker * means "there's no such device" and a particular bind
307*8b26181fSAndroid Build Coastguard Worker * error that means "that device doesn't support NIT";
308*8b26181fSAndroid Build Coastguard Worker * they might be the same error, if they both end up
309*8b26181fSAndroid Build Coastguard Worker * meaning "NIT doesn't know about that device".
310*8b26181fSAndroid Build Coastguard Worker */
311*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
312*8b26181fSAndroid Build Coastguard Worker errno, "bind: %s", snit.snit_ifname);
313*8b26181fSAndroid Build Coastguard Worker goto bad;
314*8b26181fSAndroid Build Coastguard Worker }
315*8b26181fSAndroid Build Coastguard Worker if (nit_setflags(p) < 0)
316*8b26181fSAndroid Build Coastguard Worker goto bad;
317*8b26181fSAndroid Build Coastguard Worker
318*8b26181fSAndroid Build Coastguard Worker /*
319*8b26181fSAndroid Build Coastguard Worker * NIT supports only ethernets.
320*8b26181fSAndroid Build Coastguard Worker */
321*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_EN10MB;
322*8b26181fSAndroid Build Coastguard Worker
323*8b26181fSAndroid Build Coastguard Worker p->bufsize = BUFSPACE;
324*8b26181fSAndroid Build Coastguard Worker p->buffer = malloc(p->bufsize);
325*8b26181fSAndroid Build Coastguard Worker if (p->buffer == NULL) {
326*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
327*8b26181fSAndroid Build Coastguard Worker errno, "malloc");
328*8b26181fSAndroid Build Coastguard Worker goto bad;
329*8b26181fSAndroid Build Coastguard Worker }
330*8b26181fSAndroid Build Coastguard Worker
331*8b26181fSAndroid Build Coastguard Worker /*
332*8b26181fSAndroid Build Coastguard Worker * "p->fd" is a socket, so "select()" should work on it.
333*8b26181fSAndroid Build Coastguard Worker */
334*8b26181fSAndroid Build Coastguard Worker p->selectable_fd = p->fd;
335*8b26181fSAndroid Build Coastguard Worker
336*8b26181fSAndroid Build Coastguard Worker /*
337*8b26181fSAndroid Build Coastguard Worker * This is (presumably) a real Ethernet capture; give it a
338*8b26181fSAndroid Build Coastguard Worker * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
339*8b26181fSAndroid Build Coastguard Worker * that an application can let you choose it, in case you're
340*8b26181fSAndroid Build Coastguard Worker * capturing DOCSIS traffic that a Cisco Cable Modem
341*8b26181fSAndroid Build Coastguard Worker * Termination System is putting out onto an Ethernet (it
342*8b26181fSAndroid Build Coastguard Worker * doesn't put an Ethernet header onto the wire, it puts raw
343*8b26181fSAndroid Build Coastguard Worker * DOCSIS frames out on the wire inside the low-level
344*8b26181fSAndroid Build Coastguard Worker * Ethernet framing).
345*8b26181fSAndroid Build Coastguard Worker */
346*8b26181fSAndroid Build Coastguard Worker p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
347*8b26181fSAndroid Build Coastguard Worker /*
348*8b26181fSAndroid Build Coastguard Worker * If that fails, just leave the list empty.
349*8b26181fSAndroid Build Coastguard Worker */
350*8b26181fSAndroid Build Coastguard Worker if (p->dlt_list != NULL) {
351*8b26181fSAndroid Build Coastguard Worker p->dlt_list[0] = DLT_EN10MB;
352*8b26181fSAndroid Build Coastguard Worker p->dlt_list[1] = DLT_DOCSIS;
353*8b26181fSAndroid Build Coastguard Worker p->dlt_count = 2;
354*8b26181fSAndroid Build Coastguard Worker }
355*8b26181fSAndroid Build Coastguard Worker
356*8b26181fSAndroid Build Coastguard Worker p->read_op = pcap_read_nit;
357*8b26181fSAndroid Build Coastguard Worker p->inject_op = pcap_inject_nit;
358*8b26181fSAndroid Build Coastguard Worker p->setfilter_op = install_bpf_program; /* no kernel filtering */
359*8b26181fSAndroid Build Coastguard Worker p->setdirection_op = NULL; /* Not implemented. */
360*8b26181fSAndroid Build Coastguard Worker p->set_datalink_op = NULL; /* can't change data link type */
361*8b26181fSAndroid Build Coastguard Worker p->getnonblock_op = pcap_getnonblock_fd;
362*8b26181fSAndroid Build Coastguard Worker p->setnonblock_op = pcap_setnonblock_fd;
363*8b26181fSAndroid Build Coastguard Worker p->stats_op = pcap_stats_nit;
364*8b26181fSAndroid Build Coastguard Worker
365*8b26181fSAndroid Build Coastguard Worker return (0);
366*8b26181fSAndroid Build Coastguard Worker bad:
367*8b26181fSAndroid Build Coastguard Worker pcap_cleanup_live_common(p);
368*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
369*8b26181fSAndroid Build Coastguard Worker }
370*8b26181fSAndroid Build Coastguard Worker
371*8b26181fSAndroid Build Coastguard Worker pcap_t *
pcap_create_interface(const char * device _U_,char * ebuf)372*8b26181fSAndroid Build Coastguard Worker pcap_create_interface(const char *device _U_, char *ebuf)
373*8b26181fSAndroid Build Coastguard Worker {
374*8b26181fSAndroid Build Coastguard Worker pcap_t *p;
375*8b26181fSAndroid Build Coastguard Worker
376*8b26181fSAndroid Build Coastguard Worker p = PCAP_CREATE_COMMON(ebuf, struct pcap_nit);
377*8b26181fSAndroid Build Coastguard Worker if (p == NULL)
378*8b26181fSAndroid Build Coastguard Worker return (NULL);
379*8b26181fSAndroid Build Coastguard Worker
380*8b26181fSAndroid Build Coastguard Worker p->activate_op = pcap_activate_nit;
381*8b26181fSAndroid Build Coastguard Worker return (p);
382*8b26181fSAndroid Build Coastguard Worker }
383*8b26181fSAndroid Build Coastguard Worker
384*8b26181fSAndroid Build Coastguard Worker /*
385*8b26181fSAndroid Build Coastguard Worker * XXX - there's probably a particular bind error that means "that device
386*8b26181fSAndroid Build Coastguard Worker * doesn't support NIT"; if so, we should try a bind and use that.
387*8b26181fSAndroid Build Coastguard Worker */
388*8b26181fSAndroid Build Coastguard Worker static int
can_be_bound(const char * name _U_)389*8b26181fSAndroid Build Coastguard Worker can_be_bound(const char *name _U_)
390*8b26181fSAndroid Build Coastguard Worker {
391*8b26181fSAndroid Build Coastguard Worker return (1);
392*8b26181fSAndroid Build Coastguard Worker }
393*8b26181fSAndroid Build Coastguard Worker
394*8b26181fSAndroid Build Coastguard Worker static int
get_if_flags(const char * name _U_,bpf_u_int32 * flags _U_,char * errbuf _U_)395*8b26181fSAndroid Build Coastguard Worker get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
396*8b26181fSAndroid Build Coastguard Worker {
397*8b26181fSAndroid Build Coastguard Worker /*
398*8b26181fSAndroid Build Coastguard Worker * Nothing we can do.
399*8b26181fSAndroid Build Coastguard Worker * XXX - is there a way to find out whether an adapter has
400*8b26181fSAndroid Build Coastguard Worker * something plugged into it?
401*8b26181fSAndroid Build Coastguard Worker */
402*8b26181fSAndroid Build Coastguard Worker return (0);
403*8b26181fSAndroid Build Coastguard Worker }
404*8b26181fSAndroid Build Coastguard Worker
405*8b26181fSAndroid Build Coastguard Worker int
pcap_platform_finddevs(pcap_if_list_t * devlistp,char * errbuf)406*8b26181fSAndroid Build Coastguard Worker pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
407*8b26181fSAndroid Build Coastguard Worker {
408*8b26181fSAndroid Build Coastguard Worker return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound,
409*8b26181fSAndroid Build Coastguard Worker get_if_flags));
410*8b26181fSAndroid Build Coastguard Worker }
411*8b26181fSAndroid Build Coastguard Worker
412*8b26181fSAndroid Build Coastguard Worker /*
413*8b26181fSAndroid Build Coastguard Worker * Libpcap version string.
414*8b26181fSAndroid Build Coastguard Worker */
415*8b26181fSAndroid Build Coastguard Worker const char *
pcap_lib_version(void)416*8b26181fSAndroid Build Coastguard Worker pcap_lib_version(void)
417*8b26181fSAndroid Build Coastguard Worker {
418*8b26181fSAndroid Build Coastguard Worker return (PCAP_VERSION_STRING);
419*8b26181fSAndroid Build Coastguard Worker }
420