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