stun.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2008, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. *
  21. * \brief STUN Support
  22. *
  23. * \author Mark Spencer <markster@digium.com>
  24. *
  25. * \note STUN is defined in RFC 3489.
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. #include "asterisk/logger.h"
  32. #include "asterisk/logger_category.h"
  33. #include "asterisk/_private.h"
  34. #include "asterisk/stun.h"
  35. #include "asterisk/cli.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/channel.h"
  38. /*!
  39. * \brief STUN support code
  40. *
  41. * This code provides some support for doing STUN transactions.
  42. * Eventually it should be moved elsewhere as other protocols
  43. * than RTP can benefit from it - e.g. SIP.
  44. * STUN is described in RFC3489 and it is based on the exchange
  45. * of UDP packets between a client and one or more servers to
  46. * determine the externally visible address (and port) of the client
  47. * once it has gone through the NAT boxes that connect it to the
  48. * outside.
  49. * The simplest request packet is just the header defined in
  50. * struct stun_header, and from the response we may just look at
  51. * one attribute, STUN_MAPPED_ADDRESS, that we find in the response.
  52. * By doing more transactions with different server addresses we
  53. * may determine more about the behaviour of the NAT boxes, of
  54. * course - the details are in the RFC.
  55. *
  56. * All STUN packets start with a simple header made of a type,
  57. * length (excluding the header) and a 16-byte random transaction id.
  58. * Following the header we may have zero or more attributes, each
  59. * structured as a type, length and a value (whose format depends
  60. * on the type, but often contains addresses).
  61. * Of course all fields are in network format.
  62. */
  63. typedef struct { unsigned int id[4]; } __attribute__((packed)) stun_trans_id;
  64. struct stun_header {
  65. unsigned short msgtype;
  66. unsigned short msglen;
  67. stun_trans_id id;
  68. unsigned char ies[0];
  69. } __attribute__((packed));
  70. struct stun_attr {
  71. unsigned short attr;
  72. unsigned short len;
  73. unsigned char value[0];
  74. } __attribute__((packed));
  75. /*
  76. * The format normally used for addresses carried by STUN messages.
  77. */
  78. struct stun_addr {
  79. unsigned char unused;
  80. unsigned char family;
  81. unsigned short port;
  82. unsigned int addr;
  83. } __attribute__((packed));
  84. /*! \brief STUN message types
  85. * 'BIND' refers to transactions used to determine the externally
  86. * visible addresses. 'SEC' refers to transactions used to establish
  87. * a session key for subsequent requests.
  88. * 'SEC' functionality is not supported here.
  89. */
  90. #define STUN_BINDREQ 0x0001
  91. #define STUN_BINDRESP 0x0101
  92. #define STUN_BINDERR 0x0111
  93. #define STUN_SECREQ 0x0002
  94. #define STUN_SECRESP 0x0102
  95. #define STUN_SECERR 0x0112
  96. /*! \brief Basic attribute types in stun messages.
  97. * Messages can also contain custom attributes (codes above 0x7fff)
  98. */
  99. #define STUN_MAPPED_ADDRESS 0x0001
  100. #define STUN_RESPONSE_ADDRESS 0x0002
  101. #define STUN_CHANGE_REQUEST 0x0003
  102. #define STUN_SOURCE_ADDRESS 0x0004
  103. #define STUN_CHANGED_ADDRESS 0x0005
  104. #define STUN_USERNAME 0x0006
  105. #define STUN_PASSWORD 0x0007
  106. #define STUN_MESSAGE_INTEGRITY 0x0008
  107. #define STUN_ERROR_CODE 0x0009
  108. #define STUN_UNKNOWN_ATTRIBUTES 0x000a
  109. #define STUN_REFLECTED_FROM 0x000b
  110. #define STUN_MAX_RETRIES 3
  111. /*! \brief helper function to print message names */
  112. static const char *stun_msg2str(int msg)
  113. {
  114. switch (msg) {
  115. case STUN_BINDREQ:
  116. return "Binding Request";
  117. case STUN_BINDRESP:
  118. return "Binding Response";
  119. case STUN_BINDERR:
  120. return "Binding Error Response";
  121. case STUN_SECREQ:
  122. return "Shared Secret Request";
  123. case STUN_SECRESP:
  124. return "Shared Secret Response";
  125. case STUN_SECERR:
  126. return "Shared Secret Error Response";
  127. }
  128. return "Non-RFC3489 Message";
  129. }
  130. /*! \brief helper function to print attribute names */
  131. static const char *stun_attr2str(int msg)
  132. {
  133. switch (msg) {
  134. case STUN_MAPPED_ADDRESS:
  135. return "Mapped Address";
  136. case STUN_RESPONSE_ADDRESS:
  137. return "Response Address";
  138. case STUN_CHANGE_REQUEST:
  139. return "Change Request";
  140. case STUN_SOURCE_ADDRESS:
  141. return "Source Address";
  142. case STUN_CHANGED_ADDRESS:
  143. return "Changed Address";
  144. case STUN_USERNAME:
  145. return "Username";
  146. case STUN_PASSWORD:
  147. return "Password";
  148. case STUN_MESSAGE_INTEGRITY:
  149. return "Message Integrity";
  150. case STUN_ERROR_CODE:
  151. return "Error Code";
  152. case STUN_UNKNOWN_ATTRIBUTES:
  153. return "Unknown Attributes";
  154. case STUN_REFLECTED_FROM:
  155. return "Reflected From";
  156. }
  157. return "Non-RFC3489 Attribute";
  158. }
  159. /*! \brief here we store credentials extracted from a message */
  160. struct stun_state {
  161. const char *username;
  162. const char *password;
  163. };
  164. static int stun_process_attr(struct stun_state *state, struct stun_attr *attr)
  165. {
  166. if (ast_debug_stun_packet_is_allowed) {
  167. ast_verbose("Found STUN Attribute %s (%04x), length %d\n",
  168. stun_attr2str(ntohs(attr->attr)), (unsigned)ntohs(attr->attr), ntohs(attr->len));
  169. }
  170. switch (ntohs(attr->attr)) {
  171. case STUN_USERNAME:
  172. state->username = (const char *) (attr->value);
  173. break;
  174. case STUN_PASSWORD:
  175. state->password = (const char *) (attr->value);
  176. break;
  177. default:
  178. if (ast_debug_stun_packet_is_allowed) {
  179. ast_verbose("Ignoring STUN attribute %s (%04x), length %d\n",
  180. stun_attr2str(ntohs(attr->attr)), (unsigned)ntohs(attr->attr), ntohs(attr->len));
  181. }
  182. }
  183. return 0;
  184. }
  185. /*! \brief append a string to an STUN message */
  186. static void append_attr_string(struct stun_attr **attr, int attrval, const char *s, int *len, int *left)
  187. {
  188. int str_length = strlen(s);
  189. int attr_length = str_length + ((~(str_length - 1)) & 0x3);
  190. int size = sizeof(**attr) + attr_length;
  191. if (*left > size) {
  192. (*attr)->attr = htons(attrval);
  193. (*attr)->len = htons(attr_length);
  194. memcpy((*attr)->value, s, str_length);
  195. memset((*attr)->value + str_length, 0, attr_length - str_length);
  196. (*attr) = (struct stun_attr *)((*attr)->value + attr_length);
  197. *len += size;
  198. *left -= size;
  199. }
  200. }
  201. /*! \brief append an address to an STUN message */
  202. static void append_attr_address(struct stun_attr **attr, int attrval, struct sockaddr_in *sin, int *len, int *left)
  203. {
  204. int size = sizeof(**attr) + 8;
  205. struct stun_addr *addr;
  206. if (*left > size) {
  207. (*attr)->attr = htons(attrval);
  208. (*attr)->len = htons(8);
  209. addr = (struct stun_addr *)((*attr)->value);
  210. addr->unused = 0;
  211. addr->family = 0x01;
  212. addr->port = sin->sin_port;
  213. addr->addr = sin->sin_addr.s_addr;
  214. (*attr) = (struct stun_attr *)((*attr)->value + 8);
  215. *len += size;
  216. *left -= size;
  217. }
  218. }
  219. static void handle_stun_timeout(int retry, struct sockaddr_in *dst)
  220. {
  221. char *stun_destination = "";
  222. if (dst) {
  223. ast_asprintf(&stun_destination, "to '%s' ", ast_inet_ntoa(dst->sin_addr));
  224. }
  225. if (retry < STUN_MAX_RETRIES) {
  226. ast_log(LOG_NOTICE,
  227. "Attempt %d to send STUN request %stimed out.\n",
  228. retry,
  229. stun_destination);
  230. } else {
  231. ast_log(LOG_WARNING,
  232. "Attempt %d to send STUN request %stimed out. "
  233. "Check that the server address is correct and reachable.\n",
  234. retry,
  235. stun_destination);
  236. }
  237. if (dst) {
  238. ast_free(stun_destination);
  239. }
  240. }
  241. /*! \brief wrapper to send an STUN message */
  242. static int stun_send(int s, struct sockaddr_in *dst, struct stun_header *resp)
  243. {
  244. return sendto(s, resp, ntohs(resp->msglen) + sizeof(*resp), 0,
  245. (struct sockaddr *)dst, sizeof(*dst));
  246. }
  247. /*!
  248. * \internal
  249. * \brief Compare the STUN transaction IDs.
  250. *
  251. * \param left Transaction ID.
  252. * \param right Transaction ID.
  253. *
  254. * \retval 0 if match.
  255. * \retval non-zero if not match.
  256. */
  257. static int stun_id_cmp(stun_trans_id *left, stun_trans_id *right)
  258. {
  259. return memcmp(left, right, sizeof(*left));
  260. }
  261. /*! \brief helper function to generate a random request id */
  262. static void stun_req_id(struct stun_header *req)
  263. {
  264. int x;
  265. for (x = 0; x < 4; x++)
  266. req->id.id[x] = ast_random();
  267. }
  268. int ast_stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data, size_t len, stun_cb_f *stun_cb, void *arg)
  269. {
  270. struct stun_header *hdr = (struct stun_header *)data;
  271. struct stun_attr *attr;
  272. struct stun_state st;
  273. int ret = AST_STUN_IGNORE;
  274. int x;
  275. /* On entry, 'len' is the length of the udp payload. After the
  276. * initial checks it becomes the size of unprocessed options,
  277. * while 'data' is advanced accordingly.
  278. */
  279. if (len < sizeof(struct stun_header)) {
  280. ast_debug_stun(1, "Runt STUN packet (only %d, wanting at least %d)\n", (int) len, (int) sizeof(struct stun_header));
  281. return -1;
  282. }
  283. len -= sizeof(struct stun_header);
  284. data += sizeof(struct stun_header);
  285. x = ntohs(hdr->msglen); /* len as advertised in the message */
  286. if (ast_debug_stun_packet_is_allowed)
  287. ast_verbose("STUN Packet, msg %s (%04x), length: %d\n", stun_msg2str(ntohs(hdr->msgtype)), (unsigned)ntohs(hdr->msgtype), x);
  288. if (x > len) {
  289. ast_debug_stun(1, "Scrambled STUN packet length (got %d, expecting %d)\n", x, (int)len);
  290. } else
  291. len = x;
  292. memset(&st, 0, sizeof(st));
  293. while (len) {
  294. if (len < sizeof(struct stun_attr)) {
  295. ast_debug_stun(1, "Runt Attribute (got %d, expecting %d)\n", (int)len, (int) sizeof(struct stun_attr));
  296. break;
  297. }
  298. attr = (struct stun_attr *)data;
  299. /* compute total attribute length */
  300. x = ntohs(attr->len) + sizeof(struct stun_attr);
  301. if (x > len) {
  302. ast_debug_stun(1, "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", x, (int)len);
  303. break;
  304. }
  305. if (stun_cb)
  306. stun_cb(attr, arg);
  307. if (stun_process_attr(&st, attr)) {
  308. ast_debug_stun(1, "Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr->attr)), (unsigned)ntohs(attr->attr));
  309. break;
  310. }
  311. /* Clear attribute id: in case previous entry was a string,
  312. * this will act as the terminator for the string.
  313. */
  314. attr->attr = 0;
  315. data += x;
  316. len -= x;
  317. }
  318. /* Null terminate any string.
  319. * XXX NOTE, we write past the size of the buffer passed by the
  320. * caller, so this is potentially dangerous. The only thing that
  321. * saves us is that usually we read the incoming message in a
  322. * much larger buffer in the struct ast_rtp
  323. */
  324. *data = '\0';
  325. /* Now prepare to generate a reply, which at the moment is done
  326. * only for properly formed (len == 0) STUN_BINDREQ messages.
  327. */
  328. if (len == 0) {
  329. unsigned char respdata[1024];
  330. struct stun_header *resp = (struct stun_header *)respdata;
  331. int resplen = 0; /* len excluding header */
  332. int respleft = sizeof(respdata) - sizeof(struct stun_header);
  333. char combined[33];
  334. resp->id = hdr->id;
  335. resp->msgtype = 0;
  336. resp->msglen = 0;
  337. attr = (struct stun_attr *)resp->ies;
  338. switch (ntohs(hdr->msgtype)) {
  339. case STUN_BINDREQ:
  340. if (ast_debug_stun_packet_is_allowed)
  341. ast_verbose("STUN Bind Request, username: %s\n",
  342. st.username ? st.username : "<none>");
  343. if (st.username) {
  344. append_attr_string(&attr, STUN_USERNAME, st.username, &resplen, &respleft);
  345. /*
  346. * For Google Voice, the stun username is made up of the local
  347. * and remote usernames, each being fixed at 16 bytes. We have
  348. * to swap the two at this point.
  349. */
  350. snprintf(combined, 17, "%16s", st.username + 16);
  351. snprintf(combined + 16, 17, "%16s", st.username);
  352. } else {
  353. combined[0] = '\0';
  354. }
  355. append_attr_address(&attr, STUN_MAPPED_ADDRESS, src, &resplen, &respleft);
  356. resp->msglen = htons(resplen);
  357. resp->msgtype = htons(STUN_BINDRESP);
  358. stun_send(s, src, resp);
  359. ast_stun_request(s, src, combined, NULL);
  360. ret = AST_STUN_ACCEPT;
  361. break;
  362. default:
  363. if (ast_debug_stun_packet_is_allowed)
  364. ast_verbose("Dunno what to do with STUN message %04x (%s)\n", (unsigned)ntohs(hdr->msgtype), stun_msg2str(ntohs(hdr->msgtype)));
  365. }
  366. }
  367. return ret;
  368. }
  369. /*! \brief Extract the STUN_MAPPED_ADDRESS from the stun response.
  370. * This is used as a callback for stun_handle_response
  371. * when called from ast_stun_request.
  372. */
  373. static int stun_get_mapped(struct stun_attr *attr, void *arg)
  374. {
  375. struct stun_addr *addr = (struct stun_addr *)(attr + 1);
  376. struct sockaddr_in *sa = (struct sockaddr_in *)arg;
  377. if (ntohs(attr->attr) != STUN_MAPPED_ADDRESS || ntohs(attr->len) != 8)
  378. return 1; /* not us. */
  379. sa->sin_port = addr->port;
  380. sa->sin_addr.s_addr = addr->addr;
  381. return 0;
  382. }
  383. int ast_stun_request(int s, struct sockaddr_in *dst,
  384. const char *username, struct sockaddr_in *answer)
  385. {
  386. struct stun_header *req;
  387. struct stun_header *rsp;
  388. unsigned char req_buf[1024];
  389. unsigned char rsp_buf[1024];
  390. int reqlen, reqleft;
  391. struct stun_attr *attr;
  392. int res = -1;
  393. int retry;
  394. if (answer) {
  395. /* Always clear answer in case the request fails. */
  396. memset(answer, 0, sizeof(struct sockaddr_in));
  397. }
  398. /* Create STUN bind request */
  399. req = (struct stun_header *) req_buf;
  400. stun_req_id(req);
  401. reqlen = 0;
  402. reqleft = sizeof(req_buf) - sizeof(struct stun_header);
  403. attr = (struct stun_attr *) req->ies;
  404. if (username) {
  405. append_attr_string(&attr, STUN_USERNAME, username, &reqlen, &reqleft);
  406. }
  407. req->msglen = htons(reqlen);
  408. req->msgtype = htons(STUN_BINDREQ);
  409. for (retry = 0; retry++ < STUN_MAX_RETRIES;) { /* XXX make retries configurable */
  410. /* send request, possibly wait for reply */
  411. struct sockaddr_in src;
  412. socklen_t srclen;
  413. struct timeval start;
  414. /* Send STUN message. */
  415. res = stun_send(s, dst, req);
  416. if (res < 0) {
  417. ast_debug_stun(1, "stun_send try %d failed: %s\n", retry, strerror(errno));
  418. break;
  419. }
  420. if (!answer) {
  421. /* Successful send since we don't care about any response. */
  422. res = 0;
  423. break;
  424. }
  425. start = ast_tvnow();
  426. try_again:
  427. /* Wait for response. */
  428. {
  429. struct pollfd pfds = { .fd = s, .events = POLLIN };
  430. int ms;
  431. ms = ast_remaining_ms(start, 3000);
  432. if (ms <= 0) {
  433. /* No response, timeout */
  434. handle_stun_timeout(retry, dst);
  435. res = 1;
  436. continue;
  437. }
  438. res = ast_poll(&pfds, 1, ms);
  439. if (res < 0) {
  440. /* Error */
  441. continue;
  442. }
  443. if (!res) {
  444. /* No response, timeout */
  445. handle_stun_timeout(retry, dst);
  446. res = 1;
  447. continue;
  448. }
  449. }
  450. /* Read STUN response. */
  451. memset(&src, 0, sizeof(src));
  452. srclen = sizeof(src);
  453. /* XXX pass sizeof(rsp_buf) - 1 in the size, because stun_handle_packet might
  454. * write past the end of the buffer.
  455. */
  456. res = recvfrom(s, rsp_buf, sizeof(rsp_buf) - 1,
  457. 0, (struct sockaddr *) &src, &srclen);
  458. if (res < 0) {
  459. ast_debug_stun(1, "recvfrom try %d failed: %s\n", retry, strerror(errno));
  460. break;
  461. }
  462. /* Process the STUN response. */
  463. rsp = (struct stun_header *) rsp_buf;
  464. if (ast_stun_handle_packet(s, &src, rsp_buf, res, stun_get_mapped, answer)
  465. || (rsp->msgtype != htons(STUN_BINDRESP)
  466. && rsp->msgtype != htons(STUN_BINDERR))
  467. || stun_id_cmp(&req->id, &rsp->id)) {
  468. /* Bad STUN packet, not right type, or transaction ID did not match. */
  469. memset(answer, 0, sizeof(struct sockaddr_in));
  470. /* Was not a resonse to our request. */
  471. goto try_again;
  472. }
  473. /* Success. answer contains the external address if available. */
  474. res = 0;
  475. break;
  476. }
  477. return res;
  478. }
  479. static char *handle_cli_stun_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  480. {
  481. switch (cmd) {
  482. case CLI_INIT:
  483. e->command = "stun set debug {on|off}";
  484. e->usage =
  485. "Usage: stun set debug {on|off}\n"
  486. " Enable/Disable STUN (Simple Traversal of UDP through NATs)\n"
  487. " debugging\n";
  488. return NULL;
  489. case CLI_GENERATE:
  490. return NULL;
  491. }
  492. if (a->argc != e->args)
  493. return CLI_SHOWUSAGE;
  494. if (!strncasecmp(a->argv[e->args-1], "on", 2))
  495. ast_debug_category_set_sublevel(AST_LOG_CATEGORY_STUN_PACKET, AST_LOG_CATEGORY_ENABLED);
  496. else if (!strncasecmp(a->argv[e->args-1], "off", 3))
  497. ast_debug_category_set_sublevel(AST_LOG_CATEGORY_STUN_PACKET, AST_LOG_CATEGORY_DISABLED);
  498. else
  499. return CLI_SHOWUSAGE;
  500. ast_cli(a->fd, "STUN Debugging %s\n", ast_debug_stun_packet_is_allowed ? "Enabled" : "Disabled");
  501. return CLI_SUCCESS;
  502. }
  503. static struct ast_cli_entry cli_stun[] = {
  504. AST_CLI_DEFINE(handle_cli_stun_set_debug, "Enable/Disable STUN debugging"),
  505. };
  506. static uintmax_t debug_category_stun_id;
  507. uintmax_t ast_debug_category_stun_id(void)
  508. {
  509. return debug_category_stun_id;
  510. }
  511. static uintmax_t debug_category_stun_packet_id;
  512. uintmax_t ast_debug_category_stun_packet_id(void)
  513. {
  514. return debug_category_stun_packet_id;
  515. }
  516. static void stun_shutdown(void)
  517. {
  518. ast_cli_unregister_multiple(cli_stun, sizeof(cli_stun) / sizeof(struct ast_cli_entry));
  519. ast_debug_category_unregister(AST_LOG_CATEGORY_STUN_PACKET);
  520. ast_debug_category_unregister(AST_LOG_CATEGORY_STUN);
  521. }
  522. /*! \brief Initialize the STUN system in Asterisk */
  523. void ast_stun_init(void)
  524. {
  525. ast_cli_register_multiple(cli_stun, sizeof(cli_stun) / sizeof(struct ast_cli_entry));
  526. debug_category_stun_id = ast_debug_category_register(AST_LOG_CATEGORY_STUN);
  527. debug_category_stun_packet_id = ast_debug_category_register(AST_LOG_CATEGORY_STUN_PACKET);
  528. /*
  529. * Normnally a core module should call ast_register_cleanup
  530. * which doesn't run if any module fails to unload. This
  531. * prevents resources being pulled out from under a running
  532. * module and ppossibly causing a segfault. In this case however,
  533. * the only thing we're cleaning up is the cli command and
  534. * the registers of the debug categories.
  535. */
  536. ast_register_atexit(stun_shutdown);
  537. }