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 <linux/rtnetlink.h> 20 21 #include <errno.h> 22 #include <string.h> 23 #include <unistd.h> 24 25 template<class Request> 26 static void addRouterAttribute(Request& r, 27 int type, 28 const void* data, 29 size_t size) { 30 // Calculate the offset into the character buffer where the RTA data lives 31 // We use offsetof on the buffer to get it. This avoids undefined behavior 32 // by casting the buffer (which is safe because it's char) instead of the 33 // Request struct.(which is undefined because of aliasing) 34 size_t offset = NLMSG_ALIGN(r.hdr.nlmsg_len) - offsetof(Request, buf); 35 auto attr = reinterpret_cast<struct rtattr*>(r.buf + offset); 36 attr->rta_type = type; 37 attr->rta_len = RTA_LENGTH(size); 38 memcpy(RTA_DATA(attr), data, size); 39 40 // Update the message length to include the router attribute. 41 r.hdr.nlmsg_len = NLMSG_ALIGN(r.hdr.nlmsg_len) + RTA_ALIGN(attr->rta_len); 42 } 43 44 Router::Router() : mSocketFd(-1) { 45 } 46 47 Router::~Router() { 48 if (mSocketFd != -1) { 49 ::close(mSocketFd); 50 mSocketFd = -1; 51 } 52 } 53 54 Result Router::init() { 55 // Create a netlink socket to the router 56 mSocketFd = ::socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); 57 if (mSocketFd == -1) { 58 return Result::error(strerror(errno)); 59 } 60 return Result::success(); 61 } 62 63 Result Router::setDefaultGateway(in_addr_t gateway, unsigned int ifaceIndex) { 64 struct Request { 65 struct nlmsghdr hdr; 66 struct rtmsg msg; 67 char buf[256]; 68 } request; 69 70 memset(&request, 0, sizeof(request)); 71 72 // Set up a request to create a new route 73 request.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(request.msg)); 74 request.hdr.nlmsg_type = RTM_NEWROUTE; 75 request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE; 76 77 request.msg.rtm_family = AF_INET; 78 request.msg.rtm_dst_len = 0; 79 request.msg.rtm_table = RT_TABLE_MAIN; 80 request.msg.rtm_protocol = RTPROT_BOOT; 81 request.msg.rtm_scope = RT_SCOPE_UNIVERSE; 82 request.msg.rtm_type = RTN_UNICAST; 83 84 addRouterAttribute(request, RTA_GATEWAY, &gateway, sizeof(gateway)); 85 addRouterAttribute(request, RTA_OIF, &ifaceIndex, sizeof(ifaceIndex)); 86 87 return sendNetlinkMessage(&request, request.hdr.nlmsg_len); 88 } 89 90 Result Router::sendNetlinkMessage(const void* data, size_t size) { 91 struct sockaddr_nl nlAddress; 92 memset(&nlAddress, 0, sizeof(nlAddress)); 93 nlAddress.nl_family = AF_NETLINK; 94 95 int res = ::sendto(mSocketFd, data, size, 0, 96 reinterpret_cast<sockaddr*>(&nlAddress), 97 sizeof(nlAddress)); 98 if (res == -1) { 99 return Result::error("Unable to send on netlink socket: %s", 100 strerror(errno)); 101 } 102 return Result::success(); 103 } 104 105