wired.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Received frame processing for wired interface
  3. * Copyright (c) 2010, 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 "utils/includes.h"
  9. #include <net/ethernet.h>
  10. #include <netinet/ip.h>
  11. #include <netinet/udp.h>
  12. #include "utils/common.h"
  13. #include "radius/radius.h"
  14. #include "wlantest.h"
  15. static struct wlantest_radius * radius_get(struct wlantest *wt, u32 srv,
  16. u32 cli)
  17. {
  18. struct wlantest_radius *r;
  19. dl_list_for_each(r, &wt->radius, struct wlantest_radius, list) {
  20. if (r->srv == srv && r->cli == cli)
  21. return r;
  22. }
  23. r = os_zalloc(sizeof(*r));
  24. if (r == NULL)
  25. return NULL;
  26. r->srv = srv;
  27. r->cli = cli;
  28. dl_list_add(&wt->radius, &r->list);
  29. return r;
  30. }
  31. static const char * radius_code_string(u8 code)
  32. {
  33. switch (code) {
  34. case RADIUS_CODE_ACCESS_REQUEST:
  35. return "Access-Request";
  36. case RADIUS_CODE_ACCESS_ACCEPT:
  37. return "Access-Accept";
  38. case RADIUS_CODE_ACCESS_REJECT:
  39. return "Access-Reject";
  40. case RADIUS_CODE_ACCOUNTING_REQUEST:
  41. return "Accounting-Request";
  42. case RADIUS_CODE_ACCOUNTING_RESPONSE:
  43. return "Accounting-Response";
  44. case RADIUS_CODE_ACCESS_CHALLENGE:
  45. return "Access-Challenge";
  46. case RADIUS_CODE_STATUS_SERVER:
  47. return "Status-Server";
  48. case RADIUS_CODE_STATUS_CLIENT:
  49. return "Status-Client";
  50. case RADIUS_CODE_RESERVED:
  51. return "Reserved";
  52. default:
  53. return "?Unknown?";
  54. }
  55. }
  56. static void process_radius_access_request(struct wlantest *wt, u32 dst,
  57. u32 src, const u8 *data, size_t len)
  58. {
  59. struct radius_msg *msg;
  60. struct wlantest_radius *r;
  61. msg = radius_msg_parse(data, len);
  62. if (msg == NULL) {
  63. wpa_printf(MSG_DEBUG, "Failed to parse RADIUS Access-Request");
  64. return;
  65. }
  66. r = radius_get(wt, dst, src);
  67. if (r) {
  68. radius_msg_free(r->last_req);
  69. r->last_req = msg;
  70. return;
  71. }
  72. radius_msg_free(msg);
  73. }
  74. static void wlantest_add_pmk(struct wlantest *wt, const u8 *pmk, size_t pmk_len)
  75. {
  76. struct wlantest_pmk *p;
  77. p = os_zalloc(sizeof(*p));
  78. if (p == NULL)
  79. return;
  80. os_memcpy(p->pmk, pmk, pmk_len);
  81. p->pmk_len = pmk_len;
  82. dl_list_add(&wt->pmk, &p->list);
  83. wpa_hexdump(MSG_INFO, "Add PMK", pmk, pmk_len);
  84. }
  85. static void process_radius_access_accept(struct wlantest *wt, u32 dst, u32 src,
  86. const u8 *data, size_t len)
  87. {
  88. struct radius_msg *msg;
  89. struct wlantest_radius *r;
  90. struct radius_ms_mppe_keys *keys;
  91. struct wlantest_radius_secret *s;
  92. r = radius_get(wt, src, dst);
  93. if (r == NULL || r->last_req == NULL) {
  94. wpa_printf(MSG_DEBUG, "No RADIUS Access-Challenge found for "
  95. "decrypting Access-Accept keys");
  96. return;
  97. }
  98. msg = radius_msg_parse(data, len);
  99. if (msg == NULL) {
  100. wpa_printf(MSG_DEBUG, "Failed to parse RADIUS Access-Accept");
  101. return;
  102. }
  103. dl_list_for_each(s, &wt->secret, struct wlantest_radius_secret, list) {
  104. int found = 0;
  105. keys = radius_msg_get_ms_keys(msg, r->last_req,
  106. (u8 *) s->secret,
  107. os_strlen(s->secret));
  108. if (keys && keys->send && keys->recv) {
  109. u8 pmk[PMK_LEN_MAX];
  110. size_t pmk_len, len2;
  111. wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
  112. keys->send, keys->send_len);
  113. wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
  114. keys->recv, keys->recv_len);
  115. pmk_len = keys->recv_len;
  116. if (pmk_len > PMK_LEN_MAX)
  117. pmk_len = PMK_LEN_MAX;
  118. os_memcpy(pmk, keys->recv, pmk_len);
  119. if (pmk_len < PMK_LEN_MAX) {
  120. len2 = keys->send_len;
  121. if (pmk_len + len2 > PMK_LEN_MAX)
  122. len2 = PMK_LEN_MAX - pmk_len;
  123. os_memcpy(pmk + pmk_len, keys->send, len2);
  124. pmk_len += len2;
  125. }
  126. wlantest_add_pmk(wt, pmk, pmk_len);
  127. found = 1;
  128. }
  129. if (keys) {
  130. os_free(keys->send);
  131. os_free(keys->recv);
  132. os_free(keys);
  133. }
  134. if (found)
  135. break;
  136. }
  137. radius_msg_free(msg);
  138. }
  139. static void process_radius(struct wlantest *wt, u32 dst, u16 dport, u32 src,
  140. u16 sport, const u8 *data, size_t len)
  141. {
  142. struct in_addr addr;
  143. char buf[20];
  144. const struct radius_hdr *hdr;
  145. u16 rlen;
  146. if (len < sizeof(*hdr))
  147. return;
  148. hdr = (const struct radius_hdr *) data;
  149. rlen = be_to_host16(hdr->length);
  150. if (len < rlen)
  151. return;
  152. if (len > rlen)
  153. len = rlen;
  154. addr.s_addr = dst;
  155. snprintf(buf, sizeof(buf), "%s", inet_ntoa(addr));
  156. addr.s_addr = src;
  157. wpa_printf(MSG_DEBUG, "RADIUS %s:%u -> %s:%u id=%u %s",
  158. inet_ntoa(addr), sport, buf, dport, hdr->identifier,
  159. radius_code_string(hdr->code));
  160. switch (hdr->code) {
  161. case RADIUS_CODE_ACCESS_REQUEST:
  162. process_radius_access_request(wt, dst, src, data, len);
  163. break;
  164. case RADIUS_CODE_ACCESS_ACCEPT:
  165. process_radius_access_accept(wt, dst, src, data, len);
  166. break;
  167. }
  168. }
  169. static void process_udp(struct wlantest *wt, u32 dst, u32 src,
  170. const u8 *data, size_t len)
  171. {
  172. const struct udphdr *udp;
  173. u16 sport, dport, ulen;
  174. const u8 *payload;
  175. size_t plen;
  176. if (len < sizeof(*udp))
  177. return;
  178. udp = (const struct udphdr *) data;
  179. /* TODO: check UDP checksum */
  180. sport = be_to_host16(udp->source);
  181. dport = be_to_host16(udp->dest);
  182. ulen = be_to_host16(udp->len);
  183. if (ulen > len)
  184. return;
  185. if (len < ulen)
  186. len = ulen;
  187. payload = (const u8 *) (udp + 1);
  188. plen = len - sizeof(*udp);
  189. if (sport == 1812 || dport == 1812)
  190. process_radius(wt, dst, dport, src, sport, payload, plen);
  191. }
  192. static void process_ipv4(struct wlantest *wt, const u8 *data, size_t len)
  193. {
  194. const struct iphdr *ip;
  195. const u8 *payload;
  196. size_t plen;
  197. u16 frag_off, tot_len;
  198. if (len < sizeof(*ip))
  199. return;
  200. ip = (const struct iphdr *) data;
  201. if (ip->version != 4)
  202. return;
  203. if (ip->ihl < 5)
  204. return;
  205. /* TODO: check header checksum in ip->check */
  206. frag_off = be_to_host16(ip->frag_off);
  207. if (frag_off & 0x1fff) {
  208. wpa_printf(MSG_EXCESSIVE, "IP fragment reassembly not yet "
  209. "supported");
  210. return;
  211. }
  212. tot_len = be_to_host16(ip->tot_len);
  213. if (tot_len > len)
  214. return;
  215. if (tot_len < len)
  216. len = tot_len;
  217. payload = data + 4 * ip->ihl;
  218. plen = len - 4 * ip->ihl;
  219. if (payload + plen > data + len)
  220. return;
  221. switch (ip->protocol) {
  222. case IPPROTO_UDP:
  223. process_udp(wt, ip->daddr, ip->saddr, payload, plen);
  224. break;
  225. }
  226. }
  227. void wlantest_process_wired(struct wlantest *wt, const u8 *data, size_t len)
  228. {
  229. const struct ether_header *eth;
  230. u16 ethertype;
  231. wpa_hexdump(MSG_EXCESSIVE, "Process wired frame", data, len);
  232. if (len < sizeof(*eth))
  233. return;
  234. eth = (const struct ether_header *) data;
  235. ethertype = be_to_host16(eth->ether_type);
  236. switch (ethertype) {
  237. case ETHERTYPE_IP:
  238. process_ipv4(wt, data + sizeof(*eth), len - sizeof(*eth));
  239. break;
  240. }
  241. }