|
| 1 | +/* |
| 2 | + Copyright 2014 Krishna Raman <[email protected]> |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/* |
| 18 | +Go bindings for libnetfilter_queue |
| 19 | +
|
| 20 | +This library provides access to packets in the IPTables netfilter queue (NFQUEUE). |
| 21 | +The libnetfilter_queue library is part of the http://netfilter.org/projects/libnetfilter_queue/ project. |
| 22 | +*/ |
| 23 | +package netfilter |
| 24 | + |
| 25 | +/* |
| 26 | +#cgo pkg-config: libnetfilter_queue |
| 27 | +#cgo CFLAGS: -Wall -Werror -I/usr/include |
| 28 | +#cgo LDFLAGS: -L/usr/lib64/ |
| 29 | +
|
| 30 | +#include "netfilter.h" |
| 31 | +*/ |
| 32 | +import "C" |
| 33 | + |
| 34 | +import ( |
| 35 | + "code.google.com/p/gopacket" |
| 36 | + "code.google.com/p/gopacket/layers" |
| 37 | + "fmt" |
| 38 | + "unsafe" |
| 39 | +) |
| 40 | + |
| 41 | +type NFPacket struct { |
| 42 | + Packet gopacket.Packet |
| 43 | + verdictChannel chan Verdict |
| 44 | +} |
| 45 | + |
| 46 | +//Set the verdict for the packet |
| 47 | +func (p *NFPacket) SetVerdict(v Verdict) { |
| 48 | + p.verdictChannel <- v |
| 49 | +} |
| 50 | + |
| 51 | +type NFQueue struct { |
| 52 | + h *[0]byte |
| 53 | + qh *[0]byte |
| 54 | + fd C.int |
| 55 | + packets chan NFPacket |
| 56 | +} |
| 57 | + |
| 58 | +//Verdict for a packet |
| 59 | +type Verdict C.int |
| 60 | + |
| 61 | +const ( |
| 62 | + AF_INET = 2 |
| 63 | + |
| 64 | + //Discarded the packet |
| 65 | + NF_DROP Verdict = 0 |
| 66 | + |
| 67 | + //The packet passes, continue iterations |
| 68 | + NF_ACCEPT Verdict = 1 |
| 69 | + |
| 70 | + NF_DEFAULT_PACKET_SIZE uint32 = 0xffff |
| 71 | +) |
| 72 | + |
| 73 | +//Create and bind to queue specified by queueId |
| 74 | +func NewNFQueue(queueId int, maxPacketsInQueue uint32, packetSize uint32) (*NFQueue, error) { |
| 75 | + var nfq = NFQueue{} |
| 76 | + var err error |
| 77 | + var ret C.int |
| 78 | + |
| 79 | + if nfq.h, err = C.nfq_open(); err != nil { |
| 80 | + return nil, fmt.Errorf("Error opening NFQueue handle: %v\n", err) |
| 81 | + } |
| 82 | + |
| 83 | + if ret, err = C.nfq_unbind_pf(nfq.h, AF_INET); err != nil || ret < 0 { |
| 84 | + return nil, fmt.Errorf("Error unbinding existing NFQ handler from AF_INET protocol family: %v\n", err) |
| 85 | + } |
| 86 | + |
| 87 | + if ret, err := C.nfq_bind_pf(nfq.h, AF_INET); err != nil || ret < 0 { |
| 88 | + return nil, fmt.Errorf("Error binding to AF_INET protocol family: %v\n", err) |
| 89 | + } |
| 90 | + |
| 91 | + nfq.packets = make(chan NFPacket) |
| 92 | + if nfq.qh, err = C.CreateQueue(nfq.h, C.int(queueId), unsafe.Pointer(&nfq.packets)); err != nil || nfq.qh == nil { |
| 93 | + C.nfq_close(nfq.h) |
| 94 | + return nil, fmt.Errorf("Error binding to queue: %v\n", err) |
| 95 | + } |
| 96 | + |
| 97 | + if ret, err = C.nfq_set_queue_maxlen(nfq.qh, C.u_int32_t(maxPacketsInQueue)); err != nil || ret < 0 { |
| 98 | + C.nfq_destroy_queue(nfq.qh) |
| 99 | + C.nfq_close(nfq.h) |
| 100 | + return nil, fmt.Errorf("Unable to set max packets in queue: %v\n", err) |
| 101 | + } |
| 102 | + |
| 103 | + if C.nfq_set_mode(nfq.qh, C.u_int8_t(2), C.uint(packetSize)) < 0 { |
| 104 | + C.nfq_destroy_queue(nfq.qh) |
| 105 | + C.nfq_close(nfq.h) |
| 106 | + return nil, fmt.Errorf("Unable to set packets copy mode: %v\n", err) |
| 107 | + } |
| 108 | + |
| 109 | + if nfq.fd, err = C.nfq_fd(nfq.h); err != nil { |
| 110 | + C.nfq_destroy_queue(nfq.qh) |
| 111 | + C.nfq_close(nfq.h) |
| 112 | + return nil, fmt.Errorf("Unable to get queue file-descriptor. %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + go nfq.run() |
| 116 | + |
| 117 | + return &nfq, nil |
| 118 | +} |
| 119 | + |
| 120 | +//Unbind and close the queue |
| 121 | +func (nfq *NFQueue) Close() { |
| 122 | + C.nfq_destroy_queue(nfq.qh) |
| 123 | + C.nfq_close(nfq.h) |
| 124 | +} |
| 125 | + |
| 126 | +//Get the channel for packets |
| 127 | +func (nfq *NFQueue) GetPackets() <-chan NFPacket { |
| 128 | + return nfq.packets |
| 129 | +} |
| 130 | + |
| 131 | +func (nfq *NFQueue) run() { |
| 132 | + C.Run(nfq.h, nfq.fd) |
| 133 | +} |
| 134 | + |
| 135 | +//export go_callback |
| 136 | +func go_callback(queueId C.int, data *C.uchar, len C.int, cb *chan NFPacket) Verdict { |
| 137 | + xdata := C.GoBytes(unsafe.Pointer(data), len) |
| 138 | + packet := gopacket.NewPacket(xdata, layers.LayerTypeIPv4, gopacket.DecodeOptions{true, true}) |
| 139 | + p := NFPacket{verdictChannel: make(chan Verdict), Packet: packet} |
| 140 | + select { |
| 141 | + case (*cb) <- p: |
| 142 | + return <-p.verdictChannel |
| 143 | + default: |
| 144 | + return NF_DROP |
| 145 | + } |
| 146 | +} |
0 commit comments