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