rtw_br_ext.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2017 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. *****************************************************************************/
  15. #define _RTW_BR_EXT_C_
  16. #ifdef __KERNEL__
  17. #include <linux/if_arp.h>
  18. #include <net/ip.h>
  19. #include <linux/version.h>
  20. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0))
  21. #include <net/ipx.h>
  22. #endif
  23. #include <linux/atalk.h>
  24. #include <linux/udp.h>
  25. #include <linux/if_pppox.h>
  26. #endif
  27. #if 1 /* rtw_wifi_driver */
  28. #include <drv_types.h>
  29. #else /* rtw_wifi_driver */
  30. #include "./8192cd_cfg.h"
  31. #ifndef __KERNEL__
  32. #include "./sys-support.h"
  33. #endif
  34. #include "./8192cd.h"
  35. #include "./8192cd_headers.h"
  36. #include "./8192cd_br_ext.h"
  37. #include "./8192cd_debug.h"
  38. #endif /* rtw_wifi_driver */
  39. #ifdef CL_IPV6_PASS
  40. #ifdef __KERNEL__
  41. #include <linux/ipv6.h>
  42. #include <linux/icmpv6.h>
  43. #include <net/ndisc.h>
  44. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24))
  45. #include <net/ip6_checksum.h>
  46. #else
  47. #include <net/checksum.h>
  48. #endif
  49. #endif
  50. #endif
  51. #ifdef CONFIG_BR_EXT
  52. /* #define BR_EXT_DEBUG */
  53. #define NAT25_IPV4 01
  54. #define NAT25_IPV6 02
  55. #define NAT25_IPX 03
  56. #define NAT25_APPLE 04
  57. #define NAT25_PPPOE 05
  58. #define RTL_RELAY_TAG_LEN (ETH_ALEN)
  59. #define TAG_HDR_LEN 4
  60. #define MAGIC_CODE 0x8186
  61. #define MAGIC_CODE_LEN 2
  62. #define WAIT_TIME_PPPOE 5 /* waiting time for pppoe server in sec */
  63. /*-----------------------------------------------------------------
  64. How database records network address:
  65. 0 1 2 3 4 5 6 7 8 9 10
  66. |----|----|----|----|----|----|----|----|----|----|----|
  67. IPv4 |type| | IP addr |
  68. IPX |type| Net addr | Node addr |
  69. IPX |type| Net addr |Sckt addr|
  70. Apple |type| Network |node|
  71. PPPoE |type| SID | AC MAC |
  72. -----------------------------------------------------------------*/
  73. /* Find a tag in pppoe frame and return the pointer */
  74. static __inline__ unsigned char *__nat25_find_pppoe_tag(struct pppoe_hdr *ph, unsigned short type)
  75. {
  76. unsigned char *cur_ptr, *start_ptr;
  77. unsigned short tagLen, tagType;
  78. start_ptr = cur_ptr = (unsigned char *)ph->tag;
  79. while ((cur_ptr - start_ptr) < ntohs(ph->length)) {
  80. /* prevent un-alignment access */
  81. tagType = (unsigned short)((cur_ptr[0] << 8) + cur_ptr[1]);
  82. tagLen = (unsigned short)((cur_ptr[2] << 8) + cur_ptr[3]);
  83. if (tagType == type)
  84. return cur_ptr;
  85. cur_ptr = cur_ptr + TAG_HDR_LEN + tagLen;
  86. }
  87. return 0;
  88. }
  89. static __inline__ int __nat25_add_pppoe_tag(struct sk_buff *skb, struct pppoe_tag *tag)
  90. {
  91. struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
  92. int data_len;
  93. data_len = tag->tag_len + TAG_HDR_LEN;
  94. if (skb_tailroom(skb) < data_len) {
  95. _DEBUG_ERR("skb_tailroom() failed in add SID tag!\n");
  96. return -1;
  97. }
  98. skb_put(skb, data_len);
  99. /* have a room for new tag */
  100. memmove(((unsigned char *)ph->tag + data_len), (unsigned char *)ph->tag, ntohs(ph->length));
  101. ph->length = htons(ntohs(ph->length) + data_len);
  102. memcpy((unsigned char *)ph->tag, tag, data_len);
  103. return data_len;
  104. }
  105. static int skb_pull_and_merge(struct sk_buff *skb, unsigned char *src, int len)
  106. {
  107. int tail_len;
  108. unsigned long end, tail;
  109. if ((src + len) > skb_tail_pointer(skb) || skb->len < len)
  110. return -1;
  111. tail = (unsigned long)skb_tail_pointer(skb);
  112. end = (unsigned long)src + len;
  113. if (tail < end)
  114. return -1;
  115. tail_len = (int)(tail - end);
  116. if (tail_len > 0)
  117. memmove(src, src + len, tail_len);
  118. skb_trim(skb, skb->len - len);
  119. return 0;
  120. }
  121. static __inline__ unsigned long __nat25_timeout(_adapter *priv)
  122. {
  123. unsigned long timeout;
  124. timeout = jiffies - NAT25_AGEING_TIME * HZ;
  125. return timeout;
  126. }
  127. static __inline__ int __nat25_has_expired(_adapter *priv,
  128. struct nat25_network_db_entry *fdb)
  129. {
  130. if (time_before_eq(fdb->ageing_timer, __nat25_timeout(priv)))
  131. return 1;
  132. return 0;
  133. }
  134. static __inline__ void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr,
  135. unsigned int *ipAddr)
  136. {
  137. memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
  138. networkAddr[0] = NAT25_IPV4;
  139. memcpy(networkAddr + 7, (unsigned char *)ipAddr, 4);
  140. }
  141. static __inline__ void __nat25_generate_ipx_network_addr_with_node(unsigned char *networkAddr,
  142. unsigned int *ipxNetAddr, unsigned char *ipxNodeAddr)
  143. {
  144. memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
  145. networkAddr[0] = NAT25_IPX;
  146. memcpy(networkAddr + 1, (unsigned char *)ipxNetAddr, 4);
  147. memcpy(networkAddr + 5, ipxNodeAddr, 6);
  148. }
  149. static __inline__ void __nat25_generate_ipx_network_addr_with_socket(unsigned char *networkAddr,
  150. unsigned int *ipxNetAddr, unsigned short *ipxSocketAddr)
  151. {
  152. memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
  153. networkAddr[0] = NAT25_IPX;
  154. memcpy(networkAddr + 1, (unsigned char *)ipxNetAddr, 4);
  155. memcpy(networkAddr + 5, (unsigned char *)ipxSocketAddr, 2);
  156. }
  157. static __inline__ void __nat25_generate_apple_network_addr(unsigned char *networkAddr,
  158. unsigned short *network, unsigned char *node)
  159. {
  160. memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
  161. networkAddr[0] = NAT25_APPLE;
  162. memcpy(networkAddr + 1, (unsigned char *)network, 2);
  163. networkAddr[3] = *node;
  164. }
  165. static __inline__ void __nat25_generate_pppoe_network_addr(unsigned char *networkAddr,
  166. unsigned char *ac_mac, unsigned short *sid)
  167. {
  168. memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
  169. networkAddr[0] = NAT25_PPPOE;
  170. memcpy(networkAddr + 1, (unsigned char *)sid, 2);
  171. memcpy(networkAddr + 3, (unsigned char *)ac_mac, 6);
  172. }
  173. #ifdef CL_IPV6_PASS
  174. static void __nat25_generate_ipv6_network_addr(unsigned char *networkAddr,
  175. unsigned int *ipAddr)
  176. {
  177. memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
  178. networkAddr[0] = NAT25_IPV6;
  179. memcpy(networkAddr + 1, (unsigned char *)ipAddr, 16);
  180. }
  181. static unsigned char *scan_tlv(unsigned char *data, int len, unsigned char tag, unsigned char len8b)
  182. {
  183. while (len > 0) {
  184. if (*data == tag && *(data + 1) == len8b && len >= len8b * 8)
  185. return data + 2;
  186. len -= (*(data + 1)) * 8;
  187. data += (*(data + 1)) * 8;
  188. }
  189. return NULL;
  190. }
  191. static int update_nd_link_layer_addr(unsigned char *data, int len, unsigned char *replace_mac)
  192. {
  193. struct icmp6hdr *icmphdr = (struct icmp6hdr *)data;
  194. unsigned char *mac;
  195. if (icmphdr->icmp6_type == NDISC_ROUTER_SOLICITATION) {
  196. if (len >= 8) {
  197. mac = scan_tlv(&data[8], len - 8, 1, 1);
  198. if (mac) {
  199. RTW_INFO("Router Solicitation, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
  200. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
  201. replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
  202. memcpy(mac, replace_mac, 6);
  203. return 1;
  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. RTW_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. } else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
  218. if (len >= 24) {
  219. mac = scan_tlv(&data[24], len - 24, 1, 1);
  220. if (mac) {
  221. RTW_INFO("Neighbor Solicitation, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
  222. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
  223. replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
  224. memcpy(mac, replace_mac, 6);
  225. return 1;
  226. }
  227. }
  228. } else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  229. if (len >= 24) {
  230. mac = scan_tlv(&data[24], len - 24, 2, 1);
  231. if (mac) {
  232. RTW_INFO("Neighbor Advertisement, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
  233. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
  234. replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
  235. memcpy(mac, replace_mac, 6);
  236. return 1;
  237. }
  238. }
  239. } else if (icmphdr->icmp6_type == NDISC_REDIRECT) {
  240. if (len >= 40) {
  241. mac = scan_tlv(&data[40], len - 40, 2, 1);
  242. if (mac) {
  243. RTW_INFO("Redirect, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
  244. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
  245. replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);
  246. memcpy(mac, replace_mac, 6);
  247. return 1;
  248. }
  249. }
  250. }
  251. return 0;
  252. }
  253. #ifdef SUPPORT_RX_UNI2MCAST
  254. static void convert_ipv6_mac_to_mc(struct sk_buff *skb)
  255. {
  256. struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN);
  257. unsigned char *dst_mac = skb->data;
  258. /* dst_mac[0] = 0xff; */
  259. /* dst_mac[1] = 0xff; */
  260. /*modified by qinjunjie,ipv6 multicast address ix 0x33-33-xx-xx-xx-xx*/
  261. dst_mac[0] = 0x33;
  262. dst_mac[1] = 0x33;
  263. memcpy(&dst_mac[2], &iph->daddr.s6_addr32[3], 4);
  264. #if defined(__LINUX_2_6__)
  265. /*modified by qinjunjie,warning:should not remove next line*/
  266. skb->pkt_type = PACKET_MULTICAST;
  267. #endif
  268. }
  269. #endif /* CL_IPV6_PASS */
  270. #endif /* SUPPORT_RX_UNI2MCAST */
  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. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0))
  797. /* IPX */
  798. if (ipx != NULL) {
  799. switch (method) {
  800. case NAT25_CHECK:
  801. if (!memcmp(skb->data + ETH_ALEN, ipx->ipx_source.node, ETH_ALEN)) {
  802. RTW_INFO("NAT25: Check IPX skb_copy\n");
  803. return 0;
  804. }
  805. return -1;
  806. case NAT25_INSERT: {
  807. RTW_INFO("NAT25: Insert IPX, Dest=%08x,%02x%02x%02x%02x%02x%02x,%04x Source=%08x,%02x%02x%02x%02x%02x%02x,%04x\n",
  808. ipx->ipx_dest.net,
  809. ipx->ipx_dest.node[0],
  810. ipx->ipx_dest.node[1],
  811. ipx->ipx_dest.node[2],
  812. ipx->ipx_dest.node[3],
  813. ipx->ipx_dest.node[4],
  814. ipx->ipx_dest.node[5],
  815. ipx->ipx_dest.sock,
  816. ipx->ipx_source.net,
  817. ipx->ipx_source.node[0],
  818. ipx->ipx_source.node[1],
  819. ipx->ipx_source.node[2],
  820. ipx->ipx_source.node[3],
  821. ipx->ipx_source.node[4],
  822. ipx->ipx_source.node[5],
  823. ipx->ipx_source.sock);
  824. if (!memcmp(skb->data + ETH_ALEN, ipx->ipx_source.node, ETH_ALEN)) {
  825. RTW_INFO("NAT25: Use IPX Net, and Socket as network addr\n");
  826. __nat25_generate_ipx_network_addr_with_socket(networkAddr, &ipx->ipx_source.net, &ipx->ipx_source.sock);
  827. /* change IPX source node addr to wlan STA address */
  828. memcpy(ipx->ipx_source.node, GET_MY_HWADDR(priv), ETH_ALEN);
  829. } else
  830. __nat25_generate_ipx_network_addr_with_node(networkAddr, &ipx->ipx_source.net, ipx->ipx_source.node);
  831. __nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
  832. __nat25_db_print(priv);
  833. }
  834. return 0;
  835. case NAT25_LOOKUP: {
  836. if (!memcmp(GET_MY_HWADDR(priv), ipx->ipx_dest.node, ETH_ALEN)) {
  837. RTW_INFO("NAT25: Lookup IPX, Modify Destination IPX Node addr\n");
  838. __nat25_generate_ipx_network_addr_with_socket(networkAddr, &ipx->ipx_dest.net, &ipx->ipx_dest.sock);
  839. __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
  840. /* replace IPX destination node addr with Lookup destination MAC addr */
  841. memcpy(ipx->ipx_dest.node, skb->data, ETH_ALEN);
  842. } else {
  843. __nat25_generate_ipx_network_addr_with_node(networkAddr, &ipx->ipx_dest.net, ipx->ipx_dest.node);
  844. __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
  845. }
  846. }
  847. return 0;
  848. default:
  849. return -1;
  850. }
  851. }
  852. #endif
  853. /* AARP */
  854. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0))
  855. else if (ea != NULL) {
  856. #else
  857. if (ea != NULL) {
  858. #endif
  859. /* Sanity check fields. */
  860. if (ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN) {
  861. DEBUG_WARN("NAT25: Appletalk AARP Sanity check fail!\n");
  862. return -1;
  863. }
  864. switch (method) {
  865. case NAT25_CHECK:
  866. return 0;
  867. case NAT25_INSERT: {
  868. /* change to AARP source mac address to wlan STA address */
  869. memcpy(ea->hw_src, GET_MY_HWADDR(priv), ETH_ALEN);
  870. RTW_INFO("NAT25: Insert AARP, Source=%d,%d Destination=%d,%d\n",
  871. ea->pa_src_net,
  872. ea->pa_src_node,
  873. ea->pa_dst_net,
  874. ea->pa_dst_node);
  875. __nat25_generate_apple_network_addr(networkAddr, &ea->pa_src_net, &ea->pa_src_node);
  876. __nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
  877. __nat25_db_print(priv);
  878. }
  879. return 0;
  880. case NAT25_LOOKUP: {
  881. RTW_INFO("NAT25: Lookup AARP, Source=%d,%d Destination=%d,%d\n",
  882. ea->pa_src_net,
  883. ea->pa_src_node,
  884. ea->pa_dst_net,
  885. ea->pa_dst_node);
  886. __nat25_generate_apple_network_addr(networkAddr, &ea->pa_dst_net, &ea->pa_dst_node);
  887. __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
  888. /* change to AARP destination mac address to Lookup result */
  889. memcpy(ea->hw_dst, skb->data, ETH_ALEN);
  890. }
  891. return 0;
  892. default:
  893. return -1;
  894. }
  895. }
  896. /* DDP */
  897. else if (ddp != NULL) {
  898. switch (method) {
  899. case NAT25_CHECK:
  900. return -1;
  901. case NAT25_INSERT: {
  902. RTW_INFO("NAT25: Insert DDP, Source=%d,%d Destination=%d,%d\n",
  903. ddp->deh_snet,
  904. ddp->deh_snode,
  905. ddp->deh_dnet,
  906. ddp->deh_dnode);
  907. __nat25_generate_apple_network_addr(networkAddr, &ddp->deh_snet, &ddp->deh_snode);
  908. __nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
  909. __nat25_db_print(priv);
  910. }
  911. return 0;
  912. case NAT25_LOOKUP: {
  913. RTW_INFO("NAT25: Lookup DDP, Source=%d,%d Destination=%d,%d\n",
  914. ddp->deh_snet,
  915. ddp->deh_snode,
  916. ddp->deh_dnet,
  917. ddp->deh_dnode);
  918. __nat25_generate_apple_network_addr(networkAddr, &ddp->deh_dnet, &ddp->deh_dnode);
  919. __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
  920. }
  921. return 0;
  922. default:
  923. return -1;
  924. }
  925. }
  926. return -1;
  927. }
  928. /*---------------------------------------------------*/
  929. /* Handle PPPoE frame */
  930. /*---------------------------------------------------*/
  931. else if ((protocol == __constant_htons(ETH_P_PPP_DISC)) ||
  932. (protocol == __constant_htons(ETH_P_PPP_SES))) {
  933. struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
  934. unsigned short *pMagic;
  935. switch (method) {
  936. case NAT25_CHECK:
  937. if (ph->sid == 0)
  938. return 0;
  939. return 1;
  940. case NAT25_INSERT:
  941. if (ph->sid == 0) { /* Discovery phase according to tag */
  942. if (ph->code == PADI_CODE || ph->code == PADR_CODE) {
  943. if (priv->ethBrExtInfo.addPPPoETag) {
  944. struct pppoe_tag *tag, *pOldTag;
  945. unsigned char tag_buf[40];
  946. int old_tag_len = 0;
  947. tag = (struct pppoe_tag *)tag_buf;
  948. pOldTag = (struct pppoe_tag *)__nat25_find_pppoe_tag(ph, ntohs(PTT_RELAY_SID));
  949. if (pOldTag) { /* if SID existed, copy old value and delete it */
  950. old_tag_len = ntohs(pOldTag->tag_len);
  951. if (old_tag_len + TAG_HDR_LEN + MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN > sizeof(tag_buf)) {
  952. DEBUG_ERR("SID tag length too long!\n");
  953. return -1;
  954. }
  955. memcpy(tag->tag_data + MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN,
  956. pOldTag->tag_data, old_tag_len);
  957. if (skb_pull_and_merge(skb, (unsigned char *)pOldTag, TAG_HDR_LEN + old_tag_len) < 0) {
  958. DEBUG_ERR("call skb_pull_and_merge() failed in PADI/R packet!\n");
  959. return -1;
  960. }
  961. ph->length = htons(ntohs(ph->length) - TAG_HDR_LEN - old_tag_len);
  962. }
  963. tag->tag_type = PTT_RELAY_SID;
  964. tag->tag_len = htons(MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN + old_tag_len);
  965. /* insert the magic_code+client mac in relay tag */
  966. pMagic = (unsigned short *)tag->tag_data;
  967. *pMagic = htons(MAGIC_CODE);
  968. memcpy(tag->tag_data + MAGIC_CODE_LEN, skb->data + ETH_ALEN, ETH_ALEN);
  969. /* Add relay tag */
  970. if (__nat25_add_pppoe_tag(skb, tag) < 0)
  971. return -1;
  972. RTW_INFO("NAT25: Insert PPPoE, forward %s packet\n",
  973. (ph->code == PADI_CODE ? "PADI" : "PADR"));
  974. } else { /* not add relay tag */
  975. if (priv->pppoe_connection_in_progress &&
  976. memcmp(skb->data + ETH_ALEN, priv->pppoe_addr, ETH_ALEN)) {
  977. DEBUG_ERR("Discard PPPoE packet due to another PPPoE connection is in progress!\n");
  978. return -2;
  979. }
  980. if (priv->pppoe_connection_in_progress == 0)
  981. memcpy(priv->pppoe_addr, skb->data + ETH_ALEN, ETH_ALEN);
  982. priv->pppoe_connection_in_progress = WAIT_TIME_PPPOE;
  983. }
  984. } else
  985. return -1;
  986. } else { /* session phase */
  987. RTW_INFO("NAT25: Insert PPPoE, insert session packet to %s\n", skb->dev->name);
  988. __nat25_generate_pppoe_network_addr(networkAddr, skb->data, &(ph->sid));
  989. __nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
  990. __nat25_db_print(priv);
  991. if (!priv->ethBrExtInfo.addPPPoETag &&
  992. priv->pppoe_connection_in_progress &&
  993. !memcmp(skb->data + ETH_ALEN, priv->pppoe_addr, ETH_ALEN))
  994. priv->pppoe_connection_in_progress = 0;
  995. }
  996. return 0;
  997. case NAT25_LOOKUP:
  998. if (ph->code == PADO_CODE || ph->code == PADS_CODE) {
  999. if (priv->ethBrExtInfo.addPPPoETag) {
  1000. struct pppoe_tag *tag;
  1001. unsigned char *ptr;
  1002. unsigned short tagType, tagLen;
  1003. int offset = 0;
  1004. ptr = __nat25_find_pppoe_tag(ph, ntohs(PTT_RELAY_SID));
  1005. if (ptr == 0) {
  1006. DEBUG_ERR("Fail to find PTT_RELAY_SID in FADO!\n");
  1007. return -1;
  1008. }
  1009. tag = (struct pppoe_tag *)ptr;
  1010. tagType = (unsigned short)((ptr[0] << 8) + ptr[1]);
  1011. tagLen = (unsigned short)((ptr[2] << 8) + ptr[3]);
  1012. if ((tagType != ntohs(PTT_RELAY_SID)) || (tagLen < (MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN))) {
  1013. DEBUG_ERR("Invalid PTT_RELAY_SID tag length [%d]!\n", tagLen);
  1014. return -1;
  1015. }
  1016. pMagic = (unsigned short *)tag->tag_data;
  1017. if (ntohs(*pMagic) != MAGIC_CODE) {
  1018. DEBUG_ERR("Can't find MAGIC_CODE in %s packet!\n",
  1019. (ph->code == PADO_CODE ? "PADO" : "PADS"));
  1020. return -1;
  1021. }
  1022. memcpy(skb->data, tag->tag_data + MAGIC_CODE_LEN, ETH_ALEN);
  1023. if (tagLen > MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN)
  1024. offset = TAG_HDR_LEN;
  1025. if (skb_pull_and_merge(skb, ptr + offset, TAG_HDR_LEN + MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN - offset) < 0) {
  1026. DEBUG_ERR("call skb_pull_and_merge() failed in PADO packet!\n");
  1027. return -1;
  1028. }
  1029. ph->length = htons(ntohs(ph->length) - (TAG_HDR_LEN + MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN - offset));
  1030. if (offset > 0)
  1031. tag->tag_len = htons(tagLen - MAGIC_CODE_LEN - RTL_RELAY_TAG_LEN);
  1032. RTW_INFO("NAT25: Lookup PPPoE, forward %s Packet from %s\n",
  1033. (ph->code == PADO_CODE ? "PADO" : "PADS"), skb->dev->name);
  1034. } else { /* not add relay tag */
  1035. if (!priv->pppoe_connection_in_progress) {
  1036. DEBUG_ERR("Discard PPPoE packet due to no connection in progresss!\n");
  1037. return -1;
  1038. }
  1039. memcpy(skb->data, priv->pppoe_addr, ETH_ALEN);
  1040. priv->pppoe_connection_in_progress = WAIT_TIME_PPPOE;
  1041. }
  1042. } else {
  1043. if (ph->sid != 0) {
  1044. RTW_INFO("NAT25: Lookup PPPoE, lookup session packet from %s\n", skb->dev->name);
  1045. __nat25_generate_pppoe_network_addr(networkAddr, skb->data + ETH_ALEN, &(ph->sid));
  1046. __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
  1047. __nat25_db_print(priv);
  1048. } else
  1049. return -1;
  1050. }
  1051. return 0;
  1052. default:
  1053. return -1;
  1054. }
  1055. }
  1056. /*---------------------------------------------------*/
  1057. /* Handle EAP frame */
  1058. /*---------------------------------------------------*/
  1059. else if (protocol == __constant_htons(0x888e)) {
  1060. switch (method) {
  1061. case NAT25_CHECK:
  1062. return -1;
  1063. case NAT25_INSERT:
  1064. return 0;
  1065. case NAT25_LOOKUP:
  1066. return 0;
  1067. default:
  1068. return -1;
  1069. }
  1070. }
  1071. /*---------------------------------------------------*/
  1072. /* Handle C-Media proprietary frame */
  1073. /*---------------------------------------------------*/
  1074. else if ((protocol == __constant_htons(0xe2ae)) ||
  1075. (protocol == __constant_htons(0xe2af))) {
  1076. switch (method) {
  1077. case NAT25_CHECK:
  1078. return -1;
  1079. case NAT25_INSERT:
  1080. return 0;
  1081. case NAT25_LOOKUP:
  1082. return 0;
  1083. default:
  1084. return -1;
  1085. }
  1086. }
  1087. /*---------------------------------------------------*/
  1088. /* Handle IPV6 frame */
  1089. /*---------------------------------------------------*/
  1090. #ifdef CL_IPV6_PASS
  1091. else if (protocol == __constant_htons(ETH_P_IPV6)) {
  1092. struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN);
  1093. if (sizeof(*iph) >= (skb->len - ETH_HLEN)) {
  1094. DEBUG_WARN("NAT25: malformed IPv6 packet !\n");
  1095. return -1;
  1096. }
  1097. switch (method) {
  1098. case NAT25_CHECK:
  1099. if (skb->data[0] & 1)
  1100. return 0;
  1101. return -1;
  1102. case NAT25_INSERT: {
  1103. RTW_INFO("NAT25: Insert IP, SA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x,"
  1104. " DA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x\n",
  1105. iph->saddr.s6_addr16[0], iph->saddr.s6_addr16[1], iph->saddr.s6_addr16[2], iph->saddr.s6_addr16[3],
  1106. iph->saddr.s6_addr16[4], iph->saddr.s6_addr16[5], iph->saddr.s6_addr16[6], iph->saddr.s6_addr16[7],
  1107. iph->daddr.s6_addr16[0], iph->daddr.s6_addr16[1], iph->daddr.s6_addr16[2], iph->daddr.s6_addr16[3],
  1108. iph->daddr.s6_addr16[4], iph->daddr.s6_addr16[5], iph->daddr.s6_addr16[6], iph->daddr.s6_addr16[7]);
  1109. if (memcmp(&iph->saddr, "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", 16)) {
  1110. __nat25_generate_ipv6_network_addr(networkAddr, (unsigned int *)&iph->saddr);
  1111. __nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
  1112. __nat25_db_print(priv);
  1113. if (iph->nexthdr == IPPROTO_ICMPV6 &&
  1114. skb->len > (ETH_HLEN + sizeof(*iph) + 4)) {
  1115. if (update_nd_link_layer_addr(skb->data + ETH_HLEN + sizeof(*iph),
  1116. skb->len - ETH_HLEN - sizeof(*iph), GET_MY_HWADDR(priv))) {
  1117. struct icmp6hdr *hdr = (struct icmp6hdr *)(skb->data + ETH_HLEN + sizeof(*iph));
  1118. hdr->icmp6_cksum = 0;
  1119. hdr->icmp6_cksum = csum_ipv6_magic(&iph->saddr, &iph->daddr,
  1120. iph->payload_len,
  1121. IPPROTO_ICMPV6,
  1122. csum_partial((__u8 *)hdr, iph->payload_len, 0));
  1123. }
  1124. }
  1125. }
  1126. }
  1127. return 0;
  1128. case NAT25_LOOKUP:
  1129. RTW_INFO("NAT25: Lookup IP, SA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x,"
  1130. " DA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x\n",
  1131. iph->saddr.s6_addr16[0], iph->saddr.s6_addr16[1], iph->saddr.s6_addr16[2], iph->saddr.s6_addr16[3],
  1132. iph->saddr.s6_addr16[4], iph->saddr.s6_addr16[5], iph->saddr.s6_addr16[6], iph->saddr.s6_addr16[7],
  1133. iph->daddr.s6_addr16[0], iph->daddr.s6_addr16[1], iph->daddr.s6_addr16[2], iph->daddr.s6_addr16[3],
  1134. iph->daddr.s6_addr16[4], iph->daddr.s6_addr16[5], iph->daddr.s6_addr16[6], iph->daddr.s6_addr16[7]);
  1135. __nat25_generate_ipv6_network_addr(networkAddr, (unsigned int *)&iph->daddr);
  1136. if (!__nat25_db_network_lookup_and_replace(priv, skb, networkAddr)) {
  1137. #ifdef SUPPORT_RX_UNI2MCAST
  1138. if (iph->daddr.s6_addr[0] == 0xff)
  1139. convert_ipv6_mac_to_mc(skb);
  1140. #endif
  1141. }
  1142. return 0;
  1143. default:
  1144. return -1;
  1145. }
  1146. }
  1147. #endif /* CL_IPV6_PASS */
  1148. return -1;
  1149. }
  1150. int nat25_handle_frame(_adapter *priv, struct sk_buff *skb)
  1151. {
  1152. #ifdef BR_EXT_DEBUG
  1153. if ((!priv->ethBrExtInfo.nat25_disable) && (!(skb->data[0] & 1))) {
  1154. panic_printk("NAT25: Input Frame: DA=%02x%02x%02x%02x%02x%02x SA=%02x%02x%02x%02x%02x%02x\n",
  1155. skb->data[0],
  1156. skb->data[1],
  1157. skb->data[2],
  1158. skb->data[3],
  1159. skb->data[4],
  1160. skb->data[5],
  1161. skb->data[6],
  1162. skb->data[7],
  1163. skb->data[8],
  1164. skb->data[9],
  1165. skb->data[10],
  1166. skb->data[11]);
  1167. }
  1168. #endif
  1169. if (!(skb->data[0] & 1)) {
  1170. int is_vlan_tag = 0, i, retval = 0;
  1171. unsigned short vlan_hdr = 0;
  1172. if (*((unsigned short *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_8021Q)) {
  1173. is_vlan_tag = 1;
  1174. vlan_hdr = *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2));
  1175. for (i = 0; i < 6; i++)
  1176. *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2 - i * 2)) = *((unsigned short *)(skb->data + ETH_ALEN * 2 - 2 - i * 2));
  1177. skb_pull(skb, 4);
  1178. }
  1179. if (!priv->ethBrExtInfo.nat25_disable) {
  1180. _irqL irqL;
  1181. _enter_critical_bh(&priv->br_ext_lock, &irqL);
  1182. /*
  1183. * This function look up the destination network address from
  1184. * the NAT2.5 database. Return value = -1 means that the
  1185. * corresponding network protocol is NOT support.
  1186. */
  1187. if (!priv->ethBrExtInfo.nat25sc_disable &&
  1188. (*((unsigned short *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP)) &&
  1189. !memcmp(priv->scdb_ip, skb->data + ETH_HLEN + 16, 4)) {
  1190. memcpy(skb->data, priv->scdb_mac, ETH_ALEN);
  1191. _exit_critical_bh(&priv->br_ext_lock, &irqL);
  1192. } else {
  1193. _exit_critical_bh(&priv->br_ext_lock, &irqL);
  1194. retval = nat25_db_handle(priv, skb, NAT25_LOOKUP);
  1195. }
  1196. } else {
  1197. if (((*((unsigned short *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP)) &&
  1198. !memcmp(priv->br_ip, skb->data + ETH_HLEN + 16, 4)) ||
  1199. ((*((unsigned short *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_ARP)) &&
  1200. !memcmp(priv->br_ip, skb->data + ETH_HLEN + 24, 4))) {
  1201. /* for traffic to upper TCP/IP */
  1202. retval = nat25_db_handle(priv, skb, NAT25_LOOKUP);
  1203. }
  1204. }
  1205. if (is_vlan_tag) {
  1206. skb_push(skb, 4);
  1207. for (i = 0; i < 6; i++)
  1208. *((unsigned short *)(skb->data + i * 2)) = *((unsigned short *)(skb->data + 4 + i * 2));
  1209. *((unsigned short *)(skb->data + ETH_ALEN * 2)) = __constant_htons(ETH_P_8021Q);
  1210. *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2)) = vlan_hdr;
  1211. }
  1212. if (retval == -1) {
  1213. /* DEBUG_ERR("NAT25: Lookup fail!\n"); */
  1214. return -1;
  1215. }
  1216. }
  1217. return 0;
  1218. }
  1219. #if 0
  1220. void mac_clone(_adapter *priv, unsigned char *addr)
  1221. {
  1222. struct sockaddr sa;
  1223. memcpy(sa.sa_data, addr, ETH_ALEN);
  1224. RTW_INFO("MAC Clone: Addr=%02x%02x%02x%02x%02x%02x\n",
  1225. addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
  1226. rtl8192cd_set_hwaddr(priv->dev, &sa);
  1227. }
  1228. int mac_clone_handle_frame(_adapter *priv, struct sk_buff *skb)
  1229. {
  1230. if (priv->ethBrExtInfo.macclone_enable && !priv->macclone_completed) {
  1231. if (!(skb->data[ETH_ALEN] & 1)) { /* check any other particular MAC add */
  1232. if (memcmp(skb->data + ETH_ALEN, GET_MY_HWADDR(priv), ETH_ALEN) &&
  1233. ((priv->dev->br_port) &&
  1234. memcmp(skb->data + ETH_ALEN, priv->br_mac, ETH_ALEN))) {
  1235. mac_clone(priv, skb->data + ETH_ALEN);
  1236. priv->macclone_completed = 1;
  1237. }
  1238. }
  1239. }
  1240. return 0;
  1241. }
  1242. #endif /* 0 */
  1243. #define SERVER_PORT 67
  1244. #define CLIENT_PORT 68
  1245. #define DHCP_MAGIC 0x63825363
  1246. #define BROADCAST_FLAG 0x8000
  1247. struct dhcpMessage {
  1248. u_int8_t op;
  1249. u_int8_t htype;
  1250. u_int8_t hlen;
  1251. u_int8_t hops;
  1252. u_int32_t xid;
  1253. u_int16_t secs;
  1254. u_int16_t flags;
  1255. u_int32_t ciaddr;
  1256. u_int32_t yiaddr;
  1257. u_int32_t siaddr;
  1258. u_int32_t giaddr;
  1259. u_int8_t chaddr[16];
  1260. u_int8_t sname[64];
  1261. u_int8_t file[128];
  1262. u_int32_t cookie;
  1263. u_int8_t options[308]; /* 312 - cookie */
  1264. };
  1265. void dhcp_flag_bcast(_adapter *priv, struct sk_buff *skb)
  1266. {
  1267. if (skb == NULL)
  1268. return;
  1269. if (!priv->ethBrExtInfo.dhcp_bcst_disable) {
  1270. unsigned short protocol = *((unsigned short *)(skb->data + 2 * ETH_ALEN));
  1271. if (protocol == __constant_htons(ETH_P_IP)) { /* IP */
  1272. struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);
  1273. if (iph->protocol == IPPROTO_UDP) { /* UDP */
  1274. struct udphdr *udph = (struct udphdr *)((SIZE_PTR)iph + (iph->ihl << 2));
  1275. if ((udph->source == __constant_htons(CLIENT_PORT))
  1276. && (udph->dest == __constant_htons(SERVER_PORT))) { /* DHCP request */
  1277. struct dhcpMessage *dhcph =
  1278. (struct dhcpMessage *)((SIZE_PTR)udph + sizeof(struct udphdr));
  1279. if (dhcph->cookie == __constant_htonl(DHCP_MAGIC)) { /* match magic word */
  1280. if (!(dhcph->flags & htons(BROADCAST_FLAG))) { /* if not broadcast */
  1281. register int sum = 0;
  1282. RTW_INFO("DHCP: change flag of DHCP request to broadcast.\n");
  1283. /* or BROADCAST flag */
  1284. dhcph->flags |= htons(BROADCAST_FLAG);
  1285. /* recalculate checksum */
  1286. sum = ~(udph->check) & 0xffff;
  1287. sum += dhcph->flags;
  1288. while (sum >> 16)
  1289. sum = (sum & 0xffff) + (sum >> 16);
  1290. udph->check = ~sum;
  1291. }
  1292. }
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. void *scdb_findEntry(_adapter *priv, unsigned char *macAddr,
  1299. unsigned char *ipAddr)
  1300. {
  1301. unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];
  1302. struct nat25_network_db_entry *db;
  1303. int hash;
  1304. /* _irqL irqL; */
  1305. /* _enter_critical_bh(&priv->br_ext_lock, &irqL); */
  1306. __nat25_generate_ipv4_network_addr(networkAddr, (unsigned int *)ipAddr);
  1307. hash = __nat25_network_hash(networkAddr);
  1308. db = priv->nethash[hash];
  1309. while (db != NULL) {
  1310. if (!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {
  1311. /* _exit_critical_bh(&priv->br_ext_lock, &irqL); */
  1312. return (void *)db;
  1313. }
  1314. db = db->next_hash;
  1315. }
  1316. /* _exit_critical_bh(&priv->br_ext_lock, &irqL); */
  1317. return NULL;
  1318. }
  1319. #endif /* CONFIG_BR_EXT */