blob: 409cdc8d961832ad7bf4e6228d60ad708e501164 [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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>
Elliott Hughes38a73c62015-01-28 11:47:21 -080019#include <string.h>
San Mehatf1b736b2009-10-10 17:22:08 -070020
21#include <sys/socket.h>
22#include <sys/select.h>
23#include <sys/time.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
27#include <linux/netlink.h>
28
Jeff Sharkey3472e522017-10-06 18:02:53 -060029#include <android-base/logging.h>
San Mehatf1b736b2009-10-10 17:22:08 -070030
31#include "NetlinkManager.h"
32#include "NetlinkHandler.h"
33
34NetlinkManager *NetlinkManager::sInstance = NULL;
35
36NetlinkManager *NetlinkManager::Instance() {
37 if (!sInstance)
38 sInstance = new NetlinkManager();
39 return sInstance;
40}
41
42NetlinkManager::NetlinkManager() {
43 mBroadcaster = NULL;
44}
45
46NetlinkManager::~NetlinkManager() {
47}
48
49int NetlinkManager::start() {
50 struct sockaddr_nl nladdr;
51 int sz = 64 * 1024;
Nick Kralevichc51920c2011-04-18 15:51:19 -070052 int on = 1;
San Mehatf1b736b2009-10-10 17:22:08 -070053
54 memset(&nladdr, 0, sizeof(nladdr));
55 nladdr.nl_family = AF_NETLINK;
56 nladdr.nl_pid = getpid();
57 nladdr.nl_groups = 0xffffffff;
58
Jeff Sharkey36801cc2015-03-13 16:09:20 -070059 if ((mSock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC,
60 NETLINK_KOBJECT_UEVENT)) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060061 PLOG(ERROR) << "Unable to create uevent socket";
San Mehatf1b736b2009-10-10 17:22:08 -070062 return -1;
63 }
64
Luis Hector Chavezd1b00de2017-08-29 10:29:48 -070065 // When running in a net/user namespace, SO_RCVBUFFORCE will fail because
66 // it will check for the CAP_NET_ADMIN capability in the root namespace.
67 // Try using SO_RCVBUF if that fails.
68 if ((setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) &&
69 (setsockopt(mSock, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz)) < 0)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060070 PLOG(ERROR) << "Unable to set uevent socket SO_RCVBUF/SO_RCVBUFFORCE option";
Kaspter Ju15bd71f2012-04-14 10:48:45 +080071 goto out;
Nick Kralevichc51920c2011-04-18 15:51:19 -070072 }
73
74 if (setsockopt(mSock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060075 PLOG(ERROR) << "Unable to set uevent socket SO_PASSCRED option";
Kaspter Ju15bd71f2012-04-14 10:48:45 +080076 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070077 }
78
79 if (bind(mSock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060080 PLOG(ERROR) << "Unable to bind uevent socket";
Kaspter Ju15bd71f2012-04-14 10:48:45 +080081 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070082 }
83
84 mHandler = new NetlinkHandler(mSock);
85 if (mHandler->start()) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060086 PLOG(ERROR) << "Unable to start NetlinkHandler";
Kaspter Ju15bd71f2012-04-14 10:48:45 +080087 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070088 }
Kaspter Ju15bd71f2012-04-14 10:48:45 +080089
San Mehatf1b736b2009-10-10 17:22:08 -070090 return 0;
Kaspter Ju15bd71f2012-04-14 10:48:45 +080091
92out:
93 close(mSock);
94 return -1;
San Mehatf1b736b2009-10-10 17:22:08 -070095}
96
97int NetlinkManager::stop() {
Kaspter Ju15bd71f2012-04-14 10:48:45 +080098 int status = 0;
99
San Mehatf1b736b2009-10-10 17:22:08 -0700100 if (mHandler->stop()) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600101 PLOG(ERROR) << "Unable to stop NetlinkHandler";
Kaspter Ju15bd71f2012-04-14 10:48:45 +0800102 status = -1;
San Mehatf1b736b2009-10-10 17:22:08 -0700103 }
104 delete mHandler;
105 mHandler = NULL;
106
107 close(mSock);
108 mSock = -1;
109
Kaspter Ju15bd71f2012-04-14 10:48:45 +0800110 return status;
San Mehatf1b736b2009-10-10 17:22:08 -0700111}