ser_stun.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * History
  28. * --------
  29. * 2006-10-13 created (vlada)
  30. * 2006-12-14 fixed calculation of body length (vlada)
  31. */
  32. #ifdef USE_STUN
  33. #include <arpa/inet.h>
  34. #include <openssl/sha.h>
  35. #include "ser_stun.h"
  36. #include "forward.h"
  37. /*
  38. * ****************************************************************************
  39. * Declaration of functions *
  40. * ****************************************************************************
  41. */
  42. int stun_parse_header(struct stun_msg* req, USHORT_T* error_code);
  43. int stun_parse_body(
  44. struct stun_msg* req,
  45. struct stun_unknown_att** unknown,
  46. USHORT_T* error_code);
  47. void stun_delete_unknown_attrs(struct stun_unknown_att* unknown);
  48. struct stun_unknown_att* stun_alloc_unknown_attr(USHORT_T type);
  49. int stun_add_address_attr(struct stun_msg* res,
  50. UINT_T af,
  51. USHORT_T port,
  52. UINT_T* ip_addr,
  53. USHORT_T type,
  54. int do_xor);
  55. int add_unknown_attr(struct stun_msg* res, struct stun_unknown_att* unknown);
  56. int add_error_code(struct stun_msg* res, USHORT_T error_code);
  57. int add_fingerprint(struct stun_buffer* msg);
  58. int copy_str_to_buffer(struct stun_msg* res, const char* data, UINT_T pad);
  59. int validate_fingerprint(struct stun_msg* req, USHORT_T* error_code);
  60. int reallock_buffer(struct stun_buffer* buffer, UINT_T len);
  61. int buf_copy(struct stun_buffer* msg, void* source, UINT_T len);
  62. void clean_memory(struct stun_msg* req,
  63. struct stun_msg* res, struct stun_unknown_att* unknown);
  64. int stun_create_response(
  65. struct stun_msg* req,
  66. struct stun_msg* res,
  67. struct receive_info* ri,
  68. struct stun_unknown_att* unknown,
  69. UINT_T error_code);
  70. int stun_add_common_integer_attr(struct stun_msg* res, USHORT_T type, UINT_T value);
  71. int stun_add_common_text_attr(struct stun_msg* res, USHORT_T type, char* value,
  72. USHORT_T pad);
  73. /*
  74. * ****************************************************************************
  75. * Definition of functions *
  76. * ****************************************************************************
  77. */
  78. /*
  79. * stun_process_msg():
  80. * buf - incoming message
  81. * len - length of incoming message
  82. * ri - information about socket that received a message and
  83. * also information about sender (its IP, port, protocol)
  84. *
  85. * This function ensures processing of incoming message. It's common for both
  86. * TCP and UDP protocol. There is no other function as an interface.
  87. *
  88. * Return value: 0 if there is no environment error
  89. * -1 if there is some enviroment error such as insufficiency
  90. * of memory
  91. *
  92. */
  93. int stun_process_msg(char* buf, unsigned len, struct receive_info* ri)
  94. {
  95. struct stun_msg msg_req;
  96. struct stun_msg msg_res;
  97. struct dest_info dst;
  98. struct stun_unknown_att* unknown;
  99. USHORT_T error_code;
  100. memset(&msg_req, 0, sizeof(msg_req));
  101. memset(&msg_res, 0, sizeof(msg_res));
  102. msg_req.msg.buf.s = buf;
  103. msg_req.msg.buf.len = len;
  104. unknown = NULL;
  105. error_code = RESPONSE_OK;
  106. if (stun_parse_header(&msg_req, &error_code) != 0) {
  107. goto error;
  108. }
  109. if (error_code == RESPONSE_OK) {
  110. if (stun_parse_body(&msg_req, &unknown, &error_code) != 0) {
  111. goto error;
  112. }
  113. }
  114. if (stun_create_response(&msg_req, &msg_res, ri,
  115. unknown, error_code) != 0) {
  116. goto error;
  117. }
  118. init_dst_from_rcv(&dst, ri);
  119. #ifdef EXTRA_DEBUG
  120. struct ip_addr ip;
  121. su2ip_addr(&ip, &dst.to);
  122. LOG(L_DBG, "DEBUG: stun_process_msg: decoded request from (%s:%d)\n", ip_addr2a(&ip),
  123. su_getport(&dst.to));
  124. #endif
  125. /* send STUN response */
  126. if (msg_send(&dst, msg_res.msg.buf.s, msg_res.msg.buf.len) != 0) {
  127. goto error;
  128. }
  129. #ifdef EXTRA_DEBUG
  130. LOG(L_DBG, "DEBUG: stun_process_msg: send response\n");
  131. #endif
  132. clean_memory(&msg_req, &msg_res, unknown);
  133. return 0;
  134. error:
  135. #ifdef EXTRA_DEBUG
  136. LOG(L_DBG, "DEBUG: stun_process_msg: failed to decode request\n");
  137. #endif
  138. clean_memory(&msg_req, &msg_res, unknown);
  139. return FATAL_ERROR;
  140. }
  141. /*
  142. * stun_parse_header():
  143. * - req: request from host that should be processed
  144. * - error_code: indication of any protocol error
  145. *
  146. * This function ensures parsing of incoming header.
  147. *
  148. * Return value: 0 if there is no environment error
  149. * -1 if there is some enviroment error such as insufficiency
  150. * of memory
  151. */
  152. int stun_parse_header(struct stun_msg* req, USHORT_T* error_code)
  153. {
  154. if (sizeof(req->hdr) > req->msg.buf.len) {
  155. /* the received message does not contain whole header */
  156. LOG(L_INFO, "INFO: stun_parse_header: incomplete header of STUN message\n");
  157. /* Any better solution? IMHO it's not possible to send error response
  158. * because the transaction ID is not available.
  159. */
  160. return FATAL_ERROR;
  161. }
  162. memcpy(&req->hdr, req->msg.buf.s, sizeof(struct stun_hdr));
  163. req->hdr.type = ntohs(req->hdr.type);
  164. /* the SER supports only Binding Request right now */
  165. if (req->hdr.type != BINDING_REQUEST) {
  166. LOG(L_INFO, "INFO: stun_parse_header: unsupported type of STUN message: %x\n",
  167. req->hdr.type);
  168. /* resending of same message is not welcome */
  169. *error_code = GLOBAL_FAILURE_ERR;
  170. }
  171. req->hdr.len = ntohs(req->hdr.len);
  172. /* check if there is correct magic cookie */
  173. req->old = (req->hdr.id.magic_cookie == htonl(MAGIC_COOKIE)) ? 0 : 1;
  174. #ifdef EXTRA_DEBUG
  175. LOG(L_DBG, "DEBUG: stun_parse_header: request is old: %i\n", req->old);
  176. #endif
  177. return 0;
  178. }
  179. /*
  180. * stun_parse_body():
  181. * - req: request from host that should be processed
  182. * - unknown: this is a link list header of attributes
  183. * that are unknown to SER; defaul value is NULL
  184. * - error_code: indication of any protocol error
  185. *
  186. * Return value: 0 if there is no environment error
  187. * -1 if there is some enviroment error such as insufficiency
  188. * of memory
  189. */
  190. int stun_parse_body(
  191. struct stun_msg* req,
  192. struct stun_unknown_att** unknown,
  193. USHORT_T* error_code)
  194. {
  195. UINT_T not_parsed;
  196. struct stun_attr attr;
  197. USHORT_T attr_size;
  198. UINT_T padded_len;
  199. struct stun_unknown_att* tmp_unknown;
  200. struct stun_unknown_att* body;
  201. char* buf;
  202. int fp_present;
  203. attr_size = sizeof(struct stun_attr);
  204. buf = &req->msg.buf.s[sizeof(struct stun_hdr)];
  205. fp_present = 0;
  206. /*
  207. * Mark the body lenght as unparsed.
  208. */
  209. not_parsed = req->msg.buf.len - sizeof(struct stun_hdr);
  210. if (not_parsed != req->hdr.len) {
  211. #ifdef EXTRA_DEBUG
  212. LOG(L_DBG, "DEBUG: stun_parse_body: body too short to be valid\n");
  213. #endif
  214. *error_code = BAD_REQUEST_ERR;
  215. return 0;
  216. }
  217. tmp_unknown = *unknown;
  218. body = NULL;
  219. while (not_parsed > 0 && *error_code == RESPONSE_OK) {
  220. memset(&attr, 0, attr_size);
  221. /* check if there are 4 bytes for attribute type and its value */
  222. if (not_parsed < 4) {
  223. #ifdef EXTRA_DEBUG
  224. LOG(L_DBG, "DEBUG: stun_parse_body: attribute header short to be valid\n");
  225. #endif
  226. *error_code = BAD_REQUEST_ERR;
  227. continue;
  228. }
  229. memcpy(&attr, buf, attr_size);
  230. buf += attr_size;
  231. not_parsed -= attr_size;
  232. /* check if there is enought unparsed space for attribute's value */
  233. if (not_parsed < ntohs(attr.len)) {
  234. #ifdef EXTRA_DEBUG
  235. LOG(L_DBG, "DEBUG: stun_parse_body: remaining message is shorter then attribute length\n");
  236. #endif
  237. *error_code = BAD_REQUEST_ERR;
  238. continue;
  239. }
  240. /* check if the attribute is known to the server */
  241. switch (ntohs(attr.type)) {
  242. case REALM_ATTR:
  243. case NONCE_ATTR:
  244. case MAPPED_ADDRESS_ATTR:
  245. case XOR_MAPPED_ADDRESS_ATTR:
  246. case ALTERNATE_SERVER_ATTR:
  247. case REFRESH_INTERVAL_ATTR:
  248. case RESPONSE_ADDRESS_ATTR:
  249. case SOURCE_ADDRESS_ATTR:
  250. case REFLECTED_FROM_ATTR:
  251. case CHANGE_REQUEST_ATTR:
  252. case CHANGED_ADDRESS_ATTR:
  253. padded_len = ntohs(attr.len);
  254. #ifdef EXTRA_DEBUG
  255. LOG(L_DBG, "DEBUG: stun_parse_body: known attributes\n");
  256. #endif
  257. break;
  258. /* following attributes must be padded to 4 bytes */
  259. case USERNAME_ATTR:
  260. case PASSWORD_ATTR:
  261. case ERROR_CODE_ATTR:
  262. case UNKNOWN_ATTRIBUTES_ATTR:
  263. case SERVER_ATTR:
  264. padded_len = PADDED_TO_FOUR(ntohs(attr.len));
  265. #ifdef EXTRA_DEBUG
  266. LOG(L_DBG, "DEBUG: stun_parse_body: padded to four\n");
  267. #endif
  268. break;
  269. /* MESSAGE_INTEGRITY must be padded to sixty four bytes*/
  270. case MESSAGE_INTEGRITY_ATTR:
  271. #ifdef EXTRA_DEBUG
  272. LOG(L_DBG, "DEBUG: stun_parse_body: message integrity attribute found\n");
  273. #endif
  274. padded_len = PADDED_TO_SIXTYFOUR(ntohs(attr.len));
  275. break;
  276. case FINGERPRINT_ATTR:
  277. #ifdef EXTRA_DEBUG
  278. LOG(L_DBG, "DEBUG: stun_parse_body: fingerprint attribute found\n");
  279. #endif
  280. fp_present = 1;
  281. if (ntohs(attr.len) != SHA_DIGEST_LENGTH) {
  282. LOG(L_WARN,
  283. "WARNING: STUN: Incorrect fingerprint of request.\n");
  284. *error_code = BAD_REQUEST_ERR;
  285. continue;
  286. }
  287. memcpy(req->fp, buf, SHA_DIGEST_LENGTH);
  288. if(stun_allow_fp) {
  289. if (validate_fingerprint(req, error_code) != 0) {
  290. LOG(L_WARN,
  291. "WARNING: STUN: Incorrect fingerprint of request.\n");
  292. *error_code = BAD_REQUEST_ERR;
  293. continue;
  294. }
  295. }
  296. padded_len = SHA_DIGEST_LENGTH;
  297. if (not_parsed > SHA_DIGEST_LENGTH) {
  298. #ifdef EXTRA_DEBUG
  299. LOG(L_DBG, "DEBUG: stun_parse_body: fingerprint is not the last attribute\n");
  300. #endif
  301. /* fingerprint must be last parameter in request */
  302. *error_code = BAD_REQUEST_ERR;
  303. continue;
  304. }
  305. break;
  306. default:
  307. /*
  308. * the attribute is uknnown to the server
  309. * let see if it's necessary to generate error response
  310. */
  311. #ifdef EXTRA_DEBUG
  312. LOG(L_DBG, "DEBUG: low endian: attr - 0x%x const - 0x%x\n", ntohs(attr.type), MANDATORY_ATTR);
  313. LOG(L_DBG, "DEBUG: big endian: attr - 0x%x const - 0x%x\n", attr.type, htons(MANDATORY_ATTR));
  314. #endif
  315. if (ntohs(attr.type) <= MANDATORY_ATTR) {
  316. #ifdef EXTRA_DEBUG
  317. LOG(L_DBG, "DEBUG: stun_parse_body: mandatory unknown attribute found - 0x%x\n", ntohs(attr.type));
  318. #endif
  319. tmp_unknown = stun_alloc_unknown_attr(attr.type);
  320. if (tmp_unknown == NULL) {
  321. return FATAL_ERROR;
  322. }
  323. if (*unknown == NULL) {
  324. *unknown = body = tmp_unknown;
  325. }
  326. else {
  327. body->next = tmp_unknown;
  328. body = body->next;
  329. }
  330. }
  331. #ifdef EXTRA_DEBUG
  332. else {
  333. LOG(L_DBG, "DEBUG: stun_parse_body: optional unknown attribute found - 0x%x\n", ntohs(attr.type));
  334. }
  335. #endif
  336. padded_len = ntohs(attr.len);
  337. break;
  338. }
  339. buf += padded_len;
  340. not_parsed -= padded_len;
  341. } /* while */
  342. /*
  343. * The unknown attribute error code must set after parsing of whole body
  344. * because it's necessary to obtain all of unknown attributes!
  345. */
  346. if (*error_code == RESPONSE_OK && *unknown != NULL) {
  347. *error_code = UNKNOWN_ATTRIBUTE_ERR;
  348. }
  349. if (fp_present == 0 && req->old == 0) {
  350. #ifdef EXTRA_DEBUG
  351. LOG(L_DBG, "DEBUG: stun_parse_body: fingerprint is missing is this new request\n");
  352. #endif
  353. /* missing mandatory attribute fingerprint */
  354. *error_code = BAD_REQUEST_ERR;
  355. }
  356. return 0;
  357. }
  358. /*
  359. * stun_create_response():
  360. * - req: original request from host
  361. * - res: this will represent response to host
  362. * - ri: information about request, necessary because of IP
  363. * address and port
  364. * - unknown: link list of unknown attributes
  365. * - error_code: indication of any protocol error
  366. *
  367. * The function stun_create_response ensures creating response to a host.
  368. * The type of response depends on value of error_code parameter.
  369. *
  370. * Return value: 0 if there is no environment error
  371. * -1 if there is some enviroment error such as insufficiency
  372. * of memory
  373. */
  374. int stun_create_response(
  375. struct stun_msg* req,
  376. struct stun_msg* res,
  377. struct receive_info* ri,
  378. struct stun_unknown_att* unknown,
  379. UINT_T error_code)
  380. {
  381. UINT_T msg_len;
  382. /*
  383. * Alloc some space for response.
  384. * Optimalization? - maybe it would be better to use biggish static array.
  385. */
  386. res->msg.buf.s = (char *) pkg_malloc(sizeof(char)*STUN_MSG_LEN);
  387. if (res->msg.buf.s == NULL) {
  388. LOG(L_ERR, "ERROR: STUN: out of memory\n");
  389. return FATAL_ERROR;
  390. }
  391. memset(res->msg.buf.s, 0, sizeof(char)*STUN_MSG_LEN);
  392. res->msg.buf.len = 0;
  393. res->msg.empty = STUN_MSG_LEN;
  394. /* use magic cookie and transaction ID from request */
  395. memcpy(&res->hdr.id, &req->hdr.id, sizeof(struct transaction_id));
  396. /* the correct body length will be added ASAP it will be known */
  397. res->hdr.len = htons(0);
  398. if (error_code == RESPONSE_OK) {
  399. #ifdef EXTRA_DEBUG
  400. LOG(L_DBG, "DEBUG: stun_create_response: creating normal response\n");
  401. #endif
  402. res->hdr.type = htons(BINDING_RESPONSE);
  403. /* copy header into msg buffer */
  404. if (buf_copy(&res->msg, (void *) &res->hdr,
  405. sizeof(struct stun_hdr)) != 0) {
  406. #ifdef EXTRA_DEBUG
  407. LOG(L_DBG, "DEBUG: stun_create_response: failed to copy buffer\n");
  408. #endif
  409. return FATAL_ERROR;
  410. }
  411. /*
  412. * If the SER received message in old format, then the body will
  413. * contain MAPPED-ADDRESS attribute instead of XOR-MAPPED-ADDRESS
  414. * attribute.
  415. */
  416. if (req->old == 0) {
  417. if (stun_add_address_attr(res, ri->src_ip.af, ri->src_port,
  418. ri->src_ip.u.addr32, XOR_MAPPED_ADDRESS_ATTR,
  419. XOR) != 0) {
  420. #ifdef EXTRA_DEBUG
  421. LOG(L_DBG, "DEBUG: stun_create_response: failed to add address\n");
  422. #endif
  423. return FATAL_ERROR;
  424. }
  425. if (stun_add_common_integer_attr(res, REFRESH_INTERVAL_ATTR,
  426. stun_refresh_interval) != 0) {
  427. #ifdef EXTRA_DEBUG
  428. LOG(L_DBG, "DEBUG: stun_create_response: failed to common attributes\n");
  429. #endif
  430. return FATAL_ERROR;
  431. }
  432. }
  433. else {
  434. if (stun_add_address_attr(res, ri->src_ip.af, ri->src_port,
  435. ri->src_ip.u.addr32, MAPPED_ADDRESS_ATTR, !XOR) != 0) {
  436. #ifdef EXTRA_DEBUG
  437. LOG(L_DBG, "DEBUG: stun_create_response: failed to add address\n");
  438. #endif
  439. return FATAL_ERROR;
  440. }
  441. }
  442. }
  443. else {
  444. #ifdef EXTRA_DEBUG
  445. LOG(L_DBG, "DEBUG: stun_create_response: creating error response\n");
  446. #endif
  447. res->hdr.type = htons(BINDING_ERROR_RESPONSE);
  448. if (buf_copy(&res->msg, (void *) &res->hdr,
  449. sizeof(struct stun_hdr)) != 0) {
  450. #ifdef EXTRA_DEBUG
  451. LOG(L_DBG, "DEBUG: stun_create_response: failed to copy buffer\n");
  452. #endif
  453. return FATAL_ERROR;
  454. }
  455. if (add_error_code(res, error_code) != 0) {
  456. #ifdef EXTRA_DEBUG
  457. LOG(L_DBG, "DEBUG: stun_create_response: failed to add error code\n");
  458. #endif
  459. return FATAL_ERROR;
  460. }
  461. if (unknown != NULL) {
  462. if (add_unknown_attr(res, unknown) != 0) {
  463. #ifdef EXTRA_DEBUG
  464. LOG(L_DBG, "DEBUG: stun_create_response: failed to add unknown attribute\n");
  465. #endif
  466. return FATAL_ERROR;
  467. }
  468. }
  469. }
  470. if (req->old == 0) {
  471. /*
  472. * add optional information about server; attribute SERVER is not a part of
  473. * rfc3489.txt
  474. * */
  475. if (stun_add_common_text_attr(res, SERVER_ATTR, SERVER_HDR, PAD4)!=0) {
  476. #ifdef EXTRA_DEBUG
  477. LOG(L_DBG, "DEBUG: stun_create_response: failed to add common text attribute\n");
  478. #endif
  479. return FATAL_ERROR;
  480. }
  481. }
  482. if (req->old == 0 && stun_allow_fp) {
  483. /* count length of body except header and fingerprint
  484. * and copy message length at the beginning of buffer
  485. */
  486. msg_len = res->msg.buf.len - sizeof(struct stun_hdr);
  487. msg_len += SHA_DIGEST_LENGTH + sizeof(struct stun_attr);
  488. res->hdr.len = htons(msg_len);
  489. memcpy(&res->msg.buf.s[sizeof(USHORT_T)], (void *) &res->hdr.len,
  490. sizeof(USHORT_T));
  491. if (add_fingerprint(&res->msg) != 0) {
  492. #ifdef EXTRA_DEBUG
  493. LOG(L_DBG, "DEBUG: stun_create_response: failed to add fingerprint\n");
  494. #endif
  495. return FATAL_ERROR;
  496. }
  497. }
  498. else {
  499. res->hdr.len = htons(res->msg.buf.len - sizeof(struct stun_hdr));
  500. memcpy(&res->msg.buf.s[sizeof(USHORT_T)], (void *) &res->hdr.len,
  501. sizeof(USHORT_T));
  502. }
  503. return 0;
  504. }
  505. /*
  506. * add_unknown_attr()
  507. * - res: response representation
  508. * - unknown: link list of unknown attributes
  509. *
  510. * The function add_unknown_attr ensures copy of link list of unknown
  511. * attributes into response buffer.
  512. *
  513. * Return value: 0 if there is no environment error
  514. * -1 if there is some enviroment error such as insufficiency
  515. * of memory
  516. *
  517. */
  518. int add_unknown_attr(struct stun_msg* res, struct stun_unknown_att* unknown)
  519. {
  520. struct stun_attr attr;
  521. struct stun_unknown_att* tmp_unknown;
  522. UINT_T counter;
  523. USHORT_T orig_len;
  524. counter = 0;
  525. orig_len = res->msg.buf.len;
  526. tmp_unknown = unknown;
  527. attr.type = htons(UNKNOWN_ATTRIBUTES_ATTR);
  528. attr.len = htons(0);
  529. if (buf_copy(&res->msg, (void *) &attr, sizeof(struct stun_attr)) != 0) {
  530. #ifdef EXTRA_DEBUG
  531. LOG(L_DBG, "DEBUG: add_unknown_attr: failed to copy buffer\n");
  532. #endif
  533. return FATAL_ERROR;
  534. }
  535. while (tmp_unknown != NULL) {
  536. if (buf_copy(&res->msg, (void *)&tmp_unknown->type,
  537. sizeof(USHORT_T)) != 0) {
  538. #ifdef EXTRA_DEBUG
  539. LOG(L_DBG, "DEBUG: add_unknown_attr: failed to copy unknown attribute\n");
  540. #endif
  541. return FATAL_ERROR;
  542. }
  543. tmp_unknown = tmp_unknown->next;
  544. ++counter;
  545. }
  546. attr.len = htons(res->msg.buf.len - orig_len);
  547. memcpy(&res->msg.buf.s[orig_len], (void *)&attr, sizeof(struct stun_attr));
  548. /* check if there is an odd number of unknown attributes and if yes,
  549. * repeat one of them because of padding to 32
  550. */
  551. if (counter/2 != 0 && unknown != NULL) {
  552. if (buf_copy(&res->msg, (void *)&unknown->type, sizeof(USHORT_T))!=0) {
  553. #ifdef EXTRA_DEBUG
  554. LOG(L_DBG, "DEBUG: add_unknown_attr: failed to padd\n");
  555. #endif
  556. return FATAL_ERROR;
  557. }
  558. }
  559. return 0;
  560. }
  561. /*
  562. * add_error_code()
  563. * - res: response representation
  564. * - error_code: value of error type
  565. *
  566. * The function add_error_code ensures copy of link list of unknown
  567. * attributes into response buffer.
  568. *
  569. * Return value: 0 if there is no environment error
  570. * -1 if there is some enviroment error such as insufficiency
  571. * of memory
  572. */
  573. int add_error_code(struct stun_msg* res, USHORT_T error_code)
  574. {
  575. struct stun_attr attr;
  576. USHORT_T orig_len;
  577. USHORT_T two_bytes;
  578. int text_pad;
  579. char err[2];
  580. orig_len = res->msg.buf.len;
  581. text_pad = 0;
  582. /* the type and length will be copy as last one because of unknown length*/
  583. if (res->msg.buf.len < sizeof(struct stun_attr)) {
  584. if (reallock_buffer(&res->msg, sizeof(struct stun_attr)) != 0) {
  585. #ifdef EXTRA_DEBUG
  586. LOG(L_DBG, "DEBUG: add_error_code: failed to reallocate buffer\n");
  587. #endif
  588. return FATAL_ERROR;
  589. }
  590. }
  591. res->msg.buf.len += sizeof(struct stun_attr);
  592. res->msg.empty -= sizeof(struct stun_attr);
  593. /* first two bytes are empty */
  594. two_bytes = 0x0000;
  595. if (buf_copy(&res->msg, (void *) &two_bytes, sizeof(USHORT_T)) != 0) {
  596. #ifdef EXTRA_DEBUG
  597. LOG(L_DBG, "DEBUG: add_error_code: failed to copy buffer\n");
  598. #endif
  599. return FATAL_ERROR;
  600. }
  601. err[0] = error_code / 100;
  602. err[1] = error_code % 100;
  603. if (buf_copy(&res->msg, (void *) err, sizeof(UCHAR_T)*2) != 0) {
  604. return FATAL_ERROR;
  605. }
  606. switch (error_code) {
  607. case TRY_ALTERNATE_ERR:
  608. text_pad = copy_str_to_buffer(res, TRY_ALTERNATE_TXT, PAD4);
  609. break;
  610. case BAD_REQUEST_ERR:
  611. text_pad = copy_str_to_buffer(res, BAD_REQUEST_TXT, PAD4);
  612. break;
  613. case UNAUTHORIZED_ERR:
  614. text_pad = copy_str_to_buffer(res, UNAUTHORIZED_TXT, PAD4);
  615. break;
  616. case UNKNOWN_ATTRIBUTE_ERR:
  617. text_pad = copy_str_to_buffer(res, UNKNOWN_ATTRIBUTE_TXT, PAD4);
  618. break;
  619. case STALE_CREDENTIALS_ERR:
  620. text_pad = copy_str_to_buffer(res, STALE_CREDENTIALS_TXT, PAD4);
  621. break;
  622. case INTEGRITY_CHECK_ERR:
  623. text_pad = copy_str_to_buffer(res, INTEGRITY_CHECK_TXT, PAD4);
  624. break;
  625. case MISSING_USERNAME_ERR:
  626. text_pad = copy_str_to_buffer(res, MISSING_USERNAME_TXT, PAD4);
  627. break;
  628. case USE_TLS_ERR:
  629. text_pad = copy_str_to_buffer(res, USE_TLS_TXT, PAD4);
  630. break;
  631. case MISSING_REALM_ERR:
  632. text_pad = copy_str_to_buffer(res, MISSING_REALM_TXT, PAD4);
  633. break;
  634. case MISSING_NONCE_ERR:
  635. text_pad = copy_str_to_buffer(res, MISSING_NONCE_TXT, PAD4);
  636. break;
  637. case UNKNOWN_USERNAME_ERR:
  638. text_pad = copy_str_to_buffer(res, UNKNOWN_USERNAME_TXT, PAD4);
  639. break;
  640. case STALE_NONCE_ERR:
  641. text_pad = copy_str_to_buffer(res, STALE_NONCE_TXT, PAD4);
  642. break;
  643. case SERVER_ERROR_ERR:
  644. text_pad = copy_str_to_buffer(res, SERVER_ERROR_TXT, PAD4);
  645. break;
  646. case GLOBAL_FAILURE_ERR:
  647. text_pad = copy_str_to_buffer(res, GLOBAL_FAILURE_TXT, PAD4);
  648. break;
  649. default:
  650. LOG(L_ERR, "ERROR: STUN: Unknown error code.\n");
  651. break;
  652. }
  653. if (text_pad < 0) {
  654. #ifdef EXTRA_DEBUG
  655. LOG(L_DBG, "DEBUG: add_error_code: text_pad is negative\n");
  656. #endif
  657. goto error;
  658. }
  659. attr.type = htons(ERROR_CODE_ATTR);
  660. /* count length of "value" field -> without type and lehgth field */
  661. attr.len = htons(res->msg.buf.len - orig_len -
  662. text_pad - sizeof(struct stun_attr));
  663. memcpy(&res->msg.buf.s[orig_len], (void *)&attr, sizeof(struct stun_attr));
  664. return 0;
  665. error:
  666. return FATAL_ERROR;
  667. }
  668. /*
  669. * copy_str_to_buffer()
  670. * - res: response representation
  671. * - data: text data, in our case almost text representation of error
  672. * - pad: the size of pad (for how much bytes the string should be
  673. * padded
  674. *
  675. * The function copy_str_to_buffer ensures copy of text buffer into response
  676. * buffer.
  677. *
  678. * Return value: 0 if there is no environment error
  679. * -1 if there is some enviroment error such as insufficiency
  680. * of memory
  681. */
  682. int copy_str_to_buffer(struct stun_msg* res, const char* data, UINT_T pad)
  683. {
  684. USHORT_T pad_len;
  685. UINT_T data_len;
  686. UCHAR_T empty[pad];
  687. data_len = strlen(data);
  688. memset(&empty, 0, pad);
  689. pad_len = pad - data_len%pad;
  690. if (buf_copy(&res->msg, (void *) data, sizeof(UCHAR_T)*data_len) != 0) {
  691. #ifdef EXTRA_DEBUG
  692. LOG(L_DBG, "DEBUG: copy_str_to_buffer: failed to copy buffer\n");
  693. #endif
  694. return FATAL_ERROR;
  695. }
  696. if (pad_len != 0) {
  697. if (buf_copy(&res->msg, &empty, pad_len) != 0) {
  698. #ifdef EXTRA_DEBUG
  699. LOG(L_DBG, "DEBUG: copy_str_to_buffer: failed to pad\n");
  700. #endif
  701. return FATAL_ERROR;
  702. }
  703. }
  704. return pad_len;
  705. }
  706. /*
  707. * stun_add_address_attr()
  708. * - res: response representation
  709. * - af: address family
  710. * - port: port
  711. * - ip_addr: represent both IPv4 and IPv6, the differences is in
  712. * length
  713. * - type: type of attribute
  714. * - do_xor: if the port should be XOR-ed or not.
  715. *
  716. * The function stun_add_address_attr ensures copy of any IP attribute into
  717. * response buffer.
  718. *
  719. * Return value: 0 if there is no environment error
  720. * -1 if there is some enviroment error such as insufficiency
  721. * of memory
  722. */
  723. int stun_add_address_attr(struct stun_msg* res,
  724. UINT_T af,
  725. USHORT_T port,
  726. UINT_T* ip_addr,
  727. USHORT_T type,
  728. int do_xor)
  729. {
  730. struct stun_attr attr;
  731. UINT_T id[IP_ADDR];
  732. int ip_struct_len;
  733. int i;
  734. ip_struct_len = 0;
  735. attr.type = htons(type);
  736. res->ip_addr.port = (do_xor) ? htons(port) ^ MAGIC_COOKIE_2B : htons(port);
  737. switch(af) {
  738. case AF_INET:
  739. ip_struct_len = sizeof(struct stun_ip_addr) - 3*sizeof(UINT_T);
  740. res->ip_addr.family = htons(IPV4_FAMILY);
  741. memcpy(res->ip_addr.ip, ip_addr, IPV4_LEN);
  742. res->ip_addr.ip[0] = (do_xor) ?
  743. res->ip_addr.ip[0] ^ MAGIC_COOKIE : res->ip_addr.ip[0];
  744. break;
  745. #ifdef USE_IPV6
  746. case AF_INET6:
  747. ip_struct_len = sizeof(struct stun_ip_addr);
  748. res->ip_addr.family = htons(IPV6_FAMILY);
  749. memcpy(&res->ip_addr.ip, ip_addr, IPV6_LEN);
  750. memcpy(id, &res->hdr.id, sizeof(struct transaction_id));
  751. for (i=0; i<IP_ADDR; i++) {
  752. res->ip_addr.ip[i] = (do_xor) ?
  753. res->ip_addr.ip[i] ^ id[i] : res->ip_addr.ip[i];
  754. }
  755. break;
  756. #endif /* USE_IPV6 */
  757. default:
  758. break;
  759. }
  760. attr.len = htons(ip_struct_len);
  761. /* copy type and attribute's length */
  762. if (buf_copy(&res->msg, (void *) &attr, sizeof(struct stun_attr)) != 0) {
  763. return FATAL_ERROR;
  764. }
  765. /* copy family, port and IP */
  766. if (buf_copy(&res->msg, (void *) &res->ip_addr, ip_struct_len) != 0) {
  767. return FATAL_ERROR;
  768. }
  769. return 0;
  770. }
  771. /*
  772. * add_fingerprint()
  773. * - msg: response buffer
  774. *
  775. * The function add_fingerprint ensures adding fingerprint attribute into
  776. * response buffer.
  777. *
  778. * Return value: 0 if there is no environment error
  779. * -1 if there is some enviroment error such as insufficiency
  780. * of memory
  781. */
  782. int add_fingerprint(struct stun_buffer* msg)
  783. {
  784. struct stun_attr attr;
  785. USHORT_T attr_type_size;
  786. attr_type_size = sizeof(struct stun_attr);
  787. attr.type = htons(FINGERPRINT_ATTR);
  788. attr.len = htons(SHA_DIGEST_LENGTH);
  789. if (msg->empty < (SHA_DIGEST_LENGTH + attr_type_size)) {
  790. if (reallock_buffer(msg, SHA_DIGEST_LENGTH + attr_type_size) != 0) {
  791. return FATAL_ERROR;
  792. }
  793. }
  794. memcpy(&msg->buf.s[msg->buf.len], (void *) &attr, attr_type_size);
  795. msg->buf.len += attr_type_size;
  796. msg->empty -= attr_type_size;
  797. if (SHA1((UCHAR_T *)msg->buf.s, msg->buf.len-attr_type_size,
  798. (UCHAR_T *) &msg->buf.s[msg->buf.len]) == 0) {
  799. LOG(L_ERR, "ERROR: STUN: SHA-1 algorithm failed.\n");
  800. return FATAL_ERROR;
  801. }
  802. msg->buf.len += SHA_DIGEST_LENGTH;
  803. msg->empty -= SHA_DIGEST_LENGTH;
  804. return 0;
  805. }
  806. /*
  807. * stun_alloc_unknown_attr()
  808. * - type: type of unknown attribute
  809. *
  810. * The function stun_alloc_unknown_attr ensures allocationg new element for
  811. * the link list of unknown attributes.
  812. *
  813. * Return value: pointer to new element of link list in positive case
  814. * NULL if there is some enviroment error such as insufficiency
  815. * of memory
  816. */
  817. struct stun_unknown_att* stun_alloc_unknown_attr(USHORT_T type)
  818. {
  819. struct stun_unknown_att* attr;
  820. attr = (struct stun_unknown_att *) pkg_malloc(sizeof(struct stun_unknown_att));
  821. if (attr == NULL) {
  822. LOG(L_ERR, "ERROR: STUN: out of memory\n");
  823. return NULL;
  824. }
  825. attr->type = type;
  826. attr->next = NULL;
  827. return attr;
  828. }
  829. /*
  830. * stun_delete_unknown_attrs()
  831. * - unknown: link list of unknown attributes
  832. *
  833. * The function stun_delete_unknown_attrs ensures deleting of link list
  834. *
  835. * Return value: none
  836. */
  837. void stun_delete_unknown_attrs(struct stun_unknown_att* unknown)
  838. {
  839. struct stun_unknown_att* tmp_unknown;
  840. if (unknown == NULL) {
  841. return;
  842. }
  843. while(unknown->next) {
  844. tmp_unknown = unknown->next;
  845. unknown->next = tmp_unknown->next;
  846. pkg_free(tmp_unknown);
  847. }
  848. pkg_free(unknown);
  849. }
  850. /*
  851. * validate_fingerprint()
  852. * - req: structure representing request message
  853. * - error_code: indication of any protocol error
  854. *
  855. * The function validate_fingerprint ensures validation of FINGERPRINT
  856. * attribute.
  857. *
  858. * Return value: 0 if there is no environment error
  859. * -1 if there is some enviroment error such as insufficiency
  860. * of memory
  861. */
  862. int validate_fingerprint(struct stun_msg* req, USHORT_T* error_code)
  863. {
  864. UCHAR_T msg_digest[SHA_DIGEST_LENGTH];
  865. UINT_T buf_len;
  866. buf_len = req->hdr.len + sizeof(struct stun_hdr);
  867. buf_len -= SHA_DIGEST_LENGTH + sizeof(struct stun_attr);
  868. if (SHA1((UCHAR_T *) req->msg.buf.s, buf_len, msg_digest) == 0) {
  869. LOG(L_ERR, "ERROR: STUN: SHA-1 algorithm failed.\n");
  870. return FATAL_ERROR;
  871. }
  872. if (memcmp((void *)req->fp, (void *)&msg_digest, SHA_DIGEST_LENGTH) != 0) {
  873. *error_code = BAD_REQUEST_ERR;
  874. }
  875. return 0;
  876. }
  877. /*
  878. * buf_copy()
  879. * - msg: buffer where the data will be copy to
  880. * - source: source data buffer
  881. * - len: number of bytes that should be copied
  882. *
  883. * The function buf_copy copies "len" bytes from source into msg buffer
  884. *
  885. * Return value: 0 if there is no environment error
  886. * -1 if there is some enviroment error such as insufficiency
  887. * of memory
  888. */
  889. int buf_copy(struct stun_buffer* msg, void* source, UINT_T len)
  890. {
  891. if (msg->empty < len) {
  892. if (reallock_buffer(msg, len) != 0) {
  893. return FATAL_ERROR;
  894. }
  895. }
  896. memcpy(&msg->buf.s[msg->buf.len], source, len);
  897. msg->buf.len += len;
  898. msg->empty -= len;
  899. return 0;
  900. }
  901. /*
  902. * reallock_buffer()
  903. * - buffer: original buffer
  904. * - len: represents minimum of bytes that must be available after
  905. * reallocation
  906. *
  907. * The function reallock_buffer reallocks buffer. New buffer's length will be
  908. * original length plus bigger from len and STUN_MSG_LEN constant.
  909. *
  910. * Return value: 0 if there is no environment error
  911. * -1 if there is some enviroment error such as insufficiency
  912. * of memory
  913. */
  914. int reallock_buffer(struct stun_buffer* buffer, UINT_T len)
  915. {
  916. char* tmp_buf;
  917. UINT_T new_len;
  918. new_len = (STUN_MSG_LEN < len) ? STUN_MSG_LEN+len : STUN_MSG_LEN;
  919. tmp_buf = (char *) pkg_realloc(buffer->buf.s,
  920. buffer->buf.len + buffer->empty + new_len);
  921. if (tmp_buf == 0) {
  922. LOG(L_ERR, "ERROR: STUN: out of memory\n");
  923. return FATAL_ERROR;
  924. }
  925. buffer->buf.s = tmp_buf;
  926. buffer->empty += new_len;
  927. return 0;
  928. }
  929. /*
  930. * clean_memory()
  931. * - res: structure representing response message
  932. * - unknown: link list of unknown attributes
  933. *
  934. * The function clean_memory should free dynamic allocated memory.
  935. *
  936. * Return value: none
  937. */
  938. void clean_memory(struct stun_msg* req,
  939. struct stun_msg* res, struct stun_unknown_att* unknown)
  940. {
  941. #ifdef DYN_BUF
  942. pkg_free(req->msg.buf.s);
  943. #endif
  944. if (res->msg.buf.s != NULL) {
  945. pkg_free(res->msg.buf.s);
  946. }
  947. stun_delete_unknown_attrs(unknown);
  948. }
  949. /*
  950. * stun_add_common_integer_attr()
  951. * - res: structure representing response
  952. * - type: type of attribute
  953. * - value: attribute's value
  954. *
  955. * The function stun_add_common_integer_attr copy attribute with integer value
  956. * into response buffer.
  957. *
  958. * Return value: 0 if there is no environment error
  959. * -1 if there is some enviroment error such as insufficiency
  960. * of memory
  961. */
  962. int stun_add_common_integer_attr(struct stun_msg* res,
  963. USHORT_T type,
  964. UINT_T value)
  965. {
  966. struct stun_attr attr;
  967. attr.type = htons(type);
  968. attr.len = htons(sizeof(UINT_T));
  969. if (buf_copy(&res->msg, (void *) &attr, sizeof(struct stun_attr)) != 0) {
  970. return FATAL_ERROR;
  971. }
  972. value = htonl(value);
  973. if (buf_copy(&res->msg, (void *) &value, sizeof(UINT_T)) != 0) {
  974. return FATAL_ERROR;
  975. }
  976. return 0;
  977. }
  978. /*
  979. * stun_add_common_text_attr()
  980. * - res: structure representing response
  981. * - type: type of attribute
  982. * - value: attribute's value
  983. * - pad: size of pad
  984. *
  985. * The function stun_add_common_text_attr copy attribute with string value
  986. * into response buffer.
  987. *
  988. * Return value: 0 if there is no environment error
  989. * -1 if there is some enviroment error such as insufficiency
  990. * of memory
  991. */
  992. int stun_add_common_text_attr(struct stun_msg* res,
  993. USHORT_T type,
  994. char* value,
  995. USHORT_T pad)
  996. {
  997. struct stun_attr attr;
  998. if (value == NULL) {
  999. LOG(L_INFO, "INFO: stun_add_common_text_attr: value is NULL\n");
  1000. return 0;
  1001. }
  1002. attr.type = htons(type);
  1003. attr.len = htons(strlen(value));
  1004. if (buf_copy(&res->msg, (void *) &attr, sizeof(struct stun_attr)) != 0) {
  1005. return FATAL_ERROR;
  1006. }
  1007. if (copy_str_to_buffer(res, value, pad) < 0) {
  1008. return FATAL_ERROR;
  1009. }
  1010. return 0;
  1011. }
  1012. #endif /* USE_STUN */