netd: Add enhanced routing support
Add support for policy and metric based routing APIs to netd. This
functionality is used for WQE, IMS, and Android QoS features.
Combined merge commit for following commits:
(cherry picked from Ia22847dd9611ff16eea25336716d4440461565e0)
netd: add metric support for routing
Add support for adding of cost (metric) of a route.
Changed call to ip from logwrapper to popen to handle errors caught during
a route operation.
netd: Add policy routing functionality to netd
Adding a new class RouteController to handle policy routing (source)
functionality in netd.
Installing a handler in CommandListener to handle policy routing
commands.
netd: Validate input argument for Route commands
Check for valid command line argument that is
passed to RouteController module.
CRs-fixed: 569443
RouteController: use fixed priority for ip rule
Use fixed priority when adding source policy rules
CR-fixed: 613222
Conflicts:
server/CommandListener.cpp
server/CommandListener.h
Change-Id: Ic828041d8f5be77a764ce22c45c5091ee8ae6dd2
diff --git a/server/Android.mk b/server/Android.mk
old mode 100644
new mode 100755
index 55aa87c..f3011a4
--- a/server/Android.mk
+++ b/server/Android.mk
@@ -67,6 +67,8 @@
VirtualNetwork.cpp \
main.cpp \
oem_iptables_hook.cpp \
+ QcRouteController.cpp \
+
include $(BUILD_EXECUTABLE)
diff --git a/server/CommandListener.cpp b/server/CommandListener.cpp
index e2d2308..6b17f5e 100644
--- a/server/CommandListener.cpp
+++ b/server/CommandListener.cpp
@@ -1,5 +1,8 @@
/*
* Copyright (C) 2008 The Android Open Source Project
+ * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
+ *
+ * Not a Contribution.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -45,6 +48,7 @@
#include "FirewallController.h"
#include "RouteController.h"
#include "UidRanges.h"
+#include "QcRouteController.h"
#include <string>
#include <vector>
@@ -92,6 +96,7 @@
ResolverController *CommandListener::sResolverCtrl = NULL;
FirewallController *CommandListener::sFirewallCtrl = NULL;
ClatdController *CommandListener::sClatdCtrl = NULL;
+QcRouteController *CommandListener::sQcRouteCtrl = NULL;
/**
* List of module chains to be created, along with explicit ordering. ORDERING
@@ -167,6 +172,48 @@
} while (*(++childChain) != NULL);
}
+/**
+ * Check if string is a valid interface name.
+ * Utilize if_nametoindex, on success it returns ifindex, and on error 0.
+ */
+static bool isValidIface(const char* iface) {
+ return (0 != if_nametoindex(iface));
+}
+
+/**
+ * Check if string is a valid IPv4 or IPv6 address
+ * Utilize inet_pton, on success it returns 1, and on error it returns 0 or -1.
+ * Error -1 means af is not a valid address family, but since v4/v6 is used, this is not applicable.
+ * Error 0 means string is not a valid address, and only this is applicable.
+ */
+static bool isValidIp(const char* str, const char* af) {
+ int ret = -1;
+ unsigned char c;
+ if (!strcmp(af, "v4")) {
+ struct in_addr addr;
+ ret = inet_pton(AF_INET, str, &addr);
+ }
+ else if (!strcmp(af, "v6")) {
+ struct in6_addr addr6;
+ ret = inet_pton(AF_INET6, str, &addr6);
+ }
+ if ( ret == 1 )
+ {
+ /* There is limitation on inet_pton which on success case it allows trailing
+ character of '\0' or space. The '\0' is ensured not to be accepted by the caller,
+ but the spacing should be caught here.
+ */
+ c = *str;
+ while (c)
+ {
+ if ( isspace(c) ) return false;
+ c = *++str;
+ }
+ return true;
+ }
+ return false;
+}
+
CommandListener::CommandListener() :
FrameworkListener("netd", true) {
registerCmd(new InterfaceCmd());
@@ -182,6 +229,7 @@
registerCmd(new FirewallCmd());
registerCmd(new ClatdCmd());
registerCmd(new NetworkCommand());
+ registerCmd(new QcRouteCmd());
if (!sNetCtrl)
sNetCtrl = new NetworkController();
@@ -205,6 +253,8 @@
sInterfaceCtrl = new InterfaceController();
if (!sClatdCtrl)
sClatdCtrl = new ClatdController(sNetCtrl);
+ if (!sQcRouteCtrl)
+ sQcRouteCtrl = new QcRouteController();
/*
* This is the only time we touch top-level chains in iptables; controllers
@@ -1609,4 +1659,313 @@
}
return syntaxError(client, "Unknown argument");
+
+}
+
+CommandListener::QcRouteCmd::QcRouteCmd() :
+ NetdCommand("route") {
+}
+
+int CommandListener::QcRouteCmd::runCommand(SocketClient *cli, int argc, char **argv) {
+ if (argc < 5) {
+ cli->sendMsg(ResponseCode::CommandSyntaxError,
+ "Missing argument", false);
+ return 0;
+ }
+
+ const char *ipVer = NULL;
+ int domain;
+
+ if (!strcmp(argv[3], "v4")) {
+ ipVer = "-4";
+ domain = AF_INET;
+ } else if (!strcmp(argv[3], "v6")) {
+ ipVer = "-6";
+ domain = AF_INET6;
+ } else {
+ cli->sendMsg(ResponseCode::CommandSyntaxError,
+ "Supported family v4|v6",false);
+ return 0;
+ }
+
+ if (!strcmp(argv[2], "src")) {
+ /* source based routing */
+ if (!strcmp(argv[1], "replace")) {
+ if (argc != 7 && argc != 8) {
+ cli->sendMsg(ResponseCode::CommandSyntaxError,
+ "Usage: route replace src inet_family <interface>"
+ " <ipaddr> <routeId> [<gateway>]", false);
+ return 0;
+ }
+
+ char* end;
+ long int rid = strtol(argv[6], &end, 10);
+ if (*end != '\0')
+ {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "RouteID: invalid numerical value", false);
+ return 0;
+ }
+ if ((rid < 1) || (rid > 252)) {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "0 < RouteID < 253", false);
+ return 0;
+ }
+
+ struct in_addr addr;
+ int prefix_length;
+ unsigned flags = 0;
+
+ ifc_init();
+ ifc_get_info(argv[4], &addr.s_addr, &prefix_length, &flags);
+ ifc_close();
+
+ char *iface = argv[4],
+ *srcPrefix = argv[5],
+ *routeId = argv[6],
+ *network = NULL,
+ *gateway = NULL;
+
+ if (false == isValidIface(iface)) {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "invalid interface", false);
+ return 0;
+ }
+
+ if (false == isValidIp(srcPrefix, argv[3]) ) {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "invalid IP address", false);
+ return 0;
+ }
+
+ if (argc > 7) {
+ gateway = argv[7];
+ if (false == isValidIp(gateway, argv[3]) ) {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "invalid gateway", false);
+ return 0;
+ }
+ }
+
+ // compute the network block in CIDR notation (for IPv4 only)
+ if (domain == AF_INET) {
+ struct in_addr net;
+ in_addr_t mask = prefixLengthToIpv4Netmask(prefix_length);
+ net.s_addr = (addr.s_addr & mask);
+
+
+ char net_s[INET_ADDRSTRLEN];
+ inet_ntop(AF_INET, &(net.s_addr), net_s, INET_ADDRSTRLEN);
+ asprintf(&network, "%s/%d", net_s, prefix_length);
+ }
+
+ std::string res = sQcRouteCtrl->repSrcRoute( iface,
+ srcPrefix,
+ gateway,
+ routeId,
+ ipVer);
+ if (!res.empty()) {
+ cli->sendMsg(ResponseCode::OperationFailed, res.c_str(), false);
+ } else {
+ if (network != NULL) {
+ //gateway is null for link local route, metric is 0
+ res = sQcRouteCtrl->addDstRoute(iface,
+ network, NULL, 0, routeId);
+ if (res.empty()) {
+ res = "source route replace & local subnet "
+ "route add succeeded for rid: ";
+ res += routeId;
+ }
+ cli->sendMsg(ResponseCode::CommandOkay, res.c_str(), false);
+ } else {
+ res = "source route replace succeeded for rid:";
+ res += routeId;
+ cli->sendMsg(ResponseCode::CommandOkay, res.c_str(), false);
+ }
+ }
+ free(network);
+ } else if (!strcmp(argv[1], "del")) {
+ if (argc != 5) {
+ cli->sendMsg(ResponseCode::CommandSyntaxError,
+ "Usage: route del src v[4|6] <routeId>", false);
+ return 0;
+ }
+
+ char* end;
+ long int rid = strtol(argv[4], &end, 10);
+ if (*end != '\0')
+ {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "RouteID: invalid numerical value", false);
+ return 0;
+ }
+ if ((rid < 1) || (rid > 252)) {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "RouteID: between 0 and 253", false);
+ return 0;
+ }
+
+ std::string res = sQcRouteCtrl->delSrcRoute(argv[4], ipVer);
+ if (!res.empty()) {
+ cli->sendMsg(ResponseCode::OperationFailed, res.c_str(), false);
+ } else {
+ res = "source route delete succeeded for rid:";
+ res += argv[4];
+ cli->sendMsg(ResponseCode::CommandOkay, res.c_str(), false);
+ }
+ } else {
+ cli->sendMsg(ResponseCode::CommandSyntaxError,
+ "permitted operation for src routes: <replace|del>",
+ false);
+ }
+ } else if (!strcmp(argv[2], "def")) {
+ /* default route configuration */
+ if (!strcmp(argv[1], "replace")) {
+ if ((argc != 5) && (argc != 6)) {
+ cli->sendMsg(ResponseCode::CommandSyntaxError,
+ "Usage: route replace def v[4|6]"
+ " <interface> [<gateway>]", false);
+ return 0;
+ }
+
+ char *iface = argv[4],
+ *gateway = NULL;
+
+ if (false == isValidIface(iface)) {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "invalid interface", false);
+ return 0;
+ }
+
+ if (argc > 5) {
+ gateway = argv[5];
+ if (false == isValidIp(gateway, argv[3]) ) {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "invalid gateway", false);
+ return 0;
+ }
+ }
+
+ std::string res =
+ sQcRouteCtrl->replaceDefRoute(iface, gateway, ipVer);
+ if (!res.empty()) {
+ cli->sendMsg(ResponseCode::OperationFailed, res.c_str(), false);
+ } else {
+ cli->sendMsg(ResponseCode::CommandOkay,
+ "default route replace succeeded", false);
+ }
+ } else if (!strcmp(argv[1], "add")) {
+ if ((argc !=6) && (argc != 7)) {
+ cli->sendMsg(ResponseCode::CommandSyntaxError,
+ "Usage: route add def v[4|6]"
+ " <interface> <metric> [<gateway>]", false);
+ return 0;
+ }
+
+ char *iface = argv[4],
+ *gateway = NULL;
+ int metric = atoi(argv[5]);
+
+ if (false == isValidIface(iface)) {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "invalid interface", false);
+ return 0;
+ }
+
+ if (argc > 6) {
+ gateway = argv[6];
+ if (false == isValidIp(gateway, argv[3]) ) {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "invalid gateway", false);
+ return 0;
+ }
+ }
+
+ std::string res =
+ sQcRouteCtrl->addDefRoute(iface, gateway, ipVer, metric);
+ if (!res.empty()) {
+ cli->sendMsg(ResponseCode::OperationFailed, res.c_str(), false);
+ } else {
+ cli->sendMsg(ResponseCode::CommandOkay,
+ "default route add with metric succeeded", false);
+ }
+ } else {
+ cli->sendMsg(ResponseCode::CommandSyntaxError,
+ "Permitted action for def routes <replace|add>",
+ false);
+ }
+ } else if (!strcmp(argv[2], "dst")) {
+ /* destination based route configuration */
+ if (!strcmp(argv[1], "add")) {
+ if (argc != 7 && argc != 8) {
+ cli->sendMsg(ResponseCode::CommandSyntaxError,
+ "Usage: route add dst v[4|6]"
+ " <interface> <metric> <dstIpAddr> [<gateway>]", false);
+ return 0;
+ }
+
+ char *iface = argv[4],
+ *dstPrefix = argv[6],
+ *gateway = NULL;
+ int metric = atoi(argv[5]);
+
+ if (false == isValidIface(iface)) {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "invalid interface", false);
+ return 0;
+ }
+
+ if (false == isValidIp(dstPrefix, argv[3]) ) {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "invalid IP address", false);
+ return 0;
+ }
+
+ if (argc > 7) {
+ gateway = argv[7];
+ if (false == isValidIp(gateway, argv[3]) ) {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "invalid gateway", false);
+ return 0;
+ }
+ }
+
+ std::string res =
+ sQcRouteCtrl->addDstRoute(iface, dstPrefix, gateway, metric);
+ if (!res.empty()) {
+ cli->sendMsg(ResponseCode::OperationFailed, res.c_str(), false);
+ } else {
+ cli->sendMsg(ResponseCode::CommandOkay,
+ "destination route add succeeded", false);
+ }
+ } else if (!strcmp(argv[1], "del")) {
+ if (argc != 5) {
+ cli->sendMsg(ResponseCode::CommandSyntaxError,
+ "Usage: route del dst v[4|6] <ipaddr>", false);
+ return 0;
+ }
+
+ if (false == isValidIp(argv[4], argv[3]) ) {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "invalid IP address", false);
+ return 0;
+ }
+
+ std::string res = sQcRouteCtrl->delDstRoute(argv[4]);
+ if (!res.empty()){
+ cli->sendMsg(ResponseCode::OperationFailed, res.c_str(), false);
+ } else {
+ cli->sendMsg(ResponseCode::CommandOkay,
+ "destination route delete succeeded", false);
+ }
+ } else {
+ cli->sendMsg(ResponseCode::CommandSyntaxError,
+ "permitted operation for dst routes: <add|del>",
+ false);
+ }
+ } else {
+ cli->sendMsg(ResponseCode::CommandParameterError,
+ "allowed route types: <src|dst|def>", false);
+ }
+ return 0;
}
diff --git a/server/CommandListener.h b/server/CommandListener.h
index f605647..7b60163 100644
--- a/server/CommandListener.h
+++ b/server/CommandListener.h
@@ -1,5 +1,8 @@
/*
* Copyright (C) 2008 The Android Open Source Project
+ * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
+ *
+ * Not a Contribution.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,6 +34,7 @@
#include "ResolverController.h"
#include "FirewallController.h"
#include "ClatdController.h"
+#include "QcRouteController.h"
class CommandListener : public FrameworkListener {
static TetherController *sTetherCtrl;
@@ -43,6 +47,7 @@
static ResolverController *sResolverCtrl;
static FirewallController *sFirewallCtrl;
static ClatdController *sClatdCtrl;
+ static QcRouteController *sQcRouteCtrl;
public:
static NetworkController *sNetCtrl;
@@ -153,6 +158,13 @@
int operationError(SocketClient* cli, const char* message, int ret);
int success(SocketClient* cli);
};
+
+ class QcRouteCmd : public NetdCommand {
+ public:
+ QcRouteCmd();
+ virtual ~QcRouteCmd() {}
+ int runCommand(SocketClient *c, int argc, char ** argv);
+ };
};
#endif
diff --git a/server/QcRouteController.cpp b/server/QcRouteController.cpp
new file mode 100644
index 0000000..0dbb9b0
--- /dev/null
+++ b/server/QcRouteController.cpp
@@ -0,0 +1,303 @@
+/* Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#define LOG_NDEBUG 0
+#define LOG_NDDEBUG 0
+#define LOG_NIDEBUG 0
+
+#include <stdlib.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include <sys/types.h>
+
+#include <cutils/log.h>
+#include "QcRouteController.h"
+
+const char *TAG = "QcRouteController";
+static char IP_PATH[] = "/system/bin/ip";
+const char *QcRouteController::MAIN_TABLE = "254";
+const char *QcRouteController::SOURCE_POLICY_RULE_PRIO = "150";
+const int MAXSIZE = 256;
+
+
+QcRouteController::QcRouteController() {
+}
+
+QcRouteController::~QcRouteController() {
+}
+
+std::string QcRouteController::_runIpCmd(const char * cmd) {
+ FILE *fp = NULL;
+ char line[MAXSIZE];
+ std::string res, buffer;
+
+ if (strlen(cmd) > 255) {
+ return std::string(strerror(E2BIG));
+ }
+
+ buffer = IP_PATH;
+ buffer += " ";
+ buffer += cmd;
+ buffer += " 2>&1"; //capture stderr
+
+ ALOGV(TAG,"%s", buffer.c_str());
+
+ if ((fp = popen(buffer.c_str(),"r")) == NULL) {
+ ALOGE(TAG, "failed to popen: %s", strerror(errno));
+ res = (strerror(errno));
+ } else if (fgets(line, sizeof line, fp)) {
+ ALOGV(TAG, "%s", line);
+ res = cmd;
+ res += ": ";
+ res += line;
+ }
+ pclose(fp);
+
+ return res;
+}
+
+std::string QcRouteController::repSrcRoute
+(
+ const char *iface,
+ const char *srcPrefix,
+ const char *gateway,
+ const char *table,
+ const char *ipver
+)
+{
+ std::string res = _repDefRoute(iface, gateway, table, ipver);
+ if (res.empty()) {
+ _delRule(table, ipver);
+ res = _addRule(srcPrefix, table, ipver);
+ if (res.empty())
+ res = _flushCache();
+ }
+
+ return res;
+}
+
+std::string QcRouteController::delSrcRoute
+(
+ const char *table,
+ const char *ipver
+)
+{
+ //if iface is down then route is probably purged; ignore the error.
+ _delDefRoute(table, ipver);
+ std::string res = _delRule(table, ipver);
+ if (res.empty())
+ res = _flushCache();
+
+ return res;
+}
+
+std::string QcRouteController::addDstRoute
+(
+ const char *iface,
+ const char *dstPrefix,
+ const char *gateway,
+ const int metric,
+ const char *table
+)
+{
+ char buffer[255];
+
+ if (gateway) {
+ snprintf(buffer, sizeof buffer,
+ "route add %s via %s dev %s table %s metric %d",
+ dstPrefix, gateway, iface, table, metric);
+ } else {
+ snprintf(buffer, sizeof buffer,
+ "route add %s dev %s table %s metric %d",
+ dstPrefix, iface, table, metric);
+ }
+
+ //blindly delete an indentical route if it exists.
+ _delHostRoute(dstPrefix, table);
+
+ std::string res = _runIpCmd(buffer);
+ if (res.empty() || (res.find("exists") != std::string::npos))
+ res = _flushCache();
+
+ return res;
+}
+
+std::string QcRouteController::delDstRoute
+(
+ const char *dstPrefix,
+ const char *table
+)
+{
+ std::string res = _delHostRoute(dstPrefix, table);
+ if (res.empty())
+ res = _flushCache();
+
+ return res;
+}
+
+std::string QcRouteController::_delHostRoute
+(
+ const char *dstPrefix,
+ const char *table
+)
+{
+ char buffer[255];
+ snprintf(buffer, sizeof buffer, "route del %s table %s",
+ dstPrefix, table);
+
+ return _runIpCmd(buffer);
+}
+
+std::string QcRouteController::replaceDefRoute
+(
+ const char *iface,
+ const char *gateway,
+ const char *ipver
+)
+{
+ std::string res = _repDefRoute(iface, gateway, MAIN_TABLE, ipver);
+ if (res.empty())
+ res = _flushCache();
+
+ return res;
+}
+
+std::string QcRouteController::_repDefRoute
+(
+ const char *iface,
+ const char *gateway,
+ const char *table,
+ const char *ipver
+)
+{
+ char buffer[255];
+
+ if (gateway) {
+ snprintf(buffer, sizeof buffer,
+ "%s route replace default via %s dev %s scope global table %s",
+ ipver, gateway, iface, table);
+ } else {
+ snprintf(buffer, sizeof buffer,
+ "%s route replace default dev %s table %s",
+ ipver, iface, table);
+ }
+
+ return _runIpCmd(buffer);
+}
+
+std::string QcRouteController::_delDefRoute
+(
+ const char *table,
+ const char *ipver,
+ const char *iface
+)
+{
+ char buffer[255];
+
+ if (iface) {
+ snprintf(buffer, sizeof buffer,
+ "%s route del default dev %s table %s",
+ ipver, iface, table);
+ } else {
+ snprintf(buffer, sizeof buffer,
+ "%s route del default table %s", ipver, table);
+ }
+
+ return _runIpCmd(buffer);
+}
+
+std::string QcRouteController::addDefRoute
+(
+ const char *iface,
+ const char *gateway,
+ const char *ipver,
+ const int metric,
+ const char *table
+)
+{
+ char buffer[255];
+
+ //remove existing def route for an iface before adding one with new metric
+ _delDefRoute(table, ipver, iface);
+
+ if (gateway) {
+ snprintf(buffer, sizeof buffer,
+ "%s route add default via %s dev %s table %s metric %d",
+ ipver, gateway, iface, table, metric);
+ } else {
+ snprintf(buffer, sizeof buffer,
+ "%s route add default dev %s table %s metric %d",
+ ipver, iface, table, metric);
+ }
+
+ std::string res = _runIpCmd(buffer);
+ if (res.empty())
+ res = _flushCache();
+
+ return res;
+}
+
+std::string QcRouteController::_flushCache() {
+ char buffer[255];
+
+ snprintf(buffer, sizeof buffer, "route flush cached");
+
+ return _runIpCmd(buffer);
+}
+
+std::string QcRouteController::_addRule
+(
+ const char *address,
+ const char *table,
+ const char *ipver
+)
+{
+ char buffer[255];
+
+ snprintf(buffer, sizeof buffer,
+ "%s rule add from %s lookup %s prio %s", ipver, address, table,
+ SOURCE_POLICY_RULE_PRIO);
+
+ return _runIpCmd(buffer);
+}
+
+std::string QcRouteController::_delRule
+(
+ const char *table,
+ const char *ipver
+)
+{
+ char buffer[255];
+
+ snprintf(buffer, sizeof buffer,
+ "%s rule del table %s", ipver, table);
+
+ return _runIpCmd(buffer);
+}
diff --git a/server/QcRouteController.h b/server/QcRouteController.h
new file mode 100644
index 0000000..75c7edb
--- /dev/null
+++ b/server/QcRouteController.h
@@ -0,0 +1,121 @@
+/* Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef _QCROUTE_CONTROLLER_H
+#define _QCROUTE_CONTROLLER_H
+
+#include <string.h>
+#include <string>
+
+class QcRouteController {
+public:
+ QcRouteController();
+ virtual ~QcRouteController();
+
+ std::string repSrcRoute
+ (
+ const char *iface,
+ const char *srcPrefix,
+ const char *gateway,
+ const char *table,
+ const char *ipver
+ );
+ std::string delSrcRoute
+ (
+ const char *table,
+ const char *ipver
+ );
+ std::string addDstRoute
+ (
+ const char *iface,
+ const char *dstPrefix,
+ const char *gateway,
+ const int metric,
+ const char *table = MAIN_TABLE
+ );
+ std::string delDstRoute
+ (
+ const char *dstPrefix,
+ const char *table = MAIN_TABLE
+ );
+ std::string replaceDefRoute
+ (
+ const char *iface,
+ const char *gateway,
+ const char *ipver
+ );
+ std::string addDefRoute
+ (
+ const char *iface,
+ const char *gateway,
+ const char *ipver,
+ const int metric,
+ const char *table = MAIN_TABLE
+ );
+
+private:
+ const static char *MAIN_TABLE;
+ const static char *SOURCE_POLICY_RULE_PRIO;
+ std::string _runIpCmd
+ (
+ const char *cmd
+ );
+ std::string _flushCache();
+ std::string _repDefRoute
+ (
+ const char *iface,
+ const char *gateway,
+ const char *table,
+ const char *ipver
+ );
+ std::string _delDefRoute
+ (
+ const char *table,
+ const char *ipver,
+ const char *iface = NULL
+ );
+ std::string _delHostRoute
+ (
+ const char *dstPrefix,
+ const char *table = MAIN_TABLE
+ );
+ std::string _addRule
+ (
+ const char *address,
+ const char *table,
+ const char *ipver
+ );
+ std::string _delRule
+ (
+ const char *table,
+ const char *ipver
+ );
+};
+
+#endif