| 1 |
|
| 2 |
#include "protoDebug.h" |
| 3 |
#include "protoDetour.h" |
| 4 |
#include "protoCap.h" // used for packet injection |
| 5 |
#include "protoNet.h" |
| 6 |
|
| 7 |
#include <stdlib.h> // for atoi(), getenv() |
| 8 |
#include <stdio.h> |
| 9 |
#include <unistd.h> // for close() |
| 10 |
#include <linux/netfilter_ipv4.h> // for NF_IP_LOCAL_OUT, etc |
| 11 |
#include <linux/netfilter_ipv6.h> // for NF_IP6_LOCAL_OUT, etc |
| 12 |
#include <linux/netfilter.h> // for NF_ACCEPT, etc |
| 13 |
#include <libnetfilter_queue/libnetfilter_queue.h> |
| 14 |
|
| 15 |
#include <fcntl.h> // for fcntl(), etc |
| 16 |
#include <linux/if_ether.h> // for ETH_P_IP |
| 17 |
#include <net/if_arp.h> // for ARPHRD_ETHER |
| 18 |
|
| 19 |
#include <linux/version.h> // for LINUX_VERSION_CODE |
| 20 |
|
| 21 |
/** NOTES: |
| 22 |
* |
| 23 |
* 1) This newer implementation of LinuxDetour uses netfilter_queue |
| 24 |
* instead of the ip_queue daemon of that prior implementation. |
| 25 |
* This allows multiple user space processes to access the firewall. |
| 26 |
* |
| 27 |
* 2) With netfilter_queue, you have to use the "callback" approach so |
| 28 |
* that the internal NFQ can parse the netlink message with the packet |
| 29 |
* data. Unfortunately, this means our LinuxDetour() has to do |
| 30 |
* an additional copy since we can't read the packet data directly to |
| 31 |
* the application-provided "buffer" as we could with the prior |
| 32 |
* "ip_queue" implementation. (Yuck! - but, oh well) |
| 33 |
* |
| 34 |
*/ |
| 35 |
|
| 36 |
class LinuxDetour : public ProtoDetour |
| 37 |
{ |
| 38 |
public: |
| 39 |
LinuxDetour(); |
| 40 |
~LinuxDetour(); |
| 41 |
|
| 42 |
bool Open(int hookFlags = 0, |
| 43 |
const ProtoAddress& srcFilterAddr = PROTO_ADDR_NONE, |
| 44 |
unsigned int srcFilterMask = 0, |
| 45 |
const ProtoAddress& dstFilterAddr = PROTO_ADDR_NONE, |
| 46 |
unsigned int dstFilterMask = 0, |
| 47 |
int dscpValue = -1); |
| 48 |
void Close(); |
| 49 |
|
| 50 |
virtual bool Recv(char* buffer, |
| 51 |
unsigned int& numBytes, |
| 52 |
Direction* direction = NULL, // optionally learn INBOUND/OUTBOUND direction of pkt |
| 53 |
ProtoAddress* srcMac = NULL, // optionally learn previous hop source MAC addr |
| 54 |
unsigned int* ifIndex = NULL); // optionally learn which iface (INBOUND only) |
| 55 |
|
| 56 |
bool Allow(const char* buffer, unsigned int numBytes); |
| 57 |
bool Drop(); |
| 58 |
bool Inject(const char* buffer, unsigned int numBytes); |
| 59 |
|
| 60 |
virtual bool SetMulticastInterface(const char* interfaceName); |
| 61 |
|
| 62 |
private: |
| 63 |
enum Action |
| 64 |
{ |
| 65 |
INSTALL, |
| 66 |
DELETE |
| 67 |
}; |
| 68 |
|
| 69 |
// Simple has used to randomize pid into base nfq_num |
| 70 |
UINT32 JenkinsHash(UINT32 value); |
| 71 |
|
| 72 |
bool SetIPTables(UINT16 nfqNum, |
| 73 |
Action action, |
| 74 |
int hookFlags , |
| 75 |
const ProtoAddress& srcFilterAddr, |
| 76 |
unsigned int srcFilterMask, |
| 77 |
const ProtoAddress& dstFilterAddr, |
| 78 |
unsigned int dstFilterMask, |
| 79 |
int dscpValue); |
| 80 |
|
| 81 |
int raw_fd; // for packet injection |
| 82 |
int hook_flags; |
| 83 |
ProtoAddress src_filter_addr; |
| 84 |
unsigned int src_filter_mask; |
| 85 |
ProtoAddress dst_filter_addr; |
| 86 |
unsigned int dst_filter_mask; |
| 87 |
int dscp_value; |
| 88 |
|
| 89 |
enum {NFQ_BUFFER_SIZE = 8192}; |
| 90 |
|
| 91 |
static int NfqCallback(nfq_q_handle* nfqQueue, |
| 92 |
struct nfgenmsg* nfqMsg, |
| 93 |
nfq_data* nfqData, |
| 94 |
void* userData); |
| 95 |
|
| 96 |
// This is initialized with a value set by the |
| 97 |
// "PROTO_NFQ_NUM_BASE" or uses a hash of the |
| 98 |
// process id otherwise |
| 99 |
static bool nfq_num_init; |
| 100 |
static UINT16 nfq_num_next; // |
| 101 |
|
| 102 |
struct nfq_handle* nfq_handle; |
| 103 |
struct nfq_q_handle* nfq_queue; |
| 104 |
UINT16 nfq_num; // based on pid |
| 105 |
|
| 106 |
// The NfqCallback() fills these in on a per-packet basis |
| 107 |
UINT32 nfq_pkt_id; |
| 108 |
char* nfq_pkt_data; |
| 109 |
unsigned int nfq_pkt_len; |
| 110 |
Direction nfq_direction; |
| 111 |
ProtoAddress nfq_src_macaddr; |
| 112 |
unsigned int nfq_ifindex; |
| 113 |
|
| 114 |
}; // end class LinuxDetour |
| 115 |
|
| 116 |
ProtoDetour* ProtoDetour::Create() |
| 117 |
{ |
| 118 |
return static_cast<ProtoDetour*>(new LinuxDetour()); |
| 119 |
} // end ProtoDetour::Create(), (void*)nl_head |
| 120 |
|
| 121 |
bool LinuxDetour::nfq_num_init = true; |
| 122 |
UINT16 LinuxDetour::nfq_num_next = 0; |
| 123 |
|
| 124 |
LinuxDetour::LinuxDetour() |
| 125 |
: raw_fd(-1), hook_flags(0), dscp_value(-1), |
| 126 |
nfq_handle(NULL), nfq_queue(NULL), |
| 127 |
nfq_pkt_id(0), nfq_pkt_data(NULL), nfq_pkt_len(0), |
| 128 |
nfq_direction(UNSPECIFIED), nfq_ifindex(0) |
| 129 |
|
| 130 |
{ |
| 131 |
if (nfq_num_init) |
| 132 |
{ |
| 133 |
char* cp = getenv("PROTO_NFQ_NUM_BASE"); |
| 134 |
if (NULL != cp) |
| 135 |
nfq_num_next = (UINT16)atoi(cp); |
| 136 |
else |
| 137 |
nfq_num_next = (UINT16)JenkinsHash(getpid()); // TBD - implement a semaphore for this? |
| 138 |
nfq_num_init = false; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
LinuxDetour::~LinuxDetour() |
| 143 |
{ |
| 144 |
Close(); |
| 145 |
} |
| 146 |
|
| 147 |
UINT32 LinuxDetour::JenkinsHash(UINT32 a) |
| 148 |
{ |
| 149 |
a = (a+0x7ed55d16) + (a<<12); |
| 150 |
a = (a^0xc761c23c) ^ (a>>19); |
| 151 |
a = (a+0x165667b1) + (a<<5); |
| 152 |
a = (a+0xd3a2646c) ^ (a<<9); |
| 153 |
a = (a+0xfd7046c5) + (a<<3); |
| 154 |
a = (a^0xb55a4f09) ^ (a>>16); |
| 155 |
return a; |
| 156 |
} // end LinuxDetour::JenkinsHash() |
| 157 |
|
| 158 |
bool LinuxDetour::SetIPTables(UINT16 nfqNum, |
| 159 |
Action action, |
| 160 |
int hookFlags , |
| 161 |
const ProtoAddress& srcFilterAddr, |
| 162 |
unsigned int srcFilterMask, |
| 163 |
const ProtoAddress& dstFilterAddr, |
| 164 |
unsigned int dstFilterMask, |
| 165 |
int dscpValue) |
| 166 |
{ |
| 167 |
// 1) IPv4 or IPv6 address family? |
| 168 |
// (Note we now use the "mangle" table for our stuf) |
| 169 |
const char* cmd; |
| 170 |
if (srcFilterAddr.GetType() != dstFilterAddr.GetType()) |
| 171 |
{ |
| 172 |
PLOG(PL_ERROR, "LinuxDetour::SetIPTables() error: inconsistent src/dst filter addr families\n"); |
| 173 |
return false; |
| 174 |
} |
| 175 |
else if (ProtoAddress::IPv4 == srcFilterAddr.GetType()) |
| 176 |
{ |
| 177 |
cmd = "/sbin/iptables -t mangle"; // IPv4 iptables |
| 178 |
} |
| 179 |
else if (ProtoAddress::IPv6 == srcFilterAddr.GetType()) |
| 180 |
{ |
| 181 |
cmd = "/sbin/ip6tables -t mangle"; |
| 182 |
} |
| 183 |
else |
| 184 |
{ |
| 185 |
PLOG(PL_ERROR, "LinuxDetour::SetIPTables() error: unspecified filter addr family\n"); |
| 186 |
return false; |
| 187 |
} |
| 188 |
// 2) INSTALL ("-A") or DELETE ("-D") the rule |
| 189 |
const char* mode = (INSTALL == action) ? "-A" : "-D"; |
| 190 |
// 3) For which firewall hooks? |
| 191 |
while (0 != hookFlags) |
| 192 |
{ |
| 193 |
const char* target; |
| 194 |
if (0 != (hookFlags & OUTPUT)) |
| 195 |
{ |
| 196 |
target = "OUTPUT"; |
| 197 |
hookFlags &= ~OUTPUT; |
| 198 |
} |
| 199 |
else if (0 != (hookFlags & INPUT)) |
| 200 |
{ |
| 201 |
target = "PREROUTING"; // PREROUTING precedes INPUT in mangle table |
| 202 |
hookFlags &= ~INPUT; |
| 203 |
} |
| 204 |
else if (0 != (hookFlags & FORWARD)) |
| 205 |
{ |
| 206 |
target = "FORWARD"; |
| 207 |
hookFlags &= ~FORWARD; |
| 208 |
} |
| 209 |
else |
| 210 |
{ |
| 211 |
break; // all flags have been processed |
| 212 |
} |
| 213 |
|
| 214 |
// Make and install "iptables" firewall rules |
| 215 |
const size_t RULE_MAX = 511; |
| 216 |
char rule[RULE_MAX+1]; |
| 217 |
// cmd = "iptables" or "ip6tables" |
| 218 |
// mode = "-I" or "-D" |
| 219 |
// target = "INPUT", "OUTPUT", or "FORWARD" |
| 220 |
sprintf(rule, "%s %s %s -j NFQUEUE --queue-num %hu ", cmd, mode, target, nfqNum); |
| 221 |
if (0 != srcFilterMask) |
| 222 |
{ |
| 223 |
strcat(rule, "-s "); |
| 224 |
size_t len = strlen(rule); |
| 225 |
if (!srcFilterAddr.GetHostString(rule+len, RULE_MAX - len)) |
| 226 |
{ |
| 227 |
PLOG(PL_ERROR, "LinuxDetour::SetIPTables() error: bad source addr filter\n"); |
| 228 |
return false; |
| 229 |
} |
| 230 |
len = strlen(rule); |
| 231 |
sprintf(rule+len, "/%hu ", srcFilterMask); |
| 232 |
} |
| 233 |
if (0 != dstFilterMask) |
| 234 |
{ |
| 235 |
strcat(rule, "-d "); |
| 236 |
size_t len = strlen(rule); |
| 237 |
if (!dstFilterAddr.GetHostString(rule+len, RULE_MAX - len)) |
| 238 |
{ |
| 239 |
PLOG(PL_ERROR, "LinuxDetour::SetIPTables() error: bad destination addr filter\n"); |
| 240 |
return false; |
| 241 |
} |
| 242 |
len = strlen(rule); |
| 243 |
sprintf(rule+len, "/%hu ", dstFilterMask); |
| 244 |
} |
| 245 |
|
| 246 |
// Add DSCP filter command, if applicable |
| 247 |
if (dscpValue >= 0) |
| 248 |
{ |
| 249 |
// TBD - error check dscpValue??? |
| 250 |
size_t len = strlen(rule); |
| 251 |
sprintf(rule+len, "-m dscp --dscp %d ", dscpValue); |
| 252 |
} |
| 253 |
|
| 254 |
// Add redirection so we can get stderr result |
| 255 |
strcat(rule, " 2>&1"); |
| 256 |
FILE* p = popen(rule, "r"); |
| 257 |
if (NULL != p) |
| 258 |
{ |
| 259 |
char errorMsg[256]; |
| 260 |
int result = fread(errorMsg, 1, 256, p); |
| 261 |
if ((0 == result) && (0 != ferror(p))) |
| 262 |
{ |
| 263 |
PLOG(PL_ERROR, "LinuxDetour::SetIPTables() fread() error: %s\n", |
| 264 |
GetErrorString()); |
| 265 |
return false; |
| 266 |
} |
| 267 |
char* ptr = strchr(errorMsg, '\n'); |
| 268 |
if (NULL != ptr) *ptr = '\0'; |
| 269 |
errorMsg[255] = '\0'; |
| 270 |
if (0 != pclose(p)) |
| 271 |
{ |
| 272 |
PLOG(PL_ERROR, "LinuxDetour::SetIPTables() \"%s\" error: %s\n", |
| 273 |
rule, errorMsg); |
| 274 |
return false; |
| 275 |
} |
| 276 |
} |
| 277 |
else |
| 278 |
{ |
| 279 |
PLOG(PL_ERROR, "LinuxDetour::SetIPTables() error: popen(%s): %s\n", |
| 280 |
rule, GetErrorString()); |
| 281 |
return false; |
| 282 |
} |
| 283 |
} // end while (0 != hookFlags) |
| 284 |
return true; |
| 285 |
} // end LinuxDetour::SetIPTables() |
| 286 |
|
| 287 |
bool LinuxDetour::Open(int hookFlags, |
| 288 |
const ProtoAddress& srcFilterAddr, |
| 289 |
unsigned int srcFilterMask, |
| 290 |
const ProtoAddress& dstFilterAddr, |
| 291 |
unsigned int dstFilterMask, |
| 292 |
int dscpValue) |
| 293 |
{ |
| 294 |
if (IsOpen()) Close(); |
| 295 |
|
| 296 |
// 0) Open raw socket for optional packet injection use |
| 297 |
//if (0 > (raw_fd = socket(domain, SOCK_RAW, IPPROTO_RAW))) |
| 298 |
if (0 > (raw_fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) // or can we IPv6 on an AF_INET/HDRINCL socket? |
| 299 |
{ |
| 300 |
PLOG(PL_ERROR, "LinuxDetour::Open() socket(IPPROTO_RAW) error: %s\n", |
| 301 |
GetErrorString()); |
| 302 |
return false; |
| 303 |
} |
| 304 |
//if (AF_INET == domain) |
| 305 |
{ |
| 306 |
// Note: no IP_HDRINCL for IPv6 raw sockets ? |
| 307 |
int enable = 1; |
| 308 |
if (setsockopt(raw_fd, IPPROTO_IP, IP_HDRINCL, &enable, sizeof(enable))) |
| 309 |
{ |
| 310 |
PLOG(PL_ERROR, "LinuxDetour::Open() setsockopt(IP_HDRINCL) error: %s\n", |
| 311 |
GetErrorString()); |
| 312 |
Close(); |
| 313 |
return false; |
| 314 |
} |
| 315 |
} |
| 316 |
// Set to non-blocking for our purposes (TBD) Add a SetBlocking() method |
| 317 |
if(-1 == fcntl(raw_fd, F_SETFL, fcntl(raw_fd, F_GETFL, 0) | O_NONBLOCK)) |
| 318 |
{ |
| 319 |
PLOG(PL_ERROR, "LinuxDetour::Open() fcntl(F_SETFL(O_NONBLOCK)) error: %s\n", GetErrorString()); |
| 320 |
Close(); |
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
// Check for "inject-only" mode and "rewire appropriately |
| 325 |
if (0 != (hookFlags & INJECT)) |
| 326 |
{ |
| 327 |
descriptor = raw_fd; |
| 328 |
raw_fd = -1; |
| 329 |
ProtoDetour::Open(); |
| 330 |
StopInputNotification(); |
| 331 |
return true; |
| 332 |
} |
| 333 |
|
| 334 |
int addrFamily; |
| 335 |
if (srcFilterAddr.GetType() != dstFilterAddr.GetType()) |
| 336 |
{ |
| 337 |
PLOG(PL_ERROR, "LinuxDetour::Open() error: inconsistent src/dst filter addr families\n"); |
| 338 |
Close(); |
| 339 |
return false; |
| 340 |
} |
| 341 |
else if (ProtoAddress::IPv4 == srcFilterAddr.GetType()) |
| 342 |
|
| 343 |
{ |
| 344 |
addrFamily = AF_INET; |
| 345 |
} |
| 346 |
else if (ProtoAddress::IPv6 == srcFilterAddr.GetType()) |
| 347 |
{ |
| 348 |
addrFamily = AF_INET6; |
| 349 |
} |
| 350 |
else |
| 351 |
{ |
| 352 |
PLOG(PL_ERROR, "LinuxDetour::Open() error: unspecified filter addr family\n"); |
| 353 |
Close(); |
| 354 |
return false; |
| 355 |
} |
| 356 |
|
| 357 |
// Make sure the "nfnetlink_queue" modules are loaded |
| 358 |
// (We make this non-fatal in the case the operating system has |
| 359 |
// these compiled-in and they are not used as loadable modules). |
| 360 |
// FILE* p = popen("/sbin/modprobe -l nfnetlink_queue 2>&1", "r"); |
| 361 |
FILE* p = popen("find /lib/modules/$(uname -r) -iname nfnetlink_queue.ko\\* 2>&1", "r"); |
| 362 |
if (NULL != p) |
| 363 |
{ |
| 364 |
// Read any error information fed back |
| 365 |
char errorMsg[256]; |
| 366 |
int result = fread(errorMsg, 1, 256, p); |
| 367 |
if ((0 == result) && (0 != ferror(p))) |
| 368 |
{ |
| 369 |
PLOG(PL_ERROR, "LinuxDetour::Open() fread() error: %s\n", |
| 370 |
GetErrorString()); |
| 371 |
return false; |
| 372 |
} |
| 373 |
errorMsg[255] = '\0'; |
| 374 |
if (0 != pclose(p)) |
| 375 |
PLOG(PL_ERROR, "LinuxDetour::Open() warning: \"/sbin/modprobe -l nfnetlink_queue\" error: %s", |
| 376 |
errorMsg); |
| 377 |
} |
| 378 |
else |
| 379 |
{ |
| 380 |
PLOG(PL_ERROR, "LinuxDetour::Open() warning: popen(\"/sbin/modprobe ...\") error: %s\n", |
| 381 |
GetErrorString()); |
| 382 |
} |
| 383 |
|
| 384 |
nfq_num = nfq_num_next++; // TBD - is there a better way?? |
| 385 |
|
| 386 |
// Save parameters for firewall rule removal |
| 387 |
hook_flags = hookFlags; |
| 388 |
src_filter_addr = srcFilterAddr; |
| 389 |
src_filter_mask = srcFilterMask; |
| 390 |
dst_filter_addr = dstFilterAddr; |
| 391 |
dst_filter_mask = dstFilterMask; |
| 392 |
dscp_value = dscpValue; |
| 393 |
// Set up iptables (or ip6tables) if non-zero "hookFlags" are provided |
| 394 |
if (0 != hookFlags) |
| 395 |
{ |
| 396 |
if (!SetIPTables(nfq_num, INSTALL, hookFlags, |
| 397 |
srcFilterAddr, srcFilterMask, |
| 398 |
dstFilterAddr, dstFilterMask, |
| 399 |
dscpValue)) |
| 400 |
{ |
| 401 |
PLOG(PL_ERROR, "LinuxDetour::Open() error: couldn't install firewall rules\n"); |
| 402 |
Close(); |
| 403 |
return false; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
// The first three nfq calls here set up the nfq library |
| 408 |
// Open netfilter_queue handle |
| 409 |
if (NULL == (nfq_handle = nfq_open())) |
| 410 |
{ |
| 411 |
PLOG(PL_ERROR, "LinuxDetour::Open() nfq_open() error: %s\n", GetErrorString()); |
| 412 |
Close(); |
| 413 |
return false; |
| 414 |
} |
| 415 |
descriptor = nfq_fd(nfq_handle); |
| 416 |
|
| 417 |
// Not sure this step is necessary, but was in example code |
| 418 |
if (nfq_unbind_pf(nfq_handle, addrFamily) < 0) |
| 419 |
{ |
| 420 |
PLOG(PL_ERROR, "LinuxDetour::Open() warning: nfq_unbind_pf() error: %s\n", GetErrorString()); |
| 421 |
//Close(); |
| 422 |
//return false; |
| 423 |
} |
| 424 |
// "bind" our nfq handle for the specified address family |
| 425 |
if (nfq_bind_pf(nfq_handle, addrFamily) < 0) |
| 426 |
{ |
| 427 |
PLOG(PL_ERROR, "LinuxDetour::Open() nfq_bind_pf() error: %s\n", GetErrorString()); |
| 428 |
Close(); |
| 429 |
return false; |
| 430 |
} |
| 431 |
|
| 432 |
// Next, set up an NFQ queue |
| 433 |
if (NULL == (nfq_queue = nfq_create_queue(nfq_handle, nfq_num, NfqCallback, this))) |
| 434 |
{ |
| 435 |
PLOG(PL_ERROR, "LinuxDetour::Open() nfq_create_queue() error: %s\n", GetErrorString()); |
| 436 |
Close(); |
| 437 |
return false; |
| 438 |
} |
| 439 |
|
| 440 |
// Turn on packet copy mode |
| 441 |
if (nfq_set_mode(nfq_queue, NFQNL_COPY_PACKET, NFQ_BUFFER_SIZE) < 0) |
| 442 |
{ |
| 443 |
PLOG(PL_ERROR, "LinuxDetour::Open() nfq_set_mode() error: %s\n", GetErrorString()); |
| 444 |
Close(); |
| 445 |
return false; |
| 446 |
} |
| 447 |
|
| 448 |
if (!ProtoDetour::Open()) |
| 449 |
{ |
| 450 |
PLOG(PL_ERROR, "LinuxDetour::Open() ProtoDetour::Open() error\n"); |
| 451 |
Close(); |
| 452 |
return false; |
| 453 |
} |
| 454 |
return true; |
| 455 |
} // end LinuxDetour::Open() |
| 456 |
|
| 457 |
void LinuxDetour::Close() |
| 458 |
{ |
| 459 |
if (raw_fd >= 0) |
| 460 |
{ |
| 461 |
close(raw_fd); |
| 462 |
raw_fd = -1; |
| 463 |
} |
| 464 |
if (0 != hook_flags) |
| 465 |
{ |
| 466 |
SetIPTables(nfq_num, DELETE, hook_flags, |
| 467 |
src_filter_addr, src_filter_mask, |
| 468 |
dst_filter_addr, dst_filter_mask, dscp_value); |
| 469 |
hook_flags = 0; |
| 470 |
} |
| 471 |
if (descriptor >= 0) |
| 472 |
{ |
| 473 |
ProtoDetour::Close(); |
| 474 |
descriptor = INVALID_HANDLE; |
| 475 |
} |
| 476 |
|
| 477 |
if (NULL != nfq_queue) |
| 478 |
{ |
| 479 |
nfq_destroy_queue(nfq_queue); |
| 480 |
nfq_queue = NULL; |
| 481 |
} |
| 482 |
|
| 483 |
if (NULL != nfq_handle) |
| 484 |
{ |
| 485 |
nfq_close(nfq_handle); |
| 486 |
nfq_handle = NULL; |
| 487 |
} |
| 488 |
|
| 489 |
} // end LinuxDetour::Close() |
| 490 |
|
| 491 |
bool LinuxDetour::Recv(char* buffer, |
| 492 |
unsigned int& numBytes, |
| 493 |
Direction* direction, |
| 494 |
ProtoAddress* srcMac, |
| 495 |
unsigned int* ifIndex) |
| 496 |
{ |
| 497 |
if (NULL != nfq_pkt_data) |
| 498 |
{ |
| 499 |
PLOG(PL_ERROR, "LinuxDetour::Recv() error: existing packet pending allow/drop!\n"); |
| 500 |
return false; |
| 501 |
} |
| 502 |
char nfqBuffer[NFQ_BUFFER_SIZE]; |
| 503 |
int result = recv(descriptor, nfqBuffer, NFQ_BUFFER_SIZE, 0); |
| 504 |
if (result < 0) |
| 505 |
{ |
| 506 |
numBytes = 0; |
| 507 |
if (NULL != direction) *direction = UNSPECIFIED; |
| 508 |
if (NULL != srcMac) srcMac->Invalidate(); |
| 509 |
if (NULL != ifIndex) *ifIndex = 0; |
| 510 |
if ((EAGAIN != errno) && (EINTR != errno)) |
| 511 |
{ |
| 512 |
PLOG(PL_ERROR, "LinuxDetour::Recv() recv() error: %s\n", GetErrorString()); |
| 513 |
return false; |
| 514 |
} |
| 515 |
} |
| 516 |
else |
| 517 |
{ |
| 518 |
// This will invoke our "nfq_callback" which sets a pointer to |
| 519 |
// the packet data "nfq_pkt_data" and the "nfq_pkt_len" value |
| 520 |
nfq_handle_packet(nfq_handle, nfqBuffer, result); |
| 521 |
if (NULL != nfq_pkt_data) |
| 522 |
{ |
| 523 |
if (NULL != direction) *direction = nfq_direction; |
| 524 |
if (NULL != srcMac) *srcMac = nfq_src_macaddr; |
| 525 |
if (NULL != ifIndex) *ifIndex = nfq_ifindex; |
| 526 |
if (numBytes < nfq_pkt_len) |
| 527 |
memcpy(buffer, nfq_pkt_data, numBytes); |
| 528 |
else |
| 529 |
memcpy(buffer, nfq_pkt_data, nfq_pkt_len); |
| 530 |
numBytes = nfq_pkt_len; |
| 531 |
} |
| 532 |
else |
| 533 |
{ |
| 534 |
// It was a "false alarm" (i.e. not a relevant nfq event) |
| 535 |
numBytes = 0; |
| 536 |
if (NULL != direction) *direction = UNSPECIFIED; |
| 537 |
if (NULL != srcMac) srcMac->Invalidate(); |
| 538 |
if (NULL != ifIndex) *ifIndex = 0; |
| 539 |
} |
| 540 |
} |
| 541 |
return true; |
| 542 |
} // end LinuxDetour::Recv() |
| 543 |
|
| 544 |
|
| 545 |
int LinuxDetour::NfqCallback(nfq_q_handle* nfqQueue, |
| 546 |
struct nfgenmsg* nfqMsg, |
| 547 |
nfq_data* nfqData, |
| 548 |
void* userData) |
| 549 |
{ |
| 550 |
ASSERT(NULL != userData); |
| 551 |
LinuxDetour* linuxDetour = reinterpret_cast<LinuxDetour*>(userData); |
| 552 |
// We use the "hook" point to determine inbound | outbound packet "direction" |
| 553 |
linuxDetour->nfq_direction = UNSPECIFIED; |
| 554 |
struct nfqnl_msg_packet_hdr* header = nfq_get_msg_packet_hdr(nfqData); |
| 555 |
if(NULL != header) |
| 556 |
{ |
| 557 |
linuxDetour->nfq_pkt_id = ntohl(header->packet_id); |
| 558 |
if (ProtoAddress::IPv4 == linuxDetour->src_filter_addr.GetType()) |
| 559 |
{ |
| 560 |
if (NF_IP_LOCAL_OUT == header->hook) |
| 561 |
linuxDetour->nfq_direction = OUTBOUND; |
| 562 |
else |
| 563 |
linuxDetour->nfq_direction = INBOUND; |
| 564 |
} |
| 565 |
else // (assume IPv6) |
| 566 |
{ |
| 567 |
if (NF_IP_LOCAL_OUT == header->hook) |
| 568 |
linuxDetour->nfq_direction = OUTBOUND; |
| 569 |
else |
| 570 |
linuxDetour->nfq_direction = INBOUND; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
// Get the src mac addr for the packet if available |
| 575 |
struct nfqnl_msg_packet_hw* hw = nfq_get_packet_hw(nfqData); |
| 576 |
if (NULL != hw) |
| 577 |
linuxDetour->nfq_src_macaddr.SetRawHostAddress(ProtoAddress::ETH, (char*)hw->hw_addr, (UINT8)ntohs(hw->hw_addrlen)); |
| 578 |
else |
| 579 |
linuxDetour->nfq_src_macaddr.Invalidate(); |
| 580 |
// Here, based on the "direction" we cache the associated |
| 581 |
// input or output ifindex. If direction is "UNSPECIFIED", |
| 582 |
// we look for a valid input or output ifindex to guess direction |
| 583 |
// TBD - test and refine this. |
| 584 |
if (OUTBOUND == linuxDetour->nfq_direction) |
| 585 |
linuxDetour->nfq_ifindex = nfq_get_outdev(nfqData); |
| 586 |
else if (INBOUND == linuxDetour->nfq_direction) |
| 587 |
linuxDetour->nfq_ifindex = nfq_get_indev(nfqData); |
| 588 |
else if (0 != (linuxDetour->nfq_ifindex = nfq_get_indev(nfqData))) |
| 589 |
linuxDetour->nfq_direction = INBOUND; |
| 590 |
else if (0 != (linuxDetour->nfq_ifindex = nfq_get_outdev(nfqData))) |
| 591 |
linuxDetour->nfq_direction = INBOUND; |
| 592 |
|
| 593 |
// Finally record packet length and cache pointer to IP packet data |
| 594 |
|
| 595 |
// A change to the nfq_get_payload() prototype seemed to kick in around Linux header files |
| 596 |
// version 3.6? (This will probably need to be fine tuned for the right version threshold.) |
| 597 |
|
| 598 |
#define LINUX_VERSION_MAJOR (LINUX_VERSION_CODE/65536) |
| 599 |
#define LINUX_VERSION_MINOR ((LINUX_VERSION_CODE - (LINUX_VERSION_MAJOR*65536)) / 256) |
| 600 |
|
| 601 |
#if ((LINUX_VERSION_MAJOR > 3) || ((LINUX_VERSION_MAJOR == 3) && (LINUX_VERSION_MINOR > 5))) |
| 602 |
linuxDetour->nfq_pkt_len = nfq_get_payload(nfqData, (unsigned char**)(&linuxDetour->nfq_pkt_data)); |
| 603 |
#else |
| 604 |
linuxDetour->nfq_pkt_len = nfq_get_payload(nfqData, &linuxDetour->nfq_pkt_data); |
| 605 |
#endif // |
| 606 |
return 0; |
| 607 |
} // end LinuxDetour::NfqCallback() |
| 608 |
|
| 609 |
bool LinuxDetour::Allow(const char* buffer, unsigned int numBytes) |
| 610 |
{ |
| 611 |
if (NULL == nfq_queue) |
| 612 |
{ |
| 613 |
PLOG(PL_ERROR, "LinuxDetour::Allow() error: not opened\n"); |
| 614 |
return false; |
| 615 |
} |
| 616 |
if (NULL == nfq_pkt_data) |
| 617 |
{ |
| 618 |
PLOG(PL_ERROR, "LinuxDetour::Allow() error: no pending packet\n"); |
| 619 |
return false; |
| 620 |
} |
| 621 |
if (0 > nfq_set_verdict(nfq_queue, nfq_pkt_id, NF_ACCEPT, numBytes, (unsigned char*)buffer)) |
| 622 |
{ |
| 623 |
PLOG(PL_ERROR, "LinuxDetour::Allow() nfq_set_verdict() error: %s\n", |
| 624 |
GetErrorString()); |
| 625 |
return false; |
| 626 |
} |
| 627 |
nfq_pkt_data = NULL; // indicates verdict has been set |
| 628 |
return true; |
| 629 |
} // end LinuxDetour::Allow() |
| 630 |
|
| 631 |
bool LinuxDetour::Drop() |
| 632 |
{ |
| 633 |
if (NULL == nfq_queue) |
| 634 |
{ |
| 635 |
PLOG(PL_ERROR, "LinuxDetour::Drop() error: not opened\n"); |
| 636 |
return false; |
| 637 |
} |
| 638 |
if (NULL == nfq_pkt_data) |
| 639 |
{ |
| 640 |
PLOG(PL_ERROR, "LinuxDetour::Drop() error: no pending packet\n"); |
| 641 |
return false; |
| 642 |
} |
| 643 |
if (0 > nfq_set_verdict(nfq_queue, nfq_pkt_id, NF_DROP, 0, NULL)) |
| 644 |
{ |
| 645 |
PLOG(PL_ERROR, "LinuxDetour::Drop() nfq_set_verdict() error: %s\n", |
| 646 |
GetErrorString()); |
| 647 |
return false; |
| 648 |
} |
| 649 |
nfq_pkt_data = NULL; // indicates verdict has been set |
| 650 |
return true; |
| 651 |
} // end LinuxDetour::Drop() |
| 652 |
|
| 653 |
bool LinuxDetour::Inject(const char* buffer, unsigned int numBytes) |
| 654 |
{ |
| 655 |
unsigned char version = buffer[0]; |
| 656 |
version >>= 4; |
| 657 |
ProtoAddress dstAddr; |
| 658 |
socklen_t addrLen; |
| 659 |
if (4 == version) |
| 660 |
{ |
| 661 |
dstAddr.SetRawHostAddress(ProtoAddress::IPv4, buffer+16, 4); |
| 662 |
addrLen = sizeof(struct sockaddr); |
| 663 |
} |
| 664 |
else if (6 == version) |
| 665 |
{ |
| 666 |
PLOG(PL_ERROR, "LinuxDetour::Inject() IPv6 injection not yet supported!\n"); |
| 667 |
return false; |
| 668 |
} |
| 669 |
else |
| 670 |
{ |
| 671 |
PLOG(PL_ERROR, "LinuxDetour::Inject() unknown IP version!\n"); |
| 672 |
return false; |
| 673 |
} |
| 674 |
int fd = (raw_fd < 0) ? descriptor : raw_fd; |
| 675 |
size_t result = sendto(fd, buffer, numBytes, 0, |
| 676 |
&dstAddr.GetSockAddr(), addrLen); |
| 677 |
if (result != numBytes) |
| 678 |
{ |
| 679 |
PLOG(PL_ERROR, "LinuxDetour::Inject() sendto() error: %s\n", GetErrorString()); |
| 680 |
return false; |
| 681 |
} |
| 682 |
return true; |
| 683 |
} // end LinuxDetour::Inject() |
| 684 |
|
| 685 |
bool LinuxDetour::SetMulticastInterface(const char* interfaceName) |
| 686 |
{ |
| 687 |
int fd = (raw_fd < 0) ? descriptor : raw_fd; |
| 688 |
if (fd < 0) |
| 689 |
{ |
| 690 |
PLOG(PL_ERROR, "LinuxDetour::SetMulticastInterface() error: detour not open\n"); |
| 691 |
return false; |
| 692 |
} |
| 693 |
|
| 694 |
if (interfaceName) |
| 695 |
{ |
| 696 |
int result; |
| 697 |
#ifdef HAVE_IPV6 |
| 698 |
if (ProtoAddress::IPv6 == src_filter_addr.GetType()) |
| 699 |
{ |
| 700 |
unsigned int interfaceIndex = ProtoNet::GetInterfaceIndex(interfaceName); |
| 701 |
result = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, |
| 702 |
(char*)&interfaceIndex, sizeof(interfaceIndex)); |
| 703 |
} |
| 704 |
else |
| 705 |
#endif // HAVE_IPV6 |
| 706 |
{ |
| 707 |
struct in_addr localAddr; |
| 708 |
ProtoAddress interfaceAddress; |
| 709 |
if (ProtoNet::GetInterfaceAddress(interfaceName, ProtoAddress::IPv4, interfaceAddress)) |
| 710 |
{ |
| 711 |
localAddr.s_addr = htonl(interfaceAddress.IPv4GetAddress()); |
| 712 |
} |
| 713 |
else |
| 714 |
{ |
| 715 |
PLOG(PL_ERROR, "LinuxDetour::SetMulticastInterface() invalid interface name\n"); |
| 716 |
return false; |
| 717 |
} |
| 718 |
result = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (char*)&localAddr, |
| 719 |
sizeof(localAddr)); |
| 720 |
} |
| 721 |
if (result < 0) |
| 722 |
{ |
| 723 |
PLOG(PL_ERROR, "LinuxDetour: setsockopt(IP_MULTICAST_IF) error: %s\n", |
| 724 |
GetErrorString()); |
| 725 |
return false; |
| 726 |
} |
| 727 |
} |
| 728 |
return true; |
| 729 |
} // end LinuxDetour::SetMulticastInterface() |