pcap-snit.c 13 KB

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