sendpacket.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /* sendpacket.[ch] is my attempt to write a universal packet injection
  20. * API for BPF, libpcap, libdnet, and Linux's PF_PACKET. I got sick
  21. * and tired dealing with libnet bugs and its lack of active maintenance,
  22. * but unfortunately, libpcap frame injection support is relatively new
  23. * and not everyone uses Linux, so I decided to support all four as
  24. * best as possible. If your platform/OS/hardware supports an additional
  25. * injection method, then by all means add it here (and send me a patch).
  26. *
  27. * Anyways, long story short, for now the order of preference is:
  28. * 1. TX_RING
  29. * 2. PF_PACKET
  30. * 3. BPF
  31. * 4. libdnet
  32. * 5. pcap_inject()
  33. * 6. pcap_sendpacket()
  34. *
  35. * Right now, one big problem with the pcap_* methods is that libpcap
  36. * doesn't provide a reliable method of getting the MAC address of
  37. * an interface (required for tcpbridge).
  38. * You can use PF_PACKET or BPF to get that, but if your system supports
  39. * those, might as well inject directly without going through another
  40. * level of indirection.
  41. *
  42. * Please note that some of this code was copied from Libnet 1.1.3
  43. */
  44. #include <string.h>
  45. #include <errno.h>
  46. #include <stdarg.h>
  47. #include <stdio.h>
  48. #include <sys/types.h>
  49. #include <sys/time.h>
  50. #include <sys/ioctl.h>
  51. #include <sys/file.h>
  52. #include <sys/socket.h>
  53. #include "config.h"
  54. #include "defines.h"
  55. #include "common.h"
  56. #include "sendpacket.h"
  57. #ifdef FORCE_INJECT_TX_RING
  58. /* TX_RING uses PF_PACKET API so don't undef it here */
  59. #undef HAVE_LIBDNET
  60. #undef HAVE_PCAP_INJECT
  61. #undef HAVE_PCAP_SENDPACKET
  62. #undef HAVE_BPF
  63. #endif
  64. #ifdef FORCE_INJECT_PF_PACKET
  65. #undef HAVE_TX_RING
  66. #undef HAVE_LIBDNET
  67. #undef HAVE_PCAP_INJECT
  68. #undef HAVE_PCAP_SENDPACKET
  69. #undef HAVE_BPF
  70. #endif
  71. #ifdef FORCE_INJECT_LIBDNET
  72. #undef HAVE_TX_RING
  73. #undef HAVE_PF_PACKET
  74. #undef HAVE_PCAP_INJECT
  75. #undef HAVE_PCAP_SENDPACKET
  76. #undef HAVE_BPF
  77. #endif
  78. #ifdef FORCE_INJECT_BPF
  79. #undef HAVE_TX_RING
  80. #undef HAVE_LIBDNET
  81. #undef HAVE_PCAP_INJECT
  82. #undef HAVE_PCAP_SENDPACKET
  83. #undef HAVE_PF_PACKET
  84. #endif
  85. #ifdef FORCE_INJECT_PCAP_INJECT
  86. #undef HAVE_TX_RING
  87. #undef HAVE_LIBDNET
  88. #undef HAVE_PCAP_SENDPACKET
  89. #undef HAVE_BPF
  90. #undef HAVE_PF_PACKET
  91. #endif
  92. #ifdef FORCE_INJECT_PCAP_SENDPACKET
  93. #undef HAVE_TX_RING
  94. #undef HAVE_LIBDNET
  95. #undef HAVE_PCAP_INJECT
  96. #undef HAVE_BPF
  97. #undef HAVE_PF_PACKET
  98. #endif
  99. #if (defined HAVE_WINPCAP && defined HAVE_PCAP_INJECT)
  100. #undef HAVE_PCAP_INJECT /* configure returns true for some odd reason */
  101. #endif
  102. #if !defined HAVE_PCAP_INJECT && !defined HAVE_PCAP_SENDPACKET && !defined HAVE_LIBDNET && !defined HAVE_PF_PACKET && !defined HAVE_BPF && !defined TX_RING
  103. #error You need pcap_inject() or pcap_sendpacket() from libpcap, libdnet, Linux's PF_PACKET/TX_RING or *BSD's BPF
  104. #endif
  105. #ifdef HAVE_SYS_PARAM_H
  106. #include <sys/param.h>
  107. #endif
  108. #ifdef HAVE_SYS_SYSCTL_H
  109. #include <sys/sysctl.h>
  110. #endif
  111. #ifdef HAVE_NET_ROUTE_H
  112. #include <net/route.h>
  113. #endif
  114. #include <stdlib.h>
  115. #include <unistd.h>
  116. #ifdef HAVE_PF_PACKET
  117. #undef INJECT_METHOD
  118. /* give priority to TX_RING */
  119. #ifndef HAVE_TX_RING
  120. #define INJECT_METHOD "PF_PACKET send()"
  121. #else
  122. #define INJECT_METHOD "PF_PACKET / TX_RING"
  123. #endif
  124. #include <fcntl.h>
  125. #include <sys/utsname.h>
  126. #include <net/if.h>
  127. #include <netinet/in.h>
  128. #include <net/if_arp.h>
  129. #include <netpacket/packet.h>
  130. #ifdef HAVE_TX_RING
  131. #include "txring.h"
  132. #endif
  133. static sendpacket_t *sendpacket_open_pf(const char *, char *);
  134. static struct tcpr_ether_addr *sendpacket_get_hwaddr_pf(sendpacket_t *);
  135. static int get_iface_index(int fd, const char *device, char *);
  136. #endif /* HAVE_PF_PACKET */
  137. #ifdef HAVE_TUNTAP
  138. #ifdef HAVE_LINUX
  139. #include <net/if.h>
  140. #include <linux/if_tun.h>
  141. #elif defined(HAVE_FREEBSD)
  142. #define TUNTAP_DEVICE_PREFIX "/dev/"
  143. #endif
  144. static sendpacket_t *sendpacket_open_tuntap(const char *, char *);
  145. #endif
  146. #if defined HAVE_BPF && ! defined INJECT_METHOD
  147. #undef INJECT_METHOD
  148. #define INJECT_METHOD "bpf send()"
  149. #include <net/bpf.h>
  150. #include <sys/sysctl.h>
  151. #include <net/route.h>
  152. #include <net/if.h>
  153. #include <sys/uio.h>
  154. #include <net/if_dl.h> // used for get_hwaddr_bpf()
  155. static sendpacket_t *sendpacket_open_bpf(const char *, char *) _U_;
  156. static struct tcpr_ether_addr *sendpacket_get_hwaddr_bpf(sendpacket_t *) _U_;
  157. #endif /* HAVE_BPF */
  158. #if defined HAVE_LIBDNET && ! defined INJECT_METHOD
  159. #undef INJECT_METHOD
  160. #define INJECT_METHOD "libdnet eth_send()"
  161. /* need to undef these which are pulled in via defines.h, prior to importing dnet.h */
  162. #undef icmp_id
  163. #undef icmp_seq
  164. #undef icmp_data
  165. #undef icmp_mask
  166. #ifdef HAVE_DNET_H
  167. #include <dnet.h>
  168. #endif
  169. #ifdef HAVE_DUMBNET_H
  170. #include <dumbnet.h>
  171. #endif
  172. static sendpacket_t *sendpacket_open_libdnet(const char *, char *) _U_;
  173. static struct tcpr_ether_addr *sendpacket_get_hwaddr_libdnet(sendpacket_t *) _U_;
  174. #endif /* HAVE_LIBDNET */
  175. #if (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET) && !(defined HAVE_PF_PACKET || defined BPF || defined HAVE_LIBDNET)
  176. static sendpacket_t *sendpacket_open_pcap(const char *, char *) _U_;
  177. static struct tcpr_ether_addr *sendpacket_get_hwaddr_pcap(sendpacket_t *) _U_;
  178. #endif /* HAVE_PCAP_INJECT || HAVE_PACKET_SENDPACKET */
  179. #if defined HAVE_PCAP_INJECT && ! defined INJECT_METHOD
  180. #undef INJECT_METHOD
  181. #define INJECT_METHOD "pcap_inject()"
  182. #elif defined HAVE_PCAP_SENDPACKET && ! defined INJECT_METHOD
  183. #undef INJECT_METHOD
  184. #define INJECT_METHOD "pcap_sendpacket()"
  185. #endif
  186. static void sendpacket_seterr(sendpacket_t *sp, const char *fmt, ...);
  187. static sendpacket_t * sendpacket_open_khial(const char *, char *) _U_;
  188. static struct tcpr_ether_addr * sendpacket_get_hwaddr_khial(sendpacket_t *) _U_;
  189. /**
  190. * returns number of bytes sent on success or -1 on error
  191. * Note: it is theoretically possible to get a return code >0 and < len
  192. * which for most people would be considered an error (the packet wasn't fully sent)
  193. * so you may want to test for recode != len too.
  194. *
  195. * Most socket API's have two interesting errors: ENOBUFS & EAGAIN. ENOBUFS
  196. * is usually due to the kernel buffers being full. EAGAIN happens when you
  197. * try to send traffic faster then the PHY allows.
  198. */
  199. int
  200. sendpacket(sendpacket_t *sp, const u_char *data, size_t len, struct pcap_pkthdr *pkthdr)
  201. {
  202. int retcode = 0, val;
  203. static u_char buffer[10000]; /* 10K bytes, enough for jumbo frames + pkthdr
  204. * larger than page size so made static to
  205. * prevent page misses on stack
  206. */
  207. static const size_t buffer_payload_size = sizeof(buffer) + sizeof(struct pcap_pkthdr);
  208. assert(sp);
  209. assert(data);
  210. if (len == 0)
  211. return -1;
  212. TRY_SEND_AGAIN:
  213. sp->attempt ++;
  214. switch (sp->handle_type) {
  215. case SP_TYPE_KHIAL:
  216. memcpy(buffer, pkthdr, sizeof(struct pcap_pkthdr));
  217. memcpy(buffer + sizeof(struct pcap_pkthdr), data, min(len, buffer_payload_size));
  218. /* tell the kernel module which direction the traffic is going */
  219. if (sp->cache_dir == TCPR_DIR_C2S) { /* aka PRIMARY */
  220. val = KHIAL_DIRECTION_RX;
  221. if (ioctl(sp->handle.fd, KHIAL_SET_DIRECTION, (void *)&val) < 0) {
  222. sendpacket_seterr(sp, "Error setting direction on %s: %s (%d)",
  223. sp->device, strerror(errno), errno);
  224. return -1;
  225. }
  226. } else if (sp->cache_dir == TCPR_DIR_S2C) {
  227. val = KHIAL_DIRECTION_TX;
  228. if (ioctl(sp->handle.fd, KHIAL_SET_DIRECTION, (void *)&val) < 0) {
  229. sendpacket_seterr(sp, "Error setting direction on %s: %s (%d)",
  230. sp->device, strerror(errno), errno);
  231. return -1;
  232. }
  233. }
  234. /* write the pkthdr + packet data all at once */
  235. retcode = write(sp->handle.fd, (void *)buffer, sizeof(struct pcap_pkthdr) + len);
  236. retcode -= sizeof(struct pcap_pkthdr); /* only record packet bytes we sent, not pcap data too */
  237. if (retcode < 0 && !sp->abort) {
  238. switch(errno) {
  239. case EAGAIN:
  240. sp->retry_eagain ++;
  241. goto TRY_SEND_AGAIN;
  242. break;
  243. case ENOBUFS:
  244. sp->retry_enobufs ++;
  245. goto TRY_SEND_AGAIN;
  246. break;
  247. default:
  248. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  249. "khial", sp->sent + sp->failed + 1, strerror(errno), errno);
  250. }
  251. break;
  252. }
  253. break;
  254. case SP_TYPE_TUNTAP:
  255. retcode = write(sp->handle.fd, (void *)data, len);
  256. break;
  257. /* Linux PF_PACKET and TX_RING */
  258. case SP_TYPE_PF_PACKET:
  259. case SP_TYPE_TX_RING:
  260. #if defined HAVE_PF_PACKET
  261. #ifdef HAVE_TX_RING
  262. retcode = (int)txring_put(sp->tx_ring, data, len);
  263. #else
  264. retcode = (int)send(sp->handle.fd, (void *)data, len, 0);
  265. #endif
  266. /* out of buffers, or hit max PHY speed, silently retry
  267. * as long as we're not told to abort
  268. */
  269. if (retcode < 0 && !sp->abort) {
  270. switch (errno) {
  271. case EAGAIN:
  272. sp->retry_eagain ++;
  273. goto TRY_SEND_AGAIN;
  274. break;
  275. case ENOBUFS:
  276. sp->retry_enobufs ++;
  277. goto TRY_SEND_AGAIN;
  278. break;
  279. default:
  280. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  281. INJECT_METHOD, sp->sent + sp->failed + 1, strerror(errno), errno);
  282. }
  283. }
  284. #endif /* HAVE_PF_PACKET */
  285. break;
  286. /* BPF */
  287. case SP_TYPE_BPF:
  288. #if defined HAVE_BPF
  289. retcode = write(sp->handle.fd, (void *)data, len);
  290. /* out of buffers, or hit max PHY speed, silently retry */
  291. if (retcode < 0 && !sp->abort) {
  292. switch (errno) {
  293. case EAGAIN:
  294. sp->retry_eagain ++;
  295. goto TRY_SEND_AGAIN;
  296. break;
  297. case ENOBUFS:
  298. sp->retry_enobufs ++;
  299. goto TRY_SEND_AGAIN;
  300. break;
  301. default:
  302. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  303. INJECT_METHOD, sp->sent + sp->failed + 1, strerror(errno), errno);
  304. }
  305. }
  306. #endif
  307. break;
  308. /* Libdnet */
  309. case SP_TYPE_LIBDNET:
  310. #if defined HAVE_LIBDNET
  311. retcode = eth_send(sp->handle.ldnet, (void*)data, (size_t)len);
  312. /* out of buffers, or hit max PHY speed, silently retry */
  313. if (retcode < 0 && !sp->abort) {
  314. switch (errno) {
  315. case EAGAIN:
  316. sp->retry_eagain ++;
  317. goto TRY_SEND_AGAIN;
  318. break;
  319. case ENOBUFS:
  320. sp->retry_enobufs ++;
  321. goto TRY_SEND_AGAIN;
  322. break;
  323. default:
  324. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  325. INJECT_METHOD, sp->sent + sp->failed + 1, strerror(errno), errno);
  326. }
  327. }
  328. #endif
  329. break;
  330. case SP_TYPE_LIBPCAP:
  331. #if (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET)
  332. #if defined HAVE_PCAP_INJECT
  333. /*
  334. * pcap methods don't seem to support ENOBUFS, so we just straight fail
  335. * is there a better way???
  336. */
  337. retcode = pcap_inject(sp->handle.pcap, (void*)data, len);
  338. #elif defined HAVE_PCAP_SENDPACKET
  339. retcode = pcap_sendpacket(sp->handle.pcap, data, (int)len);
  340. #endif
  341. /* out of buffers, or hit max PHY speed, silently retry */
  342. if (retcode < 0 && !sp->abort) {
  343. switch (errno) {
  344. case EAGAIN:
  345. sp->retry_eagain ++;
  346. goto TRY_SEND_AGAIN;
  347. break;
  348. case ENOBUFS:
  349. sp->retry_enobufs ++;
  350. goto TRY_SEND_AGAIN;
  351. break;
  352. default:
  353. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  354. INJECT_METHOD, sp->sent + sp->failed + 1, pcap_geterr(sp->handle.pcap), errno);
  355. }
  356. }
  357. #if defined HAVE_PCAP_SENDPACKET
  358. /*
  359. * pcap_sendpacket returns 0 on success, not the packet length!
  360. * hence, we have to fix retcode to be more standard on success
  361. */
  362. if (retcode == 0)
  363. retcode = len;
  364. #endif /* HAVE_PCAP_SENDPACKET */
  365. #endif /* HAVE_PCAP_INJECT || HAVE_PCAP_SENDPACKET */
  366. break;
  367. case SP_TYPE_NETMAP:
  368. #ifdef HAVE_NETMAP
  369. retcode = sendpacket_send_netmap(sp, data, len);
  370. if (retcode == -1) {
  371. sendpacket_seterr(sp, "interface hung!!");
  372. } else if (retcode == -2) {
  373. /* this indicates that a retry was requested - this is not a failure */
  374. sp->retry_eagain ++;
  375. retcode = 0;
  376. #ifdef HAVE_SCHED_H
  377. /* yield the CPU so other apps remain responsive */
  378. sched_yield();
  379. #endif
  380. goto TRY_SEND_AGAIN;
  381. }
  382. #endif /* HAVE_NETMAP */
  383. break;
  384. default:
  385. errx(-1, "Unsupported sp->handle_type = %d", sp->handle_type);
  386. } /* end case */
  387. if (retcode < 0) {
  388. sp->failed ++;
  389. } else if (sp->abort) {
  390. sendpacket_seterr(sp, "User abort");
  391. } else if (retcode != (int)len) {
  392. sendpacket_seterr(sp, "Only able to write %d bytes out of %u bytes total",
  393. retcode, len);
  394. sp->trunc_packets ++;
  395. } else {
  396. sp->bytes_sent += len;
  397. sp->sent ++;
  398. }
  399. return retcode;
  400. }
  401. /**
  402. * Open the given network device name and returns a sendpacket_t struct
  403. * pass the error buffer (in case there's a problem) and the direction
  404. * that this interface represents
  405. */
  406. sendpacket_t *
  407. sendpacket_open(const char *device, char *errbuf, tcpr_dir_t direction,
  408. sendpacket_type_t sendpacket_type, void *arg)
  409. {
  410. sendpacket_t *sp;
  411. struct stat sdata;
  412. assert(device);
  413. assert(errbuf);
  414. errbuf[0] = '\0';
  415. /* khial is universal */
  416. if (stat(device, &sdata) == 0) {
  417. if (((sdata.st_mode & S_IFMT) == S_IFCHR)) {
  418. sp = sendpacket_open_khial(device, errbuf);
  419. } else {
  420. switch (sdata.st_mode & S_IFMT) {
  421. case S_IFBLK:
  422. errx(-1, "\"%s\" is a block device and is not a valid Tcpreplay device",
  423. device);
  424. break;
  425. case S_IFDIR:
  426. errx(-1, "\"%s\" is a directory and is not a valid Tcpreplay device",
  427. device);
  428. break;
  429. case S_IFIFO:
  430. errx(-1, "\"%s\" is a FIFO and is not a valid Tcpreplay device",
  431. device);
  432. break;
  433. case S_IFLNK:
  434. errx(-1, "\"%s\" is a symbolic link and is not a valid Tcpreplay device",
  435. device);
  436. break;
  437. case S_IFREG:
  438. errx(-1, "\"%s\" is a file and is not a valid Tcpreplay device",
  439. device);
  440. break;
  441. default:
  442. errx(-1, "\"%s\" is not a valid Tcpreplay device",
  443. device);
  444. break;
  445. }
  446. }
  447. #ifdef HAVE_TUNTAP
  448. } else if (strncmp(device, "tap", 3) == 0) {
  449. sp = sendpacket_open_tuntap(device, errbuf);
  450. #endif
  451. } else {
  452. #ifdef HAVE_NETMAP
  453. if (sendpacket_type == SP_TYPE_NETMAP)
  454. sp = (sendpacket_t*)sendpacket_open_netmap(device, errbuf, arg);
  455. else
  456. #endif
  457. #if defined HAVE_PF_PACKET
  458. sp = sendpacket_open_pf(device, errbuf);
  459. #elif defined HAVE_BPF
  460. sp = sendpacket_open_bpf(device, errbuf);
  461. #elif defined HAVE_LIBDNET
  462. sp = sendpacket_open_libdnet(device, errbuf);
  463. #elif (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET)
  464. sp = sendpacket_open_pcap(device, errbuf);
  465. #else
  466. #error "No defined packet injection method for sendpacket_open()"
  467. #endif
  468. }
  469. if (sp) {
  470. sp->open = 1;
  471. sp->cache_dir = direction;
  472. } else {
  473. errx(-1, "failed to open device %s: %s", device, errbuf);
  474. }
  475. return sp;
  476. }
  477. /**
  478. * Get packet stats for the given sendpacket_t
  479. */
  480. size_t
  481. sendpacket_getstat(sendpacket_t *sp, char *buf, size_t buf_size)
  482. {
  483. size_t offset;
  484. assert(sp);
  485. assert(buf);
  486. memset(buf, 0, buf_size);
  487. offset = snprintf(buf, buf_size, "Statistics for network device: %s\n"
  488. "\tSuccessful packets: " COUNTER_SPEC "\n"
  489. "\tFailed packets: " COUNTER_SPEC "\n"
  490. "\tTruncated packets: " COUNTER_SPEC "\n"
  491. "\tRetried packets (ENOBUFS): " COUNTER_SPEC "\n"
  492. "\tRetried packets (EAGAIN): " COUNTER_SPEC "\n",
  493. sp->device, sp->sent, sp->failed, sp->trunc_packets,
  494. sp->retry_enobufs, sp->retry_eagain);
  495. if (sp->flow_packets && offset > 0) {
  496. offset += snprintf(&buf[offset], buf_size - offset,
  497. "\tFlows total: " COUNTER_SPEC "\n"
  498. "\tFlows unique: " COUNTER_SPEC "\n"
  499. "\tFlows expired: " COUNTER_SPEC "\n"
  500. "\tFlow packets: " COUNTER_SPEC "\n"
  501. "\tNon-flow packets: " COUNTER_SPEC "\n"
  502. "\tInvalid flow packets: " COUNTER_SPEC "\n",
  503. sp->flows, sp->flows_expired, sp->flows_expired, sp->flow_packets,
  504. sp->flow_non_flow_packets, sp->flows_invalid_packets);
  505. }
  506. return offset;
  507. }
  508. /**
  509. * close the given sendpacket
  510. */
  511. void
  512. sendpacket_close(sendpacket_t *sp)
  513. {
  514. assert(sp);
  515. switch(sp->handle_type) {
  516. case SP_TYPE_KHIAL:
  517. close(sp->handle.fd);
  518. break;
  519. case SP_TYPE_BPF:
  520. #if (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET)
  521. close(sp->handle.fd);
  522. #endif
  523. break;
  524. case SP_TYPE_PF_PACKET:
  525. case SP_TYPE_TX_RING:
  526. #ifdef HAVE_PF_PACKET
  527. close(sp->handle.fd);
  528. #endif
  529. break;
  530. case SP_TYPE_LIBPCAP:
  531. #ifdef HAVE_LIBPCAP
  532. pcap_close(sp->handle.pcap);
  533. #endif
  534. break;
  535. case SP_TYPE_LIBDNET:
  536. #ifdef HAVE_LIBDNET
  537. eth_close(sp->handle.ldnet);
  538. #endif
  539. break;
  540. case SP_TYPE_LIBNET:
  541. err(-1, "Libnet is no longer supported!");
  542. break;
  543. case SP_TYPE_NETMAP:
  544. #ifdef HAVE_NETMAP
  545. sendpacket_close_netmap(sp);
  546. #endif /* HAVE_NETMAP */
  547. break;
  548. case SP_TYPE_TUNTAP:
  549. #ifdef HAVE_TUNTAP
  550. close(sp->handle.fd);
  551. #endif
  552. break;
  553. case SP_TYPE_NONE:
  554. err(-1, "no injector selected!");
  555. break;
  556. }
  557. safe_free(sp);
  558. }
  559. /**
  560. * returns the Layer 2 address of the interface current
  561. * open. on error, return NULL
  562. */
  563. struct tcpr_ether_addr *
  564. sendpacket_get_hwaddr(sendpacket_t *sp)
  565. {
  566. struct tcpr_ether_addr *addr;
  567. assert(sp);
  568. /* if we already have our MAC address stored, just return it */
  569. if (memcmp(&sp->ether, "\x00\x00\x00\x00\x00\x00", ETHER_ADDR_LEN) != 0)
  570. return &sp->ether;
  571. if (sp->handle_type == SP_TYPE_KHIAL) {
  572. addr = sendpacket_get_hwaddr_khial(sp);
  573. } else {
  574. #if defined HAVE_PF_PACKET
  575. addr = sendpacket_get_hwaddr_pf(sp);
  576. #elif defined HAVE_BPF
  577. addr = sendpacket_get_hwaddr_bpf(sp);
  578. #elif defined HAVE_LIBDNET
  579. addr = sendpacket_get_hwaddr_libdnet(sp);
  580. #elif (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET)
  581. addr = sendpacket_get_hwaddr_pcap(sp);
  582. #endif
  583. }
  584. return addr;
  585. }
  586. /**
  587. * returns the error string
  588. */
  589. char *
  590. sendpacket_geterr(sendpacket_t *sp)
  591. {
  592. assert(sp);
  593. return sp->errbuf;
  594. }
  595. /**
  596. * Set's the error string
  597. */
  598. static void
  599. sendpacket_seterr(sendpacket_t *sp, const char *fmt, ...)
  600. {
  601. va_list ap;
  602. assert(sp);
  603. va_start(ap, fmt);
  604. if (fmt != NULL)
  605. (void)vsnprintf(sp->errbuf, SENDPACKET_ERRBUF_SIZE, fmt, ap);
  606. va_end(ap);
  607. sp->errbuf[(SENDPACKET_ERRBUF_SIZE-1)] = '\0'; // be safe
  608. }
  609. #if (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET) && !(defined HAVE_PF_PACKET || defined BPF || defined HAVE_LIBDNET)
  610. /**
  611. * Inner sendpacket_open() method for using libpcap
  612. */
  613. static sendpacket_t *
  614. sendpacket_open_pcap(const char *device, char *errbuf)
  615. {
  616. pcap_t *pcap;
  617. sendpacket_t *sp;
  618. #ifdef BIOCSHDRCMPLT
  619. u_int spoof_eth_src = 1;
  620. int fd;
  621. #endif
  622. assert(device);
  623. assert(errbuf);
  624. dbg(1, "sendpacket: using Libpcap");
  625. /* open_pcap_live automatically fills out our errbuf for us */
  626. if ((pcap = pcap_open_live(device, 0, 0, 0, errbuf)) == NULL)
  627. return NULL;
  628. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  629. strlcpy(sp->device, device, sizeof(sp->device));
  630. sp->handle.pcap = pcap;
  631. #ifdef BIOCSHDRCMPLT
  632. /*
  633. * Only systems using BPF on the backend need this...
  634. * other systems don't have ioctl and will get compile errors.
  635. */
  636. fd = pcap_get_selectable_fd(pcap);
  637. if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1)
  638. errx(-1, "Unable to enable source MAC spoof support: %s", strerror(errno));
  639. #endif
  640. sp->handle_type = SP_TYPE_LIBPCAP;
  641. return sp;
  642. }
  643. /**
  644. * Get the hardware MAC address for the given interface using libpcap
  645. */
  646. static struct tcpr_ether_addr *
  647. sendpacket_get_hwaddr_pcap(sendpacket_t *sp)
  648. {
  649. assert(sp);
  650. sendpacket_seterr(sp, "Error: sendpacket_get_hwaddr() not yet supported for pcap injection");
  651. return NULL;
  652. }
  653. #endif /* HAVE_PCAP_INJECT || HAVE_PCAP_SENDPACKET */
  654. #if defined HAVE_LIBDNET && !defined HAVE_PF_PACKET && !defined HAVE_BPF
  655. /**
  656. * Inner sendpacket_open() method for using libdnet
  657. */
  658. static sendpacket_t *
  659. sendpacket_open_libdnet(const char *device, char *errbuf)
  660. {
  661. eth_t *ldnet;
  662. sendpacket_t *sp;
  663. assert(device);
  664. assert(errbuf);
  665. dbg(1, "sendpacket: using Libdnet");
  666. if ((ldnet = eth_open(device)) == NULL)
  667. return NULL;
  668. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  669. strlcpy(sp->device, device, sizeof(sp->device));
  670. sp->handle.ldnet = ldnet;
  671. sp->handle_type = SP_TYPE_LIBDNET;
  672. return sp;
  673. }
  674. /**
  675. * Get the hardware MAC address for the given interface using libdnet
  676. */
  677. static struct tcpr_ether_addr *
  678. sendpacket_get_hwaddr_libdnet(sendpacket_t *sp)
  679. {
  680. struct tcpr_ether_addr *addr = NULL;
  681. int ret;
  682. assert(sp);
  683. ret = eth_get(sp->handle.ldnet, (eth_addr_t *)addr);
  684. if (addr == NULL || ret < 0) {
  685. sendpacket_seterr(sp, "Error getting hwaddr via libdnet: %s", strerror(errno));
  686. return NULL;
  687. }
  688. memcpy(&sp->ether, addr, sizeof(struct tcpr_ether_addr));
  689. return(&sp->ether);
  690. }
  691. #endif /* HAVE_LIBDNET */
  692. #if defined HAVE_TUNTAP
  693. /**
  694. * Inner sendpacket_open() method for tuntap devices
  695. */
  696. static sendpacket_t *
  697. sendpacket_open_tuntap(const char *device, char *errbuf)
  698. {
  699. sendpacket_t *sp;
  700. struct ifreq ifr;
  701. int tapfd;
  702. assert(device);
  703. assert(errbuf);
  704. #if defined HAVE_LINUX
  705. if ((tapfd = open("/dev/net/tun", O_RDWR)) < 0) {
  706. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Could not open /dev/net/tun control file: %s", strerror(errno));
  707. return NULL;
  708. }
  709. memset(&ifr, 0, sizeof(ifr));
  710. ifr.ifr_flags = (IFF_TAP | IFF_NO_PI);
  711. strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)-1);
  712. if (ioctl(tapfd, TUNSETIFF, (void *) &ifr) < 0) {
  713. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to create tuntap interface: %s", device);
  714. close(tapfd);
  715. return NULL;
  716. }
  717. #elif defined(HAVE_FREEBSD)
  718. if (*device == '/') {
  719. if ((tapfd = open(device, O_RDWR)) < 0) {
  720. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Could not open device %s: %s", device, strerror(errno));
  721. return NULL;
  722. }
  723. } else {
  724. /* full path needed */
  725. char *path;
  726. int prefix_length = strlen(TUNTAP_DEVICE_PREFIX);
  727. if ((path = malloc(strlen(device) + prefix_length + 1)) == NULL) {
  728. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Malloc error: %s", strerror(errno));
  729. return NULL;
  730. }
  731. snprintf(path, strlen(device) + prefix_length + 1, "%s%s", TUNTAP_DEVICE_PREFIX, device);
  732. if ((tapfd = open(path, O_RDWR)) < 0) {
  733. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Could not open device %s: %s", path, strerror(errno));
  734. free(path);
  735. return NULL;
  736. }
  737. free(path);
  738. }
  739. #endif
  740. /* prep & return our sp handle */
  741. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  742. strlcpy(sp->device, device, sizeof(sp->device));
  743. sp->handle.fd = tapfd;
  744. sp->handle_type = SP_TYPE_TUNTAP;
  745. return sp;
  746. }
  747. #endif
  748. #if defined HAVE_PF_PACKET
  749. /**
  750. * Inner sendpacket_open() method for using Linux's PF_PACKET or TX_RING
  751. */
  752. static sendpacket_t *
  753. sendpacket_open_pf(const char *device, char *errbuf)
  754. {
  755. int mysocket;
  756. sendpacket_t *sp;
  757. struct ifreq ifr;
  758. struct sockaddr_ll sa;
  759. int err;
  760. socklen_t errlen = sizeof(err);
  761. unsigned int UNUSED(mtu) = 1500;
  762. #ifdef SO_BROADCAST
  763. int n = 1;
  764. #endif
  765. assert(device);
  766. assert(errbuf);
  767. #if defined TX_RING
  768. dbg(1, "sendpacket: using TX_RING");
  769. #else
  770. dbg(1, "sendpacket: using PF_PACKET");
  771. #endif
  772. memset(&sa, 0, sizeof(sa));
  773. /* open our socket */
  774. if ((mysocket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
  775. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "socket: %s", strerror(errno));
  776. return NULL;
  777. }
  778. /* get the interface id for the device */
  779. if ((sa.sll_ifindex = get_iface_index(mysocket, device, errbuf)) < 0) {
  780. close(mysocket);
  781. return NULL;
  782. }
  783. /* bind socket to our interface id */
  784. sa.sll_family = AF_PACKET;
  785. sa.sll_protocol = htons(ETH_P_ALL);
  786. if (bind(mysocket, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
  787. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "bind error: %s", strerror(errno));
  788. close(mysocket);
  789. return NULL;
  790. }
  791. /* check for errors, network down, etc... */
  792. if (getsockopt(mysocket, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0) {
  793. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "error opening %s: %s", device,
  794. strerror(errno));
  795. close(mysocket);
  796. return NULL;
  797. }
  798. if (err > 0) {
  799. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "error opening %s: %s", device,
  800. strerror(err));
  801. close(mysocket);
  802. return NULL;
  803. }
  804. /* get hardware type for our interface */
  805. memset(&ifr, 0, sizeof(ifr));
  806. strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  807. if (ioctl(mysocket, SIOCGIFHWADDR, &ifr) < 0) {
  808. close(mysocket);
  809. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Error getting hardware type: %s",
  810. strerror(errno));
  811. return NULL;
  812. }
  813. /* make sure it's not loopback (PF_PACKET doesn't support it) */
  814. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)
  815. warnx("Unsupported physical layer type 0x%04x on %s. Maybe it works, maybe it won't."
  816. " See tickets #123/318", ifr.ifr_hwaddr.sa_family, device);
  817. #ifdef SO_BROADCAST
  818. /*
  819. * man 7 socket
  820. *
  821. * Set or get the broadcast flag. When enabled, datagram sockets
  822. * receive packets sent to a broadcast address and they are allowed
  823. * to send packets to a broadcast address. This option has no
  824. * effect on stream-oriented sockets.
  825. */
  826. if (setsockopt(mysocket, SOL_SOCKET, SO_BROADCAST, &n, sizeof(n)) == -1) {
  827. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE,
  828. "SO_BROADCAST: %s", strerror(errno));
  829. close(mysocket);
  830. return NULL;
  831. }
  832. #endif /* SO_BROADCAST */
  833. /* prep & return our sp handle */
  834. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  835. strlcpy(sp->device, device, sizeof(sp->device));
  836. sp->handle.fd = mysocket;
  837. #ifdef HAVE_TX_RING
  838. /* Look up for MTU */
  839. memset(&ifr, 0, sizeof(ifr));
  840. strlcpy(ifr.ifr_name, sp->device, sizeof(ifr.ifr_name));
  841. if (ioctl(mysocket, SIOCGIFMTU, &ifr) < 0) {
  842. close(mysocket);
  843. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Error getting MTU: %s", strerror(errno));
  844. return NULL;
  845. }
  846. mtu = ifr.ifr_ifru.ifru_mtu;
  847. /* Init TX ring for sp->handle.fd socket */
  848. if ((sp->tx_ring = txring_init(sp->handle.fd, mtu)) == 0) {
  849. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "txring_init: %s", strerror(errno));
  850. close(mysocket);
  851. return NULL;
  852. }
  853. sp->handle_type = SP_TYPE_TX_RING;
  854. #else
  855. sp->handle_type = SP_TYPE_PF_PACKET;
  856. #endif
  857. return sp;
  858. }
  859. /**
  860. * get the interface index (necessary for sending packets w/ PF_PACKET)
  861. */
  862. static int
  863. get_iface_index(int fd, const char *device, char *errbuf) {
  864. struct ifreq ifr;
  865. memset(&ifr, 0, sizeof(ifr));
  866. strlcpy(ifr.ifr_name, (const char *)device, sizeof(ifr.ifr_name));
  867. if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
  868. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "ioctl: %s", strerror(errno));
  869. return (-1);
  870. }
  871. return ifr.ifr_ifindex;
  872. }
  873. /**
  874. * gets the hardware address via Linux's PF packet interface
  875. */
  876. static struct tcpr_ether_addr *
  877. sendpacket_get_hwaddr_pf(sendpacket_t *sp)
  878. {
  879. struct ifreq ifr;
  880. int fd;
  881. assert(sp);
  882. if (!sp->open) {
  883. sendpacket_seterr(sp, "Unable to get hardware address on un-opened sendpacket handle");
  884. return NULL;
  885. }
  886. /* create dummy socket for ioctl */
  887. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  888. sendpacket_seterr(sp, "Unable to open dummy socket for get_hwaddr: %s", strerror(errno));
  889. return NULL;
  890. }
  891. memset(&ifr, 0, sizeof(ifr));
  892. strlcpy(ifr.ifr_name, sp->device, sizeof(ifr.ifr_name));
  893. if (ioctl(fd, SIOCGIFHWADDR, (int8_t *)&ifr) < 0) {
  894. close(fd);
  895. sendpacket_seterr(sp, "Error getting hardware address: %s", strerror(errno));
  896. return NULL;
  897. }
  898. memcpy(&sp->ether, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
  899. close(fd);
  900. return(&sp->ether);
  901. }
  902. #endif /* HAVE_PF_PACKET */
  903. #if defined HAVE_BPF
  904. /**
  905. * Inner sendpacket_open() method for using BSD's BPF interface
  906. */
  907. static sendpacket_t *
  908. sendpacket_open_bpf(const char *device, char *errbuf)
  909. {
  910. sendpacket_t *sp;
  911. char bpf_dev[10];
  912. int dev, mysocket, link_offset, link_type;
  913. struct ifreq ifr;
  914. struct bpf_version bv;
  915. u_int v;
  916. #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
  917. u_int spoof_eth_src = 1;
  918. #endif
  919. assert(device);
  920. assert(errbuf);
  921. memset(&ifr, '\0', sizeof(struct ifreq));
  922. dbg(1, "sendpacket: using BPF");
  923. /* open socket */
  924. mysocket = -1;
  925. for (dev = 0; dev <= 9; dev ++) {
  926. memset(bpf_dev, '\0', sizeof(bpf_dev));
  927. snprintf(bpf_dev, sizeof(bpf_dev), "/dev/bpf%d", dev);
  928. if ((mysocket = open(bpf_dev, O_RDWR, 0)) > 0) {
  929. break;
  930. }
  931. }
  932. /* error?? */
  933. if (mysocket < 0) {
  934. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE,
  935. "Unable to open /dev/bpfX: %s", strerror(errno));
  936. errbuf[SENDPACKET_ERRBUF_SIZE -1] = '\0';
  937. return NULL;
  938. }
  939. /* get BPF version */
  940. if (ioctl(mysocket, BIOCVERSION, (caddr_t)&bv) < 0) {
  941. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to get bpf version: %s", strerror(errno));
  942. return NULL;
  943. }
  944. if (bv.bv_major != BPF_MAJOR_VERSION || bv.bv_minor != BPF_MINOR_VERSION) {
  945. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Kernel's bpf version is out of date.");
  946. return NULL;
  947. }
  948. /* attach to device */
  949. strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  950. if (ioctl(mysocket, BIOCSETIF, (caddr_t)&ifr) < 0) {
  951. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to bind %s to %s: %s",
  952. bpf_dev, device, strerror(errno));
  953. return NULL;
  954. }
  955. /* get datalink type */
  956. if (ioctl(mysocket, BIOCGDLT, (caddr_t)&v) < 0) {
  957. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to get datalink type: %s",
  958. strerror(errno));
  959. return NULL;
  960. }
  961. /*
  962. * NetBSD and FreeBSD BPF have an ioctl for enabling/disabling
  963. * automatic filling of the link level source address.
  964. */
  965. #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
  966. if (ioctl(mysocket, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
  967. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE,
  968. "Unable to enable spoofing src MAC: %s", strerror(errno));
  969. return NULL;
  970. }
  971. #endif
  972. /* assign link type and offset */
  973. switch (v) {
  974. case DLT_SLIP:
  975. link_offset = 0x10;
  976. break;
  977. case DLT_RAW:
  978. link_offset = 0x0;
  979. break;
  980. case DLT_PPP:
  981. link_offset = 0x04;
  982. break;
  983. case DLT_EN10MB:
  984. default: /* default to Ethernet */
  985. link_offset = 0xe;
  986. break;
  987. }
  988. #if _BSDI_VERSION - 0 > 199510
  989. switch (v) {
  990. case DLT_SLIP:
  991. v = DLT_SLIP_BSDOS;
  992. link_offset = 0x10;
  993. break;
  994. case DLT_PPP:
  995. v = DLT_PPP_BSDOS;
  996. link_offset = 0x04;
  997. break;
  998. }
  999. #endif
  1000. link_type = v;
  1001. /* allocate our sp handle, and return it */
  1002. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  1003. strlcpy(sp->device, device, sizeof(sp->device));
  1004. sp->handle.fd = mysocket;
  1005. //sp->link_type = link_type;
  1006. //sp->link_offset = link_offset;
  1007. sp->handle_type = SP_TYPE_BPF;
  1008. return sp;
  1009. }
  1010. /**
  1011. * Get the interface hardware MAC address when using BPF
  1012. */
  1013. static struct tcpr_ether_addr *
  1014. sendpacket_get_hwaddr_bpf(sendpacket_t *sp)
  1015. {
  1016. int mib[6];
  1017. size_t len;
  1018. int8_t *buf, *next, *end;
  1019. struct if_msghdr *ifm;
  1020. struct sockaddr_dl *sdl;
  1021. assert(sp);
  1022. mib[0] = CTL_NET;
  1023. mib[1] = AF_ROUTE;
  1024. mib[2] = 0;
  1025. mib[3] = AF_LINK;
  1026. mib[4] = NET_RT_IFLIST;
  1027. mib[5] = 0;
  1028. if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) {
  1029. sendpacket_seterr(sp, "%s(): sysctl(): %s", __func__, strerror(errno));
  1030. return NULL;
  1031. }
  1032. buf = (int8_t *)safe_malloc(len);
  1033. if (sysctl(mib, 6, buf, &len, NULL, 0) == -1) {
  1034. sendpacket_seterr(sp, "%s(): sysctl(): %s", __func__, strerror(errno));
  1035. safe_free(buf);
  1036. return NULL;
  1037. }
  1038. end = buf + len;
  1039. for (next = buf; next < end; next += ifm->ifm_msglen) {
  1040. ifm = (struct if_msghdr *)next;
  1041. if (ifm->ifm_type == RTM_IFINFO) {
  1042. sdl = (struct sockaddr_dl *)(ifm + 1);
  1043. if (strncmp(&sdl->sdl_data[0], sp->device, sdl->sdl_len) == 0) {
  1044. memcpy(&sp->ether, LLADDR(sdl), ETHER_ADDR_LEN);
  1045. break;
  1046. }
  1047. }
  1048. }
  1049. safe_free(buf);
  1050. return(&sp->ether);
  1051. }
  1052. #endif /* HAVE_BPF */
  1053. /**
  1054. * Get the DLT type of the opened sendpacket
  1055. * Return -1 if we can't figure it out, else return the DLT_ value
  1056. */
  1057. int
  1058. sendpacket_get_dlt(sendpacket_t *sp)
  1059. {
  1060. int dlt = DLT_EN10MB;
  1061. if (sp->handle_type == SP_TYPE_KHIAL ||
  1062. sp->handle_type == SP_TYPE_NETMAP ||
  1063. sp->handle_type == SP_TYPE_TUNTAP) {
  1064. /* always EN10MB */
  1065. ;
  1066. } else {
  1067. #if defined HAVE_BPF
  1068. int rcode;
  1069. if ((rcode = ioctl(sp->handle.fd, BIOCGDLT, &dlt)) < 0) {
  1070. warnx("Unable to get DLT value for BPF device (%s): %s", sp->device, strerror(errno));
  1071. return(-1);
  1072. }
  1073. #elif defined HAVE_PF_PACKET || defined HAVE_LIBDNET
  1074. /* use libpcap to get dlt */
  1075. pcap_t *pcap;
  1076. char errbuf[PCAP_ERRBUF_SIZE];
  1077. if ((pcap = pcap_open_live(sp->device, 65535, 0, 0, errbuf)) == NULL) {
  1078. warnx("Unable to get DLT value for %s: %s", sp->device, errbuf);
  1079. return(-1);
  1080. }
  1081. dlt = pcap_datalink(pcap);
  1082. pcap_close(pcap);
  1083. #elif defined HAVE_PCAP_SENDPACKET || defined HAVE_PCAP_INJECT
  1084. dlt = pcap_datalink(sp->handle.pcap);
  1085. #endif
  1086. }
  1087. return dlt;
  1088. }
  1089. /**
  1090. * \brief Returns a string of the name of the injection method being used
  1091. */
  1092. const char *
  1093. sendpacket_get_method(sendpacket_t *sp)
  1094. {
  1095. if (sp == NULL) {
  1096. return INJECT_METHOD;
  1097. } else if (sp->handle_type == SP_TYPE_KHIAL) {
  1098. return "khial";
  1099. } else if (sp->handle_type == SP_TYPE_NETMAP) {
  1100. return "netmap";
  1101. } else {
  1102. return INJECT_METHOD;
  1103. }
  1104. }
  1105. /**
  1106. * Opens a character device for injecting packets directly into
  1107. * your kernel via a custom driver
  1108. */
  1109. static sendpacket_t *
  1110. sendpacket_open_khial(const char *device, char *errbuf)
  1111. {
  1112. int mysocket;
  1113. sendpacket_t *sp;
  1114. assert(device);
  1115. assert(errbuf);
  1116. if ((mysocket = open(device, O_WRONLY|O_EXCL)) < 0) {
  1117. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "error opening khial device: %s", strerror(errno));
  1118. return NULL;
  1119. }
  1120. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  1121. strlcpy(sp->device, device, sizeof(sp->device));
  1122. sp->handle.fd = mysocket;
  1123. sp->handle_type = SP_TYPE_KHIAL;
  1124. return sp;
  1125. }
  1126. /**
  1127. * Get the hardware MAC address for the given interface using khial
  1128. */
  1129. static struct tcpr_ether_addr *
  1130. sendpacket_get_hwaddr_khial(sendpacket_t *sp)
  1131. {
  1132. assert(sp);
  1133. sendpacket_seterr(sp, "Error: sendpacket_get_hwaddr() not yet supported for character devices");
  1134. return NULL;
  1135. }
  1136. /**
  1137. * \brief Cause the currently running sendpacket() call to stop
  1138. */
  1139. void
  1140. sendpacket_abort(sendpacket_t *sp)
  1141. {
  1142. assert(sp);
  1143. sp->abort = true;
  1144. }