rtw_br_ext.c 45 KB

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