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 "ieee802_1x.h"
  30. #include "eloop.h"
  31. #include "sta_info.h"
  32. #include "driver.h"
  33. #include "accounting.h"
  34. struct wired_driver_data {
  35. struct hostapd_data *hapd;
  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. accounting_sta_get_id(hapd, sta);
  74. } else {
  75. wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
  76. MAC2STR(addr));
  77. }
  78. }
  79. static void handle_data(struct hostapd_data *hapd, unsigned char *buf,
  80. size_t len)
  81. {
  82. struct ieee8023_hdr *hdr;
  83. u8 *pos, *sa;
  84. size_t left;
  85. /* must contain at least ieee8023_hdr 6 byte source, 6 byte dest,
  86. * 2 byte ethertype */
  87. if (len < 14) {
  88. wpa_printf(MSG_MSGDUMP, "handle_data: too short (%lu)",
  89. (unsigned long) len);
  90. return;
  91. }
  92. hdr = (struct ieee8023_hdr *) buf;
  93. switch (ntohs(hdr->ethertype)) {
  94. case ETH_P_PAE:
  95. wpa_printf(MSG_MSGDUMP, "Received EAPOL packet");
  96. sa = hdr->src;
  97. wired_possible_new_sta(hapd, sa);
  98. pos = (u8 *) (hdr + 1);
  99. left = len - sizeof(*hdr);
  100. ieee802_1x_receive(hapd, sa, pos, left);
  101. break;
  102. default:
  103. wpa_printf(MSG_DEBUG, "Unknown ethertype 0x%04x in data frame",
  104. ntohs(hdr->ethertype));
  105. break;
  106. }
  107. }
  108. static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
  109. {
  110. struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
  111. int len;
  112. unsigned char buf[3000];
  113. len = recv(sock, buf, sizeof(buf), 0);
  114. if (len < 0) {
  115. perror("recv");
  116. return;
  117. }
  118. handle_data(hapd, buf, len);
  119. }
  120. static void handle_dhcp(int sock, void *eloop_ctx, void *sock_ctx)
  121. {
  122. struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
  123. int len;
  124. unsigned char buf[3000];
  125. struct dhcp_message *msg;
  126. u8 *mac_address;
  127. len = recv(sock, buf, sizeof(buf), 0);
  128. if (len < 0) {
  129. perror("recv");
  130. return;
  131. }
  132. /* must contain at least dhcp_message->chaddr */
  133. if (len < 44) {
  134. wpa_printf(MSG_MSGDUMP, "handle_dhcp: too short (%d)", len);
  135. return;
  136. }
  137. msg = (struct dhcp_message *) buf;
  138. mac_address = (u8 *) &(msg->chaddr);
  139. wpa_printf(MSG_MSGDUMP, "Got DHCP broadcast packet from " MACSTR,
  140. MAC2STR(mac_address));
  141. wired_possible_new_sta(hapd, mac_address);
  142. }
  143. static int wired_init_sockets(struct wired_driver_data *drv)
  144. {
  145. struct hostapd_data *hapd = drv->hapd;
  146. struct ifreq ifr;
  147. struct sockaddr_ll addr;
  148. struct sockaddr_in addr2;
  149. struct packet_mreq mreq;
  150. u8 multicastgroup_eapol[6] = WIRED_EAPOL_MULTICAST_GROUP;
  151. int n = 1;
  152. drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_PAE));
  153. if (drv->sock < 0) {
  154. perror("socket[PF_PACKET,SOCK_RAW]");
  155. return -1;
  156. }
  157. if (eloop_register_read_sock(drv->sock, handle_read, hapd, NULL)) {
  158. printf("Could not register read socket\n");
  159. return -1;
  160. }
  161. memset(&ifr, 0, sizeof(ifr));
  162. os_strlcpy(ifr.ifr_name, hapd->conf->iface, sizeof(ifr.ifr_name));
  163. if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
  164. perror("ioctl(SIOCGIFINDEX)");
  165. return -1;
  166. }
  167. memset(&addr, 0, sizeof(addr));
  168. addr.sll_family = AF_PACKET;
  169. addr.sll_ifindex = ifr.ifr_ifindex;
  170. wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
  171. addr.sll_ifindex);
  172. if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  173. perror("bind");
  174. return -1;
  175. }
  176. /* filter multicast address */
  177. memset(&mreq, 0, sizeof(mreq));
  178. mreq.mr_ifindex = ifr.ifr_ifindex;
  179. mreq.mr_type = PACKET_MR_MULTICAST;
  180. mreq.mr_alen = 6;
  181. memcpy(mreq.mr_address, multicastgroup_eapol, mreq.mr_alen);
  182. if (setsockopt(drv->sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
  183. sizeof(mreq)) < 0) {
  184. perror("setsockopt[SOL_SOCKET,PACKET_ADD_MEMBERSHIP]");
  185. return -1;
  186. }
  187. memset(&ifr, 0, sizeof(ifr));
  188. os_strlcpy(ifr.ifr_name, hapd->conf->iface, sizeof(ifr.ifr_name));
  189. if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
  190. perror("ioctl(SIOCGIFHWADDR)");
  191. return -1;
  192. }
  193. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
  194. printf("Invalid HW-addr family 0x%04x\n",
  195. ifr.ifr_hwaddr.sa_family);
  196. return -1;
  197. }
  198. memcpy(hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
  199. /* setup dhcp listen socket for sta detection */
  200. if ((drv->dhcp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  201. perror("socket call failed for dhcp");
  202. return -1;
  203. }
  204. if (eloop_register_read_sock(drv->dhcp_sock, handle_dhcp, hapd, NULL))
  205. {
  206. printf("Could not register read socket\n");
  207. return -1;
  208. }
  209. memset(&addr2, 0, sizeof(addr2));
  210. addr2.sin_family = AF_INET;
  211. addr2.sin_port = htons(67);
  212. addr2.sin_addr.s_addr = INADDR_ANY;
  213. if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_REUSEADDR, (char *) &n,
  214. sizeof(n)) == -1) {
  215. perror("setsockopt[SOL_SOCKET,SO_REUSEADDR]");
  216. return -1;
  217. }
  218. if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BROADCAST, (char *) &n,
  219. sizeof(n)) == -1) {
  220. perror("setsockopt[SOL_SOCKET,SO_BROADCAST]");
  221. return -1;
  222. }
  223. memset(&ifr, 0, sizeof(ifr));
  224. os_strlcpy(ifr.ifr_ifrn.ifrn_name, hapd->conf->iface, IFNAMSIZ);
  225. if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BINDTODEVICE,
  226. (char *) &ifr, sizeof(ifr)) < 0) {
  227. perror("setsockopt[SOL_SOCKET,SO_BINDTODEVICE]");
  228. return -1;
  229. }
  230. if (bind(drv->dhcp_sock, (struct sockaddr *) &addr2,
  231. sizeof(struct sockaddr)) == -1) {
  232. perror("bind");
  233. return -1;
  234. }
  235. return 0;
  236. }
  237. static int wired_send_eapol(void *priv, const u8 *addr,
  238. const u8 *data, size_t data_len, int encrypt,
  239. const u8 *own_addr)
  240. {
  241. struct wired_driver_data *drv = priv;
  242. u8 pae_group_addr[ETH_ALEN] = WIRED_EAPOL_MULTICAST_GROUP;
  243. struct ieee8023_hdr *hdr;
  244. size_t len;
  245. u8 *pos;
  246. int res;
  247. len = sizeof(*hdr) + data_len;
  248. hdr = os_zalloc(len);
  249. if (hdr == NULL) {
  250. printf("malloc() failed for wired_send_eapol(len=%lu)\n",
  251. (unsigned long) len);
  252. return -1;
  253. }
  254. memcpy(hdr->dest, drv->use_pae_group_addr ? pae_group_addr : addr,
  255. ETH_ALEN);
  256. memcpy(hdr->src, own_addr, ETH_ALEN);
  257. hdr->ethertype = htons(ETH_P_PAE);
  258. pos = (u8 *) (hdr + 1);
  259. memcpy(pos, data, data_len);
  260. res = send(drv->sock, (u8 *) hdr, len, 0);
  261. free(hdr);
  262. if (res < 0) {
  263. perror("wired_send_eapol: send");
  264. printf("wired_send_eapol - packet len: %lu - failed\n",
  265. (unsigned long) len);
  266. }
  267. return res;
  268. }
  269. static void * wired_driver_init(struct hostapd_data *hapd)
  270. {
  271. struct wired_driver_data *drv;
  272. drv = os_zalloc(sizeof(struct wired_driver_data));
  273. if (drv == NULL) {
  274. printf("Could not allocate memory for wired driver data\n");
  275. return NULL;
  276. }
  277. drv->hapd = hapd;
  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. };