pcap-netmap.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (C) 2014 Luigi Rizzo. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include <config.h>
  28. #endif
  29. #include <poll.h>
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #include <netdb.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #define NETMAP_WITH_LIBS
  38. #include <net/netmap_user.h>
  39. #include "pcap-int.h"
  40. #include "pcap-netmap.h"
  41. #ifndef __FreeBSD__
  42. /*
  43. * On FreeBSD we use IFF_PPROMISC which is in ifr_flagshigh.
  44. * Remap to IFF_PROMISC on other platforms.
  45. *
  46. * XXX - DragonFly BSD?
  47. */
  48. #define IFF_PPROMISC IFF_PROMISC
  49. #endif /* __FreeBSD__ */
  50. struct pcap_netmap {
  51. struct nm_desc *d; /* pointer returned by nm_open() */
  52. pcap_handler cb; /* callback and argument */
  53. u_char *cb_arg;
  54. int must_clear_promisc; /* flag */
  55. uint64_t rx_pkts; /* # of pkts received before the filter */
  56. };
  57. static int
  58. pcap_netmap_stats(pcap_t *p, struct pcap_stat *ps)
  59. {
  60. struct pcap_netmap *pn = p->priv;
  61. ps->ps_recv = pn->rx_pkts;
  62. ps->ps_drop = 0;
  63. ps->ps_ifdrop = 0;
  64. return 0;
  65. }
  66. static void
  67. pcap_netmap_filter(u_char *arg, struct pcap_pkthdr *h, const u_char *buf)
  68. {
  69. pcap_t *p = (pcap_t *)arg;
  70. struct pcap_netmap *pn = p->priv;
  71. const struct bpf_insn *pc = p->fcode.bf_insns;
  72. ++pn->rx_pkts;
  73. if (pc == NULL || bpf_filter(pc, buf, h->len, h->caplen))
  74. pn->cb(pn->cb_arg, h, buf);
  75. }
  76. static int
  77. pcap_netmap_dispatch(pcap_t *p, int cnt, pcap_handler cb, u_char *user)
  78. {
  79. int ret;
  80. struct pcap_netmap *pn = p->priv;
  81. struct nm_desc *d = pn->d;
  82. struct pollfd pfd = { .fd = p->fd, .events = POLLIN, .revents = 0 };
  83. pn->cb = cb;
  84. pn->cb_arg = user;
  85. for (;;) {
  86. if (p->break_loop) {
  87. p->break_loop = 0;
  88. return PCAP_ERROR_BREAK;
  89. }
  90. /* nm_dispatch won't run forever */
  91. ret = nm_dispatch((void *)d, cnt, (void *)pcap_netmap_filter, (void *)p);
  92. if (ret != 0)
  93. break;
  94. errno = 0;
  95. ret = poll(&pfd, 1, p->opt.timeout);
  96. }
  97. return ret;
  98. }
  99. /* XXX need to check the NIOCTXSYNC/poll */
  100. static int
  101. pcap_netmap_inject(pcap_t *p, const void *buf, size_t size)
  102. {
  103. struct pcap_netmap *pn = p->priv;
  104. struct nm_desc *d = pn->d;
  105. return nm_inject(d, buf, size);
  106. }
  107. static int
  108. pcap_netmap_ioctl(pcap_t *p, u_long what, uint32_t *if_flags)
  109. {
  110. struct pcap_netmap *pn = p->priv;
  111. struct nm_desc *d = pn->d;
  112. struct ifreq ifr;
  113. int error, fd = d->fd;
  114. #ifdef linux
  115. fd = socket(AF_INET, SOCK_DGRAM, 0);
  116. if (fd < 0) {
  117. fprintf(stderr, "Error: cannot get device control socket.\n");
  118. return -1;
  119. }
  120. #endif /* linux */
  121. bzero(&ifr, sizeof(ifr));
  122. strncpy(ifr.ifr_name, d->req.nr_name, sizeof(ifr.ifr_name));
  123. switch (what) {
  124. case SIOCSIFFLAGS:
  125. /*
  126. * The flags we pass in are 32-bit and unsigned.
  127. *
  128. * On most if not all UN*Xes, ifr_flags is 16-bit and
  129. * signed, and the result of assigning a longer
  130. * unsigned value to a shorter signed value is
  131. * implementation-defined (even if, in practice, it'll
  132. * do what's intended on all platforms we support
  133. * result of assigning a 32-bit unsigned value).
  134. * So we mask out the upper 16 bits.
  135. */
  136. ifr.ifr_flags = *if_flags & 0xffff;
  137. #ifdef __FreeBSD__
  138. /*
  139. * In FreeBSD, we need to set the high-order flags,
  140. * as we're using IFF_PPROMISC, which is in those bits.
  141. *
  142. * XXX - DragonFly BSD?
  143. */
  144. ifr.ifr_flagshigh = *if_flags >> 16;
  145. #endif /* __FreeBSD__ */
  146. break;
  147. }
  148. error = ioctl(fd, what, &ifr);
  149. if (!error) {
  150. switch (what) {
  151. case SIOCGIFFLAGS:
  152. /*
  153. * The flags we return are 32-bit.
  154. *
  155. * On most if not all UN*Xes, ifr_flags is
  156. * 16-bit and signed, and will get sign-
  157. * extended, so that the upper 16 bits of
  158. * those flags will be forced on. So we
  159. * mask out the upper 16 bits of the
  160. * sign-extended value.
  161. */
  162. *if_flags = ifr.ifr_flags & 0xffff;
  163. #ifdef __FreeBSD__
  164. /*
  165. * In FreeBSD, we need to return the
  166. * high-order flags, as we're using
  167. * IFF_PPROMISC, which is in those bits.
  168. *
  169. * XXX - DragonFly BSD?
  170. */
  171. *if_flags |= (ifr.ifr_flagshigh << 16);
  172. #endif /* __FreeBSD__ */
  173. }
  174. }
  175. #ifdef linux
  176. close(fd);
  177. #endif /* linux */
  178. return error ? -1 : 0;
  179. }
  180. static void
  181. pcap_netmap_close(pcap_t *p)
  182. {
  183. struct pcap_netmap *pn = p->priv;
  184. struct nm_desc *d = pn->d;
  185. uint32_t if_flags = 0;
  186. if (pn->must_clear_promisc) {
  187. pcap_netmap_ioctl(p, SIOCGIFFLAGS, &if_flags); /* fetch flags */
  188. if (if_flags & IFF_PPROMISC) {
  189. if_flags &= ~IFF_PPROMISC;
  190. pcap_netmap_ioctl(p, SIOCSIFFLAGS, &if_flags);
  191. }
  192. }
  193. nm_close(d);
  194. pcap_cleanup_live_common(p);
  195. }
  196. static int
  197. pcap_netmap_activate(pcap_t *p)
  198. {
  199. struct pcap_netmap *pn = p->priv;
  200. struct nm_desc *d;
  201. uint32_t if_flags = 0;
  202. d = nm_open(p->opt.device, NULL, 0, NULL);
  203. if (d == NULL) {
  204. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  205. errno, "netmap open: cannot access %s",
  206. p->opt.device);
  207. pcap_cleanup_live_common(p);
  208. return (PCAP_ERROR);
  209. }
  210. #if 0
  211. fprintf(stderr, "%s device %s priv %p fd %d ports %d..%d\n",
  212. __FUNCTION__, p->opt.device, d, d->fd,
  213. d->first_rx_ring, d->last_rx_ring);
  214. #endif
  215. pn->d = d;
  216. p->fd = d->fd;
  217. /*
  218. * Turn a negative snapshot value (invalid), a snapshot value of
  219. * 0 (unspecified), or a value bigger than the normal maximum
  220. * value, into the maximum allowed value.
  221. *
  222. * If some application really *needs* a bigger snapshot
  223. * length, we should just increase MAXIMUM_SNAPLEN.
  224. */
  225. if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
  226. p->snapshot = MAXIMUM_SNAPLEN;
  227. if (p->opt.promisc && !(d->req.nr_ringid & NETMAP_SW_RING)) {
  228. pcap_netmap_ioctl(p, SIOCGIFFLAGS, &if_flags); /* fetch flags */
  229. if (!(if_flags & IFF_PPROMISC)) {
  230. pn->must_clear_promisc = 1;
  231. if_flags |= IFF_PPROMISC;
  232. pcap_netmap_ioctl(p, SIOCSIFFLAGS, &if_flags);
  233. }
  234. }
  235. p->linktype = DLT_EN10MB;
  236. p->selectable_fd = p->fd;
  237. p->read_op = pcap_netmap_dispatch;
  238. p->inject_op = pcap_netmap_inject;
  239. p->setfilter_op = install_bpf_program;
  240. p->setdirection_op = NULL;
  241. p->set_datalink_op = NULL;
  242. p->getnonblock_op = pcap_getnonblock_fd;
  243. p->setnonblock_op = pcap_setnonblock_fd;
  244. p->stats_op = pcap_netmap_stats;
  245. p->cleanup_op = pcap_netmap_close;
  246. return (0);
  247. }
  248. pcap_t *
  249. pcap_netmap_create(const char *device, char *ebuf, int *is_ours)
  250. {
  251. pcap_t *p;
  252. *is_ours = (!strncmp(device, "netmap:", 7) || !strncmp(device, "vale", 4));
  253. if (! *is_ours)
  254. return NULL;
  255. p = pcap_create_common(ebuf, sizeof (struct pcap_netmap));
  256. if (p == NULL)
  257. return (NULL);
  258. p->activate_op = pcap_netmap_activate;
  259. return (p);
  260. }
  261. /*
  262. * The "device name" for netmap devices isn't a name for a device, it's
  263. * an expression that indicates how the device should be set up, so
  264. * there's no way to enumerate them.
  265. */
  266. int
  267. pcap_netmap_findalldevs(pcap_if_list_t *devlistp _U_, char *err_str _U_)
  268. {
  269. return 0;
  270. }