radius.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /*
  2. * RADIUS message processing
  3. * Copyright (c) 2002-2009, 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 "utils/common.h"
  16. #include "utils/wpabuf.h"
  17. #include "crypto/md5.h"
  18. #include "crypto/crypto.h"
  19. #include "radius.h"
  20. /**
  21. * struct radius_msg - RADIUS message structure for new and parsed messages
  22. */
  23. struct radius_msg {
  24. /**
  25. * buf - Allocated buffer for RADIUS message
  26. */
  27. struct wpabuf *buf;
  28. /**
  29. * hdr - Pointer to the RADIUS header in buf
  30. */
  31. struct radius_hdr *hdr;
  32. /**
  33. * attr_pos - Array of indexes to attributes
  34. *
  35. * The values are number of bytes from buf to the beginning of
  36. * struct radius_attr_hdr.
  37. */
  38. size_t *attr_pos;
  39. /**
  40. * attr_size - Total size of the attribute pointer array
  41. */
  42. size_t attr_size;
  43. /**
  44. * attr_used - Total number of attributes in the array
  45. */
  46. size_t attr_used;
  47. };
  48. struct radius_hdr * radius_msg_get_hdr(struct radius_msg *msg)
  49. {
  50. return msg->hdr;
  51. }
  52. struct wpabuf * radius_msg_get_buf(struct radius_msg *msg)
  53. {
  54. return msg->buf;
  55. }
  56. static struct radius_attr_hdr *
  57. radius_get_attr_hdr(struct radius_msg *msg, int idx)
  58. {
  59. return (struct radius_attr_hdr *)
  60. (wpabuf_mhead_u8(msg->buf) + msg->attr_pos[idx]);
  61. }
  62. static void radius_msg_set_hdr(struct radius_msg *msg, u8 code, u8 identifier)
  63. {
  64. msg->hdr->code = code;
  65. msg->hdr->identifier = identifier;
  66. }
  67. static int radius_msg_initialize(struct radius_msg *msg)
  68. {
  69. msg->attr_pos =
  70. os_zalloc(RADIUS_DEFAULT_ATTR_COUNT * sizeof(*msg->attr_pos));
  71. if (msg->attr_pos == NULL)
  72. return -1;
  73. msg->attr_size = RADIUS_DEFAULT_ATTR_COUNT;
  74. msg->attr_used = 0;
  75. return 0;
  76. }
  77. /**
  78. * radius_msg_new - Create a new RADIUS message
  79. * @code: Code for RADIUS header
  80. * @identifier: Identifier for RADIUS header
  81. * Returns: Context for RADIUS message or %NULL on failure
  82. *
  83. * The caller is responsible for freeing the returned data with
  84. * radius_msg_free().
  85. */
  86. struct radius_msg * radius_msg_new(u8 code, u8 identifier)
  87. {
  88. struct radius_msg *msg;
  89. msg = os_zalloc(sizeof(*msg));
  90. if (msg == NULL)
  91. return NULL;
  92. msg->buf = wpabuf_alloc(RADIUS_DEFAULT_MSG_SIZE);
  93. if (msg->buf == NULL || radius_msg_initialize(msg)) {
  94. radius_msg_free(msg);
  95. return NULL;
  96. }
  97. msg->hdr = wpabuf_put(msg->buf, sizeof(struct radius_hdr));
  98. radius_msg_set_hdr(msg, code, identifier);
  99. return msg;
  100. }
  101. /**
  102. * radius_msg_free - Free a RADIUS message
  103. * @msg: RADIUS message from radius_msg_new() or radius_msg_parse()
  104. */
  105. void radius_msg_free(struct radius_msg *msg)
  106. {
  107. if (msg == NULL)
  108. return;
  109. wpabuf_free(msg->buf);
  110. os_free(msg->attr_pos);
  111. os_free(msg);
  112. }
  113. static const char *radius_code_string(u8 code)
  114. {
  115. switch (code) {
  116. case RADIUS_CODE_ACCESS_REQUEST: return "Access-Request";
  117. case RADIUS_CODE_ACCESS_ACCEPT: return "Access-Accept";
  118. case RADIUS_CODE_ACCESS_REJECT: return "Access-Reject";
  119. case RADIUS_CODE_ACCOUNTING_REQUEST: return "Accounting-Request";
  120. case RADIUS_CODE_ACCOUNTING_RESPONSE: return "Accounting-Response";
  121. case RADIUS_CODE_ACCESS_CHALLENGE: return "Access-Challenge";
  122. case RADIUS_CODE_STATUS_SERVER: return "Status-Server";
  123. case RADIUS_CODE_STATUS_CLIENT: return "Status-Client";
  124. case RADIUS_CODE_RESERVED: return "Reserved";
  125. default: return "?Unknown?";
  126. }
  127. }
  128. struct radius_attr_type {
  129. u8 type;
  130. char *name;
  131. enum {
  132. RADIUS_ATTR_UNDIST, RADIUS_ATTR_TEXT, RADIUS_ATTR_IP,
  133. RADIUS_ATTR_HEXDUMP, RADIUS_ATTR_INT32, RADIUS_ATTR_IPV6
  134. } data_type;
  135. };
  136. static struct radius_attr_type radius_attrs[] =
  137. {
  138. { RADIUS_ATTR_USER_NAME, "User-Name", RADIUS_ATTR_TEXT },
  139. { RADIUS_ATTR_USER_PASSWORD, "User-Password", RADIUS_ATTR_UNDIST },
  140. { RADIUS_ATTR_NAS_IP_ADDRESS, "NAS-IP-Address", RADIUS_ATTR_IP },
  141. { RADIUS_ATTR_NAS_PORT, "NAS-Port", RADIUS_ATTR_INT32 },
  142. { RADIUS_ATTR_FRAMED_MTU, "Framed-MTU", RADIUS_ATTR_INT32 },
  143. { RADIUS_ATTR_REPLY_MESSAGE, "Reply-Message", RADIUS_ATTR_TEXT },
  144. { RADIUS_ATTR_STATE, "State", RADIUS_ATTR_UNDIST },
  145. { RADIUS_ATTR_CLASS, "Class", RADIUS_ATTR_UNDIST },
  146. { RADIUS_ATTR_VENDOR_SPECIFIC, "Vendor-Specific", RADIUS_ATTR_UNDIST },
  147. { RADIUS_ATTR_SESSION_TIMEOUT, "Session-Timeout", RADIUS_ATTR_INT32 },
  148. { RADIUS_ATTR_IDLE_TIMEOUT, "Idle-Timeout", RADIUS_ATTR_INT32 },
  149. { RADIUS_ATTR_TERMINATION_ACTION, "Termination-Action",
  150. RADIUS_ATTR_INT32 },
  151. { RADIUS_ATTR_CALLED_STATION_ID, "Called-Station-Id",
  152. RADIUS_ATTR_TEXT },
  153. { RADIUS_ATTR_CALLING_STATION_ID, "Calling-Station-Id",
  154. RADIUS_ATTR_TEXT },
  155. { RADIUS_ATTR_NAS_IDENTIFIER, "NAS-Identifier", RADIUS_ATTR_TEXT },
  156. { RADIUS_ATTR_PROXY_STATE, "Proxy-State", RADIUS_ATTR_UNDIST },
  157. { RADIUS_ATTR_ACCT_STATUS_TYPE, "Acct-Status-Type",
  158. RADIUS_ATTR_INT32 },
  159. { RADIUS_ATTR_ACCT_DELAY_TIME, "Acct-Delay-Time", RADIUS_ATTR_INT32 },
  160. { RADIUS_ATTR_ACCT_INPUT_OCTETS, "Acct-Input-Octets",
  161. RADIUS_ATTR_INT32 },
  162. { RADIUS_ATTR_ACCT_OUTPUT_OCTETS, "Acct-Output-Octets",
  163. RADIUS_ATTR_INT32 },
  164. { RADIUS_ATTR_ACCT_SESSION_ID, "Acct-Session-Id", RADIUS_ATTR_TEXT },
  165. { RADIUS_ATTR_ACCT_AUTHENTIC, "Acct-Authentic", RADIUS_ATTR_INT32 },
  166. { RADIUS_ATTR_ACCT_SESSION_TIME, "Acct-Session-Time",
  167. RADIUS_ATTR_INT32 },
  168. { RADIUS_ATTR_ACCT_INPUT_PACKETS, "Acct-Input-Packets",
  169. RADIUS_ATTR_INT32 },
  170. { RADIUS_ATTR_ACCT_OUTPUT_PACKETS, "Acct-Output-Packets",
  171. RADIUS_ATTR_INT32 },
  172. { RADIUS_ATTR_ACCT_TERMINATE_CAUSE, "Acct-Terminate-Cause",
  173. RADIUS_ATTR_INT32 },
  174. { RADIUS_ATTR_ACCT_MULTI_SESSION_ID, "Acct-Multi-Session-Id",
  175. RADIUS_ATTR_TEXT },
  176. { RADIUS_ATTR_ACCT_LINK_COUNT, "Acct-Link-Count", RADIUS_ATTR_INT32 },
  177. { RADIUS_ATTR_ACCT_INPUT_GIGAWORDS, "Acct-Input-Gigawords",
  178. RADIUS_ATTR_INT32 },
  179. { RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS, "Acct-Output-Gigawords",
  180. RADIUS_ATTR_INT32 },
  181. { RADIUS_ATTR_EVENT_TIMESTAMP, "Event-Timestamp",
  182. RADIUS_ATTR_INT32 },
  183. { RADIUS_ATTR_NAS_PORT_TYPE, "NAS-Port-Type", RADIUS_ATTR_INT32 },
  184. { RADIUS_ATTR_TUNNEL_TYPE, "Tunnel-Type", RADIUS_ATTR_HEXDUMP },
  185. { RADIUS_ATTR_TUNNEL_MEDIUM_TYPE, "Tunnel-Medium-Type",
  186. RADIUS_ATTR_HEXDUMP },
  187. { RADIUS_ATTR_CONNECT_INFO, "Connect-Info", RADIUS_ATTR_TEXT },
  188. { RADIUS_ATTR_EAP_MESSAGE, "EAP-Message", RADIUS_ATTR_UNDIST },
  189. { RADIUS_ATTR_MESSAGE_AUTHENTICATOR, "Message-Authenticator",
  190. RADIUS_ATTR_UNDIST },
  191. { RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID, "Tunnel-Private-Group-Id",
  192. RADIUS_ATTR_HEXDUMP },
  193. { RADIUS_ATTR_ACCT_INTERIM_INTERVAL, "Acct-Interim-Interval",
  194. RADIUS_ATTR_INT32 },
  195. { RADIUS_ATTR_CHARGEABLE_USER_IDENTITY, "Chargable-User-Identity",
  196. RADIUS_ATTR_TEXT },
  197. { RADIUS_ATTR_NAS_IPV6_ADDRESS, "NAS-IPv6-Address", RADIUS_ATTR_IPV6 },
  198. };
  199. #define RADIUS_ATTRS (sizeof(radius_attrs) / sizeof(radius_attrs[0]))
  200. static struct radius_attr_type *radius_get_attr_type(u8 type)
  201. {
  202. size_t i;
  203. for (i = 0; i < RADIUS_ATTRS; i++) {
  204. if (type == radius_attrs[i].type)
  205. return &radius_attrs[i];
  206. }
  207. return NULL;
  208. }
  209. static void print_char(char c)
  210. {
  211. if (c >= 32 && c < 127)
  212. printf("%c", c);
  213. else
  214. printf("<%02x>", c);
  215. }
  216. static void radius_msg_dump_attr(struct radius_attr_hdr *hdr)
  217. {
  218. struct radius_attr_type *attr;
  219. int i, len;
  220. unsigned char *pos;
  221. attr = radius_get_attr_type(hdr->type);
  222. printf(" Attribute %d (%s) length=%d\n",
  223. hdr->type, attr ? attr->name : "?Unknown?", hdr->length);
  224. if (attr == NULL)
  225. return;
  226. len = hdr->length - sizeof(struct radius_attr_hdr);
  227. pos = (unsigned char *) (hdr + 1);
  228. switch (attr->data_type) {
  229. case RADIUS_ATTR_TEXT:
  230. printf(" Value: '");
  231. for (i = 0; i < len; i++)
  232. print_char(pos[i]);
  233. printf("'\n");
  234. break;
  235. case RADIUS_ATTR_IP:
  236. if (len == 4) {
  237. struct in_addr addr;
  238. os_memcpy(&addr, pos, 4);
  239. printf(" Value: %s\n", inet_ntoa(addr));
  240. } else
  241. printf(" Invalid IP address length %d\n", len);
  242. break;
  243. #ifdef CONFIG_IPV6
  244. case RADIUS_ATTR_IPV6:
  245. if (len == 16) {
  246. char buf[128];
  247. const char *atxt;
  248. struct in6_addr *addr = (struct in6_addr *) pos;
  249. atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
  250. printf(" Value: %s\n", atxt ? atxt : "?");
  251. } else
  252. printf(" Invalid IPv6 address length %d\n", len);
  253. break;
  254. #endif /* CONFIG_IPV6 */
  255. case RADIUS_ATTR_HEXDUMP:
  256. case RADIUS_ATTR_UNDIST:
  257. printf(" Value:");
  258. for (i = 0; i < len; i++)
  259. printf(" %02x", pos[i]);
  260. printf("\n");
  261. break;
  262. case RADIUS_ATTR_INT32:
  263. if (len == 4)
  264. printf(" Value: %u\n", WPA_GET_BE32(pos));
  265. else
  266. printf(" Invalid INT32 length %d\n", len);
  267. break;
  268. default:
  269. break;
  270. }
  271. }
  272. void radius_msg_dump(struct radius_msg *msg)
  273. {
  274. size_t i;
  275. printf("RADIUS message: code=%d (%s) identifier=%d length=%d\n",
  276. msg->hdr->code, radius_code_string(msg->hdr->code),
  277. msg->hdr->identifier, ntohs(msg->hdr->length));
  278. for (i = 0; i < msg->attr_used; i++) {
  279. struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
  280. radius_msg_dump_attr(attr);
  281. }
  282. }
  283. int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
  284. size_t secret_len)
  285. {
  286. if (secret) {
  287. u8 auth[MD5_MAC_LEN];
  288. struct radius_attr_hdr *attr;
  289. os_memset(auth, 0, MD5_MAC_LEN);
  290. attr = radius_msg_add_attr(msg,
  291. RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
  292. auth, MD5_MAC_LEN);
  293. if (attr == NULL) {
  294. wpa_printf(MSG_WARNING, "RADIUS: Could not add "
  295. "Message-Authenticator");
  296. return -1;
  297. }
  298. msg->hdr->length = htons(wpabuf_len(msg->buf));
  299. hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
  300. wpabuf_len(msg->buf), (u8 *) (attr + 1));
  301. } else
  302. msg->hdr->length = htons(wpabuf_len(msg->buf));
  303. if (wpabuf_len(msg->buf) > 0xffff) {
  304. wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
  305. (unsigned long) wpabuf_len(msg->buf));
  306. return -1;
  307. }
  308. return 0;
  309. }
  310. int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
  311. size_t secret_len, const u8 *req_authenticator)
  312. {
  313. u8 auth[MD5_MAC_LEN];
  314. struct radius_attr_hdr *attr;
  315. const u8 *addr[4];
  316. size_t len[4];
  317. os_memset(auth, 0, MD5_MAC_LEN);
  318. attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
  319. auth, MD5_MAC_LEN);
  320. if (attr == NULL) {
  321. printf("WARNING: Could not add Message-Authenticator\n");
  322. return -1;
  323. }
  324. msg->hdr->length = htons(wpabuf_len(msg->buf));
  325. os_memcpy(msg->hdr->authenticator, req_authenticator,
  326. sizeof(msg->hdr->authenticator));
  327. hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
  328. wpabuf_len(msg->buf), (u8 *) (attr + 1));
  329. /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
  330. addr[0] = (u8 *) msg->hdr;
  331. len[0] = 1 + 1 + 2;
  332. addr[1] = req_authenticator;
  333. len[1] = MD5_MAC_LEN;
  334. addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
  335. len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
  336. addr[3] = secret;
  337. len[3] = secret_len;
  338. md5_vector(4, addr, len, msg->hdr->authenticator);
  339. if (wpabuf_len(msg->buf) > 0xffff) {
  340. wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
  341. (unsigned long) wpabuf_len(msg->buf));
  342. return -1;
  343. }
  344. return 0;
  345. }
  346. void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret,
  347. size_t secret_len)
  348. {
  349. const u8 *addr[2];
  350. size_t len[2];
  351. msg->hdr->length = htons(wpabuf_len(msg->buf));
  352. os_memset(msg->hdr->authenticator, 0, MD5_MAC_LEN);
  353. addr[0] = wpabuf_head(msg->buf);
  354. len[0] = wpabuf_len(msg->buf);
  355. addr[1] = secret;
  356. len[1] = secret_len;
  357. md5_vector(2, addr, len, msg->hdr->authenticator);
  358. if (wpabuf_len(msg->buf) > 0xffff) {
  359. wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
  360. (unsigned long) wpabuf_len(msg->buf));
  361. }
  362. }
  363. static int radius_msg_add_attr_to_array(struct radius_msg *msg,
  364. struct radius_attr_hdr *attr)
  365. {
  366. if (msg->attr_used >= msg->attr_size) {
  367. size_t *nattr_pos;
  368. int nlen = msg->attr_size * 2;
  369. nattr_pos = os_realloc(msg->attr_pos,
  370. nlen * sizeof(*msg->attr_pos));
  371. if (nattr_pos == NULL)
  372. return -1;
  373. msg->attr_pos = nattr_pos;
  374. msg->attr_size = nlen;
  375. }
  376. msg->attr_pos[msg->attr_used++] =
  377. (unsigned char *) attr - wpabuf_head_u8(msg->buf);
  378. return 0;
  379. }
  380. struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type,
  381. const u8 *data, size_t data_len)
  382. {
  383. size_t buf_needed;
  384. struct radius_attr_hdr *attr;
  385. if (data_len > RADIUS_MAX_ATTR_LEN) {
  386. printf("radius_msg_add_attr: too long attribute (%lu bytes)\n",
  387. (unsigned long) data_len);
  388. return NULL;
  389. }
  390. buf_needed = sizeof(*attr) + data_len;
  391. if (wpabuf_tailroom(msg->buf) < buf_needed) {
  392. /* allocate more space for message buffer */
  393. if (wpabuf_resize(&msg->buf, buf_needed) < 0)
  394. return NULL;
  395. msg->hdr = wpabuf_mhead(msg->buf);
  396. }
  397. attr = wpabuf_put(msg->buf, sizeof(struct radius_attr_hdr));
  398. attr->type = type;
  399. attr->length = sizeof(*attr) + data_len;
  400. wpabuf_put_data(msg->buf, data, data_len);
  401. if (radius_msg_add_attr_to_array(msg, attr))
  402. return NULL;
  403. return attr;
  404. }
  405. /**
  406. * radius_msg_parse - Parse a RADIUS message
  407. * @data: RADIUS message to be parsed
  408. * @len: Length of data buffer in octets
  409. * Returns: Parsed RADIUS message or %NULL on failure
  410. *
  411. * This parses a RADIUS message and makes a copy of its data. The caller is
  412. * responsible for freeing the returned data with radius_msg_free().
  413. */
  414. struct radius_msg * radius_msg_parse(const u8 *data, size_t len)
  415. {
  416. struct radius_msg *msg;
  417. struct radius_hdr *hdr;
  418. struct radius_attr_hdr *attr;
  419. size_t msg_len;
  420. unsigned char *pos, *end;
  421. if (data == NULL || len < sizeof(*hdr))
  422. return NULL;
  423. hdr = (struct radius_hdr *) data;
  424. msg_len = ntohs(hdr->length);
  425. if (msg_len < sizeof(*hdr) || msg_len > len) {
  426. wpa_printf(MSG_INFO, "RADIUS: Invalid message length");
  427. return NULL;
  428. }
  429. if (msg_len < len) {
  430. wpa_printf(MSG_DEBUG, "RADIUS: Ignored %lu extra bytes after "
  431. "RADIUS message", (unsigned long) len - msg_len);
  432. }
  433. msg = os_zalloc(sizeof(*msg));
  434. if (msg == NULL)
  435. return NULL;
  436. msg->buf = wpabuf_alloc_copy(data, msg_len);
  437. if (msg->buf == NULL || radius_msg_initialize(msg)) {
  438. radius_msg_free(msg);
  439. return NULL;
  440. }
  441. msg->hdr = wpabuf_mhead(msg->buf);
  442. /* parse attributes */
  443. pos = wpabuf_mhead_u8(msg->buf) + sizeof(struct radius_hdr);
  444. end = wpabuf_mhead_u8(msg->buf) + wpabuf_len(msg->buf);
  445. while (pos < end) {
  446. if ((size_t) (end - pos) < sizeof(*attr))
  447. goto fail;
  448. attr = (struct radius_attr_hdr *) pos;
  449. if (pos + attr->length > end || attr->length < sizeof(*attr))
  450. goto fail;
  451. /* TODO: check that attr->length is suitable for attr->type */
  452. if (radius_msg_add_attr_to_array(msg, attr))
  453. goto fail;
  454. pos += attr->length;
  455. }
  456. return msg;
  457. fail:
  458. radius_msg_free(msg);
  459. return NULL;
  460. }
  461. int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
  462. {
  463. const u8 *pos = data;
  464. size_t left = data_len;
  465. while (left > 0) {
  466. int len;
  467. if (left > RADIUS_MAX_ATTR_LEN)
  468. len = RADIUS_MAX_ATTR_LEN;
  469. else
  470. len = left;
  471. if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
  472. pos, len))
  473. return 0;
  474. pos += len;
  475. left -= len;
  476. }
  477. return 1;
  478. }
  479. u8 *radius_msg_get_eap(struct radius_msg *msg, size_t *eap_len)
  480. {
  481. u8 *eap, *pos;
  482. size_t len, i;
  483. struct radius_attr_hdr *attr;
  484. if (msg == NULL)
  485. return NULL;
  486. len = 0;
  487. for (i = 0; i < msg->attr_used; i++) {
  488. attr = radius_get_attr_hdr(msg, i);
  489. if (attr->type == RADIUS_ATTR_EAP_MESSAGE)
  490. len += attr->length - sizeof(struct radius_attr_hdr);
  491. }
  492. if (len == 0)
  493. return NULL;
  494. eap = os_malloc(len);
  495. if (eap == NULL)
  496. return NULL;
  497. pos = eap;
  498. for (i = 0; i < msg->attr_used; i++) {
  499. attr = radius_get_attr_hdr(msg, i);
  500. if (attr->type == RADIUS_ATTR_EAP_MESSAGE) {
  501. int flen = attr->length - sizeof(*attr);
  502. os_memcpy(pos, attr + 1, flen);
  503. pos += flen;
  504. }
  505. }
  506. if (eap_len)
  507. *eap_len = len;
  508. return eap;
  509. }
  510. int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
  511. size_t secret_len, const u8 *req_auth)
  512. {
  513. u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
  514. u8 orig_authenticator[16];
  515. struct radius_attr_hdr *attr = NULL, *tmp;
  516. size_t i;
  517. for (i = 0; i < msg->attr_used; i++) {
  518. tmp = radius_get_attr_hdr(msg, i);
  519. if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
  520. if (attr != NULL) {
  521. printf("Multiple Message-Authenticator "
  522. "attributes in RADIUS message\n");
  523. return 1;
  524. }
  525. attr = tmp;
  526. }
  527. }
  528. if (attr == NULL) {
  529. printf("No Message-Authenticator attribute found\n");
  530. return 1;
  531. }
  532. os_memcpy(orig, attr + 1, MD5_MAC_LEN);
  533. os_memset(attr + 1, 0, MD5_MAC_LEN);
  534. if (req_auth) {
  535. os_memcpy(orig_authenticator, msg->hdr->authenticator,
  536. sizeof(orig_authenticator));
  537. os_memcpy(msg->hdr->authenticator, req_auth,
  538. sizeof(msg->hdr->authenticator));
  539. }
  540. hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
  541. wpabuf_len(msg->buf), auth);
  542. os_memcpy(attr + 1, orig, MD5_MAC_LEN);
  543. if (req_auth) {
  544. os_memcpy(msg->hdr->authenticator, orig_authenticator,
  545. sizeof(orig_authenticator));
  546. }
  547. if (os_memcmp(orig, auth, MD5_MAC_LEN) != 0) {
  548. printf("Invalid Message-Authenticator!\n");
  549. return 1;
  550. }
  551. return 0;
  552. }
  553. int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
  554. size_t secret_len, struct radius_msg *sent_msg, int auth)
  555. {
  556. const u8 *addr[4];
  557. size_t len[4];
  558. u8 hash[MD5_MAC_LEN];
  559. if (sent_msg == NULL) {
  560. printf("No matching Access-Request message found\n");
  561. return 1;
  562. }
  563. if (auth &&
  564. radius_msg_verify_msg_auth(msg, secret, secret_len,
  565. sent_msg->hdr->authenticator)) {
  566. return 1;
  567. }
  568. /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
  569. addr[0] = (u8 *) msg->hdr;
  570. len[0] = 1 + 1 + 2;
  571. addr[1] = sent_msg->hdr->authenticator;
  572. len[1] = MD5_MAC_LEN;
  573. addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
  574. len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
  575. addr[3] = secret;
  576. len[3] = secret_len;
  577. md5_vector(4, addr, len, hash);
  578. if (os_memcmp(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
  579. printf("Response Authenticator invalid!\n");
  580. return 1;
  581. }
  582. return 0;
  583. }
  584. int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
  585. u8 type)
  586. {
  587. struct radius_attr_hdr *attr;
  588. size_t i;
  589. int count = 0;
  590. for (i = 0; i < src->attr_used; i++) {
  591. attr = radius_get_attr_hdr(src, i);
  592. if (attr->type == type) {
  593. if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
  594. attr->length - sizeof(*attr)))
  595. return -1;
  596. count++;
  597. }
  598. }
  599. return count;
  600. }
  601. /* Create Request Authenticator. The value should be unique over the lifetime
  602. * of the shared secret between authenticator and authentication server.
  603. * Use one-way MD5 hash calculated from current timestamp and some data given
  604. * by the caller. */
  605. void radius_msg_make_authenticator(struct radius_msg *msg,
  606. const u8 *data, size_t len)
  607. {
  608. struct os_time tv;
  609. long int l;
  610. const u8 *addr[3];
  611. size_t elen[3];
  612. os_get_time(&tv);
  613. l = os_random();
  614. addr[0] = (u8 *) &tv;
  615. elen[0] = sizeof(tv);
  616. addr[1] = data;
  617. elen[1] = len;
  618. addr[2] = (u8 *) &l;
  619. elen[2] = sizeof(l);
  620. md5_vector(3, addr, elen, msg->hdr->authenticator);
  621. }
  622. /* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
  623. * Returns the Attribute payload and sets alen to indicate the length of the
  624. * payload if a vendor attribute with subtype is found, otherwise returns NULL.
  625. * The returned payload is allocated with os_malloc() and caller must free it
  626. * by calling os_free().
  627. */
  628. static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
  629. u8 subtype, size_t *alen)
  630. {
  631. u8 *data, *pos;
  632. size_t i, len;
  633. if (msg == NULL)
  634. return NULL;
  635. for (i = 0; i < msg->attr_used; i++) {
  636. struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
  637. size_t left;
  638. u32 vendor_id;
  639. struct radius_attr_vendor *vhdr;
  640. if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC)
  641. continue;
  642. left = attr->length - sizeof(*attr);
  643. if (left < 4)
  644. continue;
  645. pos = (u8 *) (attr + 1);
  646. os_memcpy(&vendor_id, pos, 4);
  647. pos += 4;
  648. left -= 4;
  649. if (ntohl(vendor_id) != vendor)
  650. continue;
  651. while (left >= sizeof(*vhdr)) {
  652. vhdr = (struct radius_attr_vendor *) pos;
  653. if (vhdr->vendor_length > left ||
  654. vhdr->vendor_length < sizeof(*vhdr)) {
  655. left = 0;
  656. break;
  657. }
  658. if (vhdr->vendor_type != subtype) {
  659. pos += vhdr->vendor_length;
  660. left -= vhdr->vendor_length;
  661. continue;
  662. }
  663. len = vhdr->vendor_length - sizeof(*vhdr);
  664. data = os_malloc(len);
  665. if (data == NULL)
  666. return NULL;
  667. os_memcpy(data, pos + sizeof(*vhdr), len);
  668. if (alen)
  669. *alen = len;
  670. return data;
  671. }
  672. }
  673. return NULL;
  674. }
  675. static u8 * decrypt_ms_key(const u8 *key, size_t len,
  676. const u8 *req_authenticator,
  677. const u8 *secret, size_t secret_len, size_t *reslen)
  678. {
  679. u8 *plain, *ppos, *res;
  680. const u8 *pos;
  681. size_t left, plen;
  682. u8 hash[MD5_MAC_LEN];
  683. int i, first = 1;
  684. const u8 *addr[3];
  685. size_t elen[3];
  686. /* key: 16-bit salt followed by encrypted key info */
  687. if (len < 2 + 16)
  688. return NULL;
  689. pos = key + 2;
  690. left = len - 2;
  691. if (left % 16) {
  692. printf("Invalid ms key len %lu\n", (unsigned long) left);
  693. return NULL;
  694. }
  695. plen = left;
  696. ppos = plain = os_malloc(plen);
  697. if (plain == NULL)
  698. return NULL;
  699. plain[0] = 0;
  700. while (left > 0) {
  701. /* b(1) = MD5(Secret + Request-Authenticator + Salt)
  702. * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
  703. addr[0] = secret;
  704. elen[0] = secret_len;
  705. if (first) {
  706. addr[1] = req_authenticator;
  707. elen[1] = MD5_MAC_LEN;
  708. addr[2] = key;
  709. elen[2] = 2; /* Salt */
  710. } else {
  711. addr[1] = pos - MD5_MAC_LEN;
  712. elen[1] = MD5_MAC_LEN;
  713. }
  714. md5_vector(first ? 3 : 2, addr, elen, hash);
  715. first = 0;
  716. for (i = 0; i < MD5_MAC_LEN; i++)
  717. *ppos++ = *pos++ ^ hash[i];
  718. left -= MD5_MAC_LEN;
  719. }
  720. if (plain[0] == 0 || plain[0] > plen - 1) {
  721. printf("Failed to decrypt MPPE key\n");
  722. os_free(plain);
  723. return NULL;
  724. }
  725. res = os_malloc(plain[0]);
  726. if (res == NULL) {
  727. os_free(plain);
  728. return NULL;
  729. }
  730. os_memcpy(res, plain + 1, plain[0]);
  731. if (reslen)
  732. *reslen = plain[0];
  733. os_free(plain);
  734. return res;
  735. }
  736. static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
  737. const u8 *req_authenticator,
  738. const u8 *secret, size_t secret_len,
  739. u8 *ebuf, size_t *elen)
  740. {
  741. int i, len, first = 1;
  742. u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
  743. const u8 *addr[3];
  744. size_t _len[3];
  745. WPA_PUT_BE16(saltbuf, salt);
  746. len = 1 + key_len;
  747. if (len & 0x0f) {
  748. len = (len & 0xf0) + 16;
  749. }
  750. os_memset(ebuf, 0, len);
  751. ebuf[0] = key_len;
  752. os_memcpy(ebuf + 1, key, key_len);
  753. *elen = len;
  754. pos = ebuf;
  755. while (len > 0) {
  756. /* b(1) = MD5(Secret + Request-Authenticator + Salt)
  757. * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
  758. addr[0] = secret;
  759. _len[0] = secret_len;
  760. if (first) {
  761. addr[1] = req_authenticator;
  762. _len[1] = MD5_MAC_LEN;
  763. addr[2] = saltbuf;
  764. _len[2] = sizeof(saltbuf);
  765. } else {
  766. addr[1] = pos - MD5_MAC_LEN;
  767. _len[1] = MD5_MAC_LEN;
  768. }
  769. md5_vector(first ? 3 : 2, addr, _len, hash);
  770. first = 0;
  771. for (i = 0; i < MD5_MAC_LEN; i++)
  772. *pos++ ^= hash[i];
  773. len -= MD5_MAC_LEN;
  774. }
  775. }
  776. struct radius_ms_mppe_keys *
  777. radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
  778. const u8 *secret, size_t secret_len)
  779. {
  780. u8 *key;
  781. size_t keylen;
  782. struct radius_ms_mppe_keys *keys;
  783. if (msg == NULL || sent_msg == NULL)
  784. return NULL;
  785. keys = os_zalloc(sizeof(*keys));
  786. if (keys == NULL)
  787. return NULL;
  788. key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
  789. RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
  790. &keylen);
  791. if (key) {
  792. keys->send = decrypt_ms_key(key, keylen,
  793. sent_msg->hdr->authenticator,
  794. secret, secret_len,
  795. &keys->send_len);
  796. os_free(key);
  797. }
  798. key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
  799. RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
  800. &keylen);
  801. if (key) {
  802. keys->recv = decrypt_ms_key(key, keylen,
  803. sent_msg->hdr->authenticator,
  804. secret, secret_len,
  805. &keys->recv_len);
  806. os_free(key);
  807. }
  808. return keys;
  809. }
  810. struct radius_ms_mppe_keys *
  811. radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
  812. const u8 *secret, size_t secret_len)
  813. {
  814. u8 *key;
  815. size_t keylen;
  816. struct radius_ms_mppe_keys *keys;
  817. if (msg == NULL || sent_msg == NULL)
  818. return NULL;
  819. keys = os_zalloc(sizeof(*keys));
  820. if (keys == NULL)
  821. return NULL;
  822. key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
  823. RADIUS_CISCO_AV_PAIR, &keylen);
  824. if (key && keylen == 51 &&
  825. os_memcmp(key, "leap:session-key=", 17) == 0) {
  826. keys->recv = decrypt_ms_key(key + 17, keylen - 17,
  827. sent_msg->hdr->authenticator,
  828. secret, secret_len,
  829. &keys->recv_len);
  830. }
  831. os_free(key);
  832. return keys;
  833. }
  834. int radius_msg_add_mppe_keys(struct radius_msg *msg,
  835. const u8 *req_authenticator,
  836. const u8 *secret, size_t secret_len,
  837. const u8 *send_key, size_t send_key_len,
  838. const u8 *recv_key, size_t recv_key_len)
  839. {
  840. struct radius_attr_hdr *attr;
  841. u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
  842. u8 *buf;
  843. struct radius_attr_vendor *vhdr;
  844. u8 *pos;
  845. size_t elen;
  846. int hlen;
  847. u16 salt;
  848. hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
  849. /* MS-MPPE-Send-Key */
  850. buf = os_malloc(hlen + send_key_len + 16);
  851. if (buf == NULL) {
  852. return 0;
  853. }
  854. pos = buf;
  855. os_memcpy(pos, &vendor_id, sizeof(vendor_id));
  856. pos += sizeof(vendor_id);
  857. vhdr = (struct radius_attr_vendor *) pos;
  858. vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
  859. pos = (u8 *) (vhdr + 1);
  860. salt = os_random() | 0x8000;
  861. WPA_PUT_BE16(pos, salt);
  862. pos += 2;
  863. encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
  864. secret_len, pos, &elen);
  865. vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
  866. attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
  867. buf, hlen + elen);
  868. os_free(buf);
  869. if (attr == NULL) {
  870. return 0;
  871. }
  872. /* MS-MPPE-Recv-Key */
  873. buf = os_malloc(hlen + send_key_len + 16);
  874. if (buf == NULL) {
  875. return 0;
  876. }
  877. pos = buf;
  878. os_memcpy(pos, &vendor_id, sizeof(vendor_id));
  879. pos += sizeof(vendor_id);
  880. vhdr = (struct radius_attr_vendor *) pos;
  881. vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
  882. pos = (u8 *) (vhdr + 1);
  883. salt ^= 1;
  884. WPA_PUT_BE16(pos, salt);
  885. pos += 2;
  886. encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
  887. secret_len, pos, &elen);
  888. vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
  889. attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
  890. buf, hlen + elen);
  891. os_free(buf);
  892. if (attr == NULL) {
  893. return 0;
  894. }
  895. return 1;
  896. }
  897. /* Add User-Password attribute to a RADIUS message and encrypt it as specified
  898. * in RFC 2865, Chap. 5.2 */
  899. struct radius_attr_hdr *
  900. radius_msg_add_attr_user_password(struct radius_msg *msg,
  901. const u8 *data, size_t data_len,
  902. const u8 *secret, size_t secret_len)
  903. {
  904. u8 buf[128];
  905. int padlen, i;
  906. size_t buf_len, pos;
  907. const u8 *addr[2];
  908. size_t len[2];
  909. u8 hash[16];
  910. if (data_len > 128)
  911. return NULL;
  912. os_memcpy(buf, data, data_len);
  913. buf_len = data_len;
  914. padlen = data_len % 16;
  915. if (padlen) {
  916. padlen = 16 - padlen;
  917. os_memset(buf + data_len, 0, padlen);
  918. buf_len += padlen;
  919. }
  920. addr[0] = secret;
  921. len[0] = secret_len;
  922. addr[1] = msg->hdr->authenticator;
  923. len[1] = 16;
  924. md5_vector(2, addr, len, hash);
  925. for (i = 0; i < 16; i++)
  926. buf[i] ^= hash[i];
  927. pos = 16;
  928. while (pos < buf_len) {
  929. addr[0] = secret;
  930. len[0] = secret_len;
  931. addr[1] = &buf[pos - 16];
  932. len[1] = 16;
  933. md5_vector(2, addr, len, hash);
  934. for (i = 0; i < 16; i++)
  935. buf[pos + i] ^= hash[i];
  936. pos += 16;
  937. }
  938. return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
  939. buf, buf_len);
  940. }
  941. int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
  942. {
  943. struct radius_attr_hdr *attr = NULL, *tmp;
  944. size_t i, dlen;
  945. for (i = 0; i < msg->attr_used; i++) {
  946. tmp = radius_get_attr_hdr(msg, i);
  947. if (tmp->type == type) {
  948. attr = tmp;
  949. break;
  950. }
  951. }
  952. if (!attr)
  953. return -1;
  954. dlen = attr->length - sizeof(*attr);
  955. if (buf)
  956. os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
  957. return dlen;
  958. }
  959. int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
  960. size_t *len, const u8 *start)
  961. {
  962. size_t i;
  963. struct radius_attr_hdr *attr = NULL, *tmp;
  964. for (i = 0; i < msg->attr_used; i++) {
  965. tmp = radius_get_attr_hdr(msg, i);
  966. if (tmp->type == type &&
  967. (start == NULL || (u8 *) tmp > start)) {
  968. attr = tmp;
  969. break;
  970. }
  971. }
  972. if (!attr)
  973. return -1;
  974. *buf = (u8 *) (attr + 1);
  975. *len = attr->length - sizeof(*attr);
  976. return 0;
  977. }
  978. int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
  979. {
  980. size_t i;
  981. int count;
  982. for (count = 0, i = 0; i < msg->attr_used; i++) {
  983. struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
  984. if (attr->type == type &&
  985. attr->length >= sizeof(struct radius_attr_hdr) + min_len)
  986. count++;
  987. }
  988. return count;
  989. }
  990. struct radius_tunnel_attrs {
  991. int tag_used;
  992. int type; /* Tunnel-Type */
  993. int medium_type; /* Tunnel-Medium-Type */
  994. int vlanid;
  995. };
  996. /**
  997. * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
  998. * @msg: RADIUS message
  999. * Returns: VLAN ID for the first tunnel configuration of -1 if none is found
  1000. */
  1001. int radius_msg_get_vlanid(struct radius_msg *msg)
  1002. {
  1003. struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
  1004. size_t i;
  1005. struct radius_attr_hdr *attr = NULL;
  1006. const u8 *data;
  1007. char buf[10];
  1008. size_t dlen;
  1009. os_memset(&tunnel, 0, sizeof(tunnel));
  1010. for (i = 0; i < msg->attr_used; i++) {
  1011. attr = radius_get_attr_hdr(msg, i);
  1012. data = (const u8 *) (attr + 1);
  1013. dlen = attr->length - sizeof(*attr);
  1014. if (attr->length < 3)
  1015. continue;
  1016. if (data[0] >= RADIUS_TUNNEL_TAGS)
  1017. tun = &tunnel[0];
  1018. else
  1019. tun = &tunnel[data[0]];
  1020. switch (attr->type) {
  1021. case RADIUS_ATTR_TUNNEL_TYPE:
  1022. if (attr->length != 6)
  1023. break;
  1024. tun->tag_used++;
  1025. tun->type = WPA_GET_BE24(data + 1);
  1026. break;
  1027. case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
  1028. if (attr->length != 6)
  1029. break;
  1030. tun->tag_used++;
  1031. tun->medium_type = WPA_GET_BE24(data + 1);
  1032. break;
  1033. case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
  1034. if (data[0] < RADIUS_TUNNEL_TAGS) {
  1035. data++;
  1036. dlen--;
  1037. }
  1038. if (dlen >= sizeof(buf))
  1039. break;
  1040. os_memcpy(buf, data, dlen);
  1041. buf[dlen] = '\0';
  1042. tun->tag_used++;
  1043. tun->vlanid = atoi(buf);
  1044. break;
  1045. }
  1046. }
  1047. for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
  1048. tun = &tunnel[i];
  1049. if (tun->tag_used &&
  1050. tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
  1051. tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&
  1052. tun->vlanid > 0)
  1053. return tun->vlanid;
  1054. }
  1055. return -1;
  1056. }
  1057. void radius_free_class(struct radius_class_data *c)
  1058. {
  1059. size_t i;
  1060. if (c == NULL)
  1061. return;
  1062. for (i = 0; i < c->count; i++)
  1063. os_free(c->attr[i].data);
  1064. os_free(c->attr);
  1065. c->attr = NULL;
  1066. c->count = 0;
  1067. }
  1068. int radius_copy_class(struct radius_class_data *dst,
  1069. const struct radius_class_data *src)
  1070. {
  1071. size_t i;
  1072. if (src->attr == NULL)
  1073. return 0;
  1074. dst->attr = os_zalloc(src->count * sizeof(struct radius_attr_data));
  1075. if (dst->attr == NULL)
  1076. return -1;
  1077. dst->count = 0;
  1078. for (i = 0; i < src->count; i++) {
  1079. dst->attr[i].data = os_malloc(src->attr[i].len);
  1080. if (dst->attr[i].data == NULL)
  1081. break;
  1082. dst->count++;
  1083. os_memcpy(dst->attr[i].data, src->attr[i].data,
  1084. src->attr[i].len);
  1085. dst->attr[i].len = src->attr[i].len;
  1086. }
  1087. return 0;
  1088. }