Debian Codegraph @

protolib/src/linux/androidDetour.cpp

raw
1 #include "protoDetour.h"
2 #include "protoCap.h" // used for packet injection
3 #include "protoSocket.h"
4 #include "protoTree.h" // for mapping ifName -> ifIndex
5
6 #include <stdio.h>
7 #include <asm/types.h>
8 #include <sys/types.h>
9 #include <unistd.h> // for getpid()
10 #include <sys/socket.h>
11 #include <linux/netlink.h>
12 #include <linux/netfilter.h>
13 #include <linux/netfilter_ipv4.h>
14 #include <linux/netfilter_ipv6.h>
15 #include <linux/netfilter_ipv4/ip_queue.h>
16 #include <fcntl.h> // for fcntl(), etc
17 #include <linux/if_ether.h> // for ETH_P_IP
18 #include <net/if_arp.h> // for ARPHRD_ETHER
19
20 /** NOTES:
21 *
22 * 1) Must run "modprobe ip_queue" before running
23 * programs using this class
24 *
25 */
26
27 // This is the "old" LinuxDetour code that uses the "ip_queue"
28 // iptables hook instead of the newer "ipnetfilter_queue" code
29
30 class LinuxDetour : public ProtoDetour
31 {
32 public:
33 LinuxDetour();
34 ~LinuxDetour();
35
36 bool Open(int hookFlags = 0,
37 const ProtoAddress& srcFilterAddr = PROTO_ADDR_NONE,
38 unsigned int srcFilterMask = 0,
39 const ProtoAddress& dstFilterAddr = PROTO_ADDR_NONE,
40 unsigned int dstFilterMask = 0,
41 int dscpValue = -1);
42 void Close();
43
44 virtual bool Recv(char* buffer,
45 unsigned int& numBytes,
46 Direction* direction = NULL, // optionally learn INBOUND/OUTBOUND direction of pkt
47 ProtoAddress* srcMac = NULL, // optionally learn previous hop source MAC addr
48 unsigned int* ifIndex = NULL); // optionally learn which iface (INBOUND only)
49
50 bool Allow(const char* buffer, unsigned int numBytes);
51 bool Drop();
52 bool Inject(const char* buffer, unsigned int numBytes);
53
54 virtual bool SetMulticastInterface(const char* interfaceName);
55
56 private:
57 enum Action
58 {
59 INSTALL,
60 DELETE
61 };
62
63
64 // used for name -> index lookup
65 class IfNameItem : public ProtoTree::Item
66 {
67 public:
68 IfNameItem();
69 ~IfNameItem();
70 bool Init(const char* ifName, unsigned int ifIndex);
71
72 const char* GetIfName() const {return if_name;}
73 unsigned int GetIfIndex() const {return if_index;}
74
75 // Required ProtoTree::Item overrides
76 const char* GetKey() const {return if_name;}
77 unsigned int GetKeysize() const {return if_name_size;}
78
79 private:
80 char* if_name;
81 unsigned int if_name_size; // in bits
82 unsigned int if_index;
83 }; // end class LinuxDetour::IfNameItem
84
85 bool SetIPTables(Action action,
86 int hookFlags ,
87 const ProtoAddress& srcFilterAddr,
88 unsigned int srcFilterMask,
89 const ProtoAddress& dstFilterAddr,
90 unsigned int dstFilterMask,
91 int dscpValue);
92
93 int pid;
94 int seq;
95 struct nlmsghdr nlh;
96 struct ipq_packet_msg pkt_msg;
97 int raw_fd; // for packet injection
98 int hook_flags;
99 ProtoAddress src_filter_addr;
100 unsigned int src_filter_mask;
101 ProtoAddress dst_filter_addr;
102 unsigned int dst_filter_mask;
103 int dscp_value;
104 ProtoTree if_name_tree;
105
106 }; // end class LinuxDetour
107
108 ProtoDetour* ProtoDetour::Create()
109 {
110 return static_cast<ProtoDetour*>(new LinuxDetour());
111 } // end ProtoDetour::Create(), (void*)nl_head
112
113
114 LinuxDetour::LinuxDetour()
115 : seq(0), raw_fd(-1), hook_flags(0), dscp_value(-1)
116 {
117
118 }
119
120 LinuxDetour::~LinuxDetour()
121 {
122 Close();
123 }
124
125
126 LinuxDetour::IfNameItem::IfNameItem()
127 : if_name(NULL), if_index(0)
128 {
129 }
130
131 bool LinuxDetour::IfNameItem::Init(const char* ifName, unsigned int ifIndex)
132 {
133 if (NULL != if_name) delete[] if_name;
134 size_t nameLen = strlen(ifName);
135 if (nameLen > IFNAMSIZ) nameLen = IFNAMSIZ;
136 nameLen++;
137 if (NULL == (if_name = new char[nameLen]))
138 {
139 PLOG(PL_ERROR, "LinuxDetour::IfNameItem::Init() new if_name error: %s\n", GetErrorString());
140 if_name_size = 0;
141 return false;
142 }
143 strcpy(if_name, ifName);
144 if_name_size = (nameLen - 1) << 3;
145 if_index = ifIndex;
146 return true;
147 } // end LinuxDetour::IfNameItem::Init()
148
149 LinuxDetour::IfNameItem::~IfNameItem()
150 {
151 if (NULL != if_name)
152 {
153 delete[] if_name;
154 if_name = NULL;
155 }
156 }
157
158 bool LinuxDetour::SetIPTables(Action action,
159 int hookFlags ,
160 const ProtoAddress& srcFilterAddr,
161 unsigned int srcFilterMask,
162 const ProtoAddress& dstFilterAddr,
163 unsigned int dstFilterMask,
164 int dscpValue)
165 {
166 // 1) IPv4 or IPv6 address family?
167 // (Note we now use the "mangle" table for our stuf)
168 const char* cmd;
169 if (srcFilterAddr.GetType() != dstFilterAddr.GetType())
170 {
171 PLOG(PL_ERROR, "LinuxDetour::SetIPTables() error: inconsistent src/dst filter addr families\n");
172 return false;
173 }
174 else if (ProtoAddress::IPv4 == srcFilterAddr.GetType())
175 {
176 cmd = "/sbin/iptables -t mangle"; // IPv4 iptables
177 }
178 else if (ProtoAddress::IPv6 == srcFilterAddr.GetType())
179 {
180 cmd = "/sbin/ip6tables -t mangle";
181 }
182 else
183 {
184 PLOG(PL_ERROR, "LinuxDetour::SetIPTables() error: unspecified filter addr family\n");
185 return false;
186 }
187 // 2) INSTALL ("-A") or DELETE ("-D") the rule
188 const char* mode = (INSTALL == action) ? "-A" : "-D";
189 // 3) For which firewall hooks?
190 while (0 != hookFlags)
191 {
192 const char* target;
193 if (0 != (hookFlags & OUTPUT))
194 {
195 target = "OUTPUT";
196 hookFlags &= ~OUTPUT;
197 }
198 else if (0 != (hookFlags & INPUT))
199 {
200 target = "PREROUTING"; // PREROUTING precedes INPUT in mangle table
201 hookFlags &= ~INPUT;
202 }
203 else if (0 != (hookFlags & FORWARD))
204 {
205 target = "FORWARD";
206 hookFlags &= ~FORWARD;
207 }
208 else
209 {
210 break; // all flags have been processed
211 }
212
213 // Make and install "iptables" firewall rules
214 const size_t RULE_MAX = 511;
215 char rule[RULE_MAX+1];
216 // cmd = "iptables" or "ip6tables"
217 // mode = "-I" or "-D"
218 // target = "INPUT", "OUTPUT", or "FORWARD"
219 sprintf(rule, "%s %s %s -j QUEUE ", cmd, mode, target);
220 if (0 != srcFilterMask)
221 {
222 strcat(rule, "-s ");
223 size_t len = strlen(rule);
224 if (!srcFilterAddr.GetHostString(rule+len, RULE_MAX - len))
225 {
226 PLOG(PL_ERROR, "LinuxDetour::SetIPTables() error: bad source addr filter\n");
227 return false;
228 }
229 len = strlen(rule);
230 sprintf(rule+len, "/%hu ", srcFilterMask);
231 }
232 if (0 != dstFilterMask)
233 {
234 strcat(rule, "-d ");
235 size_t len = strlen(rule);
236 if (!dstFilterAddr.GetHostString(rule+len, RULE_MAX - len))
237 {
238 PLOG(PL_ERROR, "LinuxDetour::SetIPTables() error: bad destination addr filter\n");
239 return false;
240 }
241 len = strlen(rule);
242 sprintf(rule+len, "/%hu ", dstFilterMask);
243 }
244 // Add DSCP filter command, if applicable
245 if (dscpValue >= 0)
246 {
247 // TBD - error check dscpValue???
248 size_t len = strlen(rule);
249 sprintf(rule+len, "-m dscp --dscp %d ", dscpValue);
250 dscp_value = dscpValue;
251 }
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 fread(errorMsg, 1, 256, p);
261 char* ptr = strchr(errorMsg, '\n');
262 if (NULL != ptr) *ptr = '\0';
263 errorMsg[255] = '\0';
264 if (0 != pclose(p))
265 {
266 PLOG(PL_ERROR, "LinuxDetour::SetIPTables() \"%s\" error: %s\n",
267 rule, errorMsg);
268 return false;
269 }
270 }
271 else
272 {
273 PLOG(PL_ERROR, "LinuxDetour::SetIPTables() error: popen(%s): %s\n",
274 rule, GetErrorString());
275 return false;
276 }
277 } // end while (0 != hookFlags)
278 return true;
279 } // end LinuxDetour::SetIPTables()
280
281 bool LinuxDetour::Open(int hookFlags,
282 const ProtoAddress& srcFilterAddr,
283 unsigned int srcFilterMask,
284 const ProtoAddress& dstFilterAddr,
285 unsigned int dstFilterMask,
286 int dscpValue)
287 {
288 if (IsOpen()) Close();
289
290 // 0) Open raw socket for optional packet injection use
291 //if (0 > (raw_fd = socket(domain, SOCK_RAW, IPPROTO_RAW)))
292 if (0 > (raw_fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) // or can we IPv6 on an AF_INET/HDRINCL socket?
293 {
294 PLOG(PL_ERROR, "LinuxDetour::Open() socket(IPPROTO_RAW) error: %s\n",
295 GetErrorString());
296 return false;
297 }
298 //if (AF_INET == domain)
299 {
300 // Note: no IP_HDRINCL for IPv6 raw sockets ?
301 int enable = 1;
302 if (setsockopt(raw_fd, IPPROTO_IP, IP_HDRINCL, &enable, sizeof(enable)))
303 {
304 PLOG(PL_ERROR, "LinuxDetour::Open() setsockopt(IP_HDRINCL) error: %s\n",
305 GetErrorString());
306 Close();
307 return false;
308 }
309 }
310 // Set to non-blocking for our purposes (TBD) Add a SetBlocking() method
311 if(-1 == fcntl(raw_fd, F_SETFL, fcntl(raw_fd, F_GETFL, 0) | O_NONBLOCK))
312 {
313 PLOG(PL_ERROR, "LinuxDetour::Open() fcntl(F_SETFL(O_NONBLOCK)) error: %s\n", GetErrorString());
314 Close();
315 return false;
316 }
317
318 // Check for "inject-only" mode and "rewire appropriately
319 if (0 != (hookFlags & INJECT))
320 {
321 descriptor = raw_fd;
322 raw_fd = -1;
323 ProtoDetour::Open();
324 StopInputNotification();
325 return true;
326 }
327
328 int domain;
329 if (srcFilterAddr.GetType() != dstFilterAddr.GetType())
330 {
331 PLOG(PL_ERROR, "LinuxDetour::Open() error: inconsistent src/dst filter addr families\n");
332 Close();
333 return false;
334 }
335 else if (ProtoAddress::IPv4 == srcFilterAddr.GetType())
336
337 {
338 domain = AF_INET;
339 }
340 else if (ProtoAddress::IPv6 == srcFilterAddr.GetType())
341 {
342 domain = AF_INET6;
343 }
344 else
345 {
346 PLOG(PL_ERROR, "LinuxDetour::Open() error: unspecified filter addr family\n");
347 Close();
348 return false;
349 }
350
351 // Make sure the "ip_queue" (or "ip6_queue") modules are loaded
352 // (We make this non-fatal in the case the operating system has
353 // these compiled-in and they are not used as loadable modules).
354 FILE* p;
355 if (AF_INET == domain)
356 p = popen("/sbin/modprobe ip_queue 2>&1", "r");
357 else
358 p = popen("/sbin/modprobe ip6_queue 2>&1", "r");
359 if (NULL != p)
360 {
361 // Read any error information fed back
362 char errorMsg[256];
363 fread(errorMsg, 1, 256, p);
364 errorMsg[255] = '\0';
365 if (0 != pclose(p))
366 PLOG(PL_ERROR, "LinuxDetour::Open() warning: \"/sbin/modprobe %s\" error: %s",
367 (AF_INET == domain) ? "ip_queue" : "ip6_queue", errorMsg);
368 }
369 else
370 {
371 PLOG(PL_ERROR, "LinuxDetour::Open() warning: popen(/sbin/modprobe) error: %s\n",
372 GetErrorString());
373 }
374
375 // Save parameters for firewall rule removal
376 hook_flags = hookFlags;
377 src_filter_addr = srcFilterAddr;
378 src_filter_mask = srcFilterMask;
379 dst_filter_addr = dstFilterAddr;
380 dst_filter_mask = dstFilterMask;
381 // Set up iptables (or ip6tables) if non-zero "hookFlags" are provided
382 if (0 != hookFlags)
383 {
384 if (!SetIPTables(INSTALL, hookFlags,
385 srcFilterAddr, srcFilterMask,
386 dstFilterAddr, dstFilterMask, dscpValue))
387 {
388 PLOG(PL_ERROR, "LinuxDetour::Open() error: couldn't install firewall rules\n");
389 Close();
390 return false;
391 }
392 }
393
394 // 1) Open netlink firewall socket
395 int protocol;
396 if (AF_INET == domain)
397 protocol = NETLINK_FIREWALL;
398 else
399 protocol = NETLINK_IP6_FW;
400 if ((descriptor = socket(PF_NETLINK, SOCK_RAW, protocol)) < 0)
401 {
402 PLOG(PL_ERROR, "LinuxDetour::Open() socket(NETLINK_FIREWALL) error: %s\n",
403 GetErrorString());
404 Close();
405 return false;
406 }
407 // 2) save our process id
408 pid = getpid();
409
410 // 3) Send a mode message
411 struct
412 {
413 struct nlmsghdr nlh;
414 ipq_mode_msg mode;
415 } req;
416 memset(&req, 0, sizeof(req));
417 req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req));
418 req.nlh.nlmsg_flags = NLM_F_REQUEST;
419 req.nlh.nlmsg_type = IPQM_MODE;
420 req.nlh.nlmsg_pid = pid;
421 req.mode.value = IPQ_COPY_PACKET;
422 req.mode.range = 0;
423
424 struct sockaddr_nl addr;
425 memset(&addr, 0, sizeof(struct sockaddr_nl));
426 addr.nl_family = AF_NETLINK;
427 addr.nl_pid = 0;
428 addr.nl_groups = 0;
429
430 if (sendto(descriptor, &req, req.nlh.nlmsg_len, 0,
431 (struct sockaddr*)&addr, sizeof(struct sockaddr_nl)) < 0)
432 {
433 PLOG(PL_ERROR, "LinuxDetour::Open() sendto() error: %s\n", GetErrorString());
434 Close();
435 return false;
436 }
437
438 if (!ProtoDetour::Open())
439 {
440 PLOG(PL_ERROR, "LinuxDetour::Open() ProtoChannel::Open() error\n");
441 Close();
442 return false;
443 }
444
445 // Get array of ifIndices to iterate over and create ifName->ifIndex ProtoTree
446 unsigned int ifIndexArray[256];
447 unsigned int ifCount = ProtoSocket::GetInterfaceIndices(ifIndexArray, 256);
448 if (ifCount < 0)
449 {
450 PLOG(PL_ERROR, "LinuxDetour::Open() warning: unable to retrieve list of network interface indices\n");
451 }
452 else if (0 == ifCount)
453 {
454 PLOG(PL_ERROR, "LinuxDetour::Open() warning: no network interface indices were found.\n");
455 }
456 else if (ifCount > 256)
457 {
458 PLOG(PL_ERROR, "LinuxDetour::Open() warning: found network interfaces indices exceeding maximum count of 256.\n");
459 ifCount = 256;
460 }
461 for (unsigned int i = 0; i < ifCount; i++)
462 {
463 char ifName[IFNAMSIZ+1];
464 ifName[IFNAMSIZ] = '\0';
465 if (ProtoSocket::GetInterfaceName(ifIndexArray[i], ifName, IFNAMSIZ))
466 {
467 IfNameItem* item = static_cast<IfNameItem*>(if_name_tree.Find(ifName, strlen(ifName) << 3));
468 if (NULL != item)
469 {
470 PLOG(PL_ERROR, "LinuxDetour::Open() warning: duplicate ifName?!\n");
471 }
472 else
473 {
474 if (NULL == (item = new IfNameItem()))
475 {
476 PLOG(PL_ERROR, "LinuxDetour::Open() new IfNameItem error: %s\n", GetErrorString());
477 Close();
478 return false;
479 }
480 if (!item->Init(ifName, ifIndexArray[i]))
481 {
482 PLOG(PL_ERROR, "LinuxDetour::Open() new IfNameItem error: %s\n", GetErrorString());
483 delete item;
484 Close();
485 return false;
486 }
487 if_name_tree.Insert(*item);
488 }
489 }
490 else
491 {
492 PLOG(PL_ERROR, "LinuxDetour::Open() warning: failed to get ifName for ifIndex:%d\n", ifIndexArray[i]);
493 }
494 }
495 return true;
496 } // end LinuxDetour::Open()
497
498 void LinuxDetour::Close()
499 {
500 // Empty our ifName->ifIndex tree
501 ProtoTree::Item* rootItem;
502 while (NULL != (rootItem = if_name_tree.GetRoot()))
503 {
504 if_name_tree.Remove(*rootItem);
505 delete rootItem;
506 }
507
508 if (raw_fd >= 0)
509 {
510 close(raw_fd);
511 raw_fd = -1;
512 }
513 if (0 != hook_flags)
514 {
515 SetIPTables(DELETE, hook_flags,
516 src_filter_addr, src_filter_mask,
517 dst_filter_addr, dst_filter_mask, dscp_value);
518 hook_flags = 0;
519 }
520 if (descriptor >= 0)
521 {
522 ProtoDetour::Close();
523 close(descriptor);
524 descriptor = INVALID_HANDLE;
525 }
526 } // end LinuxDetour::Close()
527
528 bool LinuxDetour::Recv(char* buffer,
529 unsigned int& numBytes,
530 Direction* direction,
531 ProtoAddress* srcMac,
532 unsigned int* ifIndex)
533 {
534 if (NULL != srcMac) srcMac->Invalidate();
535 struct iovec iov[3];
536 iov[0].iov_base = &nlh;
537 iov[0].iov_len = sizeof(nlh);
538 iov[1].iov_base = &pkt_msg;
539 iov[1].iov_len = sizeof(pkt_msg);
540 iov[2].iov_base = buffer;
541 iov[2].iov_len = numBytes;
542 struct sockaddr_nl addr;
543 struct msghdr msg;
544 msg.msg_name = (void*)&addr;
545 msg.msg_namelen = sizeof(struct sockaddr_nl);
546 msg.msg_iov = iov;
547 msg.msg_iovlen = 3;
548 msg.msg_control = NULL;
549 msg.msg_controllen = 0;
550 size_t result = recvmsg(descriptor, &msg, 0);
551 if (result < 0)
552 {
553 numBytes = 0;
554 if ((EAGAIN == errno) || (EINTR == errno))
555 {
556 return true;
557 }
558 else
559 {
560 PLOG(PL_ERROR, "LinuxDetour::Recv() recvfrom() error: %s\n", GetErrorString());
561 return false;
562 }
563 }
564 else if (IPQM_PACKET != nlh.nlmsg_type)
565 {
566 numBytes = 0;
567 return true;
568 }
569 else
570 {
571 if (NULL != direction)
572 {
573 switch (pkt_msg.hook)
574 {
575 case NF_IP_LOCAL_OUT:
576 //case NF_IP6_LOCAL_OUT:
577 *direction = OUTBOUND; // locally-generated packet
578 break;
579 default:
580 *direction = INBOUND; // packet from somewhere else
581 break; // (assume it is INBOUND)
582 }
583 }
584 if ((NULL != srcMac) &&
585 (ARPHRD_ETHER == pkt_msg.hw_type) &&
586 (ETH_ALEN == pkt_msg.hw_addrlen))
587 {
588 srcMac->SetRawHostAddress(ProtoAddress::ETH, (char*)pkt_msg.hw_addr, ETH_ALEN);
589 }
590 numBytes = pkt_msg.data_len;
591 if (NULL != ifIndex)
592 {
593 // Attempt to fill in "ifIndex"
594 if (NF_IP_LOCAL_OUT == pkt_msg.hook)
595 {
596 // OUTBOUND packet so set "ifIndex" to pkt_msg.outdev_name
597 size_t nameLen = strlen(pkt_msg.outdev_name);
598 if (nameLen > IFNAMSIZ) nameLen = IFNAMSIZ;
599 IfNameItem* item = static_cast<IfNameItem*>(if_name_tree.Find(pkt_msg.outdev_name, nameLen << 3));
600 if (NULL != item)
601 {
602 *ifIndex = item->GetIfIndex();
603 }
604 else
605 {
606 PLOG(PL_ERROR, "LinuxDetour::Recv() warning: OUTBOUND packet to unknown ifName: %s\n", pkt_msg.outdev_name);
607 *ifIndex = 0;
608 }
609 }
610 else
611 {
612 // Assume INBOUND packet so set "ifIndex" to pkt_msg.indev_name
613 size_t nameLen = strlen(pkt_msg.indev_name);
614 if (nameLen > IFNAMSIZ) nameLen = IFNAMSIZ;
615 IfNameItem* item = static_cast<IfNameItem*>(if_name_tree.FindClosestMatch(pkt_msg.indev_name, nameLen << 3));
616 if (NULL != item)
617 {
618 *ifIndex = item->GetIfIndex();
619 }
620 else
621 {
622 PLOG(PL_ERROR, "LinuxDetour::Recv() warning: INBOUND packet from unknown ifName: %s\n", pkt_msg.indev_name);
623 *ifIndex = 0;
624 }
625 }
626 }
627 return true;
628 }
629 } // end LinuxDetour::Recv()
630
631 bool LinuxDetour::Allow(const char* buffer, unsigned int numBytes)
632 {
633 struct
634 {
635 struct nlmsghdr nlh;
636 struct ipq_verdict_msg verdict;
637 } req;
638 memset(&req, 0, sizeof(req));
639 req.nlh.nlmsg_flags = NLM_F_REQUEST;
640 req.nlh.nlmsg_type = IPQM_VERDICT;
641 req.nlh.nlmsg_pid = pid;
642 req.verdict.value = NF_ACCEPT;
643 req.verdict.id = pkt_msg.packet_id; // filled in from last Recv()
644 req.verdict.data_len = numBytes;
645
646 struct iovec iov[3];
647 iov[0].iov_base = &req.nlh;
648 iov[0].iov_len = sizeof(struct nlmsghdr);
649 iov[1].iov_base = &req.verdict;
650 iov[1].iov_len = sizeof(struct ipq_verdict_msg);
651 iov[2].iov_base = (void*)buffer;
652 iov[2].iov_len = numBytes;
653 req.nlh.nlmsg_len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
654
655 struct sockaddr_nl addr;
656 memset(&addr, 0, sizeof(struct sockaddr_nl));
657 addr.nl_family = AF_NETLINK;
658 addr.nl_pid = 0;
659 addr.nl_groups = 0;
660
661 struct msghdr msg;
662 msg.msg_name = (void*)&addr;
663 msg.msg_namelen = sizeof(struct sockaddr_nl);
664 msg.msg_iov = iov;
665 msg.msg_iovlen = 3;
666 msg.msg_control = NULL;
667 msg.msg_controllen = 0;
668
669 if (sendmsg(descriptor, &msg, 0) < 0)
670 {
671 PLOG(PL_ERROR, "LinuxDetour::Allow() sendmsg() error: %s\n", GetErrorString());
672 return false;
673 }
674 return true;
675 } // end LinuxDetour::Allow()
676
677 bool LinuxDetour::Drop()
678 {
679 struct
680 {
681 struct nlmsghdr nlh;
682 struct ipq_verdict_msg verdict;
683 } req;
684 memset(&req, 0, sizeof(req));
685 req.nlh.nlmsg_flags = NLM_F_REQUEST;
686 req.nlh.nlmsg_type = IPQM_VERDICT;
687 req.nlh.nlmsg_pid = pid;
688 req.verdict.value = NF_DROP;
689 req.verdict.id = pkt_msg.packet_id; // filled in from last Recv()
690 req.verdict.data_len = 0;
691
692 struct iovec iov[2];
693 iov[0].iov_base = &req.nlh;
694 iov[0].iov_len = sizeof(struct nlmsghdr);
695 iov[1].iov_base = &req.verdict;
696 iov[1].iov_len = sizeof(struct ipq_verdict_msg);
697 req.nlh.nlmsg_len = iov[0].iov_len + iov[1].iov_len;
698
699 struct sockaddr_nl addr;
700 memset(&addr, 0, sizeof(struct sockaddr_nl));
701 addr.nl_family = AF_NETLINK;
702 addr.nl_pid = 0;
703 addr.nl_groups = 0;
704
705 struct msghdr msg;
706 msg.msg_name = (void*)&addr;
707 msg.msg_namelen = sizeof(struct sockaddr_nl);
708 msg.msg_iov = iov;
709 msg.msg_iovlen = 2;
710 msg.msg_control = NULL;
711 msg.msg_controllen = 0;
712
713 if (sendmsg(descriptor, &msg, 0) < 0)
714 {
715 PLOG(PL_ERROR, "LinuxDetour::Drop() sendmsg() error: %s\n", GetErrorString());
716 return false;
717 }
718 return true;
719 } // end LinuxDetour::Drop()
720
721 bool LinuxDetour::Inject(const char* buffer, unsigned int numBytes)
722 {
723 unsigned char version = buffer[0];
724 version >>= 4;
725 ProtoAddress dstAddr;
726 socklen_t addrLen;
727 if (4 == version)
728 {
729 dstAddr.SetRawHostAddress(ProtoAddress::IPv4, buffer+16, 4);
730 addrLen = sizeof(struct sockaddr);
731 }
732 else if (6 == version)
733 {
734 PLOG(PL_ERROR, "LinuxDetour::Inject() IPv6 injection not yet supported!\n");
735 return false;
736 }
737 else
738 {
739 PLOG(PL_ERROR, "LinuxDetour::Inject() unknown IP version!\n");
740 return false;
741 }
742 int fd = (raw_fd < 0) ? descriptor : raw_fd;
743 size_t result = sendto(fd, buffer, numBytes, 0,
744 &dstAddr.GetSockAddr(), addrLen);
745 if (result != numBytes)
746 {
747 PLOG(PL_ERROR, "LinuxDetour::Inject() sendto() error: %s\n", GetErrorString());
748 return false;
749 }
750 return true;
751 } // end LinuxDetour::Inject()
752
753 bool LinuxDetour::SetMulticastInterface(const char* interfaceName)
754 {
755 int fd = (raw_fd < 0) ? descriptor : raw_fd;
756 if (fd < 0)
757 {
758 PLOG(PL_ERROR, "LinuxDetour::SetMulticastInterface() error: detour not open\n");
759 return false;
760 }
761
762 if (interfaceName)
763 {
764 int result;
765 #ifdef HAVE_IPV6
766 if (ProtoAddress::IPv6 == src_filter_addr.GetType())
767 {
768 unsigned int interfaceIndex = ProtoSocket::GetInterfaceIndex(interfaceName);
769 result = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
770 (char*)&interfaceIndex, sizeof(interfaceIndex));
771 }
772 else
773 #endif // HAVE_IPV6
774 {
775 struct in_addr localAddr;
776 ProtoAddress interfaceAddress;
777 if (ProtoSocket::GetInterfaceAddress(interfaceName, ProtoAddress::IPv4, interfaceAddress))
778 {
779 localAddr.s_addr = htonl(interfaceAddress.IPv4GetAddress());
780 }
781 else
782 {
783 PLOG(PL_ERROR, "LinuxDetour::SetMulticastInterface() invalid interface name\n");
784 return false;
785 }
786 result = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (char*)&localAddr,
787 sizeof(localAddr));
788 }
789 if (result < 0)
790 {
791 PLOG(PL_ERROR, "LinuxDetour: setsockopt(IP_MULTICAST_IF) error: %s\n",
792 GetErrorString());
793 return false;
794 }
795 }
796 return true;
797 } // end LinuxDetour::SetMulticastInterface()