rtw_br_ext.c 45 KB

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