Initial commit after restructure
This commit is contained in:
201
firewall/interception/nfqueue/LICENSE
Normal file
201
firewall/interception/nfqueue/LICENSE
Normal file
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
42
firewall/interception/nfqueue/README.md
Normal file
42
firewall/interception/nfqueue/README.md
Normal file
@@ -0,0 +1,42 @@
|
||||
Go-NFQueue
|
||||
==========
|
||||
Go Wrapper For Creating IPTables' NFQueue clients in Go
|
||||
|
||||
Usage
|
||||
------
|
||||
Check the `examples/main.go` file
|
||||
|
||||
```bash
|
||||
cd $GOPATH/github.com/OneOfOne/go-nfqueue/examples
|
||||
go build -race && sudo ./examples
|
||||
```
|
||||
* Open another terminal :
|
||||
```bash
|
||||
sudo iptables -I INPUT 1 -m conntrack --ctstate NEW -j NFQUEUE --queue-num 0
|
||||
#or
|
||||
sudo iptables -I INPUT -i eth0 -m conntrack --ctstate NEW -j NFQUEUE --queue-num 0
|
||||
curl --head localhost
|
||||
ping localhost
|
||||
sudo iptables -D INPUT -m conntrack --ctstate NEW -j NFQUEUE --queue-num 0
|
||||
```
|
||||
Then you can `ctrl+c` the program to exit.
|
||||
|
||||
* If you have recent enough iptables/nfqueue you could also use a balanced (multithreaded queue).
|
||||
* check the example in `examples/mq/multiqueue.go`
|
||||
|
||||
```bash
|
||||
iptables -I INPUT 1 -m conntrack --ctstate NEW -j NFQUEUE --queue-balance 0:5 --queue-cpu-fanout
|
||||
```
|
||||
Notes
|
||||
-----
|
||||
|
||||
You must run the executable as root.
|
||||
This is *WIP*, but all patches are welcome.
|
||||
|
||||
License
|
||||
-------
|
||||
go-nfqueue is under the Apache v2 license, check the included license file.
|
||||
Copyright © [Ahmed W.](http://www.limitlessfx.com/)
|
||||
See the included `LICENSE` file.
|
||||
|
||||
> Copyright (c) 2014 Ahmed W.
|
||||
46
firewall/interception/nfqueue/multiqueue.go
Normal file
46
firewall/interception/nfqueue/multiqueue.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright Safing ICS Technologies GmbH. Use of this source code is governed by the AGPL license that can be found in the LICENSE file.
|
||||
|
||||
package nfqueue
|
||||
|
||||
import (
|
||||
"github.com/Safing/safing-core/network/packet"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type multiQueue struct {
|
||||
qs []*nfQueue
|
||||
}
|
||||
|
||||
func NewMultiQueue(min, max uint16) (mq *multiQueue) {
|
||||
mq = &multiQueue{make([]*nfQueue, 0, max-min)}
|
||||
for i := min; i < max; i++ {
|
||||
mq.qs = append(mq.qs, NewNFQueue(i))
|
||||
}
|
||||
return mq
|
||||
}
|
||||
|
||||
func (mq *multiQueue) Process() <-chan packet.Packet {
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
out = make(chan packet.Packet, len(mq.qs))
|
||||
)
|
||||
for _, q := range mq.qs {
|
||||
wg.Add(1)
|
||||
go func(ch <-chan packet.Packet) {
|
||||
for pkt := range ch {
|
||||
out <- pkt
|
||||
}
|
||||
wg.Done()
|
||||
}(q.Process())
|
||||
}
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(out)
|
||||
}()
|
||||
return out
|
||||
}
|
||||
func (mq *multiQueue) Destroy() {
|
||||
for _, q := range mq.qs {
|
||||
q.Destroy()
|
||||
}
|
||||
}
|
||||
88
firewall/interception/nfqueue/nfqueue.c
Normal file
88
firewall/interception/nfqueue/nfqueue.c
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "nfqueue.h"
|
||||
#include "_cgo_export.h"
|
||||
|
||||
|
||||
int nfqueue_cb_new(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfa, void *data) {
|
||||
|
||||
struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(nfa);
|
||||
|
||||
if(ph == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int id = ntohl(ph->packet_id);
|
||||
|
||||
unsigned char * payload;
|
||||
unsigned char * saddr, * daddr;
|
||||
uint16_t sport = 0, dport = 0, checksum = 0;
|
||||
uint32_t mark = nfq_get_nfmark(nfa);
|
||||
|
||||
int len = nfq_get_payload(nfa, &payload);
|
||||
|
||||
unsigned char * origpayload = payload;
|
||||
int origlen = len;
|
||||
|
||||
if(len < sizeof(struct iphdr)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct iphdr * ip = (struct iphdr *) payload;
|
||||
|
||||
if(ip->version == 4) {
|
||||
uint32_t ipsz = (ip->ihl << 2);
|
||||
if(len < ipsz) {
|
||||
return 0;
|
||||
}
|
||||
len -= ipsz;
|
||||
payload += ipsz;
|
||||
|
||||
saddr = (unsigned char *)&ip->saddr;
|
||||
daddr = (unsigned char *)&ip->daddr;
|
||||
|
||||
if(ip->protocol == IPPROTO_TCP) {
|
||||
if(len < sizeof(struct tcphdr)) {
|
||||
return 0;
|
||||
}
|
||||
struct tcphdr *tcp = (struct tcphdr *) payload;
|
||||
uint32_t tcpsz = (tcp->doff << 2);
|
||||
if(len < tcpsz) {
|
||||
return 0;
|
||||
}
|
||||
len -= tcpsz;
|
||||
payload += tcpsz;
|
||||
|
||||
sport = ntohs(tcp->source);
|
||||
dport = ntohs(tcp->dest);
|
||||
checksum = ntohs(tcp->check);
|
||||
} else if(ip->protocol == IPPROTO_UDP) {
|
||||
if(len < sizeof(struct udphdr)) {
|
||||
return 0;
|
||||
}
|
||||
struct udphdr *u = (struct udphdr *) payload;
|
||||
len -= sizeof(struct udphdr);
|
||||
payload += sizeof(struct udphdr);
|
||||
|
||||
sport = ntohs(u->source);
|
||||
dport = ntohs(u->dest);
|
||||
checksum = ntohs(u->check);
|
||||
}
|
||||
} else {
|
||||
struct ipv6hdr *ip6 = (struct ipv6hdr*) payload;
|
||||
saddr = (unsigned char *)&ip6->saddr;
|
||||
daddr = (unsigned char *)&ip6->daddr;
|
||||
//ipv6
|
||||
}
|
||||
//pass everything we can and let Go handle it, I'm not a big fan of C
|
||||
uint32_t verdict = go_nfq_callback(id, ntohs(ph->hw_protocol), ph->hook, &mark, ip->version, ip->protocol,
|
||||
ip->tos, ip->ttl, saddr, daddr, sport, dport, checksum, origlen, origpayload, data);
|
||||
return nfq_set_verdict2(qh, id, verdict, mark, 0, NULL);
|
||||
}
|
||||
|
||||
void loop_for_packets(struct nfq_handle *h) {
|
||||
int fd = nfq_fd(h);
|
||||
char buf[65535] __attribute__ ((aligned));
|
||||
int rv;
|
||||
while ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0) {
|
||||
nfq_handle_packet(h, buf, rv);
|
||||
}
|
||||
}
|
||||
203
firewall/interception/nfqueue/nfqueue.go
Normal file
203
firewall/interception/nfqueue/nfqueue.go
Normal file
@@ -0,0 +1,203 @@
|
||||
// Copyright Safing ICS Technologies GmbH. Use of this source code is governed by the AGPL license that can be found in the LICENSE file.
|
||||
|
||||
package nfqueue
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lnetfilter_queue
|
||||
#cgo CFLAGS: -Wall
|
||||
#include "nfqueue.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/Safing/safing-core/network/packet"
|
||||
)
|
||||
|
||||
var queues map[uint16]*nfQueue
|
||||
|
||||
func init() {
|
||||
queues = make(map[uint16]*nfQueue)
|
||||
}
|
||||
|
||||
type nfQueue struct {
|
||||
DefaultVerdict uint32
|
||||
Timeout time.Duration
|
||||
qid uint16
|
||||
qidptr *uint16
|
||||
h *C.struct_nfq_handle
|
||||
//qh *C.struct_q_handle
|
||||
qh *C.struct_nfq_q_handle
|
||||
fd int
|
||||
lk sync.Mutex
|
||||
|
||||
pktch chan packet.Packet
|
||||
}
|
||||
|
||||
func NewNFQueue(qid uint16) (nfq *nfQueue) {
|
||||
if os.Geteuid() != 0 {
|
||||
panic("Must be ran by root.")
|
||||
}
|
||||
nfq = &nfQueue{DefaultVerdict: NFQ_ACCEPT, Timeout: 100 * time.Millisecond, qid: qid, qidptr: &qid}
|
||||
queues[nfq.qid] = nfq
|
||||
return nfq
|
||||
}
|
||||
|
||||
/*
|
||||
This returns a channel that will recieve packets,
|
||||
the user then must call pkt.Accept() or pkt.Drop()
|
||||
*/
|
||||
func (this *nfQueue) Process() <-chan packet.Packet {
|
||||
if this.h != nil {
|
||||
return this.pktch
|
||||
}
|
||||
this.init()
|
||||
|
||||
go func() {
|
||||
runtime.LockOSThread()
|
||||
C.loop_for_packets(this.h)
|
||||
}()
|
||||
|
||||
return this.pktch
|
||||
}
|
||||
|
||||
func (this *nfQueue) init() {
|
||||
var err error
|
||||
if this.h, err = C.nfq_open(); err != nil || this.h == nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
//if this.qh, err = C.nfq_create_queue(this.h, qid, C.get_cb(), unsafe.Pointer(nfq)); err != nil || this.qh == nil {
|
||||
|
||||
this.pktch = make(chan packet.Packet, 1)
|
||||
|
||||
if C.nfq_unbind_pf(this.h, C.AF_INET) < 0 {
|
||||
this.Destroy()
|
||||
panic("nfq_unbind_pf(AF_INET) failed, are you running root?.")
|
||||
}
|
||||
if C.nfq_unbind_pf(this.h, C.AF_INET6) < 0 {
|
||||
this.Destroy()
|
||||
panic("nfq_unbind_pf(AF_INET6) failed.")
|
||||
}
|
||||
|
||||
if C.nfq_bind_pf(this.h, C.AF_INET) < 0 {
|
||||
this.Destroy()
|
||||
panic("nfq_bind_pf(AF_INET) failed.")
|
||||
}
|
||||
|
||||
if C.nfq_bind_pf(this.h, C.AF_INET6) < 0 {
|
||||
this.Destroy()
|
||||
panic("nfq_bind_pf(AF_INET6) failed.")
|
||||
}
|
||||
|
||||
if this.qh, err = C.create_queue(this.h, C.uint16_t(this.qid)); err != nil || this.qh == nil {
|
||||
C.nfq_close(this.h)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
this.fd = int(C.nfq_fd(this.h))
|
||||
|
||||
if C.nfq_set_mode(this.qh, C.NFQNL_COPY_PACKET, 0xffff) < 0 {
|
||||
this.Destroy()
|
||||
panic("nfq_set_mode(NFQNL_COPY_PACKET) failed.")
|
||||
}
|
||||
if C.nfq_set_queue_maxlen(this.qh, 1024*8) < 0 {
|
||||
this.Destroy()
|
||||
panic("nfq_set_queue_maxlen(1024 * 8) failed.")
|
||||
}
|
||||
}
|
||||
|
||||
func (this *nfQueue) Destroy() {
|
||||
this.lk.Lock()
|
||||
defer this.lk.Unlock()
|
||||
|
||||
if this.fd != 0 && this.Valid() {
|
||||
syscall.Close(this.fd)
|
||||
}
|
||||
if this.qh != nil {
|
||||
C.nfq_destroy_queue(this.qh)
|
||||
this.qh = nil
|
||||
}
|
||||
if this.h != nil {
|
||||
C.nfq_close(this.h)
|
||||
this.h = nil
|
||||
}
|
||||
|
||||
// TODO: don't close, we're exiting anyway
|
||||
// if this.pktch != nil {
|
||||
// close(this.pktch)
|
||||
// }
|
||||
}
|
||||
|
||||
func (this *nfQueue) Valid() bool {
|
||||
return this.h != nil && this.qh != nil
|
||||
}
|
||||
|
||||
//export go_nfq_callback
|
||||
func go_nfq_callback(id uint32, hwproto uint16, hook uint8, mark *uint32,
|
||||
version, protocol, tos, ttl uint8, saddr, daddr unsafe.Pointer,
|
||||
sport, dport, checksum uint16, payload_len uint32, payload, data unsafe.Pointer) (v uint32) {
|
||||
|
||||
qidptr := (*uint16)(data)
|
||||
qid := uint16(*qidptr)
|
||||
|
||||
// nfq := (*nfQueue)(nfqptr)
|
||||
new_version := version
|
||||
ipver := packet.IPVersion(new_version)
|
||||
ipsz := C.int(ipver.ByteSize())
|
||||
bs := C.GoBytes(payload, (C.int)(payload_len))
|
||||
|
||||
verdict := make(chan uint32, 1)
|
||||
pkt := Packet{
|
||||
QueueId: qid,
|
||||
Id: id,
|
||||
HWProtocol: hwproto,
|
||||
Hook: hook,
|
||||
Mark: *mark,
|
||||
verdict: verdict,
|
||||
// StartedHandling: time.Now(),
|
||||
}
|
||||
|
||||
// Payload
|
||||
pkt.Payload = bs
|
||||
|
||||
// IPHeader
|
||||
pkt.IPHeader = &packet.IPHeader{
|
||||
Version: 4,
|
||||
Protocol: packet.IPProtocol(protocol),
|
||||
Tos: tos,
|
||||
TTL: ttl,
|
||||
Src: net.IP(C.GoBytes(saddr, ipsz)),
|
||||
Dst: net.IP(C.GoBytes(daddr, ipsz)),
|
||||
}
|
||||
|
||||
// TCPUDPHeader
|
||||
pkt.TCPUDPHeader = &packet.TCPUDPHeader{
|
||||
SrcPort: sport,
|
||||
DstPort: dport,
|
||||
Checksum: checksum,
|
||||
}
|
||||
|
||||
// fmt.Printf("%s queuing packet\n", time.Now().Format("060102 15:04:05.000"))
|
||||
// BUG: "panic: send on closed channel" when shutting down
|
||||
queues[qid].pktch <- &pkt
|
||||
|
||||
select {
|
||||
case v = <-pkt.verdict:
|
||||
*mark = pkt.Mark
|
||||
// *mark = 1710
|
||||
case <-time.After(queues[qid].Timeout):
|
||||
v = queues[qid].DefaultVerdict
|
||||
}
|
||||
|
||||
// log.Tracef("nfqueue: took %s to handle packet", time.Now().Sub(pkt.StartedHandling).String())
|
||||
|
||||
return v
|
||||
}
|
||||
30
firewall/interception/nfqueue/nfqueue.h
Normal file
30
firewall/interception/nfqueue/nfqueue.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
// #define _BSD_SOURCE
|
||||
// #define __BSD_SOURCE
|
||||
|
||||
// #define __FAVOR_BSD // Just Using _BSD_SOURCE didn't work on my system for some reason
|
||||
// #define __USE_BSD
|
||||
#include <stdlib.h>
|
||||
// #include <sys/socket.h>
|
||||
// #include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <linux/ip.h>
|
||||
#include <linux/tcp.h>
|
||||
#include <linux/udp.h>
|
||||
#include <linux/ipv6.h>
|
||||
// #include <linux/netfilter.h>
|
||||
#include <libnetfilter_queue/libnetfilter_queue.h>
|
||||
|
||||
// extern int nfq_callback(uint8_t version, uint8_t protocol, unsigned char *saddr, unsigned char *daddr,
|
||||
// uint16_t sport, uint16_t dport, unsigned char * extra, void* data);
|
||||
|
||||
int nfqueue_cb_new(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfa, void *data);
|
||||
void loop_for_packets(struct nfq_handle *h);
|
||||
|
||||
static inline struct nfq_q_handle * create_queue(struct nfq_handle *h, uint16_t qid) {
|
||||
//we use this because it's more convient to pass the callback in C
|
||||
// FIXME: check malloc success
|
||||
uint16_t *data = malloc(sizeof(uint16_t));
|
||||
*data = qid;
|
||||
return nfq_create_queue(h, qid, &nfqueue_cb_new, (void*)data);
|
||||
}
|
||||
116
firewall/interception/nfqueue/packet.go
Normal file
116
firewall/interception/nfqueue/packet.go
Normal file
@@ -0,0 +1,116 @@
|
||||
// Copyright Safing ICS Technologies GmbH. Use of this source code is governed by the AGPL license that can be found in the LICENSE file.
|
||||
|
||||
package nfqueue
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Safing/safing-core/network/packet"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrVerdictSentOrTimedOut error = fmt.Errorf("The verdict was already sent or timed out.")
|
||||
)
|
||||
|
||||
const (
|
||||
NFQ_DROP uint32 = 0 // discarded the packet
|
||||
NFQ_ACCEPT uint32 = 1 // the packet passes, continue iterations
|
||||
NFQ_STOLEN uint32 = 2 // gone away
|
||||
NFQ_QUEUE uint32 = 3 // inject the packet into a different queue (the target queue number is in the high 16 bits of the verdict)
|
||||
NFQ_REPEAT uint32 = 4 // iterate the same cycle once more
|
||||
NFQ_STOP uint32 = 5 // accept, but don't continue iterations
|
||||
)
|
||||
|
||||
type Packet struct {
|
||||
packet.PacketBase
|
||||
|
||||
QueueId uint16
|
||||
Id uint32
|
||||
HWProtocol uint16
|
||||
Hook uint8
|
||||
Mark uint32
|
||||
|
||||
// StartedHandling time.Time
|
||||
|
||||
verdict chan uint32
|
||||
}
|
||||
|
||||
// func (pkt *Packet) String() string {
|
||||
// return fmt.Sprintf("<Packet QId: %d, Id: %d, Type: %s, Src: %s:%d, Dst: %s:%d, Mark: 0x%X, Checksum: 0x%X, TOS: 0x%X, TTL: %d>",
|
||||
// pkt.QueueId, pkt.Id, pkt.Protocol, pkt.Src, pkt.SrcPort, pkt.Dst, pkt.DstPort, pkt.Mark, pkt.Checksum, pkt.Tos, pkt.TTL)
|
||||
// }
|
||||
|
||||
func (pkt *Packet) setVerdict(v uint32) (err error) {
|
||||
defer func() {
|
||||
if x := recover(); x != nil {
|
||||
err = ErrVerdictSentOrTimedOut
|
||||
}
|
||||
}()
|
||||
pkt.verdict <- v
|
||||
close(pkt.verdict)
|
||||
// log.Tracef("firewall: packet %s verdict %d", pkt, v)
|
||||
return err
|
||||
}
|
||||
|
||||
// Marks:
|
||||
// 17: Identifier
|
||||
// 0/1: Just this packet/this Link
|
||||
// 0/1/2: Accept, Block, Drop
|
||||
|
||||
// func (pkt *Packet) Accept() error {
|
||||
// return pkt.setVerdict(NFQ_STOP)
|
||||
// }
|
||||
//
|
||||
// func (pkt *Packet) Block() error {
|
||||
// pkt.Mark = 1701
|
||||
// return pkt.setVerdict(NFQ_ACCEPT)
|
||||
// }
|
||||
//
|
||||
// func (pkt *Packet) Drop() error {
|
||||
// return pkt.setVerdict(NFQ_DROP)
|
||||
// }
|
||||
|
||||
func (pkt *Packet) Accept() error {
|
||||
pkt.Mark = 1700
|
||||
return pkt.setVerdict(NFQ_ACCEPT)
|
||||
}
|
||||
|
||||
func (pkt *Packet) Block() error {
|
||||
pkt.Mark = 1701
|
||||
return pkt.setVerdict(NFQ_ACCEPT)
|
||||
}
|
||||
|
||||
func (pkt *Packet) Drop() error {
|
||||
pkt.Mark = 1702
|
||||
return pkt.setVerdict(NFQ_ACCEPT)
|
||||
}
|
||||
|
||||
func (pkt *Packet) PermanentAccept() error {
|
||||
pkt.Mark = 1710
|
||||
return pkt.setVerdict(NFQ_ACCEPT)
|
||||
}
|
||||
|
||||
func (pkt *Packet) PermanentBlock() error {
|
||||
pkt.Mark = 1711
|
||||
return pkt.setVerdict(NFQ_ACCEPT)
|
||||
}
|
||||
|
||||
func (pkt *Packet) PermanentDrop() error {
|
||||
pkt.Mark = 1712
|
||||
return pkt.setVerdict(NFQ_ACCEPT)
|
||||
}
|
||||
|
||||
func (pkt *Packet) RerouteToNameserver() error {
|
||||
pkt.Mark = 1799
|
||||
return pkt.setVerdict(NFQ_ACCEPT)
|
||||
}
|
||||
|
||||
func (pkt *Packet) RerouteToTunnel() error {
|
||||
pkt.Mark = 1717
|
||||
return pkt.setVerdict(NFQ_ACCEPT)
|
||||
}
|
||||
|
||||
//HUGE warning, if the iptables rules aren't set correctly this can cause some problems.
|
||||
// func (pkt *Packet) Repeat() error {
|
||||
// return this.SetVerdict(REPEAT)
|
||||
// }
|
||||
Reference in New Issue
Block a user