hep.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * $Id$
  3. *
  4. * hep related functions
  5. *
  6. * Copyright (C) 2011-14 Alexandr Dubovikov <[email protected]>
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. */
  25. #include "../../sr_module.h"
  26. #include "../../dprint.h"
  27. #include "../../events.h"
  28. #include "../../ut.h"
  29. #include "../../ip_addr.h"
  30. #include "../../mem/mem.h"
  31. #include "../../mem/shm_mem.h"
  32. #include "../../lib/srdb1/db.h"
  33. #include "../../receive.h"
  34. #include "hep.h"
  35. #include "sipcapture.h"
  36. static int count = 0;
  37. struct hep_timehdr* heptime;
  38. /* HEPv2 HEPv3 */
  39. int hepv2_received(char *buf, unsigned int len, struct receive_info *ri);
  40. int hepv3_received(char *buf, unsigned int len, struct receive_info *ri);
  41. int parsing_hepv3_message(char *buf, unsigned int len);
  42. /**
  43. * HEP message
  44. */
  45. /* int hep_msg_received(char * buf, unsigned int len, struct receive_info * ri) */
  46. int hep_msg_received(void *data)
  47. {
  48. void **srevp;
  49. char *buf;
  50. unsigned *len;
  51. struct receive_info *ri;
  52. if(!hep_capture_on) {
  53. LOG(L_ERR, "sipcapture:hep_msg_received HEP is not enabled\n");
  54. return -1;
  55. }
  56. srevp = (void**)data;
  57. buf = (char *)srevp[0];
  58. len = (unsigned *)srevp[1];
  59. ri = (struct receive_info *)srevp[2];
  60. count++;
  61. struct hep_hdr *heph;
  62. /* hep_hdr */
  63. heph = (struct hep_hdr*) buf;
  64. /* Check version */
  65. if(heph->hp_v == 1 || heph->hp_v == 2) {
  66. return hepv2_received(buf, *len, ri);
  67. }
  68. else if(!memcmp(buf, "\x48\x45\x50\x33",4)) {
  69. return hepv3_received(buf, *len, ri);
  70. }
  71. else {
  72. LOG(L_ERR, "ERROR: sipcapture:hep_msg_received: not supported version or bad length: v:[%d] l:[%d]\n",
  73. heph->hp_v, heph->hp_l);
  74. return -1;
  75. }
  76. }
  77. int hepv2_received(char *buf, unsigned int len, struct receive_info *ri){
  78. int hl;
  79. struct hep_hdr *heph;
  80. struct ip_addr dst_ip, src_ip;
  81. char *hep_payload, *end, *hep_ip;
  82. struct hep_iphdr *hepiph = NULL;
  83. struct hep_timehdr* heptime_tmp = NULL;
  84. memset(heptime, 0, sizeof(struct hep_timehdr));
  85. struct hep_ip6hdr *hepip6h = NULL;
  86. hep_offset = 0;
  87. hl = hep_offset = sizeof(struct hep_hdr);
  88. end = buf + len;
  89. if (unlikely(len<hep_offset)) {
  90. LOG(L_ERR, "ERROR: sipcapture:hep_msg_received len less than offset [%i] vs [%i]\n", len, hep_offset);
  91. return -1;
  92. }
  93. /* hep_hdr */
  94. heph = (struct hep_hdr*) buf;
  95. switch(heph->hp_f){
  96. case AF_INET:
  97. hl += sizeof(struct hep_iphdr);
  98. break;
  99. case AF_INET6:
  100. hl += sizeof(struct hep_ip6hdr);
  101. break;
  102. default:
  103. LOG(L_ERR, "ERROR: sipcapture:hep_msg_received: unsupported family [%d]\n", heph->hp_f);
  104. return -1;
  105. }
  106. /* PROTO */
  107. if(heph->hp_p == IPPROTO_UDP) ri->proto=PROTO_UDP;
  108. else if(heph->hp_p == IPPROTO_TCP) ri->proto=PROTO_TCP;
  109. else if(heph->hp_p == IPPROTO_IDP) ri->proto=PROTO_TLS; /* fake protocol */
  110. #ifdef USE_SCTP
  111. else if(heph->hp_p == IPPROTO_SCTP) ri->proto=PROTO_SCTP;
  112. #endif
  113. else {
  114. LOG(L_ERR, "ERROR: sipcapture:hep_msg_received: unknow protocol [%d]\n",heph->hp_p);
  115. ri->proto = PROTO_NONE;
  116. }
  117. hep_ip = buf + sizeof(struct hep_hdr);
  118. if (unlikely(hep_ip>end)){
  119. LOG(L_ERR,"hep_ip is over buf+len\n");
  120. return -1;
  121. }
  122. switch(heph->hp_f){
  123. case AF_INET:
  124. hep_offset+=sizeof(struct hep_iphdr);
  125. hepiph = (struct hep_iphdr*) hep_ip;
  126. break;
  127. case AF_INET6:
  128. hep_offset+=sizeof(struct hep_ip6hdr);
  129. hepip6h = (struct hep_ip6hdr*) hep_ip;
  130. break;
  131. }
  132. /* VOIP payload */
  133. hep_payload = buf + hep_offset;
  134. if (unlikely(hep_payload>end)){
  135. LOG(L_ERR,"hep_payload is over buf+len\n");
  136. return -1;
  137. }
  138. /* timming */
  139. if(heph->hp_v == 2) {
  140. hep_offset+=sizeof(struct hep_timehdr);
  141. heptime_tmp = (struct hep_timehdr*) hep_payload;
  142. heptime->tv_sec = heptime_tmp->tv_sec;
  143. heptime->tv_usec = heptime_tmp->tv_usec;
  144. heptime->captid = heptime_tmp->captid;
  145. }
  146. /* fill ip from the packet to dst_ip && to */
  147. switch(heph->hp_f){
  148. case AF_INET:
  149. dst_ip.af = src_ip.af = AF_INET;
  150. dst_ip.len = src_ip.len = 4 ;
  151. memcpy(&dst_ip.u.addr, &hepiph->hp_dst, 4);
  152. memcpy(&src_ip.u.addr, &hepiph->hp_src, 4);
  153. break;
  154. case AF_INET6:
  155. dst_ip.af = src_ip.af = AF_INET6;
  156. dst_ip.len = src_ip.len = 16 ;
  157. memcpy(&dst_ip.u.addr, &hepip6h->hp6_dst, 16);
  158. memcpy(&src_ip.u.addr, &hepip6h->hp6_src, 16);
  159. break;
  160. }
  161. ri->src_ip = src_ip;
  162. ri->src_port = ntohs(heph->hp_sport);
  163. ri->dst_ip = dst_ip;
  164. ri->dst_port = ntohs(heph->hp_dport);
  165. /* cut off the offset */
  166. /*
  167. * len -= offset;
  168. * p = buf + offset;
  169. * memmove(buf, p, BUF_SIZE+1);
  170. */
  171. hep_payload = buf + hep_offset;
  172. receive_msg(hep_payload,(unsigned int)(len - hep_offset), ri);
  173. return -1;
  174. }
  175. /**
  176. * HEP message
  177. */
  178. int hepv3_received(char *buf, unsigned int len, struct receive_info *ri)
  179. {
  180. if(!parsing_hepv3_message(buf, len)) {
  181. LM_ERR("couldn't parse hepv3 message\n");
  182. return -2;
  183. }
  184. return -1;
  185. }
  186. int parsing_hepv3_message(char *buf, unsigned int len) {
  187. union sockaddr_union from;
  188. union sockaddr_union to;
  189. struct receive_info ri;
  190. char *tmp;
  191. struct ip_addr dst_ip, src_ip;
  192. struct socket_info* si = 0;
  193. int tmp_len, i;
  194. char *payload = NULL;
  195. unsigned int payload_len = 0;
  196. struct hep_chunk *chunk;
  197. struct hep_generic_recv *hg;
  198. int totelem = 0;
  199. int chunk_vendor=0, chunk_type=0, chunk_length=0;
  200. int total_length = 0;
  201. hg = (struct hep_generic_recv*)pkg_malloc(sizeof(struct hep_generic_recv));
  202. if(hg==NULL) {
  203. LM_ERR("no more pkg memory left for hg\n");
  204. return -1;
  205. }
  206. memset(hg, 0, sizeof(struct hep_generic_recv));
  207. memset(heptime, 0, sizeof(struct hep_timehdr));
  208. /* HEADER */
  209. hg->header = (hep_ctrl_t *) (buf);
  210. /*Packet size */
  211. total_length = ntohs(hg->header->length);
  212. ri.src_port = 0;
  213. ri.dst_port = 0;
  214. dst_ip.af = 0;
  215. src_ip.af = 0;
  216. payload = NULL;
  217. i = sizeof(hep_ctrl_t);
  218. while(i < total_length) {
  219. /*OUR TMP DATA */
  220. tmp = buf+i;
  221. chunk = (struct hep_chunk*) tmp;
  222. chunk_vendor = ntohs(chunk->vendor_id);
  223. chunk_type = ntohs(chunk->type_id);
  224. chunk_length = ntohs(chunk->length);
  225. /* if chunk_length */
  226. if(chunk_length == 0) {
  227. /* BAD LEN we drop this packet */
  228. goto error;
  229. }
  230. /* SKIP not general Chunks */
  231. if(chunk_vendor != 0) {
  232. i+=chunk_length;
  233. }
  234. else {
  235. switch(chunk_type) {
  236. case 0:
  237. goto error;
  238. break;
  239. case 1:
  240. hg->ip_family = (hep_chunk_uint8_t *) (tmp);
  241. i+=chunk_length;
  242. totelem++;
  243. break;
  244. case 2:
  245. hg->ip_proto = (hep_chunk_uint8_t *) (tmp);
  246. i+=chunk_length;
  247. totelem++;
  248. break;
  249. case 3:
  250. hg->hep_src_ip4 = (hep_chunk_ip4_t *) (tmp);
  251. i+=chunk_length;
  252. src_ip.af=AF_INET;
  253. src_ip.len=4;
  254. src_ip.u.addr32[0] = hg->hep_src_ip4->data.s_addr;
  255. totelem++;
  256. break;
  257. case 4:
  258. hg->hep_dst_ip4 = (hep_chunk_ip4_t *) (tmp);
  259. i+=chunk_length;
  260. dst_ip.af=AF_INET;
  261. dst_ip.len=4;
  262. dst_ip.u.addr32[0] = hg->hep_dst_ip4->data.s_addr;
  263. totelem++;
  264. break;
  265. case 5:
  266. hg->hep_src_ip6 = (hep_chunk_ip6_t *) (tmp);
  267. i+=chunk_length;
  268. src_ip.af=AF_INET6;
  269. src_ip.len=16;
  270. memcpy(src_ip.u.addr, &hg->hep_src_ip6->data, 16);
  271. totelem++;
  272. break;
  273. case 6:
  274. hg->hep_dst_ip6 = (hep_chunk_ip6_t *) (tmp);
  275. i+=chunk_length;
  276. dst_ip.af=AF_INET6;
  277. dst_ip.len=16;
  278. memcpy(dst_ip.u.addr, &hg->hep_dst_ip6->data, 16);
  279. totelem++;
  280. break;
  281. case 7:
  282. hg->src_port = (hep_chunk_uint16_t *) (tmp);
  283. ri.src_port = ntohs(hg->src_port->data);
  284. i+=chunk_length;
  285. totelem++;
  286. break;
  287. case 8:
  288. hg->dst_port = (hep_chunk_uint16_t *) (tmp);
  289. ri.dst_port = ntohs(hg->dst_port->data);
  290. i+=chunk_length;
  291. totelem++;
  292. break;
  293. case 9:
  294. hg->time_sec = (hep_chunk_uint32_t *) (tmp);
  295. hg->time_sec->data = ntohl(hg->time_sec->data);
  296. heptime->tv_sec = hg->time_sec->data;
  297. i+=chunk_length;
  298. totelem++;
  299. break;
  300. case 10:
  301. hg->time_usec = (hep_chunk_uint32_t *) (tmp);
  302. hg->time_usec->data = ntohl(hg->time_usec->data);
  303. heptime->tv_usec = hg->time_usec->data;
  304. i+=chunk_length;
  305. totelem++;
  306. break;
  307. case 11:
  308. hg->proto_t = (hep_chunk_uint8_t *) (tmp);
  309. i+=chunk_length;
  310. totelem++;
  311. break;
  312. case 12:
  313. hg->capt_id = (hep_chunk_uint32_t *) (tmp);
  314. i+=chunk_length;
  315. heptime->captid = ntohs(hg->capt_id->data);
  316. totelem++;
  317. break;
  318. case 13:
  319. hg->keep_tm = (hep_chunk_uint16_t *) (tmp);
  320. i+=chunk_length;
  321. break;
  322. case 14:
  323. authkey = (char *) tmp + sizeof(hep_chunk_t);
  324. i+=chunk_length;
  325. break;
  326. case 15:
  327. hg->payload_chunk = (hep_chunk_t *) (tmp);
  328. payload = (char *) tmp+sizeof(hep_chunk_t);
  329. payload_len = chunk_length - sizeof(hep_chunk_t);
  330. i+=chunk_length;
  331. totelem++;
  332. break;
  333. case 17:
  334. correlation_id = (char *) tmp + sizeof(hep_chunk_t);
  335. i+=chunk_length;
  336. break;
  337. default:
  338. i+=chunk_length;
  339. break;
  340. }
  341. }
  342. }
  343. /* CHECK how much elements */
  344. if(totelem < 9) {
  345. LM_ERR("Not all elements [%d]\n", totelem);
  346. goto done;
  347. }
  348. if ( dst_ip.af == 0 || src_ip.af == 0) {
  349. LM_ERR("NO IP's set\n");
  350. goto done;
  351. }
  352. ip_addr2su(&to, &dst_ip, ri.dst_port);
  353. ip_addr2su(&from, &src_ip, ri.src_port);
  354. ri.src_su=from;
  355. su2ip_addr(&ri.src_ip, &from);
  356. su2ip_addr(&ri.dst_ip, &to);
  357. if(hg->ip_proto->data == IPPROTO_TCP) ri.proto=PROTO_TCP;
  358. else if(hg->ip_proto->data == IPPROTO_UDP) ri.proto=PROTO_UDP;
  359. /* a little bit memory */
  360. si=(struct socket_info*) pkg_malloc(sizeof(struct socket_info));
  361. if (si==0) {
  362. LOG(L_ERR, "ERROR: new_sock_info: memory allocation error\n");
  363. goto error;
  364. }
  365. memset(si, 0, sizeof(struct socket_info));
  366. si->address = ri.dst_ip;
  367. si->socket=-1;
  368. /* set port & proto */
  369. si->port_no = ri.dst_port;
  370. if(hg->ip_proto->data == IPPROTO_TCP) si->proto=PROTO_TCP;
  371. else if(hg->ip_proto->data == IPPROTO_UDP) si->proto=PROTO_UDP;
  372. si->flags=0;
  373. si->addr_info_lst=0;
  374. si->address_str.s = ip_addr2a(&si->address);;
  375. si->address_str.len = strlen(si->address_str.s);
  376. si->port_no_str.s = int2str(si->port_no, &tmp_len);
  377. si->port_no_str.len = tmp_len;
  378. si->address_str.len = strlen(si->address_str.s);
  379. si->name.len = si->address_str.len;
  380. si->name.s = si->address_str.s;
  381. ri.bind_address=si;
  382. /*TIME*/
  383. heptime->tv_sec = hg->time_sec->data;
  384. heptime->tv_usec = hg->time_usec->data;
  385. heptime->captid = ntohs(hg->capt_id->data);
  386. if(payload != NULL ) {
  387. /* and now recieve message */
  388. if (hg->proto_t->data == 5) receive_logging_json_msg(payload, payload_len, hg, "rtcp_capture");
  389. else if (hg->proto_t->data == 100) receive_logging_json_msg(payload, payload_len, hg, "logs_capture");
  390. else receive_msg(payload, payload_len, &ri);
  391. }
  392. done:
  393. if(si) pkg_free(si);
  394. if(hg) pkg_free(hg);
  395. return 1;
  396. error:
  397. if(si) pkg_free(si);
  398. if(hg) pkg_free(hg);
  399. return -1;
  400. }