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