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