blob: 7a7627a26d406e825b6e5720e027475772e03d2a [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;
80 nladdr.nl_pid = getpid();
Mike J. Chen564df4e2011-06-23 15:07:35 -070081 nladdr.nl_groups = groups;
San Mehatd1830422010-01-15 08:02:39 -080082
Nick Kralevich53ea9ca2015-01-31 13:54:00 -080083 if ((*sock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, netlinkFamily)) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000084 ALOGE("Unable to create netlink socket: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -070085 return NULL;
San Mehatd1830422010-01-15 08:02:39 -080086 }
87
Mike J. Chen564df4e2011-06-23 15:07:35 -070088 if (setsockopt(*sock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000089 ALOGE("Unable to set uevent socket SO_RCVBUFFORCE option: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -070090 close(*sock);
91 return NULL;
Nick Kralevich79b579c2011-04-18 15:54:13 -070092 }
93
Mike J. Chen564df4e2011-06-23 15:07:35 -070094 if (setsockopt(*sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
Nick Kralevich79b579c2011-04-18 15:54:13 -070095 SLOGE("Unable to set uevent socket SO_PASSCRED option: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -070096 close(*sock);
97 return NULL;
San Mehatd1830422010-01-15 08:02:39 -080098 }
99
Mike J. Chen564df4e2011-06-23 15:07:35 -0700100 if (bind(*sock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000101 ALOGE("Unable to bind netlink socket: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700102 close(*sock);
103 return NULL;
San Mehatd1830422010-01-15 08:02:39 -0800104 }
105
Jeff Sharkey9088f102015-01-23 12:09:49 -0700106 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. Chen564df4e2011-06-23 15:07:35 -0700121 NetlinkHandler *handler = new NetlinkHandler(this, *sock, format);
122 if (handler->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000123 ALOGE("Unable to start NetlinkHandler: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700124 close(*sock);
125 return NULL;
126 }
127
128 return handler;
129}
130
131int NetlinkManager::start() {
132 if ((mUeventHandler = setupSocket(&mUeventSock, NETLINK_KOBJECT_UEVENT,
Jeff Sharkey9088f102015-01-23 12:09:49 -0700133 0xffffffff, NetlinkListener::NETLINK_FORMAT_ASCII, false)) == NULL) {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700134 return -1;
135 }
136
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900137 if ((mRouteHandler = setupSocket(&mRouteSock, NETLINK_ROUTE,
138 RTMGRP_LINK |
139 RTMGRP_IPV4_IFADDR |
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900140 RTMGRP_IPV6_IFADDR |
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900141 RTMGRP_IPV6_ROUTE |
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900142 (1 << (RTNLGRP_ND_USEROPT - 1)),
Jeff Sharkey9088f102015-01-23 12:09:49 -0700143 NetlinkListener::NETLINK_FORMAT_BINARY, false)) == NULL) {
San Mehatd1830422010-01-15 08:02:39 -0800144 return -1;
145 }
JP Abgralle0ebc462011-07-21 17:21:49 -0700146
147 if ((mQuotaHandler = setupSocket(&mQuotaSock, NETLINK_NFLOG,
Jeff Sharkey9088f102015-01-23 12:09:49 -0700148 NFLOG_QUOTA_GROUP, NetlinkListener::NETLINK_FORMAT_BINARY, false)) == NULL) {
Bryse Flowers246ca102016-06-01 13:00:12 -0700149 ALOGW("Unable to open qlog quota socket, check if xt_quota2 can send via UeventHandler");
JP Abgrall8a412092011-07-26 15:36:40 -0700150 // TODO: return -1 once the emulator gets a new kernel.
JP Abgralle0ebc462011-07-21 17:21:49 -0700151 }
Ashish Sharma6337b882012-04-10 19:47:09 -0700152
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700153 if ((mStrictHandler = setupSocket(&mStrictSock, NETLINK_NETFILTER,
Jeff Sharkey9088f102015-01-23 12:09:49 -0700154 0, NetlinkListener::NETLINK_FORMAT_BINARY_UNICAST, true)) == NULL) {
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700155 ALOGE("Unable to open strict socket");
156 // TODO: return -1 once the emulator gets a new kernel.
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700157 }
158
San Mehatd1830422010-01-15 08:02:39 -0800159 return 0;
160}
161
162int NetlinkManager::stop() {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700163 int status = 0;
164
165 if (mUeventHandler->stop()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000166 ALOGE("Unable to stop uevent NetlinkHandler: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700167 status = -1;
San Mehatd1830422010-01-15 08:02:39 -0800168 }
San Mehatd1830422010-01-15 08:02:39 -0800169
Mike J. Chen564df4e2011-06-23 15:07:35 -0700170 delete mUeventHandler;
171 mUeventHandler = NULL;
San Mehatd1830422010-01-15 08:02:39 -0800172
Mike J. Chen564df4e2011-06-23 15:07:35 -0700173 close(mUeventSock);
174 mUeventSock = -1;
175
176 if (mRouteHandler->stop()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000177 ALOGE("Unable to stop route NetlinkHandler: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700178 status = -1;
179 }
180
181 delete mRouteHandler;
182 mRouteHandler = NULL;
183
184 close(mRouteSock);
185 mRouteSock = -1;
186
JP Abgrall8a412092011-07-26 15:36:40 -0700187 if (mQuotaHandler) {
188 if (mQuotaHandler->stop()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000189 ALOGE("Unable to stop quota NetlinkHandler: %s", strerror(errno));
JP Abgrall8a412092011-07-26 15:36:40 -0700190 status = -1;
191 }
192
193 delete mQuotaHandler;
194 mQuotaHandler = NULL;
195
196 close(mQuotaSock);
197 mQuotaSock = -1;
JP Abgralle0ebc462011-07-21 17:21:49 -0700198 }
Ashish Sharma6337b882012-04-10 19:47:09 -0700199
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700200 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. Chen564df4e2011-06-23 15:07:35 -0700213 return status;
San Mehatd1830422010-01-15 08:02:39 -0800214}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900215
216} // namespace net
217} // namespace android