SaintAnd

(TL-WN722N)_rtl8188eus-5.3.9_src/core/rtw_br_ext.c

Jan 18th, 2022
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 44.53 KB | None | 0 0
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2017 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. *****************************************************************************/
  15. #define _RTW_BR_EXT_C_
  16.  
  17. #ifdef __KERNEL__
  18. #include <linux/if_arp.h>
  19. #include <net/ip.h>
  20. #include <linux/atalk.h>
  21. #include <linux/udp.h>
  22. #include <linux/if_pppox.h>
  23. #endif
  24.  
  25. #if 1 /* rtw_wifi_driver */
  26. #include <drv_types.h>
  27. #else /* rtw_wifi_driver */
  28. #include "./8192cd_cfg.h"
  29.  
  30. #ifndef __KERNEL__
  31. #include "./sys-support.h"
  32. #endif
  33.  
  34. #include "./8192cd.h"
  35. #include "./8192cd_headers.h"
  36. #include "./8192cd_br_ext.h"
  37. #include "./8192cd_debug.h"
  38. #endif /* rtw_wifi_driver */
  39.  
  40. #ifdef CL_IPV6_PASS
  41. #ifdef __KERNEL__
  42. #include <linux/ipv6.h>
  43. #include <linux/icmpv6.h>
  44. #include <net/ndisc.h>
  45. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24))
  46. #include <net/ip6_checksum.h>
  47. #else
  48. #include <net/checksum.h>
  49. #endif
  50. #endif
  51. #endif
  52.  
  53. #ifdef CONFIG_BR_EXT
  54.  
  55. /* #define BR_EXT_DEBUG */
  56.  
  57. #define NAT25_IPV4 01
  58. #define NAT25_IPV6 02
  59. #define NAT25_IPX 03
  60. #define NAT25_APPLE 04
  61. #define NAT25_PPPOE 05
  62.  
  63. #define RTL_RELAY_TAG_LEN (ETH_ALEN)
  64. #define TAG_HDR_LEN 4
  65.  
  66. #define MAGIC_CODE 0x8186
  67. #define MAGIC_CODE_LEN 2
  68. #define WAIT_TIME_PPPOE 5 /* waiting time for pppoe server in sec */
  69.  
  70. /*-----------------------------------------------------------------
  71. How database records network address:
  72. 0 1 2 3 4 5 6 7 8 9 10
  73. |----|----|----|----|----|----|----|----|----|----|----|
  74. IPv4 |type| | IP addr |
  75. IPX |type| Net addr | Node addr |
  76. IPX |type| Net addr |Sckt addr|
  77. Apple |type| Network |node|
  78. PPPoE |type| SID | AC MAC |
  79. -----------------------------------------------------------------*/
  80.  
  81.  
  82. /* Find a tag in pppoe frame and return the pointer */
  83. static __inline__ unsigned char *__nat25_find_pppoe_tag(struct pppoe_hdr *ph, unsigned short type)
  84. {
  85. unsigned char *cur_ptr, *start_ptr;
  86. unsigned short tagLen, tagType;
  87.  
  88. start_ptr = cur_ptr = (unsigned char *)ph->tag;
  89. while ((cur_ptr - start_ptr) < ntohs(ph->length)) {
  90. /* prevent un-alignment access */
  91. tagType = (unsigned short)((cur_ptr[0] << 8) + cur_ptr[1]);
  92. tagLen = (unsigned short)((cur_ptr[2] << 8) + cur_ptr[3]);
  93. if (tagType == type)
  94. return cur_ptr;
  95. cur_ptr = cur_ptr + TAG_HDR_LEN + tagLen;
  96. }
  97. return 0;
  98. }
  99.  
  100.  
  101. static __inline__ int __nat25_add_pppoe_tag(struct sk_buff *skb, struct pppoe_tag *tag)
  102. {
  103. struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
  104. int data_len;
  105.  
  106. data_len = tag->tag_len + TAG_HDR_LEN;
  107. if (skb_tailroom(skb) < data_len) {
  108. _DEBUG_ERR("skb_tailroom() failed in add SID tag!\n");
  109. return -1;
  110. }
  111.  
  112. skb_put(skb, data_len);
  113. /* have a room for new tag */
  114. memmove(((unsigned char *)ph->tag + data_len), (unsigned char *)ph->tag, ntohs(ph->length));
  115. ph->length = htons(ntohs(ph->length) + data_len);
  116. memcpy((unsigned char *)ph->tag, tag, data_len);
  117. return data_len;
  118. }
  119.  
  120. static int skb_pull_and_merge(struct sk_buff *skb, unsigned char *src, int len)
  121. {
  122. int tail_len;
  123. unsigned long end, tail;
  124.  
  125. if ((src + len) > skb_tail_pointer(skb) || skb->len < len)
  126. return -1;
  127.  
  128. tail = (unsigned long)skb_tail_pointer(skb);
  129. end = (unsigned long)src + len;
  130. if (tail < end)
  131. return -1;
  132.  
  133. tail_len = (int)(tail - end);
  134. if (tail_len > 0)
  135. memmove(src, src + len, tail_len);
  136.  
  137. skb_trim(skb, skb->len - len);
  138. return 0;
  139. }
  140.  
  141. static __inline__ unsigned long __nat25_timeout(_adapter *priv)
  142. {
  143. unsigned long timeout;
  144.  
  145. timeout = jiffies - NAT25_AGEING_TIME * HZ;
  146.  
  147. return timeout;
  148. }
  149.  
  150.  
  151. static __inline__ int __nat25_has_expired(_adapter *priv,
  152. struct nat25_network_db_entry *fdb)
  153. {
  154. if (time_before_eq(fdb->ageing_timer, __nat25_timeout(priv)))
  155. return 1;
  156.  
  157. return 0;
  158. }
  159.  
  160.  
  161. static __inline__ void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr,
  162. unsigned int *ipAddr)
  163. {
  164. memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
  165.  
  166. networkAddr[0] = NAT25_IPV4;
  167. memcpy(networkAddr + 7, (unsigned char *)ipAddr, 4);
  168. }
  169.  
  170.  
  171. static __inline__ void __nat25_generate_ipx_network_addr_with_node(unsigned char *networkAddr,
  172. unsigned int *ipxNetAddr, unsigned char *ipxNodeAddr)
  173. {
  174. memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
  175.  
  176. networkAddr[0] = NAT25_IPX;
  177. memcpy(networkAddr + 1, (unsigned char *)ipxNetAddr, 4);
  178. memcpy(networkAddr + 5, ipxNodeAddr, 6);
  179. }
  180.  
  181.  
  182. static __inline__ void __nat25_generate_ipx_network_addr_with_socket(unsigned char *networkAddr,
  183. unsigned int *ipxNetAddr, unsigned short *ipxSocketAddr)
  184. {
  185. memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
  186.  
  187. networkAddr[0] = NAT25_IPX;
  188. memcpy(networkAddr + 1, (unsigned char *)ipxNetAddr, 4);
  189. memcpy(networkAddr + 5, (unsigned char *)ipxSocketAddr, 2);
  190. }
  191.  
  192.  
  193. static __inline__ void __nat25_generate_apple_network_addr(unsigned char *networkAddr,
  194. unsigned short *network, unsigned char *node)
  195. {
  196. memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
  197.  
  198. networkAddr[0] = NAT25_APPLE;
  199. memcpy(networkAddr + 1, (unsigned char *)network, 2);
  200. networkAddr[3] = *node;
  201. }
  202.  
  203.  
  204. static __inline__ void __nat25_generate_pppoe_network_addr(unsigned char *networkAddr,
  205. unsigned char *ac_mac, unsigned short *sid)
  206. {
  207. memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
  208.  
  209. networkAddr[0] = NAT25_PPPOE;
  210. memcpy(networkAddr + 1, (unsigned char *)sid, 2);
  211. memcpy(networkAddr + 3, (unsigned char *)ac_mac, 6);
  212. }
  213.  
  214.  
  215. #ifdef CL_IPV6_PASS
  216. static void __nat25_generate_ipv6_network_addr(unsigned char *networkAddr,
  217. unsigned int *ipAddr)
  218. {
  219. memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
  220.  
  221. networkAddr[0] = NAT25_IPV6;
  222. memcpy(networkAddr + 1, (unsigned char *)ipAddr, 16);
  223. }
  224.  
  225.  
  226. static unsigned char *scan_tlv(unsigned char *data, int len, unsigned char tag, unsigned char len8b)
  227. {
  228. while (len > 0) {
  229. if (*data == tag && *(data + 1) == len8b && len >= len8b * 8)
  230. return data + 2;
  231.  
  232. len -= (*(data + 1)) * 8;
  233. data += (*(data + 1)) * 8;
  234. }
  235. return NULL;
  236. }
  237.  
  238.  
  239. static int update_nd_link_layer_addr(unsigned char *data, int len, unsigned char *replace_mac)
  240. {
  241. struct icmp6hdr *icmphdr = (struct icmp6hdr *)data;
  242. unsigned char *mac;
  243.  
  244. if (icmphdr->icmp6_type == NDISC_ROUTER_SOLICITATION) {
  245. if (len >= 8) {
  246. mac = scan_tlv(&data[8], len - 8, 1, 1);
  247. if (mac) {
  248. RTW_INFO("Router Solicitation, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
  249. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
  250. replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
  251. memcpy(mac, replace_mac, 6);
  252. return 1;
  253. }
  254. }
  255. } else if (icmphdr->icmp6_type == NDISC_ROUTER_ADVERTISEMENT) {
  256. if (len >= 16) {
  257. mac = scan_tlv(&data[16], len - 16, 1, 1);
  258. if (mac) {
  259. RTW_INFO("Router Advertisement, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
  260. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
  261. replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
  262. memcpy(mac, replace_mac, 6);
  263. return 1;
  264. }
  265. }
  266. } else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
  267. if (len >= 24) {
  268. mac = scan_tlv(&data[24], len - 24, 1, 1);
  269. if (mac) {
  270. RTW_INFO("Neighbor Solicitation, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
  271. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
  272. replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
  273. memcpy(mac, replace_mac, 6);
  274. return 1;
  275. }
  276. }
  277. } else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  278. if (len >= 24) {
  279. mac = scan_tlv(&data[24], len - 24, 2, 1);
  280. if (mac) {
  281. RTW_INFO("Neighbor Advertisement, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
  282. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
  283. replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
  284. memcpy(mac, replace_mac, 6);
  285. return 1;
  286. }
  287. }
  288. } else if (icmphdr->icmp6_type == NDISC_REDIRECT) {
  289. if (len >= 40) {
  290. mac = scan_tlv(&data[40], len - 40, 2, 1);
  291. if (mac) {
  292. RTW_INFO("Redirect, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
  293. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
  294. replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
  295. memcpy(mac, replace_mac, 6);
  296. return 1;
  297. }
  298. }
  299. }
  300. return 0;
  301. }
  302.  
  303. #ifdef SUPPORT_RX_UNI2MCAST
  304. static void convert_ipv6_mac_to_mc(struct sk_buff *skb)
  305. {
  306. struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN);
  307. unsigned char *dst_mac = skb->data;
  308.  
  309. /* dst_mac[0] = 0xff; */
  310. /* dst_mac[1] = 0xff; */
  311. /*modified by qinjunjie,ipv6 multicast address ix 0x33-33-xx-xx-xx-xx*/
  312. dst_mac[0] = 0x33;
  313. dst_mac[1] = 0x33;
  314. memcpy(&dst_mac[2], &iph->daddr.s6_addr32[3], 4);
  315. #if defined(__LINUX_2_6__)
  316. /*modified by qinjunjie,warning:should not remove next line*/
  317. skb->pkt_type = PACKET_MULTICAST;
  318. #endif
  319. }
  320. #endif /* CL_IPV6_PASS */
  321. #endif /* SUPPORT_RX_UNI2MCAST */
  322.  
  323.  
  324. static __inline__ int __nat25_network_hash(unsigned char *networkAddr)
  325. {
  326. if (networkAddr[0] == NAT25_IPV4) {
  327. unsigned long x;
  328.  
  329. x = networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];
  330.  
  331. return x & (NAT25_HASH_SIZE - 1);
  332. } else if (networkAddr[0] == NAT25_IPX) {
  333. unsigned long x;
  334.  
  335. x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^
  336. networkAddr[6] ^ networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];
  337.  
  338. return x & (NAT25_HASH_SIZE - 1);
  339. } else if (networkAddr[0] == NAT25_APPLE) {
  340. unsigned long x;
  341.  
  342. x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3];
  343.  
  344. return x & (NAT25_HASH_SIZE - 1);
  345. } else if (networkAddr[0] == NAT25_PPPOE) {
  346. unsigned long x;
  347.  
  348. x = networkAddr[0] ^ networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^ networkAddr[6] ^ networkAddr[7] ^ networkAddr[8];
  349.  
  350. return x & (NAT25_HASH_SIZE - 1);
  351. }
  352. #ifdef CL_IPV6_PASS
  353. else if (networkAddr[0] == NAT25_IPV6) {
  354. unsigned long x;
  355.  
  356. x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^
  357. networkAddr[6] ^ networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10] ^
  358. networkAddr[11] ^ networkAddr[12] ^ networkAddr[13] ^ networkAddr[14] ^ networkAddr[15] ^
  359. networkAddr[16];
  360.  
  361. return x & (NAT25_HASH_SIZE - 1);
  362. }
  363. #endif
  364. else {
  365. unsigned long x = 0;
  366. int i;
  367.  
  368. for (i = 0; i < MAX_NETWORK_ADDR_LEN; i++)
  369. x ^= networkAddr[i];
  370.  
  371. return x & (NAT25_HASH_SIZE - 1);
  372. }
  373. }
  374.  
  375.  
  376. static __inline__ void __network_hash_link(_adapter *priv,
  377. struct nat25_network_db_entry *ent, int hash)
  378. {
  379. /* Caller must _enter_critical_bh already! */
  380. /* _irqL irqL; */
  381. /* _enter_critical_bh(&priv->br_ext_lock, &irqL); */
  382.  
  383. ent->next_hash = priv->nethash[hash];
  384. if (ent->next_hash != NULL)
  385. ent->next_hash->pprev_hash = &ent->next_hash;
  386. priv->nethash[hash] = ent;
  387. ent->pprev_hash = &priv->nethash[hash];
  388.  
  389. /* _exit_critical_bh(&priv->br_ext_lock, &irqL); */
  390. }
  391.  
  392.  
  393. static __inline__ void __network_hash_unlink(struct nat25_network_db_entry *ent)
  394. {
  395. /* Caller must _enter_critical_bh already! */
  396. /* _irqL irqL; */
  397. /* _enter_critical_bh(&priv->br_ext_lock, &irqL); */
  398.  
  399. *(ent->pprev_hash) = ent->next_hash;
  400. if (ent->next_hash != NULL)
  401. ent->next_hash->pprev_hash = ent->pprev_hash;
  402. ent->next_hash = NULL;
  403. ent->pprev_hash = NULL;
  404.  
  405. /* _exit_critical_bh(&priv->br_ext_lock, &irqL); */
  406. }
  407.  
  408.  
  409. static int __nat25_db_network_lookup_and_replace(_adapter *priv,
  410. struct sk_buff *skb, unsigned char *networkAddr)
  411. {
  412. struct nat25_network_db_entry *db;
  413. _irqL irqL;
  414. _enter_critical_bh(&priv->br_ext_lock, &irqL);
  415.  
  416. db = priv->nethash[__nat25_network_hash(networkAddr)];
  417. while (db != NULL) {
  418. if (!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {
  419. if (!__nat25_has_expired(priv, db)) {
  420. /* replace the destination mac address */
  421. memcpy(skb->data, db->macAddr, ETH_ALEN);
  422. atomic_inc(&db->use_count);
  423.  
  424. #ifdef CL_IPV6_PASS
  425. RTW_INFO("NAT25: Lookup M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
  426. "%02x%02x%02x%02x%02x%02x\n",
  427. db->macAddr[0],
  428. db->macAddr[1],
  429. db->macAddr[2],
  430. db->macAddr[3],
  431. db->macAddr[4],
  432. db->macAddr[5],
  433. db->networkAddr[0],
  434. db->networkAddr[1],
  435. db->networkAddr[2],
  436. db->networkAddr[3],
  437. db->networkAddr[4],
  438. db->networkAddr[5],
  439. db->networkAddr[6],
  440. db->networkAddr[7],
  441. db->networkAddr[8],
  442. db->networkAddr[9],
  443. db->networkAddr[10],
  444. db->networkAddr[11],
  445. db->networkAddr[12],
  446. db->networkAddr[13],
  447. db->networkAddr[14],
  448. db->networkAddr[15],
  449. db->networkAddr[16]);
  450. #else
  451. RTW_INFO("NAT25: Lookup M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  452. db->macAddr[0],
  453. db->macAddr[1],
  454. db->macAddr[2],
  455. db->macAddr[3],
  456. db->macAddr[4],
  457. db->macAddr[5],
  458. db->networkAddr[0],
  459. db->networkAddr[1],
  460. db->networkAddr[2],
  461. db->networkAddr[3],
  462. db->networkAddr[4],
  463. db->networkAddr[5],
  464. db->networkAddr[6],
  465. db->networkAddr[7],
  466. db->networkAddr[8],
  467. db->networkAddr[9],
  468. db->networkAddr[10]);
  469. #endif
  470. }
  471. _exit_critical_bh(&priv->br_ext_lock, &irqL);
  472. return 1;
  473. }
  474.  
  475. db = db->next_hash;
  476. }
  477.  
  478. _exit_critical_bh(&priv->br_ext_lock, &irqL);
  479. return 0;
  480. }
  481.  
  482.  
  483. static void __nat25_db_network_insert(_adapter *priv,
  484. unsigned char *macAddr, unsigned char *networkAddr)
  485. {
  486. struct nat25_network_db_entry *db;
  487. int hash;
  488. _irqL irqL;
  489. _enter_critical_bh(&priv->br_ext_lock, &irqL);
  490.  
  491. hash = __nat25_network_hash(networkAddr);
  492. db = priv->nethash[hash];
  493. while (db != NULL) {
  494. if (!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {
  495. memcpy(db->macAddr, macAddr, ETH_ALEN);
  496. db->ageing_timer = jiffies;
  497. _exit_critical_bh(&priv->br_ext_lock, &irqL);
  498. return;
  499. }
  500.  
  501. db = db->next_hash;
  502. }
  503.  
  504. db = (struct nat25_network_db_entry *) rtw_malloc(sizeof(*db));
  505. if (db == NULL) {
  506. _exit_critical_bh(&priv->br_ext_lock, &irqL);
  507. return;
  508. }
  509.  
  510. memcpy(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN);
  511. memcpy(db->macAddr, macAddr, ETH_ALEN);
  512. atomic_set(&db->use_count, 1);
  513. db->ageing_timer = jiffies;
  514.  
  515. __network_hash_link(priv, db, hash);
  516.  
  517. _exit_critical_bh(&priv->br_ext_lock, &irqL);
  518. }
  519.  
  520.  
  521. static void __nat25_db_print(_adapter *priv)
  522. {
  523. _irqL irqL;
  524. _enter_critical_bh(&priv->br_ext_lock, &irqL);
  525.  
  526. #ifdef BR_EXT_DEBUG
  527. static int counter = 0;
  528. int i, j;
  529. struct nat25_network_db_entry *db;
  530.  
  531. counter++;
  532. if ((counter % 16) != 0)
  533. return;
  534.  
  535. for (i = 0, j = 0; i < NAT25_HASH_SIZE; i++) {
  536. db = priv->nethash[i];
  537.  
  538. while (db != NULL) {
  539. #ifdef CL_IPV6_PASS
  540. panic_printk("NAT25: DB(%d) H(%02d) C(%d) M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
  541. "%02x%02x%02x%02x%02x%02x\n",
  542. j,
  543. i,
  544. atomic_read(&db->use_count),
  545. db->macAddr[0],
  546. db->macAddr[1],
  547. db->macAddr[2],
  548. db->macAddr[3],
  549. db->macAddr[4],
  550. db->macAddr[5],
  551. db->networkAddr[0],
  552. db->networkAddr[1],
  553. db->networkAddr[2],
  554. db->networkAddr[3],
  555. db->networkAddr[4],
  556. db->networkAddr[5],
  557. db->networkAddr[6],
  558. db->networkAddr[7],
  559. db->networkAddr[8],
  560. db->networkAddr[9],
  561. db->networkAddr[10],
  562. db->networkAddr[11],
  563. db->networkAddr[12],
  564. db->networkAddr[13],
  565. db->networkAddr[14],
  566. db->networkAddr[15],
  567. db->networkAddr[16]);
  568. #else
  569. panic_printk("NAT25: DB(%d) H(%02d) C(%d) M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  570. j,
  571. i,
  572. atomic_read(&db->use_count),
  573. db->macAddr[0],
  574. db->macAddr[1],
  575. db->macAddr[2],
  576. db->macAddr[3],
  577. db->macAddr[4],
  578. db->macAddr[5],
  579. db->networkAddr[0],
  580. db->networkAddr[1],
  581. db->networkAddr[2],
  582. db->networkAddr[3],
  583. db->networkAddr[4],
  584. db->networkAddr[5],
  585. db->networkAddr[6],
  586. db->networkAddr[7],
  587. db->networkAddr[8],
  588. db->networkAddr[9],
  589. db->networkAddr[10]);
  590. #endif
  591. j++;
  592.  
  593. db = db->next_hash;
  594. }
  595. }
  596. #endif
  597.  
  598. _exit_critical_bh(&priv->br_ext_lock, &irqL);
  599. }
  600.  
  601.  
  602.  
  603.  
  604. /*
  605. * NAT2.5 interface
  606. */
  607.  
  608. void nat25_db_cleanup(_adapter *priv)
  609. {
  610. int i;
  611. _irqL irqL;
  612. _enter_critical_bh(&priv->br_ext_lock, &irqL);
  613.  
  614. for (i = 0; i < NAT25_HASH_SIZE; i++) {
  615. struct nat25_network_db_entry *f;
  616. f = priv->nethash[i];
  617. while (f != NULL) {
  618. struct nat25_network_db_entry *g;
  619.  
  620. g = f->next_hash;
  621. if (priv->scdb_entry == f) {
  622. memset(priv->scdb_mac, 0, ETH_ALEN);
  623. memset(priv->scdb_ip, 0, 4);
  624. priv->scdb_entry = NULL;
  625. }
  626. __network_hash_unlink(f);
  627. rtw_mfree((u8 *) f, sizeof(struct nat25_network_db_entry));
  628.  
  629. f = g;
  630. }
  631. }
  632.  
  633. _exit_critical_bh(&priv->br_ext_lock, &irqL);
  634. }
  635.  
  636.  
  637. void nat25_db_expire(_adapter *priv)
  638. {
  639. int i;
  640. _irqL irqL;
  641. _enter_critical_bh(&priv->br_ext_lock, &irqL);
  642.  
  643. /* if(!priv->ethBrExtInfo.nat25_disable) */
  644. {
  645. for (i = 0; i < NAT25_HASH_SIZE; i++) {
  646. struct nat25_network_db_entry *f;
  647. f = priv->nethash[i];
  648.  
  649. while (f != NULL) {
  650. struct nat25_network_db_entry *g;
  651. g = f->next_hash;
  652.  
  653. if (__nat25_has_expired(priv, f)) {
  654. if (atomic_dec_and_test(&f->use_count)) {
  655. #ifdef BR_EXT_DEBUG
  656. #ifdef CL_IPV6_PASS
  657. panic_printk("NAT25 Expire H(%02d) M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
  658. "%02x%02x%02x%02x%02x%02x\n",
  659. i,
  660. f->macAddr[0],
  661. f->macAddr[1],
  662. f->macAddr[2],
  663. f->macAddr[3],
  664. f->macAddr[4],
  665. f->macAddr[5],
  666. f->networkAddr[0],
  667. f->networkAddr[1],
  668. f->networkAddr[2],
  669. f->networkAddr[3],
  670. f->networkAddr[4],
  671. f->networkAddr[5],
  672. f->networkAddr[6],
  673. f->networkAddr[7],
  674. f->networkAddr[8],
  675. f->networkAddr[9],
  676. f->networkAddr[10],
  677. f->networkAddr[11],
  678. f->networkAddr[12],
  679. f->networkAddr[13],
  680. f->networkAddr[14],
  681. f->networkAddr[15],
  682. f->networkAddr[16]);
  683. #else
  684.  
  685. panic_printk("NAT25 Expire H(%02d) M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  686. i,
  687. f->macAddr[0],
  688. f->macAddr[1],
  689. f->macAddr[2],
  690. f->macAddr[3],
  691. f->macAddr[4],
  692. f->macAddr[5],
  693. f->networkAddr[0],
  694. f->networkAddr[1],
  695. f->networkAddr[2],
  696. f->networkAddr[3],
  697. f->networkAddr[4],
  698. f->networkAddr[5],
  699. f->networkAddr[6],
  700. f->networkAddr[7],
  701. f->networkAddr[8],
  702. f->networkAddr[9],
  703. f->networkAddr[10]);
  704. #endif
  705. #endif
  706. if (priv->scdb_entry == f) {
  707. memset(priv->scdb_mac, 0, ETH_ALEN);
  708. memset(priv->scdb_ip, 0, 4);
  709. priv->scdb_entry = NULL;
  710. }
  711. __network_hash_unlink(f);
  712. rtw_mfree((u8 *) f, sizeof(struct nat25_network_db_entry));
  713. }
  714. }
  715.  
  716. f = g;
  717. }
  718. }
  719. }
  720.  
  721. _exit_critical_bh(&priv->br_ext_lock, &irqL);
  722. }
  723.  
  724.  
  725. #ifdef SUPPORT_TX_MCAST2UNI
  726. static int checkIPMcAndReplace(_adapter *priv, struct sk_buff *skb, unsigned int *dst_ip)
  727. {
  728. struct stat_info *pstat;
  729. struct list_head *phead, *plist;
  730. int i;
  731.  
  732. phead = &priv->asoc_list;
  733. plist = phead->next;
  734.  
  735. while (plist != phead) {
  736. pstat = list_entry(plist, struct stat_info, asoc_list);
  737. plist = plist->next;
  738.  
  739. if (pstat->ipmc_num == 0)
  740. continue;
  741.  
  742. for (i = 0; i < MAX_IP_MC_ENTRY; i++) {
  743. if (pstat->ipmc[i].used && !memcmp(&pstat->ipmc[i].mcmac[3], ((unsigned char *)dst_ip) + 1, 3)) {
  744. memcpy(skb->data, pstat->ipmc[i].mcmac, ETH_ALEN);
  745. return 1;
  746. }
  747. }
  748. }
  749. return 0;
  750. }
  751. #endif
  752.  
  753. int nat25_db_handle(_adapter *priv, struct sk_buff *skb, int method)
  754. {
  755. unsigned short protocol;
  756. unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];
  757.  
  758. if (skb == NULL)
  759. return -1;
  760.  
  761. if ((method <= NAT25_MIN) || (method >= NAT25_MAX))
  762. return -1;
  763.  
  764. protocol = *((unsigned short *)(skb->data + 2 * ETH_ALEN));
  765.  
  766. /*---------------------------------------------------*/
  767. /* Handle IP frame */
  768. /*---------------------------------------------------*/
  769. if (protocol == __constant_htons(ETH_P_IP)) {
  770. struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);
  771.  
  772. if (((unsigned char *)(iph) + (iph->ihl << 2)) >= (skb->data + ETH_HLEN + skb->len)) {
  773. DEBUG_WARN("NAT25: malformed IP packet !\n");
  774. return -1;
  775. }
  776.  
  777. switch (method) {
  778. case NAT25_CHECK:
  779. return -1;
  780.  
  781. case NAT25_INSERT: {
  782. /* some muticast with source IP is all zero, maybe other case is illegal */
  783. /* in class A, B, C, host address is all zero or all one is illegal */
  784. if (iph->saddr == 0)
  785. return 0;
  786. RTW_INFO("NAT25: Insert IP, SA=%08x, DA=%08x\n", iph->saddr, iph->daddr);
  787. __nat25_generate_ipv4_network_addr(networkAddr, &iph->saddr);
  788. /* record source IP address and , source mac address into db */
  789. __nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
  790.  
  791. __nat25_db_print(priv);
  792. }
  793. return 0;
  794.  
  795. case NAT25_LOOKUP: {
  796. RTW_INFO("NAT25: Lookup IP, SA=%08x, DA=%08x\n", iph->saddr, iph->daddr);
  797. #ifdef SUPPORT_TX_MCAST2UNI
  798. if (priv->pshare->rf_ft_var.mc2u_disable ||
  799. ((((OPMODE & (WIFI_STATION_STATE | WIFI_ASOC_STATE))
  800. == (WIFI_STATION_STATE | WIFI_ASOC_STATE)) &&
  801. !checkIPMcAndReplace(priv, skb, &iph->daddr)) ||
  802. (OPMODE & WIFI_ADHOC_STATE)))
  803. #endif
  804. {
  805. __nat25_generate_ipv4_network_addr(networkAddr, &iph->daddr);
  806.  
  807. if (!__nat25_db_network_lookup_and_replace(priv, skb, networkAddr)) {
  808. if (*((unsigned char *)&iph->daddr + 3) == 0xff) {
  809. /* L2 is unicast but L3 is broadcast, make L2 bacome broadcast */
  810. RTW_INFO("NAT25: Set DA as boardcast\n");
  811. memset(skb->data, 0xff, ETH_ALEN);
  812. } else {
  813. /* forward unknow IP packet to upper TCP/IP */
  814. RTW_INFO("NAT25: Replace DA with BR's MAC\n");
  815. if ((*(u32 *)priv->br_mac) == 0 && (*(u16 *)(priv->br_mac + 4)) == 0) {
  816. void netdev_br_init(struct net_device *netdev);
  817. printk("Re-init netdev_br_init() due to br_mac==0!\n");
  818. netdev_br_init(priv->pnetdev);
  819. }
  820. memcpy(skb->data, priv->br_mac, ETH_ALEN);
  821. }
  822. }
  823. }
  824. }
  825. return 0;
  826.  
  827. default:
  828. return -1;
  829. }
  830. }
  831.  
  832. /*---------------------------------------------------*/
  833. /* Handle ARP frame */
  834. /*---------------------------------------------------*/
  835. else if (protocol == __constant_htons(ETH_P_ARP)) {
  836. struct arphdr *arp = (struct arphdr *)(skb->data + ETH_HLEN);
  837. unsigned char *arp_ptr = (unsigned char *)(arp + 1);
  838. unsigned int *sender, *target;
  839.  
  840. if (arp->ar_pro != __constant_htons(ETH_P_IP)) {
  841. DEBUG_WARN("NAT25: arp protocol unknown (%4x)!\n", htons(arp->ar_pro));
  842. return -1;
  843. }
  844.  
  845. switch (method) {
  846. case NAT25_CHECK:
  847. return 0; /* skb_copy for all ARP frame */
  848.  
  849. case NAT25_INSERT: {
  850. RTW_INFO("NAT25: Insert ARP, MAC=%02x%02x%02x%02x%02x%02x\n", arp_ptr[0],
  851. arp_ptr[1], arp_ptr[2], arp_ptr[3], arp_ptr[4], arp_ptr[5]);
  852.  
  853. /* change to ARP sender mac address to wlan STA address */
  854. memcpy(arp_ptr, GET_MY_HWADDR(priv), ETH_ALEN);
  855.  
  856. arp_ptr += arp->ar_hln;
  857. sender = (unsigned int *)arp_ptr;
  858.  
  859. __nat25_generate_ipv4_network_addr(networkAddr, sender);
  860.  
  861. __nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
  862.  
  863. __nat25_db_print(priv);
  864. }
  865. return 0;
  866.  
  867. case NAT25_LOOKUP: {
  868. RTW_INFO("NAT25: Lookup ARP\n");
  869.  
  870. arp_ptr += arp->ar_hln;
  871. sender = (unsigned int *)arp_ptr;
  872. arp_ptr += (arp->ar_hln + arp->ar_pln);
  873. target = (unsigned int *)arp_ptr;
  874.  
  875. __nat25_generate_ipv4_network_addr(networkAddr, target);
  876.  
  877. __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
  878.  
  879. /* change to ARP target mac address to Lookup result */
  880. arp_ptr = (unsigned char *)(arp + 1);
  881. arp_ptr += (arp->ar_hln + arp->ar_pln);
  882. memcpy(arp_ptr, skb->data, ETH_ALEN);
  883. }
  884. return 0;
  885.  
  886. default:
  887. return -1;
  888. }
  889. }
  890.  
  891. /*---------------------------------------------------*/
  892. /* Handle IPX and Apple Talk frame */
  893. /*---------------------------------------------------*/
  894. else if ((protocol == __constant_htons(ETH_P_IPX)) ||
  895. (protocol == __constant_htons(ETH_P_ATALK)) ||
  896. (protocol == __constant_htons(ETH_P_AARP))) {
  897. unsigned char ipx_header[2] = {0xFF, 0xFF};
  898. struct ipxhdr *ipx = NULL;
  899. struct elapaarp *ea = NULL;
  900. struct ddpehdr *ddp = NULL;
  901. unsigned char *framePtr = skb->data + ETH_HLEN;
  902.  
  903. if (protocol == __constant_htons(ETH_P_IPX)) {
  904. RTW_INFO("NAT25: Protocol=IPX (Ethernet II)\n");
  905. ipx = (struct ipxhdr *)framePtr;
  906. } else { /* if(protocol <= __constant_htons(ETH_FRAME_LEN)) */
  907. if (!memcmp(ipx_header, framePtr, 2)) {
  908. RTW_INFO("NAT25: Protocol=IPX (Ethernet 802.3)\n");
  909. ipx = (struct ipxhdr *)framePtr;
  910. } else {
  911. unsigned char ipx_8022_type = 0xE0;
  912. unsigned char snap_8022_type = 0xAA;
  913.  
  914. if (*framePtr == snap_8022_type) {
  915. unsigned char ipx_snap_id[5] = {0x0, 0x0, 0x0, 0x81, 0x37}; /* IPX SNAP ID */
  916. unsigned char aarp_snap_id[5] = {0x00, 0x00, 0x00, 0x80, 0xF3}; /* Apple Talk AARP SNAP ID */
  917. unsigned char ddp_snap_id[5] = {0x08, 0x00, 0x07, 0x80, 0x9B}; /* Apple Talk DDP SNAP ID */
  918.  
  919. framePtr += 3; /* eliminate the 802.2 header */
  920.  
  921. if (!memcmp(ipx_snap_id, framePtr, 5)) {
  922. framePtr += 5; /* eliminate the SNAP header */
  923.  
  924. RTW_INFO("NAT25: Protocol=IPX (Ethernet SNAP)\n");
  925. ipx = (struct ipxhdr *)framePtr;
  926. } else if (!memcmp(aarp_snap_id, framePtr, 5)) {
  927. framePtr += 5; /* eliminate the SNAP header */
  928.  
  929. ea = (struct elapaarp *)framePtr;
  930. } else if (!memcmp(ddp_snap_id, framePtr, 5)) {
  931. framePtr += 5; /* eliminate the SNAP header */
  932.  
  933. ddp = (struct ddpehdr *)framePtr;
  934. } else {
  935. DEBUG_WARN("NAT25: Protocol=Ethernet SNAP %02x%02x%02x%02x%02x\n", framePtr[0],
  936. framePtr[1], framePtr[2], framePtr[3], framePtr[4]);
  937. return -1;
  938. }
  939. } else if (*framePtr == ipx_8022_type) {
  940. framePtr += 3; /* eliminate the 802.2 header */
  941.  
  942. if (!memcmp(ipx_header, framePtr, 2)) {
  943. RTW_INFO("NAT25: Protocol=IPX (Ethernet 802.2)\n");
  944. ipx = (struct ipxhdr *)framePtr;
  945. } else
  946. return -1;
  947. }
  948. }
  949. }
  950.  
  951. /* no more IPX since 5.15 */
  952. /* AARP */
  953. if (ea != NULL) {
  954. /* Sanity check fields. */
  955. if (ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN) {
  956. DEBUG_WARN("NAT25: Appletalk AARP Sanity check fail!\n");
  957. return -1;
  958. }
  959.  
  960. switch (method) {
  961. case NAT25_CHECK:
  962. return 0;
  963.  
  964. case NAT25_INSERT: {
  965. /* change to AARP source mac address to wlan STA address */
  966. memcpy(ea->hw_src, GET_MY_HWADDR(priv), ETH_ALEN);
  967.  
  968. RTW_INFO("NAT25: Insert AARP, Source=%d,%d Destination=%d,%d\n",
  969. ea->pa_src_net,
  970. ea->pa_src_node,
  971. ea->pa_dst_net,
  972. ea->pa_dst_node);
  973.  
  974. __nat25_generate_apple_network_addr(networkAddr, &ea->pa_src_net, &ea->pa_src_node);
  975.  
  976. __nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
  977.  
  978. __nat25_db_print(priv);
  979. }
  980. return 0;
  981.  
  982. case NAT25_LOOKUP: {
  983. RTW_INFO("NAT25: Lookup AARP, Source=%d,%d Destination=%d,%d\n",
  984. ea->pa_src_net,
  985. ea->pa_src_node,
  986. ea->pa_dst_net,
  987. ea->pa_dst_node);
  988.  
  989. __nat25_generate_apple_network_addr(networkAddr, &ea->pa_dst_net, &ea->pa_dst_node);
  990.  
  991. __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
  992.  
  993. /* change to AARP destination mac address to Lookup result */
  994. memcpy(ea->hw_dst, skb->data, ETH_ALEN);
  995. }
  996. return 0;
  997.  
  998. default:
  999. return -1;
  1000. }
  1001. }
  1002.  
  1003. /* DDP */
  1004. else if (ddp != NULL) {
  1005. switch (method) {
  1006. case NAT25_CHECK:
  1007. return -1;
  1008.  
  1009. case NAT25_INSERT: {
  1010. RTW_INFO("NAT25: Insert DDP, Source=%d,%d Destination=%d,%d\n",
  1011. ddp->deh_snet,
  1012. ddp->deh_snode,
  1013. ddp->deh_dnet,
  1014. ddp->deh_dnode);
  1015.  
  1016. __nat25_generate_apple_network_addr(networkAddr, &ddp->deh_snet, &ddp->deh_snode);
  1017.  
  1018. __nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
  1019.  
  1020. __nat25_db_print(priv);
  1021. }
  1022. return 0;
  1023.  
  1024. case NAT25_LOOKUP: {
  1025. RTW_INFO("NAT25: Lookup DDP, Source=%d,%d Destination=%d,%d\n",
  1026. ddp->deh_snet,
  1027. ddp->deh_snode,
  1028. ddp->deh_dnet,
  1029. ddp->deh_dnode);
  1030.  
  1031. __nat25_generate_apple_network_addr(networkAddr, &ddp->deh_dnet, &ddp->deh_dnode);
  1032.  
  1033. __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
  1034. }
  1035. return 0;
  1036.  
  1037. default:
  1038. return -1;
  1039. }
  1040. }
  1041.  
  1042. return -1;
  1043. }
  1044.  
  1045. /*---------------------------------------------------*/
  1046. /* Handle PPPoE frame */
  1047. /*---------------------------------------------------*/
  1048. else if ((protocol == __constant_htons(ETH_P_PPP_DISC)) ||
  1049. (protocol == __constant_htons(ETH_P_PPP_SES))) {
  1050. struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
  1051. unsigned short *pMagic;
  1052.  
  1053. switch (method) {
  1054. case NAT25_CHECK:
  1055. if (ph->sid == 0)
  1056. return 0;
  1057. return 1;
  1058.  
  1059. case NAT25_INSERT:
  1060. if (ph->sid == 0) { /* Discovery phase according to tag */
  1061. if (ph->code == PADI_CODE || ph->code == PADR_CODE) {
  1062. if (priv->ethBrExtInfo.addPPPoETag) {
  1063. struct pppoe_tag *tag, *pOldTag;
  1064. unsigned char tag_buf[40];
  1065. int old_tag_len = 0;
  1066.  
  1067. tag = (struct pppoe_tag *)tag_buf;
  1068. pOldTag = (struct pppoe_tag *)__nat25_find_pppoe_tag(ph, ntohs(PTT_RELAY_SID));
  1069. if (pOldTag) { /* if SID existed, copy old value and delete it */
  1070. old_tag_len = ntohs(pOldTag->tag_len);
  1071. if (old_tag_len + TAG_HDR_LEN + MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN > sizeof(tag_buf)) {
  1072. DEBUG_ERR("SID tag length too long!\n");
  1073. return -1;
  1074. }
  1075.  
  1076. memcpy(tag->tag_data + MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN,
  1077. pOldTag->tag_data, old_tag_len);
  1078.  
  1079. if (skb_pull_and_merge(skb, (unsigned char *)pOldTag, TAG_HDR_LEN + old_tag_len) < 0) {
  1080. DEBUG_ERR("call skb_pull_and_merge() failed in PADI/R packet!\n");
  1081. return -1;
  1082. }
  1083. ph->length = htons(ntohs(ph->length) - TAG_HDR_LEN - old_tag_len);
  1084. }
  1085.  
  1086. tag->tag_type = PTT_RELAY_SID;
  1087. tag->tag_len = htons(MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN + old_tag_len);
  1088.  
  1089. /* insert the magic_code+client mac in relay tag */
  1090. pMagic = (unsigned short *)tag->tag_data;
  1091. *pMagic = htons(MAGIC_CODE);
  1092. memcpy(tag->tag_data + MAGIC_CODE_LEN, skb->data + ETH_ALEN, ETH_ALEN);
  1093.  
  1094. /* Add relay tag */
  1095. if (__nat25_add_pppoe_tag(skb, tag) < 0)
  1096. return -1;
  1097.  
  1098. RTW_INFO("NAT25: Insert PPPoE, forward %s packet\n",
  1099. (ph->code == PADI_CODE ? "PADI" : "PADR"));
  1100. } else { /* not add relay tag */
  1101. if (priv->pppoe_connection_in_progress &&
  1102. memcmp(skb->data + ETH_ALEN, priv->pppoe_addr, ETH_ALEN)) {
  1103. DEBUG_ERR("Discard PPPoE packet due to another PPPoE connection is in progress!\n");
  1104. return -2;
  1105. }
  1106.  
  1107. if (priv->pppoe_connection_in_progress == 0)
  1108. memcpy(priv->pppoe_addr, skb->data + ETH_ALEN, ETH_ALEN);
  1109.  
  1110. priv->pppoe_connection_in_progress = WAIT_TIME_PPPOE;
  1111. }
  1112. } else
  1113. return -1;
  1114. } else { /* session phase */
  1115. RTW_INFO("NAT25: Insert PPPoE, insert session packet to %s\n", skb->dev->name);
  1116.  
  1117. __nat25_generate_pppoe_network_addr(networkAddr, skb->data, &(ph->sid));
  1118.  
  1119. __nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
  1120.  
  1121. __nat25_db_print(priv);
  1122.  
  1123. if (!priv->ethBrExtInfo.addPPPoETag &&
  1124. priv->pppoe_connection_in_progress &&
  1125. !memcmp(skb->data + ETH_ALEN, priv->pppoe_addr, ETH_ALEN))
  1126. priv->pppoe_connection_in_progress = 0;
  1127. }
  1128. return 0;
  1129.  
  1130. case NAT25_LOOKUP:
  1131. if (ph->code == PADO_CODE || ph->code == PADS_CODE) {
  1132. if (priv->ethBrExtInfo.addPPPoETag) {
  1133. struct pppoe_tag *tag;
  1134. unsigned char *ptr;
  1135. unsigned short tagType, tagLen;
  1136. int offset = 0;
  1137.  
  1138. ptr = __nat25_find_pppoe_tag(ph, ntohs(PTT_RELAY_SID));
  1139. if (ptr == 0) {
  1140. DEBUG_ERR("Fail to find PTT_RELAY_SID in FADO!\n");
  1141. return -1;
  1142. }
  1143.  
  1144. tag = (struct pppoe_tag *)ptr;
  1145. tagType = (unsigned short)((ptr[0] << 8) + ptr[1]);
  1146. tagLen = (unsigned short)((ptr[2] << 8) + ptr[3]);
  1147.  
  1148. if ((tagType != ntohs(PTT_RELAY_SID)) || (tagLen < (MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN))) {
  1149. DEBUG_ERR("Invalid PTT_RELAY_SID tag length [%d]!\n", tagLen);
  1150. return -1;
  1151. }
  1152.  
  1153. pMagic = (unsigned short *)tag->tag_data;
  1154. if (ntohs(*pMagic) != MAGIC_CODE) {
  1155. DEBUG_ERR("Can't find MAGIC_CODE in %s packet!\n",
  1156. (ph->code == PADO_CODE ? "PADO" : "PADS"));
  1157. return -1;
  1158. }
  1159.  
  1160. memcpy(skb->data, tag->tag_data + MAGIC_CODE_LEN, ETH_ALEN);
  1161.  
  1162. if (tagLen > MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN)
  1163. offset = TAG_HDR_LEN;
  1164.  
  1165. if (skb_pull_and_merge(skb, ptr + offset, TAG_HDR_LEN + MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN - offset) < 0) {
  1166. DEBUG_ERR("call skb_pull_and_merge() failed in PADO packet!\n");
  1167. return -1;
  1168. }
  1169. ph->length = htons(ntohs(ph->length) - (TAG_HDR_LEN + MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN - offset));
  1170. if (offset > 0)
  1171. tag->tag_len = htons(tagLen - MAGIC_CODE_LEN - RTL_RELAY_TAG_LEN);
  1172.  
  1173. RTW_INFO("NAT25: Lookup PPPoE, forward %s Packet from %s\n",
  1174. (ph->code == PADO_CODE ? "PADO" : "PADS"), skb->dev->name);
  1175. } else { /* not add relay tag */
  1176. if (!priv->pppoe_connection_in_progress) {
  1177. DEBUG_ERR("Discard PPPoE packet due to no connection in progresss!\n");
  1178. return -1;
  1179. }
  1180. memcpy(skb->data, priv->pppoe_addr, ETH_ALEN);
  1181. priv->pppoe_connection_in_progress = WAIT_TIME_PPPOE;
  1182. }
  1183. } else {
  1184. if (ph->sid != 0) {
  1185. RTW_INFO("NAT25: Lookup PPPoE, lookup session packet from %s\n", skb->dev->name);
  1186. __nat25_generate_pppoe_network_addr(networkAddr, skb->data + ETH_ALEN, &(ph->sid));
  1187.  
  1188. __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
  1189.  
  1190. __nat25_db_print(priv);
  1191. } else
  1192. return -1;
  1193.  
  1194. }
  1195. return 0;
  1196.  
  1197. default:
  1198. return -1;
  1199. }
  1200. }
  1201.  
  1202. /*---------------------------------------------------*/
  1203. /* Handle EAP frame */
  1204. /*---------------------------------------------------*/
  1205. else if (protocol == __constant_htons(0x888e)) {
  1206. switch (method) {
  1207. case NAT25_CHECK:
  1208. return -1;
  1209.  
  1210. case NAT25_INSERT:
  1211. return 0;
  1212.  
  1213. case NAT25_LOOKUP:
  1214. return 0;
  1215.  
  1216. default:
  1217. return -1;
  1218. }
  1219. }
  1220.  
  1221. /*---------------------------------------------------*/
  1222. /* Handle C-Media proprietary frame */
  1223. /*---------------------------------------------------*/
  1224. else if ((protocol == __constant_htons(0xe2ae)) ||
  1225. (protocol == __constant_htons(0xe2af))) {
  1226. switch (method) {
  1227. case NAT25_CHECK:
  1228. return -1;
  1229.  
  1230. case NAT25_INSERT:
  1231. return 0;
  1232.  
  1233. case NAT25_LOOKUP:
  1234. return 0;
  1235.  
  1236. default:
  1237. return -1;
  1238. }
  1239. }
  1240.  
  1241. /*---------------------------------------------------*/
  1242. /* Handle IPV6 frame */
  1243. /*---------------------------------------------------*/
  1244. #ifdef CL_IPV6_PASS
  1245. else if (protocol == __constant_htons(ETH_P_IPV6)) {
  1246. struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN);
  1247.  
  1248. if (sizeof(*iph) >= (skb->len - ETH_HLEN)) {
  1249. DEBUG_WARN("NAT25: malformed IPv6 packet !\n");
  1250. return -1;
  1251. }
  1252.  
  1253. switch (method) {
  1254. case NAT25_CHECK:
  1255. if (skb->data[0] & 1)
  1256. return 0;
  1257. return -1;
  1258.  
  1259. case NAT25_INSERT: {
  1260. RTW_INFO("NAT25: Insert IP, SA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x,"
  1261. " DA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x\n",
  1262. iph->saddr.s6_addr16[0], iph->saddr.s6_addr16[1], iph->saddr.s6_addr16[2], iph->saddr.s6_addr16[3],
  1263. iph->saddr.s6_addr16[4], iph->saddr.s6_addr16[5], iph->saddr.s6_addr16[6], iph->saddr.s6_addr16[7],
  1264. iph->daddr.s6_addr16[0], iph->daddr.s6_addr16[1], iph->daddr.s6_addr16[2], iph->daddr.s6_addr16[3],
  1265. iph->daddr.s6_addr16[4], iph->daddr.s6_addr16[5], iph->daddr.s6_addr16[6], iph->daddr.s6_addr16[7]);
  1266.  
  1267. if (memcmp(&iph->saddr, "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", 16)) {
  1268. __nat25_generate_ipv6_network_addr(networkAddr, (unsigned int *)&iph->saddr);
  1269. __nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
  1270. __nat25_db_print(priv);
  1271.  
  1272. if (iph->nexthdr == IPPROTO_ICMPV6 &&
  1273. skb->len > (ETH_HLEN + sizeof(*iph) + 4)) {
  1274. if (update_nd_link_layer_addr(skb->data + ETH_HLEN + sizeof(*iph),
  1275. skb->len - ETH_HLEN - sizeof(*iph), GET_MY_HWADDR(priv))) {
  1276. struct icmp6hdr *hdr = (struct icmp6hdr *)(skb->data + ETH_HLEN + sizeof(*iph));
  1277. hdr->icmp6_cksum = 0;
  1278. hdr->icmp6_cksum = csum_ipv6_magic(&iph->saddr, &iph->daddr,
  1279. iph->payload_len,
  1280. IPPROTO_ICMPV6,
  1281. csum_partial((__u8 *)hdr, iph->payload_len, 0));
  1282. }
  1283. }
  1284. }
  1285. }
  1286. return 0;
  1287.  
  1288. case NAT25_LOOKUP:
  1289. RTW_INFO("NAT25: Lookup IP, SA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x,"
  1290. " DA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x\n",
  1291. iph->saddr.s6_addr16[0], iph->saddr.s6_addr16[1], iph->saddr.s6_addr16[2], iph->saddr.s6_addr16[3],
  1292. iph->saddr.s6_addr16[4], iph->saddr.s6_addr16[5], iph->saddr.s6_addr16[6], iph->saddr.s6_addr16[7],
  1293. iph->daddr.s6_addr16[0], iph->daddr.s6_addr16[1], iph->daddr.s6_addr16[2], iph->daddr.s6_addr16[3],
  1294. iph->daddr.s6_addr16[4], iph->daddr.s6_addr16[5], iph->daddr.s6_addr16[6], iph->daddr.s6_addr16[7]);
  1295.  
  1296.  
  1297. __nat25_generate_ipv6_network_addr(networkAddr, (unsigned int *)&iph->daddr);
  1298. if (!__nat25_db_network_lookup_and_replace(priv, skb, networkAddr)) {
  1299. #ifdef SUPPORT_RX_UNI2MCAST
  1300. if (iph->daddr.s6_addr[0] == 0xff)
  1301. convert_ipv6_mac_to_mc(skb);
  1302. #endif
  1303. }
  1304. return 0;
  1305.  
  1306. default:
  1307. return -1;
  1308. }
  1309. }
  1310. #endif /* CL_IPV6_PASS */
  1311.  
  1312. return -1;
  1313. }
  1314.  
  1315.  
  1316. int nat25_handle_frame(_adapter *priv, struct sk_buff *skb)
  1317. {
  1318. #ifdef BR_EXT_DEBUG
  1319. if ((!priv->ethBrExtInfo.nat25_disable) && (!(skb->data[0] & 1))) {
  1320. panic_printk("NAT25: Input Frame: DA=%02x%02x%02x%02x%02x%02x SA=%02x%02x%02x%02x%02x%02x\n",
  1321. skb->data[0],
  1322. skb->data[1],
  1323. skb->data[2],
  1324. skb->data[3],
  1325. skb->data[4],
  1326. skb->data[5],
  1327. skb->data[6],
  1328. skb->data[7],
  1329. skb->data[8],
  1330. skb->data[9],
  1331. skb->data[10],
  1332. skb->data[11]);
  1333. }
  1334. #endif
  1335.  
  1336. if (!(skb->data[0] & 1)) {
  1337. int is_vlan_tag = 0, i, retval = 0;
  1338. unsigned short vlan_hdr = 0;
  1339.  
  1340. if (*((unsigned short *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_8021Q)) {
  1341. is_vlan_tag = 1;
  1342. vlan_hdr = *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2));
  1343. for (i = 0; i < 6; i++)
  1344. *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2 - i * 2)) = *((unsigned short *)(skb->data + ETH_ALEN * 2 - 2 - i * 2));
  1345. skb_pull(skb, 4);
  1346. }
  1347.  
  1348. if (!priv->ethBrExtInfo.nat25_disable) {
  1349. _irqL irqL;
  1350. _enter_critical_bh(&priv->br_ext_lock, &irqL);
  1351. /*
  1352. * This function look up the destination network address from
  1353. * the NAT2.5 database. Return value = -1 means that the
  1354. * corresponding network protocol is NOT support.
  1355. */
  1356. if (!priv->ethBrExtInfo.nat25sc_disable &&
  1357. (*((unsigned short *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP)) &&
  1358. !memcmp(priv->scdb_ip, skb->data + ETH_HLEN + 16, 4)) {
  1359. memcpy(skb->data, priv->scdb_mac, ETH_ALEN);
  1360.  
  1361. _exit_critical_bh(&priv->br_ext_lock, &irqL);
  1362. } else {
  1363. _exit_critical_bh(&priv->br_ext_lock, &irqL);
  1364.  
  1365. retval = nat25_db_handle(priv, skb, NAT25_LOOKUP);
  1366. }
  1367. } else {
  1368. if (((*((unsigned short *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP)) &&
  1369. !memcmp(priv->br_ip, skb->data + ETH_HLEN + 16, 4)) ||
  1370. ((*((unsigned short *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_ARP)) &&
  1371. !memcmp(priv->br_ip, skb->data + ETH_HLEN + 24, 4))) {
  1372. /* for traffic to upper TCP/IP */
  1373. retval = nat25_db_handle(priv, skb, NAT25_LOOKUP);
  1374. }
  1375. }
  1376.  
  1377. if (is_vlan_tag) {
  1378. skb_push(skb, 4);
  1379. for (i = 0; i < 6; i++)
  1380. *((unsigned short *)(skb->data + i * 2)) = *((unsigned short *)(skb->data + 4 + i * 2));
  1381. *((unsigned short *)(skb->data + ETH_ALEN * 2)) = __constant_htons(ETH_P_8021Q);
  1382. *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2)) = vlan_hdr;
  1383. }
  1384.  
  1385. if (retval == -1) {
  1386. /* DEBUG_ERR("NAT25: Lookup fail!\n"); */
  1387. return -1;
  1388. }
  1389. }
  1390.  
  1391. return 0;
  1392. }
  1393.  
  1394. #if 0
  1395. void mac_clone(_adapter *priv, unsigned char *addr)
  1396. {
  1397. struct sockaddr sa;
  1398.  
  1399. memcpy(sa.sa_data, addr, ETH_ALEN);
  1400. RTW_INFO("MAC Clone: Addr=%02x%02x%02x%02x%02x%02x\n",
  1401. addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
  1402. rtl8192cd_set_hwaddr(priv->dev, &sa);
  1403. }
  1404.  
  1405.  
  1406. int mac_clone_handle_frame(_adapter *priv, struct sk_buff *skb)
  1407. {
  1408. if (priv->ethBrExtInfo.macclone_enable && !priv->macclone_completed) {
  1409. if (!(skb->data[ETH_ALEN] & 1)) { /* check any other particular MAC add */
  1410. if (memcmp(skb->data + ETH_ALEN, GET_MY_HWADDR(priv), ETH_ALEN) &&
  1411. ((priv->dev->br_port) &&
  1412. memcmp(skb->data + ETH_ALEN, priv->br_mac, ETH_ALEN))) {
  1413. mac_clone(priv, skb->data + ETH_ALEN);
  1414. priv->macclone_completed = 1;
  1415. }
  1416. }
  1417. }
  1418.  
  1419. return 0;
  1420. }
  1421. #endif /* 0 */
  1422.  
  1423. #define SERVER_PORT 67
  1424. #define CLIENT_PORT 68
  1425. #define DHCP_MAGIC 0x63825363
  1426. #define BROADCAST_FLAG 0x8000
  1427.  
  1428. struct dhcpMessage {
  1429. u_int8_t op;
  1430. u_int8_t htype;
  1431. u_int8_t hlen;
  1432. u_int8_t hops;
  1433. u_int32_t xid;
  1434. u_int16_t secs;
  1435. u_int16_t flags;
  1436. u_int32_t ciaddr;
  1437. u_int32_t yiaddr;
  1438. u_int32_t siaddr;
  1439. u_int32_t giaddr;
  1440. u_int8_t chaddr[16];
  1441. u_int8_t sname[64];
  1442. u_int8_t file[128];
  1443. u_int32_t cookie;
  1444. u_int8_t options[308]; /* 312 - cookie */
  1445. };
  1446.  
  1447. void dhcp_flag_bcast(_adapter *priv, struct sk_buff *skb)
  1448. {
  1449. if (skb == NULL)
  1450. return;
  1451.  
  1452. if (!priv->ethBrExtInfo.dhcp_bcst_disable) {
  1453. unsigned short protocol = *((unsigned short *)(skb->data + 2 * ETH_ALEN));
  1454.  
  1455. if (protocol == __constant_htons(ETH_P_IP)) { /* IP */
  1456. struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);
  1457.  
  1458. if (iph->protocol == IPPROTO_UDP) { /* UDP */
  1459. struct udphdr *udph = (struct udphdr *)((SIZE_PTR)iph + (iph->ihl << 2));
  1460.  
  1461. if ((udph->source == __constant_htons(CLIENT_PORT))
  1462. && (udph->dest == __constant_htons(SERVER_PORT))) { /* DHCP request */
  1463. struct dhcpMessage *dhcph =
  1464. (struct dhcpMessage *)((SIZE_PTR)udph + sizeof(struct udphdr));
  1465.  
  1466. if (dhcph->cookie == __constant_htonl(DHCP_MAGIC)) { /* match magic word */
  1467. if (!(dhcph->flags & htons(BROADCAST_FLAG))) { /* if not broadcast */
  1468. register int sum = 0;
  1469.  
  1470. RTW_INFO("DHCP: change flag of DHCP request to broadcast.\n");
  1471. /* or BROADCAST flag */
  1472. dhcph->flags |= htons(BROADCAST_FLAG);
  1473. /* recalculate checksum */
  1474. sum = ~(udph->check) & 0xffff;
  1475. sum += dhcph->flags;
  1476. while (sum >> 16)
  1477. sum = (sum & 0xffff) + (sum >> 16);
  1478. udph->check = ~sum;
  1479. }
  1480. }
  1481. }
  1482. }
  1483. }
  1484. }
  1485. }
  1486.  
  1487.  
  1488. void *scdb_findEntry(_adapter *priv, unsigned char *macAddr,
  1489. unsigned char *ipAddr)
  1490. {
  1491. unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];
  1492. struct nat25_network_db_entry *db;
  1493. int hash;
  1494. /* _irqL irqL; */
  1495. /* _enter_critical_bh(&priv->br_ext_lock, &irqL); */
  1496.  
  1497. __nat25_generate_ipv4_network_addr(networkAddr, (unsigned int *)ipAddr);
  1498. hash = __nat25_network_hash(networkAddr);
  1499. db = priv->nethash[hash];
  1500. while (db != NULL) {
  1501. if (!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {
  1502. /* _exit_critical_bh(&priv->br_ext_lock, &irqL); */
  1503. return (void *)db;
  1504. }
  1505.  
  1506. db = db->next_hash;
  1507. }
  1508.  
  1509. /* _exit_critical_bh(&priv->br_ext_lock, &irqL); */
  1510. return NULL;
  1511. }
  1512.  
  1513. #endif /* CONFIG_BR_EXT */
  1514.  
Add Comment
Please, Sign In to add comment