blob: aacf4b9d73b188586311f29bd7651cac7894f409 [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
San Mehatf1b736b2009-10-10 17:22:08 -070017#include <errno.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070018#include <stdio.h>
Elliott Hughes38a73c62015-01-28 11:47:21 -080019#include <string.h>
San Mehatf1b736b2009-10-10 17:22:08 -070020
San Mehatf1b736b2009-10-10 17:22:08 -070021#include <sys/select.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070022#include <sys/socket.h>
San Mehatf1b736b2009-10-10 17:22:08 -070023#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
San Mehatf1b736b2009-10-10 17:22:08 -070031#include "NetlinkHandler.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070032#include "NetlinkManager.h"
San Mehatf1b736b2009-10-10 17:22:08 -070033
Paul Crowley14c8c072018-09-18 13:30:21 -070034NetlinkManager* NetlinkManager::sInstance = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -070035
Paul Crowley14c8c072018-09-18 13:30:21 -070036NetlinkManager* NetlinkManager::Instance() {
37 if (!sInstance) sInstance = new NetlinkManager();
San Mehatf1b736b2009-10-10 17:22:08 -070038 return sInstance;
39}
40
41NetlinkManager::NetlinkManager() {
42 mBroadcaster = NULL;
43}
44
Paul Crowley14c8c072018-09-18 13:30:21 -070045NetlinkManager::~NetlinkManager() {}
San Mehatf1b736b2009-10-10 17:22:08 -070046
47int NetlinkManager::start() {
48 struct sockaddr_nl nladdr;
49 int sz = 64 * 1024;
Nick Kralevichc51920c2011-04-18 15:51:19 -070050 int on = 1;
San Mehatf1b736b2009-10-10 17:22:08 -070051
52 memset(&nladdr, 0, sizeof(nladdr));
53 nladdr.nl_family = AF_NETLINK;
54 nladdr.nl_pid = getpid();
55 nladdr.nl_groups = 0xffffffff;
56
Paul Crowley14c8c072018-09-18 13:30:21 -070057 if ((mSock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060058 PLOG(ERROR) << "Unable to create uevent socket";
San Mehatf1b736b2009-10-10 17:22:08 -070059 return -1;
60 }
61
Luis Hector Chavezd1b00de2017-08-29 10:29:48 -070062 // When running in a net/user namespace, SO_RCVBUFFORCE will fail because
63 // it will check for the CAP_NET_ADMIN capability in the root namespace.
64 // Try using SO_RCVBUF if that fails.
65 if ((setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) &&
66 (setsockopt(mSock, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz)) < 0)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060067 PLOG(ERROR) << "Unable to set uevent socket SO_RCVBUF/SO_RCVBUFFORCE option";
Kaspter Ju15bd71f2012-04-14 10:48:45 +080068 goto out;
Nick Kralevichc51920c2011-04-18 15:51:19 -070069 }
70
71 if (setsockopt(mSock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060072 PLOG(ERROR) << "Unable to set uevent socket SO_PASSCRED option";
Kaspter Ju15bd71f2012-04-14 10:48:45 +080073 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070074 }
75
Paul Crowley14c8c072018-09-18 13:30:21 -070076 if (bind(mSock, (struct sockaddr*)&nladdr, sizeof(nladdr)) < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060077 PLOG(ERROR) << "Unable to bind uevent socket";
Kaspter Ju15bd71f2012-04-14 10:48:45 +080078 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070079 }
80
81 mHandler = new NetlinkHandler(mSock);
82 if (mHandler->start()) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060083 PLOG(ERROR) << "Unable to start NetlinkHandler";
Kaspter Ju15bd71f2012-04-14 10:48:45 +080084 goto out;
San Mehatf1b736b2009-10-10 17:22:08 -070085 }
Kaspter Ju15bd71f2012-04-14 10:48:45 +080086
San Mehatf1b736b2009-10-10 17:22:08 -070087 return 0;
Kaspter Ju15bd71f2012-04-14 10:48:45 +080088
89out:
90 close(mSock);
91 return -1;
San Mehatf1b736b2009-10-10 17:22:08 -070092}
93
94int NetlinkManager::stop() {
Kaspter Ju15bd71f2012-04-14 10:48:45 +080095 int status = 0;
96
San Mehatf1b736b2009-10-10 17:22:08 -070097 if (mHandler->stop()) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060098 PLOG(ERROR) << "Unable to stop NetlinkHandler";
Kaspter Ju15bd71f2012-04-14 10:48:45 +080099 status = -1;
San Mehatf1b736b2009-10-10 17:22:08 -0700100 }
101 delete mHandler;
102 mHandler = NULL;
103
104 close(mSock);
105 mSock = -1;
106
Kaspter Ju15bd71f2012-04-14 10:48:45 +0800107 return status;
San Mehatf1b736b2009-10-10 17:22:08 -0700108}