sendpacket.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. /* $Id: sendpacket.c 2423 2010-03-13 07:09:49Z aturner $ */
  2. /*
  3. * Copyright (c) 2006-2010 Aaron Turner.
  4. * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
  5. * Copyright (c) 2000 Torsten Landschoff <torsten@debian.org>
  6. * Sebastian Krahmer <krahmer@cs.uni-potsdam.de>
  7. * Copyright (c) 1993, 1994, 1995, 1996, 1998
  8. * The Regents of the University of California.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the names of the copyright owners nor the names of its
  21. * contributors may be used to endorse or promote products derived from
  22. * this software without specific prior written permission.
  23. * 4. All advertising materials mentioning features or use of this software
  24. * display the following acknowledgement:
  25. * ``This product includes software developed by the University of
  26. * California, Lawrence Berkeley Laboratory and its contributors.''
  27. *
  28. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  31. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  32. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  36. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  37. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  38. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. */
  40. /* sendpacket.[ch] is my attempt to write a universal packet injection
  41. * API for BPF, libpcap, libdnet, and Linux's PF_PACKET. I got sick
  42. * and tired dealing with libnet bugs and its lack of active maintenence,
  43. * but unfortunately, libpcap frame injection support is relatively new
  44. * and not everyone uses Linux, so I decided to support all four as
  45. * best as possible. If your platform/OS/hardware supports an additional
  46. * injection method, then by all means add it here (and send me a patch).
  47. *
  48. * Anyways, long story short, for now the order of preference is:
  49. * 1. PF_PACKET
  50. * 2. BPF
  51. * 3. libdnet
  52. * 4. pcap_inject()
  53. * 5. pcap_sendpacket()
  54. *
  55. * Right now, one big problem with the pcap_* methods is that libpcap
  56. * doesn't provide a reliable method of getting the MAC address of
  57. * an interface (required for tcpbridge).
  58. * You can use PF_PACKET or BPF to get that, but if your system suports
  59. * those, might as well inject directly without going through another
  60. * level of indirection.
  61. *
  62. * Please note that some of this code was copied from Libnet 1.1.3
  63. */
  64. #include "config.h"
  65. #include "defines.h"
  66. #include "common.h"
  67. #include "sendpacket.h"
  68. #ifdef FORCE_INJECT_LIBDNET
  69. #undef HAVE_PF_PACKET
  70. #undef HAVE_PCAP_INJECT
  71. #undef HAVE_PCAP_SENDPACKET
  72. #undef HAVE_BPF
  73. #endif
  74. #ifdef FORCE_INJECT_BPF
  75. #undef HAVE_LIBDNET
  76. #undef HAVE_PCAP_INJECT
  77. #undef HAVE_PCAP_SENDPACKET
  78. #undef HAVE_PF_PACKET
  79. #endif
  80. #ifdef FORCE_INJECT_PCAP_INJECT
  81. #undef HAVE_LIBDNET
  82. #undef HAVE_PCAP_SENDPACKET
  83. #undef HAVE_BPF
  84. #undef HAVE_PF_PACKET
  85. #endif
  86. #ifdef FORCE_INJECT_PCAP_SENDPACKET
  87. #undef HAVE_LIBDNET
  88. #undef HAVE_PCAP_INJECT
  89. #undef HAVE_BPF
  90. #undef HAVE_PF_PACKET
  91. #endif
  92. #if (defined HAVE_WINPCAP && defined HAVE_PCAP_INJECT)
  93. #undef HAVE_PCAP_INJECT /* configure returns true for some odd reason */
  94. #endif
  95. #if !defined HAVE_PCAP_INJECT && !defined HAVE_PCAP_SENDPACKET && !defined HAVE_LIBDNET && !defined HAVE_PF_PACKET && !defined HAVE_BPF
  96. #error You need pcap_inject() or pcap_sendpacket() from libpcap, libdnet, Linux's PF_PACKET or *BSD's BPF
  97. #endif
  98. #include <string.h>
  99. #include <errno.h>
  100. #include <stdarg.h>
  101. #include <stdio.h>
  102. #include <sys/types.h>
  103. #include <sys/time.h>
  104. #include <sys/ioctl.h>
  105. #include <sys/file.h>
  106. #include <sys/socket.h>
  107. #ifdef HAVE_SYS_PARAM_H
  108. #include <sys/param.h>
  109. #endif
  110. #ifdef HAVE_SYS_SYSCTL_H
  111. #include <sys/sysctl.h>
  112. #endif
  113. #ifdef HAVE_NET_ROUTE_H
  114. #include <net/route.h>
  115. #endif
  116. #include <stdlib.h>
  117. #include <unistd.h>
  118. #ifdef HAVE_PF_PACKET
  119. #undef INJECT_METHOD
  120. #define INJECT_METHOD "PF_PACKET send()"
  121. #include <fcntl.h>
  122. #include <sys/utsname.h>
  123. #include <net/if.h>
  124. #include <netinet/in.h>
  125. #include <linux/if_ether.h>
  126. #include <net/if_arp.h>
  127. #include <netpacket/packet.h>
  128. #ifndef __GLIBC__
  129. typedef int socklen_t;
  130. #endif
  131. static sendpacket_t *sendpacket_open_pf(const char *, char *);
  132. static struct tcpr_ether_addr *sendpacket_get_hwaddr_pf(sendpacket_t *);
  133. static int get_iface_index(int fd, const int8_t *device, char *);
  134. #endif /* HAVE_PF_PACKET */
  135. #if defined HAVE_BPF && ! defined INJECT_METHOD
  136. #undef INJECT_METHOD
  137. #define INJECT_METHOD "bpf send()"
  138. #include <net/bpf.h>
  139. #include <sys/socket.h>
  140. #include <net/if.h>
  141. #include <sys/uio.h>
  142. #include <net/if_dl.h> // used for get_hwaddr_bpf()
  143. static sendpacket_t *sendpacket_open_bpf(const char *, char *) _U_;
  144. static struct tcpr_ether_addr *sendpacket_get_hwaddr_bpf(sendpacket_t *) _U_;
  145. #endif /* HAVE_BPF */
  146. #if defined HAVE_LIBDNET && ! defined INJECT_METHOD
  147. #undef INJECT_METHOD
  148. #define INJECT_METHOD "libdnet eth_send()"
  149. /* need to undef these which are pulled in via defines.h, prior to importing dnet.h */
  150. #undef icmp_id
  151. #undef icmp_seq
  152. #undef icmp_data
  153. #undef icmp_mask
  154. #include <dnet.h>
  155. static sendpacket_t *sendpacket_open_libdnet(const char *, char *) _U_;
  156. static struct tcpr_ether_addr *sendpacket_get_hwaddr_libdnet(sendpacket_t *) _U_;
  157. #endif /* HAVE_LIBDNET */
  158. #if (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET) && ! defined INJECT_METHOD
  159. static sendpacket_t *sendpacket_open_pcap(const char *, char *) _U_;
  160. static struct tcpr_ether_addr *sendpacket_get_hwaddr_pcap(sendpacket_t *) _U_;
  161. #endif /* HAVE_PCAP_INJECT || HAVE_PACKET_SENDPACKET */
  162. #if defined HAVE_PCAP_INJECT && ! defined INJECT_METHOD
  163. #undef INJECT_METHOD
  164. #define INJECT_METHOD "pcap_inject()"
  165. #elif defined HAVE_PCAP_SENDPACKET && ! defined INJECT_METHOD
  166. #undef INJECT_METHOD
  167. #define INJECT_METHOD "pcap_sendpacket()"
  168. #endif
  169. static void sendpacket_seterr(sendpacket_t *sp, const char *fmt, ...);
  170. /* You need to define didsig in your main .c file. Set to 1 if CTRL-C was pressed */
  171. extern volatile int didsig;
  172. /**
  173. * returns number of bytes sent on success or -1 on error
  174. * Note: it is theoretically possible to get a return code >0 and < len
  175. * which for most people would be considered an error (the packet wasn't fully sent)
  176. * so you may want to test for recode != len too.
  177. *
  178. * Most socket API's have two interesting errors: ENOBUFS & EAGAIN. ENOBUFS
  179. * is usually due to the kernel buffers being full. EAGAIN happens when you
  180. * try to send traffic faster then the PHY allows.
  181. */
  182. int
  183. sendpacket(sendpacket_t *sp, const u_char *data, size_t len)
  184. {
  185. int retcode;
  186. assert(sp);
  187. assert(data);
  188. if (len <= 0)
  189. return -1;
  190. TRY_SEND_AGAIN:
  191. sp->attempt ++;
  192. #if defined HAVE_PF_PACKET
  193. retcode = (int)send(sp->handle.fd, (void *)data, len, 0);
  194. /* out of buffers, or hit max PHY speed, silently retry */
  195. if (retcode < 0 && !didsig) {
  196. switch (errno) {
  197. case EAGAIN:
  198. sp->retry_eagain ++;
  199. goto TRY_SEND_AGAIN;
  200. break;
  201. case ENOBUFS:
  202. sp->retry_enobufs ++;
  203. goto TRY_SEND_AGAIN;
  204. break;
  205. default:
  206. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  207. INJECT_METHOD, sp->sent + sp->failed + 1, strerror(errno), errno);
  208. }
  209. }
  210. #elif defined HAVE_BPF
  211. retcode = write(sp->handle.fd, (void *)data, len);
  212. /* out of buffers, or hit max PHY speed, silently retry */
  213. if (retcode < 0 && !didsig) {
  214. switch (errno) {
  215. case EAGAIN:
  216. sp->retry_eagain ++;
  217. goto TRY_SEND_AGAIN;
  218. break;
  219. case ENOBUFS:
  220. sp->retry_enobufs ++;
  221. goto TRY_SEND_AGAIN;
  222. break;
  223. default:
  224. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  225. INJECT_METHOD, sp->sent + sp->failed + 1, strerror(errno), errno);
  226. }
  227. }
  228. #elif defined HAVE_LIBDNET
  229. retcode = eth_send(sp->handle.ldnet, (void*)data, (size_t)len);
  230. /* out of buffers, or hit max PHY speed, silently retry */
  231. if (retcode < 0 && !didsig) {
  232. switch (errno) {
  233. case EAGAIN:
  234. sp->retry_eagain ++;
  235. goto TRY_SEND_AGAIN;
  236. break;
  237. case ENOBUFS:
  238. sp->retry_enobufs ++;
  239. goto TRY_SEND_AGAIN;
  240. break;
  241. default:
  242. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  243. INJECT_METHOD, sp->sent + sp->failed + 1, strerror(errno), errno);
  244. }
  245. }
  246. #elif defined HAVE_PCAP_INJECT
  247. /*
  248. * pcap methods don't seem to support ENOBUFS, so we just straight fail
  249. * is there a better way???
  250. */
  251. retcode = pcap_inject(sp->handle.pcap, (void*)data, len);
  252. /* out of buffers, or hit max PHY speed, silently retry */
  253. if (retcode < 0 && !didsig) {
  254. switch (errno) {
  255. case EAGAIN:
  256. sp->retry_eagain ++;
  257. goto TRY_SEND_AGAIN;
  258. break;
  259. case ENOBUFS:
  260. sp->retry_enobufs ++;
  261. goto TRY_SEND_AGAIN;
  262. break;
  263. default:
  264. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  265. INJECT_METHOD, sp->sent + sp->failed + 1, pcap_geterr(sp->handle.pcap), errno);
  266. }
  267. }
  268. #elif defined HAVE_PCAP_SENDPACKET
  269. retcode = pcap_sendpacket(sp->handle.pcap, data, (int)len);
  270. /* out of buffers, or hit max PHY speed, silently retry */
  271. if (retcode < 0 && !didsig) {
  272. switch (errno) {
  273. case EAGAIN:
  274. sp->retry_eagain ++;
  275. goto TRY_SEND_AGAIN;
  276. break;
  277. case ENOBUFS:
  278. sp->retry_enobufs ++;
  279. goto TRY_SEND_AGAIN;
  280. break;
  281. default:
  282. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  283. INJECT_METHOD, sp->sent + sp->failed + 1, pcap_geterr(sp->handle.pcap), errno);
  284. }
  285. }
  286. /*
  287. * pcap_sendpacket returns 0 on success, not the packet length!
  288. * hence, we have to fix retcode to be more standard on success
  289. */
  290. if (retcode == 0)
  291. retcode = len;
  292. #endif
  293. if (retcode < 0) {
  294. sp->failed ++;
  295. } else if (retcode != (int)len) {
  296. sendpacket_seterr(sp, "Only able to write %d bytes out of %u bytes total",
  297. retcode, len);
  298. } else {
  299. sp->bytes_sent += len;
  300. sp->sent ++;
  301. }
  302. return retcode;
  303. }
  304. /**
  305. * Open the given network device name and returns a sendpacket_t struct
  306. * pass the error buffer (in case there's a problem) and the direction
  307. * that this interface represents
  308. */
  309. sendpacket_t *
  310. sendpacket_open(const char *device, char *errbuf, tcpr_dir_t direction)
  311. {
  312. sendpacket_t *sp;
  313. assert(device);
  314. assert(errbuf);
  315. #if defined HAVE_PF_PACKET
  316. sp = sendpacket_open_pf(device, errbuf);
  317. #elif defined HAVE_BPF
  318. sp = sendpacket_open_bpf(device, errbuf);
  319. #elif defined HAVE_LIBDNET
  320. sp = sendpacket_open_libdnet(device, errbuf);
  321. #elif (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET)
  322. sp = sendpacket_open_pcap(device, errbuf);
  323. #endif
  324. if (sp != NULL) {
  325. sp->open = 1;
  326. sp->cache_dir = direction;
  327. }
  328. return sp;
  329. }
  330. /**
  331. * Get packet stats for the given sendpacket_t
  332. */
  333. char *
  334. sendpacket_getstat(sendpacket_t *sp)
  335. {
  336. static char buf[1024];
  337. assert(sp);
  338. memset(buf, 0, sizeof(buf));
  339. sprintf(buf, "Statistics for network device: %s\n"
  340. "\tAttempted packets: " COUNTER_SPEC "\n"
  341. "\tSuccessful packets: " COUNTER_SPEC "\n"
  342. "\tFailed packets: " COUNTER_SPEC "\n"
  343. "\tRetried packets (ENOBUFS): " COUNTER_SPEC "\n"
  344. "\tRetried packets (EAGAIN): " COUNTER_SPEC "\n",
  345. sp->device, sp->attempt, sp->sent, sp->failed, sp->retry_enobufs, sp->retry_eagain);
  346. return(buf);
  347. }
  348. /**
  349. * close the given sendpacket
  350. */
  351. int
  352. sendpacket_close(sendpacket_t *sp)
  353. {
  354. assert(sp);
  355. switch(sp->handle_type) {
  356. case SP_TYPE_BPF:
  357. #if (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET)
  358. close(sp->handle.fd);
  359. #endif
  360. break;
  361. case SP_TYPE_PF_PACKET:
  362. #ifdef HAVE_PF_PACKET
  363. close(sp->handle.fd);
  364. #endif
  365. break;
  366. case SP_TYPE_LIBPCAP:
  367. #ifdef HAVE_LIBPCAP
  368. pcap_close(sp->handle.pcap);
  369. #endif
  370. break;
  371. case SP_TYPE_LIBDNET:
  372. #ifdef HAVE_LIBDNET
  373. eth_close(sp->handle.ldnet);
  374. #endif
  375. break;
  376. case SP_TYPE_LIBNET:
  377. err(-1, "Libnet is no longer supported!");
  378. break;
  379. }
  380. safe_free(sp);
  381. return 0;
  382. }
  383. /**
  384. * returns the Layer 2 address of the interface current
  385. * open. on error, return NULL
  386. */
  387. struct tcpr_ether_addr *
  388. sendpacket_get_hwaddr(sendpacket_t *sp)
  389. {
  390. struct tcpr_ether_addr *addr;
  391. assert(sp);
  392. /* if we already have our MAC address stored, just return it */
  393. if (memcmp(&sp->ether, "\x00\x00\x00\x00\x00\x00", ETHER_ADDR_LEN) != 0)
  394. return &sp->ether;
  395. #if defined HAVE_PF_PACKET
  396. addr = sendpacket_get_hwaddr_pf(sp);
  397. #elif defined HAVE_BPF
  398. addr = sendpacket_get_hwaddr_bpf(sp);
  399. #elif defined HAVE_LIBDNET
  400. addr = sendpacket_get_hwaddr_libdnet(sp);
  401. #elif (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET)
  402. addr = sendpacket_get_hwaddr_pcap(sp);
  403. #endif
  404. return addr;
  405. }
  406. /**
  407. * returns the error string
  408. */
  409. char *
  410. sendpacket_geterr(sendpacket_t *sp)
  411. {
  412. assert(sp);
  413. return sp->errbuf;
  414. }
  415. /**
  416. * Set's the error string
  417. */
  418. static void
  419. sendpacket_seterr(sendpacket_t *sp, const char *fmt, ...)
  420. {
  421. va_list ap;
  422. assert(sp);
  423. va_start(ap, fmt);
  424. if (fmt != NULL)
  425. (void)vsnprintf(sp->errbuf, SENDPACKET_ERRBUF_SIZE, fmt, ap);
  426. va_end(ap);
  427. sp->errbuf[(SENDPACKET_ERRBUF_SIZE-1)] = '\0'; // be safe
  428. }
  429. #if defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET
  430. /**
  431. * Inner sendpacket_open() method for using libpcap
  432. */
  433. static sendpacket_t *
  434. sendpacket_open_pcap(const char *device, char *errbuf)
  435. {
  436. pcap_t *pcap;
  437. sendpacket_t *sp;
  438. #ifdef BIOCSHDRCMPLT
  439. u_int spoof_eth_src = 1;
  440. int fd;
  441. #endif
  442. assert(device);
  443. assert(errbuf);
  444. dbg(1, "sendpacket: using Libpcap");
  445. /* open_pcap_live automatically fills out our errbuf for us */
  446. if ((pcap = pcap_open_live(device, 0, 0, 0, errbuf)) == NULL)
  447. return NULL;
  448. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  449. strlcpy(sp->device, device, sizeof(sp->device));
  450. sp->handle.pcap = pcap;
  451. #ifdef BIOCSHDRCMPLT
  452. /*
  453. * Only systems using BPF on the backend need this...
  454. * other systems don't have ioctl and will get compile errors.
  455. */
  456. fd = pcap_get_selectable_fd(pcap);
  457. if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1)
  458. errx(-1, "Unable to enable source MAC spoof support: %s", strerror(errno));
  459. #endif
  460. sp->handle_type = SP_TYPE_LIBPCAP;
  461. return sp;
  462. }
  463. /**
  464. * Get the hardware MAC address for the given interface using libpcap
  465. */
  466. static struct tcpr_ether_addr *
  467. sendpacket_get_hwaddr_pcap(sendpacket_t *sp)
  468. {
  469. assert(sp);
  470. sendpacket_seterr(sp, "Error: sendpacket_get_hwaddr() not yet supported for pcap injection");
  471. return NULL;
  472. }
  473. #endif /* HAVE_PCAP_INJECT || HAVE_PCAP_SENDPACKET */
  474. #if defined HAVE_LIBDNET
  475. /**
  476. * Inner sendpacket_open() method for using libdnet
  477. */
  478. static sendpacket_t *
  479. sendpacket_open_libdnet(const char *device, char *errbuf)
  480. {
  481. eth_t *ldnet;
  482. sendpacket_t *sp;
  483. assert(device);
  484. assert(errbuf);
  485. dbg(1, "sendpacket: using Libdnet");
  486. if ((ldnet = eth_open(device)) == NULL)
  487. return NULL;
  488. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  489. strlcpy(sp->device, device, sizeof(sp->device));
  490. sp->handle.ldnet = ldnet;
  491. sp->handle_type = SP_TYPE_LIBDNET;
  492. return sp;
  493. }
  494. /**
  495. * Get the hardware MAC address for the given interface using libdnet
  496. */
  497. static struct tcpr_ether_addr *
  498. sendpacket_get_hwaddr_libdnet(sendpacket_t *sp)
  499. {
  500. struct tcpr_ether_addr *addr;
  501. int ret;
  502. assert(sp);
  503. ret = eth_get(sp->handle.ldnet, (eth_addr_t *)addr);
  504. if (addr == NULL || ret < 0) {
  505. sendpacket_seterr(sp, "Error getting hwaddr via libdnet: %s", strerror(errno));
  506. return NULL;
  507. }
  508. memcpy(&sp->ether, addr, sizeof(struct tcpr_ether_addr));
  509. return(&sp->ether);
  510. }
  511. #endif /* HAVE_LIBDNET */
  512. #if defined HAVE_PF_PACKET
  513. /**
  514. * Inner sendpacket_open() method for using Linux's PF_PACKET
  515. */
  516. static sendpacket_t *
  517. sendpacket_open_pf(const char *device, char *errbuf)
  518. {
  519. int mysocket;
  520. sendpacket_t *sp;
  521. struct ifreq ifr;
  522. struct sockaddr_ll sa;
  523. int n = 1, err;
  524. socklen_t errlen = sizeof(err);
  525. assert(device);
  526. assert(errbuf);
  527. dbg(1, "sendpacket: using PF_PACKET");
  528. /* open our socket */
  529. if ((mysocket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
  530. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "socket: %s", strerror(errno));
  531. return NULL;
  532. }
  533. /* get the interface id for the device */
  534. if ((sa.sll_ifindex = get_iface_index(mysocket, device, errbuf)) < 0) {
  535. close(mysocket);
  536. return NULL;
  537. }
  538. /* bind socket to our interface id */
  539. sa.sll_family = AF_PACKET;
  540. sa.sll_protocol = htons(ETH_P_ALL);
  541. if (bind(mysocket, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
  542. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "bind error: %s", strerror(errno));
  543. close(mysocket);
  544. return NULL;
  545. }
  546. /* check for errors, network down, etc... */
  547. if (getsockopt(mysocket, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0) {
  548. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "error opening %s: %s", device,
  549. strerror(errno));
  550. close(mysocket);
  551. return NULL;
  552. }
  553. if (err > 0) {
  554. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "error opening %s: %s", device,
  555. strerror(err));
  556. close(mysocket);
  557. return NULL;
  558. }
  559. /* get hardware type for our interface */
  560. memset(&ifr, 0, sizeof(ifr));
  561. strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  562. if (ioctl(mysocket, SIOCGIFHWADDR, &ifr) < 0) {
  563. close(mysocket);
  564. sendpacket_seterr(sp, "Error getting hardware type: %s", strerror(errno));
  565. return NULL;
  566. }
  567. /* make sure it's not loopback (PF_PACKET doesn't support it) */
  568. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)
  569. warnx("Unsupported physical layer type 0x%04x on %s. Maybe it works, maybe it wont."
  570. " See tickets #123/318", ifr.ifr_hwaddr.sa_family, device);
  571. #ifdef SO_BROADCAST
  572. /*
  573. * man 7 socket
  574. *
  575. * Set or get the broadcast flag. When enabled, datagram sockets
  576. * receive packets sent to a broadcast address and they are allowed
  577. * to send packets to a broadcast address. This option has no
  578. * effect on stream-oriented sockets.
  579. */
  580. if (setsockopt(mysocket, SOL_SOCKET, SO_BROADCAST, &n, sizeof(n)) == -1) {
  581. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE,
  582. "SO_BROADCAST: %s\n", strerror(errno));
  583. close(mysocket);
  584. return NULL;
  585. }
  586. #endif /* SO_BROADCAST */
  587. /* prep & return our sp handle */
  588. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  589. strlcpy(sp->device, device, sizeof(sp->device));
  590. sp->handle.fd = mysocket;
  591. sp->handle_type = SP_TYPE_PF_PACKET;
  592. return sp;
  593. }
  594. /**
  595. * get the interface index (necessary for sending packets w/ PF_PACKET)
  596. */
  597. static int
  598. get_iface_index(int fd, const int8_t *device, char *errbuf) {
  599. struct ifreq ifr;
  600. memset(&ifr, 0, sizeof(ifr));
  601. strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  602. if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
  603. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "ioctl: %s", strerror(errno));
  604. return (-1);
  605. }
  606. return ifr.ifr_ifindex;
  607. }
  608. /**
  609. * get's the hardware address via Linux's PF packet interface
  610. */
  611. struct tcpr_ether_addr *
  612. sendpacket_get_hwaddr_pf(sendpacket_t *sp)
  613. {
  614. struct ifreq ifr;
  615. int fd;
  616. assert(sp);
  617. if (!sp->open) {
  618. sendpacket_seterr(sp, "Unable to get hardware address on un-opened sendpacket handle");
  619. return NULL;
  620. }
  621. /* create dummy socket for ioctl */
  622. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  623. sendpacket_seterr(sp, "Unable to open dummy socket for get_hwaddr: %s", strerror(errno));
  624. return NULL;
  625. }
  626. memset(&ifr, 0, sizeof(ifr));
  627. strlcpy(ifr.ifr_name, sp->device, sizeof(ifr.ifr_name));
  628. if (ioctl(fd, SIOCGIFHWADDR, (int8_t *)&ifr) < 0) {
  629. close(fd);
  630. sendpacket_seterr(sp, "Error getting hardware address: %s", strerror(errno));
  631. return NULL;
  632. }
  633. memcpy(&sp->ether, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
  634. close(fd);
  635. return(&sp->ether);
  636. }
  637. #endif /* HAVE_PF_PACKET */
  638. #if defined HAVE_BPF
  639. /**
  640. * Inner sendpacket_open() method for using BSD's BPF interface
  641. */
  642. static sendpacket_t *
  643. sendpacket_open_bpf(const char *device, char *errbuf)
  644. {
  645. sendpacket_t *sp;
  646. char bpf_dev[10];
  647. int dev, mysocket, link_offset, link_type;
  648. struct ifreq ifr;
  649. struct bpf_version bv;
  650. u_int v;
  651. #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
  652. u_int spoof_eth_src = 1;
  653. #endif
  654. assert(device);
  655. assert(errbuf);
  656. memset(&ifr, '\0', sizeof(struct ifreq));
  657. dbg(1, "sendpacket: using BPF");
  658. /* open socket */
  659. mysocket = -1;
  660. for (dev = 0; dev <= 9; dev ++) {
  661. memset(bpf_dev, '\0', sizeof(bpf_dev));
  662. snprintf(bpf_dev, sizeof(bpf_dev), "/dev/bpf%d", dev);
  663. if ((mysocket = open(bpf_dev, O_RDWR, 0)) > 0) {
  664. break;
  665. }
  666. }
  667. /* error?? */
  668. if (mysocket < 0) {
  669. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE,
  670. "Unable to open /dev/bpfX: %s", strerror(errno));
  671. errbuf[SENDPACKET_ERRBUF_SIZE -1] = '\0';
  672. return NULL;
  673. }
  674. /* get BPF version */
  675. if (ioctl(mysocket, BIOCVERSION, (caddr_t)&bv) < 0) {
  676. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to get bpf version: %s", strerror(errno));
  677. return NULL;
  678. }
  679. if (bv.bv_major != BPF_MAJOR_VERSION || bv.bv_minor != BPF_MINOR_VERSION) {
  680. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Kernel's bpf version is out of date.");
  681. return NULL;
  682. }
  683. /* attach to device */
  684. strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  685. if (ioctl(mysocket, BIOCSETIF, (caddr_t)&ifr) < 0) {
  686. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to bind %s to %s: %s",
  687. bpf_dev, device, strerror(errno));
  688. return NULL;
  689. }
  690. /* get datalink type */
  691. if (ioctl(mysocket, BIOCGDLT, (caddr_t)&v) < 0) {
  692. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to get datalink type: %s",
  693. strerror(errno));
  694. return NULL;
  695. }
  696. /*
  697. * NetBSD and FreeBSD BPF have an ioctl for enabling/disabling
  698. * automatic filling of the link level source address.
  699. */
  700. #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
  701. if (ioctl(mysocket, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
  702. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE,
  703. "Unable to enable spoofing src MAC: %s", strerror(errno));
  704. return NULL;
  705. }
  706. #endif
  707. /* assign link type and offset */
  708. switch (v) {
  709. case DLT_SLIP:
  710. link_offset = 0x10;
  711. break;
  712. case DLT_RAW:
  713. link_offset = 0x0;
  714. break;
  715. case DLT_PPP:
  716. link_offset = 0x04;
  717. break;
  718. case DLT_EN10MB:
  719. default: /* default to Ethernet */
  720. link_offset = 0xe;
  721. break;
  722. }
  723. #if _BSDI_VERSION - 0 > 199510
  724. switch (v) {
  725. case DLT_SLIP:
  726. v = DLT_SLIP_BSDOS;
  727. link_offset = 0x10;
  728. break;
  729. case DLT_PPP:
  730. v = DLT_PPP_BSDOS;
  731. link_offset = 0x04;
  732. break;
  733. }
  734. #endif
  735. link_type = v;
  736. /* allocate our sp handle, and return it */
  737. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  738. strlcpy(sp->device, device, sizeof(sp->device));
  739. sp->handle.fd = mysocket;
  740. //sp->link_type = link_type;
  741. //sp->link_offset = link_offset;
  742. sp->handle_type = SP_TYPE_BPF;
  743. return sp;
  744. }
  745. /**
  746. * Get the interface hardware MAC address when using BPF
  747. */
  748. struct tcpr_ether_addr *
  749. sendpacket_get_hwaddr_bpf(sendpacket_t *sp)
  750. {
  751. int mib[6];
  752. size_t len;
  753. int8_t *buf, *next, *end;
  754. struct if_msghdr *ifm;
  755. struct sockaddr_dl *sdl;
  756. assert(sp);
  757. mib[0] = CTL_NET;
  758. mib[1] = AF_ROUTE;
  759. mib[2] = 0;
  760. mib[3] = AF_LINK;
  761. mib[4] = NET_RT_IFLIST;
  762. mib[5] = 0;
  763. if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) {
  764. sendpacket_seterr(sp, "%s(): sysctl(): %s", __func__, strerror(errno));
  765. return NULL;
  766. }
  767. buf = (int8_t *)safe_malloc(len);
  768. if (sysctl(mib, 6, buf, &len, NULL, 0) == -1) {
  769. sendpacket_seterr(sp, "%s(): sysctl(): %s", __func__, strerror(errno));
  770. safe_free(buf);
  771. return NULL;
  772. }
  773. end = buf + len;
  774. for (next = buf; next < end; next += ifm->ifm_msglen) {
  775. ifm = (struct if_msghdr *)next;
  776. if (ifm->ifm_type == RTM_IFINFO) {
  777. sdl = (struct sockaddr_dl *)(ifm + 1);
  778. if (strncmp(&sdl->sdl_data[0], sp->device, sdl->sdl_len) == 0) {
  779. memcpy(&sp->ether, LLADDR(sdl), ETHER_ADDR_LEN);
  780. break;
  781. }
  782. }
  783. }
  784. safe_free(buf);
  785. return(&sp->ether);
  786. }
  787. #endif /* HAVE_BPF */
  788. /**
  789. * Get the DLT type of the opened sendpacket
  790. * Return -1 if we can't figure it out, else return the DLT_ value
  791. */
  792. int
  793. sendpacket_get_dlt(sendpacket_t *sp)
  794. {
  795. int dlt;
  796. #if defined HAVE_BPF
  797. int rcode;
  798. if ((rcode = ioctl(sp->handle.fd, BIOCGDLT, &dlt)) < 0) {
  799. warnx("Unable to get DLT value for BPF device (%s): %s", sp->device, strerror(errno));
  800. return(-1);
  801. }
  802. #elif defined HAVE_PF_PACKET || defined HAVE_LIBDNET
  803. /* use libpcap to get dlt */
  804. pcap_t *pcap;
  805. char errbuf[PCAP_ERRBUF_SIZE];
  806. if ((pcap = pcap_open_live(sp->device, 65535, 0, 0, errbuf)) == NULL) {
  807. warnx("Unable to get DLT value for %s: %s", sp->device, errbuf);
  808. return(-1);
  809. }
  810. dlt = pcap_datalink(pcap);
  811. pcap_close(pcap);
  812. #elif defined HAVE_PCAP_SENDPACKET || defined HAVE_PCAP_INJECT
  813. dlt = pcap_datalink(sp->handle.pcap);
  814. #endif
  815. return dlt;
  816. }
  817. const char *
  818. sendpacket_get_method()
  819. {
  820. return INJECT_METHOD;
  821. }