1/*
2
3	Copyright 2005, Broadcom Corporation
4	All Rights Reserved.
5
6	THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
7	KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
8	SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
9	FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
10
11*/
12
13#include "rc.h"
14
15#include <termios.h>
16#include <dirent.h>
17#include <sys/ioctl.h>
18#include <sys/mount.h>
19#include <time.h>
20#include <errno.h>
21#include <paths.h>
22#include <sys/wait.h>
23#include <sys/reboot.h>
24#include <sys/klog.h>
25#ifdef LINUX26
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <sys/sysinfo.h>
29#endif
30#include <wlutils.h>
31#include <bcmdevs.h>
32
33#include <shared.h>
34
35#ifdef RTCONFIG_RALINK
36#include <ralink.h>
37#include <flash_mtd.h>
38#endif
39
40#ifdef RTCONFIG_RALINK_RT3052
41#include <ra3052.h>
42#endif
43
44void init_devs(void)
45{
46#define MKNOD(name,mode,dev)	if(mknod(name,mode,dev)) perror("## mknod " name)
47
48#if defined(LINUX30) && !defined(RTN14U) && !defined(RTAC52U) && !defined(RTAC51U) && !defined(RTN11P) && !defined(RTN300) && !defined(RTN54U) && !defined(RTAC1200HP) && !defined(RTN56UB1) && !defined(RTN56UB2) && !defined(RTAC54U)
49	/* Below device node are used by proprietary driver.
50	 * Thus, we cannot use GPL-only symbol to create/remove device node dynamically.
51	 */
52	MKNOD("/dev/swnat0", S_IFCHR | 0666, makedev(210, 0));
53	MKNOD("/dev/hwnat0", S_IFCHR | 0666, makedev(220, 0));
54	MKNOD("/dev/acl0", S_IFCHR | 0666, makedev(230, 0));
55	MKNOD("/dev/ac0", S_IFCHR | 0666, makedev(240, 0));
56	MKNOD("/dev/mtr0", S_IFCHR | 0666, makedev(250, 0));
57	MKNOD("/dev/rtkswitch", S_IFCHR | 0666, makedev(206, 0));
58	MKNOD("/dev/nvram", S_IFCHR | 0666, makedev(228, 0));
59#else
60	MKNOD("/dev/video0", S_IFCHR | 0666, makedev(81, 0));
61#if !defined(RTN14U) && !defined(RTAC52U) && !defined(RTAC51U) && !defined(RTN11P) && !defined(RTN300) && !defined(RTN54U) && !defined(RTAC1200HP) && !defined(RTN56UB1) && !defined(RTN56UB2) && !defined(RTAC54U)
62	MKNOD("/dev/rtkswitch", S_IFCHR | 0666, makedev(206, 0));
63#endif
64	MKNOD("/dev/spiS0", S_IFCHR | 0666, makedev(217, 0));
65	MKNOD("/dev/i2cM0", S_IFCHR | 0666, makedev(218, 0));
66#if defined(RTN14U) || defined(RTAC52U) || defined(RTAC51U) || defined(RTN11P) || defined(RTN300) || defined(RTN54U) || defined(RTAC1200HP) || defined(RTN56UB1) || defined(RTN56UB2) || defined(RTAC54U)
67#else
68	MKNOD("/dev/rdm0", S_IFCHR | 0666, makedev(254, 0));
69#endif
70	MKNOD("/dev/flash0", S_IFCHR | 0666, makedev(200, 0));
71	MKNOD("/dev/swnat0", S_IFCHR | 0666, makedev(210, 0));
72	MKNOD("/dev/hwnat0", S_IFCHR | 0666, makedev(220, 0));
73	MKNOD("/dev/acl0", S_IFCHR | 0666, makedev(230, 0));
74	MKNOD("/dev/ac0", S_IFCHR | 0666, makedev(240, 0));
75	MKNOD("/dev/mtr0", S_IFCHR | 0666, makedev(250, 0));
76	MKNOD("/dev/gpio0", S_IFCHR | 0666, makedev(252, 0));
77	MKNOD("/dev/nvram", S_IFCHR | 0666, makedev(228, 0));
78	MKNOD("/dev/PCM", S_IFCHR | 0666, makedev(233, 0));
79	MKNOD("/dev/I2S", S_IFCHR | 0666, makedev(234, 0));
80#endif
81	{
82		int status;
83		if((status = WEXITSTATUS(modprobe("nvram_linux"))))	printf("## modprove(nvram_linux) fail status(%d)\n", status);
84	}
85}
86
87//void init_gpio(void)
88//{
89//	ralink_gpio_init(0, GPIO_DIR_OUT); // Power
90//	ralink_gpio_init(13, GPIO_DIR_IN); // RESET
91//	ralink_gpio_init(26, GPIO_DIR_IN); // WPS
92//}
93
94void generate_switch_para(void)
95{
96	int model;
97	int wans_cap = get_wans_dualwan() & WANSCAP_WAN;
98	int wanslan_cap = get_wans_dualwan() & WANSCAP_LAN;
99
100	// generate nvram nvram according to system setting
101	model = get_model();
102
103	switch(model) {
104		case MODEL_RTN13U:
105			if(!is_routing_enabled()) {
106				// override boardflags with no VLAN flag
107				nvram_set_int("boardflags", nvram_get_int("boardflags")&(~BFL_ENETVLAN));
108				nvram_set("lan_ifnames", "eth2 ra0");
109			}
110			else if(nvram_match("switch_stb_x", "1")) {
111				nvram_set("vlan0ports", "0 1 2 5*");
112				nvram_set("vlan1ports", "3 4 5u");
113			}
114			else if(nvram_match("swtich_stb_x", "2")) {
115				nvram_set("vlan0ports", "0 1 3 5*");
116				nvram_set("vlan1ports", "2 4 5u");
117			}
118			else if(nvram_match("switch_stb_x", "3")) {
119				nvram_set("vlan0ports", "0 2 3 5*");
120				nvram_set("vlan1ports", "1 4 5u");
121			}
122			else if(nvram_match("switch_stb_x", "4")) {
123				nvram_set("vlan0ports", "1 2 3 5*");
124				nvram_set("vlan1ports", "0 4 5u");
125			}
126			else if(nvram_match("switch_stb_x", "5")) {
127				nvram_set("vlan0ports", "2 3 5*");
128				nvram_set("vlan1ports", "0 1 4 5u");
129			}
130			else {	// default for 0
131				nvram_set("vlan0ports", "0 1 2 3 5*");
132				nvram_set("vlan1ports", "4 5u");
133			}
134			break;
135		case MODEL_RTN11P:	/* fall through */
136		case MODEL_RTN300:	/* fall through */
137		case MODEL_RTN14U:	/* fall through */
138		case MODEL_RTN54U:      /* fall through */
139		case MODEL_RTAC54U:      /* fall through */
140		case MODEL_RTN56UB1:      /* fall through */
141		case MODEL_RTN56UB2:      /* fall through */
142		case MODEL_RTAC1200HP:  /* fall through */
143		case MODEL_RTAC51U:	/* fall through */
144		case MODEL_RTAC52U:
145			nvram_unset("vlan3hwname");
146			if ((wans_cap && wanslan_cap) ||
147			    (wanslan_cap && (!nvram_match("switch_wantag", "none") && !nvram_match("switch_wantag", "")))
148			   )
149				nvram_set("vlan3hwname", "et0");
150			break;
151	}
152}
153
154static void init_switch_ralink(void)
155{
156	generate_switch_para();
157
158	// TODO: replace to nvram controlled procedure later
159	eval("ifconfig", "eth2", "hw", "ether", nvram_safe_get("et0macaddr"));
160#ifdef RTCONFIG_RALINK_RT3052
161	if(is_routing_enabled()) config_3052(nvram_get_int("switch_stb_x"));
162#else
163	if(strlen(nvram_safe_get("wan0_ifname"))) {
164		if (!nvram_match("et1macaddr", ""))
165			eval("ifconfig", nvram_safe_get("wan0_ifname"), "hw", "ether", nvram_safe_get("et1macaddr"));
166		else
167			eval("ifconfig", nvram_safe_get("wan0_ifname"), "hw", "ether", nvram_safe_get("et0macaddr"));
168	}
169#if defined(RTN56UB1) || defined(RTN56UB2) //workaround, let network device initialize before config_switch()
170	eval("ifconfig", "eth2", "up");
171	sleep(1);
172#endif
173	config_switch();
174#endif
175
176#ifdef RTCONFIG_SHP
177	if (nvram_get_int("qos_enable") == 1 || nvram_get_int("lfp_disable_force")) {
178		nvram_set("lfp_disable", "1");
179	} else {
180		nvram_set("lfp_disable", "0");
181	}
182
183	if(nvram_get_int("lfp_disable")==0) {
184		restart_lfp();
185	}
186#endif
187//	reinit_hwnat(-1);
188
189}
190
191void init_switch()
192{
193#ifdef RTCONFIG_DSL
194	init_switch_dsl();
195	config_switch_dsl();
196#else
197	init_switch_ralink();
198#endif
199}
200
201char *get_lan_hwaddr(void)
202{
203	/* TODO: handle exceptional model */
204        return nvram_safe_get("et0macaddr");
205}
206
207/**
208 * Setup a VLAN.
209 * @vid:	VLAN ID
210 * @prio:	VLAN PRIO
211 * @mask:	bit31~16:	untag mask
212 * 		bit15~0:	port member mask
213 * @return:
214 * 	0:	success
215 *  otherwise:	fail
216 *
217 * bit definition of untag mask/port member mask
218 * 0:	Port 0, LANx port which is closed to WAN port in visual.
219 * 1:	Port 1
220 * 2:	Port 2
221 * 3:	Port 3
222 * 4:	Port 4, WAN port
223 * 9:	Port 9, RGMII/MII port that is used to connect CPU and WAN port.
224 * 	a. If you only have one RGMII/MII port and it is shared by WAN/LAN ports,
225 * 	   you have to define two VLAN interface for WAN/LAN ports respectively.
226 * 	b. If your switch chip choose another port as same feature, convert bit9
227 * 	   to your own port in low-level driver.
228 */
229static int __setup_vlan(int vid, int prio, unsigned int mask)
230{
231	char vlan_str[] = "4096XXX";
232	char prio_str[] = "7XXX";
233	char mask_str[] = "0x00000000XXX";
234	char *set_vlan_argv[] = { "rtkswitch", "36", vlan_str , NULL };
235	char *set_prio_argv[] = { "rtkswitch", "37", prio_str , NULL };
236	char *set_mask_argv[] = { "rtkswitch", "39", mask_str , NULL };
237
238	if (vid > 4096) {
239		_dprintf("%s: invalid vid %d\n", __func__, vid);
240		return -1;
241	}
242
243	if (prio > 7)
244		prio = 0;
245
246	_dprintf("%s: vid %d prio %d mask 0x%08x\n", __func__, vid, prio, mask);
247
248	if (vid >= 0) {
249		sprintf(vlan_str, "%d", vid);
250		_eval(set_vlan_argv, NULL, 0, NULL);
251	}
252
253	if (prio >= 0) {
254		sprintf(prio_str, "%d", prio);
255		_eval(set_prio_argv, NULL, 0, NULL);
256	}
257
258	sprintf(mask_str, "0x%08x", mask);
259	_eval(set_mask_argv, NULL, 0, NULL);
260
261	return 0;
262}
263
264int config_switch_for_first_time = 1;
265void config_switch()
266{
267	int model = get_model();
268	int stbport;
269	int controlrate_unknown_unicast;
270	int controlrate_unknown_multicast;
271	int controlrate_multicast;
272	int controlrate_broadcast;
273	int merge_wan_port_into_lan_ports;
274
275	dbG("link down all ports\n");
276	eval("rtkswitch", "17");	// link down all ports
277
278	switch (model) {
279	case MODEL_RTN11P:	/* fall through */
280	case MODEL_RTN300:	/* fall through */
281	case MODEL_RTN14U:	/* fall through */
282	case MODEL_RTN36U3:	/* fall through */
283	case MODEL_RTN65U:	/* fall through */
284	case MODEL_RTN54U:
285	case MODEL_RTAC54U:
286	case MODEL_RTAC1200HP:
287	case MODEL_RTAC51U:	/* fall through */
288	case MODEL_RTAC52U:	/* fall through */
289	case MODEL_RTN56UB1:	/* fall through */
290	case MODEL_RTN56UB2:	/* fall through */
291		merge_wan_port_into_lan_ports = 1;
292		break;
293	default:
294		merge_wan_port_into_lan_ports = 0;
295	}
296
297	if (config_switch_for_first_time)
298		config_switch_for_first_time = 0;
299	else
300	{
301		dbG("software reset\n");
302		eval("rtkswitch", "27");	// software reset
303	}
304#if defined(RTN14U) || defined(RTAC52U) || defined(RTAC51U) || defined(RTN11P) || defined(RTN300) || defined(RTN54U) || defined(RTAC1200HP) || defined(RTN56UB1) || defined(RTAC54U) || defined(RTN56UB2)
305	system("rtkswitch 8 0"); //Barton add
306#endif
307
308	if (is_routing_enabled())
309	{
310		char parm_buf[] = "XXX";
311
312		stbport = nvram_get_int("switch_stb_x");
313		if (stbport < 0 || stbport > 6) stbport = 0;
314		dbG("ISP Profile/STB: %s/%d\n", nvram_safe_get("switch_wantag"), stbport);
315		/* stbport:	Model-independent	unifi_malaysia=1	otherwise
316		 * 		IPTV STB port		(RT-N56U)		(RT-N56U)
317		 * -----------------------------------------------------------------------
318		 *	0:	N/A			LLLLW
319		 *	1:	LAN1			LLLTW			LLLWW
320		 *	2:	LAN2			LLTLW			LLWLW
321		 *	3:	LAN3			LTLLW			LWLLW
322		 *	4:	LAN4			TLLLW			WLLLW
323		 *	5:	LAN1 + LAN2		LLTTW			LLWWW
324		 *	6:	LAN3 + LAN4		TTLLW			WWLLW
325		 */
326
327		if(!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", ""))//2012.03 Yau modify
328		{
329			char tmp[128];
330			char *p;
331			int voip_port = 0;
332			int t, vlan_val = -1, prio_val = -1;
333			unsigned int mask = 0;
334
335//			voip_port = nvram_get_int("voip_port");
336			voip_port = 3;
337			if (voip_port < 0 || voip_port > 4)
338				voip_port = 0;
339
340			/* Fixed Ports Now*/
341			stbport = 4;
342			voip_port = 3;
343
344			sprintf(tmp, "rtkswitch 29 %d", voip_port);
345			system(tmp);
346
347			if(!strncmp(nvram_safe_get("switch_wantag"), "unifi", 5)) {
348				/* Added for Unifi. Cherry Cho modified in 2011/6/28.*/
349				if(strstr(nvram_safe_get("switch_wantag"), "home")) {
350					system("rtkswitch 38 1");		/* IPTV: P0 */
351					/* Internet:	untag: P9;   port: P4, P9 */
352					__setup_vlan(500, 0, 0x02000210);
353					/* IPTV:	untag: P0;   port: P0, P4 */
354					__setup_vlan(600, 0, 0x00010011);
355				}
356				else {
357					/* No IPTV. Business package */
358					/* Internet:	untag: P9;   port: P4, P9 */
359					system("rtkswitch 38 0");
360					__setup_vlan(500, 0, 0x02000210);
361				}
362			}
363			else if(!strncmp(nvram_safe_get("switch_wantag"), "singtel", 7)) {
364				/* Added for SingTel's exStream issues. Cherry Cho modified in 2011/7/19. */
365				if(strstr(nvram_safe_get("switch_wantag"), "mio")) {
366					/* Connect Singtel MIO box to P3 */
367					system("rtkswitch 40 1");		/* admin all frames on all ports */
368					system("rtkswitch 38 3");		/* IPTV: P0  VoIP: P1 */
369					/* Internet:	untag: P9;   port: P4, P9 */
370					__setup_vlan(10, 0, 0x02000210);
371					/* VoIP:	untag: N/A;  port: P1, P4 */
372					//VoIP Port: P1 tag
373					__setup_vlan(30, 4, 0x00000012);
374				}
375				else {
376					//Connect user's own ATA to lan port and use VoIP by Singtel WAN side VoIP gateway at voip.singtel.com
377					system("rtkswitch 38 1");		/* IPTV: P0 */
378					/* Internet:	untag: P9;   port: P4, P9 */
379					__setup_vlan(10, 0, 0x02000210);
380				}
381
382				/* IPTV */
383				__setup_vlan(20, 4, 0x00010011);		/* untag: P0;   port: P0, P4 */
384			}
385			else if(!strcmp(nvram_safe_get("switch_wantag"), "m1_fiber")) {
386				//VoIP: P1 tag. Cherry Cho added in 2012/1/13.
387				system("rtkswitch 40 1");			/* admin all frames on all ports */
388				system("rtkswitch 38 2");			/* VoIP: P1  2 = 0x10 */
389				/* Internet:	untag: P9;   port: P4, P9 */
390				__setup_vlan(1103, 1, 0x02000210);
391				/* VoIP:	untag: N/A;  port: P1, P4 */
392				//VoIP Port: P1 tag
393				__setup_vlan(1107, 1, 0x00000012);
394			}
395			else if(!strcmp(nvram_safe_get("switch_wantag"), "maxis_fiber")) {
396				//VoIP: P1 tag. Cherry Cho added in 2012/11/6.
397				system("rtkswitch 40 1");			/* admin all frames on all ports */
398				system("rtkswitch 38 2");			/* VoIP: P1  2 = 0x10 */
399				/* Internet:	untag: P9;   port: P4, P9 */
400				__setup_vlan(621, 0, 0x02000210);
401				/* VoIP:	untag: N/A;  port: P1, P4 */
402				__setup_vlan(821, 0, 0x00000012);
403
404				__setup_vlan(822, 0, 0x00000012);		/* untag: N/A;  port: P1, P4 */ //VoIP Port: P1 tag
405			}
406			else if(!strcmp(nvram_safe_get("switch_wantag"), "maxis_fiber_sp")) {
407				//VoIP: P1 tag. Cherry Cho added in 2012/11/6.
408				system("rtkswitch 40 1");			/* admin all frames on all ports */
409				system("rtkswitch 38 2");			/* VoIP: P1  2 = 0x10 */
410				/* Internet:	untag: P9;   port: P4, P9 */
411				__setup_vlan(11, 0, 0x02000210);
412				/* VoIP:	untag: N/A;  port: P1, P4 */
413				//VoIP Port: P1 tag
414				__setup_vlan(14, 0, 0x00000012);
415			}
416#ifdef RTCONFIG_MULTICAST_IPTV
417			else if (!strcmp(nvram_safe_get("switch_wantag"), "movistar")) {
418#if 0	//set in set_wan_tag() since (switch_stb_x > 6) and need vlan interface by vconfig.
419				system("rtkswitch 40 1");			/* admin all frames on all ports */
420				/* Internet/STB/VoIP:	untag: N/A;   port: P4, P9 */
421				__setup_vlan(6, 0, 0x00000210);
422				__setup_vlan(2, 0, 0x00000210);
423				__setup_vlan(3, 0, 0x00000210);
424#endif
425			}
426#endif
427			else if (!strcmp(nvram_safe_get("switch_wantag"), "meo")) {
428				system("rtkswitch 40 1");			/* admin all frames on all ports */
429				system("rtkswitch 38 1");			/* VoIP: P0 */
430				/* Internet/VoIP:	untag: P9;   port: P0, P4, P9 */
431				__setup_vlan(12, 0, 0x02000211);
432			}
433			else if (!strcmp(nvram_safe_get("switch_wantag"), "vodafone")) {
434				system("rtkswitch 40 1");			/* admin all frames on all ports */
435				system("rtkswitch 38 3");			/* Vodafone: P0  IPTV: P1 */
436				/* Internet:	untag: P9;   port: P4, P9 */
437				__setup_vlan(100, 1, 0x02000210);
438				/* IPTV:	untag: N/A;  port: P0, P4 */
439				__setup_vlan(101, 0, 0x00000011);
440				/* Vodafone:	untag: P1;   port: P0, P1, P4 */
441				__setup_vlan(105, 1, 0x00020013);
442			}
443			else {
444				/* Cherry Cho added in 2011/7/11. */
445				/* Initialize VLAN and set Port Isolation */
446				if(strcmp(nvram_safe_get("switch_wan1tagid"), "") && strcmp(nvram_safe_get("switch_wan2tagid"), ""))
447					system("rtkswitch 38 3");		// 3 = 0x11 IPTV: P0  VoIP: P1
448				else if(strcmp(nvram_safe_get("switch_wan1tagid"), ""))
449					system("rtkswitch 38 1");		// 1 = 0x01 IPTV: P0
450				else if(strcmp(nvram_safe_get("switch_wan2tagid"), ""))
451					system("rtkswitch 38 2");		// 2 = 0x10 VoIP: P1
452				else
453					system("rtkswitch 38 0");		//No IPTV and VoIP ports
454
455				/*++ Get and set Vlan Information */
456				if(strcmp(nvram_safe_get("switch_wan0tagid"), "") != 0) {
457					// Internet on WAN (port 4)
458					if ((p = nvram_get("switch_wan0tagid")) != NULL) {
459						t = atoi(p);
460						if((t >= 2) && (t <= 4094))
461							vlan_val = t;
462					}
463
464					if((p = nvram_get("switch_wan0prio")) != NULL && *p != '\0')
465						prio_val = atoi(p);
466
467					__setup_vlan(vlan_val, prio_val, 0x02000210);
468				}
469
470				if(strcmp(nvram_safe_get("switch_wan1tagid"), "") != 0) {
471					// IPTV on LAN4 (port 0)
472					if ((p = nvram_get("switch_wan1tagid")) != NULL) {
473						t = atoi(p);
474						if((t >= 2) && (t <= 4094))
475							vlan_val = t;
476					}
477
478					if((p = nvram_get("switch_wan1prio")) != NULL && *p != '\0')
479						prio_val = atoi(p);
480
481					if(!strcmp(nvram_safe_get("switch_wan1tagid"), nvram_safe_get("switch_wan2tagid")))
482						mask = 0x00030013;	//IPTV=VOIP
483					else
484						mask = 0x00010011;	//IPTV Port: P0 untag 65553 = 0x10 011
485
486					__setup_vlan(vlan_val, prio_val, mask);
487				}
488
489				if(strcmp(nvram_safe_get("switch_wan2tagid"), "") != 0) {
490					// VoIP on LAN3 (port 1)
491					if ((p = nvram_get("switch_wan2tagid")) != NULL) {
492						t = atoi(p);
493						if((t >= 2) && (t <= 4094))
494							vlan_val = t;
495					}
496
497					if((p = nvram_get("switch_wan2prio")) != NULL && *p != '\0')
498						prio_val = atoi(p);
499
500					if(!strcmp(nvram_safe_get("switch_wan1tagid"), nvram_safe_get("switch_wan2tagid")))
501						mask = 0x00030013;	//IPTV=VOIP
502					else
503						mask = 0x00020012;	//VoIP Port: P1 untag
504
505					__setup_vlan(vlan_val, prio_val, mask);
506				}
507
508			}
509		}
510		else
511		{
512			sprintf(parm_buf, "%d", stbport);
513			if (stbport)
514				eval("rtkswitch", "8", parm_buf);
515		}
516
517		/* unknown unicast storm control */
518		if (!nvram_get("switch_ctrlrate_unknown_unicast"))
519			controlrate_unknown_unicast = 0;
520		else
521			controlrate_unknown_unicast = nvram_get_int("switch_ctrlrate_unknown_unicast");
522		if (controlrate_unknown_unicast < 0 || controlrate_unknown_unicast > 1024)
523			controlrate_unknown_unicast = 0;
524		if (controlrate_unknown_unicast)
525		{
526			sprintf(parm_buf, "%d", controlrate_unknown_unicast);
527			eval("rtkswitch", "22", parm_buf);
528		}
529
530		/* unknown multicast storm control */
531		if (!nvram_get("switch_ctrlrate_unknown_multicast"))
532			controlrate_unknown_multicast = 0;
533		else
534			controlrate_unknown_multicast = nvram_get_int("switch_ctrlrate_unknown_multicast");
535		if (controlrate_unknown_multicast < 0 || controlrate_unknown_multicast > 1024)
536			controlrate_unknown_multicast = 0;
537		if (controlrate_unknown_multicast)
538		{
539			sprintf(parm_buf, "%d", controlrate_unknown_multicast);
540			eval("rtkswitch", "23", parm_buf);
541		}
542
543		/* multicast storm control */
544		if (!nvram_get("switch_ctrlrate_multicast"))
545			controlrate_multicast = 0;
546		else
547			controlrate_multicast = nvram_get_int("switch_ctrlrate_multicast");
548		if (controlrate_multicast < 0 || controlrate_multicast > 1024)
549			controlrate_multicast = 0;
550		if (controlrate_multicast)
551		{
552			sprintf(parm_buf, "%d", controlrate_multicast);
553			eval("rtkswitch", "24", parm_buf);
554		}
555
556		/* broadcast storm control */
557		if (!nvram_get("switch_ctrlrate_broadcast"))
558			controlrate_broadcast = 0;
559		else
560			controlrate_broadcast = nvram_get_int("switch_ctrlrate_broadcast");
561		if (controlrate_broadcast < 0 || controlrate_broadcast > 1024)
562			controlrate_broadcast = 0;
563		if (controlrate_broadcast)
564		{
565			sprintf(parm_buf, "%d", controlrate_broadcast);
566			eval("rtkswitch", "25", parm_buf);
567		}
568
569#ifdef RTN56U
570		if (nvram_match("switch_wanport_force_1g", "1"))
571			eval("rtkswitch", "26");
572#endif
573	}
574	else if (is_apmode_enabled())
575	{
576		if (merge_wan_port_into_lan_ports)
577			eval("rtkswitch", "8", "100");
578	}
579#if defined(RTCONFIG_WIRELESSREPEATER) && defined(RTCONFIG_PROXYSTA)
580	else if (mediabridge_mode())
581	{
582	}
583#endif
584
585	if (is_routing_enabled() || is_apmode_enabled()) {
586#ifdef RTCONFIG_DSL
587		dbG("link up all ports\n");
588		eval("rtkswitch", "16");	// link up all ports
589#else
590		dbG("link up wan port(s)\n");
591		eval("rtkswitch", "114");	// link up wan port(s)
592#endif
593	}
594
595#if defined(RTCONFIG_BLINK_LED)
596	if (is_swports_bled("led_lan_gpio")) {
597		update_swports_bled("led_lan_gpio", nvram_get_int("lanports_mask"));
598	}
599	if (is_swports_bled("led_wan_gpio")) {
600		update_swports_bled("led_wan_gpio", nvram_get_int("wanports_mask"));
601	}
602#endif
603}
604
605int
606switch_exist(void)
607{
608	int ret;
609#ifdef RTCONFIG_DSL
610	// 0 means switch exist
611	ret = 0;
612#else
613	ret = eval("rtkswitch", "41");
614	_dprintf("eval(rtkswitch, 41) ret(%d)\n", ret);
615#endif
616	return (ret == 0);
617}
618
619void init_wl(void)
620{
621	if (!module_loaded("rt2860v2_ap"))
622		modprobe("rt2860v2_ap");
623#if defined (RTCONFIG_WLMODULE_RT3090_AP)
624	if (!module_loaded("RTPCI_ap"))
625	{
626		modprobe("RTPCI_ap");
627	}
628#endif
629#if defined (RTCONFIG_WLMODULE_RT3352_INIC_MII)
630	if (!module_loaded("iNIC_mii"))
631		modprobe("iNIC_mii", "mode=ap", "bridge=1", "miimaster=eth2", "syncmiimac=0");	// set iNIC mac address from eeprom need insmod with "syncmiimac=0"
632#endif
633#if defined (RTCONFIG_WLMODULE_MT7610_AP)
634	if (!module_loaded("MT7610_ap"))
635		modprobe("MT7610_ap");
636#endif
637#if defined (RTCONFIG_WLMODULE_RLT_WIFI)
638	if (!module_loaded("rlt_wifi"))
639	{
640		modprobe("rlt_wifi");
641	}
642#endif
643#if defined (RTCONFIG_WLMODULE_MT7603E_AP)
644	if (!module_loaded("rlt_wifi_7603e"))
645		modprobe("rlt_wifi_7603e");
646#endif
647	sleep(1);
648}
649
650void fini_wl(void)
651{
652	if (module_loaded("hw_nat"))
653		modprobe_r("hw_nat");
654
655#if defined (RTCONFIG_WLMODULE_MT7610_AP)
656	if (module_loaded("MT7610_ap"))
657		modprobe_r("MT7610_ap");
658#endif
659#if defined (RTCONFIG_WLMODULE_RLT_WIFI)
660	if (module_loaded("rlt_wifi"))
661	{
662		modprobe_r("rlt_wifi");
663#if defined(RTAC1200HP)
664		//remove wifi driver, 5G wifi gpio led turn off
665		sleep(1);
666		led_onoff(1);
667#endif
668	}
669#endif
670#if defined (RTCONFIG_WLMODULE_MT7603E_AP)
671	if (module_loaded("rlt_wifi_7603e"))
672		modprobe_r("rlt_wifi_7603e");
673#endif
674#if defined (RTCONFIG_WLMODULE_RT3352_INIC_MII)
675	if (module_loaded("iNIC_mii"))
676		modprobe_r("iNIC_mii");
677#endif
678
679#if defined (RTCONFIG_WLMODULE_RT3090_AP)
680	if (module_loaded("RTPCI_ap"))
681	{
682		modprobe_r("RTPCI_ap");
683	}
684#endif
685
686	if (module_loaded("rt2860v2_ap"))
687		modprobe_r("rt2860v2_ap");
688}
689
690
691#if ! defined(RTCONFIG_NEW_REGULATION_DOMAIN)
692static void chk_valid_country_code(char *country_code)
693{
694	if ((unsigned char)country_code[0]!=0xff)
695	{
696		//for specific power
697		if     (memcmp(country_code, "Z1", 2) == 0)
698			strcpy(country_code, "US");
699		else if(memcmp(country_code, "Z2", 2) == 0)
700			strcpy(country_code, "GB");
701		else if(memcmp(country_code, "Z3", 2) == 0)
702			strcpy(country_code, "TW");
703		else if(memcmp(country_code, "Z4", 2) == 0)
704			strcpy(country_code, "CN");
705		//for normal
706		if(memcmp(country_code, "BR", 2) == 0)
707			strcpy(country_code, "UZ");
708	}
709	else
710	{
711		strcpy(country_code, "DB");
712	}
713}
714#endif
715
716#ifdef RA_SINGLE_SKU
717static void create_SingleSKU(const char *path, const char *pBand, const char *reg_spec, const char *pFollow)
718{
719	char src[128];
720	char dest[128];
721
722	sprintf(src , "/ra_SKU/SingleSKU%s_%s%s.dat", pBand, reg_spec, pFollow);
723	sprintf(dest, "%s/SingleSKU%s.dat", path, pBand);
724
725	eval("mkdir", "-p", (char*)path);
726	unlink(dest);
727	eval("ln", "-s", src, dest);
728}
729
730void gen_ra_sku(const char *reg_spec)
731{
732#ifdef RTAC52U	// [0x40002] == 0x00 0x02
733	unsigned char dst[16];
734	if (!(FRead(dst, OFFSET_EEPROM_VER, 2) < 0) && dst[0] == 0x00 && dst[1] == 0x02)
735	{
736		create_SingleSKU("/etc/Wireless/RT2860", "", reg_spec, "_0002");
737	}
738	else
739#endif
740	create_SingleSKU("/etc/Wireless/RT2860", "", reg_spec, "");
741
742#ifdef RTCONFIG_HAS_5G
743#ifdef RTAC52U	// [0x40002] == 0x00 0x02
744	if (!(FRead(dst, OFFSET_EEPROM_VER, 2) < 0) && dst[0] == 0x00 && dst[1] == 0x02)
745	{
746		create_SingleSKU("/etc/Wireless/iNIC", "_5G", reg_spec, "_0002");
747	}
748	else
749#endif
750	create_SingleSKU("/etc/Wireless/iNIC", "_5G", reg_spec, "");
751#endif	/* RTCONFIG_HAS_5G */
752}
753#endif	/* RA_SINGLE_SKU */
754
755void init_syspara(void)
756{
757	unsigned char buffer[16];
758	unsigned char *dst;
759	unsigned int bytes;
760	int i;
761	char macaddr[]="00:11:22:33:44:55";
762	char macaddr2[]="00:11:22:33:44:58";
763	char country_code[3];
764	char pin[9];
765	char productid[13];
766	char fwver[8];
767	char blver[20];
768	unsigned char txbf_para[33];
769	char ea[ETHER_ADDR_LEN];
770	const char *reg_spec_def;
771
772#if defined(RTAC1200HP) || defined(RTN56UB1) || defined(RTN56UB2)
773	char fixch;
774	char value_str[MAX_REGSPEC_LEN+1];
775	memset(value_str, 0, sizeof(value_str));
776#endif
777	nvram_set("buildno", rt_serialno);
778	nvram_set("extendno", rt_extendno);
779	nvram_set("buildinfo", rt_buildinfo);
780	nvram_set("swpjverno", rt_swpjverno);
781
782	/* /dev/mtd/2, RF parameters, starts from 0x40000 */
783	dst = buffer;
784	bytes = 6;
785	memset(buffer, 0, sizeof(buffer));
786	memset(country_code, 0, sizeof(country_code));
787	memset(pin, 0, sizeof(pin));
788	memset(productid, 0, sizeof(productid));
789	memset(fwver, 0, sizeof(fwver));
790	memset(txbf_para, 0, sizeof(txbf_para));
791
792	if (FRead(dst, OFFSET_MAC_ADDR, bytes)<0)
793	{
794		_dprintf("READ MAC address: Out of scope\n");
795	}
796	else
797	{
798		if (buffer[0]!=0xff)
799			ether_etoa(buffer, macaddr);
800	}
801
802#if !defined(RTN14U) && !defined(RTN11P) && !defined(RTN300) // single band
803	if (FRead(dst, OFFSET_MAC_ADDR_2G, bytes)<0)
804	{
805		_dprintf("READ MAC address 2G: Out of scope\n");
806	}
807	else
808	{
809		if (buffer[0]!=0xff)
810			ether_etoa(buffer, macaddr2);
811	}
812#endif
813
814#if defined(RTAC1200HP) || defined(RTN56UB1) || defined(RTN56UB2)
815	fixch='0';
816	FRead(&fixch, OFFSET_FIX_CHANNEL, 1);
817	if(fixch=='1')
818	{
819		_dprintf("Fix Channel for RF Cal. and disable br0's STP\n");
820		nvram_set("wl0_channel","1");
821		nvram_set("wl1_channel","36");
822		nvram_set("lan_stp","0");
823	}
824
825	FRead(value_str, REGSPEC_ADDR, MAX_REGSPEC_LEN);
826	for(i = 0; i < MAX_REGSPEC_LEN && value_str[i] != '\0'; i++) {
827		if ((unsigned char)value_str[i] == 0xff)
828		{
829			value_str[i] = '\0';
830			break;
831		}
832	}
833	if(!strcmp(value_str,"JP"))
834	   nvram_set("JP_CS","1");
835	else
836	   nvram_set("JP_CS","0");
837#endif
838#if defined(RTN14U) || defined(RTN11P) || defined(RTN300) // single band
839	if (!mssid_mac_validate(macaddr))
840#else
841	if (!mssid_mac_validate(macaddr) || !mssid_mac_validate(macaddr2))
842#endif
843		nvram_set("wl_mssid", "0");
844	else
845		nvram_set("wl_mssid", "1");
846
847#if defined(RTN14U) || defined(RTN11P) || defined(RTN300) // single band
848	nvram_set("et0macaddr", macaddr);
849	nvram_set("et1macaddr", macaddr);
850#else
851	//TODO: separate for different chipset solution
852	nvram_set("et0macaddr", macaddr);
853	nvram_set("et1macaddr", macaddr2);
854#endif
855
856	if (FRead(dst, OFFSET_MAC_GMAC0, bytes)<0)
857		dbg("READ MAC address GMAC0: Out of scope\n");
858	else
859	{
860		if (buffer[0]==0xff)
861		{
862			if (ether_atoe(macaddr, ea))
863				FWrite(ea, OFFSET_MAC_GMAC0, 6);
864		}
865	}
866
867	if (FRead(dst, OFFSET_MAC_GMAC2, bytes)<0)
868		dbg("READ MAC address GMAC2: Out of scope\n");
869	else
870	{
871		if (buffer[0]==0xff)
872		{
873			if (ether_atoe(macaddr2, ea))
874				FWrite(ea, OFFSET_MAC_GMAC2, 6);
875		}
876	}
877
878#ifdef RA_SINGLE_SKU
879#if defined(RTAC52U) || defined(RTAC51U) || defined(RTN11P) || defined(RTN300) || defined(RTN54U) || defined(RTAC1200HP) || defined(RTN56UB1) || defined(RTAC54U) || defined(RTN56UB2)
880	gen_ra_sku(nvram_safe_get("reg_spec"));
881#endif	/* RTAC52U && RTAC51U && RTN54U && RTAC54U && RTAC1200HP && RTN56UB1 && RTN56UB1 && RTN11P && RTN300 */
882#endif	/* RA_SINGLE_SKU */
883
884	{
885#ifdef RTCONFIG_ODMPID
886		char modelname[16];
887		FRead(modelname, OFFSET_ODMPID, sizeof(modelname));
888		modelname[sizeof(modelname)-1] = '\0';
889		if(modelname[0] != 0 && (unsigned char)(modelname[0]) != 0xff && is_valid_hostname(modelname) && strcmp(modelname, "ASUS"))
890		{
891#if defined(RTN11P)
892			if(strcmp(modelname, "RT-N12E_B")==0)
893				nvram_set("odmpid", "RT-N12E_B1");
894			else
895#endif	/* RTN11P */
896			nvram_set("odmpid", modelname);
897		}
898		else
899#endif
900			nvram_unset("odmpid");
901	}
902
903	/* reserved for Ralink. used as ASUS country code. */
904#if !defined(RTCONFIG_NEW_REGULATION_DOMAIN)
905	dst = (unsigned char*) country_code;
906	bytes = 2;
907	if (FRead(dst, OFFSET_COUNTRY_CODE, bytes)<0)
908	{
909		_dprintf("READ ASUS country code: Out of scope\n");
910		nvram_set("wl_country_code", "");
911	}
912	else
913	{
914		chk_valid_country_code(country_code);
915		nvram_set("wl_country_code", country_code);
916		nvram_set("wl0_country_code", country_code);
917#ifdef RTCONFIG_HAS_5G
918		nvram_set("wl1_country_code", country_code);
919#endif
920	}
921#if defined(RTN14U) // for CE Adaptivity
922	if ((strcmp(country_code, "DE") == 0) || (strcmp(country_code, "EU") == 0))
923		nvram_set("reg_spec", "CE");
924	else
925		nvram_set("reg_spec", "NDF");
926#endif
927#else	/* ! RTCONFIG_NEW_REGULATION_DOMAIN */
928	dst = buffer;
929
930#if defined(RTAC51U) || defined(RTN11P)
931	reg_spec_def = "CE";
932#else
933	reg_spec_def = "FCC";
934#endif
935	bytes = MAX_REGSPEC_LEN;
936	memset(dst, 0, MAX_REGSPEC_LEN+1);
937	if(FRead(dst, REGSPEC_ADDR, bytes) < 0)
938		nvram_set("reg_spec", reg_spec_def); // DEFAULT
939	else
940	{
941		for (i=(MAX_REGSPEC_LEN-1);i>=0;i--) {
942			if ((dst[i]==0xff) || (dst[i]=='\0'))
943				dst[i]='\0';
944		}
945		if (dst[0]!=0x00)
946			nvram_set("reg_spec", dst);
947		else
948			nvram_set("reg_spec", reg_spec_def); // DEFAULT
949	}
950
951	if (FRead(dst, REG2G_EEPROM_ADDR, MAX_REGDOMAIN_LEN)<0 || memcmp(dst,"2G_CH", 5) != 0)
952	{
953		_dprintf("Read REG2G_EEPROM_ADDR fail or invalid value\n");
954		nvram_set("wl_country_code", "");
955		nvram_set("wl0_country_code", "DB");
956		nvram_set("wl_reg_2g", "2G_CH14");
957	}
958	else
959	{
960		for(i = 0; i < MAX_REGDOMAIN_LEN; i++)
961			if(dst[i] == 0xff || dst[i] == 0)
962				break;
963
964		dst[i] = 0;
965		nvram_set("wl_reg_2g", dst);
966		if      (strcmp(dst, "2G_CH11") == 0)
967			nvram_set("wl0_country_code", "US");
968		else if (strcmp(dst, "2G_CH13") == 0)
969			nvram_set("wl0_country_code", "GB");
970		else if (strcmp(dst, "2G_CH14") == 0)
971			nvram_set("wl0_country_code", "DB");
972		else
973			nvram_set("wl0_country_code", "DB");
974	}
975#ifdef RTCONFIG_HAS_5G
976	if (FRead(dst, REG5G_EEPROM_ADDR, MAX_REGDOMAIN_LEN)<0 || memcmp(dst,"5G_", 3) != 0)
977	{
978		_dprintf("Read REG5G_EEPROM_ADDR fail or invalid value\n");
979		nvram_set("wl_country_code", "");
980		nvram_set("wl1_country_code", "DB");
981		nvram_set("wl_reg_5g", "5G_ALL");
982	}
983	else
984	{
985		for(i = 0; i < MAX_REGDOMAIN_LEN; i++)
986			if(dst[i] == 0xff || dst[i] == 0)
987				break;
988
989		dst[i] = 0;
990		nvram_set("wl_reg_5g", dst);
991		nvram_set("wl1_IEEE80211H", "0");
992		if      (strcmp(dst, "5G_BAND1") == 0)
993			nvram_set("wl1_country_code", "GB");
994		else if (strcmp(dst, "5G_BAND123") == 0)
995		{
996			nvram_set("wl1_country_code", "GB");
997#ifdef RTCONFIG_RALINK_DFS
998			nvram_set("wl1_IEEE80211H", "1");
999#endif	/* RTCONFIG_RALINK_DFS */
1000		}
1001		else if (strcmp(dst, "5G_BAND14") == 0)
1002			nvram_set("wl1_country_code", "US");
1003		else if (strcmp(dst, "5G_BAND24") == 0)
1004			nvram_set("wl1_country_code", "TW");
1005		else if (strcmp(dst, "5G_BAND4") == 0)
1006			nvram_set("wl1_country_code", "CN");
1007		else if (strcmp(dst, "5G_BAND124") == 0)
1008			nvram_set("wl1_country_code", "IN");
1009		else
1010			nvram_set("wl1_country_code", "DB");
1011	}
1012#endif	/* RTCONFIG_HAS_5G */
1013#endif	/* ! RTCONFIG_NEW_REGULATION_DOMAIN */
1014#if defined(RTN56U) || defined(RTCONFIG_DSL)
1015		if (nvram_match("wl_country_code", "BR"))
1016		{
1017			nvram_set("wl_country_code", "UZ");
1018			nvram_set("wl0_country_code", "UZ");
1019#ifdef RTCONFIG_HAS_5G
1020			nvram_set("wl1_country_code", "UZ");
1021#endif	/* RTCONFIG_HAS_5G */
1022		}
1023#endif
1024		if (nvram_match("wl_country_code", "HK") && nvram_match("preferred_lang", ""))
1025			nvram_set("preferred_lang", "TW");
1026
1027	/* reserved for Ralink. used as ASUS pin code. */
1028	dst = (char*)pin;
1029	bytes = 8;
1030	if (FRead(dst, OFFSET_PIN_CODE, bytes)<0)
1031	{
1032		_dprintf("READ ASUS pin code: Out of scope\n");
1033		nvram_set("wl_pin_code", "");
1034	}
1035	else
1036	{
1037		if ((unsigned char)pin[0]!=0xff)
1038			nvram_set("secret_code", pin);
1039		else
1040			nvram_set("secret_code", "12345670");
1041	}
1042
1043	dst = buffer;
1044	bytes = 16;
1045	if (linuxRead(dst, 0x20, bytes)<0)	/* The "linux" MTD partition, offset 0x20. */
1046	{
1047		fprintf(stderr, "READ firmware header: Out of scope\n");
1048		nvram_set("productid", "unknown");
1049		nvram_set("firmver", "unknown");
1050	}
1051	else
1052	{
1053		strncpy(productid, buffer + 4, 12);
1054		productid[12] = 0;
1055		sprintf(fwver, "%d.%d.%d.%d", buffer[0], buffer[1], buffer[2], buffer[3]);
1056		nvram_set("productid", trim_r(productid));
1057		nvram_set("firmver", trim_r(fwver));
1058	}
1059
1060#if defined(RTCONFIG_TCODE)
1061	/* Territory code */
1062	memset(buffer, 0, sizeof(buffer));
1063	if (FRead(buffer, OFFSET_TERRITORY_CODE, 5) < 0) {
1064		_dprintf("READ ASUS territory code: Out of scope\n");
1065		nvram_unset("territory_code");
1066	} else {
1067		/* [A-Z][A-Z]/[0-9][0-9] */
1068		if (buffer[2] != '/' ||
1069		    !isupper(buffer[0]) || !isupper(buffer[1]) ||
1070		    !isdigit(buffer[3]) || !isdigit(buffer[4]))
1071		{
1072			nvram_unset("territory_code");
1073		} else {
1074			nvram_set("territory_code", buffer);
1075		}
1076	}
1077
1078#if defined(RTN56UB1)
1079	if((nvram_match("territory_code","EU/01")|| nvram_match("territory_code","UK/01"))&& !nvram_match("wl1_IEEE80211H","1"))
1080	{
1081#ifdef RTCONFIG_RALINK_DFS
1082			nvram_set("wl1_IEEE80211H", "1");
1083#endif	/* RTCONFIG_RALINK_DFS */
1084	}
1085#endif
1086
1087#if defined(RTN11P)
1088	if (nvram_match("odmpid", "RT-N12+") && nvram_match("reg_spec", "CN"))
1089	{
1090		char *str;
1091		str = nvram_get("territory_code");
1092		if(str == NULL || str[0] == '\0') {
1093			nvram_set("territory_code", "CN/01");
1094		}
1095	}
1096
1097	if( nvram_match("odmpid", "RT-N12+") && !strncmp(nvram_safe_get("territory_code"), "IN", 2 )) {
1098		nvram_set("reg_spec", "NCC");
1099		nvram_set("wl0_country_code", "US");
1100	}
1101#endif	/* RTN11P */
1102
1103	/* PSK */
1104        memset(buffer, 0, sizeof(buffer));
1105	if (FRead(buffer, OFFSET_PSK, 14) < 0) {
1106	_dprintf("READ ASUS PSK: Out of scope\n");
1107		nvram_set("wifi_psk", "");
1108	 } else {
1109	if (buffer[0] == 0xff)
1110		nvram_set("wifi_psk", "");
1111	else
1112		nvram_set("wifi_psk", buffer);
1113	}
1114#endif
1115
1116	memset(buffer, 0, sizeof(buffer));
1117	FRead(buffer, OFFSET_BOOT_VER, 4);
1118//	sprintf(blver, "%c.%c.%c.%c", buffer[0], buffer[1], buffer[2], buffer[3]);
1119	sprintf(blver, "%s-0%c-0%c-0%c-0%c", trim_r(productid), buffer[0], buffer[1], buffer[2], buffer[3]);
1120	nvram_set("blver", trim_r(blver));
1121
1122	_dprintf("mtd productid: %s\n", nvram_safe_get("productid"));
1123	_dprintf("bootloader version: %s\n", nvram_safe_get("blver"));
1124	_dprintf("firmware version: %s\n", nvram_safe_get("firmver"));
1125
1126	dst = txbf_para;
1127	int count_0xff = 0;
1128	if (FRead(dst, OFFSET_TXBF_PARA, 33) < 0)
1129	{
1130		fprintf(stderr, "READ TXBF PARA address: Out of scope\n");
1131	}
1132	else
1133	{
1134		for (i = 0; i < 33; i++)
1135		{
1136			if (txbf_para[i] == 0xff)
1137				count_0xff++;
1138/*
1139			if ((i % 16) == 0) fprintf(stderr, "\n");
1140			fprintf(stderr, "%02x ", (unsigned char) txbf_para[i]);
1141*/
1142		}
1143/*
1144		fprintf(stderr, "\n");
1145
1146		fprintf(stderr, "TxBF parameter 0xFF count: %d\n", count_0xff);
1147*/
1148	}
1149
1150	if (count_0xff == 33)
1151		nvram_set("wl1_txbf_en", "0");
1152	else
1153		nvram_set("wl1_txbf_en", "1");
1154
1155#if defined (RTCONFIG_WLMODULE_RT3352_INIC_MII)
1156#define EEPROM_INIC_SIZE (512)
1157#define EEPROM_INIT_ADDR 0x48000
1158#define EEPROM_INIT_FILE "/etc/Wireless/iNIC/iNIC_e2p.bin"
1159	{
1160		char eeprom[EEPROM_INIC_SIZE];
1161		if(FRead(eeprom, EEPROM_INIT_ADDR, sizeof(eeprom)) < 0)
1162		{
1163			fprintf(stderr, "FRead(eeprom, 0x%08x, 0x%x) failed\n", EEPROM_INIT_ADDR, sizeof(eeprom));
1164		}
1165		else
1166		{
1167			FILE *fp;
1168			char *filepath = EEPROM_INIT_FILE;
1169
1170			system("mkdir -p /etc/Wireless/iNIC/");
1171			if((fp = fopen(filepath, "w")) == NULL)
1172			{
1173				fprintf(stderr, "fopen(%s) failed!!\n", filepath);
1174			}
1175			else
1176			{
1177				if(fwrite(eeprom, sizeof(eeprom), 1, fp) < 1)
1178				{
1179					perror("fwrite(eeprom)");
1180				}
1181				fclose(fp);
1182			}
1183		}
1184	}
1185#endif
1186
1187	nvram_set("firmver", rt_version);
1188	nvram_set("productid", rt_buildname);
1189
1190	_dprintf("odmpid: %s\n", nvram_safe_get("odmpid"));
1191	_dprintf("current FW productid: %s\n", nvram_safe_get("productid"));
1192	_dprintf("current FW firmver: %s\n", nvram_safe_get("firmver"));
1193}
1194
1195void generate_wl_para(int unit, int subunit)
1196{
1197}
1198
1199#if defined(RTAC52U) || defined(RTAC51U) || defined(RTN54U) || defined(RTAC1200HP) || defined(RTN56UB1) || defined(RTN56UB2)  || defined(RTAC54U)
1200#define HW_NAT_WIFI_OFFLOADING		(0xFF00)
1201#define HW_NAT_DEVNAME			"hwnat0"
1202static void adjust_hwnat_wifi_offloading(void)
1203{
1204	int enable_hwnat_wifi = 1, fd;
1205
1206	if (!nvram_match("switch_wantag", "none") && !nvram_match("switch_wantag", "")) {
1207		nvram_unset("isp_profile_hwnat_not_safe");
1208		eval("rtkswitch", "50");
1209		if (nvram_get_int("isp_profile_hwnat_not_safe") == 1)
1210			enable_hwnat_wifi = 0;
1211	}
1212
1213	if ((fd = open("/dev/" HW_NAT_DEVNAME, O_RDONLY)) < 0) {
1214		_dprintf("Open /dev/%s fail. errno %d (%s)\n", HW_NAT_DEVNAME, errno, strerror(errno));
1215		return;
1216	}
1217
1218	_dprintf("hwnat_wifi = %d\n", enable_hwnat_wifi);
1219	if (ioctl(fd, HW_NAT_WIFI_OFFLOADING, &enable_hwnat_wifi) < 0)
1220		_dprintf("ioctl error. errno %d (%s)\n", errno, strerror(errno));
1221
1222	close(fd);
1223}
1224#else
1225static inline void adjust_hwnat_wifi_offloading(void) { }
1226#endif
1227
1228// only ralink solution can reload it dynamically
1229// only happened when hwnat=1
1230// only loaded when unloaded, and unloaded when loaded
1231// in restart_firewall for fw_pt_l2tp/fw_pt_ipsec
1232// in restart_qos for qos_enable
1233// in restart_wireless for wlx_mrate_x, etc
1234void reinit_hwnat(int unit)
1235{
1236	int prim_unit = wan_primary_ifunit();
1237	int act = 1;	/* -1/0/otherwise: ignore/remove hwnat/load hwnat */
1238#if defined(RTCONFIG_DUALWAN)
1239	int nat_x = -1, i, l, t, link_wan = 1, link_wans_lan = 1;
1240	int wans_cap = get_wans_dualwan() & WANSCAP_WAN;
1241	int wanslan_cap = get_wans_dualwan() & WANSCAP_LAN;
1242	char nat_x_str[] = "wanX_nat_xXXXXXX";
1243#endif
1244	if (!nvram_get_int("hwnat"))
1245		return;
1246
1247	/* If QoS is enabled, disable hwnat. */
1248	if (nvram_get_int("qos_enable") == 1 && nvram_get_int("qos_type") != 1)
1249		act = 0;
1250
1251#if defined(RTN14U) || defined(RTAC52U) || defined(RTAC51U) || defined(RTN11P) || defined(RTN300) || defined(RTN54U) || defined(RTAC1200HP) || defined(RTN56UB1) || defined(RTAC54U) || defined(RTN56UB2)
1252	if (act > 0 && !nvram_match("switch_wantag", "none") && !nvram_match("switch_wantag", ""))
1253		act = 0;
1254#endif
1255
1256	if (act > 0) {
1257#if defined(RTCONFIG_DUALWAN)
1258		if (unit < 0 || unit > WAN_UNIT_SECOND) {
1259			if ((wans_cap && wanslan_cap) ||
1260			    (wanslan_cap && (!nvram_match("switch_wantag", "none") && !nvram_match("switch_wantag", "")))
1261			   )
1262				act = 0;
1263		} else {
1264			sprintf(nat_x_str, "wan%d_nat_x", unit);
1265			nat_x = nvram_get_int(nat_x_str);
1266			if (unit == prim_unit && !nat_x)
1267				act = 0;
1268			else if ((wans_cap && wanslan_cap) ||
1269				 (wanslan_cap && (!nvram_match("switch_wantag", "none") && !nvram_match("switch_wantag", "")))
1270				)
1271				act = 0;
1272			else if (unit != prim_unit)
1273				act = -1;
1274		}
1275#else
1276		if (!is_nat_enabled())
1277			act = 0;
1278#endif
1279	}
1280
1281#if defined(RTN65U) || defined(RTN56U) || defined(RTN14U) || defined(RTAC52U) || defined(RTAC51U) || defined(RTN11P) || defined(RTN300) || defined(RTN54U) || defined(RTAC1200HP) || defined(RTN56UB1) || defined(RTAC54U) || defined(RTN56UB2)
1282	if (act > 0) {
1283#if defined(RTCONFIG_DUALWAN)
1284		if (unit < 0 || unit > WAN_UNIT_SECOND || nvram_match("wans_mode", "lb")) {
1285			if (get_wans_dualwan() & WANSCAP_USB)
1286				act = 0;
1287		} else {
1288			if (unit == prim_unit && get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_USB)
1289				act = 0;
1290		}
1291#else
1292		if (dualwan_unit__usbif(prim_unit))
1293			act = 0;
1294#endif
1295	}
1296#endif
1297
1298#if defined(RTCONFIG_DUALWAN)
1299	if (act != 0 &&
1300	    ((wans_cap && wanslan_cap) || (wanslan_cap && (!nvram_match("switch_wantag", "none") && !nvram_match("switch_wantag", ""))))
1301	   )
1302	{
1303		/* If WANS_LAN and WAN is enabled, WANS_LAN is link-up and WAN is not link-up, hw_nat MUST be removed.
1304		 * If hw_nat exists in such scenario, LAN PC can't connect to Internet through WANS_LAN correctly.
1305		 *
1306		 * FIXME:
1307		 * If generic IPTV feature is enabled, STB port and VoIP port are recognized as WAN port(s).
1308		 * In such case, we don't know whether real WAN port is link-up/down.
1309		 * Thus, if WAN is link-up and primary unit is not WAN, assume WAN is link-down.
1310		 */
1311		for (i = WAN_UNIT_FIRST; i < WAN_UNIT_MAX; ++i) {
1312			if ((t = get_dualwan_by_unit(i)) == WANS_DUALWAN_IF_USB)
1313				continue;
1314
1315			l = wanport_status(i);
1316			switch (t) {
1317			case WANS_DUALWAN_IF_WAN:
1318				link_wan = l && (i == prim_unit);
1319				break;
1320			case WANS_DUALWAN_IF_DSL:
1321				link_wan = l;
1322				break;
1323			case WANS_DUALWAN_IF_LAN:
1324				link_wans_lan = l;
1325				break;
1326			default:
1327				_dprintf("%s: Unknown WAN type %d\n", __func__, t);
1328			}
1329		}
1330
1331		if (!link_wan && link_wans_lan)
1332			act = 0;
1333	}
1334
1335	_dprintf("%s:DUALWAN: unit %d,%d type %d iptv [%s] nat_x %d qos %d wans_mode %s link %d,%d: action %d.\n",
1336		__func__, unit, prim_unit, get_dualwan_by_unit(unit), nvram_safe_get("switch_wantag"), nat_x,
1337		nvram_get_int("qos_enable"), nvram_safe_get("wans_mode"),
1338		link_wan, link_wans_lan, act);
1339#else
1340	_dprintf("%s:WAN: unit %d,%d type %d nat_x %d qos %d: action %d.\n",
1341		__func__, unit, prim_unit, get_dualwan_by_unit(unit),
1342		nvram_get_int("wan0_nat_x"), nvram_get_int("qos_enable"), act);
1343#endif
1344
1345	if (act < 0)
1346		return;
1347
1348	switch (act) {
1349	case 0:		/* remove hwnat */
1350		if (module_loaded("hw_nat")) {
1351			modprobe_r("hw_nat");
1352			sleep(1);
1353		}
1354		break;
1355	default:	/* load hwnat */
1356		if (!module_loaded("hw_nat")) {
1357			modprobe("hw_nat");
1358			sleep(1);
1359		}
1360		adjust_hwnat_wifi_offloading();
1361	}
1362}
1363
1364char *get_wlifname(int unit, int subunit, int subunit_x, char *buf)
1365{
1366	char wifbuf[32];
1367	char prefix[]="wlXXXXXX_", tmp[100];
1368#if defined(RTCONFIG_WIRELESSREPEATER)
1369	if (nvram_get_int("sw_mode") == SW_MODE_REPEATER  && nvram_get_int("wlc_band") == unit && subunit==1)
1370	{
1371		if(unit == 1)
1372			sprintf(buf, "%s", APCLI_5G);
1373		else
1374			sprintf(buf, "%s", APCLI_2G);
1375	}
1376	else
1377#endif /* RTCONFIG_WIRELESSREPEATER */
1378	{
1379		memset(wifbuf, 0, sizeof(wifbuf));
1380
1381		if(unit==0) strncpy(wifbuf, WIF_2G, strlen(WIF_2G)-1);
1382#if defined(RTCONFIG_HAS_5G)
1383		else strncpy(wifbuf, WIF_5G, strlen(WIF_5G)-1);
1384#endif	/* RTCONFIG_HAS_5G */
1385
1386		snprintf(prefix, sizeof(prefix), "wl%d.%d_", unit, subunit);
1387		if (nvram_match(strcat_r(prefix, "bss_enabled", tmp), "1"))
1388			sprintf(buf, "%s%d", wifbuf, subunit_x);
1389		else
1390			sprintf(buf, "%s", "");
1391	}
1392	return buf;
1393}
1394
1395int
1396wl_exist(char *ifname, int band)
1397{
1398	int ret = 0;
1399	ret = eval("iwpriv", ifname, "stat");
1400	_dprintf("eval(iwpriv, %s, stat) ret(%d)\n", ifname, ret);
1401	return !ret;
1402}
1403
1404void
1405set_wan_tag(char *interface) {
1406	int model, wan_vid; //, iptv_vid, voip_vid, wan_prio, iptv_prio, voip_prio;
1407	char wan_dev[10], port_id[7];
1408
1409	model = get_model();
1410	wan_vid = nvram_get_int("switch_wan0tagid");
1411
1412	sprintf(wan_dev, "vlan%d", wan_vid);
1413
1414	switch(model) {
1415	case MODEL_RTAC1200HP:
1416	case MODEL_RTAC51U:
1417	case MODEL_RTAC52U:
1418	case MODEL_RTAC54U:
1419	case MODEL_RTN11P:
1420	case MODEL_RTN14U:
1421	case MODEL_RTN54U:
1422	case MODEL_RTN56UB1:
1423	case MODEL_RTN56UB2:
1424		ifconfig(interface, IFUP, 0, 0);
1425		if(wan_vid) { /* config wan port */
1426			eval("vconfig", "rem", "vlan2");
1427			sprintf(port_id, "%d", wan_vid);
1428			eval("vconfig", "add", interface, port_id);
1429		}
1430		/* Set Wan port PRIO */
1431		if(nvram_invmatch("switch_wan0prio", "0"))
1432			eval("vconfig", "set_egress_map", wan_dev, "0", nvram_get("switch_wan0prio"));
1433		break;
1434	}
1435
1436#ifdef RTCONFIG_MULTICAST_IPTV
1437	{
1438		int iptv_vid, voip_vid, iptv_prio, voip_prio, switch_stb;
1439		int mang_vid, mang_prio;
1440
1441		iptv_vid  = nvram_get_int("switch_wan1tagid") & 0x0fff;
1442		voip_vid  = nvram_get_int("switch_wan2tagid") & 0x0fff;
1443		iptv_prio = nvram_get_int("switch_wan1prio") & 0x7;
1444		voip_prio = nvram_get_int("switch_wan2prio") & 0x7;
1445		mang_vid  = nvram_get_int("switch_wan3tagid") & 0x0fff;
1446		mang_prio = nvram_get_int("switch_wan3prio") & 0x7;
1447
1448		switch_stb = nvram_get_int("switch_stb_x");
1449		if (switch_stb >= 7) {
1450			system("rtkswitch 40 1");			/* admin all frames on all ports */
1451			if(wan_vid) { /* config wan port */
1452				__setup_vlan(wan_vid, 0, 0x00000210);	/* config WAN & WAN_MAC port */
1453			}
1454
1455			if (iptv_vid) { /* config IPTV on wan port */
1456				sprintf(wan_dev, "vlan%d", iptv_vid);
1457				nvram_set("wan10_ifname", wan_dev);
1458				sprintf(port_id, "%d", iptv_vid);
1459				eval("vconfig", "add", interface, port_id);
1460
1461				__setup_vlan(iptv_vid, iptv_prio, 0x00000210);	/* config WAN & WAN_MAC port */
1462
1463				if (iptv_prio) { /* config priority */
1464					eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
1465				}
1466			}
1467		}
1468		if (switch_stb >= 8) {
1469			if (voip_vid) { /* config voip on wan port */
1470				sprintf(wan_dev, "vlan%d", voip_vid);
1471				nvram_set("wan11_ifname", wan_dev);
1472				sprintf(port_id, "%d", voip_vid);
1473				eval("vconfig", "add", interface, port_id);
1474
1475				__setup_vlan(voip_vid, voip_prio, 0x00000210);	/* config WAN & WAN_MAC port */
1476
1477				if (voip_prio) { /* config priority */
1478					eval("vconfig", "set_egress_map", wan_dev, "0", (char *)voip_prio);
1479				}
1480			}
1481		}
1482		if (switch_stb >=9 ) {
1483			if (mang_vid) { /* config tr069 on wan port */
1484				sprintf(wan_dev, "vlan%d", mang_vid);
1485				nvram_set("wan12_ifname", wan_dev);
1486				sprintf(port_id, "%d", mang_vid);
1487				eval("vconfig", "add", interface, port_id);
1488
1489				__setup_vlan(mang_vid, mang_prio, 0x00000210);	/* config WAN & WAN_MAC port */
1490
1491				if (mang_prio) { /* config priority */
1492					eval("vconfig", "set_egress_map", wan_dev, "0", (char *)iptv_prio);
1493				}
1494			}
1495		}
1496	}
1497#endif
1498}
1499
1500#ifdef RA_SINGLE_SKU
1501void reset_ra_sku(const char *location, const char *country, const char *reg_spec)
1502{
1503	const char *try_list[] = { reg_spec, location, country, "CE", "FCC"};
1504	int i;
1505	for (i = 0; i < ARRAY_SIZE(try_list); i++) {
1506		if(try_list[i] != NULL && setRegSpec(try_list[i], 0) == 0)
1507			break;
1508	}
1509
1510	if(i >= ARRAY_SIZE(try_list)) {
1511		cprintf("## NO SKU suit for %s\n", location);
1512		return;
1513	}
1514
1515	cprintf("using %s SKU for %s\n", try_list[i], location);
1516	gen_ra_sku(try_list[i]);
1517}
1518#endif	/* RA_SINGLE_SKU */
1519
1520