driver_wired.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * hostapd / Kernel driver communication for wired (Ethernet) drivers
  3. * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2004, Gunter Burchardt <tira@isx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "includes.h"
  16. #include <sys/ioctl.h>
  17. #ifdef USE_KERNEL_HEADERS
  18. #include <asm/types.h>
  19. #include <linux/if_packet.h>
  20. #include <linux/if_ether.h> /* The L2 protocols */
  21. #include <linux/if_arp.h>
  22. #include <linux/if.h>
  23. #else /* USE_KERNEL_HEADERS */
  24. #include <net/if_arp.h>
  25. #include <net/if.h>
  26. #include <netpacket/packet.h>
  27. #endif /* USE_KERNEL_HEADERS */
  28. #include "hostapd.h"
  29. #include "eloop.h"
  30. #include "sta_info.h"
  31. #include "driver.h"
  32. #include "accounting.h"
  33. struct wired_driver_data {
  34. struct hostapd_data *hapd;
  35. char iface[IFNAMSIZ + 1];
  36. int sock; /* raw packet socket for driver access */
  37. int dhcp_sock; /* socket for dhcp packets */
  38. int use_pae_group_addr;
  39. };
  40. #define WIRED_EAPOL_MULTICAST_GROUP {0x01,0x80,0xc2,0x00,0x00,0x03}
  41. /* TODO: detecting new devices should eventually be changed from using DHCP
  42. * snooping to trigger on any packet from a new layer 2 MAC address, e.g.,
  43. * based on ebtables, etc. */
  44. struct dhcp_message {
  45. u_int8_t op;
  46. u_int8_t htype;
  47. u_int8_t hlen;
  48. u_int8_t hops;
  49. u_int32_t xid;
  50. u_int16_t secs;
  51. u_int16_t flags;
  52. u_int32_t ciaddr;
  53. u_int32_t yiaddr;
  54. u_int32_t siaddr;
  55. u_int32_t giaddr;
  56. u_int8_t chaddr[16];
  57. u_int8_t sname[64];
  58. u_int8_t file[128];
  59. u_int32_t cookie;
  60. u_int8_t options[308]; /* 312 - cookie */
  61. };
  62. static void wired_possible_new_sta(struct hostapd_data *hapd, u8 *addr)
  63. {
  64. struct sta_info *sta;
  65. sta = ap_get_sta(hapd, addr);
  66. if (sta)
  67. return;
  68. wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
  69. " - adding a new STA", MAC2STR(addr));
  70. sta = ap_sta_add(hapd, addr);
  71. if (sta) {
  72. hostapd_new_assoc_sta(hapd, sta, 0);
  73. } else {
  74. wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
  75. MAC2STR(addr));
  76. }
  77. }
  78. static void handle_data(struct hostapd_data *hapd, unsigned char *buf,
  79. size_t len)
  80. {
  81. struct ieee8023_hdr *hdr;
  82. u8 *pos, *sa;
  83. size_t left;
  84. /* must contain at least ieee8023_hdr 6 byte source, 6 byte dest,
  85. * 2 byte ethertype */
  86. if (len < 14) {
  87. wpa_printf(MSG_MSGDUMP, "handle_data: too short (%lu)",
  88. (unsigned long) len);
  89. return;
  90. }
  91. hdr = (struct ieee8023_hdr *) buf;
  92. switch (ntohs(hdr->ethertype)) {
  93. case ETH_P_PAE:
  94. wpa_printf(MSG_MSGDUMP, "Received EAPOL packet");
  95. sa = hdr->src;
  96. wired_possible_new_sta(hapd, sa);
  97. pos = (u8 *) (hdr + 1);
  98. left = len - sizeof(*hdr);
  99. hostapd_eapol_receive(hapd, sa, pos, left);
  100. break;
  101. default:
  102. wpa_printf(MSG_DEBUG, "Unknown ethertype 0x%04x in data frame",
  103. ntohs(hdr->ethertype));
  104. break;
  105. }
  106. }
  107. static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
  108. {
  109. struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
  110. int len;
  111. unsigned char buf[3000];
  112. len = recv(sock, buf, sizeof(buf), 0);
  113. if (len < 0) {
  114. perror("recv");
  115. return;
  116. }
  117. handle_data(hapd, buf, len);
  118. }
  119. static void handle_dhcp(int sock, void *eloop_ctx, void *sock_ctx)
  120. {
  121. struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
  122. int len;
  123. unsigned char buf[3000];
  124. struct dhcp_message *msg;
  125. u8 *mac_address;
  126. len = recv(sock, buf, sizeof(buf), 0);
  127. if (len < 0) {
  128. perror("recv");
  129. return;
  130. }
  131. /* must contain at least dhcp_message->chaddr */
  132. if (len < 44) {
  133. wpa_printf(MSG_MSGDUMP, "handle_dhcp: too short (%d)", len);
  134. return;
  135. }
  136. msg = (struct dhcp_message *) buf;
  137. mac_address = (u8 *) &(msg->chaddr);
  138. wpa_printf(MSG_MSGDUMP, "Got DHCP broadcast packet from " MACSTR,
  139. MAC2STR(mac_address));
  140. wired_possible_new_sta(hapd, mac_address);
  141. }
  142. static int wired_init_sockets(struct wired_driver_data *drv)
  143. {
  144. struct hostapd_data *hapd = drv->hapd;
  145. struct ifreq ifr;
  146. struct sockaddr_ll addr;
  147. struct sockaddr_in addr2;
  148. struct packet_mreq mreq;
  149. u8 multicastgroup_eapol[6] = WIRED_EAPOL_MULTICAST_GROUP;
  150. int n = 1;
  151. drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_PAE));
  152. if (drv->sock < 0) {
  153. perror("socket[PF_PACKET,SOCK_RAW]");
  154. return -1;
  155. }
  156. if (eloop_register_read_sock(drv->sock, handle_read, hapd, NULL)) {
  157. printf("Could not register read socket\n");
  158. return -1;
  159. }
  160. memset(&ifr, 0, sizeof(ifr));
  161. os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
  162. if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
  163. perror("ioctl(SIOCGIFINDEX)");
  164. return -1;
  165. }
  166. memset(&addr, 0, sizeof(addr));
  167. addr.sll_family = AF_PACKET;
  168. addr.sll_ifindex = ifr.ifr_ifindex;
  169. wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
  170. addr.sll_ifindex);
  171. if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  172. perror("bind");
  173. return -1;
  174. }
  175. /* filter multicast address */
  176. memset(&mreq, 0, sizeof(mreq));
  177. mreq.mr_ifindex = ifr.ifr_ifindex;
  178. mreq.mr_type = PACKET_MR_MULTICAST;
  179. mreq.mr_alen = 6;
  180. memcpy(mreq.mr_address, multicastgroup_eapol, mreq.mr_alen);
  181. if (setsockopt(drv->sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
  182. sizeof(mreq)) < 0) {
  183. perror("setsockopt[SOL_SOCKET,PACKET_ADD_MEMBERSHIP]");
  184. return -1;
  185. }
  186. memset(&ifr, 0, sizeof(ifr));
  187. os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
  188. if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
  189. perror("ioctl(SIOCGIFHWADDR)");
  190. return -1;
  191. }
  192. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
  193. printf("Invalid HW-addr family 0x%04x\n",
  194. ifr.ifr_hwaddr.sa_family);
  195. return -1;
  196. }
  197. memcpy(hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
  198. /* setup dhcp listen socket for sta detection */
  199. if ((drv->dhcp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  200. perror("socket call failed for dhcp");
  201. return -1;
  202. }
  203. if (eloop_register_read_sock(drv->dhcp_sock, handle_dhcp, hapd, NULL))
  204. {
  205. printf("Could not register read socket\n");
  206. return -1;
  207. }
  208. memset(&addr2, 0, sizeof(addr2));
  209. addr2.sin_family = AF_INET;
  210. addr2.sin_port = htons(67);
  211. addr2.sin_addr.s_addr = INADDR_ANY;
  212. if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_REUSEADDR, (char *) &n,
  213. sizeof(n)) == -1) {
  214. perror("setsockopt[SOL_SOCKET,SO_REUSEADDR]");
  215. return -1;
  216. }
  217. if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BROADCAST, (char *) &n,
  218. sizeof(n)) == -1) {
  219. perror("setsockopt[SOL_SOCKET,SO_BROADCAST]");
  220. return -1;
  221. }
  222. memset(&ifr, 0, sizeof(ifr));
  223. os_strlcpy(ifr.ifr_ifrn.ifrn_name, drv->iface, IFNAMSIZ);
  224. if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BINDTODEVICE,
  225. (char *) &ifr, sizeof(ifr)) < 0) {
  226. perror("setsockopt[SOL_SOCKET,SO_BINDTODEVICE]");
  227. return -1;
  228. }
  229. if (bind(drv->dhcp_sock, (struct sockaddr *) &addr2,
  230. sizeof(struct sockaddr)) == -1) {
  231. perror("bind");
  232. return -1;
  233. }
  234. return 0;
  235. }
  236. static int wired_send_eapol(void *priv, const u8 *addr,
  237. const u8 *data, size_t data_len, int encrypt,
  238. const u8 *own_addr)
  239. {
  240. struct wired_driver_data *drv = priv;
  241. u8 pae_group_addr[ETH_ALEN] = WIRED_EAPOL_MULTICAST_GROUP;
  242. struct ieee8023_hdr *hdr;
  243. size_t len;
  244. u8 *pos;
  245. int res;
  246. len = sizeof(*hdr) + data_len;
  247. hdr = os_zalloc(len);
  248. if (hdr == NULL) {
  249. printf("malloc() failed for wired_send_eapol(len=%lu)\n",
  250. (unsigned long) len);
  251. return -1;
  252. }
  253. memcpy(hdr->dest, drv->use_pae_group_addr ? pae_group_addr : addr,
  254. ETH_ALEN);
  255. memcpy(hdr->src, own_addr, ETH_ALEN);
  256. hdr->ethertype = htons(ETH_P_PAE);
  257. pos = (u8 *) (hdr + 1);
  258. memcpy(pos, data, data_len);
  259. res = send(drv->sock, (u8 *) hdr, len, 0);
  260. free(hdr);
  261. if (res < 0) {
  262. perror("wired_send_eapol: send");
  263. printf("wired_send_eapol - packet len: %lu - failed\n",
  264. (unsigned long) len);
  265. }
  266. return res;
  267. }
  268. static void * wired_driver_init(struct hostapd_data *hapd)
  269. {
  270. struct wired_driver_data *drv;
  271. drv = os_zalloc(sizeof(struct wired_driver_data));
  272. if (drv == NULL) {
  273. printf("Could not allocate memory for wired driver data\n");
  274. return NULL;
  275. }
  276. drv->hapd = hapd;
  277. os_strlcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
  278. drv->use_pae_group_addr = hapd->conf->use_pae_group_addr;
  279. if (wired_init_sockets(drv)) {
  280. free(drv);
  281. return NULL;
  282. }
  283. return drv;
  284. }
  285. static void wired_driver_deinit(void *priv)
  286. {
  287. struct wired_driver_data *drv = priv;
  288. if (drv->sock >= 0)
  289. close(drv->sock);
  290. if (drv->dhcp_sock >= 0)
  291. close(drv->dhcp_sock);
  292. free(drv);
  293. }
  294. const struct wpa_driver_ops wpa_driver_wired_ops = {
  295. .name = "wired",
  296. .init = wired_driver_init,
  297. .deinit = wired_driver_deinit,
  298. .send_eapol = wired_send_eapol,
  299. };