NetlinkManager.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #include <errno.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/socket.h>
  20. #include <sys/time.h>
  21. #include <sys/types.h>
  22. #include <sys/un.h>
  23. #include <linux/netlink.h>
  24. #include <linux/rtnetlink.h>
  25. #define LOG_TAG "Netd"
  26. #include <log/log.h>
  27. #include <linux/netfilter/nfnetlink.h>
  28. #include <linux/netfilter/nfnetlink_log.h>
  29. #include <linux/netfilter/nfnetlink_compat.h>
  30. #include <arpa/inet.h>
  31. #include "NetlinkManager.h"
  32. #include "NetlinkHandler.h"
  33. #include "pcap-netfilter-linux-android.h"
  34. namespace android {
  35. namespace net {
  36. const int NetlinkManager::NFLOG_QUOTA_GROUP = 1;
  37. const int NetlinkManager::NETFILTER_STRICT_GROUP = 2;
  38. const int NetlinkManager::NFLOG_WAKEUP_GROUP = 3;
  39. NetlinkManager *NetlinkManager::sInstance = nullptr;
  40. NetlinkManager *NetlinkManager::Instance() {
  41. if (!sInstance)
  42. sInstance = new NetlinkManager();
  43. return sInstance;
  44. }
  45. NetlinkManager::NetlinkManager() {
  46. mBroadcaster = nullptr;
  47. }
  48. NetlinkManager::~NetlinkManager() {
  49. }
  50. NetlinkHandler *NetlinkManager::setupSocket(int *sock, int netlinkFamily,
  51. int groups, int format, bool configNflog) {
  52. struct sockaddr_nl nladdr;
  53. int sz = 64 * 1024;
  54. int on = 1;
  55. memset(&nladdr, 0, sizeof(nladdr));
  56. nladdr.nl_family = AF_NETLINK;
  57. // Kernel will assign a unique nl_pid if set to zero.
  58. nladdr.nl_pid = 0;
  59. nladdr.nl_groups = groups;
  60. if ((*sock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, netlinkFamily)) < 0) {
  61. ALOGE("Unable to create netlink socket for family %d: %s", netlinkFamily, strerror(errno));
  62. return nullptr;
  63. }
  64. // When running in a net/user namespace, SO_RCVBUFFORCE will fail because
  65. // it will check for the CAP_NET_ADMIN capability in the root namespace.
  66. // Try using SO_RCVBUF if that fails.
  67. if (setsockopt(*sock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0 &&
  68. setsockopt(*sock, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz)) < 0) {
  69. ALOGE("Unable to set uevent socket SO_RCVBUF option: %s", strerror(errno));
  70. close(*sock);
  71. return nullptr;
  72. }
  73. if (setsockopt(*sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
  74. SLOGE("Unable to set uevent socket SO_PASSCRED option: %s", strerror(errno));
  75. close(*sock);
  76. return nullptr;
  77. }
  78. if (bind(*sock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) {
  79. ALOGE("Unable to bind netlink socket: %s", strerror(errno));
  80. close(*sock);
  81. return nullptr;
  82. }
  83. if (configNflog) {
  84. if (android_nflog_send_config_cmd(*sock, 0, NFULNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) {
  85. ALOGE("Failed NFULNL_CFG_CMD_PF_UNBIND: %s", strerror(errno));
  86. return nullptr;
  87. }
  88. if (android_nflog_send_config_cmd(*sock, 0, NFULNL_CFG_CMD_PF_BIND, AF_INET) < 0) {
  89. ALOGE("Failed NFULNL_CFG_CMD_PF_BIND: %s", strerror(errno));
  90. return nullptr;
  91. }
  92. if (android_nflog_send_config_cmd(*sock, 0, NFULNL_CFG_CMD_BIND, AF_UNSPEC) < 0) {
  93. ALOGE("Failed NFULNL_CFG_CMD_BIND: %s", strerror(errno));
  94. return nullptr;
  95. }
  96. }
  97. NetlinkHandler *handler = new NetlinkHandler(this, *sock, format);
  98. if (handler->start()) {
  99. ALOGE("Unable to start NetlinkHandler: %s", strerror(errno));
  100. close(*sock);
  101. return nullptr;
  102. }
  103. return handler;
  104. }
  105. int NetlinkManager::start() {
  106. if ((mUeventHandler = setupSocket(&mUeventSock, NETLINK_KOBJECT_UEVENT,
  107. 0xffffffff, NetlinkListener::NETLINK_FORMAT_ASCII, false)) == nullptr) {
  108. return -1;
  109. }
  110. if ((mRouteHandler = setupSocket(&mRouteSock, NETLINK_ROUTE,
  111. RTMGRP_LINK |
  112. RTMGRP_IPV4_IFADDR |
  113. RTMGRP_IPV6_IFADDR |
  114. RTMGRP_IPV6_ROUTE |
  115. (1 << (RTNLGRP_ND_USEROPT - 1)),
  116. NetlinkListener::NETLINK_FORMAT_BINARY, false)) == nullptr) {
  117. return -1;
  118. }
  119. if ((mQuotaHandler = setupSocket(&mQuotaSock, NETLINK_NFLOG,
  120. NFLOG_QUOTA_GROUP, NetlinkListener::NETLINK_FORMAT_BINARY, false)) == nullptr) {
  121. ALOGW("Unable to open qlog quota socket, check if xt_quota2 can send via UeventHandler");
  122. // TODO: return -1 once the emulator gets a new kernel.
  123. }
  124. if ((mStrictHandler = setupSocket(&mStrictSock, NETLINK_NETFILTER,
  125. 0, NetlinkListener::NETLINK_FORMAT_BINARY_UNICAST, true)) == nullptr) {
  126. ALOGE("Unable to open strict socket");
  127. // TODO: return -1 once the emulator gets a new kernel.
  128. }
  129. return 0;
  130. }
  131. int NetlinkManager::stop() {
  132. int status = 0;
  133. if (mUeventHandler->stop()) {
  134. ALOGE("Unable to stop uevent NetlinkHandler: %s", strerror(errno));
  135. status = -1;
  136. }
  137. delete mUeventHandler;
  138. mUeventHandler = nullptr;
  139. close(mUeventSock);
  140. mUeventSock = -1;
  141. if (mRouteHandler->stop()) {
  142. ALOGE("Unable to stop route NetlinkHandler: %s", strerror(errno));
  143. status = -1;
  144. }
  145. delete mRouteHandler;
  146. mRouteHandler = nullptr;
  147. close(mRouteSock);
  148. mRouteSock = -1;
  149. if (mQuotaHandler) {
  150. if (mQuotaHandler->stop()) {
  151. ALOGE("Unable to stop quota NetlinkHandler: %s", strerror(errno));
  152. status = -1;
  153. }
  154. delete mQuotaHandler;
  155. mQuotaHandler = nullptr;
  156. close(mQuotaSock);
  157. mQuotaSock = -1;
  158. }
  159. if (mStrictHandler) {
  160. if (mStrictHandler->stop()) {
  161. ALOGE("Unable to stop strict NetlinkHandler: %s", strerror(errno));
  162. status = -1;
  163. }
  164. delete mStrictHandler;
  165. mStrictHandler = nullptr;
  166. close(mStrictSock);
  167. mStrictSock = -1;
  168. }
  169. return status;
  170. }
  171. } // namespace net
  172. } // namespace android