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> |
Stan Chesnutt | f6a7ad8 | 2011-01-05 11:47:37 -0800 | [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 | |
Stan Chesnutt | f6a7ad8 | 2011-01-05 11:47:37 -0800 | [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; |
| 56 | |
| 57 | memset(&nladdr, 0, sizeof(nladdr)); |
| 58 | nladdr.nl_family = AF_NETLINK; |
| 59 | nladdr.nl_pid = getpid(); |
Stan Chesnutt | f6a7ad8 | 2011-01-05 11:47:37 -0800 | [diff] [blame^] | 60 | nladdr.nl_groups = groups; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 61 | |
Stan Chesnutt | f6a7ad8 | 2011-01-05 11:47:37 -0800 | [diff] [blame^] | 62 | if ((*sock = socket(PF_NETLINK, SOCK_DGRAM, socketType)) < 0) { |
| 63 | LOGE("Unable to create netlink socket: %s", strerror(errno)); |
| 64 | return NULL; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Stan Chesnutt | f6a7ad8 | 2011-01-05 11:47:37 -0800 | [diff] [blame^] | 67 | if (setsockopt(*sock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) { |
| 68 | LOGE("Unable to set netlink socket options: %s", strerror(errno)); |
| 69 | close(*sock); |
| 70 | return NULL; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Stan Chesnutt | f6a7ad8 | 2011-01-05 11:47:37 -0800 | [diff] [blame^] | 73 | if (bind(*sock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) { |
| 74 | LOGE("Unable to bind netlink socket: %s", strerror(errno)); |
| 75 | close(*sock); |
| 76 | return NULL; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Stan Chesnutt | f6a7ad8 | 2011-01-05 11:47:37 -0800 | [diff] [blame^] | 79 | NetlinkHandler *handler = new NetlinkHandler(this, *sock, format); |
| 80 | if (handler->start()) { |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 81 | LOGE("Unable to start NetlinkHandler: %s", strerror(errno)); |
Stan Chesnutt | f6a7ad8 | 2011-01-05 11:47:37 -0800 | [diff] [blame^] | 82 | close(*sock); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | return handler; |
| 87 | } |
| 88 | |
| 89 | int NetlinkManager::start() { |
| 90 | if ((mUeventHandler = setupSocket(&mUeventSock, NETLINK_KOBJECT_UEVENT, |
| 91 | 0xffffffff, NetlinkListener::NETLINK_FORMAT_ASCII)) == NULL) { |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | if ((mRouteHandler = setupSocket(&mRouteSock, NETLINK_ROUTE, RTMGRP_LINK, |
| 96 | NetlinkListener::NETLINK_FORMAT_BINARY)) == NULL) { |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 97 | return -1; |
| 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | int NetlinkManager::stop() { |
Stan Chesnutt | f6a7ad8 | 2011-01-05 11:47:37 -0800 | [diff] [blame^] | 103 | int status = 0; |
| 104 | |
| 105 | if (mUeventHandler->stop()) { |
| 106 | LOGE("Unable to stop uevent NetlinkHandler: %s", strerror(errno)); |
| 107 | status = -1; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 108 | } |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 109 | |
Stan Chesnutt | f6a7ad8 | 2011-01-05 11:47:37 -0800 | [diff] [blame^] | 110 | delete mUeventHandler; |
| 111 | mUeventHandler = NULL; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 112 | |
Stan Chesnutt | f6a7ad8 | 2011-01-05 11:47:37 -0800 | [diff] [blame^] | 113 | close(mUeventSock); |
| 114 | mUeventSock = -1; |
| 115 | |
| 116 | if (mRouteHandler->stop()) { |
| 117 | LOGE("Unable to stop route NetlinkHandler: %s", strerror(errno)); |
| 118 | status = -1; |
| 119 | } |
| 120 | |
| 121 | delete mRouteHandler; |
| 122 | mRouteHandler = NULL; |
| 123 | |
| 124 | close(mRouteSock); |
| 125 | mRouteSock = -1; |
| 126 | |
| 127 | return status; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 128 | } |