l2_packet_linux.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * WPA Supplicant - Layer2 packet handling with Linux packet sockets
  3. * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include <sys/ioctl.h>
  10. #include <netpacket/packet.h>
  11. #include <net/if.h>
  12. #include <linux/filter.h>
  13. #include "common.h"
  14. #include "eloop.h"
  15. #include "crypto/sha1.h"
  16. #include "crypto/crypto.h"
  17. #include "l2_packet.h"
  18. struct l2_packet_data {
  19. int fd; /* packet socket for EAPOL frames */
  20. char ifname[IFNAMSIZ + 1];
  21. int ifindex;
  22. u8 own_addr[ETH_ALEN];
  23. void (*rx_callback)(void *ctx, const u8 *src_addr,
  24. const u8 *buf, size_t len);
  25. void *rx_callback_ctx;
  26. int l2_hdr; /* whether to include layer 2 (Ethernet) header data
  27. * buffers */
  28. /* For working around Linux packet socket behavior and regression. */
  29. int fd_br_rx;
  30. int last_from_br;
  31. u8 last_hash[SHA1_MAC_LEN];
  32. unsigned int num_rx, num_rx_br;
  33. };
  34. /* Generated by 'sudo tcpdump -s 3000 -dd greater 278 and ip and udp and
  35. * src port bootps and dst port bootpc'
  36. */
  37. static struct sock_filter dhcp_sock_filter_insns[] = {
  38. { 0x80, 0, 0, 0x00000000 },
  39. { 0x35, 0, 12, 0x00000116 },
  40. { 0x28, 0, 0, 0x0000000c },
  41. { 0x15, 0, 10, 0x00000800 },
  42. { 0x30, 0, 0, 0x00000017 },
  43. { 0x15, 0, 8, 0x00000011 },
  44. { 0x28, 0, 0, 0x00000014 },
  45. { 0x45, 6, 0, 0x00001fff },
  46. { 0xb1, 0, 0, 0x0000000e },
  47. { 0x48, 0, 0, 0x0000000e },
  48. { 0x15, 0, 3, 0x00000043 },
  49. { 0x48, 0, 0, 0x00000010 },
  50. { 0x15, 0, 1, 0x00000044 },
  51. { 0x6, 0, 0, 0x00000bb8 },
  52. { 0x6, 0, 0, 0x00000000 },
  53. };
  54. static const struct sock_fprog dhcp_sock_filter = {
  55. .len = ARRAY_SIZE(dhcp_sock_filter_insns),
  56. .filter = dhcp_sock_filter_insns,
  57. };
  58. /* Generated by 'sudo tcpdump -dd -s 1500 multicast and ip6[6]=58' */
  59. static struct sock_filter ndisc_sock_filter_insns[] = {
  60. { 0x30, 0, 0, 0x00000000 },
  61. { 0x45, 0, 5, 0x00000001 },
  62. { 0x28, 0, 0, 0x0000000c },
  63. { 0x15, 0, 3, 0x000086dd },
  64. { 0x30, 0, 0, 0x00000014 },
  65. { 0x15, 0, 1, 0x0000003a },
  66. { 0x6, 0, 0, 0x000005dc },
  67. { 0x6, 0, 0, 0x00000000 },
  68. };
  69. static const struct sock_fprog ndisc_sock_filter = {
  70. .len = ARRAY_SIZE(ndisc_sock_filter_insns),
  71. .filter = ndisc_sock_filter_insns,
  72. };
  73. int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
  74. {
  75. os_memcpy(addr, l2->own_addr, ETH_ALEN);
  76. return 0;
  77. }
  78. int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
  79. const u8 *buf, size_t len)
  80. {
  81. int ret;
  82. if (l2 == NULL)
  83. return -1;
  84. if (l2->l2_hdr) {
  85. ret = send(l2->fd, buf, len, 0);
  86. if (ret < 0)
  87. wpa_printf(MSG_ERROR, "l2_packet_send - send: %s",
  88. strerror(errno));
  89. } else {
  90. struct sockaddr_ll ll;
  91. os_memset(&ll, 0, sizeof(ll));
  92. ll.sll_family = AF_PACKET;
  93. ll.sll_ifindex = l2->ifindex;
  94. ll.sll_protocol = htons(proto);
  95. ll.sll_halen = ETH_ALEN;
  96. os_memcpy(ll.sll_addr, dst_addr, ETH_ALEN);
  97. ret = sendto(l2->fd, buf, len, 0, (struct sockaddr *) &ll,
  98. sizeof(ll));
  99. if (ret < 0) {
  100. wpa_printf(MSG_ERROR, "l2_packet_send - sendto: %s",
  101. strerror(errno));
  102. }
  103. }
  104. return ret;
  105. }
  106. static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
  107. {
  108. struct l2_packet_data *l2 = eloop_ctx;
  109. u8 buf[2300];
  110. int res;
  111. struct sockaddr_ll ll;
  112. socklen_t fromlen;
  113. l2->num_rx++;
  114. os_memset(&ll, 0, sizeof(ll));
  115. fromlen = sizeof(ll);
  116. res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &ll,
  117. &fromlen);
  118. if (res < 0) {
  119. wpa_printf(MSG_DEBUG, "l2_packet_receive - recvfrom: %s",
  120. strerror(errno));
  121. return;
  122. }
  123. wpa_printf(MSG_DEBUG, "%s: src=" MACSTR " len=%d",
  124. __func__, MAC2STR(ll.sll_addr), (int) res);
  125. if (l2->fd_br_rx >= 0) {
  126. u8 hash[SHA1_MAC_LEN];
  127. const u8 *addr[1];
  128. size_t len[1];
  129. /*
  130. * Close the workaround socket if the kernel version seems to be
  131. * able to deliver packets through the packet socket before
  132. * authorization has been completed (in dormant state).
  133. */
  134. if (l2->num_rx_br <= 1) {
  135. wpa_printf(MSG_DEBUG,
  136. "l2_packet_receive: Main packet socket for %s seems to have working RX - close workaround bridge socket",
  137. l2->ifname);
  138. eloop_unregister_read_sock(l2->fd_br_rx);
  139. close(l2->fd_br_rx);
  140. l2->fd_br_rx = -1;
  141. }
  142. addr[0] = buf;
  143. len[0] = res;
  144. sha1_vector(1, addr, len, hash);
  145. if (l2->last_from_br &&
  146. os_memcmp(hash, l2->last_hash, SHA1_MAC_LEN) == 0) {
  147. wpa_printf(MSG_DEBUG, "%s: Drop duplicate RX",
  148. __func__);
  149. return;
  150. }
  151. os_memcpy(l2->last_hash, hash, SHA1_MAC_LEN);
  152. }
  153. l2->last_from_br = 0;
  154. l2->rx_callback(l2->rx_callback_ctx, ll.sll_addr, buf, res);
  155. }
  156. static void l2_packet_receive_br(int sock, void *eloop_ctx, void *sock_ctx)
  157. {
  158. struct l2_packet_data *l2 = eloop_ctx;
  159. u8 buf[2300];
  160. int res;
  161. struct sockaddr_ll ll;
  162. socklen_t fromlen;
  163. u8 hash[SHA1_MAC_LEN];
  164. const u8 *addr[1];
  165. size_t len[1];
  166. l2->num_rx_br++;
  167. os_memset(&ll, 0, sizeof(ll));
  168. fromlen = sizeof(ll);
  169. res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &ll,
  170. &fromlen);
  171. if (res < 0) {
  172. wpa_printf(MSG_DEBUG, "l2_packet_receive_br - recvfrom: %s",
  173. strerror(errno));
  174. return;
  175. }
  176. wpa_printf(MSG_DEBUG, "%s: src=" MACSTR " len=%d",
  177. __func__, MAC2STR(ll.sll_addr), (int) res);
  178. addr[0] = buf;
  179. len[0] = res;
  180. sha1_vector(1, addr, len, hash);
  181. if (!l2->last_from_br &&
  182. os_memcmp(hash, l2->last_hash, SHA1_MAC_LEN) == 0) {
  183. wpa_printf(MSG_DEBUG, "%s: Drop duplicate RX", __func__);
  184. return;
  185. }
  186. l2->last_from_br = 1;
  187. os_memcpy(l2->last_hash, hash, SHA1_MAC_LEN);
  188. l2->rx_callback(l2->rx_callback_ctx, ll.sll_addr, buf, res);
  189. }
  190. struct l2_packet_data * l2_packet_init(
  191. const char *ifname, const u8 *own_addr, unsigned short protocol,
  192. void (*rx_callback)(void *ctx, const u8 *src_addr,
  193. const u8 *buf, size_t len),
  194. void *rx_callback_ctx, int l2_hdr)
  195. {
  196. struct l2_packet_data *l2;
  197. struct ifreq ifr;
  198. struct sockaddr_ll ll;
  199. l2 = os_zalloc(sizeof(struct l2_packet_data));
  200. if (l2 == NULL)
  201. return NULL;
  202. os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
  203. l2->rx_callback = rx_callback;
  204. l2->rx_callback_ctx = rx_callback_ctx;
  205. l2->l2_hdr = l2_hdr;
  206. l2->fd_br_rx = -1;
  207. l2->fd = socket(PF_PACKET, l2_hdr ? SOCK_RAW : SOCK_DGRAM,
  208. htons(protocol));
  209. if (l2->fd < 0) {
  210. wpa_printf(MSG_ERROR, "%s: socket(PF_PACKET): %s",
  211. __func__, strerror(errno));
  212. os_free(l2);
  213. return NULL;
  214. }
  215. os_memset(&ifr, 0, sizeof(ifr));
  216. os_strlcpy(ifr.ifr_name, l2->ifname, sizeof(ifr.ifr_name));
  217. if (ioctl(l2->fd, SIOCGIFINDEX, &ifr) < 0) {
  218. wpa_printf(MSG_ERROR, "%s: ioctl[SIOCGIFINDEX]: %s",
  219. __func__, strerror(errno));
  220. close(l2->fd);
  221. os_free(l2);
  222. return NULL;
  223. }
  224. l2->ifindex = ifr.ifr_ifindex;
  225. os_memset(&ll, 0, sizeof(ll));
  226. ll.sll_family = PF_PACKET;
  227. ll.sll_ifindex = ifr.ifr_ifindex;
  228. ll.sll_protocol = htons(protocol);
  229. if (bind(l2->fd, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  230. wpa_printf(MSG_ERROR, "%s: bind[PF_PACKET]: %s",
  231. __func__, strerror(errno));
  232. close(l2->fd);
  233. os_free(l2);
  234. return NULL;
  235. }
  236. if (ioctl(l2->fd, SIOCGIFHWADDR, &ifr) < 0) {
  237. wpa_printf(MSG_ERROR, "%s: ioctl[SIOCGIFHWADDR]: %s",
  238. __func__, strerror(errno));
  239. close(l2->fd);
  240. os_free(l2);
  241. return NULL;
  242. }
  243. os_memcpy(l2->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
  244. eloop_register_read_sock(l2->fd, l2_packet_receive, l2, NULL);
  245. return l2;
  246. }
  247. struct l2_packet_data * l2_packet_init_bridge(
  248. const char *br_ifname, const char *ifname, const u8 *own_addr,
  249. unsigned short protocol,
  250. void (*rx_callback)(void *ctx, const u8 *src_addr,
  251. const u8 *buf, size_t len),
  252. void *rx_callback_ctx, int l2_hdr)
  253. {
  254. struct l2_packet_data *l2;
  255. struct sock_filter ethertype_sock_filter_insns[] = {
  256. /* Load ethertype */
  257. BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 2 * ETH_ALEN),
  258. /* Jump over next statement if ethertype does not match */
  259. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, protocol, 0, 1),
  260. /* Ethertype match - return all */
  261. BPF_STMT(BPF_RET | BPF_K, ~0),
  262. /* No match - drop */
  263. BPF_STMT(BPF_RET | BPF_K, 0)
  264. };
  265. const struct sock_fprog ethertype_sock_filter = {
  266. .len = ARRAY_SIZE(ethertype_sock_filter_insns),
  267. .filter = ethertype_sock_filter_insns,
  268. };
  269. struct sockaddr_ll ll;
  270. l2 = l2_packet_init(br_ifname, own_addr, protocol, rx_callback,
  271. rx_callback_ctx, l2_hdr);
  272. if (!l2)
  273. return NULL;
  274. /*
  275. * The Linux packet socket behavior has changed over the years and there
  276. * is an inconvenient regression in it that breaks RX for a specific
  277. * protocol from interfaces in a bridge when that interface is not in
  278. * fully operation state (i.e., when in station mode and not completed
  279. * authorization). To work around this, register ETH_P_ALL version of
  280. * the packet socket bound to the real netdev and use socket filter to
  281. * match the ethertype value. This version is less efficient, but
  282. * required for functionality with many kernel version. If the main
  283. * packet socket is found to be working, this less efficient version
  284. * gets closed automatically.
  285. */
  286. l2->fd_br_rx = socket(PF_PACKET, l2_hdr ? SOCK_RAW : SOCK_DGRAM,
  287. htons(ETH_P_ALL));
  288. if (l2->fd_br_rx < 0) {
  289. wpa_printf(MSG_DEBUG, "%s: socket(PF_PACKET-fd_br_rx): %s",
  290. __func__, strerror(errno));
  291. /* try to continue without the workaround RX socket */
  292. return l2;
  293. }
  294. os_memset(&ll, 0, sizeof(ll));
  295. ll.sll_family = PF_PACKET;
  296. ll.sll_ifindex = if_nametoindex(ifname);
  297. ll.sll_protocol = htons(ETH_P_ALL);
  298. if (bind(l2->fd_br_rx, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  299. wpa_printf(MSG_DEBUG, "%s: bind[PF_PACKET-fd_br_rx]: %s",
  300. __func__, strerror(errno));
  301. /* try to continue without the workaround RX socket */
  302. close(l2->fd_br_rx);
  303. l2->fd_br_rx = -1;
  304. return l2;
  305. }
  306. if (setsockopt(l2->fd_br_rx, SOL_SOCKET, SO_ATTACH_FILTER,
  307. &ethertype_sock_filter, sizeof(struct sock_fprog))) {
  308. wpa_printf(MSG_DEBUG,
  309. "l2_packet_linux: setsockopt(SO_ATTACH_FILTER) failed: %s",
  310. strerror(errno));
  311. /* try to continue without the workaround RX socket */
  312. close(l2->fd_br_rx);
  313. l2->fd_br_rx = -1;
  314. return l2;
  315. }
  316. eloop_register_read_sock(l2->fd_br_rx, l2_packet_receive_br, l2, NULL);
  317. return l2;
  318. }
  319. void l2_packet_deinit(struct l2_packet_data *l2)
  320. {
  321. if (l2 == NULL)
  322. return;
  323. if (l2->fd >= 0) {
  324. eloop_unregister_read_sock(l2->fd);
  325. close(l2->fd);
  326. }
  327. if (l2->fd_br_rx >= 0) {
  328. eloop_unregister_read_sock(l2->fd_br_rx);
  329. close(l2->fd_br_rx);
  330. }
  331. os_free(l2);
  332. }
  333. int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
  334. {
  335. int s;
  336. struct ifreq ifr;
  337. struct sockaddr_in *saddr;
  338. size_t res;
  339. s = socket(PF_INET, SOCK_DGRAM, 0);
  340. if (s < 0) {
  341. wpa_printf(MSG_ERROR, "%s: socket: %s",
  342. __func__, strerror(errno));
  343. return -1;
  344. }
  345. os_memset(&ifr, 0, sizeof(ifr));
  346. os_strlcpy(ifr.ifr_name, l2->ifname, sizeof(ifr.ifr_name));
  347. if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
  348. if (errno != EADDRNOTAVAIL)
  349. wpa_printf(MSG_ERROR, "%s: ioctl[SIOCGIFADDR]: %s",
  350. __func__, strerror(errno));
  351. close(s);
  352. return -1;
  353. }
  354. close(s);
  355. saddr = aliasing_hide_typecast(&ifr.ifr_addr, struct sockaddr_in);
  356. if (saddr->sin_family != AF_INET)
  357. return -1;
  358. res = os_strlcpy(buf, inet_ntoa(saddr->sin_addr), len);
  359. if (res >= len)
  360. return -1;
  361. return 0;
  362. }
  363. void l2_packet_notify_auth_start(struct l2_packet_data *l2)
  364. {
  365. }
  366. int l2_packet_set_packet_filter(struct l2_packet_data *l2,
  367. enum l2_packet_filter_type type)
  368. {
  369. const struct sock_fprog *sock_filter;
  370. switch (type) {
  371. case L2_PACKET_FILTER_DHCP:
  372. sock_filter = &dhcp_sock_filter;
  373. break;
  374. case L2_PACKET_FILTER_NDISC:
  375. sock_filter = &ndisc_sock_filter;
  376. break;
  377. default:
  378. return -1;
  379. }
  380. if (setsockopt(l2->fd, SOL_SOCKET, SO_ATTACH_FILTER,
  381. sock_filter, sizeof(struct sock_fprog))) {
  382. wpa_printf(MSG_ERROR,
  383. "l2_packet_linux: setsockopt(SO_ATTACH_FILTER) failed: %s",
  384. strerror(errno));
  385. return -1;
  386. }
  387. return 0;
  388. }