blob: 0a10b0cd233f6608044057d32ffa3719d7bb5c02 [file] [log] [blame]
San Mehatd1830422010-01-15 08:02:39 -08001/*
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. Chen564df4e2011-06-23 15:07:35 -070027#include <linux/rtnetlink.h>
San Mehatd1830422010-01-15 08:02:39 -080028
29#define LOG_TAG "Netd"
30
31#include <cutils/log.h>
32
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -070033#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 Mehatd1830422010-01-15 08:02:39 -080044#include "NetlinkManager.h"
45#include "NetlinkHandler.h"
46
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -070047#include "pcap-netfilter-linux-android.h"
48
Lorenzo Colitti7035f222017-02-13 18:29:00 +090049namespace android {
50namespace net {
51
JP Abgralle0ebc462011-07-21 17:21:49 -070052const int NetlinkManager::NFLOG_QUOTA_GROUP = 1;
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -070053const int NetlinkManager::NETFILTER_STRICT_GROUP = 2;
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090054const int NetlinkManager::NFLOG_WAKEUP_GROUP = 3;
JP Abgralle0ebc462011-07-21 17:21:49 -070055
San Mehatd1830422010-01-15 08:02:39 -080056NetlinkManager *NetlinkManager::sInstance = NULL;
57
58NetlinkManager *NetlinkManager::Instance() {
59 if (!sInstance)
60 sInstance = new NetlinkManager();
61 return sInstance;
62}
63
64NetlinkManager::NetlinkManager() {
65 mBroadcaster = NULL;
66}
67
68NetlinkManager::~NetlinkManager() {
69}
70
JP Abgralle0ebc462011-07-21 17:21:49 -070071NetlinkHandler *NetlinkManager::setupSocket(int *sock, int netlinkFamily,
Jeff Sharkey9088f102015-01-23 12:09:49 -070072 int groups, int format, bool configNflog) {
Mike J. Chen564df4e2011-06-23 15:07:35 -070073
San Mehatd1830422010-01-15 08:02:39 -080074 struct sockaddr_nl nladdr;
75 int sz = 64 * 1024;
Nick Kralevich79b579c2011-04-18 15:54:13 -070076 int on = 1;
San Mehatd1830422010-01-15 08:02:39 -080077
78 memset(&nladdr, 0, sizeof(nladdr));
79 nladdr.nl_family = AF_NETLINK;
Joel Scherpelz519ffc32017-06-14 10:27:47 +090080 // Kernel will assign a unique nl_pid if set to zero.
81 nladdr.nl_pid = 0;
Mike J. Chen564df4e2011-06-23 15:07:35 -070082 nladdr.nl_groups = groups;
San Mehatd1830422010-01-15 08:02:39 -080083
Nick Kralevich53ea9ca2015-01-31 13:54:00 -080084 if ((*sock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, netlinkFamily)) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000085 ALOGE("Unable to create netlink socket: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -070086 return NULL;
San Mehatd1830422010-01-15 08:02:39 -080087 }
88
Luis Hector Chavez3c349ac2017-06-29 19:27:43 -070089 // 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. Chen564df4e2011-06-23 15:07:35 -070095 close(*sock);
96 return NULL;
Nick Kralevich79b579c2011-04-18 15:54:13 -070097 }
98
Mike J. Chen564df4e2011-06-23 15:07:35 -070099 if (setsockopt(*sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
Nick Kralevich79b579c2011-04-18 15:54:13 -0700100 SLOGE("Unable to set uevent socket SO_PASSCRED option: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700101 close(*sock);
102 return NULL;
San Mehatd1830422010-01-15 08:02:39 -0800103 }
104
Mike J. Chen564df4e2011-06-23 15:07:35 -0700105 if (bind(*sock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000106 ALOGE("Unable to bind netlink socket: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700107 close(*sock);
108 return NULL;
San Mehatd1830422010-01-15 08:02:39 -0800109 }
110
Jeff Sharkey9088f102015-01-23 12:09:49 -0700111 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. Chen564df4e2011-06-23 15:07:35 -0700126 NetlinkHandler *handler = new NetlinkHandler(this, *sock, format);
127 if (handler->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000128 ALOGE("Unable to start NetlinkHandler: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700129 close(*sock);
130 return NULL;
131 }
132
133 return handler;
134}
135
136int NetlinkManager::start() {
137 if ((mUeventHandler = setupSocket(&mUeventSock, NETLINK_KOBJECT_UEVENT,
Jeff Sharkey9088f102015-01-23 12:09:49 -0700138 0xffffffff, NetlinkListener::NETLINK_FORMAT_ASCII, false)) == NULL) {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700139 return -1;
140 }
141
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900142 if ((mRouteHandler = setupSocket(&mRouteSock, NETLINK_ROUTE,
143 RTMGRP_LINK |
144 RTMGRP_IPV4_IFADDR |
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900145 RTMGRP_IPV6_IFADDR |
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900146 RTMGRP_IPV6_ROUTE |
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900147 (1 << (RTNLGRP_ND_USEROPT - 1)),
Jeff Sharkey9088f102015-01-23 12:09:49 -0700148 NetlinkListener::NETLINK_FORMAT_BINARY, false)) == NULL) {
San Mehatd1830422010-01-15 08:02:39 -0800149 return -1;
150 }
JP Abgralle0ebc462011-07-21 17:21:49 -0700151
152 if ((mQuotaHandler = setupSocket(&mQuotaSock, NETLINK_NFLOG,
Jeff Sharkey9088f102015-01-23 12:09:49 -0700153 NFLOG_QUOTA_GROUP, NetlinkListener::NETLINK_FORMAT_BINARY, false)) == NULL) {
Bryse Flowers246ca102016-06-01 13:00:12 -0700154 ALOGW("Unable to open qlog quota socket, check if xt_quota2 can send via UeventHandler");
JP Abgrall8a412092011-07-26 15:36:40 -0700155 // TODO: return -1 once the emulator gets a new kernel.
JP Abgralle0ebc462011-07-21 17:21:49 -0700156 }
Ashish Sharma6337b882012-04-10 19:47:09 -0700157
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700158 if ((mStrictHandler = setupSocket(&mStrictSock, NETLINK_NETFILTER,
Jeff Sharkey9088f102015-01-23 12:09:49 -0700159 0, NetlinkListener::NETLINK_FORMAT_BINARY_UNICAST, true)) == NULL) {
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700160 ALOGE("Unable to open strict socket");
161 // TODO: return -1 once the emulator gets a new kernel.
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700162 }
163
San Mehatd1830422010-01-15 08:02:39 -0800164 return 0;
165}
166
167int NetlinkManager::stop() {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700168 int status = 0;
169
170 if (mUeventHandler->stop()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000171 ALOGE("Unable to stop uevent NetlinkHandler: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700172 status = -1;
San Mehatd1830422010-01-15 08:02:39 -0800173 }
San Mehatd1830422010-01-15 08:02:39 -0800174
Mike J. Chen564df4e2011-06-23 15:07:35 -0700175 delete mUeventHandler;
176 mUeventHandler = NULL;
San Mehatd1830422010-01-15 08:02:39 -0800177
Mike J. Chen564df4e2011-06-23 15:07:35 -0700178 close(mUeventSock);
179 mUeventSock = -1;
180
181 if (mRouteHandler->stop()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000182 ALOGE("Unable to stop route NetlinkHandler: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700183 status = -1;
184 }
185
186 delete mRouteHandler;
187 mRouteHandler = NULL;
188
189 close(mRouteSock);
190 mRouteSock = -1;
191
JP Abgrall8a412092011-07-26 15:36:40 -0700192 if (mQuotaHandler) {
193 if (mQuotaHandler->stop()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000194 ALOGE("Unable to stop quota NetlinkHandler: %s", strerror(errno));
JP Abgrall8a412092011-07-26 15:36:40 -0700195 status = -1;
196 }
197
198 delete mQuotaHandler;
199 mQuotaHandler = NULL;
200
201 close(mQuotaSock);
202 mQuotaSock = -1;
JP Abgralle0ebc462011-07-21 17:21:49 -0700203 }
Ashish Sharma6337b882012-04-10 19:47:09 -0700204
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700205 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. Chen564df4e2011-06-23 15:07:35 -0700218 return status;
San Mehatd1830422010-01-15 08:02:39 -0800219}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900220
221} // namespace net
222} // namespace android