udp_server.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. /** Kamailio core :: udp send and loop-receive functions.
  22. * @file udp_server.c
  23. * @ingroup core
  24. * Module: @ref core
  25. */
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <netinet/in.h>
  31. #include <netinet/in_systm.h>
  32. #include <netinet/ip.h>
  33. #include <errno.h>
  34. #include <arpa/inet.h>
  35. #ifdef __linux__
  36. #include <linux/types.h>
  37. #include <linux/errqueue.h>
  38. #endif
  39. #include "udp_server.h"
  40. #include "compiler_opt.h"
  41. #include "globals.h"
  42. #include "config.h"
  43. #include "dprint.h"
  44. #include "receive.h"
  45. #include "mem/mem.h"
  46. #include "ip_addr.h"
  47. #include "cfg/cfg_struct.h"
  48. #include "events.h"
  49. #include "stun.h"
  50. #ifdef USE_RAW_SOCKS
  51. #include "raw_sock.h"
  52. #endif /* USE_RAW_SOCKS */
  53. #ifdef DBG_MSG_QA
  54. /* message quality assurance -- frequently, bugs in ser have
  55. been indicated by zero characters or long whitespaces
  56. in generated messages; this debugging option aborts if
  57. any such message is sighted
  58. */
  59. static int dbg_msg_qa(char *buf, int len)
  60. {
  61. #define _DBG_WS_LEN 3
  62. #define _DBG_WS " "
  63. char *scan;
  64. int my_len;
  65. int space_cnt;
  66. enum { QA_ANY, QA_SPACE, QA_EOL1 } state;
  67. /* is there a zero character in there ? */
  68. if (memchr(buf, 0, len)) {
  69. LM_CRIT("message with 0 in it\n");
  70. return 0;
  71. }
  72. my_len=len;
  73. scan=buf;
  74. state=QA_ANY;
  75. space_cnt=0;
  76. while(my_len) {
  77. switch(*scan) {
  78. case ' ': if (state==QA_SPACE) {
  79. space_cnt++;
  80. if (space_cnt==4) {
  81. LM_CRIT("too many spaces\n");
  82. return 0;
  83. }
  84. } else space_cnt=0;
  85. state=QA_SPACE;
  86. break;
  87. case '\r': /* ignore */
  88. space_cnt=0;
  89. break;
  90. case '\n': /* don't proceed to body on EoH */
  91. if (state==QA_EOL1) goto qa_passed;
  92. space_cnt=0;
  93. state=QA_EOL1;
  94. break;
  95. default: space_cnt=0;
  96. state=QA_ANY;
  97. break;
  98. }
  99. scan++;
  100. my_len--;
  101. }
  102. qa_passed:
  103. return 1;
  104. }
  105. #endif
  106. int probe_max_receive_buffer( int udp_sock )
  107. {
  108. int optval;
  109. int ioptval;
  110. unsigned int ioptvallen;
  111. int foptval;
  112. unsigned int foptvallen;
  113. int voptval;
  114. unsigned int voptvallen;
  115. int phase=0;
  116. /* jku: try to increase buffer size as much as we can */
  117. ioptvallen=sizeof(ioptval);
  118. if (getsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF, (void*) &ioptval,
  119. &ioptvallen) == -1 )
  120. {
  121. LM_ERR("getsockopt: %s\n", strerror(errno));
  122. return -1;
  123. }
  124. if ( ioptval==0 )
  125. {
  126. LM_DBG("SO_RCVBUF initially set to 0; resetting to %d\n",
  127. BUFFER_INCREMENT );
  128. ioptval=BUFFER_INCREMENT;
  129. } else LM_INFO("SO_RCVBUF is initially %d\n", ioptval );
  130. for (optval=ioptval; ; ) {
  131. /* increase size; double in initial phase, add linearly later */
  132. if (phase==0) optval <<= 1; else optval+=BUFFER_INCREMENT;
  133. if (optval > maxbuffer){
  134. if (phase==1) break;
  135. else { phase=1; optval >>=1; continue; }
  136. }
  137. LM_DBG("trying SO_RCVBUF: %d\n", optval );
  138. if (setsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF,
  139. (void*)&optval, sizeof(optval)) ==-1){
  140. /* Solaris returns -1 if asked size too big; Linux ignores */
  141. LM_DBG("SOL_SOCKET failed for %d, phase %d: %s\n", optval, phase, strerror(errno));
  142. /* if setting buffer size failed and still in the aggressive
  143. phase, try less aggressively; otherwise give up
  144. */
  145. if (phase==0) { phase=1; optval >>=1 ; continue; }
  146. else break;
  147. }
  148. /* verify if change has taken effect */
  149. /* Linux note -- otherwise I would never know that; funny thing: Linux
  150. doubles size for which we asked in setsockopt
  151. */
  152. voptvallen=sizeof(voptval);
  153. if (getsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF, (void*) &voptval,
  154. &voptvallen) == -1 )
  155. {
  156. LM_ERR("getsockopt: %s\n", strerror(errno));
  157. return -1;
  158. } else {
  159. LM_DBG("setting SO_RCVBUF; set=%d,verify=%d\n",
  160. optval, voptval);
  161. if (voptval<optval) {
  162. LM_DBG("setting SO_RCVBUF has no effect\n");
  163. /* if setting buffer size failed and still in the aggressive
  164. phase, try less aggressively; otherwise give up
  165. */
  166. if (phase==0) { phase=1; optval >>=1 ; continue; }
  167. else break;
  168. }
  169. }
  170. } /* for ... */
  171. foptvallen=sizeof(foptval);
  172. if (getsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF, (void*) &foptval,
  173. &foptvallen) == -1 )
  174. {
  175. LM_ERR("getsockopt: %s\n", strerror(errno));
  176. return -1;
  177. }
  178. LM_INFO("SO_RCVBUF is finally %d\n", foptval );
  179. return 0;
  180. /* EoJKU */
  181. }
  182. #ifdef USE_MCAST
  183. /*
  184. * Setup multicast receiver
  185. */
  186. static int setup_mcast_rcvr(int sock, union sockaddr_union* addr)
  187. {
  188. struct ip_mreq mreq;
  189. struct ipv6_mreq mreq6;
  190. if (addr->s.sa_family==AF_INET){
  191. memcpy(&mreq.imr_multiaddr, &addr->sin.sin_addr,
  192. sizeof(struct in_addr));
  193. mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  194. if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,&mreq,
  195. sizeof(mreq))==-1){
  196. LM_ERR("setsockopt: %s\n", strerror(errno));
  197. return -1;
  198. }
  199. } else if (addr->s.sa_family==AF_INET6){
  200. memcpy(&mreq6.ipv6mr_multiaddr, &addr->sin6.sin6_addr,
  201. sizeof(struct in6_addr));
  202. mreq6.ipv6mr_interface = 0;
  203. #ifdef __OS_linux
  204. if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6,
  205. #else
  206. if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
  207. #endif
  208. sizeof(mreq6))==-1){
  209. LM_ERR("setsockopt:%s\n", strerror(errno));
  210. return -1;
  211. }
  212. } else {
  213. LM_ERR("setup_mcast_rcvr: Unsupported protocol family\n");
  214. return -1;
  215. }
  216. return 0;
  217. }
  218. #endif /* USE_MCAST */
  219. int udp_init(struct socket_info* sock_info)
  220. {
  221. union sockaddr_union* addr;
  222. int optval;
  223. #ifdef USE_MCAST
  224. unsigned char m_ttl, m_loop;
  225. #endif
  226. addr=&sock_info->su;
  227. /*
  228. addr=(union sockaddr_union*)pkg_malloc(sizeof(union sockaddr_union));
  229. if (addr==0){
  230. LM_ERR("out of memory\n");
  231. goto error;
  232. }
  233. */
  234. sock_info->proto=PROTO_UDP;
  235. if (init_su(addr, &sock_info->address, sock_info->port_no)<0){
  236. LM_ERR("could not init sockaddr_union\n");
  237. goto error;
  238. }
  239. sock_info->socket = socket(AF2PF(addr->s.sa_family), SOCK_DGRAM, 0);
  240. if (sock_info->socket==-1){
  241. LM_ERR("socket: %s\n", strerror(errno));
  242. goto error;
  243. }
  244. /* set sock opts? */
  245. optval=1;
  246. if (setsockopt(sock_info->socket, SOL_SOCKET, SO_REUSEADDR ,
  247. (void*)&optval, sizeof(optval)) ==-1){
  248. LM_ERR("setsockopt: %s\n", strerror(errno));
  249. goto error;
  250. }
  251. /* tos */
  252. optval = tos;
  253. if (addr->s.sa_family==AF_INET){
  254. if (setsockopt(sock_info->socket, IPPROTO_IP, IP_TOS, (void*)&optval,
  255. sizeof(optval)) ==-1){
  256. LM_WARN("setsockopt tos: %s\n", strerror(errno));
  257. /* continue since this is not critical */
  258. }
  259. } else if (addr->s.sa_family==AF_INET6){
  260. if (setsockopt(sock_info->socket, IPPROTO_IPV6, IPV6_TCLASS,
  261. (void*)&optval, sizeof(optval)) ==-1) {
  262. LM_WARN("setsockopt v6 tos: %s\n", strerror(errno));
  263. /* continue since this is not critical */
  264. }
  265. }
  266. #if defined (__OS_linux) && defined(UDP_ERRORS)
  267. optval=1;
  268. /* enable error receiving on unconnected sockets */
  269. if(setsockopt(sock_info->socket, SOL_IP, IP_RECVERR,
  270. (void*)&optval, sizeof(optval)) ==-1){
  271. LM_ERR("setsockopt: %s\n", strerror(errno));
  272. goto error;
  273. }
  274. #endif
  275. #if defined (__OS_linux)
  276. /* if pmtu_discovery=1 then set DF bit and do Path MTU discovery
  277. * disabled by default */
  278. optval= (pmtu_discovery) ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
  279. if(setsockopt(sock_info->socket, IPPROTO_IP, IP_MTU_DISCOVER,
  280. (void*)&optval, sizeof(optval)) ==-1){
  281. LM_ERR("setsockopt: %s\n", strerror(errno));
  282. goto error;
  283. }
  284. #endif
  285. #ifdef USE_MCAST
  286. if ((sock_info->flags & SI_IS_MCAST)
  287. && (setup_mcast_rcvr(sock_info->socket, addr)<0)){
  288. goto error;
  289. }
  290. /* set the multicast options */
  291. if (addr->s.sa_family==AF_INET){
  292. m_loop=mcast_loopback;
  293. if (setsockopt(sock_info->socket, IPPROTO_IP, IP_MULTICAST_LOOP,
  294. &m_loop, sizeof(m_loop))==-1){
  295. LM_WARN("setsockopt(IP_MULTICAST_LOOP): %s\n", strerror(errno));
  296. /* it's only a warning because we might get this error if the
  297. network interface doesn't support multicasting -- andrei */
  298. }
  299. if (mcast_ttl>=0){
  300. m_ttl=mcast_ttl;
  301. if (setsockopt(sock_info->socket, IPPROTO_IP, IP_MULTICAST_TTL,
  302. &m_ttl, sizeof(m_ttl))==-1){
  303. LM_WARN("setsockopt (IP_MULTICAST_TTL): %s\n", strerror(errno));
  304. }
  305. }
  306. } else if (addr->s.sa_family==AF_INET6){
  307. if (setsockopt(sock_info->socket, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  308. &mcast_loopback, sizeof(mcast_loopback))==-1){
  309. LM_WARN("setsockopt (IPV6_MULTICAST_LOOP): %s\n", strerror(errno));
  310. }
  311. if (mcast_ttl>=0){
  312. if (setsockopt(sock_info->socket, IPPROTO_IP, IPV6_MULTICAST_HOPS,
  313. &mcast_ttl, sizeof(mcast_ttl))==-1){
  314. LM_WARN("setssckopt (IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
  315. }
  316. }
  317. } else {
  318. LM_ERR("Unsupported protocol family %d\n", addr->s.sa_family);
  319. goto error;
  320. }
  321. #endif /* USE_MCAST */
  322. if ( probe_max_receive_buffer(sock_info->socket)==-1) goto error;
  323. if (bind(sock_info->socket, &addr->s, sockaddru_len(*addr))==-1){
  324. LM_ERR("bind(%x, %p, %d) on %s: %s\n",
  325. sock_info->socket, &addr->s,
  326. (unsigned)sockaddru_len(*addr),
  327. sock_info->address_str.s,
  328. strerror(errno));
  329. if (addr->s.sa_family==AF_INET6)
  330. LM_ERR("might be caused by using a link local address, try site local or global\n");
  331. goto error;
  332. }
  333. /* pkg_free(addr);*/
  334. return 0;
  335. error:
  336. /* if (addr) pkg_free(addr);*/
  337. return -1;
  338. }
  339. int udp_rcv_loop()
  340. {
  341. unsigned len;
  342. #ifdef DYN_BUF
  343. char* buf;
  344. #else
  345. static char buf [BUF_SIZE+1];
  346. #endif
  347. char *tmp;
  348. union sockaddr_union* from;
  349. unsigned int fromlen;
  350. struct receive_info ri;
  351. from=(union sockaddr_union*) pkg_malloc(sizeof(union sockaddr_union));
  352. if (from==0){
  353. LM_ERR("out of memory\n");
  354. goto error;
  355. }
  356. memset(from, 0 , sizeof(union sockaddr_union));
  357. ri.bind_address=bind_address; /* this will not change, we do it only once*/
  358. ri.dst_port=bind_address->port_no;
  359. ri.dst_ip=bind_address->address;
  360. ri.proto=PROTO_UDP;
  361. ri.proto_reserved1=ri.proto_reserved2=0;
  362. /* initialize the config framework */
  363. if (cfg_child_init()) goto error;
  364. for(;;){
  365. #ifdef DYN_BUF
  366. buf=pkg_malloc(BUF_SIZE+1);
  367. if (buf==0){
  368. LM_ERR("could not allocate receive buffer\n");
  369. goto error;
  370. }
  371. #endif
  372. fromlen=sockaddru_len(bind_address->su);
  373. len=recvfrom(bind_address->socket, buf, BUF_SIZE, 0, &from->s,
  374. &fromlen);
  375. if (len==-1){
  376. if (errno==EAGAIN){
  377. LM_DBG("packet with bad checksum received\n");
  378. continue;
  379. }
  380. LM_ERR("recvfrom:[%d] %s\n", errno, strerror(errno));
  381. if ((errno==EINTR)||(errno==EWOULDBLOCK)|| (errno==ECONNREFUSED))
  382. continue; /* goto skip;*/
  383. else goto error;
  384. }
  385. /* we must 0-term the messages, receive_msg expects it */
  386. buf[len]=0; /* no need to save the previous char */
  387. ri.src_su=*from;
  388. su2ip_addr(&ri.src_ip, from);
  389. ri.src_port=su_getport(from);
  390. if(unlikely(sr_event_enabled(SREV_NET_DGRAM_IN)))
  391. {
  392. void *sredp[3];
  393. sredp[0] = (void*)buf;
  394. sredp[1] = (void*)(&len);
  395. sredp[2] = (void*)(&ri);
  396. if(sr_event_exec(SREV_NET_DGRAM_IN, (void*)sredp)<0) {
  397. /* data handled by callback - continue to next packet */
  398. continue;
  399. }
  400. }
  401. #ifndef NO_ZERO_CHECKS
  402. if (!unlikely(sr_event_enabled(SREV_STUN_IN)) || (unsigned char)*buf != 0x00) {
  403. if (len<MIN_UDP_PACKET) {
  404. tmp=ip_addr2a(&ri.src_ip);
  405. LM_DBG("probing packet received from %s %d\n", tmp, htons(ri.src_port));
  406. continue;
  407. }
  408. }
  409. /* historically, zero-terminated packets indicated a bug in clients
  410. * that calculated wrongly packet length and included string-terminating
  411. * zero; today clients exist with legitimate binary payloads and we
  412. * shall not check for zero-terminated payloads
  413. */
  414. #ifdef TRASH_ZEROTERMINATED_PACKETS
  415. if (buf[len-1]==0) {
  416. tmp=ip_addr2a(&ri.src_ip);
  417. LM_WARN("upstream bug - 0-terminated packet from %s %d\n",
  418. tmp, htons(ri.src_port));
  419. len--;
  420. }
  421. #endif
  422. #endif
  423. #ifdef DBG_MSG_QA
  424. if (!dbg_msg_qa(buf, len)) {
  425. LM_WARN("an incoming message didn't pass test,"
  426. " drop it: %.*s\n", len, buf );
  427. continue;
  428. }
  429. #endif
  430. if (ri.src_port==0){
  431. tmp=ip_addr2a(&ri.src_ip);
  432. LM_INFO("dropping 0 port packet from %s\n", tmp);
  433. continue;
  434. }
  435. /* update the local config */
  436. cfg_update();
  437. if (unlikely(sr_event_enabled(SREV_STUN_IN)) && (unsigned char)*buf == 0x00) {
  438. /* stun_process_msg releases buf memory if necessary */
  439. if ((stun_process_msg(buf, len, &ri)) != 0) {
  440. continue; /* some error occurred */
  441. }
  442. } else {
  443. /* receive_msg must free buf too!*/
  444. receive_msg(buf, len, &ri);
  445. }
  446. /* skip: do other stuff */
  447. }
  448. /*
  449. if (from) pkg_free(from);
  450. return 0;
  451. */
  452. error:
  453. if (from) pkg_free(from);
  454. return -1;
  455. }
  456. /* send buf:len over udp to dst (uses only the to and send_sock dst members)
  457. * returns the numbers of bytes sent on success (>=0) and -1 on error
  458. */
  459. int udp_send(struct dest_info* dst, char *buf, unsigned len)
  460. {
  461. int n;
  462. int tolen;
  463. struct ip_addr ip; /* used only on error, for debugging */
  464. #ifdef USE_RAW_SOCKS
  465. int mtu;
  466. #endif /* USE_RAW_SOCKS */
  467. #ifdef DBG_MSG_QA
  468. /* aborts on error, does nothing otherwise */
  469. if (!dbg_msg_qa( buf, len )) {
  470. LM_ERR("dbg_msg_qa failed\n");
  471. abort();
  472. }
  473. #endif
  474. #ifdef USE_RAW_SOCKS
  475. if (likely( ! (raw_udp4_send_sock >= 0 &&
  476. cfg_get(core, core_cfg, udp4_raw) &&
  477. dst->send_sock->address.af == AF_INET) )) {
  478. #endif /* USE_RAW_SOCKS */
  479. /* normal send over udp socket */
  480. tolen=sockaddru_len(dst->to);
  481. again:
  482. n=sendto(dst->send_sock->socket, buf, len, 0, &dst->to.s, tolen);
  483. #ifdef XL_DEBUG
  484. LM_INFO("send status: %d\n", n);
  485. #endif
  486. if (unlikely(n==-1)){
  487. su2ip_addr(&ip, &dst->to);
  488. LM_ERR("sendto(sock,%p,%u,0,%s:%d,%d): %s(%d)\n",
  489. buf,len, ip_addr2a(&ip),
  490. su_getport(&dst->to), tolen, strerror(errno), errno);
  491. if (errno==EINTR) goto again;
  492. if (errno==EINVAL) {
  493. LM_CRIT("invalid sendtoparameters\n"
  494. "one possible reason is the server is bound to localhost and\n"
  495. "attempts to send to the net\n");
  496. }
  497. }
  498. #ifdef USE_RAW_SOCKS
  499. } else {
  500. /* send over a raw socket */
  501. mtu = cfg_get(core, core_cfg, udp4_raw_mtu);
  502. raw_again:
  503. n=raw_iphdr_udp4_send(raw_udp4_send_sock, buf, len,
  504. &dst->send_sock->su,
  505. &dst->to,
  506. mtu);
  507. if (unlikely(n==-1)){
  508. su2ip_addr(&ip, &dst->to);
  509. LM_ERR("raw_iphdr_udp4_send(%d,%p,%u,...,%s:%d,%d): %s(%d)\n",
  510. raw_udp4_send_sock, buf,len, ip_addr2a(&ip),
  511. su_getport(&dst->to), mtu, strerror(errno), errno);
  512. if (errno==EINTR) goto raw_again;
  513. }
  514. }
  515. #endif /* USE_RAW_SOCKS */
  516. return n;
  517. }