San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [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> |
| 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. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 27 | #include <linux/rtnetlink.h> |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 28 | |
| 29 | #define LOG_TAG "Netd" |
| 30 | |
| 31 | #include <cutils/log.h> |
| 32 | |
| 33 | #include "NetlinkManager.h" |
| 34 | #include "NetlinkHandler.h" |
| 35 | |
| 36 | NetlinkManager *NetlinkManager::sInstance = NULL; |
| 37 | |
| 38 | NetlinkManager *NetlinkManager::Instance() { |
| 39 | if (!sInstance) |
| 40 | sInstance = new NetlinkManager(); |
| 41 | return sInstance; |
| 42 | } |
| 43 | |
| 44 | NetlinkManager::NetlinkManager() { |
| 45 | mBroadcaster = NULL; |
| 46 | } |
| 47 | |
| 48 | NetlinkManager::~NetlinkManager() { |
| 49 | } |
| 50 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 51 | NetlinkHandler *NetlinkManager::setupSocket(int *sock, int socketType, |
| 52 | int groups, int format) { |
| 53 | |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 54 | struct sockaddr_nl nladdr; |
| 55 | int sz = 64 * 1024; |
Nick Kralevich | 79b579c | 2011-04-18 15:54:13 -0700 | [diff] [blame] | 56 | int on = 1; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 57 | |
| 58 | memset(&nladdr, 0, sizeof(nladdr)); |
| 59 | nladdr.nl_family = AF_NETLINK; |
| 60 | nladdr.nl_pid = getpid(); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 61 | nladdr.nl_groups = groups; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 62 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 63 | if ((*sock = socket(PF_NETLINK, SOCK_DGRAM, socketType)) < 0) { |
| 64 | LOGE("Unable to create netlink socket: %s", strerror(errno)); |
| 65 | return NULL; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 68 | if (setsockopt(*sock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) { |
Nick Kralevich | 79b579c | 2011-04-18 15:54:13 -0700 | [diff] [blame] | 69 | LOGE("Unable to set uevent socket SO_RCVBUFFORCE option: %s", strerror(errno)); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 70 | close(*sock); |
| 71 | return NULL; |
Nick Kralevich | 79b579c | 2011-04-18 15:54:13 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 74 | if (setsockopt(*sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) { |
Nick Kralevich | 79b579c | 2011-04-18 15:54:13 -0700 | [diff] [blame] | 75 | SLOGE("Unable to set uevent socket SO_PASSCRED option: %s", strerror(errno)); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 76 | close(*sock); |
| 77 | return NULL; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 80 | if (bind(*sock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) { |
| 81 | LOGE("Unable to bind netlink socket: %s", strerror(errno)); |
| 82 | close(*sock); |
| 83 | return NULL; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 86 | NetlinkHandler *handler = new NetlinkHandler(this, *sock, format); |
| 87 | if (handler->start()) { |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 88 | LOGE("Unable to start NetlinkHandler: %s", strerror(errno)); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 89 | close(*sock); |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | return handler; |
| 94 | } |
| 95 | |
| 96 | int NetlinkManager::start() { |
| 97 | if ((mUeventHandler = setupSocket(&mUeventSock, NETLINK_KOBJECT_UEVENT, |
| 98 | 0xffffffff, NetlinkListener::NETLINK_FORMAT_ASCII)) == NULL) { |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | if ((mRouteHandler = setupSocket(&mRouteSock, NETLINK_ROUTE, RTMGRP_LINK, |
| 103 | NetlinkListener::NETLINK_FORMAT_BINARY)) == NULL) { |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 104 | return -1; |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | int NetlinkManager::stop() { |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 110 | int status = 0; |
| 111 | |
| 112 | if (mUeventHandler->stop()) { |
| 113 | LOGE("Unable to stop uevent NetlinkHandler: %s", strerror(errno)); |
| 114 | status = -1; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 115 | } |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 116 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 117 | delete mUeventHandler; |
| 118 | mUeventHandler = NULL; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 119 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame^] | 120 | close(mUeventSock); |
| 121 | mUeventSock = -1; |
| 122 | |
| 123 | if (mRouteHandler->stop()) { |
| 124 | LOGE("Unable to stop route NetlinkHandler: %s", strerror(errno)); |
| 125 | status = -1; |
| 126 | } |
| 127 | |
| 128 | delete mRouteHandler; |
| 129 | mRouteHandler = NULL; |
| 130 | |
| 131 | close(mRouteSock); |
| 132 | mRouteSock = -1; |
| 133 | |
| 134 | return status; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 135 | } |