2 * libipq - backwards compatibility library for libnfnetlink_queue
4 * (C) 2005 by Harald Welte <laforge@netfilter.org>
6 * Based on original libipq.c,
7 * Author: James Morris <jmorris@intercode.com.au>
8 * 07-11-2001 Modified by Fernando Anton to add support for IPv6.
9 * Copyright (c) 2000-2001 Netfilter Core Team
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
28 #include <sys/types.h>
30 #include "libnfnetlink_queue.h"
33 /****************************************************************************
37 ****************************************************************************/
58 #define IPQ_MAXERR IPQ_ERR_PROTOCOL
64 { IPQ_ERR_NONE, "Unknown error" },
65 { IPQ_ERR_IMPL, "Implementation error" },
66 { IPQ_ERR_HANDLE, "Unable to create netlink handle" },
67 { IPQ_ERR_SOCKET, "Unable to create netlink socket" },
68 { IPQ_ERR_BIND, "Unable to bind netlink socket" },
69 { IPQ_ERR_BUFFER, "Unable to allocate buffer" },
70 { IPQ_ERR_RECV, "Failed to receive netlink message" },
71 { IPQ_ERR_NLEOF, "Received EOF on netlink socket" },
72 { IPQ_ERR_ADDRLEN, "Invalid peer address length" },
73 { IPQ_ERR_STRUNC, "Sent message truncated" },
74 { IPQ_ERR_RTRUNC, "Received message truncated" },
75 { IPQ_ERR_NLRECV, "Received error from netlink" },
76 { IPQ_ERR_SEND, "Failed to send netlink message" },
77 { IPQ_ERR_SUPP, "Operation not supported" },
78 { IPQ_ERR_RECVBUF, "Receive buffer size invalid" },
79 { IPQ_ERR_TIMEOUT, "Timeout"},
80 { IPQ_ERR_PROTOCOL, "Invalid protocol specified" }
83 static int ipq_errno = IPQ_ERR_NONE;
86 static ssize_t ipq_netlink_sendto(const struct ipq_handle *h,
87 const void *msg, size_t len);
89 static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h,
90 unsigned char *buf, size_t len,
93 static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h,
94 const struct msghdr *msg,
97 static char *ipq_strerror(int errcode);
99 static ssize_t ipq_netlink_sendto(const struct ipq_handle *h,
100 const void *msg, size_t len)
102 int status = sendto(h->fd, msg, len, 0,
103 (struct sockaddr *)&h->peer, sizeof(h->peer));
105 ipq_errno = IPQ_ERR_SEND;
109 static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h,
110 const struct msghdr *msg,
113 int status = sendmsg(h->fd, msg, flags);
115 ipq_errno = IPQ_ERR_SEND;
119 static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h,
120 unsigned char *buf, size_t len,
123 unsigned int addrlen;
125 struct nlmsghdr *nlh;
127 if (len < sizeof(struct nlmsgerr)) {
128 ipq_errno = IPQ_ERR_RECVBUF;
131 addrlen = sizeof(h->peer);
139 /* non-block non-timeout */
143 tv.tv_sec = timeout / 1000000;
144 tv.tv_usec = timeout % 1000000;
148 FD_SET(h->fd, &read_fds);
149 ret = select(h->fd+1, &read_fds, NULL, NULL, &tv);
151 if (errno == EINTR) {
154 ipq_errno = IPQ_ERR_RECV;
158 if (!FD_ISSET(h->fd, &read_fds)) {
159 ipq_errno = IPQ_ERR_TIMEOUT;
163 status = recvfrom(h->fd, buf, len, 0,
164 (struct sockaddr *)&h->peer, &addrlen);
166 ipq_errno = IPQ_ERR_RECV;
169 if (addrlen != sizeof(h->peer)) {
170 ipq_errno = IPQ_ERR_RECV;
173 if (h->peer.nl_pid != 0) {
174 ipq_errno = IPQ_ERR_RECV;
178 ipq_errno = IPQ_ERR_NLEOF;
181 nlh = (struct nlmsghdr *)buf;
182 if (nlh->nlmsg_flags & MSG_TRUNC || nlh->nlmsg_len > status) {
183 ipq_errno = IPQ_ERR_RTRUNC;
190 static char *ipq_strerror(int errcode)
192 if (errcode < 0 || errcode > IPQ_MAXERR)
193 errcode = IPQ_ERR_IMPL;
194 return ipq_errmap[errcode].message;
197 /****************************************************************************
201 ****************************************************************************/
204 * Create and initialise an ipq handle.
206 struct ipq_handle *ipq_create_handle(u_int32_t flags, u_int32_t protocol)
209 struct ipq_handle *h;
211 h = (struct ipq_handle *)malloc(sizeof(struct ipq_handle));
213 ipq_errno = IPQ_ERR_HANDLE;
217 memset(h, 0, sizeof(struct ipq_handle));
219 status = nfqnl_open(&h->nfqnlh);
221 ipq_errno = IPQ_ERR_SOCKET;
225 if (protocol == PF_INET)
226 status = nfqnl_bind_pf(&h->nfqnlh, PF_INET);
227 else if (protocol == PF_INET6)
228 status = nfqnl_bind_pf(&h->nfqnlh, PF_INET6);
230 ipq_errno = IPQ_ERR_PROTOCOL;
233 h->family = protocol;
235 ipq_errno = IPQ_ERR_BIND;
239 status = nfqnl_create_queue(&h->nfqnlh, &h->qh, 0);
241 ipq_errno = IPQ_ERR_BIND;
248 nfqnl_close(&h->nfqnlh);
255 * No error condition is checked here at this stage, but it may happen
256 * if/when reliable messaging is implemented.
258 int ipq_destroy_handle(struct ipq_handle *h)
261 nfqnl_close(&h->nfqnlh);
267 int ipq_set_mode(const struct ipq_handle *h,
268 u_int8_t mode, size_t range)
270 return nfqnl_set_mode(&h->qh, mode, range);
274 * timeout is in microseconds (1 second is 1000000 (1 million) microseconds)
277 ssize_t ipq_read(const struct ipq_handle *h,
278 unsigned char *buf, size_t len, int timeout)
280 struct nfattr *tb[NFQA_MAX];
281 struct nlmsghdr *nlh = (struct nlmsghdr *)buf;
282 struct nfgenmsg *msg = NULL;
285 //return ipq_netlink_recvfrom(h, buf, len, timeout);
287 /* This really sucks. We have to copy the whole packet
288 * in order to build a data structure that is compatible to
289 * the old ipq interface... */
291 nfa = nfnl_parse_hdr(&h->nfqnlh.nfnlh, nlh, &msg);
295 if (msg->nfgen_family != h->family)
298 nfnl_parse_attr(tb, NFQA_MAX, nfa, 0xffff);
303 int ipq_message_type(const unsigned char *buf)
305 return ((struct nlmsghdr*)buf)->nlmsg_type;
308 int ipq_get_msgerr(const unsigned char *buf)
310 struct nlmsghdr *h = (struct nlmsghdr *)buf;
311 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
315 ipq_packet_msg_t *ipq_get_packet(const unsigned char *buf)
317 return NLMSG_DATA((struct nlmsghdr *)(buf));
320 int ipq_set_verdict(const struct ipq_handle *h,
322 unsigned int verdict,
326 return nfqnl_set_verdict(&h->qh, id, verdict, data_len, buf);
329 /* Not implemented yet */
330 int ipq_ctl(const struct ipq_handle *h, int request, ...)
335 char *ipq_errstr(void)
337 return ipq_strerror(ipq_errno);
340 void ipq_perror(const char *s)
345 fputs("ERROR", stderr);
347 fprintf(stderr, ": %s", ipq_errstr());
349 fprintf(stderr, ": %s", strerror(errno));