add (incomplete) support for libipq emulation API
[platform/upstream/libnetfilter_queue.git] / libipq_compat.c
1 /*
2  * libipq - backwards compatibility library for libnfnetlink_queue
3  *
4  * (C) 2005 by Harald Welte <laforge@netfilter.org>
5  *
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
10  *
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.
15  *
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.
20  *
21  */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29
30 #include "libnfnetlink_queue.h"
31 #include "libipq.h"
32
33 /****************************************************************************
34  *
35  * Private interface
36  *
37  ****************************************************************************/
38
39 enum {
40         IPQ_ERR_NONE = 0,
41         IPQ_ERR_IMPL,
42         IPQ_ERR_HANDLE,
43         IPQ_ERR_SOCKET,
44         IPQ_ERR_BIND,
45         IPQ_ERR_BUFFER,
46         IPQ_ERR_RECV,
47         IPQ_ERR_NLEOF,
48         IPQ_ERR_ADDRLEN,
49         IPQ_ERR_STRUNC,
50         IPQ_ERR_RTRUNC,
51         IPQ_ERR_NLRECV,
52         IPQ_ERR_SEND,
53         IPQ_ERR_SUPP,
54         IPQ_ERR_RECVBUF,
55         IPQ_ERR_TIMEOUT,
56         IPQ_ERR_PROTOCOL
57 };
58 #define IPQ_MAXERR IPQ_ERR_PROTOCOL
59
60 struct ipq_errmap_t {
61         int errcode;
62         char *message;
63 } ipq_errmap[] = {
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" }
81 };
82
83 static int ipq_errno = IPQ_ERR_NONE;
84
85 #if 0
86 static ssize_t ipq_netlink_sendto(const struct ipq_handle *h,
87                                   const void *msg, size_t len);
88
89 static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h,
90                                     unsigned char *buf, size_t len,
91                                     int timeout);
92
93 static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h,
94                                    const struct msghdr *msg,
95                                    unsigned int flags);
96
97 static char *ipq_strerror(int errcode);
98
99 static ssize_t ipq_netlink_sendto(const struct ipq_handle *h,
100                                   const void *msg, size_t len)
101 {
102         int status = sendto(h->fd, msg, len, 0,
103                             (struct sockaddr *)&h->peer, sizeof(h->peer));
104         if (status < 0)
105                 ipq_errno = IPQ_ERR_SEND;
106         return status;
107 }
108
109 static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h,
110                                    const struct msghdr *msg,
111                                    unsigned int flags)
112 {
113         int status = sendmsg(h->fd, msg, flags);
114         if (status < 0)
115                 ipq_errno = IPQ_ERR_SEND;
116         return status;
117 }
118
119 static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h,
120                                     unsigned char *buf, size_t len,
121                                     int timeout)
122 {
123         unsigned int addrlen;
124         int status;
125         struct nlmsghdr *nlh;
126
127         if (len < sizeof(struct nlmsgerr)) {
128                 ipq_errno = IPQ_ERR_RECVBUF;
129                 return -1;
130         }
131         addrlen = sizeof(h->peer);
132
133         if (timeout != 0) {
134                 int ret;
135                 struct timeval tv;
136                 fd_set read_fds;
137                 
138                 if (timeout < 0) {
139                         /* non-block non-timeout */
140                         tv.tv_sec = 0;
141                         tv.tv_usec = 0;
142                 } else {
143                         tv.tv_sec = timeout / 1000000;
144                         tv.tv_usec = timeout % 1000000;
145                 }
146
147                 FD_ZERO(&read_fds);
148                 FD_SET(h->fd, &read_fds);
149                 ret = select(h->fd+1, &read_fds, NULL, NULL, &tv);
150                 if (ret < 0) {
151                         if (errno == EINTR) {
152                                 return 0;
153                         } else {
154                                 ipq_errno = IPQ_ERR_RECV;
155                                 return -1;
156                         }
157                 }
158                 if (!FD_ISSET(h->fd, &read_fds)) {
159                         ipq_errno = IPQ_ERR_TIMEOUT;
160                         return 0;
161                 }
162         }
163         status = recvfrom(h->fd, buf, len, 0,
164                               (struct sockaddr *)&h->peer, &addrlen);
165         if (status < 0) {
166                 ipq_errno = IPQ_ERR_RECV;
167                 return status;
168         }
169         if (addrlen != sizeof(h->peer)) {
170                 ipq_errno = IPQ_ERR_RECV;
171                 return -1;
172         }
173         if (h->peer.nl_pid != 0) {
174                 ipq_errno = IPQ_ERR_RECV;
175                 return -1;
176         }
177         if (status == 0) {
178                 ipq_errno = IPQ_ERR_NLEOF;
179                 return -1;
180         }
181         nlh = (struct nlmsghdr *)buf;
182         if (nlh->nlmsg_flags & MSG_TRUNC || nlh->nlmsg_len > status) {
183                 ipq_errno = IPQ_ERR_RTRUNC;
184                 return -1;
185         }
186         return status;
187 }
188 #endif
189
190 static char *ipq_strerror(int errcode)
191 {
192         if (errcode < 0 || errcode > IPQ_MAXERR)
193                 errcode = IPQ_ERR_IMPL;
194         return ipq_errmap[errcode].message;
195 }
196
197 /****************************************************************************
198  *
199  * Public interface
200  *
201  ****************************************************************************/
202
203 /*
204  * Create and initialise an ipq handle.
205  */
206 struct ipq_handle *ipq_create_handle(u_int32_t flags, u_int32_t protocol)
207 {
208         int status;
209         struct ipq_handle *h;
210
211         h = (struct ipq_handle *)malloc(sizeof(struct ipq_handle));
212         if (h == NULL) {
213                 ipq_errno = IPQ_ERR_HANDLE;
214                 return NULL;
215         }
216         
217         memset(h, 0, sizeof(struct ipq_handle));
218
219         status = nfqnl_open(&h->nfqnlh);
220         if (status < 0) {
221                 ipq_errno = IPQ_ERR_SOCKET;
222                 goto err_free;
223         }
224         
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);
229         else {
230                 ipq_errno = IPQ_ERR_PROTOCOL;
231                 goto err_close;
232         }
233         h->family = protocol;
234         if (status < 0) {
235                 ipq_errno = IPQ_ERR_BIND;
236                 goto err_close;
237         }
238
239         status = nfqnl_create_queue(&h->nfqnlh, &h->qh, 0);
240         if (status < 0) {
241                 ipq_errno = IPQ_ERR_BIND;
242                 goto err_close;
243         }
244
245         return h;
246
247 err_close:
248         nfqnl_close(&h->nfqnlh);
249 err_free:
250         free(h);
251         return NULL;
252 }
253
254 /*
255  * No error condition is checked here at this stage, but it may happen
256  * if/when reliable messaging is implemented.
257  */
258 int ipq_destroy_handle(struct ipq_handle *h)
259 {
260         if (h) {
261                 nfqnl_close(&h->nfqnlh);
262                 free(h);
263         }
264         return 0;
265 }
266
267 int ipq_set_mode(const struct ipq_handle *h,
268                  u_int8_t mode, size_t range)
269 {
270         return nfqnl_set_mode(&h->qh, mode, range);
271 }
272
273 /*
274  * timeout is in microseconds (1 second is 1000000 (1 million) microseconds)
275  *
276  */
277 ssize_t ipq_read(const struct ipq_handle *h,
278                  unsigned char *buf, size_t len, int timeout)
279 {
280         struct nfattr *tb[NFQA_MAX];
281         struct nlmsghdr *nlh = (struct nlmsghdr *)buf;
282         struct nfgenmsg *msg = NULL;
283         struct nfattr *nfa;
284
285         //return ipq_netlink_recvfrom(h, buf, len, timeout);
286         
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... */
290
291         nfa = nfnl_parse_hdr(&h->nfqnlh.nfnlh, nlh, &msg);
292         if (!msg || !nfa)
293                 return 0;
294
295         if (msg->nfgen_family != h->family)
296                 return 0;
297         
298         nfnl_parse_attr(tb, NFQA_MAX, nfa, 0xffff);
299
300
301 }
302
303 int ipq_message_type(const unsigned char *buf)
304 {
305         return ((struct nlmsghdr*)buf)->nlmsg_type;
306 }
307
308 int ipq_get_msgerr(const unsigned char *buf)
309 {
310         struct nlmsghdr *h = (struct nlmsghdr *)buf;
311         struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
312         return -err->error;
313 }
314
315 ipq_packet_msg_t *ipq_get_packet(const unsigned char *buf)
316 {
317         return NLMSG_DATA((struct nlmsghdr *)(buf));
318 }
319
320 int ipq_set_verdict(const struct ipq_handle *h,
321                     ipq_id_t id,
322                     unsigned int verdict,
323                     size_t data_len,
324                     unsigned char *buf)
325 {
326         return nfqnl_set_verdict(&h->qh, id, verdict, data_len, buf);
327 }
328
329 /* Not implemented yet */
330 int ipq_ctl(const struct ipq_handle *h, int request, ...)
331 {
332         return 1;
333 }
334
335 char *ipq_errstr(void)
336 {
337         return ipq_strerror(ipq_errno);
338 }
339
340 void ipq_perror(const char *s)
341 {
342         if (s)
343                 fputs(s, stderr);
344         else
345                 fputs("ERROR", stderr);
346         if (ipq_errno)
347                 fprintf(stderr, ": %s", ipq_errstr());
348         if (errno)
349                 fprintf(stderr, ": %s", strerror(errno));
350         fputc('\n', stderr);
351 }