www.usr.com/support/gpl/USR9113_release1.0.tar.gz
[bcm963xx.git] / userapps / broadcom / cfm / util / system / syscall.c
index 7b8e82a..daad4d0 100755 (executable)
@@ -55,6 +55,9 @@
 #include "clidefs.h"
 #include "cliapi.h"
 #include "adslctlapi.h"
+#if defined(SUPPORT_VDSL)
+#include "vdslctlapi.h"
+#endif
 #include "secapi.h"
 #include "dbapi.h"
 #include "dbobject.h"
 #include <atm.h>
 #include "portMirror.h"
 #include "atmapidrv.h"
-#endif
-#endif
+#endif   /* PORT_MIRRORING */
+#include "bcmcfm.h"
+#endif   /* USE_ALL */
 
+#include "bcmxdsl.h"
 #include "syscall.h"
 #include "board_api.h"
 #include "bcmTag.h"
 #include "busybox.h"
 #endif
 
+#ifndef USE_ALL
+#define IFC_LARGE_LEN        264
+#endif
+
 extern char **environ;
 extern char *getIfName(void);
 
+extern char glbErrMsg[IFC_LARGE_LEN];
 #define READ_BUF_SIZE        128
 #define CONNTRACK_MUX        2000
 
@@ -123,8 +133,8 @@ int bcmSystemNoHang (char *command) {
          case -1:   // error occurs then return -1
             return -1;
          case 0:    // child does not exited yet
-            sleep(1);
-            if ( ++counter > 19 ) {
+            usleep(20);
+            if ( ++counter > 20000 ) {
 #ifdef BRCM_DEBUG
                printf("app: child process cannot exits while executing command - %s\n", command);
 #endif
@@ -430,6 +440,36 @@ int bcmCreateResolvCfg(char *dns1, char *dns2) {
 // global ADSL info variable is declared here (in syscall.c)
 // and is used in syscall.c, sysdiag.c, cgimain.c, and cgists.c
 ADSL_CONNECTION_INFO glbAdslInfo;
+#if defined(SUPPORT_VDSL)
+XDSL_CONNECTION_INFO glbVdslInfo;
+#endif
+
+/***************************************************************************
+// Function Name: bcmGetXdslStatus().
+// Description  : get ADSL or VDSL status, depending on which link is up
+// Parameters   : none
+// Returns      : Link satus as in BCMXDSL_STATUS
+****************************************************************************/
+int bcmGetXdslStatus() 
+{
+  BCMXDSL_STATUS  ret = BCM_XDSL_LINK_DOWN;
+  BCMADSL_STATUS  sts;
+
+    sts = BcmAdslCtl_GetConnectionInfo(&glbAdslInfo);
+    if( sts == BCMADSL_STATUS_SUCCESS )
+      ret = glbAdslInfo.LinkState;
+
+#if defined(SUPPORT_VDSL)
+    if((sts == BCMADSL_STATUS_ERROR) || ( glbAdslInfo.LinkState == BCM_XDSL_LINK_DOWN))
+    {
+      sts = BcmVdslCtl_GetConnectionInfo(&glbVdslInfo, glbErrMsg);
+      if( sts == BCMADSL_STATUS_SUCCESS )
+        ret = glbVdslInfo.LinkState;
+    }
+#endif
+
+  return((int)ret);
+}
 
 /***************************************************************************
 // Function Name: bcmGetAdslStatus().
@@ -444,16 +484,27 @@ int bcmGetAdslStatus() {
 
    if (BcmAdslCtl_GetConnectionInfo(&glbAdslInfo) != BCMADSL_STATUS_ERROR) {
       ret = glbAdslInfo.LinkState;
-//      if ( glbAdslInfo.LinkState == BCM_ADSL_LINK_UP )
-//         ret = ADSL_LINK_UP;
-//      else
-//         ret = ADSL_LINK_DOWN;
    } else
       ret = BCMADSL_STATUS_ERROR;
 
    return ret;
 }
 
+#if defined(SUPPORT_VDSL)
+/***************************************************************************
+// Function Name: bcmGetVdslStatus().
+// Description  : get VDSL status.
+// Parameters   : none
+// Returns      : 0 - VDSL link Up (OK)
+//                1 - VDSL link Down
+//                2 - Other error
+****************************************************************************/
+int bcmGetVdslStatus() {
+   return((BcmVdslCtl_GetConnectionInfo(&glbVdslInfo, glbErrMsg) != BCMADSL_STATUS_ERROR) ?
+           glbVdslInfo.LinkState : BCMADSL_STATUS_ERROR);
+}
+#endif
+
 /***************************************************************************
 // Function Name: bcmGetPppStatus().
 // Description  : get PPP status.
@@ -562,6 +613,8 @@ void bcmDisplayLed(int status) {
       /* this means ADSL is UP, but not PPP */
       //sysLedCtrl(kLedPPP, kLedStateFail);
       sysLedCtrl(kLedPPP, kLedStateOff);       // USR9108
+      /* TR68 - no change unless authentication error. */
+      /* sysLedCtrl(kLedPPP, kLedStateFail); */
       break;
    case MNTR_STS_OK:
       /* this means ADSL and PPP are up */
@@ -600,19 +653,20 @@ void bcmGetDynamicDnsAddr(char *dns, int primary) {
 ****************************************************************************/
 void bcmGetDns(char *dns) {
    IFC_DNS_INFO dnsInfo;
+   int sts = BcmDb_getDnsInfo(&dnsInfo);
 
    dns[0] = '\0';
-   if ( ((BcmDb_getDnsInfo(&dnsInfo) == DB_GET_OK) && (dnsInfo.dynamic)) ||
-        (BcmDb_getDnsInfo(&dnsInfo) != DB_GET_OK) ) {
-      if (dnsInfo.dynamic) {
+   if ( sts == DB_GET_OK ) {
+      if ( dnsInfo.dynamic == TRUE )
          bcmGetDynamicDnsAddr(dns,BCM_PRIMARY_DNS);
-      }
-   } 
-   else {
-     /* static */
-      if ( dnsInfo.preferredDns.s_addr != INADDR_NONE )
-         strcpy(dns, inet_ntoa(dnsInfo.preferredDns));
-   }
+      else {
+         if ( dnsInfo.preferredDns.s_addr != INADDR_NONE )
+            strcpy(dns, inet_ntoa(dnsInfo.preferredDns));
+      }                
+   } else
+      // in bcmGetDynamicDnsAddr, if cannot find primary dns info then
+      // assign default value which is router IP address
+      bcmGetDynamicDnsAddr(dns,BCM_PRIMARY_DNS);
 }
 
 /***************************************************************************
@@ -623,19 +677,20 @@ void bcmGetDns(char *dns) {
 ****************************************************************************/
 void bcmGetDns2(char *dns) {
    IFC_DNS_INFO dnsInfo;
+   int sts = BcmDb_getDnsInfo(&dnsInfo);
 
    dns[0] = '\0';
-   if ( ((BcmDb_getDnsInfo(&dnsInfo) == DB_GET_OK) && (dnsInfo.dynamic)) ||
-        (BcmDb_getDnsInfo(&dnsInfo) != DB_GET_OK) ) {
-      if (dnsInfo.dynamic) {
-         bcmGetDynamicDnsAddr(dns, BCM_SECONDARY_DNS);
-      }
-   } 
-   else {
-     /* static */
-      if ( dnsInfo.alternateDns.s_addr != INADDR_NONE )
-         strcpy(dns, inet_ntoa(dnsInfo.alternateDns));
-   }
+   if ( sts == DB_GET_OK ) {
+      if ( dnsInfo.dynamic == TRUE )
+         bcmGetDynamicDnsAddr(dns,BCM_SECONDARY_DNS);
+      else {
+         if ( dnsInfo.alternateDns.s_addr != INADDR_NONE )
+            strcpy(dns, inet_ntoa(dnsInfo.alternateDns));
+      }                
+   } else
+      // in bcmGetDynamicDnsAddr, if cannot find primary dns info then
+      // assign default value which is router IP address
+      bcmGetDynamicDnsAddr(dns,BCM_SECONDARY_DNS);
 }
 
 void bcmGetDnsSettings(int *mode, char *primary, char *secondary)
@@ -679,13 +734,15 @@ void bcmRestartDnsProbe(void) {
 void bcmConfigDns(char *dns1, char *dns2, int dynamic) {
    char cmd[CLI_MAX_BUF_SZ], addr[CLI_MAX_BUF_SZ], buf[CLI_MAX_BUF_SZ];
    IFC_DNS_INFO dnsInfo;
+    // need to call getDnsInfo before setDnsInfo to retrieve domain name
+   int sts = BcmDb_getDnsInfo(&dnsInfo);
 
    // get local ip address
    bcmGetIfDestAddr("br0", addr);
 
    // if user changes from static to auto assgined dns
    if (dynamic) {
-      if ( BcmDb_getDnsInfo(&dnsInfo) == DB_GET_OK &&
+      if ( sts == DB_GET_OK &&
            bcmIsModuleInserted("iptable_nat") == TRUE ) {
          strcpy(buf, inet_ntoa(dnsInfo.preferredDns));
          // del the old rule
@@ -732,7 +789,6 @@ void bcmConfigDns(char *dns1, char *dns2, int dynamic) {
    } /* dynamic to static */
    dnsInfo.dynamic = dynamic;
    BcmDb_setDnsInfo(&dnsInfo);
-   BcmPsi_flush();
 }
 
 /***************************************************************************
@@ -841,6 +897,9 @@ void bcmSetAutoDefaultGateway(char *errMsg) {
    char cmd[IFC_LARGE_LEN];
    FILE *fs = NULL;
    IFC_DEF_GW_INFO defgw;
+// USR9109/9113 Just in case if something was wrong.
+   errMsg[0] = '\0';
 
    // remove static default gateway
    if ( BcmDb_getDefaultGatewayInfo(&defgw) == DB_GET_OK ) {
@@ -1415,7 +1474,6 @@ char *bcmGetIfcNameById(int ifcId, char *ifcName) {
    if ( ifcId >= IFC_ENET_ID && ifcId < IFC_USB_ID )
       sprintf(ifcName, "eth%d", ifcId - IFC_ENET_ID);
    else if ( ifcId >= IFC_USB_ID && ifcId < IFC_HPNA_ID )
-//      strcpy(ifcName, "eth1");
       sprintf(ifcName, "usb%d", ifcId - IFC_USB_ID);
    else if ( ifcId >= IFC_HPNA_ID && ifcId < IFC_WIRELESS_ID )
       sprintf(ifcName, "il%d", ifcId - IFC_HPNA_ID);
@@ -1428,6 +1486,10 @@ char *bcmGetIfcNameById(int ifcId, char *ifcName) {
          sprintf(ifcName, "wl0.%d", num); 
       }
    }
+   else if (ifcId >= IFC_ENET0_VNET_ID && ifcId < IFC_ENET0_VNET_ID + IFC_LAN_MAX + 2)
+       sprintf(ifcName, "eth0.%d", ifcId - IFC_ENET0_VNET_ID);
+   else if (ifcId >= IFC_ENET1_VNET_ID && ifcId < IFC_ENET1_VNET_ID + IFC_LAN_MAX + 2)
+       sprintf(ifcName, "eth1.%d", ifcId - IFC_ENET1_VNET_ID);
 
    return ifcName;
 }
@@ -1608,7 +1670,7 @@ int bcmRemoveIpTableRule(char *device, char *table, char *chain) {
    // execute iptables command to create iptable file
    sprintf(line, "iptables -t %s -L %s -v --line-numbers > /var/iptable",
            table, chain);
-   bcmSystemMute(line);
+   bcmSystemNoHang(line);
 
    fs = fopen("/var/iptable", "r");
    if ( fs != NULL ) {
@@ -1625,7 +1687,7 @@ int bcmRemoveIpTableRule(char *device, char *table, char *chain) {
               strcmp(col[IP_TBL_COL_OUT], device) == 0 ) {
             sprintf(line, "iptables -t %s -D %s %s 2>/dev/null",
                     table, chain, col[IP_TBL_COL_NUM]);
-            bcmSystemMute(line);
+            bcmSystemNoHang(line);
             ret = TRUE;
             break;
          }
@@ -2021,6 +2083,28 @@ int bcmIsMarkStrChar(char c) {
    return ret;
 }
 
+#if defined(SUPPORT_VDSL)
+static char glbVdslSwVer[SYS_CMD_LEN];
+//**************************************************************************
+//**************************************************************************
+// Function Name: bcmSetSwVer
+// Description  : store software version to global variable
+// Parameters   : swVer - the software version that needs to be set.
+// Returns      : none.
+//**************************************************************************
+void bcmSetVdslSwVer(char *swVer) 
+{
+   if ( swVer == NULL ) return;
+
+   if ( strlen(swVer) < SYS_CMD_LEN - 1 )
+      strcpy(glbVdslSwVer, swVer);
+   else {
+      strncpy(glbVdslSwVer, swVer, SYS_CMD_LEN - 2);
+      glbVdslSwVer[SYS_CMD_LEN - 1] = '\0';
+   }
+}
+#endif
+
 //**************************************************************************
 // Function Name: bcmGetSwVer
 // Description  : retrieve software version from global variable
@@ -2038,7 +2122,6 @@ void bcmGetSwVer(char *swVer, int size) {
    // create software version
    sprintf(version, "%s%s", SOFTWARE_VERSION, adslPhyVersion);
    strncpy(swVer, version, size);
-   swVer[size-1] = '\0';
 }
 
 // USR9108
@@ -2065,6 +2148,26 @@ void bcmGetBuildVer(char *swVer, int size) {
 #endif
 }
 
+#if defined(SUPPORT_VDSL)
+//**************************************************************************
+// Function Name: bcmGetVdslSwVer
+// Description  : retrieve software version from global variable
+// Parameters   : swVer - buffer to get the software version.
+//                size - size of buffer.
+// Returns      : none.
+//**************************************************************************
+void bcmGetVdslSwVer(char *swVer, int size) {
+   if ( swVer == NULL ) return;
+
+   if ( strlen(glbVdslSwVer) < size - 1 )
+      strcpy(swVer, glbVdslSwVer);
+   else {
+      strncpy(swVer, glbVdslSwVer, size - 2);
+      swVer[size - 1] = '\0';
+   }
+}
+#endif
+
 //**************************************************************************
 // Function Name: bcmcheck_enable
 // Description  : check the appName with ip address against the psi
@@ -2592,36 +2695,17 @@ int isAccessFromLan(struct in_addr clntAddr)
 int checkChipId(char *strTagChipId, char *sig2)
 {
     int tagChipId = 0;
-       unsigned int chipId = (int) sysGetChipId();
-    int result = 0;
-   
-    if (strstr(sig2, "Firmware"))
-        return result;      // skip the pre 2_14L02 release, where the signiture_2 is "Firmware version 1.0"
-
-    tagChipId = atoi(strTagChipId);
+    unsigned int chipId = (int) sysGetChipId();
+    int result;
 
-       switch (chipId) 
-       {
-        case 0x6338:
-                       if (!(tagChipId == 6338))
-                result = -1;
-            break;
-        case 0x6345:
-                       if (!(tagChipId == 6345 || tagChipId == 6335))
-                result = -1;
-            break;
-               case 0x6348:
-                       if (tagChipId != 6348)
-                result = -1;
-            break;
-        default:
-                       printf("Chip id %04x not supported.\n", chipId);
-            result = -1;
-            break;
-       }
+    tagChipId = strtol(strTagChipId, NULL, 16);
 
-    if (result == -1)
-           printf("Chip Id error.  Image Chip Id = %d, Board Chip Id = %04x.\n", tagChipId, chipId);
+    if (tagChipId == chipId)
+        result = 0;
+    else {
+        printf("Chip Id error.  Image Chip Id = %04x, Board Chip Id = %04x.\n", tagChipId, chipId);
+        result = -1;
+    }
 
     return result;
 }
@@ -2639,6 +2723,9 @@ int bcmCheckForRedirect(void)
    FILE* fs;  
    int count = 0;
 
+   if (bcmIsModuleInserted("iptable_nat") == FALSE)
+      return FALSE;
+
    bcmSystem("iptables -t nat -L > /var/nat_redirect");
 
    fs = fopen("/var/nat_redirect", "r");
@@ -2672,10 +2759,8 @@ void bcmRemoveModules(int lanIf)
 {
    char *modList[]=
    {
-      "bcm_usb",
-#ifndef SUPPORT_ETHWAN
       "bcm_enet",
-#endif
+      "bcm_usb",
       "ipt_state",
       "ipt_mark",
       "ipt_limit",
@@ -2715,9 +2800,17 @@ void bcmRemoveModules(int lanIf)
 
    if (lanIf == 0)         // if lan, do not kill bcm_usb and bcm_enet
       i = 2;
+   else // if in ipow mode, leave bcm_enet out   
+   {
+      FILE *fs = fopen("/proc/var/fyi/wan/eth0/pid", "r");
+      if (fs != NULL) 
+      {
+         i = 1;        
+         fclose(fs);
+      }
+   }
 
    saveNat = bcmCheckForRedirect();
-
    if (bcmIsModuleInserted("iptable_filter") == TRUE)
    {  
        strncpy(cmd, "iptables -t filter -F", SYS_CMD_LEN-1);
@@ -2954,35 +3047,64 @@ void bcmAddTrafficControlRules(UINT16 vpi, UINT16 vci, UINT16 conId, UINT8 proto
     // Before we do anythng, lets add an ebtable rule at the bottom, that
     // marks the packets with low priority mark as default 0x0001.
     sprintf(cmd, "ebtables -t broute -A BROUTING -j mark --set-mark 0x0001 -p IPv4");
-    bcmSystem(cmd);
+    bcmSystemNoHang(cmd);
 
     // Create the root. This also creates the classes 1:1, 1:2 and 1:3
     // automatically
     sprintf(cmd, "tc qdisc add dev %s root handle 1: htb default 1", ifc);
-    bcmSystem(cmd);
+    bcmSystemNoHang(cmd);
     sprintf(cmd, "tc class add dev %s parent 1: classid 1:1 htb rate %lukbit",
             ifc, adslInfo.ulInterleavedUpStreamRate);
-    bcmSystem(cmd);
+    bcmSystemNoHang(cmd);
     sprintf(cmd, "tc qdisc add dev %s parent 1:1 handle 10: prio bands 3 "
             "priomap 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", ifc);
-    bcmSystem(cmd);
+    bcmSystemNoHang(cmd);
     // Create the htb's under each class.
     sprintf(cmd, "tc qdisc add dev %s parent 10:1 handle 100: pfifo limit 10", ifc);
-    bcmSystem(cmd);
+    bcmSystemNoHang(cmd);
     sprintf(cmd, "tc qdisc add dev %s parent 10:2 handle 200: pfifo limit 10", ifc);
-    bcmSystem(cmd);
+    bcmSystemNoHang(cmd);
     sprintf(cmd, "tc qdisc add dev %s parent 10:3 handle 300: pfifo limit 10", ifc);
-    bcmSystem(cmd);
+    bcmSystemNoHang(cmd);
     // Now add the filters for each sfq using the default handles.
     sprintf(cmd, "tc filter add dev %s protocol ip parent 10:0 prio 1 handle %d fw classid 10:1",
             ifc, PRIORITY_HIGH);
-    bcmSystem(cmd);
+    bcmSystemNoHang(cmd);
     sprintf(cmd, "tc filter add dev %s protocol ip parent 10:0 prio 1 handle %d fw classid 10:2",
             ifc, PRIORITY_MEDIUM);
-    bcmSystem(cmd);
+    bcmSystemNoHang(cmd);
     sprintf(cmd, "tc filter add dev %s protocol ip parent 10:0 prio 1 handle %d fw classid 10:3",
             ifc, PRIORITY_LOW);
-    bcmSystem(cmd);
+    bcmSystemNoHang(cmd);
 }
+
+/* this function stores device into procfs file for other processes to access */
+void bcmStoreDeviceInfoToProcFile(void)
+{
+   FILE* fs;
+   void *info=NULL;
+   PBcmCfm_DevInfoCfg_t pDevInfo = NULL;
+
+   bcmSystemMute("mkdir -p /var/fyi/sys");
+   bcmSystemMute("echo > /var/fyi/sys/info");
+   fs = fopen("/var/fyi/sys/info", "w+");
+
+   /* OUI %s */
+   /* SerialNumber %s */
+   /* ProductClass %s */
+   if ( fs != NULL ) {
+     if (BcmCfm_objGet(BCMCFM_OBJ_SYS_DEVICE_INFO, &info, 0) == BcmCfm_Ok) {
+       pDevInfo = (PBcmCfm_DevInfoCfg_t)info;
+       if (pDevInfo != NULL) {
+         fprintf(fs,"OUI %s\n",pDevInfo->manufacturerOui);
+         fprintf(fs,"SerialNumber %s\n",pDevInfo->serialNumber);
+         fprintf(fs,"ProductClass %s\n",pDevInfo->productClass);
+         BcmCfm_objFree(BCMCFM_OBJ_SYS_DEVICE_INFO, info);
+       }
+     }
+     fclose(fs);
+   } /* fs != NULL */
+}
+
 #endif