henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/firewallsocketserver.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/asyncsocket.h" |
| 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/logging.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | |
| 19 | namespace rtc { |
| 20 | |
| 21 | class FirewallSocket : public AsyncSocketAdapter { |
| 22 | public: |
| 23 | FirewallSocket(FirewallSocketServer* server, AsyncSocket* socket, int type) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 24 | : AsyncSocketAdapter(socket), server_(server), type_(type) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 26 | int Bind(const SocketAddress& addr) override { |
| 27 | if (!server_->IsBindableIp(addr.ipaddr())) { |
| 28 | SetError(EINVAL); |
| 29 | return SOCKET_ERROR; |
| 30 | } |
| 31 | return AsyncSocketAdapter::Bind(addr); |
| 32 | } |
| 33 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 34 | int Connect(const SocketAddress& addr) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 35 | if (type_ == SOCK_STREAM) { |
| 36 | if (!server_->Check(FP_TCP, GetLocalAddress(), addr)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 37 | RTC_LOG(LS_VERBOSE) << "FirewallSocket outbound TCP connection from " |
| 38 | << GetLocalAddress().ToSensitiveString() << " to " |
| 39 | << addr.ToSensitiveString() << " denied"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | // TODO: Handle this asynchronously. |
| 41 | SetError(EHOSTUNREACH); |
| 42 | return SOCKET_ERROR; |
| 43 | } |
| 44 | } |
| 45 | return AsyncSocketAdapter::Connect(addr); |
| 46 | } |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 47 | int Send(const void* pv, size_t cb) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 48 | return SendTo(pv, cb, GetRemoteAddress()); |
| 49 | } |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 50 | int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override { |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 51 | RTC_DCHECK(type_ == SOCK_DGRAM || type_ == SOCK_STREAM); |
| 52 | FirewallProtocol protocol = (type_ == SOCK_DGRAM) ? FP_UDP : FP_TCP; |
| 53 | if (!server_->Check(protocol, GetLocalAddress(), addr)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 54 | RTC_LOG(LS_VERBOSE) << "FirewallSocket outbound packet with type " |
| 55 | << type_ << " from " |
| 56 | << GetLocalAddress().ToSensitiveString() << " to " |
| 57 | << addr.ToSensitiveString() << " dropped"; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 58 | return static_cast<int>(cb); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 59 | } |
| 60 | return AsyncSocketAdapter::SendTo(pv, cb, addr); |
| 61 | } |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 62 | int Recv(void* pv, size_t cb, int64_t* timestamp) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 63 | SocketAddress addr; |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 64 | return RecvFrom(pv, cb, &addr, timestamp); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 65 | } |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 66 | int RecvFrom(void* pv, |
| 67 | size_t cb, |
| 68 | SocketAddress* paddr, |
| 69 | int64_t* timestamp) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | if (type_ == SOCK_DGRAM) { |
| 71 | while (true) { |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 72 | int res = AsyncSocketAdapter::RecvFrom(pv, cb, paddr, timestamp); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 73 | if (res <= 0) |
| 74 | return res; |
| 75 | if (server_->Check(FP_UDP, *paddr, GetLocalAddress())) |
| 76 | return res; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 77 | RTC_LOG(LS_VERBOSE) |
| 78 | << "FirewallSocket inbound UDP packet from " |
| 79 | << paddr->ToSensitiveString() << " to " |
| 80 | << GetLocalAddress().ToSensitiveString() << " dropped"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 83 | return AsyncSocketAdapter::RecvFrom(pv, cb, paddr, timestamp); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 84 | } |
| 85 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 86 | int Listen(int backlog) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 87 | if (!server_->tcp_listen_enabled()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 88 | RTC_LOG(LS_VERBOSE) << "FirewallSocket listen attempt denied"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | return AsyncSocketAdapter::Listen(backlog); |
| 93 | } |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 94 | AsyncSocket* Accept(SocketAddress* paddr) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 95 | SocketAddress addr; |
| 96 | while (AsyncSocket* sock = AsyncSocketAdapter::Accept(&addr)) { |
| 97 | if (server_->Check(FP_TCP, addr, GetLocalAddress())) { |
| 98 | if (paddr) |
| 99 | *paddr = addr; |
| 100 | return sock; |
| 101 | } |
| 102 | sock->Close(); |
| 103 | delete sock; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 104 | RTC_LOG(LS_VERBOSE) << "FirewallSocket inbound TCP connection from " |
| 105 | << addr.ToSensitiveString() << " to " |
| 106 | << GetLocalAddress().ToSensitiveString() << " denied"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | FirewallSocketServer* server_; |
| 113 | int type_; |
| 114 | }; |
| 115 | |
| 116 | FirewallSocketServer::FirewallSocketServer(SocketServer* server, |
| 117 | FirewallManager* manager, |
| 118 | bool should_delete_server) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 119 | : server_(server), |
| 120 | manager_(manager), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 121 | should_delete_server_(should_delete_server), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 122 | udp_sockets_enabled_(true), |
| 123 | tcp_sockets_enabled_(true), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 124 | tcp_listen_enabled_(true) { |
| 125 | if (manager_) |
| 126 | manager_->AddServer(this); |
| 127 | } |
| 128 | |
| 129 | FirewallSocketServer::~FirewallSocketServer() { |
| 130 | if (manager_) |
| 131 | manager_->RemoveServer(this); |
| 132 | |
| 133 | if (server_ && should_delete_server_) { |
| 134 | delete server_; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 135 | server_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 139 | void FirewallSocketServer::AddRule(bool allow, |
| 140 | FirewallProtocol p, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 141 | FirewallDirection d, |
| 142 | const SocketAddress& addr) { |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 143 | SocketAddress any; |
| 144 | if (d == FD_IN || d == FD_ANY) { |
| 145 | AddRule(allow, p, any, addr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 146 | } |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 147 | if (d == FD_OUT || d == FD_ANY) { |
| 148 | AddRule(allow, p, addr, any); |
| 149 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 152 | void FirewallSocketServer::AddRule(bool allow, |
| 153 | FirewallProtocol p, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 154 | const SocketAddress& src, |
| 155 | const SocketAddress& dst) { |
| 156 | Rule r; |
| 157 | r.allow = allow; |
| 158 | r.p = p; |
| 159 | r.src = src; |
| 160 | r.dst = dst; |
| 161 | CritScope scope(&crit_); |
| 162 | rules_.push_back(r); |
| 163 | } |
| 164 | |
| 165 | void FirewallSocketServer::ClearRules() { |
| 166 | CritScope scope(&crit_); |
| 167 | rules_.clear(); |
| 168 | } |
| 169 | |
| 170 | bool FirewallSocketServer::Check(FirewallProtocol p, |
| 171 | const SocketAddress& src, |
| 172 | const SocketAddress& dst) { |
| 173 | CritScope scope(&crit_); |
| 174 | for (size_t i = 0; i < rules_.size(); ++i) { |
| 175 | const Rule& r = rules_[i]; |
| 176 | if ((r.p != p) && (r.p != FP_ANY)) |
| 177 | continue; |
| 178 | if ((r.src.ipaddr() != src.ipaddr()) && !r.src.IsNil()) |
| 179 | continue; |
| 180 | if ((r.src.port() != src.port()) && (r.src.port() != 0)) |
| 181 | continue; |
| 182 | if ((r.dst.ipaddr() != dst.ipaddr()) && !r.dst.IsNil()) |
| 183 | continue; |
| 184 | if ((r.dst.port() != dst.port()) && (r.dst.port() != 0)) |
| 185 | continue; |
| 186 | return r.allow; |
| 187 | } |
| 188 | return true; |
| 189 | } |
| 190 | |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 191 | void FirewallSocketServer::SetUnbindableIps( |
| 192 | const std::vector<rtc::IPAddress>& unbindable_ips) { |
| 193 | unbindable_ips_ = unbindable_ips; |
| 194 | } |
| 195 | |
| 196 | bool FirewallSocketServer::IsBindableIp(const rtc::IPAddress& ip) { |
| 197 | return std::find(unbindable_ips_.begin(), unbindable_ips_.end(), ip) == |
| 198 | unbindable_ips_.end(); |
| 199 | } |
| 200 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 201 | Socket* FirewallSocketServer::CreateSocket(int family, int type) { |
| 202 | return WrapSocket(server_->CreateAsyncSocket(family, type), type); |
| 203 | } |
| 204 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 205 | AsyncSocket* FirewallSocketServer::CreateAsyncSocket(int family, int type) { |
| 206 | return WrapSocket(server_->CreateAsyncSocket(family, type), type); |
| 207 | } |
| 208 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 209 | void FirewallSocketServer::SetMessageQueue(MessageQueue* queue) { |
| 210 | server_->SetMessageQueue(queue); |
| 211 | } |
| 212 | |
| 213 | bool FirewallSocketServer::Wait(int cms, bool process_io) { |
| 214 | return server_->Wait(cms, process_io); |
| 215 | } |
| 216 | |
| 217 | void FirewallSocketServer::WakeUp() { |
| 218 | return server_->WakeUp(); |
| 219 | } |
| 220 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 221 | AsyncSocket* FirewallSocketServer::WrapSocket(AsyncSocket* sock, int type) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 222 | if (!sock || (type == SOCK_STREAM && !tcp_sockets_enabled_) || |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 223 | (type == SOCK_DGRAM && !udp_sockets_enabled_)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 224 | RTC_LOG(LS_VERBOSE) << "FirewallSocketServer socket creation denied"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 225 | delete sock; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 226 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 227 | } |
| 228 | return new FirewallSocket(this, sock, type); |
| 229 | } |
| 230 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 231 | FirewallManager::FirewallManager() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 232 | |
| 233 | FirewallManager::~FirewallManager() { |
kwiberg | 22487b2 | 2016-09-13 01:17:10 -0700 | [diff] [blame] | 234 | RTC_DCHECK(servers_.empty()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void FirewallManager::AddServer(FirewallSocketServer* server) { |
| 238 | CritScope scope(&crit_); |
| 239 | servers_.push_back(server); |
| 240 | } |
| 241 | |
| 242 | void FirewallManager::RemoveServer(FirewallSocketServer* server) { |
| 243 | CritScope scope(&crit_); |
| 244 | servers_.erase(std::remove(servers_.begin(), servers_.end(), server), |
| 245 | servers_.end()); |
| 246 | } |
| 247 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 248 | void FirewallManager::AddRule(bool allow, |
| 249 | FirewallProtocol p, |
| 250 | FirewallDirection d, |
| 251 | const SocketAddress& addr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 252 | CritScope scope(&crit_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 253 | for (std::vector<FirewallSocketServer*>::const_iterator it = servers_.begin(); |
| 254 | it != servers_.end(); ++it) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 255 | (*it)->AddRule(allow, p, d, addr); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void FirewallManager::ClearRules() { |
| 260 | CritScope scope(&crit_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 261 | for (std::vector<FirewallSocketServer*>::const_iterator it = servers_.begin(); |
| 262 | it != servers_.end(); ++it) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 263 | (*it)->ClearRules(); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | } // namespace rtc |