route.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * $Id$
  3. *
  4. * SIP routing engine
  5. *
  6. *
  7. * Copyright (C) 2001-2003 Fhg Fokus
  8. *
  9. * This file is part of ser, a free SIP server.
  10. *
  11. * ser is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * For a license to use the ser software under conditions
  17. * other than those described here, or to purchase support for this
  18. * software, please contact iptel.org by e-mail at the following addresses:
  19. * [email protected]
  20. *
  21. * ser is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #include <stdlib.h>
  31. #include <sys/types.h>
  32. #include <regex.h>
  33. #include <netdb.h>
  34. #include <string.h>
  35. #include <sys/socket.h>
  36. #include <netinet/in.h>
  37. #include <arpa/inet.h>
  38. #include <netdb.h>
  39. #include "route.h"
  40. #include "forward.h"
  41. #include "dprint.h"
  42. #include "proxy.h"
  43. #include "action.h"
  44. #include "sr_module.h"
  45. #include "ip_addr.h"
  46. #include "resolve.h"
  47. #include "parser/parse_uri.h"
  48. #ifdef DEBUG_DMALLOC
  49. #include <dmalloc.h>
  50. #endif
  51. /* main routing script table */
  52. struct action* rlist[RT_NO];
  53. /* reply routing table */
  54. struct action* reply_rlist[REPLY_RT_NO];
  55. static int fix_actions(struct action* a); /*fwd declaration*/
  56. /* traverses an expr tree and compiles the REs where necessary)
  57. * returns: 0 for ok, <0 if errors */
  58. static int fix_expr(struct expr* exp)
  59. {
  60. regex_t* re;
  61. int ret;
  62. ret=E_BUG;
  63. if (exp==0){
  64. LOG(L_CRIT, "BUG: fix_expr: null pointer\n");
  65. return E_BUG;
  66. }
  67. if (exp->type==EXP_T){
  68. switch(exp->op){
  69. case AND_OP:
  70. case OR_OP:
  71. if ((ret=fix_expr(exp->l.expr))!=0)
  72. return ret;
  73. ret=fix_expr(exp->r.expr);
  74. break;
  75. case NOT_OP:
  76. ret=fix_expr(exp->l.expr);
  77. break;
  78. default:
  79. LOG(L_CRIT, "BUG: fix_expr: unknown op %d\n",
  80. exp->op);
  81. }
  82. }else if (exp->type==ELEM_T){
  83. if (exp->op==MATCH_OP){
  84. if (exp->subtype==STRING_ST){
  85. re=(regex_t*)malloc(sizeof(regex_t));
  86. if (re==0){
  87. LOG(L_CRIT, "ERROR: fix_expr: memory allocation"
  88. " failure\n");
  89. return E_OUT_OF_MEM;
  90. }
  91. if (regcomp(re, (char*) exp->r.param,
  92. REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
  93. LOG(L_CRIT, "ERROR: fix_expr : bad re \"%s\"\n",
  94. (char*) exp->r.param);
  95. free(re);
  96. return E_BAD_RE;
  97. }
  98. /* replace the string with the re */
  99. free(exp->r.param);
  100. exp->r.param=re;
  101. exp->subtype=RE_ST;
  102. }else if (exp->subtype!=RE_ST){
  103. LOG(L_CRIT, "BUG: fix_expr : invalid type for match\n");
  104. return E_BUG;
  105. }
  106. }
  107. if (exp->l.operand==ACTION_O){
  108. ret=fix_actions((struct action*)exp->r.param);
  109. if (ret!=0){
  110. LOG(L_CRIT, "ERROR: fix_expr : fix_actions error\n");
  111. return ret;
  112. }
  113. }
  114. ret=0;
  115. }
  116. return ret;
  117. }
  118. /* adds the proxies in the proxy list & resolves the hostnames */
  119. /* returns 0 if ok, <0 on error */
  120. static int fix_actions(struct action* a)
  121. {
  122. struct action *t;
  123. struct proxy_l* p;
  124. char *tmp;
  125. int ret,r;
  126. struct sr_module* mod;
  127. str s;
  128. if (a==0){
  129. LOG(L_CRIT,"BUG: fix_actions: null pointer\n");
  130. return E_BUG;
  131. }
  132. for(t=a; t!=0; t=t->next){
  133. switch(t->type){
  134. case FORWARD_T:
  135. case FORWARD_TCP_T:
  136. case FORWARD_UDP_T:
  137. case SEND_T:
  138. case SEND_TCP_T:
  139. switch(t->p1_type){
  140. case IP_ST:
  141. tmp=strdup(ip_addr2a(
  142. (struct ip_addr*)t->p1.data));
  143. if (tmp==0){
  144. LOG(L_CRIT, "ERROR: fix_actions:"
  145. "memory allocation failure\n");
  146. return E_OUT_OF_MEM;
  147. }
  148. t->p1_type=STRING_ST;
  149. t->p1.string=tmp;
  150. /* no break */
  151. case STRING_ST:
  152. s.s = t->p1.string;
  153. s.len = strlen(s.s);
  154. p=add_proxy(&s, t->p2.number);
  155. if (p==0) return E_BAD_ADDRESS;
  156. t->p1.data=p;
  157. t->p1_type=PROXY_ST;
  158. break;
  159. case URIHOST_ST:
  160. break;
  161. default:
  162. LOG(L_CRIT, "BUG: fix_actions: invalid type"
  163. "%d (should be string or number)\n",
  164. t->type);
  165. return E_BUG;
  166. }
  167. break;
  168. case IF_T:
  169. if (t->p1_type!=EXPR_ST){
  170. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  171. "%d for if (should be expr)\n",
  172. t->p1_type);
  173. return E_BUG;
  174. }else if( (t->p2_type!=ACTIONS_ST)&&(t->p2_type!=NOSUBTYPE) ){
  175. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  176. "%d for if() {...} (should be action)\n",
  177. t->p2_type);
  178. return E_BUG;
  179. }else if( (t->p3_type!=ACTIONS_ST)&&(t->p3_type!=NOSUBTYPE) ){
  180. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  181. "%d for if() {} else{...}(should be action)\n",
  182. t->p3_type);
  183. return E_BUG;
  184. }
  185. if (t->p1.data){
  186. if ((ret=fix_expr((struct expr*)t->p1.data))<0)
  187. return ret;
  188. }
  189. if ( (t->p2_type==ACTIONS_ST)&&(t->p2.data) ){
  190. if ((ret=fix_actions((struct action*)t->p2.data))<0)
  191. return ret;
  192. }
  193. if ( (t->p3_type==ACTIONS_ST)&&(t->p3.data) ){
  194. if ((ret=fix_actions((struct action*)t->p3.data))<0)
  195. return ret;
  196. }
  197. break;
  198. case MODULE_T:
  199. if ((mod=find_module(t->p1.data, &r))!=0){
  200. DBG("fixing %s %s\n", mod->path,
  201. mod->exports->cmd_names[r]);
  202. if (mod->exports->fixup_pointers[r]){
  203. if (mod->exports->param_no[r]>0){
  204. ret=mod->exports->fixup_pointers[r](&t->p2.data,
  205. 1);
  206. t->p2_type=MODFIXUP_ST;
  207. if (ret<0) return ret;
  208. }
  209. if (mod->exports->param_no[r]>1){
  210. ret=mod->exports->fixup_pointers[r](&t->p3.data,
  211. 2);
  212. t->p3_type=MODFIXUP_ST;
  213. if (ret<0) return ret;
  214. }
  215. }
  216. }
  217. }
  218. }
  219. return 0;
  220. }
  221. /* eval_elem helping function, returns str op param */
  222. static int comp_str(char* str, void* param, int op, int subtype)
  223. {
  224. int ret;
  225. ret=-1;
  226. if (op==EQUAL_OP){
  227. if (subtype!=STRING_ST){
  228. LOG(L_CRIT, "BUG: comp_str: bad type %d, "
  229. "string expected\n", subtype);
  230. goto error;
  231. }
  232. ret=(strcasecmp(str, (char*)param)==0);
  233. }else if (op==MATCH_OP){
  234. if (subtype!=RE_ST){
  235. LOG(L_CRIT, "BUG: comp_str: bad type %d, "
  236. " RE expected\n", subtype);
  237. goto error;
  238. }
  239. ret=(regexec((regex_t*)param, str, 0, 0, 0)==0);
  240. }else{
  241. LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
  242. goto error;
  243. }
  244. return ret;
  245. error:
  246. return -1;
  247. }
  248. /* eval_elem helping function, returns an op param */
  249. static int comp_ip(struct ip_addr* ip, void* param, int op, int subtype)
  250. {
  251. struct hostent* he;
  252. char ** h;
  253. int ret;
  254. str tmp;
  255. ret=-1;
  256. switch(subtype){
  257. case NET_ST:
  258. ret=matchnet(ip, (struct net*) param);
  259. /*ret=(a&((struct net*)param)->mask)==((struct net*)param)->ip;*/
  260. break;
  261. case STRING_ST:
  262. case RE_ST:
  263. /* 1: compare with ip2str*/
  264. /* !!!??? review reminder ( resolve(name) & compare w/ all ips? */
  265. #if 0
  266. ret=comp_str(inet_ntoa(*(struct in_addr*)&a), param, op,
  267. subtype);
  268. if (ret==1) break;
  269. #endif
  270. /* 2: (slow) rev dns the address
  271. * and compare with all the aliases */
  272. he=rev_resolvehost(ip);
  273. if (he==0){
  274. DBG( "comp_ip: could not rev_resolve ip address: ");
  275. print_ip(ip);
  276. DBG("\n");
  277. ret=0;
  278. }else{
  279. /* compare with primary host name */
  280. ret=comp_str(he->h_name, param, op, subtype);
  281. /* compare with all the aliases */
  282. for(h=he->h_aliases; (ret!=1) && (*h); h++){
  283. ret=comp_str(*h, param, op, subtype);
  284. }
  285. }
  286. break;
  287. case MYSELF_ST: /* check if it's one of our addresses*/
  288. tmp.s=ip_addr2a(ip);
  289. tmp.len=strlen(tmp.s);
  290. ret=check_self(&tmp, 0);
  291. break;
  292. default:
  293. LOG(L_CRIT, "BUG: comp_ip: invalid type for "
  294. " src_ip or dst_ip (%d)\n", subtype);
  295. ret=-1;
  296. }
  297. return ret;
  298. }
  299. /* returns: 0/1 (false/true) or -1 on error, -127 EXPR_DROP */
  300. static int eval_elem(struct expr* e, struct sip_msg* msg)
  301. {
  302. int ret;
  303. ret=E_BUG;
  304. if (e->type!=ELEM_T){
  305. LOG(L_CRIT," BUG: eval_elem: invalid type\n");
  306. goto error;
  307. }
  308. switch(e->l.operand){
  309. case METHOD_O:
  310. ret=comp_str(msg->first_line.u.request.method.s, e->r.param,
  311. e->op, e->subtype);
  312. break;
  313. case URI_O:
  314. if(msg->new_uri.s){
  315. if (e->subtype==MYSELF_ST){
  316. if (parse_sip_msg_uri(msg)<0) ret=-1;
  317. else ret=check_self(&msg->parsed_uri.host,
  318. msg->parsed_uri.port_no?
  319. msg->parsed_uri.port_no:SIP_PORT);
  320. }else{
  321. ret=comp_str(msg->new_uri.s, e->r.param,
  322. e->op, e->subtype);
  323. }
  324. }else{
  325. if (e->subtype==MYSELF_ST){
  326. if (parse_sip_msg_uri(msg)<0) ret=-1;
  327. else ret=check_self(&msg->parsed_uri.host,
  328. msg->parsed_uri.port_no?
  329. msg->parsed_uri.port_no:SIP_PORT);
  330. }else{
  331. ret=comp_str(msg->first_line.u.request.uri.s,
  332. e->r.param, e->op, e->subtype);
  333. }
  334. }
  335. break;
  336. case SRCIP_O:
  337. ret=comp_ip(&msg->rcv.src_ip, e->r.param, e->op, e->subtype);
  338. break;
  339. case DSTIP_O:
  340. ret=comp_ip(&msg->rcv.dst_ip, e->r.param, e->op, e->subtype);
  341. break;
  342. case NUMBER_O:
  343. ret=!(!e->r.intval); /* !! to transform it in {0,1} */
  344. break;
  345. case ACTION_O:
  346. ret=run_actions( (struct action*)e->r.param, msg);
  347. if (ret<=0) ret=(ret==0)?EXPR_DROP:0;
  348. else ret=1;
  349. break;
  350. default:
  351. LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
  352. e->l.operand);
  353. }
  354. return ret;
  355. error:
  356. return -1;
  357. }
  358. /* ret= 0/1 (true/false) , -1 on error or EXPR_DROP (-127) */
  359. int eval_expr(struct expr* e, struct sip_msg* msg)
  360. {
  361. static int rec_lev=0;
  362. int ret;
  363. rec_lev++;
  364. if (rec_lev>MAX_REC_LEV){
  365. LOG(L_CRIT, "ERROR: eval_expr: too many expressions (%d)\n",
  366. rec_lev);
  367. ret=-1;
  368. goto skip;
  369. }
  370. if (e->type==ELEM_T){
  371. ret=eval_elem(e, msg);
  372. }else if (e->type==EXP_T){
  373. switch(e->op){
  374. case AND_OP:
  375. ret=eval_expr(e->l.expr, msg);
  376. /* if error or false stop evaluating the rest */
  377. if (ret!=1) break;
  378. ret=eval_expr(e->r.expr, msg); /*ret1 is 1*/
  379. break;
  380. case OR_OP:
  381. ret=eval_expr(e->l.expr, msg);
  382. /* if true or error stop evaluating the rest */
  383. if (ret!=0) break;
  384. ret=eval_expr(e->r.expr, msg); /* ret1 is 0 */
  385. break;
  386. case NOT_OP:
  387. ret=eval_expr(e->l.expr, msg);
  388. if (ret<0) break;
  389. ret= ! ret;
  390. break;
  391. default:
  392. LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
  393. ret=-1;
  394. }
  395. }else{
  396. LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
  397. ret=-1;
  398. }
  399. skip:
  400. rec_lev--;
  401. return ret;
  402. }
  403. /* adds an action list to head; a must be null terminated (last a->next=0))*/
  404. void push(struct action* a, struct action** head)
  405. {
  406. struct action *t;
  407. if (*head==0){
  408. *head=a;
  409. return;
  410. }
  411. for (t=*head; t->next;t=t->next);
  412. t->next=a;
  413. }
  414. int add_actions(struct action* a, struct action** head)
  415. {
  416. int ret;
  417. LOG(L_DBG, "add_actions: fixing actions...\n");
  418. if ((ret=fix_actions(a))!=0) goto error;
  419. push(a,head);
  420. return 0;
  421. error:
  422. return ret;
  423. }
  424. /* fixes all action tables */
  425. /* returns 0 if ok , <0 on error */
  426. int fix_rls()
  427. {
  428. int i,ret;
  429. for(i=0;i<RT_NO;i++){
  430. if(rlist[i]){
  431. if ((ret=fix_actions(rlist[i]))!=0){
  432. return ret;
  433. }
  434. }
  435. }
  436. for(i=0;i<REPLY_RT_NO;i++){
  437. if(reply_rlist[i]){
  438. if ((ret=fix_actions(reply_rlist[i]))!=0){
  439. return ret;
  440. }
  441. }
  442. }
  443. return 0;
  444. }
  445. /* debug function, prints main routing table */
  446. void print_rl()
  447. {
  448. struct action* t;
  449. int i,j;
  450. for(j=0; j<RT_NO; j++){
  451. if (rlist[j]==0){
  452. if (j==0) DBG("WARNING: the main routing table is empty\n");
  453. continue;
  454. }
  455. DBG("routing table %d:\n",j);
  456. for (t=rlist[j],i=0; t; i++, t=t->next){
  457. print_action(t);
  458. }
  459. DBG("\n");
  460. }
  461. for(j=0; j<REPLY_RT_NO; j++){
  462. if (reply_rlist[j]==0){
  463. if (j==0) DBG("WARNING: the main reply routing table is empty\n");
  464. continue;
  465. }
  466. DBG("routing table %d:\n",j);
  467. for (t=reply_rlist[j],i=0; t; i++, t=t->next){
  468. print_action(t);
  469. }
  470. DBG("\n");
  471. }
  472. }