1d18304287dbabc7835be771400b85d4ae8b63de6San Mehat/*
2d18304287dbabc7835be771400b85d4ae8b63de6San Mehat * Copyright (C) 2008 The Android Open Source Project
3d18304287dbabc7835be771400b85d4ae8b63de6San Mehat *
4d18304287dbabc7835be771400b85d4ae8b63de6San Mehat * Licensed under the Apache License, Version 2.0 (the "License");
5d18304287dbabc7835be771400b85d4ae8b63de6San Mehat * you may not use this file except in compliance with the License.
6d18304287dbabc7835be771400b85d4ae8b63de6San Mehat * You may obtain a copy of the License at
7d18304287dbabc7835be771400b85d4ae8b63de6San Mehat *
8d18304287dbabc7835be771400b85d4ae8b63de6San Mehat *      http://www.apache.org/licenses/LICENSE-2.0
9d18304287dbabc7835be771400b85d4ae8b63de6San Mehat *
10d18304287dbabc7835be771400b85d4ae8b63de6San Mehat * Unless required by applicable law or agreed to in writing, software
11d18304287dbabc7835be771400b85d4ae8b63de6San Mehat * distributed under the License is distributed on an "AS IS" BASIS,
12d18304287dbabc7835be771400b85d4ae8b63de6San Mehat * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d18304287dbabc7835be771400b85d4ae8b63de6San Mehat * See the License for the specific language governing permissions and
14d18304287dbabc7835be771400b85d4ae8b63de6San Mehat * limitations under the License.
15d18304287dbabc7835be771400b85d4ae8b63de6San Mehat */
16d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
171fb02dfc26e06b83e756ab3538b7ebc2136f535dJP Abgrall// #define LOG_NDEBUG 0
18db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall
19d18304287dbabc7835be771400b85d4ae8b63de6San Mehat#include <stdlib.h>
20d18304287dbabc7835be771400b85d4ae8b63de6San Mehat#include <sys/socket.h>
21d18304287dbabc7835be771400b85d4ae8b63de6San Mehat#include <sys/types.h>
22d18304287dbabc7835be771400b85d4ae8b63de6San Mehat#include <netinet/in.h>
23d18304287dbabc7835be771400b85d4ae8b63de6San Mehat#include <arpa/inet.h>
24d18304287dbabc7835be771400b85d4ae8b63de6San Mehat#include <dirent.h>
25d18304287dbabc7835be771400b85d4ae8b63de6San Mehat#include <errno.h>
26ff2c0d8c13457e43f0d4bf06d3177271aac104c1Olivier Bailly#include <string.h>
2732a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff#include <fcntl.h>
285c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat#include <linux/if.h>
295c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
30d18304287dbabc7835be771400b85d4ae8b63de6San Mehat#define LOG_TAG "CommandListener"
31d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
321ed47c4a016e420a855ee46922e512150245ff61Lorenzo Colitti#include <cutils/log.h>
331ed47c4a016e420a855ee46922e512150245ff61Lorenzo Colitti#include <netutils/ifc.h>
34d18304287dbabc7835be771400b85d4ae8b63de6San Mehat#include <sysutils/SocketClient.h>
35d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
36d18304287dbabc7835be771400b85d4ae8b63de6San Mehat#include "CommandListener.h"
37d18304287dbabc7835be771400b85d4ae8b63de6San Mehat#include "ResponseCode.h"
38a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#include "ThrottleController.h"
394a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include "BandwidthController.h"
400031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall#include "IdletimerController.h"
41fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt#include "SecondaryTableController.h"
420031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall#include "oem_iptables_hook.h"
438e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey#include "NetdConstants.h"
44d8c64026aaae5a9987151b719bd840ec7d68747aJeff Sharkey#include "FirewallController.h"
455c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
469d10b341a0ba46f108cb96e46691197d778cbc06San MehatTetherController *CommandListener::sTetherCtrl = NULL;
479ff78fb7da7158f5bd7c86d89a842691820259cfSan MehatNatController *CommandListener::sNatCtrl = NULL;
48d5573d34c8fac49e16b20cf144486125bf940086San MehatPppController *CommandListener::sPppCtrl = NULL;
495af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry ShmidtSoftapController *CommandListener::sSoftapCtrl = NULL;
504a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP AbgrallBandwidthController * CommandListener::sBandwidthCtrl = NULL;
510031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP AbgrallIdletimerController * CommandListener::sIdletimerCtrl = NULL;
522eab1f762badb7ba46b95716e62ea4548a979903Dmitry ShmidtInterfaceController *CommandListener::sInterfaceCtrl = NULL;
5389c1e975f0c03997808d35c7be10b244c5309f9fMattias FalkResolverController *CommandListener::sResolverCtrl = NULL;
54fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert GreenwaltSecondaryTableController *CommandListener::sSecondaryTableCtrl = NULL;
55d8c64026aaae5a9987151b719bd840ec7d68747aJeff SharkeyFirewallController *CommandListener::sFirewallCtrl = NULL;
569d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
578e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey/**
588e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey * List of module chains to be created, along with explicit ordering. ORDERING
598e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey * IS CRITICAL, AND SHOULD BE TRIPLE-CHECKED WITH EACH CHANGE.
608e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey */
618e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeystatic const char* FILTER_INPUT[] = {
62d8c64026aaae5a9987151b719bd840ec7d68747aJeff Sharkey        // Bandwidth should always be early in input chain, to make sure we
63d8c64026aaae5a9987151b719bd840ec7d68747aJeff Sharkey        // correctly count incoming traffic against data plan.
648e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        BandwidthController::LOCAL_INPUT,
65d8c64026aaae5a9987151b719bd840ec7d68747aJeff Sharkey        FirewallController::LOCAL_INPUT,
668e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        NULL,
678e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey};
688e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
698e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeystatic const char* FILTER_FORWARD[] = {
708e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        OEM_IPTABLES_FILTER_FORWARD,
71d8c64026aaae5a9987151b719bd840ec7d68747aJeff Sharkey        FirewallController::LOCAL_FORWARD,
728e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        BandwidthController::LOCAL_FORWARD,
738e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        NatController::LOCAL_FORWARD,
748e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        NULL,
758e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey};
768e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
778e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeystatic const char* FILTER_OUTPUT[] = {
788e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        OEM_IPTABLES_FILTER_OUTPUT,
79d8c64026aaae5a9987151b719bd840ec7d68747aJeff Sharkey        FirewallController::LOCAL_OUTPUT,
808e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        BandwidthController::LOCAL_OUTPUT,
818e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        NULL,
828e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey};
838e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
848e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeystatic const char* RAW_PREROUTING[] = {
858e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        BandwidthController::LOCAL_RAW_PREROUTING,
865c4faf7602de0268aa09a3a525b427f83d617b5cHaoyu Bai        IdletimerController::LOCAL_RAW_PREROUTING,
878e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        NULL,
888e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey};
898e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
908e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeystatic const char* MANGLE_POSTROUTING[] = {
918e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        BandwidthController::LOCAL_MANGLE_POSTROUTING,
925c4faf7602de0268aa09a3a525b427f83d617b5cHaoyu Bai        IdletimerController::LOCAL_MANGLE_POSTROUTING,
938e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        NULL,
948e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey};
958e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
968e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeystatic const char* NAT_PREROUTING[] = {
978e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        OEM_IPTABLES_NAT_PREROUTING,
988e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        NULL,
998e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey};
1008e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
1018e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeystatic const char* NAT_POSTROUTING[] = {
1028e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        NatController::LOCAL_NAT_POSTROUTING,
1038e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        NULL,
1048e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey};
1058e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
1068e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeystatic void createChildChains(IptablesTarget target, const char* table, const char* parentChain,
1078e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        const char** childChains) {
1088e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    const char** childChain = childChains;
1098e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    do {
1108e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        // Order is important:
1118e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        // -D to delete any pre-existing jump rule (removes references
1128e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        //    that would prevent -X from working)
1138e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        // -F to flush any existing chain
1148e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        // -X to delete any existing chain
1158e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        // -N to create the chain
1168e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        // -A to append the chain to parent
1178e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
1188e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        execIptablesSilently(target, "-t", table, "-D", parentChain, "-j", *childChain, NULL);
1198e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        execIptablesSilently(target, "-t", table, "-F", *childChain, NULL);
1208e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        execIptablesSilently(target, "-t", table, "-X", *childChain, NULL);
1218e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        execIptables(target, "-t", table, "-N", *childChain, NULL);
1228e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        execIptables(target, "-t", table, "-A", parentChain, "-j", *childChain, NULL);
1238e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    } while (*(++childChain) != NULL);
1248e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey}
1258e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
126d18304287dbabc7835be771400b85d4ae8b63de6San MehatCommandListener::CommandListener() :
127a8e59f8c50aed3f91dead377b8ae21646978506cRobert Greenwalt                 FrameworkListener("netd", true) {
1285c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat    registerCmd(new InterfaceCmd());
129d18304287dbabc7835be771400b85d4ae8b63de6San Mehat    registerCmd(new IpFwdCmd());
130d18304287dbabc7835be771400b85d4ae8b63de6San Mehat    registerCmd(new TetherCmd());
131d18304287dbabc7835be771400b85d4ae8b63de6San Mehat    registerCmd(new NatCmd());
132d5573d34c8fac49e16b20cf144486125bf940086San Mehat    registerCmd(new ListTtysCmd());
133d5573d34c8fac49e16b20cf144486125bf940086San Mehat    registerCmd(new PppdCmd());
1345af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt    registerCmd(new SoftapCmd());
1354a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    registerCmd(new BandwidthControlCmd());
1360031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    registerCmd(new IdletimerControlCmd());
13789c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    registerCmd(new ResolverCmd());
138d8c64026aaae5a9987151b719bd840ec7d68747aJeff Sharkey    registerCmd(new FirewallCmd());
1399d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
140fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt    if (!sSecondaryTableCtrl)
141fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt        sSecondaryTableCtrl = new SecondaryTableController();
1429d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    if (!sTetherCtrl)
1439d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        sTetherCtrl = new TetherController();
1449ff78fb7da7158f5bd7c86d89a842691820259cfSan Mehat    if (!sNatCtrl)
145fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt        sNatCtrl = new NatController(sSecondaryTableCtrl);
146d5573d34c8fac49e16b20cf144486125bf940086San Mehat    if (!sPppCtrl)
147d5573d34c8fac49e16b20cf144486125bf940086San Mehat        sPppCtrl = new PppController();
1485af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt    if (!sSoftapCtrl)
1495af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt        sSoftapCtrl = new SoftapController();
1504a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall    if (!sBandwidthCtrl)
1514a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall        sBandwidthCtrl = new BandwidthController();
1520031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    if (!sIdletimerCtrl)
1530031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall        sIdletimerCtrl = new IdletimerController();
15489c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    if (!sResolverCtrl)
15589c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        sResolverCtrl = new ResolverController();
156d8c64026aaae5a9987151b719bd840ec7d68747aJeff Sharkey    if (!sFirewallCtrl)
157d8c64026aaae5a9987151b719bd840ec7d68747aJeff Sharkey        sFirewallCtrl = new FirewallController();
1582eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt    if (!sInterfaceCtrl)
1592eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt        sInterfaceCtrl = new InterfaceController();
1600031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
1610031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    /*
1628e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey     * This is the only time we touch top-level chains in iptables; controllers
1638e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey     * should only mutate rules inside of their children chains, as created by
1648e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey     * the constants above.
1658e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey     *
1668e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey     * Modules should never ACCEPT packets (except in well-justified cases);
1678e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey     * they should instead defer to any remaining modules using RETURN, or
1688e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey     * otherwise DROP/REJECT.
1690031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     */
1708e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
1718e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    // Create chains for children modules
1728e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    createChildChains(V4V6, "filter", "INPUT", FILTER_INPUT);
1738e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    createChildChains(V4V6, "filter", "FORWARD", FILTER_FORWARD);
1748e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    createChildChains(V4V6, "filter", "OUTPUT", FILTER_OUTPUT);
1758e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    createChildChains(V4V6, "raw", "PREROUTING", RAW_PREROUTING);
1768e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    createChildChains(V4V6, "mangle", "POSTROUTING", MANGLE_POSTROUTING);
1778e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    createChildChains(V4, "nat", "PREROUTING", NAT_PREROUTING);
1788e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    createChildChains(V4, "nat", "POSTROUTING", NAT_POSTROUTING);
1798e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
1808e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    // Let each module setup their child chains
1810031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    setupOemIptablesHook();
1828e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
183d8c64026aaae5a9987151b719bd840ec7d68747aJeff Sharkey    /* When enabled, DROPs all packets except those matching rules. */
184d8c64026aaae5a9987151b719bd840ec7d68747aJeff Sharkey    sFirewallCtrl->setupIptablesHooks();
185d8c64026aaae5a9987151b719bd840ec7d68747aJeff Sharkey
1860031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    /* Does DROPs in FORWARD by default */
1870031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    sNatCtrl->setupIptablesHooks();
1880031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    /*
1890031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     * Does REJECT in INPUT, OUTPUT. Does counting also.
1900031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     * No DROP/REJECT allowed later in netfilter-flow hook order.
1910031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     */
1920031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    sBandwidthCtrl->setupIptablesHooks();
1930031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    /*
1940031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     * Counts in nat: PREROUTING, POSTROUTING.
1950031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     * No DROP/REJECT allowed later in netfilter-flow hook order.
1960031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall     */
1970031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    sIdletimerCtrl->setupIptablesHooks();
1980031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall
1990031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrall    sBandwidthCtrl->enableBandwidthControl(false);
200d18304287dbabc7835be771400b85d4ae8b63de6San Mehat}
201d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
2025c1b8af16dbbc20c89aaca2f93e725e12b16d055San MehatCommandListener::InterfaceCmd::InterfaceCmd() :
2035c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                 NetdCommand("interface") {
204d18304287dbabc7835be771400b85d4ae8b63de6San Mehat}
205d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
206c1b3870147acd0dd42e2e767df08b4d64d5d2423repo syncint CommandListener::writeFile(const char *path, const char *value, int size) {
207c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync    int fd = open(path, O_WRONLY);
208c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync    if (fd < 0) {
2095ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Failed to open %s: %s", path, strerror(errno));
210c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync        return -1;
211c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync    }
212c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync
213c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync    if (write(fd, value, size) != size) {
2145ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Failed to write %s: %s", path, strerror(errno));
215c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync        close(fd);
216c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync        return -1;
217c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync    }
218c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync    close(fd);
219c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync    return 0;
220c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync}
221c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync
2225c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehatint CommandListener::InterfaceCmd::runCommand(SocketClient *cli,
223d18304287dbabc7835be771400b85d4ae8b63de6San Mehat                                                      int argc, char **argv) {
2245c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat    if (argc < 2) {
2255c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
2265c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        return 0;
2275c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat    }
2285c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
2295c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat    if (!strcmp(argv[1], "list")) {
2305c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        DIR *d;
2315c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        struct dirent *de;
2325c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
2335c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        if (!(d = opendir("/sys/class/net"))) {
2345c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            cli->sendMsg(ResponseCode::OperationFailed, "Failed to open sysfs dir", true);
2355c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            return 0;
2365c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        }
2375c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
2385c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        while((de = readdir(d))) {
2395c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            if (de->d_name[0] == '.')
2405c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                continue;
2415c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            cli->sendMsg(ResponseCode::InterfaceListResult, de->d_name, false);
2425c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        }
2435c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        closedir(d);
2445c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        cli->sendMsg(ResponseCode::CommandOkay, "Interface list completed", false);
2455c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        return 0;
2468e46896b4c4be5deaef651a12b17314f63ae9070San Mehat    } else if (!strcmp(argv[1], "readrxcounter")) {
2478e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        if (argc != 3) {
2488e46896b4c4be5deaef651a12b17314f63ae9070San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError,
2498e46896b4c4be5deaef651a12b17314f63ae9070San Mehat                    "Usage: interface readrxcounter <interface>", false);
2508e46896b4c4be5deaef651a12b17314f63ae9070San Mehat            return 0;
2518e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        }
2528e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        unsigned long rx = 0, tx = 0;
2538e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        if (readInterfaceCounters(argv[2], &rx, &tx)) {
2548e46896b4c4be5deaef651a12b17314f63ae9070San Mehat            cli->sendMsg(ResponseCode::OperationFailed, "Failed to read counters", true);
2558e46896b4c4be5deaef651a12b17314f63ae9070San Mehat            return 0;
2568e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        }
2578e46896b4c4be5deaef651a12b17314f63ae9070San Mehat
2588e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        char *msg;
2598e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        asprintf(&msg, "%lu", rx);
2608e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        cli->sendMsg(ResponseCode::InterfaceRxCounterResult, msg, false);
2618e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        free(msg);
2628e46896b4c4be5deaef651a12b17314f63ae9070San Mehat
2638e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        return 0;
2648e46896b4c4be5deaef651a12b17314f63ae9070San Mehat    } else if (!strcmp(argv[1], "readtxcounter")) {
2658e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        if (argc != 3) {
2668e46896b4c4be5deaef651a12b17314f63ae9070San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError,
2678e46896b4c4be5deaef651a12b17314f63ae9070San Mehat                    "Usage: interface readtxcounter <interface>", false);
2688e46896b4c4be5deaef651a12b17314f63ae9070San Mehat            return 0;
2698e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        }
2708e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        unsigned long rx = 0, tx = 0;
2718e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        if (readInterfaceCounters(argv[2], &rx, &tx)) {
2728e46896b4c4be5deaef651a12b17314f63ae9070San Mehat            cli->sendMsg(ResponseCode::OperationFailed, "Failed to read counters", true);
2738e46896b4c4be5deaef651a12b17314f63ae9070San Mehat            return 0;
2748e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        }
2758e46896b4c4be5deaef651a12b17314f63ae9070San Mehat
2768e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        char *msg = NULL;
2778e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        asprintf(&msg, "%lu", tx);
2788e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        cli->sendMsg(ResponseCode::InterfaceTxCounterResult, msg, false);
2798e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        free(msg);
2808e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        return 0;
2818e46896b4c4be5deaef651a12b17314f63ae9070San Mehat    } else if (!strcmp(argv[1], "getthrottle")) {
282a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        if (argc != 4 || (argc == 4 && (strcmp(argv[3], "rx") && (strcmp(argv[3], "tx"))))) {
2838e46896b4c4be5deaef651a12b17314f63ae9070San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError,
2848e46896b4c4be5deaef651a12b17314f63ae9070San Mehat                    "Usage: interface getthrottle <interface> <rx|tx>", false);
2858e46896b4c4be5deaef651a12b17314f63ae9070San Mehat            return 0;
2868e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        }
287a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        int val = 0;
288a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        int rc = 0;
289a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        int voldRc = ResponseCode::InterfaceRxThrottleResult;
290a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
291a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        if (!strcmp(argv[3], "rx")) {
292a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat            rc = ThrottleController::getInterfaceRxThrottle(argv[2], &val);
293a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        } else {
294a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat            rc = ThrottleController::getInterfaceTxThrottle(argv[2], &val);
295a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat            voldRc = ResponseCode::InterfaceTxThrottleResult;
296a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        }
297a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        if (rc) {
298a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat            cli->sendMsg(ResponseCode::OperationFailed, "Failed to get throttle", true);
299a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        } else {
300a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat            char *msg = NULL;
301a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat            asprintf(&msg, "%u", val);
302a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat            cli->sendMsg(voldRc, msg, false);
303a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat            free(msg);
304a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat            return 0;
305a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        }
3068e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        return 0;
3078e46896b4c4be5deaef651a12b17314f63ae9070San Mehat    } else if (!strcmp(argv[1], "setthrottle")) {
3088e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        if (argc != 5) {
3098e46896b4c4be5deaef651a12b17314f63ae9070San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError,
310a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat                    "Usage: interface setthrottle <interface> <rx_kbps> <tx_kbps>", false);
3118e46896b4c4be5deaef651a12b17314f63ae9070San Mehat            return 0;
3128e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        }
313a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        if (ThrottleController::setInterfaceThrottle(argv[2], atoi(argv[3]), atoi(argv[4]))) {
314a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat            cli->sendMsg(ResponseCode::OperationFailed, "Failed to set throttle", true);
315a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        } else {
316a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat            cli->sendMsg(ResponseCode::CommandOkay, "Interface throttling set", false);
317a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        }
3188e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        return 0;
3192eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt    } else if (!strcmp(argv[1], "driver")) {
3202eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt        int rc;
3212eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt        char *rbuf;
3222eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt
3232eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt        if (argc < 4) {
3242eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt            cli->sendMsg(ResponseCode::CommandSyntaxError,
3252eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt                    "Usage: interface driver <interface> <cmd> <args>", false);
3262eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt            return 0;
3272eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt        }
3282eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt        rc = sInterfaceCtrl->interfaceCommand(argc, argv, &rbuf);
3292eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt        if (rc) {
3302eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt            cli->sendMsg(ResponseCode::OperationFailed, "Failed to execute command", true);
3312eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt        } else {
3322eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt            cli->sendMsg(ResponseCode::CommandOkay, rbuf, false);
3332eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt        }
3342eab1f762badb7ba46b95716e62ea4548a979903Dmitry Shmidt        return 0;
3355c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat    } else {
3365c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        /*
3375c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat         * These commands take a minimum of 3 arguments
3385c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat         */
3395c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        if (argc < 3) {
3405c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
3415c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            return 0;
3425c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        }
343e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt
344fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt        //     0       1       2        3          4           5     6      7
345fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt        // interface route add/remove iface default/secondary dest prefix gateway
346e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt        if (!strcmp(argv[1], "route")) {
347e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt            int prefix_length = 0;
348fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt            if (argc < 8) {
349e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt                cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
350e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt                return 0;
351e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt            }
352fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt            if (sscanf(argv[6], "%d", &prefix_length) != 1) {
353e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt                cli->sendMsg(ResponseCode::CommandParameterError, "Invalid route prefix", false);
354e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt                return 0;
355e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt            }
356e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt            if (!strcmp(argv[2], "add")) {
357fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                if (!strcmp(argv[4], "default")) {
358fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                    if (ifc_add_route(argv[3], argv[5], prefix_length, argv[7])) {
359fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                        cli->sendMsg(ResponseCode::OperationFailed,
360fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                                "Failed to add route to default table", true);
361fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                    } else {
362fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                        cli->sendMsg(ResponseCode::CommandOkay,
363fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                                "Route added to default table", false);
364fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                    }
365fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                } else if (!strcmp(argv[4], "secondary")) {
366fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                    return sSecondaryTableCtrl->addRoute(cli, argv[3], argv[5],
367fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                            prefix_length, argv[7]);
368e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt                } else {
369fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                    cli->sendMsg(ResponseCode::CommandParameterError,
370fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                            "Invalid route type, expecting 'default' or 'secondary'", false);
371fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                    return 0;
372e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt                }
373e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt            } else if (!strcmp(argv[2], "remove")) {
374fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                if (!strcmp(argv[4], "default")) {
375fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                    if (ifc_remove_route(argv[3], argv[5], prefix_length, argv[7])) {
376fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                        cli->sendMsg(ResponseCode::OperationFailed,
377fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                                "Failed to remove route from default table", true);
378fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                    } else {
379fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                        cli->sendMsg(ResponseCode::CommandOkay,
380fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                                "Route removed from default table", false);
381fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                    }
382fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                } else if (!strcmp(argv[4], "secondary")) {
383fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                    return sSecondaryTableCtrl->removeRoute(cli, argv[3], argv[5],
384fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                            prefix_length, argv[7]);
385e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt                } else {
386fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                    cli->sendMsg(ResponseCode::CommandParameterError,
387fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                            "Invalid route type, expecting 'default' or 'secondary'", false);
388fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt                    return 0;
389e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt                }
390e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt            } else {
391e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt                cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown interface cmd", false);
392e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt            }
393e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt            return 0;
394e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt        }
395e019b65925217bcc3ad9b662aa3125f0593057afRobert Greenwalt
3965c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        if (!strcmp(argv[1], "getcfg")) {
3971d93e170f13b221362ee268fdc94b25db06c21c9Robert Greenwalt            struct in_addr addr;
3981d93e170f13b221362ee268fdc94b25db06c21c9Robert Greenwalt            int prefixLength;
3995c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            unsigned char hwaddr[6];
4005c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            unsigned flags = 0;
4015c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
4025c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            ifc_init();
4035c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            memset(hwaddr, 0, sizeof(hwaddr));
4045c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
4051d93e170f13b221362ee268fdc94b25db06c21c9Robert Greenwalt            if (ifc_get_info(argv[2], &addr.s_addr, &prefixLength, &flags)) {
4065c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                cli->sendMsg(ResponseCode::OperationFailed, "Interface not found", true);
407e734edd35d8d02da96330d77420650c044a12527Irfan Sheriff                ifc_close();
4085c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                return 0;
4095c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            }
4105c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
4115c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            if (ifc_get_hwaddr(argv[2], (void *) hwaddr)) {
4120e76b761a1514d5182675dd7b7d33725f62d6bc5Steve Block                ALOGW("Failed to retrieve HW addr for %s (%s)", argv[2], strerror(errno));
4135c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            }
4145c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
4155c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            char *addr_s = strdup(inet_ntoa(addr));
4165c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            const char *updown, *brdcst, *loopbk, *ppp, *running, *multi;
4175c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
4185c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            updown =  (flags & IFF_UP)           ? "up" : "down";
4195c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            brdcst =  (flags & IFF_BROADCAST)    ? " broadcast" : "";
4205c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            loopbk =  (flags & IFF_LOOPBACK)     ? " loopback" : "";
4215c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            ppp =     (flags & IFF_POINTOPOINT)  ? " point-to-point" : "";
4225c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            running = (flags & IFF_RUNNING)      ? " running" : "";
4235c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            multi =   (flags & IFF_MULTICAST)    ? " multicast" : "";
4245c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
4255c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            char *flag_s;
4265c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
42731ea0b03afeedd17f703b7a6d1e5c9feb2d6000dJeff Sharkey            asprintf(&flag_s, "%s%s%s%s%s%s", updown, brdcst, loopbk, ppp, running, multi);
4285c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
4295c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            char *msg = NULL;
4301d93e170f13b221362ee268fdc94b25db06c21c9Robert Greenwalt            asprintf(&msg, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x %s %d %s",
4315c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                     hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5],
4321d93e170f13b221362ee268fdc94b25db06c21c9Robert Greenwalt                     addr_s, prefixLength, flag_s);
4335c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
43401fed783828b592cda3b774deea4099a9e690e3dSan Mehat            cli->sendMsg(ResponseCode::InterfaceGetCfgResult, msg, false);
4355c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
4365c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            free(addr_s);
4375c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            free(flag_s);
4385c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            free(msg);
439e734edd35d8d02da96330d77420650c044a12527Irfan Sheriff
440e734edd35d8d02da96330d77420650c044a12527Irfan Sheriff            ifc_close();
4415c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            return 0;
4425c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        } else if (!strcmp(argv[1], "setcfg")) {
443778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt            // arglist: iface [addr prefixLength] flags
444778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt            if (argc < 4) {
4455c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
4465c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                return 0;
4475c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            }
4487b984e3f7e724f8a3547a707210319f3d479f261Steve Block            ALOGD("Setting iface cfg");
4495c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
4501d93e170f13b221362ee268fdc94b25db06c21c9Robert Greenwalt            struct in_addr addr;
4515c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            unsigned flags = 0;
452778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt            int index = 5;
4535c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
4545c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            ifc_init();
4555c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
456778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt            if (!inet_aton(argv[3], &addr)) {
457778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt                // Handle flags only case
458778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt                index = 3;
459778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt            } else {
460778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt                if (ifc_set_addr(argv[2], addr.s_addr)) {
461778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt                    cli->sendMsg(ResponseCode::OperationFailed, "Failed to set address", true);
462778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt                    ifc_close();
463778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt                    return 0;
464778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt                }
465778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt
466778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt                // Set prefix length on a non zero address
467778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt                if (addr.s_addr != 0 && ifc_set_prefixLength(argv[2], atoi(argv[4]))) {
468778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt                   cli->sendMsg(ResponseCode::OperationFailed, "Failed to set prefixLength", true);
469778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt                   ifc_close();
470778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt                   return 0;
471778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt               }
4725c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            }
4735c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat
4745c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            /* Process flags */
475778ffe4d3dcab5453b57a0d960ea7f066374b953Dmitry Shmidt            for (int i = index; i < argc; i++) {
476e7b9422f0dc7a18041536c63efe74c7095496258Robert Greenwalt                char *flag = argv[i];
477e7b9422f0dc7a18041536c63efe74c7095496258Robert Greenwalt                if (!strcmp(flag, "up")) {
4787b984e3f7e724f8a3547a707210319f3d479f261Steve Block                    ALOGD("Trying to bring up %s", argv[2]);
4795c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                    if (ifc_up(argv[2])) {
4805ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block                        ALOGE("Error upping interface");
4815c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                        cli->sendMsg(ResponseCode::OperationFailed, "Failed to up interface", true);
482e734edd35d8d02da96330d77420650c044a12527Irfan Sheriff                        ifc_close();
4835c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                        return 0;
4845c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                    }
485e7b9422f0dc7a18041536c63efe74c7095496258Robert Greenwalt                } else if (!strcmp(flag, "down")) {
4867b984e3f7e724f8a3547a707210319f3d479f261Steve Block                    ALOGD("Trying to bring down %s", argv[2]);
4875c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                    if (ifc_down(argv[2])) {
4885ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block                        ALOGE("Error downing interface");
4895c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                        cli->sendMsg(ResponseCode::OperationFailed, "Failed to down interface", true);
490e734edd35d8d02da96330d77420650c044a12527Irfan Sheriff                        ifc_close();
4915c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                        return 0;
4925c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                    }
493e7b9422f0dc7a18041536c63efe74c7095496258Robert Greenwalt                } else if (!strcmp(flag, "broadcast")) {
4947c2ddd84ec501aaedb806a3ecec5de00d618a036Jeff Sharkey                    // currently ignored
495e7b9422f0dc7a18041536c63efe74c7095496258Robert Greenwalt                } else if (!strcmp(flag, "multicast")) {
4967c2ddd84ec501aaedb806a3ecec5de00d618a036Jeff Sharkey                    // currently ignored
4977c2ddd84ec501aaedb806a3ecec5de00d618a036Jeff Sharkey                } else if (!strcmp(flag, "running")) {
4987c2ddd84ec501aaedb806a3ecec5de00d618a036Jeff Sharkey                    // currently ignored
4997c2ddd84ec501aaedb806a3ecec5de00d618a036Jeff Sharkey                } else if (!strcmp(flag, "loopback")) {
5007c2ddd84ec501aaedb806a3ecec5de00d618a036Jeff Sharkey                    // currently ignored
5017c2ddd84ec501aaedb806a3ecec5de00d618a036Jeff Sharkey                } else if (!strcmp(flag, "point-to-point")) {
5027c2ddd84ec501aaedb806a3ecec5de00d618a036Jeff Sharkey                    // currently ignored
5035c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                } else {
5045c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                    cli->sendMsg(ResponseCode::CommandParameterError, "Flag unsupported", false);
505e734edd35d8d02da96330d77420650c044a12527Irfan Sheriff                    ifc_close();
5065c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                    return 0;
5075c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat                }
5085c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            }
509e734edd35d8d02da96330d77420650c044a12527Irfan Sheriff
5105c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            cli->sendMsg(ResponseCode::CommandOkay, "Interface configuration set", false);
511e734edd35d8d02da96330d77420650c044a12527Irfan Sheriff            ifc_close();
512e734edd35d8d02da96330d77420650c044a12527Irfan Sheriff            return 0;
513e734edd35d8d02da96330d77420650c044a12527Irfan Sheriff        } else if (!strcmp(argv[1], "clearaddrs")) {
514e734edd35d8d02da96330d77420650c044a12527Irfan Sheriff            // arglist: iface
5157b984e3f7e724f8a3547a707210319f3d479f261Steve Block            ALOGD("Clearing all IP addresses on %s", argv[2]);
516e734edd35d8d02da96330d77420650c044a12527Irfan Sheriff
5171ed47c4a016e420a855ee46922e512150245ff61Lorenzo Colitti            ifc_clear_addresses(argv[2]);
5181ed47c4a016e420a855ee46922e512150245ff61Lorenzo Colitti
519e734edd35d8d02da96330d77420650c044a12527Irfan Sheriff            cli->sendMsg(ResponseCode::CommandOkay, "Interface IP addresses cleared", false);
5205c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            return 0;
52132a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff        } else if (!strcmp(argv[1], "ipv6privacyextensions")) {
52232a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff            if (argc != 4) {
52332a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff                cli->sendMsg(ResponseCode::CommandSyntaxError,
52432a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff                        "Usage: interface ipv6privacyextensions <interface> <enable|disable>",
52532a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff                        false);
52632a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff                return 0;
52732a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff            }
52832a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff
529c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync            char *tmp;
53032a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff            asprintf(&tmp, "/proc/sys/net/ipv6/conf/%s/use_tempaddr", argv[2]);
531c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync
532c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync            if (writeFile(tmp, !strncmp(argv[3], "enable", 7) ? "2" : "0", 1) < 0) {
53332a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff                free(tmp);
53432a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff                cli->sendMsg(ResponseCode::OperationFailed,
53532a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff                        "Failed to set ipv6 privacy extensions", true);
53632a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff                return 0;
53732a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff            }
53832a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff
539c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync            free(tmp);
540c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync            cli->sendMsg(ResponseCode::CommandOkay, "IPv6 privacy extensions changed", false);
541c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync            return 0;
542c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync        } else if (!strcmp(argv[1], "ipv6")) {
543c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync            if (argc != 4) {
544c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync                cli->sendMsg(ResponseCode::CommandSyntaxError,
545c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync                        "Usage: interface ipv6 <interface> <enable|disable>",
546c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync                        false);
547c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync                return 0;
548c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync            }
549c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync
550c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync            char *tmp;
551c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync            asprintf(&tmp, "/proc/sys/net/ipv6/conf/%s/disable_ipv6", argv[2]);
552c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync
553c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync            if (writeFile(tmp, !strncmp(argv[3], "enable", 7) ? "0" : "1", 1) < 0) {
55432a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff                free(tmp);
55532a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff                cli->sendMsg(ResponseCode::OperationFailed,
556c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync                        "Failed to change IPv6 state", true);
55732a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff                return 0;
55832a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff            }
559c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync
56032a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff            free(tmp);
561c1b3870147acd0dd42e2e767df08b4d64d5d2423repo sync            cli->sendMsg(ResponseCode::CommandOkay, "IPv6 state changed", false);
56232a9dc6f484deb3462d11084caca21e3f1f662b2Irfan Sheriff            return 0;
5635c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        } else {
5645c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown interface cmd", false);
5655c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat            return 0;
5665c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat        }
5675c1b8af16dbbc20c89aaca2f93e725e12b16d055San Mehat    }
568d18304287dbabc7835be771400b85d4ae8b63de6San Mehat    return 0;
569d18304287dbabc7835be771400b85d4ae8b63de6San Mehat}
570d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
5714a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
572d5573d34c8fac49e16b20cf144486125bf940086San MehatCommandListener::ListTtysCmd::ListTtysCmd() :
573d5573d34c8fac49e16b20cf144486125bf940086San Mehat                 NetdCommand("list_ttys") {
574d5573d34c8fac49e16b20cf144486125bf940086San Mehat}
575d5573d34c8fac49e16b20cf144486125bf940086San Mehat
576d5573d34c8fac49e16b20cf144486125bf940086San Mehatint CommandListener::ListTtysCmd::runCommand(SocketClient *cli,
577d5573d34c8fac49e16b20cf144486125bf940086San Mehat                                             int argc, char **argv) {
578d5573d34c8fac49e16b20cf144486125bf940086San Mehat    TtyCollection *tlist = sPppCtrl->getTtyList();
579d5573d34c8fac49e16b20cf144486125bf940086San Mehat    TtyCollection::iterator it;
580d5573d34c8fac49e16b20cf144486125bf940086San Mehat
581d5573d34c8fac49e16b20cf144486125bf940086San Mehat    for (it = tlist->begin(); it != tlist->end(); ++it) {
582d5573d34c8fac49e16b20cf144486125bf940086San Mehat        cli->sendMsg(ResponseCode::TtyListResult, *it, false);
583d5573d34c8fac49e16b20cf144486125bf940086San Mehat    }
584d5573d34c8fac49e16b20cf144486125bf940086San Mehat
585d5573d34c8fac49e16b20cf144486125bf940086San Mehat    cli->sendMsg(ResponseCode::CommandOkay, "Ttys listed.", false);
586d5573d34c8fac49e16b20cf144486125bf940086San Mehat    return 0;
587d5573d34c8fac49e16b20cf144486125bf940086San Mehat}
588d5573d34c8fac49e16b20cf144486125bf940086San Mehat
589d18304287dbabc7835be771400b85d4ae8b63de6San MehatCommandListener::IpFwdCmd::IpFwdCmd() :
590d18304287dbabc7835be771400b85d4ae8b63de6San Mehat                 NetdCommand("ipfwd") {
591d18304287dbabc7835be771400b85d4ae8b63de6San Mehat}
592d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
593d18304287dbabc7835be771400b85d4ae8b63de6San Mehatint CommandListener::IpFwdCmd::runCommand(SocketClient *cli,
594d18304287dbabc7835be771400b85d4ae8b63de6San Mehat                                                      int argc, char **argv) {
5959d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    int rc = 0;
5969d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
5979d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    if (argc < 2) {
5989d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
5999d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        return 0;
6009d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    }
6019d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
6029d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    if (!strcmp(argv[1], "status")) {
6039d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        char *tmp = NULL;
6049d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
6059d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        asprintf(&tmp, "Forwarding %s", (sTetherCtrl->getIpFwdEnabled() ? "enabled" : "disabled"));
6069d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        cli->sendMsg(ResponseCode::IpFwdStatusResult, tmp, false);
6079d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        free(tmp);
6089d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        return 0;
6099d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    } else if (!strcmp(argv[1], "enable")) {
6109d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        rc = sTetherCtrl->setIpFwdEnabled(true);
6119d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    } else if (!strcmp(argv[1], "disable")) {
6129d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        rc = sTetherCtrl->setIpFwdEnabled(false);
6139d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    } else {
6149d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown ipfwd cmd", false);
6159d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        return 0;
6169d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    }
6179d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
6189d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    if (!rc) {
6199d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        cli->sendMsg(ResponseCode::CommandOkay, "ipfwd operation succeeded", false);
6209d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    } else {
6219d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        cli->sendMsg(ResponseCode::OperationFailed, "ipfwd operation failed", true);
6229d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    }
623d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
624d18304287dbabc7835be771400b85d4ae8b63de6San Mehat    return 0;
625d18304287dbabc7835be771400b85d4ae8b63de6San Mehat}
626d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
627d18304287dbabc7835be771400b85d4ae8b63de6San MehatCommandListener::TetherCmd::TetherCmd() :
628d18304287dbabc7835be771400b85d4ae8b63de6San Mehat                 NetdCommand("tether") {
629d18304287dbabc7835be771400b85d4ae8b63de6San Mehat}
630d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
631d18304287dbabc7835be771400b85d4ae8b63de6San Mehatint CommandListener::TetherCmd::runCommand(SocketClient *cli,
632d18304287dbabc7835be771400b85d4ae8b63de6San Mehat                                                      int argc, char **argv) {
6339d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    int rc = 0;
6349d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
635d18304287dbabc7835be771400b85d4ae8b63de6San Mehat    if (argc < 2) {
636d18304287dbabc7835be771400b85d4ae8b63de6San Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
637d18304287dbabc7835be771400b85d4ae8b63de6San Mehat        return 0;
638d18304287dbabc7835be771400b85d4ae8b63de6San Mehat    }
639d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
6409d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    if (!strcmp(argv[1], "stop")) {
6419d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        rc = sTetherCtrl->stopTethering();
6429589a4c6e0b6a5ec60c65b248e7deff2805749b2zzy    } else if(!strcmp(argv[1], "start-reverse")) {
6439caaa44f635f86200e2b9b4cd36d1471ce910a6aMatthew Xie        ALOGD("CommandListener::TetherCmd::run, call startReverseTethering, iface:%s", argv[2]);
6449589a4c6e0b6a5ec60c65b248e7deff2805749b2zzy        sTetherCtrl->startReverseTethering(argv[2]);
6459589a4c6e0b6a5ec60c65b248e7deff2805749b2zzy    } else if (!strcmp(argv[1], "stop-reverse")) {
6469caaa44f635f86200e2b9b4cd36d1471ce910a6aMatthew Xie        ALOGD("CommandListener::TetherCmd::run, call stopReverseTethering");
6479589a4c6e0b6a5ec60c65b248e7deff2805749b2zzy        rc = sTetherCtrl->stopReverseTethering();
648d18304287dbabc7835be771400b85d4ae8b63de6San Mehat    } else if (!strcmp(argv[1], "status")) {
6499d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        char *tmp = NULL;
6509d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
6519d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        asprintf(&tmp, "Tethering services %s",
6529d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                 (sTetherCtrl->isTetheringStarted() ? "started" : "stopped"));
6539d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        cli->sendMsg(ResponseCode::TetherStatusResult, tmp, false);
6549d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        free(tmp);
655d18304287dbabc7835be771400b85d4ae8b63de6San Mehat        return 0;
6569d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    } else {
6579d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        /*
6589d10b341a0ba46f108cb96e46691197d778cbc06San Mehat         * These commands take a minimum of 4 arguments
6599d10b341a0ba46f108cb96e46691197d778cbc06San Mehat         */
6609d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        if (argc < 4) {
6619d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
6629d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            return 0;
6639d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        }
6649d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
6659d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        if (!strcmp(argv[1], "start")) {
6663208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt            if (argc % 2 == 1) {
6673208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt                cli->sendMsg(ResponseCode::CommandSyntaxError, "Bad number of arguments", false);
6689d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                return 0;
6699d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            }
6703208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt
6713208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt            int num_addrs = argc - 2;
6723208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt            int arg_index = 2;
6733208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt            int array_index = 0;
6743208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt            in_addr *addrs = (in_addr *)malloc(sizeof(in_addr) * num_addrs);
6753208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt            while (array_index < num_addrs) {
6763208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt                if (!inet_aton(argv[arg_index++], &(addrs[array_index++]))) {
6773208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt                    cli->sendMsg(ResponseCode::CommandParameterError, "Invalid address", false);
6783208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt                    free(addrs);
6793208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt                    return 0;
6803208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt                }
6819d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            }
6823208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt            rc = sTetherCtrl->startTethering(num_addrs, addrs);
6833208ea0b6cce28e7aef8459d548fd86df329e34fRobert Greenwalt            free(addrs);
6849d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        } else if (!strcmp(argv[1], "interface")) {
6859d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            if (!strcmp(argv[2], "add")) {
6869d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                rc = sTetherCtrl->tetherInterface(argv[3]);
6879d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            } else if (!strcmp(argv[2], "remove")) {
6889d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                rc = sTetherCtrl->untetherInterface(argv[3]);
6899d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            } else if (!strcmp(argv[2], "list")) {
6909d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                InterfaceCollection *ilist = sTetherCtrl->getTetheredInterfaceList();
6919d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                InterfaceCollection::iterator it;
6929d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
6939d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                for (it = ilist->begin(); it != ilist->end(); ++it) {
6949d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                    cli->sendMsg(ResponseCode::TetherInterfaceListResult, *it, false);
6959d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                }
6969d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            } else {
6979d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                cli->sendMsg(ResponseCode::CommandParameterError,
6989d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                             "Unknown tether interface operation", false);
6999d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                return 0;
7009d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            }
7019d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        } else if (!strcmp(argv[1], "dns")) {
7029d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            if (!strcmp(argv[2], "set")) {
7039d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                rc = sTetherCtrl->setDnsForwarders(&argv[3], argc - 3);
7049d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            } else if (!strcmp(argv[2], "list")) {
7059d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                NetAddressCollection *dlist = sTetherCtrl->getDnsForwarders();
7069d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                NetAddressCollection::iterator it;
7079d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
7089d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                for (it = dlist->begin(); it != dlist->end(); ++it) {
7099d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                    cli->sendMsg(ResponseCode::TetherDnsFwdTgtListResult, inet_ntoa(*it), false);
7109d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                }
7119d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            } else {
7129d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                cli->sendMsg(ResponseCode::CommandParameterError,
7139d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                             "Unknown tether interface operation", false);
7149d10b341a0ba46f108cb96e46691197d778cbc06San Mehat                return 0;
7159d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            }
7169d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        } else {
7179d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown tether cmd", false);
7189d10b341a0ba46f108cb96e46691197d778cbc06San Mehat            return 0;
7199d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        }
7209d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    }
7219d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
7229d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    if (!rc) {
7239d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        cli->sendMsg(ResponseCode::CommandOkay, "Tether operation succeeded", false);
7249d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    } else {
7259d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        cli->sendMsg(ResponseCode::OperationFailed, "Tether operation failed", true);
726d18304287dbabc7835be771400b85d4ae8b63de6San Mehat    }
727d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
728d18304287dbabc7835be771400b85d4ae8b63de6San Mehat    return 0;
729d18304287dbabc7835be771400b85d4ae8b63de6San Mehat}
730d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
731d18304287dbabc7835be771400b85d4ae8b63de6San MehatCommandListener::NatCmd::NatCmd() :
732d18304287dbabc7835be771400b85d4ae8b63de6San Mehat                 NetdCommand("nat") {
733d18304287dbabc7835be771400b85d4ae8b63de6San Mehat}
734d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
735d18304287dbabc7835be771400b85d4ae8b63de6San Mehatint CommandListener::NatCmd::runCommand(SocketClient *cli,
736d18304287dbabc7835be771400b85d4ae8b63de6San Mehat                                                      int argc, char **argv) {
7379d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    int rc = 0;
7389d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
739fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt    if (argc < 5) {
7409d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
7419d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        return 0;
7429d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    }
7439d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
7449ff78fb7da7158f5bd7c86d89a842691820259cfSan Mehat    if (!strcmp(argv[1], "enable")) {
745fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt        rc = sNatCtrl->enableNat(argc, argv);
746c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        if(!rc) {
747c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall            /* Ignore ifaces for now. */
748c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall            rc = sBandwidthCtrl->setGlobalAlertInForwardChain();
749c6c673496184bed6d62cf92a6fc7ed43fd94acd5JP Abgrall        }
7509ff78fb7da7158f5bd7c86d89a842691820259cfSan Mehat    } else if (!strcmp(argv[1], "disable")) {
751f7bf29c8a37d65e132a4dceb7c5a4200ed5c3d79Robert Greenwalt        /* Ignore ifaces for now. */
752fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt        rc = sBandwidthCtrl->removeGlobalAlertInForwardChain();
753fc97b82e02979f246d56a4bfd60e4aab8686d3f6Robert Greenwalt        rc |= sNatCtrl->disableNat(argc, argv);
7549d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    } else {
7559d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown nat cmd", false);
7569d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        return 0;
7579d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    }
7589d10b341a0ba46f108cb96e46691197d778cbc06San Mehat
7599d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    if (!rc) {
7609d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        cli->sendMsg(ResponseCode::CommandOkay, "Nat operation succeeded", false);
7619d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    } else {
7629d10b341a0ba46f108cb96e46691197d778cbc06San Mehat        cli->sendMsg(ResponseCode::OperationFailed, "Nat operation failed", true);
7639d10b341a0ba46f108cb96e46691197d778cbc06San Mehat    }
764d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
765d18304287dbabc7835be771400b85d4ae8b63de6San Mehat    return 0;
766d18304287dbabc7835be771400b85d4ae8b63de6San Mehat}
767d18304287dbabc7835be771400b85d4ae8b63de6San Mehat
768d5573d34c8fac49e16b20cf144486125bf940086San MehatCommandListener::PppdCmd::PppdCmd() :
769d5573d34c8fac49e16b20cf144486125bf940086San Mehat                 NetdCommand("pppd") {
770d5573d34c8fac49e16b20cf144486125bf940086San Mehat}
771d5573d34c8fac49e16b20cf144486125bf940086San Mehat
772d5573d34c8fac49e16b20cf144486125bf940086San Mehatint CommandListener::PppdCmd::runCommand(SocketClient *cli,
773d5573d34c8fac49e16b20cf144486125bf940086San Mehat                                                      int argc, char **argv) {
774d5573d34c8fac49e16b20cf144486125bf940086San Mehat    int rc = 0;
775d5573d34c8fac49e16b20cf144486125bf940086San Mehat
776d5573d34c8fac49e16b20cf144486125bf940086San Mehat    if (argc < 3) {
777d5573d34c8fac49e16b20cf144486125bf940086San Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
778d5573d34c8fac49e16b20cf144486125bf940086San Mehat        return 0;
779d5573d34c8fac49e16b20cf144486125bf940086San Mehat    }
780d5573d34c8fac49e16b20cf144486125bf940086San Mehat
781d5573d34c8fac49e16b20cf144486125bf940086San Mehat    if (!strcmp(argv[1], "attach")) {
78274d8fdd307275bfdf95dcf61d9e6366e87da24ecRobert Greenwalt        struct in_addr l, r, dns1, dns2;
78374d8fdd307275bfdf95dcf61d9e6366e87da24ecRobert Greenwalt
78474d8fdd307275bfdf95dcf61d9e6366e87da24ecRobert Greenwalt        memset(&dns1, sizeof(struct in_addr), 0);
78574d8fdd307275bfdf95dcf61d9e6366e87da24ecRobert Greenwalt        memset(&dns2, sizeof(struct in_addr), 0);
786d5573d34c8fac49e16b20cf144486125bf940086San Mehat
787d5573d34c8fac49e16b20cf144486125bf940086San Mehat        if (!inet_aton(argv[3], &l)) {
788d5573d34c8fac49e16b20cf144486125bf940086San Mehat            cli->sendMsg(ResponseCode::CommandParameterError, "Invalid local address", false);
789d5573d34c8fac49e16b20cf144486125bf940086San Mehat            return 0;
790d5573d34c8fac49e16b20cf144486125bf940086San Mehat        }
791d5573d34c8fac49e16b20cf144486125bf940086San Mehat        if (!inet_aton(argv[4], &r)) {
792d5573d34c8fac49e16b20cf144486125bf940086San Mehat            cli->sendMsg(ResponseCode::CommandParameterError, "Invalid remote address", false);
793d5573d34c8fac49e16b20cf144486125bf940086San Mehat            return 0;
794d5573d34c8fac49e16b20cf144486125bf940086San Mehat        }
79574d8fdd307275bfdf95dcf61d9e6366e87da24ecRobert Greenwalt        if ((argc > 3) && (!inet_aton(argv[5], &dns1))) {
79674d8fdd307275bfdf95dcf61d9e6366e87da24ecRobert Greenwalt            cli->sendMsg(ResponseCode::CommandParameterError, "Invalid dns1 address", false);
79774d8fdd307275bfdf95dcf61d9e6366e87da24ecRobert Greenwalt            return 0;
79874d8fdd307275bfdf95dcf61d9e6366e87da24ecRobert Greenwalt        }
79974d8fdd307275bfdf95dcf61d9e6366e87da24ecRobert Greenwalt        if ((argc > 4) && (!inet_aton(argv[6], &dns2))) {
80074d8fdd307275bfdf95dcf61d9e6366e87da24ecRobert Greenwalt            cli->sendMsg(ResponseCode::CommandParameterError, "Invalid dns2 address", false);
80174d8fdd307275bfdf95dcf61d9e6366e87da24ecRobert Greenwalt            return 0;
80274d8fdd307275bfdf95dcf61d9e6366e87da24ecRobert Greenwalt        }
80374d8fdd307275bfdf95dcf61d9e6366e87da24ecRobert Greenwalt        rc = sPppCtrl->attachPppd(argv[2], l, r, dns1, dns2);
804d5573d34c8fac49e16b20cf144486125bf940086San Mehat    } else if (!strcmp(argv[1], "detach")) {
805d5573d34c8fac49e16b20cf144486125bf940086San Mehat        rc = sPppCtrl->detachPppd(argv[2]);
806d5573d34c8fac49e16b20cf144486125bf940086San Mehat    } else {
807d5573d34c8fac49e16b20cf144486125bf940086San Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown pppd cmd", false);
808d5573d34c8fac49e16b20cf144486125bf940086San Mehat        return 0;
809d5573d34c8fac49e16b20cf144486125bf940086San Mehat    }
810d5573d34c8fac49e16b20cf144486125bf940086San Mehat
811d5573d34c8fac49e16b20cf144486125bf940086San Mehat    if (!rc) {
812d5573d34c8fac49e16b20cf144486125bf940086San Mehat        cli->sendMsg(ResponseCode::CommandOkay, "Pppd operation succeeded", false);
813d5573d34c8fac49e16b20cf144486125bf940086San Mehat    } else {
814d5573d34c8fac49e16b20cf144486125bf940086San Mehat        cli->sendMsg(ResponseCode::OperationFailed, "Pppd operation failed", true);
815d5573d34c8fac49e16b20cf144486125bf940086San Mehat    }
816d5573d34c8fac49e16b20cf144486125bf940086San Mehat
817d5573d34c8fac49e16b20cf144486125bf940086San Mehat    return 0;
818d5573d34c8fac49e16b20cf144486125bf940086San Mehat}
8191bdac9e98c1ef37ba568b6cf23bfbc09cfe6bf15San Mehat
8205af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry ShmidtCommandListener::SoftapCmd::SoftapCmd() :
8215af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt                 NetdCommand("softap") {
8225af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt}
8235af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt
8245af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidtint CommandListener::SoftapCmd::runCommand(SocketClient *cli,
8255af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt                                        int argc, char **argv) {
82631fd6c5f3e9c39a13b028718dba5921a4300988cDmitry Shmidt    int rc = 0, flag = 0;
827666fe255fa581965d2835bd581cd5a8959411067Dmitry Shmidt    char *retbuf = NULL;
8285af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt
8295af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt    if (argc < 2) {
8305af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt        cli->sendMsg(ResponseCode::CommandSyntaxError, "Softap Missing argument", false);
8315af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt        return 0;
8325af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt    }
8335af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt
8347e9eb7b48345af69283afa7ca58d3be0a329931bIrfan Sheriff    if (!strcmp(argv[1], "startap")) {
835c3539e22bd7a59a12542b9ad961f7d7f0db9a8dfDmitry Shmidt        rc = sSoftapCtrl->startSoftap();
836c3539e22bd7a59a12542b9ad961f7d7f0db9a8dfDmitry Shmidt    } else if (!strcmp(argv[1], "stopap")) {
8375af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt        rc = sSoftapCtrl->stopSoftap();
83831fd6c5f3e9c39a13b028718dba5921a4300988cDmitry Shmidt    } else if (!strcmp(argv[1], "fwreload")) {
83931fd6c5f3e9c39a13b028718dba5921a4300988cDmitry Shmidt        rc = sSoftapCtrl->fwReloadSoftap(argc, argv);
840666fe255fa581965d2835bd581cd5a8959411067Dmitry Shmidt    } else if (!strcmp(argv[1], "clients")) {
841666fe255fa581965d2835bd581cd5a8959411067Dmitry Shmidt        rc = sSoftapCtrl->clientsSoftap(&retbuf);
842666fe255fa581965d2835bd581cd5a8959411067Dmitry Shmidt        if (!rc) {
843666fe255fa581965d2835bd581cd5a8959411067Dmitry Shmidt            cli->sendMsg(ResponseCode::CommandOkay, retbuf, false);
844666fe255fa581965d2835bd581cd5a8959411067Dmitry Shmidt            free(retbuf);
845666fe255fa581965d2835bd581cd5a8959411067Dmitry Shmidt            return 0;
846666fe255fa581965d2835bd581cd5a8959411067Dmitry Shmidt        }
8475af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt    } else if (!strcmp(argv[1], "status")) {
848666fe255fa581965d2835bd581cd5a8959411067Dmitry Shmidt        asprintf(&retbuf, "Softap service %s",
8495af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt                 (sSoftapCtrl->isSoftapStarted() ? "started" : "stopped"));
850666fe255fa581965d2835bd581cd5a8959411067Dmitry Shmidt        cli->sendMsg(ResponseCode::SoftapStatusResult, retbuf, false);
851666fe255fa581965d2835bd581cd5a8959411067Dmitry Shmidt        free(retbuf);
8525af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt        return 0;
8535af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt    } else if (!strcmp(argv[1], "set")) {
8545af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt        rc = sSoftapCtrl->setSoftap(argc, argv);
8555af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt    } else {
8565af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt        cli->sendMsg(ResponseCode::CommandSyntaxError, "Softap Unknown cmd", false);
8575af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt        return 0;
8585af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt    }
8595af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt
8605af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt    if (!rc) {
8615af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt        cli->sendMsg(ResponseCode::CommandOkay, "Softap operation succeeded", false);
8625af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt    } else {
8635af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt        cli->sendMsg(ResponseCode::OperationFailed, "Softap operation failed", true);
8645af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt    }
8655af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt
8665af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt    return 0;
8675af38c360587ca2eef0badf6137ccf018f8cd4aaDmitry Shmidt}
8683e6ba3ce1abda6071d5b01f53ca6039bb7b5e2d3Robert Greenwalt
86989c1e975f0c03997808d35c7be10b244c5309f9fMattias FalkCommandListener::ResolverCmd::ResolverCmd() :
87089c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        NetdCommand("resolver") {
87189c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk}
87289c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk
87389c1e975f0c03997808d35c7be10b244c5309f9fMattias Falkint CommandListener::ResolverCmd::runCommand(SocketClient *cli, int argc, char **argv) {
87489c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    int rc = 0;
87589c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    struct in_addr addr;
87689c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk
87789c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    if (argc < 2) {
87889c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        cli->sendMsg(ResponseCode::CommandSyntaxError, "Resolver missing arguments", false);
87989c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        return 0;
88089c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    }
88189c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk
88289c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    if (!strcmp(argv[1], "setdefaultif")) { // "resolver setdefaultif <iface>"
88389c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        if (argc == 3) {
88489c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            rc = sResolverCtrl->setDefaultInterface(argv[2]);
88589c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        } else {
88689c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            cli->sendMsg(ResponseCode::CommandSyntaxError,
88789c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk                    "Wrong number of arguments to resolver setdefaultif", false);
88889c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            return 0;
88989c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        }
89089c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    } else if (!strcmp(argv[1], "setifdns")) { // "resolver setifdns <iface> <dns1> <dns2> ..."
89189c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        if (argc >= 4) {
89289c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            rc = sResolverCtrl->setInterfaceDnsServers(argv[2], &argv[3], argc - 3);
89389c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        } else {
89489c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            cli->sendMsg(ResponseCode::CommandSyntaxError,
89589c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk                    "Wrong number of arguments to resolver setifdns", false);
89689c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            return 0;
89789c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        }
89889c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk
89989c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        // set the address of the interface to which the name servers
90089c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        // are bound. Required in order to bind to right interface when
90189c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        // doing the dns query.
90289c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        if (!rc) {
90389c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            ifc_init();
90489c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            ifc_get_info(argv[2], &addr.s_addr, NULL, 0);
90589c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk
90689c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            rc = sResolverCtrl->setInterfaceAddress(argv[2], &addr);
90789c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        }
90889c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    } else if (!strcmp(argv[1], "flushdefaultif")) { // "resolver flushdefaultif"
90989c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        if (argc == 2) {
91089c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            rc = sResolverCtrl->flushDefaultDnsCache();
91189c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        } else {
91289c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            cli->sendMsg(ResponseCode::CommandSyntaxError,
91389c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk                    "Wrong number of arguments to resolver flushdefaultif", false);
91489c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            return 0;
91589c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        }
91689c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    } else if (!strcmp(argv[1], "flushif")) { // "resolver flushif <iface>"
91789c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        if (argc == 3) {
91889c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            rc = sResolverCtrl->flushInterfaceDnsCache(argv[2]);
91989c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        } else {
92089c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            cli->sendMsg(ResponseCode::CommandSyntaxError,
92189c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk                    "Wrong number of arguments to resolver setdefaultif", false);
92289c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk            return 0;
92389c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        }
92489c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    } else {
92589c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        cli->sendMsg(ResponseCode::CommandSyntaxError,"Resolver unknown command", false);
92689c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        return 0;
92789c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    }
92889c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk
92989c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    if (!rc) {
93089c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        cli->sendMsg(ResponseCode::CommandOkay, "Resolver command succeeded", false);
93189c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    } else {
93289c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk        cli->sendMsg(ResponseCode::OperationFailed, "Resolver command failed", true);
93389c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    }
93489c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk
93589c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk    return 0;
93689c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk}
93789c1e975f0c03997808d35c7be10b244c5309f9fMattias Falk
9388e46896b4c4be5deaef651a12b17314f63ae9070San Mehatint CommandListener::readInterfaceCounters(const char *iface, unsigned long *rx, unsigned long *tx) {
9398e46896b4c4be5deaef651a12b17314f63ae9070San Mehat    FILE *fp = fopen("/proc/net/dev", "r");
9408e46896b4c4be5deaef651a12b17314f63ae9070San Mehat    if (!fp) {
9415ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Failed to open /proc/net/dev (%s)", strerror(errno));
9428e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        return -1;
9438e46896b4c4be5deaef651a12b17314f63ae9070San Mehat    }
9448e46896b4c4be5deaef651a12b17314f63ae9070San Mehat
9458e46896b4c4be5deaef651a12b17314f63ae9070San Mehat    char buffer[512];
9468e46896b4c4be5deaef651a12b17314f63ae9070San Mehat
9478e46896b4c4be5deaef651a12b17314f63ae9070San Mehat    fgets(buffer, sizeof(buffer), fp); // Header 1
9488e46896b4c4be5deaef651a12b17314f63ae9070San Mehat    fgets(buffer, sizeof(buffer), fp); // Header 2
9498e46896b4c4be5deaef651a12b17314f63ae9070San Mehat    while(fgets(buffer, sizeof(buffer), fp)) {
9508e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        buffer[strlen(buffer)-1] = '\0';
9518e46896b4c4be5deaef651a12b17314f63ae9070San Mehat
952651417c7c8662572de761c3b9260b1241ff21a79Robert Greenwalt        char name[31];
9538e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        unsigned long d;
954651417c7c8662572de761c3b9260b1241ff21a79Robert Greenwalt        sscanf(buffer, "%30s %lu %lu %lu %lu %lu %lu %lu %lu %lu",
9558e46896b4c4be5deaef651a12b17314f63ae9070San Mehat                name, rx, &d, &d, &d, &d, &d, &d, &d, tx);
956651417c7c8662572de761c3b9260b1241ff21a79Robert Greenwalt        char *rxString = strchr(name, ':');
957651417c7c8662572de761c3b9260b1241ff21a79Robert Greenwalt        *rxString = '\0';
958651417c7c8662572de761c3b9260b1241ff21a79Robert Greenwalt        rxString++;
959651417c7c8662572de761c3b9260b1241ff21a79Robert Greenwalt        // when the rx count gets too big it changes from "name: 999" to "name:1000"
960651417c7c8662572de761c3b9260b1241ff21a79Robert Greenwalt        // and the sscanf munge the two together.  Detect that and fix
961651417c7c8662572de761c3b9260b1241ff21a79Robert Greenwalt        // note that all the %lu will be off by one and the real tx value will be in d
962651417c7c8662572de761c3b9260b1241ff21a79Robert Greenwalt        if (*rxString != '\0') {
963651417c7c8662572de761c3b9260b1241ff21a79Robert Greenwalt            *tx = d;
964651417c7c8662572de761c3b9260b1241ff21a79Robert Greenwalt            sscanf(rxString, "%20lu", rx);
965651417c7c8662572de761c3b9260b1241ff21a79Robert Greenwalt        }
9668e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        if (strcmp(name, iface)) {
9678e46896b4c4be5deaef651a12b17314f63ae9070San Mehat            continue;
9688e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        }
9698e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        fclose(fp);
9708e46896b4c4be5deaef651a12b17314f63ae9070San Mehat        return 0;
9718e46896b4c4be5deaef651a12b17314f63ae9070San Mehat    }
9728e46896b4c4be5deaef651a12b17314f63ae9070San Mehat
9738e46896b4c4be5deaef651a12b17314f63ae9070San Mehat    fclose(fp);
97431c61c80a013bd1a050f0f4103e13fba62bf1717Robert Greenwalt    *rx = 0;
97531c61c80a013bd1a050f0f4103e13fba62bf1717Robert Greenwalt    *tx = 0;
97631c61c80a013bd1a050f0f4103e13fba62bf1717Robert Greenwalt    return 0;
9778e46896b4c4be5deaef651a12b17314f63ae9070San Mehat}
9784a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
979