3 # - use $DESTDIR for prefix (Debian interest)
4 # - use libnetfilter_queue instead of deprecated libipq
6 # Under GPLv2 - Copyright (C) 2008 - EADS - Arnaud Ebalard <arno@natisbad.org>
9 @@ -19,7 +19,7 @@ OS=linux
14 +prefix=$(DESTDIR)/usr
16 # Set to "y" to build MT versions of sendd and cgatool
20 @@ -20,7 +20,7 @@ Linux:
21 o CONFIG_NETFILTER, CONFIG_IPV6, CONFIG_IP6_NF_QUEUE, CONFIG_IP6_NF_IPTABLES,
22 CONFIG_IP6_NF_FILTER enabled in your kernel config.
23 o netfilter ip6tables command
24 - o netfilter libipq development library and headers
25 + o netfilter libnetfilter_queue development library and headers
28 o NETGRAPH, NETGRAPH_BPF, NETGRAPH_ETHER, NETGRAPH_SOCKET enabled in
29 --- a/cgatool/Makefile
30 +++ b/cgatool/Makefile
31 @@ -25,7 +25,7 @@ endif
33 ifeq ($(USE_CONSOLE),y)
34 ifeq ($(USE_READLINE),y)
35 -LDLIBS += -lreadline -lncurses
42 @@ -37,7 +37,7 @@ endif
44 ifeq ($(USE_CONSOLE),y)
45 ifeq ($(USE_READLINE),y)
46 -LDLIBS += -lreadline -lncurses
51 --- a/sendd/os-linux/Makefile
52 +++ b/sendd/os-linux/Makefile
55 OBJS += os/addr.o os/ipq.o os/rand.o os/snd_linux.o
57 +OSLIBS= -lnetfilter_queue
59 -OSEXTRA= os/sendd os/snd_upd_fw
62 -EXTRAINSTALL= $(ETCINIT)/sendd $(ETCINIT)/snd_upd_fw $(ETCINIT)/snd_fw_functions.sh
63 -EXTRAUNINSTALL=$(EXTRAINSTALL)
64 -EXTRACLEAN= os/sendd os/snd_upd_fw os/snd_fw_functions.sh
70 - sed "s/@etcinit@/\/etc\/init.d/g" $< > $@
73 - @./os/find_ip6tables.sh
75 -# Sometimes libipq.h is installed in include/libipq.h, other times it is
76 -# installed in include/libipq/libipq.h. This rule helps cpp to find it.
77 -os/ipq.o: CPPFLAGS += -I/usr/include/libipq -I/usr/local/include/libipq
78 +os/ipq.o: CPPFLAGS += -I/usr/include/libnetfilter_queue
79 --- a/sendd/os-linux/ipq.c
80 +++ b/sendd/os-linux/ipq.c
83 #include <sys/select.h>
84 #include <netinet/in.h>
85 +#include <linux/types.h>
86 #include <linux/netfilter.h>
88 +#include <libnetfilter_queue.h>
93 #include "../sendd_local.h"
94 #include "snd_linux.h"
96 -static struct ipq_handle *qh;
98 extern unsigned if_nametoindex(const char *);
101 -process_pkt(ipq_packet_msg_t *pkt, struct sbuff *b)
103 +struct nfq_handle *h = NULL;
104 +struct nfq_q_handle *qh = NULL;
106 +/* This is the default queue number used in our init script */
107 +#define SND_DEFAULT_NFQUEUE_NUMBER 13
109 +/* The sbuff is must be made available to the callback function that will
110 + handle the packet */
111 +struct callback_data {
115 +struct callback_data process_pkt_data;
117 +/* nfqueue callback */
118 +static int process_pkt(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
119 + struct nfq_data *nfa, void *data)
121 + struct callback_data * d = (struct callback_data *)data;
122 + struct nfqnl_msg_packet_hdr *ph;
123 + struct sbuff *b = d->b;
127 - b->data = pkt->payload;
128 - b->len = pkt->data_len;
129 + b->len = nfq_get_payload(nfa, &pkt_data);
130 + if (b->len == -1) {
131 + applog(LOG_ERR, "%s: nfq_get_payload() failed.", __FUNCTION__);
134 + b->data = (unsigned char *)pkt_data;
136 - if (*(pkt->indev_name)) {
137 + if ((ifidx = nfq_get_indev(nfa)) && ifidx != 0)
139 - ifidx = if_nametoindex(pkt->indev_name);
140 - } else if (*(pkt->outdev_name)) {
141 + else if ((ifidx = nfq_get_outdev(nfa)) && ifidx != 0)
143 - ifidx = if_nametoindex(pkt->outdev_name);
146 applog(LOG_ERR, "%s: pkt has neither indev nor outdev",
153 - snd_recv_pkt(b, ifidx, in, pkt);
155 + /* Grab packet header to get its id later */
156 + ph = nfq_get_msg_packet_hdr(nfa); /* FIXME Check return value */
162 - struct sbuff *b = snd_get_buf();
163 + snd_recv_pkt(b, ifidx, in, (void *)ph);
168 - if ((r = ipq_read(qh, b->head, b->rem, -1)) < 0) {
169 - applog(LOG_ERR, "%s: ipq_read(): %s", __FUNCTION__,
172 - } else if (r == 0) {
177 - switch ((r = ipq_message_type(b->head))) {
179 - applog(LOG_ERR, "%s: nlmsg error: %s", __FUNCTION__,
180 - strerror(ipq_get_msgerr(b->head)));
183 - process_pkt(ipq_get_packet(b->head), b);
195 -linux_ipq_add_fds(fd_set *fds, int *maxfd)
196 +linux_nfq_add_fds(fd_set *fds, int *maxfd)
198 - FD_SET(qh->fd, fds);
199 - *maxfd = sendd_max(*maxfd, qh->fd);
200 + int fd = nfnl_fd(nfq_nfnlh(h));
203 + *maxfd = sendd_max(*maxfd, fd);
207 -linux_ipq_dispatch_fds(fd_set *fds)
208 +linux_nfq_dispatch_fds(fd_set *fds)
210 - if (FD_ISSET(qh->fd, fds)) {
212 + int fd = nfnl_fd(nfq_nfnlh(h));
215 + if (FD_ISSET(fd, fds)) {
216 + struct sbuff *b = snd_get_buf();
222 + if ((r = recv(fd, b->head, b->rem, 0)) && r <= 0) {
223 + if (r < 0) /* not a timeout */
224 + applog(LOG_ERR, "%s: recv failed.",
230 + process_pkt_data.b = b; /* make sbuff available to
231 + callback function */
232 + nfq_handle_packet(h, (char *)b->head, r);
237 os_specific_deliver_pkt(void *p, struct sbuff *b, int drop, int changed)
239 - ipq_packet_msg_t *pkt = p;
240 - void *newpkt = NULL;
241 + struct nfqnl_msg_packet_hdr *ph = (struct nfqnl_msg_packet_hdr *)p;
242 + unsigned char *newpkt = NULL;
247 + id = ntohl(ph->packet_id);
249 if (changed && !drop) {
250 - newpkt = sbuff_data(b);
251 + newpkt = (unsigned char *)b->data;
255 - ipq_set_verdict(qh, pkt->packet_id, drop ? NF_DROP : NF_ACCEPT,
257 + nfq_set_verdict(qh, id, drop ? NF_DROP : NF_ACCEPT, plen, newpkt);
262 -linux_ipq_init(void)
263 +linux_nfq_init(void)
265 - if ((qh = ipq_create_handle(0, PF_INET6)) == NULL) {
266 - applog(LOG_ERR, "%s: ipq_create_handle() failed: %s",
267 - __FUNCTION__, ipq_errstr());
270 - if (ipq_set_mode(qh, IPQ_COPY_PACKET, SND_MAX_PKT) < 0) {
271 - applog(LOG_ERR, "%s: ipq_set_mode() failed: %s",
272 - __FUNCTION__, ipq_errstr());
273 - if (errno == ECONNREFUSED) {
274 - applog(LOG_ERR, "%s: perhaps you need to modprobe "
275 - "ip6_queue?", __FUNCTION__);
278 + struct nfnl_handle *nh;
279 + u_int16_t nfqueue_num = SND_DEFAULT_NFQUEUE_NUMBER;
281 + /* Get netfilter queue connection handle */
284 + applog(LOG_ERR, "%s: nfq_open() failed.", __FUNCTION__);
288 + /* Unbinding existing nfqueue handlers for AF_INET6. We ignore the
289 + return value: http://www.spinics.net/lists/netfilter/msg42063.html.
290 + Note that this call is required, otherwise, nfq_bind_pf() fails. */
291 + nfq_unbind_pf(h, PF_INET6);
293 + if (nfq_bind_pf(h, PF_INET6) < 0) {
294 + applog(LOG_ERR, "%s: nfq_bind_pf failed.\n", __FUNCTION__);
298 + /* Binding this socket to queue number nfqueue_num and installing
299 + our packet handler */
300 + qh = nfq_create_queue(h, nfqueue_num,
301 + (nfq_callback *) &process_pkt,
302 + (void *)&process_pkt_data);
304 + applog(LOG_ERR, "%s: nfq_create_queue() failed.\n",
309 + /* Asking for entire copy of queued packets */
310 + if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) { /* XXX was SND_MAX_PKT */
311 + fprintf(stderr, "nfq_set_mode() failed.\n");
316 + /* XXX - Check if we have an interest in setting queue length */
318 + /* The netlink handle associated with our queue connection handle */
325 -linux_ipq_fini(void)
326 +linux_nfq_fini(void)
328 - ipq_destroy_handle(qh);
329 + /* Remove the binding for our queue handler*/
331 + nfq_destroy_queue(qh);
333 + /* Close connection associated with our connection handler */
337 --- a/sendd/os-linux/snd_linux.c
338 +++ b/sendd/os-linux/snd_linux.c
341 os_specific_add_fds(fd_set *fds, int *maxfd)
343 - linux_ipq_add_fds(fds, maxfd);
344 + linux_nfq_add_fds(fds, maxfd);
348 os_specific_dispatch_fds(fd_set *fds)
350 - linux_ipq_dispatch_fds(fds);
351 + linux_nfq_dispatch_fds(fds);
355 @@ -59,7 +59,7 @@ int
356 os_specific_init(void)
358 if (linux_rand_init() < 0 ||
359 - linux_ipq_init() < 0) {
360 + linux_nfq_init() < 0) {
364 @@ -68,6 +68,6 @@ os_specific_init(void)
366 os_specific_fini(void)
372 --- a/sendd/os-linux/snd_linux.h
373 +++ b/sendd/os-linux/snd_linux.h
375 #ifndef _SEND_LINUX_H
376 #define _SEND_LINUX_H
378 -extern void linux_ipq_add_fds(fd_set *, int *);
379 -extern void linux_ipq_dispatch_fds(fd_set *);
380 -extern int linux_ipq_init(void);
381 -extern void linux_ipq_fini(void);
382 +extern void linux_nfq_add_fds(fd_set *, int *);
383 +extern void linux_nfq_dispatch_fds(fd_set *);
384 +extern int linux_nfq_init(void);
385 +extern void linux_nfq_fini(void);
387 extern void linux_rand_fini(void);
388 extern int linux_rand_init(void);