blob: 79b00ee3e41040dc2b62a3e1d5b4e87680dcadf5 [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,
Mike J. Chen564df4e2011-06-23 15:07:35 -070068 int groups, int format) {
69
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
JP Abgralle0ebc462011-07-21 17:21:49 -070079 if ((*sock = socket(PF_NETLINK, SOCK_DGRAM, 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
Mike J. Chen564df4e2011-06-23 15:07:35 -0700102 NetlinkHandler *handler = new NetlinkHandler(this, *sock, format);
103 if (handler->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000104 ALOGE("Unable to start NetlinkHandler: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700105 close(*sock);
106 return NULL;
107 }
108
109 return handler;
110}
111
112int NetlinkManager::start() {
113 if ((mUeventHandler = setupSocket(&mUeventSock, NETLINK_KOBJECT_UEVENT,
114 0xffffffff, NetlinkListener::NETLINK_FORMAT_ASCII)) == NULL) {
115 return -1;
116 }
117
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900118 if ((mRouteHandler = setupSocket(&mRouteSock, NETLINK_ROUTE,
119 RTMGRP_LINK |
120 RTMGRP_IPV4_IFADDR |
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900121 RTMGRP_IPV6_IFADDR |
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900122 RTMGRP_IPV6_ROUTE |
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900123 (1 << (RTNLGRP_ND_USEROPT - 1)),
Mike J. Chen564df4e2011-06-23 15:07:35 -0700124 NetlinkListener::NETLINK_FORMAT_BINARY)) == NULL) {
San Mehatd1830422010-01-15 08:02:39 -0800125 return -1;
126 }
JP Abgralle0ebc462011-07-21 17:21:49 -0700127
128 if ((mQuotaHandler = setupSocket(&mQuotaSock, NETLINK_NFLOG,
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700129 NFLOG_QUOTA_GROUP, NetlinkListener::NETLINK_FORMAT_BINARY)) == NULL) {
130 ALOGE("Unable to open quota socket");
JP Abgrall8a412092011-07-26 15:36:40 -0700131 // TODO: return -1 once the emulator gets a new kernel.
JP Abgralle0ebc462011-07-21 17:21:49 -0700132 }
Ashish Sharma6337b882012-04-10 19:47:09 -0700133
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700134 if ((mStrictHandler = setupSocket(&mStrictSock, NETLINK_NETFILTER,
135 0, NetlinkListener::NETLINK_FORMAT_BINARY_UNICAST)) == NULL) {
136 ALOGE("Unable to open strict socket");
137 // TODO: return -1 once the emulator gets a new kernel.
138 } else {
139 if (android_nflog_send_config_cmd(mStrictSock, 0, NFULNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) {
140 ALOGE("Failed NFULNL_CFG_CMD_PF_UNBIND: %s", strerror(errno));
141 }
142 if (android_nflog_send_config_cmd(mStrictSock, 0, NFULNL_CFG_CMD_PF_BIND, AF_INET) < 0) {
143 ALOGE("Failed NFULNL_CFG_CMD_PF_BIND: %s", strerror(errno));
144 }
145 if (android_nflog_send_config_cmd(mStrictSock, 0, NFULNL_CFG_CMD_BIND, AF_UNSPEC) < 0) {
146 ALOGE("Failed NFULNL_CFG_CMD_BIND: %s", strerror(errno));
147 }
148 }
149
San Mehatd1830422010-01-15 08:02:39 -0800150 return 0;
151}
152
153int NetlinkManager::stop() {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700154 int status = 0;
155
156 if (mUeventHandler->stop()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000157 ALOGE("Unable to stop uevent NetlinkHandler: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700158 status = -1;
San Mehatd1830422010-01-15 08:02:39 -0800159 }
San Mehatd1830422010-01-15 08:02:39 -0800160
Mike J. Chen564df4e2011-06-23 15:07:35 -0700161 delete mUeventHandler;
162 mUeventHandler = NULL;
San Mehatd1830422010-01-15 08:02:39 -0800163
Mike J. Chen564df4e2011-06-23 15:07:35 -0700164 close(mUeventSock);
165 mUeventSock = -1;
166
167 if (mRouteHandler->stop()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000168 ALOGE("Unable to stop route NetlinkHandler: %s", strerror(errno));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700169 status = -1;
170 }
171
172 delete mRouteHandler;
173 mRouteHandler = NULL;
174
175 close(mRouteSock);
176 mRouteSock = -1;
177
JP Abgrall8a412092011-07-26 15:36:40 -0700178 if (mQuotaHandler) {
179 if (mQuotaHandler->stop()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000180 ALOGE("Unable to stop quota NetlinkHandler: %s", strerror(errno));
JP Abgrall8a412092011-07-26 15:36:40 -0700181 status = -1;
182 }
183
184 delete mQuotaHandler;
185 mQuotaHandler = NULL;
186
187 close(mQuotaSock);
188 mQuotaSock = -1;
JP Abgralle0ebc462011-07-21 17:21:49 -0700189 }
Ashish Sharma6337b882012-04-10 19:47:09 -0700190
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700191 if (mStrictHandler) {
192 if (mStrictHandler->stop()) {
193 ALOGE("Unable to stop strict NetlinkHandler: %s", strerror(errno));
194 status = -1;
195 }
196
197 delete mStrictHandler;
198 mStrictHandler = NULL;
199
200 close(mStrictSock);
201 mStrictSock = -1;
202 }
203
Mike J. Chen564df4e2011-06-23 15:07:35 -0700204 return status;
San Mehatd1830422010-01-15 08:02:39 -0800205}