eap.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ip.h>
  4. #include "dat.h"
  5. #include "protos.h"
  6. typedef struct Hdr Hdr;
  7. struct Hdr
  8. {
  9. uchar code;
  10. uchar id;
  11. uchar len[2]; /* length including this header */
  12. uchar tp; /* optional, only for Request/Response */
  13. };
  14. enum
  15. {
  16. EAPHDR= 4, /* sizeof(code)+sizeof(id)+sizeof(len) */
  17. TPHDR= 1, /* sizeof(tp) */
  18. /* eap types */
  19. Request = 1,
  20. Response,
  21. Success,
  22. Fail,
  23. /* eap request/response sub-types */
  24. Identity = 1, /* Identity */
  25. Notify, /* Notification */
  26. Nak, /* Nak (Response only) */
  27. Md5, /* MD5-challenge */
  28. Otp, /* one time password */
  29. Gtc, /* generic token card */
  30. Ttls = 21, /* tunneled TLS */
  31. Xpnd = 254, /* expanded types */
  32. Xprm, /* experimental use */
  33. };
  34. enum
  35. {
  36. Ot,
  37. };
  38. static Mux p_mux[] =
  39. {
  40. { "eap_identity", Identity, },
  41. { "eap_notify", Notify, },
  42. { "eap_nak", Nak, },
  43. { "eap_md5", Md5, },
  44. { "eap_otp", Otp, },
  45. { "eap_gtc", Gtc, },
  46. { "ttls", Ttls, },
  47. { "eap_xpnd", Xpnd, },
  48. { "eap_xprm", Xprm, },
  49. { 0 }
  50. };
  51. static char *eapsubtype[256] =
  52. {
  53. [Identity] "Identity",
  54. [Notify] "Notify",
  55. [Nak] "Nak",
  56. [Md5] "Md5",
  57. [Otp] "Otp",
  58. [Gtc] "Gtc",
  59. [Ttls] "Ttls",
  60. [Xpnd] "Xpnd",
  61. [Xprm] "Xprm",
  62. };
  63. static void
  64. p_compile(Filter *f)
  65. {
  66. Mux *m;
  67. for(m = p_mux; m->name != nil; m++)
  68. if(strcmp(f->s, m->name) == 0){
  69. f->pr = m->pr;
  70. f->ulv = m->val;
  71. f->subop = Ot;
  72. return;
  73. }
  74. sysfatal("unknown eap field or type: %s", f->s);
  75. }
  76. static int
  77. p_filter(Filter *f, Msg *m)
  78. {
  79. Hdr *h;
  80. int len;
  81. if(f->subop != Ot)
  82. return 0;
  83. if(m->pe - m->ps < EAPHDR)
  84. return -1;
  85. h = (Hdr*)m->ps;
  86. /* truncate the message if there's extra */
  87. /* len includes header */
  88. len = NetS(h->len);
  89. if(m->ps+len < m->pe)
  90. m->pe = m->ps+len;
  91. else if(m->ps+len > m->pe)
  92. return -1;
  93. m->ps += EAPHDR;
  94. if(h->code != Request && h->code != Response)
  95. return 0;
  96. m->ps += TPHDR;
  97. if(h->tp == f->ulv)
  98. return 1;
  99. return 0;
  100. }
  101. static char*
  102. op(int i)
  103. {
  104. static char x[20];
  105. switch(i){
  106. case Request:
  107. return "Request";
  108. case Response:
  109. return "Response";
  110. case Success:
  111. return "Success";
  112. case Fail:
  113. return "Fail";
  114. default:
  115. sprint(x, "%1d", i);
  116. return x;
  117. }
  118. }
  119. static char*
  120. subop(uchar val)
  121. {
  122. static char x[20], *p;
  123. p = eapsubtype[val];
  124. if(p != nil)
  125. return p;
  126. else {
  127. sprint(x, "%1d", val);
  128. return x;
  129. }
  130. }
  131. static int
  132. p_seprint(Msg *m)
  133. {
  134. Hdr *h;
  135. int len;
  136. char *p, *e;
  137. if(m->pe - m->ps < EAPHDR)
  138. return -1;
  139. p = m->p;
  140. e = m->e;
  141. h = (Hdr*)m->ps;
  142. /* resize packet (should already be done by eapol) */
  143. /* len includes header */
  144. len = NetS(h->len);
  145. if(m->ps+len < m->pe)
  146. m->pe = m->ps+len;
  147. else if(m->ps+len > m->pe)
  148. return -1;
  149. m->ps += EAPHDR;
  150. p = seprint(p, e, "id=%1d code=%s", h->id, op(h->code));
  151. switch(h->code) {
  152. case Request:
  153. case Response:
  154. m->ps += TPHDR;
  155. p = seprint(p, e, " type=%s", subop(h->tp));
  156. /* special case needed to print eap_notify notification as unicode */
  157. demux(p_mux, h->tp, h->tp, m, &dump);
  158. break;
  159. default:
  160. demux(p_mux, 0, 0, m, &dump);
  161. break;
  162. }
  163. m->p = seprint(p, e, " len=%1d", len);
  164. return 0;
  165. }
  166. static int
  167. p_seprintidentity(Msg *m)
  168. {
  169. char *ps, *pe, *z;
  170. int len;
  171. m->pr = nil;
  172. ps = (char*)m->ps;
  173. pe = (char*)m->pe;
  174. /* we would like to do this depending on the 'context':
  175. * - one for eap_identity request and
  176. * - one for eap_identity response
  177. * but we've lost the context, or haven't we?
  178. * so we treat them the same, so we might erroneously
  179. * print a response as if it was a request. too bad. - axel
  180. */
  181. for (z=ps; *z != '\0' && z+1 < pe; z++)
  182. ;
  183. if (*z == '\0' && z+1 < pe) {
  184. m->p = seprint(m->p, m->e, "prompt=(%s)", ps);
  185. len = pe - (z+1);
  186. m->p = seprint(m->p, m->e, " options=(%.*s)", len, z+1);
  187. } else {
  188. len = pe - ps;
  189. m->p = seprint(m->p, m->e, "%.*s", len, ps);
  190. }
  191. return 0;
  192. }
  193. Proto eap =
  194. {
  195. "eap",
  196. p_compile,
  197. p_filter,
  198. p_seprint,
  199. p_mux,
  200. "%lud",
  201. nil,
  202. defaultframer,
  203. };
  204. Proto eap_identity =
  205. {
  206. "eap_identity",
  207. p_compile,
  208. p_filter,
  209. p_seprintidentity,
  210. nil,
  211. nil,
  212. nil,
  213. defaultframer,
  214. };