ser_stun.c 28 KB

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