pcap-snoop.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright (c) 1993, 1994, 1995, 1996, 1997
  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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <sys/param.h>
  25. #include <sys/file.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/socket.h>
  28. #include <sys/time.h>
  29. #include <net/raw.h>
  30. #include <net/if.h>
  31. #include <netinet/in.h>
  32. #include <netinet/in_systm.h>
  33. #include <netinet/ip.h>
  34. #include <netinet/if_ether.h>
  35. #include <netinet/ip_var.h>
  36. #include <netinet/udp.h>
  37. #include <netinet/udp_var.h>
  38. #include <netinet/tcp.h>
  39. #include <netinet/tcpip.h>
  40. #include <errno.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include "pcap-int.h"
  46. #ifdef HAVE_OS_PROTO_H
  47. #include "os-proto.h"
  48. #endif
  49. /*
  50. * Private data for capturing on snoop devices.
  51. */
  52. struct pcap_snoop {
  53. struct pcap_stat stat;
  54. };
  55. static int
  56. pcap_read_snoop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  57. {
  58. struct pcap_snoop *psn = p->priv;
  59. int cc;
  60. register struct snoopheader *sh;
  61. register u_int datalen;
  62. register u_int caplen;
  63. register u_char *cp;
  64. again:
  65. /*
  66. * Has "pcap_breakloop()" been called?
  67. */
  68. if (p->break_loop) {
  69. /*
  70. * Yes - clear the flag that indicates that it
  71. * has, and return -2 to indicate that we were
  72. * told to break out of the loop.
  73. */
  74. p->break_loop = 0;
  75. return (-2);
  76. }
  77. cc = read(p->fd, (char *)p->buffer, p->bufsize);
  78. if (cc < 0) {
  79. /* Don't choke when we get ptraced */
  80. switch (errno) {
  81. case EINTR:
  82. goto again;
  83. case EWOULDBLOCK:
  84. return (0); /* XXX */
  85. }
  86. pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
  87. errno, "read");
  88. return (-1);
  89. }
  90. sh = (struct snoopheader *)p->buffer;
  91. datalen = sh->snoop_packetlen;
  92. /*
  93. * XXX - Sigh, snoop_packetlen is a 16 bit quantity. If we
  94. * got a short length, but read a full sized snoop pakcet,
  95. * assume we overflowed and add back the 64K...
  96. */
  97. if (cc == (p->snapshot + sizeof(struct snoopheader)) &&
  98. (datalen < p->snapshot))
  99. datalen += (64 * 1024);
  100. caplen = (datalen < p->snapshot) ? datalen : p->snapshot;
  101. cp = (u_char *)(sh + 1) + p->offset; /* XXX */
  102. /*
  103. * XXX unfortunately snoop loopback isn't exactly like
  104. * BSD's. The address family is encoded in the first 2
  105. * bytes rather than the first 4 bytes! Luckily the last
  106. * two snoop loopback bytes are zeroed.
  107. */
  108. if (p->linktype == DLT_NULL && *((short *)(cp + 2)) == 0) {
  109. u_int *uip = (u_int *)cp;
  110. *uip >>= 16;
  111. }
  112. if (p->fcode.bf_insns == NULL ||
  113. bpf_filter(p->fcode.bf_insns, cp, datalen, caplen)) {
  114. struct pcap_pkthdr h;
  115. ++psn->stat.ps_recv;
  116. h.ts.tv_sec = sh->snoop_timestamp.tv_sec;
  117. h.ts.tv_usec = sh->snoop_timestamp.tv_usec;
  118. h.len = datalen;
  119. h.caplen = caplen;
  120. (*callback)(user, &h, cp);
  121. return (1);
  122. }
  123. return (0);
  124. }
  125. static int
  126. pcap_inject_snoop(pcap_t *p, const void *buf, size_t size)
  127. {
  128. int ret;
  129. /*
  130. * XXX - libnet overwrites the source address with what I
  131. * presume is the interface's address; is that required?
  132. */
  133. ret = write(p->fd, buf, size);
  134. if (ret == -1) {
  135. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  136. errno, "send");
  137. return (-1);
  138. }
  139. return (ret);
  140. }
  141. static int
  142. pcap_stats_snoop(pcap_t *p, struct pcap_stat *ps)
  143. {
  144. struct pcap_snoop *psn = p->priv;
  145. register struct rawstats *rs;
  146. struct rawstats rawstats;
  147. rs = &rawstats;
  148. memset(rs, 0, sizeof(*rs));
  149. if (ioctl(p->fd, SIOCRAWSTATS, (char *)rs) < 0) {
  150. pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
  151. errno, "SIOCRAWSTATS");
  152. return (-1);
  153. }
  154. /*
  155. * "ifdrops" are those dropped by the network interface
  156. * due to resource shortages or hardware errors.
  157. *
  158. * "sbdrops" are those dropped due to socket buffer limits.
  159. *
  160. * As filter is done in userland, "sbdrops" counts packets
  161. * regardless of whether they would've passed the filter.
  162. *
  163. * XXX - does this count *all* Snoop or Drain sockets,
  164. * rather than just this socket? If not, why does it have
  165. * both Snoop and Drain statistics?
  166. */
  167. psn->stat.ps_drop =
  168. rs->rs_snoop.ss_ifdrops + rs->rs_snoop.ss_sbdrops +
  169. rs->rs_drain.ds_ifdrops + rs->rs_drain.ds_sbdrops;
  170. /*
  171. * "ps_recv" counts only packets that passed the filter.
  172. * As filtering is done in userland, this does not include
  173. * packets dropped because we ran out of buffer space.
  174. */
  175. *ps = psn->stat;
  176. return (0);
  177. }
  178. /* XXX can't disable promiscuous */
  179. static int
  180. pcap_activate_snoop(pcap_t *p)
  181. {
  182. int fd;
  183. struct sockaddr_raw sr;
  184. struct snoopfilter sf;
  185. u_int v;
  186. int ll_hdrlen;
  187. int snooplen;
  188. struct ifreq ifr;
  189. fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_SNOOP);
  190. if (fd < 0) {
  191. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  192. errno, "snoop socket");
  193. goto bad;
  194. }
  195. p->fd = fd;
  196. memset(&sr, 0, sizeof(sr));
  197. sr.sr_family = AF_RAW;
  198. (void)strncpy(sr.sr_ifname, p->opt.device, sizeof(sr.sr_ifname));
  199. if (bind(fd, (struct sockaddr *)&sr, sizeof(sr))) {
  200. /*
  201. * XXX - there's probably a particular bind error that
  202. * means "there's no such device" and a particular bind
  203. * error that means "that device doesn't support snoop";
  204. * they might be the same error, if they both end up
  205. * meaning "snoop doesn't know about that device".
  206. */
  207. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  208. errno, "snoop bind");
  209. goto bad;
  210. }
  211. memset(&sf, 0, sizeof(sf));
  212. if (ioctl(fd, SIOCADDSNOOP, &sf) < 0) {
  213. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  214. errno, "SIOCADDSNOOP");
  215. goto bad;
  216. }
  217. if (p->opt.buffer_size != 0)
  218. v = p->opt.buffer_size;
  219. else
  220. v = 64 * 1024; /* default to 64K buffer size */
  221. (void)setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&v, sizeof(v));
  222. /*
  223. * XXX hack - map device name to link layer type
  224. */
  225. if (strncmp("et", p->opt.device, 2) == 0 || /* Challenge 10 Mbit */
  226. strncmp("ec", p->opt.device, 2) == 0 || /* Indigo/Indy 10 Mbit,
  227. O2 10/100 */
  228. strncmp("ef", p->opt.device, 2) == 0 || /* O200/2000 10/100 Mbit */
  229. strncmp("eg", p->opt.device, 2) == 0 || /* Octane/O2xxx/O3xxx Gigabit */
  230. strncmp("gfe", p->opt.device, 3) == 0 || /* GIO 100 Mbit */
  231. strncmp("fxp", p->opt.device, 3) == 0 || /* Challenge VME Enet */
  232. strncmp("ep", p->opt.device, 2) == 0 || /* Challenge 8x10 Mbit EPLEX */
  233. strncmp("vfe", p->opt.device, 3) == 0 || /* Challenge VME 100Mbit */
  234. strncmp("fa", p->opt.device, 2) == 0 ||
  235. strncmp("qaa", p->opt.device, 3) == 0 ||
  236. strncmp("cip", p->opt.device, 3) == 0 ||
  237. strncmp("el", p->opt.device, 2) == 0) {
  238. p->linktype = DLT_EN10MB;
  239. p->offset = RAW_HDRPAD(sizeof(struct ether_header));
  240. ll_hdrlen = sizeof(struct ether_header);
  241. /*
  242. * This is (presumably) a real Ethernet capture; give it a
  243. * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
  244. * that an application can let you choose it, in case you're
  245. * capturing DOCSIS traffic that a Cisco Cable Modem
  246. * Termination System is putting out onto an Ethernet (it
  247. * doesn't put an Ethernet header onto the wire, it puts raw
  248. * DOCSIS frames out on the wire inside the low-level
  249. * Ethernet framing).
  250. *
  251. * XXX - are there any sorts of "fake Ethernet" that have
  252. * Ethernet link-layer headers but that *shouldn't offer
  253. * DLT_DOCSIS as a Cisco CMTS won't put traffic onto it
  254. * or get traffic bridged onto it? "el" is for ATM LANE
  255. * Ethernet devices, so that might be the case for them;
  256. * the same applies for "qaa" classical IP devices. If
  257. * "fa" devices are for FORE SPANS, that'd apply to them
  258. * as well; what are "cip" devices - some other ATM
  259. * Classical IP devices?
  260. */
  261. p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
  262. /*
  263. * If that fails, just leave the list empty.
  264. */
  265. if (p->dlt_list != NULL) {
  266. p->dlt_list[0] = DLT_EN10MB;
  267. p->dlt_list[1] = DLT_DOCSIS;
  268. p->dlt_count = 2;
  269. }
  270. } else if (strncmp("ipg", p->opt.device, 3) == 0 ||
  271. strncmp("rns", p->opt.device, 3) == 0 || /* O2/200/2000 FDDI */
  272. strncmp("xpi", p->opt.device, 3) == 0) {
  273. p->linktype = DLT_FDDI;
  274. p->offset = 3; /* XXX yeah? */
  275. ll_hdrlen = 13;
  276. } else if (strncmp("ppp", p->opt.device, 3) == 0) {
  277. p->linktype = DLT_RAW;
  278. ll_hdrlen = 0; /* DLT_RAW meaning "no PPP header, just the IP packet"? */
  279. } else if (strncmp("qfa", p->opt.device, 3) == 0) {
  280. p->linktype = DLT_IP_OVER_FC;
  281. ll_hdrlen = 24;
  282. } else if (strncmp("pl", p->opt.device, 2) == 0) {
  283. p->linktype = DLT_RAW;
  284. ll_hdrlen = 0; /* Cray UNICOS/mp pseudo link */
  285. } else if (strncmp("lo", p->opt.device, 2) == 0) {
  286. p->linktype = DLT_NULL;
  287. ll_hdrlen = 4;
  288. } else {
  289. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  290. "snoop: unknown physical layer type");
  291. goto bad;
  292. }
  293. if (p->opt.rfmon) {
  294. /*
  295. * No monitor mode on Irix (no Wi-Fi devices on
  296. * hardware supported by Irix).
  297. */
  298. return (PCAP_ERROR_RFMON_NOTSUP);
  299. }
  300. /*
  301. * Turn a negative snapshot value (invalid), a snapshot value of
  302. * 0 (unspecified), or a value bigger than the normal maximum
  303. * value, into the maximum allowed value.
  304. *
  305. * If some application really *needs* a bigger snapshot
  306. * length, we should just increase MAXIMUM_SNAPLEN.
  307. */
  308. if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
  309. p->snapshot = MAXIMUM_SNAPLEN;
  310. #ifdef SIOCGIFMTU
  311. /*
  312. * XXX - IRIX appears to give you an error if you try to set the
  313. * capture length to be greater than the MTU, so let's try to get
  314. * the MTU first and, if that succeeds, trim the snap length
  315. * to be no greater than the MTU.
  316. */
  317. (void)strncpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name));
  318. if (ioctl(fd, SIOCGIFMTU, (char *)&ifr) < 0) {
  319. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  320. errno, "SIOCGIFMTU");
  321. goto bad;
  322. }
  323. /*
  324. * OK, we got it.
  325. *
  326. * XXX - some versions of IRIX 6.5 define "ifr_mtu" and have an
  327. * "ifru_metric" member of the "ifr_ifru" union in an "ifreq"
  328. * structure, others don't.
  329. *
  330. * I've no idea what's going on, so, if "ifr_mtu" isn't defined,
  331. * we define it as "ifr_metric", as using that field appears to
  332. * work on the versions that lack "ifr_mtu" (and, on those that
  333. * don't lack it, "ifru_metric" and "ifru_mtu" are both "int"
  334. * members of the "ifr_ifru" union, which suggests that they
  335. * may be interchangeable in this case).
  336. */
  337. #ifndef ifr_mtu
  338. #define ifr_mtu ifr_metric
  339. #endif
  340. if (p->snapshot > ifr.ifr_mtu + ll_hdrlen)
  341. p->snapshot = ifr.ifr_mtu + ll_hdrlen;
  342. #endif
  343. /*
  344. * The argument to SIOCSNOOPLEN is the number of link-layer
  345. * payload bytes to capture - it doesn't count link-layer
  346. * header bytes.
  347. */
  348. snooplen = p->snapshot - ll_hdrlen;
  349. if (snooplen < 0)
  350. snooplen = 0;
  351. if (ioctl(fd, SIOCSNOOPLEN, &snooplen) < 0) {
  352. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  353. errno, "SIOCSNOOPLEN");
  354. goto bad;
  355. }
  356. v = 1;
  357. if (ioctl(fd, SIOCSNOOPING, &v) < 0) {
  358. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  359. errno, "SIOCSNOOPING");
  360. goto bad;
  361. }
  362. p->bufsize = 4096; /* XXX */
  363. p->buffer = malloc(p->bufsize);
  364. if (p->buffer == NULL) {
  365. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  366. errno, "malloc");
  367. goto bad;
  368. }
  369. /*
  370. * "p->fd" is a socket, so "select()" should work on it.
  371. */
  372. p->selectable_fd = p->fd;
  373. p->read_op = pcap_read_snoop;
  374. p->inject_op = pcap_inject_snoop;
  375. p->setfilter_op = install_bpf_program; /* no kernel filtering */
  376. p->setdirection_op = NULL; /* Not implemented. */
  377. p->set_datalink_op = NULL; /* can't change data link type */
  378. p->getnonblock_op = pcap_getnonblock_fd;
  379. p->setnonblock_op = pcap_setnonblock_fd;
  380. p->stats_op = pcap_stats_snoop;
  381. return (0);
  382. bad:
  383. pcap_cleanup_live_common(p);
  384. return (PCAP_ERROR);
  385. }
  386. pcap_t *
  387. pcap_create_interface(const char *device _U_, char *ebuf)
  388. {
  389. pcap_t *p;
  390. p = pcap_create_common(ebuf, sizeof (struct pcap_snoop));
  391. if (p == NULL)
  392. return (NULL);
  393. p->activate_op = pcap_activate_snoop;
  394. return (p);
  395. }
  396. /*
  397. * XXX - there's probably a particular bind error that means "that device
  398. * doesn't support snoop"; if so, we should try a bind and use that.
  399. */
  400. static int
  401. can_be_bound(const char *name _U_)
  402. {
  403. return (1);
  404. }
  405. static int
  406. get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
  407. {
  408. /*
  409. * Nothing we can do.
  410. * XXX - is there a way to find out whether an adapter has
  411. * something plugged into it?
  412. */
  413. return (0);
  414. }
  415. int
  416. pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
  417. {
  418. return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound,
  419. get_if_flags));
  420. }
  421. /*
  422. * Libpcap version string.
  423. */
  424. const char *
  425. pcap_lib_version(void)
  426. {
  427. return (PCAP_VERSION_STRING);
  428. }