2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <sys/socket.h>
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
27 #define LOG_TAG "CommandListener"
28 #include <cutils/log.h>
30 #include <sysutils/SocketClient.h>
32 #include "CommandListener.h"
33 #include "ResponseCode.h"
34 #include "ThrottleController.h"
37 extern "C" int ifc_init(void);
38 extern "C" int ifc_get_hwaddr(const char *name, void *ptr);
39 extern "C" int ifc_get_info(const char *name, in_addr_t *addr, in_addr_t *mask, unsigned *flags);
40 extern "C" int ifc_set_addr(const char *name, in_addr_t addr);
41 extern "C" int ifc_set_mask(const char *name, in_addr_t mask);
42 extern "C" int ifc_up(const char *name);
43 extern "C" int ifc_down(const char *name);
45 TetherController *CommandListener::sTetherCtrl = NULL;
46 NatController *CommandListener::sNatCtrl = NULL;
47 PppController *CommandListener::sPppCtrl = NULL;
48 PanController *CommandListener::sPanCtrl = NULL;
49 SoftapController *CommandListener::sSoftapCtrl = NULL;
50 UsbController *CommandListener::sUsbCtrl = NULL;
52 CommandListener::CommandListener() :
53 FrameworkListener("netd") {
54 registerCmd(new InterfaceCmd());
55 registerCmd(new IpFwdCmd());
56 registerCmd(new TetherCmd());
57 registerCmd(new NatCmd());
58 registerCmd(new ListTtysCmd());
59 registerCmd(new PppdCmd());
60 registerCmd(new PanCmd());
61 registerCmd(new SoftapCmd());
62 registerCmd(new UsbCmd());
65 sTetherCtrl = new TetherController();
67 sNatCtrl = new NatController();
69 sPppCtrl = new PppController();
71 sPanCtrl = new PanController();
73 sSoftapCtrl = new SoftapController();
75 sUsbCtrl = new UsbController();
78 CommandListener::InterfaceCmd::InterfaceCmd() :
79 NetdCommand("interface") {
82 int CommandListener::InterfaceCmd::runCommand(SocketClient *cli,
83 int argc, char **argv) {
85 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
89 if (!strcmp(argv[1], "list")) {
93 if (!(d = opendir("/sys/class/net"))) {
94 cli->sendMsg(ResponseCode::OperationFailed, "Failed to open sysfs dir", true);
98 while((de = readdir(d))) {
99 if (de->d_name[0] == '.')
101 cli->sendMsg(ResponseCode::InterfaceListResult, de->d_name, false);
104 cli->sendMsg(ResponseCode::CommandOkay, "Interface list completed", false);
106 } else if (!strcmp(argv[1], "readrxcounter")) {
108 cli->sendMsg(ResponseCode::CommandSyntaxError,
109 "Usage: interface readrxcounter <interface>", false);
112 unsigned long rx = 0, tx = 0;
113 if (readInterfaceCounters(argv[2], &rx, &tx)) {
114 cli->sendMsg(ResponseCode::OperationFailed, "Failed to read counters", true);
119 asprintf(&msg, "%lu", rx);
120 cli->sendMsg(ResponseCode::InterfaceRxCounterResult, msg, false);
124 } else if (!strcmp(argv[1], "readtxcounter")) {
126 cli->sendMsg(ResponseCode::CommandSyntaxError,
127 "Usage: interface readtxcounter <interface>", false);
130 unsigned long rx = 0, tx = 0;
131 if (readInterfaceCounters(argv[2], &rx, &tx)) {
132 cli->sendMsg(ResponseCode::OperationFailed, "Failed to read counters", true);
137 asprintf(&msg, "%lu", tx);
138 cli->sendMsg(ResponseCode::InterfaceTxCounterResult, msg, false);
141 } else if (!strcmp(argv[1], "getthrottle")) {
142 if (argc != 4 || (argc == 4 && (strcmp(argv[3], "rx") && (strcmp(argv[3], "tx"))))) {
143 cli->sendMsg(ResponseCode::CommandSyntaxError,
144 "Usage: interface getthrottle <interface> <rx|tx>", false);
149 int voldRc = ResponseCode::InterfaceRxThrottleResult;
151 if (!strcmp(argv[3], "rx")) {
152 rc = ThrottleController::getInterfaceRxThrottle(argv[2], &val);
154 rc = ThrottleController::getInterfaceTxThrottle(argv[2], &val);
155 voldRc = ResponseCode::InterfaceTxThrottleResult;
158 cli->sendMsg(ResponseCode::OperationFailed, "Failed to get throttle", true);
161 asprintf(&msg, "%u", val);
162 cli->sendMsg(voldRc, msg, false);
167 } else if (!strcmp(argv[1], "setthrottle")) {
169 cli->sendMsg(ResponseCode::CommandSyntaxError,
170 "Usage: interface setthrottle <interface> <rx_kbps> <tx_kbps>", false);
173 if (ThrottleController::setInterfaceThrottle(argv[2], atoi(argv[3]), atoi(argv[4]))) {
174 cli->sendMsg(ResponseCode::OperationFailed, "Failed to set throttle", true);
176 cli->sendMsg(ResponseCode::CommandOkay, "Interface throttling set", false);
181 * These commands take a minimum of 3 arguments
184 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
187 if (!strcmp(argv[1], "getcfg")) {
188 struct in_addr addr, mask;
189 unsigned char hwaddr[6];
193 memset(hwaddr, 0, sizeof(hwaddr));
195 if (ifc_get_info(argv[2], &addr.s_addr, &mask.s_addr, &flags)) {
196 cli->sendMsg(ResponseCode::OperationFailed, "Interface not found", true);
200 if (ifc_get_hwaddr(argv[2], (void *) hwaddr)) {
201 LOGW("Failed to retrieve HW addr for %s (%s)", argv[2], strerror(errno));
204 char *addr_s = strdup(inet_ntoa(addr));
205 char *mask_s = strdup(inet_ntoa(mask));
206 const char *updown, *brdcst, *loopbk, *ppp, *running, *multi;
208 updown = (flags & IFF_UP) ? "up" : "down";
209 brdcst = (flags & IFF_BROADCAST) ? " broadcast" : "";
210 loopbk = (flags & IFF_LOOPBACK) ? " loopback" : "";
211 ppp = (flags & IFF_POINTOPOINT) ? " point-to-point" : "";
212 running = (flags & IFF_RUNNING) ? " running" : "";
213 multi = (flags & IFF_MULTICAST) ? " multicast" : "";
217 asprintf(&flag_s, "[%s%s%s%s%s%s]", updown, brdcst, loopbk, ppp, running, multi);
220 asprintf(&msg, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x %s %s %s",
221 hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5],
222 addr_s, mask_s, flag_s);
224 cli->sendMsg(ResponseCode::InterfaceGetCfgResult, msg, false);
231 } else if (!strcmp(argv[1], "setcfg")) {
232 // arglist: iface addr mask [flags]
234 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
237 LOGD("Setting iface cfg");
239 struct in_addr addr, mask;
242 if (!inet_aton(argv[3], &addr)) {
243 cli->sendMsg(ResponseCode::CommandParameterError, "Invalid address", false);
247 if (!inet_aton(argv[4], &mask)) {
248 cli->sendMsg(ResponseCode::CommandParameterError, "Invalid netmask", false);
253 if (ifc_set_addr(argv[2], addr.s_addr)) {
254 cli->sendMsg(ResponseCode::OperationFailed, "Failed to set address", true);
258 if (ifc_set_mask(argv[2], mask.s_addr)) {
259 cli->sendMsg(ResponseCode::OperationFailed, "Failed to set netmask", true);
264 /* read from "[XX" arg to "YY]" arg */
265 bool bStarted = false;
266 for (int i = 5; i < argc; i++) {
267 char *flag = argv[i];
276 int len = strlen(flag);
277 if (flag[len-1] == ']') {
278 i = argc; // stop after this loop
281 if (!strcmp(flag, "up")) {
282 LOGD("Trying to bring up %s", argv[2]);
283 if (ifc_up(argv[2])) {
284 LOGE("Error upping interface");
285 cli->sendMsg(ResponseCode::OperationFailed, "Failed to up interface", true);
288 } else if (!strcmp(flag, "down")) {
289 LOGD("Trying to bring down %s", argv[2]);
290 if (ifc_down(argv[2])) {
291 LOGE("Error downing interface");
292 cli->sendMsg(ResponseCode::OperationFailed, "Failed to down interface", true);
295 } else if (!strcmp(flag, "broadcast")) {
296 LOGD("broadcast flag ignored");
297 } else if (!strcmp(flag, "multicast")) {
298 LOGD("multicast flag ignored");
300 cli->sendMsg(ResponseCode::CommandParameterError, "Flag unsupported", false);
304 cli->sendMsg(ResponseCode::CommandOkay, "Interface configuration set", false);
307 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown interface cmd", false);
314 CommandListener::ListTtysCmd::ListTtysCmd() :
315 NetdCommand("list_ttys") {
318 int CommandListener::ListTtysCmd::runCommand(SocketClient *cli,
319 int argc, char **argv) {
320 TtyCollection *tlist = sPppCtrl->getTtyList();
321 TtyCollection::iterator it;
323 for (it = tlist->begin(); it != tlist->end(); ++it) {
324 cli->sendMsg(ResponseCode::TtyListResult, *it, false);
327 cli->sendMsg(ResponseCode::CommandOkay, "Ttys listed.", false);
331 CommandListener::IpFwdCmd::IpFwdCmd() :
332 NetdCommand("ipfwd") {
335 int CommandListener::IpFwdCmd::runCommand(SocketClient *cli,
336 int argc, char **argv) {
340 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
344 if (!strcmp(argv[1], "status")) {
347 asprintf(&tmp, "Forwarding %s", (sTetherCtrl->getIpFwdEnabled() ? "enabled" : "disabled"));
348 cli->sendMsg(ResponseCode::IpFwdStatusResult, tmp, false);
351 } else if (!strcmp(argv[1], "enable")) {
352 rc = sTetherCtrl->setIpFwdEnabled(true);
353 } else if (!strcmp(argv[1], "disable")) {
354 rc = sTetherCtrl->setIpFwdEnabled(false);
356 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown ipfwd cmd", false);
361 cli->sendMsg(ResponseCode::CommandOkay, "ipfwd operation succeeded", false);
363 cli->sendMsg(ResponseCode::OperationFailed, "ipfwd operation failed", true);
369 CommandListener::TetherCmd::TetherCmd() :
370 NetdCommand("tether") {
373 int CommandListener::TetherCmd::runCommand(SocketClient *cli,
374 int argc, char **argv) {
378 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
382 if (!strcmp(argv[1], "stop")) {
383 rc = sTetherCtrl->stopTethering();
384 } else if (!strcmp(argv[1], "status")) {
387 asprintf(&tmp, "Tethering services %s",
388 (sTetherCtrl->isTetheringStarted() ? "started" : "stopped"));
389 cli->sendMsg(ResponseCode::TetherStatusResult, tmp, false);
394 * These commands take a minimum of 4 arguments
397 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
401 if (!strcmp(argv[1], "start")) {
403 cli->sendMsg(ResponseCode::CommandSyntaxError, "Bad number of arguments", false);
407 int num_addrs = argc - 2;
410 in_addr *addrs = (in_addr *)malloc(sizeof(in_addr) * num_addrs);
411 while (array_index < num_addrs) {
412 if (!inet_aton(argv[arg_index++], &(addrs[array_index++]))) {
413 cli->sendMsg(ResponseCode::CommandParameterError, "Invalid address", false);
418 rc = sTetherCtrl->startTethering(num_addrs, addrs);
420 } else if (!strcmp(argv[1], "interface")) {
421 if (!strcmp(argv[2], "add")) {
422 rc = sTetherCtrl->tetherInterface(argv[3]);
423 } else if (!strcmp(argv[2], "remove")) {
424 rc = sTetherCtrl->untetherInterface(argv[3]);
425 } else if (!strcmp(argv[2], "list")) {
426 InterfaceCollection *ilist = sTetherCtrl->getTetheredInterfaceList();
427 InterfaceCollection::iterator it;
429 for (it = ilist->begin(); it != ilist->end(); ++it) {
430 cli->sendMsg(ResponseCode::TetherInterfaceListResult, *it, false);
433 cli->sendMsg(ResponseCode::CommandParameterError,
434 "Unknown tether interface operation", false);
437 } else if (!strcmp(argv[1], "dns")) {
438 if (!strcmp(argv[2], "set")) {
439 rc = sTetherCtrl->setDnsForwarders(&argv[3], argc - 3);
440 } else if (!strcmp(argv[2], "list")) {
441 NetAddressCollection *dlist = sTetherCtrl->getDnsForwarders();
442 NetAddressCollection::iterator it;
444 for (it = dlist->begin(); it != dlist->end(); ++it) {
445 cli->sendMsg(ResponseCode::TetherDnsFwdTgtListResult, inet_ntoa(*it), false);
448 cli->sendMsg(ResponseCode::CommandParameterError,
449 "Unknown tether interface operation", false);
453 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown tether cmd", false);
459 cli->sendMsg(ResponseCode::CommandOkay, "Tether operation succeeded", false);
461 cli->sendMsg(ResponseCode::OperationFailed, "Tether operation failed", true);
467 CommandListener::NatCmd::NatCmd() :
471 int CommandListener::NatCmd::runCommand(SocketClient *cli,
472 int argc, char **argv) {
476 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
480 if (!strcmp(argv[1], "enable")) {
481 rc = sNatCtrl->enableNat(argv[2], argv[3]);
482 } else if (!strcmp(argv[1], "disable")) {
483 rc = sNatCtrl->disableNat(argv[2], argv[3]);
485 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown nat cmd", false);
490 cli->sendMsg(ResponseCode::CommandOkay, "Nat operation succeeded", false);
492 cli->sendMsg(ResponseCode::OperationFailed, "Nat operation failed", true);
498 CommandListener::PppdCmd::PppdCmd() :
499 NetdCommand("pppd") {
502 int CommandListener::PppdCmd::runCommand(SocketClient *cli,
503 int argc, char **argv) {
507 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
511 if (!strcmp(argv[1], "attach")) {
512 struct in_addr l, r, dns1, dns2;
514 memset(&dns1, sizeof(struct in_addr), 0);
515 memset(&dns2, sizeof(struct in_addr), 0);
517 if (!inet_aton(argv[3], &l)) {
518 cli->sendMsg(ResponseCode::CommandParameterError, "Invalid local address", false);
521 if (!inet_aton(argv[4], &r)) {
522 cli->sendMsg(ResponseCode::CommandParameterError, "Invalid remote address", false);
525 if ((argc > 3) && (!inet_aton(argv[5], &dns1))) {
526 cli->sendMsg(ResponseCode::CommandParameterError, "Invalid dns1 address", false);
529 if ((argc > 4) && (!inet_aton(argv[6], &dns2))) {
530 cli->sendMsg(ResponseCode::CommandParameterError, "Invalid dns2 address", false);
533 rc = sPppCtrl->attachPppd(argv[2], l, r, dns1, dns2);
534 } else if (!strcmp(argv[1], "detach")) {
535 rc = sPppCtrl->detachPppd(argv[2]);
537 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown pppd cmd", false);
542 cli->sendMsg(ResponseCode::CommandOkay, "Pppd operation succeeded", false);
544 cli->sendMsg(ResponseCode::OperationFailed, "Pppd operation failed", true);
550 CommandListener::PanCmd::PanCmd() :
554 int CommandListener::PanCmd::runCommand(SocketClient *cli,
555 int argc, char **argv) {
559 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
563 if (!strcmp(argv[1], "start")) {
564 rc = sPanCtrl->startPan();
565 } else if (!strcmp(argv[1], "stop")) {
566 rc = sPanCtrl->stopPan();
567 } else if (!strcmp(argv[1], "status")) {
570 asprintf(&tmp, "Pan services %s",
571 (sPanCtrl->isPanStarted() ? "started" : "stopped"));
572 cli->sendMsg(ResponseCode::PanStatusResult, tmp, false);
576 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown pan cmd", false);
581 cli->sendMsg(ResponseCode::CommandOkay, "Pan operation succeeded", false);
583 cli->sendMsg(ResponseCode::OperationFailed, "Pan operation failed", true);
589 CommandListener::SoftapCmd::SoftapCmd() :
590 NetdCommand("softap") {
593 int CommandListener::SoftapCmd::runCommand(SocketClient *cli,
594 int argc, char **argv) {
595 int rc = 0, flag = 0;
598 cli->sendMsg(ResponseCode::CommandSyntaxError, "Softap Missing argument", false);
602 if (!strcmp(argv[1], "start")) {
603 rc = sSoftapCtrl->startDriver(argv[2]);
604 } else if (!strcmp(argv[1], "stop")) {
605 rc = sSoftapCtrl->stopDriver(argv[2]);
606 } else if (!strcmp(argv[1], "startap")) {
607 rc = sSoftapCtrl->startSoftap();
608 } else if (!strcmp(argv[1], "stopap")) {
609 rc = sSoftapCtrl->stopSoftap();
610 } else if (!strcmp(argv[1], "fwreload")) {
611 rc = sSoftapCtrl->fwReloadSoftap(argc, argv);
612 } else if (!strcmp(argv[1], "status")) {
615 asprintf(&tmp, "Softap service %s",
616 (sSoftapCtrl->isSoftapStarted() ? "started" : "stopped"));
617 cli->sendMsg(ResponseCode::SoftapStatusResult, tmp, false);
620 } else if (!strcmp(argv[1], "set")) {
621 rc = sSoftapCtrl->setSoftap(argc, argv);
623 cli->sendMsg(ResponseCode::CommandSyntaxError, "Softap Unknown cmd", false);
628 cli->sendMsg(ResponseCode::CommandOkay, "Softap operation succeeded", false);
630 cli->sendMsg(ResponseCode::OperationFailed, "Softap operation failed", true);
636 CommandListener::UsbCmd::UsbCmd() :
640 int CommandListener::UsbCmd::runCommand(SocketClient *cli, int argc, char **argv) {
644 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usb Missing argument", false);
648 if (!strcmp(argv[1], "startrndis")) {
649 rc = sUsbCtrl->startRNDIS();
650 } else if (!strcmp(argv[1], "stoprndis")) {
651 rc = sUsbCtrl->stopRNDIS();
652 } else if (!strcmp(argv[1], "rndisstatus")) {
655 asprintf(&tmp, "Usb RNDIS %s",
656 (sUsbCtrl->isRNDISStarted() ? "started" : "stopped"));
657 cli->sendMsg(ResponseCode::UsbRNDISStatusResult, tmp, false);
661 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usb Unknown cmd", false);
666 cli->sendMsg(ResponseCode::CommandOkay, "Usb operation succeeded", false);
668 cli->sendMsg(ResponseCode::OperationFailed, "Softap operation failed", true);
674 int CommandListener::readInterfaceCounters(const char *iface, unsigned long *rx, unsigned long *tx) {
675 FILE *fp = fopen("/proc/net/dev", "r");
677 LOGE("Failed to open /proc/net/dev (%s)", strerror(errno));
683 fgets(buffer, sizeof(buffer), fp); // Header 1
684 fgets(buffer, sizeof(buffer), fp); // Header 2
685 while(fgets(buffer, sizeof(buffer), fp)) {
686 buffer[strlen(buffer)-1] = '\0';
690 sscanf(buffer, "%30s %lu %lu %lu %lu %lu %lu %lu %lu %lu",
691 name, rx, &d, &d, &d, &d, &d, &d, &d, tx);
692 char *rxString = strchr(name, ':');
695 // when the rx count gets too big it changes from "name: 999" to "name:1000"
696 // and the sscanf munge the two together. Detect that and fix
697 // note that all the %lu will be off by one and the real tx value will be in d
698 if (*rxString != '\0') {
700 sscanf(rxString, "%20lu", rx);
702 if (strcmp(name, iface)) {