San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 38a73c6 | 2015-01-28 11:47:21 -0800 | [diff] [blame] | 19 | #include <string.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 20 | |
| 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 Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 29 | #include <android-base/logging.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 30 | |
| 31 | #include "NetlinkManager.h" |
| 32 | #include "NetlinkHandler.h" |
| 33 | |
| 34 | NetlinkManager *NetlinkManager::sInstance = NULL; |
| 35 | |
| 36 | NetlinkManager *NetlinkManager::Instance() { |
| 37 | if (!sInstance) |
| 38 | sInstance = new NetlinkManager(); |
| 39 | return sInstance; |
| 40 | } |
| 41 | |
| 42 | NetlinkManager::NetlinkManager() { |
| 43 | mBroadcaster = NULL; |
| 44 | } |
| 45 | |
| 46 | NetlinkManager::~NetlinkManager() { |
| 47 | } |
| 48 | |
| 49 | int NetlinkManager::start() { |
| 50 | struct sockaddr_nl nladdr; |
| 51 | int sz = 64 * 1024; |
Nick Kralevich | c51920c | 2011-04-18 15:51:19 -0700 | [diff] [blame] | 52 | int on = 1; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 53 | |
| 54 | memset(&nladdr, 0, sizeof(nladdr)); |
| 55 | nladdr.nl_family = AF_NETLINK; |
| 56 | nladdr.nl_pid = getpid(); |
| 57 | nladdr.nl_groups = 0xffffffff; |
| 58 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 59 | if ((mSock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, |
| 60 | NETLINK_KOBJECT_UEVENT)) < 0) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 61 | PLOG(ERROR) << "Unable to create uevent socket"; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 62 | return -1; |
| 63 | } |
| 64 | |
Luis Hector Chavez | d1b00de | 2017-08-29 10:29:48 -0700 | [diff] [blame] | 65 | // 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 Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 70 | PLOG(ERROR) << "Unable to set uevent socket SO_RCVBUF/SO_RCVBUFFORCE option"; |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 71 | goto out; |
Nick Kralevich | c51920c | 2011-04-18 15:51:19 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | if (setsockopt(mSock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 75 | PLOG(ERROR) << "Unable to set uevent socket SO_PASSCRED option"; |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 76 | goto out; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | if (bind(mSock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 80 | PLOG(ERROR) << "Unable to bind uevent socket"; |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 81 | goto out; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | mHandler = new NetlinkHandler(mSock); |
| 85 | if (mHandler->start()) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 86 | PLOG(ERROR) << "Unable to start NetlinkHandler"; |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 87 | goto out; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 88 | } |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 89 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 90 | return 0; |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 91 | |
| 92 | out: |
| 93 | close(mSock); |
| 94 | return -1; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | int NetlinkManager::stop() { |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 98 | int status = 0; |
| 99 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 100 | if (mHandler->stop()) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 101 | PLOG(ERROR) << "Unable to stop NetlinkHandler"; |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 102 | status = -1; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 103 | } |
| 104 | delete mHandler; |
| 105 | mHandler = NULL; |
| 106 | |
| 107 | close(mSock); |
| 108 | mSock = -1; |
| 109 | |
Kaspter Ju | 15bd71f | 2012-04-14 10:48:45 +0800 | [diff] [blame] | 110 | return status; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 111 | } |