-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathfirewall.c
More file actions
15437 lines (13714 loc) · 581 KB
/
firewall.c
File metadata and controls
15437 lines (13714 loc) · 581 KB
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
/*
* If not stated otherwise in this file or this component's Licenses.txt file the
* following copyright and licenses apply:
*
* Copyright 2015 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**********************************************************************
Copyright [2014] [Cisco Systems, Inc.]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**********************************************************************/
/*
============================================================================
Introduction to IPv4 Firewall
-------------------------------
The firewall is based on iptables. It uses the mangle, nat, and filters tables,
and for each of these, it add several subtables.
The reason for using subtables is that a subtable represents a block of rules
which can be erased (using -F), and reconstituted using syscfg and sysevent,
without affecting the rest of the firewall. That makes its easier to organize
a complex firewall into smaller functional groups.
The main tables, INPUT OUTPUT, and FORWARD, contain jumps to subtables that better represent
a Utopia firewall: wan2self, lan2self, lan2wan, wan2wan. Each of these subtables
further specifies the order of rules and jumps to further subtables.
As mentioned earlier, the firewall is iptables based. There are two ways to use iptables:
iptables-restore using an input file, or issuing a series of iptables commands. Using iptables-restore
disrupts netfilters connection tracking which causes established connections to appear to be invalid.
Using iptables is slower, and it requires that Utopia firewall table structure already exists. This means
that it cannot be used to initially structure the firewall.
The behavior of firewall.c is to check whether the iptables file (/tmp/.ipt)
exists. If it doesn't exist, then a new one is created and instantiated via iptables-restore.
On the other hand if .ipt already exists, then all subtables are flushed and reconstituted
using iptables rules.
Here is a list of subtables and how each subtable is populated:
Note that some syscfg/sysevent tuples are used to populate more than one subtable
raw
---
prerouting_ephemeral:
output_ephemeral:
Rules are made from:
-sysevent RawFirewallRule
prerouting_raw:
output_raw:
Rules are made from:
- syscfg set RawTableFirewallRule
prerouting_nowan:
output_nowan:
Rules are made when current_wan_ipaddr is 0.0.0.0
mangle
-----
prerouting_trigger:
Rules are made from:
- syscfg PortRangeTrigger_x
prerouting_qos:
postrouting_qos:
Rules are made from:
- syscfg QoSPolicy_x
- syscfg QoSUserDefinedPolicy_x
- syscfg QoSDefinedPolicy_x
- syscfg QoSMacAddr_x
- syscfg QoSVoiceDevice_x
postrouting_lan2lan
Rules are made from:
- syscfg block_nat_redirection
nat
---
prerouting_fromwan:
- syscfg SinglePortForward_x
- syscfg PortRangeFoward_x
- syscfg WellKnownPortForward_x
- sysevent portmap_dyn_pool
prerouting_mgmt_override:
- syscfg mgmt_httpaccess
- syscfg mgmt_httpsaccess
- syscfg http_admin_port
prerouting_plugins:
Root of subtables used by plugins such as parental control which use a local logic
to provision its subtables
prerouting_fromwan_todmz:
- syscfg dmz_enabled
prerouting_fromlan:
postrouting_tolan:
Rules are made from:
- syscfg SinglePortForward_x
- syscfg PortRangeFoward_x
- syscfg WellKnownPortForward_x
- sysevent portmap_dyn_pool
postrouting_towan:
Rules are made from:
- syscfg nat_enabled
prerouting_ephemeral:
postrouting_ephemeral:
powerful rules that are run early on the PREROUTING|POSTROUTING chain
Rules are made from:
- sysevent NatFirewallRule
xlog_drop_lanattack:
attacks from lan
postrouting_plugins:
Root of subtables used by plugins such as parental control which use a local logic
to provision its subtables
filter
------
The filter table splits traffic into chains based on the incoming interface and the destination.
Traffic specific chains are:
lan2wan
wan2lan
lan2self
wan2self
Each chain further classifies traffic, and acts upon the traffic that fits the rule's criterea
general_input:
general_output:
general_forward:
powerful rules that are run early on the INPUT/OUTPUT/FORWARD chains
Rules are made from:
- syscfg GeneralPurposeFirewallRule_x
- sysevent GeneralPurposeFirewallRule
- a DNAT trigger (via GeneralPurposeFirewallRule)
lan2wan:
used to jump to other sub tables that are interested in traffic from lan to wan
lan2wan_disable:
Rules ase made from:
- If nat is disable all lan to wan traffic dorped
lan2wan_misc:
Rules are made from:
- sysevent get current_wan_ipaddr. If the current_wan_ipaddr is 0.0.0.0 then
there is no lan to wan traffic allowed
- sysevent ppp_clamp_mtu
lan2wan_triggers:
Rules are made from:
- syscfg PortRangeTrigger_x
lan2wan_webfilters:
Rules are made from:
- syscfg block_webproxy
- syscfg block_java
- syscfg block_activex
- syscfg block_cookies
lan2wan_iap :
Rules are made from:
- syscfg InternetAccessPolicy_x
This subtable is used to hold Internet Access Policy subtables
* namespace_classification
* namespace_rules
lan2wan_plugins :
Root of subtables used by plugins such as parental control which use a local logic
to provision its subtables
wan2lan:
used to jump to other tables that are interested in traffic from wan to lan
wan2lan_disabled:
Rules are made from:
- sysevent get current_wan_ipaddr. If the current_wan_ipaddr is 0.0.0.0 then
there is no wan to lan traffic allowed
wan2lan_forwarding_accept:
Rules are made from:
- syscfg SinglePortForward_x
- syscfg PortRangeFoward_x
- syscfg WellKnownPortForward_x
- sysevent portmap_dyn_pool
- syscfg StaticRoute_x
wan2lan_misc:
Rules are made from:
- syscfg W2LFirewallRule_x
- syscfg W2LWellKnownFirewallRule_x
- sysevent ppp_clamp_mtu
wan2lan_accept:
Rules are accept multicast
wan2lan_nonat:
When nat is disabled then firewall doesn't block forwarding to lan hosts
Rules are made from:
- syscfg nat_enabled (if not enabled)
wan2lan_plugins:
Root of subtables used by plugins such as parental control which use a local logic
to provision its subtables
wan2lan_dmz:
Rules are made from:
- syscfg dmz_enabled
lan2self:
used to jump to other tables that are interested in traffic from lan to utopia
These tables are:
lan2self_mgmt
lan2self_attack
host_detect
lan2self_mgmt:
Rules are made from:
- syscfg mgmt_wifi_access
lanattack:
Rules are made from well known rules to protect from attacks on our trusted interface
host_detect:
Rules are made dynamically as lan host are discovered
lan2self_plugins:
Root of subtables used by plugins such as parental control which use a local logic
to provision its subtables
self2lan:
used to jump to other tables that are interested in traffic from utopia to the lan
These tables are:
self2lan_plugins
self2lan_plugins:
Root of subtables used by plugins such as parental control which use a local logic
to provision its subtables
wan2self:
used to jump to other tables that are interested in traffic from wan to utopia
These tables are:
wan2self_ports
wan2self_mgmt
wan2self_attack
wan2self_mgmt:
Rules are made from:
- syscfg mgmt_wan_access
wan2self_ports:
powerful port control for packets from wan to our untrusted interface. They are examined early
and allow accept/deny priviledges for ports/protocols etc.
Rules are made from:
syscfg rip_enabled
syscfg firewall_development_override
syscfg block_ping
syscfg block_multicast
syscfg block_ident
wanattack:
Rules are made from well known rules to protect from attacks on our untrusted interface
terminal rules:
Many rules end with a jump to the appropriate log. In these rules, if logging is turned on, then a
log will be emitted. Otherwise no log is emitted, but the packet will be either accepted or dropped.
xlog_accept_lan2wan:
xlog_accept_wan2lan:
xlog_accept_wan2self:
xlog_drop_wan2lan:
xlog_drop_lan2wan:
xlog_drop_wan2self:
xlog_drop_wanattack:
xlog_drop_lanattack:
xlogdrop:
xlogreject:
Rules are to log and drop/accept/reject
NOTES:
1) Port Range Triggering requires the userspace process "trigger" to be included in the image
Author: enright@cisco.com
Defines used to control conditional compilation
-----------------------------------------------
CONFIG_BUILD_TRIGGER:
Port Range Triggering built in. This requires the userspace process "trigger" to
be built into the image
OBSOLETE:
NOT_DEF:
Not used code, but not yet removed
============================================================================
*/
#include "autoconf.h"
//zqiu: ARRISXB3-893
#ifdef CONFIG_INTEL_NF_TRIGGER_SUPPORT
#define CONFIG_KERNEL_NF_TRIGGER_SUPPORT CONFIG_INTEL_NF_TRIGGER_SUPPORT
#endif
#include"firewall.h"
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <syslog.h>
#include <ctype.h>
#include <ulog/ulog.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/file.h>
#include <sys/mman.h>
#include "secure_wrapper.h"
#include "util.h"
#if defined (WAN_FAILOVER_SUPPORTED) || defined(RDKB_EXTENDER_ENABLED)
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#endif
#ifdef FEATURE_464XLAT
#define XLAT_IF "xlat"
#define XLAT_IP "192.0.0.1"
#endif
#if defined(RDKB_EXTENDER_ENABLED)
char cellular_ifname[32];
#endif
#if defined (_PROPOSED_BUG_FIX_)
#include <linux/version.h>
#endif
#define PORTMAPPING_2WAY_PASSTHROUGH
#define MAX_URL_LEN 1024
#ifdef CONFIG_CISCO_PARCON_WALLED_GARDEN
#define PARCON_WALLED_GARDEN_HTTP_PORT_SITEBLK "18080" // the same as the port in lighttpd.conf
#define PARCON_WALLED_GARDEN_HTTPS_PORT_SITEBLK "10443" // the same as the port in lighttpd.conf
//#define DNS_QUERY_QUEUE_NUM 5
#define DNS_RES_QUEUE_NUM_START 6 //should be the same range as system_defaults-xxx
#define DNS_RES_QUEUE_NUM_END 8
#define DNSV6_RES_QUEUE_NUM_START 9 //should be the same range as system_defaults-xxx
#define DNSV6_RES_QUEUE_NUM_END 10
#define HTTP_GET_QUEUE_NUM_START 11
#define HTTP_GET_QUEUE_NUM_END 12
#define HTTPV6_GET_QUEUE_NUM_START 13
#define HTTPV6_GET_QUEUE_NUM_END 14
#if (HTTP_GET_QUEUE_NUM_END == HTTP_GET_QUEUE_NUM_START)
#define __IPT_GET_QUEUE_CONFIG__(x) "--queue-num " #x
#define _IPT_GET_QUEUE_CONFIG_(x) __IPT_GET_QUEUE_CONFIG__(x)
#define HTTP_GET_QUEUE_CONFIG _IPT_GET_QUEUE_CONFIG_(DNS_RES_QUEUE_NUM_START)
#define DNSR_GET_QUEUE_CONFIG _IPT_GET_QUEUE_CONFIG_(HTTP_GET_QUEUE_NUM_START)
#define HTTPV6_GET_QUEUE_CONFIG _IPT_GET_QUEUE_CONFIG_(DNSV6_RES_QUEUE_NUM_START)
#define DNSV6R_GET_QUEUE_CONFIG _IPT_GET_QUEUE_CONFIG_(HTTPV6_GET_QUEUE_NUM_START)
#else
#define __IPT_GET_QUEUE_CONFIG__(s,e) "--queue-balance " #s ":" #e
#define _IPT_GET_QUEUE_CONFIG_(s,e) __IPT_GET_QUEUE_CONFIG__(s,e)
#define HTTP_GET_QUEUE_CONFIG _IPT_GET_QUEUE_CONFIG_(HTTP_GET_QUEUE_NUM_START, HTTP_GET_QUEUE_NUM_END)
#define DNSR_GET_QUEUE_CONFIG _IPT_GET_QUEUE_CONFIG_(DNS_RES_QUEUE_NUM_START, DNS_RES_QUEUE_NUM_END)
#define HTTPV6_GET_QUEUE_CONFIG _IPT_GET_QUEUE_CONFIG_(HTTPV6_GET_QUEUE_NUM_START, HTTPV6_GET_QUEUE_NUM_END)
#define DNSV6R_GET_QUEUE_CONFIG _IPT_GET_QUEUE_CONFIG_(DNSV6_RES_QUEUE_NUM_START, DNSV6_RES_QUEUE_NUM_END)
#endif
#endif
#ifdef CONFIG_CISCO_FEATURE_CISCOCONNECT
#define PARCON_ALLOW_LIST "/var/.parcon_allow_list"
#define PARCON_IP_URL "/var/parcon"
#define PARCON_WALLED_GARDEN_HTTP_PORT_SITEBLK "18080" // the same as the port in lighttpd.conf
#define PARCON_WALLED_GARDEN_HTTPS_PORT_SITEBLK "10443" // the same as the port in lighttpd.conf
#define PARCON_WALLED_GARDEN_HTTP_PORT_TIMEBLK "38080" // the same as the port in lighttpd.conf
#define PARCON_WALLED_GARDEN_HTTPS_PORT_TIMEBLK "30443" // the same as the port in lighttpd.conf
#define DNS_QUERY_QUEUE_NUM 5
#define DNS_RES_QUEUE_NUM_START 6 //should be the same range as system_defaults-xxx
#define DNS_RES_QUEUE_NUM_END 8
#define HTTP_GET_QUEUE_NUM_START 11
#define HTTP_GET_QUEUE_NUM_END 12
#endif
#define FW_DEBUG 1
#ifdef _COSA_FOR_BCI_
#define BRIDGE_MODE_IP_ADDRESS "10.1.10.1"
#else
#define BRIDGE_MODE_IP_ADDRESS "10.0.0.1"
#endif
#if defined(_LG_OFW_)
#define BLOCK_WPAD_ISATAP
#endif
#define IS_EMPTY_STRING(s) ((s == NULL) || (*s == '\0'))
#define BUFLEN_8 8
#define BUFLEN_32 32
#define BUFLEN_64 64
#define RET_OK 0
#define RET_ERR -1
#define SET "set"
#define RESET "reset"
#define UP "up"
#if defined (FEATURE_MAPT) || defined (FEATURE_SUPPORT_MAPT_NAT46)
#define SYSEVENT_MAPT_CONFIG_FLAG "mapt_config_flag"
#define SYSEVENT_MAPT_IP_ADDRESS "mapt_ip_address"
#define MAPT_NAT_IPV4_POST_ROUTING_TABLE "postrouting_towan"
#define SYSEVENT_MAPT_RATIO "mapt_ratio"
#define SYSEVENT_MAPT_IPV6_ADDRESS "mapt_ipv6_address"
#define SYSEVENT_MAPT_PSID_OFFSET "mapt_psid_offset"
#define SYSEVENT_MAPT_PSID_VALUE "mapt_psid_value"
#define SYSEVENT_MAPT_PSID_LENGTH "mapt_psid_length"
BOOL isMAPTSet(void);
static int do_wan_nat_lan_clients_mapt(FILE *fp);
static char mapt_ip_address[BUFLEN_32];
#ifdef FEATURE_MAPT_DEBUG
void logPrintMain(char* filename, int line, char *fmt,...);
#define LOG_PRINT_MAIN(...) logPrintMain(__FILE__, __LINE__, __VA_ARGS__ )
#endif
#endif //FEATURE_MAPT
#ifdef FEATURE_SUPPORT_MAPT_NAT46
#define XHS_BRIDGE "brlan1"
#define LNF_BRIDGE "br106"
#endif
#define V4_BLOCKFRAGIPPKT "v4_BlockFragIPPkts"
#define V4_PORTSCANPROTECT "v4_PortScanProtect"
#define V4_IPFLOODDETECT "v4_IPFloodDetect"
#define XHS_GRE_CLAMP_MSS 1400
#define XHS_EB_MARK 4703
//core net lib
#include <stdint.h>
#ifdef CORE_NET_LIB
#include <libnet.h>
#endif
char *sysevent_name = "firewall";
int firewall_lib_init(void *bus_handle, int sysevent_fd, token_t sysevent_token);
#if defined(CONFIG_KERNEL_NETFILTER_XT_TARGET_CT)
static int do_lan2wan_helpers(FILE *raw_fp);
#endif
FILE *firewallfp = NULL;
//#define CONFIG_BUILD_TRIGGER 1
/*
* Service template declarations & definitions
*/
static char *service_name = "firewall";
//void* bus_handle = NULL;
const char* const firewall_component_id = "ccsp.firewall";
//pthread_mutex_t firewall_check;
fw_shm_mutex fwmutex;
#define SERVICE_EV_COUNT 4
enum{
NAT_DISABLE = 0,
NAT_DHCP,
NAT_STATICIP,
NAT_DISABLE_STATICIP,
};
#define PCMD_LIST "/tmp/.pcmd"
typedef struct _decMacs_
{
char mac[19];
}devMacSt;
#ifdef CISCO_CONFIG_TRUE_STATIC_IP
#define MAX_TS_ASN_COUNT 64
typedef struct{
char ip[20];
char mask[20];
}staticip_subnet_t;
static char wan_staticip_status[20]; // wan_service-status
static char current_wan_static_ipaddr[20];//ipv4 static ip address
static char current_wan_static_mask[20];//ipv4 static ip mask
static char firewall_true_static_ip_enable[20];
//static char firewall_true_static_ip_enablev6[20];
static int isWanStaticIPReady;
static int isFWTS_enable = 0;
static int StaticIPSubnetNum = 0;
staticip_subnet_t StaticIPSubnet[MAX_TS_ASN_COUNT];
#define MAX_IP4_SIZE 20
char PfRangeIP[MAX_TS_ASN_COUNT][MAX_IP4_SIZE];
static int PfRangeCount = 0;
#if defined(_BWG_PRODUCT_REQ_)
staticip_subnet_t StaticClientIP[MAX_TS_ASN_COUNT];
static int StaticNatCount = 0;
#endif
#endif
typedef enum {
SERVICE_EV_UNKNOWN,
SERVICE_EV_START,
SERVICE_EV_STOP,
SERVICE_EV_RESTART,
// add custom events here
SERVICE_EV_SYSLOG_STATUS,
} service_ev_t;
/* iptables module name
* Note: when get priorty from sysevent failed, it will use the default priority order
* the default priority is IPT_PRI_XXXXX.
* 1 is the highest priorty
*/
enum{
IPT_PRI_NONEED = 0,
#ifdef CISCO_CONFIG_TRUE_STATIC_IP
IPT_PRI_STATIC_IP,
#endif
IPT_PRI_PORTMAPPING,
IPT_PRI_PORTTRIGGERING,
IPT_PRI_FIREWALL,
IPT_PRI_DMZ,
IPT_PRI_MAX = IPT_PRI_DMZ,
};
#ifdef FEATURE_RDKB_CONFIGURABLE_WAN_INTERFACE
static void wanmgr_get_wan_interface(char *wanInterface);
#endif
/*
* Service event mapping table
*/
struct {
service_ev_t ev;
char *ev_string;
} service_ev_map[SERVICE_EV_COUNT] =
{
{ SERVICE_EV_START, "firewall-start" },
{ SERVICE_EV_STOP, "firewall-stop" },
{ SERVICE_EV_RESTART, "firewall-restart" },
// add entries for custom events here
{ SERVICE_EV_SYSLOG_STATUS, "syslog-status" },
} ;
static char eth_wan_enabled[20];
static char wan_service_status[20]; // wan_service-status
static char current_wan_ipaddr[20]; // ipv4 address of the wan interface, whether ppp or regular
static char lan_ipaddr[20]; // ipv4 address of the lan interface
static char lan_netmask[20]; // ipv4 netmask of the lan interface
static char lan_3_octets[20]; // first 3 octets of the lan ipv4 address
static char iot_primaryAddress[50]; //IOT primary IP address
#if defined(_COSA_BCM_MIPS_)
static char lan0_ipaddr[20]; // ipv4 address of the lan0 interface used to access web ui in bridge mode
#endif
static char rip_enabled[20]; // is rip enabled
static char rip_interface_wan[20]; // if rip is enabled, then is it enabled on the wan interface
static char nat_enabled[20]; // is nat enabled
static char dmz_enabled[20]; // is dmz enabled
static char firewall_enabled[20]; // is the firewall enabled
static char container_enabled[20]; // is the container enabled
static char bridge_mode[20]; // is system in bridging mode
static char log_level[5]; // if logging is enabled then this is the log level
static int log_leveli; // an integer version of the above
static char reserved_mgmt_port[10]; // mgmt port of utopia
static char transparent_cache_state[10]; // state of the transparent http cache
static char byoi_bridge_mode[10]; // whether or not byoi is in bridge mode
static char cmdiag_enabled[20]; // If eCM diagnostic Interface Enabled
static char firewall_level[20]; // None, Low, Medium, High, or Custom
static char natip4[20];
static char captivePortalEnabled[50]; //to ccheck captive portal is enabled or not
#if defined (_XB6_PRODUCT_REQ_)
static char rfCaptivePortalEnabled[50]; //to check RF captive portal is enabled or not
#endif
static char redirectionFlag[50]; //Captive portal mode flag
static char iptables_pri_level[IPT_PRI_MAX];
static char lxcBridgeName[20];
//static int portmapping_pri;
//static int porttriggering_pri;
//static int firewall_pri;
//static int dmz_pri;
static int isHairpin;
static int isWanReady;
static int isRFC1918Blocked;
static int allowOpenPorts;
static int isRipEnabled;
static int isRipWanEnabled;
static int isNatEnabled;
static int isLogEnabled;
static int isLogSecurityEnabled;
static int isLogIncomingEnabled;
static int isLogOutgoingEnabled;
static int isCronRestartNeeded;
static int isPingBlocked;
static int isIdentBlocked;
static int isMulticastBlocked;
static int isNatRedirectionBlocked;
static int isPortscanDetectionEnabled;
static int isWanPingDisable;
static int isNtpFinished = 0;
#ifndef CONFIG_KERNEL_NF_TRIGGER_SUPPORT
static int isTriggerMonitorRestartNeeded = 0;
#endif
static int isLanHostTracking = 0;
static int isDMZbyMAC = 0; // DMZ is known by MAC address
static int isCacheActive = 0;
static int isHttpBlocked; // Block incoming HTTP/HTTPS traffic
static int isP2pBlocked; // Block incoming P2P traffic
static int flush = 0;
#ifdef CONFIG_CISCO_FEATURE_CISCOCONNECT
static int isGuestNetworkEnabled;
static char guest_network_ipaddr[20];
static char guest_network_mask[20];
#endif
static int ppFlushNeeded = 0;
#ifdef _HUB4_PRODUCT_REQ_
static int isProdImage = 0;
#endif
#if defined(_ENABLE_EPON_SUPPORT_)
static BOOL isEponEnable = TRUE;
#else
static BOOL isEponEnable = FALSE;
#endif
int lan_local_ipv6_num = 0;
char current_wan_ip6_addr[128];
bool isDefHttpsPortUsed = FALSE ;
int current_wan_ipv6_num = 0;
char default_wan_ifname[50]; // name of the regular wan interface
int rfstatus;
/*
* For timed internet access rules we use cron
*/
#define crontab_dir "/var/spool/cron/crontabs/"
#define crontab_filename "firewall"
#define cron_everyminute_dir "/etc/cron/cron.everyminute"
/*
* For tracking lan hosts
*/
#define lan_hosts_dir "/tmp/lanhosts"
#define hosts_filename "lanhosts"
/*
* various files that we use to make well known name to rule mappings
* This allows User Interface and Firewall to refer to the rules by name.
*/
#define qos_classification_file_dir "/etc/"
#define qos_classification_file "qos_classification_rules"
#define wellknown_ports_file_dir "/etc/"
#define wellknown_ports_file "services"
#define otherservices_dir "/etc/"
#define otherservices_file "otherservices"
/*
* triggers use this well known namespace within iptables LOGs.
* keep this in sync with trigger_monitor.sh
*/
#define LOG_TRIGGER_PREFIX "UTOPIA.TRIGGER"
/*
* For simplicity purposes we cap the number of syscfg entries within a
* specific namespace. This cap is controlled by MAX_SYSCFG_ENTRIES
*/
#define MAX_PORT 65535
#define MAX_NAMESPACE 64
#define MAX_SRC_IP_TABLE_ROW 10 /*RDKB-7145, CID-33123, defining max size for src_ip[MAX_SRC_IP_TABLE_ENTRY][]*/
#define MAX_SRC_IP_ENTRY_LEN 25 /*RDKB-7145, CID-33123, defining max size for src_ip[][MAX_SRC_IP_ENTRY_LEN]*/
/*
* For URL blocking,
* The string lengths of "http://" and "https://"
*/
#define STRLEN_HTTP_URL_PREFIX (7)
#define STRLEN_HTTPS_URL_PREFIX (8)
#ifdef WAN_FAILOVER_SUPPORTED
#define REMOTEWAN_ROUTER_IP "remotewan_router_ip"
#define REMOTEWAN_ROUTER_IPv6 "MeshWANInterface_UlaAddr"
#endif
/*
* local date and time
*/
static struct tm local_now;
/*
* iptables priority level
*/
static inline void SET_IPT_PRI_DEFAULT(void){
iptables_pri_level[IPT_PRI_PORTMAPPING -1 ]= IPT_PRI_PORTMAPPING;
iptables_pri_level[IPT_PRI_PORTTRIGGERING -1]= IPT_PRI_PORTTRIGGERING;
iptables_pri_level[IPT_PRI_DMZ-1]= IPT_PRI_DMZ;
iptables_pri_level[IPT_PRI_FIREWALL -1]= IPT_PRI_FIREWALL;
#ifdef CISCO_CONFIG_TRUE_STATIC_IP
iptables_pri_level[IPT_PRI_STATIC_IP -1]= IPT_PRI_STATIC_IP;
#endif
}
static inline int SET_IPT_PRI_MODULD(char *s){
if(strcmp(s, "portmapping") == 0)
return IPT_PRI_PORTMAPPING;
else if(strcmp(s, "porttriggering") == 0)
return IPT_PRI_PORTTRIGGERING;
else if(strcmp(s, "dmz") == 0)
return IPT_PRI_DMZ;
else if(strcmp(s, "firewall") == 0)
return IPT_PRI_FIREWALL;
#ifdef CISCO_CONFIG_TRUE_STATIC_IP
else if(strcmp(s, "staticip") == 0)
return IPT_PRI_STATIC_IP;
#endif
else
return 0;
}
/*
* Get PSM value
*/
#ifdef CISCO_CONFIG_TRUE_STATIC_IP
#define PSM_NAME_TRUE_STATIC_IP_ADDRESS "dmsb.truestaticip.Ipaddress"
#define PSM_NAME_TRUE_STATIC_IP_NETMASK "dmsb.truestaticip.Subnetmask"
#define PSM_NAME_TRUE_STATIC_IP_ENABLE "dmsb.truestaticip.Enable"
#define PSM_NAME_TRUE_STATIC_ASN "dmsb.truestaticip.Asn."
#define PSM_NAME_TRUE_STATIC_ASN_IP "Ipaddress"
#define PSM_NAME_TRUE_STATIC_ASN_MASK "Subnetmask"
#define PSM_NAME_TRUE_STATIC_ASN_ENABLE "Enable"
#endif
#define PSM_VALUE_GET_INS(name, pIns, ppInsArry) PsmGetNextLevelInstances(bus_handle, CCSP_SUBSYS, name, pIns, ppInsArry)
#define PSM_NAME_SPEEDTEST_SERVER_CAPABILITY "eRT.com.cisco.spvtg.ccsp.tr181pa.Device.IP.Diagnostics.X_RDKCENTRAL-COM_SpeedTest.Server.Capability"
#if defined(FEATURE_SUPPORT_RADIUSGREYLIST) && (defined(_COSA_INTEL_XB3_ARM_) || defined(_XB6_PRODUCT_REQ_) || defined (_XB8_PRODUCT_REQ_) || defined (_CBR2_PRODUCT_REQ_))
#define PSM_NAME_RADIUS_GREY_LIST_ENABLED "Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Feature.RadiusGreyList.Enable"
#endif
/*
*/
#define REMOTE_ACCESS_IP_RANGE_MAX_RULE 20
/* DSCP val for gre*/
#define PSM_NAME_GRE_DSCP_VALUE "dmsb.hotspot.tunnel.1.DSCPMarkPolicy"
int greDscp = 44; // Default initialized to 44
/* Configure WiFi flag for captive Portal*/
#define PSM_NAME_CP_NOTIFY_VALUE "eRT.com.cisco.spvtg.ccsp.Device.WiFi.NotifyWiFiChanges"
#define PSM_IDM_INTERFACE_NAME "dmsb.interdevicemanager.BroadcastInterface"
#if defined(FEATURE_RDKB_INTER_DEVICE_MANAGER)
char idmInterface[32] = {0};
#endif
/*
=================================================================
utilities
=================================================================
*/
static int isInRFCaptivePortal();
#define LOG_BUFF_SIZE 512
void firewall_log( char* fmt, ...)
{
time_t now_time;
struct tm *lc_time;
char buff[LOG_BUFF_SIZE] = "";
va_list args;
int time_size;
if(firewallfp == NULL)
return;
va_start(args, fmt);
time(&now_time);
lc_time=localtime(&now_time);
time_size = strftime(buff, LOG_BUFF_SIZE,"%y%m%d-%X ", lc_time);
strncat(buff,fmt, (LOG_BUFF_SIZE - time_size -1));
vfprintf(firewallfp, buff, args);
va_end(args);
return;
}
#ifdef WAN_FAILOVER_SUPPORTED
unsigned int Get_Device_Mode()
{
FIREWALL_DEBUG("Inside Get_Device_Mode\n");
syscfg_get(NULL, "Device_Mode", dev_type, sizeof(dev_type));
unsigned int dev_mode = atoi(dev_type);
Dev_Mode mode;
if(dev_mode==1)
{
mode =EXTENDER_MODE;
}
else
mode = ROUTER;
return mode;
}
#endif
#ifdef WAN_FAILOVER_SUPPORTED
int create_socket()
{
int sockfd = 0;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1){
fprintf(stderr, "Could not get socket.\n");
return -1;
}
return sockfd;
}
char* get_iface_ipaddr(const char* iface_name)
{
if(!iface_name )
return NULL;
struct ifreq ifr;
memset(&ifr,0,sizeof(struct ifreq));
int skfd = 0;
if ((skfd = create_socket() ) < 0) {
printf("socket error %s\n", strerror(errno));
return NULL;
}
ifr.ifr_addr.sa_family = AF_INET ;
strncpy(ifr.ifr_name, iface_name, IFNAMSIZ-1);
if ( ioctl(skfd, SIOCGIFADDR, &ifr) < 0 )
{
printf("Failed to get %s IP Address\n",iface_name);
close(skfd);
return NULL;
}
close(skfd);
return (inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
}
bool isServiceNeeded()
{
FIREWALL_DEBUG("Inside isServiceNeeded\n");
if (Get_Device_Mode()==EXTENDER_MODE)
{
FIREWALL_DEBUG("Service Not Needed\n");
return FALSE;
}
else
{
#ifdef FEATURE_RDKB_CONFIGURABLE_WAN_INTERFACE
if(strcmp(current_wan_ifname, mesh_wan_ifname ) == 0)
#else
if(strcmp(current_wan_ifname,default_wan_ifname ) != 0)
#endif
{
FIREWALL_DEBUG("current Wam interface Name is not equal to default wan ifname\n");
return FALSE;
}
}
FIREWALL_DEBUG("returning true\n");
return TRUE;
}
#endif
int IsValidIPv6Addr(char* ip_addr_string)
{
struct in6_addr addr;
if(ip_addr_string == NULL)
return 0;
if(!inet_pton(AF_INET6, ip_addr_string, &addr))
{
return 0;
}
/* Here non valid IPv6 address are
* 1) 0:0:0:0:0:0:0:0
* 2) ::
*/
if( (0 == strcmp("0:0:0:0:0:0:0:0", ip_addr_string)) ||
(0 == strcmp("::", ip_addr_string)))
{
return 0;
}
return 1;
}
#ifdef FEATURE_464XLAT
void do_xlat_rule(FILE *nat_fp)
{
char status[16] = {0};
syscfg_get(NULL, "xlat_status", status, sizeof(status));
if(strcmp(status,"up") == 0)
{
fprintf(nat_fp, "-I POSTROUTING -o %s -j SNAT --to-source %s\n",XLAT_IF,XLAT_IP);
}
}
#endif
#ifdef DSLITE_FEATURE_SUPPORT
static void add_dslite_mss_clamping(FILE *fp);
#endif
#if defined(FEATURE_MAPT) || defined(FEATURE_SUPPORT_MAPT_NAT46)
static int IsValidIPv4Addr(char* ip_addr_string)
{
int ret = 1;
struct in_addr ip_value;
if(!inet_pton(AF_INET, ip_addr_string, &(ip_value.s_addr)))
{
return 0;
}
/* Here non valid IPv4 address are
* 1) 0.0.0.0
* 2) 255.255.255.255
* 3) multicast addresses
*/
if( (0 == strcmp("0.0.0.0", ip_addr_string)) ||
(0 == strcmp("255.255.255.255", ip_addr_string)) ||