/
lan.c
6802 lines (6043 loc) · 184 KB
/
lan.c
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2005, Broadcom Corporation
All Rights Reserved.
THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
*/
/*
wificonf, OpenWRT
Copyright (C) 2005 Felix Fietkau <nbd@vd-s.ath.cx>
*/
/*
Modified for Tomato Firmware
Portions, Copyright (C) 2006-2009 Jonathan Zarate
*/
#include <shared.h>
#include <rc.h>
#ifndef UU_INT
typedef u_int64_t u64;
typedef u_int32_t u32;
typedef u_int16_t u16;
typedef u_int8_t u8;
#endif
#include <linux/types.h>
#include <linux/sockios.h>
#if !defined(__GLIBC__) && !defined(__UCLIBC__) /* musl */
#include <netinet/if_ether.h> //have to in front of <linux/ethtool.h> to avoid redefinition of 'struct ethhdr'
#endif
#include <linux/ethtool.h>
#include <sys/ioctl.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <wlutils.h>
#include <limits.h> //PATH_MAX, LONG_MIN, LONG_MAX
#ifdef CONFIG_BCMWL5
#include <bcmparams.h>
#include <wlioctl.h>
#include <security_ipc.h>
#ifndef WL_BSS_INFO_VERSION
#error WL_BSS_INFO_VERSION
#endif
#if WL_BSS_INFO_VERSION >= 108
#include <etioctl.h>
#else
#include <etsockio.h>
#endif
#include <wlif_utils.h>
#endif /* CONFIG_BCMWL5 */
#ifndef RTCONFIG_DUALWAN
#include <rtstate.h> /* for get_wanunit_by_type inline function */
#endif
#ifdef RTCONFIG_RALINK
#include <ralink.h>
#endif
#ifdef RTCONFIG_REALTEK
#include "realtek.h"
#include "sysdeps/realtek/mib_adapter/rtk_wifi_drvmib.h"
#endif
#ifdef RTCONFIG_QCA
#include <qca.h>
#endif
#ifdef RTCONFIG_ALPINE
#include <alpine.h>
#endif
#ifdef RTCONFIG_LANTIQ
#include <lantiq.h>
#endif
#ifdef RTCONFIG_DPSTA
#include <dpsta_linux.h>
#endif
#ifdef HND_ROUTER
#include <linux/if_bridge.h>
#endif /* HND_ROUTER */
#ifdef RTCONFIG_AMAS
#include <cfg_onboarding.h>
#endif
#ifdef RTCONFIG_CFGSYNC
#include <cfg_event.h>
#endif
#ifdef RTCONFIG_AMAS_ADTBW
#include <amas_adtbw.h>
#endif
#if defined(RTCONFIG_NOTIFICATION_CENTER)
#include <libnt.h>
#include <json.h>
#endif
const int ifup_vap =
#if defined(RTCONFIG_QCA)
0;
#else
1;
#endif
#define sin_addr(s) (((struct sockaddr_in *)(s))->sin_addr)
static time_t s_last_lan_port_stopped_ts = 0;
void update_lan_state(int state, int reason)
{
char prefix[32];
char tmp[100], tmp1[100], *ptr;
snprintf(prefix, sizeof(prefix), "lan_");
_dprintf("%s(%s, %d, %d)\n", __FUNCTION__, prefix, state, reason);
#ifdef RTCONFIG_QTN /* AP and Repeater mode, workaround to infosvr, RT-AC87U bug#38, bug#44, bug#46 */
_dprintf("%s(workaround to infosvr)\n", __FUNCTION__);
eval("ifconfig", "br0:0", "169.254.39.3", "netmask", "255.255.255.0");
if(*nvram_safe_get("QTN_RPC_CLIENT"))
eval("ifconfig", "br0:0", nvram_safe_get("QTN_RPC_CLIENT"), "netmask", "255.255.255.0");
else
eval("ifconfig", "br0:0", "169.254.39.1", "netmask", "255.255.255.0");
#endif
nvram_set_int(strcat_r(prefix, "state_t", tmp), state);
nvram_set_int(strcat_r(prefix, "sbstate_t", tmp), 0);
if(state==LAN_STATE_INITIALIZING)
{
//if(nvram_match(strcat_r(prefix, "proto", tmp), "dhcp")) {
// // always keep in default ip before getting ip
// nvram_set(strcat_r(prefix, "dns", tmp), nvram_default_get("lan_ipaddr"));
//}
ptr = nvram_get_int(strcat_r(prefix, "dnsenable_x", tmp)) ? "" :
get_userdns_r(prefix, tmp1, sizeof(tmp1));
nvram_set(strcat_r(prefix, "dns", tmp), ptr);
}
else if(state==LAN_STATE_STOPPED) {
// Save Stopped Reason
// keep ip info if it is stopped from connected
nvram_set_int(strcat_r(prefix, "sbstate_t", tmp), reason);
}
}
/* Check NVRam to see if "name" is explicitly enabled */
static inline int
wl_vif_enabled(const char *name, char *tmp)
{
if (name == NULL) return 0;
return (nvram_get_int(strcat_r(name, "_bss_enabled", tmp)));
}
#ifdef RTCONFIG_EMF
void
emf_mfdb_update(char *lan_ifname, char *lan_port_ifname, bool add)
{
char word[256], *next;
char *mgrp, *ifname;
/* Add/Delete MFDB entries corresponding to new interface */
foreach(word, nvram_safe_get("emf_entry"), next) {
ifname = word;
mgrp = strsep(&ifname, ":");
if ((mgrp == 0) || (ifname == 0))
continue;
/* Add/Delete MFDB entry using the group addr and interface */
if (strcmp(lan_port_ifname, ifname) == 0) {
eval("emf", ((add) ? "add" : "del"),
"mfdb", lan_ifname, mgrp, ifname);
}
}
return;
}
void
emf_uffp_update(char *lan_ifname, char *lan_port_ifname, bool add)
{
char word[256], *next;
char *ifname;
/* Add/Delete UFFP entries corresponding to new interface */
foreach(word, nvram_safe_get("emf_uffp_entry"), next) {
ifname = word;
if (ifname == 0)
continue;
/* Add/Delete UFFP entry for the interface */
if (strcmp(lan_port_ifname, ifname) == 0) {
eval("emf", ((add) ? "add" : "del"),
"uffp", lan_ifname, ifname);
}
}
return;
}
void
emf_rtport_update(char *lan_ifname, char *lan_port_ifname, bool add)
{
char word[256], *next;
char *ifname;
/* Add/Delete RTPORT entries corresponding to new interface */
foreach(word, nvram_safe_get("emf_rtport_entry"), next) {
ifname = word;
if (ifname == 0)
continue;
/* Add/Delete RTPORT entry for the interface */
if (strcmp(lan_port_ifname, ifname) == 0) {
eval("emf", ((add) ? "add" : "del"),
"rtport", lan_ifname, ifname);
}
}
return;
}
void
start_emf(char *lan_ifname)
{
char word[256], *next;
char *mgrp, *ifname;
#ifdef BLUECAVE
return;
#endif
#ifdef RTCONFIG_DSL_BCM
//snooping control by bcmmcastctl
if (nvram_get_int("emf_enable")) {
eval("bcmmcastctl", "mode", "-i", "br0", "-p", "1", "-m", (psta_exist() || psr_exist() || (sw_mode() == SW_MODE_AP)) ? "0" : "1");
eval("bcmmcastctl", "mode", "-i", "br0", "-p", "2", "-m", (psta_exist() || psr_exist() || (sw_mode() == SW_MODE_AP)) ? "0" : "1");
}
else {
eval("bcmmcastctl", "mode", "-i", "br0", "-p", "1", "-m", "0");
eval("bcmmcastctl", "mode", "-i", "br0", "-p", "2", "-m", "0");
}
return;
#endif
#ifdef HND_ROUTER
#ifdef MCPD_PROXY
/* Disable EMF.
* Since Runner is involved in Ethernet side when MCPD is enabled
*/
if (nvram_get_int("emf_enable")) {
nvram_set_int("emf_enable", 0);
nvram_commit();
}
#endif
if (nvram_match("switch_wantag", "hinet_mesh")) {
eval("bcmmcastctl", "mode", "-i", "br0", "-p", "1", "-m", "1");
eval("bcmmcastctl", "mode", "-i", "br0", "-p", "2", "-m", "1");
return;
}
#ifdef RTCONFIG_PROXYSTA
#define BCM_MCAST_SNOOPING_DISABLED_FLOOD "0" /* snooping is disabled, IP multicast is flooded */
#define BCM_MCAST_SNOOPING_STANDARD_MODE "1" /* snoping is enabled, unsolicited IP multicast is flooded */
#define BCM_MCAST_SNOOPING_BLOCKING_MODE "2" /* snoping is enabled, unsolicited IP mutlicast is dropped */
#ifdef RTCONFIG_HND_ROUTER_AX
int bcm_client_mode = psta_exist() || psr_exist();
int bcm_mcast_snooping_disabled = (sw_mode() == SW_MODE_AP) && !bcm_client_mode && !nvram_get_int("bcm_snooping");
eval("bcmmcastctl", "mode", "-i", "br0", "-p", "1", "-m", bcm_mcast_snooping_disabled ? BCM_MCAST_SNOOPING_DISABLED_FLOOD : BCM_MCAST_SNOOPING_STANDARD_MODE);
eval("bcmmcastctl", "mode", "-i", "br0", "-p", "2", "-m", bcm_mcast_snooping_disabled ? BCM_MCAST_SNOOPING_DISABLED_FLOOD : BCM_MCAST_SNOOPING_STANDARD_MODE);
#else
eval("bcmmcastctl", "mode", "-i", "br0", "-p", "1", "-m", BCM_MCAST_SNOOPING_STANDARD_MODE);
eval("bcmmcastctl", "mode", "-i", "br0", "-p", "2", "-m", BCM_MCAST_SNOOPING_STANDARD_MODE);
#endif
#endif
return;
#endif
#ifdef HND_ROUTER
if (!nvram_get_int("emf_enable"))
#else
if (!nvram_get_int("emf_enable")
#ifdef RTCONFIG_BCMWL6
&& !wl_igs_enabled()
#endif
)
#endif
return;
/* Start EMF */
eval("emf", "start", lan_ifname);
/* Add the static MFDB entries */
foreach(word, nvram_safe_get("emf_entry"), next) {
ifname = word;
mgrp = strsep(&ifname, ":");
if ((mgrp == 0) || (ifname == 0))
continue;
/* Add MFDB entry using the group addr and interface */
eval("emf", "add", "mfdb", lan_ifname, mgrp, ifname);
}
/* Add the UFFP entries */
foreach(word, nvram_safe_get("emf_uffp_entry"), next) {
ifname = word;
if (ifname == 0)
continue;
/* Add UFFP entry for the interface */
eval("emf", "add", "uffp", lan_ifname, ifname);
}
/* Add the RTPORT entries */
foreach(word, nvram_safe_get("emf_rtport_entry"), next) {
ifname = word;
if (ifname == 0)
continue;
/* Add RTPORT entry for the interface */
eval("emf", "add", "rtport", lan_ifname, ifname);
}
return;
}
static void stop_emf(char *lan_ifname)
{
#if defined(HND_ROUTER) && defined(RTCONFIG_PROXYSTA)
eval("bcmmcastctl", "mode", "-i", "br0", "-p", "1", "-m", "0");
eval("bcmmcastctl", "mode", "-i", "br0", "-p", "2", "-m", "0");
#endif
/* Stop the EMF for this LAN */
eval("emf", "stop", lan_ifname);
/* Remove Bridge from igs */
eval("igs", "del", "bridge", lan_ifname);
eval("emf", "del", "bridge", lan_ifname);
}
#endif
void stop_snooper(void)
{
#if defined(CONFIG_BCMWL5) || defined(RTCONFIG_RALINK)
killall_tk("snooper");
#endif
}
void start_snooper(void)
{
#if defined(CONFIG_BCMWL5) || defined(RTCONFIG_RALINK)
char *lan_ifname = nvram_safe_get("lan_ifname");
char word[64], *next, *flood;
stop_snooper();
if (!nvram_get_int("emf_enable"))
return;
#if defined(RTCONFIG_RALINK) && defined(RTCONFIG_RALINK_MT7621) /* RT-N56UB1, RT-N56UB2 */
flood = "-x";
#else
flood = nvram_get_int("switch_stb_x") ? "-x" : NULL;
#endif
foreach (word, nvram_safe_get("lan_ifnames"), next) {
if (eval("/usr/sbin/snooper", "-b", lan_ifname, "-s", word, flood) == 0)
break;
}
#endif
}
void start_wl(void)
{
#ifdef CONFIG_BCMWL5
char lan_ifname[16], *lan_ifnames, *ifname, *p;
int unit, subunit;
int is_client = 0;
char tmp[100], tmp2[100], prefix[] = "wlXXXXXXXXXXXXXX";
snprintf(lan_ifname, sizeof(lan_ifname), "%s", nvram_safe_get("lan_ifname"));
if (strncmp(lan_ifname, "br", 2) == 0) {
if ((lan_ifnames = strdup(nvram_safe_get("lan_ifnames"))) != NULL) {
p = lan_ifnames;
while ((ifname = strsep(&p, " ")) != NULL) {
while (*ifname == ' ') ++ifname;
if (*ifname == 0) break;
unit = -1; subunit = -1;
#ifdef RTCONFIG_QTN
if (!strcmp(ifname, "wifi0")) continue;
#endif
// ignore disabled wl vifs
if (strncmp(ifname, "wl", 2) == 0) {
if (get_ifname_unit(ifname, &unit, &subunit) < 0)
continue;
if (!wl_vif_enabled(ifname, tmp)) {
continue; /* Ignore dsiabled WL VIF */
}
}
// get the instance number of the wl i/f
else if (wl_ioctl(ifname, WLC_GET_INSTANCE, &unit, sizeof(unit)))
continue;
is_client |= wl_client(unit, subunit) && nvram_get_int(wl_nvname("radio", unit, 0));
#ifdef CONFIG_BCMWL5
snprintf(prefix, sizeof(prefix), "wl%d_", unit);
if (nvram_match(strcat_r(prefix, "radio", tmp), "0")
#if defined(RTCONFIG_BCMWL6) && defined(RTCONFIG_PROXYSTA)
|| psta_exist_except(unit)
#endif
) {
nvram_set_int(strcat_r(prefix, "timesched", tmp2), 0); // disable wifi time-scheduler
#ifdef GT10
ledbh_war(ifname);
#endif
eval("wlconf", ifname, "down");
eval("wl", "-i", ifname, "radio", "off");
} else{
eval("wlconf", ifname, "start"); /* start wl iface */
}
wlconf_post(ifname);
#endif // CONFIG_BCMWL5
}
free(lan_ifnames);
}
}
else if (strcmp(lan_ifname, "")) {
/* specific non-bridged lan iface */
eval("wlconf", lan_ifname, "start");
}
#if 0
killall("wldist", SIGTERM);
eval("wldist");
#endif
if (is_client)
xstart("radio", "join");
#ifdef RTCONFIG_PORT_BASED_VLAN
start_vlan_wl();
#endif
/* manual turn on 5g led for some gpio-ctrl-5g-led */
#ifdef RTCONFIG_BCMWL6
#if defined(RTAC66U) || defined(BCM4352)
if (nvram_match("wl1_radio", "1"))
{
#ifndef RTCONFIG_LED_BTN
if (!(sw_mode()==SW_MODE_AP && nvram_get_int("wlc_psta") && nvram_get_int("wlc_band")==0)) {
nvram_set("led_5g", "1");
led_control(LED_5G, LED_ON);
}
#else
nvram_set("led_5g", "1");
if (nvram_get_int("AllLED"))
led_control(LED_5G, LED_ON);
#endif
}
else
{
nvram_set("led_5g", "0");
led_control(LED_5G, LED_OFF);
}
#endif
#if defined(RTAX82U) && !defined(RTCONFIG_BCM_MFG)
if (!nvram_get_int("LED_order") && nvram_match("AllLED", "1"))
led_control(LED_5G, nvram_match("wl1_radio", "1") ? LED_ON: LED_OFF);
#endif
#endif
#endif /* CONFIG_BCMWL5 */
#if defined(RTCONFIG_USER_LOW_RSSI)
init_wllc();
#endif
#ifdef RTCONFIG_QTN
start_qtn_monitor();
#endif
#ifdef RTCONFIG_QSR10G
if(nvram_get_int("qtn_ready")==1){
_dprintf("[%s][%d] call qtn_monitor()\n", __func__, __LINE__);
qtn_monitor_main();
}
#endif
#ifdef RTCONFIG_LANTIQBAK
_dprintf("[%s][%d] call wave_monitor()-01\n", __func__, __LINE__);
nvram_set("wave_action", "1");
kill_pidfile_s("/var/run/wave_monitor.pid", SIGUSR1);
_dprintf("[%s][%d] call wave_monitor()-02\n", __func__, __LINE__);
#endif
#if defined(RTCONFIG_RALINK_MT7621)
setup_smp(); /* for adjust smp_affinity of cpu */
#endif
nvram_set("reload_svc_radio", "1");
#ifndef RTCONFIG_QCA
nvram_set_int("wlready", 1);
timecheck();
#endif
#ifdef RTCONFIG_CFGSYNC
if (
#ifdef RTCONFIG_WIFI_SON
!nvram_match("wifison_ready", "1") &&
#endif /* WIFI_SON */
is_cfg_relist_exist())
update_macfilter_relist();
#endif
}
void stop_wl(void)
{
}
int
chk_inlan(char *chk_ip)
{
char word[80], *next;
char *ipaddr, *netmask, *gateway, *metric;
char *buf;
int ret = 0;
if (!chk_ip)
return 0;
buf = strdup(nvram_safe_get("lan_route"));
if (buf == NULL)
return 0;
foreach(word, buf, next) {
netmask = word;
ipaddr = strsep(&netmask, ":");
if (!ipaddr || !netmask)
continue;
gateway = netmask;
netmask = strsep(&gateway, ":");
if (!netmask || !gateway)
continue;
metric = gateway;
gateway = strsep(&metric, ":");
if (!gateway || !metric)
continue;
if (((inet_addr(chk_ip) & inet_addr(netmask)) == (inet_addr(ipaddr) & inet_addr(netmask)))) {
ret = 1;
break;
}
}
free(buf);
return ret;
}
static int
add_lan_routes(char *lan_ifname)
{
return add_routes("lan_", "route", lan_ifname);
}
static int
del_lan_routes(char *lan_ifname)
{
return del_routes("lan_", "route", lan_ifname);
}
char *get_hwaddr(const char *ifname)
{
int s = -1;
struct ifreq ifr;
char eabuf[32];
char *p = NULL;
if (ifname == NULL) return NULL;
if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) return NULL;
strcpy(ifr.ifr_name, ifname);
if (ioctl(s, SIOCGIFHWADDR, &ifr)) goto error;
p = strdup(ether_etoa((const unsigned char *)ifr.ifr_hwaddr.sa_data, eabuf));
error:
close(s);
return p;
}
void set_hwaddr(const char *ifname, const char *mac)
{
int s;
struct ifreq ifr;
unsigned char ea[ETHER_ADDR_LEN];
if (ifname == NULL || mac == NULL)
return;
if (!ether_atoe(mac, ea))
return;
if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
return;
strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
memcpy(ifr.ifr_hwaddr.sa_data, ea, ETHER_ADDR_LEN);
if (ioctl(s, SIOCSIFHWADDR, &ifr)) {
dbg("%s: error setting MAC address of %s to %s\n", __FUNCTION__, ifname, mac);
}
close(s);
}
#ifdef RTCONFIG_IPV6
void ipv6_sysconf(const char *ifname, const char *name, int value)
{
char path[PATH_MAX], sval[16];
if (ifname == NULL || name == NULL)
return;
snprintf(path, sizeof(path), "/proc/sys/net/ipv6/conf/%s/%s", ifname, name);
snprintf(sval, sizeof(sval), "%d", value);
f_write_string(path, sval, 0, 0);
}
int ipv6_getconf(const char *ifname, const char *name)
{
char path[PATH_MAX], sval[16];
if (ifname == NULL || name == NULL)
return 0;
snprintf(path, sizeof(path), "/proc/sys/net/ipv6/conf/%s/%s", ifname, name);
if (f_read_string(path, sval, sizeof(sval)) <= 0)
return 0;
return atoi(sval);
}
void set_default_accept_ra(int flag)
{
ipv6_sysconf("all", "accept_ra", flag ? 1 : 0);
ipv6_sysconf("default", "accept_ra", flag ? 1 : 0);
}
void set_default_accept_ra_defrtr(int flag)
{
ipv6_sysconf("all", "accept_ra_defrtr", flag ? 1 : 0);
ipv6_sysconf("default", "accept_ra_defrtr", flag ? 1 : 0);
}
void set_default_forwarding(int flag)
{
ipv6_sysconf("all", "forwarding", flag ? 1 : 0);
ipv6_sysconf("default", "forwarding", flag ? 1 : 0);
}
void set_intf_ipv6_dad(const char *ifname, int addbr, int flag)
{
if (ifname == NULL) return;
ipv6_sysconf(ifname, "accept_dad", flag ? 2 : 0);
if (flag)
ipv6_sysconf(ifname, "dad_transmits", addbr ? 2 : 1);
}
void enable_ipv6(const char *ifname)
{
if (ifname == NULL) return;
ipv6_sysconf(ifname, "disable_ipv6", 0);
}
void disable_ipv6(const char *ifname)
{
if (ifname == NULL) return;
ipv6_sysconf(ifname, "disable_ipv6", 1);
}
void config_ipv6(int enable, int incl_wan)
{
DIR *dir;
struct dirent *dirent;
char word[256], *next;
char prefix[sizeof("wanXXXXXXXXXX_")], *wan_proto;
int service, match, accept_defrtr;
if (enable) {
enable_ipv6("default");
enable_ipv6("all");
} else {
disable_ipv6("default");
disable_ipv6("all");
}
if ((dir = opendir("/proc/sys/net/ipv6/conf")) != NULL) {
while ((dirent = readdir(dir)) != NULL) {
if (dirent->d_name[0] == '.' ||
strcmp(dirent->d_name, "all") == 0 ||
strcmp(dirent->d_name, "default") == 0)
continue;
if (!incl_wan) {
match = 0;
foreach (word, nvram_safe_get("wan_ifnames"), next) {
if (strcmp(dirent->d_name, word) == 0) {
match = 1;
break;
}
}
if (match)
continue;
}
if (enable) {
enable_ipv6(dirent->d_name);
if (!with_ipv6_linklocal_addr(dirent->d_name)) {
reset_ipv6_linklocal_addr(dirent->d_name, 0);
enable_ipv6(dirent->d_name);
}
} else
disable_ipv6(dirent->d_name);
}
closedir(dir);
}
if (is_routing_enabled())
{
service = get_ipv6_service();
switch (service) {
case IPV6_NATIVE_DHCP:
#ifdef RTCONFIG_6RELAYD
case IPV6_PASSTHROUGH:
#endif
snprintf(prefix, sizeof(prefix), "wan%d_", wan_primary_ifunit_ipv6());
wan_proto = nvram_safe_get(strcat_r(prefix, "proto", word));
accept_defrtr = service == IPV6_NATIVE_DHCP && /* limit to native by now */
strcmp(wan_proto, "dhcp") != 0 && strcmp(wan_proto, "static") != 0 &&
nvram_match(ipv6_nvname("ipv6_ifdev"), "ppp") ?
nvram_get_int(ipv6_nvname("ipv6_accept_defrtr")) : 1;
set_default_accept_ra(1);
set_default_accept_ra_defrtr(accept_defrtr);
break;
case IPV6_6IN4:
case IPV6_6TO4:
case IPV6_6RD:
case IPV6_MANUAL:
default:
set_default_accept_ra(0);
break;
}
set_default_forwarding(1);
}
else set_default_accept_ra(0);
}
void start_lan_ipv6(void)
{
char lan_ifname[16];
int unit, ipv6_service = 0;
strlcpy(lan_ifname, nvram_safe_get("lan_ifname"), sizeof(lan_ifname));
for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit)
if (get_ipv6_service_by_unit(unit) != IPV6_DISABLED) {
ipv6_service = 1;
break;
}
if (!ipv6_service)
return;
set_intf_ipv6_dad(lan_ifname, 0, 1);
config_ipv6(ipv6_enabled() && is_routing_enabled(), 0);
start_ipv6();
}
void stop_lan_ipv6(void)
{
char lan_ifname[16];
strlcpy(lan_ifname, nvram_safe_get("lan_ifname"), sizeof(lan_ifname));
stop_ipv6();
set_intf_ipv6_dad(lan_ifname, 0, 0);
config_ipv6(0, 1);
}
#ifdef RTCONFIG_DUALWAN
void restart_dnsmasq_ipv6(void)
{
char dualwan_wans[16];
char dualwan_mode[16];
snprintf(dualwan_wans, sizeof(dualwan_wans), "%s", nvram_safe_get("wans_dualwan"));
snprintf(dualwan_mode, sizeof(dualwan_mode), "%s", nvram_safe_get("wans_mode"));
if (!(!strstr(dualwan_wans, "none") &&
(!strcmp(dualwan_mode, "fo") || !strcmp(dualwan_mode, "fb"))))
return;
if (!ipv6_enabled())
return;
start_dnsmasq();
}
#endif
#endif
#ifdef RTCONFIG_DPSTA
static int
dpsta_ioctl(char *name, void *buf, int len)
{
struct ifreq ifr;
int ret = 0;
int s;
/* open socket to kernel */
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
return errno;
}
strncpy(ifr.ifr_name, name, IFNAMSIZ);
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
ifr.ifr_data = (caddr_t)buf;
if ((ret = ioctl(s, SIOCDEVPRIVATE, &ifr)) < 0)
perror(ifr.ifr_name);
/* cleanup */
close(s);
return ret;
}
#endif
/**
* @return:
* 0: invalid bonding policy.
* otherwise: valid bonding policy.
*/
static int check_bonding_mode(const char *mode)
{
int v, ret = 0;
char *bonding_mode[] = {
"balance-rr",
"active-backup",
"balance-xor",
"broadcast",
"802.3ad",
"balance-tlb",
"balance-alb",
NULL,
}, **p;
if (!mode)
return 0;
if (isdigit(*mode) && strlen(mode) == 1) {
v = atoi(mode);
if (v < 0 || v > 6)
return 0;
ret = 1;
} else {
for (p = bonding_mode; *p != NULL; p++) {
if (!strcmp(mode, *p))
return 1;
}
}
return ret;
}
/**
* @return:
* 0: invalid bonding mode.
* otherwise: valid bonding mode.
*/
static int check_bonding_policy(const char *policy)
{
int ret = 0;
char *bonding_policy[] = {
"layer2",
"layer2+3",
"layer3+4",
"encap2+3",
"encap3+4",
NULL,
}, **p;
if (!policy)
return 0;
for (p = bonding_policy; *p != NULL; p++) {
if (!strcmp(policy, *p))
return 1;
}
return ret;
}
/* Add interfaces in @slaves to @bond_if interface.
* @bond_if: bonding interface name, it must be created before using this function.
* @slaves: space char seperated string, e.g., "eth1 eth2".
* If it's readed from a sysfs attribute, make sure trailing new-line character removed.
* @return:
* 0: successful
* otherwise: error
*/
static int add_slaves_to_bonding_iface(const char *bond_if, char *slaves)
{
const char *bonding_entry = "/sys/class/net/%s/bonding/%s";
char *p, *next, path[256], iface[IFNAMSIZ], value[IFNAMSIZ + 1], slave_ifs[20 * IFNAMSIZ];
char mac[sizeof("00:00:00:00:00:00")] = { 0 };
unsigned char ea[ETHER_ADDR_LEN];
int r = 0;
if (!bond_if || *bond_if == '\0' || !slaves || *slaves == '\0')
return -1;
if ((p = get_hwaddr(bond_if)) != NULL) {
strlcpy(mac, p, sizeof(mac));
ether_atoe(mac, ea);
free(p);
}
snprintf(path, sizeof(path), bonding_entry, bond_if, "slaves");
*slave_ifs = '\0';
f_read_string(path, slave_ifs, sizeof(slave_ifs));
trimNL(slave_ifs);
foreach (iface, slaves, next) {
if (find_word(slave_ifs, iface))
continue;
ifconfig(iface, 0, NULL, NULL);
if (*mac != '\0') {
set_hwaddr(iface, mac);
ether_inc(ea, 1);
ether_etoa(ea, mac);
}
snprintf(value, sizeof(value), "+%s", iface);
if (f_write_string(path, value, 0, 0) <= 0)
r++;
}
return r;
}
/* Remove slave interfaces from @bond_if interface.
* @bond_if: bonding interface name, it must be created before using this function.
* @return:
* 0: successful
* otherwise: error
*/
static int remove_slaves_from_bonding_iface(const char *bond_if)
{
const char *bonding_entry = "/sys/class/net/%s/bonding/%s";
char *next, path[256], iface[IFNAMSIZ], value[IFNAMSIZ + 1], slave_ifs[20 * IFNAMSIZ];
int r = 0;
if (!bond_if || *bond_if == '\0')
return -1;
snprintf(path, sizeof(path), bonding_entry, bond_if, "slaves");
*slave_ifs = '\0';
f_read_string(path, slave_ifs, sizeof(slave_ifs));
trimNL(slave_ifs);
foreach (iface, slave_ifs, next) {
ifconfig(iface, 0, NULL, NULL);
snprintf(value, sizeof(value), "-%s", iface);
if (f_write_string(path, value, 0, 0) <= 0)
r++;
}
return r;
}
int set_bonding(const char *bond_if, const char *mode, const char *policy, char *hwaddr)
{
const char *bonding_masters = "/sys/class/net/bonding_masters";
const char *bonding_entry = "/sys/class/net/%s/bonding/%s";
#if defined(RTCONFIG_SWITCH_RTL8370M_PHY_QCA8033_X2) || \
defined(RTCONFIG_SWITCH_RTL8370MB_PHY_QCA8033_X2)
const char *default_mode = "balance-xor"; // using "802.3ad" the Tx packet would go through eth0 almost.
int wans = get_wans_dualwan();
#else
const char *default_mode = "802.3ad";
#endif
const char *default_policy = "layer3+4";
char path[256], value[32], slave_ifs[IFNAMSIZ * 11], *p = NULL;
unsigned char mac[ETHER_ADDR_LEN], old_mac[ETHER_ADDR_LEN] = { 0 };