1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "router.h"
18 
19 #include "netlink.h"
20 
21 #include <linux/rtnetlink.h>
22 
23 #include <errno.h>
24 #include <string.h>
25 #include <unistd.h>
26 
Router()27 Router::Router() : mSocketFd(-1) {
28 }
29 
~Router()30 Router::~Router() {
31     if (mSocketFd != -1) {
32         ::close(mSocketFd);
33         mSocketFd = -1;
34     }
35 }
36 
init()37 Result Router::init() {
38     // Create a netlink socket to the router
39     mSocketFd = ::socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
40     if (mSocketFd == -1) {
41         return Result::error(strerror(errno));
42     }
43     return Result::success();
44 }
45 
setDefaultGateway(in_addr_t gateway,unsigned int ifaceIndex)46 Result Router::setDefaultGateway(in_addr_t gateway, unsigned int ifaceIndex) {
47     struct Request {
48         struct nlmsghdr hdr;
49         struct rtmsg msg;
50         char buf[256];
51     } request;
52 
53     memset(&request, 0, sizeof(request));
54 
55     // Set up a request to create a new route
56     request.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(request.msg));
57     request.hdr.nlmsg_type = RTM_NEWROUTE;
58     request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
59 
60     request.msg.rtm_family = AF_INET;
61     request.msg.rtm_dst_len = 0;
62     request.msg.rtm_table = RT_TABLE_MAIN;
63     request.msg.rtm_protocol = RTPROT_BOOT;
64     request.msg.rtm_scope = RT_SCOPE_UNIVERSE;
65     request.msg.rtm_type = RTN_UNICAST;
66 
67     addRouterAttribute(request, RTA_GATEWAY, &gateway, sizeof(gateway));
68     addRouterAttribute(request, RTA_OIF, &ifaceIndex, sizeof(ifaceIndex));
69 
70     return sendNetlinkMessage(&request, request.hdr.nlmsg_len);
71 }
72 
sendNetlinkMessage(const void * data,size_t size)73 Result Router::sendNetlinkMessage(const void* data, size_t size) {
74     struct sockaddr_nl nlAddress;
75     memset(&nlAddress, 0, sizeof(nlAddress));
76     nlAddress.nl_family = AF_NETLINK;
77 
78     int res = ::sendto(mSocketFd, data, size, 0,
79                        reinterpret_cast<sockaddr*>(&nlAddress),
80                        sizeof(nlAddress));
81     if (res == -1) {
82         return Result::error("Unable to send on netlink socket: %s",
83                              strerror(errno));
84     }
85     return Result::success();
86 }
87 
88