rtw_br_ext.c 45 KB

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