-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandListener.cpp
1581 lines (1415 loc) · 57.3 KB
/
CommandListener.cpp
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 (C) 2008 The Android Open Source Project
*
* 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.
*/
// #define LOG_NDEBUG 0
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <linux/if.h>
#define LOG_TAG "CommandListener"
#include <cutils/log.h>
#include <netutils/ifc.h>
#include <sysutils/SocketClient.h>
#include "CommandListener.h"
#include "ResponseCode.h"
#include "BandwidthController.h"
#include "IdletimerController.h"
#include "SecondaryTableController.h"
#include "oem_iptables_hook.h"
#include "NetdConstants.h"
#include "FirewallController.h"
TetherController *CommandListener::sTetherCtrl = NULL;
NatController *CommandListener::sNatCtrl = NULL;
PppController *CommandListener::sPppCtrl = NULL;
SoftapController *CommandListener::sSoftapCtrl = NULL;
BandwidthController * CommandListener::sBandwidthCtrl = NULL;
IdletimerController * CommandListener::sIdletimerCtrl = NULL;
InterfaceController *CommandListener::sInterfaceCtrl = NULL;
ResolverController *CommandListener::sResolverCtrl = NULL;
SecondaryTableController *CommandListener::sSecondaryTableCtrl = NULL;
FirewallController *CommandListener::sFirewallCtrl = NULL;
ClatdController *CommandListener::sClatdCtrl = NULL;
/**
* List of module chains to be created, along with explicit ordering. ORDERING
* IS CRITICAL, AND SHOULD BE TRIPLE-CHECKED WITH EACH CHANGE.
*/
static const char* FILTER_INPUT[] = {
// Bandwidth should always be early in input chain, to make sure we
// correctly count incoming traffic against data plan.
BandwidthController::LOCAL_INPUT,
FirewallController::LOCAL_INPUT,
NULL,
};
static const char* FILTER_FORWARD[] = {
OEM_IPTABLES_FILTER_FORWARD,
FirewallController::LOCAL_FORWARD,
BandwidthController::LOCAL_FORWARD,
NatController::LOCAL_FORWARD,
NULL,
};
static const char* FILTER_OUTPUT[] = {
OEM_IPTABLES_FILTER_OUTPUT,
FirewallController::LOCAL_OUTPUT,
BandwidthController::LOCAL_OUTPUT,
NULL,
};
static const char* RAW_PREROUTING[] = {
BandwidthController::LOCAL_RAW_PREROUTING,
IdletimerController::LOCAL_RAW_PREROUTING,
NULL,
};
static const char* MANGLE_POSTROUTING[] = {
BandwidthController::LOCAL_MANGLE_POSTROUTING,
IdletimerController::LOCAL_MANGLE_POSTROUTING,
SecondaryTableController::LOCAL_MANGLE_POSTROUTING,
NULL,
};
static const char* MANGLE_OUTPUT[] = {
SecondaryTableController::LOCAL_MANGLE_OUTPUT,
NULL,
};
static const char* NAT_PREROUTING[] = {
OEM_IPTABLES_NAT_PREROUTING,
NULL,
};
static const char* NAT_POSTROUTING[] = {
NatController::LOCAL_NAT_POSTROUTING,
SecondaryTableController::LOCAL_NAT_POSTROUTING,
NULL,
};
static void createChildChains(IptablesTarget target, const char* table, const char* parentChain,
const char** childChains) {
const char** childChain = childChains;
do {
// Order is important:
// -D to delete any pre-existing jump rule (removes references
// that would prevent -X from working)
// -F to flush any existing chain
// -X to delete any existing chain
// -N to create the chain
// -A to append the chain to parent
execIptablesSilently(target, "-t", table, "-D", parentChain, "-j", *childChain, NULL);
execIptablesSilently(target, "-t", table, "-F", *childChain, NULL);
execIptablesSilently(target, "-t", table, "-X", *childChain, NULL);
execIptables(target, "-t", table, "-N", *childChain, NULL);
execIptables(target, "-t", table, "-A", parentChain, "-j", *childChain, NULL);
} while (*(++childChain) != NULL);
}
CommandListener::CommandListener(UidMarkMap *map) :
FrameworkListener("netd", true) {
registerCmd(new InterfaceCmd());
registerCmd(new IpFwdCmd());
registerCmd(new TetherCmd());
registerCmd(new NatCmd());
registerCmd(new ListTtysCmd());
registerCmd(new PppdCmd());
registerCmd(new SoftapCmd());
registerCmd(new BandwidthControlCmd());
registerCmd(new IdletimerControlCmd());
registerCmd(new ResolverCmd());
registerCmd(new FirewallCmd());
registerCmd(new ClatdCmd());
if (!sSecondaryTableCtrl)
sSecondaryTableCtrl = new SecondaryTableController(map);
if (!sTetherCtrl)
sTetherCtrl = new TetherController();
if (!sNatCtrl)
sNatCtrl = new NatController(sSecondaryTableCtrl);
if (!sPppCtrl)
sPppCtrl = new PppController();
if (!sSoftapCtrl)
sSoftapCtrl = new SoftapController();
if (!sBandwidthCtrl)
sBandwidthCtrl = new BandwidthController();
if (!sIdletimerCtrl)
sIdletimerCtrl = new IdletimerController();
if (!sResolverCtrl)
sResolverCtrl = new ResolverController();
if (!sFirewallCtrl)
sFirewallCtrl = new FirewallController();
if (!sInterfaceCtrl)
sInterfaceCtrl = new InterfaceController();
if (!sClatdCtrl)
sClatdCtrl = new ClatdController();
/*
* This is the only time we touch top-level chains in iptables; controllers
* should only mutate rules inside of their children chains, as created by
* the constants above.
*
* Modules should never ACCEPT packets (except in well-justified cases);
* they should instead defer to any remaining modules using RETURN, or
* otherwise DROP/REJECT.
*/
// Create chains for children modules
createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT);
createChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD);
createChildChains(V4V6, "filter", "OUTPUT", FILTER_OUTPUT);
createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING);
createChildChains(V4V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING);
createChildChains(V4V6, "mangle", "OUTPUT", MANGLE_OUTPUT);
createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING);
createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING);
// Let each module setup their child chains
setupOemIptablesHook();
/* When enabled, DROPs all packets except those matching rules. */
sFirewallCtrl->setupIptablesHooks();
/* Does DROPs in FORWARD by default */
sNatCtrl->setupIptablesHooks();
/*
* Does REJECT in INPUT, OUTPUT. Does counting also.
* No DROP/REJECT allowed later in netfilter-flow hook order.
*/
sBandwidthCtrl->setupIptablesHooks();
/*
* Counts in nat: PREROUTING, POSTROUTING.
* No DROP/REJECT allowed later in netfilter-flow hook order.
*/
sIdletimerCtrl->setupIptablesHooks();
sBandwidthCtrl->enableBandwidthControl(false);
sSecondaryTableCtrl->setupIptablesHooks();
}
CommandListener::InterfaceCmd::InterfaceCmd() :
NetdCommand("interface") {
}
int CommandListener::InterfaceCmd::runCommand(SocketClient *cli,
int argc, char **argv) {
if (argc < 2) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
if (!strcmp(argv[1], "list")) {
DIR *d;
struct dirent *de;
if (!(d = opendir("/sys/class/net"))) {
cli->sendMsg(ResponseCode::OperationFailed, "Failed to open sysfs dir", true);
return 0;
}
while((de = readdir(d))) {
if (de->d_name[0] == '.')
continue;
cli->sendMsg(ResponseCode::InterfaceListResult, de->d_name, false);
}
closedir(d);
cli->sendMsg(ResponseCode::CommandOkay, "Interface list completed", false);
return 0;
} else if (!strcmp(argv[1], "driver")) {
int rc;
char *rbuf;
if (argc < 4) {
cli->sendMsg(ResponseCode::CommandSyntaxError,
"Usage: interface driver <interface> <cmd> <args>", false);
return 0;
}
rc = sInterfaceCtrl->interfaceCommand(argc, argv, &rbuf);
if (rc) {
cli->sendMsg(ResponseCode::OperationFailed, "Failed to execute command", true);
} else {
cli->sendMsg(ResponseCode::CommandOkay, rbuf, false);
}
return 0;
} else {
/*
* These commands take a minimum of 3 arguments
*/
if (argc < 3) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
// 0 1 2 3 4 5 6 7
// interface route add/remove iface default/secondary dest prefix gateway
// interface fwmark rule add/remove iface
// interface fwmark route add/remove iface dest prefix
// interface fwmark uid add/remove iface uid_start uid_end
// interface fwmark exempt add/remove dest
// interface fwmark get protect
// interface fwmark get mark uid
if (!strcmp(argv[1], "fwmark")) {
if (!strcmp(argv[2], "rule")) {
if (argc < 5) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
if (!strcmp(argv[3], "add")) {
if (!sSecondaryTableCtrl->addFwmarkRule(argv[4])) {
cli->sendMsg(ResponseCode::CommandOkay,
"Fwmark rule successfully added", false);
} else {
cli->sendMsg(ResponseCode::OperationFailed, "Failed to add fwmark rule",
true);
}
} else if (!strcmp(argv[3], "remove")) {
if (!sSecondaryTableCtrl->removeFwmarkRule(argv[4])) {
cli->sendMsg(ResponseCode::CommandOkay,
"Fwmark rule successfully removed", false);
} else {
cli->sendMsg(ResponseCode::OperationFailed,
"Failed to remove fwmark rule", true);
}
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fwmark rule cmd",
false);
}
return 0;
} else if (!strcmp(argv[2], "route")) {
if (argc < 7) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
if (!strcmp(argv[3], "add")) {
if (!sSecondaryTableCtrl->addFwmarkRoute(argv[4], argv[5], atoi(argv[6]))) {
cli->sendMsg(ResponseCode::CommandOkay,
"Fwmark route successfully added", false);
} else {
cli->sendMsg(ResponseCode::OperationFailed,
"Failed to add fwmark route", true);
}
} else if (!strcmp(argv[3], "remove")) {
if (!sSecondaryTableCtrl->removeFwmarkRoute(argv[4], argv[5],
atoi(argv[6]))) {
cli->sendMsg(ResponseCode::CommandOkay,
"Fwmark route successfully removed", false);
} else {
cli->sendMsg(ResponseCode::OperationFailed,
"Failed to remove fwmark route", true);
}
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fwmark route cmd",
false);
}
return 0;
} else if (!strcmp(argv[2], "uid")) {
if (argc < 7) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
if (!strcmp(argv[3], "add")) {
if (!sSecondaryTableCtrl->addUidRule(argv[4], atoi(argv[5]), atoi(argv[6]))) {
cli->sendMsg(ResponseCode::CommandOkay, "uid rule successfully added",
false);
} else {
cli->sendMsg(ResponseCode::OperationFailed, "Failed to add uid rule", true);
}
} else if (!strcmp(argv[3], "remove")) {
if (!sSecondaryTableCtrl->removeUidRule(argv[4],
atoi(argv[5]), atoi(argv[6]))) {
cli->sendMsg(ResponseCode::CommandOkay, "uid rule successfully removed",
false);
} else {
cli->sendMsg(ResponseCode::OperationFailed, "Failed to remove uid rule",
true);
}
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown uid cmd", false);
}
return 0;
} else if (!strcmp(argv[2], "exempt")) {
if (argc < 5) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
if (!strcmp(argv[3], "add")) {
if (!sSecondaryTableCtrl->addHostExemption(argv[4])) {
cli->sendMsg(ResponseCode::CommandOkay, "exemption rule successfully added",
false);
} else {
cli->sendMsg(ResponseCode::OperationFailed, "Failed to add exemption rule",
true);
}
} else if (!strcmp(argv[3], "remove")) {
if (!sSecondaryTableCtrl->removeHostExemption(argv[4])) {
cli->sendMsg(ResponseCode::CommandOkay,
"exemption rule successfully removed", false);
} else {
cli->sendMsg(ResponseCode::OperationFailed,
"Failed to remove exemption rule", true);
}
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown exemption cmd", false);
}
return 0;
} else if (!strcmp(argv[2], "get")) {
if (argc < 4) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
if (!strcmp(argv[3], "protect")) {
sSecondaryTableCtrl->getProtectMark(cli);
return 0;
} else if (!strcmp(argv[3], "mark")) {
if (argc < 5) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
sSecondaryTableCtrl->getUidMark(cli, atoi(argv[4]));
return 0;
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fwmark get cmd", false);
return 0;
}
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fwmark cmd", false);
return 0;
}
}
if (!strcmp(argv[1], "route")) {
int prefix_length = 0;
if (argc < 8) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
if (sscanf(argv[6], "%d", &prefix_length) != 1) {
cli->sendMsg(ResponseCode::CommandParameterError, "Invalid route prefix", false);
return 0;
}
if (!strcmp(argv[2], "add")) {
if (!strcmp(argv[4], "default")) {
if (ifc_add_route(argv[3], argv[5], prefix_length, argv[7])) {
cli->sendMsg(ResponseCode::OperationFailed,
"Failed to add route to default table", true);
} else {
cli->sendMsg(ResponseCode::CommandOkay,
"Route added to default table", false);
}
} else if (!strcmp(argv[4], "secondary")) {
return sSecondaryTableCtrl->addRoute(cli, argv[3], argv[5],
prefix_length, argv[7]);
} else {
cli->sendMsg(ResponseCode::CommandParameterError,
"Invalid route type, expecting 'default' or 'secondary'", false);
return 0;
}
} else if (!strcmp(argv[2], "remove")) {
if (!strcmp(argv[4], "default")) {
if (ifc_remove_route(argv[3], argv[5], prefix_length, argv[7])) {
cli->sendMsg(ResponseCode::OperationFailed,
"Failed to remove route from default table", true);
} else {
cli->sendMsg(ResponseCode::CommandOkay,
"Route removed from default table", false);
}
} else if (!strcmp(argv[4], "secondary")) {
return sSecondaryTableCtrl->removeRoute(cli, argv[3], argv[5],
prefix_length, argv[7]);
} else {
cli->sendMsg(ResponseCode::CommandParameterError,
"Invalid route type, expecting 'default' or 'secondary'", false);
return 0;
}
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown interface cmd", false);
}
return 0;
}
if (!strcmp(argv[1], "getcfg")) {
struct in_addr addr;
int prefixLength;
unsigned char hwaddr[6];
unsigned flags = 0;
ifc_init();
memset(hwaddr, 0, sizeof(hwaddr));
if (ifc_get_info(argv[2], &addr.s_addr, &prefixLength, &flags)) {
cli->sendMsg(ResponseCode::OperationFailed, "Interface not found", true);
ifc_close();
return 0;
}
if (ifc_get_hwaddr(argv[2], (void *) hwaddr)) {
ALOGW("Failed to retrieve HW addr for %s (%s)", argv[2], strerror(errno));
}
char *addr_s = strdup(inet_ntoa(addr));
const char *updown, *brdcst, *loopbk, *ppp, *running, *multi;
updown = (flags & IFF_UP) ? "up" : "down";
brdcst = (flags & IFF_BROADCAST) ? " broadcast" : "";
loopbk = (flags & IFF_LOOPBACK) ? " loopback" : "";
ppp = (flags & IFF_POINTOPOINT) ? " point-to-point" : "";
running = (flags & IFF_RUNNING) ? " running" : "";
multi = (flags & IFF_MULTICAST) ? " multicast" : "";
char *flag_s;
asprintf(&flag_s, "%s%s%s%s%s%s", updown, brdcst, loopbk, ppp, running, multi);
char *msg = NULL;
asprintf(&msg, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x %s %d %s",
hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5],
addr_s, prefixLength, flag_s);
cli->sendMsg(ResponseCode::InterfaceGetCfgResult, msg, false);
free(addr_s);
free(flag_s);
free(msg);
ifc_close();
return 0;
} else if (!strcmp(argv[1], "setcfg")) {
// arglist: iface [addr prefixLength] flags
if (argc < 4) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
ALOGD("Setting iface cfg");
struct in_addr addr;
unsigned flags = 0;
int index = 5;
ifc_init();
if (!inet_aton(argv[3], &addr)) {
// Handle flags only case
index = 3;
} else {
if (ifc_set_addr(argv[2], addr.s_addr)) {
cli->sendMsg(ResponseCode::OperationFailed, "Failed to set address", true);
ifc_close();
return 0;
}
// Set prefix length on a non zero address
if (addr.s_addr != 0 && ifc_set_prefixLength(argv[2], atoi(argv[4]))) {
cli->sendMsg(ResponseCode::OperationFailed, "Failed to set prefixLength", true);
ifc_close();
return 0;
}
}
/* Process flags */
for (int i = index; i < argc; i++) {
char *flag = argv[i];
if (!strcmp(flag, "up")) {
ALOGD("Trying to bring up %s", argv[2]);
if (ifc_up(argv[2])) {
ALOGE("Error upping interface");
cli->sendMsg(ResponseCode::OperationFailed, "Failed to up interface", true);
ifc_close();
return 0;
}
} else if (!strcmp(flag, "down")) {
ALOGD("Trying to bring down %s", argv[2]);
if (ifc_down(argv[2])) {
ALOGE("Error downing interface");
cli->sendMsg(ResponseCode::OperationFailed, "Failed to down interface", true);
ifc_close();
return 0;
}
} else if (!strcmp(flag, "broadcast")) {
// currently ignored
} else if (!strcmp(flag, "multicast")) {
// currently ignored
} else if (!strcmp(flag, "running")) {
// currently ignored
} else if (!strcmp(flag, "loopback")) {
// currently ignored
} else if (!strcmp(flag, "point-to-point")) {
// currently ignored
} else {
cli->sendMsg(ResponseCode::CommandParameterError, "Flag unsupported", false);
ifc_close();
return 0;
}
}
cli->sendMsg(ResponseCode::CommandOkay, "Interface configuration set", false);
ifc_close();
return 0;
} else if (!strcmp(argv[1], "clearaddrs")) {
// arglist: iface
ALOGD("Clearing all IP addresses on %s", argv[2]);
ifc_clear_addresses(argv[2]);
cli->sendMsg(ResponseCode::CommandOkay, "Interface IP addresses cleared", false);
return 0;
} else if (!strcmp(argv[1], "ipv6privacyextensions")) {
if (argc != 4) {
cli->sendMsg(ResponseCode::CommandSyntaxError,
"Usage: interface ipv6privacyextensions <interface> <enable|disable>",
false);
return 0;
}
int enable = !strncmp(argv[3], "enable", 7);
if (sInterfaceCtrl->setIPv6PrivacyExtensions(argv[2], enable) == 0) {
cli->sendMsg(ResponseCode::CommandOkay, "IPv6 privacy extensions changed", false);
} else {
cli->sendMsg(ResponseCode::OperationFailed,
"Failed to set ipv6 privacy extensions", true);
}
return 0;
} else if (!strcmp(argv[1], "ipv6")) {
if (argc != 4) {
cli->sendMsg(ResponseCode::CommandSyntaxError,
"Usage: interface ipv6 <interface> <enable|disable>",
false);
return 0;
}
int enable = !strncmp(argv[3], "enable", 7);
if (sInterfaceCtrl->setEnableIPv6(argv[2], enable) == 0) {
cli->sendMsg(ResponseCode::CommandOkay, "IPv6 state changed", false);
} else {
cli->sendMsg(ResponseCode::OperationFailed,
"Failed to change IPv6 state", true);
}
return 0;
} else if (!strcmp(argv[1], "getmtu")) {
char *msg = NULL;
int mtu = 0;
if (sInterfaceCtrl->getMtu(argv[2], &mtu) == 0) {
asprintf(&msg, "MTU = %d", mtu);
cli->sendMsg(ResponseCode::InterfaceGetMtuResult, msg, false);
free(msg);
} else {
cli->sendMsg(ResponseCode::OperationFailed,
"Failed to get MTU", true);
}
return 0;
} else if (!strcmp(argv[1], "setmtu")) {
if (argc != 4) {
cli->sendMsg(ResponseCode::CommandSyntaxError,
"Usage: interface setmtu <interface> <val>", false);
return 0;
}
if (sInterfaceCtrl->setMtu(argv[2], argv[3]) == 0) {
cli->sendMsg(ResponseCode::CommandOkay, "MTU changed", false);
} else {
cli->sendMsg(ResponseCode::OperationFailed,
"Failed to get MTU", true);
}
return 0;
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown interface cmd", false);
return 0;
}
}
return 0;
}
CommandListener::ListTtysCmd::ListTtysCmd() :
NetdCommand("list_ttys") {
}
int CommandListener::ListTtysCmd::runCommand(SocketClient *cli,
int argc, char **argv) {
TtyCollection *tlist = sPppCtrl->getTtyList();
TtyCollection::iterator it;
for (it = tlist->begin(); it != tlist->end(); ++it) {
cli->sendMsg(ResponseCode::TtyListResult, *it, false);
}
cli->sendMsg(ResponseCode::CommandOkay, "Ttys listed.", false);
return 0;
}
CommandListener::IpFwdCmd::IpFwdCmd() :
NetdCommand("ipfwd") {
}
int CommandListener::IpFwdCmd::runCommand(SocketClient *cli,
int argc, char **argv) {
int rc = 0;
if (argc < 2) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
if (!strcmp(argv[1], "status")) {
char *tmp = NULL;
asprintf(&tmp, "Forwarding %s", (sTetherCtrl->getIpFwdEnabled() ? "enabled" : "disabled"));
cli->sendMsg(ResponseCode::IpFwdStatusResult, tmp, false);
free(tmp);
return 0;
} else if (!strcmp(argv[1], "enable")) {
rc = sTetherCtrl->setIpFwdEnabled(true);
} else if (!strcmp(argv[1], "disable")) {
rc = sTetherCtrl->setIpFwdEnabled(false);
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown ipfwd cmd", false);
return 0;
}
if (!rc) {
cli->sendMsg(ResponseCode::CommandOkay, "ipfwd operation succeeded", false);
} else {
cli->sendMsg(ResponseCode::OperationFailed, "ipfwd operation failed", true);
}
return 0;
}
CommandListener::TetherCmd::TetherCmd() :
NetdCommand("tether") {
}
int CommandListener::TetherCmd::runCommand(SocketClient *cli,
int argc, char **argv) {
int rc = 0;
if (argc < 2) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
if (!strcmp(argv[1], "stop")) {
rc = sTetherCtrl->stopTethering();
} else if (!strcmp(argv[1], "status")) {
char *tmp = NULL;
asprintf(&tmp, "Tethering services %s",
(sTetherCtrl->isTetheringStarted() ? "started" : "stopped"));
cli->sendMsg(ResponseCode::TetherStatusResult, tmp, false);
free(tmp);
return 0;
} else if (argc == 3) {
if (!strcmp(argv[1], "interface") && !strcmp(argv[2], "list")) {
InterfaceCollection *ilist = sTetherCtrl->getTetheredInterfaceList();
InterfaceCollection::iterator it;
for (it = ilist->begin(); it != ilist->end(); ++it) {
cli->sendMsg(ResponseCode::TetherInterfaceListResult, *it, false);
}
} else if (!strcmp(argv[1], "dns") && !strcmp(argv[2], "list")) {
NetAddressCollection *dlist = sTetherCtrl->getDnsForwarders();
NetAddressCollection::iterator it;
for (it = dlist->begin(); it != dlist->end(); ++it) {
cli->sendMsg(ResponseCode::TetherDnsFwdTgtListResult, inet_ntoa(*it), false);
}
}
} else {
/*
* These commands take a minimum of 4 arguments
*/
if (argc < 4) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
if (!strcmp(argv[1], "start")) {
if (argc % 2 == 1) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Bad number of arguments", false);
return 0;
}
int num_addrs = argc - 2;
int arg_index = 2;
int array_index = 0;
in_addr *addrs = (in_addr *)malloc(sizeof(in_addr) * num_addrs);
while (array_index < num_addrs) {
if (!inet_aton(argv[arg_index++], &(addrs[array_index++]))) {
cli->sendMsg(ResponseCode::CommandParameterError, "Invalid address", false);
free(addrs);
return 0;
}
}
rc = sTetherCtrl->startTethering(num_addrs, addrs);
free(addrs);
} else if (!strcmp(argv[1], "interface")) {
if (!strcmp(argv[2], "add")) {
rc = sTetherCtrl->tetherInterface(argv[3]);
} else if (!strcmp(argv[2], "remove")) {
rc = sTetherCtrl->untetherInterface(argv[3]);
/* else if (!strcmp(argv[2], "list")) handled above */
} else {
cli->sendMsg(ResponseCode::CommandParameterError,
"Unknown tether interface operation", false);
return 0;
}
} else if (!strcmp(argv[1], "dns")) {
if (!strcmp(argv[2], "set")) {
rc = sTetherCtrl->setDnsForwarders(&argv[3], argc - 3);
/* else if (!strcmp(argv[2], "list")) handled above */
} else {
cli->sendMsg(ResponseCode::CommandParameterError,
"Unknown tether interface operation", false);
return 0;
}
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown tether cmd", false);
return 0;
}
}
if (!rc) {
cli->sendMsg(ResponseCode::CommandOkay, "Tether operation succeeded", false);
} else {
cli->sendMsg(ResponseCode::OperationFailed, "Tether operation failed", true);
}
return 0;
}
CommandListener::NatCmd::NatCmd() :
NetdCommand("nat") {
}
int CommandListener::NatCmd::runCommand(SocketClient *cli,
int argc, char **argv) {
int rc = 0;
if (argc < 5) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
if (!strcmp(argv[1], "enable")) {
rc = sNatCtrl->enableNat(argc, argv);
if(!rc) {
/* Ignore ifaces for now. */
rc = sBandwidthCtrl->setGlobalAlertInForwardChain();
}
} else if (!strcmp(argv[1], "disable")) {
/* Ignore ifaces for now. */
rc = sBandwidthCtrl->removeGlobalAlertInForwardChain();
rc |= sNatCtrl->disableNat(argc, argv);
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown nat cmd", false);
return 0;
}
if (!rc) {
cli->sendMsg(ResponseCode::CommandOkay, "Nat operation succeeded", false);
} else {
cli->sendMsg(ResponseCode::OperationFailed, "Nat operation failed", true);
}
return 0;
}
CommandListener::PppdCmd::PppdCmd() :
NetdCommand("pppd") {
}
int CommandListener::PppdCmd::runCommand(SocketClient *cli,
int argc, char **argv) {
int rc = 0;
if (argc < 3) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
if (!strcmp(argv[1], "attach")) {
struct in_addr l, r, dns1, dns2;
memset(&dns1, 0, sizeof(struct in_addr));
memset(&dns2, 0, sizeof(struct in_addr));
if (!inet_aton(argv[3], &l)) {
cli->sendMsg(ResponseCode::CommandParameterError, "Invalid local address", false);
return 0;
}
if (!inet_aton(argv[4], &r)) {
cli->sendMsg(ResponseCode::CommandParameterError, "Invalid remote address", false);
return 0;
}
if ((argc > 3) && (!inet_aton(argv[5], &dns1))) {
cli->sendMsg(ResponseCode::CommandParameterError, "Invalid dns1 address", false);
return 0;
}
if ((argc > 4) && (!inet_aton(argv[6], &dns2))) {
cli->sendMsg(ResponseCode::CommandParameterError, "Invalid dns2 address", false);
return 0;
}
rc = sPppCtrl->attachPppd(argv[2], l, r, dns1, dns2);
} else if (!strcmp(argv[1], "detach")) {
rc = sPppCtrl->detachPppd(argv[2]);
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown pppd cmd", false);
return 0;
}
if (!rc) {
cli->sendMsg(ResponseCode::CommandOkay, "Pppd operation succeeded", false);
} else {
cli->sendMsg(ResponseCode::OperationFailed, "Pppd operation failed", true);
}
return 0;
}
CommandListener::SoftapCmd::SoftapCmd() :
NetdCommand("softap") {
}
int CommandListener::SoftapCmd::runCommand(SocketClient *cli,
int argc, char **argv) {
int rc = ResponseCode::SoftapStatusResult;
int flag = 0;
char *retbuf = NULL;
if (sSoftapCtrl == NULL) {
cli->sendMsg(ResponseCode::ServiceStartFailed, "SoftAP is not available", false);
return -1;
}
if (argc < 2) {
cli->sendMsg(ResponseCode::CommandSyntaxError,
"Missing argument in a SoftAP command", false);
return 0;
}
if (!strcmp(argv[1], "startap")) {
rc = sSoftapCtrl->startSoftap();
} else if (!strcmp(argv[1], "stopap")) {
rc = sSoftapCtrl->stopSoftap();
} else if (!strcmp(argv[1], "fwreload")) {
rc = sSoftapCtrl->fwReloadSoftap(argc, argv);
} else if (!strcmp(argv[1], "status")) {
asprintf(&retbuf, "Softap service %s running",
(sSoftapCtrl->isSoftapStarted() ? "is" : "is not"));
cli->sendMsg(rc, retbuf, false);
free(retbuf);
return 0;
} else if (!strcmp(argv[1], "set")) {
rc = sSoftapCtrl->setSoftap(argc, argv);
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Unrecognized SoftAP command", false);
return 0;
}
if (rc >= 400 && rc < 600)
cli->sendMsg(rc, "SoftAP command has failed", false);
else
cli->sendMsg(rc, "Ok", false);
return 0;
}
CommandListener::ResolverCmd::ResolverCmd() :
NetdCommand("resolver") {
}
int CommandListener::ResolverCmd::runCommand(SocketClient *cli, int argc, char **margv) {
int rc = 0;
struct in_addr addr;
const char **argv = const_cast<const char **>(margv);
if (argc < 2) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Resolver missing arguments", false);
return 0;
}
if (!strcmp(argv[1], "setdefaultif")) { // "resolver setdefaultif <iface>"
if (argc == 3) {
rc = sResolverCtrl->setDefaultInterface(argv[2]);
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError,
"Wrong number of arguments to resolver setdefaultif", false);
return 0;
}
} else if (!strcmp(argv[1], "setifdns")) {
// "resolver setifdns <iface> <domains> <dns1> <dns2> ..."
if (argc >= 5) {
rc = sResolverCtrl->setInterfaceDnsServers(argv[2], argv[3], &argv[4], argc - 4);
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError,
"Wrong number of arguments to resolver setifdns", false);
return 0;
}
// set the address of the interface to which the name servers
// are bound. Required in order to bind to right interface when
// doing the dns query.
if (!rc) {
ifc_init();
ifc_get_info(argv[2], &addr.s_addr, NULL, 0);
rc = sResolverCtrl->setInterfaceAddress(argv[2], &addr);
}
} else if (!strcmp(argv[1], "flushdefaultif")) { // "resolver flushdefaultif"
if (argc == 2) {
rc = sResolverCtrl->flushDefaultDnsCache();
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError,
"Wrong number of arguments to resolver flushdefaultif", false);
return 0;
}
} else if (!strcmp(argv[1], "flushif")) { // "resolver flushif <iface>"
if (argc == 3) {
rc = sResolverCtrl->flushInterfaceDnsCache(argv[2]);
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError,
"Wrong number of arguments to resolver setdefaultif", false);
return 0;
}
} else if (!strcmp(argv[1], "setifaceforpid")) { // resolver setifaceforpid <iface> <pid>
if (argc == 4) {
rc = sResolverCtrl->setDnsInterfaceForPid(argv[2], atoi(argv[3]));
} else {
cli->sendMsg(ResponseCode::CommandSyntaxError,
"Wrong number of arguments to resolver setifaceforpid", false);
return 0;
}