summaryrefslogtreecommitdiff
path: root/release/src/router/shared/dynamic_route.c
blob: f70cb67d4e7738956d8bdb757b586cf005607574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

/*
 *********************************************************
 *   Copyright 2003, CyberTAN  Inc.  All Rights Reserved *
 *********************************************************

 This is UNPUBLISHED PROPRIETARY SOURCE CODE of CyberTAN Inc.
 the contents of this file may not be disclosed to third parties,
 copied or duplicated in any form without the prior written
 permission of CyberTAN Inc.

 This software should be used as a reference only, and it not
 intended for production use!


 THIS SOFTWARE IS OFFERED "AS IS", AND CYBERTAN GRANTS NO WARRANTIES OF ANY
 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE.  CYBERTAN
 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/route.h>

#include <broadcom.h>


/* Dump route in <tr><td>IP</td><td>MASK</td><td>GW</td><td>Hop Count</td><td>interface</td></tr> format */
int
ej_dump_route_table(int eid, webs_t wp, int argc, char_t **argv)
{
	int ret = 0;
	int count = 0;
	char *format;
	FILE *fp, *fp1;
	int flgs, ref, use, metric;
        unsigned long dest, gw, netmask;
	char line[256];
	struct in_addr dest_ip;
        struct in_addr gw_ip;
        struct in_addr netmask_ip;
	char sdest[16], sgw[16];
	int debug = 0, blank = 1;

	if (ejArgs(argc, argv, "%s", &format) < 1) {
		websError(wp, 400, "Insufficient args\n");
		return -1;
	}

	/* open route table*/
	if ((fp = fopen("/tmp/routed_gb", "r")) == NULL) {
		if ((fp = fopen("/proc/net/route", "r")) == NULL) {
       			websError(wp, 400, "No route table\n");
        		return -1;
		}
	}

        /* Read the route cache entries. */
	//Iface   Destination     Gateway         Flags   RefCnt  Use     Metric  Mask            MTU     Window  IRTT                                                       
	//vmnet1  004410AC        00000000        0001    0       0       0       00FFFFFF        40      0       0                   

	while( fgets(line, sizeof(line), fp) != NULL ) {
		if(count) {
			int ifl = 0;
			while(line[ifl]!=' ' && line[ifl]!='\t' && line[ifl]!='\0')
			ifl++;
			line[ifl]=0;    /* interface */
			if(sscanf(line+ifl+1, "%lx%lx%X%d%d%d%lx", &dest, &gw, &flgs, &ref, &use, &metric, &netmask)!=7) {
				break;
		        }  
			debug = 0;
			dest_ip.s_addr = dest;
			gw_ip.s_addr   = gw;
			netmask_ip.s_addr = netmask;

			strcpy(sdest,  (dest_ip.s_addr==0 ? "0.0.0.0" : inet_ntoa(dest_ip)));    //default
			strcpy(sgw,    (gw_ip.s_addr==0   ? "0.0.0.0" : inet_ntoa(gw_ip)));      //*

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): route table[%d] dest[%lx][%s] gw[%lx][%s] flags[%X] ref[%d] use[%d] metric[%d] netmask[%lx][%s] [%s]",__FUNCTION__,count,dest,sdest,gw,sgw,flgs,ref,use,metric,netmask,inet_ntoa(netmask_ip),(flgs&RTF_UP ? "" : "deleted"));
#endif
		
			/* not 0x0001 route usable */
			if(!(flgs&RTF_UP))	continue; 

			/* filter ppp pseudo interface for DOD */
			if(!strcmp(sdest,PPP_PSEUDO_IP) && !strcmp(sgw,PPP_PSEUDO_GW))	debug = 1;
			
			/* Don't show loopback device */
			if(!strcmp(line,"lo"))	debug = 1;

			/* Don't show eth1 information for pppoe mode */
			if(!strcmp(line, nvram_safe_get("wan_ifname")) && nvram_match("wan_proto","pppoe"))	debug = 1;

#ifdef UNNUMBERIP_SUPPORT
			/* Don't show eth1 information for Unnumber ip mode */
                        if(!strcmp(line, nvram_safe_get("wan_ifname")) && nvram_match("wan_proto","unnumberip")) debug = 1;
#endif
#ifdef WAN_AUTO_DETECT_SUPPORT
			/* Don't show eth1 information for pppoe mode */
                        if(!strcmp(line, nvram_safe_get("wan_ifname")) && nvram_match("wan_proto","auto_pppoe")) debug = 1;
#endif

#if 1
			/* Don't show pseudo interface */
                        if(!strncmp(line, nvram_safe_get("pppoe_ifname0"), 4)){
                                fp1 = fopen("/tmp/ppp/link", "r");
                                if(!fp1)        debug = 1;
                                else            fclose(fp1);
                        }

			/* Don't show pseudo interface */
                        if(!strncmp(line, nvram_safe_get("pppoe_ifname1"), 4)){
                                fp1 = fopen("/tmp/ppp/link_1", "r");
                                if(!fp1)        debug = 1;
                                else            fclose(fp1);
                        }
#else
			/* Don't show pseudo interface */
			if(!strncmp(line, "ppp", 3)){
				fp1 = fopen("/tmp/ppp/link", "r");
				if(!fp1)	debug = 1;
				else		fclose(fp1);
			}
#endif

			ret += websWrite(wp,"%s%c'%s','%s','%s','%d','%s'\n",
					debug ? "//" : "",
					blank ? ' ' : ',', 
					sdest,
					inet_ntoa(netmask_ip),
					sgw,
                                        ((metric + 1) > 16) ? 16 : (metric + 1),//2006.10.09 add by zg
					(strcmp(line,"br0") ? "WAN" : "LAN"));    

			if(debug && blank)	blank = 1;
			else		blank = 0;


		}

		count++;
	}

	return ret;
}

void
validate_dynamic_route(webs_t wp, char *value, struct variable *v)
{
	struct variable dr_variables[] = {
		{ longname: "Danamic Route", argv: ARGV("0", "1", "2", "3") },
	}, *which;
	char *dr_setting;
	
	which = &dr_variables[0];

	if (valid_choice(wp, value, v)) 
		nvram_set(v->name, value);

	dr_setting = websGetVar(wp, "dr_setting", NULL);
	if(!dr_setting)	return;

	if (!valid_choice(wp, dr_setting, &which[0])) 
		return;
		nvram_set("dr_setting",dr_setting);
	if(!dr_setting || atoi(dr_setting) == 0){
		nvram_set("dr_lan_tx","0");
		nvram_set("dr_lan_rx","0");
		nvram_set("dr_wan_tx","0");
		nvram_set("dr_wan_rx","0");
	}
	else if(atoi(dr_setting) == 1){
		nvram_set("dr_lan_tx","1 2");
		nvram_set("dr_lan_rx","1 2");
		nvram_set("dr_wan_tx","0");
		nvram_set("dr_wan_rx","0");
	}
	else if(atoi(dr_setting) == 2){
		nvram_set("dr_lan_tx","0");
		nvram_set("dr_lan_rx","0");
		nvram_set("dr_wan_tx","1 2");
		nvram_set("dr_wan_rx","1 2");
	}
	else if(atoi(dr_setting) == 3){ 
		nvram_set("dr_lan_tx","1 2");
		nvram_set("dr_lan_rx","1 2");
		nvram_set("dr_wan_tx","1 2");
		nvram_set("dr_wan_rx","1 2");
	}
	else{
		nvram_set("dr_lan_tx","0");
		nvram_set("dr_lan_rx","0");
		nvram_set("dr_wan_tx","0");
		nvram_set("dr_wan_rx","0");
	}

}