l2_packet_linux.c 13 KB

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