napt_ipv4.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. /*
  2. NAPT(Network Address Port Translation)
  3. IPv4版本的NAPT,支持TCP/UDP/ICMP协议
  4. 原版代码来源:
  5. https://gitee.com/openLuat/luatos-soc-air101
  6. https://www.winnermicro.com/html/1/156/158/558.html
  7. 为适应luatos环境做了大量修改, 脱离了平台依赖性
  8. */
  9. #include "luat_base.h"
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "lwip/ip.h"
  13. #include "lwip/udp.h"
  14. #include "lwip/icmp.h"
  15. #include "lwip/dns.h"
  16. #include "lwip/priv/tcp_priv.h"
  17. #include "lwip/timeouts.h"
  18. #include "luat_napt.h"
  19. #include "luat_mem.h"
  20. #define LUAT_LOG_TAG "napt"
  21. #include "luat_log.h"
  22. //#define NAPT_ALLOC_DEBUG
  23. #ifdef NAPT_ALLOC_DEBUG
  24. static u16 napt4ic_cnt;
  25. static u16 napt4tcp_cnt;
  26. static u16 napt4udp_cnt;
  27. #endif
  28. //#define NAPT_DEBUG
  29. #ifdef NAPT_DEBUG
  30. #define NAPT_PRINT printf
  31. #else
  32. #define NAPT_PRINT(...)
  33. #endif
  34. #define IP_PROTO_GRE 47
  35. #define NAPT_ETH_HDR_LEN sizeof(struct ethhdr)
  36. #define NAPT_CHKSUM_16BIT_LEN sizeof(u16)
  37. #define NAPT_TABLE_FOREACH(pos, head)\
  38. for (pos = head.next; NULL != pos; pos = pos->next)
  39. /* napt tcp/udp */
  40. struct napt_addr_4tu{
  41. struct napt_addr_4tu *next;
  42. u16 src_port;
  43. u16 new_port;
  44. u8 src_ip;
  45. u8 time_stamp;
  46. u8 mac[6];
  47. };
  48. /* napt icmp */
  49. struct napt_addr_4ic{
  50. struct napt_addr_4ic *next;
  51. u16 src_id;/* icmp id */
  52. u16 new_id;
  53. u8 src_ip;
  54. u8 time_stamp;
  55. u8 mac[6];
  56. };
  57. struct napt_addr_gre{
  58. u8 src_ip;
  59. u8 time_stamp;
  60. u8 is_used;
  61. u8 mac[6];
  62. };
  63. struct napt_table_head_4tu{
  64. struct napt_addr_4tu *next;
  65. #ifdef NAPT_TABLE_LIMIT
  66. u16 cnt;
  67. #endif
  68. };
  69. struct napt_table_head_4ic{
  70. struct napt_addr_4ic *next;
  71. #ifdef NAPT_TABLE_LIMIT
  72. u16 cnt;
  73. #endif
  74. };
  75. /* napt head */
  76. static struct napt_table_head_4tu napt_table_4tcp;
  77. static struct napt_table_head_4tu napt_table_4udp;
  78. static struct napt_table_head_4ic napt_table_4ic;
  79. //#define NAPT_USE_HW_TIMER
  80. //#define NAPT_TABLE_MUTEX_LOCK
  81. #ifdef NAPT_TABLE_MUTEX_LOCK
  82. static tls_os_sem_t *napt_table_lock_4tcp;
  83. static tls_os_sem_t *napt_table_lock_4udp;
  84. static tls_os_sem_t *napt_table_lock_4ic;
  85. #ifdef NAPT_USE_HW_TIMER
  86. static bool napt_check_tcp = FALSE;
  87. static bool napt_check_udp = FALSE;
  88. static bool napt_check_ic = FALSE;
  89. #endif /* NAPT_USE_HW_TIMER */
  90. #endif /* NAPT_TABLE_MUTEX_LOCK */
  91. /* tcp&udp */
  92. static u16 napt_curr_port;
  93. /* icmp id */
  94. static u16 napt_curr_id;
  95. /* gre for vpn */
  96. static struct napt_addr_gre gre_info;
  97. static inline void *luat_napt_mem_alloc(u32 size)
  98. {
  99. return luat_heap_malloc(size);
  100. }
  101. static inline void luat_napt_mem_free(void *p)
  102. {
  103. luat_heap_free(p);
  104. }
  105. /*****************************************************************************
  106. Prototype : luat_napt_try_lock
  107. Description : try hold lock
  108. Input : tls_os_sem_t *lock point lock
  109. Output : None
  110. Return Value : int 0 success
  111. -1 failed
  112. ------------------------------------------------------------------------------
  113. History :
  114. 1.Date : 2019/2/1
  115. Author : Li Limin, lilm@winnermicro.com
  116. Modification : Created function
  117. *****************************************************************************/
  118. // static inline int luat_napt_try_lock(tls_os_sem_t *lock)
  119. // {
  120. // return (tls_os_sem_acquire(lock, HZ / 10) == TLS_OS_SUCCESS) ? 0 : -1;
  121. // return 0;
  122. // }
  123. /*****************************************************************************
  124. Prototype : luat_napt_lock
  125. Description : hold lock
  126. Input : tls_os_sem_t *lock point lock
  127. Output : None
  128. Return Value : void
  129. ------------------------------------------------------------------------------
  130. History :
  131. 1.Date : 2019/2/1
  132. Author : Li Limin, lilm@winnermicro.com
  133. Modification : Created function
  134. *****************************************************************************/
  135. // static inline void luat_napt_lock(tls_os_sem_t *lock)
  136. // {
  137. // tls_os_sem_acquire(lock, 0);
  138. // return;
  139. // }
  140. /*****************************************************************************
  141. Prototype : luat_napt_unlock
  142. Description : release lock
  143. Input : tls_os_sem_t *lock point lock
  144. Output : None
  145. Return Value : void
  146. ------------------------------------------------------------------------------
  147. History :
  148. 1.Date : 2019/2/1
  149. Author : Li Limin, lilm@winnermicro.com
  150. Modification : Created function
  151. *****************************************************************************/
  152. // static inline void luat_napt_unlock(tls_os_sem_t *lock)
  153. // {
  154. // tls_os_sem_release(lock);
  155. // }
  156. #ifdef NAPT_TABLE_LIMIT
  157. /*****************************************************************************
  158. Prototype : luat_napt_table_is_full
  159. Description : the napt table is full
  160. Input : void
  161. Output : None
  162. Return Value : bool true the napt table is full
  163. false the napt table not is full
  164. ------------------------------------------------------------------------------
  165. History :
  166. 1.Date : 2015/3/10
  167. Author : Li Limin, lilm@winnermicro.com
  168. Modification : Created function
  169. *****************************************************************************/
  170. static inline bool luat_napt_table_is_full(void)
  171. {
  172. bool is_full = false;
  173. if ((napt_table_4tcp.cnt + napt_table_4udp.cnt + napt_table_4ic.cnt) >= NAPT_TABLE_SIZE_MAX)
  174. {
  175. #ifdef NAPT_ALLOC_DEBUG
  176. printf("@@@ napt batle: limit is reached for tcp/udp.\r\n");
  177. #endif
  178. NAPT_PRINT("napt batle: limit is reached for tcp/udp.\n");
  179. is_full = true;
  180. }
  181. return is_full;
  182. }
  183. #endif
  184. static inline u16 luat_napt_port_alloc(void)
  185. {
  186. u8_t i;
  187. u16 cnt = 0;
  188. struct udp_pcb *udp_pcb;
  189. struct tcp_pcb *tcp_pcb;
  190. struct napt_addr_4tu *napt_tcp;
  191. struct napt_addr_4tu *napt_udp;
  192. again:
  193. if (napt_curr_port++ == NAPT_LOCAL_PORT_RANGE_END)
  194. {
  195. napt_curr_port = NAPT_LOCAL_PORT_RANGE_START;
  196. }
  197. /* udp */
  198. for(udp_pcb = udp_pcbs; udp_pcb != NULL; udp_pcb = udp_pcb->next)
  199. {
  200. if (udp_pcb->local_port == napt_curr_port)
  201. {
  202. if (++cnt > (NAPT_LOCAL_PORT_RANGE_END - NAPT_LOCAL_PORT_RANGE_START))
  203. {
  204. return 0;
  205. }
  206. goto again;
  207. }
  208. }
  209. /* tcp */
  210. for (i = 0; i < NUM_TCP_PCB_LISTS; i++)
  211. {
  212. for(tcp_pcb = *tcp_pcb_lists[i]; tcp_pcb != NULL; tcp_pcb = tcp_pcb->next)
  213. {
  214. if (tcp_pcb->local_port == napt_curr_port)
  215. {
  216. if (++cnt > (NAPT_LOCAL_PORT_RANGE_END - NAPT_LOCAL_PORT_RANGE_START))
  217. {
  218. return 0;
  219. }
  220. goto again;
  221. }
  222. }
  223. }
  224. /* tcp napt */
  225. NAPT_TABLE_FOREACH(napt_tcp, napt_table_4tcp)
  226. {
  227. if (napt_tcp->new_port == napt_curr_port)
  228. {
  229. if (++cnt > (NAPT_LOCAL_PORT_RANGE_END - NAPT_LOCAL_PORT_RANGE_START))
  230. {
  231. return 0;
  232. }
  233. goto again;
  234. }
  235. }
  236. /* udp napt */
  237. NAPT_TABLE_FOREACH(napt_udp, napt_table_4udp)
  238. {
  239. if (napt_udp->new_port == napt_curr_port)
  240. {
  241. if (++cnt > (NAPT_LOCAL_PORT_RANGE_END - NAPT_LOCAL_PORT_RANGE_START))
  242. {
  243. return 0;
  244. }
  245. goto again;
  246. }
  247. }
  248. return napt_curr_port;
  249. }
  250. static inline struct napt_addr_4ic *luat_napt_get_by_src_id(u16 id, u8 ip)
  251. {
  252. struct napt_addr_4ic *ret = NULL;
  253. struct napt_addr_4ic *napt;
  254. NAPT_TABLE_FOREACH(napt, napt_table_4ic)
  255. {
  256. if ((id == napt->src_id) && (ip == napt->src_ip))
  257. {
  258. ret = napt;
  259. break;
  260. }
  261. }
  262. return ret;
  263. }
  264. static inline struct napt_addr_4ic *luat_napt_get_by_dst_id(u16 id)
  265. {
  266. struct napt_addr_4ic *ret = NULL;
  267. struct napt_addr_4ic *napt;
  268. NAPT_TABLE_FOREACH(napt, napt_table_4ic)
  269. {
  270. if (id == napt->new_id)
  271. {
  272. ret = napt;
  273. break;
  274. }
  275. }
  276. return ret;
  277. }
  278. static u16 luat_napt_icmp_id_alloc(void)
  279. {
  280. u16 cnt = 0;
  281. struct napt_addr_4ic *napt;
  282. again:
  283. if (napt_curr_id++ == NAPT_ICMP_ID_RANGE_END)
  284. {
  285. napt_curr_id = NAPT_ICMP_ID_RANGE_START;
  286. }
  287. NAPT_TABLE_FOREACH(napt, napt_table_4ic)
  288. {
  289. if (napt_curr_id == napt->new_id)
  290. {
  291. if (++cnt > (NAPT_ICMP_ID_RANGE_END - NAPT_ICMP_ID_RANGE_START))
  292. {
  293. return 0;
  294. }
  295. goto again;
  296. }
  297. }
  298. return napt_curr_id;
  299. }
  300. static inline struct napt_addr_4ic *luat_napt_table_insert_4ic(u16 id, u8 ip)
  301. {
  302. struct napt_addr_4ic *napt;
  303. #ifdef NAPT_TABLE_LIMIT
  304. if (true == luat_napt_table_is_full())
  305. {
  306. return NULL;
  307. }
  308. #endif
  309. napt = luat_napt_mem_alloc(sizeof(struct napt_addr_4ic));
  310. if (NULL == napt)
  311. {
  312. return NULL;
  313. }
  314. memset(napt, 0, sizeof(struct napt_addr_4ic));
  315. napt->src_id = id;
  316. napt->new_id = luat_napt_icmp_id_alloc();
  317. if (0 == napt->new_id)
  318. {
  319. luat_napt_mem_free(napt);
  320. return NULL;
  321. }
  322. napt->src_ip = ip;
  323. napt->time_stamp++;
  324. #ifdef NAPT_TABLE_LIMIT
  325. napt_table_4ic.cnt++;
  326. #endif
  327. napt->next = napt_table_4ic.next;
  328. napt_table_4ic.next = napt;
  329. #ifdef NAPT_ALLOC_DEBUG
  330. printf("@@ napt id alloc %hu\r\n", ++napt4ic_cnt);
  331. #endif
  332. return napt;
  333. }
  334. static inline void luat_napt_table_update_4ic(struct napt_addr_4ic *napt)
  335. {
  336. if (!++napt->time_stamp)
  337. napt->time_stamp++;
  338. return;
  339. }
  340. static inline struct napt_addr_4tu *luat_napt_get_tcp_port_by_dest(u16 port)
  341. {
  342. struct napt_addr_4tu *ret = NULL;
  343. struct napt_addr_4tu *napt;
  344. NAPT_TABLE_FOREACH(napt, napt_table_4tcp)
  345. {
  346. if (napt->new_port == port)
  347. {
  348. ret = napt;
  349. break;
  350. }
  351. }
  352. return ret;
  353. }
  354. static inline struct napt_addr_4tu *luat_napt_get_tcp_port_by_src(u16 port, u8 ip)
  355. {
  356. struct napt_addr_4tu *ret = NULL;
  357. struct napt_addr_4tu *napt;
  358. NAPT_TABLE_FOREACH(napt, napt_table_4tcp)
  359. {
  360. if (port == napt->src_port)
  361. {
  362. if (ip == napt->src_ip)
  363. {
  364. ret = napt;
  365. break;
  366. }
  367. }
  368. }
  369. return ret;
  370. }
  371. static inline struct napt_addr_4tu *luat_napt_table_insert_4tcp(u16 src_port, u8 ip)
  372. {
  373. u16 new_port;
  374. struct napt_addr_4tu *napt;
  375. #ifdef NAPT_TABLE_LIMIT
  376. if (true == luat_napt_table_is_full())
  377. {
  378. return NULL;
  379. }
  380. #endif
  381. napt = luat_napt_mem_alloc(sizeof(struct napt_addr_4tu));
  382. if (NULL == napt)
  383. {
  384. return NULL;
  385. }
  386. memset(napt, 0, sizeof(struct napt_addr_4tu));
  387. napt->src_port = src_port;
  388. new_port = luat_napt_port_alloc();
  389. if (0 == new_port)
  390. {
  391. luat_napt_mem_free(napt);
  392. return NULL;
  393. }
  394. napt->new_port = htons(new_port);
  395. napt->src_ip = ip;
  396. napt->time_stamp++;
  397. #ifdef NAPT_TABLE_LIMIT
  398. napt_table_4tcp.cnt++;
  399. #endif
  400. napt->next = napt_table_4tcp.next;
  401. napt_table_4tcp.next = napt;
  402. #ifdef NAPT_ALLOC_DEBUG
  403. printf("@@ napt tcp port alloc %hu\r\n", ++napt4tcp_cnt);
  404. #endif
  405. return napt;
  406. }
  407. static inline void luat_napt_table_update_4tcp(struct napt_addr_4tu *napt)
  408. {
  409. if (!++napt->time_stamp)
  410. napt->time_stamp++;
  411. return;
  412. }
  413. static inline struct napt_addr_4tu *luat_napt_get_udp_port_by_dest(u16 port)
  414. {
  415. struct napt_addr_4tu *ret = NULL;
  416. struct napt_addr_4tu *napt;
  417. NAPT_TABLE_FOREACH(napt, napt_table_4udp)
  418. {
  419. if (napt->new_port == port)
  420. {
  421. ret = napt;
  422. break;
  423. }
  424. }
  425. return ret;
  426. }
  427. static inline struct napt_addr_4tu *luat_napt_get_udp_port_by_src(u16 port, u8 ip)
  428. {
  429. struct napt_addr_4tu *ret = NULL;
  430. struct napt_addr_4tu *napt;
  431. NAPT_TABLE_FOREACH(napt, napt_table_4udp)
  432. {
  433. if (port == napt->src_port)
  434. {
  435. if (ip == napt->src_ip)
  436. {
  437. ret = napt;
  438. break;
  439. }
  440. }
  441. }
  442. return ret;
  443. }
  444. static inline struct napt_addr_4tu *luat_napt_table_insert_4udp(u16 src_port, u8 ip)
  445. {
  446. u16 new_port;
  447. struct napt_addr_4tu *napt;
  448. #ifdef NAPT_TABLE_LIMIT
  449. if (true == luat_napt_table_is_full())
  450. {
  451. return NULL;
  452. }
  453. #endif
  454. napt = luat_napt_mem_alloc(sizeof(struct napt_addr_4tu));
  455. if (NULL == napt)
  456. {
  457. return NULL;
  458. }
  459. memset(napt, 0, sizeof(struct napt_addr_4tu));
  460. napt->src_port = src_port;
  461. new_port = luat_napt_port_alloc();
  462. if (0 == new_port)
  463. {
  464. luat_napt_mem_free(napt);
  465. return NULL;
  466. }
  467. napt->new_port = htons(new_port);
  468. napt->src_ip = ip;
  469. napt->time_stamp++;
  470. #ifdef NAPT_TABLE_LIMIT
  471. napt_table_4udp.cnt++;
  472. #endif
  473. napt->next = napt_table_4udp.next;
  474. napt_table_4udp.next = napt;
  475. #ifdef NAPT_ALLOC_DEBUG
  476. printf("@@ napt udp port alloc %hu\r\n", ++napt4udp_cnt);
  477. #endif
  478. return napt;
  479. }
  480. static inline void luat_napt_table_update_4udp(struct napt_addr_4tu *napt)
  481. {
  482. if (!++napt->time_stamp)
  483. napt->time_stamp++;
  484. return;
  485. }
  486. static void luat_napt_table_check_4tcp(void)
  487. {
  488. struct napt_addr_4tu *napt4tcp;
  489. struct napt_addr_4tu *napt4tcp_prev;
  490. /* tcp */
  491. #ifdef NAPT_TABLE_MUTEX_LOCK
  492. #ifdef NAPT_USE_HW_TIMER
  493. if (luat_napt_try_lock(napt_table_lock_4tcp))
  494. {
  495. //printf("## try tcp\r\n");
  496. napt_check_tcp = TRUE;
  497. return;
  498. }
  499. napt_check_tcp = FALSE;
  500. #else
  501. luat_napt_lock(napt_table_lock_4tcp);
  502. #endif
  503. #endif
  504. for (napt4tcp_prev = napt_table_4tcp.next;\
  505. NULL != napt4tcp_prev;\
  506. napt4tcp_prev = napt4tcp_prev->next)
  507. {
  508. napt4tcp = napt4tcp_prev->next;
  509. if (NULL != napt4tcp)
  510. {
  511. if (0 == napt4tcp->time_stamp)
  512. {
  513. #ifdef NAPT_TABLE_LIMIT
  514. napt_table_4tcp.cnt--;
  515. #endif
  516. napt4tcp_prev->next = napt4tcp->next;
  517. napt4tcp->next = NULL;
  518. luat_napt_mem_free(napt4tcp);
  519. #ifdef NAPT_ALLOC_DEBUG
  520. printf("@@ napt tcp port free %hu\r\n", --napt4tcp_cnt);
  521. #endif
  522. }
  523. else
  524. {
  525. napt4tcp->time_stamp = 0;
  526. }
  527. }
  528. }
  529. napt4tcp = napt_table_4tcp.next;
  530. if (NULL != napt4tcp)
  531. {
  532. if (0 == napt4tcp->time_stamp)
  533. {
  534. #ifdef NAPT_TABLE_LIMIT
  535. napt_table_4tcp.cnt--;
  536. #endif
  537. napt_table_4tcp.next = napt4tcp->next;
  538. napt4tcp->next = NULL;
  539. luat_napt_mem_free(napt4tcp);
  540. #ifdef NAPT_ALLOC_DEBUG
  541. printf("@@ napt tcp port free %hu\r\n", --napt4tcp_cnt);
  542. #endif
  543. }
  544. else
  545. {
  546. napt4tcp->time_stamp = 0;
  547. }
  548. }
  549. #ifdef NAPT_TABLE_MUTEX_LOCK
  550. luat_napt_unlock(napt_table_lock_4tcp);
  551. #endif
  552. return;
  553. }
  554. static void luat_napt_table_check_4udp(void)
  555. {
  556. struct napt_addr_4tu *napt4udp;
  557. struct napt_addr_4tu *napt4udp_prev;
  558. /* udp */
  559. #ifdef NAPT_TABLE_MUTEX_LOCK
  560. #ifdef NAPT_USE_HW_TIMER
  561. if (luat_napt_try_lock(napt_table_lock_4udp))
  562. {
  563. //printf("## try udp\r\n");
  564. napt_check_udp = TRUE;
  565. return;
  566. }
  567. napt_check_udp = FALSE;
  568. #else
  569. luat_napt_lock(napt_table_lock_4udp);
  570. #endif
  571. #endif
  572. for (napt4udp_prev = napt_table_4udp.next;\
  573. NULL != napt4udp_prev;\
  574. napt4udp_prev = napt4udp_prev->next)
  575. {
  576. napt4udp = napt4udp_prev->next;
  577. if (NULL != napt4udp)
  578. {
  579. if (0 == napt4udp->time_stamp)
  580. {
  581. #ifdef NAPT_TABLE_LIMIT
  582. napt_table_4udp.cnt--;
  583. #endif
  584. napt4udp_prev->next = napt4udp->next;
  585. napt4udp->next = NULL;
  586. luat_napt_mem_free(napt4udp);
  587. #ifdef NAPT_ALLOC_DEBUG
  588. printf("@@ napt udp port free %hu\r\n", --napt4udp_cnt);
  589. #endif
  590. }
  591. else
  592. {
  593. napt4udp->time_stamp = 0;
  594. }
  595. }
  596. }
  597. napt4udp = napt_table_4udp.next;
  598. if (NULL != napt4udp)
  599. {
  600. if (0 == napt4udp->time_stamp)
  601. {
  602. #ifdef NAPT_TABLE_LIMIT
  603. napt_table_4udp.cnt--;
  604. #endif
  605. napt_table_4udp.next = napt4udp->next;
  606. napt4udp->next = NULL;
  607. luat_napt_mem_free(napt4udp);
  608. #ifdef NAPT_ALLOC_DEBUG
  609. printf("@@ napt udp port free %hu\r\n", --napt4udp_cnt);
  610. #endif
  611. }
  612. else
  613. {
  614. napt4udp->time_stamp = 0;
  615. }
  616. }
  617. #ifdef NAPT_TABLE_MUTEX_LOCK
  618. luat_napt_unlock(napt_table_lock_4udp);
  619. #endif
  620. return;
  621. }
  622. static void luat_napt_table_check_4ic(void)
  623. {
  624. struct napt_addr_4ic *napt4ic;
  625. struct napt_addr_4ic *napt4ic_prev;
  626. /* icmp */
  627. #ifdef NAPT_TABLE_MUTEX_LOCK
  628. #ifdef NAPT_USE_HW_TIMER
  629. if (luat_napt_try_lock(napt_table_lock_4ic))
  630. {
  631. //printf("## try ic\r\n");
  632. napt_check_ic = TRUE;
  633. return;
  634. }
  635. napt_check_ic = FALSE;
  636. #else
  637. luat_napt_lock(napt_table_lock_4ic);
  638. #endif
  639. #endif
  640. /* skip the first item */
  641. for (napt4ic_prev = napt_table_4ic.next;\
  642. NULL != napt4ic_prev;\
  643. napt4ic_prev = napt4ic_prev->next)
  644. {
  645. napt4ic = napt4ic_prev->next;
  646. if (NULL != napt4ic)
  647. {
  648. if (0 == napt4ic->time_stamp)
  649. {
  650. #ifdef NAPT_TABLE_LIMIT
  651. napt_table_4ic.cnt--;
  652. #endif
  653. napt4ic_prev->next = napt4ic->next;
  654. napt4ic->next = NULL;
  655. luat_napt_mem_free(napt4ic);
  656. #ifdef NAPT_ALLOC_DEBUG
  657. printf("@@ napt id free %hu\r\n", --napt4ic_cnt);
  658. #endif
  659. }
  660. else
  661. {
  662. napt4ic->time_stamp = 0;
  663. }
  664. }
  665. }
  666. /* check the first item */
  667. napt4ic = napt_table_4ic.next;
  668. if (NULL != napt4ic)
  669. {
  670. if (0 == napt4ic->time_stamp)
  671. {
  672. #ifdef NAPT_TABLE_LIMIT
  673. napt_table_4ic.cnt--;
  674. #endif
  675. napt_table_4ic.next = napt4ic->next;
  676. napt4ic->next = NULL;
  677. luat_napt_mem_free(napt4ic);
  678. #ifdef NAPT_ALLOC_DEBUG
  679. printf("@@ napt id free %hu\r\n", --napt4ic_cnt);
  680. #endif
  681. }
  682. else
  683. {
  684. napt4ic->time_stamp = 0;
  685. }
  686. }
  687. #ifdef NAPT_TABLE_MUTEX_LOCK
  688. luat_napt_unlock(napt_table_lock_4ic);
  689. #endif
  690. return;
  691. }
  692. static void luat_napt_table_check_4gre(void)
  693. {
  694. if (0 == gre_info.time_stamp)
  695. {
  696. gre_info.is_used = 0;
  697. //gre_info.src_ip = 0;
  698. }
  699. else
  700. {
  701. gre_info.time_stamp = 0;
  702. }
  703. return;
  704. }
  705. void luat_napt_table_check(void *arg)
  706. {
  707. luat_napt_table_check_4tcp();
  708. luat_napt_table_check_4udp();
  709. luat_napt_table_check_4ic();
  710. luat_napt_table_check_4gre();
  711. return;
  712. }
  713. #ifdef NAPT_USE_HW_TIMER
  714. static void luat_napt_table_check_try(void)
  715. {
  716. if (napt_check_tcp)
  717. luat_napt_table_check_4tcp();
  718. if (napt_check_udp)
  719. luat_napt_table_check_4udp();
  720. if (napt_check_ic)
  721. luat_napt_table_check_4ic();
  722. return;
  723. }
  724. #endif
  725. static inline u32 alg_hdr_16bitsum(const u16 *buff, u16 len)
  726. {
  727. u32 sum = 0;
  728. u16 *pos = (u16 *)buff;
  729. u16 remainder_size = len;
  730. while (remainder_size > 1)
  731. {
  732. sum += *pos ++;
  733. remainder_size -= NAPT_CHKSUM_16BIT_LEN;
  734. }
  735. if (remainder_size > 0)
  736. {
  737. sum += *(u8*)pos;
  738. }
  739. return sum;
  740. }
  741. static inline u16 alg_iphdr_chksum(const u16 *buff, u16 len)
  742. {
  743. u32 sum = alg_hdr_16bitsum(buff, len);
  744. sum = (sum >> 16) + (sum & 0xFFFF);
  745. sum += (sum >> 16);
  746. return (u16)(~sum);
  747. }
  748. static inline u16 alg_tcpudphdr_chksum(u32 src_addr, u32 dst_addr, u8 proto,
  749. const u16 *buff, u16 len)
  750. {
  751. u32 sum = 0;
  752. sum += (src_addr & 0xffffUL);
  753. sum += ((src_addr >> 16) & 0xffffUL);
  754. sum += (dst_addr & 0xffffUL);
  755. sum += ((dst_addr >> 16) & 0xffffUL);
  756. sum += (u32)htons((u16)proto);
  757. sum += (u32)htons(len);
  758. sum += alg_hdr_16bitsum(buff, len);
  759. sum = (sum >> 16) + (sum & 0xFFFF);
  760. sum += (sum >> 16);
  761. return (u16)(~sum);
  762. }
  763. static int alg_icmp_proc(u8 is_inet,
  764. struct ip_hdr *ip_hdr,
  765. ip_addr_t* gw_ip)
  766. {
  767. struct napt_addr_4ic *napt;
  768. struct icmp_echo_hdr *icmp_hdr;
  769. u8* ptr = ((u8*)ip_hdr) - 14;
  770. u8 iphdr_len;
  771. iphdr_len = (ip_hdr->_v_hl & 0x0F) * 4;
  772. icmp_hdr = (struct icmp_echo_hdr *)((u8 *)ip_hdr + iphdr_len);
  773. if (is_inet)
  774. {
  775. #ifdef NAPT_TABLE_MUTEX_LOCK
  776. luat_napt_lock(napt_table_lock_4ic);
  777. #endif
  778. napt = luat_napt_get_by_src_id(icmp_hdr->id, ip_hdr->src.addr >> 24);
  779. if (NULL == napt)
  780. {
  781. napt = luat_napt_table_insert_4ic(icmp_hdr->id, ip_hdr->src.addr >> 24);
  782. if (NULL == napt)
  783. {
  784. #ifdef NAPT_TABLE_MUTEX_LOCK
  785. luat_napt_unlock(napt_table_lock_4ic);
  786. #endif
  787. return -1;
  788. }
  789. }
  790. else
  791. {
  792. luat_napt_table_update_4ic(napt);
  793. }
  794. icmp_hdr->id = napt->new_id;
  795. memcpy(napt->mac, ptr + 6, 6);
  796. #ifdef NAPT_TABLE_MUTEX_LOCK
  797. luat_napt_unlock(napt_table_lock_4ic);
  798. #endif
  799. icmp_hdr->chksum = 0;
  800. icmp_hdr->chksum = alg_iphdr_chksum((u16 *)icmp_hdr, ntohs(ip_hdr->_len) - iphdr_len);
  801. ip_hdr->src.addr = ip_addr_get_ip4_u32(gw_ip);
  802. ip_hdr->_chksum = 0;
  803. ip_hdr->_chksum = alg_iphdr_chksum((u16 *)ip_hdr, iphdr_len);
  804. return 0; // 已经改造完成, 可以返回了
  805. }
  806. else
  807. {
  808. #ifdef NAPT_TABLE_MUTEX_LOCK
  809. luat_napt_lock(napt_table_lock_4ic);
  810. #endif
  811. napt = luat_napt_get_by_dst_id(icmp_hdr->id);
  812. if (NULL != napt)
  813. {
  814. //luat_napt_table_update_4ic(napt);
  815. icmp_hdr->id = napt->src_id;
  816. icmp_hdr->chksum = 0;
  817. icmp_hdr->chksum = alg_iphdr_chksum((u16 *)icmp_hdr, ntohs(ip_hdr->_len) - iphdr_len);
  818. ip_hdr->dest.addr = ((napt->src_ip) << 24) | (ip_addr_get_ip4_u32(gw_ip) & 0x00ffffff);
  819. #ifdef NAPT_TABLE_MUTEX_LOCK
  820. luat_napt_unlock(napt_table_lock_4ic);
  821. #endif
  822. ip_hdr->_chksum = 0;
  823. ip_hdr->_chksum = alg_iphdr_chksum((u16 *)ip_hdr, iphdr_len);
  824. memcpy(ptr, napt->mac, 6);
  825. return 0; // 已经改造完成, 可以返回了
  826. }
  827. else
  828. {
  829. #ifdef NAPT_TABLE_MUTEX_LOCK
  830. luat_napt_unlock(napt_table_lock_4ic);
  831. #endif
  832. }
  833. }
  834. return -1;
  835. }
  836. static int alg_tcp_proc(u8 is_inet,
  837. struct ip_hdr *ip_hdr,
  838. ip_addr_t* gw_ip)
  839. {
  840. u8 src_ip;
  841. struct napt_addr_4tu *napt;
  842. struct tcp_hdr *tcp_hdr;
  843. u8* ptr = ((u8*)ip_hdr) - 14;
  844. u8 iphdr_len;
  845. iphdr_len = (ip_hdr->_v_hl & 0x0F) * 4;
  846. tcp_hdr = (struct tcp_hdr *)((u8 *)ip_hdr + iphdr_len);
  847. if (is_inet)
  848. {
  849. #ifdef NAPT_TABLE_MUTEX_LOCK
  850. luat_napt_lock(napt_table_lock_4tcp);
  851. #endif
  852. src_ip = ip_hdr->src.addr >> 24;
  853. napt = luat_napt_get_tcp_port_by_src(tcp_hdr->src, src_ip);
  854. if (NULL == napt)
  855. {
  856. napt = luat_napt_table_insert_4tcp(tcp_hdr->src, src_ip);
  857. if (NULL == napt)
  858. {
  859. #ifdef NAPT_TABLE_MUTEX_LOCK
  860. luat_napt_unlock(napt_table_lock_4tcp);
  861. #endif
  862. return -1;
  863. }
  864. // LLOGD("分配新的TCP映射 ip %d port %d -> %d", src_ip, tcp_hdr->src, napt->new_port);
  865. }
  866. else
  867. {
  868. luat_napt_table_update_4tcp(napt);
  869. // LLOGD("复用老的TCP映射 ip %d port %d -> %d", src_ip, tcp_hdr->src, napt->new_port);
  870. }
  871. memcpy(napt->mac, ptr + 6, 6); //保存源mac地址
  872. // LLOGD("记录内网-->外网的源MAC %02X%02X%02X%02X%02X%02X", napt->mac[0], napt->mac[1], napt->mac[2], napt->mac[3], napt->mac[4], napt->mac[5]);
  873. tcp_hdr->src = napt->new_port;
  874. #ifdef NAPT_TABLE_MUTEX_LOCK
  875. luat_napt_unlock(napt_table_lock_4tcp);
  876. #endif
  877. ip_hdr->src.addr = ip_addr_get_ip4_u32(gw_ip);
  878. ip_hdr->_chksum = 0;
  879. ip_hdr->_chksum = alg_iphdr_chksum((u16 *)ip_hdr, iphdr_len);
  880. tcp_hdr->chksum = 0;
  881. tcp_hdr->chksum = alg_tcpudphdr_chksum(ip_hdr->src.addr,
  882. ip_hdr->dest.addr,
  883. IP_PROTO_TCP,
  884. (u16 *)tcp_hdr,
  885. ntohs(ip_hdr->_len) - iphdr_len);
  886. return 0;
  887. }
  888. /* from ap... */
  889. else
  890. {
  891. #ifdef NAPT_TABLE_MUTEX_LOCK
  892. luat_napt_lock(napt_table_lock_4tcp);
  893. #endif
  894. napt = luat_napt_get_tcp_port_by_dest(tcp_hdr->dest);
  895. /* forward to sta... */
  896. if (NULL != napt)
  897. {
  898. //luat_napt_table_update_4tcp(napt);
  899. ip_hdr->dest.addr = (napt->src_ip << 24) | (ip_addr_get_ip4_u32(gw_ip) & 0x00ffffff);
  900. ip_hdr->_chksum = 0;
  901. ip_hdr->_chksum = alg_iphdr_chksum((u16 *)ip_hdr, iphdr_len);
  902. tcp_hdr->dest = napt->src_port;
  903. #ifdef NAPT_TABLE_MUTEX_LOCK
  904. luat_napt_unlock(napt_table_lock_4tcp);
  905. #endif
  906. tcp_hdr->chksum = 0;
  907. tcp_hdr->chksum = alg_tcpudphdr_chksum(ip_hdr->src.addr,
  908. ip_hdr->dest.addr,
  909. IP_PROTO_TCP,
  910. (u16 *)tcp_hdr,
  911. ntohs(ip_hdr->_len) - iphdr_len);
  912. memcpy(ptr, napt->mac, 6); // 还原MAC地址
  913. return 0;
  914. }
  915. /* deliver to default gateway */
  916. else
  917. {
  918. #ifdef NAPT_TABLE_MUTEX_LOCK
  919. luat_napt_unlock(napt_table_lock_4tcp);
  920. #endif
  921. }
  922. }
  923. return -1;
  924. }
  925. static int alg_udp_proc(u8 is_inet,
  926. struct ip_hdr *ip_hdr,
  927. ip_addr_t* gw_ip)
  928. {
  929. u8 src_ip;
  930. struct napt_addr_4tu *napt;
  931. struct udp_hdr *udp_hdr;
  932. u8* ptr = ((u8*)ip_hdr) - 14;
  933. u8 iphdr_len;
  934. iphdr_len = (ip_hdr->_v_hl & 0x0F) * 4;
  935. udp_hdr = (struct udp_hdr *)((u8 *)ip_hdr + iphdr_len);
  936. /* from sta... */
  937. if (is_inet)
  938. {
  939. /* create/update napt item */
  940. #ifdef NAPT_TABLE_MUTEX_LOCK
  941. luat_napt_lock(napt_table_lock_4udp);
  942. #endif
  943. src_ip = ip_hdr->src.addr >> 24;
  944. napt = luat_napt_get_udp_port_by_src(udp_hdr->src, src_ip);
  945. if (NULL == napt)
  946. {
  947. napt = luat_napt_table_insert_4udp(udp_hdr->src, src_ip);
  948. if (NULL == napt)
  949. {
  950. #ifdef NAPT_TABLE_MUTEX_LOCK
  951. luat_napt_unlock(napt_table_lock_4udp);
  952. #endif
  953. return -1;
  954. }
  955. }
  956. else
  957. {
  958. luat_napt_table_update_4udp(napt);
  959. }
  960. udp_hdr->src = napt->new_port;
  961. memcpy(napt->mac, ptr + 6, 6); // 拷贝源MAC地址
  962. #ifdef NAPT_TABLE_MUTEX_LOCK
  963. luat_napt_unlock(napt_table_lock_4udp);
  964. #endif
  965. // redo:
  966. ip_hdr->src.addr = ip_addr_get_ip4_u32(gw_ip);
  967. ip_hdr->_chksum = 0;
  968. ip_hdr->_chksum = alg_iphdr_chksum((u16 *)ip_hdr, iphdr_len);
  969. if (0 != udp_hdr->chksum)
  970. {
  971. udp_hdr->chksum = 0;
  972. udp_hdr->chksum = alg_tcpudphdr_chksum(ip_hdr->src.addr,
  973. ip_hdr->dest.addr,
  974. IP_PROTO_UDP,
  975. (u16 *)udp_hdr,
  976. ntohs(ip_hdr->_len) - iphdr_len);
  977. }
  978. return 0;
  979. }
  980. /* form ap... */
  981. else
  982. {
  983. #ifdef NAPT_TABLE_MUTEX_LOCK
  984. luat_napt_lock(napt_table_lock_4udp);
  985. #endif
  986. napt = luat_napt_get_udp_port_by_dest(udp_hdr->dest);
  987. /* forward to sta... */
  988. if (NULL != napt)
  989. {
  990. luat_napt_table_update_4udp(napt);
  991. ip_hdr->dest.addr = (napt->src_ip << 24) | (ip_addr_get_ip4_u32(gw_ip) & 0x00ffffff);
  992. udp_hdr->dest = napt->src_port;
  993. #ifdef NAPT_TABLE_MUTEX_LOCK
  994. luat_napt_unlock(napt_table_lock_4udp);
  995. #endif
  996. ip_hdr->_chksum = 0;
  997. ip_hdr->_chksum = alg_iphdr_chksum((u16 *)ip_hdr, iphdr_len);
  998. if (0 != udp_hdr->chksum)
  999. {
  1000. udp_hdr->chksum = 0;
  1001. udp_hdr->chksum = alg_tcpudphdr_chksum(ip_hdr->src.addr,
  1002. ip_hdr->dest.addr,
  1003. IP_PROTO_UDP,
  1004. (u16 *)udp_hdr,
  1005. ntohs(ip_hdr->_len) - iphdr_len);
  1006. }
  1007. memcpy(ptr, napt->mac, 6);
  1008. return 0;
  1009. }
  1010. /* deliver to default gateway */
  1011. else
  1012. {
  1013. #ifdef NAPT_TABLE_MUTEX_LOCK
  1014. luat_napt_unlock(napt_table_lock_4udp);
  1015. #endif
  1016. }
  1017. }
  1018. // end:
  1019. return -1;
  1020. }
  1021. static int alg_gre_proc(u8 is_inet, struct ip_hdr *ip_hdr, ip_addr_t* gw_ip)
  1022. {
  1023. // int err;
  1024. u8 src_ip;
  1025. u8 iphdr_len;
  1026. iphdr_len = (ip_hdr->_v_hl & 0x0F) * 4;
  1027. /* from sta... */
  1028. if (is_inet)
  1029. {
  1030. src_ip = ip_hdr->src.addr >> 24;
  1031. if (src_ip == gre_info.src_ip) /* current vpn */
  1032. {
  1033. gre_info.time_stamp++;
  1034. }
  1035. else if (1 == gre_info.is_used)/* vpn used */
  1036. {
  1037. return -1;
  1038. }
  1039. else /* new vpn */
  1040. {
  1041. gre_info.is_used = 1;
  1042. gre_info.src_ip = src_ip;
  1043. if (!++gre_info.time_stamp)
  1044. gre_info.time_stamp++;
  1045. }
  1046. ip_hdr->src.addr = ip_addr_get_ip4_u32(gw_ip);
  1047. ip_hdr->_chksum = 0;
  1048. ip_hdr->_chksum = alg_iphdr_chksum((u16 *)ip_hdr, iphdr_len);
  1049. return 0;
  1050. }
  1051. /* from ap... */
  1052. else
  1053. {
  1054. ip_hdr->dest.addr = (gre_info.src_ip << 24) | (ip_addr_get_ip4_u32(gw_ip) & 0x00ffffff);
  1055. ip_hdr->_chksum = 0;
  1056. ip_hdr->_chksum = alg_iphdr_chksum((u16 *)ip_hdr, iphdr_len);
  1057. return 0;
  1058. }
  1059. }
  1060. int luat_napt_input(u8 is_inet, u8 *pkt_body, u32 pkt_len, ip_addr_t* gw_ip)
  1061. {
  1062. int err = -1;
  1063. struct ip_hdr *ip_hdr = NULL;
  1064. #ifdef NAPT_USE_HW_TIMER
  1065. luat_napt_table_check_try();
  1066. #endif
  1067. ip_hdr = (struct ip_hdr *)(pkt_body + 14);
  1068. switch(ip_hdr->_proto)
  1069. {
  1070. case IP_PROTO_ICMP:
  1071. {
  1072. err = alg_icmp_proc(is_inet, ip_hdr, gw_ip);
  1073. if (err)
  1074. LLOGD("ICMP包 rc %d", err);
  1075. break;
  1076. }
  1077. case IP_PROTO_TCP:
  1078. {
  1079. err = alg_tcp_proc(is_inet, ip_hdr, gw_ip);
  1080. if (err)
  1081. LLOGD("TCP包 rc %d", err);
  1082. break;
  1083. }
  1084. case IP_PROTO_UDP:
  1085. {
  1086. err = alg_udp_proc(is_inet, ip_hdr, gw_ip);
  1087. if (err)
  1088. LLOGD("UDP包 rc %d", err);
  1089. break;
  1090. }
  1091. case IP_PROTO_GRE:/* vpn */
  1092. {
  1093. err = alg_gre_proc(is_inet, ip_hdr, gw_ip);
  1094. if (err)
  1095. LLOGD("GRE包 rc %d", err);
  1096. break;
  1097. }
  1098. default:
  1099. {
  1100. LLOGW("不认识的协议包 0x%02X", ip_hdr->_proto);
  1101. err = -1;
  1102. break;
  1103. }
  1104. }
  1105. return err;
  1106. }
  1107. u8 luat_napt_port_is_used(u16 port)
  1108. {
  1109. u8 is_used = 0;
  1110. struct napt_addr_4tu *napt_tcp;
  1111. struct napt_addr_4tu *napt_udp;
  1112. #ifdef NAPT_TABLE_MUTEX_LOCK
  1113. luat_napt_lock(napt_table_lock_4tcp);
  1114. #endif
  1115. NAPT_TABLE_FOREACH(napt_tcp, napt_table_4tcp)
  1116. {
  1117. if (port == napt_tcp->new_port)
  1118. {
  1119. is_used = 1;
  1120. break;
  1121. }
  1122. }
  1123. #ifdef NAPT_TABLE_MUTEX_LOCK
  1124. luat_napt_unlock(napt_table_lock_4tcp);
  1125. #endif
  1126. if (1 != is_used)
  1127. {
  1128. #ifdef NAPT_TABLE_MUTEX_LOCK
  1129. luat_napt_lock(napt_table_lock_4udp);
  1130. #endif
  1131. NAPT_TABLE_FOREACH(napt_udp, napt_table_4udp)
  1132. {
  1133. if (port == napt_udp->new_port)
  1134. {
  1135. is_used = 1;
  1136. break;
  1137. }
  1138. }
  1139. #ifdef NAPT_TABLE_MUTEX_LOCK
  1140. luat_napt_unlock(napt_table_lock_4udp);
  1141. #endif
  1142. }
  1143. return is_used;
  1144. }
  1145. int luat_napt_init(void)
  1146. {
  1147. int err = 0;
  1148. #ifdef NAPT_USE_HW_TIMER
  1149. struct tls_timer_cfg timer_cfg;
  1150. #endif
  1151. memset(&napt_table_4tcp, 0, sizeof(struct napt_table_head_4tu));
  1152. memset(&napt_table_4udp, 0, sizeof(struct napt_table_head_4tu));
  1153. memset(&napt_table_4ic, 0, sizeof(struct napt_table_head_4ic));
  1154. napt_curr_port = NAPT_LOCAL_PORT_RANGE_START;
  1155. napt_curr_id = NAPT_ICMP_ID_RANGE_START;
  1156. #ifdef NAPT_TABLE_MUTEX_LOCK
  1157. err = tls_os_sem_create(&napt_table_lock_4tcp, 1);
  1158. if (TLS_OS_SUCCESS != err)
  1159. {
  1160. NAPT_PRINT("failed to init alg.\n");
  1161. return err;
  1162. }
  1163. err = tls_os_sem_create(&napt_table_lock_4udp, 1);
  1164. if (TLS_OS_SUCCESS != err)
  1165. {
  1166. NAPT_PRINT("failed to init alg.\n");
  1167. return err;
  1168. }
  1169. err = tls_os_sem_create(&napt_table_lock_4ic, 1);
  1170. if (TLS_OS_SUCCESS != err)
  1171. {
  1172. NAPT_PRINT("failed to init alg.\n");
  1173. }
  1174. #endif
  1175. #ifdef NAPT_ALLOC_DEBUG
  1176. napt4ic_cnt = 0;
  1177. napt4tcp_cnt = 0;
  1178. napt4udp_cnt = 0;
  1179. #endif
  1180. #ifdef NAPT_USE_HW_TIMER
  1181. memset(&timer_cfg, 0, sizeof(timer_cfg));
  1182. timer_cfg.unit = TLS_TIMER_UNIT_MS;
  1183. timer_cfg.timeout = NAPT_TMR_INTERVAL;
  1184. timer_cfg.is_repeat = TRUE;
  1185. timer_cfg.callback = luat_napt_table_check;
  1186. err = tls_timer_create(&timer_cfg);
  1187. if (WM_TIMER_ID_INVALID != err)
  1188. {
  1189. tls_timer_start(err);
  1190. err = 0;
  1191. }
  1192. else
  1193. {
  1194. NAPT_PRINT("failed to init alg timer.\n");
  1195. }
  1196. #else
  1197. #endif
  1198. return err;
  1199. }