rtw_br_ext.c 45 KB

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