sendpacket.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /* $Id: sendpacket.c 1935 2007-11-01 16:46:28Z 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. #ifdef BIOCSHDRCMPLT
  344. u_int spoof_eth_src = 1;
  345. int fd;
  346. #endif
  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. #ifdef BIOCSHDRCMPLT
  357. /*
  358. * Only systems using BPF on the backend need this...
  359. * other systems don't have ioctl and will get compile errors.
  360. */
  361. fd = pcap_get_selectable_fd(pcap);
  362. if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1)
  363. errx(1, "Unable to enable source MAC spoof support: %s", strerror(errno));
  364. #endif
  365. return sp;
  366. }
  367. /**
  368. * Get the hardware MAC address for the given interface using libpcap
  369. */
  370. static struct tcpr_ether_addr *
  371. sendpacket_get_hwaddr_pcap(sendpacket_t *sp)
  372. {
  373. assert(sp);
  374. sendpacket_seterr(sp, "Error: sendpacket_get_hwaddr() not yet supported for pcap injection");
  375. return NULL;
  376. }
  377. #endif /* HAVE_PCAP_INJECT || HAVE_PCAP_SENDPACKET */
  378. #if defined HAVE_LIBNET
  379. /**
  380. * Inner sendpacket_open() method for using libnet
  381. */
  382. static sendpacket_t *
  383. sendpacket_open_libnet(const char *device, char *errbuf)
  384. {
  385. libnet_t *lnet;
  386. sendpacket_t *sp;
  387. assert(device);
  388. assert(errbuf);
  389. dbg(1, "sendpacket: using Libnet");
  390. if ((lnet = libnet_init(LIBNET_LINK_ADV, device, errbuf)) == NULL)
  391. return NULL;
  392. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  393. strlcpy(sp->device, device, sizeof(sp->device));
  394. sp->handle.lnet = lnet;
  395. return sp;
  396. }
  397. /**
  398. * Get the hardware MAC address for the given interface using libnet
  399. */
  400. static struct tcpr_ether_addr *
  401. sendpacket_get_hwaddr_libnet(sendpacket_t *sp)
  402. {
  403. struct tcpr_ether_addr *addr;
  404. assert(sp);
  405. addr = (struct tcpr_ether_addr *)libnet_get_hwaddr(sp->handle.lnet);
  406. if (addr == NULL) {
  407. sendpacket_seterr(sp, "Error getting hwaddr via libnet: %s", libnet_geterror(sp->handle.lnet));
  408. return NULL;
  409. }
  410. memcpy(&sp->ether, addr, sizeof(struct tcpr_ether_addr));
  411. return(&sp->ether);
  412. }
  413. #endif /* HAVE_LIBNET */
  414. #if defined HAVE_PF_PACKET
  415. /**
  416. * Inner sendpacket_open() method for using Linux's PF_PACKET
  417. */
  418. static sendpacket_t *
  419. sendpacket_open_pf(const char *device, char *errbuf)
  420. {
  421. int mysocket;
  422. sendpacket_t *sp;
  423. struct ifreq ifr;
  424. struct sockaddr_ll sa;
  425. int n = 1, err;
  426. socklen_t errlen = sizeof(err);
  427. assert(device);
  428. assert(errbuf);
  429. dbg(1, "sendpacket: using PF_PACKET");
  430. /* open our socket */
  431. if ((mysocket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
  432. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "socket: %s", strerror(errno));
  433. return NULL;
  434. }
  435. /* get the interface id for the device */
  436. if ((sa.sll_ifindex = get_iface_index(mysocket, device, errbuf)) < 0) {
  437. close(mysocket);
  438. return NULL;
  439. }
  440. /* bind socket to our interface id */
  441. sa.sll_family = AF_PACKET;
  442. sa.sll_protocol = htons(ETH_P_ALL);
  443. if (bind(mysocket, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
  444. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "bind error: %s", strerror(errno));
  445. close(mysocket);
  446. return NULL;
  447. }
  448. /* check for errors, network down, etc... */
  449. if (getsockopt(mysocket, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0) {
  450. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "error opening %s: %s", device,
  451. strerror(errno));
  452. close(mysocket);
  453. return NULL;
  454. }
  455. if (err > 0) {
  456. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "error opening %s: %s", device,
  457. strerror(err));
  458. close(mysocket);
  459. return NULL;
  460. }
  461. /* get hardware type for our interface */
  462. memset(&ifr, 0, sizeof(ifr));
  463. strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  464. if (ioctl(mysocket, SIOCGIFHWADDR, &ifr) < 0) {
  465. close(mysocket);
  466. sendpacket_seterr(sp, "Error getting hardware type: %s", strerror(errno));
  467. return NULL;
  468. }
  469. /* make sure it's ethernet */
  470. switch (ifr.ifr_hwaddr.sa_family) {
  471. case ARPHRD_ETHER:
  472. break;
  473. default:
  474. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE,
  475. "unsupported pysical layer type 0x%x", ifr.ifr_hwaddr.sa_family);
  476. close(mysocket);
  477. return NULL;
  478. }
  479. #ifdef SO_BROADCAST
  480. /*
  481. * man 7 socket
  482. *
  483. * Set or get the broadcast flag. When enabled, datagram sockets
  484. * receive packets sent to a broadcast address and they are allowed
  485. * to send packets to a broadcast address. This option has no
  486. * effect on stream-oriented sockets.
  487. */
  488. if (setsockopt(mysocket, SOL_SOCKET, SO_BROADCAST, &n, sizeof(n)) == -1) {
  489. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE,
  490. "SO_BROADCAS: %s\n", strerror(errno));
  491. close(mysocket);
  492. return NULL;
  493. }
  494. #endif /* SO_BROADCAST */
  495. /* prep & return our sp handle */
  496. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  497. strlcpy(sp->device, device, sizeof(sp->device));
  498. sp->handle.fd = mysocket;
  499. return sp;
  500. }
  501. /**
  502. * get the interface index (necessary for sending packets w/ PF_PACKET)
  503. */
  504. static int
  505. get_iface_index(int fd, const int8_t *device, char *errbuf) {
  506. struct ifreq ifr;
  507. memset(&ifr, 0, sizeof(ifr));
  508. strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  509. if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
  510. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "ioctl: %s", strerror(errno));
  511. return (-1);
  512. }
  513. return ifr.ifr_ifindex;
  514. }
  515. /**
  516. * get's the hardware address via Linux's PF packet interface
  517. */
  518. struct tcpr_ether_addr *
  519. sendpacket_get_hwaddr_pf(sendpacket_t *sp)
  520. {
  521. struct ifreq ifr;
  522. int fd;
  523. assert(sp);
  524. if (!sp->open) {
  525. sendpacket_seterr(sp, "Unable to get hardware address on un-opened sendpacket handle");
  526. return NULL;
  527. }
  528. /* create dummy socket for ioctl */
  529. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  530. sendpacket_seterr(sp, "Unable to open dummy socket for get_hwaddr: %s", strerror(errno));
  531. return NULL;
  532. }
  533. memset(&ifr, 0, sizeof(ifr));
  534. strlcpy(ifr.ifr_name, sp->device, sizeof(ifr.ifr_name));
  535. if (ioctl(fd, SIOCGIFHWADDR, (int8_t *)&ifr) < 0) {
  536. close(fd);
  537. sendpacket_seterr(sp, "Error getting hardware address: %s", strerror(errno));
  538. return NULL;
  539. }
  540. memcpy(&sp->ether, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
  541. close(fd);
  542. return(&sp->ether);
  543. }
  544. #endif /* HAVE_PF_PACKET */
  545. #if defined HAVE_BPF
  546. /**
  547. * Inner sendpacket_open() method for using BSD's BPF interface
  548. */
  549. static sendpacket_t *
  550. sendpacket_open_bpf(const char *device, char *errbuf)
  551. {
  552. sendpacket_t *sp;
  553. char bpf_dev[10];
  554. int dev, mysocket, link_offset, link_type;
  555. struct ifreq ifr;
  556. struct bpf_version bv;
  557. u_int v;
  558. #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
  559. u_int spoof_eth_src = 1;
  560. #endif
  561. assert(device);
  562. assert(errbuf);
  563. memset(&ifr, '\0', sizeof(struct ifreq));
  564. dbg(1, "sendpacket: using BPF");
  565. /* open socket */
  566. mysocket = -1;
  567. for (dev = 0; dev <= 9; dev ++) {
  568. memset(bpf_dev, '\0', sizeof(bpf_dev));
  569. snprintf(bpf_dev, sizeof(bpf_dev), "/dev/bpf%d", dev);
  570. if ((mysocket = open(bpf_dev, O_RDWR, 0)) > 0) {
  571. break;
  572. }
  573. }
  574. /* error?? */
  575. if (mysocket < 0) {
  576. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE,
  577. "Unable to open /dev/bpfX: %s", strerror(errno));
  578. errbuf[SENDPACKET_ERRBUF_SIZE -1] = '\0';
  579. return NULL;
  580. }
  581. /* get BPF version */
  582. if (ioctl(mysocket, BIOCVERSION, (caddr_t)&bv) < 0) {
  583. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to get bpf version: %s", strerror(errno));
  584. return NULL;
  585. }
  586. if (bv.bv_major != BPF_MAJOR_VERSION || bv.bv_minor != BPF_MINOR_VERSION) {
  587. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Kernel's bpf version is out of date.");
  588. return NULL;
  589. }
  590. /* attach to device */
  591. strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  592. if (ioctl(mysocket, BIOCSETIF, (caddr_t)&ifr) < 0) {
  593. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to bind %s to %s: %s",
  594. bpf_dev, device, strerror(errno));
  595. return NULL;
  596. }
  597. /* get datalink type */
  598. if (ioctl(mysocket, BIOCGDLT, (caddr_t)&v) < 0) {
  599. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to get datalink type: %s",
  600. strerror(errno));
  601. return NULL;
  602. }
  603. /*
  604. * NetBSD and FreeBSD BPF have an ioctl for enabling/disabling
  605. * automatic filling of the link level source address.
  606. */
  607. #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
  608. if (ioctl(mysocket, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
  609. snprintf(errbuf, SENDPACKET_ERRBUF_SIZE,
  610. "Unable to enable spoofing src MAC: %s", strerror(errno));
  611. return NULL;
  612. }
  613. #endif
  614. /* assign link type and offset */
  615. switch (v) {
  616. case DLT_SLIP:
  617. link_offset = 0x10;
  618. break;
  619. case DLT_RAW:
  620. link_offset = 0x0;
  621. break;
  622. case DLT_PPP:
  623. link_offset = 0x04;
  624. break;
  625. case DLT_EN10MB:
  626. default: /* default to Ethernet */
  627. link_offset = 0xe;
  628. break;
  629. }
  630. #if _BSDI_VERSION - 0 > 199510
  631. switch (v) {
  632. case DLT_SLIP:
  633. v = DLT_SLIP_BSDOS;
  634. link_offset = 0x10;
  635. break;
  636. case DLT_PPP:
  637. v = DLT_PPP_BSDOS;
  638. link_offset = 0x04;
  639. break;
  640. }
  641. #endif
  642. link_type = v;
  643. /* allocate our sp handle, and return it */
  644. sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
  645. strlcpy(sp->device, device, sizeof(sp->device));
  646. sp->handle.fd = mysocket;
  647. //sp->link_type = link_type;
  648. //sp->link_offset = link_offset;
  649. return sp;
  650. }
  651. /**
  652. * Get the interface hardware MAC address when using BPF
  653. */
  654. struct tcpr_ether_addr *
  655. sendpacket_get_hwaddr_bpf(sendpacket_t *sp)
  656. {
  657. int mib[6];
  658. size_t len;
  659. int8_t *buf, *next, *end;
  660. struct if_msghdr *ifm;
  661. struct sockaddr_dl *sdl;
  662. assert(sp);
  663. mib[0] = CTL_NET;
  664. mib[1] = AF_ROUTE;
  665. mib[2] = 0;
  666. mib[3] = AF_LINK;
  667. mib[4] = NET_RT_IFLIST;
  668. mib[5] = 0;
  669. if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) {
  670. sendpacket_seterr(sp, "%s(): sysctl(): %s", __func__, strerror(errno));
  671. return NULL;
  672. }
  673. buf = (int8_t *)safe_malloc(len);
  674. if (sysctl(mib, 6, buf, &len, NULL, 0) == -1) {
  675. sendpacket_seterr(sp, "%s(): sysctl(): %s", __func__, strerror(errno));
  676. safe_free(buf);
  677. return NULL;
  678. }
  679. end = buf + len;
  680. for (next = buf; next < end; next += ifm->ifm_msglen) {
  681. ifm = (struct if_msghdr *)next;
  682. if (ifm->ifm_type == RTM_IFINFO) {
  683. sdl = (struct sockaddr_dl *)(ifm + 1);
  684. if (strncmp(&sdl->sdl_data[0], sp->device, sdl->sdl_len) == 0) {
  685. memcpy(&sp->ether, LLADDR(sdl), ETHER_ADDR_LEN);
  686. break;
  687. }
  688. }
  689. }
  690. safe_free(buf);
  691. return(&sp->ether);
  692. }
  693. #endif /* HAVE_BPF */
  694. /**
  695. * Get the DLT type of the opened sendpacket
  696. * Return -1 if we can't figure it out, else return the DLT_ value
  697. */
  698. int
  699. sendpacket_get_dlt(sendpacket_t *sp)
  700. {
  701. int dlt;
  702. #if defined HAVE_BPF
  703. int rcode;
  704. if ((rcode = ioctl(sp->handle.fd, BIOCGDLT, &dlt)) < 0) {
  705. warnx("Unable to get DLT value for BPF device (%s): %s", sp->device, strerror(errno));
  706. return(-1);
  707. }
  708. #elif defined HAVE_PF_PACKET || defined HAVE_LIBNET
  709. /* use libpcap to get dlt */
  710. pcap_t *pcap;
  711. char errbuf[PCAP_ERRBUF_SIZE];
  712. if ((pcap = pcap_open_live(sp->device, 65535, 0, 0, errbuf)) == NULL) {
  713. warnx("Unable to get DLT value for %s: %s", sp->device, errbuf);
  714. return(-1);
  715. }
  716. dlt = pcap_datalink(pcap);
  717. pcap_close(pcap);
  718. #elif defined HAVE_PCAP_SENDPACKET || defined HAVE_PCAP_INJECT
  719. dlt = pcap_datalink(sp->handle.pcap);
  720. #endif
  721. return dlt;
  722. }