dst_blacklist.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. /*
  2. * $Id$
  3. *
  4. * resolver related functions
  5. *
  6. * Copyright (C) 2006 iptelorg GmbH
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser 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. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. /* History:
  30. * --------
  31. * 2006-07-29 created by andrei
  32. * 2007-05-39 added hooks for add; more locks to reduce contention (andrei)
  33. * 2007-06-26 added hooks for search (andrei)
  34. * 2007-07-30 added dst_blacklist_del() and dst_blacklist_add_to() (andrei)
  35. * 2007-07-30 dst blacklist measurements added (Gergo)
  36. * 2008-02-11 dns_blacklist_init cfg parameter is introduced (Miklos)
  37. * 2009-02-26 added dst_blacklist_su* variant (andrei)
  38. */
  39. /*!
  40. * \file
  41. * \brief SIP-router core ::
  42. * \ingroup core
  43. * Module: \ref core
  44. */
  45. #ifdef USE_DST_BLACKLIST
  46. #include "dst_blacklist.h"
  47. #include "globals.h"
  48. #include "cfg_core.h"
  49. #include "mem/shm_mem.h"
  50. #include "hashes.h"
  51. #include "locking.h"
  52. #include "timer.h"
  53. #include "timer_ticks.h"
  54. #include "ip_addr.h"
  55. #include "error.h"
  56. #include "rpc.h"
  57. #include "compiler_opt.h"
  58. #include "resolve.h" /* for str2ip */
  59. #ifdef USE_DST_BLACKLIST_STATS
  60. #include "pt.h"
  61. #endif
  62. struct dst_blst_entry{
  63. struct dst_blst_entry* next;
  64. ticks_t expire;
  65. unsigned short port;
  66. unsigned char proto;
  67. unsigned char flags; /* contains the address type + error flags */
  68. unsigned char ip[4]; /* 4 for ipv4, 16 for ipv6 */
  69. };
  70. #define DST_BLST_ENTRY_SIZE(b) \
  71. (sizeof(struct dst_blst_entry)+((b).flags&BLST_IS_IPV6)*12)
  72. #define DST_BLST_HASH_SIZE 1024
  73. #define DEFAULT_BLST_TIMER_INTERVAL 60 /* 1 min */
  74. /* lock method */
  75. #ifdef GEN_LOCK_T_UNLIMITED
  76. #define BLST_LOCK_PER_BUCKET
  77. #elif defined GEN_LOCK_SET_T_UNLIMITED
  78. #define BLST_LOCK_SET
  79. #else
  80. #define BLST_ONE_LOCK
  81. #endif
  82. #ifdef BLST_LOCK_PER_BUCKET
  83. /* lock included in the hash bucket */
  84. #define LOCK_BLST(h) lock_get(&dst_blst_hash[(h)].lock)
  85. #define UNLOCK_BLST(h) lock_release(&dst_blst_hash[(h)].lock)
  86. #elif defined BLST_LOCK_SET
  87. static gen_lock_set_t* blst_lock_set=0;
  88. #define LOCK_BLST(h) lock_set_get(blst_lock_set, (h))
  89. #define UNLOCK_BLST(h) lock_set_release(blst_lock_set, (h))
  90. #else
  91. /* use only one lock */
  92. static gen_lock_t* blst_lock=0;
  93. #define LOCK_BLST(h) lock_get(blst_lock)
  94. #define UNLOCK_BLST(h) lock_release(blst_lock)
  95. #endif
  96. #define BLST_HASH_STATS
  97. #ifdef BLST_HASH_STATS
  98. #define BLST_HASH_STATS_DEC(h) dst_blst_hash[(h)].entries--
  99. #define BLST_HASH_STATS_INC(h) dst_blst_hash[(h)].entries++
  100. #else
  101. #define BLST_HASH_STATS_DEC(h) do{}while(0)
  102. #define BLST_HASH_STATS_INC(h) do{}while(0)
  103. #endif
  104. struct dst_blst_lst_head{
  105. struct dst_blst_entry* first;
  106. #ifdef BLST_LOCK_PER_BUCKET
  107. gen_lock_t lock;
  108. #endif
  109. #ifdef BLST_HASH_STATS
  110. unsigned int entries;
  111. #endif
  112. };
  113. int dst_blacklist_init=1; /* if 0, the dst blacklist is not initialized at startup */
  114. static struct timer_ln* blst_timer_h=0;
  115. static volatile unsigned int* blst_mem_used=0;
  116. unsigned int blst_timer_interval=DEFAULT_BLST_TIMER_INTERVAL;
  117. struct dst_blst_lst_head* dst_blst_hash=0;
  118. #ifdef USE_DST_BLACKLIST_STATS
  119. struct t_dst_blacklist_stats* dst_blacklist_stats=0;
  120. #endif
  121. /* blacklist per protocol event ignore mask array */
  122. unsigned blst_proto_imask[PROTO_LAST+1];
  123. #ifdef DST_BLACKLIST_HOOKS
  124. /* there 2 types of callbacks supported: on add new entry to the blacklist
  125. * (DST_BLACKLIST_ADD_CB) and on blacklist search (DST_BLACKLIST_SEARCH_CB).
  126. * Both of them take a struct dest_info*, a flags pointer(unsigned char*),
  127. * and a struct sip_msg* as parameters. The flags can be changed.
  128. * A callback should return one of:
  129. * DST_BLACKLIST_CONTINUE - do nothing, let other callbacks run
  130. * DST_BLACKLIST_ACCEPT - for blacklist add: force accept immediately,
  131. * for blacklist search: force match and use
  132. * the flags as the blacklist search return.
  133. * ( so the flags should be set to some valid
  134. * non zero BLST flags value )
  135. * DST_BLACKLIST_DENY - for blacklist add: don't allow adding the
  136. * destination to the blacklist.
  137. * for blacklist search: force return not found
  138. */
  139. #define MAX_BLST_HOOKS 1
  140. struct blst_callbacks_lst{
  141. struct blacklist_hook* hooks;
  142. unsigned int max_hooks;
  143. int last_idx;
  144. };
  145. static struct blst_callbacks_lst blst_add_cb;
  146. static struct blst_callbacks_lst blst_search_cb;
  147. static int init_blst_callback_lst(struct blst_callbacks_lst* cb_lst, int max)
  148. {
  149. cb_lst->max_hooks=MAX_BLST_HOOKS;
  150. cb_lst->last_idx=0;
  151. cb_lst->hooks=pkg_malloc(cb_lst->max_hooks*sizeof(struct blacklist_hook));
  152. if (cb_lst->hooks==0)
  153. goto error;
  154. memset(cb_lst->hooks, 0, cb_lst->max_hooks*sizeof(struct blacklist_hook));
  155. return 0;
  156. error:
  157. return -1;
  158. }
  159. static void destroy_blst_callback_lst(struct blst_callbacks_lst* cb_lst)
  160. {
  161. int r;
  162. if (cb_lst && cb_lst->hooks){
  163. for (r=0; r<cb_lst->last_idx; r++){
  164. if (cb_lst->hooks[r].destroy)
  165. cb_lst->hooks[r].destroy();
  166. }
  167. pkg_free(cb_lst->hooks);
  168. cb_lst->hooks=0;
  169. cb_lst->last_idx=0;
  170. cb_lst->max_hooks=0;
  171. }
  172. }
  173. static void destroy_blacklist_hooks()
  174. {
  175. destroy_blst_callback_lst(&blst_add_cb);
  176. destroy_blst_callback_lst(&blst_search_cb);
  177. }
  178. static int init_blacklist_hooks()
  179. {
  180. if (init_blst_callback_lst(&blst_add_cb, MAX_BLST_HOOKS)!=0)
  181. goto error;
  182. if (init_blst_callback_lst(&blst_search_cb, MAX_BLST_HOOKS)!=0)
  183. goto error;
  184. return 0;
  185. error:
  186. LOG(L_ERR, "blacklist_hooks: failure initializing internal lists\n");
  187. destroy_blacklist_hooks();
  188. return -1;
  189. }
  190. /* allocates a new hook
  191. * returns 0 on success and -1 on error
  192. * must be called from mod init (from the main process, before forking)*/
  193. int register_blacklist_hook(struct blacklist_hook *h, int type)
  194. {
  195. struct blst_callbacks_lst* cb_lst;
  196. struct blacklist_hook* tmp;
  197. int new_max_hooks;
  198. if (dst_blacklist_init==0) {
  199. LOG(L_ERR, "register_blacklist_hook: blacklist is turned off, "
  200. "the hook cannot be registered\n");
  201. goto error;
  202. }
  203. switch(type){
  204. case DST_BLACKLIST_ADD_CB:
  205. cb_lst=&blst_add_cb;
  206. break;
  207. case DST_BLACKLIST_SEARCH_CB:
  208. cb_lst=&blst_search_cb;
  209. break;
  210. default:
  211. BUG("register_blacklist_hook: invalid type %d\n", type);
  212. goto error;
  213. }
  214. if (cb_lst==0 || cb_lst->hooks==0 || cb_lst->max_hooks==0){
  215. BUG("register_blacklist_hook: intialization error\n");
  216. goto error;
  217. }
  218. if (cb_lst->last_idx >= cb_lst->max_hooks){
  219. new_max_hooks=2*cb_lst->max_hooks;
  220. tmp=pkg_realloc(cb_lst->hooks,
  221. new_max_hooks*sizeof(struct blacklist_hook));
  222. if (tmp==0){
  223. goto error;
  224. }
  225. cb_lst->hooks=tmp;
  226. /* init the new chunk (but not the current entry which is
  227. * overwritten anyway) */
  228. memset(&cb_lst->hooks[cb_lst->max_hooks+1], 0,
  229. (new_max_hooks-cb_lst->max_hooks-1)*
  230. sizeof(struct blacklist_hook));
  231. cb_lst->max_hooks=new_max_hooks;
  232. }
  233. cb_lst->hooks[cb_lst->last_idx]=*h;
  234. cb_lst->last_idx++;
  235. return 0;
  236. error:
  237. return -1;
  238. }
  239. inline static int blacklist_run_hooks(struct blst_callbacks_lst *cb_lst,
  240. struct dest_info* si, unsigned char* flags,
  241. struct sip_msg* msg)
  242. {
  243. int r;
  244. int ret;
  245. ret=DST_BLACKLIST_CONTINUE; /* default, if no hook installed accept
  246. blacklist operation */
  247. if (likely(cb_lst->last_idx==0))
  248. return ret;
  249. for (r=0; r<cb_lst->last_idx; r++){
  250. ret=cb_lst->hooks[r].on_blst_action(si, flags, msg);
  251. if (ret!=DST_BLACKLIST_CONTINUE) break;
  252. }
  253. return ret;
  254. }
  255. #endif /* DST_BLACKLIST_HOOKS */
  256. /** init per protocol blacklist event ignore masks.
  257. * @return 0 on success, < 0 on error.
  258. */
  259. int blst_init_ign_masks(void)
  260. {
  261. if ((PROTO_UDP > PROTO_LAST) || (PROTO_TCP > PROTO_LAST) ||
  262. (PROTO_TLS > PROTO_LAST) || (PROTO_SCTP > PROTO_LAST)){
  263. BUG("protocol array too small\n");
  264. return -1;
  265. }
  266. blst_proto_imask[PROTO_UDP]=cfg_get(core, core_cfg, blst_udp_imask);
  267. blst_proto_imask[PROTO_TCP]=cfg_get(core, core_cfg, blst_tcp_imask);
  268. blst_proto_imask[PROTO_TLS]=cfg_get(core, core_cfg, blst_tls_imask);
  269. blst_proto_imask[PROTO_SCTP]=cfg_get(core, core_cfg, blst_sctp_imask);
  270. blst_proto_imask[PROTO_NONE]=blst_proto_imask[PROTO_UDP];
  271. return 0;
  272. }
  273. inline static void blst_destroy_entry(struct dst_blst_entry* e)
  274. {
  275. shm_free(e);
  276. }
  277. static ticks_t blst_timer(ticks_t ticks, struct timer_ln* tl, void* data);
  278. inline static void dst_blst_entry2ip(struct ip_addr* ip,
  279. struct dst_blst_entry* e)
  280. {
  281. #ifdef USE_IPV6
  282. if (e->flags & BLST_IS_IPV6){
  283. ip->af=AF_INET6;
  284. ip->len=16;
  285. }else
  286. #endif /* USE_IPV6 */
  287. {
  288. ip->af=AF_INET;
  289. ip->len=4;
  290. }
  291. memcpy(ip->u.addr, e->ip, ip->len);
  292. }
  293. inline static unsigned short dst_blst_hash_no(unsigned char proto,
  294. struct ip_addr* ip,
  295. unsigned short port)
  296. {
  297. str s1;
  298. str s2;
  299. s1.s=(char*)ip->u.addr;
  300. s1.len=ip->len;
  301. s2.s=(char*)&port;
  302. s2.len=sizeof(unsigned short);
  303. return get_hash2_raw(&s1, &s2)%DST_BLST_HASH_SIZE;
  304. }
  305. void destroy_dst_blacklist()
  306. {
  307. int r;
  308. struct dst_blst_entry** crt;
  309. struct dst_blst_entry* e;
  310. if (blst_timer_h){
  311. timer_del(blst_timer_h);
  312. timer_free(blst_timer_h);
  313. blst_timer_h=0;
  314. }
  315. #ifdef BLST_LOCK_PER_BUCKET
  316. if (dst_blst_hash)
  317. for(r=0; r<DST_BLST_HASH_SIZE; r++)
  318. lock_destroy(&dst_blst_hash[r].lock);
  319. #elif defined BLST_LOCK_SET
  320. if (blst_lock_set){
  321. lock_set_destroy(blst_lock_set);
  322. lock_set_dealloc(blst_lock_set);
  323. blst_lock_set=0;
  324. }
  325. #else
  326. if (blst_lock){
  327. lock_destroy(blst_lock);
  328. lock_dealloc(blst_lock);
  329. blst_lock=0;
  330. }
  331. #endif
  332. if (dst_blst_hash){
  333. for(r=0; r<DST_BLST_HASH_SIZE; r++){
  334. crt=&dst_blst_hash[r].first;
  335. while(*crt){
  336. e=*crt;
  337. *crt=(*crt)->next;
  338. blst_destroy_entry(e);
  339. }
  340. }
  341. shm_free(dst_blst_hash);
  342. dst_blst_hash=0;
  343. }
  344. if (blst_mem_used){
  345. shm_free((void*)blst_mem_used);
  346. blst_mem_used=0;
  347. }
  348. #ifdef DST_BLACKLIST_HOOKS
  349. destroy_blacklist_hooks();
  350. #endif
  351. #ifdef USE_DST_BLACKLIST_STATS
  352. if (dst_blacklist_stats)
  353. shm_free(dst_blacklist_stats);
  354. #endif
  355. }
  356. int init_dst_blacklist()
  357. {
  358. int ret;
  359. #ifdef BLST_LOCK_PER_BUCKET
  360. int r;
  361. #endif
  362. if (dst_blacklist_init==0) {
  363. /* the dst blacklist is turned off */
  364. default_core_cfg.use_dst_blacklist=0;
  365. return 0;
  366. }
  367. ret=-1;
  368. #ifdef DST_BLACKLIST_HOOKS
  369. if (init_blacklist_hooks()!=0){
  370. ret=E_OUT_OF_MEM;
  371. goto error;
  372. }
  373. #endif
  374. blst_mem_used=shm_malloc(sizeof(*blst_mem_used));
  375. if (blst_mem_used==0){
  376. ret=E_OUT_OF_MEM;
  377. goto error;
  378. }
  379. *blst_mem_used=0;
  380. dst_blst_hash=shm_malloc(sizeof(struct dst_blst_lst_head) *
  381. DST_BLST_HASH_SIZE);
  382. if (dst_blst_hash==0){
  383. ret=E_OUT_OF_MEM;
  384. goto error;
  385. }
  386. memset(dst_blst_hash, 0, sizeof(struct dst_blst_lst_head) *
  387. DST_BLST_HASH_SIZE);
  388. #ifdef BLST_LOCK_PER_BUCKET
  389. for (r=0; r<DST_BLST_HASH_SIZE; r++){
  390. if (lock_init(&dst_blst_hash[r].lock)==0){
  391. ret=-1;
  392. goto error;
  393. }
  394. }
  395. #elif defined BLST_LOCK_SET
  396. blst_lock_set=lock_set_alloc(DST_BLST_HASH_SIZE);
  397. if (blst_lock_set==0){
  398. ret=E_OUT_OF_MEM;
  399. goto error;
  400. }
  401. if (lock_set_init(blst_lock_set)==0){
  402. lock_set_dealloc(blst_lock_set);
  403. blst_lock_set=0;
  404. ret=-1;
  405. goto error;
  406. }
  407. #else /* BLST_ONE_LOCK */
  408. blst_lock=lock_alloc();
  409. if (blst_lock==0){
  410. ret=E_OUT_OF_MEM;
  411. goto error;
  412. }
  413. if (lock_init(blst_lock)==0){
  414. lock_dealloc(blst_lock);
  415. blst_lock=0;
  416. ret=-1;
  417. goto error;
  418. }
  419. #endif /* BLST*LOCK*/
  420. blst_timer_h=timer_alloc();
  421. if (blst_timer_h==0){
  422. ret=E_OUT_OF_MEM;
  423. goto error;
  424. }
  425. /* fix options */
  426. default_core_cfg.blst_max_mem<<=10; /* in Kb */ /* TODO: test with 0 */
  427. if (blst_timer_interval){
  428. timer_init(blst_timer_h, blst_timer, 0 ,0); /* slow timer */
  429. if (timer_add(blst_timer_h, S_TO_TICKS(blst_timer_interval))<0){
  430. LOG(L_CRIT, "BUG: init_dst_blacklist: failed to add the timer\n");
  431. timer_free(blst_timer_h);
  432. blst_timer_h=0;
  433. goto error;
  434. }
  435. }
  436. if (blst_init_ign_masks() < 0){
  437. ret=E_BUG;
  438. goto error;
  439. }
  440. return 0;
  441. error:
  442. destroy_dst_blacklist();
  443. return ret;
  444. }
  445. #ifdef USE_DST_BLACKLIST_STATS
  446. int init_dst_blacklist_stats(int iproc_num)
  447. {
  448. /* do not initialize the stats array if the dst blacklist will not be used */
  449. if (dst_blacklist_init==0) return 0;
  450. /* if it is already initialized */
  451. if (dst_blacklist_stats)
  452. shm_free(dst_blacklist_stats);
  453. dst_blacklist_stats=shm_malloc(sizeof(*dst_blacklist_stats) * iproc_num);
  454. if (dst_blacklist_stats==0){
  455. return E_OUT_OF_MEM;
  456. }
  457. memset(dst_blacklist_stats, 0, sizeof(*dst_blacklist_stats) * iproc_num);
  458. return 0;
  459. }
  460. #endif
  461. /* must be called with the lock held
  462. * struct dst_blst_entry** head, struct dst_blst_entry* e */
  463. #define dst_blacklist_lst_add(head, e)\
  464. do{ \
  465. (e)->next=*(head); \
  466. *(head)=(e); \
  467. }while(0)
  468. /* must be called with the lock held
  469. * returns a pointer to the blacklist entry if found, 0 otherwise
  470. * it also deletes expired elements (expire<=now) as it searches
  471. * proto==PROTO_NONE = wildcard */
  472. inline static struct dst_blst_entry* _dst_blacklist_lst_find(
  473. unsigned short hash,
  474. struct ip_addr* ip,
  475. unsigned char proto,
  476. unsigned short port,
  477. ticks_t now)
  478. {
  479. struct dst_blst_entry** crt;
  480. struct dst_blst_entry** tmp;
  481. struct dst_blst_entry* e;
  482. struct dst_blst_entry** head;
  483. unsigned char type;
  484. head=&dst_blst_hash[hash].first;
  485. #ifdef USE_IPV6
  486. type=(ip->af==AF_INET6)*BLST_IS_IPV6;
  487. #else /* USE_IPV6 */
  488. if (unlikely(ip->af!=AF_INET)) return 0;
  489. type=0;
  490. #endif /* USE_IPV6 */
  491. for (crt=head, tmp=&(*head)->next; *crt; crt=tmp, tmp=&(*crt)->next){
  492. e=*crt;
  493. prefetch_loc_r((*crt)->next, 1);
  494. /* remove old expired entries */
  495. if ((s_ticks_t)(now-(*crt)->expire)>=0){
  496. *crt=(*crt)->next;
  497. tmp=crt;
  498. *blst_mem_used-=DST_BLST_ENTRY_SIZE(*e);
  499. BLST_HASH_STATS_DEC(hash);
  500. blst_destroy_entry(e);
  501. }else if ((e->port==port) && ((e->flags & BLST_IS_IPV6)==type) &&
  502. ((e->proto==PROTO_NONE) || (proto==PROTO_NONE) ||
  503. (e->proto==proto)) &&
  504. (memcmp(ip->u.addr, e->ip, ip->len)==0)){
  505. return e;
  506. }
  507. }
  508. return 0;
  509. }
  510. /* must be called with the lock held
  511. * returns 1 if a matching entry was deleted, 0 otherwise
  512. * it also deletes expired elements (expire<=now) as it searches
  513. * proto==PROTO_NONE = wildcard */
  514. inline static int _dst_blacklist_del(
  515. unsigned short hash,
  516. struct ip_addr* ip,
  517. unsigned char proto,
  518. unsigned short port,
  519. ticks_t now)
  520. {
  521. struct dst_blst_entry** crt;
  522. struct dst_blst_entry** tmp;
  523. struct dst_blst_entry* e;
  524. struct dst_blst_entry** head;
  525. unsigned char type;
  526. head=&dst_blst_hash[hash].first;
  527. #ifdef USE_IPV6
  528. type=(ip->af==AF_INET6)*BLST_IS_IPV6;
  529. #else /* USE_IPV6 */
  530. if (unlikely(ip->af!=AF_INET)) return 0;
  531. type=0;
  532. #endif /* USE_IPV6 */
  533. for (crt=head, tmp=&(*head)->next; *crt; crt=tmp, tmp=&(*crt)->next){
  534. e=*crt;
  535. prefetch_loc_r((*crt)->next, 1);
  536. /* remove old expired entries */
  537. if ((s_ticks_t)(now-(*crt)->expire)>=0){
  538. *crt=(*crt)->next;
  539. tmp=crt;
  540. *blst_mem_used-=DST_BLST_ENTRY_SIZE(*e);
  541. BLST_HASH_STATS_DEC(hash);
  542. blst_destroy_entry(e);
  543. }else if ((e->port==port) && ((e->flags & BLST_IS_IPV6)==type) &&
  544. ((e->proto==PROTO_NONE) || (proto==PROTO_NONE) ||
  545. (e->proto==proto)) &&
  546. (memcmp(ip->u.addr, e->ip, ip->len)==0)){
  547. *crt=(*crt)->next;
  548. tmp=crt;
  549. *blst_mem_used-=DST_BLST_ENTRY_SIZE(*e);
  550. BLST_HASH_STATS_DEC(hash);
  551. blst_destroy_entry(e);
  552. return 1;
  553. }
  554. }
  555. return 0;
  556. }
  557. /* frees all the expired entries until either there are no more of them
  558. * or the total memory used is <= target (to free all of them use -1 for
  559. * targer)
  560. * params: target - free expired entries until no more then taget memory
  561. * is used (use 0 to free all of them)
  562. * delta - consider an entry expired if it expires after delta
  563. * ticks from now
  564. * timeout - exit after timeout ticks
  565. *
  566. * returns: number of deleted entries
  567. * This function should be called periodically from a timer
  568. */
  569. inline static int dst_blacklist_clean_expired(unsigned int target,
  570. ticks_t delta,
  571. ticks_t timeout)
  572. {
  573. static unsigned int start=0;
  574. unsigned int h;
  575. struct dst_blst_entry** crt;
  576. struct dst_blst_entry** tmp;
  577. struct dst_blst_entry* e;
  578. ticks_t start_time;
  579. ticks_t now;
  580. int no=0;
  581. int i;
  582. now=start_time=get_ticks_raw();
  583. for(h=start; h!=(start+DST_BLST_HASH_SIZE); h++){
  584. i=h%DST_BLST_HASH_SIZE;
  585. if (dst_blst_hash[i].first){
  586. LOCK_BLST(i);
  587. for (crt=&dst_blst_hash[i].first, tmp=&(*crt)->next;
  588. *crt; crt=tmp, tmp=&(*crt)->next){
  589. e=*crt;
  590. prefetch_loc_r((*crt)->next, 1);
  591. if ((s_ticks_t)(now+delta-(*crt)->expire)>=0){
  592. *crt=(*crt)->next;
  593. tmp=crt;
  594. *blst_mem_used-=DST_BLST_ENTRY_SIZE(*e);
  595. blst_destroy_entry(e);
  596. BLST_HASH_STATS_DEC(i);
  597. no++;
  598. if (*blst_mem_used<=target){
  599. UNLOCK_BLST(i);
  600. goto skip;
  601. }
  602. }
  603. }
  604. UNLOCK_BLST(i);
  605. /* check for timeout only "between" hash cells */
  606. now=get_ticks_raw();
  607. if ((now-start_time)>=timeout){
  608. DBG("_dst_blacklist_clean_expired_unsafe: timeout: %d > %d\n",
  609. TICKS_TO_MS(now-start_time), TICKS_TO_MS(timeout));
  610. goto skip;
  611. }
  612. }
  613. }
  614. skip:
  615. start=h; /* next time we start where we left */
  616. if (no){
  617. DBG("dst_blacklist_clean_expired, %d entries removed\n", no);
  618. }
  619. return no;
  620. }
  621. /* timer */
  622. static ticks_t blst_timer(ticks_t ticks, struct timer_ln* tl, void* data)
  623. {
  624. dst_blacklist_clean_expired(0, 0, 2); /*spend max. 2 ticks*/
  625. return (ticks_t)(-1);
  626. }
  627. /* adds a proto ip:port combination to the blacklist
  628. * returns 0 on success, -1 on error (blacklist full -- would use more then
  629. * blst:_max_mem, or out of shm. mem.)
  630. */
  631. inline static int dst_blacklist_add_ip(unsigned char err_flags,
  632. unsigned char proto,
  633. struct ip_addr* ip, unsigned short port,
  634. ticks_t timeout)
  635. {
  636. int size;
  637. struct dst_blst_entry* e;
  638. unsigned short hash;
  639. ticks_t now;
  640. int ret;
  641. ret=0;
  642. if (ip->af==AF_INET){
  643. err_flags&=~BLST_IS_IPV6; /* make sure the ipv6 flag is reset */
  644. size=sizeof(struct dst_blst_entry);
  645. }else{
  646. err_flags|=BLST_IS_IPV6;
  647. size=sizeof(struct dst_blst_entry)+12 /* ipv6 addr - 4 */;
  648. }
  649. now=get_ticks_raw();
  650. hash=dst_blst_hash_no(proto, ip, port);
  651. /* check if the entry already exists */
  652. LOCK_BLST(hash);
  653. e=_dst_blacklist_lst_find(hash, ip, proto, port, now);
  654. if (e){
  655. e->flags|=err_flags;
  656. e->expire=now+timeout; /* update the timeout */
  657. }else{
  658. if (unlikely((*blst_mem_used+size) >=
  659. cfg_get(core, core_cfg, blst_max_mem))){
  660. #ifdef USE_DST_BLACKLIST_STATS
  661. dst_blacklist_stats[process_no].bkl_lru_cnt++;
  662. #endif
  663. UNLOCK_BLST(hash);
  664. /* first try to free some memory (~ 12%), but don't
  665. * spend more then 250 ms*/
  666. dst_blacklist_clean_expired(*blst_mem_used/16*14, 0,
  667. MS_TO_TICKS(250));
  668. if (unlikely(*blst_mem_used+size >=
  669. cfg_get(core, core_cfg, blst_max_mem))){
  670. ret=-1;
  671. goto error;
  672. }
  673. LOCK_BLST(hash);
  674. }
  675. e=shm_malloc(size);
  676. if (e==0){
  677. UNLOCK_BLST(hash);
  678. ret=E_OUT_OF_MEM;
  679. goto error;
  680. }
  681. *blst_mem_used+=size;
  682. e->flags=err_flags;
  683. e->proto=proto;
  684. e->port=port;
  685. memcpy(e->ip, ip->u.addr, ip->len);
  686. e->expire=now+timeout; /* update the timeout */
  687. e->next=0;
  688. dst_blacklist_lst_add(&dst_blst_hash[hash].first, e);
  689. BLST_HASH_STATS_INC(hash);
  690. }
  691. UNLOCK_BLST(hash);
  692. error:
  693. return ret;
  694. }
  695. /* if no blacklisted returns 0, else returns the blacklist flags */
  696. inline static int dst_is_blacklisted_ip(unsigned char proto,
  697. struct ip_addr* ip,
  698. unsigned short port)
  699. {
  700. struct dst_blst_entry* e;
  701. unsigned short hash;
  702. ticks_t now;
  703. int ret;
  704. ret=0;
  705. now=get_ticks_raw();
  706. hash=dst_blst_hash_no(proto, ip, port);
  707. if (unlikely(dst_blst_hash[hash].first)){
  708. LOCK_BLST(hash);
  709. e=_dst_blacklist_lst_find(hash, ip, proto, port, now);
  710. if (e){
  711. ret=e->flags;
  712. }
  713. UNLOCK_BLST(hash);
  714. }
  715. return ret;
  716. }
  717. /** add dst to the blacklist, specifying the timeout.
  718. * @param err_flags - reason (bitmap)
  719. * @param si - destination (protocol, ip and port)
  720. * @param msg - sip message that triggered the blacklisting (can be 0 if
  721. * not known)
  722. * @param timeout - timeout in ticks
  723. * @return 0 on success, -1 on error
  724. */
  725. int dst_blacklist_force_add_to(unsigned char err_flags, struct dest_info* si,
  726. struct sip_msg* msg, ticks_t timeout)
  727. {
  728. struct ip_addr ip;
  729. #ifdef DST_BLACKLIST_HOOKS
  730. if (unlikely (blacklist_run_hooks(&blst_add_cb, si, &err_flags, msg) ==
  731. DST_BLACKLIST_DENY))
  732. return 0;
  733. #endif
  734. su2ip_addr(&ip, &si->to);
  735. return dst_blacklist_add_ip(err_flags, si->proto, &ip,
  736. su_getport(&si->to), timeout);
  737. }
  738. /** add dst to the blacklist, specifying the timeout.
  739. * (like @function dst_blacklist_force_add_to)= above, but uses
  740. * (proto, sockaddr_union) instead of struct dest_info)
  741. */
  742. int dst_blacklist_force_su_to(unsigned char err_flags, unsigned char proto,
  743. union sockaddr_union* dst,
  744. struct sip_msg* msg, ticks_t timeout)
  745. {
  746. struct ip_addr ip;
  747. #ifdef DST_BLACKLIST_HOOKS
  748. struct dest_info si;
  749. init_dest_info(&si);
  750. si.to=*dst;
  751. si.proto=proto;
  752. if (unlikely (blacklist_run_hooks(&blst_add_cb, &si, &err_flags, msg) ==
  753. DST_BLACKLIST_DENY))
  754. return 0;
  755. #endif
  756. su2ip_addr(&ip, dst);
  757. return dst_blacklist_add_ip(err_flags, proto, &ip,
  758. su_getport(dst), timeout);
  759. }
  760. int dst_is_blacklisted(struct dest_info* si, struct sip_msg* msg)
  761. {
  762. int ires;
  763. struct ip_addr ip;
  764. #ifdef DST_BLACKLIST_HOOKS
  765. unsigned char err_flags;
  766. int action;
  767. #endif
  768. su2ip_addr(&ip, &si->to);
  769. #ifdef DST_BLACKLIST_HOOKS
  770. err_flags=0;
  771. if (unlikely((action=(blacklist_run_hooks(&blst_search_cb, si, &err_flags, msg))
  772. ) != DST_BLACKLIST_CONTINUE)){
  773. if (action==DST_BLACKLIST_DENY)
  774. return 0;
  775. else /* if (action==DST_BLACKLIST_ACCEPT) */
  776. return err_flags;
  777. }
  778. #endif
  779. ires=dst_is_blacklisted_ip(si->proto, &ip, su_getport(&si->to));
  780. #ifdef USE_DST_BLACKLIST_STATS
  781. if (ires)
  782. dst_blacklist_stats[process_no].bkl_hit_cnt++;
  783. #endif
  784. return ires;
  785. }
  786. /* returns 1 if the entry was deleted, 0 if not found */
  787. int dst_blacklist_del(struct dest_info* si, struct sip_msg* msg)
  788. {
  789. unsigned short hash;
  790. struct ip_addr ip;
  791. ticks_t now;
  792. int ret;
  793. unsigned short port;
  794. ret=0;
  795. su2ip_addr(&ip, &si->to);
  796. port=su_getport(&si->to);
  797. now=get_ticks_raw();
  798. hash=dst_blst_hash_no(si->proto, &ip, port);
  799. if (unlikely(dst_blst_hash[hash].first)){
  800. LOCK_BLST(hash);
  801. ret=_dst_blacklist_del(hash, &ip, si->proto, port, now);
  802. UNLOCK_BLST(hash);
  803. }
  804. return ret;
  805. }
  806. /* rpc functions */
  807. void dst_blst_mem_info(rpc_t* rpc, void* ctx)
  808. {
  809. if (!cfg_get(core, core_cfg, use_dst_blacklist)){
  810. rpc->fault(ctx, 500, "dst blacklist support disabled");
  811. return;
  812. }
  813. rpc->add(ctx, "dd", *blst_mem_used, cfg_get(core, core_cfg, blst_max_mem));
  814. }
  815. #ifdef USE_DST_BLACKLIST_STATS
  816. static unsigned long stat_sum(int ivar, int breset) {
  817. unsigned long isum=0;
  818. int i1=0;
  819. for (; i1 < get_max_procs(); i1++)
  820. switch (ivar) {
  821. case 0:
  822. isum+=dst_blacklist_stats[i1].bkl_hit_cnt;
  823. if (breset)
  824. dst_blacklist_stats[i1].bkl_hit_cnt=0;
  825. break;
  826. case 1:
  827. isum+=dst_blacklist_stats[i1].bkl_lru_cnt;
  828. if (breset)
  829. dst_blacklist_stats[i1].bkl_lru_cnt=0;
  830. break;
  831. }
  832. return isum;
  833. }
  834. void dst_blst_stats_get(rpc_t* rpc, void* c)
  835. {
  836. char *name=NULL;
  837. void *handle;
  838. int found=0,i=0;
  839. int reset=0;
  840. char* dst_blacklist_stats_names[] = {
  841. "bkl_hit_cnt",
  842. "bkl_lru_cnt",
  843. NULL
  844. };
  845. if (!cfg_get(core, core_cfg, use_dst_blacklist)){
  846. rpc->fault(c, 500, "dst blacklist support disabled");
  847. return;
  848. }
  849. if (rpc->scan(c, "s", &name) < 0)
  850. return;
  851. if (rpc->scan(c, "d", &reset) < 0)
  852. return;
  853. if (!strcasecmp(name, DST_BLACKLIST_ALL_STATS)) {
  854. /* dump all the dns cache stat values */
  855. rpc->add(c, "{", &handle);
  856. for (i=0; dst_blacklist_stats_names[i]; i++)
  857. rpc->struct_add(handle, "d",
  858. dst_blacklist_stats_names[i],
  859. stat_sum(i, reset));
  860. found=1;
  861. } else {
  862. for (i=0; dst_blacklist_stats_names[i]; i++)
  863. if (!strcasecmp(dst_blacklist_stats_names[i], name)) {
  864. rpc->add(c, "{", &handle);
  865. rpc->struct_add(handle, "d",
  866. dst_blacklist_stats_names[i],
  867. stat_sum(i, reset));
  868. found=1;
  869. break;
  870. }
  871. }
  872. if(!found)
  873. rpc->fault(c, 500, "unknown dst blacklist stat parameter");
  874. return;
  875. }
  876. #endif /* USE_DST_BLACKLIST_STATS */
  877. /* only for debugging, it helds the lock too long for "production" use */
  878. void dst_blst_debug(rpc_t* rpc, void* ctx)
  879. {
  880. int h;
  881. struct dst_blst_entry* e;
  882. ticks_t now;
  883. struct ip_addr ip;
  884. if (!cfg_get(core, core_cfg, use_dst_blacklist)){
  885. rpc->fault(ctx, 500, "dst blacklist support disabled");
  886. return;
  887. }
  888. now=get_ticks_raw();
  889. for(h=0; h<DST_BLST_HASH_SIZE; h++){
  890. LOCK_BLST(h);
  891. for(e=dst_blst_hash[h].first; e; e=e->next){
  892. dst_blst_entry2ip(&ip, e);
  893. rpc->add(ctx, "ssddd", get_proto_name(e->proto),
  894. ip_addr2a(&ip), e->port,
  895. (s_ticks_t)(now-e->expire)<=0?
  896. TICKS_TO_S(e->expire-now):
  897. -TICKS_TO_S(now-e->expire) ,
  898. e->flags);
  899. }
  900. UNLOCK_BLST(h);
  901. }
  902. }
  903. /* only for debugging, it helds the lock too long for "production" use */
  904. void dst_blst_hash_stats(rpc_t* rpc, void* ctx)
  905. {
  906. int h;
  907. struct dst_blst_entry* e;
  908. #ifdef BLST_HASH_STATS
  909. int n;
  910. n=0;
  911. #endif
  912. if (!cfg_get(core, core_cfg, use_dst_blacklist)){
  913. rpc->fault(ctx, 500, "dst blacklist support disabled");
  914. return;
  915. }
  916. for(h=0; h<DST_BLST_HASH_SIZE; h++){
  917. #ifdef BLST_HASH_STATS
  918. LOCK_BLST(h);
  919. for(e=dst_blst_hash[h].first; e; e=e->next) n++;
  920. UNLOCK_BLST(h);
  921. rpc->add(ctx, "dd", h, n);
  922. #else
  923. rpc->add(ctx, "dd", h, dst_blst_hash[h].entries);
  924. #endif
  925. }
  926. }
  927. /* dumps the content of the blacklist in a human-readable format */
  928. void dst_blst_view(rpc_t* rpc, void* ctx)
  929. {
  930. int h;
  931. int expires;
  932. struct dst_blst_entry* e;
  933. ticks_t now;
  934. struct ip_addr ip;
  935. if (!cfg_get(core, core_cfg, use_dst_blacklist)){
  936. rpc->fault(ctx, 500, "dst blacklist support disabled");
  937. return;
  938. }
  939. now=get_ticks_raw();
  940. for(h=0; h<DST_BLST_HASH_SIZE; h++) {
  941. LOCK_BLST(h);
  942. for(e=dst_blst_hash[h].first; e; e=e->next) {
  943. expires = (s_ticks_t)(now-e->expire)<=0?
  944. TICKS_TO_S(e->expire-now): -TICKS_TO_S(now-e->expire);
  945. /* don't include expired entries into view report */
  946. if (expires < 0) {
  947. continue;
  948. }
  949. dst_blst_entry2ip(&ip, e);
  950. rpc->printf(ctx, "{\n protocol: %s", get_proto_name(e->proto));
  951. rpc->printf(ctx, " ip: %s", ip_addr2a(&ip));
  952. rpc->printf(ctx, " port: %d", e->port);
  953. rpc->printf(ctx, " expires in (s): %d", expires);
  954. rpc->printf(ctx, " flags: %d\n}", e->flags);
  955. }
  956. UNLOCK_BLST(h);
  957. }
  958. }
  959. /* deletes all the entries from the blacklist except the permanent ones
  960. * (which are marked with BLST_PERMANENT)
  961. */
  962. void dst_blst_flush(void)
  963. {
  964. int h;
  965. struct dst_blst_entry* e;
  966. struct dst_blst_entry** crt;
  967. struct dst_blst_entry** tmp;
  968. for(h=0; h<DST_BLST_HASH_SIZE; h++){
  969. LOCK_BLST(h);
  970. for (crt=&dst_blst_hash[h].first, tmp=&(*crt)->next;
  971. *crt; crt=tmp, tmp=&(*crt)->next){
  972. e=*crt;
  973. prefetch_loc_r((*crt)->next, 1);
  974. if (!(e->flags & BLST_PERMANENT)){
  975. *crt=(*crt)->next;
  976. tmp=crt;
  977. *blst_mem_used-=DST_BLST_ENTRY_SIZE(*e);
  978. blst_destroy_entry(e);
  979. BLST_HASH_STATS_DEC(h);
  980. }
  981. }
  982. UNLOCK_BLST(h);
  983. }
  984. }
  985. /* rpc wrapper function for dst_blst_flush() */
  986. void dst_blst_delete_all(rpc_t* rpc, void* ctx)
  987. {
  988. if (!cfg_get(core, core_cfg, use_dst_blacklist)){
  989. rpc->fault(ctx, 500, "dst blacklist support disabled");
  990. return;
  991. }
  992. dst_blst_flush();
  993. }
  994. /* Adds a new entry to the blacklist */
  995. void dst_blst_add(rpc_t* rpc, void* ctx)
  996. {
  997. str ip;
  998. int port, proto, flags;
  999. unsigned char err_flags;
  1000. struct ip_addr *ip_addr;
  1001. if (!cfg_get(core, core_cfg, use_dst_blacklist)){
  1002. rpc->fault(ctx, 500, "dst blacklist support disabled");
  1003. return;
  1004. }
  1005. if (rpc->scan(ctx, "Sddd", &ip, &port, &proto, &flags) < 4)
  1006. return;
  1007. err_flags = (unsigned char)flags;
  1008. /* sanity checks */
  1009. if ((unsigned char)proto > PROTO_SCTP) {
  1010. rpc->fault(ctx, 400, "Unknown protocol");
  1011. return;
  1012. }
  1013. if (err_flags & BLST_IS_IPV6) {
  1014. #ifdef USE_IPV6
  1015. /* IPv6 address is specified */
  1016. ip_addr = str2ip6(&ip);
  1017. #else /* USE_IPV6 */
  1018. rpc->fault(ctx, 400, "IPv6 support disabled");
  1019. return;
  1020. #endif /* USE_IPV6 */
  1021. } else {
  1022. /* try IPv4 first, than IPv6 */
  1023. ip_addr = str2ip(&ip);
  1024. if (!ip_addr) {
  1025. #ifdef USE_IPV6
  1026. ip_addr = str2ip6(&ip);
  1027. err_flags |= BLST_IS_IPV6;
  1028. #else /* USE_IPV6 */
  1029. rpc->fault(ctx, 400, "Malformed or IPv6 ip address");
  1030. return;
  1031. #endif /* USE_IPV6 */
  1032. }
  1033. }
  1034. if (!ip_addr) {
  1035. rpc->fault(ctx, 400, "Malformed ip address");
  1036. return;
  1037. }
  1038. if (dst_blacklist_add_ip(err_flags, proto, ip_addr, port,
  1039. S_TO_TICKS(cfg_get(core, core_cfg, blst_timeout))))
  1040. rpc->fault(ctx, 400, "Failed to add the entry to the blacklist");
  1041. }
  1042. /* fixup function for use_dst_blacklist
  1043. * verifies that dst_blacklist_init is set to 1
  1044. */
  1045. int use_dst_blacklist_fixup(void *handle, str *gname, str *name, void **val)
  1046. {
  1047. if ((int)(long)(*val) && !dst_blacklist_init) {
  1048. LOG(L_ERR, "ERROR: use_dst_blacklist_fixup(): "
  1049. "dst blacklist is turned off by dst_blacklist_init=0, "
  1050. "it cannot be enabled runtime.\n");
  1051. return -1;
  1052. }
  1053. return 0;
  1054. }
  1055. /* KByte to Byte conversion */
  1056. int blst_max_mem_fixup(void *handle, str *gname, str *name, void **val)
  1057. {
  1058. unsigned int u;
  1059. u = ((unsigned int)(long)(*val))<<10;
  1060. (*val) = (void *)(long)u;
  1061. return 0;
  1062. }
  1063. /** re-inint per child blst_proto_ign_mask array. */
  1064. void blst_reinit_ign_masks(str* gname, str* name)
  1065. {
  1066. blst_init_ign_masks();
  1067. }
  1068. #endif /* USE_DST_BLACKLIST */