| 1 |
|
| 2 |
#include "protoDetour.h" |
| 3 |
#include "protoSocket.h" |
| 4 |
#include "protoRouteMgr.h" // for ifAddr/ifIndex mapping |
| 5 |
|
| 6 |
#include <sys/types.h> |
| 7 |
#include <sys/socket.h> |
| 8 |
#include <netinet/in.h> |
| 9 |
#include <string.h> |
| 10 |
#include <arpa/inet.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <string.h> |
| 13 |
#include <unistd.h> |
| 14 |
#include <fcntl.h> // for fcntl(), etc |
| 15 |
|
| 16 |
class BsdDetour : public ProtoDetour |
| 17 |
{ |
| 18 |
public: |
| 19 |
BsdDetour(); |
| 20 |
~BsdDetour(); |
| 21 |
|
| 22 |
bool Open(int hookFlags = 0, |
| 23 |
const ProtoAddress& srcFilterAddr = PROTO_ADDR_NONE, |
| 24 |
unsigned int srcFilterMask = 0, |
| 25 |
const ProtoAddress& dstFilterAddr = PROTO_ADDR_NONE, |
| 26 |
unsigned int dstFilterMask = 0, |
| 27 |
int dscpValue = -1); |
| 28 |
void Close(); |
| 29 |
bool Recv(char* buffer, |
| 30 |
unsigned int& numBytes, |
| 31 |
Direction* direction = NULL, |
| 32 |
ProtoAddress* srcMac = NULL, |
| 33 |
unsigned int* ifIndex = NULL); |
| 34 |
|
| 35 |
bool Allow(const char* buffer, unsigned int numBytes); |
| 36 |
bool Drop(); |
| 37 |
bool Inject(const char* buffer, unsigned int numBytes); |
| 38 |
|
| 39 |
virtual bool SetMulticastInterface(const char* interfaceName); |
| 40 |
|
| 41 |
enum {DIVERT_PORT = 2998}; |
| 42 |
|
| 43 |
private: |
| 44 |
enum Action |
| 45 |
{ |
| 46 |
INSTALL, |
| 47 |
DELETE |
| 48 |
}; |
| 49 |
bool SetIPFirewall(Action action, |
| 50 |
int hookFlags , |
| 51 |
const ProtoAddress& srcFilterAddr, |
| 52 |
unsigned int srcFilterMask, |
| 53 |
const ProtoAddress& dstFilterAddr, |
| 54 |
unsigned int dstFilterMask); |
| 55 |
|
| 56 |
ProtoAddress pkt_addr; // iface IP addr for inbound, unspecified for outbound |
| 57 |
|
| 58 |
int domain; // AF_INET or AF_INET6 |
| 59 |
int hook_flags; |
| 60 |
ProtoAddress src_filter_addr; |
| 61 |
unsigned int src_filter_mask; |
| 62 |
ProtoAddress dst_filter_addr; |
| 63 |
unsigned int dst_filter_mask; |
| 64 |
|
| 65 |
unsigned short rule_number[3]; |
| 66 |
unsigned int rule_count; |
| 67 |
bool inject_only; |
| 68 |
|
| 69 |
|
| 70 |
class AddressListItem : public ProtoTree::Item |
| 71 |
{ |
| 72 |
public: |
| 73 |
AddressListItem(const ProtoAddress& addr, unsigned int ifIndex); |
| 74 |
|
| 75 |
unsigned int GetInterfaceIndex() const |
| 76 |
{return if_index;} |
| 77 |
|
| 78 |
// ProtoTree::Item overrides |
| 79 |
const char* GetKey() const |
| 80 |
{return if_addr.GetRawHostAddress();} |
| 81 |
unsigned int GetKeysize() const |
| 82 |
{return (if_addr.GetLength() << 3);} |
| 83 |
|
| 84 |
private: |
| 85 |
ProtoAddress if_addr; |
| 86 |
unsigned int if_index; |
| 87 |
}; |
| 88 |
|
| 89 |
ProtoTree if_addr_list; // for if_addr -> ifIndex lookup |
| 90 |
|
| 91 |
}; // end class BsdDetour |
| 92 |
|
| 93 |
ProtoDetour* ProtoDetour::Create() |
| 94 |
{ |
| 95 |
return static_cast<ProtoDetour*>(new BsdDetour()); |
| 96 |
} // end ProtoDetour::Create(), (void*)nl_head |
| 97 |
|
| 98 |
BsdDetour::AddressListItem::AddressListItem(const ProtoAddress& ifAddr, unsigned int ifIndex) |
| 99 |
: if_addr(ifAddr), if_index(ifIndex) |
| 100 |
{ |
| 101 |
} |
| 102 |
|
| 103 |
BsdDetour::BsdDetour() |
| 104 |
: hook_flags(0), rule_count(0), inject_only(false) |
| 105 |
{ |
| 106 |
memset(&pkt_addr, 0, sizeof(struct sockaddr_storage)); |
| 107 |
} |
| 108 |
|
| 109 |
BsdDetour::~BsdDetour() |
| 110 |
{ |
| 111 |
Close(); |
| 112 |
} |
| 113 |
|
| 114 |
bool BsdDetour::SetIPFirewall(Action action, |
| 115 |
int hookFlags , |
| 116 |
const ProtoAddress& srcFilterAddr, |
| 117 |
unsigned int srcFilterMask, |
| 118 |
const ProtoAddress& dstFilterAddr, |
| 119 |
unsigned int dstFilterMask) |
| 120 |
{ |
| 121 |
// 1) IPv4 or IPv6 address family? |
| 122 |
const char* cmd; |
| 123 |
if (srcFilterAddr.GetType() != dstFilterAddr.GetType()) |
| 124 |
{ |
| 125 |
PLOG(PL_ERROR, "BsdDetour::SetIPFirewall() error: inconsistent src/dst filter addr families\n"); |
| 126 |
return false; |
| 127 |
} |
| 128 |
else if (ProtoAddress::IPv4 == srcFilterAddr.GetType()) |
| 129 |
{ |
| 130 |
cmd = "/sbin/ipfw"; // IPv4 ipfw |
| 131 |
} |
| 132 |
else if (ProtoAddress::IPv6 == srcFilterAddr.GetType()) |
| 133 |
{ |
| 134 |
cmd = "/sbin/ipfw"; // IPv6 ipfw (ipfw2 does both, in theory) |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
PLOG(PL_ERROR, "BsdDetour::SetIPFirewall() error: unspecified filter addr family\n"); |
| 139 |
return false; |
| 140 |
} |
| 141 |
// 2) For (DELETE == action) re-interpret hookFlags value as rule number |
| 142 |
if (DELETE == action) hookFlags++; |
| 143 |
// 3) For which firewall hooks? |
| 144 |
while (0 != hookFlags) |
| 145 |
{ |
| 146 |
// Make and install "iptables" firewall rules |
| 147 |
const size_t RULE_MAX = 511; |
| 148 |
char rule[RULE_MAX+1]; |
| 149 |
|
| 150 |
if (INSTALL == action) |
| 151 |
{ |
| 152 |
if (rule_count > 2) |
| 153 |
{ |
| 154 |
PLOG(PL_ERROR, "BsdDetour::SetIPFirewall() max ruleCount exceeded!\n"); |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
const char* target; |
| 159 |
if (0 != (hookFlags & OUTPUT)) |
| 160 |
{ |
| 161 |
target = "out"; |
| 162 |
hookFlags &= ~OUTPUT; |
| 163 |
} |
| 164 |
else if (0 != (hookFlags & INPUT)) |
| 165 |
{ |
| 166 |
target = "in"; |
| 167 |
hookFlags &= ~INPUT; |
| 168 |
} |
| 169 |
else if (0 != (hookFlags & FORWARD)) |
| 170 |
{ |
| 171 |
target = "via any"; |
| 172 |
hookFlags &= ~FORWARD; |
| 173 |
} |
| 174 |
else |
| 175 |
{ |
| 176 |
break; // all flags have been processed |
| 177 |
} |
| 178 |
|
| 179 |
// cmd = "ipfw" or "ip6fw" |
| 180 |
const char* f = (ProtoAddress::IPv4 == srcFilterAddr.GetType()) ? |
| 181 |
"ip" : "ipv6"; |
| 182 |
sprintf(rule, "%s add divert %hu %s ", cmd, (UINT16)DIVERT_PORT, f); |
| 183 |
if (0 != srcFilterMask) |
| 184 |
{ |
| 185 |
strcat(rule, "from "); |
| 186 |
size_t len = strlen(rule); |
| 187 |
if (!srcFilterAddr.GetHostString(rule+len, RULE_MAX - len)) |
| 188 |
{ |
| 189 |
PLOG(PL_ERROR, "BsdDetour::SetIPFirewall() error: bad source addr filter\n"); |
| 190 |
return false; |
| 191 |
} |
| 192 |
len = strlen(rule); |
| 193 |
sprintf(rule+len, "/%u ", srcFilterMask); |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
size_t len = strlen(rule); |
| 198 |
sprintf(rule+len, "from any "); |
| 199 |
} |
| 200 |
if (0 != dstFilterMask) |
| 201 |
{ |
| 202 |
strcat(rule, "to "); |
| 203 |
size_t len = strlen(rule); |
| 204 |
if (!dstFilterAddr.GetHostString(rule+len, RULE_MAX - len)) |
| 205 |
{ |
| 206 |
PLOG(PL_ERROR, "BsdDetour::SetIPFirewall() error: bad destination addr filter\n"); |
| 207 |
return false; |
| 208 |
} |
| 209 |
len = strlen(rule); |
| 210 |
sprintf(rule+len, "/%u ", dstFilterMask); |
| 211 |
} |
| 212 |
else |
| 213 |
{ |
| 214 |
size_t len = strlen(rule); |
| 215 |
sprintf(rule+len, "to any "); |
| 216 |
} |
| 217 |
|
| 218 |
// target = "in", "out", or "via any" |
| 219 |
strcat(rule, target); |
| 220 |
} |
| 221 |
else // (DELETE == action) |
| 222 |
{ |
| 223 |
sprintf(rule, "%s delete %d\n", cmd, hookFlags - 1); |
| 224 |
hookFlags = 0; |
| 225 |
} |
| 226 |
|
| 227 |
// Add redirection so we can get stderr result |
| 228 |
strcat(rule, " 2>&1"); |
| 229 |
|
| 230 |
FILE* p = popen(rule, "r"); |
| 231 |
if (NULL != p) |
| 232 |
{ |
| 233 |
char feedback[256]; |
| 234 |
if (0 == fread(feedback, 1, 256, p)) |
| 235 |
{ |
| 236 |
PLOG(PL_WARN, "BsdDetour::SetIPFirewall() error: fread(%s): %s\n", |
| 237 |
rule, GetErrorString()); |
| 238 |
} |
| 239 |
char* ptr = strchr(feedback, '\n'); |
| 240 |
if (NULL != ptr) *ptr = '\0'; |
| 241 |
feedback[255] = '\0'; |
| 242 |
if (0 == pclose(p)) |
| 243 |
{ |
| 244 |
if (INSTALL == action) |
| 245 |
{ |
| 246 |
// Add firewall rule number to list for delete on close |
| 247 |
if (1 != sscanf(feedback, "%05hu\n", rule_number+rule_count)) |
| 248 |
{ |
| 249 |
PLOG(PL_ERROR, "BsdDetour::SetIPFirewall() warning: couldn't record firewall rule number\n"); |
| 250 |
return true; |
| 251 |
} |
| 252 |
rule_count++; |
| 253 |
} |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
PLOG(PL_ERROR, "BsdDetour::SetIPFirewall() \"%s\" error: %s\n", |
| 258 |
rule, feedback); |
| 259 |
return false; |
| 260 |
} |
| 261 |
} |
| 262 |
else |
| 263 |
{ |
| 264 |
PLOG(PL_ERROR, "BsdDetour::SetIPFirewall() error: popen(%s): %s\n", |
| 265 |
rule, GetErrorString()); |
| 266 |
return false; |
| 267 |
} |
| 268 |
} // end while (0 != hookFlags) |
| 269 |
return true; |
| 270 |
} // end BsdDetour::SetIPFirewall() |
| 271 |
|
| 272 |
bool BsdDetour::Open(int hookFlags, |
| 273 |
const ProtoAddress& srcFilterAddr, |
| 274 |
unsigned int srcFilterMask, |
| 275 |
const ProtoAddress& dstFilterAddr, |
| 276 |
unsigned int dstFilterMask, |
| 277 |
int /*dscpValue*/) // TBD - support DSCP |
| 278 |
{ |
| 279 |
if (IsOpen()) Close(); |
| 280 |
|
| 281 |
// Check for "inject-only" mode and "rewire appropriately |
| 282 |
inject_only = false; |
| 283 |
if (0 != (hookFlags & INJECT)) |
| 284 |
{ |
| 285 |
// 0) Open raw socket for optional packet injection use |
| 286 |
//if (0 > (raw_fd = socket(domain, SOCK_RAW, IPPROTO_RAW))) |
| 287 |
if (0 > (descriptor = socket(PF_INET, SOCK_RAW, IPPROTO_RAW))) // or can we IPv6 on an AF_INET/HDRINCL socket? |
| 288 |
{ |
| 289 |
PLOG(PL_ERROR, "BsdDetour::Open() socket(IPPROTO_RAW) error: %s\n", |
| 290 |
GetErrorString()); |
| 291 |
return false; |
| 292 |
} |
| 293 |
domain = AF_INET; |
| 294 |
if (AF_INET == domain) |
| 295 |
{ |
| 296 |
// Note: no IP_HDRINCL for IPv6 raw sockets ? |
| 297 |
int enable = 1; |
| 298 |
if (setsockopt(descriptor, IPPROTO_IP, IP_HDRINCL, &enable, sizeof(enable))) |
| 299 |
{ |
| 300 |
PLOG(PL_ERROR, "BsdDetour::Open() setsockopt(IP_HDRINCL) error: %s\n", |
| 301 |
GetErrorString()); |
| 302 |
Close(); |
| 303 |
return false; |
| 304 |
} |
| 305 |
} |
| 306 |
ProtoDetour::Open(); |
| 307 |
StopInputNotification(); |
| 308 |
|
| 309 |
// Set to non-blocking for our purposes (TBD) Add a SetBlocking() method |
| 310 |
if(-1 == fcntl(descriptor, F_SETFL, fcntl(descriptor, F_GETFL, 0) | O_NONBLOCK)) |
| 311 |
{ |
| 312 |
PLOG(PL_ERROR, "BsdDetour::Open() fcntl(F_SETFL(O_NONBLOCK)) error: %s\n", GetErrorString()); |
| 313 |
Close(); |
| 314 |
return false; |
| 315 |
} |
| 316 |
inject_only = true; |
| 317 |
return true; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
if (srcFilterAddr.GetType() != dstFilterAddr.GetType()) |
| 322 |
{ |
| 323 |
PLOG(PL_ERROR, "BsdDetour::Open() error: inconsistent src/dst filter addr families\n"); |
| 324 |
return false; |
| 325 |
} |
| 326 |
else if (ProtoAddress::IPv4 == srcFilterAddr.GetType()) |
| 327 |
|
| 328 |
{ |
| 329 |
domain = AF_INET; |
| 330 |
} |
| 331 |
else if (ProtoAddress::IPv6 == srcFilterAddr.GetType()) |
| 332 |
{ |
| 333 |
domain = AF_INET6; |
| 334 |
} |
| 335 |
else |
| 336 |
{ |
| 337 |
PLOG(PL_ERROR, "BsdDetour::Open() error: unspecified filter addr family\n"); |
| 338 |
return false; |
| 339 |
} |
| 340 |
|
| 341 |
// Setup ipfw rule(s) ... |
| 342 |
// Save parameters for firewall rule removal |
| 343 |
hook_flags = hookFlags; |
| 344 |
src_filter_addr = srcFilterAddr; |
| 345 |
src_filter_mask = srcFilterMask; |
| 346 |
dst_filter_addr = dstFilterAddr; |
| 347 |
dst_filter_mask = dstFilterMask; |
| 348 |
if (0 != hookFlags) |
| 349 |
{ |
| 350 |
if (!SetIPFirewall(INSTALL, hookFlags, |
| 351 |
srcFilterAddr, srcFilterMask, |
| 352 |
dstFilterAddr, dstFilterMask)) |
| 353 |
{ |
| 354 |
PLOG(PL_ERROR, "BsdDetour::Open() error: couldn't install firewall rules\n"); |
| 355 |
Close(); |
| 356 |
return false; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// Open a divert socket ... |
| 361 |
if ((descriptor = socket(domain, SOCK_RAW, IPPROTO_DIVERT)) < 0) |
| 362 |
{ |
| 363 |
PLOG(PL_ERROR, "BsdDetour::Open() socket() error: %s\n", GetErrorString()); |
| 364 |
Close(); |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
// Bind to our ipfw divert "port" |
| 369 |
UINT16 ipfwPort = DIVERT_PORT; // (TBD) manage port numbers dynamically? |
| 370 |
struct sockaddr_storage socketAddr; |
| 371 |
socklen_t addrSize; |
| 372 |
if (AF_INET == domain) |
| 373 |
{ |
| 374 |
addrSize = sizeof(struct sockaddr_in); |
| 375 |
memset((char*)&socketAddr, 0, sizeof(struct sockaddr_in)); |
| 376 |
((struct sockaddr_in*)&socketAddr)->sin_family = AF_INET; |
| 377 |
((struct sockaddr_in*)&socketAddr)->sin_port = htons(ipfwPort); |
| 378 |
} |
| 379 |
else // if (AF_INET6 == domain) |
| 380 |
{ |
| 381 |
addrSize = sizeof(struct sockaddr_in6); |
| 382 |
memset((char*)&socketAddr, 0, sizeof(struct sockaddr_in6)); |
| 383 |
((struct sockaddr_in6*)&socketAddr)->sin6_family = AF_INET6; |
| 384 |
((struct sockaddr_in6*)&socketAddr)->sin6_port = htons(ipfwPort); |
| 385 |
((struct sockaddr_in6*)&socketAddr)->sin6_flowinfo = 0; |
| 386 |
} |
| 387 |
if (bind(descriptor, (struct sockaddr*)&socketAddr, addrSize) < 0) |
| 388 |
{ |
| 389 |
PLOG(PL_ERROR, "BsdDetour::Open() bind() error: %s\n", GetErrorString()); |
| 390 |
Close(); |
| 391 |
return false; |
| 392 |
} |
| 393 |
|
| 394 |
if (!ProtoDetour::Open()) |
| 395 |
{ |
| 396 |
PLOG(PL_ERROR, "BsdDetour::Open() ProtoChannel::Open() error\n"); |
| 397 |
Close(); |
| 398 |
return false; |
| 399 |
} |
| 400 |
|
| 401 |
// Set up ProtoAddressList for lookup of ifAddr -> ifIndex |
| 402 |
ProtoRouteMgr* rtMgr = ProtoRouteMgr::Create(); |
| 403 |
if (NULL == rtMgr) |
| 404 |
{ |
| 405 |
PLOG(PL_ERROR, "BsdDetour::Open(): ProtoRouteMgr::Create() error: %s\n", GetErrorString()); |
| 406 |
Close(); |
| 407 |
return false; |
| 408 |
} |
| 409 |
if (!rtMgr->Open()) |
| 410 |
{ |
| 411 |
PLOG(PL_ERROR, "BsdDetour::Open(): error: unable to open ProtoRouteMgr\n"); |
| 412 |
delete rtMgr; |
| 413 |
Close(); |
| 414 |
return false; |
| 415 |
} |
| 416 |
unsigned int ifIndexArray[256]; |
| 417 |
unsigned int ifCount = ProtoSocket::GetInterfaceIndices(ifIndexArray, 256); |
| 418 |
if (0 == ifCount) |
| 419 |
{ |
| 420 |
PLOG(PL_ERROR, "BsdDetour::Open(): warning: no network interface indices were found.\n"); |
| 421 |
} |
| 422 |
else if (ifCount > 256) |
| 423 |
{ |
| 424 |
PLOG(PL_ERROR, "BsdDetour::Open(): warning: found network interfaces indices exceeding maximum count.\n"); |
| 425 |
ifCount = 256; |
| 426 |
} |
| 427 |
// Add any IP addrs assigned to this iface to our if_addr_list |
| 428 |
for (unsigned int i = 0; i < ifCount; i++) |
| 429 |
{ |
| 430 |
unsigned int ifIndex = ifIndexArray[i]; |
| 431 |
ProtoAddressList tempList; |
| 432 |
if (!rtMgr->GetInterfaceAddressList(ifIndex, ProtoAddress::IPv4, tempList)) |
| 433 |
{ |
| 434 |
PLOG(PL_ERROR, "BsdDetour::Open() warning: couldn't retrieve IPv4 address for iface index: %d\n", ifIndex); |
| 435 |
} |
| 436 |
if (!rtMgr->GetInterfaceAddressList(ifIndex, ProtoAddress::IPv6, tempList)) |
| 437 |
{ |
| 438 |
PLOG(PL_ERROR, "BsdDetour::Open() warning: couldn't retrieve IPv6 address for iface index: %d\n", ifIndex); |
| 439 |
} |
| 440 |
ProtoAddressList::Iterator it(tempList); |
| 441 |
ProtoAddress addr; |
| 442 |
while (it.GetNextAddress(addr)) |
| 443 |
{ |
| 444 |
AddressListItem* ifItem = new AddressListItem(addr, ifIndex); |
| 445 |
if (NULL == ifItem) |
| 446 |
{ |
| 447 |
PLOG(PL_ERROR, "BsdDetour::Open() new AddressListItem error: %s\n", GetErrorString()); |
| 448 |
delete rtMgr; |
| 449 |
Close(); |
| 450 |
return false; |
| 451 |
} |
| 452 |
if_addr_list.Insert(*ifItem); |
| 453 |
} |
| 454 |
} |
| 455 |
delete rtMgr; |
| 456 |
return true; |
| 457 |
} // end BsdDetour::Open() |
| 458 |
|
| 459 |
bool BsdDetour::SetMulticastInterface(const char* interfaceName) |
| 460 |
{ |
| 461 |
ASSERT(IsOpen()); |
| 462 |
if (NULL != interfaceName) |
| 463 |
{ |
| 464 |
int result; |
| 465 |
#ifdef HAVE_IPV6 |
| 466 |
if (AF_INET6 == domain) |
| 467 |
{ |
| 468 |
unsigned int interfaceIndex = ProtoSocket::GetInterfaceIndex(interfaceName); |
| 469 |
result = setsockopt(descriptor, IPPROTO_IPV6, IPV6_MULTICAST_IF, |
| 470 |
(char*)&interfaceIndex, sizeof(interfaceIndex)); |
| 471 |
} |
| 472 |
else |
| 473 |
#endif // HAVE_IPV6 |
| 474 |
{ |
| 475 |
struct in_addr localAddr; |
| 476 |
ProtoAddress interfaceAddress; |
| 477 |
if (ProtoSocket::GetInterfaceAddress(interfaceName, ProtoAddress::IPv4, interfaceAddress)) |
| 478 |
{ |
| 479 |
localAddr.s_addr = htonl(interfaceAddress.IPv4GetAddress()); |
| 480 |
} |
| 481 |
else |
| 482 |
{ |
| 483 |
PLOG(PL_ERROR, "BsdDetour::SetMulticastInterface() invalid interface name\n"); |
| 484 |
return false; |
| 485 |
} |
| 486 |
result = setsockopt(descriptor, IPPROTO_IP, IP_MULTICAST_IF, (char*)&localAddr, |
| 487 |
sizeof(localAddr)); |
| 488 |
} |
| 489 |
if (result < 0) |
| 490 |
{ |
| 491 |
PLOG(PL_ERROR, "BsdDetour: setsockopt(IP_MULTICAST_IF) error: %s\n", |
| 492 |
GetErrorString()); |
| 493 |
return false; |
| 494 |
} |
| 495 |
} |
| 496 |
return true; |
| 497 |
} // end BsdDetour::SetMulticastInterface() |
| 498 |
|
| 499 |
void BsdDetour::Close() |
| 500 |
{ |
| 501 |
if (0 != hook_flags) |
| 502 |
{ |
| 503 |
for (unsigned int i =0; i < rule_count; i++) |
| 504 |
SetIPFirewall(DELETE, (int)rule_number[i], |
| 505 |
src_filter_addr, src_filter_mask, |
| 506 |
dst_filter_addr, dst_filter_mask); |
| 507 |
rule_count = 0; |
| 508 |
hook_flags = 0; |
| 509 |
} |
| 510 |
if (descriptor >= 0) |
| 511 |
{ |
| 512 |
ProtoDetour::Close(); |
| 513 |
close(descriptor); |
| 514 |
descriptor = INVALID_HANDLE; |
| 515 |
} |
| 516 |
} // end BsdDetour::Close() |
| 517 |
|
| 518 |
bool BsdDetour::Recv(char* buffer, |
| 519 |
unsigned int& numBytes, |
| 520 |
Direction* direction, |
| 521 |
ProtoAddress* srcMac, |
| 522 |
unsigned int* ifIndex) |
| 523 |
{ |
| 524 |
// (TBD) can BSD divert socket get src MAC addr? |
| 525 |
// (It looks like we _could_ get a full MAC header if we |
| 526 |
// use a "layer 2" hook to capture packets in our |
| 527 |
// ipfw rule) |
| 528 |
if (NULL != srcMac) srcMac->Invalidate(); |
| 529 |
|
| 530 |
struct sockaddr_storage sockAddr; |
| 531 |
socklen_t addrLen = sizeof(sockAddr); |
| 532 |
ssize_t result = recvfrom(descriptor, buffer, numBytes, 0, |
| 533 |
(struct sockaddr*)&sockAddr, &addrLen); |
| 534 |
if (result < 0) |
| 535 |
{ |
| 536 |
pkt_addr.Invalidate(); |
| 537 |
switch (errno) |
| 538 |
{ |
| 539 |
case EINTR: |
| 540 |
case EAGAIN: |
| 541 |
numBytes = 0; |
| 542 |
if (NULL != direction) |
| 543 |
*direction = UNSPECIFIED; |
| 544 |
return true; |
| 545 |
default: |
| 546 |
numBytes = 0; |
| 547 |
PLOG(PL_ERROR, "BsdDetour::Recv() recvfrom() error: %s\n", GetErrorString()); |
| 548 |
return false; |
| 549 |
} |
| 550 |
} |
| 551 |
else |
| 552 |
{ |
| 553 |
pkt_addr.SetSockAddr(*((struct sockaddr*)&sockAddr)); |
| 554 |
numBytes = result; |
| 555 |
if (NULL != direction) |
| 556 |
{ |
| 557 |
if (pkt_addr.IsUnspecified()) |
| 558 |
*direction = OUTBOUND; |
| 559 |
else |
| 560 |
*direction = INBOUND; |
| 561 |
} |
| 562 |
if (NULL != ifIndex) |
| 563 |
{ |
| 564 |
if (pkt_addr.IsUnspecified()) |
| 565 |
{ |
| 566 |
*ifIndex = 0; |
| 567 |
} |
| 568 |
else |
| 569 |
{ |
| 570 |
// Lookup "ifIndex" for given interface IP address |
| 571 |
AddressListItem* ifItem = |
| 572 |
static_cast<AddressListItem*>(if_addr_list.Find(pkt_addr.GetRawHostAddress(), pkt_addr.GetLength() << 3)); |
| 573 |
if (NULL != ifItem) |
| 574 |
*ifIndex = ifItem->GetInterfaceIndex(); |
| 575 |
else |
| 576 |
*ifIndex = 0; |
| 577 |
} |
| 578 |
} |
| 579 |
return true; |
| 580 |
} |
| 581 |
} // end BsdDetour::Recv() |
| 582 |
|
| 583 |
bool BsdDetour::Allow(const char* buffer, unsigned int numBytes) |
| 584 |
{ |
| 585 |
if (pkt_addr.IsValid()) |
| 586 |
{ |
| 587 |
socklen_t addrSize = (ProtoAddress::IPv6 == pkt_addr.GetType()) ? |
| 588 |
sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); |
| 589 |
ssize_t result = sendto(descriptor, buffer, (size_t)numBytes, 0, |
| 590 |
&pkt_addr.GetSockAddr(), addrSize); |
| 591 |
if (result < 0) |
| 592 |
{ |
| 593 |
PLOG(PL_ERROR, "BsdDetour::Allow() sendto() error: %s\n", GetErrorString()); |
| 594 |
return false; |
| 595 |
} |
| 596 |
pkt_addr.Invalidate(); |
| 597 |
return true; |
| 598 |
} |
| 599 |
else |
| 600 |
{ |
| 601 |
PLOG(PL_ERROR, "BsdDetour::Allow() error: no packet pending\n"); |
| 602 |
return false; |
| 603 |
} |
| 604 |
} // end BsdDetour::Allow() |
| 605 |
|
| 606 |
bool BsdDetour::Drop() |
| 607 |
{ |
| 608 |
if (pkt_addr.IsValid()) |
| 609 |
{ |
| 610 |
pkt_addr.Invalidate(); |
| 611 |
return true; |
| 612 |
} |
| 613 |
else |
| 614 |
{ |
| 615 |
PLOG(PL_ERROR, "BsdDetour::Drop() error: no packet pending\n"); |
| 616 |
return false; |
| 617 |
} |
| 618 |
} // end BsdDetour::Drop() |
| 619 |
|
| 620 |
bool BsdDetour::Inject(const char* buffer, unsigned int numBytes) |
| 621 |
{ |
| 622 |
if (inject_only) |
| 623 |
{ |
| 624 |
unsigned char version = buffer[0]; |
| 625 |
version >>= 4; |
| 626 |
ProtoAddress dstAddr; |
| 627 |
socklen_t addrLen; |
| 628 |
if (4 == version) |
| 629 |
{ |
| 630 |
dstAddr.SetRawHostAddress(ProtoAddress::IPv4, buffer+16, 4); |
| 631 |
addrLen = sizeof(struct sockaddr); |
| 632 |
} |
| 633 |
else if (6 == version) |
| 634 |
{ |
| 635 |
//PLOG(PL_ERROR, "BsdDetour::Inject() IPv6 injection not yet supported!\n"); |
| 636 |
dstAddr.SetRawHostAddress(ProtoAddress::IPv6, buffer+24, 16); |
| 637 |
addrLen = sizeof(struct sockaddr_in6); |
| 638 |
//return false; |
| 639 |
} |
| 640 |
else |
| 641 |
{ |
| 642 |
PLOG(PL_ERROR, "BsdDetour::Inject() unknown IP version!\n"); |
| 643 |
return false; |
| 644 |
} |
| 645 |
size_t result = sendto(descriptor, buffer, numBytes, 0, |
| 646 |
&dstAddr.GetSockAddr(), addrLen); |
| 647 |
if (result != numBytes) |
| 648 |
{ |
| 649 |
PLOG(PL_ERROR, "BsdDetour::Inject() sendto() error: %s\n", GetErrorString()); |
| 650 |
return false; |
| 651 |
} |
| 652 |
return true; |
| 653 |
} |
| 654 |
else |
| 655 |
{ |
| 656 |
size_t result = write(descriptor, buffer, (size_t)numBytes); |
| 657 |
if (result != numBytes) |
| 658 |
{ |
| 659 |
PLOG(PL_ERROR, "BsdDetour::Inject() write() error: %s\n", GetErrorString()); |
| 660 |
return false; |
| 661 |
} |
| 662 |
} |
| 663 |
return true; |
| 664 |
} // end BsdDetour::Inject() |
| 665 |
|