Guest User

Untitled

a guest
Jan 5th, 2013
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 287.06 KB | None | 0 0
  1. /*
  2.  *      Operation routines for FLASH MIB access
  3.  *
  4.  *      Authors: David Hsu  <[email protected]>
  5.  *
  6.  *      $Id: flash.c,v 1.93 2009/09/15 02:12:32 bradhuang Exp $
  7.  *
  8.  */
  9.  
  10.  
  11. /* System include files */
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <arpa/inet.h>
  20. #include <netinet/in.h>
  21. #include <stdlib.h>
  22. #include <ctype.h>
  23. #include <regex.h>
  24. #define noPARSE_TXT_FILE
  25.  
  26. #define WLAN_FAST_INIT
  27. #define BR_SHORTCUT
  28.  
  29. /* Local include files */
  30. #include "apmib.h"
  31. #include "mibtbl.h"
  32.  
  33. #include <linux/if.h>
  34. #include <linux/if_ether.h>
  35. #include <linux/if_pppox.h>
  36. #include <linux/if_packet.h>
  37.  
  38. extern char *apmib_load_csconf(void);
  39. extern char *apmib_load_dsconf(void);
  40. extern char *apmib_load_hwconf(void);
  41. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  42. extern int comapi_initWlan(char *ifname);
  43. extern int dumpCfgFile(char *ifname, struct wifi_mib *pmib);
  44. #endif
  45. extern int Encode(unsigned char *ucInput, unsigned int inLen, unsigned char *ucOutput);
  46. extern int Decode(unsigned char *ucInput, unsigned int inLen, unsigned char *ucOutput);
  47. extern unsigned int mib_get_setting_len(CONFIG_DATA_T type);
  48. extern unsigned int mib_tlv_save(CONFIG_DATA_T type, void *mib_data, unsigned char *mib_tlvfile, unsigned int *tlv_content_len);
  49. extern unsigned int mib_get_real_len(CONFIG_DATA_T type);
  50. extern unsigned int mib_get_flash_offset(CONFIG_DATA_T type);
  51.  
  52. /* Constand definitions */
  53. #define DEC_FORMAT  ("%d")
  54. #define BYTE5_FORMAT    ("%02x%02x%02x%02x%02x")
  55. #define BYTE6_FORMAT    ("%02x%02x%02x%02x%02x%02x")
  56. #define BYTE13_FORMAT   ("%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x")
  57. #define STR_FORMAT  ("%s")
  58. #define HEX_FORMAT  ("%02x")
  59. #define SCHEDULE_FORMAT ("%d,%d,%d,%d")
  60. #ifdef HOME_GATEWAY
  61. #define PORTFW_FORMAT   ("%s, %d, %d, %d")
  62. #define PORTFILTER_FORMAT ("%d, %d, %d")
  63. #define IPFILTER_FORMAT ("%s, %d")
  64. #define TRIGGERPORT_FORMAT ("%d, %d, %d, %d, %d, %d")
  65. #endif
  66. #define MACFILTER_FORMAT ("%02x%02x%02x%02x%02x%02x")
  67. #define MACFILTER_COLON_FORMAT ("%02x:%02x:%02x:%02x:%02x:%02x")
  68. #define WDS_FORMAT  ("%02x%02x%02x%02x%02x%02x,%d")
  69. #define DHCPRSVDIP_FORMAT ("%02x%02x%02x%02x%02x%02x,%s,%s")
  70. #define VLANCONFIG_FORMAT ("%s,%d,%d,%d,%d,%d,%d")
  71. #ifdef HOME_GATEWAY
  72. #ifdef VPN_SUPPORT
  73. //#define IPSECTUNNEL_FORMAT ("%d, %d, %s, %d, %s, %d, %d, %s , %d, %s, %d, %d, %d, %d, %d, %d, %s, %d, %d, %d, %lu, %lu, %d, %s, %s, %s")
  74. #define IPSECTUNNEL_FORMAT ("%d, %d, %s, %d, %s, %d, %d, %s , %d, %s, %d, %d,  %d, %d,  %s, %d, %d, %d, %lu, %lu, %d, %s, %s, %s, %d, %s, %s, %d, %d, %s")
  75. #endif
  76. #ifdef CONFIG_IPV6
  77. #define RADVD_FORMAT ("%d, %s, %d, %d, %d, %d, %d, %d, %d, %d ,%d, %d, %s, %d, %d, %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x, %d, %d, %d, %d, %d, %d, %s, %d, %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x, %d, %d, %d, %d, %d, %d, %s, %d")
  78. #define DNSV6_FORMAT ("%d, %s")
  79. #define DHCPV6S_FORMAT ("%d, %s, %s, %s, %s")
  80. #define ADDR6_FORMAT ("%d, %d, %d, %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x, %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x")
  81. #endif
  82. #endif
  83.  
  84. #ifdef CONFIG_RTL_WAPI_SUPPORT
  85. #define CA_CERT "/var/myca/CA.cert"
  86. #endif
  87.  
  88. #define SPACE   (' ')
  89. #define EOL ('\n')
  90.  
  91. #define LOCAL_ADMIN_BIT 0x02
  92.  
  93. typedef enum {
  94.     HW_MIB_AREA=1,
  95.     HW_MIB_WLAN_AREA,
  96.     DEF_MIB_AREA,
  97.     DEF_MIB_WLAN_AREA,
  98.     MIB_AREA,
  99.     MIB_WLAN_AREA
  100. } CONFIG_AREA_T;
  101.  
  102. static CONFIG_AREA_T config_area;
  103. static int send_ppp_msg(int sessid, unsigned char *peerMacStr);
  104. static int send_l2tp_msg(unsigned short l2tp_ns, int buf_length, unsigned char *l2tp_buff, unsigned char *lanIp, unsigned char *serverIp);
  105. #define RTL_L2TP_POWEROFF_PATCH 1
  106.  
  107. /* Macro definition */
  108. static int _is_hex(char c)
  109. {
  110.     return (((c >= '0') && (c <= '9')) ||
  111.             ((c >= 'A') && (c <= 'F')) ||
  112.             ((c >= 'a') && (c <= 'f')));
  113. }
  114.  
  115. static int string_to_hex(char *string, unsigned char *key, int len)
  116. {
  117.     char tmpBuf[4];
  118.     int idx, ii=0;
  119.     for (idx=0; idx<len; idx+=2) {
  120.         tmpBuf[0] = string[idx];
  121.         tmpBuf[1] = string[idx+1];
  122.         tmpBuf[2] = 0;
  123.         if ( !_is_hex(tmpBuf[0]) || !_is_hex(tmpBuf[1]))
  124.             return 0;
  125.  
  126.         key[ii++] = (unsigned char) strtol(tmpBuf, (char**)NULL, 16);
  127.     }
  128.     return 1;
  129. }
  130.  
  131. static void convert_lower(char *str)
  132. {   int i;
  133.     int len = strlen(str);
  134.     for (i=0; i<len; i++)
  135.         str[i] = tolower(str[i]);
  136. }
  137.  
  138. static int APMIB_GET(int id, void *val)
  139. {
  140.     if (config_area == DEF_MIB_AREA || config_area == DEF_MIB_WLAN_AREA)
  141.         return apmib_getDef(id, val);
  142.     else
  143.         return apmib_get(id, val);
  144. }
  145.  
  146. static int APMIB_SET(int id, void *val)
  147. {
  148.     if (config_area == DEF_MIB_AREA || config_area == DEF_MIB_WLAN_AREA)
  149.         return apmib_setDef(id, val);
  150.     else
  151.         return apmib_set(id, val);
  152. }
  153.  
  154. /* Local declarations routines */
  155. static int flash_read(char *buf, int offset, int len);
  156. static int writeDefault(int isAll);
  157. static int searchMIB(char *token);
  158. static void getMIB(char *name, int id, TYPE_T type, int num, int array_separate, char **val);
  159. static void setMIB(char *name, int id, TYPE_T type, int len, int valnum, char **val);
  160. static void dumpAll(void);
  161. static void dumpAllHW(void);
  162. static void showHelp(void);
  163. static void showAllMibName(void);
  164. static void showAllHWMibName(void);
  165. static void showSetACHelp(void);
  166.  
  167. #if defined(CONFIG_RTK_MESH) && defined(_MESH_ACL_ENABLE_)
  168. static void showSetMeshACLHelp(void);
  169. #endif
  170. static void showSetVlanConfigHelp(void);
  171. static void showSetWdsHelp(void);
  172. static int read_flash_webpage(char *prefix, char *file);
  173. #ifdef TLS_CLIENT
  174. static int read_flash_cert(char *prefix, char *certfile);
  175. #endif
  176. #ifdef VPN_SUPPORT
  177. static int read_flash_rsa(char *prefix);
  178. #endif
  179. #ifdef HOME_GATEWAY
  180. static void showSetPortFwHelp(void);
  181. static void showSetPortFilterHelp(void);
  182. static void showSetIpFilterHelp(void);
  183. static void showSetMacFilterHelp(void);
  184. static void showSetUrlFilterHelp(void);
  185. static void showSetTriggerPortHelp(void);
  186. #ifdef GW_QOS_ENGINE
  187. //static void showSetQosHelp(void);
  188. #endif
  189. #ifdef ROUTE_SUPPORT
  190. static void showSetStaticRouteHelp(void);
  191. #endif
  192.  
  193. #ifdef VPN_SUPPORT
  194. static void showSetIpsecTunnelHelp(void);
  195. #endif
  196. static int generatePPPConf(int is_pppoe, char *option_file, char *pap_file, char *chap_file);
  197. #endif
  198.  
  199. #ifdef TLS_CLIENT
  200. static void showSetCertRootHelp(void);
  201. static void showSetCertUserHelp(void);
  202. #endif
  203.  
  204. static void generateWpaConf(char *outputFile, int isWds);
  205. static int generateHostapdConf(void);
  206.  
  207. #ifdef WLAN_FAST_INIT
  208. static int initWlan(char *ifname);
  209. #endif
  210.  
  211. #ifdef WIFI_SIMPLE_CONFIG
  212.  
  213. #if defined(UNIVERSAL_REPEATER) && defined(CONFIG_REPEATER_WPS_SUPPORT)
  214. static int updateWscConf(char *in, char *out, int genpin, char *wlanif_name);
  215. #else
  216. static int updateWscConf(char *in, char *out, int genpin);
  217. #endif //#if defined(UNIVERSAL_REPEATER) && defined(CONFIG_REPEATER_WPS_SUPPORT)
  218.  
  219. #endif
  220.  
  221. #ifdef COMPRESS_MIB_SETTING
  222. int flash_mib_checksum_ok(int offset);
  223. int flash_mib_compress_write(CONFIG_DATA_T type, char *data, PARAM_HEADER_T *pheader, unsigned char *pchecksum);
  224. #endif
  225.  
  226. #ifdef PARSE_TXT_FILE
  227. static int parseTxtConfig(char *filename, APMIB_Tp pConfig);
  228. static int getToken(char *line, char *value);
  229. static int set_mib(APMIB_Tp pMib, int id, void *value);
  230. static void getVal2(char *value, char **p1, char **p2);
  231. #ifdef HOME_GATEWAY
  232. static void getVal3(char *value, char **p1, char **p2, char **p3);
  233. static void getVal4(char *value, char **p1, char **p2, char **p3, char **p4);
  234. static void getVal5(char *value, char **p1, char **p2, char **p3, char **p4, char **p5);
  235. #endif
  236.  
  237. #ifdef MIB_TLV
  238.  
  239. //int mib_write_to_raw(const mib_table_entry_T *mib_tbl, void *data, char *pfile, unsigned int *idx);
  240. #endif
  241.  
  242.  
  243. static int acNum;
  244.  
  245. #if defined(CONFIG_RTK_MESH) && defined(_MESH_ACL_ENABLE_)
  246. static int meshAclNum;
  247. #endif
  248.  
  249. static int wdsNum;
  250.  
  251. #ifdef HOME_GATEWAY
  252. static int macFilterNum, portFilterNum, ipFilterNum, portFwNum, triggerPortNum, staticRouteNum;
  253. static int urlFilterNum;
  254.  
  255. #if defined(GW_QOS_ENGINE) || defined(QOS_BY_BANDWIDTH)
  256. static int qosRuleNum;
  257. #endif
  258. #endif
  259. static int dhcpRsvdIpNum;
  260. #ifdef TLS_CLIENT
  261. static int certRootNum, certUserNum ;
  262. #endif
  263. #if defined(VLAN_CONFIG_SUPPORTED)
  264. static int vlanConfigNum=MAX_IFACE_VLAN_CONFIG;
  265. #endif
  266. static is_wlan_mib=0;
  267.  
  268. /////////////////////////////////////////////////////////////////////////////////////////
  269. static char __inline__ *getVal(char *value, char **p)
  270. {
  271.     int len=0;
  272.  
  273.     while (*value == ' ' ) value++;
  274.  
  275.     *p = value;
  276.  
  277.     while (*value && *value!=',') {
  278.         value++;
  279.         len++;
  280.     }
  281.  
  282.     if ( !len ) {
  283.         *p = NULL;
  284.         return NULL;
  285.     }
  286.  
  287.     if ( *value == 0)
  288.         return NULL;
  289.  
  290.     *value = 0;
  291.     value++;
  292.  
  293.     return value;
  294. }
  295. #endif  // PARSE_TXT_FILE
  296.  
  297. //////////////////////////////////////////////////////////////////////
  298.  
  299. static int SetWlan_idx(char * wlan_iface_name)
  300. {
  301.     int idx;
  302.    
  303.         idx = atoi(&wlan_iface_name[4]);
  304.         if (idx >= NUM_WLAN_INTERFACE) {
  305.                 printf("invalid wlan interface index number!\n");
  306.                 return 0;
  307.         }
  308.         wlan_idx = idx;
  309.         vwlan_idx = 0;
  310.    
  311. #ifdef MBSSID      
  312.        
  313.         if (strlen(wlan_iface_name) >= 9 && wlan_iface_name[5] == '-' &&
  314.                 wlan_iface_name[6] == 'v' && wlan_iface_name[7] == 'a') {
  315.                 idx = atoi(&wlan_iface_name[8]);
  316.                 if (idx >= NUM_VWLAN_INTERFACE) {
  317.                     printf("invalid virtual wlan interface index number!\n");
  318.                     return 0;
  319.                 }
  320.                
  321.                 vwlan_idx = idx+1;
  322.                 idx = atoi(&wlan_iface_name[4]);
  323.                 wlan_idx = idx;
  324.         }
  325. #endif 
  326. #ifdef UNIVERSAL_REPEATER
  327.                 if (strlen(wlan_iface_name) >= 9 && wlan_iface_name[5] == '-' &&
  328.                         !memcmp(&wlan_iface_name[6], "vxd", 3)) {
  329.                     vwlan_idx = NUM_VWLAN_INTERFACE;
  330.                     idx = atoi(&wlan_iface_name[4]);
  331.                     wlan_idx = idx;
  332.                 }
  333.    
  334. #endif             
  335.                
  336. return 1;      
  337. }      
  338.  
  339. static char *get_token(char *data, char *token)
  340. {
  341.     char *ptr=data;
  342.     int len=0, idx=0;
  343.  
  344.     while (*ptr && *ptr != '\n' ) {
  345.         if (*ptr == '=') {
  346.             if (len <= 1)
  347.                 return NULL;
  348.             memcpy(token, data, len);
  349.  
  350.             /* delete ending space */
  351.             for (idx=len-1; idx>=0; idx--) {
  352.                 if (token[idx] !=  ' ')
  353.                     break;
  354.             }
  355.             token[idx+1] = '\0';
  356.            
  357.             return ptr+1;
  358.         }
  359.         len++;
  360.         ptr++;
  361.     }
  362.     return NULL;
  363. }
  364.  
  365. //////////////////////////////////////////////////////////////////////
  366. static int get_value(char *data, char *value)
  367. {
  368.     char *ptr=data;
  369.     int len=0, idx, i;
  370.  
  371.     while (*ptr && *ptr != '\n' && *ptr != '\r') {
  372.         len++;
  373.         ptr++;
  374.     }
  375.  
  376.     /* delete leading space */
  377.     idx = 0;
  378.     while (len-idx > 0) {
  379.         if (data[idx] != ' ')
  380.             break; 
  381.         idx++;
  382.     }
  383.     len -= idx;
  384.  
  385.     /* delete bracing '"' */
  386.     if (data[idx] == '"') {
  387.         for (i=idx+len-1; i>idx; i--) {
  388.             if (data[i] == '"') {
  389.                 idx++;
  390.                 len = i - idx;
  391.             }
  392.             break;
  393.         }
  394.     }
  395.  
  396.     if (len > 0) {
  397.         memcpy(value, &data[idx], len);
  398.         value[len] = '\0';
  399.     }
  400.     return len;
  401. }
  402.  
  403. static int setSystemTime_flash(void)
  404. {
  405.     #include <time.h>      
  406.     int cur_year=0;
  407.     //int time_mode=0;
  408.     struct tm tm_time;
  409.     time_t tm;
  410.     FILE *fp;
  411.     char *month_index[]={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  412.     char week_date[20], month[20], date[20], year[20], tmp1[20];
  413.     int i;
  414.     char buffer[200];
  415.     regex_t re;
  416.     regmatch_t match[2];
  417.     int status;
  418.  
  419.     fp = fopen("/proc/version", "r");
  420.     if (!fp) {
  421.         printf("Read /proc/version failed!\n");
  422.     }else
  423.         {
  424.             fgets(buffer, sizeof(buffer), fp);
  425.             fclose(fp);
  426.         }
  427.  
  428.         if (regcomp(&re, "#[0-9][0-9]* \\(.*\\)$", 0) == 0)
  429.         {
  430.             status = regexec(&re, buffer, 2, match, 0);
  431.             regfree(&re);
  432.             if (status == 0 &&
  433.                 match[1].rm_so >= 0)
  434.             {
  435.                 buffer[match[1].rm_eo] = 0;
  436.                
  437.             }
  438.         }
  439.     //Mon Nov 10 16:42:19 CST 2008
  440.    
  441.     memset(&tm_time, 0 , sizeof(tm_time));
  442.     sscanf(&buffer[match[1].rm_so],"%s %s %s %d:%d:%d %s %s", week_date, month, date, &(tm_time.tm_hour), &(tm_time.tm_min), &(tm_time.tm_sec), tmp1, year);
  443.     tm_time.tm_isdst = -1;  /* Be sure to recheck dst. */
  444.         for(i=0;i< 12;i++){
  445.             if(strcmp(month_index[i], month)==0){
  446.                 tm_time.tm_mon = i;
  447.                 break;
  448.             }
  449.         }
  450.         tm_time.tm_year = atoi(year) - 1900;
  451.         tm_time.tm_mday =atoi(date);
  452.         tm = mktime(&tm_time);
  453.         if(tm < 0){
  454.             fprintf(stderr, "set Time Error for tm!");
  455.         }
  456.         if(stime(&tm) < 0){
  457.             fprintf(stderr, "set system Time Error");
  458.         }
  459.    
  460.     if ( !apmib_init()) {
  461.         printf("Initialize AP MIB failed in setSystemTime_flash!\n");
  462.         return -1;
  463.     }
  464.     //apmib_get( MIB_NTP_ENABLED, (void *)&time_mode);
  465.  
  466.     //if(time_mode == 0)
  467.     {      
  468.         apmib_get( MIB_SYSTIME_YEAR, (void *)&cur_year);
  469.  
  470.         if(cur_year != 0){
  471.             tm_time.tm_year = cur_year - 1900;
  472.             apmib_get( MIB_SYSTIME_MON, (void *)&(tm_time.tm_mon));
  473.             apmib_get( MIB_SYSTIME_DAY, (void *)&(tm_time.tm_mday));
  474.             apmib_get( MIB_SYSTIME_HOUR, (void *)&(tm_time.tm_hour));
  475.             apmib_get( MIB_SYSTIME_MIN, (void *)&(tm_time.tm_min));
  476.             apmib_get( MIB_SYSTIME_SEC, (void *)&(tm_time.tm_sec));
  477.             tm = mktime(&tm_time);
  478.             if(tm < 0){
  479.                 fprintf(stderr, "make Time Error!\n");
  480.             }
  481.             if(stime(&tm) < 0){
  482.                 fprintf(stderr, "set system Time Error\n");
  483.             }
  484.         }
  485.     }
  486.  
  487.     system("echo done > /var/system/set_time");
  488.     return 0;  
  489. }
  490. //////////////////////////////////////////////////////////////////////
  491. static void readFileSetParam(char *file)
  492. {
  493.     FILE *fp;
  494.     char line[200], token[40], value[100], *ptr;
  495.     int idx, hw_setting_found=0, ds_setting_found=0, cs_setting_found=0;
  496.     char *arrayval[2];
  497.     mib_table_entry_T *pTbl;
  498.  
  499.    
  500.     if ( !apmib_init()) {
  501.         printf("Initialize AP MIB failed!\n");
  502.         return;
  503.     }
  504.  
  505.     fp = fopen(file, "r");
  506.     if (fp == NULL) {
  507.         printf("read file [%s] failed!\n", file);
  508.         return;
  509.     }
  510.  
  511.     arrayval[0] = value;
  512.  
  513.     while ( fgets(line, 200, fp) ) {
  514.         ptr = get_token(line, token);
  515.         if (ptr == NULL)
  516.             continue;
  517.         if (get_value(ptr, value)==0)
  518.             continue;
  519.  
  520.         idx = searchMIB(token);
  521.         if ( idx == -1 ) {
  522.             printf("invalid param [%s]!\n", token);
  523.             return;
  524.         }
  525.         if (config_area == HW_MIB_AREA || config_area == HW_MIB_WLAN_AREA) {
  526.             hw_setting_found = 1;
  527.             if (config_area == HW_MIB_AREA)
  528.                 pTbl = hwmib_table;
  529.             else
  530.                 pTbl = hwmib_wlan_table;
  531.         }
  532.         else if (config_area == DEF_MIB_AREA || config_area == DEF_MIB_WLAN_AREA) {
  533.             ds_setting_found = 1;
  534.             if (config_area == DEF_MIB_AREA)
  535.                 pTbl = mib_table;
  536.             else
  537.                 pTbl = mib_wlan_table;
  538.         }
  539.         else {
  540.             cs_setting_found = 1;
  541.             if (config_area == MIB_AREA)
  542.                 pTbl = mib_table;
  543.             else
  544.                 pTbl = mib_wlan_table;
  545.         }
  546.         config_area = 0;
  547.         setMIB(token, pTbl[idx].id, pTbl[idx].type, pTbl[idx].size, 1, arrayval);
  548.     }
  549.     fclose(fp);
  550.  
  551.     if (hw_setting_found)
  552.         apmib_update(HW_SETTING);
  553.     if (ds_setting_found)
  554.         apmib_update(DEFAULT_SETTING);
  555.     if (cs_setting_found)
  556.         apmib_update(CURRENT_SETTING);
  557. }
  558.  
  559. //////////////////////////////////////////////////////////////////////
  560. static int resetDefault(void)
  561. {
  562.     char *defMib;
  563.     int fh;
  564.     PARAM_HEADER_T header;
  565.     unsigned char checksum;
  566.     char *data;
  567.     int offset;
  568.     //int status;
  569. #ifdef COMPRESS_MIB_SETTING
  570.     //unsigned char* pContent = NULL;
  571.  
  572.     //COMPRESS_MIB_HEADER_T compHeader;
  573.     //unsigned char *expPtr, *compPtr;
  574.     //unsigned int expLen = header.len+sizeof(PARAM_HEADER_T);
  575.     //unsigned int compLen;
  576.     //unsigned int real_size = 0;
  577.     //int zipRate=0;
  578. #endif // #ifdef COMPRESS_MIB_SETTING
  579.  
  580. #ifdef MIB_TLV
  581.     unsigned char *pfile = NULL;
  582.     unsigned char *mib_tlv_data = NULL;
  583.     unsigned int tlv_content_len = 0;
  584.     unsigned int mib_tlv_max_len = 0;
  585. #endif
  586.  
  587.     fh = open(FLASH_DEVICE_NAME, O_RDWR);
  588.  
  589. #if CONFIG_APMIB_SHARED_MEMORY == 1
  590.     apmib_sem_lock();
  591. #endif
  592.  
  593.     if ((defMib=apmib_dsconf()) == NULL) {
  594.         printf("Default configuration invalid!\n");
  595. #if CONFIG_APMIB_SHARED_MEMORY == 1
  596.         apmib_sem_unlock();
  597. #endif
  598.         return -1;
  599.     }
  600.  
  601. #ifdef MIB_TLV
  602.  
  603. //mib_display_data_content(DEFAULT_SETTING, pMib, sizeof(APMIB_T));
  604.  
  605.     mib_tlv_max_len = mib_get_setting_len(DEFAULT_SETTING)*4;
  606.     tlv_content_len = 0;
  607.     pfile = malloc(mib_tlv_max_len);
  608.     memset(pfile, 0x00, mib_tlv_max_len);
  609.    
  610.     if( pfile != NULL && mib_tlv_save(DEFAULT_SETTING, (void*)defMib, pfile, &tlv_content_len) == 1)
  611.     {
  612.         mib_tlv_data = malloc(tlv_content_len+1); // 1:checksum
  613.         if(mib_tlv_data != NULL)
  614.         {
  615.             memcpy(mib_tlv_data, pfile, tlv_content_len);
  616.         }          
  617.         free(pfile);
  618.     }
  619.    
  620.     if(mib_tlv_data != NULL)
  621.     {
  622.         sprintf((char *)header.signature, "%s%02d", DEFAULT_SETTING_HEADER_TAG, DEFAULT_SETTING_VER);
  623.         header.len = tlv_content_len+1;
  624.         data = (char *)mib_tlv_data;
  625.         checksum = CHECKSUM((unsigned char *)data, header.len-1);
  626.         data[tlv_content_len] = CHECKSUM((unsigned char *)data, tlv_content_len);
  627.         //mib_display_tlv_content(DEFAULT_SETTING, data, header.len);      
  628.     }
  629.     else
  630.     {
  631. #endif //#ifdef MIB_TLV
  632.     data = (char *)defMib; 
  633.     sprintf((char *)header.signature, "%s%02d", DEFAULT_SETTING_HEADER_TAG, DEFAULT_SETTING_VER);
  634.     header.len = sizeof(APMIB_T) + sizeof(checksum);
  635.     checksum = CHECKSUM((unsigned char *)data, header.len-1);
  636.  
  637. #ifdef MIB_TLV
  638.     }
  639. #endif 
  640.  
  641. #ifdef MIB_TLV
  642.     if(mib_tlv_data != NULL)
  643.     {
  644.        
  645.         sprintf((char *)header.signature, "%s%02d", CURRENT_SETTING_HEADER_TAG, CURRENT_SETTING_VER);
  646.         header.len = tlv_content_len+1;
  647.         checksum = CHECKSUM((unsigned char *)data, header.len-1);
  648.         //mib_display_tlv_content(CURRENT_SETTING, data, header.len);      
  649.     }
  650.     else
  651.     {
  652. #endif
  653.     sprintf((char *)header.signature, "%s%02d", CURRENT_SETTING_HEADER_TAG, CURRENT_SETTING_VER);
  654.     header.len = sizeof(APMIB_T) + sizeof(checksum);
  655.  
  656. #ifdef MIB_TLV
  657.     }
  658. #endif
  659.  
  660.     lseek(fh, CURRENT_SETTING_OFFSET, SEEK_SET);
  661. #ifdef COMPRESS_MIB_SETTING
  662.     if(flash_mib_compress_write(CURRENT_SETTING, data, &header, &checksum) == 1)
  663.     {
  664.         COMP_TRACE(stderr,"\r\n flash_mib_compress_write CURRENT_SETTING DONE, __[%s-%u]", __FILE__,__LINE__);         
  665.     }
  666.     else
  667.     {
  668. #endif //#ifdef COMPRESS_MIB_SETTING
  669.         if ( write(fh, (const void *)&dsHeader, sizeof(dsHeader))!=sizeof(dsHeader) ) {
  670.             printf("write cs header failed!\n");
  671. #if CONFIG_APMIB_SHARED_MEMORY == 1
  672.             apmib_shm_free(defMib, DSCONF_SHM_KEY);
  673.             apmib_sem_unlock();
  674. #else
  675.             free(defMib);
  676. #endif
  677.             return -1;
  678.         }
  679.         if ( write(fh, (const void *)defMib, dsHeader.len) != dsHeader.len ) {
  680.             printf("write cs MIB failed!\n");
  681. #if CONFIG_APMIB_SHARED_MEMORY == 1
  682.             apmib_shm_free(defMib, DSCONF_SHM_KEY);
  683.             apmib_sem_unlock();
  684. #else
  685.             free(defMib);
  686. #endif
  687.             return -1;
  688.         }
  689. #ifdef COMPRESS_MIB_SETTING
  690.     }
  691. #endif
  692.     close(fh);
  693.     sync();
  694.  
  695.     offset = CURRENT_SETTING_OFFSET;
  696. #ifdef COMPRESS_MIB_SETTING
  697.     if(flash_mib_checksum_ok(offset) == 1)
  698.     {
  699.         COMP_TRACE(stderr,"\r\n CURRENT_SETTING hecksum_ok\n");
  700.     }
  701. #endif
  702.  
  703.  
  704. #if CONFIG_APMIB_SHARED_MEMORY == 1
  705.     apmib_load_csconf(); //read current settings diectly from flash
  706.     apmib_shm_free(defMib, DSCONF_SHM_KEY);
  707.     apmib_sem_unlock();
  708. #else
  709.     free(defMib);
  710. #endif
  711.  
  712.     return 0;
  713. }
  714.  
  715.  
  716.  
  717. //////////////////////////////////////////////////////////////////////
  718. int main(int argc, char** argv)
  719. {
  720.     int argNum=1, action=0, idx, num, valNum=0;
  721.     char mib[100]={0}, valueArray[199][100], *value[199], *ptr;
  722. #ifdef PARSE_TXT_FILE
  723.     char filename[100]={0};
  724.     APMIB_T apmib;
  725. #endif
  726.     mib_table_entry_T *pTbl=NULL;
  727.  
  728. if(0)
  729. {
  730.     int kk=0;
  731.     for(kk=0; kk < argc ; kk++)
  732.     {
  733.         printf("\r\n argv[%d]=[%s],__[%s-%u]\r\n",kk, argv[kk],__FILE__,__LINE__);
  734.  
  735.     }
  736. }
  737.  
  738.     if ( argc > 1 ) {
  739. #ifdef PARSE_TXT_FILE
  740.         if ( !strcmp(argv[argNum], "-f") ) {
  741.             if (++argNum < argc)
  742.                 sscanf(argv[argNum++], "%s", filename);
  743.         }
  744. #endif
  745.         if ( !strcmp(argv[argNum], "get") ) {
  746.             action = 1;
  747.             if (++argNum < argc) {
  748.                 if (argc > 3 && !memcmp(argv[argNum], "wlan", 4)) {
  749.                     int idx;
  750. #ifdef MBSSID
  751.                     if (strlen(argv[argNum]) >= 9 && argv[argNum][5] == '-' &&
  752.                         argv[argNum][6] == 'v' && argv[argNum][7] == 'a') {
  753.                         idx = atoi(&argv[argNum][8]);
  754.                         if (idx >= NUM_VWLAN_INTERFACE) {
  755.                             printf("invalid virtual wlan interface index number!\n");
  756.                             return 0;
  757.                         }
  758.                         vwlan_idx = idx+1;
  759.                     }
  760. #ifdef UNIVERSAL_REPEATER
  761.                 if (strlen(argv[argNum]) >= 9 && argv[argNum][5] == '-' &&
  762.                         !memcmp(&argv[argNum][6], "vxd", 3)) {
  763.                     vwlan_idx = NUM_VWLAN_INTERFACE;
  764.                 }
  765. #endif                                 
  766. #endif
  767.                     idx = atoi(&argv[argNum++][4]);
  768.                     if (idx >= NUM_WLAN_INTERFACE) {
  769.                         printf("invalid wlan interface index number!\n");
  770.                         goto normal_return;
  771.                     }
  772.                     wlan_idx = idx;
  773.                 }
  774.                 sscanf(argv[argNum], "%s", mib);
  775.                 while (++argNum < argc) {
  776.                     sscanf(argv[argNum], "%s", valueArray[valNum]);
  777.                     value[valNum] = valueArray[valNum];
  778.                     valNum++;
  779.                 }
  780.                 value[valNum]= NULL;
  781.             }
  782.         }
  783.         else if ( !strcmp(argv[argNum], "set") ) {
  784.             action = 2;
  785.             if (++argNum < argc) {
  786.                 if (argc > 4 && !memcmp(argv[argNum], "wlan", 4)) {
  787.                     int idx;
  788. #ifdef MBSSID
  789.                     if (strlen(argv[argNum]) >= 9 && argv[argNum][5] == '-' &&
  790.                         argv[argNum][6] == 'v' && argv[argNum][7] == 'a') {
  791.                         idx = atoi(&argv[argNum][8]);
  792.                         if (idx >= NUM_VWLAN_INTERFACE) {
  793.                             printf("invalid virtual wlan interface index number!\n");
  794.                             return 0;
  795.                         }
  796.                         vwlan_idx = idx+1;
  797.                     }
  798. #ifdef UNIVERSAL_REPEATER
  799.                 if (strlen(argv[argNum]) >= 9 && argv[argNum][5] == '-' &&
  800.                         !memcmp(&argv[argNum][6], "vxd", 3)) {
  801.                     vwlan_idx = NUM_VWLAN_INTERFACE;
  802.                 }
  803. #endif                                 
  804. #endif
  805.                     idx = atoi(&argv[argNum++][4]);
  806.                     if (idx >= NUM_WLAN_INTERFACE) {
  807.                         printf("invalid wlan interface index number!\n");
  808.                         goto normal_return;
  809.                     }
  810.                     wlan_idx = idx;
  811.                 }
  812.                 sscanf(argv[argNum], "%s", mib);
  813.                 int SettingSSID = 0;
  814.                 if( !strcmp(mib , "SSID") ||
  815.                     !strcmp(mib , "WLAN0_SSID") ||
  816.                     !strcmp(mib , "WLAN0_WSC_SSID") ||
  817.                     !strcmp(mib , "WLAN0_VAP0_SSID") ||
  818.                     !strcmp(mib , "WLAN0_VAP1_SSID") ||
  819.                     !strcmp(mib , "WLAN0_VAP2_SSID") ||
  820.                     !strcmp(mib , "WLAN0_VAP3_SSID") ||
  821.                     !strcmp(mib , "WLAN0_VAP4_SSID") ||
  822.                     !strcmp(mib , "REPEATER_SSID1") || 
  823.                     !strcmp(mib , "REPEATER_SSID2") ||
  824.                     !strcmp(mib , "NTP_TIMEZONE")
  825.                     ){
  826.                    
  827.                     SettingSSID = 1;
  828.                 }
  829.                
  830.                 while (++argNum < argc) {
  831.                     if(SettingSSID == 1){                      
  832.  
  833.                         //memcpy(valueArray[valNum] , argv[argNum] , strlen(argv[argNum]));
  834.                         strcpy(valueArray[valNum] , argv[argNum] );
  835.                         value[valNum] = valueArray[valNum];
  836.                         valNum++;
  837.                         break;
  838.                     }                  
  839.                     sscanf(argv[argNum], "%s", valueArray[valNum]);
  840.                     value[valNum] = valueArray[valNum];
  841.                     valNum++;
  842.                 }
  843.                 value[valNum]= NULL;
  844.             }
  845.         }
  846.         else if ( !strcmp(argv[argNum], "all") ) {
  847.             action = 3;
  848.         }
  849.         else if ( !strcmp(argv[argNum], "default") ) {
  850.             return writeDefault(1);
  851.         }
  852.         else if ( !strcmp(argv[argNum], "default-sw") ) {
  853.             return writeDefault(0);
  854.         }
  855.         else if ( !strcmp(argv[argNum], "reset") ) {
  856.             return resetDefault();
  857.         }
  858.         else if ( !strcmp(argv[argNum], "hostapd") ){
  859.             return generateHostapdConf();
  860.         }
  861.         else if ( !strcmp(argv[argNum], "extr") ) {
  862.             if (++argNum < argc) {
  863.                 char prefix[20], file[20]={0};
  864.                 int ret;
  865.                 sscanf(argv[argNum], "%s", prefix);
  866.                 if (++argNum < argc)
  867.                     sscanf(argv[argNum], "%s", file);
  868.                 ret  = read_flash_webpage(prefix, file);
  869.                 if (ret == 0) // success
  870.                     unlink(file);
  871.                 return ret;
  872.             }
  873.             printf("Usage: %s web prefix\n", argv[0]);
  874.             return -1;
  875.         }
  876. #ifdef TLS_CLIENT
  877.         else if ( !strcmp(argv[argNum], "cert") ) {
  878.             if (++argNum < argc) {
  879.                 char prefix[20], file[20]={0};
  880.                 int ret;
  881.                 sscanf(argv[argNum], "%s", prefix);
  882.                 if (++argNum < argc)
  883.                     sscanf(argv[argNum], "%s", file);
  884.                 ret  = read_flash_cert(prefix,file);
  885.                 //if (ret == 0) // success
  886.                 //  unlink(file);
  887.                 return ret;
  888.             }
  889.             printf("Usage: %s cert prefix\n", argv[0]);
  890.             return -1;
  891.         }
  892. #endif
  893. #ifdef VPN_SUPPORT
  894.         else if ( !strcmp(argv[argNum], "rsa") ) {
  895.             if (++argNum < argc) {
  896.                 char prefix[20], file[20]={0};
  897.                 int ret;
  898.                 sscanf(argv[argNum], "%s", prefix);
  899.                 if (++argNum < argc)
  900.                     sscanf(argv[argNum], "%s", file);
  901.                 ret  = read_flash_rsa(prefix);
  902.                 //if (ret == 0) // success
  903.                 //  unlink(file);
  904.                 return ret;
  905.             }
  906.             printf("Usage: %s rsa prefix\n", argv[0]);
  907.             return -1;
  908.         }
  909. #endif
  910.         else if ( !strcmp(argv[argNum], "test-hwconf") ) {
  911. #if CONFIG_APMIB_SHARED_MEMORY == 1
  912.             apmib_sem_lock();
  913. #endif
  914.             if ((ptr=apmib_hwconf()) == NULL) {
  915. #if CONFIG_APMIB_SHARED_MEMORY == 1
  916.                 apmib_sem_unlock();
  917. #endif
  918.                 return -1;
  919.             }
  920. #if CONFIG_APMIB_SHARED_MEMORY == 1
  921.             apmib_shm_free(ptr, HWCONF_SHM_KEY);
  922.             apmib_sem_unlock();
  923. #else
  924.             free(ptr);
  925. #endif
  926.             return 0;
  927.         }
  928.         else if ( !strcmp(argv[argNum], "test-dsconf") ) {
  929. #if CONFIG_APMIB_SHARED_MEMORY == 1
  930.             apmib_sem_lock();
  931. #endif
  932.             if ((ptr=apmib_dsconf()) == NULL) {
  933. #if CONFIG_APMIB_SHARED_MEMORY == 1
  934.                 apmib_sem_unlock();
  935. #endif
  936.                 return -1;
  937.             }
  938. #if CONFIG_APMIB_SHARED_MEMORY == 1
  939.             apmib_shm_free(ptr, DSCONF_SHM_KEY);
  940.             apmib_sem_unlock();
  941. #else
  942.             free(ptr);
  943. #endif
  944.             return 0;
  945.         }
  946.         else if ( !strcmp(argv[argNum], "test-csconf") ) {
  947. #if CONFIG_APMIB_SHARED_MEMORY == 1
  948.             apmib_sem_lock();
  949. #endif
  950.             if ((ptr=apmib_csconf()) == NULL) {
  951. #if CONFIG_APMIB_SHARED_MEMORY == 1
  952.                 apmib_sem_unlock();
  953. #endif
  954.                 return -1;
  955.             }
  956. #if CONFIG_APMIB_SHARED_MEMORY == 1
  957.             apmib_shm_free(ptr, CSCONF_SHM_KEY);
  958.             apmib_sem_unlock();
  959. #else
  960.             free(ptr);
  961. #endif
  962.             return 0;
  963.         }
  964.         else if ( !strcmp(argv[argNum], "default-fc") ) {
  965.             int value=0;
  966.  
  967.             apmib_init();
  968.             value=BRIDGE_MODE;
  969.             apmib_set(MIB_OP_MODE,(void *)&value);
  970.             apmib_get(MIB_DHCP, (void *)&value);        //for FC, dhcp server not allowed when client
  971.             if( value > DHCP_CLIENT )   value=DHCP_DISABLED;
  972.             apmib_set(MIB_DHCP, (void *)&value);
  973.             value = 0xc0a801fa;
  974.             apmib_set(MIB_IP_ADDR, (void *)&value);     //for FC, default IP to 192.168.1.250
  975.             value=BANDMODESINGLE;
  976.             apmib_set(MIB_WLAN_BAND2G5G_SELECT,(void *)&value);
  977.             value=SMACSPHY;
  978.             SetWlan_idx("wlan0");
  979.             apmib_set(MIB_WLAN_MAC_PHY_MODE,(void *)&value);
  980.             //value=0;
  981.             //apmib_set(MIB_WLAN_WLAN_DISABLED, (void *)&value);
  982.             /*
  983.             SetWlan_idx("wlan1");
  984.             value=SMACSPHY;
  985.             apmib_set(MIB_WLAN_MAC_PHY_MODE,(void *)&value);
  986.             value=1;
  987.             apmib_set(MIB_WLAN_WLAN_DISABLED, (void *)&value);
  988.             */
  989.             apmib_update(CURRENT_SETTING);
  990.  
  991.             return 0;
  992.         } else if ( !strcmp(argv[argNum], "wpa") ) {
  993.             int isWds = 0;
  994.             if ((argNum+2) < argc) {
  995.                 if (memcmp(argv[++argNum], "wlan", 4)) {
  996.                     printf("Miss wlan_interface argument!\n");
  997.                     return 0;
  998.                 }
  999. #ifdef MBSSID
  1000.                 if (strlen(argv[argNum]) >= 9 && argv[argNum][5] == '-' &&
  1001.                         argv[argNum][6] == 'v' && argv[argNum][7] == 'a') {
  1002.                     idx = atoi(&argv[argNum][8]);
  1003.                     if (idx >= NUM_VWLAN_INTERFACE) {
  1004.                         printf("invalid virtual wlan interface index number!\n");
  1005.                         return 0;
  1006.                     }
  1007.                     vwlan_idx = idx+1;
  1008.                 }
  1009. #ifdef UNIVERSAL_REPEATER
  1010.                 if (strlen(argv[argNum]) >= 9 && argv[argNum][5] == '-' &&
  1011.                         !memcmp(&argv[argNum][6], "vxd", 3)) {
  1012.                     vwlan_idx = NUM_VWLAN_INTERFACE;
  1013.                 }
  1014. #endif             
  1015. #endif
  1016.                 wlan_idx = atoi(&argv[argNum][4]);
  1017.                 if (wlan_idx >= NUM_WLAN_INTERFACE) {
  1018.                     printf("invalid wlan interface index number!\n");
  1019.                     goto normal_return;
  1020.                 }
  1021.  
  1022. #ifdef CONFIG_RTK_MESH
  1023.                 if ((argNum+2) < argc)
  1024.                 {
  1025.                     if( !strcmp(argv[argNum+2], "wds"))
  1026.                         isWds = 1;
  1027.                     else if(!strcmp(argv[argNum+2], "msh"))
  1028.                         isWds = 7;
  1029.                 }
  1030. #else
  1031.                 if (((argc-1) > (argNum+1)) && !strcmp(argv[argNum+2], "wds"))
  1032.                     isWds = 1;
  1033. #endif // CONFIG_RTK_MESH
  1034.  
  1035.                 generateWpaConf(argv[argNum+1], isWds);
  1036.                 goto normal_return;
  1037.             }
  1038.             else {
  1039.                 printf("Miss arguments [wlan_interface config_filename]!\n");
  1040.                 return 0;
  1041.             }
  1042.         }
  1043.         // set flash parameters by reading from file
  1044.         else if ( !strcmp(argv[argNum], "-param_file") ) {
  1045.             if ((argNum+2) < argc) {
  1046.                 if (memcmp(argv[++argNum], "wlan", 4)) {
  1047.                     printf("Miss wlan_interface argument!\n");
  1048.                     return 0;
  1049.                 }
  1050. #ifdef MBSSID
  1051.                 if (strlen(argv[argNum]) >= 9 && argv[argNum][5] == '-' &&
  1052.                         argv[argNum][6] == 'v' && argv[argNum][7] == 'a') {
  1053.                     idx = atoi(&argv[argNum][8]);
  1054.                     if (idx >= NUM_VWLAN_INTERFACE) {
  1055.                         printf("invalid virtual wlan interface index number!\n");
  1056.                         return 0;
  1057.                     }
  1058.                     vwlan_idx = idx+1;
  1059.                 }
  1060. #ifdef UNIVERSAL_REPEATER
  1061.                 if (strlen(argv[argNum]) >= 9 && argv[argNum][5] == '-' &&
  1062.                         !memcmp(&argv[argNum][6], "vxd", 3)) {
  1063.                     vwlan_idx = NUM_VWLAN_INTERFACE;
  1064.                 }
  1065. #endif             
  1066. #endif
  1067.                 wlan_idx = atoi(&argv[argNum][4]);
  1068.                 if (wlan_idx >= NUM_WLAN_INTERFACE) {
  1069.                     printf("invalid wlan interface index number!\n");
  1070.                     goto normal_return;
  1071.                 }
  1072.                 readFileSetParam(argv[argNum+1]);
  1073. #if defined(UNIVERSAL_REPEATER) && defined(CONFIG_REPEATER_WPS_SUPPORT)            
  1074.                
  1075.                 if(strcmp(argv[argNum],"wlan0-vxd") == 0 || strcmp(argv[argNum],"wlan1-vxd") == 0)                 
  1076.                 {
  1077.                     char tmpStr[MAX_SSID_LEN];     
  1078.                                            
  1079.                     apmib_get(MIB_WLAN_SSID, (void *)tmpStr);
  1080.  
  1081.                     apmib_set(MIB_WLAN_WSC_SSID, (void *)tmpStr);
  1082.                                        
  1083.                     if(wlan_idx == 0)
  1084.                         apmib_set(MIB_REPEATER_SSID1, (void *)tmpStr);
  1085.                     else
  1086.                         apmib_set(MIB_REPEATER_SSID2, (void *)tmpStr); 
  1087.                        
  1088.                     apmib_update(CURRENT_SETTING);
  1089.                 }
  1090. #endif // #if defined(UNIVERSAL_REPEATER) && defined(CONFIG_REPEATER_WPS_SUPPORT)              
  1091.             }
  1092.             else
  1093.                 printf("Miss arguments [wlan_interface param_file]!\n");
  1094.             return 0;
  1095.         }
  1096. // added by rock /////////////////////////////////////////
  1097. #ifdef VOIP_SUPPORT
  1098.         else if ( !strcmp(argv[argNum], "size") ) {
  1099.             printf("Flash Setting Size = %d, MIB Size = %d\n",
  1100.                 CURRENT_SETTING_OFFSET - DEFAULT_SETTING_OFFSET, sizeof(*pMib));
  1101.             return 0;
  1102.         }
  1103.         else if (strcmp(argv[argNum], "voip") == 0) {
  1104.  
  1105.             if (argc >= 2) // have voip param
  1106.                 return flash_voip_cmd(argc - 2, &argv[2]);
  1107.             else
  1108.                 return flash_voip_cmd(0, NULL);
  1109.         }
  1110. #endif     
  1111. #ifdef CONFIG_RTL_WAPI_SUPPORT
  1112.         else if(0==strcmp(argv[argNum],"wapi-check"))
  1113.         {
  1114.             /*if WAPI ,check need to init CA*/
  1115.              struct stat status;
  1116.             int init;
  1117.             if ( !apmib_init()) {
  1118.                 printf("Initialize AP MIB failed!\n");
  1119.                 return -1;
  1120.             }
  1121.             apmib_get(MIB_WLAN_WAPI_CA_INIT,(void *)&init);
  1122.             if(init)
  1123.                 return 0;
  1124.             /*check if CA.cert exists. since the defauts maybe load*/
  1125.             if (stat(CA_CERT, &status) < 0)
  1126.             {
  1127.                 system("initCAFiles.sh");
  1128.                 init=1;
  1129.                 if(!apmib_set(MIB_WLAN_WAPI_CA_INIT,(void *)&init))
  1130.                     printf("set MIB_WLAN_WAPI_CA_INIT error\n");
  1131.                 apmib_update(CURRENT_SETTING);
  1132.             }
  1133.             return 0;
  1134.         }
  1135. #else  
  1136.     else if(0==strcmp(argv[argNum],"wapi-check"))
  1137.     {
  1138.         //printf("WAPI Support Not Enabled\n");
  1139.         return 0;
  1140.     }
  1141. #endif
  1142. #ifdef WLAN_FAST_INIT
  1143.         else if ( !strcmp(argv[argNum], "set_mib") ) {
  1144.             if ((argNum+1) < argc) {
  1145.                 if (memcmp(argv[++argNum], "wlan", 4)) {
  1146.                     printf("Miss wlan_interface argument!\n");
  1147.                     return -1;
  1148.                 }
  1149. #ifdef MBSSID
  1150.                 if (strlen(argv[argNum]) >= 9 && argv[argNum][5] == '-' &&
  1151.                         argv[argNum][6] == 'v' && argv[argNum][7] == 'a') {
  1152.                     idx = atoi(&argv[argNum][8]);
  1153.                     if (idx >= NUM_VWLAN_INTERFACE) {
  1154.                         printf("invalid virtual wlan interface index number!\n");
  1155.                         return 0;
  1156.                     }
  1157.                     vwlan_idx = idx+1;
  1158.                 }
  1159. #ifdef UNIVERSAL_REPEATER
  1160.                 if (strlen(argv[argNum]) >= 9 && argv[argNum][5] == '-' &&
  1161.                         !memcmp(&argv[argNum][6], "vxd", 3)) {
  1162.                     vwlan_idx = NUM_VWLAN_INTERFACE;
  1163.                 }
  1164. #endif
  1165. #endif
  1166.                 wlan_idx = atoi(&argv[argNum][4]);
  1167.                 if (wlan_idx >= NUM_WLAN_INTERFACE) {
  1168.                     printf("invalid wlan interface index number!\n");
  1169.                     goto error_return;
  1170.                 }
  1171.                
  1172. #ifdef MBSSID
  1173.                 int ret;
  1174. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  1175. #if defined(CONFIG_RTL_92D_SUPPORT)
  1176.                 char tmpstr[10];
  1177.                 sprintf(tmpstr,"wlan%d",wlan_idx);
  1178.                
  1179.                 SetWlan_idx(argv[argNum]);
  1180.                 ret = comapi_initWlan(argv[argNum]);
  1181.                 SetWlan_idx(tmpstr);
  1182. #else
  1183.                 ret = comapi_initWlan(argv[argNum]);
  1184. #endif
  1185. #else
  1186.                 ret = initWlan(argv[argNum]);
  1187. #endif
  1188.                 vwlan_idx = 0;
  1189.                 return ret;
  1190. #else
  1191.                 return initWlan(argv[argNum]);
  1192. #endif
  1193.  
  1194.             }
  1195.             else
  1196.                 printf("Miss arguments [wlan_interface]!\n");
  1197.             return -1;
  1198.         }
  1199. #endif
  1200. #ifdef HOME_GATEWAY
  1201.         else if ( !strcmp(argv[argNum], "gen-pppoe") ) { // generate pppoe config file
  1202.             if ((argNum+3) < argc) {
  1203.                 return generatePPPConf(1, argv[argNum+1], argv[argNum+2], argv[argNum+3]);
  1204.             }
  1205.             else {
  1206.                 printf("Miss arguments [option-file pap-file chap-file]!\n");
  1207.                 return -1;
  1208.             }
  1209.         }
  1210.         else if ( !strcmp(argv[argNum], "gen-pptp") ) { // generate pptp config file
  1211.             if ((argNum+3) < argc) {
  1212.                 return generatePPPConf(0, argv[argNum+1], argv[argNum+2], argv[argNum+3]);
  1213.             }
  1214.             else {
  1215.                 printf("Miss arguments [option-file pap-file chap-file]!\n");
  1216.                 return -1;
  1217.             }
  1218.         }
  1219.         else if ( !strcmp(argv[argNum], "clearppp") )   //To send PPP LCP(Termination Request)
  1220.         {
  1221.             int sessid = atoi(argv[++argNum]);
  1222.             unsigned char * pppServMacPtr=(unsigned char *)argv[++argNum];
  1223.             return send_ppp_msg(sessid, pppServMacPtr);
  1224.         }
  1225. #if defined(RTL_L2TP_POWEROFF_PATCH)//patch for l2tp by jiawenjian
  1226.         else if ( !strcmp(argv[argNum], "clearl2tp"))
  1227.         {
  1228.             unsigned short l2tp_ns = atoi(argv[++argNum]);
  1229.             int l2tp_buf_length = atoi(argv[++argNum]);
  1230.             unsigned char * lanIp = (unsigned char *)argv[++argNum];
  1231.             unsigned char * serverIp = (unsigned char *)argv[++argNum];
  1232.             unsigned char * l2tp_Buff = (unsigned char *)argv[++argNum];
  1233.                    
  1234.             return send_l2tp_msg(l2tp_ns, l2tp_buf_length, l2tp_Buff, lanIp, serverIp);
  1235.         }
  1236. #endif
  1237.  
  1238. #endif     
  1239.  
  1240.     else if ( !strcmp(argv[argNum], "settime") ) {
  1241.             return setSystemTime_flash();          
  1242.         }
  1243.  
  1244.  
  1245. #ifdef WIFI_SIMPLE_CONFIG
  1246.         else if ( !strcmp(argv[argNum], "upd-wsc-conf") ) {
  1247. #if defined(UNIVERSAL_REPEATER) && defined(CONFIG_REPEATER_WPS_SUPPORT)
  1248.             return updateWscConf(argv[argNum+1], argv[argNum+2], 0, argv[argNum+3]);
  1249. #else
  1250.             return updateWscConf(argv[argNum+1], argv[argNum+2], 0);           
  1251. #endif //#if defined(UNIVERSAL_REPEATER) && defined(CONFIG_REPEATER_WPS_SUPPORT)           
  1252.         }
  1253. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  1254.         else if ( !strcmp(argv[argNum], "def-wsc-conf") ) {
  1255.             return defaultWscConf(argv[argNum+1], argv[argNum+2], 0);          
  1256.         }
  1257. #endif
  1258.         else if ( !strcmp(argv[argNum], "gen-pin") ) {
  1259. #if defined(UNIVERSAL_REPEATER) && defined(CONFIG_REPEATER_WPS_SUPPORT)
  1260.             char strtmp[10];
  1261.             sprintf(strtmp,"wlan%d",wlan_idx);
  1262. //printf("\r\n strtmp=[%s],__[%s-%u]\r\n",strtmp,__FILE__,__LINE__);           
  1263.             return updateWscConf(0, 0, 1, strtmp);
  1264. #else          
  1265.             return updateWscConf(0, 0, 1);         
  1266. #endif         
  1267.         }      
  1268. #endif // WIFI_SIMPLE_CONFIG
  1269.     else if ( !strcmp(argv[argNum], "gethw") ) {
  1270.             action = 4;
  1271.             if (++argNum < argc) {
  1272.                 if (argc > 3 && !memcmp(argv[argNum], "wlan", 4)) {
  1273.                     int idx;
  1274.                     idx = atoi(&argv[argNum++][4]);
  1275.                     if (idx >= NUM_WLAN_INTERFACE) {
  1276.                         printf("invalid wlan interface index number!\n");
  1277.                         return 0;
  1278.                     }
  1279.                     wlan_idx = idx;
  1280.                 }
  1281.                 sscanf(argv[argNum], "%s", mib);
  1282.                 while (++argNum < argc) {
  1283.                     sscanf(argv[argNum], "%s", valueArray[valNum]);
  1284.                     value[valNum] = valueArray[valNum];
  1285.                     valNum++;
  1286.                 }
  1287.                 value[valNum]= NULL;
  1288.             }
  1289.         }else if ( !strcmp(argv[argNum], "sethw") ) {
  1290.             action = 5;
  1291.             if (++argNum < argc) {
  1292.                 if (argc > 4 && !memcmp(argv[argNum], "wlan", 4)) {
  1293.                     int idx;
  1294.                     idx = atoi(&argv[argNum++][4]);
  1295.                     if (idx >= NUM_WLAN_INTERFACE) {
  1296.                         printf("invalid wlan interface index number!\n");
  1297.                         return 0;
  1298.                     }
  1299.                     wlan_idx = idx;
  1300.                 }
  1301.                 sscanf(argv[argNum], "%s", mib);
  1302.                 while (++argNum < argc) {
  1303.                     sscanf(argv[argNum], "%s", valueArray[valNum]);
  1304.                     value[valNum] = valueArray[valNum];
  1305.                     valNum++;
  1306.                 }
  1307.                 value[valNum]= NULL;
  1308.             }
  1309.         }else if ( !strcmp(argv[argNum], "allhw") ) {
  1310.             action = 6;
  1311.         }
  1312.     }
  1313.  
  1314.     if ( action == 0) {
  1315.         showHelp();
  1316.         goto error_return;
  1317.     }
  1318.     if ( (action==1 && !mib[0]) ||
  1319.          (action==2 && !mib[0]) ) {
  1320.         showAllMibName();
  1321.         goto error_return;
  1322.     }
  1323.  
  1324.     if ( action==2 && (!strcmp(mib, "MACAC_ADDR") || !strcmp(mib, "DEF_MACAC_ADDR"))) {
  1325.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1326.             showSetACHelp();
  1327.             goto error_return;
  1328.         }
  1329.         if ( (!strcmp(value[0], "del") && !value[1]) || (!strcmp(value[0], "add") && !value[1]) ) {
  1330.             showSetACHelp();
  1331.             goto error_return;
  1332.         }
  1333.     }
  1334.  
  1335. #if defined(CONFIG_RTK_MESH) && defined(_MESH_ACL_ENABLE_) // below code copy above ACL code
  1336.     if ( action==2 && (!strcmp(mib, "MESH_ACL_ADDR") || !strcmp(mib, "DEF_MESH_ACL_ADDR"))) {
  1337.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1338.             showSetMeshACLHelp();
  1339.             goto error_return;
  1340.         }
  1341.         if ( (!strcmp(value[0], "del") && !value[1]) || (!strcmp(value[0], "add") && !value[1]) ) {
  1342.             showSetMeshACLHelp();
  1343.             goto error_return;
  1344.         }
  1345.     }
  1346. #endif
  1347. #if defined(VLAN_CONFIG_SUPPORTED)
  1348.  
  1349.     if ( action==2 && (!strcmp(mib, "VLANCONFIG_TBL") || !strcmp(mib, "DEF_VLANCONFIG_TBL")) ) {
  1350.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1351.             showSetVlanConfigHelp();
  1352.             goto error_return;
  1353.         }
  1354.     }
  1355. #endif 
  1356.    
  1357.  
  1358.     if ( action==2 && (!strcmp(mib, "WDS") || !strcmp(mib, "DEF_WDS"))) {
  1359.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1360.             showSetWdsHelp();
  1361.             goto error_return;
  1362.         }
  1363.         if ( (!strcmp(value[0], "del") && !value[1]) || (!strcmp(value[0], "add") && !value[1]) ) {
  1364.             showSetWdsHelp();
  1365.             goto error_return;
  1366.         }
  1367.     }
  1368.  
  1369. #ifdef HOME_GATEWAY
  1370.     if ( action==2 && (!strcmp(mib, "PORTFW_TBL") || !strcmp(mib, "DEF_PORTFW_TBL"))) {
  1371.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1372.             showSetPortFwHelp();
  1373.             goto error_return;
  1374.         }
  1375.     }
  1376.     if ( action==2 && (!strcmp(mib, "PORTFILTER_TBL") || !strcmp(mib, "DEF_PORTFILTER_TBL")) ) {
  1377.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1378.             showSetPortFilterHelp();
  1379.             goto error_return;
  1380.         }
  1381.     }
  1382.     if ( action==2 && (!strcmp(mib, "IPFILTER_TBL") || !strcmp(mib, "DEF_IPFILTER_TBL"))) {
  1383.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1384.             showSetIpFilterHelp();
  1385.             goto error_return;
  1386.         }
  1387.     }
  1388.     if ( action==2 && (!strcmp(mib, "MACFILTER_TBL") || !strcmp(mib, "DEF_MACFILTER_TBL")) ) {
  1389.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1390.             showSetMacFilterHelp();
  1391.             goto error_return;
  1392.         }
  1393.     }
  1394.     if ( action==2 && (!strcmp(mib, "URLFILTER_TBL") || !strcmp(mib, "DEF_URLFILTER_TBL")) ) {
  1395.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1396.             showSetUrlFilterHelp();
  1397.             goto error_return;
  1398.         }
  1399.     }
  1400.     if ( action==2 && (!strcmp(mib, "TRIGGERPORT_TBL") || !strcmp(mib, "DEF_TRIGGERPORT_TBL")) ) {
  1401.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1402.             showSetTriggerPortHelp();
  1403.             goto error_return;
  1404.         }
  1405.     }
  1406. #ifdef ROUTE_SUPPORT
  1407.     if ( action==2 && (!strcmp(mib, "STATICROUTE_TBL") || !strcmp(mib, "DEF_STATICROUTE_TBL")) ) {
  1408.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1409.             showSetStaticRouteHelp();
  1410.             goto error_return;
  1411.         }
  1412.     }
  1413. #endif //ROUTE
  1414. #endif
  1415. #ifdef HOME_GATEWAY
  1416. #ifdef VPN_SUPPORT
  1417.     if ( action==2 && (!strcmp(mib, "IPSECTUNNEL_TBL") || !strcmp(mib, "DEF_IPSECTUNNEL_TBL")) ) {
  1418.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1419.             showSetIpsecTunnelHelp();
  1420.             goto error_return;
  1421.         }
  1422.     }
  1423. #endif
  1424. #endif
  1425. #ifdef TLS_CLIENT
  1426.     if ( action==2 && (!strcmp(mib, "CERTROOT_TBL") || !strcmp(mib, "DEF_CERTROOT_TBL")) ) {
  1427.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1428.             showSetCertRootHelp();
  1429.             goto error_return;
  1430.         }
  1431.     }
  1432.     if ( action==2 && (!strcmp(mib, "CERTUSER_TBL") || !strcmp(mib, "DEF_CERTUSER_TBL")) ) {
  1433.         if (!valNum || (strcmp(value[0], "add") && strcmp(value[0], "del") && strcmp(value[0], "delall"))) {
  1434.             showSetCertUserHelp();
  1435.             goto error_return;
  1436.         }
  1437.     }  
  1438. #endif
  1439.  
  1440.     switch (action) {
  1441.     case 1: // get
  1442.  
  1443. #ifdef PARSE_TXT_FILE
  1444.         if ( filename[0] ) {
  1445.             if ( parseTxtConfig(filename, &apmib) < 0) {
  1446.                 printf("Parse text file error!\n");
  1447.                 goto error_return;
  1448.             }
  1449.  
  1450.             if ( !apmib_init(&apmib)) {
  1451.                 printf("Initialize AP MIB failed!\n");
  1452.                 goto error_return;
  1453.             }
  1454.         }
  1455.         else
  1456.  
  1457. #endif
  1458.         if ( !apmib_init()) {
  1459.             printf("Initialize AP MIB failed!\n");
  1460.             goto error_return;
  1461.         }
  1462.  
  1463.         idx = searchMIB(mib);
  1464.  
  1465.         if ( idx == -1 ) {
  1466.             showHelp();
  1467.             showAllMibName();
  1468.             goto error_return;
  1469.         }
  1470.         num = 1;
  1471.  
  1472.         if (config_area == DEF_MIB_WLAN_AREA || config_area == MIB_WLAN_AREA) { // wlan default or current
  1473.             if (mib_wlan_table[idx].id == MIB_WLAN_MACAC_ADDR)
  1474.                 APMIB_GET(MIB_WLAN_MACAC_NUM, (void *)&num);
  1475.             else if (mib_wlan_table[idx].id == MIB_WLAN_WDS)
  1476.                 APMIB_GET(MIB_WLAN_WDS_NUM, (void *)&num);
  1477.         }
  1478. #if defined(CONFIG_RTK_MESH) && defined(_MESH_ACL_ENABLE_) // below code copy above ACL code
  1479. #if 0
  1480.         else if (config_area == MIB_AREA) { // mib_table
  1481.             if (mib_table[idx].id == MIB_MESH_ACL_ADDR)
  1482.             {              
  1483.                 APMIB_GET(MIB_MESH_ACL_NUM, (void *)&num);
  1484.             }
  1485.            
  1486.         }
  1487. #else
  1488.         else if (mib_table[idx].id == MIB_MESH_ACL_ADDR)
  1489.             APMIB_GET(MIB_MESH_ACL_NUM, (void *)&num);     
  1490. #endif
  1491.  
  1492. #endif
  1493. #ifdef HOME_GATEWAY
  1494.         else if (!strcmp(mib, "PORTFW_TBL"))
  1495.             APMIB_GET(MIB_PORTFW_TBL_NUM, (void *)&num);
  1496.         else if (!strcmp(mib, "PORTFILTER_TBL"))
  1497.             APMIB_GET(MIB_PORTFILTER_TBL_NUM, (void *)&num);
  1498.         else if (!strcmp(mib, "IPFILTER_TBL"))
  1499.             APMIB_GET(MIB_IPFILTER_TBL_NUM, (void *)&num);
  1500.         else if (!strcmp(mib, "MACFILTER_TBL"))
  1501.             APMIB_GET(MIB_MACFILTER_TBL_NUM, (void *)&num);
  1502.         else if (!strcmp(mib, "URLFILTER_TBL"))
  1503.             APMIB_GET(MIB_URLFILTER_TBL_NUM, (void *)&num);
  1504.         else if (!strcmp(mib, "TRIGGERPORT_TBL"))
  1505.             APMIB_GET(MIB_TRIGGERPORT_TBL_NUM, (void *)&num);
  1506.  
  1507. #if defined(GW_QOS_ENGINE) || defined(QOS_BY_BANDWIDTH)
  1508.         else if (!strcmp(mib, "QOS_RULE_TBL"))
  1509.             APMIB_GET(MIB_QOS_RULE_TBL_NUM, (void *)&num);
  1510. #endif
  1511.  
  1512. #ifdef ROUTE_SUPPORT
  1513.         else if (!strcmp(mib, "STATICROUTE_TBL"))
  1514.             APMIB_GET(MIB_STATICROUTE_TBL_NUM, (void *)&num);
  1515. #endif //ROUTE
  1516. #endif
  1517.  
  1518. #ifdef HOME_GATEWAY
  1519. #ifdef VPN_SUPPORT
  1520.         else if (!strcmp(mib, "IPSECTUNNEL_TBL"))
  1521.             APMIB_GET(MIB_IPSECTUNNEL_TBL_NUM, (void *)&num);
  1522. #endif
  1523. #endif
  1524.  
  1525. #ifdef TLS_CLIENT
  1526.         else if (!strcmp(mib, "CERTROOT_TBL"))
  1527.             APMIB_GET(MIB_CERTROOT_TBL_NUM, (void *)&num);
  1528.         else if (!strcmp(mib, "CERTUSER_TBL"))
  1529.             APMIB_GET(MIB_CERTUSER_TBL_NUM, (void *)&num);         
  1530. #endif
  1531.         else if (!strcmp(mib, "DHCPRSVDIP_TBL"))
  1532.             APMIB_GET(MIB_DHCPRSVDIP_TBL_NUM, (void *)&num);
  1533. #if defined(VLAN_CONFIG_SUPPORTED)
  1534.         else if (!strcmp(mib, "VLANCONFIG_TBL"))
  1535.         {
  1536.             APMIB_GET(MIB_VLANCONFIG_TBL_NUM, (void *)&num);
  1537.         }
  1538. #endif
  1539.  
  1540.         if (config_area == HW_MIB_AREA)
  1541.             pTbl = hwmib_table;
  1542.         else if (config_area == HW_MIB_WLAN_AREA)
  1543.             pTbl = hwmib_wlan_table;
  1544.         else if (config_area == DEF_MIB_AREA || config_area == MIB_AREA)
  1545.             pTbl = mib_table;
  1546.         else
  1547.             pTbl = mib_wlan_table;
  1548.  
  1549.         getMIB(mib, pTbl[idx].id, pTbl[idx].type, num, 1 ,value);
  1550.         break;
  1551.  
  1552.     case 2: // set
  1553.         if ( !apmib_init()) {
  1554.             printf("Initialize AP MIB failed!\n");
  1555.             goto error_return;
  1556.         }
  1557.         idx = searchMIB(mib);
  1558.         if ( idx == -1 ) {
  1559.             showHelp();
  1560.             showAllMibName();
  1561.             goto error_return;
  1562.         }
  1563.         if ( valNum < 1) {
  1564.             showHelp();
  1565.             goto error_return;
  1566.         }
  1567.         if (config_area == HW_MIB_AREA)
  1568.             pTbl = hwmib_table;
  1569.         else if (config_area == HW_MIB_WLAN_AREA)
  1570.             pTbl = hwmib_wlan_table;
  1571.         else if (config_area ==DEF_MIB_AREA  || config_area == MIB_AREA)
  1572.             pTbl = mib_table;
  1573.         else
  1574.             pTbl = mib_wlan_table;
  1575.         setMIB(mib, pTbl[idx].id, pTbl[idx].type, pTbl[idx].size, valNum, value);
  1576.         break;
  1577.  
  1578.     case 3: // all
  1579.         dumpAll();
  1580.         break;
  1581.     case 4: // gethw
  1582.         if ( !apmib_init_HW()) {
  1583.             printf("Initialize AP HW MIB failed!\n");
  1584.             return -1;
  1585.         }
  1586.         idx = searchMIB(mib);
  1587.         if ( idx == -1 ) {
  1588.             showHelp();
  1589.             showAllHWMibName();
  1590.             return -1;
  1591.         }
  1592.         num = 1;
  1593.         if (config_area == HW_MIB_AREA)
  1594.             pTbl = hwmib_table;
  1595.         else if (config_area == HW_MIB_WLAN_AREA)
  1596.             pTbl = hwmib_wlan_table;
  1597.         getMIB(mib, pTbl[idx].id, pTbl[idx].type, num, 1 ,value);
  1598.         break;
  1599.        
  1600.         case 5: // sethw
  1601.         if ( !apmib_init_HW()) {
  1602.             printf("Initialize AP MIB failed!\n");
  1603.             return -1;
  1604.         }
  1605.         idx = searchMIB(mib);
  1606.         if ( idx == -1 ) {
  1607.             showHelp();
  1608.             showAllHWMibName();
  1609.             return -1;
  1610.         }
  1611.         if ( valNum < 1) {
  1612.             showHelp();
  1613.             return -1;
  1614.         }
  1615.         if (config_area == HW_MIB_AREA)
  1616.             pTbl = hwmib_table;
  1617.         else if (config_area == HW_MIB_WLAN_AREA)
  1618.             pTbl = hwmib_wlan_table;
  1619.         setMIB(mib, pTbl[idx].id, pTbl[idx].type, pTbl[idx].size, valNum, value);
  1620.         break;
  1621.         case 6: // allhw
  1622.         dumpAllHW();
  1623.         break; 
  1624.     }
  1625.  
  1626. normal_return:
  1627.     vwlan_idx = 0;
  1628.  
  1629.     return 0;
  1630.  
  1631. error_return:
  1632.     vwlan_idx = 0;
  1633.    
  1634.     return -1;
  1635. }
  1636.  
  1637. //////////////////////////////////////////////////////////////////////////////////
  1638. /*
  1639. static unsigned char getWLAN_ChipVersion()
  1640. {
  1641.     FILE *stream;
  1642.     typedef enum { CHIP_UNKNOWN=0, CHIP_RTL8188C=1, CHIP_RTL8192C=2} CHIP_VERSION_T;
  1643.     CHIP_VERSION_T chipVersion = CHIP_UNKNOWN; 
  1644.    
  1645.     stream = fopen ( "/proc/wlan0/mib_rf", "r" );
  1646.     if ( stream != NULL )
  1647.     {      
  1648.         char *strtmp;
  1649.         char line[100];
  1650.                                  
  1651.         while (fgets(line, sizeof(line), stream))
  1652.         {
  1653.            
  1654.             strtmp = line;
  1655.             while(*strtmp == ' ')
  1656.             {
  1657.                 strtmp++;
  1658.             }
  1659.            
  1660.  
  1661.             if(strstr(strtmp,"RTL8192SE") != 0)
  1662.             {
  1663.                 chipVersion = CHIP_UNKNOWN;
  1664.             }
  1665.             else if(strstr(strtmp,"RTL8188C") != 0)
  1666.             {
  1667.                 chipVersion = CHIP_RTL8188C;
  1668.             }
  1669.             else if(strstr(strtmp,"RTL8192C") != 0)
  1670.             {
  1671.                 chipVersion = CHIP_RTL8192C;
  1672.             }
  1673.         }          
  1674.         fclose ( stream );
  1675.     }
  1676.  
  1677.     return chipVersion;
  1678.  
  1679.  
  1680. }
  1681. */
  1682.  
  1683. #ifdef CONFIG_RTL_92D_SUPPORT
  1684. PHYBAND_TYPE_T wlanPhyBandDef[] = {PHYBAND_5G, PHYBAND_2G}; /* phybandcheck */
  1685. #else
  1686. PHYBAND_TYPE_T wlanPhyBandDef[] = {PHYBAND_2G, PHYBAND_2G}; /* phybandcheck */
  1687. #endif
  1688.  
  1689. static int writeDefault(int isAll)
  1690. {   PARAM_HEADER_T header;
  1691.     APMIB_T mib;
  1692.     APMIB_Tp pMib=&mib;
  1693.     HW_SETTING_T hwmib;
  1694.     char *data=NULL;
  1695.     int status, fh, offset, i, idx;
  1696.     unsigned char checksum;
  1697. //  unsigned char buff[sizeof(APMIB_T)+sizeof(checksum)+1];
  1698.     unsigned char *buff;
  1699. #ifdef VLAN_CONFIG_SUPPORTED
  1700.     int vlan_entry=0;
  1701. #endif
  1702. #ifdef COMPRESS_MIB_SETTING
  1703.     //unsigned char* pContent = NULL;
  1704.  
  1705.     //COMPRESS_MIB_HEADER_T compHeader;
  1706.     //unsigned char *expPtr, *compPtr;
  1707.     //unsigned int expLen = header.len+sizeof(PARAM_HEADER_T);
  1708.     //unsigned int compLen;
  1709.     //unsigned int real_size = 0;
  1710.     //int zipRate=0;
  1711. #endif // #ifdef COMPRESS_MIB_SETTING
  1712.  
  1713. #ifdef MIB_TLV
  1714.     unsigned char *pfile = NULL;
  1715.     unsigned char *mib_tlv_data = NULL;
  1716.     unsigned int tlv_content_len = 0;
  1717.     unsigned int mib_tlv_max_len = 0;
  1718. #endif
  1719.  
  1720.     char hw_setting_start[6];  
  1721.     int comp_hw_setting = 1;
  1722.     int vlanIdx = 0;
  1723.  
  1724.     //buff=calloc(1, 0x6000);
  1725.     buff=calloc(1, HW_SETTING_SECTOR_LEN + DEFAULT_SETTING_SECTOR_LEN +
  1726.         CURRENT_SETTING_SECTOR_LEN);
  1727.     if ( buff == NULL ) {
  1728.         printf("Allocate buffer failed!\n");
  1729.         return -1;
  1730.     }
  1731.  
  1732. #ifdef __mips__
  1733.     fh = open(FLASH_DEVICE_NAME, O_RDWR);
  1734. #endif
  1735.  
  1736. #ifdef __i386__
  1737.     fh = open(FLASH_DEVICE_NAME, O_RDWR|O_CREAT|O_TRUNC);
  1738.     write(fh, buff, HW_SETTING_SECTOR_LEN + DEFAULT_SETTING_SECTOR_LEN +
  1739.         CURRENT_SETTING_SECTOR_LEN);
  1740.     //write(fh, buff, 0x6000);
  1741. #endif
  1742.     if ( fh == -1 ) {
  1743.         printf("create file failed!\n");
  1744.         return -1;
  1745.     }
  1746.  
  1747.     if (isAll) {
  1748.         // write hw setting
  1749.         sprintf((char *)header.signature, "%s%02d", HW_SETTING_HEADER_TAG, HW_SETTING_VER);
  1750.         header.len = sizeof(hwmib) + sizeof(checksum);
  1751.  
  1752.         memset((char *)&hwmib, '\0', sizeof(hwmib));
  1753.         hwmib.boardVer = 1;
  1754. #if defined(CONFIG_RTL_8196B)
  1755.         memcpy(hwmib.nic0Addr, "\x0\xe0\x4c\x81\x96\xb1", 6);
  1756.         memcpy(hwmib.nic1Addr, "\x0\xe0\x4c\x81\x96", 6);
  1757.         hwmib.nic1Addr[5] = 0xb1 + NUM_WLAN_MULTIPLE_SSID;
  1758. #elif defined(CONFIG_RTL_8196C) || defined(CONFIG_RTL_8198)
  1759.             memcpy(hwmib.nic0Addr, "\x0\xe0\x4c\x81\x96\xc1", 6);
  1760.         memcpy(hwmib.nic1Addr, "\x0\xe0\x4c\x81\x96", 6);
  1761.         hwmib.nic1Addr[5] = 0xc1 + NUM_WLAN_MULTIPLE_SSID;
  1762. #else
  1763. //!CONFIG_RTL_8196B =>rtl8651c+rtl8190
  1764.         memcpy(hwmib.nic0Addr, "\x0\xe0\x4c\x86\x51\xd1", 6);
  1765.         memcpy(hwmib.nic1Addr, "\x0\xe0\x4c\x86\x51", 6);
  1766.         hwmib.nic1Addr[5] = 0xd1 + NUM_WLAN_MULTIPLE_SSID;     
  1767. #endif 
  1768.         // set RF parameters
  1769.         for (idx=0; idx<NUM_WLAN_INTERFACE; idx++) {
  1770. #if defined(CONFIG_RTL_8196B)  
  1771.             memcpy(hwmib.wlan[idx].macAddr, "\x0\xe0\x4c\x81\x96", 5);     
  1772.             hwmib.wlan[idx].macAddr[5] = 0xb1 + idx;           
  1773.            
  1774.             memcpy(hwmib.wlan[idx].macAddr1, "\x0\xe0\x4c\x81\x96", 5);    
  1775.             hwmib.wlan[idx].macAddr1[5] = 0xb1 + 1;
  1776.            
  1777.             memcpy(hwmib.wlan[idx].macAddr2, "\x0\xe0\x4c\x81\x96", 5);    
  1778.             hwmib.wlan[idx].macAddr2[5] = 0xb1 + 2;
  1779.            
  1780.             memcpy(hwmib.wlan[idx].macAddr3, "\x0\xe0\x4c\x81\x96", 5);    
  1781.             hwmib.wlan[idx].macAddr3[5] = 0xb1 + 3;
  1782.            
  1783.             memcpy(hwmib.wlan[idx].macAddr4, "\x0\xe0\x4c\x81\x96", 5);    
  1784.             hwmib.wlan[idx].macAddr4[5] = 0xb1 + 4;        
  1785.  
  1786.             memcpy(hwmib.wlan[idx].macAddr5, "\x0\xe0\x4c\x81\x96", 5);    
  1787.             hwmib.wlan[idx].macAddr5[5] = 0xb1 + 5;
  1788.            
  1789.             memcpy(hwmib.wlan[idx].macAddr6, "\x0\xe0\x4c\x81\x96", 5);    
  1790.             hwmib.wlan[idx].macAddr6[5] = 0xb1 + 6;
  1791.            
  1792.             memcpy(hwmib.wlan[idx].macAddr7, "\x0\xe0\x4c\x81\x96", 5);    
  1793.             hwmib.wlan[idx].macAddr7[5] = 0xb1 + 7;    
  1794.             hwmib.wlan[idx].regDomain = FCC;
  1795.             hwmib.wlan[idx].rfType = 10;
  1796.             hwmib.wlan[idx].xCap = 0;
  1797.             hwmib.wlan[idx].Ther = 0;
  1798.             for (i=0; i<MAX_CCK_CHAN_NUM; i++)
  1799.                 hwmib.wlan[idx].txPowerCCK[i] = 0;
  1800.             for (i=0; i<MAX_OFDM_CHAN_NUM; i++)
  1801.                 hwmib.wlan[idx].txPowerOFDM_HT_OFDM_1S[i] = 0;
  1802.             for (i=0; i<MAX_OFDM_CHAN_NUM; i++)
  1803.                 hwmib.wlan[idx].txPowerOFDM_HT_OFDM_2S[i] = 0;
  1804.             hwmib.wlan[idx].LOFDMPwDiffA = 0;
  1805.             hwmib.wlan[idx].LOFDMPwDiffB = 0;
  1806.             hwmib.wlan[idx].TSSI1 = 0;
  1807.             hwmib.wlan[idx].TSSI2 = 0;
  1808. #elif defined(CONFIG_RTL_8196C) || defined(CONFIG_RTL_8198)
  1809.             memcpy(hwmib.wlan[idx].macAddr, "\x0\xe0\x4c\x81\x96", 5);     
  1810.             hwmib.wlan[idx].macAddr[5] = 0xc1 + (idx*16);          
  1811.            
  1812.             memcpy(hwmib.wlan[idx].macAddr1, "\x0\xe0\x4c\x81\x96", 5);    
  1813.             hwmib.wlan[idx].macAddr1[5] = hwmib.wlan[idx].macAddr[5] + 1;  
  1814.            
  1815.             memcpy(hwmib.wlan[idx].macAddr2, "\x0\xe0\x4c\x81\x96", 5);    
  1816.             hwmib.wlan[idx].macAddr2[5] = hwmib.wlan[idx].macAddr[5] + 2;  
  1817.            
  1818.             memcpy(hwmib.wlan[idx].macAddr3, "\x0\xe0\x4c\x81\x96", 5);    
  1819.             hwmib.wlan[idx].macAddr3[5] = hwmib.wlan[idx].macAddr[5] + 3;  
  1820.            
  1821.             memcpy(hwmib.wlan[idx].macAddr4, "\x0\xe0\x4c\x81\x96", 5);    
  1822.             hwmib.wlan[idx].macAddr4[5] = hwmib.wlan[idx].macAddr[5] + 4;          
  1823.  
  1824.             memcpy(hwmib.wlan[idx].macAddr5, "\x0\xe0\x4c\x81\x96", 5);    
  1825.             hwmib.wlan[idx].macAddr5[5] = hwmib.wlan[idx].macAddr[5] + 5;  
  1826.            
  1827.             memcpy(hwmib.wlan[idx].macAddr6, "\x0\xe0\x4c\x81\x96", 5);    
  1828.             hwmib.wlan[idx].macAddr6[5] = hwmib.wlan[idx].macAddr[5] + 6;  
  1829.            
  1830.             memcpy(hwmib.wlan[idx].macAddr7, "\x0\xe0\x4c\x81\x96", 5);    
  1831.             hwmib.wlan[idx].macAddr7[5] = hwmib.wlan[idx].macAddr[5] + 7;      
  1832.             hwmib.wlan[idx].regDomain = FCC;
  1833.             hwmib.wlan[idx].rfType = 10;
  1834.             hwmib.wlan[idx].xCap = 0;
  1835.             hwmib.wlan[idx].Ther = 0;
  1836. /*
  1837. #if defined(CONFIG_RTL_8196C)
  1838.                         hwmib.wlan[idx].trswitch = 0;
  1839. #elif defined(CONFIG_RTL_8198)
  1840.                         hwmib.wlan[idx].trswitch = 1;
  1841. #endif 
  1842. */
  1843.             for (i=0; i<MAX_2G_CHANNEL_NUM_MIB; i++)
  1844.                 hwmib.wlan[idx].pwrlevelCCK_A[i] = 0;
  1845.                
  1846.             for (i=0; i<MAX_2G_CHANNEL_NUM_MIB; i++)
  1847.                 hwmib.wlan[idx].pwrlevelCCK_B[i] = 0;
  1848.                
  1849.             for (i=0; i<MAX_2G_CHANNEL_NUM_MIB; i++)
  1850.                 hwmib.wlan[idx].pwrlevelHT40_1S_A[i] = 0;  
  1851.                
  1852.             for (i=0; i<MAX_2G_CHANNEL_NUM_MIB; i++)
  1853.                 hwmib.wlan[idx].pwrlevelHT40_1S_B[i] = 0;  
  1854.                
  1855.             for (i=0; i<MAX_2G_CHANNEL_NUM_MIB; i++)
  1856.                 hwmib.wlan[idx].pwrdiffHT40_2S[i] = 0; 
  1857.                
  1858.             for (i=0; i<MAX_2G_CHANNEL_NUM_MIB; i++)
  1859.                 hwmib.wlan[idx].pwrdiffHT20[i] = 0;
  1860.                
  1861.             for (i=0; i<MAX_2G_CHANNEL_NUM_MIB; i++)
  1862.                 hwmib.wlan[idx].pwrdiffOFDM[i] = 0;
  1863.                
  1864.             hwmib.wlan[idx].TSSI1 = 0;
  1865.             hwmib.wlan[idx].TSSI2 = 0;
  1866.            
  1867.             for (i=0; i<MAX_5G_CHANNEL_NUM_MIB; i++)
  1868.                 hwmib.wlan[idx].pwrlevel5GHT40_1S_A[i] = 0;
  1869.             for (i=0; i<MAX_5G_CHANNEL_NUM_MIB; i++)
  1870.                 hwmib.wlan[idx].pwrlevel5GHT40_1S_B[i] = 0;
  1871.             for (i=0; i<MAX_5G_CHANNEL_NUM_MIB; i++)
  1872.                 hwmib.wlan[idx].pwrdiff5GHT40_2S[i] = 0;       
  1873.             for (i=0; i<MAX_5G_CHANNEL_NUM_MIB; i++)
  1874.                 hwmib.wlan[idx].pwrdiff5GHT20[i] = 0;
  1875.             for (i=0; i<MAX_5G_CHANNEL_NUM_MIB; i++)
  1876.                 hwmib.wlan[idx].pwrdiff5GOFDM[i] = 0;  
  1877.  
  1878.             hwmib.wlan[idx].trswpape_C9 = 0;//0xAA;
  1879.             hwmib.wlan[idx].trswpape_CC = 0;//0xAF;
  1880.            
  1881.             hwmib.wlan[idx].target_pwr = 0;
  1882.  
  1883. #else
  1884. //!CONFIG_RTL_8196B => rtl8651c+rtl8190
  1885.             memcpy(hwmib.wlan[idx].macAddr, "\x0\xe0\x4c\x86\x51", 5);     
  1886.             hwmib.wlan[idx].macAddr[5] = 0xd1 + idx;       
  1887.             memcpy(hwmib.wlan[idx].macAddr1, "\x0\xe0\x4c\x86\x51", 5);    
  1888.             hwmib.wlan[idx].macAddr1[5] = 0xd1 + 1;
  1889.             memcpy(hwmib.wlan[idx].macAddr2, "\x0\xe0\x4c\x86\x51", 5);    
  1890.             hwmib.wlan[idx].macAddr2[5] = 0xd1 + 2;
  1891.             memcpy(hwmib.wlan[idx].macAddr3, "\x0\xe0\x4c\x86\x51", 5);    
  1892.             hwmib.wlan[idx].macAddr3[5] = 0xd1 + 3;
  1893.             memcpy(hwmib.wlan[idx].macAddr4, "\x0\xe0\x4c\x86\x51", 5);    
  1894.             hwmib.wlan[idx].macAddr4[5] = 0xd1 + 4;
  1895.             hwmib.wlan[idx].regDomain = FCC;
  1896.             //hwmib.wlan[idx].rfType = (unsigned char)RF_ZEBRA;
  1897.             hwmib.wlan[idx].rfType = 10;
  1898.             hwmib.wlan[idx].edType = 3;
  1899.                            
  1900.             hwmib.wlan[idx].antDiversity = (unsigned char)0; // disabled
  1901.             hwmib.wlan[idx].txAnt = 0;
  1902.             hwmib.wlan[idx].initGain = 4;
  1903.             hwmib.wlan[idx].xCap = 0;
  1904.             hwmib.wlan[idx].LOFDMPwDiff = 0;
  1905. //          hwmib.wlan[idx].AntPwDiff_B = 0;
  1906.             hwmib.wlan[idx].AntPwDiff_C = 0;
  1907. //          hwmib.wlan[idx].AntPwDiff_D = 0;
  1908.             hwmib.wlan[idx].TherRFIC = 0;
  1909.             //strcpy(hwmib.wlan[idx].wscPin, "12345670"); //move to hw setting
  1910.             for (i=0; i<MAX_CCK_CHAN_NUM; i++)
  1911.                 hwmib.wlan[idx].txPowerCCK[i] = 0;
  1912.             for (i=0; i<MAX_OFDM_CHAN_NUM; i++)
  1913.                 hwmib.wlan[idx].txPowerOFDM[i] = 0;
  1914. #endif
  1915.             }
  1916. #ifdef MIB_TLV
  1917. #if 1  
  1918.     if ( flash_read(hw_setting_start, HW_SETTING_OFFSET, 6)==0 ) {
  1919.             printf("Read hw setting header failed!\n");                
  1920.     }
  1921.     else
  1922.     {
  1923.         //printf("===,the start char from HW_SETTING_OFFSET is %c%c%c%c%c%c\n",
  1924.         //      hw_setting_start[0], hw_setting_start[1], hw_setting_start[2],
  1925.         //      hw_setting_start[3], hw_setting_start[4], hw_setting_start[5]);
  1926.  
  1927.         if (!memcmp(hw_setting_start, HW_SETTING_HEADER_TAG, TAG_LEN) ||
  1928.             !memcmp(hw_setting_start, HW_SETTING_HEADER_FORCE_TAG, TAG_LEN) ||
  1929.             !memcmp(hw_setting_start, HW_SETTING_HEADER_UPGRADE_TAG, TAG_LEN))
  1930.         {
  1931.             comp_hw_setting = 0;
  1932.         }
  1933.         else
  1934.         {
  1935.             comp_hw_setting = 1;
  1936.         }
  1937.     }
  1938. #endif
  1939.  
  1940.         if(comp_hw_setting == 1)
  1941.         {
  1942. //mib_display_data_content(HW_SETTING, &hwmib, sizeof(HW_SETTING_T));  
  1943.  
  1944.         mib_tlv_max_len = mib_get_setting_len(HW_SETTING)*4;
  1945.  
  1946.         pfile = malloc(mib_tlv_max_len);
  1947.         if(pfile != NULL && mib_tlv_save(HW_SETTING, (void*)&hwmib, pfile, &tlv_content_len) == 1)
  1948.         {
  1949.  
  1950.             mib_tlv_data = malloc(tlv_content_len+1); // 1:checksum
  1951.             if(mib_tlv_data != NULL)
  1952.             {
  1953.                 memcpy(mib_tlv_data, pfile, tlv_content_len);
  1954.             }
  1955.                
  1956.             free(pfile);
  1957.  
  1958.         }
  1959.        
  1960.         if(mib_tlv_data != NULL)
  1961.         {
  1962.             header.len = tlv_content_len+1;
  1963.             data = (char *)mib_tlv_data;
  1964.             checksum = CHECKSUM((unsigned char *)data, header.len-1);
  1965.             data[tlv_content_len] = CHECKSUM((unsigned char *)data, tlv_content_len);
  1966. //mib_display_tlv_content(HW_SETTING, data, header.len);               
  1967.         }
  1968.         }
  1969.         else
  1970.         {
  1971. #endif //#ifdef MIB_TLV
  1972.         data = (char *)&hwmib;
  1973.         checksum = CHECKSUM((unsigned char *)data, header.len-1);
  1974. #ifdef MIB_TLV
  1975.         }
  1976. #endif
  1977.         lseek(fh, HW_SETTING_OFFSET, SEEK_SET);
  1978. #ifdef COMPRESS_MIB_SETTING
  1979.         if(comp_hw_setting == 1)
  1980.         {
  1981.         if(flash_mib_compress_write(HW_SETTING, data, &header, &checksum) == 1)
  1982.         {
  1983.             COMP_TRACE(stderr,"\r\n flash_mib_compress_write HW_SETTING DONE, __[%s-%u]", __FILE__,__LINE__);          
  1984.         }
  1985.         }
  1986.         else
  1987.         {
  1988.  
  1989. #endif //#ifdef COMPRESS_MIB_SETTING       
  1990.         if ( write(fh, (const void *)&header, sizeof(header))!=sizeof(header) ) {
  1991.             printf("write hs header failed!\n");
  1992.             return -1;
  1993.         }
  1994.         if ( write(fh, (const void *)&hwmib, sizeof(hwmib))!=sizeof(hwmib) ) {
  1995.             printf("write hs MIB failed!\n");
  1996.             return -1;
  1997.         }
  1998.         if ( write(fh, (const void *)&checksum, sizeof(checksum))!=sizeof(checksum) ) {
  1999.             printf("write hs checksum failed!\n");
  2000.             return -1;
  2001.         }
  2002. #ifdef COMPRESS_MIB_SETTING
  2003.         }
  2004. #endif 
  2005.  
  2006. #ifdef MIB_TLV
  2007.         if(comp_hw_setting == 1)
  2008.         {
  2009.         if(mib_tlv_data != NULL)
  2010.             free(mib_tlv_data);
  2011.         }
  2012. #endif
  2013.         close(fh);
  2014.         sync();
  2015.         fh = open(FLASH_DEVICE_NAME, O_RDWR);
  2016.     }
  2017.     // write default & current setting
  2018.     memset(pMib, '\0', sizeof(APMIB_T));
  2019.  
  2020.     // give a initial value for testing purpose
  2021. #if defined(CONFIG_RTL_8196B)  
  2022.     strcpy((char *)pMib->deviceName, "RTL8196b");
  2023. #elif defined(CONFIG_RTL_8196C) || defined(CONFIG_RTL_8198)
  2024.     strcpy((char *)pMib->deviceName, "RTL8196c");
  2025. #else
  2026.     strcpy((char *)pMib->deviceName, "RTL865x");
  2027. #endif
  2028.  
  2029.     strcpy((char *)pMib->domainName,"Realtek");
  2030.  
  2031.     for (idx=0; idx<NUM_WLAN_INTERFACE; idx++) {
  2032.         for (i=0; i<(NUM_VWLAN_INTERFACE+1); i++) {    
  2033. #if defined(CONFIG_RTL_92D_SUPPORT) && !defined(CONFIG_RTL_92D_DMDP)
  2034.             if (idx!=0)
  2035.                 pMib->wlan[idx][i].wlanDisabled =1;
  2036. #endif
  2037. #ifdef MBSSID
  2038.                        if ((i > 0) && (i<=4))
  2039.                        {
  2040.                             if(0 == idx)
  2041.                                 sprintf((char *)pMib->wlan[idx][i].ssid, "RTK 11n AP VAP%d", i);
  2042.                     else
  2043.                     sprintf((char *)pMib->wlan[idx][i].ssid, "RTK 11n AP %d VAP%d", idx,i);
  2044.                        }                   
  2045.                        else if(i == 0)
  2046. #endif         
  2047.               {
  2048. #ifdef FOR_DUAL_BAND
  2049.                     if(0 == idx)
  2050.                         strcpy(pMib->wlan[idx][i].ssid, "RTK 11n AP 5G" );
  2051.                     else if(1 == idx)
  2052.                         strcpy(pMib->wlan[idx][i].ssid, "RTK 11n AP 2.4G" );
  2053.                     else
  2054.                         sprintf(pMib->wlan[idx][i].ssid, "RTK 11n AP %d",idx);
  2055. #else
  2056.                     if(0 == idx)
  2057.                                 strcpy((char *)pMib->wlan[idx][i].ssid, "RTK 11n AP" );
  2058.                     else
  2059.                     sprintf((char *)pMib->wlan[idx][i].ssid, "RTK 11n AP %d",idx);
  2060. #endif
  2061.               }                
  2062.             if (i == 0)
  2063.             {  
  2064. #ifdef CONFIG_RTL_92D_SUPPORT
  2065.                 if(wlanPhyBandDef[idx] == PHYBAND_5G) {
  2066.                     pMib->wlan[idx][i].controlsideband = 1; // lower
  2067.                     pMib->wlan[idx][i].channel = 44;
  2068.                     pMib->wlan[idx][i].basicRates = TX_RATE_6M|TX_RATE_9M|TX_RATE_12M|TX_RATE_18M|TX_RATE_24M|
  2069.                             TX_RATE_36M|TX_RATE_48M|TX_RATE_54M;               
  2070.                     pMib->wlan[idx][i].supportedRates = TX_RATE_6M|TX_RATE_9M|TX_RATE_12M|TX_RATE_18M|TX_RATE_24M|
  2071.                             TX_RATE_36M|TX_RATE_48M|TX_RATE_54M;
  2072.                 } else
  2073. #endif
  2074.                 {
  2075.                     pMib->wlan[idx][i].channel = 11;
  2076.                     pMib->wlan[idx][i].basicRates = TX_RATE_1M|TX_RATE_2M|TX_RATE_5M|TX_RATE_11M;              
  2077.                     pMib->wlan[idx][i].supportedRates = TX_RATE_1M|TX_RATE_2M|TX_RATE_5M |TX_RATE_11M|
  2078.                         TX_RATE_6M|TX_RATE_9M|TX_RATE_12M|TX_RATE_18M|TX_RATE_24M|
  2079.                             TX_RATE_36M|TX_RATE_48M|TX_RATE_54M;
  2080.                 }
  2081.                 pMib->wlan[idx][i].wep = WEP_DISABLED;
  2082.                 pMib->wlan[idx][i].beaconInterval = 100;
  2083.                 pMib->wlan[idx][i].wepKeyType=1;
  2084.                 pMib->wlan[idx][i].protectionDisabled=1;
  2085.                 strcpy((char *)pMib->wlan[idx][i].wdsWepKey, "0000000000" );       
  2086.                 pMib->wlan[idx][i].rtsThreshold = 2347;
  2087.                 pMib->wlan[idx][i].wpa2Cipher = (unsigned char)WPA_CIPHER_AES; //Keith
  2088.             }
  2089.             if(i <= 4)
  2090.             {
  2091.                 pMib->wlan[idx][i].rateAdaptiveEnabled = 1;
  2092.  
  2093. #ifdef CONFIG_RTL_92D_SUPPORT              
  2094.                 if(wlanPhyBandDef[idx] == PHYBAND_5G)
  2095.                     pMib->wlan[idx][i].wlanBand = BAND_11A | BAND_11N;
  2096.                 else
  2097. #endif
  2098.                     pMib->wlan[idx][i].wlanBand = BAND_11BG | BAND_11N;
  2099.  
  2100.                 pMib->wlan[idx][i].wmmEnabled = 1;
  2101.             }
  2102.             pMib->wlan[idx][i].preambleType = LONG_PREAMBLE;
  2103.            
  2104.             //pMib->wlan[idx][i].fragThreshold = 2346;
  2105.             //pMib->wlan[idx][i].authType = AUTH_BOTH;
  2106.             //pMib->wlan[idx][i].inactivityTime = 30000; // 300 sec
  2107.             //pMib->wlan[idx][i].dtimPeriod = 1;
  2108.             if(i>4)
  2109.             {
  2110.                 pMib->wlan[idx][i].wpaGroupRekeyTime = 86400;
  2111.                 pMib->wlan[idx][i].wpaAuth = (unsigned char)WPA_AUTH_PSK;
  2112.                 pMib->wlan[idx][i].wpaCipher = (unsigned char)WPA_CIPHER_AES; //Keith
  2113.                 pMib->wlan[idx][i].wpa2Cipher = (unsigned char)WPA_CIPHER_AES; //Keith
  2114.             }
  2115.             pMib->wlan[idx][i].rsPort = 1812;
  2116.             pMib->wlan[idx][i].accountRsPort = 0;
  2117.             pMib->wlan[idx][i].accountRsUpdateDelay = 0;
  2118.             pMib->wlan[idx][i].rsMaxRetry = 3;
  2119.             pMib->wlan[idx][i].rsIntervalTime = 5;
  2120.             pMib->wlan[idx][i].accountRsMaxRetry = 0;
  2121.             pMib->wlan[idx][i].accountRsIntervalTime = 0;
  2122.        
  2123.             if (i > 0)
  2124.                 pMib->wlan[idx][i].wlanDisabled = 1;       
  2125.  
  2126. #ifdef WLAN_EASY_CONFIG
  2127.             pMib->wlan[idx][i].acfMode = MODE_BUTTON;
  2128.             pMib->wlan[idx][i].acfAlgReq = ACF_ALGORITHM_WPA2_AES;
  2129.             pMib->wlan[idx][i].acfAlgSupp = ACF_ALGORITHM_WPA_TKIP  | ACF_ALGORITHM_WPA2_AES;
  2130.             strcpy(pMib->wlan[idx][i].acfScanSSID, "REALTEK_EASY_CONFIG");
  2131. #endif
  2132.  
  2133. #ifdef WIFI_SIMPLE_CONFIG
  2134.    
  2135.             //strcpy(pMib->wlan[idx][i].wscSsid, pMib->wlan[idx][i].ssid ); //must be the same as pMib->wlan[idx].ssid
  2136.             pMib->wlan[idx][i].wscDisable = 0;
  2137. #endif
  2138.             // for 11n         
  2139.             if (i == 0)
  2140.             {
  2141.                 pMib->wlan[idx][i].aggregation = 1;
  2142.                 pMib->wlan[idx][i].channelbonding = 1;
  2143.                 pMib->wlan[idx][i].shortgiEnabled = 1;
  2144.                 pMib->wlan[idx][i].fragThreshold=2346; 
  2145.                 pMib->wlan[idx][i].authType = AUTH_BOTH;
  2146.                 pMib->wlan[idx][i].inactivityTime = 30000; // 300 sec
  2147.                 pMib->wlan[idx][i].dtimPeriod = 1;
  2148.                 pMib->wlan[idx][i].wpaGroupRekeyTime = 86400;
  2149.                 pMib->wlan[idx][i].wpaAuth = (unsigned char)WPA_AUTH_PSK;
  2150.                 pMib->wlan[idx][i].wpaCipher = (unsigned char)WPA_CIPHER_AES; //Keith  
  2151.                 pMib->wlan[idx][i].wscMethod = 3;
  2152.                 //strcpy(pMib->wlan[idx].wscPin, "12345670"); //move to hw setting
  2153.                 pMib->wlan[idx][i].wscAuth = WSC_AUTH_OPEN; //open
  2154.                 pMib->wlan[idx][i].wscEnc = WSC_ENCRYPT_NONE; //open
  2155.                 pMib->wlan[idx][i].wscUpnpEnabled = 1;
  2156.                 pMib->wlan[idx][i].wscRegistrarEnabled = 1;
  2157.                 #if defined(NEW_SCHEDULE_SUPPORT)
  2158.                 pMib->wlan[idx][i].scheduleRuleNum = MAX_SCHEDULE_NUM;
  2159.                 #endif
  2160.             }          
  2161.             pMib->wlan[idx][i].phyBandSelect = wlanPhyBandDef[idx];
  2162. #if defined(CONFIG_RTL_92D_SUPPORT) && defined(CONFIG_RTL_92D_DMDP) && !defined(CONFIG_RTL_DUAL_PCIESLOT_BIWLAN_D)
  2163.             pMib->wlan[idx][i].macPhyMode = DMACDPHY;
  2164. #else  
  2165.             pMib->wlan[idx][i].macPhyMode = SMACSPHY;
  2166. #endif
  2167.             pMib->wlan[idx][i].STBCEnabled = 0;
  2168.             pMib->wlan[idx][i].CoexistEnabled= 0;
  2169.         }
  2170.     }
  2171.  
  2172. #if defined(CONFIG_RTL_819X)
  2173.     pMib->wifiSpecific = 2; //Brad modify for 11n wifi test
  2174. #endif 
  2175.     pMib->ipAddr[0] = 192;
  2176.     pMib->ipAddr[1] = 168;
  2177.     pMib->ipAddr[2] = 1;
  2178.     pMib->ipAddr[3] = 254;
  2179.  
  2180.     pMib->subnetMask[0] = 255;
  2181.     pMib->subnetMask[1] = 255;
  2182.     pMib->subnetMask[2] = 255;
  2183.     pMib->subnetMask[3] = 0;
  2184.  
  2185.     pMib->dhcpClientStart[0] = 192;
  2186.     pMib->dhcpClientStart[1] = 168;
  2187.     pMib->dhcpClientStart[2] = 1;
  2188.     pMib->dhcpClientStart[3] = 100;
  2189.  
  2190.     pMib->dhcpClientEnd[0] = 192;
  2191.     pMib->dhcpClientEnd[1] = 168;
  2192.     pMib->dhcpClientEnd[2] = 1;
  2193.     pMib->dhcpClientEnd[3] = 200;
  2194. #if defined(CONFIG_DOMAIN_NAME_QUERY_SUPPORT)
  2195.     pMib->dhcp = DHCP_AUTO;
  2196. #else
  2197.     pMib->dhcp = DHCP_SERVER;
  2198. #endif
  2199.  
  2200.     strcpy((char *)pMib->superName, "super");
  2201.     strcpy((char *)pMib->superPassword, "super");
  2202.  
  2203.    
  2204. #if defined(CONFIG_RTL_8198_AP_ROOT)
  2205.     pMib->ntpEnabled=0;
  2206.     pMib->daylightsaveEnabled=0;
  2207.     strcpy(pMib->ntpTimeZone,"-8 4");
  2208.     pMib->ntpServerIp1[0]=0;
  2209.     pMib->ntpServerIp1[1]=0;
  2210.     pMib->ntpServerIp1[2]=0;
  2211.     pMib->ntpServerIp1[3]=0;
  2212.     pMib->ntpServerIp2[0]=0;
  2213.     pMib->ntpServerIp2[1]=0;
  2214.     pMib->ntpServerIp2[2]=0;
  2215.     pMib->ntpServerIp2[3]=0;
  2216. #endif
  2217. #ifdef HOME_GATEWAY
  2218.     pMib->qosAutoUplinkSpeed=1;
  2219.     pMib->qosManualUplinkSpeed=512;
  2220.     pMib->qosAutoDownLinkSpeed=1;
  2221.     pMib->qosManualDownLinkSpeed=512;
  2222.    
  2223.     pMib->vpnPassthruIPsecEnabled=1;
  2224.     pMib->vpnPassthruPPTPEnabled=1;
  2225.     pMib->vpnPassthruL2TPEnabled=1;
  2226.  
  2227.     pMib->wanIpAddr[0] = 172;
  2228.     pMib->wanIpAddr[1] = 1;
  2229.     pMib->wanIpAddr[2] = 1;
  2230.     pMib->wanIpAddr[3] = 1;
  2231.  
  2232.     pMib->wanSubnetMask[0] = 255;
  2233.     pMib->wanSubnetMask[1] = 255;
  2234.     pMib->wanSubnetMask[2] = 255;
  2235.     pMib->wanSubnetMask[3] = 0;
  2236.  
  2237.     pMib->wanDefaultGateway[0] = 172;
  2238.     pMib->wanDefaultGateway[1] = 1;
  2239.     pMib->wanDefaultGateway[2] = 1;
  2240.     pMib->wanDefaultGateway[3] = 254;
  2241.  
  2242.     pMib->wanDhcp = DHCP_CLIENT;
  2243.     pMib->dnsMode = DNS_AUTO;
  2244.     pMib->pppIdleTime = 300;
  2245.     pMib->pptpIdleTime = 300;
  2246.     pMib->l2tpIdleTime = 300;
  2247.     strcpy((char *)pMib->usb3g_idleTime, "300");
  2248.     strcpy((char *)pMib->usb3g_connType, "0");
  2249.     pMib->pppMtuSize = 1452;
  2250.     pMib->pptpIpAddr[0]=172;
  2251.     pMib->pptpIpAddr[1]=1;
  2252.     pMib->pptpIpAddr[2]=1;
  2253.     pMib->pptpIpAddr[3]=2;
  2254.     pMib->pptpSubnetMask[0]=255;
  2255.     pMib->pptpSubnetMask[1]=255;
  2256.     pMib->pptpSubnetMask[2]=255;
  2257.     pMib->pptpSubnetMask[3]=0; 
  2258.     pMib->pptpServerIpAddr[0]=172;
  2259.     pMib->pptpServerIpAddr[1]=1;
  2260.     pMib->pptpServerIpAddr[2]=1;
  2261.     pMib->pptpServerIpAddr[3]=1;           
  2262.     pMib->pptpMtuSize = 1460;
  2263.     pMib->l2tpIpAddr[0]=172;
  2264.     pMib->l2tpIpAddr[1]=1;
  2265.     pMib->l2tpIpAddr[2]=1;
  2266.     pMib->l2tpIpAddr[3]=2;
  2267.     pMib->l2tpSubnetMask[0]=255;
  2268.     pMib->l2tpSubnetMask[1]=255;
  2269.     pMib->l2tpSubnetMask[2]=255;
  2270.     pMib->l2tpSubnetMask[3]=0;
  2271.     pMib->l2tpServerIpAddr[0]=172;
  2272.     pMib->l2tpServerIpAddr[1]=1;
  2273.     pMib->l2tpServerIpAddr[2]=1;
  2274.     pMib->l2tpServerIpAddr[3]=1;   
  2275.     pMib->l2tpMtuSize = 1460; /* keith: add l2tp support. 20080515 */
  2276.     pMib->L2tpwanIPMode = 1; /* keith: add l2tp support. 20080515 */
  2277.     strcpy((char *)pMib->usb3g_mtuSize,"1490");
  2278.     pMib->fixedIpMtuSize = 1500;
  2279.     pMib->dhcpMtuSize = 1492;
  2280.     pMib->ntpEnabled=0;
  2281.     pMib->daylightsaveEnabled=0;
  2282.     strcpy((char *)pMib->ntpTimeZone,"-8 4");
  2283.     pMib->ntpServerIp1[0]=0;
  2284.     pMib->ntpServerIp1[1]=0;
  2285.     pMib->ntpServerIp1[2]=0;
  2286.     pMib->ntpServerIp1[3]=0;
  2287.     pMib->ntpServerIp2[0]=0;
  2288.     pMib->ntpServerIp2[1]=0;
  2289.     pMib->ntpServerIp2[2]=0;
  2290.     pMib->ntpServerIp2[3]=0;
  2291.    
  2292.     pMib->ddnsEnabled=0;
  2293.     pMib->ddnsType=0;
  2294.     strcpy((char *)pMib->ddnsDomainName,"host.dyndns.org");
  2295.  
  2296.    
  2297.  
  2298. #ifdef CONFIG_IPV6
  2299.     pMib->radvdCfgParam.enabled =0;
  2300.     strcpy(pMib->radvdCfgParam.interface.Name,"br0");
  2301.     pMib->radvdCfgParam.interface.MaxRtrAdvInterval=600;
  2302.     pMib->radvdCfgParam.interface.MinRtrAdvInterval=600*0.33;
  2303.     pMib->radvdCfgParam.interface.MinDelayBetweenRAs=3;
  2304.     pMib->radvdCfgParam.interface.AdvManagedFlag=0;
  2305.     pMib->radvdCfgParam.interface.AdvOtherConfigFlag=0;
  2306.     pMib->radvdCfgParam.interface.AdvLinkMTU=1500;
  2307.     pMib->radvdCfgParam.interface.AdvReachableTime=0;
  2308.     pMib->radvdCfgParam.interface.AdvRetransTimer=0;
  2309.     pMib->radvdCfgParam.interface.AdvCurHopLimit=64;
  2310.     pMib->radvdCfgParam.interface.AdvDefaultLifetime=600*3;
  2311.     strcpy(pMib->radvdCfgParam.interface.AdvDefaultPreference,"medium");
  2312.     pMib->radvdCfgParam.interface.AdvSourceLLAddress=1;
  2313.     pMib->radvdCfgParam.interface.UnicastOnly=0;
  2314.                 /*prefix 1*/
  2315.     pMib->radvdCfgParam.interface.prefix[0].Prefix[0]=0x2001;
  2316.     pMib->radvdCfgParam.interface.prefix[0].Prefix[1]=0x0;
  2317.     pMib->radvdCfgParam.interface.prefix[0].Prefix[2]=0x0;
  2318.     pMib->radvdCfgParam.interface.prefix[0].Prefix[3]=0x0;
  2319.     pMib->radvdCfgParam.interface.prefix[0].Prefix[4]=0x0;
  2320.     pMib->radvdCfgParam.interface.prefix[0].Prefix[5]=0x0;
  2321.     pMib->radvdCfgParam.interface.prefix[0].Prefix[6]=0x0;
  2322.     pMib->radvdCfgParam.interface.prefix[0].Prefix[7]=0x0;
  2323.     pMib->radvdCfgParam.interface.prefix[0].PrefixLen=64;
  2324.     pMib->radvdCfgParam.interface.prefix[0].AdvOnLinkFlag=1;
  2325.     pMib->radvdCfgParam.interface.prefix[0].AdvAutonomousFlag=1;
  2326.     pMib->radvdCfgParam.interface.prefix[0].AdvValidLifetime=2592000;
  2327.     pMib->radvdCfgParam.interface.prefix[0].AdvPreferredLifetime=604800;
  2328.     pMib->radvdCfgParam.interface.prefix[0].AdvRouterAddr=0;
  2329.     strcpy(pMib->radvdCfgParam.interface.prefix[0].if6to4,"eth1");
  2330.     pMib->radvdCfgParam.interface.prefix[0].enabled=1;
  2331.                 /*prefix 2*/
  2332.     pMib->radvdCfgParam.interface.prefix[1].Prefix[0]=0x2002;
  2333.     pMib->radvdCfgParam.interface.prefix[1].Prefix[1]=0;
  2334.     pMib->radvdCfgParam.interface.prefix[1].Prefix[2]=0;
  2335.     pMib->radvdCfgParam.interface.prefix[1].Prefix[3]=0;
  2336.     pMib->radvdCfgParam.interface.prefix[1].Prefix[4]=0;
  2337.     pMib->radvdCfgParam.interface.prefix[1].Prefix[5]=0;
  2338.     pMib->radvdCfgParam.interface.prefix[1].Prefix[6]=0;
  2339.     pMib->radvdCfgParam.interface.prefix[1].Prefix[7]=0;
  2340.     pMib->radvdCfgParam.interface.prefix[1].PrefixLen=64;
  2341.     pMib->radvdCfgParam.interface.prefix[1].AdvOnLinkFlag=1;
  2342.     pMib->radvdCfgParam.interface.prefix[1].AdvAutonomousFlag=1;
  2343.     pMib->radvdCfgParam.interface.prefix[1].AdvValidLifetime=2592000;
  2344.     pMib->radvdCfgParam.interface.prefix[1].AdvPreferredLifetime=604800;
  2345.     pMib->radvdCfgParam.interface.prefix[1].AdvRouterAddr=0;
  2346. //  pMib->radvdCfgParam.interface.prefix[1].if6to4="";
  2347.     pMib->radvdCfgParam.interface.prefix[1].enabled=1;
  2348.  
  2349. //dnsv6
  2350.     pMib->dnsCfgParam.enabled=0;
  2351.     strcpy(pMib->dnsCfgParam.routerName,"router.my");
  2352. //dhcp6
  2353.     pMib->dhcp6sCfgParam.enabled=0;
  2354.     strcpy(pMib->dhcp6sCfgParam.DNSaddr6,"2001:db8::35");
  2355.     strcpy(pMib->dhcp6sCfgParam.addr6PoolS,"2001:db8:1:2::1000");
  2356.     strcpy(pMib->dhcp6sCfgParam.addr6PoolE,"2001:db8:1:2::2000");
  2357.     strcpy(pMib->dhcp6sCfgParam.interfaceNameds,"br0");
  2358. //ipv6 addr
  2359.     pMib->addrIPv6CfgParam.enabled=0;
  2360.     pMib->addrIPv6CfgParam.prefix_len[0]=0;
  2361.     pMib->addrIPv6CfgParam.prefix_len[1]=0;
  2362.     {
  2363.         int itemp;
  2364.         for(itemp=0;itemp<8;itemp++)
  2365.         {
  2366.             pMib->addrIPv6CfgParam.addrIPv6[0][itemp]=0;
  2367.             pMib->addrIPv6CfgParam.addrIPv6[1][itemp]=0;
  2368.         }
  2369.     }
  2370. //6to4
  2371.     pMib->tunnelCfgParam.enabled=0;
  2372. #endif
  2373. #endif
  2374.  
  2375. // added by rock /////////////////////////////////////////
  2376. #ifdef VOIP_SUPPORT
  2377.     flash_voip_default(&pMib->voipCfgParam);
  2378. #endif
  2379.  
  2380.     // SNMP, Forrest added, 2007.10.25.
  2381. #ifdef CONFIG_SNMP
  2382.     pMib->snmpEnabled = 1;
  2383.     sprintf(pMib->snmpName, "%s", "Realtek");
  2384.     sprintf(pMib->snmpLocation, "%s", "AP");
  2385.     sprintf(pMib->snmpContact, "%s", "Router");
  2386.     sprintf(pMib->snmpRWCommunity, "%s", "private");
  2387.     sprintf(pMib->snmpROCommunity, "%s", "public");
  2388.     pMib->snmpTrapReceiver1[0] = 0;
  2389.     pMib->snmpTrapReceiver1[1] = 0;
  2390.     pMib->snmpTrapReceiver1[2] = 0;
  2391.     pMib->snmpTrapReceiver1[3] = 0;
  2392.     pMib->snmpTrapReceiver2[0] = 0;
  2393.     pMib->snmpTrapReceiver2[1] = 0;
  2394.     pMib->snmpTrapReceiver2[2] = 0;
  2395.     pMib->snmpTrapReceiver2[3] = 0;
  2396.     pMib->snmpTrapReceiver3[0] = 0;
  2397.     pMib->snmpTrapReceiver3[1] = 0;
  2398.     pMib->snmpTrapReceiver3[2] = 0;
  2399.     pMib->snmpTrapReceiver3[3] = 0;
  2400. #endif
  2401.  
  2402. #ifdef CONFIG_RTK_MESH
  2403.     pMib->meshEnabled = 0;
  2404. #ifdef CONFIG_NEW_MESH_UI
  2405.     pMib->meshRootEnabled = 0; 
  2406. #else
  2407.     pMib->meshRootEnabled = 0;      // if meshEnabled default value "1", Here "1" also
  2408. #endif
  2409.     pMib->meshMaxNumOfNeighbors = 32;
  2410.     strcpy(pMib->meshID, "RTK-mesh");
  2411.    
  2412. #ifdef  _11s_TEST_MODE_
  2413.     pMib->meshTestParam6 = 43627 ;
  2414.     pMib->meshTestParam7 = 48636 ;
  2415.     pMib->meshTestParam8 = 42090 ;
  2416.     pMib->meshTestParam9 = 43627 ;
  2417.     pMib->meshTestParama = 42606 ;
  2418.     pMib->meshTestParamb = 47016 ;
  2419.     pMib->meshTestParamc = 57710 ;
  2420.     pMib->meshTestParamd = 46323 ;
  2421.     pMib->meshTestParame = 47016 ;
  2422.     pMib->meshTestParamf = 47811 ;
  2423. #endif
  2424.  
  2425. #endif // CONFIG_RTK_MESH
  2426. #if defined(HOME_GATEWAY) && defined(ROUTE_SUPPORT)
  2427.     pMib->natEnabled = 1;
  2428. #endif
  2429.  
  2430.  
  2431. #ifdef HOME_GATEWAY
  2432. #if defined(CONFIG_APP_SAMBA)
  2433.     pMib->sambaEnabled = 1;
  2434. #else
  2435.     pMib->sambaEnabled = 0;
  2436. #endif
  2437.  
  2438.     pMib->pppSessionNum = 0;
  2439. #endif
  2440.  
  2441. #ifdef SNMP_SUPPORT
  2442.     strcpy(pMib->snmpROcommunity,"public");
  2443.     strcpy(pMib->snmpRWcommunity,"private");
  2444. #endif
  2445. #if defined(VLAN_CONFIG_SUPPORTED)
  2446. for(vlan_entry=0;vlan_entry<MAX_IFACE_VLAN_CONFIG;vlan_entry++){
  2447.     pMib->VlanConfigArray[vlan_entry].enabled=0;
  2448.     pMib->VlanConfigArray[vlan_entry].vlanId = 1;
  2449.     pMib->VlanConfigArray[vlan_entry].cfi = 0;
  2450. }  
  2451. pMib->VlanConfigEnabled=0;
  2452. pMib->VlanConfigNum=MAX_IFACE_VLAN_CONFIG;
  2453. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "eth0");
  2454. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "eth2");
  2455. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "eth3");
  2456. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "eth4");
  2457. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "wlan0");
  2458. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "wlan0-va0");
  2459. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "wlan0-va1");
  2460. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "wlan0-va2");
  2461. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "wlan0-va3");
  2462. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "wlan1");
  2463. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "wlan1-va0");
  2464. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "wlan1-va1");
  2465. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "wlan1-va2");
  2466. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "wlan1-va3");
  2467. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "eth1");
  2468. #if defined(CONFIG_RTL_8198_AP_ROOT) && defined(GMII_ENABLED)
  2469. sprintf((char *)pMib->VlanConfigArray[vlanIdx++].netIface, "%s", "eth6");
  2470. #endif
  2471.  
  2472. vlanIdx = 0;
  2473. pMib->VlanConfigArray[vlanIdx++].vlanId = 3022;
  2474. pMib->VlanConfigArray[vlanIdx].priority = 7;
  2475. pMib->VlanConfigArray[vlanIdx++].vlanId = 3030;
  2476. pMib->VlanConfigArray[vlanIdx].priority = 0;
  2477. pMib->VlanConfigArray[vlanIdx++].vlanId = 500;
  2478. pMib->VlanConfigArray[vlanIdx].priority = 3;
  2479. #endif
  2480.  
  2481. #if defined(GW_QOS_ENGINE) || defined(QOS_BY_BANDWIDTH)
  2482. pMib->qosManualUplinkSpeed = 512;
  2483. pMib->qosManualDownLinkSpeed = 512;
  2484.  
  2485. #endif
  2486. #if defined(CONFIG_RTL_92D_SUPPORT)
  2487.   #if defined(CONFIG_RTL_92D_DMDP)
  2488.     pMib->wlanBand2G5GSelect = BANDMODEBOTH;
  2489.   #else
  2490.     pMib->wlanBand2G5GSelect = BANDMODE5G;
  2491.   #endif
  2492. #else
  2493.     pMib->wlanBand2G5GSelect = BANDMODE2G;
  2494. #endif
  2495.  
  2496. #if defined(CONFIG_RTL_8198_AP_ROOT)
  2497.     pMib->opMode=BRIDGE_MODE;
  2498. #endif
  2499.  
  2500.     data = (char *)pMib;
  2501.  
  2502.     // write default setting
  2503.  
  2504. #ifdef MIB_TLV
  2505.  
  2506. //mib_display_data_content(DEFAULT_SETTING, pMib, sizeof(APMIB_T));
  2507.  
  2508.     mib_tlv_max_len = mib_get_setting_len(DEFAULT_SETTING)*4;
  2509.  
  2510.     tlv_content_len = 0;
  2511.  
  2512.     pfile = malloc(mib_tlv_max_len);
  2513.     memset(pfile, 0x00, mib_tlv_max_len);
  2514.    
  2515.     if( pfile != NULL && mib_tlv_save(DEFAULT_SETTING, (void*)pMib, pfile, &tlv_content_len) == 1)
  2516.     {
  2517.  
  2518.         mib_tlv_data = malloc(tlv_content_len+1); // 1:checksum
  2519.         if(mib_tlv_data != NULL)
  2520.         {
  2521.             memcpy(mib_tlv_data, pfile, tlv_content_len);
  2522.         }
  2523.            
  2524.         free(pfile);
  2525.  
  2526.     }
  2527.    
  2528.     if(mib_tlv_data != NULL)
  2529.     {
  2530.        
  2531.         sprintf((char *)header.signature, "%s%02d", DEFAULT_SETTING_HEADER_TAG, DEFAULT_SETTING_VER);
  2532.         header.len = tlv_content_len+1;
  2533.         data = (char *)mib_tlv_data;
  2534.         checksum = CHECKSUM((unsigned char *)data, header.len-1);
  2535.         data[tlv_content_len] = CHECKSUM((unsigned char *)data, tlv_content_len);
  2536. //mib_display_tlv_content(DEFAULT_SETTING, data, header.len);
  2537.        
  2538.     }
  2539.     else
  2540.     {
  2541. #endif //#ifdef MIB_TLV
  2542.     data = (char *)pMib;   
  2543.     sprintf((char *)header.signature, "%s%02d", DEFAULT_SETTING_HEADER_TAG, DEFAULT_SETTING_VER);
  2544.     header.len = sizeof(APMIB_T) + sizeof(checksum);
  2545.     checksum = CHECKSUM((unsigned char *)data, header.len-1);
  2546.  
  2547. #ifdef MIB_TLV
  2548.     }
  2549. #endif
  2550.  
  2551.     lseek(fh, DEFAULT_SETTING_OFFSET, SEEK_SET);
  2552. #ifdef COMPRESS_MIB_SETTING
  2553.  
  2554. //fprintf(stderr,"\r\n header.len=%u, __[%s-%u]",header.len,__FILE__,__LINE__);
  2555.  
  2556.     if(flash_mib_compress_write(DEFAULT_SETTING, data, &header, &checksum) == 1)
  2557.     {
  2558.         COMP_TRACE(stderr,"\r\n flash_mib_compress_write DEFAULT_SETTING DONE, __[%s-%u]", __FILE__,__LINE__);         
  2559.     }
  2560.     else
  2561.     {
  2562.  
  2563. #endif //#ifdef COMPRESS_MIB_SETTING
  2564.    
  2565.     if ( write(fh, (const void *)&header, sizeof(header))!=sizeof(header) ) {
  2566.         printf("write ds header failed!\n");
  2567.         return -1;
  2568.     }
  2569.     if ( write(fh, (const void *)pMib, sizeof(mib))!=sizeof(mib) ) {
  2570.         printf("write ds MIB failed!\n");
  2571.         return -1;
  2572.     }
  2573.     if ( write(fh, (const void *)&checksum, sizeof(checksum))!=sizeof(checksum) ) {
  2574.         printf("write ds checksum failed!\n");
  2575.         return -1;
  2576.     }
  2577. #ifdef COMPRESS_MIB_SETTING
  2578.     }
  2579. #endif
  2580.  
  2581.     close(fh);
  2582.     sync();
  2583.    
  2584.     fh = open(FLASH_DEVICE_NAME, O_RDWR);
  2585.  
  2586.     // write current setting
  2587.  
  2588. #ifdef MIB_TLV
  2589.     if(mib_tlv_data != NULL)
  2590.     {
  2591.         sprintf((char *)header.signature, "%s%02d", CURRENT_SETTING_HEADER_TAG, CURRENT_SETTING_VER);
  2592.         header.len = tlv_content_len+1;
  2593.         checksum = CHECKSUM((unsigned char *)data, header.len-1);
  2594. //mib_display_tlv_content(CURRENT_SETTING, data, header.len);      
  2595.     }
  2596.     else
  2597.     {
  2598. #endif
  2599.     sprintf((char *)header.signature, "%s%02d", CURRENT_SETTING_HEADER_TAG, CURRENT_SETTING_VER);
  2600.     header.len = sizeof(APMIB_T) + sizeof(checksum);
  2601.  
  2602. #ifdef MIB_TLV
  2603.     }
  2604. #endif
  2605.  
  2606.     lseek(fh, CURRENT_SETTING_OFFSET, SEEK_SET);
  2607. #ifdef COMPRESS_MIB_SETTING
  2608.  
  2609.     if(flash_mib_compress_write(CURRENT_SETTING, data, &header, &checksum) == 1)
  2610.     {
  2611.         COMP_TRACE(stderr,"\r\n flash_mib_compress_write CURRENT_SETTING DONE, __[%s-%u]", __FILE__,__LINE__);         
  2612.     }
  2613.     else
  2614.     {
  2615.  
  2616. #endif //#ifdef COMPRESS_MIB_SETTING
  2617.     if ( write(fh, (const void *)&header, sizeof(header))!=sizeof(header) ) {
  2618.         printf("write cs header failed!\n");
  2619.         return -1;
  2620.     }
  2621.     if ( write(fh, (const void *)pMib, sizeof(mib))!=sizeof(mib) ) {
  2622.         printf("write cs MIB failed!\n");
  2623.         return -1;
  2624.     }
  2625.     if ( write(fh, (const void *)&checksum, sizeof(checksum))!=sizeof(checksum) ) {
  2626.         printf("write cs checksum failed!\n");
  2627.         return -1;
  2628.     }
  2629. #ifdef COMPRESS_MIB_SETTING
  2630.     }
  2631. #endif
  2632.  
  2633.     close(fh);
  2634.     sync();
  2635.  
  2636.     // check if hw, ds, cs checksum is ok
  2637.     offset = HW_SETTING_OFFSET;
  2638. #ifdef COMPRESS_MIB_SETTING
  2639.     if(comp_hw_setting == 1)
  2640.     {
  2641.  
  2642.     if(flash_mib_checksum_ok(offset) == 1)
  2643.     {
  2644.         COMP_TRACE(stderr,"\r\n HW_SETTING hecksum_ok\n");
  2645.     }
  2646.     }
  2647.     else
  2648.     {
  2649.         if(comp_hw_setting == 1)
  2650.         {
  2651.         fprintf(stderr,"flash mib checksum error!\n");
  2652.         }
  2653. #endif // #ifdef COMPRESS_MIB_SETTING  
  2654.     if ( flash_read((char *)&header, offset, sizeof(header)) == 0) {
  2655.         printf("read hs header failed!\n");
  2656.         return -1;
  2657.     }
  2658.     offset += sizeof(header);
  2659.     if ( flash_read((char *)buff, offset, header.len) == 0) {
  2660.         printf("read hs MIB failed!\n");
  2661.         return -1;
  2662.     }
  2663.     status = CHECKSUM_OK(buff, header.len);
  2664.     if ( !status) {
  2665.         printf("hs Checksum error!\n");
  2666.         return -1;
  2667.     }
  2668. #ifdef COMPRESS_MIB_SETTING
  2669.     }
  2670. #endif 
  2671.  
  2672.     offset = DEFAULT_SETTING_OFFSET;
  2673. #ifdef COMPRESS_MIB_SETTING
  2674.     if(flash_mib_checksum_ok(offset) == 1)
  2675.     {
  2676.         COMP_TRACE(stderr,"\r\n DEFAULT_SETTING hecksum_ok\n");
  2677.     }
  2678.     else
  2679.     {
  2680. #endif // #ifdef COMPRESS_MIB_SETTING  
  2681.     if ( flash_read((char *)&header, offset, sizeof(header)) == 0) {
  2682.         printf("read ds header failed!\n");
  2683.         return -1;
  2684.     }
  2685.     offset += sizeof(header);
  2686.     if ( flash_read((char *)buff, offset, header.len) == 0) {
  2687.         printf("read ds MIB failed!\n");
  2688.         return -1;
  2689.     }
  2690.     status = CHECKSUM_OK(buff, header.len);
  2691.     if ( !status) {
  2692.         printf("ds Checksum error!\n");
  2693.         return -1;
  2694.     }
  2695. #ifdef COMPRESS_MIB_SETTING
  2696.     }
  2697. #endif
  2698.  
  2699.     offset = CURRENT_SETTING_OFFSET;
  2700. #ifdef COMPRESS_MIB_SETTING
  2701.     if(flash_mib_checksum_ok(offset) == 1)
  2702.     {
  2703.         COMP_TRACE(stderr,"\r\n CURRENT_SETTING hecksum_ok\n");
  2704.     }
  2705.     else
  2706.     {
  2707. #endif // #ifdef COMPRESS_MIB_SETTING  
  2708.     if ( flash_read((char *)&header, offset, sizeof(header)) == 0) {
  2709.         printf("read cs header failed!\n");
  2710.         return -1;
  2711.     }
  2712.     offset += sizeof(header);
  2713.     if ( flash_read((char *)buff, offset, header.len) == 0) {
  2714.         printf("read cs MIB failed!\n");
  2715.         return -1;
  2716.     }
  2717.     status = CHECKSUM_OK(buff, header.len);
  2718.  
  2719.     if ( !status) {
  2720.         printf("cs Checksum error!\n");
  2721.         return -1;
  2722.     }
  2723. #ifdef COMPRESS_MIB_SETTING
  2724.     }
  2725. #endif 
  2726.     free(buff);
  2727.  
  2728. #if CONFIG_APMIB_SHARED_MEMORY == 1
  2729.     apmib_sem_lock();
  2730.     apmib_load_hwconf();
  2731.     apmib_load_dsconf();
  2732.     apmib_load_csconf();
  2733.     apmib_sem_unlock();
  2734. #endif
  2735.  
  2736.     return 0;
  2737. }
  2738.  
  2739. ///////////////////////////////////////////////////////////////////////////////
  2740. static int flash_read(char *buf, int offset, int len)
  2741. {
  2742.     int fh;
  2743.     int ok=1;
  2744.  
  2745. #ifdef __mips__
  2746.     fh = open(FLASH_DEVICE_NAME, O_RDWR);
  2747. #endif
  2748.  
  2749. #ifdef __i386__
  2750.     fh = open(FLASH_DEVICE_NAME, O_RDONLY);
  2751. #endif
  2752.     if ( fh == -1 )
  2753.         return 0;
  2754.  
  2755.     lseek(fh, offset, SEEK_SET);
  2756.  
  2757.     if ( read(fh, buf, len) != len)
  2758.         ok = 0;
  2759.  
  2760.     close(fh);
  2761.  
  2762.     return ok;
  2763. }
  2764.  
  2765. static int flash_write(char *buf, int offset, int len)
  2766. {
  2767.     int fh;
  2768.     int ok=1;
  2769.  
  2770.     fh = open(FLASH_DEVICE_NAME, O_RDWR);
  2771.  
  2772.     if ( fh == -1 )
  2773.         return 0;
  2774.  
  2775.     lseek(fh, offset, SEEK_SET);
  2776.  
  2777.     if ( write(fh, buf, len) != len)
  2778.         ok = 0;
  2779.  
  2780.     close(fh);
  2781.     sync();
  2782.  
  2783.     return ok;
  2784. }
  2785. ///////////////////////////////////////////////////////////////////////////////
  2786. static int searchMIB(char *token)
  2787. {
  2788.     int idx = 0;
  2789.     char tmpBuf[100];
  2790.     int desired_config=0;
  2791.  
  2792.     if (!memcmp(token, "HW_", 3)) {
  2793.         config_area = HW_MIB_AREA;
  2794.         if (!memcmp(&token[3], "WLAN", 4) && token[8] == '_') {
  2795.             wlan_idx = token[7] - '0';
  2796.             if (wlan_idx >= NUM_WLAN_INTERFACE)
  2797.                 return -1;
  2798.             strcpy(tmpBuf, &token[9]);
  2799.             desired_config = config_area+1;
  2800.         }
  2801.         else
  2802.             strcpy(tmpBuf, &token[3]);
  2803.     }
  2804.     else if (!memcmp(token, "DEF_", 4)) {
  2805.         config_area = DEF_MIB_AREA;
  2806.  
  2807.         if (!memcmp(&token[4], "WLAN", 4) && token[9] == '_') {
  2808.             wlan_idx = token[8] - '0';
  2809.             if (wlan_idx >= NUM_WLAN_INTERFACE)
  2810.                 return -1;
  2811. #ifdef MBSSID
  2812.             if (!memcmp(&token[10], "VAP", 3) && token[14] == '_') {
  2813.                 vwlan_idx = token[13] - '0';
  2814.                 if (vwlan_idx >= NUM_VWLAN_INTERFACE) {
  2815.                     vwlan_idx = 0;
  2816.                     return -1;
  2817.                 }
  2818.                 vwlan_idx += 1;
  2819.                 strcpy(tmpBuf, &token[15]);
  2820.             }
  2821.             else
  2822. #endif
  2823.             strcpy(tmpBuf, &token[10]);
  2824.             desired_config = config_area+1;
  2825.         }
  2826.         else
  2827.             strcpy(tmpBuf, &token[4]);
  2828.     }
  2829.     else {
  2830.         config_area = MIB_AREA;
  2831.  
  2832.         if (!memcmp(&token[0], "WLAN", 4) && token[5] == '_') {
  2833.             wlan_idx = token[4] - '0';
  2834.             if (wlan_idx >= NUM_WLAN_INTERFACE)
  2835.                 return -1;
  2836. #ifdef MBSSID
  2837.             if (!memcmp(&token[6], "VAP", 3) && token[10] == '_') {
  2838.                 vwlan_idx = token[9] - '0';
  2839.                 if (vwlan_idx >= NUM_VWLAN_INTERFACE) {
  2840.                     vwlan_idx = 0;
  2841.                     return -1;
  2842.                 }
  2843.                 vwlan_idx += 1;
  2844.                 strcpy(tmpBuf, &token[11]);
  2845.             }
  2846. #ifdef UNIVERSAL_REPEATER
  2847.             else if (!memcmp(&token[6], "VXD", 3) && token[9] == '_') {
  2848.                 vwlan_idx = NUM_VWLAN_INTERFACE;
  2849.                 strcpy(tmpBuf, &token[10]);
  2850.             }
  2851. #endif
  2852.             else
  2853. #endif
  2854.             strcpy(tmpBuf, &token[6]);
  2855.             desired_config = config_area+1;
  2856.         }
  2857.         else
  2858.             strcpy(tmpBuf, &token[0]);
  2859.     }
  2860.  
  2861.     if ( config_area == HW_MIB_AREA ) {
  2862.         while (hwmib_table[idx].id) {
  2863.             if ( !strcmp(hwmib_table[idx].name, tmpBuf)) {
  2864.                 if (desired_config && config_area != desired_config)
  2865.                     return -1;
  2866.                 return idx;
  2867.             }
  2868.             idx++;
  2869.         }
  2870.         idx=0;
  2871.         while (hwmib_wlan_table[idx].id) {
  2872.             if ( !strcmp(hwmib_wlan_table[idx].name, tmpBuf)) {
  2873.                 config_area++;
  2874.                 if (desired_config && config_area != desired_config)
  2875.                     return -1;
  2876.                 return idx;
  2877.             }
  2878.             idx++;
  2879.         }
  2880.         return -1;
  2881.     }
  2882.     else {
  2883.         while (mib_table[idx].id) {
  2884.             if ( !strcmp(mib_table[idx].name, tmpBuf)) {
  2885.                 if (desired_config && config_area != desired_config)
  2886.                     return -1;
  2887.                 return idx;
  2888.             }
  2889.             idx++;
  2890.         }
  2891.         idx=0;
  2892.         while (mib_wlan_table[idx].id) {
  2893.             if ( !strcmp(mib_wlan_table[idx].name, tmpBuf)) {
  2894.                 config_area++;
  2895.                 if (desired_config && config_area != desired_config)
  2896.                     return -1;
  2897.                 return idx;
  2898.             }
  2899.             idx++;
  2900.         }
  2901.         return -1;
  2902.     }
  2903. }
  2904.  
  2905. ///////////////////////////////////////////////////////////////////////////////
  2906. static void getMIB(char *name, int id, TYPE_T type, int num, int array_separate, char **val)
  2907. {
  2908.     unsigned char array_val[2048];
  2909.     struct in_addr ia_val;
  2910.     void *value;
  2911.     unsigned char tmpBuf[1024]={0}, *format=NULL, *buf, tmp1[400];
  2912.     int int_val, i;
  2913.     int index=1, tbl=0;
  2914.     char mibName[100]={0};
  2915.  
  2916.     if (num ==0)
  2917.         goto ret;
  2918.  
  2919.     strcat(mibName, name);
  2920.  
  2921.  
  2922. getval:
  2923.     buf = &tmpBuf[strlen((char *)tmpBuf)];
  2924.     switch (type) {
  2925.     case BYTE_T:
  2926.         value = (void *)&int_val;
  2927.         format = (unsigned char *)DEC_FORMAT;
  2928.         break;
  2929.     case WORD_T:
  2930.         value = (void *)&int_val;
  2931.         format = (unsigned char *)DEC_FORMAT;
  2932.         break;
  2933.     case IA_T:
  2934.         value = (void *)&ia_val;
  2935.         format = (unsigned char *)STR_FORMAT;
  2936.         break;
  2937.     case BYTE5_T:
  2938.         value = (void *)array_val;
  2939.         format = (unsigned char *)BYTE5_FORMAT;
  2940.         break;
  2941.     case BYTE6_T:
  2942.         value = (void *)array_val;
  2943.         format = (unsigned char *)BYTE6_FORMAT;
  2944.         break;
  2945.     case BYTE13_T:
  2946.         value = (void *)array_val;
  2947.         format = (unsigned char *)BYTE13_FORMAT;
  2948.         break;
  2949.  
  2950.     case STRING_T:
  2951.         value = (void *)array_val;
  2952.         format = (unsigned char *)STR_FORMAT;
  2953.         break;
  2954.  
  2955.     case BYTE_ARRAY_T:
  2956.         value = (void *)array_val;
  2957.         break;
  2958.  
  2959.     case DWORD_T:
  2960.         value = (void *)&int_val;
  2961.         format = (unsigned char *)DEC_FORMAT;
  2962.         break;
  2963.  
  2964.     case WLAC_ARRAY_T:
  2965. //#if defined(CONFIG_RTK_MESH) && defined(_MESH_ACL_ENABLE_)
  2966.     case MESH_ACL_ARRAY_T:
  2967. //#endif
  2968.     case WDS_ARRAY_T:
  2969.     case DHCPRSVDIP_ARRY_T:
  2970.     case SCHEDULE_ARRAY_T:
  2971. #ifdef HOME_GATEWAY
  2972.     case PORTFW_ARRAY_T:
  2973.     case IPFILTER_ARRAY_T:
  2974.     case PORTFILTER_ARRAY_T:
  2975.     case MACFILTER_ARRAY_T:
  2976.     case URLFILTER_ARRAY_T:
  2977.     case TRIGGERPORT_ARRAY_T:
  2978.  
  2979. #if defined(GW_QOS_ENGINE) || defined(QOS_BY_BANDWIDTH)
  2980.     case QOS_ARRAY_T:
  2981. #endif
  2982.        
  2983. #ifdef ROUTE_SUPPORT
  2984.     case STATICROUTE_ARRAY_T:
  2985. #endif
  2986. #ifdef VPN_SUPPORT
  2987.     case IPSECTUNNEL_ARRAY_T:
  2988. #endif
  2989. #ifdef CONFIG_IPV6
  2990.     case RADVDPREFIX_T:
  2991.     case DNSV6_T:
  2992.     case DHCPV6S_T:
  2993.     case ADDR6_T:
  2994.     case TUNNEL6_T:
  2995. #endif
  2996. #endif
  2997. #ifdef TLS_CLIENT
  2998.     case CERTROOT_ARRAY_T:
  2999.     case CERTUSER_ARRAY_T:
  3000. #endif
  3001.  
  3002. #if defined(VLAN_CONFIG_SUPPORTED)
  3003.     case VLANCONFIG_ARRAY_T:
  3004. #endif     
  3005.         tbl = 1;
  3006.         value = (void *)array_val;
  3007.         array_val[0] = index++;
  3008.         break;
  3009.     default: printf("invalid mib!\n"); return;
  3010.     }
  3011.  
  3012.     if ( !APMIB_GET(id, value)) {
  3013.         printf("Get MIB failed!\n");
  3014.         return;
  3015.     }
  3016.  
  3017.     if ( type == IA_T )
  3018.         value = inet_ntoa(ia_val);
  3019.  
  3020.     if (type == BYTE_T || type == WORD_T)
  3021.         sprintf((char *)buf, (char *)format, int_val);
  3022.     else if ( type == IA_T || type == STRING_T ) {
  3023.         sprintf((char *)buf, (char *)format, value);
  3024.        
  3025.         if (type == STRING_T ) {
  3026.             unsigned char tmpBuf1[1024];
  3027.             int srcIdx, dstIdx;
  3028.             for (srcIdx=0, dstIdx=0; buf[srcIdx]; srcIdx++, dstIdx++) {
  3029.                 if ( buf[srcIdx] == '"' || buf[srcIdx] == '\\' || buf[srcIdx] == '$' || buf[srcIdx] == '`' || buf[srcIdx] == ' ' )
  3030.                     tmpBuf1[dstIdx++] = '\\';
  3031.  
  3032.                 tmpBuf1[dstIdx] = buf[srcIdx];
  3033.             }
  3034.             if (dstIdx != srcIdx) {
  3035.                 memcpy(buf, tmpBuf1, dstIdx);
  3036.                 buf[dstIdx] ='\0';
  3037.             }
  3038.         }
  3039.     }
  3040.     else if (type == BYTE5_T) {
  3041.         sprintf((char *)buf, (char *)format, array_val[0],array_val[1],array_val[2],
  3042.             array_val[3],array_val[4],array_val[5]);
  3043.         convert_lower((char *)buf);
  3044.     }
  3045.     else if (type == BYTE6_T ) {
  3046.         sprintf((char *)buf, (char *)format, array_val[0],array_val[1],array_val[2],
  3047.             array_val[3],array_val[4],array_val[5],array_val[6]);
  3048.         convert_lower((char *)buf);
  3049.     }
  3050.     else if (type == BYTE13_T) {
  3051.         sprintf((char *)buf, (char *)format, array_val[0],array_val[1],array_val[2],
  3052.             array_val[3],array_val[4],array_val[5],array_val[6],
  3053.             array_val[7],array_val[8],array_val[9],array_val[10],
  3054.             array_val[11],array_val[12]);
  3055.         convert_lower((char *)buf);
  3056.     }
  3057.     else if(type == BYTE_ARRAY_T ) {
  3058.         int max_chan_num=MAX_2G_CHANNEL_NUM_MIB;
  3059.         int chan;
  3060. #if defined(CONFIG_RTL_8196B)
  3061.         max_chan_num = (id == MIB_HW_TX_POWER_CCK)? MAX_CCK_CHAN_NUM : MAX_OFDM_CHAN_NUM;
  3062.         if(val == NULL || val[0] == NULL){
  3063.             for(i=0 ;i< max_chan_num ;i++){
  3064.                 sprintf(tmp1, "%02x", array_val[i]);
  3065.                 strcat(buf, tmp1);
  3066.             }
  3067.             convert_lower(buf);
  3068.         }
  3069.         else{
  3070.             chan = atoi(val[0]);
  3071.             if(chan < 1 || chan > max_chan_num){
  3072.                 if((chan<1) || (id==MIB_HW_TX_POWER_CCK) || ((id==MIB_HW_TX_POWER_OFDM) && (chan>216))){
  3073.                     printf("invalid channel number\n");
  3074.                     return;
  3075.                 }
  3076.                 else{
  3077.                     if((chan >= 163) && (chan <= 181))
  3078.                         chan -= 148;
  3079.                     else // 182 ~ 216
  3080.                         chan -= 117;
  3081.                 }
  3082.             }
  3083.             sprintf(buf, "%d", *(((unsigned char *)value)+chan-1) );
  3084.         }
  3085. #else
  3086.         if((id >= MIB_HW_TX_POWER_CCK_A &&  id <=MIB_HW_TX_POWER_DIFF_OFDM))
  3087.             max_chan_num = MAX_2G_CHANNEL_NUM_MIB;
  3088.         else if((id >= MIB_HW_TX_POWER_5G_HT40_1S_A &&  id <=MIB_HW_TX_POWER_DIFF_5G_OFDM))
  3089.             max_chan_num = MAX_5G_CHANNEL_NUM_MIB;
  3090.         else if(id == MIB_L2TP_PAYLOAD)
  3091.             max_chan_num = MAX_L2TP_BUFF_LEN;
  3092.        
  3093.         if(val == NULL || val[0] == NULL){
  3094.             for(i=0;i< max_chan_num;i++){
  3095.                 sprintf((char *)tmp1, "%02x", array_val[i]);
  3096.                 strcat((char *)buf, (char *)tmp1);
  3097.             }
  3098.             convert_lower((char *)buf);
  3099.         }
  3100.         else{
  3101.             chan = atoi(val[0]);
  3102.             if(chan < 1 || chan > max_chan_num){
  3103.                 printf("invalid channel number\n");
  3104.                 return;
  3105.             }
  3106.             sprintf((char *)buf, "%d", *(((unsigned char *)value)+chan-1) );
  3107.         }
  3108. #endif
  3109.     }
  3110.     else if (type == DWORD_T)
  3111.         sprintf((char *)buf, (char *)format, int_val);
  3112.  
  3113.     else if (type == DHCPRSVDIP_ARRY_T) {      
  3114.         DHCPRSVDIP_Tp pEntry=(DHCPRSVDIP_Tp)array_val;
  3115.         sprintf((char *)buf, DHCPRSVDIP_FORMAT,            
  3116.             pEntry->macAddr[0],pEntry->macAddr[1],pEntry->macAddr[2],
  3117.             pEntry->macAddr[3],pEntry->macAddr[4],pEntry->macAddr[5],
  3118.             inet_ntoa(*((struct in_addr*)pEntry->ipAddr)), pEntry->hostName);
  3119.     }  
  3120. #if defined(VLAN_CONFIG_SUPPORTED) 
  3121.     else if (type == VLANCONFIG_ARRAY_T)
  3122.     {
  3123.         OPMODE_T opmode=-1;
  3124.         int isLan=1;
  3125.     #ifdef RTK_USB3G_PORT5_LAN
  3126.         DHCP_T wan_dhcp = -1;
  3127.         apmib_get( MIB_DHCP, (void *)&wan_dhcp);
  3128.     #endif
  3129.         apmib_get( MIB_OP_MODE, (void *)&opmode);
  3130.        
  3131.         VLAN_CONFIG_Tp pEntry=(VLAN_CONFIG_Tp)array_val;
  3132.  
  3133.         if(strncmp((char *)pEntry->netIface,"eth1",strlen("eth1")) == 0)
  3134.         {          
  3135.     #ifdef RTK_USB3G_PORT5_LAN     
  3136.             if(opmode == WISP_MODE || opmode == BRIDGE_MODE || wan_dhcp == USB3G)
  3137.     #else
  3138.             if(opmode == WISP_MODE || opmode == BRIDGE_MODE)
  3139.     #endif
  3140.                 isLan=1;
  3141.             else
  3142.                 isLan=0;
  3143.         }
  3144.         else if(strncmp("wlan0",(char *)pEntry->netIface, strlen((char *)pEntry->netIface)) == 0)
  3145.         {                      
  3146.             if(opmode == WISP_MODE)
  3147.                 isLan=0;
  3148.             else
  3149.                 isLan=1;
  3150.         }
  3151.         else
  3152.         {                      
  3153.             isLan=1;
  3154.         }
  3155.  
  3156.         sprintf((char *)buf, VLANCONFIG_FORMAT,            
  3157.             pEntry->netIface,pEntry->enabled,pEntry->tagged,pEntry->priority,pEntry->cfi, pEntry->vlanId,isLan);
  3158.     }      
  3159. #endif 
  3160.     else if (type == SCHEDULE_ARRAY_T)
  3161.     {
  3162.         SCHEDULE_Tp pEntry=(SCHEDULE_Tp)array_val;
  3163.         sprintf((char *)buf, SCHEDULE_FORMAT, pEntry->eco, pEntry->fTime, pEntry->tTime, pEntry->day);
  3164.         if ( strlen((char *)pEntry->text) ) {
  3165.             sprintf((char *)tmp1, ",%s", pEntry->text);
  3166.             strcat((char *)buf, (char *)tmp1);
  3167.         }
  3168.     }
  3169. #ifdef HOME_GATEWAY
  3170.     else if (type == PORTFW_ARRAY_T) {
  3171.         PORTFW_Tp pEntry=(PORTFW_Tp)array_val;
  3172.         sprintf((char *)buf, PORTFW_FORMAT, inet_ntoa(*((struct in_addr*)pEntry->ipAddr)),
  3173.              pEntry->fromPort, pEntry->toPort, pEntry->protoType);
  3174.         if ( strlen((char *)pEntry->comment) ) {
  3175.             sprintf((char *)tmp1, ", %s", pEntry->comment);
  3176.             strcat((char *)buf, (char *)tmp1);
  3177.         }
  3178.     }
  3179.     else if (type == PORTFILTER_ARRAY_T) {
  3180.         PORTFILTER_Tp pEntry=(PORTFILTER_Tp)array_val;
  3181.         sprintf((char *)buf, PORTFILTER_FORMAT,
  3182.              pEntry->fromPort, pEntry->toPort, pEntry->protoType);
  3183.         if ( strlen((char *)pEntry->comment) ) {
  3184.             sprintf((char *)tmp1, ", %s", pEntry->comment);
  3185.             strcat((char *)buf, (char *)tmp1);
  3186.         }
  3187.     }
  3188.     else if (type == IPFILTER_ARRAY_T) {
  3189.         IPFILTER_Tp pEntry=(IPFILTER_Tp)array_val;
  3190.         sprintf((char *)buf, IPFILTER_FORMAT, inet_ntoa(*((struct in_addr*)pEntry->ipAddr)),
  3191.              pEntry->protoType);
  3192.         if ( strlen((char *)pEntry->comment) ) {
  3193.             sprintf((char *)tmp1, ", %s", pEntry->comment);
  3194.             strcat((char *)buf, (char *)tmp1);
  3195.         }
  3196.     }
  3197.     else if (type == MACFILTER_ARRAY_T) {
  3198.         MACFILTER_Tp pEntry=(MACFILTER_Tp)array_val;
  3199.         sprintf((char *)buf, MACFILTER_COLON_FORMAT, pEntry->macAddr[0],pEntry->macAddr[1],pEntry->macAddr[2],
  3200.              pEntry->macAddr[3],pEntry->macAddr[4],pEntry->macAddr[5]);
  3201.         if ( strlen((char *)pEntry->comment) ) {
  3202.             sprintf((char *)tmp1, ", %s", pEntry->comment);
  3203.             strcat((char *)buf, (char *)tmp1);
  3204.         }
  3205.     }
  3206.     else if (type == URLFILTER_ARRAY_T) {
  3207.         URLFILTER_Tp pEntry=(URLFILTER_Tp)array_val;
  3208.         if(!strncmp((char *)pEntry->urlAddr,"http://",7))              
  3209.             sprintf((char *)buf, STR_FORMAT, pEntry->urlAddr+7);
  3210.         else
  3211.             sprintf((char *)buf, STR_FORMAT, pEntry->urlAddr);
  3212.         //if ( strlen(pEntry->comment) ) {
  3213.         //  sprintf(tmp1, ", %s", pEntry->comment);
  3214.         //  strcat(buf, tmp1);
  3215.         //}
  3216.     }
  3217.     else if (type == TRIGGERPORT_ARRAY_T) {
  3218.         TRIGGERPORT_Tp pEntry=(TRIGGERPORT_Tp)array_val;
  3219.         sprintf((char *)buf, TRIGGERPORT_FORMAT,
  3220.              pEntry->tri_fromPort, pEntry->tri_toPort, pEntry->tri_protoType,
  3221.              pEntry->inc_fromPort, pEntry->inc_toPort, pEntry->inc_protoType);
  3222.         if ( strlen((char *)pEntry->comment) ) {
  3223.             sprintf((char *)tmp1, ", %s", pEntry->comment);
  3224.             strcat((char *)buf, (char *)tmp1);
  3225.         }
  3226.     }
  3227. #ifdef GW_QOS_ENGINE
  3228.     else if (type == QOS_ARRAY_T) {
  3229.         QOS_Tp pEntry=(QOS_Tp)array_val;
  3230.             strcpy(tmp1, inet_ntoa(*((struct in_addr*)pEntry->local_ip_start)));
  3231.             strcpy(&tmp1[20], inet_ntoa(*((struct in_addr*)pEntry->local_ip_end)));
  3232.             strcpy(&tmp1[40], inet_ntoa(*((struct in_addr*)pEntry->remote_ip_start)));
  3233.             strcpy(&tmp1[60], inet_ntoa(*((struct in_addr*)pEntry->remote_ip_end)));
  3234.         sprintf(buf, QOS_FORMAT, pEntry->enabled,
  3235.                       pEntry->priority, pEntry->protocol,
  3236.                       tmp1, &tmp1[20],
  3237.                       pEntry->local_port_start, pEntry->local_port_end,
  3238.                       &tmp1[40], &tmp1[60],
  3239.                pEntry->remote_port_start, pEntry->remote_port_end, pEntry->entry_name);
  3240.     }
  3241. #endif
  3242.  
  3243. #ifdef QOS_BY_BANDWIDTH
  3244.     else if (type == QOS_ARRAY_T) {
  3245.         IPQOS_Tp pEntry=(IPQOS_Tp)array_val;
  3246.             strcpy((char *)tmp1, inet_ntoa(*((struct in_addr*)pEntry->local_ip_start)));
  3247.             strcpy((char *)&tmp1[20], inet_ntoa(*((struct in_addr*)pEntry->local_ip_end)));
  3248.         sprintf((char *)buf, QOS_FORMAT, pEntry->enabled,
  3249.                       pEntry->mac[0],pEntry->mac[1],pEntry->mac[2],pEntry->mac[3],pEntry->mac[4],pEntry->mac[5],
  3250.                       pEntry->mode, tmp1, &tmp1[20],
  3251.                       (int)pEntry->bandwidth, (int)pEntry->bandwidth_downlink,
  3252.                pEntry->entry_name);
  3253.     }
  3254. #endif
  3255.  
  3256. #ifdef ROUTE_SUPPORT
  3257.     else if (type == STATICROUTE_ARRAY_T) {
  3258.         char strIp[20], strMask[20], strGw[20];
  3259.         STATICROUTE_Tp pEntry=(STATICROUTE_Tp)array_val;
  3260.         strcpy(strIp, inet_ntoa(*((struct in_addr*)pEntry->dstAddr)));
  3261.                 strcpy(strMask, inet_ntoa(*((struct in_addr*)pEntry->netmask)));
  3262.                 strcpy(strGw, inet_ntoa(*((struct in_addr*)pEntry->gateway)));
  3263.         sprintf((char *)buf, "%s, %s, %s, %d, %d",strIp, strMask, strGw, pEntry->interface, pEntry->metric);
  3264.     }
  3265. #endif //ROUTE
  3266. #endif
  3267. #ifdef HOME_GATEWAY
  3268. #ifdef VPN_SUPPORT
  3269.     else if (type == IPSECTUNNEL_ARRAY_T) {
  3270.         IPSECTUNNEL_Tp pEntry=(IPSECTUNNEL_Tp)array_val;
  3271.         char strLcIp[20], strRtIp[20], strRtGw[20];
  3272.         strcpy(strLcIp, inet_ntoa(*((struct in_addr*)pEntry->lc_ipAddr)));
  3273.         strcpy(strRtIp, inet_ntoa(*((struct in_addr*)pEntry->rt_ipAddr)));
  3274.         strcpy(strRtGw, inet_ntoa(*((struct in_addr*)pEntry->rt_gwAddr)));
  3275.  
  3276.         sprintf(buf, IPSECTUNNEL_FORMAT, pEntry->tunnelId, pEntry->enable,  pEntry->connName,
  3277.         pEntry->lcType, strLcIp, pEntry->lc_maskLen,
  3278.         pEntry->rtType, strRtIp, pEntry->rt_maskLen,
  3279.         strRtGw, pEntry->keyMode,
  3280.         pEntry->conType, pEntry->espEncr, pEntry->espAuth,
  3281.         pEntry->psKey, pEntry->ikeEncr, pEntry->ikeAuth, pEntry->ikeKeyGroup, pEntry->ikeLifeTime,
  3282.         pEntry->ipsecLifeTime, pEntry->ipsecPfs, pEntry->spi, pEntry->encrKey, pEntry->authKey, pEntry->authType, pEntry->lcId, pEntry->rtId, pEntry->lcIdType, pEntry->rtIdType, pEntry->rsaKey
  3283.         );
  3284.     }
  3285. #endif
  3286. #ifdef CONFIG_IPV6
  3287.     else if(type == RADVDPREFIX_T)
  3288.         {
  3289.             radvdCfgParam_Tp pEntry=(radvdCfgParam_Tp)array_val;
  3290.             sprintf(buf,RADVD_FORMAT,
  3291.                 /*enabled*/
  3292.                 pEntry->enabled,
  3293.                 /*interface*/
  3294.                 pEntry->interface.Name,pEntry->interface.MaxRtrAdvInterval,pEntry->interface.MinRtrAdvInterval,
  3295.                 pEntry->interface.MinDelayBetweenRAs,pEntry->interface.AdvManagedFlag,pEntry->interface.AdvOtherConfigFlag,pEntry->interface.AdvLinkMTU,
  3296.                 pEntry->interface.AdvReachableTime,pEntry->interface.AdvRetransTimer,pEntry->interface.AdvCurHopLimit,pEntry->interface.AdvDefaultLifetime,
  3297.                 pEntry->interface.AdvDefaultPreference,pEntry->interface.AdvSourceLLAddress,pEntry->interface.UnicastOnly,
  3298.                 /*prefix 1*/
  3299.                 pEntry->interface.prefix[0].Prefix[0],pEntry->interface.prefix[0].Prefix[1],pEntry->interface.prefix[0].Prefix[2],pEntry->interface.prefix[0].Prefix[3],
  3300.                 pEntry->interface.prefix[0].Prefix[4],pEntry->interface.prefix[0].Prefix[5],pEntry->interface.prefix[0].Prefix[6],pEntry->interface.prefix[0].Prefix[7],
  3301.                 pEntry->interface.prefix[0].PrefixLen,pEntry->interface.prefix[0].AdvOnLinkFlag,pEntry->interface.prefix[0].AdvAutonomousFlag,pEntry->interface.prefix[0].AdvValidLifetime,
  3302.                 pEntry->interface.prefix[0].AdvPreferredLifetime,pEntry->interface.prefix[0].AdvRouterAddr,pEntry->interface.prefix[0].if6to4,pEntry->interface.prefix[0].enabled,
  3303.                 /*prefix 2*/
  3304.                 pEntry->interface.prefix[1].Prefix[0],pEntry->interface.prefix[1].Prefix[1],pEntry->interface.prefix[1].Prefix[2],pEntry->interface.prefix[1].Prefix[3],
  3305.                 pEntry->interface.prefix[1].Prefix[4],pEntry->interface.prefix[1].Prefix[5],pEntry->interface.prefix[1].Prefix[6],pEntry->interface.prefix[1].Prefix[7],
  3306.                 pEntry->interface.prefix[1].PrefixLen,pEntry->interface.prefix[1].AdvOnLinkFlag,pEntry->interface.prefix[1].AdvAutonomousFlag,pEntry->interface.prefix[1].AdvValidLifetime,
  3307.                 pEntry->interface.prefix[1].AdvPreferredLifetime,pEntry->interface.prefix[1].AdvRouterAddr,pEntry->interface.prefix[1].if6to4,pEntry->interface.prefix[1].enabled);
  3308.         }
  3309.     else if(type == DNSV6_T)
  3310.         {
  3311.             dnsv6CfgParam_Tp pEntry=(dnsv6CfgParam_Tp)array_val;
  3312.             sprintf(buf,DNSV6_FORMAT,pEntry->enabled,pEntry->routerName);
  3313.         }
  3314.     else if(type == DHCPV6S_T)
  3315.         {
  3316.             dhcp6sCfgParam_Tp pEntry=(dhcp6sCfgParam_Tp)array_val;
  3317.             sprintf(buf,DHCPV6S_FORMAT,pEntry->enabled,pEntry->DNSaddr6,pEntry->addr6PoolS,pEntry->addr6PoolE,pEntry->interfaceNameds);
  3318.         }  
  3319.     else if(type == ADDR6_T)
  3320.         {
  3321.             daddrIPv6CfgParam_Tp pEntry=(daddrIPv6CfgParam_Tp)array_val;
  3322.             sprintf(buf,ADDR6_FORMAT,pEntry->enabled,pEntry->prefix_len[0],pEntry->prefix_len[1],
  3323.                 pEntry->addrIPv6[0][0],pEntry->addrIPv6[0][1],pEntry->addrIPv6[0][2],pEntry->addrIPv6[0][3],
  3324.                 pEntry->addrIPv6[0][4],pEntry->addrIPv6[0][5],pEntry->addrIPv6[0][6],pEntry->addrIPv6[0][7],
  3325.                 pEntry->addrIPv6[1][0],pEntry->addrIPv6[1][1],pEntry->addrIPv6[1][2],pEntry->addrIPv6[1][3],
  3326.                 pEntry->addrIPv6[1][4],pEntry->addrIPv6[1][5],pEntry->addrIPv6[1][6],pEntry->addrIPv6[1][7]);
  3327.         }
  3328.     else if(type == TUNNEL6_T)
  3329.         {
  3330.             tunnelCfgParam_Tp pEntry=(tunnelCfgParam_Tp)array_val;
  3331.             sprintf(buf,"%d",pEntry->enabled);
  3332.         }
  3333. #endif
  3334. #endif
  3335. #ifdef TLS_CLIENT
  3336.     else if (type == CERTROOT_ARRAY_T) {
  3337.         CERTROOT_Tp pEntry=(CERTROOT_Tp)array_val;
  3338.         sprintf(buf, "%s", pEntry->comment);
  3339.     }
  3340.     else if (type == CERTUSER_ARRAY_T) {
  3341.         CERTUSER_Tp pEntry=(CERTUSER_Tp)array_val;
  3342.         sprintf(buf, "%s,%s", pEntry->comment, pEntry->pass);
  3343.     }  
  3344. #endif
  3345.     else if (type == WLAC_ARRAY_T) {
  3346.         MACFILTER_Tp pEntry=(MACFILTER_Tp)array_val;
  3347.         sprintf((char *)buf, MACFILTER_FORMAT, pEntry->macAddr[0],pEntry->macAddr[1],pEntry->macAddr[2],
  3348.              pEntry->macAddr[3],pEntry->macAddr[4],pEntry->macAddr[5]);
  3349.         if ( strlen((char *)pEntry->comment) ) {
  3350.             sprintf((char *)tmp1, ", %s", pEntry->comment);
  3351.             strcat((char *)buf, (char *)tmp1);
  3352.         }
  3353.     }
  3354.    
  3355. //#if defined(CONFIG_RTK_MESH) && defined(_MESH_ACL_ENABLE_) // below code copy above ACL code
  3356.     else if (type == MESH_ACL_ARRAY_T) {
  3357.         MACFILTER_Tp pEntry=(MACFILTER_Tp)array_val;
  3358.         sprintf((char *)buf, MACFILTER_FORMAT, pEntry->macAddr[0],pEntry->macAddr[1],pEntry->macAddr[2],
  3359.              pEntry->macAddr[3],pEntry->macAddr[4],pEntry->macAddr[5]);
  3360.         if ( strlen((char *)pEntry->comment) ) {
  3361.             sprintf((char *)tmp1, ", %s", pEntry->comment);
  3362.             strcat((char *)buf, (char *)tmp1);
  3363.         }
  3364.     }
  3365. //#endif
  3366.  
  3367.     else if (type == WDS_ARRAY_T) {
  3368.         WDS_Tp pEntry=(WDS_Tp)array_val;
  3369.         sprintf((char *)buf, WDS_FORMAT, pEntry->macAddr[0],pEntry->macAddr[1],pEntry->macAddr[2],
  3370.              pEntry->macAddr[3],pEntry->macAddr[4],pEntry->macAddr[5], pEntry->fixedTxRate);
  3371.         if ( strlen((char *)pEntry->comment) ) {
  3372.             sprintf((char *)tmp1, ", %s", pEntry->comment);
  3373.             strcat((char *)buf, (char *)tmp1);
  3374.         }
  3375.     }
  3376. #ifdef VOIP_SUPPORT
  3377.     else if (type == VOIP_T) {
  3378.         // use flash voip get xxx to replace
  3379.     }
  3380. #endif
  3381.  
  3382.     if (--num > 0) {
  3383.         if (!array_separate)
  3384.             strcat((char *)tmpBuf, " ");
  3385.         else {
  3386.             if (tbl){
  3387.                 if(type == STRING_T)
  3388.                     printf("%s%d=\"%s\"\n", mibName, index-1, tmpBuf);
  3389.                 else
  3390.                     printf("%s%d=%s\n", mibName, index-1, tmpBuf);
  3391.             }
  3392.             else{
  3393.                 if(type == STRING_T)
  3394.                     printf("%s=\"%s\"\n", mibName, tmpBuf);
  3395.                 else
  3396.                     printf("%s=%s\n", mibName, tmpBuf);
  3397.             }
  3398.             tmpBuf[0] = '\0';
  3399.         }
  3400.         goto getval;
  3401.     }
  3402. ret:
  3403.     if (tbl) {
  3404.         if(type == STRING_T)
  3405.             printf("%s%d=\"%s\"\n", mibName, index-1, tmpBuf);
  3406.         else
  3407.             printf("%s%d=%s\n", mibName, index-1, tmpBuf);
  3408.     }
  3409.     else{
  3410.         if(type == STRING_T)
  3411.                                 printf("%s=\"%s\"\n", mibName, tmpBuf);
  3412.         else
  3413.             printf("%s=%s\n", mibName, tmpBuf);
  3414.     }
  3415. }
  3416.  
  3417. ///////////////////////////////////////////////////////////////////////////////
  3418. static void setMIB(char *name, int id, TYPE_T type, int len, int valNum, char **val)
  3419. {
  3420.     unsigned char key[200];
  3421.     struct in_addr ia_val;
  3422.     void *value=NULL;
  3423.     int int_val, i;
  3424.     MACFILTER_T wlAc;   // Use with MESH_ACL
  3425.     WDS_T wds;
  3426.  
  3427. #ifdef HOME_GATEWAY
  3428.     PORTFW_T portFw;
  3429.     PORTFILTER_T portFilter;
  3430.     IPFILTER_T ipFilter;
  3431.     MACFILTER_T macFilter;
  3432.     URLFILTER_T urlFilter;
  3433.     TRIGGERPORT_T triggerPort;
  3434.  
  3435. #ifdef GW_QOS_ENGINE
  3436.     QOS_T qos;
  3437. #endif
  3438.  
  3439. #ifdef QOS_BY_BANDWIDTH
  3440.     IPQOS_T qos;
  3441. #endif
  3442.  
  3443. #ifdef ROUTE_SUPPORT
  3444.     STATICROUTE_T staticRoute;
  3445. #endif
  3446. #endif
  3447.  
  3448. #ifdef HOME_GATEWAY
  3449. #ifdef VPN_SUPPORT
  3450.     IPSECTUNNEL_T ipsecTunnel;
  3451. #endif
  3452.  
  3453. #ifdef CONFIG_IPV6
  3454.     char tmp[5];
  3455.     char *pstart ,*pend;
  3456.     int size;
  3457.     radvdCfgParam_t radvdCfgParam;
  3458.     dnsv6CfgParam_t dnsv6CfgParam;
  3459.     dhcp6sCfgParam_t dhcp6sCfgParam;
  3460.     addrIPv6CfgParam_t addrIPv6CfgParam;
  3461.     tunnelCfgParam_t tunnelCfgParam;
  3462. #endif
  3463. #endif
  3464. #ifdef TLS_CLIENT
  3465.     CERTROOT_T certRoot;
  3466.     CERTUSER_T certUser;
  3467. #endif
  3468.     DHCPRSVDIP_T dhcpRsvd;
  3469.    
  3470. #if defined(VLAN_CONFIG_SUPPORTED) 
  3471.     VLAN_CONFIG_T vlanConfig_entry;
  3472. #endif     
  3473.     int entryNum;
  3474.     int max_chan_num=0, tx_power_cnt=0;
  3475.  
  3476.     switch (type) {
  3477.     case BYTE_T:
  3478.     case WORD_T:
  3479.         int_val = atoi(val[0]);
  3480.         value = (void *)&int_val;
  3481.         break;
  3482.  
  3483.     case IA_T:
  3484.         if ( !inet_aton(val[0], &ia_val) ) {
  3485.             printf("invalid internet address!\n");
  3486.             return;
  3487.         }
  3488.         value = (void *)&ia_val;
  3489.         break;
  3490.  
  3491.     case BYTE5_T:
  3492.         if ( strlen(val[0])!=10 || !string_to_hex(val[0], key, 10)) {
  3493.             printf("invalid value!\n");
  3494.             return;
  3495.         }
  3496.         value = (void *)key;
  3497.         break;
  3498.  
  3499.     case BYTE6_T:
  3500.         if ( strlen(val[0])!=12 || !string_to_hex(val[0], key, 12)) {
  3501.             printf("invalid value!\n");
  3502.             return;
  3503.         }
  3504.         value = (void *)key;
  3505.         break;
  3506.  
  3507.     case BYTE_ARRAY_T:
  3508.         #if defined(CONFIG_RTL_8196B)
  3509.         if ( (id!=MIB_HW_TX_POWER_CCK) && (id!=MIB_HW_TX_POWER_OFDM_1S) && (id!=MIB_HW_TX_POWER_OFDM_2S)) {    
  3510.             printf("invalid mib!\n");
  3511.             return;
  3512.         }
  3513.             max_chan_num = (id==MIB_HW_TX_POWER_CCK)? MAX_CCK_CHAN_NUM : MAX_OFDM_CHAN_NUM ;
  3514.        
  3515.             for (i=0; i<max_chan_num ; i++) {
  3516.                 if(val[i] == NULL) break;
  3517.                 if ( !sscanf(val[i], "%d", &int_val) ) {
  3518.                     printf("invalid value!\n");
  3519.                     return;
  3520.                 }
  3521.                 key[i] = (unsigned char)int_val;
  3522.                 tx_power_cnt ++;
  3523.             }      
  3524.             if(tx_power_cnt != 1 && tx_power_cnt !=2 && tx_power_cnt != max_chan_num){
  3525.                 unsigned char key_tmp[170];
  3526.                 memcpy(key_tmp, key, tx_power_cnt);    
  3527.                 APMIB_GET(id, key);
  3528.                 memcpy(key, key_tmp, tx_power_cnt);
  3529.             }
  3530.             if(tx_power_cnt == 1){
  3531.                 for(i=1 ; i < max_chan_num; i++) {
  3532.                     key[i] = key[0];
  3533.                 }
  3534.             }
  3535.             else if(tx_power_cnt ==2){
  3536.                 //key[0] is channel number to set
  3537.                 //key[1] is tx power value
  3538.                 if(key[0] < 1 || key[0] > max_chan_num){
  3539.                     if((key[0]<1) || (id==MIB_HW_TX_POWER_CCK) || ((id==MIB_HW_TX_POWER_OFDM) && (key[0]>216))){
  3540.                         printf("invalid channel number\n");
  3541.                         return;
  3542.                     }
  3543.                     else{
  3544.                         if ((key[0] >= 163) && (key[0] <= 181))
  3545.                             key[0] -= 148;
  3546.                         else // 182 ~ 216
  3547.                             key[0] -= 117;
  3548.                     }
  3549.                 }
  3550.                 key[2] = 0xff ;
  3551.             }
  3552. #elif defined(CONFIG_RTL_8196C) || defined(CONFIG_RTL_8198)
  3553.     if(!(id >= MIB_HW_TX_POWER_CCK_A &&  id <=MIB_HW_TX_POWER_DIFF_OFDM) &&
  3554.             !(id >= MIB_HW_TX_POWER_5G_HT40_1S_A &&  id <=MIB_HW_TX_POWER_DIFF_5G_OFDM)
  3555.             && (id != MIB_L2TP_PAYLOAD)){
  3556.                 printf("invalid mib!\n");
  3557.                 return;
  3558.             }
  3559.         if((id >= MIB_HW_TX_POWER_CCK_A &&  id <=MIB_HW_TX_POWER_DIFF_OFDM))
  3560.             max_chan_num = MAX_2G_CHANNEL_NUM_MIB;
  3561.         else if((id >= MIB_HW_TX_POWER_5G_HT40_1S_A &&  id <=MIB_HW_TX_POWER_DIFF_5G_OFDM))
  3562.             max_chan_num = MAX_5G_CHANNEL_NUM_MIB;
  3563.        
  3564. #if defined(RTL_L2TP_POWEROFF_PATCH)
  3565.     if(id == MIB_L2TP_PAYLOAD)
  3566.     {
  3567.         if ( strlen(val[0])!=76 || !string_to_hex(val[0], key, 76)) {
  3568.         printf("invalid value!\n");
  3569.         return;
  3570.         }
  3571.         value = (void *)key;
  3572.         break;
  3573.     }
  3574. #endif
  3575.            
  3576.             for (i=0; i<max_chan_num ; i++) {
  3577.                 if(val[i] == NULL) break;
  3578.                 if ( !sscanf(val[i], "%d", &int_val) ) {
  3579.                     printf("invalid value!\n");
  3580.                     return;
  3581.                 }
  3582.                 key[i+1] = (unsigned char)int_val;
  3583.                 tx_power_cnt ++;
  3584.             }  
  3585.             if(tx_power_cnt != 1 && tx_power_cnt !=2 && tx_power_cnt != max_chan_num){
  3586.                 unsigned char key_tmp[200];
  3587.                 memcpy(key_tmp, key+1, tx_power_cnt);      
  3588.                 APMIB_GET(id, key+1);
  3589.                 memcpy(key+1, key_tmp, tx_power_cnt);
  3590.             }
  3591.             if(tx_power_cnt == 1){
  3592.                 for(i=1 ; i <= max_chan_num; i++) {
  3593.                     key[i] = key[1];
  3594.                 }
  3595.             }
  3596.         else if(tx_power_cnt ==2){
  3597.             //key[1] is channel number to set
  3598.             //key[2] is tx power value
  3599.                     //key[3] is tx power key for check set mode
  3600.             if(key[1] < 1 || key[1] > max_chan_num){
  3601.                 if((key[1]<1) || ((id >= MIB_HW_TX_POWER_CCK_A &&  id <=MIB_HW_TX_POWER_DIFF_OFDM)) ||
  3602.                      ((id >= MIB_HW_TX_POWER_5G_HT40_1S_A &&  id <=MIB_HW_TX_POWER_DIFF_5G_OFDM) && (key[1]>216))){
  3603.                     printf("invalid channel number\n");
  3604.                     return;
  3605.                 }
  3606.             }
  3607.             key[3] = 0xff ;
  3608.         }
  3609.         key[0] = tx_power_cnt;
  3610.         #else
  3611.         //!CONFIG_RTL_8196B => rtl8651c+rtl8190
  3612.         if ( (id!=MIB_HW_TX_POWER_CCK) && (id!=MIB_HW_TX_POWER_OFDM)) {    
  3613.             printf("invalid mib!\n");
  3614.             return;
  3615.         }
  3616.         max_chan_num = (id==MIB_HW_TX_POWER_CCK)? MAX_CCK_CHAN_NUM : MAX_OFDM_CHAN_NUM ;
  3617.         for (i=0; i<max_chan_num ; i++) {
  3618.             if(val[i] == NULL) break;
  3619.             if ( !sscanf(val[i], "%d", &int_val) ) {
  3620.                 printf("invalid value!\n");
  3621.                 return;
  3622.             }
  3623.             key[i] = (unsigned char)int_val;
  3624.             tx_power_cnt ++;
  3625.         }      
  3626.         if(tx_power_cnt != 1 && tx_power_cnt !=2 && tx_power_cnt != max_chan_num){
  3627.             unsigned char key_tmp[170];
  3628.             memcpy(key_tmp, key, tx_power_cnt);    
  3629.             APMIB_GET(id, key);
  3630.             memcpy(key, key_tmp, tx_power_cnt);
  3631.         }
  3632.         if(tx_power_cnt == 1){
  3633.             for(i=1 ; i < max_chan_num; i++) {
  3634.                 key[i] = key[0];
  3635.             }
  3636.         }
  3637.         else if(tx_power_cnt ==2){
  3638.             //key[0] is channel number to set
  3639.             //key[1] is tx power value
  3640.             if(key[0] < 1 || key[0] > max_chan_num){
  3641.                 if((key[0]<1) || (id==MIB_HW_TX_POWER_CCK) || ((id==MIB_HW_TX_POWER_OFDM) && (key[0]>216))){
  3642.                     printf("invalid channel number\n");
  3643.                     return;
  3644.                 }
  3645.                 else{
  3646.                     if ((key[0] >= 163) && (key[0] <= 181))
  3647.                         key[0] -= 148;
  3648.                     else // 182 ~ 216
  3649.                         key[0] -= 117;
  3650.                 }
  3651.             }
  3652.             key[2] = 0xff ;
  3653.         }
  3654.         #endif
  3655.         value = (void *)key;
  3656.         break;
  3657.     case DWORD_T:
  3658.         int_val = atoi(val[0]);
  3659.         value = (void *)&int_val;
  3660.         break;
  3661.  
  3662. #ifdef HOME_GATEWAY
  3663.     case PORTFW_ARRAY_T:
  3664.         if ( !strcmp(val[0], "add")) {
  3665.             id = MIB_PORTFW_ADD;
  3666.             if ( valNum < 5 ) {
  3667.                 printf("input argument is not enough!\n");
  3668.                 return;
  3669.             }
  3670.             if ( !inet_aton(val[1], (struct in_addr *)&portFw.ipAddr)) {
  3671.                 printf("invalid internet address!\n");
  3672.                 return;
  3673.             }
  3674.             portFw.fromPort = atoi(val[2]);
  3675.             portFw.toPort = atoi(val[3]);
  3676.             portFw.protoType = atoi(val[4]);
  3677.             if ( valNum > 5)
  3678.                 strcpy((char *)portFw.comment, val[5]);
  3679.             else
  3680.                 portFw.comment[0] = '\0';
  3681.  
  3682.         }
  3683.         else if ( !strcmp(val[0], "del")) {
  3684.             id = MIB_PORTFW_DEL;
  3685.             if ( valNum < 2 ) {
  3686.                 printf("input argument is not enough!\n");
  3687.                 return;
  3688.             }
  3689.             int_val = atoi(val[1]);
  3690.             if ( !APMIB_GET(MIB_PORTFW_TBL_NUM, (void *)&entryNum)) {
  3691.                 printf("Get port forwarding entry number error!");
  3692.                 return;
  3693.             }
  3694.             if ( int_val > entryNum ) {
  3695.                 printf("Element number is too large!\n");
  3696.                 return;
  3697.             }
  3698.             *((char *)&portFw) = (char)int_val;
  3699.             if ( !APMIB_GET(MIB_PORTFW_TBL, (void *)&portFw)) {
  3700.                 printf("Get table entry error!");
  3701.                 return;
  3702.             }
  3703.         }
  3704.         else if ( !strcmp(val[0], "delall"))
  3705.             id = MIB_PORTFW_DELALL;
  3706.  
  3707.         value = (void *)&portFw;
  3708.         break;
  3709.  
  3710.     case PORTFILTER_ARRAY_T:
  3711.         if ( !strcmp(val[0], "add")) {
  3712.             id = MIB_PORTFILTER_ADD;
  3713.             if ( valNum < 4 ) {
  3714.                 printf("input argument is not enough!\n");
  3715.                 return;
  3716.             }
  3717.             portFilter.fromPort = atoi(val[1]);
  3718.             portFilter.toPort = atoi(val[2]);
  3719.             portFilter.protoType = atoi(val[3]);
  3720.             if ( valNum > 4)
  3721.                 strcpy((char *)portFilter.comment, val[4]);
  3722.             else
  3723.                 portFilter.comment[0] = '\0';
  3724.  
  3725.         }
  3726.         else if ( !strcmp(val[0], "del")) {
  3727.             id = MIB_PORTFILTER_DEL;
  3728.             if ( valNum < 2 ) {
  3729.                 printf("input argument is not enough!\n");
  3730.                 return;
  3731.             }
  3732.             int_val = atoi(val[1]);
  3733.             if ( !APMIB_GET(MIB_PORTFILTER_TBL_NUM, (void *)&entryNum)) {
  3734.                 printf("Get port filter entry number error!");
  3735.                 return;
  3736.             }
  3737.             if ( int_val > entryNum ) {
  3738.                 printf("Element number is too large!\n");
  3739.                 return;
  3740.             }
  3741.             *((char *)&portFilter) = (char)int_val;
  3742.             if ( !APMIB_GET(MIB_PORTFILTER_TBL, (void *)&portFilter)) {
  3743.                 printf("Get table entry error!");
  3744.                 return;
  3745.             }
  3746.         }
  3747.         else if ( !strcmp(val[0], "delall"))
  3748.             id = MIB_PORTFILTER_DELALL;
  3749.  
  3750.         value = (void *)&portFilter;
  3751.         break;
  3752.  
  3753.     case IPFILTER_ARRAY_T:
  3754.         if ( !strcmp(val[0], "add")) {
  3755.             id = MIB_IPFILTER_ADD;
  3756.             if ( valNum < 3 ) {
  3757.                 printf("input argument is not enough!\n");
  3758.                 return;
  3759.             }
  3760.             if ( !inet_aton(val[1], (struct in_addr *)&ipFilter.ipAddr)) {
  3761.                 printf("invalid internet address!\n");
  3762.                 return;
  3763.             }
  3764.             ipFilter.protoType = atoi(val[2]);
  3765.             if ( valNum > 3)
  3766.                 strcpy((char *)ipFilter.comment, val[3]);
  3767.             else
  3768.                 ipFilter.comment[0] = '\0';
  3769.  
  3770.         }
  3771.         else if ( !strcmp(val[0], "del")) {
  3772.             id = MIB_IPFILTER_DEL;
  3773.             if ( valNum < 2 ) {
  3774.                 printf("input argument is not enough!\n");
  3775.                 return;
  3776.             }
  3777.             int_val = atoi(val[1]);
  3778.             if ( !APMIB_GET(MIB_IPFILTER_TBL_NUM, (void *)&entryNum)) {
  3779.                 printf("Get port forwarding entry number error!");
  3780.                 return;
  3781.             }
  3782.             if ( int_val > entryNum ) {
  3783.                 printf("Element number is too large!\n");
  3784.                 return;
  3785.             }
  3786.             *((char *)&ipFilter) = (char)int_val;
  3787.             if ( !APMIB_GET(MIB_IPFILTER_TBL, (void *)&ipFilter)) {
  3788.                 printf("Get table entry error!");
  3789.                 return;
  3790.             }
  3791.         }
  3792.         else if ( !strcmp(val[0], "delall"))
  3793.             id = MIB_IPFILTER_DELALL;
  3794.        
  3795.         value = (void *)&ipFilter;
  3796.         break;
  3797.  
  3798.     case MACFILTER_ARRAY_T:
  3799.         if ( !strcmp(val[0], "add")) {
  3800.             id = MIB_MACFILTER_ADD;
  3801.             if ( valNum < 2 ) {
  3802.                 printf("input argument is not enough!\n");
  3803.                 return;
  3804.             }
  3805.             if ( strlen(val[1])!=12 || !string_to_hex(val[1], wlAc.macAddr, 12)) {
  3806.                 printf("invalid value!\n");
  3807.                 return;
  3808.             }
  3809.  
  3810.             if ( valNum > 2)
  3811.                 strcpy((char *)macFilter.comment, val[2]);
  3812.             else
  3813.                 macFilter.comment[0] = '\0';
  3814.  
  3815.         }
  3816.         else if ( !strcmp(val[0], "del")) {
  3817.             id = MIB_MACFILTER_DEL;
  3818.             if ( valNum < 2 ) {
  3819.                 printf("input argument is not enough!\n");
  3820.                 return;
  3821.             }
  3822.             int_val = atoi(val[1]);
  3823.             if ( !APMIB_GET(MIB_MACFILTER_TBL_NUM, (void *)&entryNum)) {
  3824.                 printf("Get port forwarding entry number error!");
  3825.                 return;
  3826.             }
  3827.             if ( int_val > entryNum ) {
  3828.                 printf("Element number is too large!\n");
  3829.                 return;
  3830.             }
  3831.             *((char *)&macFilter) = (char)int_val;
  3832.             if ( !APMIB_GET(MIB_MACFILTER_TBL, (void *)&macFilter)) {
  3833.                 printf("Get table entry error!");
  3834.                 return;
  3835.             }
  3836.         }
  3837.         else if ( !strcmp(val[0], "delall"))
  3838.             id = MIB_MACFILTER_DELALL;
  3839.         value = (void *)&macFilter;
  3840.         break;
  3841.  
  3842.     case URLFILTER_ARRAY_T:
  3843.         if ( !strcmp(val[0], "add")) {
  3844.             id = MIB_URLFILTER_ADD;
  3845.             if ( valNum < 2 ) {
  3846.                 printf("input argument is not enough!\n");
  3847.                 return;
  3848.             }
  3849.             //if ( strlen(val[1])!=12 || !string_to_hex(val[1], wlAc.macAddr, 12)) {
  3850.             //  printf("invalid value!\n");
  3851.             //  return;
  3852.             //}
  3853.  
  3854.             //if ( valNum > 2)
  3855.             //  strcpy(urlFilter.comment, val[2]);
  3856.             //else
  3857.             //  uslFilter.comment[0] = '\0';
  3858.  
  3859.         }
  3860.         else if ( !strcmp(val[0], "del")) {
  3861.             id = MIB_URLFILTER_DEL;
  3862.             if ( valNum < 2 ) {
  3863.                 printf("input argument is not enough!\n");
  3864.                 return;
  3865.             }
  3866.             int_val = atoi(val[1]);
  3867.             if ( !APMIB_GET(MIB_URLFILTER_TBL_NUM, (void *)&entryNum)) {
  3868.                 printf("Get port forwarding entry number error!");
  3869.                 return;
  3870.             }
  3871.             if ( int_val > entryNum ) {
  3872.                 printf("Element number is too large!\n");
  3873.                 return;
  3874.             }
  3875.             *((char *)&urlFilter) = (char)int_val;
  3876.             if ( !APMIB_GET(MIB_URLFILTER_TBL, (void *)&urlFilter)) {
  3877.                 printf("Get table entry error!");
  3878.                 return;
  3879.             }
  3880.         }
  3881.         else if ( !strcmp(val[0], "delall"))
  3882.             id = MIB_URLFILTER_DELALL;
  3883.         value = (void *)&urlFilter;
  3884.         break;
  3885.        
  3886.     case TRIGGERPORT_ARRAY_T:
  3887.         if ( !strcmp(val[0], "add")) {
  3888.             id = MIB_TRIGGERPORT_ADD;
  3889.             if ( valNum < 7 ) {
  3890.                 printf("input argument is not enough!\n");
  3891.                 return;
  3892.             }
  3893.             triggerPort.tri_fromPort = atoi(val[1]);
  3894.             triggerPort.tri_toPort = atoi(val[2]);
  3895.             triggerPort.tri_protoType = atoi(val[3]);
  3896.             triggerPort.inc_fromPort = atoi(val[4]);
  3897.             triggerPort.inc_toPort = atoi(val[5]);
  3898.             triggerPort.inc_protoType = atoi(val[6]);
  3899.  
  3900.             if ( valNum > 7)
  3901.                 strcpy((char *)triggerPort.comment, val[7]);
  3902.             else
  3903.                 triggerPort.comment[0] = '\0';
  3904.  
  3905.         }
  3906.         else if ( !strcmp(val[0], "del")) {
  3907.             id = MIB_TRIGGERPORT_DEL;
  3908.             if ( valNum < 2 ) {
  3909.                 printf("input argument is not enough!\n");
  3910.                 return;
  3911.             }
  3912.             int_val = atoi(val[1]);
  3913.             if ( !APMIB_GET(MIB_TRIGGERPORT_TBL_NUM, (void *)&entryNum)) {
  3914.                 printf("Get trigger-port entry number error!");
  3915.                 return;
  3916.             }
  3917.             if ( int_val > entryNum ) {
  3918.                 printf("Element number is too large!\n");
  3919.                 return;
  3920.             }
  3921.             *((char *)&triggerPort) = (char)int_val;
  3922.             if ( !APMIB_GET(MIB_TRIGGERPORT_TBL, (void *)&triggerPort)) {
  3923.                 printf("Get trigger-port table entry error!");
  3924.                 return;
  3925.             }
  3926.         }
  3927.         else if ( !strcmp(val[0], "delall"))
  3928.             id = MIB_TRIGGERPORT_DELALL;
  3929.  
  3930.         value = (void *)&triggerPort;
  3931.         break;
  3932. #ifdef GW_QOS_ENGINE
  3933.     case QOS_ARRAY_T:
  3934.         if ( !strcmp(val[0], "add")) {
  3935.             id = MIB_QOS_ADD;
  3936.             if ( valNum < 13 ) {
  3937.                 printf("input argument is not enough!\n");
  3938.                 return;
  3939.             }
  3940.             qos.enabled = atoi(val[1]);
  3941.             if ( !inet_aton(val[4], (struct in_addr *)&qos.local_ip_start) ||
  3942.                         !inet_aton(val[5], (struct in_addr *)&qos.local_ip_end) ||
  3943.                         !inet_aton(val[8], (struct in_addr *)&qos.remote_ip_start) ||
  3944.                         !inet_aton(val[9], (struct in_addr *)&qos.remote_ip_end)
  3945.                       ) {
  3946.                 printf("invalid internet address!\n");
  3947.                 return;
  3948.             }
  3949.             qos.priority = atoi(val[2]);
  3950.             qos.protocol = atoi(val[3]);
  3951.             qos.local_port_start = atoi(val[6]);
  3952.             qos.local_port_end = atoi(val[7]);
  3953.             qos.remote_port_start = atoi(val[10]);
  3954.             qos.remote_port_end = atoi(val[11]);
  3955.             strcpy(qos.entry_name, val[12]);
  3956.  
  3957.         }
  3958.         else if ( !strcmp(val[0], "del")) {
  3959.             id = MIB_QOS_DEL;
  3960.             if ( valNum < 2 ) {
  3961.                 printf("input argument is not enough!\n");
  3962.                 return;
  3963.             }
  3964.             int_val = atoi(val[1]);
  3965.             if ( !APMIB_GET(MIB_QOS_RULE_TBL_NUM, (void *)&entryNum)) {
  3966.                 printf("Get QoS entry number error!");
  3967.                 return;
  3968.             }
  3969.             if ( int_val > entryNum ) {
  3970.                 printf("Element number is too large!\n");
  3971.                 return;
  3972.             }
  3973.             *((char *)&qos) = (char)int_val;
  3974.             if ( !APMIB_GET(MIB_QOS_RULE_TBL, (void *)&qos)) {
  3975.                 printf("Get table entry error!");
  3976.                 return;
  3977.             }
  3978.         }
  3979.         else if ( !strcmp(val[0], "delall"))
  3980.             id = MIB_QOS_DELALL;
  3981.  
  3982.         value = (void *)&qos;
  3983.         break;
  3984. #endif
  3985.  
  3986. #ifdef QOS_BY_BANDWIDTH
  3987.     case QOS_ARRAY_T:
  3988.         if ( !strcmp(val[0], "add")) {
  3989.             id = MIB_QOS_ADD;
  3990.             if ( valNum < 9 ) {
  3991.                 printf("input argument is not enough!\n");
  3992.                 return;
  3993.             }
  3994.             qos.enabled = atoi(val[1]);
  3995.             if ( !inet_aton(val[4], (struct in_addr *)&qos.local_ip_start) ||
  3996.                         !inet_aton(val[5], (struct in_addr *)&qos.local_ip_end)
  3997.                       ) {
  3998.                 printf("invalid internet address!\n");
  3999.                 return;
  4000.             }
  4001.             //strcpy(qos.mac, val[2]);
  4002.             if (strlen(val[2])!=12 || !string_to_hex(val[2], qos.mac, 12))  {
  4003.                 printf("invalid MAC address!\n");
  4004.                 return;        
  4005.             }
  4006.             qos.mode = atoi(val[3]);
  4007.             qos.bandwidth = atoi(val[6]);
  4008.             strcpy((char *)qos.entry_name, val[7]);
  4009.             qos.bandwidth_downlink = atoi(val[8]);
  4010.  
  4011.         }
  4012.         else if ( !strcmp(val[0], "del")) {
  4013.             id = MIB_QOS_DEL;
  4014.             if ( valNum < 2 ) {
  4015.                 printf("input argument is not enough!\n");
  4016.                 return;
  4017.             }
  4018.             int_val = atoi(val[1]);
  4019.             if ( !APMIB_GET(MIB_QOS_RULE_TBL_NUM, (void *)&entryNum)) {
  4020.                 printf("Get QoS entry number error!");
  4021.                 return;
  4022.             }
  4023.             if ( int_val > entryNum ) {
  4024.                 printf("Element number is too large!\n");
  4025.                 return;
  4026.             }
  4027.             *((char *)&qos) = (char)int_val;
  4028.             if ( !APMIB_GET(MIB_QOS_RULE_TBL, (void *)&qos)) {
  4029.                 printf("Get table entry error!");
  4030.                 return;
  4031.             }
  4032.         }
  4033.         else if ( !strcmp(val[0], "delall"))
  4034.             id = MIB_QOS_DELALL;
  4035.  
  4036.         value = (void *)&qos;
  4037.         break;
  4038. #endif
  4039.  
  4040. #ifdef ROUTE_SUPPORT
  4041.         case STATICROUTE_ARRAY_T:
  4042.         if ( !strcmp(val[0], "add")) {
  4043.             id = MIB_STATICROUTE_ADD;
  4044.             if ( valNum < 3 ) {
  4045.                 printf("input argument is not enough!\n");
  4046.                 return;
  4047.             }
  4048.             if ( !inet_aton(val[1], (struct in_addr *)&staticRoute.dstAddr)) {
  4049.                                 printf("invalid destination IP address!\n");
  4050.                                 return;
  4051.                         }
  4052.             if ( !inet_aton(val[2], (struct in_addr *)&staticRoute.netmask)) {
  4053.                                 printf("invalid netmask !\n");
  4054.                                 return;
  4055.                         }
  4056.             if ( !inet_aton(val[3], (struct in_addr *)&staticRoute.gateway)) {
  4057.                                 printf("invalid gateway address!\n");
  4058.                                 return;
  4059.                         }
  4060.         }
  4061.         else if ( !strcmp(val[0], "del")) {
  4062.             id = MIB_STATICROUTE_DEL;
  4063.             if ( valNum < 2 ) {
  4064.                 printf("input argument is not enough!\n");
  4065.                 return;
  4066.             }
  4067.             int_val = atoi(val[1]);
  4068.             if ( !APMIB_GET(MIB_STATICROUTE_TBL_NUM, (void *)&entryNum)) {
  4069.                 printf("Get trigger-port entry number error!");
  4070.                 return;
  4071.             }
  4072.             if ( int_val > entryNum ) {
  4073.                 printf("Element number is too large!\n");
  4074.                 return;
  4075.             }
  4076.             *((char *)&staticRoute) = (char)int_val;
  4077.             if ( !APMIB_GET(MIB_STATICROUTE_TBL, (void *)&staticRoute)) {
  4078.                 printf("Get trigger-port table entry error!");
  4079.                 return;
  4080.             }
  4081.         }
  4082.         else if ( !strcmp(val[0], "delall"))
  4083.             id = MIB_STATICROUTE_DELALL;
  4084.  
  4085.         value = (void *)&staticRoute;
  4086.         break;
  4087. #endif //ROUTE
  4088.  
  4089. #endif
  4090.  
  4091.     case SCHEDULE_ARRAY_T:
  4092.         if ( !strcmp(val[0], "add")) {
  4093.             //may not used
  4094.         }
  4095.         else if ( !strcmp(val[0], "del")) {
  4096.             //may not used
  4097.         }
  4098.         else if ( !strcmp(val[0], "delall")){
  4099.             //may not used
  4100.         }
  4101.  
  4102.         break;
  4103.     case DHCPRSVDIP_ARRY_T:
  4104.         if ( !strcmp(val[0], "add")) {
  4105.             id = MIB_DHCPRSVDIP_ADD;
  4106.             if ( valNum < 3 ) {
  4107.                 printf("input argument is not enough!\n");
  4108.                 return;
  4109.             }
  4110.             if ( strlen(val[1])!=12 || !string_to_hex(val[1], dhcpRsvd.macAddr, 12)) {
  4111.                 printf("invalid value!\n");
  4112.                 return;
  4113.             }
  4114.             if (!inet_aton(val[2], (struct in_addr *)&dhcpRsvd.ipAddr)) {
  4115.                 printf("invalid internet address!\n");
  4116.                 return;
  4117.             }      
  4118.             if ( valNum > 3 )
  4119.                 strcpy((char *)dhcpRsvd.hostName, val[3]);         
  4120.         }
  4121.         else if ( !strcmp(val[0], "del")) {
  4122.             id = MIB_DHCPRSVDIP_DEL;
  4123.             if ( valNum < 2 ) {
  4124.                 printf("input argument is not enough!\n");
  4125.                 return;
  4126.             }
  4127.             int_val = atoi(val[1]);
  4128.             if ( !APMIB_GET(MIB_DHCPRSVDIP_TBL_NUM, (void *)&entryNum)) {
  4129.                 printf("Get DHCP resvd IP entry number error!");
  4130.                 return;
  4131.             }
  4132.             if ( int_val > entryNum ) {
  4133.                 printf("Element number is too large!\n");
  4134.                 return;
  4135.             }
  4136.             *((char *)&dhcpRsvd) = (char)int_val;
  4137.             if ( !APMIB_GET(MIB_DHCPRSVDIP_TBL, (void *)&dhcpRsvd)) {
  4138.                 printf("Get table entry error!");
  4139.                 return;
  4140.             }
  4141.         }
  4142.         else if ( !strcmp(val[0], "delall"))
  4143.             id = MIB_DHCPRSVDIP_DELALL;
  4144.         value = (void *)&dhcpRsvd;
  4145.         break;
  4146. #if defined(VLAN_CONFIG_SUPPORTED) 
  4147.         case VLANCONFIG_ARRAY_T:
  4148.         if ( !strcmp(val[0], "add")) {
  4149.             id = MIB_VLANCONFIG_ADD;
  4150.             if ( valNum < 6 ) {
  4151.                 printf("input argument is not enough!\n");
  4152.                 return;
  4153.             }
  4154.             vlanConfig_entry.enabled=(unsigned char)atoi(val[1]);
  4155.             sprintf((char *)vlanConfig_entry.netIface, "%s", val[2]);
  4156.             vlanConfig_entry.tagged = (unsigned char)atoi(val[3]);
  4157.             vlanConfig_entry.priority = (unsigned char)atoi(val[4]);
  4158.             vlanConfig_entry.cfi = (unsigned char)atoi(val[5]);
  4159.             vlanConfig_entry.vlanId = (unsigned short)atoi(val[6]);
  4160.         }
  4161.         else if ( !strcmp(val[0], "del")) {
  4162.             id = MIB_VLANCONFIG_DEL;
  4163.             if ( valNum < 8 ) {
  4164.                 printf("input argument is not enough!\n");
  4165.                 return;
  4166.             }
  4167.             int_val = atoi(val[1]); //index of entry
  4168.             if ( !APMIB_GET(MIB_VLANCONFIG_TBL_NUM, (void *)&entryNum)) {
  4169.                 printf("Get VLAN config entry number error!");
  4170.                 return;
  4171.             }
  4172.             if ( int_val > entryNum ) {
  4173.                 printf("Element number is too large!\n");
  4174.                 return;
  4175.             }
  4176.             *((char *)&vlanConfig_entry) = (char)int_val;
  4177.             if ( !APMIB_GET(MIB_VLANCONFIG_TBL, (void *)&vlanConfig_entry)) {
  4178.                 printf("Get table entry error!");
  4179.                 return;
  4180.             }
  4181.             vlanConfig_entry.enabled=0;
  4182.  
  4183.         }
  4184.         else if ( !strcmp(val[0], "delall"))
  4185.             id = MIB_VLANCONFIG_DELALL;
  4186.         value = (void *)&vlanConfig_entry;
  4187.         break;
  4188. #endif
  4189.     case WLAC_ARRAY_T:
  4190.         if ( !strcmp(val[0], "add")) {
  4191.             id = MIB_WLAN_AC_ADDR_ADD;
  4192.             if ( valNum < 2 ) {
  4193.                 printf("input argument is not enough!\n");
  4194.                 return;
  4195.             }
  4196.             if ( strlen(val[1])!=12 || !string_to_hex(val[1], wlAc.macAddr, 12)) {
  4197.                 printf("invalid value!\n");
  4198.                 return;
  4199.             }
  4200.  
  4201.             if ( valNum > 2)
  4202.                 strcpy((char *)wlAc.comment, val[2]);
  4203.             else
  4204.                 wlAc.comment[0] = '\0';
  4205.         }
  4206.         else if ( !strcmp(val[0], "del")) {
  4207.             id = MIB_WLAN_AC_ADDR_DEL;
  4208.             if ( valNum < 2 ) {
  4209.                 printf("input argument is not enough!\n");
  4210.                 return;
  4211.             }
  4212.             int_val = atoi(val[1]);
  4213.             if ( !APMIB_GET(MIB_WLAN_MACAC_NUM, (void *)&entryNum)) {
  4214.                 printf("Get port forwarding entry number error!");
  4215.                 return;
  4216.             }
  4217.             if ( int_val > entryNum ) {
  4218.                 printf("Element number is too large!\n");
  4219.                 return;
  4220.             }
  4221.             *((char *)&wlAc) = (char)int_val;
  4222.             if ( !APMIB_GET(MIB_WLAN_MACAC_ADDR, (void *)&wlAc)) {
  4223.                 printf("Get table entry error!");
  4224.                 return;
  4225.             }
  4226.         }
  4227.         else if ( !strcmp(val[0], "delall"))
  4228.             id = MIB_WLAN_AC_ADDR_DELALL;
  4229.         value = (void *)&wlAc;
  4230.         break;
  4231.  
  4232. //#if defined(CONFIG_RTK_MESH) && defined(_MESH_ACL_ENABLE_) // below code copy above ACL code
  4233.     case MESH_ACL_ARRAY_T:
  4234.         if ( !strcmp(val[0], "add")) {
  4235.             id = MIB_MESH_ACL_ADDR_ADD;
  4236.             if ( valNum < 2 ) {
  4237.                 printf("Mesh Acl Addr input argument is not enough!\n");
  4238.                 return;
  4239.             }
  4240.             if ( strlen(val[1])!=12 || !string_to_hex(val[1], wlAc.macAddr, 12)) {
  4241.                 printf("Mesh Acl Addr invalid value!\n");
  4242.                 return;
  4243.             }
  4244.  
  4245.             if ( valNum > 2)
  4246.                 strcpy((char *)wlAc.comment, val[2]);
  4247.             else
  4248.                 wlAc.comment[0] = '\0';
  4249.         }
  4250.         else if ( !strcmp(val[0], "del")) {
  4251.             id = MIB_MESH_ACL_ADDR_DEL;
  4252.             if ( valNum < 2 ) {
  4253.                 printf("Mesh Acl Addr input argument is not enough!\n");
  4254.                 return;
  4255.             }
  4256.             int_val = atoi(val[1]);
  4257.             if ( !APMIB_GET(MIB_MESH_ACL_NUM, (void *)&entryNum)) {
  4258.                 printf("Mesh Acl Addr get port forwarding entry number error!");
  4259.                 return;
  4260.             }
  4261.             if ( int_val > entryNum ) {
  4262.                 printf("Mesh Acl Addr element number is too large!\n");
  4263.                 return;
  4264.             }
  4265.             *((char *)&wlAc) = (char)int_val;
  4266.             if ( !APMIB_GET(MIB_MESH_ACL_ADDR, (void *)&wlAc)) {
  4267.                 printf("Mesh Acl Addr get table entry error!");
  4268.                 return;
  4269.             }
  4270.         }
  4271.         else if ( !strcmp(val[0], "delall"))
  4272.             id = MIB_MESH_ACL_ADDR_DELALL;
  4273.         value = (void *)&wlAc;
  4274.         break;
  4275. //#endif    // CONFIG_RTK_MESH && _MESH_ACL_ENABLE_
  4276.  
  4277.     case WDS_ARRAY_T:
  4278.         if ( !strcmp(val[0], "add")) {
  4279.             id = MIB_WLAN_WDS_ADD;
  4280.             if ( valNum < 3 ) {
  4281.                 printf("input argument is not enough!\n");
  4282.                 return;
  4283.             }
  4284.             if ( strlen(val[1])!=12 || !string_to_hex(val[1], wds.macAddr, 12)) {
  4285.                 printf("invalid value!\n");
  4286.                 return;
  4287.             }
  4288.  
  4289.             if ( valNum > 2)
  4290.                 strcpy((char *)wds.comment, val[2]);
  4291.             else
  4292.                 wds.comment[0] = '\0';
  4293.                
  4294.             wds.fixedTxRate = atoi(val[3]);
  4295.         }
  4296.         else if ( !strcmp(val[0], "del")) {
  4297.             id = MIB_WLAN_WDS_DEL;
  4298.             if ( valNum < 2 ) {
  4299.                 printf("input argument is not enough!\n");
  4300.                 return;
  4301.             }
  4302.             int_val = atoi(val[1]);
  4303.             if ( !APMIB_GET(MIB_WLAN_WDS_NUM, (void *)&entryNum)) {
  4304.                 printf("Get wds number error!");
  4305.                 return;
  4306.             }
  4307.             if ( int_val > entryNum ) {
  4308.                 printf("Element number is too large!\n");
  4309.                 return;
  4310.             }
  4311.             *((char *)&wds) = (char)int_val;
  4312.             if ( !APMIB_GET(MIB_WLAN_WDS, (void *)&wds)) {
  4313.                 printf("Get table entry error!");
  4314.                 return;
  4315.             }
  4316.         }
  4317.         else if ( !strcmp(val[0], "delall"))
  4318.             id = MIB_WLAN_WDS_DELALL;
  4319.         value = (void *)&wds;
  4320.         break;
  4321.  
  4322. #ifdef HOME_GATEWAY
  4323. #ifdef VPN_SUPPORT
  4324.     case IPSECTUNNEL_ARRAY_T:
  4325.         if ( !strcmp(val[0], "add")) {
  4326.             id = MIB_IPSECTUNNEL_ADD;
  4327.             if ( valNum < 27 ) {
  4328.                 printf("input argument is not enough!\n");
  4329.                 return;
  4330.             }
  4331.             if ( !inet_aton(val[5], (struct in_addr *)&ipsecTunnel.lc_ipAddr)) {
  4332.                 printf("invalid local IP address!\n");
  4333.                 return;
  4334.             }
  4335.            
  4336.             if ( !inet_aton(val[8], (struct in_addr *)&ipsecTunnel.rt_ipAddr)) {
  4337.                 printf("invalid remote IP address!\n");
  4338.                 return;
  4339.             }
  4340.             if ( !inet_aton(val[10], (struct in_addr *)&ipsecTunnel.rt_gwAddr)) {
  4341.                 printf("invalid remote gateway address!\n");
  4342.                 return;
  4343.             }
  4344.             ipsecTunnel.tunnelId =  atoi(val[1]);
  4345.             ipsecTunnel.enable = atoi(val[2]);
  4346.             ipsecTunnel.lcType = atoi(val[4]);
  4347.  
  4348.             if(strlen(val[3]) > (MAX_NAME_LEN-1)){
  4349.                 printf("Connection Name too long !\n");
  4350.                 return;
  4351.             }else
  4352.                 strcpy(ipsecTunnel.connName, val[3]);
  4353.             ipsecTunnel.lc_maskLen = atoi(val[6]);
  4354.             ipsecTunnel.rt_maskLen  = atoi(val[9]);
  4355.             ipsecTunnel.keyMode= atoi(val[11]);
  4356.             ipsecTunnel.conType = atoi(val[12]);
  4357.             ipsecTunnel.espEncr = atoi(val[13]);
  4358.             ipsecTunnel.espAuth = atoi(val[14]);
  4359.             if(strlen(val[15]) >  (MAX_NAME_LEN-1)){
  4360.                 printf("Preshared Key too long !\n");
  4361.                 return;
  4362.             }else
  4363.                 strcpy(ipsecTunnel.psKey, val[15]);
  4364.  
  4365.             ipsecTunnel.ikeEncr = atoi(val[16]);
  4366.             ipsecTunnel.ikeAuth = atoi(val[17]);
  4367.             ipsecTunnel.ikeKeyGroup = atoi(val[18]);
  4368.             ipsecTunnel.ikeLifeTime= strtol(val[19], (char **)NULL, 10);
  4369.  
  4370.             ipsecTunnel.ipsecLifeTime= strtol(val[20], (char **)NULL, 10);
  4371.             ipsecTunnel.ipsecPfs= atoi(val[21]);
  4372.             if(strlen(val[22]) >  (MAX_SPI_LEN-1)){
  4373.                 printf("SPI too long !\n");
  4374.                 return;
  4375.             }else
  4376.                 strcpy(ipsecTunnel.spi, val[22]);
  4377.  
  4378.             if(strlen(val[23]) >  (MAX_ENCRKEY_LEN-1)){
  4379.                 printf("Encryption key too long !\n");
  4380.                 return;
  4381.             }else
  4382.                 strcpy(ipsecTunnel.encrKey, val[23]);
  4383.  
  4384.             if(strlen(val[24]) >  (MAX_AUTHKEY_LEN-1)){
  4385.                 printf("Authentication key too long !\n");
  4386.                 return;
  4387.             }else
  4388.                 strcpy(ipsecTunnel.authKey, val[24]);
  4389.  
  4390.  
  4391.         }
  4392.         else if ( !strcmp(val[0], "del")) {
  4393.             id = MIB_IPSECTUNNEL_DEL;
  4394.             if ( valNum < 2 ) {
  4395.                 printf("input argument is not enough!\n");
  4396.                 return;
  4397.             }
  4398.             int_val = atoi(val[1]);
  4399.             if ( !APMIB_GET(MIB_IPSECTUNNEL_TBL_NUM, (void *)&entryNum)) {
  4400.                 printf("Get ipsec tunnel number error!");
  4401.                 return;
  4402.             }
  4403.             if ( int_val > entryNum ) {
  4404.                 printf("Element number is too large!\n");
  4405.                 return;
  4406.             }
  4407.             *((char *)&ipsecTunnel) = (char)int_val;
  4408.             if ( !APMIB_GET(MIB_IPSECTUNNEL_TBL, (void *)&ipsecTunnel)) {
  4409.                 printf("Get table entry error!");
  4410.                 return;
  4411.             }
  4412.         }
  4413.         else if ( !strcmp(val[0], "delall"))
  4414.             id = MIB_IPSECTUNNEL_DELALL;
  4415.         value = (void *)&ipsecTunnel;
  4416.         break;
  4417.  
  4418. #endif
  4419.  
  4420. #ifdef CONFIG_IPV6
  4421.     case RADVDPREFIX_T:
  4422.         if(valNum != 24 || valNum != 33)
  4423.             printf("input argumentis not enough");
  4424.         radvdCfgParam.enabled=atoi(val[0]);
  4425.         strcpy(radvdCfgParam.interface.Name,val[1]);
  4426.         radvdCfgParam.interface.MaxRtrAdvInterval=atoi(val[3]);
  4427.         radvdCfgParam.interface.MinRtrAdvInterval=atoi(val[4]);
  4428.         radvdCfgParam.interface.MinDelayBetweenRAs=atoi(val[5]);
  4429.         radvdCfgParam.interface.AdvManagedFlag=atoi(val[6]);
  4430.         radvdCfgParam.interface.AdvOtherConfigFlag=atoi(val[7]);
  4431.         radvdCfgParam.interface.AdvLinkMTU=atoi(val[8]);
  4432.         radvdCfgParam.interface.AdvReachableTime=atoi(val[9]);
  4433.         radvdCfgParam.interface.AdvRetransTimer=atoi(val[10]);
  4434.         radvdCfgParam.interface.AdvCurHopLimit=atoi(val[11]);
  4435.         strcpy(radvdCfgParam.interface.AdvDefaultPreference,val[12]);
  4436.         radvdCfgParam.interface.AdvSourceLLAddress=atoi(val[13]);
  4437.         radvdCfgParam.interface.UnicastOnly=atoi(val[14]);
  4438.  
  4439.         /*prefix1*/
  4440.         memset(tmp,0,5);
  4441.         pstart=val[15];
  4442.         pend=pstart;
  4443.         for(i=0;i<8;i++)
  4444.         {
  4445.             size=0;
  4446.             while(*pend !=':')
  4447.             {
  4448.                 pend++;
  4449.                 size++;
  4450.             }
  4451.             pend++;
  4452.             if(*pend == ':')
  4453.                 pend++;
  4454.             memcpy(tmp,pstart,size);
  4455.             pstart=pend;
  4456.             radvdCfgParam.interface.prefix[0].Prefix[i]=strtol(tmp,NULL,16);
  4457.         }
  4458.         radvdCfgParam.interface.prefix[0].PrefixLen=atoi(val[16]);
  4459.         radvdCfgParam.interface.prefix[0].AdvOnLinkFlag=atoi(val[17]);
  4460.         radvdCfgParam.interface.prefix[0].AdvAutonomousFlag=atoi(val[18]);
  4461.         radvdCfgParam.interface.prefix[0].AdvValidLifetime=atoi(val[19]);
  4462.         radvdCfgParam.interface.prefix[0].AdvPreferredLifetime=atoi(val[20]);
  4463.         radvdCfgParam.interface.prefix[0].AdvRouterAddr=atoi(val[21]);
  4464.         strcpy(radvdCfgParam.interface.prefix[0].if6to4,val[22]);
  4465.         radvdCfgParam.interface.prefix[0].enabled=atoi(val[23]);
  4466.  
  4467.         /*prefix2*/
  4468.         memset(tmp,0,5);
  4469.         pstart=val[24];
  4470.         pend=pstart;
  4471.         for(i=0;i<8;i++)
  4472.         {
  4473.             size=0;
  4474.             while(*pend !=':')
  4475.             {
  4476.                 pend++;
  4477.                 size++;
  4478.             }
  4479.             pend++;
  4480.             if(*pend == ':')
  4481.                 pend++;
  4482.             memcpy(tmp,pstart,size);
  4483.             pstart=pend;
  4484.             radvdCfgParam.interface.prefix[1].Prefix[i]=strtol(tmp,NULL,16);
  4485.         }
  4486.         radvdCfgParam.interface.prefix[1].PrefixLen=atoi(val[25]);
  4487.         radvdCfgParam.interface.prefix[1].AdvOnLinkFlag=atoi(val[26]);
  4488.         radvdCfgParam.interface.prefix[1].AdvAutonomousFlag=atoi(val[26]);
  4489.         radvdCfgParam.interface.prefix[1].AdvValidLifetime=atoi(val[28]);
  4490.         radvdCfgParam.interface.prefix[1].AdvPreferredLifetime=atoi(val[29]);
  4491.         radvdCfgParam.interface.prefix[1].AdvRouterAddr=atoi(val[30]);
  4492.         strcpy(radvdCfgParam.interface.prefix[1].if6to4,val[31]);
  4493.         radvdCfgParam.interface.prefix[1].enabled=atoi(val[32]);
  4494.         value=(void *)&radvdCfgParam;
  4495.         break;
  4496.     case DNSV6_T:
  4497.         if(valNum < 2)
  4498.             printf("input argumentis not enough");
  4499.         dnsv6CfgParam.enabled=atoi(val[0]);
  4500.         strcpy(dnsv6CfgParam.routerName,val[1]);
  4501.         value=(void *)&dnsv6CfgParam;
  4502.         break;
  4503.     case DHCPV6S_T:
  4504.         if(valNum < 5)
  4505.             printf("input argumentis not enough");
  4506.         dhcp6sCfgParam.enabled=atoi(val[0]);
  4507.         strcpy(dhcp6sCfgParam.DNSaddr6,val[1]);
  4508.         strcpy(dhcp6sCfgParam.addr6PoolS,val[2]);
  4509.         strcpy(dhcp6sCfgParam.addr6PoolE,val[3]);
  4510.         strcpy(dhcp6sCfgParam.interfaceNameds,val[4]);
  4511.         value=(void *)&dhcp6sCfgParam;
  4512.         break;
  4513.     case ADDR6_T:
  4514.         if(valNum < 19)
  4515.             printf("input argumentis not enough");
  4516.         addrIPv6CfgParam.enabled=atoi(val[0]);
  4517.         addrIPv6CfgParam.prefix_len[0]=atoi(val[1]);
  4518.         addrIPv6CfgParam.prefix_len[1]=atoi(val[2]);
  4519.  
  4520.         addrIPv6CfgParam.addrIPv6[0][0]=atoi(val[3]);
  4521.         addrIPv6CfgParam.addrIPv6[0][1]=atoi(val[4]);
  4522.         addrIPv6CfgParam.addrIPv6[0][2]=atoi(val[5]);
  4523.         addrIPv6CfgParam.addrIPv6[0][3]=atoi(val[6]);
  4524.         addrIPv6CfgParam.addrIPv6[0][4]=atoi(val[7]);
  4525.         addrIPv6CfgParam.addrIPv6[0][5]=atoi(val[8]);
  4526.         addrIPv6CfgParam.addrIPv6[0][6]=atoi(val[9]);
  4527.         addrIPv6CfgParam.addrIPv6[0][7]=atoi(val[10]);
  4528.  
  4529.         addrIPv6CfgParam.addrIPv6[1][0]=atoi(val[11]);
  4530.         addrIPv6CfgParam.addrIPv6[1][1]=atoi(val[12]);
  4531.         addrIPv6CfgParam.addrIPv6[1][2]=atoi(val[13]);
  4532.         addrIPv6CfgParam.addrIPv6[1][3]=atoi(val[14]);
  4533.         addrIPv6CfgParam.addrIPv6[1][4]=atoi(val[15]);
  4534.         addrIPv6CfgParam.addrIPv6[1][5]=atoi(val[16]);
  4535.         addrIPv6CfgParam.addrIPv6[1][6]=atoi(val[17]);
  4536.         addrIPv6CfgParam.addrIPv6[1][7]=atoi(val[18]);
  4537.         value=(void *)&addrIPv6CfgParam;
  4538.         break;
  4539.     case TUNNEL6_T:
  4540.         tunnelCfgParam.enabled=atoi(val[0]);
  4541.         value=(void *)&tunnelCfgParam;
  4542.         break;
  4543. #endif
  4544. #endif
  4545. #ifdef TLS_CLIENT
  4546.     case CERTROOT_ARRAY_T:
  4547.     if ( !strcmp(val[0], "add")) {
  4548.         id = MIB_CERTROOT_ADD;
  4549.         strcpy(certRoot.comment, val[1]);
  4550.     }
  4551.     else if ( !strcmp(val[0], "del")) {
  4552.             id = MIB_CERTROOT_DEL;
  4553.             if ( valNum < 2 ) {
  4554.                 printf("input argument is not enough!\n");
  4555.                 return;
  4556.             }
  4557.             int_val = atoi(val[1]);
  4558.             if ( !APMIB_GET(MIB_CERTROOT_TBL_NUM, (void *)&entryNum)) {
  4559.                 printf("Get cert ca number error!");
  4560.                 return;
  4561.             }
  4562.             if ( int_val > entryNum ) {
  4563.                 printf("Element number is too large!\n");
  4564.                 return;
  4565.             }
  4566.             *((char *)&certRoot) = (char)int_val;
  4567.             if ( !APMIB_GET(MIB_CERTROOT_TBL, (void *)&certRoot)) {
  4568.                 printf("Get table entry error!");
  4569.                 return;
  4570.             }          
  4571.     }
  4572.     else if ( !strcmp(val[0], "delall"))
  4573.             id = MIB_CERTROOT_DELALL;
  4574.     value = (void *)&certRoot;
  4575.     break;
  4576.     case CERTUSER_ARRAY_T:
  4577.     if ( !strcmp(val[0], "add")) {
  4578.         id = MIB_CERTUSER_ADD;
  4579.         strcpy(certUser.comment, val[1]);
  4580.         strcpy(certUser.pass , val[2]);
  4581.     }
  4582.     else if ( !strcmp(val[0], "del")) {
  4583.             id = MIB_CERTUSER_DEL;
  4584.             if ( valNum < 2 ) {
  4585.                 printf("input argument is not enough!\n");
  4586.                 return;
  4587.             }
  4588.             int_val = atoi(val[1]);
  4589.             if ( !APMIB_GET(MIB_CERTUSER_TBL_NUM, (void *)&entryNum)) {
  4590.                 printf("Get cert ca number error!");
  4591.                 return;
  4592.             }
  4593.             if ( int_val > entryNum ) {
  4594.                 printf("Element number is too large!\n");
  4595.                 return;
  4596.             }
  4597.             *((char *)&certUser) = (char)int_val;
  4598.             if ( !APMIB_GET(MIB_CERTUSER_TBL, (void *)&certUser)) {
  4599.                 printf("Get table entry error!");
  4600.                 return;
  4601.             }          
  4602.     }
  4603.     else if ( !strcmp(val[0], "delall"))
  4604.             id = MIB_CERTUSER_DELALL;
  4605.     value = (void *)&certUser;
  4606.     break; 
  4607. #endif
  4608.     case BYTE13_T:
  4609.         if ( strlen(val[0])!=26 || !string_to_hex(val[0], key, 26)) {
  4610.             printf("invalid value!\n");
  4611.             return;
  4612.         }
  4613.         value = (void *)key;
  4614.         break;
  4615.  
  4616.     case STRING_T:
  4617.         if ( strlen(val[0]) > len) {
  4618.             printf("string value too long!\n");
  4619.             return;
  4620.         }
  4621.         value = (void *)val[0];
  4622.         break;
  4623. #ifdef VOIP_SUPPORT
  4624.     case VOIP_T:
  4625.         // use flash voip set xxx to replace
  4626.         break;
  4627. #endif
  4628.     default: printf("invalid mib!\n"); return;
  4629.     }
  4630.  
  4631.     if ( !APMIB_SET(id, value))
  4632.         printf("set MIB failed!\n");
  4633.  
  4634.     if (config_area) {
  4635.         if (config_area == HW_MIB_AREA || config_area == HW_MIB_WLAN_AREA)
  4636.             apmib_update(HW_SETTING);
  4637.         else if (config_area == DEF_MIB_AREA || config_area == DEF_MIB_WLAN_AREA)
  4638.             apmib_update(DEFAULT_SETTING);
  4639.         else
  4640.             apmib_update(CURRENT_SETTING);
  4641.     }
  4642. }
  4643. static void dumpAllHW(void)
  4644. {
  4645.     int idx=0, num;
  4646.     mib_table_entry_T *pTbl=NULL;
  4647.  
  4648.     if ( !apmib_init_HW()) {
  4649.         printf("Initialize AP MIB failed!\n");
  4650.         return;
  4651.     }
  4652. #ifdef MBSSID
  4653.     vwlan_idx=0;
  4654. #endif
  4655.      config_area=0;
  4656.  
  4657. next_tbl:
  4658.     if (++config_area > HW_MIB_WLAN_AREA)
  4659.         return;
  4660.     if (config_area == HW_MIB_AREA)
  4661.         pTbl = hwmib_table;
  4662.     else if (config_area == HW_MIB_WLAN_AREA)
  4663.         pTbl = hwmib_wlan_table;
  4664.    
  4665.  
  4666. next_wlan:
  4667.     while (pTbl[idx].id) {
  4668.             num = 1;
  4669.         if (num >0) {
  4670.             if ( config_area == HW_MIB_AREA ||
  4671.                 config_area == HW_MIB_WLAN_AREA)
  4672.             {
  4673.                 printf("HW_");
  4674.                 if (config_area == HW_MIB_WLAN_AREA) {
  4675.                     printf("WLAN%d_", wlan_idx);
  4676.                 }              
  4677.             }
  4678. #ifdef MIB_TLV
  4679.             if(pTbl[idx].type == TABLE_LIST_T) // ignore table root entry. keith
  4680.             {
  4681.                 idx++;
  4682.                 continue;
  4683.             }
  4684. #endif // #ifdef MIB_TLV           
  4685.             getMIB(pTbl[idx].name, pTbl[idx].id, pTbl[idx].type, num, 1 , NULL);
  4686.         }
  4687.         idx++;
  4688.     }
  4689.     idx = 0;
  4690.  
  4691.     if (config_area == HW_MIB_WLAN_AREA ) {
  4692.         if (++wlan_idx < NUM_WLAN_INTERFACE)
  4693.             goto next_wlan;
  4694.         else
  4695.             wlan_idx = 0;      
  4696.     }
  4697.    
  4698.     goto next_tbl;
  4699. }
  4700.  
  4701.  
  4702.  
  4703. ////////////////////////////////////////////////////////////////////////////////
  4704. static void dumpAll(void)
  4705. {
  4706.     int idx=0, num;
  4707.     mib_table_entry_T *pTbl=NULL;
  4708.  
  4709.     if ( !apmib_init()) {
  4710.         printf("Initialize AP MIB failed!\n");
  4711.         return;
  4712.     }
  4713.  
  4714. #ifdef MBSSID
  4715.     vwlan_idx=0;
  4716. #endif
  4717.      config_area=0;
  4718.  
  4719. next_tbl:
  4720.     if (++config_area > MIB_WLAN_AREA)
  4721.         return;
  4722.     if (config_area == HW_MIB_AREA)
  4723.         pTbl = hwmib_table;
  4724.     else if (config_area == HW_MIB_WLAN_AREA)
  4725.         pTbl = hwmib_wlan_table;
  4726.     else if (config_area == DEF_MIB_AREA || config_area == MIB_AREA)
  4727.         pTbl = mib_table;
  4728.     else if (config_area == DEF_MIB_WLAN_AREA || config_area == MIB_WLAN_AREA)
  4729.         pTbl = mib_wlan_table;
  4730.  
  4731. next_wlan:
  4732.     while (pTbl[idx].id) {
  4733.         if ( pTbl[idx].id == MIB_WLAN_MACAC_ADDR)
  4734.             APMIB_GET(MIB_WLAN_MACAC_NUM, (void *)&num);       
  4735. #if defined(CONFIG_RTK_MESH) && defined(_MESH_ACL_ENABLE_) // below code copy above ACL code
  4736.         else if ( pTbl[idx].id == MIB_MESH_ACL_ADDR)
  4737.             APMIB_GET(MIB_MESH_ACL_NUM, (void *)&num);
  4738. #endif
  4739.  
  4740.         else if ( pTbl[idx].id == MIB_WLAN_WDS)
  4741.             APMIB_GET(MIB_WLAN_WDS_NUM, (void *)&num);
  4742.         else if ( pTbl[idx].id == MIB_WLAN_SCHEDULE_TBL)
  4743.             APMIB_GET(MIB_WLAN_SCHEDULE_TBL_NUM, (void *)&num);    
  4744.            
  4745. #if defined(VLAN_CONFIG_SUPPORTED)             
  4746.         else if ( pTbl[idx].id == MIB_VLANCONFIG_TBL){
  4747.             APMIB_GET(MIB_VLANCONFIG_TBL_NUM, (void *)&num);
  4748.         }
  4749. #endif                     
  4750. #ifdef HOME_GATEWAY
  4751.         else if ( pTbl[idx].id == MIB_PORTFW_TBL)
  4752.             APMIB_GET(MIB_PORTFW_TBL_NUM, (void *)&num);
  4753.         else if ( pTbl[idx].id == MIB_PORTFILTER_TBL)
  4754.             APMIB_GET(MIB_PORTFILTER_TBL_NUM, (void *)&num);
  4755.         else if ( pTbl[idx].id == MIB_IPFILTER_TBL)
  4756.             APMIB_GET(MIB_IPFILTER_TBL_NUM, (void *)&num);
  4757.         else if ( pTbl[idx].id == MIB_MACFILTER_TBL)
  4758.             APMIB_GET(MIB_MACFILTER_TBL_NUM, (void *)&num);
  4759.         else if ( pTbl[idx].id == MIB_URLFILTER_TBL)
  4760.             APMIB_GET(MIB_URLFILTER_TBL_NUM, (void *)&num);
  4761.         else if ( pTbl[idx].id == MIB_TRIGGERPORT_TBL)
  4762.             APMIB_GET(MIB_TRIGGERPORT_TBL_NUM, (void *)&num);
  4763. #if defined(GW_QOS_ENGINE) || defined(QOS_BY_BANDWIDTH)
  4764.         else if ( pTbl[idx].id == MIB_QOS_RULE_TBL)
  4765.             APMIB_GET(MIB_QOS_RULE_TBL_NUM, (void *)&num);
  4766. #endif
  4767. #ifdef ROUTE_SUPPORT
  4768.         else if ( pTbl[idx].id == MIB_STATICROUTE_TBL)
  4769.             APMIB_GET(MIB_STATICROUTE_TBL_NUM, (void *)&num);
  4770. #endif //ROUTE
  4771. #ifdef VPN_SUPPORT
  4772.         else if ( pTbl[idx].id == MIB_IPSECTUNNEL_TBL)
  4773.             APMIB_GET(MIB_IPSECTUNNEL_TBL_NUM, (void *)&num);
  4774. #endif
  4775. #endif // #ifdef HOME_GATEWAY
  4776. #ifdef TLS_CLIENT
  4777.         else if ( pTbl[idx].id == MIB_CERTROOT_TBL)
  4778.             APMIB_GET(MIB_CERTROOT_TBL_NUM, (void *)&num);
  4779.         else if ( pTbl[idx].id == MIB_CERTUSER_TBL)
  4780.             APMIB_GET(MIB_CERTUSER_TBL_NUM, (void *)&num);         
  4781. #endif
  4782.         else if ( pTbl[idx].id == MIB_DHCPRSVDIP_TBL)
  4783.         {
  4784.             APMIB_GET(MIB_DHCPRSVDIP_TBL_NUM, (void *)&num);           
  4785.         }
  4786.         else
  4787.         {
  4788.             num = 1;
  4789.         }
  4790.  
  4791.         if (num >0) {          
  4792. #ifdef MIB_TLV
  4793.             /*skip printf HW_ or DEF_ prefix when table root entry*/
  4794.             if(pTbl[idx].type == TABLE_LIST_T) // ignore table root entry. keith
  4795.             {
  4796.                 idx++;
  4797.                 continue;
  4798.             }
  4799. #endif // #ifdef MIB_TLV
  4800.             if ( config_area == HW_MIB_AREA ||
  4801.                 config_area == HW_MIB_WLAN_AREA)
  4802.             {
  4803.                 printf("HW_");
  4804.             }
  4805.             else if (config_area == DEF_MIB_AREA || config_area == DEF_MIB_WLAN_AREA)
  4806.                 printf("DEF_");
  4807.  
  4808.             if (config_area == HW_MIB_WLAN_AREA || config_area == DEF_MIB_WLAN_AREA || config_area == MIB_WLAN_AREA) {
  4809. #ifdef MBSSID
  4810.                 if ((config_area == DEF_MIB_WLAN_AREA || config_area == MIB_WLAN_AREA) && vwlan_idx > 0)
  4811.                     printf("WLAN%d_VAP%d_", wlan_idx, vwlan_idx-1);
  4812.                 else
  4813. #endif
  4814.                 printf("WLAN%d_", wlan_idx);
  4815.             }
  4816.            
  4817.             getMIB(pTbl[idx].name, pTbl[idx].id,
  4818.                         pTbl[idx].type, num, 1 , NULL);
  4819.         }
  4820.         idx++;
  4821.     }
  4822.     idx = 0;
  4823.  
  4824.     if (config_area == HW_MIB_WLAN_AREA || config_area == DEF_MIB_WLAN_AREA || config_area == MIB_WLAN_AREA) {
  4825. #ifdef MBSSID
  4826.         if (config_area == DEF_MIB_WLAN_AREA || config_area == MIB_WLAN_AREA) {
  4827.                
  4828.             if (++vwlan_idx <= NUM_VWLAN_INTERFACE)
  4829.                 goto next_wlan;
  4830.             else
  4831.                 vwlan_idx = 0;
  4832.         }
  4833. #endif
  4834.         if (++wlan_idx < NUM_WLAN_INTERFACE)
  4835.             goto next_wlan;
  4836.         else
  4837.             wlan_idx = 0;      
  4838.     }
  4839.    
  4840.     goto next_tbl;
  4841. }
  4842.  
  4843. //////////////////////////////////////////////////////////////////////////////////
  4844. static void showHelp(void)
  4845. {
  4846.     printf("Usage: flash cmd\n");
  4847.     printf("option:\n");
  4848.     printf("cmd:\n");
  4849.     printf("      default -- write all flash parameters from hard code.\n");
  4850. #ifdef VOIP_SUPPORT
  4851.     printf("      default-sw -- write flash default/current setting from hard code.\n");
  4852. #endif
  4853.     printf("      get [wlan interface-index] mib-name -- get a specific mib from flash\n");
  4854.     printf("          memory.\n");
  4855.     printf("      set [wlan interface-index] mib-name mib-value -- set a specific mib into\n");
  4856.     printf("          flash memory.\n");
  4857.     printf("      all -- dump all flash parameters.\n");
  4858.     printf("      gethw hw-mib-name -- get a specific mib from flash\n");
  4859.     printf("          memory.\n");
  4860.     printf("      sethw hw-mib-name mib-value -- set a specific mib into\n");
  4861.     printf("          flash memory.\n");
  4862.     printf("      allhw -- dump all hw flash parameters.\n");
  4863. #ifdef VOIP_SUPPORT
  4864.     printf("      voip -- dump all voip mib parameters.\n");
  4865.     printf("      voip get mib-name -- get a specific VoIP mib value.\n");
  4866.     printf("      voip set mib-name mib-vale -- set a specific VoIP mib value.\n");
  4867. #endif
  4868.     printf("      reset -- reset current setting to default.\n");
  4869. #ifdef WLAN_FAST_INIT
  4870.     printf("      set_mib -- get mib from flash and set to wlan interface.\n");
  4871. #endif
  4872.     printf("\n");
  4873. }
  4874.  
  4875. //////////////////////////////////////////////////////////////////////////////////
  4876. static void showAllHWMibName(void)
  4877. {
  4878.     int idx;
  4879.     mib_table_entry_T *pTbl;
  4880.  
  4881.     config_area = 0;
  4882.     while (config_area++ < 7) {
  4883.         idx = 0;
  4884.         if (config_area == HW_MIB_AREA || config_area == HW_MIB_WLAN_AREA) {
  4885.             if (config_area == HW_MIB_AREA)
  4886.                 pTbl = hwmib_table;
  4887.             else
  4888.                 pTbl = hwmib_wlan_table;
  4889.             while (pTbl[idx].id) {
  4890.                 printf("HW_%s\n", pTbl[idx].name);
  4891.                 idx++;
  4892.             }
  4893.         }
  4894.     }
  4895. }
  4896. //////////////////////////////////////////////////////////////////////////////////
  4897. static void showAllMibName(void)
  4898. {
  4899.     int idx;
  4900.     mib_table_entry_T *pTbl;
  4901.  
  4902.     config_area = 0;
  4903.     while (config_area++ < 7) {
  4904.         idx = 0;
  4905.         if (config_area == HW_MIB_AREA || config_area == HW_MIB_WLAN_AREA) {
  4906.             if (config_area == HW_MIB_AREA)
  4907.                 pTbl = hwmib_table;
  4908.             else
  4909.                 pTbl = hwmib_wlan_table;
  4910.             while (pTbl[idx].id) {
  4911.                 printf("HW_%s\n", pTbl[idx].name);
  4912.                 idx++;
  4913.             }
  4914.         }
  4915.         else {
  4916.             if (config_area == DEF_MIB_AREA || config_area == MIB_AREA)
  4917.                 pTbl = mib_table;
  4918.             else
  4919.                 pTbl = mib_wlan_table;
  4920.  
  4921.             if (config_area == DEF_MIB_AREA || config_area == DEF_MIB_WLAN_AREA)
  4922.                 printf("DEF_");
  4923.  
  4924.             while (pTbl[idx].id) {
  4925.                 printf("%s\n", pTbl[idx].name);
  4926.                 idx++;
  4927.             }
  4928.         }
  4929.     }
  4930. }
  4931.  
  4932. ///////////////////////////////////////////////////////////////////////////////////
  4933. static void showSetACHelp(void)
  4934. {
  4935. #if 0
  4936.     printf("flash set MACAC_ADDR cmd\n");
  4937.     printf("cmd:\n");
  4938.     printf("      add mac-addr comment -- append a filter mac address.\n");
  4939.     printf("      del entry-number -- delete a filter entry.\n");
  4940.     printf("      delall -- delete all filter mac address.\n");
  4941. #endif 
  4942. }
  4943.  
  4944. #if defined(CONFIG_RTK_MESH) && defined(_MESH_ACL_ENABLE_) // below code copy above ACL code
  4945. static void showSetMeshACLHelp(void)
  4946. {
  4947. #if 0
  4948.     printf("flash set MESH_ACL_ADDR cmd\n");
  4949.     printf("cmd:\n");
  4950.     printf("      add mac-addr comment -- append a filter mac address.\n");
  4951.     printf("      del entry-number -- delete a filter entry.\n");
  4952.     printf("      delall -- delete all filter mac address.\n");
  4953. #endif 
  4954. }
  4955. #endif
  4956.  
  4957. #ifdef VLAN_CONFIG_SUPPORTED
  4958. ////////////////////////////////////////////////////////////////////////////////////
  4959. static void showSetVlanConfigHelp(void)
  4960. {
  4961. #if 0
  4962.     printf("flash set VLAN CONFIG  cmd\n");
  4963.     printf("cmd:\n");
  4964.     printf("      add enable iface -- update vlan config for specific iface.\n");
  4965.     printf("      del entry-number -- delete a vlan config entry.\n");
  4966.     printf("      delall -- delete all vlan config entry\n");
  4967. #endif     
  4968. }
  4969. #endif
  4970.  
  4971. ///////////////////////////////////////////////////////////////////////////////////
  4972. static void showSetWdsHelp(void)
  4973. {
  4974. #if 0
  4975.     printf("flash set WDS cmd\n");
  4976.     printf("cmd:\n");
  4977.     printf("      add mac-addr comment -- append a WDS mac address.\n");
  4978.     printf("      del entry-number -- delete a WDS entry.\n");
  4979.     printf("      delall -- delete all WDS mac address.\n");
  4980. #endif 
  4981. }
  4982.  
  4983. #ifdef HOME_GATEWAY
  4984. ///////////////////////////////////////////////////////////////////////////////////
  4985. static void showSetPortFwHelp(void)
  4986. {
  4987. #if 0
  4988.     printf("flash set PORTFW_TBL cmd\n");
  4989.     printf("cmd:\n");
  4990.     printf("      add ip from-port to-port protocol comment -- add a filter.\n");
  4991.     printf("      del entry-number -- delete a filter.\n");
  4992.     printf("      delall -- delete all filter.\n");
  4993. #endif 
  4994. }
  4995.  
  4996.  
  4997. ///////////////////////////////////////////////////////////////////////////////////
  4998. static void showSetPortFilterHelp(void)
  4999. {
  5000. #if 0
  5001.     printf("flash set PORTFILTER_TBL cmd\n");
  5002.     printf("cmd:\n");
  5003.     printf("      add from-port to-port protocol comment -- add a filter.\n");
  5004.     printf("      del entry-number -- delete a filter.\n");
  5005.     printf("      delall -- delete all filter.\n");
  5006. #endif 
  5007. }
  5008.  
  5009. ///////////////////////////////////////////////////////////////////////////////////
  5010. static void showSetIpFilterHelp(void)
  5011. {
  5012. #if 0
  5013.     printf("flash set IPFILTER_TBL cmd\n");
  5014.     printf("cmd:\n");
  5015.     printf("      add ip protocol comment -- add a filter.\n");
  5016.     printf("      del entry-number -- delete a filter.\n");
  5017.     printf("      delall -- delete all filter.\n");
  5018. #endif 
  5019. }
  5020.  
  5021. ///////////////////////////////////////////////////////////////////////////////////
  5022. static void showSetMacFilterHelp(void)
  5023. {
  5024. #if 0
  5025.     printf("flash set MACFILTER_TBL cmd\n");
  5026.     printf("cmd:\n");
  5027.     printf("      add mac-addr comment -- add a filter.\n");
  5028.     printf("      del entry-number -- delete a filter.\n");
  5029.     printf("      delall -- delete all filter.\n");
  5030. #endif 
  5031. }
  5032. ///////////////////////////////////////////////////////////////////////////////////
  5033. static void showSetUrlFilterHelp(void)
  5034. {
  5035. #if 0
  5036.     printf("flash set URLFILTER_TBL cmd\n");
  5037.     printf("cmd:\n");
  5038.     printf("      add url-addr -- add a filter.\n");
  5039.     printf("      del entry-number -- delete a filter.\n");
  5040.     printf("      delall -- delete all filter.\n");
  5041. #endif 
  5042. }
  5043. ///////////////////////////////////////////////////////////////////////////////////
  5044. static void showSetTriggerPortHelp(void)
  5045. {
  5046. #if 0
  5047.     printf("flash set TRIGGER_PORT cmd\n");
  5048.     printf("cmd:\n");
  5049.     printf("   add trigger-from trigger-to trigger-proto incoming-from incoming-to incoming-proto comment -- add a trigger-port.\n");
  5050.     printf("   del entry-number -- delete a trigger-port.\n");
  5051.     printf("   delall -- delete all trigger-port.\n");
  5052. #endif 
  5053. }
  5054.  
  5055.  
  5056.  
  5057.  
  5058. #ifdef GW_QOS_ENGINE
  5059. ///////////////////////////////////////////////////////////////////////////////////
  5060. //static void showSetQosHelp(void) {}
  5061. #endif
  5062. ///////////////////////////////////////////////////////////////////////////////////
  5063. #ifdef ROUTE_SUPPORT
  5064. static void showSetStaticRouteHelp(void)
  5065. {
  5066.     printf("flash set STATICROUTE_TBL cmd\n");
  5067.     printf("cmd:\n");
  5068.     printf("   add dest_ip netmask gateway  -- add a static route.\n");
  5069.     printf("   del entry-number -- delete a static route.\n");
  5070.     printf("   delall -- delete all static route.\n");
  5071.  
  5072.  
  5073. }
  5074. #endif //ROUTE
  5075. #endif
  5076.  
  5077. #ifdef HOME_GATEWAY
  5078. #ifdef VPN_SUPPORT
  5079. static void  showSetIpsecTunnelHelp(void)
  5080. {
  5081.         printf("flash set IPSECTUNNEL_TBL cmd\n");
  5082.         printf("cmd:\n");
  5083.         printf("   add tunnel_id enable name local_type local_ip local_mask_len remote_type remote_ip remote_mask_len remote_gw keymode connectType espEncr espAuth psKey ike_encr ike_auth ike_keygroup ike_lifetime ipsec_lifetime ipsec_pfs spi encrKey authKey -- add a ipsec manual tunnel.\n");
  5084.         printf("   del entry-number -- delete a vpn tunnel.\n");
  5085.         printf("   delall -- delete all tunnel.\n");
  5086. }
  5087. #endif
  5088. #endif
  5089.  
  5090. #ifdef TLS_CLIENT
  5091. static void  showSetCertRootHelp(void)
  5092. {
  5093.         printf("flash set CERTROOT_TBL cmd\n");
  5094.         printf("cmd:\n");
  5095.         printf("   add comment.\n");
  5096.         printf("   del entry-number -- delete a certca .\n");
  5097.         printf("   delall -- delete all certca.\n");
  5098. }
  5099. static void  showSetCertUserHelp(void)
  5100. {
  5101.         printf("flash set CERTUSER_TBL cmd\n");
  5102.         printf("cmd:\n");
  5103.         printf("   add comment password.\n");
  5104.         printf("   del entry-number -- delete a certca .\n");
  5105.         printf("   delall -- delete all certca.\n");
  5106. }
  5107. #endif
  5108.  
  5109. #ifdef PARSE_TXT_FILE
  5110. ////////////////////////////////////////////////////////////////////////////////
  5111. static int parseTxtConfig(char *filename, APMIB_Tp pConfig)
  5112. {
  5113.     char line[300], value[300];
  5114.     FILE *fp;
  5115.     int id;
  5116.  
  5117.     fp = fopen(filename, "r");
  5118.     if ( fp == NULL )
  5119.         return -1;
  5120.  
  5121.     acNum = 0;
  5122.    
  5123. #if defined(CONFIG_RTK_MESH) && defined(_MESH_ACL_ENABLE_)
  5124.     meshAclNum = 0;
  5125. #endif
  5126.  
  5127.     wdsNum = 0;
  5128.  
  5129. #ifdef HOME_GATEWAY
  5130.     portFilterNum = ipFilterNum = macFilterNum = portFwNum = staticRouteNum=0;
  5131.     urlFilterNum = 0;
  5132.  
  5133. #if defined(GW_QOS_ENGINE) || defined(QOS_BY_BANDWIDTH)
  5134.     qosRuleNum = 0;
  5135. #endif
  5136. #endif
  5137. #ifdef TLS_CLIENT
  5138.     certRootNum =  certUserNum = 0;
  5139. #endif
  5140.  
  5141. #if defined(VLAN_CONFIG_SUPPORTED)
  5142.  vlanConfigNum=0;  
  5143. #endif
  5144.     while ( fgets(line, 100, fp) ) {
  5145.         id = getToken(line, value);
  5146.         if ( id == 0 )
  5147.             continue;
  5148.         if ( set_mib(pConfig, id, value) < 0) {
  5149.             printf("Parse MIB [%d] error!\n", id );
  5150.             fclose(fp);
  5151.             return -1;
  5152.         }
  5153.     }
  5154.  
  5155.     fclose(fp);
  5156.     return 0;
  5157. }
  5158.  
  5159. ////////////////////////////////////////////////////////////////////////////////
  5160. static int getToken(char *line, char *value)
  5161. {
  5162.     char *ptr=line, *p1;
  5163.     char token[300]={0};
  5164.     int len=0, idx;
  5165.  
  5166.     if ( *ptr == ';' )  // comments
  5167.         return 0;
  5168.  
  5169.     // get token
  5170.     while (*ptr && *ptr!=EOL) {
  5171.         if ( *ptr == '=' ) {
  5172.             memcpy(token, line, len);
  5173.  
  5174.             // delete ending space
  5175.             for (idx=len-1; idx>=0; idx--) {
  5176.                 if (token[idx]!= SPACE )
  5177.                     break;
  5178.             }
  5179.             token[idx+1] = '\0';
  5180.             ptr++;
  5181.             break;
  5182.         }
  5183.         ptr++;
  5184.         len++;
  5185.     }
  5186.     if ( !token[0] )
  5187.         return 0;
  5188.  
  5189.     // get value
  5190.     len=0;
  5191.     while (*ptr == SPACE ) ptr++; // delete space
  5192.  
  5193.     p1 = ptr;
  5194.     while ( *ptr && *ptr!=EOL) {
  5195.         ptr++;
  5196.         len++;
  5197.     }
  5198.     memcpy(value, p1, len );
  5199.     value[len] = '\0';
  5200.  
  5201.     idx = 0;
  5202.     while (mib_table[idx].id) {
  5203.         if (!strcmp(mib_table[idx].name, token))
  5204.             return mib_table[idx].id;
  5205.         idx++;
  5206.     }
  5207.     return 0;
  5208. }
  5209.  
  5210.  
  5211. ////////////////////////////////////////////////////////////////////////////////
  5212. static int set_mib(APMIB_Tp pMib, int id, void *value)
  5213. {
  5214.     unsigned char key[100];
  5215.     char *p1, *p2;
  5216. #ifdef HOME_GATEWAY
  5217.     char *p3, *p4, *p5;
  5218. #if defined(GW_QOS_ENGINE) || defined(VLAN_CONFIG_SUPPORTED) && !defined(QOS_BY_BANDWIDTH)
  5219.     char *p6, *p7, *p8, *p9, *p10, *p11, *p12;
  5220. #endif
  5221.  
  5222. #if defined(QOS_BY_BANDWIDTH) && !defined(GW_QOS_ENGINE)
  5223.  
  5224. #if defined(VLAN_CONFIG_SUPPORTED)
  5225.     char *p6, *p7, *p8;
  5226. #else
  5227.     char *p6, *p7;
  5228. #endif
  5229.  
  5230. #endif
  5231.  
  5232. #else
  5233.  
  5234. #if defined(VLAN_CONFIG_SUPPORTED) 
  5235.     char *p3, *p4, *p5, *p6, *p7, *p8;
  5236. #endif
  5237.  
  5238. #endif
  5239.     struct in_addr inAddr;
  5240.     int i;
  5241.     MACFILTER_Tp pWlAc;
  5242.     WDS_Tp pWds;
  5243.  
  5244. #ifdef HOME_GATEWAY
  5245.     PORTFW_Tp pPortFw;
  5246.     PORTFILTER_Tp pPortFilter;
  5247.     IPFILTER_Tp pIpFilter;
  5248.     MACFILTER_Tp pMacFilter;
  5249.     URLFILTER_Tp pUrlFilter;
  5250.  
  5251. #ifdef GW_QOS_ENGINE
  5252.     QOS_Tp pQos;    
  5253. #endif
  5254.  
  5255. #ifdef QOS_BY_BANDWIDTH
  5256.     IPQOS_Tp pQos;    
  5257. #endif
  5258. #endif
  5259.  
  5260. #ifdef TLS_CLIENT
  5261.     CERTROOT_Tp pCertRoot;
  5262.     CERTUSER_Tp pCertUser;
  5263. #endif
  5264.     DHCPRSVDIP_Tp pDhcpRsvd;
  5265. #if defined(VLAN_CONFIG_SUPPORTED) 
  5266.     VLAN_CONFIG_Tp pVlanConfig;
  5267.     int j;
  5268. #endif 
  5269.     for (i=0; mib_table[i].id; i++) {
  5270.         if ( mib_table[i].id == id )
  5271.             break;
  5272.     }
  5273.     if ( mib_table[i].id == 0 )
  5274.         return -1;
  5275.  
  5276.     switch (mib_table[i].type) {
  5277.     case BYTE_T:
  5278.         *((unsigned char *)(((long)pMib) + mib_table[i].offset)) = (unsigned char)atoi(value);
  5279.         break;
  5280.  
  5281.     case WORD_T:
  5282.         *((unsigned short *)(((long)pMib) + mib_table[i].offset)) = (unsigned short)atoi(value);
  5283.         break;
  5284.  
  5285.     case STRING_T:
  5286.         if ( strlen(value)+1 > mib_table[i].size )
  5287.             return 0;
  5288.         strcpy((char *)(((long)pMib) + mib_table[i].offset), (char *)value);
  5289.         break;
  5290.  
  5291.     case BYTE5_T:
  5292.         if (strlen(value)!=10 || !string_to_hex(value, key, 10))
  5293.             return -1;
  5294.         memcpy((unsigned char *)(((long)pMib) + mib_table[i].offset), key, 5);
  5295.         break;
  5296.  
  5297.     case BYTE6_T:
  5298.         if (strlen(value)!=12 || !string_to_hex(value, key, 12))
  5299.             return -1;
  5300.         memcpy((unsigned char *)(((long)pMib) + mib_table[i].offset), key, 6);
  5301.         break;
  5302.  
  5303.     case BYTE13_T:
  5304.         if (strlen(value)!=26 || !string_to_hex(value, key, 26))
  5305.             return -1;
  5306.         memcpy((unsigned char *)(((long)pMib) + mib_table[i].offset), key, 13);
  5307.         break;
  5308.    
  5309.     case DWORD_T:
  5310.         *((unsigned long *)(((long)pMib) + mib_table[i].offset)) = (unsigned long)atoi(value);
  5311.         break;
  5312.  
  5313.     case IA_T:
  5314.         if ( !inet_aton(value, &inAddr) )
  5315.             return -1;
  5316.         memcpy((unsigned char *)(((long)pMib) + mib_table[i].offset), (unsigned char *)&inAddr,  4);
  5317.         break;
  5318.  
  5319.     // CONFIG_RTK_MESH Note: The statement haven't use maybe, Because mib_table haven't WLAC_ARRAY_T
  5320.     case WLAC_ARRAY_T:
  5321.         getVal2((char *)value, &p1, &p2);
  5322.         if (p1 == NULL) {
  5323.             printf("Invalid WLAC in argument!\n");
  5324.             break;
  5325.         }
  5326.         if (strlen(p1)!=12 || !string_to_hex(p1, key, 12))
  5327.             return -1;
  5328.  
  5329.         pWlAc = (MACFILTER_Tp)(((long)pMib)+mib_table[i].offset+acNum*sizeof(MACFILTER_T));
  5330.         memcpy(pWlAc->macAddr, key, 6);
  5331.         if (p2 != NULL )
  5332.             strcpy(pWlAc->comment, p2);
  5333.         acNum++;
  5334.         break;
  5335.  
  5336. //#if defined(CONFIG_RTK_MESH) && defined(_MESH_ACL_ENABLE_) // below code copy above ACL code
  5337.     case MESH_ACL_ARRAY_T:
  5338.         getVal2((char *)value, &p1, &p2);
  5339.         if (p1 == NULL) {
  5340.             printf("Invalid Mesh Acl in argument!\n");
  5341.             break;
  5342.         }
  5343.         if (strlen(p1)!=12 || !string_to_hex(p1, key, 12))
  5344.             return -1;
  5345.  
  5346.         pWlAc = (MACFILTER_Tp)(((long)pMib)+mib_table[i].offset+meshAclNum*sizeof(MACFILTER_T));
  5347.         memcpy(pWlAc->macAddr, key, 6);
  5348.         if (p2 != NULL )
  5349.             strcpy(pWlAc->comment, p2);
  5350.         meshAclNum++;
  5351.         break;
  5352. //#endif
  5353.  
  5354.     case WDS_ARRAY_T:
  5355.         getVal3((char *)value, &p1, &p2, &p3);
  5356.         if (p1 == NULL) {
  5357.             printf("Invalid WDS in argument!\n");
  5358.             break;
  5359.         }
  5360.         if (strlen(p1)!=12 || !string_to_hex(p1, key, 12))
  5361.             return -1;
  5362.  
  5363.         pWds = (WDS_Tp)(((long)pMib)+mib_table[i].offset+wdsNum*sizeof(WDS_T));
  5364.         memcpy(pWds->macAddr, key, 6);
  5365.         if (p2 != NULL )
  5366.             strcpy(pWds->comment, p2);
  5367.         pWds->fixedTxRate = (unsigned int)atoi(p3);
  5368.         wdsNum++;
  5369.         break;
  5370.  
  5371. #ifdef HOME_GATEWAY
  5372.     case MACFILTER_ARRAY_T:
  5373.         getVal2((char *)value, &p1, &p2);
  5374.         if (p1 == NULL) {
  5375.             printf("Invalid MACFILTER in argument!\n");
  5376.             break;
  5377.         }
  5378.         if (strlen(p1)!=12 || !string_to_hex(p1, key, 12))
  5379.             return -1;
  5380.  
  5381.         pMacFilter = (MACFILTER_Tp)(((long)pMib)+mib_table[i].offset+macFilterNum*sizeof(MACFILTER_T));
  5382.         memcpy(pMacFilter->macAddr, key, 6);
  5383.         if (p2 != NULL )
  5384.             strcpy(pMacFilter->comment, p2);
  5385.         macFilterNum++;
  5386.         break;
  5387.  
  5388.     case URLFILTER_ARRAY_T:
  5389.         getVal2((char *)value, &p1, &p2);
  5390.         if (p1 == NULL) {
  5391.             printf("Invalid URLFILTER in argument!\n");
  5392.             break;
  5393.         }
  5394.         //if (strlen(p1)!=12 || !string_to_hex(p1, key, 12))
  5395.         //  return -1;
  5396.  
  5397.         pUrlFilter = (URLFILTER_Tp)(((long)pMib)+mib_table[i].offset+urlFilterNum*sizeof(URLFILTER_T));
  5398.         if(!strncmp(pEntry->urlAddr,"http://",7))              
  5399.             memcpy(pUrlFilter->urlAddr+7, key, 20);
  5400.         else
  5401.             memcpy(pUrlFilter->urlAddr, key, 20);
  5402.        
  5403.         //if (p2 != NULL )
  5404.         //  strcpy(pMacFilter->comment, p2);
  5405.         urlFilterNum++;
  5406.         break;
  5407.  
  5408.     case PORTFW_ARRAY_T:
  5409.         getVal5((char *)value, &p1, &p2, &p3, &p4, &p5);
  5410.         if (p1 == NULL || p2 == NULL || p3 == NULL || p4 == NULL ) {
  5411.             printf("Invalid PORTFW arguments!\n");
  5412.             break;
  5413.         }
  5414.         if ( !inet_aton(p1, &inAddr) )
  5415.             return -1;
  5416.  
  5417.         pPortFw = (PORTFW_Tp)(((long)pMib)+mib_table[i].offset+portFwNum*sizeof(PORTFW_T));
  5418.         memcpy(pPortFw->ipAddr, (unsigned char *)&inAddr, 4);
  5419.         pPortFw->fromPort = (unsigned short)atoi(p2);
  5420.         pPortFw->toPort = (unsigned short)atoi(p3);
  5421.         pPortFw->protoType = (unsigned char)atoi(p4);
  5422.         if ( p5 )
  5423.             strcpy( pPortFw->comment, p5 );
  5424.         portFwNum++;
  5425.         break;
  5426.  
  5427.     case IPFILTER_ARRAY_T:
  5428.         getVal3((char *)value, &p1, &p2, &p3);
  5429.         if (p1 == NULL || p2 == NULL) {
  5430.             printf("Invalid IPFILTER arguments!\n");
  5431.             break;
  5432.         }
  5433.         if ( !inet_aton(p1, &inAddr) )
  5434.             return -1;
  5435.         pIpFilter = (IPFILTER_Tp)(((long)pMib)+mib_table[i].offset+ipFilterNum*sizeof(IPFILTER_T));
  5436.         memcpy(pIpFilter->ipAddr, (unsigned char *)&inAddr, 4);
  5437.         pIpFilter->protoType = (unsigned char)atoi(p2);
  5438.         if ( p3 )
  5439.             strcpy( pIpFilter->comment, p3 );
  5440.         ipFilterNum++;
  5441.         break;
  5442.  
  5443.     case PORTFILTER_ARRAY_T:
  5444.         getVal4((char *)value, &p1, &p2, &p3, &p4);
  5445.         if (p1 == NULL || p2 == NULL || p3 == NULL) {
  5446.             printf("Invalid PORTFILTER arguments!\n");
  5447.             break;
  5448.         }
  5449.         if ( !inet_aton(p1, &inAddr) )
  5450.             return -1;
  5451.         pPortFilter = (PORTFILTER_Tp)(((long)pMib)+mib_table[i].offset+portFilterNum*sizeof(PORTFILTER_T));
  5452.         pPortFilter->fromPort = (unsigned short)atoi(p1);
  5453.         pPortFilter->toPort = (unsigned short)atoi(p2);
  5454.         pPortFilter->protoType = (unsigned char)atoi(p3);
  5455.         if ( p4 )
  5456.             strcpy( pPortFilter->comment, p4 );
  5457.         portFilterNum++;
  5458.         break;
  5459. #ifdef GW_QOS_ENGINE
  5460.     case QOS_ARRAY_T:
  5461.         getVal12((char *)value, &p1, &p2, &p3, &p4, &p5, &p6, &p7, &p8, &p9, &p10, &p11, &p12);
  5462.         if (p1 == NULL || p2 == NULL || p3 == NULL || p4 == NULL || p5 == NULL || p6 == NULL || p7 == NULL ||
  5463.             p8 == NULL || p9 == NULL || p10 == NULL || p11 == NULL || p12 == NULL ) {
  5464.             printf("Invalid QoS arguments!\n");
  5465.             break;
  5466.         }
  5467.         pQos = (QOS_Tp)(((long)pMib)+mib_table[i].offset+qosRuleNum*sizeof(QOS_T));
  5468.         pQos->enabled = (unsigned char)atoi(p1);
  5469.         pQos->priority = (unsigned char)atoi(p2);
  5470.         pQos->protocol = (unsigned short)atoi(p3);
  5471.         if ( !inet_aton(p4, &inAddr) )
  5472.             return -1;
  5473.         memcpy(pQos->local_ip_start, (unsigned char *)&inAddr, 4);
  5474.         if ( !inet_aton(p5, &inAddr) )
  5475.             return -1;
  5476.         memcpy(pQos->local_ip_end, (unsigned char *)&inAddr, 4);
  5477.        
  5478.         pQos->local_port_start = (unsigned short)atoi(p6);
  5479.         pQos->local_port_end = (unsigned short)atoi(p7);
  5480.         if ( !inet_aton(p8, &inAddr) )
  5481.             return -1;
  5482.         memcpy(pQos->remote_ip_start, (unsigned char *)&inAddr, 4);
  5483.         if ( !inet_aton(p9, &inAddr) )
  5484.             return -1;
  5485.         memcpy(pQos->remote_ip_end, (unsigned char *)&inAddr, 4);
  5486.  
  5487.         pQos->remote_port_start = (unsigned short)atoi(p10);
  5488.         pQos->remote_port_end = (unsigned short)atoi(p11);
  5489.             strcpy( pQos->entry_name, p12 );
  5490.         qosRuleNum++;        
  5491.         break;
  5492. #endif
  5493.  
  5494. #ifdef QOS_BY_BANDWIDTH
  5495.     case QOS_ARRAY_T:
  5496.         getVal7((char *)value, &p1, &p2, &p3, &p4, &p5, &p6, &p7);
  5497.         if (p1 == NULL || p2 == NULL || p3 == NULL || p4 == NULL || p5 == NULL || p6 == NULL || p7 == NULL) {
  5498.             printf("Invalid QoS arguments!\n");
  5499.             break;
  5500.         }
  5501.         pQos = (IPQOS_Tp)(((long)pMib)+mib_table[i].offset+qosRuleNum*sizeof(IPQOS_T));
  5502.         pQos->enabled = (unsigned char)atoi(p1);
  5503.             //strcpy( pQos->mac, p2 );
  5504.         if (strlen(p2)!=12 || !string_to_hex(p2, pQos->mac, 12))
  5505.             return -1;     
  5506.         pQos->mode = (unsigned char)atoi(p3);
  5507.         if ( !inet_aton(p4, &inAddr) )
  5508.             return -1;
  5509.         memcpy(pQos->local_ip_start, (unsigned char *)&inAddr, 4);
  5510.         if ( !inet_aton(p5, &inAddr) )
  5511.             return -1;
  5512.         memcpy(pQos->local_ip_end, (unsigned char *)&inAddr, 4);
  5513.        
  5514.         pQos->bandwidth = (unsigned long)atoi(p6);
  5515.  
  5516.             strcpy( pQos->entry_name, p7 );
  5517.         qosRuleNum++;        
  5518.         break;
  5519. #endif
  5520.  
  5521. #ifdef ROUTE_SUPPORT
  5522.     case STATICROUTE_ARRAY_T:
  5523.         getVal5((char *)value, &p1, &p2, &p3, &p4, &p5);
  5524.         if (p1 == NULL || p2 == NULL || p3 == NULL) {
  5525.             printf("Invalid PORTFILTER arguments!\n");
  5526.             break;
  5527.         }
  5528.         if ( !inet_aton(p1, &inAddr) )
  5529.             return -1;
  5530.         pStaticRoute = (STATICROUTE_Tp)(((long)pMib)+mib_table[i].offset+staticRouteNum*sizeof(STATICROUTE_T));
  5531.         if( !inet_aton(p1, &pStaticRoute->destAddr))
  5532.             return -1 ;
  5533.         if( !inet_aton(p2, &pStaticRoute->netmask))
  5534.             return -1 ;
  5535.         if( !inet_aton(p3, &pStaticRoute->gateway))
  5536.             return -1 ;
  5537.         pStaticRoute->interface=(unsigned char)atoi(p4);
  5538.         pStaticRoute->metric=(unsigned char)atoi(p5);
  5539.            
  5540.         staticRouteNum++;
  5541.         break;
  5542. #endif // ROUTE_SUPPORT
  5543. #endif
  5544. #ifdef TLS_CLIENT
  5545.     case CERTROOT_ARRAY_T:
  5546.         getVal1((char *)value, &p1);
  5547.         if (p1 == NULL ) {
  5548.             printf("Invalid CERTCA arguments!\n");
  5549.             break;
  5550.         }
  5551.         pCertRoot = (CERTROOT_Tp)(((long)pMib)+mib_table[i].offset+certRootNum*sizeof(CERTROOT_T));
  5552.         strcpy( pCertRoot->comment, p1 );
  5553.         certRootNum++;
  5554.         break;
  5555.     case CERTUSER_ARRAY_T:
  5556.         getVal2((char *)value,&p1, &p2);
  5557.         if (p1 == NULL || p2 = NULL) {
  5558.             printf("Invalid CERTPR arguments!\n");
  5559.             break;
  5560.         }
  5561.         pCertUser = (CERTUSER_Tp)(((long)pMib)+mib_table[i].offset+certUserNum*sizeof(CERTUSER_T));
  5562.         strcpy( pCertUser->pass, p1 );
  5563.         strcpy( pCertUser->comment, p2 );
  5564.         certUserNum++;
  5565.         break;     
  5566. #endif
  5567.  
  5568.     case DHCPRSVDIP_ARRY_T:
  5569.         getVal3((char *)value, &p1, &p2, &p3);
  5570.         if (p1 == NULL || p2 == NULL || p3 == NULL) {
  5571.             printf("Invalid DHCPRSVDIP in argument!\n");
  5572.             break;
  5573.         }  
  5574.         if (strlen(p2)!=12 || !string_to_hex(p2, key, 12))
  5575.             return -1;
  5576.         pDhcpRsvd= (DHCPRSVDIP_Tp)(((long)pMib)+mib_table[i].offset+dhcpRsvdIpNum*sizeof(DHCPRSVDIP_T));
  5577.         strcpy(pDhcpRsvd->hostName, p1);       
  5578.         memcpy(pDhcpRsvd->macAddr, key, 6);
  5579.         if( !inet_aton(p3, &pDhcpRsvd->ipAddr))
  5580.             return -1;
  5581.         dhcpRsvdIpNum++;
  5582.         break;
  5583.     case SCHEDULE_ARRAY_T:
  5584.     //may not used
  5585.         break;
  5586. #if defined(VLAN_CONFIG_SUPPORTED)     
  5587.     case VLANCONFIG_ARRAY_T:
  5588.     getVal6((char *)value, &p1, &p2, &p3, &p4, &p5, &p6);
  5589.         if (p1 == NULL || p2 == NULL || p3 == NULL || p4 == NULL || p5 == NULL || p6 == NULL ) {
  5590.             printf("Invalid VLAN Config arguments!\n");
  5591.             break;
  5592.         }  
  5593.         if (p2){
  5594.             pVlanConfig = (VLAN_CONFIG_Tp)(((long)pMib)+mib_table[i].offset);
  5595.             for(j=0;j<vlanConfigNum;j++){
  5596.             if(!strcmp((pVlanConfig+(j*sizeof(VLAN_CONFIG_T)))->netIface, p2){
  5597.                 pVlanConfig =  (VLAN_CONFIG_Tp)(((long)pMib)+mib_table[i].offset+(j*sizeof(VLAN_CONFIG_T)));
  5598.                 pVlanConfig->enabled = (unsigned char)atoi(p1);
  5599.                 pVlanConfig->tagged = (unsigned char)atoi(p3);
  5600.                 pVlanConfig->priority = (unsigned char)atoi(p4);
  5601.                 pVlanConfig->cfi = (unsigned char)atoi(p5);
  5602.                 pVlanConfig->vlanId = (unsigned short)atoi(p6);
  5603.                 }
  5604.             }
  5605.             }
  5606.     break;
  5607. #endif     
  5608.     default:
  5609.         return -1;
  5610.     }
  5611.     return 0;
  5612. }
  5613.  
  5614. ////////////////////////////////////////////////////////////////////////////////
  5615. static void getVal2(char *value, char **p1, char **p2)
  5616. {
  5617.     value = getVal(value, p1);
  5618.     if ( value )
  5619.         getVal(value, p2);
  5620.     else
  5621.         *p2 = NULL;
  5622. }
  5623.  
  5624. #ifdef HOME_GATEWAY
  5625. ////////////////////////////////////////////////////////////////////////////////
  5626. static void getVal3(char *value, char **p1, char **p2, char **p3)
  5627. {
  5628.     *p1 = *p2 = *p3 = NULL;
  5629.  
  5630.     value = getVal(value, p1);
  5631.     if ( !value )
  5632.         return;
  5633.     value = getVal(value, p2);
  5634.     if ( !value )
  5635.         return;
  5636.     getVal(value, p3);
  5637. }
  5638.  
  5639. ////////////////////////////////////////////////////////////////////////////////
  5640. static void getVal4(char *value, char **p1, char **p2, char **p3, char **p4)
  5641. {
  5642.     *p1 = *p2 = *p3 = *p4 = NULL;
  5643.  
  5644.     value = getVal(value, p1);
  5645.     if ( !value )
  5646.         return;
  5647.     value = getVal(value, p2);
  5648.     if ( !value )
  5649.         return;
  5650.     value = getVal(value, p3);
  5651.     if ( !value )
  5652.         return;
  5653.     getVal(value, p4);
  5654. }
  5655.  
  5656. ////////////////////////////////////////////////////////////////////////////////
  5657. static void getVal5(char *value, char **p1, char **p2, char **p3, char **p4, char **p5)
  5658. {
  5659.     *p1 = *p2 = *p3 = *p4 = *p5 = NULL;
  5660.  
  5661.     value = getVal(value, p1);
  5662.     if ( !value )
  5663.         return;
  5664.     value = getVal(value, p2);
  5665.     if ( !value )
  5666.         return;
  5667.     value = getVal(value, p3);
  5668.     if ( !value )
  5669.         return;
  5670.     value = getVal(value, p4);
  5671.     if ( !value )
  5672.         return;
  5673.     getVal(value, p5);
  5674. }
  5675. #endif // HOME_GATEWAY
  5676.  
  5677. #endif // PARSE_TXT_FILE
  5678.  
  5679. ////////////////////////////////////////////////////////////////////////////////
  5680. static int getdir(char *fullname, char *path, int loop)
  5681. {
  5682.     char tmpBuf[100], *p, *p1;
  5683.  
  5684.     strcpy(tmpBuf, fullname);
  5685.     path[0] = '\0';
  5686.  
  5687.     p1 = tmpBuf;
  5688.     while (1) {
  5689.         if ((p=strchr(p1, '/'))) {
  5690.             if (--loop == 0) {
  5691.                 *p = '\0';
  5692.                 strcpy(path, tmpBuf);
  5693.                 return 0;
  5694.             }
  5695.             p1 = ++p;
  5696.         }
  5697.         else
  5698.             break;
  5699.     }
  5700.     return -1;
  5701. }
  5702.  
  5703. ////////////////////////////////////////////////////////////////////////////////
  5704. static int read_flash_webpage(char *prefix, char *webfile)
  5705. {
  5706.     WEB_HEADER_T header;
  5707.     char *buf, tmpFile[100], tmpFile1[100], tmpBuf[100];
  5708.     int fh=0, i, loop, size;
  5709.     FILE_ENTRY_Tp pEntry;
  5710.     struct stat sbuf;
  5711.     char *file;
  5712.     if (webfile[0])
  5713.         file = webfile;
  5714.     else
  5715.         file = NULL;
  5716.  
  5717.     if (!file) {
  5718.         if ( flash_read((char *)&header, WEB_PAGE_OFFSET, sizeof(header)) == 0) {
  5719.             printf("Read web header failed!\n");
  5720.             return -1;
  5721.         }
  5722.     }
  5723.     else {
  5724.         if ((fh = open(file, O_RDONLY)) < 0) {
  5725.             printf("Can't open file %s\n", file);
  5726.             return -1;
  5727.         }
  5728.         lseek(fh, 0L, SEEK_SET);
  5729.         if (read(fh, &header, sizeof(header)) != sizeof(header)) {
  5730.             printf("Read web header failed %s!\n", file);
  5731.             close(fh);
  5732.             return -1;
  5733.         }
  5734.     }
  5735. #ifndef __mips__
  5736.     header.len = DWORD_SWAP(header.len);
  5737. #endif
  5738.  
  5739.     if (memcmp(header.signature, WEB_HEADER, SIGNATURE_LEN)) {
  5740.         printf("Invalid web image! Expect %s\n",WEB_HEADER);
  5741.         return -1;
  5742.     }
  5743.  
  5744. // for debug
  5745. //printf("web size=%ld\n", header.len);
  5746.     buf = malloc(header.len);
  5747.     if (buf == NULL) {
  5748.         sprintf(tmpBuf, "Allocate buffer failed %d!\n", header.len);
  5749.         printf(tmpBuf);
  5750.         return -1;
  5751.     }
  5752.  
  5753.     if (!file) {
  5754.         if ( flash_read(buf, WEB_PAGE_OFFSET+sizeof(header), header.len) == 0) {
  5755.             printf("Read web image failed!\n");
  5756.             free(buf);
  5757.             return -1;
  5758.         }
  5759.     }
  5760.     else {
  5761.         if (read(fh, buf, header.len) != header.len) {
  5762.             printf("Read web image failed!\n");
  5763.             free(buf);
  5764.             return -1;
  5765.         }
  5766.         close(fh);
  5767.     }
  5768.  
  5769.     if ( !CHECKSUM_OK((unsigned char *)buf, header.len) ) {
  5770.         printf("Web image invalid!\n");
  5771.         free(buf);
  5772.         return -1;
  5773.     }
  5774. // for debug
  5775. //printf("checksum ok!\n");
  5776.  
  5777.     // save to a file
  5778.     strcpy(tmpFile, "flashweb.bz2");
  5779.     fh = open(tmpFile, O_RDWR|O_CREAT|O_TRUNC);
  5780.     if (fh == -1) {
  5781.         printf("Create output file error %s!\n", tmpFile );
  5782.         free(buf);
  5783.         return -1;
  5784.     }
  5785.     if ( write(fh, buf, header.len-1) != header.len -1) {
  5786.         printf("write file error %s!\n", tmpFile);
  5787.         free(buf);
  5788.         close(fh);
  5789.         return -1;
  5790.     }
  5791.     close(fh);
  5792.     free(buf);
  5793.     sync();
  5794.  
  5795.     // decompress file
  5796.     sprintf(tmpFile1, "%sXXXXXX", tmpFile);
  5797.     mkstemp(tmpFile1);
  5798.  
  5799.     sprintf(tmpBuf, "bunzip2 -c %s > %s", tmpFile, tmpFile1);
  5800.     system(tmpBuf);
  5801.  
  5802.     unlink(tmpFile);
  5803.     sync();
  5804.  
  5805.     if (stat(tmpFile1, &sbuf) != 0) {
  5806.         printf("Stat file error %s!\n", tmpFile1);
  5807.         return -1;
  5808.     }
  5809.     if (sbuf.st_size < sizeof(FILE_ENTRY_T) ) {
  5810.         sprintf(tmpBuf, "Invalid decompress file size %ld!\n", sbuf.st_size);
  5811.         printf(tmpBuf);
  5812.         unlink(tmpFile1);
  5813.         return -1;
  5814.     }
  5815. // for debug
  5816. //printf("decompress size=%ld\n", sbuf.st_size);
  5817.  
  5818.     buf = malloc(sbuf.st_size);
  5819.     if (buf == NULL) {
  5820.         sprintf(tmpBuf,"Allocate buffer failed %ld!\n", sbuf.st_size);
  5821.         printf(tmpBuf);
  5822.         return -1;
  5823.     }
  5824.     if ((fh = open(tmpFile1, O_RDONLY)) < 0) {
  5825.         printf("Can't open file %s\n", tmpFile1);
  5826.         free(buf);
  5827.         return -1;
  5828.     }
  5829.     lseek(fh, 0L, SEEK_SET);
  5830.     if ( read(fh, buf, sbuf.st_size) != sbuf.st_size) {
  5831.         printf("Read file error %ld!\n", sbuf.st_size);
  5832.         free(buf);
  5833.         close(fh);
  5834.         return -1;
  5835.     }
  5836.     close(fh);
  5837.     unlink(tmpFile1);
  5838.     sync();
  5839.     size = sbuf.st_size;
  5840.     for (i=0; i<size; ) {
  5841.         pEntry = (FILE_ENTRY_Tp)&buf[i];
  5842.  
  5843. #ifndef __mips__
  5844.         pEntry->size = DWORD_SWAP(pEntry->size);
  5845. #endif
  5846.  
  5847.         strcpy(tmpFile, prefix);
  5848.         strcat(tmpFile, "/");
  5849.         strcat(tmpFile, pEntry->name);
  5850.  
  5851.         loop = 0;
  5852.         while (1) {
  5853.             if (getdir(tmpFile, tmpBuf, ++loop) < 0)
  5854.                 break;
  5855.             if (tmpBuf[0] && stat(tmpBuf, &sbuf) < 0) { // not exist
  5856.                 if ( mkdir(tmpBuf, S_IREAD|S_IWRITE) < 0) {
  5857.                     printf("Create directory %s failed!\n", tmpBuf);
  5858.                     return -1;
  5859.                 }
  5860.             }
  5861.         }
  5862. // for debug
  5863. //printf("write file %s, size=%ld\n", tmpFile, pEntry->size);
  5864.  
  5865.         fh = open(tmpFile, O_RDWR|O_CREAT|O_TRUNC);
  5866.         if (fh == -1) {
  5867.             printf("Create output file error %s!\n", tmpFile );
  5868.             free(buf);
  5869.             return -1;
  5870.         }
  5871. // for debug
  5872. //if ( (i+sizeof(FILE_ENTRY_T)+pEntry->size) > size ) {
  5873. //printf("error in size, %ld !\n", pEntry->size);
  5874. //}
  5875.         if ( write(fh, &buf[i+sizeof(FILE_ENTRY_T)], pEntry->size) != pEntry->size ) {
  5876.             printf("Write file error %s, len=%d!\n", tmpFile, pEntry->size);
  5877.             free(buf);
  5878.             close(fh);
  5879.             return -1;
  5880.         }
  5881.         close(fh);
  5882.         // always set execuatble for script file
  5883. //      chmod(tmpFile,  S_IXUSR);
  5884.  
  5885.         i += (pEntry->size + sizeof(FILE_ENTRY_T));
  5886.     }
  5887.     free(buf);
  5888.     return 0;
  5889. }
  5890.  
  5891. #ifdef VPN_SUPPORT
  5892. static int read_flash_rsa(char *outputFile)
  5893. {
  5894.     int fh;
  5895.     char *rsaBuf;
  5896.  
  5897.     if ( !apmib_init()) {
  5898.         printf("Initialize AP MIB failed!\n");
  5899.         return -1;
  5900.     }
  5901.  
  5902.     fh = open(outputFile,  O_RDWR|O_CREAT);
  5903.     if (fh == -1) {
  5904.         printf("Create WPA config file error!\n");
  5905.         return -1;
  5906.     }
  5907.     rsaBuf = malloc(sizeof(unsigned char) * MAX_RSA_FILE_LEN);
  5908.     apmib_get( MIB_IPSEC_RSA_FILE, (void *)rsaBuf);
  5909.     write(fh, rsaBuf, MAX_RSA_FILE_LEN);
  5910.     close(fh);
  5911.     free(rsaBuf);
  5912.     chmod(outputFile,  DEFFILEMODE);
  5913.     return 0;
  5914. }
  5915. #endif
  5916. #ifdef TLS_CLIENT
  5917. ////////////////////////////////////////////////////////////////////////////////
  5918. static int read_flash_cert(char *prefix, char *certfile)
  5919. {
  5920.     CERT_HEADER_T header;
  5921.     char *buf, tmpFile[100], tmpFile1[100], tmpBuf[100];
  5922.     int fh=0, i, loop, size;
  5923.     FILE_ENTRY_Tp pEntry;
  5924.     struct stat sbuf;
  5925.     char *file;
  5926.  
  5927.     if (certfile[0])
  5928.         file = certfile;
  5929.     else
  5930.         file = NULL;
  5931.  
  5932.     if (!file) {
  5933.         if ( flash_read((char *)&header, CERT_PAGE_OFFSET, sizeof(header)) == 0) {
  5934.             printf("Read web header failed!\n");
  5935.             return -1;
  5936.         }
  5937.     }
  5938.     else {
  5939.         if ((fh = open(file, O_RDONLY)) < 0) {
  5940.             printf("Can't open file %s\n", file);
  5941.             return -1;
  5942.         }
  5943.         lseek(fh, 0L, SEEK_SET);
  5944.         if (read(fh, &header, sizeof(header)) != sizeof(header)) {
  5945.             printf("Read web header failed %s!\n", file);
  5946.             close(fh);
  5947.             return -1;
  5948.         }
  5949.     }
  5950. #ifndef __mips__
  5951.     header.len = DWORD_SWAP(header.len);
  5952. #endif
  5953.    
  5954.     if (memcmp(header.signature, CERT_HEADER, SIGNATURE_LEN)) {
  5955.         printf("Invalid cert image!\n");
  5956.         return -1;
  5957.     }
  5958.  
  5959. // for debug
  5960. //printf("web size=%ld\n", header.len);
  5961.     buf = malloc(header.len);
  5962.     if (buf == NULL) {
  5963.         sprintf(tmpBuf, "Allocate buffer failed %ld!\n", header.len);
  5964.         printf(tmpBuf);
  5965.         return -1;
  5966.     }
  5967.  
  5968.     if (!file) {
  5969.         if ( flash_read(buf, CERT_PAGE_OFFSET+sizeof(header), header.len) == 0) {
  5970.             printf("Read web image failed!\n");
  5971.             return -1;
  5972.         }
  5973.     }
  5974.     else {
  5975.         if (read(fh, buf, header.len) != header.len) {
  5976.             printf("Read web image failed!\n");
  5977.             return -1;
  5978.         }
  5979.         close(fh);
  5980.     }
  5981.  
  5982.     if ( !CHECKSUM_OK(buf, header.len) ) {
  5983.         printf("Web image invalid!\n");
  5984.         free(buf);
  5985.         return -1;
  5986.     }
  5987. // for debug
  5988. //printf("checksum ok!\n");
  5989.  
  5990.     // save to a file
  5991.     strcpy(tmpFile, "/tmp/cert.tmp");
  5992.     fh = open(tmpFile, O_RDWR|O_CREAT|O_TRUNC);
  5993.     if (fh == -1) {
  5994.         printf("Create output file error %s!\n", tmpFile );
  5995.         return -1;
  5996.     }
  5997.     if ( write(fh, buf, header.len-1) != header.len -1) {
  5998.         printf("write file error %s!\n", tmpFile);
  5999.         return -1;
  6000.     }
  6001.     close(fh);
  6002.     free(buf);
  6003.     sync();
  6004.  
  6005.     // decompress file
  6006.     sprintf(tmpFile1, "%sXXXXXX", tmpFile);
  6007.     mkstemp(tmpFile1);
  6008.  
  6009.     //sprintf(tmpBuf, "bunzip2 -c %s > %s", tmpFile, tmpFile1);
  6010.     sprintf(tmpBuf, "cat %s  > %s", tmpFile, tmpFile1);
  6011.     system(tmpBuf);
  6012.  
  6013.     unlink(tmpFile);
  6014.     sync();
  6015.  
  6016.     if (stat(tmpFile1, &sbuf) != 0) {
  6017.         printf("Stat file error %s!\n", tmpFile1);
  6018.         return -1;
  6019.     }
  6020.     if (sbuf.st_size < sizeof(FILE_ENTRY_T) ) {
  6021.         sprintf(tmpBuf, "Invalid decompress file size %ld!\n", sbuf.st_size);
  6022.         printf(tmpBuf);
  6023.         unlink(tmpFile1);
  6024.         return -1;
  6025.     }
  6026. // for debug
  6027. //printf("decompress size=%ld\n", sbuf.st_size);
  6028.  
  6029.     buf = malloc(sbuf.st_size);
  6030.     if (buf == NULL) {
  6031.         sprintf(tmpBuf,"Allocate buffer failed %ld!\n", sbuf.st_size);
  6032.         printf(tmpBuf);
  6033.         return -1;
  6034.     }
  6035.     if ((fh = open(tmpFile1, O_RDONLY)) < 0) {
  6036.         printf("Can't open file %s\n", tmpFile1);
  6037.         return -1;
  6038.     }
  6039.     lseek(fh, 0L, SEEK_SET);
  6040.     if ( read(fh, buf, sbuf.st_size) != sbuf.st_size) {
  6041.         printf("Read file error %ld!\n", sbuf.st_size);
  6042.         return -1;
  6043.     }
  6044.     close(fh);
  6045.     unlink(tmpFile1);
  6046.     sync();
  6047.     size = sbuf.st_size;
  6048.     for (i=0; i<size; ) {
  6049.         pEntry = (FILE_ENTRY_Tp)&buf[i];
  6050.  
  6051. #ifndef __mips__
  6052.         pEntry->size = DWORD_SWAP(pEntry->size);
  6053. #endif
  6054.  
  6055.         strcpy(tmpFile, prefix);
  6056.         strcat(tmpFile, "/");
  6057.         strcat(tmpFile, pEntry->name);
  6058.         if(!strcmp(pEntry->name , ""))
  6059.             break;
  6060.         //printf("name = %s\n", pEntry->name); 
  6061.         loop = 0;
  6062.         while (1) {
  6063.             if (getdir(tmpFile, tmpBuf, ++loop) < 0)
  6064.                 break;
  6065.             if (tmpBuf[0] && stat(tmpBuf, &sbuf) < 0) { // not exist
  6066.                 if ( mkdir(tmpBuf, S_IREAD|S_IWRITE) < 0) {
  6067.                     printf("Create directory %s failed!\n", tmpBuf);
  6068.                     return -1;
  6069.                 }
  6070.             }
  6071.         }
  6072. // for debug
  6073. //printf("write file %s, size=%ld\n", tmpFile, pEntry->size);
  6074.  
  6075.         fh = open(tmpFile, O_RDWR|O_CREAT|O_TRUNC);
  6076.         if (fh == -1) {
  6077.             printf("Create output file error %s!\n", tmpFile );
  6078.             return -1;
  6079.         }
  6080. // for debug
  6081. //if ( (i+sizeof(FILE_ENTRY_T)+pEntry->size) > size ) {
  6082. //printf("error in size, %ld !\n", pEntry->size);
  6083. //}
  6084.  
  6085.         if ( write(fh, &buf[i+sizeof(FILE_ENTRY_T)], pEntry->size) != pEntry->size ) {
  6086.             printf("Write file error %s, len=%ld!\n", tmpFile, pEntry->size);
  6087.             return -1;
  6088.         }
  6089.         close(fh);
  6090.         // always set execuatble for script file
  6091. //      chmod(tmpFile,  S_IXUSR);
  6092.  
  6093.         i += (pEntry->size + sizeof(FILE_ENTRY_T));
  6094.     }
  6095.  
  6096.     return 0;
  6097. }
  6098. #endif
  6099. ////////////////////////////////////////////////////////////////////////////////
  6100. static void __inline__ WRITE_WPA_FILE(int fh, unsigned char *buf)
  6101. {
  6102.     if ( write(fh, buf, strlen((char *)buf)) != strlen((char *)buf) ) {
  6103.         printf("Write WPA config file error!\n");
  6104.         close(fh);
  6105.         exit(1);
  6106.     }
  6107. }
  6108.  
  6109. ////////////////////////////////////////////////////////////////////////////////
  6110. static void generateWpaConf(char *outputFile, int isWds)
  6111. {
  6112.     int fh, intVal, encrypt, enable1x, wep;
  6113.     unsigned char buf1[1024], buf2[1024];
  6114.  
  6115. #ifdef CONFIG_RTL_802_1X_CLIENT_SUPPORT
  6116.     int wlan_mode;
  6117. #endif
  6118.  
  6119. #if 0
  6120. //#ifdef UNIVERSAL_REPEATER
  6121.     int isVxd = 0;
  6122.    
  6123.     if (strstr(outputFile, "-vxd"))
  6124.         isVxd = 1; 
  6125. #endif     
  6126.    
  6127.     if ( !apmib_init()) {
  6128.         printf("Initialize AP MIB failed!\n");
  6129.         return;
  6130.     }
  6131.  
  6132.     fh = open(outputFile, O_RDWR|O_CREAT|O_TRUNC);
  6133.     if (fh == -1) {
  6134.         printf("Create WPA config file error!\n");
  6135.         return;
  6136.     }
  6137.  
  6138. #ifdef CONFIG_RTL_802_1X_CLIENT_SUPPORT
  6139.     apmib_get( MIB_WLAN_MODE, (void *)&wlan_mode);
  6140. #endif
  6141.    
  6142.     if (!isWds) {
  6143.     apmib_get( MIB_WLAN_ENCRYPT, (void *)&encrypt);
  6144.  
  6145. #if 0
  6146. //#ifdef UNIVERSAL_REPEATER
  6147.     if (isVxd && (encrypt == ENCRYPT_WPA2_MIXED)) {
  6148.         apmib_get( MIB_WLAN_MODE, (void *)&intVal);
  6149.         if (intVal == AP_MODE || intVal == AP_WDS_MODE)
  6150.             encrypt = ENCRYPT_WPA;     
  6151.     }
  6152. #endif         
  6153.    
  6154.     sprintf((char *)buf2, "encryption = %d\n", encrypt);
  6155.     WRITE_WPA_FILE(fh, buf2);
  6156.  
  6157. #if 0
  6158. //#ifdef UNIVERSAL_REPEATER
  6159.     if (isVxd) {
  6160.         if (strstr(outputFile, "wlan0-vxd"))
  6161.             apmib_get( MIB_REPEATER_SSID1, (void *)buf1);      
  6162.         else           
  6163.             apmib_get( MIB_REPEATER_SSID2, (void *)buf1);  
  6164.     }
  6165.     else
  6166. #endif
  6167.     apmib_get( MIB_WLAN_SSID,  (void *)buf1);
  6168.     sprintf((char *)buf2, "ssid = \"%s\"\n", buf1);
  6169.     WRITE_WPA_FILE(fh, buf2);
  6170.  
  6171.     apmib_get( MIB_WLAN_ENABLE_1X, (void *)&enable1x);
  6172.     sprintf((char *)buf2, "enable1x = %d\n", enable1x);
  6173.     WRITE_WPA_FILE(fh, buf2);
  6174.  
  6175.     apmib_get( MIB_WLAN_MAC_AUTH_ENABLED, (void *)&intVal);
  6176.     sprintf((char *)buf2, "enableMacAuth = %d\n", intVal);
  6177.     WRITE_WPA_FILE(fh, buf2);
  6178.  
  6179.     apmib_get( MIB_WLAN_ENABLE_SUPP_NONWPA, (void *)&intVal);
  6180.     if (intVal)
  6181.         apmib_get( MIB_WLAN_SUPP_NONWPA, (void *)&intVal);
  6182.  
  6183.     sprintf((char *)buf2, "supportNonWpaClient = %d\n", intVal);
  6184.     WRITE_WPA_FILE(fh, buf2);
  6185.  
  6186.     apmib_get( MIB_WLAN_WEP, (void *)&wep);
  6187.     sprintf((char *)buf2, "wepKey = %d\n", wep);
  6188.     WRITE_WPA_FILE(fh, buf2);
  6189.  
  6190.     if ( encrypt==1 && enable1x ) {
  6191.         if (wep == 1) {
  6192.             apmib_get( MIB_WLAN_WEP64_KEY1, (void *)buf1);
  6193.             sprintf((char *)buf2, "wepGroupKey = \"%02x%02x%02x%02x%02x\"\n", buf1[0],buf1[1],buf1[2],buf1[3],buf1[4]);
  6194.         }
  6195.         else {
  6196.             apmib_get( MIB_WLAN_WEP128_KEY1, (void *)buf1);
  6197.             sprintf((char *)buf2, "wepGroupKey = \"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\"\n",
  6198.                 buf1[0],buf1[1],buf1[2],buf1[3],buf1[4],
  6199.                 buf1[5],buf1[6],buf1[7],buf1[8],buf1[9],
  6200.                 buf1[10],buf1[11],buf1[12]);
  6201.         }
  6202.     }
  6203.     else
  6204.         strcpy((char *)buf2, "wepGroupKey = \"\"\n");
  6205.     WRITE_WPA_FILE(fh, buf2);
  6206.  
  6207.     apmib_get( MIB_WLAN_WPA_AUTH, (void *)&intVal);
  6208.     sprintf((char *)buf2, "authentication = %d\n", intVal);
  6209.     WRITE_WPA_FILE(fh, buf2);
  6210.  
  6211.     apmib_get( MIB_WLAN_WPA_CIPHER_SUITE, (void *)&intVal);
  6212.     sprintf((char *)buf2, "unicastCipher = %d\n", intVal);
  6213.     WRITE_WPA_FILE(fh, buf2);
  6214.  
  6215.     apmib_get( MIB_WLAN_WPA2_CIPHER_SUITE, (void *)&intVal);
  6216.     sprintf((char *)buf2, "wpa2UnicastCipher = %d\n", intVal);
  6217.     WRITE_WPA_FILE(fh, buf2);
  6218.  
  6219.     apmib_get( MIB_WLAN_WPA2_PRE_AUTH, (void *)&intVal);
  6220.     sprintf((char *)buf2, "enablePreAuth = %d\n", intVal);
  6221.     WRITE_WPA_FILE(fh, buf2);
  6222.  
  6223.     apmib_get( MIB_WLAN_PSK_FORMAT, (void *)&intVal);
  6224.     if (intVal==0)
  6225.         sprintf((char *)buf2, "usePassphrase = 1\n");
  6226.     else
  6227.         sprintf((char *)buf2, "usePassphrase = 0\n");
  6228.     WRITE_WPA_FILE(fh, buf2);
  6229.  
  6230.     apmib_get( MIB_WLAN_WPA_PSK, (void *)buf1);
  6231.     sprintf((char *)buf2, "psk = \"%s\"\n", buf1);
  6232.     WRITE_WPA_FILE(fh, buf2);
  6233.  
  6234.     apmib_get( MIB_WLAN_WPA_GROUP_REKEY_TIME, (void *)&intVal);
  6235.     sprintf((char *)buf2, "groupRekeyTime = %d\n", intVal);
  6236.     WRITE_WPA_FILE(fh, buf2);
  6237.  
  6238. #ifdef CONFIG_RTL_802_1X_CLIENT_SUPPORT
  6239.     if (wlan_mode == CLIENT_MODE) { // wlan client mode    
  6240.         apmib_get( MIB_WLAN_EAP_TYPE, (void *)&intVal);
  6241.         sprintf(buf2, "eapType = %d\n", intVal);
  6242.         WRITE_WPA_FILE(fh, buf2);
  6243.  
  6244.         apmib_get( MIB_WLAN_EAP_INSIDE_TYPE, (void *)&intVal);
  6245.         sprintf(buf2, "eapInsideType = %d\n", intVal);
  6246.         WRITE_WPA_FILE(fh, buf2);
  6247.  
  6248.         apmib_get( MIB_WLAN_EAP_USER_ID, (void *)buf1);
  6249.         sprintf(buf2, "eapUserId = \"%s\"\n", buf1);
  6250.         WRITE_WPA_FILE(fh, buf2);
  6251.  
  6252.         apmib_get( MIB_WLAN_RS_USER_NAME, (void *)buf1);
  6253.         sprintf(buf2, "rsUserName = \"%s\"\n", buf1);
  6254.         WRITE_WPA_FILE(fh, buf2);
  6255.  
  6256.         apmib_get( MIB_WLAN_RS_USER_PASSWD, (void *)buf1);
  6257.         sprintf(buf2, "rsUserPasswd = \"%s\"\n", buf1);
  6258.         WRITE_WPA_FILE(fh, buf2);
  6259.  
  6260.         apmib_get( MIB_WLAN_RS_USER_CERT_PASSWD, (void *)buf1);
  6261.         sprintf(buf2, "rsUserCertPasswd = \"%s\"\n", buf1);
  6262.         WRITE_WPA_FILE(fh, buf2);
  6263.  
  6264.         apmib_get( MIB_WLAN_RS_BAND_SEL, (void *)&intVal);
  6265.         sprintf(buf2, "rsBandSel = %d\n", intVal);
  6266.         WRITE_WPA_FILE(fh, buf2);
  6267.  
  6268.         //Patch for auth daemon at wlan client mode
  6269.         // 127.0.0.1 : 12345
  6270.         if(intVal == PHYBAND_5G)
  6271.             sprintf(buf2, "rsPort = %d\n", 12344);
  6272.         else
  6273.             sprintf(buf2, "rsPort = %d\n", 12345);
  6274.         WRITE_WPA_FILE(fh, buf2);
  6275.  
  6276.         sprintf(buf2, "rsIP = %s\n", "127.0.0.1");
  6277.         WRITE_WPA_FILE(fh, buf2);
  6278.         //End patch.
  6279.     }
  6280.     else
  6281. #endif
  6282.     {
  6283.     apmib_get( MIB_WLAN_RS_PORT, (void *)&intVal);
  6284.     sprintf((char *)buf2, "rsPort = %d\n", intVal);
  6285.     WRITE_WPA_FILE(fh, buf2);
  6286.  
  6287.     apmib_get( MIB_WLAN_RS_IP, (void *)buf1);
  6288.     sprintf((char *)buf2, "rsIP = %s\n", inet_ntoa(*((struct in_addr *)buf1)));
  6289.     WRITE_WPA_FILE(fh, buf2);
  6290.  
  6291.     apmib_get( MIB_WLAN_RS_PASSWORD, (void *)buf1);
  6292.     sprintf((char *)buf2, "rsPassword = \"%s\"\n", buf1);
  6293.     WRITE_WPA_FILE(fh, buf2);
  6294.  
  6295.     apmib_get( MIB_WLAN_RS_MAXRETRY, (void *)&intVal);
  6296.     sprintf((char *)buf2, "rsMaxReq = %d\n", intVal);
  6297.     WRITE_WPA_FILE(fh, buf2);
  6298.  
  6299.     apmib_get( MIB_WLAN_RS_INTERVAL_TIME, (void *)&intVal);
  6300.     sprintf((char *)buf2, "rsAWhile = %d\n", intVal);
  6301.     WRITE_WPA_FILE(fh, buf2);
  6302.  
  6303.     apmib_get( MIB_WLAN_ACCOUNT_RS_ENABLED, (void *)&intVal);
  6304.     sprintf((char *)buf2, "accountRsEnabled = %d\n", intVal);
  6305.     WRITE_WPA_FILE(fh, buf2);
  6306.  
  6307.     apmib_get( MIB_WLAN_ACCOUNT_RS_PORT, (void *)&intVal);
  6308.     sprintf((char *)buf2, "accountRsPort = %d\n", intVal);
  6309.     WRITE_WPA_FILE(fh, buf2);
  6310.  
  6311.     apmib_get( MIB_WLAN_ACCOUNT_RS_IP, (void *)buf1);
  6312.     sprintf((char *)buf2, "accountRsIP = %s\n", inet_ntoa(*((struct in_addr *)buf1)));
  6313.     WRITE_WPA_FILE(fh, buf2);
  6314.  
  6315.     apmib_get( MIB_WLAN_ACCOUNT_RS_PASSWORD, (void *)buf1);
  6316.     sprintf((char *)buf2, "accountRsPassword = \"%s\"\n", buf1);
  6317.     WRITE_WPA_FILE(fh, buf2);
  6318.  
  6319.     apmib_get( MIB_WLAN_ACCOUNT_RS_UPDATE_ENABLED, (void *)&intVal);
  6320.     sprintf((char *)buf2, "accountRsUpdateEnabled = %d\n", intVal);
  6321.     WRITE_WPA_FILE(fh, buf2);
  6322.  
  6323.     apmib_get( MIB_WLAN_ACCOUNT_RS_UPDATE_DELAY, (void *)&intVal);
  6324.     sprintf((char *)buf2, "accountRsUpdateTime = %d\n", intVal);
  6325.     WRITE_WPA_FILE(fh, buf2);
  6326.  
  6327.     apmib_get( MIB_WLAN_ACCOUNT_RS_MAXRETRY, (void *)&intVal);
  6328.     sprintf((char *)buf2, "accountRsMaxReq = %d\n", intVal);
  6329.     WRITE_WPA_FILE(fh, buf2);
  6330.  
  6331.     apmib_get( MIB_WLAN_ACCOUNT_RS_INTERVAL_TIME, (void *)&intVal);
  6332.     sprintf((char *)buf2, "accountRsAWhile = %d\n", intVal);
  6333.     WRITE_WPA_FILE(fh, buf2);
  6334.     }
  6335.     }
  6336.  
  6337. #ifdef CONFIG_RTK_MESH
  6338.     else if (isWds==7) {
  6339.        
  6340.         apmib_get( MIB_MESH_ENCRYPT, (void *)&encrypt);
  6341.         sprintf((char *)buf2, "encryption = %d\n", encrypt);
  6342.         WRITE_WPA_FILE(fh, buf2);
  6343.  
  6344.         apmib_get( MIB_MESH_ID,  (void *)buf1);
  6345.         sprintf((char *)buf2, "ssid = \"%s\"\n", buf1);
  6346.         WRITE_WPA_FILE(fh, buf2);
  6347.        
  6348.         WRITE_WPA_FILE(fh, "enable1x = 0\n");
  6349.         WRITE_WPA_FILE(fh, "enableMacAuth = 0\n");
  6350.         WRITE_WPA_FILE(fh, "supportNonWpaClient = 0\n");
  6351.         WRITE_WPA_FILE(fh, "wepKey = 0\n");
  6352.         WRITE_WPA_FILE(fh,  "wepGroupKey = \"\"\n");
  6353.  
  6354.         apmib_get( MIB_MESH_WPA_AUTH, (void *)&intVal);
  6355.         sprintf((char *)buf2, "authentication = %d\n", intVal);
  6356.         WRITE_WPA_FILE(fh, buf2);
  6357.  
  6358.         //sprintf(buf2, "unicastCipher = %d\n", intVal);
  6359.         sprintf((char *)buf2, "unicastCipher = 1\n");
  6360.         WRITE_WPA_FILE(fh, buf2);
  6361.        
  6362.         apmib_get( MIB_MESH_WPA2_CIPHER_SUITE, (void *)&intVal);   
  6363.  
  6364.         sprintf((char *)buf2, "wpa2UnicastCipher = %d\n", intVal);
  6365.         WRITE_WPA_FILE(fh, buf2);
  6366.  
  6367.         WRITE_WPA_FILE(fh, "enablePreAuth = 0\n");
  6368.  
  6369.         apmib_get( MIB_MESH_PSK_FORMAT, (void *)&intVal);
  6370.         if (intVal==0)
  6371.             sprintf((char *)buf2, "usePassphrase = 1\n");
  6372.         else
  6373.             sprintf((char *)buf2, "usePassphrase = 0\n");
  6374.         WRITE_WPA_FILE(fh, buf2);
  6375.  
  6376.         apmib_get( MIB_MESH_WPA_PSK, (void *)buf1);
  6377.         sprintf((char *)buf2, "psk = \"%s\"\n", buf1);
  6378.         WRITE_WPA_FILE(fh, buf2);
  6379.  
  6380.         WRITE_WPA_FILE(fh, "groupRekeyTime = 86400\n");
  6381.         WRITE_WPA_FILE(fh, "rsPort = 1812\n");
  6382.         WRITE_WPA_FILE(fh, "rsIP = 0.0.0.0\n");
  6383.         WRITE_WPA_FILE(fh, "rsPassword = \"\"\n");
  6384.         WRITE_WPA_FILE(fh, "rsMaxReq = 3\n");
  6385.         WRITE_WPA_FILE(fh, "rsAWhile = 5\n");
  6386.         WRITE_WPA_FILE(fh, "accountRsEnabled = 0\n");
  6387.         WRITE_WPA_FILE(fh, "accountRsPort = 1813\n");
  6388.         WRITE_WPA_FILE(fh, "accountRsIP = 0.0.0.0\n");
  6389.         WRITE_WPA_FILE(fh, "accountRsPassword = \"\"\n");
  6390.         WRITE_WPA_FILE(fh, "accountRsUpdateEnabled = 0\n");
  6391.         WRITE_WPA_FILE(fh, "accountRsUpdateTime = 60\n");
  6392.         WRITE_WPA_FILE(fh, "accountRsMaxReq = 3\n");
  6393.         WRITE_WPA_FILE(fh, "accountRsAWhile = 5\n");
  6394.     }
  6395. #endif // CONFIG_RTK_MESH
  6396.  
  6397.     else {
  6398.         apmib_get( MIB_WLAN_WDS_ENCRYPT, (void *)&encrypt);
  6399.         if (encrypt == WDS_ENCRYPT_TKIP)       
  6400.             encrypt = ENCRYPT_WPA;
  6401.         else if (encrypt == WDS_ENCRYPT_AES)       
  6402.             encrypt = ENCRYPT_WPA2;    
  6403.         else
  6404.             encrypt = 0;
  6405.    
  6406.         sprintf((char *)buf2, "encryption = %d\n", encrypt);
  6407.         WRITE_WPA_FILE(fh, buf2);
  6408.         WRITE_WPA_FILE(fh, (unsigned char *)"ssid = \"REALTEK\"\n");
  6409.         WRITE_WPA_FILE(fh, (unsigned char *)"enable1x = 1\n");
  6410.         WRITE_WPA_FILE(fh, (unsigned char *)"enableMacAuth = 0\n");
  6411.         WRITE_WPA_FILE(fh, (unsigned char *)"supportNonWpaClient = 0\n");
  6412.         WRITE_WPA_FILE(fh, (unsigned char *)"wepKey = 0\n");
  6413.         WRITE_WPA_FILE(fh, (unsigned char *)"wepGroupKey = \"\"\n");
  6414.         WRITE_WPA_FILE(fh, (unsigned char *)"authentication = 2\n");
  6415.  
  6416.         if (encrypt == ENCRYPT_WPA)
  6417.             intVal = WPA_CIPHER_TKIP;
  6418.         else
  6419.             intVal = WPA_CIPHER_AES;
  6420.            
  6421.         sprintf((char *)buf2, "unicastCipher = %d\n", intVal);
  6422.         WRITE_WPA_FILE(fh, buf2);
  6423.  
  6424.         sprintf((char *)buf2, "wpa2UnicastCipher = %d\n", intVal);
  6425.         WRITE_WPA_FILE(fh, buf2);
  6426.  
  6427.         WRITE_WPA_FILE(fh, (unsigned char *)"enablePreAuth = 0\n");
  6428.  
  6429.         apmib_get( MIB_WLAN_WDS_PSK_FORMAT, (void *)&intVal);
  6430.         if (intVal==0)
  6431.             sprintf((char *)buf2, "usePassphrase = 1\n");
  6432.         else
  6433.             sprintf((char *)buf2, "usePassphrase = 0\n");
  6434.         WRITE_WPA_FILE(fh, buf2);
  6435.  
  6436.         apmib_get( MIB_WLAN_WDS_PSK, (void *)buf1);
  6437.         sprintf((char *)buf2, "psk = \"%s\"\n", buf1);
  6438.         WRITE_WPA_FILE(fh, buf2);
  6439.  
  6440.         WRITE_WPA_FILE(fh, (unsigned char *)"groupRekeyTime = 0\n");
  6441.         WRITE_WPA_FILE(fh, (unsigned char *)"rsPort = 1812\n");
  6442.         WRITE_WPA_FILE(fh, (unsigned char *)"rsIP = 192.168.1.1\n");
  6443.         WRITE_WPA_FILE(fh, (unsigned char *)"rsPassword = \"\"\n");
  6444.         WRITE_WPA_FILE(fh, (unsigned char *)"rsMaxReq = 3\n");
  6445.         WRITE_WPA_FILE(fh, (unsigned char *)"rsAWhile = 10\n");
  6446.         WRITE_WPA_FILE(fh, (unsigned char *)"accountRsEnabled = 0\n");
  6447.         WRITE_WPA_FILE(fh, (unsigned char *)"accountRsPort = 1813\n");
  6448.         WRITE_WPA_FILE(fh, (unsigned char *)"accountRsIP = 192.168.1.1\n");
  6449.         WRITE_WPA_FILE(fh, (unsigned char *)"accountRsPassword = \"\"\n");
  6450.         WRITE_WPA_FILE(fh, (unsigned char *)"accountRsUpdateEnabled = 0\n");
  6451.         WRITE_WPA_FILE(fh, (unsigned char *)"accountRsUpdateTime = 1000\n");
  6452.         WRITE_WPA_FILE(fh, (unsigned char *)"accountRsMaxReq = 3\n");
  6453.         WRITE_WPA_FILE(fh, (unsigned char *)"accountRsAWhile = 1\n");
  6454.     }
  6455.  
  6456.     close(fh);
  6457. }
  6458.  
  6459. static int generateHostapdConf(void)
  6460. {
  6461.  
  6462.     FILE *fh = NULL;
  6463.     int intValue;
  6464.  
  6465.     char line[1024], strValue[1024];
  6466.  
  6467.  
  6468.     if ( !apmib_init()) {
  6469.         printf("Initialize AP MIB failed!\n");
  6470.         return -1;
  6471.     }
  6472.  
  6473.     fh = fopen("/etc/hostapd.conf", "w");
  6474.     if (!fh) {
  6475.         printf("Create hostapd config file error!\n");
  6476.         return -1;
  6477.     }
  6478.     else {
  6479.         /* mib independent fields */
  6480.         sprintf(line,"interface=wlan0\n");
  6481.         fputs(line,fh);
  6482.         /*
  6483.         sprintf(line,"bridge=br0\n");
  6484.         fputs(line,fh);
  6485.         sprintf(line,"driver=nl80211\n");
  6486.         fputs(line,fh);*/
  6487.         /*
  6488.         bit 0 (1) = IEEE 802.11
  6489.         bit 1 (2) = IEEE 802.1X
  6490.         bit 2 (4) = RADIUS
  6491.         bit 3 (8) = WPA
  6492.         bit 4 (16) = driver interface
  6493.         bit 5 (32) = IAPP
  6494.         bit 6 (64) = MLME
  6495.         */
  6496.         sprintf(line,"logger_syslog=-1\n");
  6497.         fputs(line,fh);
  6498.         sprintf(line,"logger_stdout=-1\n");
  6499.         fputs(line,fh);
  6500.         /*
  6501.         Levels (minimum value for logged events):
  6502.          0 = verbose debugging
  6503.          1 = debugging
  6504.          2 = informational messages
  6505.          3 = notification
  6506.          4 = warning
  6507.         */
  6508.         sprintf(line,"logger_syslog_level=2\n");
  6509.         fputs(line,fh);
  6510.         sprintf(line,"logger_stdout_level=2\n");
  6511.         fputs(line,fh);
  6512.         sprintf(line,"dump_file=/tmp/hostapd.dump\n");
  6513.         fputs(line,fh);
  6514.         sprintf(line,"ctrl_interface=/var/run/hostapd\n");
  6515.         fputs(line,fh);
  6516.         sprintf(line,"ctrl_interface_group=0\n");
  6517.         fputs(line,fh);
  6518.        
  6519.    
  6520.         /* mib dependent fields */
  6521.         apmib_get(MIB_WLAN_SSID,strValue);
  6522.         sprintf(line,"ssid=%s\n",strValue);
  6523.         fputs(line,fh);
  6524.         /* country_code
  6525.         apmib_get(MIB_WLAN_SSID,value);
  6526.         sprintf(line,"ssid=%s\n",value);
  6527.         fputs(line,fh);*/
  6528.         /* ieee80211d
  6529.         apmib_get(MIB_WLAN_SSID,value);
  6530.         intValue=atoi(value);
  6531.         sprintf(line,"ssid=%d\n",intValue);
  6532.         fputs(line,fh); */
  6533.         /* hw_mode */
  6534.         apmib_get(MIB_WLAN_BAND,&intValue);
  6535.         if(intValue == 1)
  6536.             sprintf(line,"hw_mode=b\n");
  6537.         else
  6538.             sprintf(line,"hw_mode=g\n");
  6539.         fputs(line,fh);
  6540.  
  6541.         if( intValue > 6 ){
  6542.             sprintf(line,"ieee80211n=1\n");
  6543.             sprintf(line,"%sht_capab=",line);
  6544.             /*ht_capab [40+][40-] [LDPC] [SMPS-STATIC][SMPS-DYNAMIC]  [SHORT-GI-20][SHORT-GI-40]
  6545.                [TX-STBC][RX-STBC] [DELAYED-BA] [MAX-AMSDU-7935] [DSSS_CCK-40] [PSMP] [LSIG-TXOP-PROT] */
  6546.             /* channel */
  6547.             apmib_get(MIB_WLAN_CHANNEL_BONDING, &intValue);
  6548.             if( intValue ){
  6549.                 apmib_get(MIB_WLAN_CONTROL_SIDEBAND, &intValue);
  6550.                 if( intValue )
  6551.                     sprintf(line,"%s%s",line,"[40+]");
  6552.                 else
  6553.                     sprintf(line,"%s%s",line,"[40-]");
  6554.                 apmib_get(MIB_WLAN_SHORT_GI,&intValue);
  6555.                 if( intValue )
  6556.                     sprintf(line,"%s %s",line,"[SHORT-GI-40]");
  6557.             }
  6558.             else{
  6559.                 apmib_get(MIB_WLAN_SHORT_GI,&intValue);
  6560.                 if( intValue )
  6561.                     sprintf(line,"%s%s",line,"[SHORT-GI-20]");
  6562.             }
  6563.             /*
  6564.             apmib_get(MIB_WLAN_11N_STBC,&intValue);
  6565.             if( intValue )
  6566.                 sprintf(line,"%s %s",line,"[TX-STBC]");
  6567.             */
  6568.             apmib_get(MIB_WLAN_AGGREGATION,&intValue);
  6569.             if( intValue > 0 )
  6570.                 sprintf(line,"%s %s",line,"[MAX-AMSDU-7935]");
  6571.         }
  6572.         else{
  6573.             sprintf(line,"ieee80211n=0");
  6574.         }
  6575.         sprintf(line,"%s\n",line);
  6576.         fputs(line,fh);
  6577.        
  6578.         apmib_get(MIB_WLAN_CHANNEL,&intValue);
  6579.         // channel=0 means auto then bypass
  6580.         if(intValue){
  6581.             sprintf(line,"channel=%d\n",intValue);
  6582.             fputs(line,fh);
  6583.         }
  6584.         /* beacon_int */
  6585.         apmib_get(MIB_WLAN_BEACON_INTERVAL,&intValue);
  6586.         sprintf(line,"beacon_int=%d\n",intValue);
  6587.         fputs(line,fh);
  6588.         /* dtim_period */
  6589.         apmib_get(MIB_WLAN_DTIM_PERIOD, &intValue);
  6590.         if( atoi(strValue) )
  6591.             sprintf(line,"dtim_period=%d\n",intValue);
  6592.         else
  6593.             sprintf(line,"dtim_period=2\n");
  6594.         fputs(line,fh);
  6595.         /* max_num_sta */
  6596.         sprintf(line,"max_num_sta=255\n");
  6597.         fputs(line,fh);
  6598.         /* rts_threshold */
  6599.         apmib_get(MIB_WLAN_RTS_THRESHOLD, &intValue);
  6600.         if( intValue )
  6601.             sprintf(line,"rts_threshold=%d\n",intValue);
  6602.         else
  6603.             sprintf(line,"rts_threshold=2347\n");
  6604.         fputs(line,fh);
  6605.         /* fragm_threshold */
  6606.         apmib_get(MIB_WLAN_FRAG_THRESHOLD, &intValue);
  6607.         if( intValue )
  6608.             sprintf(line,"fragm_threshold=%d\n",intValue);
  6609.         else
  6610.             sprintf(line,"fragm_threshold=2346\n");
  6611.         fputs(line,fh);
  6612.         /* supported_rates : in most case, setting for the selected hw */
  6613.         /* basic_rates */
  6614.         /* preamble 0:not use short;1:use */
  6615.         apmib_get(MIB_WLAN_PREAMBLE_TYPE, &intValue);
  6616.         sprintf(line,"preamble=%d\n",intValue);
  6617.         fputs(line,fh);
  6618.         /* macaddr_acl:0:accept unless in deny list ; 1:deny unless in accept list ; external RADIUS server */
  6619.         {
  6620.             int count;
  6621.             FILE *fp = NULL;
  6622.             apmib_get(MIB_WLAN_MACAC_ENABLED, &intValue);
  6623.             apmib_get(MIB_WLAN_MACAC_NUM, &count);
  6624.             if( intValue == 1 ){        /* deny unless in (accept) list */
  6625.                 sprintf(line,"macaddr_acl=1\n");
  6626.                 fputs(line,fh);
  6627.                 if( (fp = fopen("/etc/hostapd.accept","w")) ){
  6628.                     while( count ){
  6629.                         apmib_get(MIB_WLAN_MACAC_ADDR, strValue);
  6630.                         sprintf(line,"%c%c:%c%c:%c%c:%c%c:%c%c:%c%c\n",strValue[0],strValue[1],strValue[2],strValue[3],strValue[4],strValue[5],\
  6631.                                                                 strValue[6],strValue[7],strValue[8],strValue[9],strValue[10],strValue[11]);
  6632.                         fputs(line,fp);
  6633.                         count--;
  6634.                     }
  6635.                     fclose(fp);
  6636.                 }
  6637.             }
  6638.             else if( intValue == 2 ){   /* accept unless in (deny) list */
  6639.                 sprintf(line,"macaddr_acl=0\n");
  6640.                 fputs(line,fh);
  6641.                 if( (fp = fopen("/etc/hostapd.deny","w") ) ){
  6642.                     while( count ){
  6643.                         apmib_get(MIB_WLAN_MACAC_ADDR, strValue);
  6644.                         sprintf(line,"%c%c:%c%c:%c%c:%c%c:%c%c:%c%c\n",strValue[0],strValue[1],strValue[2],strValue[3],strValue[4],strValue[5],\
  6645.                                                                 strValue[6],strValue[7],strValue[8],strValue[9],strValue[10],strValue[11]);
  6646.                         fputs(line,fp);
  6647.                         count--;
  6648.                     }
  6649.                     fclose(fp);
  6650.                 }
  6651.             }
  6652.             else{
  6653.                 sprintf(line,"#macaddr_acl=0\n");
  6654.                 fputs(line,fh);
  6655.             }
  6656.         }
  6657.         /* 802.11i */
  6658.         /* WEP */
  6659.         apmib_get(MIB_WLAN_ENCRYPT,&intValue);
  6660.         if( intValue == 1 ){
  6661.             /* auth_algs 1:open ; 2:WEP ; 3: open & WEP */
  6662.             /*
  6663.             apmib_get(MIB_WLAN_AUTH_TYPE,&intValue);
  6664.             if( !intValue )
  6665.                 sprintf(line,"auth_algs=1\n");
  6666.             else
  6667.                 sprintf(line,"auth_algs=2\n");
  6668.             */
  6669.             sprintf(line,"auth_algs=2\n");
  6670.             apmib_get(MIB_WLAN_WEP,&intValue);
  6671.             if( intValue == 1 ){
  6672.                 apmib_get(MIB_WLAN_WEP64_KEY1,strValue);
  6673.                 if( strlen(strValue) > 0 )
  6674.                     sprintf(line,"%swep_key0=%s\n",line,strValue);
  6675.                 apmib_get(MIB_WLAN_WEP64_KEY2,strValue);
  6676.                 if( strlen(strValue) > 0 )
  6677.                     sprintf(line,"%swep_key1=%s\n",line,strValue);
  6678.                 apmib_get(MIB_WLAN_WEP64_KEY3,strValue);
  6679.                 if( strlen(strValue) > 0 )
  6680.                     sprintf(line,"%swep_key2=%s\n",line,strValue);
  6681.                 apmib_get(MIB_WLAN_WEP64_KEY4,strValue);
  6682.                 if( strlen(strValue) > 0 )
  6683.                     sprintf(line,"%swep_key3=%s\n",line,strValue);
  6684.             }
  6685.             else{
  6686.                 apmib_get(MIB_WLAN_WEP128_KEY1,strValue);
  6687.                 if( strlen(strValue) > 0 )
  6688.                     sprintf(line,"%swep_key0=%s\n",line,strValue);
  6689.                 apmib_get(MIB_WLAN_WEP128_KEY2,strValue);
  6690.                 if( strlen(strValue) > 0 )
  6691.                     sprintf(line,"%swep_key1=%s\n",line,strValue);
  6692.                 apmib_get(MIB_WLAN_WEP128_KEY3,strValue);
  6693.                 if( strlen(strValue) > 0 )
  6694.                     sprintf(line,"%swep_key2=%s\n",line,strValue);
  6695.                 apmib_get(MIB_WLAN_WEP128_KEY4,strValue);
  6696.                 if( strlen(strValue) > 0 )
  6697.                     sprintf(line,"%swep_key3=%s\n",line,strValue);
  6698.             }
  6699.             fputs(line,fh);
  6700.         }
  6701.         else if( intValue > 1 ){
  6702.             int security=0;
  6703.             /* WPA */
  6704.             sprintf(line,"auth_algs=1\n");
  6705.             sprintf(line,"%swpa=%d\n",line,intValue>>1);
  6706.             security = intValue;
  6707.             apmib_get(MIB_WLAN_WPA_PSK,strValue);
  6708.             if( strlen(strValue) > 0 )
  6709.                 sprintf(line,"%swpa_passphrase=%s\n",line,strValue);
  6710.             //wpa_key_mgmt WPA-PSK;WPA-EAP
  6711.             apmib_get(MIB_WLAN_WPA_AUTH,&intValue);
  6712.             if( intValue == 1 ){
  6713.                 sprintf(line,"%swpa_key_mgmt=WPA-EAP\n",line);
  6714.                 apmib_get(MIB_WLAN_RS_IP,&intValue);
  6715.                 sprintf(line,"%sauth_server_addr=%u.%u.%u.%u\n",line,*((char *)&intValue)&0xff,*(((char *)&intValue)+1)&0xff,*(((char *)&intValue)+2)&0xff,*(((char *)&intValue)+3)&0xff);
  6716.                 apmib_get(MIB_WLAN_RS_PORT,&intValue);
  6717.                 sprintf(line,"%sauth_server_port=%d\n",line,intValue);
  6718.                 apmib_get(MIB_WLAN_RS_PASSWORD,strValue);
  6719.                 if(strlen(strValue))
  6720.                     sprintf(line,"%sauth_server_shared_secret=%s\n",line,strValue);
  6721.                 else
  6722.                     sprintf(line,"%sauth_server_shared_secret=\n",line);
  6723.             }
  6724.             else{
  6725.                 sprintf(line,"%swpa_key_mgmt=WPA-PSK\n",line);
  6726.             }
  6727.             //wpa_passphrase
  6728.             //wpa_psk_file         
  6729.             //wpa_pairwise TKIP;CCMP
  6730.             apmib_get(MIB_WLAN_WPA_CIPHER_SUITE,&intValue);
  6731.             if( (intValue > 0) && (security&0x02) ){
  6732.                 sprintf(line,"%swpa_pairwise=",line);
  6733.                 if( intValue == 1 )
  6734.                     sprintf(line,"%sTKIP\n",line);
  6735.                 else if( intValue == 3 )
  6736.                     sprintf(line,"%sTKIP CCMP\n",line);
  6737.                 else
  6738.                     sprintf(line,"%sCCMP\n",line);
  6739.             }
  6740.             //rsn_pairwise CCMP (WP2)
  6741.             apmib_get(MIB_WLAN_WPA2_CIPHER_SUITE,&intValue);
  6742.             if( (intValue > 0) && (security&0x04) ){
  6743.                 sprintf(line,"%srsn_pairwise=",line);
  6744.                 if( intValue == 1 )
  6745.                     sprintf(line,"%sTKIP\n",line);
  6746.                 else if( intValue == 3 )
  6747.                     sprintf(line,"%sTKIP CCMP\n",line);
  6748.                 else
  6749.                     sprintf(line,"%sCCMP\n",line);
  6750.             }
  6751.             apmib_get(MIB_WLAN_WPA_GROUP_REKEY_TIME,&intValue);
  6752.             sprintf(line,"%swpa_group_rekey=%d\n",line,intValue);
  6753.             //sprintf(line,"%swpa_strict_rekey=%d\n",line,intValue);
  6754.             //sprintf(line,"%swpa_gmk_rekey=%d\n",line,intValue);
  6755.             sprintf(line,"%swpa_ptk_rekey=%d\n",line,intValue);
  6756.             apmib_get(MIB_WLAN_WPA2_PRE_AUTH,&intValue);
  6757.             sprintf(line,"%srsn_preauth=%d\n",line,intValue);
  6758.             //rsn_preauth_interfaces
  6759.             //peerkey
  6760.             fputs(line,fh);
  6761.         }
  6762.         else{
  6763.             sprintf(line,"wpa=0\n");
  6764.             fputs(line,fh);
  6765.         }
  6766.        
  6767.         /* ignore_broadcast_ssid 0:disable ; 1:empty bssid in beacon ; 2:set SSID in beacon as 0 1,2 all ignore probe with broadcast SSID */
  6768.         apmib_get(MIB_WLAN_HIDDEN_SSID,&intValue);
  6769.         sprintf(line,"ignore_broadcast_ssid=%d\n",intValue);
  6770.         fputs(line,fh);
  6771.         /*TX queue parameters*/
  6772.         //tx_queue_data3_aifs
  6773.         //tx_queue_data3_cwmin
  6774.         //tx_queue_data3_cwmax
  6775.         //tx_queue_data3_burst
  6776.         //tx_queue_data2_aifs
  6777.         //tx_queue_data2_cwmin
  6778.         //tx_queue_data2_cwmax
  6779.         //tx_queue_data2_burst
  6780.         //tx_queue_data1_aifs
  6781.         //tx_queue_data1_cwmin
  6782.         //tx_queue_data1_cwmax
  6783.         //tx_queue_data0_aifs
  6784.         //tx_queue_data0_cwmin
  6785.         //tx_queue_data0_cwmax
  6786.         //tx_queue_data0_burst
  6787.         //tx_queue_after_beacon_aifs
  6788.         //tx_queue_after_beacon_cwmin
  6789.         //tx_queue_after_beacon_cwmax
  6790.         //tx_queue_after_beacon_burst
  6791.         //tx_queue_beacon_aifs
  6792.         //tx_queue_beacon_cwmin
  6793.         //tx_queue_beacon_cwmax
  6794.         //tx_queue_beacon_burst
  6795.  
  6796.         /* wmm configuration */
  6797.         apmib_get(MIB_WLAN_WMM_ENABLED,&intValue);
  6798.         sprintf(line,"wme_enabled=%d\n",intValue);
  6799.         fputs(line, fh);
  6800.         //wme_ac_bk_cwmin
  6801.         //wme_ac_bk_cwmax
  6802.         //wme_ac_bk_aifs
  6803.         //wme_ac_bk_txop_limit
  6804.         //wme_ac_bk_acm
  6805.         //wme_ac_be_cwmin
  6806.         //wme_ac_be_cwmax
  6807.         //wme_ac_be_aifs
  6808.         //wme_ac_be_txop_limit
  6809.         //wme_ac_be_acm
  6810.         //wme_ac_vi_cwmin
  6811.         //wme_ac_vi_cwmax
  6812.         //wme_ac_vi_aifs
  6813.         //wme_ac_vi_txop_limit
  6814.         //wme_ac_vi_acm
  6815.         //wme_ac_vo_cwmin
  6816.         //wme_ac_vo_cwmax
  6817.         //wme_ac_vo_aifs
  6818.         //wme_ac_vo_txop_limit
  6819.         //wme_ac_vo_acm
  6820.  
  6821.         /* ap_max_inactivity */
  6822.         apmib_get(MIB_WLAN_INACTIVITY_TIME,&intValue);
  6823.         sprintf(line,"ap_max_inactivity=%d\n",intValue);
  6824.         fputs(line,fh);
  6825.         //bridge_packets : internal bridge
  6826.         //max_listen_interval
  6827.  
  6828.         /* Authentication */
  6829.         /* ieee8021x */
  6830.         apmib_get(MIB_WLAN_ENABLE_1X,&intValue);
  6831.         if( intValue ){
  6832.             /* RADIUS client configuration */
  6833.             sprintf(line,"ieee8021x=%d\n",intValue);
  6834.             //own_ip_addr
  6835.             //nas_identifier
  6836.             apmib_get(MIB_WLAN_RS_IP,&intValue);
  6837.             sprintf(line,"%sauth_server_addr=%u.%u.%u.%u\n",line,*((char *)&intValue)&0xff,*(((char *)&intValue)+1)&0xff,*(((char *)&intValue)+2)&0xff,*(((char *)&intValue)+3)&0xff);
  6838.             apmib_get(MIB_WLAN_RS_PORT,&intValue);
  6839.             sprintf(line,"%sauth_server_port=%d\n",line,intValue);
  6840.             apmib_get(MIB_WLAN_RS_PASSWORD,strValue);
  6841.             if(strlen(strValue))
  6842.                 sprintf(line,"%sauth_server_shared_secret=%s\n",line,strValue);
  6843.             else
  6844.                 sprintf(line,"%sauth_server_shared_secret=\n",line);
  6845.             apmib_get(MIB_WLAN_ACCOUNT_RS_ENABLED,&intValue);
  6846.             if( intValue ){
  6847.                 apmib_get(MIB_WLAN_ACCOUNT_RS_IP,&intValue);
  6848.                 sprintf(line,"%sacct_server_addr=%u.%u.%u.%u\n",line,*((char *)&intValue)&0xff,*(((char *)&intValue)+1)&0xff,*(((char *)&intValue)+2)&0xff,*(((char *)&intValue)+3)&0xff);
  6849.                 apmib_get(MIB_WLAN_ACCOUNT_RS_PORT,&intValue);
  6850.                 sprintf(line,"%sacct_server_port=%d\n",line,intValue);
  6851.             }
  6852.             //acct_server_shared_secret
  6853.             apmib_get(MIB_WLAN_RS_MAXRETRY,&intValue);
  6854.             sprintf(line,"%sradius_retry_primary_interval=%d\n",line,intValue);
  6855.             apmib_get(MIB_WLAN_RS_INTERVAL_TIME,&intValue);
  6856.             sprintf(line,"%sradius_acct_interim_interval=%d\n",line,intValue);
  6857.             fputs(line,fh);
  6858.             //dynamic_vlan
  6859.             //vlan_file
  6860.             //vlan_tagged_interface
  6861.         }
  6862.         //eapol_version
  6863.         //eap_message
  6864.         //wep_key_len_broadcast
  6865.         //wep_key_len_unicast
  6866.         //wep_rekey_period
  6867.         //eapol_key_index_workaround
  6868.         //eap_reauth_period
  6869.         //use_pae_group_addr
  6870.  
  6871.         /* eap server */
  6872.         //eap_server = 0
  6873.  
  6874.         /* 802.11f */
  6875.         apmib_get(MIB_WLAN_IAPP_DISABLED,&intValue);
  6876.         if( !intValue ){
  6877.             //sprintf(line,"iapp_interface=eth0\n");
  6878.             sprintf(line,"iapp_interface=br0\n");
  6879.             fputs(line,fh);
  6880.         }
  6881.  
  6882.         /* RADIUS server configuration */
  6883.         //radius_server_clients
  6884.         //radius_server_auth_port
  6885.         //radius_server_ipv6
  6886.  
  6887.         //ieee80211w 0:disabled ; 1:optional ; 2:required
  6888.         //assoc_sa_query_max_timeout
  6889.         //assoc_sa_query_retry_timeout
  6890.         //okc
  6891.  
  6892.         /* 8021.11r */
  6893.         //mobility_domain (2-octet identifier as a hex string)
  6894.         //r0_key_lifetime
  6895.         //r1_key_holder
  6896.         //reassociation_deadline
  6897.         //r0kh format: <MAC address> <R0KH-ID> <128-bit key as hex string>
  6898.         //r1kh format: <MAC address> <R0KH-ID> <128-bit key as hex string>
  6899.         //pmk_r1_push
  6900.         //passive_scan_interval
  6901.         //passive_scan_listen
  6902.         //passive_scan_mode
  6903.         //ap_table_max_size
  6904.         //ap_table_expiration_time
  6905.  
  6906.         /* WPS */
  6907. #if 0
  6908.         //wps_state 0:disabled;1:enabled, not configured;2:enabled, configured
  6909.         apmib_get(MIB_WSC_DISABLE,&intValue);
  6910.         if( intValue ){
  6911.             sprintf(line,"wps_state=0\n");
  6912.         }
  6913.         else{
  6914.             apmib_get(MIB_WLAN_WSC_CONFIGURED,&intValue);
  6915.             if( intValue )
  6916.                 sprintf(line,"wps_state=2\n");
  6917.             else
  6918.                 sprintf(line,"wps_state=1\n");
  6919.         }
  6920.         sprintf(line,"%sdevice_name=Realtek Wireless AP\n",line);
  6921.         sprintf(line,"%smanufacturer=Realtek Semiconductor Corp.\n",line);
  6922.         sprintf(line,"%smodel_name=RTL8xxx\n",line);
  6923.         sprintf(line,"%smodel_number=EV-2009-02-06\n",line);
  6924.         sprintf(line,"%sserial_number=123456789012347\n",line);
  6925.         /*
  6926.            device_attrib_id=1
  6927.            device_oui=0050f204
  6928.            device_category_id=6
  6929.            device_sub_category_id=1
  6930.         */
  6931.         sprintf(line,"%sdevice_type=6-0050f204-1\n",line);
  6932.         //ap_setup_locked
  6933.         sprintf(line,"%suuid=63041253101920061228aabbccddeeff\n",line);
  6934.         sprintf(line,"%smanufacturer_url=http://www.realtek.com/\n",line);
  6935.         sprintf(line,"%smodel_description=WLAN Access Point\n",line);
  6936.         sprintf(line,"%smodel_url=http://www.realtek.com\n",line);
  6937.         apmib_get(MIB_WSC_METHOD,&intValue);
  6938.         sprintf(line,"%sconfig_methods=%d\n",line,intValue);
  6939.         fputs(line,fh);
  6940.         //wps_pin_requests
  6941.         //os_version
  6942.         //ap_pin
  6943.         //skip_cred_build
  6944.         //extra_cred
  6945.         //wps_cred_processing
  6946.         //ap_settings
  6947.         //upnp_iface
  6948.         //friendly_name
  6949.         //upc
  6950. #endif
  6951.         /* Multiple BSSID */
  6952.         //bss
  6953.         //ssid
  6954.         //bssid
  6955.         fclose(fh);
  6956.     }
  6957.     return 0;
  6958. }
  6959.  
  6960. ////////////////////////////////////////////////////////////////////////////////
  6961. #ifdef WLAN_FAST_INIT
  6962.  
  6963. #include <sys/socket.h>
  6964. #include <sys/ioctl.h>
  6965. #include <linux/wireless.h>
  6966. #if defined(CONFIG_RTL_8196B)
  6967. #include "../../../linux-2.6.30/drivers/net/wireless/rtl8190/ieee802_mib.h"
  6968. #else /*rtl8196C*/
  6969. #include "../../../linux-2.6.30/drivers/net/wireless/rtl8192cd/ieee802_mib.h"
  6970. #endif
  6971.  
  6972. void calc_incr(unsigned char *mac, int idx)
  6973. {
  6974.     if( (*mac+idx) == 0x0 )
  6975.         calc_incr(mac-1,1);
  6976.     else
  6977.         *mac += idx;
  6978. }
  6979.  
  6980. int get_root_mac(unsigned char *ifname,unsigned char *mac)
  6981. {
  6982.     int fd;
  6983.     struct ifreq ifr;
  6984.     unsigned char zero_mac[6]={0}, broadcat_mac[6]={0xff};
  6985.     unsigned char if_root[6]={'\0'};
  6986. #if defined(CONFIG_RTL_92D_SUPPORT)
  6987.     unsigned char buf2[1024] = {'\0'};
  6988.     int intVal;
  6989. #endif
  6990.  
  6991.     fd = socket(AF_INET, SOCK_DGRAM, 0);
  6992.  
  6993. #if defined(CONFIG_RTL_92D_SUPPORT)
  6994.     apmib_get(MIB_WLAN_PHY_BAND_SELECT,&intVal);
  6995.     if(!strcmp(ifname,"wlan0") && intVal == 1) {
  6996.         memcpy(if_root,"wlan1",5);
  6997.     }
  6998.     else
  6999. #endif
  7000.     {
  7001.         memcpy(if_root,ifname,5);
  7002.     }
  7003.  
  7004. #if defined(CONFIG_RTL_92D_SUPPORT)
  7005.     if( strlen(ifname) < 6 ) {
  7006.         sprintf(buf2,"iwpriv %s bandadd %d",ifname,intVal);
  7007.         system(buf2);  
  7008.     }
  7009. #endif
  7010.     ifr.ifr_addr.sa_family = AF_INET;
  7011.     strcpy(ifr.ifr_name, (char *)if_root);
  7012.     if( ioctl(fd, SIOCGIFHWADDR, &ifr) < 0 ){
  7013.         close(fd);
  7014.         return -1;
  7015.     }
  7016.     close(fd);
  7017.  
  7018.     if( !memcmp(ifr.ifr_hwaddr.sa_data,zero_mac,6) || !memcmp(ifr.ifr_hwaddr.sa_data,broadcat_mac,6) ){
  7019.         return -1;
  7020.     }
  7021.  
  7022.     memcpy(mac,ifr.ifr_hwaddr.sa_data,6);
  7023.     return 0;
  7024. }
  7025.  
  7026. static int initWlan(char *ifname)
  7027. {
  7028.     struct wifi_mib *pmib;
  7029.     int i, intVal, intVal2, encrypt, enable1x, wep, mode/*, enable1xVxd*/;
  7030.     unsigned char buf1[1024], buf2[1024], mac[6];
  7031.     int skfd;
  7032.     struct iwreq wrq, wrq_root;
  7033.     int wlan_band=0, channel_bound=0, aggregation=0;
  7034.     MACFILTER_T *pAcl=NULL;
  7035.     struct wdsEntry *wds_Entry=NULL;
  7036.     WDS_Tp pwds_EntryUI;
  7037. #ifdef MBSSID
  7038.     int v_previous=0;
  7039. #ifdef CONFIG_RTL_819X
  7040.     int vap_enable=0, intVal4=0;
  7041. #endif
  7042. #endif
  7043.    
  7044.     skfd = socket(AF_INET, SOCK_DGRAM, 0);
  7045.     if (skfd < 0) {
  7046.         printf("socket() fail\n");
  7047.         return -1;
  7048.     }
  7049.     strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
  7050.     if (ioctl(skfd, SIOCGIWNAME, &wrq) < 0) {
  7051.         printf("Interface %s open failed!\n", ifname);
  7052.         close(skfd);
  7053.         return -1;
  7054.     }
  7055.  
  7056.     if ((pmib = (struct wifi_mib *)malloc(sizeof(struct wifi_mib))) == NULL) {
  7057.         printf("MIB buffer allocation failed!\n");
  7058.         close(skfd);
  7059.         return -1;
  7060.     }
  7061.  
  7062.     if (!apmib_init()) {
  7063.         printf("Initialize AP MIB failed!\n");
  7064.         free(pmib);
  7065.         close(skfd);
  7066.         return -1;
  7067.     }
  7068.  
  7069.     // Disable WLAN MAC driver and shutdown interface first
  7070.     sprintf((char *)buf1, "ifconfig %s down", ifname);
  7071.     system((char *)buf1);
  7072.  
  7073.     if (vwlan_idx == 0) {
  7074.         // shutdown all WDS interface
  7075.         for (i=0; i<8; i++) {
  7076.             sprintf((char *)buf1, "ifconfig %s-wds%d down", ifname, i);
  7077.             system((char *)buf1);
  7078.         }
  7079.    
  7080.         // kill wlan application daemon
  7081.         sprintf((char *)buf1, "wlanapp.sh kill %s", ifname);
  7082.         system((char *)buf1);
  7083.     }
  7084.     else { // virtual interface
  7085.         sprintf((char *)buf1, "wlan%d", wlan_idx);
  7086.         strncpy(wrq_root.ifr_name, (char *)buf1, IFNAMSIZ);
  7087.         if (ioctl(skfd, SIOCGIWNAME, &wrq_root) < 0) {
  7088.             printf("Root Interface %s open failed!\n", buf1);
  7089.             free(pmib);
  7090.             close(skfd);
  7091.             return -1;
  7092.         }
  7093.     }
  7094.  
  7095.     if (vwlan_idx == 0) {
  7096.         apmib_get(MIB_HW_RF_TYPE, (void *)&intVal);
  7097.         if (intVal == 0) {
  7098.             printf("RF type is NULL!\n");
  7099.             free(pmib);
  7100.             close(skfd);
  7101.             return 0;
  7102.         }
  7103.     }
  7104.     apmib_get(MIB_WLAN_WLAN_DISABLED, (void *)&intVal);
  7105.  
  7106.     if (intVal == 1) {
  7107.         free(pmib);
  7108.         close(skfd);
  7109.         return 0;
  7110.     }
  7111.  
  7112.     // get mib from driver
  7113.     wrq.u.data.pointer = (caddr_t)pmib;
  7114.     wrq.u.data.length = sizeof(struct wifi_mib);
  7115.  
  7116.     if (vwlan_idx == 0) {
  7117.         if (ioctl(skfd, 0x8B42, &wrq) < 0) {
  7118.             printf("Get WLAN MIB failed!\n");
  7119.             free(pmib);
  7120.             close(skfd);
  7121.             return -1;
  7122.         }
  7123.     }
  7124.     else {
  7125.         wrq_root.u.data.pointer = (caddr_t)pmib;
  7126.         wrq_root.u.data.length = sizeof(struct wifi_mib);              
  7127.         if (ioctl(skfd, 0x8B42, &wrq_root) < 0) {
  7128.             printf("Get WLAN MIB failed!\n");
  7129.             free(pmib);
  7130.             close(skfd);
  7131.             return -1;
  7132.         }      
  7133.     }
  7134.  
  7135.     // check mib version
  7136.     if (pmib->mib_version != MIB_VERSION) {
  7137.         printf("WLAN MIB version mismatch!\n");
  7138.         free(pmib);
  7139.         close(skfd);
  7140.         return -1;
  7141.     }
  7142.  
  7143.     if (vwlan_idx > 0) {    //if not root interface, clone root mib to virtual interface
  7144.         wrq.u.data.pointer = (caddr_t)pmib;
  7145.         wrq.u.data.length = sizeof(struct wifi_mib);
  7146.         if (ioctl(skfd, 0x8B43, &wrq) < 0) {
  7147.             printf("Set WLAN MIB failed!\n");
  7148.             free(pmib);
  7149.             close(skfd);
  7150.             return -1;
  7151.         }
  7152.         pmib->miscEntry.func_off = 0;  
  7153.     }
  7154.  
  7155.     // Set parameters to driver
  7156.     if (vwlan_idx == 0) {  
  7157.         apmib_get(MIB_HW_REG_DOMAIN, (void *)&intVal);
  7158.         pmib->dot11StationConfigEntry.dot11RegDomain = intVal;
  7159.     }
  7160.  
  7161.     apmib_get(MIB_WLAN_WLAN_MAC_ADDR, (void *)mac);
  7162.     if (!memcmp(mac, "\x00\x00\x00\x00\x00\x00", 6)) {
  7163. #ifdef WLAN_MAC_FROM_EFUSE
  7164.         if( !get_root_mac(ifname,mac) ){
  7165. #ifdef MBSSID
  7166.         if( vwlan_idx > 0 ) {
  7167.                         if (*(char *)(ifname+4) == '0' ) //wlan0
  7168.                           *(char *)mac |= LOCAL_ADMIN_BIT;
  7169.                         else {
  7170.                             (*(char *)mac) += 4;
  7171.                             (*(char *)mac) |= LOCAL_ADMIN_BIT;
  7172.                         }
  7173.         }
  7174.                 calc_incr((char *)mac+MACADDRLEN-1,vwlan_idx);
  7175. #endif
  7176.         } else
  7177. #endif
  7178.     {
  7179. #ifdef MBSSID
  7180.         if (vwlan_idx > 0 && vwlan_idx != NUM_VWLAN_INTERFACE) {
  7181.             switch (vwlan_idx)
  7182.             {
  7183.                 case 1:
  7184.                     apmib_get(MIB_HW_WLAN_ADDR1, (void *)mac);
  7185.                     break;
  7186.                 case 2:
  7187.                     apmib_get(MIB_HW_WLAN_ADDR2, (void *)mac);
  7188.                     break;
  7189.                 case 3:
  7190.                     apmib_get(MIB_HW_WLAN_ADDR3, (void *)mac);
  7191.                     break;
  7192.                 case 4:
  7193.                     apmib_get(MIB_HW_WLAN_ADDR4, (void *)mac);
  7194.                     break;
  7195.                 default:
  7196.                     printf("Fail to get MAC address of VAP%d!\n", vwlan_idx-1);
  7197.                     free(pmib);
  7198.                     close(skfd);
  7199.                     return 0;
  7200.             }
  7201.         }
  7202.         else
  7203. #endif
  7204.         apmib_get(MIB_HW_WLAN_ADDR, (void *)mac);
  7205.     }
  7206.     }
  7207.     // ifconfig all wlan interface when not in WISP
  7208.     // ifconfig wlan1 later interface when in WISP mode, the wlan0  will be setup in WAN interface
  7209.     apmib_get(MIB_OP_MODE, (void *)&intVal);
  7210.     apmib_get(MIB_WISP_WAN_ID, (void *)&intVal2);
  7211.     sprintf((char *)buf1, "wlan%d", intVal2);
  7212.     /*notice: config repeater interface's mac address will overwrite root interface's mac*/
  7213.     if (   
  7214. #ifdef MBSSID      
  7215.         (vwlan_idx != NUM_VWLAN_INTERFACE) &&
  7216. #endif     
  7217.         ((intVal != 2) || strcmp((char *)buf1,ifname) ||
  7218. #ifdef MBSSID
  7219.         vwlan_idx > 0
  7220. #endif
  7221.         )) {
  7222.         sprintf((char *)buf2, "ifconfig %s hw ether %02x%02x%02x%02x%02x%02x", ifname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  7223.         system((char *)buf2);
  7224.         memcpy(&(pmib->dot11OperationEntry.hwaddr[0]), mac, 6);
  7225.     }
  7226.  
  7227. #ifdef BR_SHORTCUT 
  7228.     if (intVal == 2
  7229. #ifdef MBSSID
  7230.         && vwlan_idx == 0
  7231. #endif
  7232.     )
  7233.         pmib->dot11OperationEntry.disable_brsc = 1;
  7234. #endif
  7235.    
  7236.     apmib_get(MIB_HW_LED_TYPE, (void *)&intVal);
  7237.     pmib->dot11OperationEntry.ledtype = intVal;
  7238.  
  7239.     // set AP/client/WDS mode
  7240.     apmib_get(MIB_WLAN_SSID, (void *)buf1);
  7241.     intVal2 = strlen((char *)buf1);
  7242.     pmib->dot11StationConfigEntry.dot11DesiredSSIDLen = intVal2;
  7243.     memset(pmib->dot11StationConfigEntry.dot11DesiredSSID, 0, 32);
  7244.     memcpy(pmib->dot11StationConfigEntry.dot11DesiredSSID, buf1, intVal2);
  7245.     if ((pmib->dot11StationConfigEntry.dot11DesiredSSIDLen == 3) &&
  7246.         ((pmib->dot11StationConfigEntry.dot11DesiredSSID[0] == 'A') || (pmib->dot11StationConfigEntry.dot11DesiredSSID[0] == 'a')) &&
  7247.         ((pmib->dot11StationConfigEntry.dot11DesiredSSID[1] == 'N') || (pmib->dot11StationConfigEntry.dot11DesiredSSID[1] == 'n')) &&
  7248.         ((pmib->dot11StationConfigEntry.dot11DesiredSSID[2] == 'Y') || (pmib->dot11StationConfigEntry.dot11DesiredSSID[2] == 'y'))) {
  7249.         pmib->dot11StationConfigEntry.dot11SSIDtoScanLen = 0;
  7250.         memset(pmib->dot11StationConfigEntry.dot11SSIDtoScan, 0, 32);
  7251.     }
  7252.     else {
  7253.         pmib->dot11StationConfigEntry.dot11SSIDtoScanLen = intVal2;
  7254.         memset(pmib->dot11StationConfigEntry.dot11SSIDtoScan, 0, 32);
  7255.         memcpy(pmib->dot11StationConfigEntry.dot11SSIDtoScan, buf1, intVal2);
  7256.     }
  7257.  
  7258.     apmib_get(MIB_WLAN_MODE, (void *)&mode);
  7259.     if (mode == 1) {
  7260.         // client mode
  7261.         apmib_get(MIB_WLAN_NETWORK_TYPE, (void *)&intVal2);
  7262.         if (intVal2 == 0)
  7263.             pmib->dot11OperationEntry.opmode = 8;
  7264.         else {
  7265.             pmib->dot11OperationEntry.opmode = 32;
  7266.             apmib_get(MIB_WLAN_DEFAULT_SSID, (void *)buf1);
  7267.             intVal2 = strlen((char *)buf1);
  7268.             pmib->dot11StationConfigEntry.dot11DefaultSSIDLen = intVal2;
  7269.             memset(pmib->dot11StationConfigEntry.dot11DefaultSSID, 0, 32);
  7270.             memcpy(pmib->dot11StationConfigEntry.dot11DefaultSSID, buf1, intVal2);
  7271.         }
  7272.     }
  7273.     else
  7274.         pmib->dot11OperationEntry.opmode = 16;
  7275.  
  7276.     if (mode == 2)  // WDS only
  7277.         pmib->dot11WdsInfo.wdsPure = 1;
  7278.     else
  7279.         pmib->dot11WdsInfo.wdsPure = 0;
  7280.  
  7281.     pmib->dot11StationConfigEntry.dot11AclNum = 0;
  7282.     apmib_get(MIB_WLAN_MACAC_ENABLED, (void *)&intVal);
  7283.     pmib->dot11StationConfigEntry.dot11AclMode = intVal;
  7284.     if (intVal != 0) {
  7285.         apmib_get(MIB_WLAN_MACAC_NUM, (void *)&intVal);
  7286.         if (intVal != 0) {
  7287.             for (i=0; i<intVal; i++) {
  7288.                 buf1[0] = i+1;
  7289.                 apmib_get(MIB_WLAN_MACAC_ADDR, (void *)buf1);
  7290.                 pAcl = (MACFILTER_T *)buf1;
  7291.                 memcpy(&(pmib->dot11StationConfigEntry.dot11AclAddr[i][0]), &(pAcl->macAddr[0]), 6);
  7292.                 pmib->dot11StationConfigEntry.dot11AclNum++;
  7293.             }
  7294.         }
  7295.     }
  7296.  
  7297.     if (vwlan_idx == 0) { // root interface
  7298.         // set RF parameters
  7299.         apmib_get(MIB_HW_RF_TYPE, (void *)&intVal);
  7300.         pmib->dot11RFEntry.dot11RFType = intVal;
  7301.        
  7302. #if defined(CONFIG_RTL_8196B)
  7303.  
  7304.         apmib_get(MIB_HW_BOARD_VER, (void *)&intVal);
  7305.         if (intVal == 1)
  7306.             pmib->dot11RFEntry.MIMO_TR_mode = 3;    // 2T2R
  7307.         else if(intVal == 2)
  7308.             pmib->dot11RFEntry.MIMO_TR_mode = 4; // 1T1R
  7309.         else
  7310.             pmib->dot11RFEntry.MIMO_TR_mode = 1;    // 1T2R
  7311.  
  7312.         apmib_get(MIB_HW_TX_POWER_CCK, (void *)buf1);
  7313.         memcpy(pmib->dot11RFEntry.pwrlevelCCK, buf1, 14);
  7314.        
  7315.         apmib_get(MIB_HW_TX_POWER_OFDM_1S, (void *)buf1);
  7316.         memcpy(pmib->dot11RFEntry.pwrlevelOFDM_1SS, buf1, 162);
  7317.        
  7318.         apmib_get(MIB_HW_TX_POWER_OFDM_2S, (void *)buf1);
  7319.         memcpy(pmib->dot11RFEntry.pwrlevelOFDM_2SS, buf1, 162);
  7320.  
  7321.         // not used for RTL8192SE
  7322.         //apmib_get(MIB_HW_11N_XCAP, (void *)&intVal);
  7323.         //pmib->dot11RFEntry.crystalCap = intVal;
  7324.        
  7325.         apmib_get(MIB_HW_11N_LOFDMPWDA, (void *)&intVal);
  7326.         pmib->dot11RFEntry.LOFDM_pwd_A = intVal;
  7327.  
  7328.         apmib_get(MIB_HW_11N_LOFDMPWDB, (void *)&intVal);
  7329.         pmib->dot11RFEntry.LOFDM_pwd_B = intVal;
  7330.  
  7331.         apmib_get(MIB_HW_11N_TSSI1, (void *)&intVal);
  7332.         pmib->dot11RFEntry.tssi1 = intVal;
  7333.  
  7334.         apmib_get(MIB_HW_11N_TSSI2, (void *)&intVal);
  7335.         pmib->dot11RFEntry.tssi2 = intVal;
  7336.  
  7337.         apmib_get(MIB_HW_11N_THER, (void *)&intVal);
  7338.         pmib->dot11RFEntry.ther = intVal;
  7339.  
  7340.         if (pmib->dot11RFEntry.dot11RFType == 10) { // Zebra
  7341.             apmib_get(MIB_WLAN_RFPOWER_SCALE, (void *)&intVal);
  7342.             if(intVal == 1)
  7343.                 intVal = 3;
  7344.             else if(intVal == 2)
  7345.                     intVal = 6;
  7346.                 else if(intVal == 3)
  7347.                         intVal = 9;
  7348.                     else if(intVal == 4)
  7349.                             intVal = 17;
  7350.             if (intVal) {
  7351.                 for (i=0; i<14; i++) {
  7352.                     if(pmib->dot11RFEntry.pwrlevelCCK[i] != 0){
  7353.                         if ((pmib->dot11RFEntry.pwrlevelCCK[i] - intVal) >= 1)
  7354.                             pmib->dot11RFEntry.pwrlevelCCK[i] -= intVal;
  7355.                         else
  7356.                             pmib->dot11RFEntry.pwrlevelCCK[i] = 1;
  7357.                     }
  7358.                 }
  7359.                 for (i=0; i<162; i++) {
  7360.                     if (pmib->dot11RFEntry.pwrlevelOFDM_1SS[i] != 0){
  7361.                         if((pmib->dot11RFEntry.pwrlevelOFDM_1SS[i] - intVal) >= 1)
  7362.                             pmib->dot11RFEntry.pwrlevelOFDM_1SS[i] -= intVal;
  7363.                         else
  7364.                             pmib->dot11RFEntry.pwrlevelOFDM_1SS[i] = 1;
  7365.                     }
  7366.                     if (pmib->dot11RFEntry.pwrlevelOFDM_2SS[i] != 0){
  7367.                         if((pmib->dot11RFEntry.pwrlevelOFDM_2SS[i] - intVal) >= 1)
  7368.                             pmib->dot11RFEntry.pwrlevelOFDM_2SS[i] -= intVal;
  7369.                         else
  7370.                             pmib->dot11RFEntry.pwrlevelOFDM_2SS[i] = 1;
  7371.                     }
  7372.                 }      
  7373.             }
  7374.         }
  7375. #elif defined(CONFIG_RTL_8196C) || defined(CONFIG_RTL_8198)
  7376.     apmib_get(MIB_HW_BOARD_VER, (void *)&intVal);
  7377.     if (intVal == 1)
  7378.         pmib->dot11RFEntry.MIMO_TR_mode = 3;    // 2T2R
  7379.     else if(intVal == 2)
  7380.         pmib->dot11RFEntry.MIMO_TR_mode = 4; // 1T1R
  7381.     else
  7382.         pmib->dot11RFEntry.MIMO_TR_mode = 1;    // 1T2R
  7383.     apmib_get(MIB_HW_TX_POWER_CCK_A, (void *)buf1);
  7384.     memcpy(pmib->dot11RFEntry.pwrlevelCCK_A, buf1, MAX_2G_CHANNEL_NUM_MIB);
  7385.    
  7386.     apmib_get(MIB_HW_TX_POWER_CCK_B, (void *)buf1);
  7387.     memcpy(pmib->dot11RFEntry.pwrlevelCCK_B, buf1, MAX_2G_CHANNEL_NUM_MIB);
  7388.    
  7389.     apmib_get(MIB_HW_TX_POWER_HT40_1S_A, (void *)buf1);
  7390.     memcpy(pmib->dot11RFEntry.pwrlevelHT40_1S_A, buf1, MAX_2G_CHANNEL_NUM_MIB);
  7391.    
  7392.     apmib_get(MIB_HW_TX_POWER_HT40_1S_B, (void *)buf1);
  7393.     memcpy(pmib->dot11RFEntry.pwrlevelHT40_1S_B, buf1, MAX_2G_CHANNEL_NUM_MIB);
  7394.    
  7395.     apmib_get(MIB_HW_TX_POWER_DIFF_HT40_2S, (void *)buf1);
  7396.     memcpy(pmib->dot11RFEntry.pwrdiffHT40_2S, buf1, MAX_2G_CHANNEL_NUM_MIB);
  7397.    
  7398.     apmib_get(MIB_HW_TX_POWER_DIFF_HT20, (void *)buf1);
  7399.     memcpy(pmib->dot11RFEntry.pwrdiffHT20, buf1, MAX_2G_CHANNEL_NUM_MIB);
  7400.    
  7401.     apmib_get(MIB_HW_TX_POWER_DIFF_OFDM, (void *)buf1);
  7402.     memcpy(pmib->dot11RFEntry.pwrdiffOFDM, buf1, MAX_2G_CHANNEL_NUM_MIB);
  7403.    
  7404. #if defined(CONFIG_RTL_92D_SUPPORT)
  7405.     apmib_get(MIB_HW_TX_POWER_5G_HT40_1S_A, (void *)buf1);
  7406.     memcpy(pmib->dot11RFEntry.pwrlevel5GHT40_1S_A, buf1, MAX_5G_CHANNEL_NUM_MIB);
  7407.    
  7408.     apmib_get(MIB_HW_TX_POWER_5G_HT40_1S_B, (void *)buf1);
  7409.     memcpy(pmib->dot11RFEntry.pwrlevel5GHT40_1S_B, buf1, MAX_5G_CHANNEL_NUM_MIB);
  7410.    
  7411.     apmib_get(MIB_HW_TX_POWER_DIFF_5G_HT40_2S, (void *)buf1);
  7412.     memcpy(pmib->dot11RFEntry.pwrdiff5GHT40_2S, buf1, MAX_5G_CHANNEL_NUM_MIB);
  7413.    
  7414.     apmib_get(MIB_HW_TX_POWER_DIFF_5G_HT20, (void *)buf1);
  7415.     memcpy(pmib->dot11RFEntry.pwrdiff5GHT20, buf1, MAX_5G_CHANNEL_NUM_MIB);
  7416.    
  7417.     apmib_get(MIB_HW_TX_POWER_DIFF_5G_OFDM, (void *)buf1);
  7418.     memcpy(pmib->dot11RFEntry.pwrdiff5GOFDM, buf1, MAX_5G_CHANNEL_NUM_MIB);
  7419. #endif
  7420.    
  7421.     apmib_get(MIB_HW_11N_TSSI1, (void *)&intVal);
  7422.     pmib->dot11RFEntry.tssi1 = intVal;
  7423.  
  7424.     apmib_get(MIB_HW_11N_TSSI2, (void *)&intVal);
  7425.     pmib->dot11RFEntry.tssi2 = intVal;
  7426.  
  7427.     apmib_get(MIB_HW_11N_THER, (void *)&intVal);
  7428.     pmib->dot11RFEntry.ther = intVal;
  7429.  
  7430.     apmib_get(MIB_HW_11N_TRSWITCH, (void *)&intVal);
  7431.     pmib->dot11RFEntry.trswitch = intVal;  
  7432.    
  7433.     apmib_get(MIB_HW_11N_TRSWPAPE_C9, (void *)&intVal);
  7434.     pmib->dot11RFEntry.trsw_pape_C9 = intVal;
  7435.  
  7436.     apmib_get(MIB_HW_11N_TRSWPAPE_CC, (void *)&intVal);
  7437.     pmib->dot11RFEntry.trsw_pape_CC = intVal;
  7438.  
  7439.     apmib_get(MIB_HW_11N_XCAP, (void *)&intVal);
  7440.     pmib->dot11RFEntry.xcap = intVal;
  7441.  
  7442.     apmib_get(MIB_HW_11N_TARGET_PWR, (void *)&intVal);
  7443.     pmib->dot11RFEntry.target_pwr = intVal;
  7444.    
  7445.     if (pmib->dot11RFEntry.dot11RFType == 10) { // Zebra
  7446.         apmib_get(MIB_WLAN_RFPOWER_SCALE, (void *)&intVal);
  7447.         if(intVal == 1)
  7448.             intVal = 3;
  7449.         else if(intVal == 2)
  7450.                 intVal = 6;
  7451.             else if(intVal == 3)
  7452.                     intVal = 9;
  7453.                 else if(intVal == 4)
  7454.                         intVal = 17;
  7455.         if (intVal) {
  7456.             for (i=0; i<MAX_2G_CHANNEL_NUM_MIB; i++) {
  7457.                 if(pmib->dot11RFEntry.pwrlevelCCK_A[i] != 0){
  7458.                     if ((pmib->dot11RFEntry.pwrlevelCCK_A[i] - intVal) >= 1)
  7459.                         pmib->dot11RFEntry.pwrlevelCCK_A[i] -= intVal;
  7460.                     else
  7461.                         pmib->dot11RFEntry.pwrlevelCCK_A[i] = 1;
  7462.                 }
  7463.                 if(pmib->dot11RFEntry.pwrlevelCCK_B[i] != 0){
  7464.                     if ((pmib->dot11RFEntry.pwrlevelCCK_B[i] - intVal) >= 1)
  7465.                         pmib->dot11RFEntry.pwrlevelCCK_B[i] -= intVal;
  7466.                     else
  7467.                         pmib->dot11RFEntry.pwrlevelCCK_B[i] = 1;
  7468.                 }
  7469.                 if(pmib->dot11RFEntry.pwrlevelHT40_1S_A[i] != 0){
  7470.                     if ((pmib->dot11RFEntry.pwrlevelHT40_1S_A[i] - intVal) >= 1)
  7471.                         pmib->dot11RFEntry.pwrlevelHT40_1S_A[i] -= intVal;
  7472.                     else
  7473.                         pmib->dot11RFEntry.pwrlevelHT40_1S_A[i] = 1;
  7474.                 }
  7475.                 if(pmib->dot11RFEntry.pwrlevelHT40_1S_B[i] != 0){
  7476.                     if ((pmib->dot11RFEntry.pwrlevelHT40_1S_B[i] - intVal) >= 1)
  7477.                         pmib->dot11RFEntry.pwrlevelHT40_1S_B[i] -= intVal;
  7478.                     else
  7479.                         pmib->dot11RFEntry.pwrlevelHT40_1S_B[i] = 1;
  7480.                 }
  7481.             }  
  7482.            
  7483. #if defined(CONFIG_RTL_92D_SUPPORT)        
  7484.             for (i=0; i<MAX_5G_CHANNEL_NUM_MIB; i++) {
  7485.                 if(pmib->dot11RFEntry.pwrlevel5GHT40_1S_A[i] != 0){
  7486.                     if ((pmib->dot11RFEntry.pwrlevel5GHT40_1S_A[i] - intVal) >= 1)
  7487.                         pmib->dot11RFEntry.pwrlevel5GHT40_1S_A[i] -= intVal;
  7488.                     else
  7489.                         pmib->dot11RFEntry.pwrlevel5GHT40_1S_A[i] = 1;                 
  7490.                 }
  7491.                 if(pmib->dot11RFEntry.pwrlevel5GHT40_1S_B[i] != 0){
  7492.                     if ((pmib->dot11RFEntry.pwrlevel5GHT40_1S_B[i] - intVal) >= 1)
  7493.                         pmib->dot11RFEntry.pwrlevel5GHT40_1S_B[i] -= intVal;
  7494.                     else
  7495.                         pmib->dot11RFEntry.pwrlevel5GHT40_1S_B[i] = 1;
  7496.                 }
  7497.             }
  7498. #endif //#if defined(CONFIG_RTL_92D_SUPPORT)                       
  7499.         }  
  7500.     }  
  7501. #else
  7502. //!CONFIG_RTL_8196B => rtl8651c+rtl8190
  7503.         apmib_get(MIB_HW_ANT_DIVERSITY, (void *)&intVal);
  7504.         pmib->dot11RFEntry.dot11DiversitySupport = intVal;
  7505.  
  7506.         apmib_get(MIB_HW_TX_ANT, (void *)&intVal);
  7507.         pmib->dot11RFEntry.defaultAntennaB = intVal;
  7508.  
  7509. #if 0
  7510.         apmib_get(MIB_HW_INIT_GAIN, (void *)&intVal);
  7511.         pmib->dot11RFEntry.initialGain = intVal;
  7512. #endif
  7513.  
  7514.         apmib_get(MIB_HW_TX_POWER_CCK, (void *)buf1);
  7515.         memcpy(pmib->dot11RFEntry.pwrlevelCCK, buf1, 14);
  7516.        
  7517.         apmib_get(MIB_HW_TX_POWER_OFDM, (void *)buf1);
  7518.         memcpy(pmib->dot11RFEntry.pwrlevelOFDM, buf1, 162);
  7519.  
  7520.         apmib_get(MIB_HW_11N_LOFDMPWD, (void *)&intVal);
  7521.         pmib->dot11RFEntry.legacyOFDM_pwrdiff = intVal;
  7522.        
  7523.         apmib_get(MIB_HW_11N_ANTPWD_C, (void *)&intVal);
  7524.         pmib->dot11RFEntry.antC_pwrdiff = intVal;
  7525.        
  7526.         apmib_get(MIB_HW_11N_THER_RFIC, (void *)&intVal);
  7527.         pmib->dot11RFEntry.ther_rfic = intVal;
  7528.        
  7529.         apmib_get(MIB_HW_11N_XCAP, (void *)&intVal);
  7530.         pmib->dot11RFEntry.crystalCap = intVal;
  7531.  
  7532.         // set output power scale
  7533.         //if (pmib->dot11RFEntry.dot11RFType == 7) { // Zebra
  7534.             if (pmib->dot11RFEntry.dot11RFType == 10) { // Zebra
  7535.             apmib_get(MIB_WLAN_RFPOWER_SCALE, (void *)&intVal);
  7536.             if(intVal == 1)
  7537.                 intVal = 3;
  7538.             else if(intVal == 2)
  7539.                     intVal = 6;
  7540.                 else if(intVal == 3)
  7541.                         intVal = 9;
  7542.                     else if(intVal == 4)
  7543.                             intVal = 17;
  7544.             if (intVal) {
  7545.                 for (i=0; i<14; i++) {
  7546.                     if(pmib->dot11RFEntry.pwrlevelCCK[i] != 0){
  7547.                         if ((pmib->dot11RFEntry.pwrlevelCCK[i] - intVal) >= 1)
  7548.                             pmib->dot11RFEntry.pwrlevelCCK[i] -= intVal;
  7549.                         else
  7550.                             pmib->dot11RFEntry.pwrlevelCCK[i] = 1;
  7551.                     }
  7552.                 }
  7553.                 for (i=0; i<162; i++) {
  7554.                     if (pmib->dot11RFEntry.pwrlevelOFDM[i] != 0){
  7555.                         if((pmib->dot11RFEntry.pwrlevelOFDM[i] - intVal) >= 1)
  7556.                             pmib->dot11RFEntry.pwrlevelOFDM[i] -= intVal;
  7557.                         else
  7558.                             pmib->dot11RFEntry.pwrlevelOFDM[i] = 1;
  7559.                     }
  7560.                 }      
  7561.             }
  7562.         }
  7563. #endif  //For Check CONFIG_RTL_8196B
  7564.        
  7565.         apmib_get(MIB_WLAN_BEACON_INTERVAL, (void *)&intVal);
  7566.         pmib->dot11StationConfigEntry.dot11BeaconPeriod = intVal;
  7567.  
  7568.         apmib_get(MIB_WLAN_CHANNEL, (void *)&intVal);
  7569.         pmib->dot11RFEntry.dot11channel = intVal;
  7570.  
  7571.         apmib_get(MIB_WLAN_RTS_THRESHOLD, (void *)&intVal);
  7572.         pmib->dot11OperationEntry.dot11RTSThreshold = intVal;
  7573.  
  7574.         apmib_get(MIB_WLAN_FRAG_THRESHOLD, (void *)&intVal);
  7575.         pmib->dot11OperationEntry.dot11FragmentationThreshold = intVal;
  7576.  
  7577.         apmib_get(MIB_WLAN_INACTIVITY_TIME, (void *)&intVal);
  7578.         pmib->dot11OperationEntry.expiretime = intVal;
  7579.  
  7580.         apmib_get(MIB_WLAN_PREAMBLE_TYPE, (void *)&intVal);
  7581.         pmib->dot11RFEntry.shortpreamble = intVal;
  7582.  
  7583.         apmib_get(MIB_WLAN_DTIM_PERIOD, (void *)&intVal);
  7584.         pmib->dot11StationConfigEntry.dot11DTIMPeriod = intVal;
  7585.  
  7586.         /*STBC and Coexist*/
  7587.         apmib_get(MIB_WLAN_STBC_ENABLED,(void *)&intVal);
  7588.         pmib->dot11nConfigEntry.dot11nSTBC = intVal;
  7589.  
  7590.         apmib_get(MIB_WLAN_COEXIST_ENABLED,(void *)&intVal);
  7591.         pmib->dot11nConfigEntry.dot11nCoexist = intVal;
  7592.  
  7593.         apmib_get(MIB_WLAN_ACK_TIMEOUT,(void *)&intVal);
  7594.         pmib->miscEntry.ack_timeout = intVal;
  7595.  
  7596.         //### add by sen_liu 2011.3.29 TX Beamforming update to mib in 92D
  7597.         apmib_get(MIB_WLAN_TX_BEAMFORMING,(void *)&intVal);
  7598.         pmib->dot11RFEntry.txbf = intVal;
  7599.         //### end priv->pmib->dot11RFEntry.txbf
  7600.        
  7601.         // enable/disable the notification for IAPP
  7602.         apmib_get(MIB_WLAN_IAPP_DISABLED, (void *)&intVal);
  7603.         if (intVal == 0)
  7604.             pmib->dot11OperationEntry.iapp_enable = 1;
  7605.         else
  7606.             pmib->dot11OperationEntry.iapp_enable = 0;
  7607.  
  7608.         // set 11g protection mode
  7609.         apmib_get(MIB_WLAN_PROTECTION_DISABLED, (void *)&intVal);
  7610.         pmib->dot11StationConfigEntry.protectionDisabled = intVal;
  7611.  
  7612.         // set block relay
  7613.         apmib_get(MIB_WLAN_BLOCK_RELAY, (void *)&intVal);
  7614.         pmib->dot11OperationEntry.block_relay = intVal;
  7615.  
  7616.         // set WiFi specific mode
  7617.         apmib_get(MIB_WIFI_SPECIFIC, (void *)&intVal);
  7618.         pmib->dot11OperationEntry.wifi_specific = intVal;
  7619.  
  7620.         // Set WDS
  7621.         apmib_get(MIB_WLAN_WDS_ENABLED, (void *)&intVal);
  7622.         apmib_get(MIB_WLAN_WDS_NUM, (void *)&intVal2);
  7623.         pmib->dot11WdsInfo.wdsNum = 0;
  7624. #ifdef MBSSID
  7625.         if (v_previous > 0)
  7626.             intVal = 0;
  7627. #endif
  7628.         if (((mode == 2) || (mode == 3)) &&
  7629.             (intVal != 0) &&
  7630.             (intVal2 != 0)) {
  7631.             for (i=0; i<intVal2; i++) {
  7632.                 buf1[0] = i+1;
  7633.                 apmib_get(MIB_WLAN_WDS, (void *)buf1);
  7634.                 pwds_EntryUI = (WDS_Tp)buf1;
  7635.                 wds_Entry = &(pmib->dot11WdsInfo.entry[i]);
  7636.                 memcpy(wds_Entry->macAddr, &(pwds_EntryUI->macAddr[0]), 6);
  7637.                 wds_Entry->txRate = pwds_EntryUI->fixedTxRate;
  7638.                 pmib->dot11WdsInfo.wdsNum++;
  7639.                 sprintf((char *)buf2, "ifconfig %s-wds%d hw ether %02x%02x%02x%02x%02x%02x", ifname, i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  7640.                 system((char *)buf2);
  7641.             }
  7642.             pmib->dot11WdsInfo.wdsEnabled = intVal;
  7643.         }
  7644.         else
  7645.             pmib->dot11WdsInfo.wdsEnabled = 0;
  7646.  
  7647.         if (((mode == 2) || (mode == 3)) &&
  7648.             (intVal != 0)) {
  7649.             apmib_get(MIB_WLAN_WDS_ENCRYPT, (void *)&intVal);
  7650.             if (intVal == 0)
  7651.                 pmib->dot11WdsInfo.wdsPrivacy = 0;
  7652.             else if (intVal == 1) {
  7653.                 apmib_get(MIB_WLAN_WDS_WEP_KEY, (void *)buf1);
  7654.                 pmib->dot11WdsInfo.wdsPrivacy = 1;
  7655.                 string_to_hex((char *)buf1, &(pmib->dot11WdsInfo.wdsWepKey[0]), 10);
  7656.             }
  7657.             else if (intVal == 2) {
  7658.                 apmib_get(MIB_WLAN_WDS_WEP_KEY, (void *)buf1);
  7659.                 pmib->dot11WdsInfo.wdsPrivacy = 5;
  7660.                 string_to_hex((char *)buf1, &(pmib->dot11WdsInfo.wdsWepKey[0]), 26);
  7661.             }
  7662.             else if (intVal == 3)
  7663.                 pmib->dot11WdsInfo.wdsPrivacy = 2;
  7664.             else
  7665.                 pmib->dot11WdsInfo.wdsPrivacy = 4;
  7666.         }
  7667.  
  7668.         // enable/disable the notification for IAPP
  7669.         apmib_get(MIB_WLAN_IAPP_DISABLED, (void *)&intVal);
  7670.         if (intVal == 0)
  7671.             pmib->dot11OperationEntry.iapp_enable = 1;
  7672.         else
  7673.             pmib->dot11OperationEntry.iapp_enable = 0;
  7674.  
  7675. #if defined(CONFIG_RTK_MESH) && defined(_MESH_ACL_ENABLE_) // below code copy above ACL code
  7676.         // Copy Webpage setting to userspace MIB struct table
  7677.         pmib->dot1180211sInfo.mesh_acl_num = 0;
  7678.         apmib_get(MIB_MESH_ACL_ENABLED, (void *)&intVal);
  7679.         pmib->dot1180211sInfo.mesh_acl_mode = intVal;
  7680.  
  7681.         if (intVal != 0) {
  7682.             apmib_get(MIB_MESH_ACL_NUM, (void *)&intVal);
  7683.             if (intVal != 0) {
  7684.                 for (i=0; i<intVal; i++) {
  7685.                     buf1[0] = i+1;
  7686.                     apmib_get(MIB_MESH_ACL_ADDR, (void *)buf1);
  7687.                     pAcl = (MACFILTER_T *)buf1;
  7688.                     memcpy(&(pmib->dot1180211sInfo.mesh_acl_addr[i][0]), &(pAcl->macAddr[0]), 6);
  7689.                     pmib->dot1180211sInfo.mesh_acl_num++;
  7690.                 }
  7691.             }
  7692.         }
  7693. #endif
  7694.  
  7695.         // set nat2.5 disable when client and mac clone is set
  7696.         apmib_get(MIB_WLAN_MACCLONE_ENABLED, (void *)&intVal);
  7697.         if ((intVal == 1) && (mode == 1)) {
  7698.             pmib->ethBrExtInfo.nat25_disable = 1;
  7699.             pmib->ethBrExtInfo.macclone_enable = 1;
  7700.         }
  7701.         else {
  7702.             pmib->ethBrExtInfo.nat25_disable = 0;
  7703.             pmib->ethBrExtInfo.macclone_enable = 0;
  7704.         }      
  7705.  
  7706.         // set nat2.5 disable and macclone disable when wireless isp mode
  7707.         apmib_get(MIB_OP_MODE, (void *)&intVal);
  7708.         if (intVal == 2) {
  7709.             pmib->ethBrExtInfo.nat25_disable = 0;   // enable nat25 for ipv6-passthru ping6 fail issue at wisp mode && wlan client mode .
  7710.             pmib->ethBrExtInfo.macclone_enable = 0;
  7711.         }
  7712.  
  7713. #ifdef WIFI_SIMPLE_CONFIG
  7714.         pmib->wscEntry.wsc_enable = 0;
  7715. #endif
  7716.  
  7717.     // for 11n
  7718.         apmib_get(MIB_WLAN_CHANNEL_BONDING, &channel_bound);
  7719.         pmib->dot11nConfigEntry.dot11nUse40M = channel_bound;
  7720.         apmib_get(MIB_WLAN_CONTROL_SIDEBAND, &intVal);
  7721.         if(channel_bound ==0){
  7722.             pmib->dot11nConfigEntry.dot11n2ndChOffset = 0;
  7723.         }else {
  7724.             if(intVal == 0 )
  7725.                 pmib->dot11nConfigEntry.dot11n2ndChOffset = 1;
  7726.             if(intVal == 1 )
  7727.                 pmib->dot11nConfigEntry.dot11n2ndChOffset = 2; 
  7728.         }
  7729.         apmib_get(MIB_WLAN_SHORT_GI, &intVal);
  7730.         pmib->dot11nConfigEntry.dot11nShortGIfor20M = intVal;
  7731.         pmib->dot11nConfigEntry.dot11nShortGIfor40M = intVal;
  7732.         /*
  7733.         apmib_get(MIB_WLAN_11N_STBC, &intVal);
  7734.         pmib->dot11nConfigEntry.dot11nSTBC = intVal;
  7735.         apmib_get(MIB_WLAN_11N_COEXIST, &intVal);
  7736.         pmib->dot11nConfigEntry.dot11nCoexist = intVal;
  7737.         */
  7738.         apmib_get(MIB_WLAN_AGGREGATION, &aggregation);
  7739.         if(aggregation ==0){
  7740.             pmib->dot11nConfigEntry.dot11nAMPDU = 0;
  7741.             pmib->dot11nConfigEntry.dot11nAMSDU = 0;
  7742.         }else if(aggregation ==1){
  7743.             pmib->dot11nConfigEntry.dot11nAMPDU = 1;
  7744.             pmib->dot11nConfigEntry.dot11nAMSDU = 0;
  7745.         }else if(aggregation ==2){
  7746.             pmib->dot11nConfigEntry.dot11nAMPDU = 0;
  7747.             pmib->dot11nConfigEntry.dot11nAMSDU = 1;
  7748.         }
  7749.         else if(aggregation ==3){
  7750.             pmib->dot11nConfigEntry.dot11nAMPDU = 1;
  7751.             pmib->dot11nConfigEntry.dot11nAMSDU = 1;
  7752.         }
  7753.  
  7754. #if defined(CONFIG_RTL_819X) && defined(MBSSID)
  7755.         if(pmib->dot11OperationEntry.opmode & 0x00000010){// AP mode
  7756.             for (vwlan_idx = 1; vwlan_idx < 5; vwlan_idx++) {
  7757.                 apmib_get(MIB_WLAN_WLAN_DISABLED, (void *)&intVal4);
  7758.                 if (intVal4 == 0)
  7759.                     vap_enable++;
  7760.                 intVal4=0;
  7761.             }
  7762.             vwlan_idx = 0;
  7763.         }
  7764.         if (vap_enable && (mode ==  AP_MODE || mode ==  AP_WDS_MODE))  
  7765.             pmib->miscEntry.vap_enable=1;
  7766.         else
  7767.             pmib->miscEntry.vap_enable=0;
  7768. #endif
  7769.     }
  7770.  
  7771.     if (vwlan_idx != NUM_VWLAN_INTERFACE) { // not repeater interface
  7772.         apmib_get(MIB_WLAN_BASIC_RATES, (void *)&intVal);
  7773.         pmib->dot11StationConfigEntry.dot11BasicRates = intVal;
  7774.  
  7775.         apmib_get(MIB_WLAN_SUPPORTED_RATES, (void *)&intVal);
  7776.         pmib->dot11StationConfigEntry.dot11SupportedRates = intVal;
  7777.  
  7778.         apmib_get(MIB_WLAN_RATE_ADAPTIVE_ENABLED, (void *)&intVal);
  7779.         if (intVal == 0) {
  7780.             pmib->dot11StationConfigEntry.autoRate = 0;
  7781.             apmib_get(MIB_WLAN_FIX_RATE, (void *)&intVal);
  7782.             pmib->dot11StationConfigEntry.fixedTxRate = intVal;
  7783.         }
  7784.         else
  7785.             pmib->dot11StationConfigEntry.autoRate = 1;
  7786.  
  7787.         apmib_get(MIB_WLAN_HIDDEN_SSID, (void *)&intVal);
  7788.         pmib->dot11OperationEntry.hiddenAP = intVal;
  7789.  
  7790. #if defined(CONFIG_RTL_92D_SUPPORT)
  7791.         apmib_get(MIB_WLAN_PHY_BAND_SELECT, (void *)&intVal);
  7792.         pmib->dot11RFEntry.phyBandSelect = intVal;
  7793.         apmib_get(MIB_WLAN_MAC_PHY_MODE, (void *)&intVal);
  7794.         pmib->dot11RFEntry.macPhyMode = intVal;
  7795. #endif
  7796.  
  7797.     // set band
  7798.         apmib_get(MIB_WLAN_BAND, (void *)&intVal);
  7799.         wlan_band = intVal;
  7800.         if ((mode != 1) && (pmib->dot11OperationEntry.wifi_specific == 1) && (wlan_band == 2))
  7801.             wlan_band = 3;
  7802.  
  7803.         if (wlan_band == 8) { // pure-11n
  7804. #if defined(CONFIG_RTL_92D_SUPPORT)
  7805.             if(pmib->dot11RFEntry.phyBandSelect == PHYBAND_5G)
  7806.                 wlan_band += 4; // a+n
  7807.             else if (pmib->dot11RFEntry.phyBandSelect == PHYBAND_2G)
  7808. #endif
  7809.                 wlan_band += 3; // b+g+n
  7810.             pmib->dot11StationConfigEntry.legacySTADeny = 3;
  7811.         }
  7812.         else if (wlan_band == 2) { // pure-11g
  7813.             wlan_band += 1; // b+g
  7814.             pmib->dot11StationConfigEntry.legacySTADeny = 1;
  7815.         }
  7816.         else if (wlan_band == 10) { // g+n
  7817.             wlan_band += 1; // b+g+n
  7818.             pmib->dot11StationConfigEntry.legacySTADeny = 1;
  7819.         }
  7820.         else
  7821.             pmib->dot11StationConfigEntry.legacySTADeny = 0;   
  7822.  
  7823.         pmib->dot11BssType.net_work_type = wlan_band;
  7824.        
  7825.         // set guest access
  7826.         apmib_get(MIB_WLAN_ACCESS, (void *)&intVal);
  7827.         pmib->dot11OperationEntry.guest_access = intVal;
  7828.  
  7829.         // set WMM
  7830.         apmib_get(MIB_WLAN_WMM_ENABLED, (void *)&intVal);
  7831.         pmib->dot11QosEntry.dot11QosEnable = intVal;       
  7832.     }
  7833.  
  7834.     apmib_get(MIB_WLAN_AUTH_TYPE, (void *)&intVal);
  7835.     apmib_get(MIB_WLAN_ENCRYPT, (void *)&encrypt);
  7836. #ifdef CONFIG_RTL_WAPI_SUPPORT
  7837.     /*wapi is independed. disable WAPI first if not WAPI*/
  7838.     if(7 !=encrypt)
  7839.     {
  7840.         pmib->wapiInfo.wapiType=0; 
  7841.     }
  7842. #endif
  7843.     if ((intVal == 1) && (encrypt != 1)) {
  7844.         // shared-key and not WEP enabled, force to open-system
  7845.         intVal = 0;
  7846.     }
  7847.     pmib->dot1180211AuthEntry.dot11AuthAlgrthm = intVal;
  7848.  
  7849.     if (encrypt == 0)
  7850.         pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 0;
  7851.     else if (encrypt == 1) {
  7852.         // WEP mode
  7853.         apmib_get(MIB_WLAN_WEP, (void *)&wep);
  7854.         if (wep == 1) {
  7855.             pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 1;
  7856.             apmib_get(MIB_WLAN_WEP64_KEY1, (void *)buf1);
  7857.             memcpy(&(pmib->dot11DefaultKeysTable.keytype[0]), buf1, 5);
  7858.             apmib_get(MIB_WLAN_WEP64_KEY2, (void *)buf1);
  7859.             memcpy(&(pmib->dot11DefaultKeysTable.keytype[1]), buf1, 5);
  7860.             apmib_get(MIB_WLAN_WEP64_KEY3, (void *)buf1);
  7861.             memcpy(&(pmib->dot11DefaultKeysTable.keytype[2]), buf1, 5);
  7862.             apmib_get(MIB_WLAN_WEP64_KEY4, (void *)buf1);
  7863.             memcpy(&(pmib->dot11DefaultKeysTable.keytype[3]), buf1, 5);
  7864.             apmib_get(MIB_WLAN_WEP_DEFAULT_KEY, (void *)&intVal);
  7865.             pmib->dot1180211AuthEntry.dot11PrivacyKeyIndex = intVal;
  7866.         }
  7867.         else {
  7868.             pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 5;
  7869.             apmib_get(MIB_WLAN_WEP128_KEY1, (void *)buf1);
  7870.             memcpy(&(pmib->dot11DefaultKeysTable.keytype[0]), buf1, 13);
  7871.             apmib_get(MIB_WLAN_WEP128_KEY2, (void *)buf1);
  7872.             memcpy(&(pmib->dot11DefaultKeysTable.keytype[1]), buf1, 13);
  7873.             apmib_get(MIB_WLAN_WEP128_KEY3, (void *)buf1);
  7874.             memcpy(&(pmib->dot11DefaultKeysTable.keytype[2]), buf1, 13);
  7875.             apmib_get(MIB_WLAN_WEP128_KEY4, (void *)buf1);
  7876.             memcpy(&(pmib->dot11DefaultKeysTable.keytype[3]), buf1, 13);
  7877.             apmib_get(MIB_WLAN_WEP_DEFAULT_KEY, (void *)&intVal);
  7878.             pmib->dot1180211AuthEntry.dot11PrivacyKeyIndex = intVal;
  7879.         }
  7880.     }
  7881. #ifdef CONFIG_RTL_WAPI_SUPPORT 
  7882.     else if(7 == encrypt)
  7883.     {
  7884.         pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 7;
  7885.         pmib->dot1180211AuthEntry.dot11AuthAlgrthm = 0;
  7886.     }
  7887. #endif 
  7888.     else {
  7889.         // WPA mode
  7890.         pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 2;
  7891.     }
  7892.  
  7893. #ifndef CONFIG_RTL8196B_TLD
  7894. #ifdef MBSSID
  7895.     if (vwlan_idx > 0 && pmib->dot11OperationEntry.guest_access)
  7896.         pmib->dot11OperationEntry.block_relay = 1; 
  7897. #endif
  7898. #endif
  7899.  
  7900.     // Set 802.1x flag
  7901.     enable1x = 0;
  7902.     if (encrypt < 2) {
  7903.         apmib_get(MIB_WLAN_ENABLE_1X, (void *)&intVal);
  7904.         apmib_get(MIB_WLAN_MAC_AUTH_ENABLED, (void *)&intVal2);
  7905.         if ((intVal != 0) || (intVal2 != 0))
  7906.             enable1x = 1;
  7907.     }
  7908. #ifdef CONFIG_RTL_WAPI_SUPPORT
  7909.     else if(encrypt == 7)
  7910.     {
  7911.         /*wapi*/
  7912.         enable1x = 0;
  7913.     }
  7914. #endif 
  7915.     else
  7916.         enable1x = 1;
  7917.     pmib->dot118021xAuthEntry.dot118021xAlgrthm = enable1x;
  7918.  
  7919. #ifdef CONFIG_RTL_WAPI_SUPPORT
  7920.     if(7 == encrypt)
  7921.     {
  7922.         //apmib_get(MIB_WLAN_WAPI_ASIPADDR,);
  7923.         apmib_get(MIB_WLAN_WAPI_AUTH,(void *)&intVal);
  7924.         pmib->wapiInfo.wapiType=intVal;
  7925.  
  7926.         apmib_get(MIB_WLAN_WAPI_MCAST_PACKETS,(void *)&intVal);
  7927.         pmib->wapiInfo.wapiUpdateMCastKeyPktNum=intVal;
  7928.        
  7929.         apmib_get(MIB_WLAN_WAPI_MCASTREKEY,(void *)&intVal);
  7930.         pmib->wapiInfo.wapiUpdateMCastKeyType=intVal;
  7931.  
  7932.         apmib_get(MIB_WLAN_WAPI_MCAST_TIME,(void *)&intVal);
  7933.         pmib->wapiInfo.wapiUpdateMCastKeyTimeout=intVal;
  7934.  
  7935.         apmib_get(MIB_WLAN_WAPI_UCAST_PACKETS,(void *)&intVal);
  7936.         pmib->wapiInfo.wapiUpdateUCastKeyPktNum=intVal;
  7937.        
  7938.         apmib_get(MIB_WLAN_WAPI_UCASTREKEY,(void *)&intVal);
  7939.         pmib->wapiInfo.wapiUpdateUCastKeyType=intVal;
  7940.  
  7941.         apmib_get(MIB_WLAN_WAPI_UCAST_TIME,(void *)&intVal);
  7942.         pmib->wapiInfo.wapiUpdateUCastKeyTimeout=intVal;
  7943.  
  7944.         /*1: hex  -else passthru*/
  7945.         apmib_get(MIB_WLAN_WAPI_PSK_FORMAT,(void *)&intVal2);
  7946.         apmib_get(MIB_WLAN_WAPI_PSKLEN,(void *)&intVal);
  7947.         apmib_get(MIB_WLAN_WAPI_PSK,(void *)buf1);
  7948.         pmib->wapiInfo.wapiPsk.len=intVal;
  7949.         if(1 == intVal2 )
  7950.         {
  7951.             /*hex*/
  7952.             string_to_hex(buf1, buf2, pmib->wapiInfo.wapiPsk.len*2);
  7953.         }else
  7954.         {
  7955.             /*passthru*/
  7956.             strcpy(buf2,buf1);
  7957.         }
  7958.         memcpy(pmib->wapiInfo.wapiPsk.octet,buf2,pmib->wapiInfo.wapiPsk.len);
  7959.     }
  7960. #endif
  7961.  
  7962. #ifdef CONFIG_RTK_MESH
  7963.  
  7964. #ifdef CONFIG_NEW_MESH_UI
  7965.     //new feature:Mesh enable/disable
  7966.     //brian add new key:MIB_MESH_ENABLE
  7967.     pmib->dot1180211sInfo.meshSilence = 0;
  7968.  
  7969.     apmib_get(MIB_MESH_ENABLE,(void *)&intVal);
  7970.     if (mode == AP_MESH_MODE || mode == MESH_MODE)
  7971.     {
  7972.         if( intVal )
  7973.             pmib->dot1180211sInfo.mesh_enable = 1;
  7974.         else
  7975.             pmib->dot1180211sInfo.mesh_enable = 0;
  7976.     }
  7977.     else
  7978.         pmib->dot1180211sInfo.mesh_enable = 0;
  7979.  
  7980.     // set mesh argument
  7981.     // brian change to shutdown portal/root as default
  7982.     if (mode == AP_MESH_MODE)
  7983.     {
  7984.         pmib->dot1180211sInfo.mesh_ap_enable = 1;
  7985.         pmib->dot1180211sInfo.mesh_portal_enable = 0;
  7986.     }
  7987.     else if (mode == MESH_MODE)
  7988.     {
  7989.         if( !intVal )
  7990.             //pmib->dot11OperationEntry.opmode += 64; // WIFI_MESH_STATE = 0x00000040
  7991.             pmib->dot1180211sInfo.meshSilence = 1;
  7992.  
  7993.         pmib->dot1180211sInfo.mesh_ap_enable = 0;
  7994.         pmib->dot1180211sInfo.mesh_portal_enable = 0;      
  7995.     }
  7996.     else
  7997.     {
  7998.         pmib->dot1180211sInfo.mesh_ap_enable = 0;
  7999.         pmib->dot1180211sInfo.mesh_portal_enable = 0;  
  8000.     }
  8001.     #if 0   //by brian, dont enable root by default
  8002.     apmib_get(MIB_MESH_ROOT_ENABLE, (void *)&intVal);
  8003.     pmib->dot1180211sInfo.mesh_root_enable = intVal;
  8004.     #else
  8005.     pmib->dot1180211sInfo.mesh_root_enable = 0;
  8006.     #endif
  8007. #else
  8008.     if (mode == AP_MPP_MODE)
  8009.     {
  8010.         pmib->dot1180211sInfo.mesh_enable = 1;
  8011.         pmib->dot1180211sInfo.mesh_ap_enable = 1;
  8012.         pmib->dot1180211sInfo.mesh_portal_enable = 1;  
  8013.     }
  8014.     else if (mode == MPP_MODE)
  8015.     {
  8016.         pmib->dot1180211sInfo.mesh_enable = 1;
  8017.         pmib->dot1180211sInfo.mesh_ap_enable = 0;
  8018.         pmib->dot1180211sInfo.mesh_portal_enable = 1;
  8019.     }
  8020.     else if (mode == MAP_MODE)
  8021.     {
  8022.         pmib->dot1180211sInfo.mesh_enable = 1;
  8023.         pmib->dot1180211sInfo.mesh_ap_enable = 1;
  8024.         pmib->dot1180211sInfo.mesh_portal_enable = 0;
  8025.     }      
  8026.     else if (mode == MP_MODE)
  8027.     {
  8028.         pmib->dot1180211sInfo.mesh_enable = 1;
  8029.         pmib->dot1180211sInfo.mesh_ap_enable = 0;
  8030.         pmib->dot1180211sInfo.mesh_portal_enable = 0;      
  8031.     }
  8032.     else
  8033.     {
  8034.         pmib->dot1180211sInfo.mesh_enable = 0;
  8035.         pmib->dot1180211sInfo.mesh_ap_enable = 0;
  8036.         pmib->dot1180211sInfo.mesh_portal_enable = 0;  
  8037.     }
  8038.  
  8039.     apmib_get(MIB_MESH_ROOT_ENABLE, (void *)&intVal);
  8040.     pmib->dot1180211sInfo.mesh_root_enable = intVal;
  8041. #endif
  8042.     apmib_get(MIB_MESH_MAX_NEIGHTBOR, (void *)&intVal);
  8043.     pmib->dot1180211sInfo.mesh_max_neightbor = intVal;
  8044.  
  8045.     apmib_get(MIB_SCRLOG_ENABLED, (void *)&intVal);
  8046.     pmib->dot1180211sInfo.log_enabled = intVal;
  8047.  
  8048.     apmib_get(MIB_MESH_ID, (void *)buf1);
  8049.     intVal2 = strlen(buf1);
  8050.     memset(pmib->dot1180211sInfo.mesh_id, 0, 32);
  8051.     memcpy(pmib->dot1180211sInfo.mesh_id, buf1, intVal2);
  8052.  
  8053.     apmib_get(MIB_MESH_ENCRYPT, (void *)&intVal);
  8054.     apmib_get(MIB_MESH_WPA_AUTH, (void *)&intVal2);
  8055.  
  8056.     if( intVal2 == 2 && intVal)
  8057.         pmib->dot11sKeysTable.dot11Privacy  = 2;
  8058.     else
  8059.         pmib->dot11sKeysTable.dot11Privacy  = 0;
  8060.    
  8061. #ifdef  _11s_TEST_MODE_
  8062.  
  8063.     apmib_get(MIB_MESH_TEST_PARAM1, (void *)&intVal);
  8064.     pmib->dot1180211sInfo.mesh_reserved1 = intVal;
  8065.  
  8066.     apmib_get(MIB_MESH_TEST_PARAM2, (void *)&intVal);
  8067.     pmib->dot1180211sInfo.mesh_reserved2 = intVal;
  8068.  
  8069.     apmib_get(MIB_MESH_TEST_PARAM3, (void *)&intVal);
  8070.     pmib->dot1180211sInfo.mesh_reserved3 = intVal;
  8071.  
  8072.     apmib_get(MIB_MESH_TEST_PARAM4, (void *)&intVal);
  8073.     pmib->dot1180211sInfo.mesh_reserved4 = intVal;
  8074.  
  8075.     apmib_get(MIB_MESH_TEST_PARAM5, (void *)&intVal);
  8076.     pmib->dot1180211sInfo.mesh_reserved5 = intVal;
  8077.  
  8078.     apmib_get(MIB_MESH_TEST_PARAM6, (void *)&intVal);
  8079.     pmib->dot1180211sInfo.mesh_reserved6 = intVal;
  8080.  
  8081.     apmib_get(MIB_MESH_TEST_PARAM7, (void *)&intVal);
  8082.     pmib->dot1180211sInfo.mesh_reserved7 = intVal;
  8083.  
  8084.     apmib_get(MIB_MESH_TEST_PARAM8, (void *)&intVal);
  8085.     pmib->dot1180211sInfo.mesh_reserved8 = intVal;
  8086.    
  8087.     apmib_get(MIB_MESH_TEST_PARAM9, (void *)&intVal);
  8088.     pmib->dot1180211sInfo.mesh_reserved9 = intVal;
  8089.  
  8090.     apmib_get(MIB_MESH_TEST_PARAMA, (void *)&intVal);
  8091.     pmib->dot1180211sInfo.mesh_reserveda = intVal;
  8092.  
  8093.     apmib_get(MIB_MESH_TEST_PARAMB, (void *)&intVal);
  8094.     pmib->dot1180211sInfo.mesh_reservedb = intVal;
  8095.    
  8096.     apmib_get(MIB_MESH_TEST_PARAMC, (void *)&intVal);
  8097.     pmib->dot1180211sInfo.mesh_reservedc = intVal;
  8098.  
  8099.     apmib_get(MIB_MESH_TEST_PARAMD, (void *)&intVal);
  8100.     pmib->dot1180211sInfo.mesh_reservedd = intVal;
  8101.  
  8102.     apmib_get(MIB_MESH_TEST_PARAME, (void *)&intVal);
  8103.     pmib->dot1180211sInfo.mesh_reservede = intVal;
  8104.  
  8105.     apmib_get(MIB_MESH_TEST_PARAMF, (void *)&intVal);
  8106.     pmib->dot1180211sInfo.mesh_reservedf = intVal;
  8107.    
  8108.     apmib_get(MIB_MESH_TEST_PARAMSTR1, (void *)buf1);
  8109.     intVal2 = strlen(buf1)<15 ? strlen(buf1) : 15;
  8110.     memset(pmib->dot1180211sInfo.mesh_reservedstr1, 0, 16);
  8111.     memcpy(pmib->dot1180211sInfo.mesh_reservedstr1, buf1, intVal2);
  8112.    
  8113. #endif
  8114.    
  8115. #endif // CONFIG_RTK_MESH
  8116.  
  8117.     // When using driver base WPA, set wpa setting to driver
  8118. #if 1
  8119.     int intVal3;
  8120.     apmib_get(MIB_WLAN_WPA_AUTH, (void *)&intVal3);
  8121. //#ifdef CONFIG_RTL_8196B
  8122. // button 2009.05.21
  8123. #if 1
  8124.     if ((intVal3 & WPA_AUTH_PSK) && encrypt >= 2
  8125. #ifdef CONFIG_RTL_WAPI_SUPPORT
  8126. && encrypt < 7
  8127. #endif
  8128. )
  8129. #else
  8130.     if (mode != 1 && (intVal3 & WPA_AUTH_PSK) && encrypt >= 2
  8131. #ifdef CONFIG_RTL_WAPI_SUPPORT
  8132. && encrypt < 7
  8133. #endif
  8134. )
  8135. #endif
  8136.     {
  8137.         if (encrypt == 2)
  8138.             intVal = 1;
  8139.         else if (encrypt == 4)
  8140.             intVal = 2;
  8141.         else if (encrypt == 6)
  8142.             intVal = 3;
  8143.         else {
  8144.             printf("invalid ENCRYPT value!\n");
  8145.             return -1;
  8146.         }
  8147.         pmib->dot1180211AuthEntry.dot11EnablePSK = intVal;
  8148.  
  8149.         apmib_get(MIB_WLAN_WPA_PSK, (void *)buf1);
  8150.         strcpy((char *)pmib->dot1180211AuthEntry.dot11PassPhrase, (char *)buf1);
  8151.  
  8152.         apmib_get(MIB_WLAN_WPA_GROUP_REKEY_TIME, (void *)&intVal);
  8153.         pmib->dot1180211AuthEntry.dot11GKRekeyTime = intVal;           
  8154.     }
  8155.     else       
  8156.         pmib->dot1180211AuthEntry.dot11EnablePSK = 0;
  8157.  
  8158. #if 1
  8159. if (intVal3 != 0 && encrypt >= 2
  8160. #ifdef CONFIG_RTL_WAPI_SUPPORT
  8161. && encrypt < 7
  8162. #endif
  8163. )
  8164. #else
  8165.     if (mode != 1 && intVal3 != 0 && encrypt >= 2
  8166. #ifdef CONFIG_RTL_WAPI_SUPPORT
  8167. && encrypt < 7
  8168. #endif
  8169. )
  8170. #endif
  8171.     {
  8172.         if (encrypt == 2 || encrypt == 6) {
  8173.             apmib_get(MIB_WLAN_WPA_CIPHER_SUITE, (void *)&intVal2);
  8174.             if (intVal2 == 1)
  8175.                 intVal = 2;
  8176.             else if (intVal2 == 2)
  8177.                 intVal = 8;
  8178.             else if (intVal2 == 3)
  8179.                 intVal = 10;
  8180.             else {
  8181.                 printf("invalid WPA_CIPHER_SUITE value!\n");
  8182.                 return -1;
  8183.             }
  8184.             pmib->dot1180211AuthEntry.dot11WPACipher = intVal;         
  8185.         }
  8186.        
  8187.         if (encrypt == 4 || encrypt == 6) {
  8188.             apmib_get(MIB_WLAN_WPA2_CIPHER_SUITE, (void *)&intVal2);
  8189.             if (intVal2 == 1)
  8190.                 intVal = 2;
  8191.             else if (intVal2 == 2)
  8192.                 intVal = 8;
  8193.             else if (intVal2 == 3)
  8194.                 intVal = 10;
  8195.             else {
  8196.                 printf("invalid WPA2_CIPHER_SUITE value!\n");
  8197.                 return -1;
  8198.             }
  8199.             pmib->dot1180211AuthEntry.dot11WPA2Cipher = intVal;        
  8200.         }
  8201.     }
  8202. #endif
  8203.  
  8204.     wrq.u.data.pointer = (caddr_t)pmib;
  8205.     wrq.u.data.length = sizeof(struct wifi_mib);
  8206.     if (ioctl(skfd, 0x8B43, &wrq) < 0) {
  8207.         printf("Set WLAN MIB failed!\n");
  8208.         free(pmib);
  8209.         close(skfd);
  8210.         return -1;
  8211.     }
  8212.     close(skfd);
  8213.  
  8214. #if 0
  8215. //#ifdef UNIVERSAL_REPEATER
  8216.     // set repeater interface
  8217.     if (!strcmp(ifname, "wlan0")) {
  8218.         apmib_get(MIB_REPEATER_ENABLED1, (void *)&intVal);
  8219.         apmib_get(MIB_WLAN_NETWORK_TYPE, (void *)&intVal2);    
  8220.         system("ifconfig wlan0-vxd down");
  8221.         if (intVal != 0 && mode != WDS_MODE &&
  8222.                 !(mode==CLIENT_MODE && intVal2==ADHOC)) {
  8223.             skfd = socket(AF_INET, SOCK_DGRAM, 0);
  8224.             strncpy(wrq.ifr_name, "wlan0-vxd", IFNAMSIZ);
  8225.             if (ioctl(skfd, SIOCGIWNAME, &wrq) < 0) {
  8226.                 printf("Interface open failed!\n");
  8227.                 free(pmib);
  8228.                 close(skfd);
  8229.                 return -1;
  8230.             }
  8231.  
  8232.             wrq.u.data.pointer = (caddr_t)pmib;
  8233.             wrq.u.data.length = sizeof(struct wifi_mib);
  8234.             if (ioctl(skfd, 0x8B42, &wrq) < 0) {
  8235.                 printf("Get WLAN MIB failed!\n");
  8236.                 free(pmib);
  8237.                 close(skfd);
  8238.                 return -1;
  8239.             }
  8240.  
  8241.             apmib_get(MIB_REPEATER_SSID1, (void *)buf1);
  8242.             intVal2 = strlen(buf1);
  8243.             pmib->dot11StationConfigEntry.dot11DesiredSSIDLen = intVal2;
  8244.             memset(pmib->dot11StationConfigEntry.dot11DesiredSSID, 0, 32);
  8245.             memcpy(pmib->dot11StationConfigEntry.dot11DesiredSSID, buf1, intVal2);
  8246.  
  8247.             sprintf(buf1, "ifconfig %s-vxd hw ether %02x%02x%02x%02x%02x%02x", ifname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  8248.             system(buf1);
  8249.  
  8250.             enable1xVxd = 0;
  8251.             if (encrypt == 0)
  8252.                 pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 0;
  8253.             else if (encrypt == 1) {
  8254.                 if (enable1x == 0) {
  8255.                     if (wep == 1)
  8256.                         pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 1;
  8257.                     else
  8258.                         pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 5;
  8259.                 }
  8260.                 else
  8261.                     pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 0;
  8262.             }
  8263.             else {
  8264.                 apmib_get(MIB_WLAN_WPA_AUTH, (void *)&intVal2);
  8265.                 if (intVal2 == 2) {
  8266.                     pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 2;
  8267.                     enable1xVxd = 1;
  8268.                 }
  8269.                 else
  8270.                     pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 0;
  8271.             }
  8272.             pmib->dot118021xAuthEntry.dot118021xAlgrthm = enable1xVxd;
  8273.            
  8274.             wrq.u.data.pointer = (caddr_t)pmib;
  8275.             wrq.u.data.length = sizeof(struct wifi_mib);
  8276.             if (ioctl(skfd, 0x8B43, &wrq) < 0) {
  8277.                 printf("Set WLAN MIB failed!\n");
  8278.                 free(pmib);
  8279.                 close(skfd);
  8280.                 return -1;
  8281.             }
  8282.             close(skfd);
  8283.         }
  8284.     }
  8285.  
  8286.     if (!strcmp(ifname, "wlan1")) {
  8287.         apmib_get(MIB_REPEATER_ENABLED1, (void *)&intVal);
  8288.         system("ifconfig wlan1-vxd down");
  8289.         if (intVal != 0) {
  8290.             skfd = socket(AF_INET, SOCK_DGRAM, 0);
  8291.             strncpy(wrq.ifr_name, "wlan1-vxd", IFNAMSIZ);
  8292.             if (ioctl(skfd, SIOCGIWNAME, &wrq) < 0) {
  8293.                 printf("Interface open failed!\n");
  8294.                 free(pmib);
  8295.                 close(skfd);
  8296.                 return -1;
  8297.             }
  8298.  
  8299.             wrq.u.data.pointer = (caddr_t)pmib;
  8300.             wrq.u.data.length = sizeof(struct wifi_mib);
  8301.             if (ioctl(skfd, 0x8B42, &wrq) < 0) {
  8302.                 printf("Get WLAN MIB failed!\n");
  8303.                 free(pmib);
  8304.                 close(skfd);
  8305.                 return -1;
  8306.             }
  8307.  
  8308.             apmib_get(MIB_REPEATER_SSID1, (void *)buf1);
  8309.             intVal2 = strlen(buf1);
  8310.             pmib->dot11StationConfigEntry.dot11DesiredSSIDLen = intVal2;
  8311.             memset(pmib->dot11StationConfigEntry.dot11DesiredSSID, 0, 32);
  8312.             memcpy(pmib->dot11StationConfigEntry.dot11DesiredSSID, buf1, intVal2);
  8313.  
  8314.             sprintf(buf1, "ifconfig %s-vxd hw ether %02x%02x%02x%02x%02x%02x", ifname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  8315.             system(buf1);
  8316.  
  8317.             enable1xVxd = 0;
  8318.             if (encrypt == 0)
  8319.                 pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 0;
  8320.             else if (encrypt == 1) {
  8321.                 if (enable1x == 0) {
  8322.                     if (wep == 1)
  8323.                         pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 1;
  8324.                     else
  8325.                         pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 5;
  8326.                 }
  8327.                 else
  8328.                     pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 0;
  8329.             }
  8330.             else {
  8331.                 apmib_get(MIB_WLAN_WPA_AUTH, (void *)&intVal2);
  8332.                 if (intVal2 == 2) {
  8333.                     pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 2;
  8334.                     enable1xVxd = 1;
  8335.                 }
  8336.                 else
  8337.                     pmib->dot1180211AuthEntry.dot11PrivacyAlgrthm = 0;
  8338.             }
  8339.             pmib->dot118021xAuthEntry.dot118021xAlgrthm = enable1xVxd;
  8340.  
  8341.             wrq.u.data.pointer = (caddr_t)pmib;
  8342.             wrq.u.data.length = sizeof(struct wifi_mib);
  8343.             if (ioctl(skfd, 0x8B43, &wrq) < 0) {
  8344.                 printf("Set WLAN MIB failed!\n");
  8345.                 free(pmib);
  8346.                 close(skfd);
  8347.                 return -1;
  8348.             }
  8349.             close(skfd);
  8350.         }
  8351.     }
  8352. #endif
  8353.  
  8354.     free(pmib);
  8355.     return 0;
  8356. }
  8357. #endif // WLAN_FAST_INIT
  8358.  
  8359. #ifdef HOME_GATEWAY
  8360. static int generatePPPConf(int is_pppoe, char *option_file, char *pap_file, char *chap_file)
  8361. {
  8362.     FILE *fd;
  8363.     char tmpbuf[200], buf1[100], buf2[100];
  8364.     int srcIdx, dstIdx;
  8365.  
  8366.     if ( !apmib_init()) {
  8367.         printf("Initialize AP MIB failed!\n");
  8368.         return -1;
  8369.     }
  8370.  
  8371.     if (is_pppoe) {
  8372.         apmib_get(MIB_PPP_USER_NAME, (void *)buf1);
  8373.         apmib_get(MIB_PPP_PASSWORD, (void *)buf2);
  8374.  
  8375.     }
  8376.     else {
  8377.         apmib_get(MIB_PPTP_USER_NAME, (void *)buf1);
  8378.         apmib_get(MIB_PPTP_PASSWORD, (void *)buf2);    
  8379.     }
  8380.     // delete '"' in the value
  8381.     for (srcIdx=0, dstIdx=0; buf1[srcIdx]; srcIdx++, dstIdx++) {
  8382.         if (buf1[srcIdx] == '"')
  8383.             tmpbuf[dstIdx++] = '\\';
  8384.         tmpbuf[dstIdx] = buf1[srcIdx];
  8385.     }
  8386.     if (dstIdx != srcIdx) {
  8387.         memcpy(buf1, tmpbuf, dstIdx);
  8388.         buf1[dstIdx] ='\0';
  8389.     }
  8390.    
  8391.     for (srcIdx=0, dstIdx=0; buf2[srcIdx]; srcIdx++, dstIdx++) {
  8392.         if (buf2[srcIdx] == '"')
  8393.             tmpbuf[dstIdx++] = '\\';
  8394.         tmpbuf[dstIdx] = buf2[srcIdx];
  8395.     }
  8396.     if (dstIdx != srcIdx) {
  8397.         memcpy(buf2, tmpbuf, dstIdx);
  8398.         buf2[dstIdx] ='\0';
  8399.     }
  8400.    
  8401.     fd = fopen(option_file, "w");
  8402.     if (fd == NULL) {
  8403.         printf("open file %s error!\n", option_file);
  8404.         return -1;
  8405.     }
  8406.     sprintf(tmpbuf, "name \"%s\"\n", buf1);
  8407.     fputs(tmpbuf, fd);
  8408.     if(strlen(buf2)>31)
  8409.     {
  8410.       sprintf(tmpbuf, "-mschap\r\n");
  8411.         fputs(tmpbuf, fd);
  8412.         sprintf(tmpbuf, "-mschap-v2\r\n");
  8413.         fputs(tmpbuf, fd);     
  8414.     }
  8415.     fclose(fd);
  8416.    
  8417.     fd = fopen(pap_file, "w");
  8418.     if (fd == NULL) {
  8419.         printf("open file %s error!\n", pap_file);
  8420.         return -1;
  8421.     }
  8422.     fputs("#################################################\n", fd);
  8423.     sprintf(tmpbuf, "\"%s\" *   \"%s\"\n", buf1, buf2);
  8424.     fputs(tmpbuf, fd);
  8425.  
  8426.     fd = fopen(chap_file, "w");
  8427.     if (fd == NULL) {
  8428.         printf("open file %s error!\n", chap_file);
  8429.         return -1;
  8430.     }
  8431.     fputs("#################################################\n", fd);
  8432.     sprintf(tmpbuf, "\"%s\" *   \"%s\"\n", buf1, buf2);
  8433.     fputs(tmpbuf, fd);
  8434.     return 0;
  8435. }
  8436. #endif // HOME_GATEWAY
  8437.  
  8438. #ifdef WIFI_SIMPLE_CONFIG
  8439. enum {
  8440.     MODE_AP_UNCONFIG=1,             // AP unconfigured (enrollee)
  8441.     MODE_CLIENT_UNCONFIG=2,         // client unconfigured (enrollee)
  8442.     MODE_CLIENT_CONFIG=3,           // client configured (registrar)
  8443.     MODE_AP_PROXY=4,            // AP configured (proxy)
  8444.     MODE_AP_PROXY_REGISTRAR=5,      // AP configured (proxy and registrar)
  8445.     MODE_CLIENT_UNCONFIG_REGISTRAR=6        // client unconfigured (registrar)
  8446. };
  8447.  
  8448. #define WRITE_WSC_PARAM(dst, tmp, str, val) {   \
  8449.     sprintf(tmp, str, val); \
  8450.     memcpy(dst, tmp, strlen(tmp)); \
  8451.     dst += strlen(tmp); \
  8452. }
  8453.  
  8454. static void convert_bin_to_str(unsigned char *bin, int len, char *out)
  8455. {
  8456.     int i;
  8457.     char tmpbuf[10];
  8458.  
  8459.     out[0] = '\0';
  8460.  
  8461.     for (i=0; i<len; i++) {
  8462.         sprintf(tmpbuf, "%02x", bin[i]);
  8463.         strcat(out, tmpbuf);
  8464.     }
  8465. }
  8466.  
  8467. static void convert_hex_to_ascii(unsigned long code, char *out)
  8468. {
  8469.     *out++ = '0' + ((code / 10000000) % 10);  
  8470.     *out++ = '0' + ((code / 1000000) % 10);
  8471.     *out++ = '0' + ((code / 100000) % 10);
  8472.     *out++ = '0' + ((code / 10000) % 10);
  8473.     *out++ = '0' + ((code / 1000) % 10);
  8474.     *out++ = '0' + ((code / 100) % 10);
  8475.     *out++ = '0' + ((code / 10) % 10);
  8476.     *out++ = '0' + ((code / 1) % 10);
  8477.     *out = '\0';
  8478. }
  8479.  
  8480. static int compute_pin_checksum(unsigned long int PIN)
  8481. {
  8482.     unsigned long int accum = 0;
  8483.     int digit;
  8484.    
  8485.     PIN *= 10;
  8486.     accum += 3 * ((PIN / 10000000) % 10);  
  8487.     accum += 1 * ((PIN / 1000000) % 10);
  8488.     accum += 3 * ((PIN / 100000) % 10);
  8489.     accum += 1 * ((PIN / 10000) % 10);
  8490.     accum += 3 * ((PIN / 1000) % 10);
  8491.     accum += 1 * ((PIN / 100) % 10);
  8492.     accum += 3 * ((PIN / 10) % 10);
  8493.  
  8494.     digit = (accum % 10);
  8495.     return (10 - digit) % 10;
  8496. }
  8497.  
  8498. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  8499. static int defaultWscConf(char *in, char *out)
  8500. {
  8501.     int fh;
  8502.     struct stat status;
  8503.     char *buf, *ptr;
  8504.     int intVal, intVal2, is_client, is_registrar, len, is_wep=0, wep_key_type=0, wep_transmit_key=0;
  8505.     char tmpbuf[100], tmp1[100];
  8506.        
  8507.     if ( !apmib_init()) {
  8508.         printf("Initialize AP MIB failed!\n");
  8509.         return -1;
  8510.     }
  8511.  
  8512.         apmib_get(MIB_WLAN_MODE, (void *)&is_client);
  8513.     if (is_client == CLIENT_MODE)
  8514.                 return ;
  8515.  
  8516.     if (stat(in, &status) < 0) {
  8517.         printf("stat() error [%s]!\n", in);
  8518.         return -1;
  8519.     }
  8520.  
  8521.     buf = malloc(status.st_size+2048);
  8522.     if (buf == NULL) {
  8523.         printf("malloc() error [%d]!\n", (int)status.st_size+2048);
  8524.         return -1;     
  8525.     }
  8526.  
  8527.     ptr = buf;
  8528.  
  8529.     WRITE_WSC_PARAM(ptr, tmpbuf, "mode = %d\n", MODE_AP_UNCONFIG);
  8530.     WRITE_WSC_PARAM(ptr, tmpbuf, "upnp = 1\n", NULL);
  8531.     WRITE_WSC_PARAM(ptr, tmpbuf, "config_method = %d\n", CONFIG_METHOD_ETH | CONFIG_METHOD_PIN | CONFIG_METHOD_PBC);
  8532.     WRITE_WSC_PARAM(ptr, tmpbuf, "auth_type = 1\n", NULL);
  8533.     WRITE_WSC_PARAM(ptr, tmpbuf, "encrypt_type = 1\n", NULL);
  8534.     WRITE_WSC_PARAM(ptr, tmpbuf, "connection_type = 1\n", NULL);
  8535.     WRITE_WSC_PARAM(ptr, tmpbuf, "manual_config = 0\n", NULL);
  8536.     WRITE_WSC_PARAM(ptr, tmpbuf, "network_key = \n", NULL);
  8537.     WRITE_WSC_PARAM(ptr, tmpbuf, "ssid = \n", NULL);
  8538.     apmib_get(MIB_HW_WSC_PIN, (void *)&tmp1);  
  8539.     WRITE_WSC_PARAM(ptr, tmpbuf, "pin_code = %s\n", tmp1);
  8540.     WRITE_WSC_PARAM(ptr, tmpbuf, "rf_band = 1\n", NULL);
  8541.     WRITE_WSC_PARAM(ptr, tmpbuf, "device_name = \n", NULL);
  8542.  
  8543.     len = (int)(((long)ptr)-((long)buf));
  8544.    
  8545.     fh = open(in, O_RDONLY);
  8546.     if (fh == -1) {
  8547.         printf("open() error [%s]!\n", in);
  8548.         return -1;
  8549.     }
  8550.  
  8551.     lseek(fh, 0L, SEEK_SET);
  8552.     if (read(fh, ptr, status.st_size) != status.st_size) {     
  8553.         printf("read() error [%s]!\n", in);
  8554.         return -1; 
  8555.     }
  8556.     close(fh);
  8557.  
  8558.     // search UUID field, replace last 12 char with hw mac address
  8559.     ptr = strstr(ptr, "uuid =");
  8560.     if (ptr) {
  8561.         char tmp2[100];
  8562.         apmib_get(MIB_HW_NIC0_ADDR, (void *)&tmp1);
  8563.         convert_bin_to_str(tmp1, 6, tmp2);
  8564.         memcpy(ptr+27, tmp2, 12);
  8565.     }
  8566.  
  8567.     fh = open(out, O_RDWR|O_CREAT|O_TRUNC);
  8568.     if (fh == -1) {
  8569.         printf("open() error [%s]!\n", out);
  8570.         return -1;
  8571.     }
  8572.  
  8573.     if (write(fh, buf, len+status.st_size) != len+status.st_size ) {
  8574.         printf("Write() file error [%s]!\n", out);
  8575.         return -1;
  8576.     }
  8577.     close(fh);
  8578.     free(buf);
  8579.  
  8580.     return 0;
  8581. }
  8582. #endif
  8583.  
  8584. #if defined(UNIVERSAL_REPEATER) && defined(CONFIG_REPEATER_WPS_SUPPORT)
  8585. static int updateWscConf(char *in, char *out, int genpin, char *wlanif_name)
  8586. {
  8587.     int fh;
  8588.     struct stat status;
  8589.     char *buf, *ptr;
  8590.     int intVal;
  8591.     //int intVal2;
  8592.     int is_client, is_config, is_registrar, len, is_wep=0, wep_key_type=0, wep_transmit_key=0;
  8593.     char tmpbuf[100], tmp1[100];
  8594.    
  8595.     //int is_repeater_enabled=0;
  8596.     int isUpnpEnabled=0, wsc_method = 0, wsc_auth=0, wsc_enc=0;
  8597.     int wlan_network_type=0, wsc_manual_enabled=0, wlan_encrpty=0, wlan_wep=0;
  8598.     char wlan_wep64_key1[100], wlan_wep64_key2[100], wlan_wep64_key3[100], wlan_wep64_key4[100];
  8599.     char wlan_wep128_key1[100], wlan_wep128_key2[100], wlan_wep128_key3[100], wlan_wep128_key4[100];
  8600.     char wlan_wpa_psk[100];
  8601.     char wlan_ssid[100], device_name[100], wsc_pin[100];
  8602.     int wlan_chan_num=0, wsc_config_by_ext_reg=0;
  8603.     int is_vxdif=0;
  8604.  
  8605.     // 1104
  8606.     int wlan0_wsc_disabled=0;
  8607. #ifdef FOR_DUAL_BAND   
  8608.     int wlan1_wsc_disabled=0;
  8609. #endif
  8610.  
  8611.     // WPS2DOTX
  8612.     int wlan0_hidden_ssid=0;
  8613.     int wlan1_hidden_ssid=0;
  8614.     // WPS2DOTX
  8615.    
  8616.     int band_select_5g2g;  // 0:2.4g  ; 1:5G   ; 2:both
  8617.    
  8618.     //printf("\r\n wlanif_name=[%s],__[%s-%u]\r\n",wlanif_name,__FILE__,__LINE__); 
  8619.  
  8620.     if(wlanif_name != NULL)
  8621.     {
  8622.         if(strstr(wlanif_name,"-vxd") != 0)
  8623.             is_vxdif = 1;
  8624.     }
  8625.    
  8626.        
  8627.     if ( !apmib_init()) {
  8628.         printf("Initialize AP MIB failed!\n");
  8629.         return -1;
  8630.     }
  8631.  
  8632.     SetWlan_idx("wlan0");
  8633.     apmib_get(MIB_HW_WSC_PIN, (void *)wsc_pin);
  8634.  
  8635.     apmib_get(MIB_WLAN_WSC_REGISTRAR_ENABLED, (void *)&is_registrar);
  8636.     apmib_get(MIB_WLAN_WSC_UPNP_ENABLED, (void *)&isUpnpEnabled);
  8637.     apmib_get(MIB_WLAN_WSC_METHOD, (void *)&wsc_method);
  8638.  
  8639.     apmib_get(MIB_WLAN_NETWORK_TYPE, (void *)&wlan_network_type);
  8640.     apmib_get(MIB_WLAN_WSC_MANUAL_ENABLED, (void *)&wsc_manual_enabled);
  8641.     apmib_get(MIB_WLAN_CHANNEL, (void *)&wlan_chan_num);
  8642.     apmib_get(MIB_DEVICE_NAME, (void *)&device_name);
  8643.     apmib_get(MIB_DEVICE_NAME, (void *)&device_name);  
  8644.     // 1104
  8645.     apmib_get(MIB_WLAN_BAND2G5G_SELECT, (void *)&band_select_5g2g);
  8646.    
  8647.    
  8648.     if(wlanif_name != NULL)
  8649.     {
  8650.         SetWlan_idx(wlanif_name);
  8651.     }
  8652.     apmib_get(MIB_WLAN_WSC_AUTH, (void *)&wsc_auth);
  8653.     apmib_get(MIB_WLAN_WSC_ENC, (void *)&wsc_enc);
  8654.     apmib_get(MIB_WLAN_WSC_CONFIGURED, (void *)&is_config);
  8655.     apmib_get(MIB_WLAN_WSC_CONFIGBYEXTREG, (void *)&wsc_config_by_ext_reg);
  8656.     apmib_get(MIB_WLAN_SSID, (void *)&wlan_ssid);  
  8657.    
  8658.     apmib_get(MIB_WLAN_MODE, (void *)&is_client);  
  8659.     apmib_get(MIB_WLAN_ENCRYPT, (void *)&wlan_encrpty);
  8660.     apmib_get(MIB_WLAN_WEP, (void *)&wlan_wep);
  8661.     apmib_get(MIB_WLAN_WEP_KEY_TYPE, (void *)&wep_key_type);
  8662.     apmib_get(MIB_WLAN_WEP_DEFAULT_KEY, (void *)&wep_transmit_key);
  8663.     apmib_get(MIB_WLAN_WEP64_KEY1, (void *)&wlan_wep64_key1);
  8664.     apmib_get(MIB_WLAN_WEP64_KEY2, (void *)&wlan_wep64_key2);
  8665.     apmib_get(MIB_WLAN_WEP64_KEY3, (void *)&wlan_wep64_key3);
  8666.     apmib_get(MIB_WLAN_WEP64_KEY4, (void *)&wlan_wep64_key4);
  8667.     apmib_get(MIB_WLAN_WEP128_KEY1, (void *)&wlan_wep128_key1);
  8668.     apmib_get(MIB_WLAN_WEP128_KEY2, (void *)&wlan_wep128_key2);
  8669.     apmib_get(MIB_WLAN_WEP128_KEY3, (void *)&wlan_wep128_key3);
  8670.     apmib_get(MIB_WLAN_WEP128_KEY4, (void *)&wlan_wep128_key4);
  8671.     apmib_get(MIB_WLAN_WPA_PSK, (void *)&wlan_wpa_psk);
  8672.     apmib_get(MIB_WLAN_WSC_DISABLE, (void *)&wlan0_wsc_disabled);   // 1104
  8673.    
  8674.     /* WPS2DOTX ; 2011-0524*/  
  8675.     if(is_client == AP_MODE || is_client ==AP_WDS_MODE){
  8676.         apmib_get(MIB_WLAN_HIDDEN_SSID, (void *)&wlan0_hidden_ssid);       
  8677.         if(wlan0_hidden_ssid)
  8678.             wlan0_wsc_disabled=1 ; // if hidden ssid is enabled ; disabled WPS2
  8679.  
  8680.         if(wsc_auth==WSC_AUTH_WPAPSK){
  8681.             //printf("(%s %d)if WPA only ; disabled WPS2\n\n",__FUNCTION__,__LINE__);              
  8682.             wlan0_wsc_disabled=1 ;
  8683.         }
  8684.  
  8685.         if( wsc_enc==WSC_ENCRYPT_WEP || wsc_enc==WSC_ENCRYPT_TKIP){
  8686.             //printf("(%s %d)if WEP/TKIP only ; disabled WPS2\n\n",__FUNCTION__,__LINE__);             
  8687.             wlan0_wsc_disabled=1 ;
  8688.         }
  8689.     }
  8690.  
  8691.     /* WPS2DOTX ; 2011-0524*/  
  8692.  
  8693.        
  8694.     if (genpin || !memcmp(wsc_pin, "\x0\x0\x0\x0\x0\x0\x0\x0", PIN_LEN)) {
  8695.         #include <sys/time.h>          
  8696.         struct timeval tod;
  8697.         unsigned long num;
  8698.        
  8699.         gettimeofday(&tod , NULL);
  8700.  
  8701.         apmib_get(MIB_HW_NIC0_ADDR, (void *)&tmp1);        
  8702.         tod.tv_sec += tmp1[4]+tmp1[5];     
  8703.         srand(tod.tv_sec);
  8704.         num = rand() % 10000000;
  8705.         num = num*10 + compute_pin_checksum(num);
  8706.         convert_hex_to_ascii((unsigned long)num, wsc_pin);
  8707.  
  8708.         apmib_set(MIB_HW_WSC_PIN, (void *)wsc_pin);
  8709. #ifdef FOR_DUAL_BAND           // 2010-10-20
  8710.                wlan_idx=1;
  8711.                apmib_set(MIB_HW_WSC_PIN, (void *)wsc_pin);
  8712.                wlan_idx=0;
  8713. #endif     
  8714.         //apmib_update(CURRENT_SETTING);       
  8715.         apmib_update(HW_SETTING);      
  8716.  
  8717.         printf("Generated PIN = %s\n", wsc_pin);
  8718.  
  8719.         if (genpin)
  8720.             return 0;
  8721.     }
  8722.  
  8723.     if (stat(in, &status) < 0) {
  8724.         printf("stat() error [%s]!\n", in);
  8725.         return -1;
  8726.     }
  8727.  
  8728.     buf = malloc(status.st_size+2048);
  8729.     if (buf == NULL) {
  8730.         printf("malloc() error [%d]!\n", (int)status.st_size+2048);
  8731.         return -1;     
  8732.     }
  8733.  
  8734.     ptr = buf;
  8735.        
  8736.  
  8737.     if (is_client == CLIENT_MODE) {
  8738.         {
  8739.             if (is_registrar) {
  8740.                 if (!is_config)
  8741.                     intVal = MODE_CLIENT_UNCONFIG_REGISTRAR;
  8742.                 else
  8743.                     intVal = MODE_CLIENT_CONFIG;           
  8744.             }
  8745.             else
  8746.                 intVal = MODE_CLIENT_UNCONFIG;
  8747.         }
  8748.     }
  8749.     else {
  8750.         if (!is_config)
  8751.             intVal = MODE_AP_UNCONFIG;
  8752.         else
  8753.             intVal = MODE_AP_PROXY_REGISTRAR;
  8754.     }
  8755.     WRITE_WSC_PARAM(ptr, tmpbuf, "mode = %d\n", intVal);
  8756.  
  8757.    
  8758.     if (is_client == CLIENT_MODE) {
  8759. #ifdef CONFIG_RTL8186_KLD_REPEATER
  8760.         if (wps_vxdAP_enabled)
  8761.             apmib_get(MIB_WSC_UPNP_ENABLED, (void *)&intVal);
  8762.         else
  8763. #endif
  8764.             intVal = 0;
  8765.     }
  8766.     else
  8767.         intVal = isUpnpEnabled;
  8768.  
  8769.     WRITE_WSC_PARAM(ptr, tmpbuf, "upnp = %d\n", intVal);
  8770.  
  8771.     intVal = 0;
  8772.     intVal = wsc_method;
  8773.     //Ethernet(0x2)+Label(0x4)+PushButton(0x80) Bitwise OR
  8774.     if (intVal == 1) //Pin+Ethernet
  8775.         intVal = (CONFIG_METHOD_ETH | CONFIG_METHOD_PIN);
  8776.     else if (intVal == 2) //PBC+Ethernet
  8777.         intVal = (CONFIG_METHOD_ETH | CONFIG_METHOD_PBC);
  8778.     if (intVal == 3) //Pin+PBC+Ethernet
  8779.         intVal = (CONFIG_METHOD_ETH | CONFIG_METHOD_PIN | CONFIG_METHOD_PBC);
  8780.     WRITE_WSC_PARAM(ptr, tmpbuf, "config_method = %d\n", intVal);
  8781.  
  8782.     // 1104
  8783.     WRITE_WSC_PARAM(ptr, tmpbuf, "wlan0_wsc_disabled = %d\n", wlan0_wsc_disabled);
  8784.    
  8785.     WRITE_WSC_PARAM(ptr, tmpbuf, "auth_type = %d\n", wsc_auth);
  8786.    
  8787.     WRITE_WSC_PARAM(ptr, tmpbuf, "encrypt_type = %d\n", wsc_enc);
  8788.  
  8789.     if (wsc_enc == WSC_ENCRYPT_WEP)
  8790.         is_wep = 1;
  8791.  
  8792.     if((wlanif_name != NULL) && ((strcmp(wlanif_name,"wlan0") == 0) || (strcmp(wlanif_name,"wlan1") == 0)))
  8793.     {
  8794.         if (is_client == CLIENT_MODE)
  8795.         {
  8796.             if (wlan_network_type == 0)
  8797.                 intVal = 1;
  8798.             else
  8799.                 intVal = 2;
  8800.         }
  8801.         else
  8802.             intVal = 1;
  8803.     }
  8804.     else
  8805.         intVal = 1;
  8806.        
  8807.     WRITE_WSC_PARAM(ptr, tmpbuf, "connection_type = %d\n", intVal);
  8808.    
  8809.     WRITE_WSC_PARAM(ptr, tmpbuf, "manual_config = %d\n", wsc_manual_enabled);
  8810.  
  8811.  
  8812.     if (is_wep) { // only allow WEP in none-MANUAL mode (configured by external registrar)     
  8813.         if (wlan_encrpty != ENCRYPT_WEP) {
  8814.             printf("WEP mismatched between WPS and host system\n");
  8815.             free(buf);
  8816.             return -1;
  8817.         }      
  8818.         if (wlan_wep <= WEP_DISABLED || wlan_wep > WEP128) {
  8819.             printf("WEP encrypt length error\n");
  8820.             free(buf);
  8821.             return -1;
  8822.         }
  8823.        
  8824.         wep_transmit_key++;
  8825.         WRITE_WSC_PARAM(ptr, tmpbuf, "wep_transmit_key = %d\n", wep_transmit_key);
  8826.  
  8827.         /*whatever key type is ASSIC or HEX always use String-By-Hex fromat
  8828.         ;2011-0419,fixed,need patch with wscd daemon , search 2011-0419*/
  8829.         if (wlan_wep == WEP64) {
  8830.             sprintf(tmpbuf,"%s",wlan_wep64_key1);
  8831.             convert_bin_to_str((unsigned char *)tmpbuf, 5, tmp1);
  8832.             tmp1[10] = '\0';
  8833.             WRITE_WSC_PARAM(ptr, tmpbuf, "network_key = %s\n", tmp1);
  8834.  
  8835.             sprintf(tmpbuf,"%s",wlan_wep64_key2);
  8836.  
  8837.             convert_bin_to_str((unsigned char *)tmpbuf, 5, tmp1);
  8838.             tmp1[10] = '\0';
  8839.  
  8840.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key2 = %s\n", tmp1);
  8841.  
  8842.             sprintf(tmpbuf,"%s",wlan_wep64_key3);
  8843.  
  8844.             convert_bin_to_str((unsigned char *)tmpbuf, 5, tmp1);
  8845.             tmp1[10] = '\0';
  8846.  
  8847.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key3 = %s\n", tmp1);
  8848.  
  8849.  
  8850.             sprintf(tmpbuf,"%s",wlan_wep64_key4);
  8851.  
  8852.             convert_bin_to_str((unsigned char *)tmpbuf, 5, tmp1);
  8853.             tmp1[10] = '\0';
  8854.            
  8855.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key4 = %s\n", tmp1);
  8856.         }
  8857.         else {
  8858.             sprintf(tmpbuf,"%s",wlan_wep128_key1);
  8859.  
  8860.             convert_bin_to_str((unsigned char *)tmpbuf, 13, tmp1);
  8861.             tmp1[26] = '\0';
  8862.  
  8863.  
  8864.             WRITE_WSC_PARAM(ptr, tmpbuf, "network_key = %s\n", tmp1);
  8865.  
  8866.             sprintf(tmpbuf,"%s",wlan_wep128_key2);
  8867.  
  8868.             convert_bin_to_str((unsigned char *)tmpbuf, 13, tmp1);
  8869.             tmp1[26] = '\0';
  8870.  
  8871.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key2 = %s\n", tmp1);
  8872.  
  8873.             sprintf(tmpbuf,"%s",wlan_wep128_key3);
  8874.  
  8875.             convert_bin_to_str((unsigned char *)tmpbuf, 13, tmp1);
  8876.             tmp1[26] = '\0';
  8877.  
  8878.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key3 = %s\n", tmp1);
  8879.  
  8880.             sprintf(tmpbuf,"%s",wlan_wep128_key3);
  8881.  
  8882.             convert_bin_to_str((unsigned char *)tmpbuf, 13, tmp1);
  8883.             tmp1[26] = '\0';
  8884.  
  8885.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key4 = %s\n", tmp1);
  8886.         }
  8887.     }
  8888.     else {
  8889.         WRITE_WSC_PARAM(ptr, tmpbuf, "network_key = %s\n", wlan_wpa_psk);      
  8890.     }
  8891.  
  8892.     WRITE_WSC_PARAM(ptr, tmpbuf, "ssid = \"%s\"\n", wlan_ssid);
  8893.  
  8894.     WRITE_WSC_PARAM(ptr, tmpbuf, "pin_code = %s\n", wsc_pin);
  8895.    
  8896.  
  8897.     if (wlan_chan_num > 14)
  8898.         intVal = 2;
  8899.     else
  8900.         intVal = 1;
  8901.     WRITE_WSC_PARAM(ptr, tmpbuf, "rf_band = %d\n", intVal);
  8902.  
  8903. #ifdef FOR_DUAL_BAND
  8904.  
  8905.     /* switch to wlan1 */
  8906.     wlan_idx = 1;
  8907.     vwlan_idx = 0;
  8908.  
  8909.  
  8910.     apmib_get(MIB_WLAN_WSC_AUTH, (void *)&wsc_auth);
  8911.     apmib_get(MIB_WLAN_WSC_ENC, (void *)&wsc_enc);
  8912.     apmib_get(MIB_WLAN_SSID, (void *)&wlan_ssid);  
  8913.     apmib_get(MIB_WLAN_MODE, (void *)&is_client);  
  8914.     apmib_get(MIB_WLAN_ENCRYPT, (void *)&wlan_encrpty);
  8915.     apmib_get(MIB_WLAN_WEP, (void *)&wlan_wep);
  8916.     apmib_get(MIB_WLAN_WEP_KEY_TYPE, (void *)&wep_key_type);
  8917.     apmib_get(MIB_WLAN_WEP_DEFAULT_KEY, (void *)&wep_transmit_key);
  8918.     apmib_get(MIB_WLAN_WEP64_KEY1, (void *)&wlan_wep64_key1);
  8919.     apmib_get(MIB_WLAN_WEP64_KEY2, (void *)&wlan_wep64_key2);
  8920.     apmib_get(MIB_WLAN_WEP64_KEY3, (void *)&wlan_wep64_key3);
  8921.     apmib_get(MIB_WLAN_WEP64_KEY4, (void *)&wlan_wep64_key4);
  8922.     apmib_get(MIB_WLAN_WEP128_KEY1, (void *)&wlan_wep128_key1);
  8923.     apmib_get(MIB_WLAN_WEP128_KEY2, (void *)&wlan_wep128_key2);
  8924.     apmib_get(MIB_WLAN_WEP128_KEY3, (void *)&wlan_wep128_key3);
  8925.     apmib_get(MIB_WLAN_WEP128_KEY4, (void *)&wlan_wep128_key4);
  8926.     apmib_get(MIB_WLAN_WPA_PSK, (void *)&wlan_wpa_psk);
  8927.     apmib_get(MIB_WLAN_WSC_DISABLE, (void *)&wlan1_wsc_disabled);   // 1104
  8928.    
  8929.     /* WPS2DOTX ; 2011-0524*/  
  8930.     if(is_client == AP_MODE || is_client ==AP_WDS_MODE){
  8931.  
  8932.         apmib_get(MIB_WLAN_HIDDEN_SSID, (void *)&wlan1_hidden_ssid);
  8933.         if(wlan1_hidden_ssid)
  8934.             wlan1_wsc_disabled = 1 ; // if hidden ssid is enabled ; disabled WPS2
  8935.  
  8936.         if(wsc_auth==WSC_AUTH_WPAPSK){
  8937.             //printf("(%s %d)if WPA only ; disabled WPS2\n\n",__FUNCTION__,__LINE__);              
  8938.             wlan1_wsc_disabled=1 ;
  8939.         }
  8940.  
  8941.         if( wsc_enc==WSC_ENCRYPT_WEP || wsc_enc==WSC_ENCRYPT_TKIP){
  8942.             //printf("(%s %d)if WEP/TKIP only ; disabled WPS2\n\n",__FUNCTION__,__LINE__);             
  8943.             wlan1_wsc_disabled=1 ;
  8944.         }
  8945.     }
  8946.  
  8947.     /* WPS2DOTX ; 2011-0524*/  
  8948.    
  8949.     WRITE_WSC_PARAM(ptr, tmpbuf, "#=====wlan1 start==========%d\n",intVal);
  8950.     WRITE_WSC_PARAM(ptr, tmpbuf, "ssid2 = \"%s\"\n",wlan_ssid );   
  8951.     WRITE_WSC_PARAM(ptr, tmpbuf, "auth_type2 = %d\n", wsc_auth);
  8952.     WRITE_WSC_PARAM(ptr, tmpbuf, "encrypt_type2 = %d\n", wsc_enc);
  8953.  
  8954.     // 1104
  8955.     if(band_select_5g2g!=2)     //  != dual band
  8956.     {
  8957.         intVal=1;
  8958.         WRITE_WSC_PARAM(ptr, tmpbuf, "wlan1_wsc_disabled = %d\n",intVal);
  8959.     }
  8960.     else    // else see if wlan1 is enabled
  8961.     {
  8962.         WRITE_WSC_PARAM(ptr, tmpbuf, "wlan1_wsc_disabled = %d\n", wlan1_wsc_disabled);     
  8963.     }
  8964.  
  8965.     is_wep = 0;
  8966.    
  8967.     if (wsc_enc == WSC_ENCRYPT_WEP)
  8968.         is_wep = 1;
  8969.  
  8970.     if (is_wep) { // only allow WEP in none-MANUAL mode (configured by external registrar)     
  8971.         if (wlan_encrpty != ENCRYPT_WEP) {
  8972.             printf("WEP mismatched between WPS and host system\n");
  8973.             free(buf);
  8974.             return -1;
  8975.         }      
  8976.         if (wlan_wep <= WEP_DISABLED || wlan_wep > WEP128) {
  8977.             printf("WEP encrypt length error\n");
  8978.             free(buf);
  8979.             return -1;
  8980.         }
  8981.        
  8982.         wep_transmit_key++;
  8983.         WRITE_WSC_PARAM(ptr, tmpbuf, "wep_transmit_key2 = %d\n", wep_transmit_key);
  8984.  
  8985.  
  8986.         /*whatever key type is ASSIC or HEX always use String-By-Hex fromat
  8987.         ;2011-0419,fixed,need patch with wscd daemon , search 2011-0419*/
  8988.         if (wlan_wep == WEP64) {
  8989.            
  8990.             sprintf(tmpbuf,"%s",wlan_wep64_key1);
  8991.                
  8992.             convert_bin_to_str(tmpbuf, 5, tmp1);
  8993.             tmp1[10] = '\0';
  8994.                
  8995.            
  8996.             WRITE_WSC_PARAM(ptr, tmpbuf, "network_key2 = %s\n", tmp1);
  8997.  
  8998.             sprintf(tmpbuf,"%s",wlan_wep64_key2);
  8999.                
  9000.             convert_bin_to_str(tmpbuf, 5, tmp1);
  9001.             tmp1[10] = '\0';
  9002.                
  9003.            
  9004.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key22 = %s\n", tmp1);
  9005.  
  9006.             sprintf(tmpbuf,"%s",wlan_wep64_key3);
  9007.                
  9008.             convert_bin_to_str(tmpbuf, 5, tmp1);
  9009.             tmp1[10] = '\0';
  9010.                
  9011.            
  9012.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key32 = %s\n", tmp1);
  9013.  
  9014.  
  9015.             sprintf(tmpbuf,"%s",wlan_wep64_key4);
  9016.                
  9017.             convert_bin_to_str(tmpbuf, 5, tmp1);
  9018.             tmp1[10] = '\0';
  9019.                
  9020.            
  9021.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key42 = %s\n", tmp1);
  9022.         }
  9023.         else {
  9024.             sprintf(tmpbuf,"%s",wlan_wep128_key1);
  9025.  
  9026.             convert_bin_to_str(tmpbuf, 13, tmp1);
  9027.             tmp1[26] = '\0';
  9028.  
  9029.  
  9030.             WRITE_WSC_PARAM(ptr, tmpbuf, "network_key2 = %s\n", tmp1);
  9031.  
  9032.             sprintf(tmpbuf,"%s",wlan_wep128_key2);
  9033.  
  9034.             convert_bin_to_str(tmpbuf, 13, tmp1);
  9035.             tmp1[26] = '\0';
  9036.  
  9037.  
  9038.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key22 = %s\n", tmp1);
  9039.  
  9040.             sprintf(tmpbuf,"%s",wlan_wep128_key3);
  9041.  
  9042.             convert_bin_to_str(tmpbuf, 13, tmp1);
  9043.             tmp1[26] = '\0';
  9044.  
  9045.  
  9046.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key32 = %s\n", tmp1);
  9047.  
  9048.             sprintf(tmpbuf,"%s",wlan_wep128_key3);
  9049.  
  9050.             convert_bin_to_str(tmpbuf, 13, tmp1);
  9051.             tmp1[26] = '\0';
  9052.  
  9053.  
  9054.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key42 = %s\n", tmp1);
  9055.         }
  9056.     }
  9057.     else {
  9058.         WRITE_WSC_PARAM(ptr, tmpbuf, "network_key2 = %s\n", wlan_wpa_psk);     
  9059.     }
  9060.  
  9061.  
  9062.  
  9063.     intVal =2 ;
  9064.     WRITE_WSC_PARAM(ptr, tmpbuf, "#=====wlan1 end==========%d\n",intVal);
  9065.  
  9066.     /*sync the PIN code of wlan0 and wlan1*/
  9067.     apmib_set(MIB_HW_WSC_PIN, (void *)wsc_pin);
  9068.  
  9069.  
  9070.     /* switch back to wlan0 */
  9071.     wlan_idx = 0;
  9072.     vwlan_idx = 0;
  9073.  
  9074.    
  9075. #endif  // END of FOR_DUAL_BAND
  9076.     WRITE_WSC_PARAM(ptr, tmpbuf, "device_name = \"%s\"\n", device_name);
  9077.    
  9078.     WRITE_WSC_PARAM(ptr, tmpbuf, "config_by_ext_reg = %d\n", wsc_config_by_ext_reg);
  9079.  
  9080.     len = (int)(((long)ptr)-((long)buf));
  9081.    
  9082.     fh = open(in, O_RDONLY);
  9083.     if (fh == -1) {
  9084.         printf("open() error [%s]!\n", in);
  9085.         return -1;
  9086.     }
  9087.  
  9088.     lseek(fh, 0L, SEEK_SET);
  9089.     if (read(fh, ptr, status.st_size) != status.st_size) {     
  9090.         printf("read() error [%s]!\n", in);
  9091.         return -1; 
  9092.     }
  9093.     close(fh);
  9094.  
  9095.     // search UUID field, replace last 12 char with hw mac address
  9096.     ptr = strstr(ptr, "uuid =");
  9097.     if (ptr) {
  9098.         char tmp2[100];
  9099.         apmib_get(MIB_HW_NIC0_ADDR, (void *)&tmp1);
  9100.         convert_bin_to_str((unsigned char *)tmp1, 6, tmp2);
  9101.         memcpy(ptr+27, tmp2, 12);      
  9102.     }
  9103.  
  9104.     fh = open(out, O_RDWR|O_CREAT|O_TRUNC);
  9105.     if (fh == -1) {
  9106.         printf("open() error [%s]!\n", out);
  9107.         return -1;
  9108.     }
  9109.  
  9110.     if (write(fh, buf, len+status.st_size) != len+status.st_size ) {
  9111.         printf("Write() file error [%s]!\n", out);
  9112.         return -1;
  9113.     }
  9114.     close(fh);
  9115.     free(buf);
  9116.  
  9117.  
  9118.    
  9119.     return 0;
  9120. }
  9121. #else //#if defined(UNIVERSAL_REPEATER) && defined(CONFIG_REPEATER_WPS_SUPPORT)
  9122. static int updateWscConf(char *in, char *out, int genpin)
  9123. {
  9124.     int fh;
  9125.     struct stat status;
  9126.     char *buf, *ptr;
  9127.     int intVal, intVal2, is_client, is_config, is_registrar, len, is_wep=0, wep_key_type=0, wep_transmit_key=0;
  9128.     char tmpbuf[100], tmp1[100];
  9129.     char wsc_pin[100]; 
  9130.  
  9131.        
  9132.     if ( !apmib_init()) {
  9133.         printf("Initialize AP MIB failed!\n");
  9134.         return -1;
  9135.     }
  9136.  
  9137.     apmib_get(MIB_HW_WSC_PIN, (void *)wsc_pin);
  9138.     if (genpin || !memcmp(tmpbuf, "\x0\x0\x0\x0\x0\x0\x0\x0", PIN_LEN)) {
  9139.         #include <sys/time.h>          
  9140.         struct timeval tod;
  9141.         unsigned long num;
  9142.        
  9143.         gettimeofday(&tod , NULL);
  9144.  
  9145.         apmib_get(MIB_HW_NIC0_ADDR, (void *)&tmp1);        
  9146.         tod.tv_sec += tmp1[4]+tmp1[5];     
  9147.         srand(tod.tv_sec);
  9148.         num = rand() % 10000000;
  9149.         num = num*10 + compute_pin_checksum(num);
  9150.         convert_hex_to_ascii((unsigned long)num, wsc_pin);
  9151.  
  9152.         apmib_set(MIB_HW_WSC_PIN, (void *)wsc_pin);
  9153.         //apmib_update(CURRENT_SETTING);       
  9154.         apmib_update(HW_SETTING);      
  9155.  
  9156.         printf("Generated PIN = %s\n", tmpbuf);
  9157.  
  9158.         if (genpin)
  9159.             return 0;
  9160.     }
  9161.  
  9162.     if (stat(in, &status) < 0) {
  9163.         printf("stat() error [%s]!\n", in);
  9164.         return -1;
  9165.     }
  9166.  
  9167.     buf = malloc(status.st_size+2048);
  9168.     if (buf == NULL) {
  9169.         printf("malloc() error [%d]!\n", (int)status.st_size+2048);
  9170.         return -1;     
  9171.     }
  9172.  
  9173.     ptr = buf;
  9174.     apmib_get(MIB_WLAN_MODE, (void *)&is_client);
  9175.     apmib_get(MIB_WLAN_WSC_CONFIGURED, (void *)&is_config);
  9176.     apmib_get(MIB_WLAN_WSC_REGISTRAR_ENABLED, (void *)&is_registrar);  
  9177. #ifdef CONFIG_RTL8186_KLD_REPEATER
  9178.     int is_repeater_enabled;
  9179.     int wps_vxdAP_enabled=0;
  9180.     apmib_get(MIB_REPEATER_ENABLED1, (void *)&is_repeater_enabled);
  9181. #endif
  9182.     if (is_client == CLIENT_MODE) {
  9183. #ifdef CONFIG_RTL8186_KLD_REPEATER
  9184.         if (is_repeater_enabled && is_config) {
  9185.             intVal = MODE_AP_PROXY_REGISTRAR;
  9186.             wps_vxdAP_enabled = 1;
  9187.             WRITE_WSC_PARAM(ptr, tmpbuf, "disable_configured_by_exReg = %d\n", 1);
  9188.         }
  9189.         else
  9190. #endif
  9191.         {
  9192.             if (is_registrar) {
  9193.                 if (!is_config)
  9194.                     intVal = MODE_CLIENT_UNCONFIG_REGISTRAR;
  9195.                 else
  9196.                     intVal = MODE_CLIENT_CONFIG;           
  9197.             }
  9198.             else
  9199.                 intVal = MODE_CLIENT_UNCONFIG;
  9200.         }
  9201.     }
  9202.     else {
  9203.         if (!is_config)
  9204.             intVal = MODE_AP_UNCONFIG;
  9205.         else
  9206.             intVal = MODE_AP_PROXY_REGISTRAR;
  9207.     }
  9208.     WRITE_WSC_PARAM(ptr, tmpbuf, "mode = %d\n", intVal);
  9209.    
  9210.     if (is_client == CLIENT_MODE) {
  9211. #ifdef CONFIG_RTL8186_KLD_REPEATER
  9212.         if (wps_vxdAP_enabled)
  9213.             apmib_get(MIB_WLAN_WSC_UPNP_ENABLED, (void *)&intVal);
  9214.         else
  9215. #endif
  9216.             intVal = 0;
  9217.     }
  9218.     else
  9219.         apmib_get(MIB_WLAN_WSC_UPNP_ENABLED, (void *)&intVal);
  9220. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  9221.     WRITE_WSC_PARAM(ptr, tmpbuf, "wlan0_wsc_upnp_enable = %d\n", intVal);
  9222. #else  
  9223.         WRITE_WSC_PARAM(ptr, tmpbuf, "upnp = %d\n", intVal);
  9224. #endif
  9225.  
  9226.     intVal = 0;
  9227.     apmib_get(MIB_WLAN_WSC_METHOD, (void *)&intVal);
  9228.     //Ethernet(0x2)+Label(0x4)+PushButton(0x80) Bitwise OR
  9229.     if (intVal == 1) //Pin+Ethernet
  9230.         intVal = (CONFIG_METHOD_ETH | CONFIG_METHOD_PIN);
  9231.     else if (intVal == 2) //PBC+Ethernet
  9232.         intVal = (CONFIG_METHOD_ETH | CONFIG_METHOD_PBC);
  9233.     if (intVal == 3) //Pin+PBC+Ethernet
  9234.         intVal = (CONFIG_METHOD_ETH | CONFIG_METHOD_PIN | CONFIG_METHOD_PBC);
  9235.     WRITE_WSC_PARAM(ptr, tmpbuf, "config_method = %d\n", intVal);
  9236.  
  9237.     apmib_get(MIB_WLAN_WSC_AUTH, (void *)&intVal2);
  9238. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  9239.     WRITE_WSC_PARAM(ptr, tmpbuf, "wlan0_wsc_auth = %d\n", intVal2);
  9240. #else
  9241.     WRITE_WSC_PARAM(ptr, tmpbuf, "auth_type = %d\n", intVal2);
  9242. #endif
  9243.  
  9244.     apmib_get(MIB_WLAN_WSC_ENC, (void *)&intVal);
  9245. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  9246.     WRITE_WSC_PARAM(ptr, tmpbuf, "wlan0_wsc_enc = %d\n", intVal);
  9247. #else
  9248.     WRITE_WSC_PARAM(ptr, tmpbuf, "encrypt_type = %d\n", intVal);
  9249. #endif
  9250.     if (intVal == WSC_ENCRYPT_WEP)
  9251.         is_wep = 1;
  9252.  
  9253.     if (is_client == CLIENT_MODE) {
  9254. #ifdef CONFIG_RTL8186_KLD_REPEATER
  9255.         if (wps_vxdAP_enabled)
  9256.             intVal = 1;
  9257.         else
  9258. #endif
  9259.         {
  9260.             apmib_get(MIB_WLAN_NETWORK_TYPE, (void *)&intVal);
  9261.             if (intVal == 0)
  9262.                 intVal = 1;
  9263.             else
  9264.                 intVal = 2;
  9265.         }
  9266.     }
  9267.     else
  9268.         intVal = 1;
  9269.  
  9270. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  9271.     WRITE_WSC_PARAM(ptr, tmpbuf, "wsc_connection_type = %d\n", intVal);
  9272. #else
  9273.     WRITE_WSC_PARAM(ptr, tmpbuf, "connection_type = %d\n", intVal);
  9274. #endif
  9275.  
  9276.     apmib_get(MIB_WLAN_WSC_MANUAL_ENABLED, (void *)&intVal);
  9277.  
  9278. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  9279.     WRITE_WSC_PARAM(ptr, tmpbuf, "wsc_manual_enabled = %d\n", intVal);
  9280. #else
  9281.     WRITE_WSC_PARAM(ptr, tmpbuf, "manual_config = %d\n", intVal);
  9282. #endif
  9283.  
  9284.     if (is_wep) { // only allow WEP in none-MANUAL mode (configured by external registrar)
  9285.         apmib_get(MIB_WLAN_ENCRYPT, (void *)&intVal);
  9286.         if (intVal != ENCRYPT_WEP) {
  9287.             printf("WEP mismatched between WPS and host system\n");
  9288.             free(buf);
  9289.             return -1;
  9290.         }
  9291.         apmib_get(MIB_WLAN_WEP, (void *)&intVal);
  9292.         if (intVal <= WEP_DISABLED || intVal > WEP128) {
  9293.             printf("WEP encrypt length error\n");
  9294.             free(buf);
  9295.             return -1;
  9296.         }
  9297.         apmib_get(MIB_WLAN_WEP_KEY_TYPE, (void *)&wep_key_type);
  9298.         apmib_get(MIB_WLAN_WEP_DEFAULT_KEY, (void *)&wep_transmit_key);
  9299.         wep_transmit_key++;
  9300.         WRITE_WSC_PARAM(ptr, tmpbuf, "wep_transmit_key = %d\n", wep_transmit_key);
  9301.         if (intVal == WEP64) {
  9302.             apmib_get(MIB_WLAN_WEP64_KEY1, (void *)&tmpbuf);
  9303.             if (wep_key_type == KEY_ASCII) {
  9304.                 memcpy(tmp1, tmpbuf, 5);
  9305.                 tmp1[5] = '\0';
  9306.             }
  9307.             else {
  9308.                 convert_bin_to_str(tmpbuf, 5, tmp1);
  9309.                 tmp1[10] = '\0';
  9310.             }          
  9311. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  9312.             WRITE_WSC_PARAM(ptr, tmpbuf, "wlan0_network_key = %s\n", tmp1);
  9313. #else
  9314.             WRITE_WSC_PARAM(ptr, tmpbuf, "network_key = %s\n", tmp1);
  9315. #endif
  9316.  
  9317.             apmib_get(MIB_WLAN_WEP64_KEY2, (void *)&tmpbuf);
  9318.             if (wep_key_type == KEY_ASCII) {
  9319.                 memcpy(tmp1, tmpbuf, 5);
  9320.                 tmp1[5] = '\0';
  9321.             }
  9322.             else {
  9323.                 convert_bin_to_str(tmpbuf, 5, tmp1);
  9324.                 tmp1[10] = '\0';
  9325.             }          
  9326.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key2 = %s\n", tmp1);
  9327.  
  9328.             apmib_get(MIB_WLAN_WEP64_KEY3, (void *)&tmpbuf);
  9329.             if (wep_key_type == KEY_ASCII) {
  9330.                 memcpy(tmp1, tmpbuf, 5);
  9331.                 tmp1[5] = '\0';
  9332.             }
  9333.             else {
  9334.                 convert_bin_to_str(tmpbuf, 5, tmp1);
  9335.                 tmp1[10] = '\0';
  9336.             }          
  9337.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key3 = %s\n", tmp1);
  9338.  
  9339.  
  9340.             apmib_get(MIB_WLAN_WEP64_KEY4, (void *)&tmpbuf);
  9341.             if (wep_key_type == KEY_ASCII) {
  9342.                 memcpy(tmp1, tmpbuf, 5);
  9343.                 tmp1[5] = '\0';
  9344.             }
  9345.             else {
  9346.                 convert_bin_to_str(tmpbuf, 5, tmp1);
  9347.                 tmp1[10] = '\0';
  9348.             }          
  9349.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key4 = %s\n", tmp1);
  9350.         }
  9351.         else {
  9352.             apmib_get(MIB_WLAN_WEP128_KEY1, (void *)&tmpbuf);
  9353.             if (wep_key_type == KEY_ASCII) {
  9354.                 memcpy(tmp1, tmpbuf, 13);
  9355.                 tmp1[13] = '\0';
  9356.             }
  9357.             else {
  9358.                 convert_bin_to_str(tmpbuf, 13, tmp1);
  9359.                 tmp1[26] = '\0';
  9360.             }
  9361. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  9362.             WRITE_WSC_PARAM(ptr, tmpbuf, "wlan0_network_key = %s\n", tmp1);
  9363. #else
  9364.             WRITE_WSC_PARAM(ptr, tmpbuf, "network_key = %s\n", tmp1);
  9365. #endif
  9366.  
  9367.             apmib_get(MIB_WLAN_WEP128_KEY2, (void *)&tmpbuf);
  9368.             if (wep_key_type == KEY_ASCII) {
  9369.                 memcpy(tmp1, tmpbuf, 13);
  9370.                 tmp1[13] = '\0';
  9371.             }
  9372.             else {
  9373.                 convert_bin_to_str(tmpbuf, 13, tmp1);
  9374.                 tmp1[26] = '\0';
  9375.             }
  9376.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key2 = %s\n", tmp1);
  9377.  
  9378.             apmib_get(MIB_WLAN_WEP128_KEY3, (void *)&tmpbuf);
  9379.             if (wep_key_type == KEY_ASCII) {
  9380.                 memcpy(tmp1, tmpbuf, 13);
  9381.                 tmp1[13] = '\0';
  9382.             }
  9383.             else {
  9384.                 convert_bin_to_str(tmpbuf, 13, tmp1);
  9385.                 tmp1[26] = '\0';
  9386.             }
  9387.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key3 = %s\n", tmp1);
  9388.  
  9389.             apmib_get(MIB_WLAN_WEP128_KEY4, (void *)&tmpbuf);
  9390.             if (wep_key_type == KEY_ASCII) {
  9391.                 memcpy(tmp1, tmpbuf, 13);
  9392.                 tmp1[13] = '\0';
  9393.             }
  9394.             else {
  9395.                 convert_bin_to_str(tmpbuf, 13, tmp1);
  9396.                 tmp1[26] = '\0';
  9397.             }
  9398.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key4 = %s\n", tmp1);
  9399.         }
  9400.     }
  9401.     else {
  9402.         apmib_get(MIB_WLAN_WPA_PSK, (void *)&tmp1);    
  9403.        
  9404. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  9405.         WRITE_WSC_PARAM(ptr, tmpbuf, "wlan0_network_key = %s\n", tmp1);
  9406. #else
  9407.         WRITE_WSC_PARAM(ptr, tmpbuf, "network_key = %s\n", tmp1);
  9408. #endif
  9409.        
  9410.     }
  9411.  
  9412. #ifdef CONFIG_RTL8186_KLD_REPEATER
  9413.     if (wps_vxdAP_enabled)
  9414.         apmib_get(MIB_REPEATER_SSID1, (void *)&tmp1);  
  9415.     else
  9416. #endif
  9417.         apmib_get(MIB_WLAN_SSID, (void *)&tmp1);   
  9418.  
  9419. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  9420.         WRITE_WSC_PARAM(ptr, tmpbuf, "wlan0_ssid = %s\n", tmp1);   
  9421. #else
  9422.     WRITE_WSC_PARAM(ptr, tmpbuf, "ssid = %s\n", tmp1); 
  9423. #endif
  9424.  
  9425. #if 0  
  9426. //  }
  9427. //  else {         
  9428.         apmib_get(MIB_WLAN_WSC_PSK, (void *)&tmp1);
  9429.         WRITE_WSC_PARAM(ptr, tmpbuf, "network_key = %s\n", tmp1);      
  9430.        
  9431.         apmib_get(MIB_WLAN_WSC_SSID, (void *)&tmp1);
  9432.         WRITE_WSC_PARAM(ptr, tmpbuf, "ssid = %s\n", tmp1);
  9433. //  }
  9434. #endif
  9435.  
  9436.     apmib_get(MIB_HW_WSC_PIN, (void *)&tmp1);
  9437.    
  9438. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  9439.     WRITE_WSC_PARAM(ptr, tmpbuf, "wsc_pin_code = %s\n", tmp1);
  9440. #else
  9441.     WRITE_WSC_PARAM(ptr, tmpbuf, "pin_code = %s\n", tmp1);
  9442. #endif
  9443.    
  9444.  
  9445.     apmib_get(MIB_WLAN_CHANNEL, (void *)&intVal);
  9446.     if (intVal > 14)
  9447.         intVal = 2;
  9448.     else
  9449.         intVal = 1;
  9450.     WRITE_WSC_PARAM(ptr, tmpbuf, "rf_band = %d\n", intVal);
  9451.  
  9452. #ifdef FOR_DUAL_BAND
  9453.  
  9454.  
  9455.     /*switch to  wlan1 , get mib from wlan1*/
  9456.  
  9457.     wlan_idx = 1;
  9458.     vwlan_idx = 0;
  9459.    
  9460.     WRITE_WSC_PARAM(ptr, tmpbuf, "#=====wlan1 start==========%d\n",intVal);
  9461.     apmib_get(MIB_WLAN_WSC_AUTH, (void *)&intVal2);
  9462.     WRITE_WSC_PARAM(ptr, tmpbuf, "auth_type2 = %d\n", intVal2);
  9463.  
  9464.     apmib_get(MIB_WLAN_WSC_ENC, (void *)&intVal);
  9465.     WRITE_WSC_PARAM(ptr, tmpbuf, "encrypt_type2 = %d\n", intVal);
  9466.  
  9467.     if (intVal == WSC_ENCRYPT_WEP)
  9468.         is_wep = 1;
  9469.  
  9470.  
  9471.  
  9472.  
  9473.     if (is_wep) { // only allow WEP in none-MANUAL mode (configured by external registrar)
  9474.         apmib_get(MIB_WLAN_ENCRYPT, (void *)&intVal);
  9475.         if (intVal != ENCRYPT_WEP) {
  9476.             printf("WEP mismatched between WPS and host system\n");
  9477.             free(buf);
  9478.             return -1;
  9479.         }
  9480.         apmib_get(MIB_WLAN_WEP, (void *)&intVal);
  9481.         if (intVal <= WEP_DISABLED || intVal > WEP128) {
  9482.             printf("WEP encrypt length error\n");
  9483.             free(buf);
  9484.             return -1;
  9485.         }
  9486.         apmib_get(MIB_WLAN_WEP_KEY_TYPE, (void *)&wep_key_type);
  9487.         apmib_get(MIB_WLAN_WEP_DEFAULT_KEY, (void *)&wep_transmit_key);
  9488.         wep_transmit_key++;
  9489.         WRITE_WSC_PARAM(ptr, tmpbuf, "wep_transmit_key2 = %d\n", wep_transmit_key);
  9490.         if (intVal == WEP64) {
  9491.             apmib_get(MIB_WLAN_WEP64_KEY1, (void *)&tmpbuf);
  9492.             if (wep_key_type == KEY_ASCII) {
  9493.                 memcpy(tmp1, tmpbuf, 5);
  9494.                 tmp1[5] = '\0';
  9495.             }
  9496.             else {
  9497.                 convert_bin_to_str(tmpbuf, 5, tmp1);
  9498.                 tmp1[10] = '\0';
  9499.             }          
  9500.             WRITE_WSC_PARAM(ptr, tmpbuf, "network_key2 = %s\n", tmp1);
  9501.  
  9502.             apmib_get(MIB_WLAN_WEP64_KEY2, (void *)&tmpbuf);
  9503.             if (wep_key_type == KEY_ASCII) {
  9504.                 memcpy(tmp1, tmpbuf, 5);
  9505.                 tmp1[5] = '\0';
  9506.             }
  9507.             else {
  9508.                 convert_bin_to_str(tmpbuf, 5, tmp1);
  9509.                 tmp1[10] = '\0';
  9510.             }          
  9511.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key22 = %s\n", tmp1);
  9512.  
  9513.             apmib_get(MIB_WLAN_WEP64_KEY3, (void *)&tmpbuf);
  9514.             if (wep_key_type == KEY_ASCII) {
  9515.                 memcpy(tmp1, tmpbuf, 5);
  9516.                 tmp1[5] = '\0';
  9517.             }
  9518.             else {
  9519.                 convert_bin_to_str(tmpbuf, 5, tmp1);
  9520.                 tmp1[10] = '\0';
  9521.             }          
  9522.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key32 = %s\n", tmp1);
  9523.  
  9524.  
  9525.             apmib_get(MIB_WLAN_WEP64_KEY4, (void *)&tmpbuf);
  9526.             if (wep_key_type == KEY_ASCII) {
  9527.                 memcpy(tmp1, tmpbuf, 5);
  9528.                 tmp1[5] = '\0';
  9529.             }
  9530.             else {
  9531.                 convert_bin_to_str(tmpbuf, 5, tmp1);
  9532.                 tmp1[10] = '\0';
  9533.             }          
  9534.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key42 = %s\n", tmp1);
  9535.         }
  9536.         else {
  9537.             apmib_get(MIB_WLAN_WEP128_KEY1, (void *)&tmpbuf);
  9538.             if (wep_key_type == KEY_ASCII) {
  9539.                 memcpy(tmp1, tmpbuf, 13);
  9540.                 tmp1[13] = '\0';
  9541.             }
  9542.             else {
  9543.                 convert_bin_to_str(tmpbuf, 13, tmp1);
  9544.                 tmp1[26] = '\0';
  9545.             }
  9546.             WRITE_WSC_PARAM(ptr, tmpbuf, "network_key2 = %s\n", tmp1);
  9547.  
  9548.             apmib_get(MIB_WLAN_WEP128_KEY2, (void *)&tmpbuf);
  9549.             if (wep_key_type == KEY_ASCII) {
  9550.                 memcpy(tmp1, tmpbuf, 13);
  9551.                 tmp1[13] = '\0';
  9552.             }
  9553.             else {
  9554.                 convert_bin_to_str(tmpbuf, 13, tmp1);
  9555.                 tmp1[26] = '\0';
  9556.             }
  9557.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key22 = %s\n", tmp1);
  9558.  
  9559.             apmib_get(MIB_WLAN_WEP128_KEY3, (void *)&tmpbuf);
  9560.             if (wep_key_type == KEY_ASCII) {
  9561.                 memcpy(tmp1, tmpbuf, 13);
  9562.                 tmp1[13] = '\0';
  9563.             }
  9564.             else {
  9565.                 convert_bin_to_str(tmpbuf, 13, tmp1);
  9566.                 tmp1[26] = '\0';
  9567.             }
  9568.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key32 = %s\n", tmp1);
  9569.  
  9570.             apmib_get(MIB_WLAN_WEP128_KEY4, (void *)&tmpbuf);
  9571.             if (wep_key_type == KEY_ASCII) {
  9572.                 memcpy(tmp1, tmpbuf, 13);
  9573.                 tmp1[13] = '\0';
  9574.             }
  9575.             else {
  9576.                 convert_bin_to_str(tmpbuf, 13, tmp1);
  9577.                 tmp1[26] = '\0';
  9578.             }
  9579.             WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key42 = %s\n", tmp1);
  9580.         }
  9581.     }
  9582.     else {
  9583.         apmib_get(MIB_WLAN_WPA_PSK, (void *)&tmp1);
  9584.         WRITE_WSC_PARAM(ptr, tmpbuf, "network_key2 = %s\n", tmp1);
  9585.        
  9586.     }
  9587.  
  9588. #ifdef CONFIG_RTL8186_KLD_REPEATER
  9589.     if (wps_vxdAP_enabled)
  9590.         apmib_get(MIB_REPEATER_SSID1, (void *)&tmp1);  
  9591.     else
  9592. #endif
  9593.         apmib_get(MIB_WLAN_SSID, (void *)&tmp1);   
  9594.         WRITE_WSC_PARAM(ptr, tmpbuf, "ssid2 = %s\n", tmp1);
  9595.  
  9596.     intVal = 2 ;
  9597.     WRITE_WSC_PARAM(ptr, tmpbuf, "#=====wlan1 end==========%d\n",intVal);
  9598.  
  9599.  
  9600.     /*sync the PIN code of wlan0 and wlan1*/
  9601.     apmib_set(MIB_HW_WSC_PIN, (void *)wsc_pin);
  9602.  
  9603.  
  9604.    
  9605.     /*  switch back to wlan0 */
  9606.     wlan_idx = 0;
  9607.     vwlan_idx = 0; 
  9608.    
  9609. #endif   // End of FOR_DUAL_BAND
  9610.  
  9611.  
  9612.  
  9613.     apmib_get(MIB_HW_WSC_PIN, (void *)&tmp1);
  9614.     WRITE_WSC_PARAM(ptr, tmpbuf, "pin_code = %s\n", tmp1);
  9615.    
  9616.  
  9617. /*
  9618.     apmib_get(MIB_HW_MODEL_NUM, (void *)&tmp1);
  9619.     WRITE_WSC_PARAM(ptr, tmpbuf, "model_num = \"%s\"\n", tmp1);
  9620.  
  9621.     apmib_get(MIB_HW_SERIAL_NUM, (void *)&tmp1);   
  9622.     WRITE_WSC_PARAM(ptr, tmpbuf, "serial_num = \"%s\"\n", tmp1);   
  9623. */
  9624.     apmib_get(MIB_DEVICE_NAME, (void *)&tmp1); 
  9625.  
  9626.     apmib_get(MIB_WLAN_WSC_CONFIGBYEXTREG, (void *)&intVal);
  9627.     WRITE_WSC_PARAM(ptr, tmpbuf, "config_by_ext_reg = %d\n", intVal);
  9628.    
  9629. #ifdef CONFIG_RTL_COMAPI_CFGFILE
  9630.     WRITE_WSC_PARAM(ptr, tmpbuf, "wsc_device_name = \"%s\"\n", tmp1);
  9631. #else
  9632.     WRITE_WSC_PARAM(ptr, tmpbuf, "device_name = \"%s\"\n", tmp1);
  9633. #endif
  9634.  
  9635.     len = (int)(((long)ptr)-((long)buf));
  9636.    
  9637.     fh = open(in, O_RDONLY);
  9638.     if (fh == -1) {
  9639.         printf("open() error [%s]!\n", in);
  9640.         return -1;
  9641.     }
  9642.  
  9643.     lseek(fh, 0L, SEEK_SET);
  9644.     if (read(fh, ptr, status.st_size) != status.st_size) {     
  9645.         printf("read() error [%s]!\n", in);
  9646.         return -1; 
  9647.     }
  9648.     close(fh);
  9649.  
  9650.     // search UUID field, replace last 12 char with hw mac address
  9651.     ptr = strstr(ptr, "uuid =");
  9652.     if (ptr) {
  9653.         char tmp2[100];
  9654.         apmib_get(MIB_HW_NIC0_ADDR, (void *)&tmp1);
  9655.         convert_bin_to_str(tmp1, 6, tmp2);
  9656.         memcpy(ptr+27, tmp2, 12);      
  9657.     }
  9658.  
  9659.     fh = open(out, O_RDWR|O_CREAT|O_TRUNC);
  9660.     if (fh == -1) {
  9661.         printf("open() error [%s]!\n", out);
  9662.         return -1;
  9663.     }
  9664.  
  9665.     if (write(fh, buf, len+status.st_size) != len+status.st_size ) {
  9666.         printf("Write() file error [%s]!\n", out);
  9667.         return -1;
  9668.     }
  9669.     close(fh);
  9670.     free(buf);
  9671.  
  9672.     return 0;
  9673. }
  9674. #endif
  9675. #endif //#if defined(UNIVERSAL_REPEATER) && defined(CONFIG_REPEATER_WPS_SUPPORT)
  9676.  
  9677. #ifdef COMPRESS_MIB_SETTING
  9678.  
  9679. int flash_mib_checksum_ok(int offset)
  9680. {  
  9681.     unsigned char* pContent = NULL;
  9682.  
  9683.     COMPRESS_MIB_HEADER_T compHeader;
  9684.     unsigned char *expPtr, *compPtr;
  9685.     unsigned int expLen = 0;
  9686.     unsigned int compLen = 0;
  9687.     unsigned int real_size = 0;
  9688.     int zipRate=0;
  9689.     PARAM_HEADER_T header;
  9690.     CONFIG_DATA_T type;
  9691.     char *pcomp_sig;
  9692.     int status;
  9693.    
  9694.     switch(offset)
  9695.     {
  9696.         case HW_SETTING_OFFSET:
  9697.             pcomp_sig = COMP_HS_SIGNATURE;
  9698.             type = HW_SETTING;
  9699.             break;
  9700.         case DEFAULT_SETTING_OFFSET:
  9701.             type = DEFAULT_SETTING;
  9702.             pcomp_sig = COMP_DS_SIGNATURE;
  9703.             break;
  9704.         case CURRENT_SETTING_OFFSET:
  9705.             type = CURRENT_SETTING;
  9706.             pcomp_sig = COMP_CS_SIGNATURE;
  9707.             break;
  9708.         default:
  9709.             printf("offset error-[%s-%u]\n",__FILE__,__LINE__);        
  9710.             return -1;
  9711.     }
  9712.    
  9713.     real_size = mib_get_real_len(type);
  9714.  
  9715.     flash_read((char *)&compHeader, offset, sizeof(COMPRESS_MIB_HEADER_T));
  9716.     if((strncmp((char *)compHeader.signature, pcomp_sig, COMP_SIGNATURE_LEN) == 0) &&
  9717.         (compHeader.compLen > 0) &&
  9718.         (compHeader.compLen <= real_size)
  9719.     ) //check whether compress mib data
  9720.     {
  9721.         zipRate = compHeader.compRate;
  9722.         compLen = compHeader.compLen;
  9723.  
  9724.         compPtr=calloc(1,compLen+sizeof(COMPRESS_MIB_HEADER_T));
  9725.         if(compPtr==NULL)
  9726.         {
  9727.             COMP_TRACE(stderr,"\r\n ERR:compPtr malloc fail! __[%s-%u]",__FILE__,__LINE__);
  9728.             return -1;
  9729.         }
  9730.         expPtr=calloc(1,zipRate*compLen);
  9731.         if(expPtr==NULL)
  9732.         {
  9733.             COMP_TRACE(stderr,"\r\n ERR:expPtr malloc fail! __[%s-%u]",__FILE__,__LINE__);         
  9734.             free(compPtr);
  9735.             return -1;
  9736.         }
  9737.  
  9738.         flash_read((char *)compPtr, offset, compLen+sizeof(COMPRESS_MIB_HEADER_T));
  9739.  
  9740.         expLen = Decode(compPtr+sizeof(COMPRESS_MIB_HEADER_T), compLen, expPtr);
  9741.         //printf("expandLen read len: %d\n", expandLen);
  9742.  
  9743.         // copy the mib header from expFile
  9744.         memcpy((char *)&header, expPtr, sizeof(header));
  9745.         pContent = calloc(1,header.len);
  9746.         if(pContent==NULL)
  9747.         {
  9748.             COMP_TRACE(stderr,"\r\n ERR:pContent malloc fail! __[%s-%u]",__FILE__,__LINE__);           
  9749.             free(compPtr);
  9750.             free(expPtr);
  9751.             return -1;
  9752.         }
  9753.         memcpy(pContent, expPtr+sizeof(PARAM_HEADER_T), header.len);
  9754.         status = CHECKSUM_OK(pContent, header.len);
  9755.         if ( !status) {
  9756.             COMP_TRACE(stderr,"comp Checksum type[%u] error!\n", type);
  9757.             return -1;
  9758.         }
  9759.         if(compPtr != NULL)
  9760.             free(compPtr);
  9761.         if(expPtr != NULL)
  9762.             free(expPtr);
  9763.         if(pContent != NULL)
  9764.             free(pContent);
  9765.  
  9766.         return 1;
  9767.     }
  9768.     else
  9769.     {
  9770.         COMP_TRACE(stderr,"\r\n Invalid comp sig or compress len __[%s-%u]",__FILE__,__LINE__);        
  9771.     }
  9772.     return -1;
  9773. }
  9774.  
  9775. int flash_mib_compress_write(CONFIG_DATA_T type, char *data, PARAM_HEADER_T *pheader, unsigned char *pchecksum)
  9776. {
  9777.     unsigned char* pContent = NULL;
  9778.  
  9779.     COMPRESS_MIB_HEADER_T compHeader;
  9780.     unsigned char *expPtr, *compPtr;
  9781.     unsigned int expLen = 0;
  9782.     unsigned int compLen;
  9783.     unsigned int real_size = 0;
  9784.     //int zipRate=0;
  9785.     char *pcomp_sig;
  9786.     unsigned char checksum  = *pchecksum;
  9787.     int dst = mib_get_flash_offset(type);
  9788.  
  9789.     if(dst < 0)
  9790.     {
  9791.         COMP_TRACE("\r\n ERR!! no flash offset! __[%s-%u]\n",__FILE__,__LINE__);
  9792.         return 0;
  9793.     }
  9794.    
  9795.     switch(type)
  9796.     {
  9797.         case HW_SETTING:
  9798.             pcomp_sig = COMP_HS_SIGNATURE;
  9799.             break;
  9800.         case DEFAULT_SETTING:
  9801.             pcomp_sig = COMP_DS_SIGNATURE;
  9802.             break;
  9803.         case CURRENT_SETTING:
  9804.             pcomp_sig = COMP_CS_SIGNATURE;
  9805.             break;
  9806.         default:
  9807.             COMP_TRACE("\r\n ERR!! no type match __[%s-%u]\n",__FILE__,__LINE__);
  9808.             return 0;
  9809.  
  9810.     }
  9811.     expLen = pheader->len+sizeof(PARAM_HEADER_T);
  9812.     if(expLen == 0)
  9813.     {
  9814.         COMP_TRACE("\r\n ERR!! no expLen! __[%s-%u]\n",__FILE__,__LINE__);
  9815.         return 0;
  9816.     }
  9817.     real_size = mib_get_real_len(type);
  9818.     if(real_size == 0)
  9819.     {
  9820.         COMP_TRACE("\r\n ERR!! no expLen! __[%s-%u]\n",__FILE__,__LINE__);
  9821.         return 0;
  9822.     }
  9823.    
  9824.     if( (compPtr = malloc(real_size)) == NULL)
  9825.     {
  9826.         COMP_TRACE("\r\n ERR!! malloc  size %u failed! __[%s-%u]\n",real_size,__FILE__,__LINE__);
  9827.     }
  9828.  
  9829.     if( (expPtr = malloc(expLen)) == NULL)
  9830.     {
  9831.         COMP_TRACE("\r\n ERR!! malloc  size %u failed! __[%s-%u]\n",expLen,__FILE__,__LINE__);
  9832.         if(compPtr != NULL)
  9833.             free(compPtr);
  9834.     }
  9835.    
  9836.     if(compPtr != NULL && expPtr!= NULL)
  9837.     {
  9838.         //int status;
  9839.         pContent = &expPtr[sizeof(PARAM_HEADER_T)]; // point to start of MIB data
  9840.  
  9841.         memcpy(pContent, data, pheader->len-sizeof(checksum));
  9842.    
  9843.         memcpy(pContent+pheader->len-sizeof(checksum), &checksum, sizeof(checksum));
  9844.         memcpy(expPtr, pheader, sizeof(PARAM_HEADER_T));
  9845.  
  9846.         compLen = Encode(expPtr, expLen, compPtr+sizeof(COMPRESS_MIB_HEADER_T));
  9847.         sprintf((char *)compHeader.signature,"%s",pcomp_sig);
  9848.         compHeader.compRate = (expLen/compLen)+1;
  9849.         compHeader.compLen = compLen;
  9850.         memcpy(compPtr, &compHeader, sizeof(COMPRESS_MIB_HEADER_T));
  9851.  
  9852.    
  9853.  
  9854.         if ( flash_write((char *)compPtr, dst, compLen+sizeof(COMPRESS_MIB_HEADER_T))==0 )
  9855. //      if ( write(fh, (const void *)compPtr, compLen+sizeof(COMPRESS_MIB_HEADER_T))!=compLen+sizeof(COMPRESS_MIB_HEADER_T) )
  9856.         {
  9857.             printf("Write flash compress setting[%u] failed![%s-%u]\n",type,__FILE__,__LINE__);        
  9858.             return 0;
  9859.         }
  9860. #if 0
  9861. fprintf(stderr,"\r\n Write compress data to 0x%x. Expenlen=%u, CompRate=%u, CompLen=%u, __[%s-%u]",dst, expLen, compHeader.compRate, compHeader.compLen,__FILE__,__LINE__);
  9862.  
  9863. #endif
  9864.        
  9865.         if(compPtr != NULL)
  9866.             free(compPtr);
  9867.         if(expPtr != NULL)
  9868.             free(expPtr);
  9869.  
  9870.         return 1;
  9871.            
  9872.     }
  9873.     else
  9874.     {
  9875.         return 0;
  9876.     }
  9877.  
  9878.     return 0;
  9879. }
  9880. #endif //#ifdef COMPRESS_MIB_SETTING
  9881.  
  9882. static int send_ppp_terminate(int sock, struct sockaddr_ll *ME,
  9883.                      struct sockaddr_ll *HE, int sessid)
  9884. {  
  9885.     unsigned char buf[256];
  9886.     struct pppoe_hdr *ah = (struct pppoe_hdr *) buf;
  9887. //  unsigned char *p = (unsigned char *) (ah + 1);
  9888.     unsigned char *p = buf;
  9889.     int c;
  9890.    
  9891.     ah->ver = 1;
  9892.     ah->type = 1;
  9893.     ah->code = 0;
  9894.    
  9895.     //ah->sid = (short*)sessid;
  9896.     ah->sid = (short)sessid;
  9897.    
  9898.     ah->length = 18;
  9899.    
  9900.     p+=sizeof(struct pppoe_hdr);
  9901.  
  9902.     memcpy(p, "\xc0\x21", 2);
  9903.     p += 2;
  9904.  
  9905.     memcpy(p, "\x05", 1);
  9906.     p += 1;
  9907.    
  9908.     memcpy(p, "\x02", 1);
  9909.     p += 1;
  9910.    
  9911.     memcpy(p, "\x00\x10", 2);
  9912.     p += 2;
  9913.    
  9914.     memcpy(p, "\x55\x73\x65\x72\x20\x72\x65\x71\x75\x65\x73\x74", 12);
  9915.     p += 12;
  9916.        
  9917.     c = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
  9918.    
  9919.     if (c < 0 || c != p - buf)
  9920.     {
  9921.         printf("\r\n \r\n c = %d",c);
  9922.         return -1;
  9923.     }
  9924.    
  9925.     return 0;
  9926. }
  9927.  
  9928. static int send_ppp_msg(int sessid, unsigned char *peerMacStr)
  9929. {
  9930.     unsigned char peerMacAddr[MAC_ADDR_LEN];
  9931.     char *device_if[]={"eth1"};
  9932.     char *device = NULL;
  9933.     int i;
  9934.  
  9935.     for(i=0 ; i<1 ; i++)
  9936.     {
  9937.         struct sockaddr_ll me;
  9938.         struct sockaddr_ll he;
  9939.         int sock;
  9940.         struct ifreq ifr;
  9941.        
  9942.         int ifindex = 0;
  9943.         //unsigned char lanIp[4];
  9944.        
  9945.        
  9946.         //int   entryNum, index;
  9947.         //DHCPRSVDIP_T entry;
  9948.        
  9949.         device = device_if[i];         
  9950.        
  9951.         sock = socket(PF_PACKET, SOCK_DGRAM, 0);
  9952.    
  9953.         if (sock < 0) {
  9954.             printf("\r\n  Get sock fail!");
  9955.             return -1;
  9956.         }
  9957.        
  9958.         memset(&ifr, 0, sizeof(ifr));
  9959.         strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
  9960.         if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
  9961.             printf("\r\n Interface %s not found", device);
  9962.             close(sock);
  9963.             continue;
  9964.         }
  9965.         ifindex = ifr.ifr_ifindex;
  9966.    
  9967.         if (ioctl(sock, SIOCGIFFLAGS, (char *) &ifr)) {
  9968.             printf("\r\n SIOCGIFFLAGS");
  9969.             close(sock);
  9970.             return -1;
  9971.         }
  9972.         if (!(ifr.ifr_flags & IFF_UP)) {
  9973.             printf("\r\n Interface %s is down", device);
  9974.             close(sock);
  9975.             continue;
  9976.         }
  9977.         if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {          
  9978.             printf("\r\n Interface %s is not ARPable", device);
  9979.             close(sock);
  9980.             return -1;
  9981.         }
  9982.    
  9983.         //apmib_get(MIB_IP_ADDR,  (void *)lanIp);
  9984.         //memcpy(&src, lanIp, 4);
  9985.        
  9986.         //if (src.s_addr)
  9987.         {
  9988.             struct sockaddr_in saddr;
  9989.             int probe_fd = socket(AF_INET, SOCK_DGRAM, 0);
  9990.        
  9991.             if (probe_fd < 0) {        
  9992.                 printf("\r\n socket");
  9993.                 close(sock);
  9994.                 return -1;
  9995.             }
  9996.            
  9997.             if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,strlen(device) + 1) == -1)
  9998.                     printf("\r\n WARNING: interface %s is ignored", device);
  9999.                    
  10000.             memset(&saddr, 0, sizeof(saddr));
  10001.             saddr.sin_family = AF_INET;
  10002.            
  10003.             //saddr.sin_addr = src;
  10004.             if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1)
  10005.             {
  10006.                 fprintf(stderr,"bind");
  10007.                 close(sock);
  10008.                 close(probe_fd);
  10009.                 return -1;
  10010.             }
  10011.             close(probe_fd);
  10012.         }
  10013.        
  10014.         me.sll_family = AF_PACKET;
  10015.         me.sll_ifindex = ifindex;
  10016.         me.sll_protocol = htons(ETH_P_PPP_SES);
  10017.         if (bind(sock, (struct sockaddr *) &me, sizeof(me)) == -1) {
  10018.             printf("\r\n bind");
  10019.             close(sock);
  10020.             return -1;
  10021.         }
  10022.    
  10023.         {
  10024.             //int alen = sizeof(me);
  10025.             socklen_t alen = sizeof(me);
  10026.    
  10027.             if (getsockname(sock, (struct sockaddr *) &me, &alen) == -1) {
  10028.                 printf("\r\n getsockname");
  10029.                 close(sock);
  10030.                 return -1;
  10031.             }
  10032.         }
  10033.         if (me.sll_halen == 0) {
  10034.             printf("\r\n Interface \"%s\" is not ARPable (no ll address)", device);
  10035.             close(sock);
  10036.             return -1;
  10037.         }
  10038.         he = me;
  10039.         memset(he.sll_addr, -1, he.sll_halen);
  10040.        
  10041.         //apmib_get(MIB_DHCPRSVDIP_NUM, (void *)&entryNum);
  10042.        
  10043.        
  10044.         //for(index = 1; index<=entryNum ; index++)
  10045.         {
  10046. #if 0          
  10047.             *((char *)&entry) = (char)index;
  10048.             if (!apmib_get(MIB_DHCPRSVDIP, (void *)&entry))
  10049.                 continue;
  10050.            
  10051.             if(strlen(entry.hostName) == 0)
  10052.                 continue;
  10053.                
  10054.             memcpy(&dst, entry.ipAddr , 4);
  10055. #endif         
  10056.             //memcpy(he.sll_addr, "\xff\xff\xff\xff\xff\xff" , he.sll_halen);
  10057.             memset(peerMacAddr,0, sizeof(peerMacAddr));
  10058.             string_to_hex((char *)peerMacStr,peerMacAddr,MAC_ADDR_LEN<<1);
  10059.  
  10060.             if((he.sll_halen != MAC_ADDR_LEN) || (!memcmp(peerMacAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LEN))){
  10061.                 printf("%s(%d): wrong he.sll_halen(%d) or wrong peerMacAddr[%02x:%02x:%02x:%02x:%02x:%02x]!\n",
  10062.                     __FUNCTION__,__LINE__,he.sll_halen,
  10063.                     peerMacAddr[0],peerMacAddr[1],peerMacAddr[2],peerMacAddr[3],peerMacAddr[4],peerMacAddr[5]);//Added for test
  10064.                 close(sock);
  10065.                 return -1;
  10066.             }
  10067.             else{
  10068.                 memcpy(he.sll_addr, peerMacAddr , he.sll_halen);
  10069.             }
  10070.            
  10071.            
  10072.             if(send_ppp_terminate(sock, &me, &he, sessid) == -1)
  10073.             {
  10074.                 printf("\r\n \r\n send_arp Fail!");
  10075.                 close(sock);
  10076.                 return -1;
  10077.                
  10078.             }
  10079.         }
  10080.        
  10081.         close(sock);
  10082.    
  10083.     } //end of for(i=0 ; i<3 ; i++)
  10084.    
  10085.     return 0;
  10086.    
  10087. }
  10088.  
  10089. static int send_l2tp_msg(unsigned short l2tp_ns, int buf_length, unsigned char * l2tp_buff, unsigned char *lanIp, unsigned char *serverIp)
  10090. {
  10091.     struct sockaddr_in client_addr;
  10092.     unsigned char l2tp_buff_hex[buf_length];
  10093.     unsigned char lanIpAddr[4], serverIpAddr[4];
  10094.     unsigned int lanIpAd, serverIpAd;
  10095.     int on = 1;
  10096.     int c;
  10097.    
  10098.     memset(lanIpAddr,0, sizeof(lanIpAddr));
  10099.     string_to_hex((char *)lanIp,lanIpAddr,4<<1);
  10100.  
  10101.     memset(serverIpAddr,0, sizeof(serverIpAddr));
  10102.     string_to_hex((char *)serverIp,serverIpAddr,4<<1);
  10103.  
  10104.     memcpy((void *)&lanIpAd, (void* )lanIpAddr, 4);
  10105.     memcpy((void *)&serverIpAd, (void* )serverIpAddr, 4);
  10106.  
  10107.     string_to_hex((char *)l2tp_buff, l2tp_buff_hex, buf_length<<1);
  10108.    
  10109. #if 0
  10110.     printf("lanIp is %d.%d.%d.%d\n",  lanIpAddr[0], lanIpAddr[1], lanIpAddr[2], lanIpAddr[3]);
  10111.     printf("serverIp is %d.%d.%d.%d\n",  serverIpAddr[0], serverIpAddr[1], serverIpAddr[2], serverIpAddr[3]);
  10112.     printf("mib length is %d\n", buf_length);
  10113.  
  10114.     printf("send_l2tp_msg l2tp_payload is %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  10115.     l2tp_buff_hex[0], l2tp_buff_hex[1], l2tp_buff_hex[2], l2tp_buff_hex[3], l2tp_buff_hex[4], l2tp_buff_hex[5], l2tp_buff_hex[6], l2tp_buff_hex[7],
  10116.     l2tp_buff_hex[8], l2tp_buff_hex[9], l2tp_buff_hex[10], l2tp_buff_hex[11], l2tp_buff_hex[12], l2tp_buff_hex[13], l2tp_buff_hex[14], l2tp_buff_hex[15],
  10117.     l2tp_buff_hex[16], l2tp_buff_hex[17], l2tp_buff_hex[18], l2tp_buff_hex[19], l2tp_buff_hex[20], l2tp_buff_hex[21], l2tp_buff_hex[22], l2tp_buff_hex[23],
  10118.     l2tp_buff_hex[24], l2tp_buff_hex[25], l2tp_buff_hex[26], l2tp_buff_hex[27], l2tp_buff_hex[28], l2tp_buff_hex[29], l2tp_buff_hex[30], l2tp_buff_hex[31],
  10119.     l2tp_buff_hex[32], l2tp_buff_hex[33], l2tp_buff_hex[34], l2tp_buff_hex[35], l2tp_buff_hex[36], l2tp_buff_hex[37]);
  10120. #endif
  10121.  
  10122.     bzero(&client_addr,sizeof(client_addr));
  10123.     client_addr.sin_family = AF_INET;  
  10124.     client_addr.sin_addr.s_addr = htonl(lanIpAd);
  10125.     client_addr.sin_port = htons(1701);
  10126.  
  10127.     int client_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  10128.     setsockopt( client_socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) );
  10129.     if(client_socket < 0)
  10130.     {
  10131.         printf("Create Socket Failed!\n");
  10132.         exit(1);
  10133.     }
  10134.  
  10135.     if (bind(client_socket, (struct sockaddr *) &client_addr, sizeof(client_addr)) == -1)
  10136.     {
  10137.         perror("bind");
  10138.         close(client_socket);
  10139.         return -1;
  10140.     }
  10141.    
  10142.     struct sockaddr_in server_addr;
  10143.     bzero(&server_addr,sizeof(server_addr));
  10144.     server_addr.sin_family = AF_INET;
  10145.     server_addr.sin_addr.s_addr = serverIpAd;
  10146.     server_addr.sin_port = htons(1701);
  10147.     socklen_t n = sizeof(server_addr);
  10148.    
  10149.     memcpy((void*)&l2tp_buff_hex[8], (void*)&l2tp_ns, 2);
  10150.     c = sendto(client_socket, l2tp_buff_hex, buf_length, 0, (struct sockaddr*)&server_addr, n);
  10151.     if (c < 0 || c != buf_length)
  10152.     {
  10153.         printf("\r\n \r\n l2tp sendto is failed: c = %d", c);
  10154.         return -1;
  10155.     }
  10156.    
  10157.     close(client_socket);
  10158.     return 0;
  10159. }
Add Comment
Please, Sign In to add comment