sendpacket.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /* $Id: sendpacket.c 2059 2008-06-09 19:37:53Z aturner $ */
  2. /*
  3. * Copyright (c) 2006 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, libnet, 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. libnet
  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_LIBNET
  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_LIBNET
  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_LIBNET
  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_LIBNET
  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_LIBNET && !defined HAVE_PF_PACKET && !defined HAVE_BPF
  96. #error You need pcap_inject() or pcap_sendpacket() from libpcap, libnet 1.1.3+, 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. #ifdef HAVE_BPF
  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. #ifdef HAVE_LIBNET
  147. #undef INJECT_METHOD
  148. #define INJECT_METHOD "libnet send()"
  149. static sendpacket_t *sendpacket_open_libnet(const char *, char *) _U_;
  150. static struct tcpr_ether_addr *sendpacket_get_hwaddr_libnet(sendpacket_t *) _U_;
  151. #endif /* HAVE_LIBNET */
  152. #if (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET)
  153. static sendpacket_t *sendpacket_open_pcap(const char *, char *) _U_;
  154. static struct tcpr_ether_addr *sendpacket_get_hwaddr_pcap(sendpacket_t *) _U_;
  155. #endif /* HAVE_PCAP_INJECT || HAVE_PACKET_SENDPACKET */
  156. #ifdef HAVE_PCAP_INJECT
  157. #undef INJECT_METHOD
  158. #define INJECT_METHOD "pcap_inject()"
  159. #elif defined HAVE_PCAP_SENDPACKET
  160. #undef INJECT_METHOD
  161. #define INJECT_METHOD "pcap_sendpacket()"
  162. #endif
  163. static void sendpacket_seterr(sendpacket_t *sp, const char *fmt, ...);
  164. /* You need to define didsig in your main .c file. Set to 1 if CTRL-C was pressed */
  165. extern volatile int didsig;
  166. /**
  167. * returns number of bytes sent on success or -1 on error
  168. * Note: it is theoretically possible to get a return code >0 and < len
  169. * which for most people would be considered an error (the packet wasn't fully sent)
  170. * so you may want to test for recode != len too.
  171. *
  172. * Most socket API's have two interesting errors: ENOBUFS & EAGAIN. ENOBUFS
  173. * is usually due to the kernel buffers being full. EAGAIN happens when you
  174. * try to send traffic faster then the PHY allows.
  175. */
  176. int
  177. sendpacket(sendpacket_t *sp, const u_char *data, size_t len)
  178. {
  179. int retcode;
  180. assert(sp);
  181. assert(data);
  182. if (len <= 0)
  183. return -1;
  184. TRY_SEND_AGAIN:
  185. sp->attempt ++;
  186. #if defined HAVE_PF_PACKET
  187. retcode = (int)send(sp->handle.fd, (void *)data, len, 0);
  188. /* out of buffers, or hit max PHY speed, silently retry */
  189. if (retcode < 0 && !didsig) {
  190. switch (errno) {
  191. case EAGAIN:
  192. sp->retry_eagain ++;
  193. goto TRY_SEND_AGAIN;
  194. break;
  195. case ENOBUFS:
  196. sp->retry_enobufs ++;
  197. goto TRY_SEND_AGAIN;
  198. break;
  199. default:
  200. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  201. INJECT_METHOD, sp->sent + 1, strerror(errno), errno);
  202. }
  203. }
  204. #elif defined HAVE_BPF
  205. retcode = write(sp->handle.fd, (void *)data, len);
  206. /* out of buffers, or hit max PHY speed, silently retry */
  207. if (retcode < 0 && !didsig) {
  208. switch (errno) {
  209. case EAGAIN:
  210. sp->retry_eagain ++;
  211. goto TRY_SEND_AGAIN;
  212. break;
  213. case ENOBUFS:
  214. sp->retry_enobufs ++;
  215. goto TRY_SEND_AGAIN;
  216. break;
  217. default:
  218. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  219. INJECT_METHOD, sp->sent + 1, strerror(errno), errno);
  220. }
  221. }
  222. #elif defined HAVE_LIBNET
  223. retcode = libnet_adv_write_link(sp->handle.lnet, (u_int8_t*)data, (u_int32_t)len);
  224. /* out of buffers, or hit max PHY speed, silently retry */
  225. if (retcode < 0 && !didsig) {
  226. switch (errno) {
  227. case EAGAIN:
  228. sp->retry_eagain ++;
  229. goto TRY_SEND_AGAIN;
  230. break;
  231. case ENOBUFS:
  232. sp->retry_enobufs ++;
  233. goto TRY_SEND_AGAIN;
  234. break;
  235. default:
  236. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  237. INJECT_METHOD, sp->sent + 1, strerror(errno), errno);
  238. }
  239. }
  240. #elif defined HAVE_PCAP_INJECT
  241. /*
  242. * pcap methods don't seem to support ENOBUFS, so we just straight fail
  243. * is there a better way???
  244. */
  245. retcode = pcap_inject(sp->handle.pcap, (void*)data, len);
  246. /* out of buffers, or hit max PHY speed, silently retry */
  247. if (retcode < 0 && !didsig) {
  248. switch (errno) {
  249. case EAGAIN:
  250. sp->retry_eagain ++;
  251. goto TRY_SEND_AGAIN;
  252. break;
  253. case ENOBUFS:
  254. sp->retry_enobufs ++;
  255. goto TRY_SEND_AGAIN;
  256. break;
  257. default:
  258. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  259. INJECT_METHOD, sp->sent + 1, pcap_geterr(sp->handle.pcap), errno);
  260. }
  261. }
  262. #elif defined HAVE_PCAP_SENDPACKET
  263. retcode = pcap_sendpacket(sp->handle.pcap, data, (int)len);
  264. /* out of buffers, or hit max PHY speed, silently retry */
  265. if (retcode < 0 && !didsig) {
  266. switch (errno) {
  267. case EAGAIN:
  268. sp->retry_eagain ++;
  269. goto TRY_SEND_AGAIN;
  270. break;
  271. case ENOBUFS:
  272. sp->retry_enobufs ++;
  273. goto TRY_SEND_AGAIN;
  274. break;
  275. default:
  276. sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
  277. INJECT_METHOD, sp->sent + 1, pcap_geterr(sp->handle.pcap), errno);
  278. }
  279. }
  280. /*
  281. * pcap_sendpacket returns 0 on success, not the packet length!
  282. * hence, we have to fix retcode to be more standard on success
  283. */
  284. if (retcode == 0)
  285. retcode = len;
  286. #endif
  287. if (retcode < 0) {
  288. sp->failed ++;
  289. } else if (retcode != (int)len) {
  290. sendpacket_seterr(sp, "Only able to write %d bytes out of %u bytes total",
  291. retcode, len);
  292. } else {
  293. sp->bytes_sent += len;
  294. sp->sent ++;
  295. }
  296. return retcode;
  297. }
  298. /**
  299. * Open the given network device name and returns a sendpacket_t struct
  300. * pass the error buffer (in case there's a problem) and the direction
  301. * that this interface represents
  302. */
  303. sendpacket_t *
  304. sendpacket_open(const char *device, char *errbuf, tcpr_dir_t direction)
  305. {
  306. sendpacket_t *sp;
  307. assert(device);
  308. assert(errbuf);
  309. #if defined HAVE_PF_PACKET
  310. sp = sendpacket_open_pf(device, errbuf);
  311. #elif defined HAVE_BPF
  312. sp = sendpacket_open_bpf(device, errbuf);
  313. #elif defined HAVE_LIBNET
  314. sp = sendpacket_open_libnet(device, errbuf);
  315. #elif (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET)
  316. sp = sendpacket_open_pcap(device, errbuf);
  317. #endif
  318. if (sp != NULL) {
  319. sp->open = 1;
  320. sp->cache_dir = direction;
  321. }
  322. return sp;
  323. }
  324. /**
  325. * Get packet stats for the given sendpacket_t
  326. */
  327. char *
  328. sendpacket_getstat(sendpacket_t *sp)
  329. {
  330. static char buf[1024];
  331. assert(sp);
  332. memset(buf, 0, sizeof(buf));
  333. sprintf(buf, "Statistics for network device: %s\n"
  334. "\tAttempted packets: " COUNTER_SPEC "\n"
  335. "\tSuccessful packets: " COUNTER_SPEC "\n"
  336. "\tFailed packets: " COUNTER_SPEC "\n"
  337. "\tRetried packets (ENOBUFS): " COUNTER_SPEC "\n"
  338. "\tRetried packets (EAGAIN): " COUNTER_SPEC "\n",
  339. sp->device, sp->attempt, sp->sent, sp->failed, sp->retry_enobufs, sp->retry_eagain);
  340. return(buf);
  341. }
  342. /**
  343. * close the given sendpacket
  344. */
  345. int
  346. sendpacket_close(sendpacket_t *sp)
  347. {
  348. assert(sp);
  349. safe_free(sp);
  350. return 0;
  351. }
  352. /**
  353. * returns the Layer 2 address of the interface current
  354. * open. on error, return NULL
  355. */
  356. struct tcpr_ether_addr *
  357. sendpacket_get_hwaddr(sendpacket_t *sp)
  358. {
  359. struct tcpr_ether_addr *addr;
  360. assert(sp);
  361. /* if we already have our MAC address stored, just return it */
  362. if (memcmp(&sp->ether, "\x00\x00\x00\x00\x00\x00", ETHER_ADDR_LEN) != 0)
  363. return &sp->ether;
  364. #if defined HAVE_PF_PACKET
  365. addr = sendpacket_get_hwaddr_pf(sp);
  366. #elif defined HAVE_BPF
  367. addr = sendpacket_get_hwaddr_bpf(sp);
  368. #elif defined HAVE_LIBNET
  369. addr = sendpacket_get_hwaddr_libnet(sp);
  370. #elif (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET)
  371. addr = sendpacket_get_hwaddr_pcap(sp);
  372. #endif
  373. return addr;
  374. }
  375. /**
  376. * returns the error string
  377. */
  378. char *
  379. sendpacket_geterr(sendpacket_t *sp)
  380. {
  381. assert(sp);
  382. return sp->errbuf;
  383. }
  384. /**
  385. * Set's the error string
  386. */
  387. static void
  388. sendpacket_seterr(sendpacket_t *sp, const char *fmt, ...)
  389. {
  390. va_list ap;
  391. assert(sp);
  392. va_start(ap, fmt);
  393. if (fmt != NULL)
  394. (void)vsnprintf(sp->errbuf, SENDPACKET_ERRBUF_SIZE, fmt, ap);
  395. va_end(ap);
  396. sp->errbuf[(SENDPACKET_ERRBUF_SIZE-1)] = '\0'; // be safe
  397. }
  398. #if defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET
  399. /**
  400. * Inner sendpacket_open() method for using libpcap
  401. */
  402. static sendpacket_t *
  403. sendpacket_open_pcap(const char *device, char *errbuf)
  404. {
  405. pcap_t *pcap;
  406. sendpacket_t *sp;
  407. #ifdef BIOCSHDRCMPLT
  408. u_int spoof_eth_src = 1;
  409. int fd;
  410. #endif
  411. assert(device);
  412. assert(errbuf);
  413. dbg(1, "sendpacket: using Libpcap");
  414. /* open_pcap_live automatically fills out our errbuf for us */
  415. if ((pcap = pcap_open_live(device, 0, 0, 0, errbuf)) == NULL)
  416. return NULL;
  417. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  418. strlcpy(sp->device, device, sizeof(sp->device));
  419. sp->handle.pcap = pcap;
  420. #ifdef BIOCSHDRCMPLT
  421. /*
  422. * Only systems using BPF on the backend need this...
  423. * other systems don't have ioctl and will get compile errors.
  424. */
  425. fd = pcap_get_selectable_fd(pcap);
  426. if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1)
  427. errx(1, "Unable to enable source MAC spoof support: %s", strerror(errno));
  428. #endif
  429. return sp;
  430. }
  431. /**
  432. * Get the hardware MAC address for the given interface using libpcap
  433. */
  434. static struct tcpr_ether_addr *
  435. sendpacket_get_hwaddr_pcap(sendpacket_t *sp)
  436. {
  437. assert(sp);
  438. sendpacket_seterr(sp, "Error: sendpacket_get_hwaddr() not yet supported for pcap injection");
  439. return NULL;
  440. }
  441. #endif /* HAVE_PCAP_INJECT || HAVE_PCAP_SENDPACKET */
  442. #if defined HAVE_LIBNET
  443. /**
  444. * Inner sendpacket_open() method for using libnet
  445. */
  446. static sendpacket_t *
  447. sendpacket_open_libnet(const char *device, char *errbuf)
  448. {
  449. libnet_t *lnet;
  450. sendpacket_t *sp;
  451. assert(device);
  452. assert(errbuf);
  453. dbg(1, "sendpacket: using Libnet");
  454. if ((lnet = libnet_init(LIBNET_LINK_ADV, device, errbuf)) == NULL)
  455. return NULL;
  456. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  457. strlcpy(sp->device, device, sizeof(sp->device));
  458. sp->handle.lnet = lnet;
  459. return sp;
  460. }
  461. /**
  462. * Get the hardware MAC address for the given interface using libnet
  463. */
  464. static struct tcpr_ether_addr *
  465. sendpacket_get_hwaddr_libnet(sendpacket_t *sp)
  466. {
  467. struct tcpr_ether_addr *addr;
  468. assert(sp);
  469. addr = (struct tcpr_ether_addr *)libnet_get_hwaddr(sp->handle.lnet);
  470. if (addr == NULL) {
  471. sendpacket_seterr(sp, "Error getting hwaddr via libnet: %s", libnet_geterror(sp->handle.lnet));
  472. return NULL;
  473. }
  474. memcpy(&sp->ether, addr, sizeof(struct tcpr_ether_addr));
  475. return(&sp->ether);
  476. }
  477. #endif /* HAVE_LIBNET */
  478. #if defined HAVE_PF_PACKET
  479. /**
  480. * Inner sendpacket_open() method for using Linux's PF_PACKET
  481. */
  482. static sendpacket_t *
  483. sendpacket_open_pf(const char *device, char *errbuf)
  484. {
  485. int mysocket;
  486. sendpacket_t *sp;
  487. struct ifreq ifr;
  488. struct sockaddr_ll sa;
  489. int n = 1, err;
  490. socklen_t errlen = sizeof(err);
  491. assert(device);
  492. assert(errbuf);
  493. dbg(1, "sendpacket: using PF_PACKET");
  494. /* open our socket */
  495. if ((mysocket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
  496. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "socket: %s", strerror(errno));
  497. return NULL;
  498. }
  499. /* get the interface id for the device */
  500. if ((sa.sll_ifindex = get_iface_index(mysocket, device, errbuf)) < 0) {
  501. close(mysocket);
  502. return NULL;
  503. }
  504. /* bind socket to our interface id */
  505. sa.sll_family = AF_PACKET;
  506. sa.sll_protocol = htons(ETH_P_ALL);
  507. if (bind(mysocket, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
  508. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "bind error: %s", strerror(errno));
  509. close(mysocket);
  510. return NULL;
  511. }
  512. /* check for errors, network down, etc... */
  513. if (getsockopt(mysocket, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0) {
  514. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "error opening %s: %s", device,
  515. strerror(errno));
  516. close(mysocket);
  517. return NULL;
  518. }
  519. if (err > 0) {
  520. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "error opening %s: %s", device,
  521. strerror(err));
  522. close(mysocket);
  523. return NULL;
  524. }
  525. /* get hardware type for our interface */
  526. memset(&ifr, 0, sizeof(ifr));
  527. strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  528. if (ioctl(mysocket, SIOCGIFHWADDR, &ifr) < 0) {
  529. close(mysocket);
  530. sendpacket_seterr(sp, "Error getting hardware type: %s", strerror(errno));
  531. return NULL;
  532. }
  533. /* make sure it's not loopback (PF_PACKET doesn't support it) */
  534. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)
  535. warnx("Unsupported physical layer type 0x%04x on %s. Maybe it works, maybe it wont."
  536. " See tickets #123/318", ifr.ifr_hwaddr.sa_family, device);
  537. #ifdef SO_BROADCAST
  538. /*
  539. * man 7 socket
  540. *
  541. * Set or get the broadcast flag. When enabled, datagram sockets
  542. * receive packets sent to a broadcast address and they are allowed
  543. * to send packets to a broadcast address. This option has no
  544. * effect on stream-oriented sockets.
  545. */
  546. if (setsockopt(mysocket, SOL_SOCKET, SO_BROADCAST, &n, sizeof(n)) == -1) {
  547. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE,
  548. "SO_BROADCAST: %s\n", strerror(errno));
  549. close(mysocket);
  550. return NULL;
  551. }
  552. #endif /* SO_BROADCAST */
  553. /* prep & return our sp handle */
  554. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  555. strlcpy(sp->device, device, sizeof(sp->device));
  556. sp->handle.fd = mysocket;
  557. return sp;
  558. }
  559. /**
  560. * get the interface index (necessary for sending packets w/ PF_PACKET)
  561. */
  562. static int
  563. get_iface_index(int fd, const int8_t *device, char *errbuf) {
  564. struct ifreq ifr;
  565. memset(&ifr, 0, sizeof(ifr));
  566. strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  567. if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
  568. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "ioctl: %s", strerror(errno));
  569. return (-1);
  570. }
  571. return ifr.ifr_ifindex;
  572. }
  573. /**
  574. * get's the hardware address via Linux's PF packet interface
  575. */
  576. struct tcpr_ether_addr *
  577. sendpacket_get_hwaddr_pf(sendpacket_t *sp)
  578. {
  579. struct ifreq ifr;
  580. int fd;
  581. assert(sp);
  582. if (!sp->open) {
  583. sendpacket_seterr(sp, "Unable to get hardware address on un-opened sendpacket handle");
  584. return NULL;
  585. }
  586. /* create dummy socket for ioctl */
  587. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  588. sendpacket_seterr(sp, "Unable to open dummy socket for get_hwaddr: %s", strerror(errno));
  589. return NULL;
  590. }
  591. memset(&ifr, 0, sizeof(ifr));
  592. strlcpy(ifr.ifr_name, sp->device, sizeof(ifr.ifr_name));
  593. if (ioctl(fd, SIOCGIFHWADDR, (int8_t *)&ifr) < 0) {
  594. close(fd);
  595. sendpacket_seterr(sp, "Error getting hardware address: %s", strerror(errno));
  596. return NULL;
  597. }
  598. memcpy(&sp->ether, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
  599. close(fd);
  600. return(&sp->ether);
  601. }
  602. #endif /* HAVE_PF_PACKET */
  603. #if defined HAVE_BPF
  604. /**
  605. * Inner sendpacket_open() method for using BSD's BPF interface
  606. */
  607. static sendpacket_t *
  608. sendpacket_open_bpf(const char *device, char *errbuf)
  609. {
  610. sendpacket_t *sp;
  611. char bpf_dev[10];
  612. int dev, mysocket, link_offset, link_type;
  613. struct ifreq ifr;
  614. struct bpf_version bv;
  615. u_int v;
  616. #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
  617. u_int spoof_eth_src = 1;
  618. #endif
  619. assert(device);
  620. assert(errbuf);
  621. memset(&ifr, '\0', sizeof(struct ifreq));
  622. dbg(1, "sendpacket: using BPF");
  623. /* open socket */
  624. mysocket = -1;
  625. for (dev = 0; dev <= 9; dev ++) {
  626. memset(bpf_dev, '\0', sizeof(bpf_dev));
  627. snprintf(bpf_dev, sizeof(bpf_dev), "/dev/bpf%d", dev);
  628. if ((mysocket = open(bpf_dev, O_RDWR, 0)) > 0) {
  629. break;
  630. }
  631. }
  632. /* error?? */
  633. if (mysocket < 0) {
  634. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE,
  635. "Unable to open /dev/bpfX: %s", strerror(errno));
  636. errbuf[SENDPACKET_ERRBUF_SIZE -1] = '\0';
  637. return NULL;
  638. }
  639. /* get BPF version */
  640. if (ioctl(mysocket, BIOCVERSION, (caddr_t)&bv) < 0) {
  641. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to get bpf version: %s", strerror(errno));
  642. return NULL;
  643. }
  644. if (bv.bv_major != BPF_MAJOR_VERSION || bv.bv_minor != BPF_MINOR_VERSION) {
  645. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Kernel's bpf version is out of date.");
  646. return NULL;
  647. }
  648. /* attach to device */
  649. strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  650. if (ioctl(mysocket, BIOCSETIF, (caddr_t)&ifr) < 0) {
  651. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to bind %s to %s: %s",
  652. bpf_dev, device, strerror(errno));
  653. return NULL;
  654. }
  655. /* get datalink type */
  656. if (ioctl(mysocket, BIOCGDLT, (caddr_t)&v) < 0) {
  657. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to get datalink type: %s",
  658. strerror(errno));
  659. return NULL;
  660. }
  661. /*
  662. * NetBSD and FreeBSD BPF have an ioctl for enabling/disabling
  663. * automatic filling of the link level source address.
  664. */
  665. #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
  666. if (ioctl(mysocket, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
  667. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE,
  668. "Unable to enable spoofing src MAC: %s", strerror(errno));
  669. return NULL;
  670. }
  671. #endif
  672. /* assign link type and offset */
  673. switch (v) {
  674. case DLT_SLIP:
  675. link_offset = 0x10;
  676. break;
  677. case DLT_RAW:
  678. link_offset = 0x0;
  679. break;
  680. case DLT_PPP:
  681. link_offset = 0x04;
  682. break;
  683. case DLT_EN10MB:
  684. default: /* default to Ethernet */
  685. link_offset = 0xe;
  686. break;
  687. }
  688. #if _BSDI_VERSION - 0 > 199510
  689. switch (v) {
  690. case DLT_SLIP:
  691. v = DLT_SLIP_BSDOS;
  692. link_offset = 0x10;
  693. break;
  694. case DLT_PPP:
  695. v = DLT_PPP_BSDOS;
  696. link_offset = 0x04;
  697. break;
  698. }
  699. #endif
  700. link_type = v;
  701. /* allocate our sp handle, and return it */
  702. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  703. strlcpy(sp->device, device, sizeof(sp->device));
  704. sp->handle.fd = mysocket;
  705. //sp->link_type = link_type;
  706. //sp->link_offset = link_offset;
  707. return sp;
  708. }
  709. /**
  710. * Get the interface hardware MAC address when using BPF
  711. */
  712. struct tcpr_ether_addr *
  713. sendpacket_get_hwaddr_bpf(sendpacket_t *sp)
  714. {
  715. int mib[6];
  716. size_t len;
  717. int8_t *buf, *next, *end;
  718. struct if_msghdr *ifm;
  719. struct sockaddr_dl *sdl;
  720. assert(sp);
  721. mib[0] = CTL_NET;
  722. mib[1] = AF_ROUTE;
  723. mib[2] = 0;
  724. mib[3] = AF_LINK;
  725. mib[4] = NET_RT_IFLIST;
  726. mib[5] = 0;
  727. if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) {
  728. sendpacket_seterr(sp, "%s(): sysctl(): %s", __func__, strerror(errno));
  729. return NULL;
  730. }
  731. buf = (int8_t *)safe_malloc(len);
  732. if (sysctl(mib, 6, buf, &len, NULL, 0) == -1) {
  733. sendpacket_seterr(sp, "%s(): sysctl(): %s", __func__, strerror(errno));
  734. safe_free(buf);
  735. return NULL;
  736. }
  737. end = buf + len;
  738. for (next = buf; next < end; next += ifm->ifm_msglen) {
  739. ifm = (struct if_msghdr *)next;
  740. if (ifm->ifm_type == RTM_IFINFO) {
  741. sdl = (struct sockaddr_dl *)(ifm + 1);
  742. if (strncmp(&sdl->sdl_data[0], sp->device, sdl->sdl_len) == 0) {
  743. memcpy(&sp->ether, LLADDR(sdl), ETHER_ADDR_LEN);
  744. break;
  745. }
  746. }
  747. }
  748. safe_free(buf);
  749. return(&sp->ether);
  750. }
  751. #endif /* HAVE_BPF */
  752. /**
  753. * Get the DLT type of the opened sendpacket
  754. * Return -1 if we can't figure it out, else return the DLT_ value
  755. */
  756. int
  757. sendpacket_get_dlt(sendpacket_t *sp)
  758. {
  759. int dlt;
  760. #if defined HAVE_BPF
  761. int rcode;
  762. if ((rcode = ioctl(sp->handle.fd, BIOCGDLT, &dlt)) < 0) {
  763. warnx("Unable to get DLT value for BPF device (%s): %s", sp->device, strerror(errno));
  764. return(-1);
  765. }
  766. #elif defined HAVE_PF_PACKET || defined HAVE_LIBNET
  767. /* use libpcap to get dlt */
  768. pcap_t *pcap;
  769. char errbuf[PCAP_ERRBUF_SIZE];
  770. if ((pcap = pcap_open_live(sp->device, 65535, 0, 0, errbuf)) == NULL) {
  771. warnx("Unable to get DLT value for %s: %s", sp->device, errbuf);
  772. return(-1);
  773. }
  774. dlt = pcap_datalink(pcap);
  775. pcap_close(pcap);
  776. #elif defined HAVE_PCAP_SENDPACKET || defined HAVE_PCAP_INJECT
  777. dlt = pcap_datalink(sp->handle.pcap);
  778. #endif
  779. return dlt;
  780. }