sendpacket.c 24 KB

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