openchannel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * signed openchannel (struct channel * channel);
  44. *
  45. * channel.h
  46. *
  47. * open a raw ethernet channel;
  48. *
  49. *
  50. * Contributor(s):
  51. * Charles Maier
  52. * Nathaniel Houghton
  53. *
  54. *--------------------------------------------------------------------*/
  55. #ifndef OPENCHANNEL_SOURCE
  56. #define OPENCHANNEL_SOURCE
  57. #include <unistd.h>
  58. #include <memory.h>
  59. #include <errno.h>
  60. #if defined (__linux__)
  61. # include <net/if_arp.h>
  62. # include <netpacket/packet.h>
  63. # include <sys/ioctl.h>
  64. #elif defined (__APPLE__)
  65. # include <sys/ioctl.h>
  66. # include <sys/stat.h>
  67. # include <fcntl.h>
  68. # include <stdlib.h>
  69. #elif defined (__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__)
  70. # include <sys/ioctl.h>
  71. # include <sys/stat.h>
  72. # include <sys/types.h>
  73. # include <fcntl.h>
  74. # include <stdlib.h>
  75. #elif defined (WINPCAP)
  76. # include <string.h>
  77. #else
  78. #error "Unknown environment"
  79. #endif
  80. #include "../ether/channel.h"
  81. #include "../tools/memory.h"
  82. #include "../tools/flags.h"
  83. #include "../tools/error.h"
  84. #if defined (__APPLE__) || defined (__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__)
  85. # include "../ether/gethwaddr.c"
  86. #endif
  87. signed openchannel (struct channel * channel)
  88. {
  89. #if defined (__linux__)
  90. struct ifreq ifreq;
  91. struct sockaddr_ll sockaddr_ll =
  92. {
  93. PF_PACKET,
  94. 0x0000,
  95. 0x0000,
  96. ARPHRD_ETHER,
  97. PACKET_HOST,
  98. ETHER_ADDR_LEN,
  99. {
  100. 0x00,
  101. 0x00,
  102. 0x00,
  103. 0x00,
  104. 0x00,
  105. 0x00,
  106. 0x00,
  107. 0x00
  108. }
  109. };
  110. /*
  111. * raw packets require root privileges on linux; one does not have to be
  112. * root when this program is installed setuid using 'chown root:root' and
  113. * 'chmod 4555';
  114. */
  115. if (geteuid ())
  116. {
  117. error (1, EPERM, ERROR_NOTROOT);
  118. }
  119. memset (&ifreq, 0, sizeof (ifreq));
  120. sockaddr_ll.sll_protocol = htons (channel->type);
  121. if ((channel->fd = socket (sockaddr_ll.sll_family, SOCK_RAW, sockaddr_ll.sll_protocol)) == -1)
  122. {
  123. error (1, errno, "%s", channel->ifname);
  124. }
  125. memcpy (ifreq.ifr_name, channel->ifname, sizeof (ifreq.ifr_name));
  126. if (ioctl (channel->fd, SIOCGIFINDEX, &ifreq) == -1)
  127. {
  128. error (1, errno, "%s", ifreq.ifr_name);
  129. }
  130. channel->ifindex = sockaddr_ll.sll_ifindex = ifreq.ifr_ifindex;
  131. if (ioctl (channel->fd, SIOCGIFHWADDR, &ifreq) == -1)
  132. {
  133. error (1, errno, "%s", ifreq.ifr_name);
  134. }
  135. memcpy (sockaddr_ll.sll_addr, ifreq.ifr_ifru.ifru_hwaddr.sa_data, sizeof (sockaddr_ll.sll_addr));
  136. if (bind (channel->fd, (struct sockaddr *) (&sockaddr_ll), sizeof (sockaddr_ll)) == -1)
  137. {
  138. error (1, errno, "%s", ifreq.ifr_name);
  139. }
  140. memcpy (channel->host, sockaddr_ll.sll_addr, sizeof (channel->host));
  141. if (ioctl (channel->fd, SIOCGIFFLAGS, &ifreq) == -1)
  142. {
  143. error (1, errno, "%s", ifreq.ifr_name);
  144. }
  145. channel->ifstate = ifreq.ifr_flags;
  146. _setbits (ifreq.ifr_flags, (IFF_UP | IFF_BROADCAST | IFF_MULTICAST));
  147. _clrbits (ifreq.ifr_flags, (IFF_ALLMULTI | IFF_PROMISC));
  148. if (ioctl (channel->fd, SIOCSIFFLAGS, &ifreq) == -1)
  149. {
  150. error (1, errno, "%s", ifreq.ifr_name);
  151. }
  152. #else
  153. struct bpf_program bpf_program;
  154. static struct bpf_insn bpf_insn [] =
  155. {
  156. {
  157. BPF_LD + BPF_H + BPF_ABS,
  158. 0,
  159. 0,
  160. 12
  161. },
  162. {
  163. BPF_JMP + BPF_JEQ + BPF_K,
  164. 0,
  165. 18,
  166. 0
  167. },
  168. {
  169. BPF_LD + BPF_B + BPF_ABS,
  170. 0,
  171. 0,
  172. 0
  173. },
  174. {
  175. BPF_JMP + BPF_JEQ + BPF_K,
  176. 0,
  177. 10,
  178. 0
  179. },
  180. {
  181. BPF_LD + BPF_B + BPF_ABS,
  182. 0,
  183. 0,
  184. 1
  185. },
  186. {
  187. BPF_JMP + BPF_JEQ + BPF_K,
  188. 0,
  189. 8,
  190. 0
  191. },
  192. {
  193. BPF_LD + BPF_B + BPF_ABS,
  194. 0,
  195. 0,
  196. 2
  197. },
  198. {
  199. BPF_JMP + BPF_JEQ + BPF_K,
  200. 0,
  201. 6,
  202. 0
  203. },
  204. {
  205. BPF_LD + BPF_B + BPF_ABS,
  206. 0,
  207. 0,
  208. 3
  209. },
  210. {
  211. BPF_JMP + BPF_JEQ + BPF_K,
  212. 0,
  213. 4,
  214. 0
  215. },
  216. {
  217. BPF_LD + BPF_B + BPF_ABS,
  218. 0,
  219. 0,
  220. 4
  221. },
  222. {
  223. BPF_JMP + BPF_JEQ + BPF_K,
  224. 0,
  225. 2,
  226. 0
  227. },
  228. {
  229. BPF_LD + BPF_B + BPF_ABS,
  230. 0,
  231. 0,
  232. 5
  233. },
  234. {
  235. BPF_JMP + BPF_JEQ + BPF_K,
  236. 4,
  237. 0,
  238. 0
  239. },
  240. {
  241. BPF_LD + BPF_W + BPF_ABS,
  242. 0,
  243. 0,
  244. 0
  245. },
  246. {
  247. BPF_JMP + BPF_JEQ + BPF_K,
  248. 0,
  249. 4,
  250. 0xFFFFFFFF
  251. },
  252. {
  253. BPF_LD + BPF_H + BPF_ABS,
  254. 0,
  255. 0,
  256. 4
  257. },
  258. {
  259. BPF_JMP + BPF_JEQ + BPF_K,
  260. 0,
  261. 2,
  262. 0xFFFF
  263. },
  264. {
  265. BPF_LD + BPF_W + BPF_LEN,
  266. 0,
  267. 0,
  268. 0
  269. },
  270. {
  271. BPF_RET + BPF_A,
  272. 0,
  273. 0,
  274. 0
  275. },
  276. {
  277. BPF_RET + BPF_K,
  278. 0,
  279. 0,
  280. 0
  281. }
  282. };
  283. #if defined (__APPLE__) || defined (__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__)
  284. struct ifreq ifreq;
  285. struct timeval timeval;
  286. struct bpf * bpf;
  287. char filename [sizeof (CHANNEL_BPFDEVICE) + 1];
  288. unsigned count;
  289. unsigned state;
  290. int stat_errno = 0;
  291. int open_errno = 0;
  292. for (count = 0; count < 100; count++)
  293. {
  294. struct stat st;
  295. snprintf (filename, sizeof (filename), CHANNEL_BPFDEVICE, count);
  296. if (stat(filename, &st) == -1)
  297. {
  298. stat_errno = errno;
  299. continue;
  300. }
  301. if ((channel->fd = open (filename, O_RDWR)) != -1)
  302. {
  303. break;
  304. }
  305. else
  306. {
  307. open_errno = errno;
  308. }
  309. }
  310. if (channel->fd == -1)
  311. {
  312. if (open_errno)
  313. {
  314. error (1, open_errno, "Could not open bpf device");
  315. }
  316. else
  317. {
  318. error (1, stat_errno, "No bpf device found");
  319. }
  320. }
  321. memcpy (ifreq.ifr_name, channel->ifname, sizeof (ifreq.ifr_name));
  322. if (ioctl (channel->fd, BIOCSETIF, &ifreq) == -1)
  323. {
  324. error (1, errno, "%s", ifreq.ifr_name);
  325. }
  326. channel->bpf = bpf = malloc (sizeof (* bpf));
  327. if (ioctl (channel->fd, BIOCGBLEN, &bpf->bpf_length) == -1)
  328. {
  329. error (1, errno, "Can't determine buffer length: %s", ifreq.ifr_name);
  330. }
  331. bpf->bpf_bp = bpf->bpf_buffer = malloc (bpf->bpf_length);
  332. if (bpf->bpf_buffer == NULL)
  333. {
  334. error (1, errno, "Can't allocate receive buffer");
  335. }
  336. #if defined (__APPLE__) || defined (__NetBSD__)
  337. state = 0;
  338. if (ioctl (channel->fd, BIOCSSEESENT, &state) == -1)
  339. {
  340. error (1, errno, "Can't hide outgoing frames: %s", ifreq.ifr_name);
  341. }
  342. #elif defined (__OpenBSD__)
  343. state = BPF_DIRECTION_OUT;
  344. if (ioctl (channel->fd, BIOCSDIRFILT, &state) == -1)
  345. {
  346. error (0, errno, "Can't hide outgoing frames");
  347. }
  348. #elif defined (__FreeBSD__)
  349. state = BPF_D_IN;
  350. if (ioctl (channel->fd, BIOCSDIRECTION, &state) == -1)
  351. {
  352. error (0, errno, "Can't hide outgoing frames");
  353. }
  354. #else
  355. #error "Abandon all hope"
  356. #endif
  357. if (channel->capture > 1000)
  358. {
  359. timeval.tv_sec = channel->capture / 1000;
  360. timeval.tv_usec = 0;
  361. }
  362. else
  363. {
  364. #if defined (__MAC_10_6)
  365. /*
  366. * accommodate known bug in BPF on MAC OS X 10.6; shorter times cause socket read
  367. * operations to block indefinitely if no frames are waiting because tv_usec gets
  368. * clobbered;
  369. */
  370. timeval.tv_sec = 1;
  371. timeval.tv_usec = 0;
  372. #else
  373. timeval.tv_sec = 0;
  374. timeval.tv_usec = channel->capture * 1000;
  375. #endif
  376. }
  377. if (ioctl (channel->fd, BIOCSRTIMEOUT, &timeval) == -1)
  378. {
  379. error (1, errno, "Can't set channel timeout: %s", ifreq.ifr_name);
  380. }
  381. state = 1;
  382. if (ioctl (channel->fd, BIOCIMMEDIATE, &state) == -1)
  383. {
  384. error (1, errno, "Can't set immediate mode: %s", ifreq.ifr_name);
  385. }
  386. #if 1
  387. state = 1;
  388. if (ioctl (channel->fd, BIOCSHDRCMPLT, &state) == -1)
  389. {
  390. error (1, errno, "Can't set header complete mode: %s", ifreq.ifr_name);
  391. }
  392. #endif
  393. #if 1
  394. gethwaddr (channel->host, channel->ifname);
  395. #else
  396. if (ioctl (channel->fd, SIOCGIFADDR, &ifreq) > 0)
  397. {
  398. error (1, errno, "%s", ifreq.ifr_name);
  399. }
  400. memcpy (channel->host, LLADDR (ifreq.ifr_ifru.ifru_addr), sizeof (channel->host));
  401. #endif
  402. bpf_program.bf_len = sizeof (bpf_insn) / sizeof (struct bpf_insn);
  403. bpf_program.bf_insns = bpf_insn;
  404. if (channel->type == ETH_P_802_2)
  405. {
  406. bpf_insn [1].code = BPF_JMP + BPF_JGT + BPF_K;
  407. bpf_insn [1].jt = 18;
  408. bpf_insn [1].jf = 0;
  409. bpf_insn [1].k = ETHERMTU;
  410. }
  411. else
  412. {
  413. bpf_insn [1].code = BPF_JMP + BPF_JEQ + BPF_K;
  414. bpf_insn [1].jt = 0;
  415. bpf_insn [1].jf = 18;
  416. bpf_insn [1].k = channel->type;
  417. }
  418. bpf_insn [3].k = channel->host [0];
  419. bpf_insn [5].k = channel->host [1];
  420. bpf_insn [7].k = channel->host [2];
  421. bpf_insn [9].k = channel->host [3];
  422. bpf_insn [11].k = channel->host [4];
  423. bpf_insn [13].k = channel->host [5];
  424. if (ioctl (channel->fd, BIOCSETF, &bpf_program) == -1)
  425. {
  426. error (1, errno, "Can't store filter: %s", channel->ifname);
  427. }
  428. #elif defined (WINPCAP) || defined (LIBPCAP)
  429. channel->ifname = getifname (channel->ifindex);
  430. gethwaddr (channel->host, channel->ifname);
  431. channel->socket = pcap_open_live (channel->ifname, 65536, 0, channel->capture, channel->errbuf);
  432. snprintf ((char *)(channel->ifname), strlen (channel->ifname), "nic%d", channel->ifindex);
  433. if (!channel->socket)
  434. {
  435. error (1, errno, "Can't open interface: %s", channel->ifname);
  436. }
  437. bpf_program.bf_len = sizeof (bpf_insn)/sizeof (struct bpf_insn);
  438. bpf_program.bf_insns = bpf_insn;
  439. if (channel->type == ETH_P_802_2)
  440. {
  441. bpf_insn [1].code = BPF_JMP + BPF_JGT + BPF_K;
  442. bpf_insn [1].jt = 18;
  443. bpf_insn [1].jf = 0;
  444. bpf_insn [1].k = ETHERMTU;
  445. }
  446. else
  447. {
  448. bpf_insn [1].code = BPF_JMP + BPF_JEQ + BPF_K;
  449. bpf_insn [1].jt = 0;
  450. bpf_insn [1].jf = 18;
  451. bpf_insn [1].k = channel->type;
  452. }
  453. bpf_insn [3].k = channel->host [0];
  454. bpf_insn [5].k = channel->host [1];
  455. bpf_insn [7].k = channel->host [2];
  456. bpf_insn [9].k = channel->host [3];
  457. bpf_insn [11].k = channel->host [4];
  458. bpf_insn [13].k = channel->host [5];
  459. if (pcap_setfilter (channel->socket, &bpf_program) < 0)
  460. {
  461. error (1, errno, "Can't store filter: %s", channel->ifname);
  462. }
  463. if (pcap_setmintocopy (channel->socket, ETHER_MIN_LEN) < 0)
  464. {
  465. error (1, errno, "Can't set pcap mintocopy: %s", channel->ifname);
  466. }
  467. #else
  468. #error "Unknown Environment"
  469. #endif
  470. #endif
  471. return (0);
  472. }
  473. #endif