rtw_br_ext.c 46 KB

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