San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 <stdio.h> |
| 18 | #include <errno.h> |
| 19 | |
| 20 | #include <sys/socket.h> |
| 21 | #include <sys/select.h> |
| 22 | #include <sys/time.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/un.h> |
| 25 | |
| 26 | #include <linux/netlink.h> |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 27 | #include <linux/rtnetlink.h> |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 28 | |
| 29 | #define LOG_TAG "Netd" |
| 30 | |
| 31 | #include <cutils/log.h> |
| 32 | |
Jeff Sharkey | fbe497f | 2014-10-28 16:50:07 -0700 | [diff] [blame] | 33 | #include <netlink/attr.h> |
| 34 | #include <netlink/genl/genl.h> |
| 35 | #include <netlink/handlers.h> |
| 36 | #include <netlink/msg.h> |
| 37 | |
| 38 | #include <linux/netfilter/nfnetlink.h> |
| 39 | #include <linux/netfilter/nfnetlink_log.h> |
| 40 | #include <linux/netfilter/nfnetlink_compat.h> |
| 41 | |
| 42 | #include <arpa/inet.h> |
| 43 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 44 | #include "NetlinkManager.h" |
| 45 | #include "NetlinkHandler.h" |
| 46 | |
Jeff Sharkey | fbe497f | 2014-10-28 16:50:07 -0700 | [diff] [blame] | 47 | #include "pcap-netfilter-linux-android.h" |
| 48 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 49 | namespace android { |
| 50 | namespace net { |
| 51 | |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 52 | const int NetlinkManager::NFLOG_QUOTA_GROUP = 1; |
Jeff Sharkey | fbe497f | 2014-10-28 16:50:07 -0700 | [diff] [blame] | 53 | const int NetlinkManager::NETFILTER_STRICT_GROUP = 2; |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 54 | const int NetlinkManager::NFLOG_WAKEUP_GROUP = 3; |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 55 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 56 | NetlinkManager *NetlinkManager::sInstance = NULL; |
| 57 | |
| 58 | NetlinkManager *NetlinkManager::Instance() { |
| 59 | if (!sInstance) |
| 60 | sInstance = new NetlinkManager(); |
| 61 | return sInstance; |
| 62 | } |
| 63 | |
| 64 | NetlinkManager::NetlinkManager() { |
| 65 | mBroadcaster = NULL; |
| 66 | } |
| 67 | |
| 68 | NetlinkManager::~NetlinkManager() { |
| 69 | } |
| 70 | |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 71 | NetlinkHandler *NetlinkManager::setupSocket(int *sock, int netlinkFamily, |
Jeff Sharkey | 9088f10 | 2015-01-23 12:09:49 -0700 | [diff] [blame] | 72 | int groups, int format, bool configNflog) { |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 73 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 74 | struct sockaddr_nl nladdr; |
| 75 | int sz = 64 * 1024; |
Nick Kralevich | 79b579c | 2011-04-18 15:54:13 -0700 | [diff] [blame] | 76 | int on = 1; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 77 | |
| 78 | memset(&nladdr, 0, sizeof(nladdr)); |
| 79 | nladdr.nl_family = AF_NETLINK; |
Joel Scherpelz | 519ffc3 | 2017-06-14 10:27:47 +0900 | [diff] [blame] | 80 | // Kernel will assign a unique nl_pid if set to zero. |
| 81 | nladdr.nl_pid = 0; |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 82 | nladdr.nl_groups = groups; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 83 | |
Nick Kralevich | 53ea9ca | 2015-01-31 13:54:00 -0800 | [diff] [blame] | 84 | if ((*sock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, netlinkFamily)) < 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 85 | ALOGE("Unable to create netlink socket: %s", strerror(errno)); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 86 | return NULL; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Luis Hector Chavez | 3c349ac | 2017-06-29 19:27:43 -0700 | [diff] [blame^] | 89 | // When running in a net/user namespace, SO_RCVBUFFORCE will fail because |
| 90 | // it will check for the CAP_NET_ADMIN capability in the root namespace. |
| 91 | // Try using SO_RCVBUF if that fails. |
| 92 | if (setsockopt(*sock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0 && |
| 93 | setsockopt(*sock, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz)) < 0) { |
| 94 | ALOGE("Unable to set uevent socket SO_RCVBUF option: %s", strerror(errno)); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 95 | close(*sock); |
| 96 | return NULL; |
Nick Kralevich | 79b579c | 2011-04-18 15:54:13 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 99 | if (setsockopt(*sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) { |
Nick Kralevich | 79b579c | 2011-04-18 15:54:13 -0700 | [diff] [blame] | 100 | SLOGE("Unable to set uevent socket SO_PASSCRED option: %s", strerror(errno)); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 101 | close(*sock); |
| 102 | return NULL; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 105 | if (bind(*sock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 106 | ALOGE("Unable to bind netlink socket: %s", strerror(errno)); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 107 | close(*sock); |
| 108 | return NULL; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Jeff Sharkey | 9088f10 | 2015-01-23 12:09:49 -0700 | [diff] [blame] | 111 | if (configNflog) { |
| 112 | if (android_nflog_send_config_cmd(*sock, 0, NFULNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) { |
| 113 | ALOGE("Failed NFULNL_CFG_CMD_PF_UNBIND: %s", strerror(errno)); |
| 114 | return NULL; |
| 115 | } |
| 116 | if (android_nflog_send_config_cmd(*sock, 0, NFULNL_CFG_CMD_PF_BIND, AF_INET) < 0) { |
| 117 | ALOGE("Failed NFULNL_CFG_CMD_PF_BIND: %s", strerror(errno)); |
| 118 | return NULL; |
| 119 | } |
| 120 | if (android_nflog_send_config_cmd(*sock, 0, NFULNL_CFG_CMD_BIND, AF_UNSPEC) < 0) { |
| 121 | ALOGE("Failed NFULNL_CFG_CMD_BIND: %s", strerror(errno)); |
| 122 | return NULL; |
| 123 | } |
| 124 | } |
| 125 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 126 | NetlinkHandler *handler = new NetlinkHandler(this, *sock, format); |
| 127 | if (handler->start()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 128 | ALOGE("Unable to start NetlinkHandler: %s", strerror(errno)); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 129 | close(*sock); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | return handler; |
| 134 | } |
| 135 | |
| 136 | int NetlinkManager::start() { |
| 137 | if ((mUeventHandler = setupSocket(&mUeventSock, NETLINK_KOBJECT_UEVENT, |
Jeff Sharkey | 9088f10 | 2015-01-23 12:09:49 -0700 | [diff] [blame] | 138 | 0xffffffff, NetlinkListener::NETLINK_FORMAT_ASCII, false)) == NULL) { |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 139 | return -1; |
| 140 | } |
| 141 | |
Lorenzo Colitti | 9b3cd76 | 2013-08-02 05:57:47 +0900 | [diff] [blame] | 142 | if ((mRouteHandler = setupSocket(&mRouteSock, NETLINK_ROUTE, |
| 143 | RTMGRP_LINK | |
| 144 | RTMGRP_IPV4_IFADDR | |
Lorenzo Colitti | 12acae8 | 2013-10-24 14:51:57 +0900 | [diff] [blame] | 145 | RTMGRP_IPV6_IFADDR | |
Lorenzo Colitti | bd0f224 | 2014-06-12 13:51:05 +0900 | [diff] [blame] | 146 | RTMGRP_IPV6_ROUTE | |
Lorenzo Colitti | 12acae8 | 2013-10-24 14:51:57 +0900 | [diff] [blame] | 147 | (1 << (RTNLGRP_ND_USEROPT - 1)), |
Jeff Sharkey | 9088f10 | 2015-01-23 12:09:49 -0700 | [diff] [blame] | 148 | NetlinkListener::NETLINK_FORMAT_BINARY, false)) == NULL) { |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 149 | return -1; |
| 150 | } |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 151 | |
| 152 | if ((mQuotaHandler = setupSocket(&mQuotaSock, NETLINK_NFLOG, |
Jeff Sharkey | 9088f10 | 2015-01-23 12:09:49 -0700 | [diff] [blame] | 153 | NFLOG_QUOTA_GROUP, NetlinkListener::NETLINK_FORMAT_BINARY, false)) == NULL) { |
Bryse Flowers | 246ca10 | 2016-06-01 13:00:12 -0700 | [diff] [blame] | 154 | ALOGW("Unable to open qlog quota socket, check if xt_quota2 can send via UeventHandler"); |
JP Abgrall | 8a41209 | 2011-07-26 15:36:40 -0700 | [diff] [blame] | 155 | // TODO: return -1 once the emulator gets a new kernel. |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 156 | } |
Ashish Sharma | 6337b88 | 2012-04-10 19:47:09 -0700 | [diff] [blame] | 157 | |
Jeff Sharkey | fbe497f | 2014-10-28 16:50:07 -0700 | [diff] [blame] | 158 | if ((mStrictHandler = setupSocket(&mStrictSock, NETLINK_NETFILTER, |
Jeff Sharkey | 9088f10 | 2015-01-23 12:09:49 -0700 | [diff] [blame] | 159 | 0, NetlinkListener::NETLINK_FORMAT_BINARY_UNICAST, true)) == NULL) { |
Jeff Sharkey | fbe497f | 2014-10-28 16:50:07 -0700 | [diff] [blame] | 160 | ALOGE("Unable to open strict socket"); |
| 161 | // TODO: return -1 once the emulator gets a new kernel. |
Jeff Sharkey | fbe497f | 2014-10-28 16:50:07 -0700 | [diff] [blame] | 162 | } |
| 163 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | int NetlinkManager::stop() { |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 168 | int status = 0; |
| 169 | |
| 170 | if (mUeventHandler->stop()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 171 | ALOGE("Unable to stop uevent NetlinkHandler: %s", strerror(errno)); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 172 | status = -1; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 173 | } |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 174 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 175 | delete mUeventHandler; |
| 176 | mUeventHandler = NULL; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 177 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 178 | close(mUeventSock); |
| 179 | mUeventSock = -1; |
| 180 | |
| 181 | if (mRouteHandler->stop()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 182 | ALOGE("Unable to stop route NetlinkHandler: %s", strerror(errno)); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 183 | status = -1; |
| 184 | } |
| 185 | |
| 186 | delete mRouteHandler; |
| 187 | mRouteHandler = NULL; |
| 188 | |
| 189 | close(mRouteSock); |
| 190 | mRouteSock = -1; |
| 191 | |
JP Abgrall | 8a41209 | 2011-07-26 15:36:40 -0700 | [diff] [blame] | 192 | if (mQuotaHandler) { |
| 193 | if (mQuotaHandler->stop()) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 194 | ALOGE("Unable to stop quota NetlinkHandler: %s", strerror(errno)); |
JP Abgrall | 8a41209 | 2011-07-26 15:36:40 -0700 | [diff] [blame] | 195 | status = -1; |
| 196 | } |
| 197 | |
| 198 | delete mQuotaHandler; |
| 199 | mQuotaHandler = NULL; |
| 200 | |
| 201 | close(mQuotaSock); |
| 202 | mQuotaSock = -1; |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 203 | } |
Ashish Sharma | 6337b88 | 2012-04-10 19:47:09 -0700 | [diff] [blame] | 204 | |
Jeff Sharkey | fbe497f | 2014-10-28 16:50:07 -0700 | [diff] [blame] | 205 | if (mStrictHandler) { |
| 206 | if (mStrictHandler->stop()) { |
| 207 | ALOGE("Unable to stop strict NetlinkHandler: %s", strerror(errno)); |
| 208 | status = -1; |
| 209 | } |
| 210 | |
| 211 | delete mStrictHandler; |
| 212 | mStrictHandler = NULL; |
| 213 | |
| 214 | close(mStrictSock); |
| 215 | mStrictSock = -1; |
| 216 | } |
| 217 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 218 | return status; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 219 | } |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 220 | |
| 221 | } // namespace net |
| 222 | } // namespace android |