rtw_br_ext.c 45 KB

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