blob: 76af46f64a3ff4e4538c76658e16846cfa27cb76 [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
JP Abgralle0ebc462011-07-21 17:21:49 -070049const int NetlinkManager::NFLOG_QUOTA_GROUP = 1;
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -070050const int NetlinkManager::NETFILTER_STRICT_GROUP = 2;
JP Abgralle0ebc462011-07-21 17:21:49 -070051
San Mehatd1830422010-01-15 08:02:39 -080052NetlinkManager *NetlinkManager::sInstance = NULL;
53
54NetlinkManager *NetlinkManager::Instance() {
55 if (!sInstance)
56 sInstance = new NetlinkManager();
57 return sInstance;
58}
59
60NetlinkManager::NetlinkManager() {
61 mBroadcaster = NULL;
62}
63
64NetlinkManager::~NetlinkManager() {
65}
66
JP Abgralle0ebc462011-07-21 17:21:49 -070067NetlinkHandler *NetlinkManager::setupSocket(int *sock, int netlinkFamily,
Jeff Sharkey9088f102015-01-23 12:09:49 -070068 int groups, int format, bool configNflog) {
Mike J. Chen564df4e2011-06-23 15:07:35 -070069
San Mehatd1830422010-01-15 08:02:39 -080070 struct sockaddr_nl nladdr;
71 int sz = 64 * 1024;
Nick Kralevich79b579c2011-04-18 15:54:13 -070072 int on = 1;
San Mehatd1830422010-01-15 08:02:39 -080073
74 memset(&nladdr, 0, sizeof(nladdr));
75 nladdr.nl_family = AF_NETLINK;
76 nladdr.nl_pid = getpid();
Mike J. Chen564df4e2011-06-23 15:07:35 -070077 nladdr.nl_groups = groups;
San Mehatd1830422010-01-15 08:02:39 -080078
Nick Kralevich53ea9ca2015-01-31 13:54:00 -080079 if ((*sock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, netlinkFamily)) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000080 ALOGE("Unable to create netlink socket: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -070081 return NULL;
San Mehatd1830422010-01-15 08:02:39 -080082 }
83
Mike J. Chen564df4e2011-06-23 15:07:35 -070084 if (setsockopt(*sock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000085 ALOGE("Unable to set uevent socket SO_RCVBUFFORCE option: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -070086 close(*sock);
87 return NULL;
Nick Kralevich79b579c2011-04-18 15:54:13 -070088 }
89
Mike J. Chen564df4e2011-06-23 15:07:35 -070090 if (setsockopt(*sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
Nick Kralevich79b579c2011-04-18 15:54:13 -070091 SLOGE("Unable to set uevent socket SO_PASSCRED option: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -070092 close(*sock);
93 return NULL;
San Mehatd1830422010-01-15 08:02:39 -080094 }
95
Mike J. Chen564df4e2011-06-23 15:07:35 -070096 if (bind(*sock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000097 ALOGE("Unable to bind netlink socket: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -070098 close(*sock);
99 return NULL;
San Mehatd1830422010-01-15 08:02:39 -0800100 }
101
Jeff Sharkey9088f102015-01-23 12:09:49 -0700102 if (configNflog) {
103 if (android_nflog_send_config_cmd(*sock, 0, NFULNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) {
104 ALOGE("Failed NFULNL_CFG_CMD_PF_UNBIND: %s", strerror(errno));
105 return NULL;
106 }
107 if (android_nflog_send_config_cmd(*sock, 0, NFULNL_CFG_CMD_PF_BIND, AF_INET) < 0) {
108 ALOGE("Failed NFULNL_CFG_CMD_PF_BIND: %s", strerror(errno));
109 return NULL;
110 }
111 if (android_nflog_send_config_cmd(*sock, 0, NFULNL_CFG_CMD_BIND, AF_UNSPEC) < 0) {
112 ALOGE("Failed NFULNL_CFG_CMD_BIND: %s", strerror(errno));
113 return NULL;
114 }
115 }
116
Mike J. Chen564df4e2011-06-23 15:07:35 -0700117 NetlinkHandler *handler = new NetlinkHandler(this, *sock, format);
118 if (handler->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000119 ALOGE("Unable to start NetlinkHandler: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700120 close(*sock);
121 return NULL;
122 }
123
124 return handler;
125}
126
127int NetlinkManager::start() {
128 if ((mUeventHandler = setupSocket(&mUeventSock, NETLINK_KOBJECT_UEVENT,
Jeff Sharkey9088f102015-01-23 12:09:49 -0700129 0xffffffff, NetlinkListener::NETLINK_FORMAT_ASCII, false)) == NULL) {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700130 return -1;
131 }
132
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900133 if ((mRouteHandler = setupSocket(&mRouteSock, NETLINK_ROUTE,
134 RTMGRP_LINK |
135 RTMGRP_IPV4_IFADDR |
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900136 RTMGRP_IPV6_IFADDR |
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900137 RTMGRP_IPV6_ROUTE |
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900138 (1 << (RTNLGRP_ND_USEROPT - 1)),
Jeff Sharkey9088f102015-01-23 12:09:49 -0700139 NetlinkListener::NETLINK_FORMAT_BINARY, false)) == NULL) {
San Mehatd1830422010-01-15 08:02:39 -0800140 return -1;
141 }
JP Abgralle0ebc462011-07-21 17:21:49 -0700142
143 if ((mQuotaHandler = setupSocket(&mQuotaSock, NETLINK_NFLOG,
Jeff Sharkey9088f102015-01-23 12:09:49 -0700144 NFLOG_QUOTA_GROUP, NetlinkListener::NETLINK_FORMAT_BINARY, false)) == NULL) {
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700145 ALOGE("Unable to open quota socket");
JP Abgrall8a412092011-07-26 15:36:40 -0700146 // TODO: return -1 once the emulator gets a new kernel.
JP Abgralle0ebc462011-07-21 17:21:49 -0700147 }
Ashish Sharma6337b882012-04-10 19:47:09 -0700148
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700149 if ((mStrictHandler = setupSocket(&mStrictSock, NETLINK_NETFILTER,
Jeff Sharkey9088f102015-01-23 12:09:49 -0700150 0, NetlinkListener::NETLINK_FORMAT_BINARY_UNICAST, true)) == NULL) {
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700151 ALOGE("Unable to open strict socket");
152 // TODO: return -1 once the emulator gets a new kernel.
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700153 }
154
San Mehatd1830422010-01-15 08:02:39 -0800155 return 0;
156}
157
158int NetlinkManager::stop() {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700159 int status = 0;
160
161 if (mUeventHandler->stop()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000162 ALOGE("Unable to stop uevent NetlinkHandler: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700163 status = -1;
San Mehatd1830422010-01-15 08:02:39 -0800164 }
San Mehatd1830422010-01-15 08:02:39 -0800165
Mike J. Chen564df4e2011-06-23 15:07:35 -0700166 delete mUeventHandler;
167 mUeventHandler = NULL;
San Mehatd1830422010-01-15 08:02:39 -0800168
Mike J. Chen564df4e2011-06-23 15:07:35 -0700169 close(mUeventSock);
170 mUeventSock = -1;
171
172 if (mRouteHandler->stop()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000173 ALOGE("Unable to stop route NetlinkHandler: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700174 status = -1;
175 }
176
177 delete mRouteHandler;
178 mRouteHandler = NULL;
179
180 close(mRouteSock);
181 mRouteSock = -1;
182
JP Abgrall8a412092011-07-26 15:36:40 -0700183 if (mQuotaHandler) {
184 if (mQuotaHandler->stop()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000185 ALOGE("Unable to stop quota NetlinkHandler: %s", strerror(errno));
JP Abgrall8a412092011-07-26 15:36:40 -0700186 status = -1;
187 }
188
189 delete mQuotaHandler;
190 mQuotaHandler = NULL;
191
192 close(mQuotaSock);
193 mQuotaSock = -1;
JP Abgralle0ebc462011-07-21 17:21:49 -0700194 }
Ashish Sharma6337b882012-04-10 19:47:09 -0700195
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700196 if (mStrictHandler) {
197 if (mStrictHandler->stop()) {
198 ALOGE("Unable to stop strict NetlinkHandler: %s", strerror(errno));
199 status = -1;
200 }
201
202 delete mStrictHandler;
203 mStrictHandler = NULL;
204
205 close(mStrictSock);
206 mStrictSock = -1;
207 }
208
Mike J. Chen564df4e2011-06-23 15:07:35 -0700209 return status;
San Mehatd1830422010-01-15 08:02:39 -0800210}