wired.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Received frame processing for wired interface
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include <net/ethernet.h>
  16. #include <netinet/ip.h>
  17. #include <netinet/udp.h>
  18. #include "utils/common.h"
  19. #include "radius/radius.h"
  20. #include "wlantest.h"
  21. static const char * radius_code_string(u8 code)
  22. {
  23. switch (code) {
  24. case RADIUS_CODE_ACCESS_REQUEST:
  25. return "Access-Request";
  26. case RADIUS_CODE_ACCESS_ACCEPT:
  27. return "Access-Accept";
  28. case RADIUS_CODE_ACCESS_REJECT:
  29. return "Access-Reject";
  30. case RADIUS_CODE_ACCOUNTING_REQUEST:
  31. return "Accounting-Request";
  32. case RADIUS_CODE_ACCOUNTING_RESPONSE:
  33. return "Accounting-Response";
  34. case RADIUS_CODE_ACCESS_CHALLENGE:
  35. return "Access-Challenge";
  36. case RADIUS_CODE_STATUS_SERVER:
  37. return "Status-Server";
  38. case RADIUS_CODE_STATUS_CLIENT:
  39. return "Status-Client";
  40. case RADIUS_CODE_RESERVED:
  41. return "Reserved";
  42. default:
  43. return "?Unknown?";
  44. }
  45. }
  46. static void process_radius_access_request(struct wlantest *wt, u32 dst,
  47. u32 src, const u8 *data, size_t len)
  48. {
  49. struct radius_msg *msg;
  50. msg = radius_msg_parse(data, len);
  51. if (msg == NULL) {
  52. wpa_printf(MSG_DEBUG, "Failed to parse RADIUS Access-Request");
  53. return;
  54. }
  55. radius_msg_free(msg);
  56. }
  57. static void process_radius_access_accept(struct wlantest *wt, u32 dst, u32 src,
  58. const u8 *data, size_t len)
  59. {
  60. struct radius_msg *msg;
  61. msg = radius_msg_parse(data, len);
  62. if (msg == NULL) {
  63. wpa_printf(MSG_DEBUG, "Failed to parse RADIUS Access-Accept");
  64. return;
  65. }
  66. radius_msg_free(msg);
  67. }
  68. static void process_radius(struct wlantest *wt, u32 dst, u16 dport, u32 src,
  69. u16 sport, const u8 *data, size_t len)
  70. {
  71. struct in_addr addr;
  72. char buf[20];
  73. const struct radius_hdr *hdr;
  74. u16 rlen;
  75. if (len < sizeof(*hdr))
  76. return;
  77. hdr = (const struct radius_hdr *) data;
  78. rlen = be_to_host16(hdr->length);
  79. if (len < rlen)
  80. return;
  81. if (len > rlen)
  82. len = rlen;
  83. addr.s_addr = dst;
  84. snprintf(buf, sizeof(buf), "%s", inet_ntoa(addr));
  85. addr.s_addr = src;
  86. wpa_printf(MSG_DEBUG, "RADIUS %s:%u -> %s:%u id=%u %s",
  87. inet_ntoa(addr), sport, buf, dport, hdr->identifier,
  88. radius_code_string(hdr->code));
  89. switch (hdr->code) {
  90. case RADIUS_CODE_ACCESS_REQUEST:
  91. process_radius_access_request(wt, dst, src, data, len);
  92. break;
  93. case RADIUS_CODE_ACCESS_ACCEPT:
  94. process_radius_access_accept(wt, dst, src, data, len);
  95. break;
  96. }
  97. }
  98. static void process_udp(struct wlantest *wt, u32 dst, u32 src,
  99. const u8 *data, size_t len)
  100. {
  101. const struct udphdr *udp;
  102. u16 sport, dport, ulen;
  103. const u8 *payload;
  104. size_t plen;
  105. if (len < sizeof(*udp))
  106. return;
  107. udp = (const struct udphdr *) data;
  108. /* TODO: check UDP checksum */
  109. sport = be_to_host16(udp->source);
  110. dport = be_to_host16(udp->dest);
  111. ulen = be_to_host16(udp->len);
  112. if (ulen > len)
  113. return;
  114. if (len < ulen)
  115. len = ulen;
  116. payload = (const u8 *) (udp + 1);
  117. plen = len - sizeof(*udp);
  118. if (sport == 1812 || dport == 1812)
  119. process_radius(wt, dst, dport, src, sport, payload, plen);
  120. }
  121. static void process_ipv4(struct wlantest *wt, const u8 *data, size_t len)
  122. {
  123. const struct iphdr *ip;
  124. const u8 *payload;
  125. size_t plen;
  126. u16 frag_off, tot_len;
  127. if (len < sizeof(*ip))
  128. return;
  129. ip = (const struct iphdr *) data;
  130. if (ip->version != 4)
  131. return;
  132. if (ip->ihl < 5)
  133. return;
  134. /* TODO: check header checksum in ip->check */
  135. frag_off = be_to_host16(ip->frag_off);
  136. if (frag_off & 0x1fff) {
  137. wpa_printf(MSG_EXCESSIVE, "IP fragment reassembly not yet "
  138. "supported");
  139. return;
  140. }
  141. tot_len = be_to_host16(ip->tot_len);
  142. if (tot_len > len)
  143. return;
  144. if (tot_len < len)
  145. len = tot_len;
  146. payload = data + 4 * ip->ihl;
  147. plen = len - 4 * ip->ihl;
  148. if (payload + plen > data + len)
  149. return;
  150. switch (ip->protocol) {
  151. case IPPROTO_UDP:
  152. process_udp(wt, ip->daddr, ip->saddr, payload, plen);
  153. break;
  154. }
  155. }
  156. void wlantest_process_wired(struct wlantest *wt, const u8 *data, size_t len)
  157. {
  158. const struct ether_header *eth;
  159. u16 ethertype;
  160. wpa_hexdump(MSG_EXCESSIVE, "Process wired frame", data, len);
  161. if (len < sizeof(*eth))
  162. return;
  163. eth = (const struct ether_header *) data;
  164. ethertype = be_to_host16(eth->ether_type);
  165. switch (ethertype) {
  166. case ETHERTYPE_IP:
  167. process_ipv4(wt, data + sizeof(*eth), len - sizeof(*eth));
  168. break;
  169. }
  170. }