1/* 2 * Copyright 2004, ASUSTek Inc. 3 * All Rights Reserved. 4 * 5 * THIS SOFTWARE IS OFFERED "AS IS", AND ASUS GRANTS NO WARRANTIES OF ANY 6 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM 7 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS 8 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE. 9 * 10 * $Id: network_ex.c,v 1.3 2009/03/03 02:39:54 james26_jang Exp $ 11 */ 12 13#include <stdio.h> 14#include <stdlib.h> 15#include <errno.h> 16#include <syslog.h> 17#include <ctype.h> 18#include <string.h> 19#include <unistd.h> 20#include <sys/stat.h> 21#include <sys/ioctl.h> 22#include <sys/types.h> 23#include <sys/socket.h> 24#include <net/if.h> 25#include <netinet/in.h> 26#include <arpa/inet.h> 27#include <net/if_arp.h> 28#include <signal.h> 29 30#include <bcmnvram.h> 31#include <netconf.h> 32#include <shutils.h> 33#include <wlutils.h> 34#include <nvparse.h> 35#include <rc.h> 36#include <bcmutils.h> 37 38#ifdef REMOVE 39int start_pppoe(void) 40{ 41 42 int timeout = 5; 43 char pppunit[] = "XXXXXXXXXXXX"; 44 45 /* Add optional arguments */ 46 for (arg = pppoe_argv; *arg; arg++); 47 if (nvram_invmatch(strcat_r(prefix, "pppoe_service", tmp), "")) { 48 *arg++ = "-s"; 49 *arg++ = nvram_safe_get(strcat_r(prefix, "pppoe_service", tmp)); 50 } 51 if (nvram_invmatch(strcat_r(prefix, "pppoe_ac", tmp), "")) { 52 *arg++ = "-a"; 53 *arg++ = nvram_safe_get(strcat_r(prefix, "pppoe_ac", tmp)); 54 } 55 if (nvram_match(strcat_r(prefix, "pppoe_demand", tmp), "1") || 56 nvram_match(strcat_r(prefix, "pppoe_keepalive", tmp), "1")) 57 *arg++ = "-k"; 58 snprintf(pppunit, sizeof(pppunit), "%d", unit); 59 *arg++ = "-U"; 60 *arg++ = pppunit; 61 62 /* launch pppoe client daemon */ 63 64 65 /* ppp interface name is referenced from this point on */ 66 wan_ifname = nvram_safe_get(strcat_r(prefix, "pppoe_ifname", tmp)); 67 68 /* Pretend that the WAN interface is up */ 69 if (nvram_match(strcat_r(prefix, "pppoe_demand", tmp), "1")) { 70 /* Wait for pppx to be created */ 71 while (ifconfig(wan_ifname, IFUP, NULL, NULL) && timeout--) 72 sleep(1); 73 74 /* Retrieve IP info */ 75 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) 76 continue; 77 strncpy(ifr.ifr_name, wan_ifname, IFNAMSIZ); 78 79 /* Set temporary IP address */ 80 if (ioctl(s, SIOCGIFADDR, &ifr)) 81 perror(wan_ifname); 82 nvram_set(strcat_r(prefix, "ipaddr", tmp), inet_ntoa(sin_addr(&ifr.ifr_addr))); 83 nvram_set(strcat_r(prefix, "netmask", tmp), "255.255.255.255"); 84 85 /* Set temporary P-t-P address */ 86 if (ioctl(s, SIOCGIFDSTADDR, &ifr)) 87 perror(wan_ifname); 88 nvram_set(strcat_r(prefix, "gateway", tmp), inet_ntoa(sin_addr(&ifr.ifr_dstaddr))); 89 90 close(s); 91 92 /* 93 * Preset routes so that traffic can be sent to proper pppx even before 94 * the link is brought up. 95 */ 96 preset_wan_routes(wan_ifname); 97} 98#endif 99 100int start_pppd(char *prefix) 101{ 102 int ret; 103 FILE *fp; 104 char options[80]; 105 char *pppd_argv[] = { "pppd", "file", options, NULL}; 106 char tmp[100]; 107 mode_t mask; 108 109 sprintf(options, "/tmp/ppp/options.wan%s", nvram_safe_get(strcat_r(prefix, "unit", tmp))); 110 111 mask = umask(066); 112 113 /* Generate options file */ 114 if (!(fp = fopen(options, "w"))) { 115 perror(options); 116 umask(mask); 117 return -1; 118 } 119 120 umask(mask); 121 122 /* do not authenticate peer and do not use eap */ 123 fprintf(fp, "noauth refuse-eap\n"); 124 fprintf(fp, "user '%s'\n", nvram_safe_get(strcat_r(prefix, "pppoe_username", tmp))); 125 fprintf(fp, "password '%s'\n", nvram_safe_get(strcat_r(prefix, "pppoe_passwd", tmp))); 126 127 if (nvram_match(strcat_r(prefix, "proto", tmp), "pptp")) 128 { 129 /* 130 fprintf(fp, "connect true\n"); 131 fprintf(fp, "sync pty '/usr/sbin/pptp %s --nolaunchpppd --nobuffer --sync'\n", 132 nvram_invmatch("wan_heartbeat_x", "")?nvram_safe_get("wan_heartbeat_x"):nvram_safe_get(strcat_r(prefix, "pppoe_gateway", tmp))); 133 fprintf(fp, "lock\n"); 134 */ 135 fprintf(fp, "plugin pptp.so\n"); 136 fprintf(fp, "pptp_server '%s'\n", 137 nvram_invmatch("wan_heartbeat_x", "")?nvram_safe_get("wan_heartbeat_x"):nvram_safe_get(strcat_r(prefix, "pppoe_gateway", tmp)));//*/ 138 139 /* see KB Q189595 -- historyless & mtu */ 140 fprintf(fp, "nomppe-stateful %s mtu 1400\n", nvram_safe_get(strcat_r(prefix, "pptp_options_x", tmp))); 141 } 142 else { 143 fprintf(fp, "nomppe nomppc\n"); 144 } 145 146 if (nvram_match(strcat_r(prefix, "proto", tmp), "pppoe")) 147 { 148 fprintf(fp, "plugin rp-pppoe.so"); 149 150 if (nvram_invmatch(strcat_r(prefix, "pppoe_service", tmp), "")) { 151 fprintf(fp, " rp_pppoe_service '%s'\n", 152 nvram_safe_get(strcat_r(prefix, "pppoe_service", tmp))); 153 } 154 155 if (nvram_invmatch(strcat_r(prefix, "pppoe_ac", tmp), "")) { 156 fprintf(fp, " rp_pppoe_ac '%s'\n", 157 nvram_safe_get(strcat_r(prefix, "pppoe_ac", tmp))); 158 } 159 160 fprintf(fp, " nic-%s\n", nvram_safe_get(strcat_r(prefix, "ifname", tmp))); 161 162 fprintf(fp, "mru %s mtu %s\n", 163 nvram_safe_get(strcat_r(prefix, "pppoe_mru", tmp)), 164 nvram_safe_get(strcat_r(prefix, "pppoe_mtu", tmp))); 165 } 166 167 if (atoi(nvram_safe_get(strcat_r(prefix, "pppoe_idletime", tmp))) && 168 nvram_match(strcat_r(prefix, "pppoe_demand", tmp), "1")) 169 { 170 fprintf(fp, "idle %s ", nvram_safe_get(strcat_r(prefix, "pppoe_idletime", tmp))); 171 if (nvram_invmatch(strcat_r(prefix, "pppoe_txonly_x", tmp), "0")) { 172 fprintf(fp, "tx_only "); 173 } 174 fprintf(fp, "demand\n"); 175 } 176 177 fprintf(fp, "maxfail 0\n"); 178 179 if(nvram_invmatch(strcat_r(prefix, "dnsenable_x", tmp), "0")) 180 fprintf(fp, "usepeerdns\n"); 181 182 if (nvram_invmatch(strcat_r(prefix, "proto", tmp), "l2tp")) 183 fprintf(fp, "persist\n"); 184 185 fprintf(fp, "ipcp-accept-remote ipcp-accept-local noipdefault\n"); 186 fprintf(fp, "ktune\n"); 187 188 /* pppoe set these options automatically */ 189 /* looks like pptp also likes them */ 190 fprintf(fp, "default-asyncmap nopcomp noaccomp\n"); 191 192 /* pppoe disables "vj bsdcomp deflate" automagically */ 193 /* ccp should still be enabled - mppe/mppc requires this */ 194 fprintf(fp, "novj nobsdcomp nodeflate\n"); 195 196 /* echo failures */ 197 fprintf(fp, "lcp-echo-interval 10\n"); 198 fprintf(fp, "lcp-echo-failure 6\n"); 199 200 fprintf(fp, "unit %s\n", nvram_get(strcat_r(prefix, "unit", tmp)) ? : "0"); 201 202 /* user specific options */ 203 fprintf(fp, "%s\n", nvram_safe_get(strcat_r(prefix, "pppoe_options_x", tmp))); 204 205 fclose(fp); 206 207 if (nvram_match(strcat_r(prefix, "proto", tmp), "l2tp")) 208 { 209 if (!(fp = fopen("/tmp/l2tp.conf", "w"))) { 210 perror(options); 211 return -1; 212 } 213 214 fprintf(fp, "# automagically generated\n" 215 "global\n\n" 216 "load-handler \"sync-pppd.so\"\n" 217 "load-handler \"cmd.so\"\n\n" 218 "section sync-pppd\n\n" 219 "lac-pppd-opts \"file %s\"\n\n" 220 "section peer\n" 221 "peername %s\n" 222 "lac-handler sync-pppd\n" 223 "persist yes\n" 224 "maxfail %s\n" 225 "holdoff %s\n" 226 "section cmd\n\n", 227 options, 228 nvram_invmatch("wan_heartbeat_x", "")?nvram_safe_get("wan_heartbeat_x"):nvram_safe_get(strcat_r(prefix, "pppoe_gateway", tmp)), 229 nvram_invmatch(strcat_r(prefix, "pppoe_maxfail", tmp), "")?nvram_safe_get(strcat_r(prefix, "pppoe_maxfail", tmp)):"32767", 230 nvram_invmatch(strcat_r(prefix, "pppoe_holdoff", tmp), "")?nvram_safe_get(strcat_r(prefix, "pppoe_holdoff", tmp)):"30"); 231 232 fclose(fp); 233 234 /* launch l2tp */ 235 eval("l2tpd"); 236 237 sleep(1); 238 239 /* start-session */ 240 ret = eval("l2tp-control", "start-session 0.0.0.0"); 241 242 /* pppd sync nodetach noaccomp nobsdcomp nodeflate */ 243 /* nopcomp novj novjccomp file /tmp/ppp/options.l2tp */ 244 } else{ 245 eval("killall", "pppd"); 246 ret = _eval(pppd_argv, NULL, 0, NULL); 247 } 248 249 return ret; 250} 251 252void start_pppoe_relay(char *wan_if) 253{ 254 if (nvram_match("wan_pppoe_relay_x", "1")) 255 { 256 char *pppoerelay_argv[] = {"pppoe-relay", "-C", "br0", "-S", wan_if, NULL}; 257 int ret; 258 pid_t pid; 259 260 ret = _eval(pppoerelay_argv, NULL, 0, &pid); 261 } 262} 263 264void setup_ethernet(char *wan_if) 265{ 266 //if (nvram_invmatch("wan_etherspeed_x", "auto")) 267} 268 269#ifdef CDMA 270int write_cdma_conf(){ 271 FILE *fp; 272 char username[32], passwd[32]; 273 char ttyUSB_num_str[2]; 274 int ttyUSB_num = -1; 275 276 /* Get user name and password*/ 277 memset(username, 0, 32); 278 strcpy(username, nvram_safe_get("hsdpa_username")); 279 memset(passwd, 0, 32); 280 strcpy(passwd, nvram_safe_get("hsdpa_passwd")); 281 282 /* Setting pppd config file */ 283 if(!(fp = fopen("/tmp/ppp/peers/cdma", "w"))){ 284 perror("/tmp/ppp/peers/cdma"); 285 return -1; 286 } 287 288#ifdef WCDMA 289// fprintf(fp, "-detach\n"); 290 if(nvram_match("hsdpa_combo", "1")){ 291 memset(ttyUSB_num_str, 0, 2); 292 strcpy(ttyUSB_num_str, nvram_safe_get("hsdpa_ttyUSB_num")); 293 ttyUSB_num = atoi(ttyUSB_num_str); 294 295 if(!strcmp(nvram_safe_get("got_HSDPA"), "1")){ 296 ; 297 } 298 else if(strlen(ttyUSB_num_str) > 0){ 299 ++ttyUSB_num; 300 ttyUSB_num %= 4; 301 ttyUSB_num_str[0] = '0'+ttyUSB_num; 302 nvram_set("hsdpa_ttyUSB_num", ttyUSB_num_str); 303 } 304 else{ 305 ttyUSB_num = 0; 306 nvram_set("hsdpa_ttyUSB_num", "0"); 307 } 308printf("--- ttyUSB_num=%d. ---\n", ttyUSB_num); 309 fprintf(fp, "/dev/ttyUSB%d\n", ttyUSB_num); 310 } 311 else 312 fprintf(fp, "/dev/ttyACM0\n"); 313 //fprintf(fp, "460800\n"); 314 fprintf(fp, "%s\n", nvram_safe_get("hsdpa_bitrate")); 315 fprintf(fp, "modem\n"); 316 //fprintf(fp, "noauth\n"); 317 if(strcmp(nvram_safe_get("hsdpa_authtype"), "") != 0) 318 fprintf(fp, "auth +%s\n", nvram_safe_get("hsdpa_authtype")); 319 else 320 fprintf(fp, "noauth\n");//*/ 321 fprintf(fp, "defaultroute\n"); 322 fprintf(fp, "noipdefault\n"); 323 if(nvram_match("hsdpa_dnsenable_x", "1")) 324 fprintf(fp, "usepeerdns\n"); 325// fprintf(fp, "-detach\n"); 326// fprintf(fp, "usehostname\n"); 327 fprintf(fp, "debug\n"); 328// fprintf(fp, "local\n"); 329 if(strlen(username) > 0 || strlen(passwd) > 0){ 330 fprintf(fp, "user %s\n", username); 331 fprintf(fp, "password %s\n", passwd); 332 } 333// fprintf(fp, "persist\n"); 334// fprintf(fp, "show-password\n"); 335 fprintf(fp, "crtscts\n"); 336 fprintf(fp, "noccp\n"); 337 fprintf(fp, "novj\n"); 338 fprintf(fp, "nodeflate\n"); 339 fprintf(fp, "nobsdcomp\n"); 340 fprintf(fp, "connect-delay 5000\n"); 341 //fprintf(fp, "connect '/usr/sbin/chat -vf /tmp/ppp/peers/cdma_chat'\n"); 342 fprintf(fp, "connect '/usr/sbin/chat -svf /tmp/ppp/peers/cdma_chat'\n"); 343 fprintf(fp, "mru %s\n", nvram_safe_get("hsdpa_mru")); 344 fprintf(fp, "mtu %s\n", nvram_safe_get("hsdpa_mtu")); 345// fprintf(fp, "disconnect '/usr/sbin/chat -vf /tmp/ppp/peers/cdma_disconnect'\n"); 346 fprintf(fp, "lcp-echo-interval 10\n"); 347 fprintf(fp, "lcp-echo-failure 6\n"); 348#else 349 fprintf(fp, "%s\n", nvram_safe_get("hsdpa_bitrate")); 350 fprintf(fp, "connect '/usr/sbin/chat -s -v \"\" \"ATEV1&F&D2&C1S0=0\" OK \"ATX1\" OK AT OK \"ATDT%s\" CONNECT'\n", nvram_safe_get("hsdpa_dial_number")); 351 fprintf(fp, "debug\n"); 352 fprintf(fp, "updetach\n"); 353 fprintf(fp, "crtscts\n"); 354 fprintf(fp, "noipdefault\n"); 355 fprintf(fp, "novj\n"); 356 fprintf(fp, "lcp-echo-failure 0\n"); 357 fprintf(fp, "nobsdcomp\n"); 358 fprintf(fp, "novjccomp\n"); 359 fprintf(fp, "nopcomp\n"); 360 fprintf(fp, "noaccomp\n"); 361 fprintf(fp, "noauth\n"); 362 fprintf(fp, "user mobile\n"); 363 fprintf(fp, "modem /dev/ttyUSB0\n"); 364 fprintf(fp, "usepeerdns\n"); 365 fprintf(fp, "defaultroute\n"); 366 fprintf(fp, "connect-delay 5000\n"); 367#endif 368 369 fclose(fp); 370 371 /* Create auth file */ 372 if(!(fp = fopen("/tmp/ppp/chap-secrets", "w"))){ 373 perror("/tmp/ppp/chap-secrets"); 374 return -1; 375 } 376 377 fprintf(fp, "# Secrets for authentication using CHAP\n"); 378 if(strlen(username) > 0 || strlen(passwd) > 0) 379 fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd); 380 381 fclose(fp); 382 383 /* Writing chat file */ 384 if(!(fp = fopen("/tmp/ppp/peers/cdma_chat", "w"))){ 385 perror("/tmp/ppp/peers/cdma_chat"); 386 return -1; 387 } 388 389#ifdef WCDMA 390// fprintf(fp, "'' 'AT'\n"); 391// fprintf(fp, "'OK' 'ATE0V1&F&D2&C1&C2S0=0'\n"); 392// fprintf(fp, "'OK' 'ATE0V1'\n"); 393// fprintf(fp, "'OK' 'ATS7=60'\n"); 394// fprintf(fp, "'OK' 'ATDT#777'\n"); 395// fprintf(fp, "ABORT BUSY\n"); 396// fprintf(fp, "ABORT ERROR\n"); 397// fprintf(fp, "ABORT VOICE\n"); 398 fprintf(fp, "ABORT 'NO CARRIER'\n"); 399 fprintf(fp, "ABORT 'NO DAILTONE'\n"); 400// fprintf(fp, "ABORT 'NO DAIL TONE'\n"); 401// fprintf(fp, "ABORT 'NO ANSWER'\n"); 402// fprintf(fp, "REPORT CONNECT\n"); 403// fprintf(fp, "TIMEOUT 10\n"); 404// fprintf(fp, "OK ATZ\n"); 405 fprintf(fp, "\"\" AT\n"); 406 fprintf(fp, "OK AT+CFUN=1\n"); 407 //if(nvram_match("enable_apn", "1")) 408 if(strcmp(nvram_safe_get("private_apn"), "")) 409 fprintf(fp, "OK \'AT+CGDCONT=1, \"ip\",\"%s\"\'\n", nvram_safe_get("private_apn")); 410// fprintf(fp, "OK \'AT+CGDCONT=1, \"ip\",\"internet\"\'\n"); 411 /*if(nvram_match("pin_code_enable", "1")) 412 fprintf(fp, "OK AT+CPIN=\"%s\"\n", nvram_safe_get("pin_code"));//*/ 413// if(nvram_invmatch("wan_pppoe_passwd", "")) 414// fprintf(fp, "OK AT+CPIN=\"%s\"\n", passwd); 415// fprintf(fp, "\\d \\d \\d \\d \\d \n"); 416// fprintf(fp, "OK ATQ0V1E1S0=0&C1&D2+FCLASS=0\n"); 417 fprintf(fp, "OK ATQ0V1E1S0=0&C1&D2\n"); 418// fprintf(fp, "OK \"AT+IPR=115200\"\n"); 419// fprintf(fp, "OK \"ATE1\"\n"); 420// fprintf(fp, "TIMEOUT 60\n"); 421 fprintf(fp, "\"\" ATDT%s\n", nvram_safe_get("hsdpa_dial_number")); 422 fprintf(fp, "CONNECT \"\"\n"); 423// fprintf("\n"); 424#else 425 fprintf(fp, "'' ''\n"); 426 fprintf(fp, "'' 'ATZ'\n"); 427 fprintf(fp, "'OK' 'ATI'\n"); 428 fprintf(fp, "'OK' 'ATDT%s'\n", nvram_safe_get("hsdpa_dial_number")); 429 fprintf(fp, "'CONNECT' 'ATO'\n"); 430#endif 431 432 fclose(fp); 433 434 return 0; 435} 436 437int start_cdma(void) // HSDPA 438{ 439 char *cdma_argv[] = {"pppd", "call", "cdma", NULL}; 440 441 write_cdma_conf(); 442 443 eval("killall", "pppd"); 444 445 // Call CMDA connection 446csprintf("--- Wait to start HSDPA... ---\n"); 447 //sleep(10); 448 sleep(2); 449csprintf("--- Start HSDPA... ---\n"); 450 _eval(cdma_argv, NULL, 0, NULL); 451 452 return 0; 453} 454 455int 456stop_cdma(void) 457{ 458 int ret; 459 460 ret = eval("killall", "pppd"); 461 ret += eval("killall", "chat"); 462 dprintf("done\n"); 463 464 return ret; 465} 466#endif 467