a25ad4ceccfcf9670fdb5d0496f29de2dc26ca37
[iptables.git] / libipq / libipq.c
1 /*
2  * libipq.c
3  *
4  * IPQ userspace library.
5  *
6  * Please note that this library is still developmental, and there may
7  * be some API changes.
8  *
9  * Author: James Morris <jmorris@intercode.com.au>
10  *
11  * 07-11-2001 Modified by Fernando Anton to add support for IPv6.
12  *
13  * Copyright (c) 2000-2001 Netfilter Core Team
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  */
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33
34 #include <libipq/libipq.h>
35
36 /****************************************************************************
37  *
38  * Private interface
39  *
40  ****************************************************************************/
41
42 enum {
43         IPQ_ERR_NONE = 0,
44         IPQ_ERR_IMPL,
45         IPQ_ERR_HANDLE,
46         IPQ_ERR_SOCKET,
47         IPQ_ERR_BIND,
48         IPQ_ERR_BUFFER,
49         IPQ_ERR_RECV,
50         IPQ_ERR_NLEOF,
51         IPQ_ERR_ADDRLEN,
52         IPQ_ERR_STRUNC,
53         IPQ_ERR_RTRUNC,
54         IPQ_ERR_NLRECV,
55         IPQ_ERR_SEND,
56         IPQ_ERR_SUPP,
57         IPQ_ERR_RECVBUF,
58         IPQ_ERR_TIMEOUT,
59         IPQ_ERR_PROTOCOL
60 };
61 #define IPQ_MAXERR IPQ_ERR_PROTOCOL
62
63 struct ipq_errmap_t {
64         int errcode;
65         char *message;
66 } ipq_errmap[] = {
67         { IPQ_ERR_NONE, "Unknown error" },
68         { IPQ_ERR_IMPL, "Implementation error" },
69         { IPQ_ERR_HANDLE, "Unable to create netlink handle" },
70         { IPQ_ERR_SOCKET, "Unable to create netlink socket" },
71         { IPQ_ERR_BIND, "Unable to bind netlink socket" },
72         { IPQ_ERR_BUFFER, "Unable to allocate buffer" },
73         { IPQ_ERR_RECV, "Failed to receive netlink message" },
74         { IPQ_ERR_NLEOF, "Received EOF on netlink socket" },
75         { IPQ_ERR_ADDRLEN, "Invalid peer address length" },
76         { IPQ_ERR_STRUNC, "Sent message truncated" },
77         { IPQ_ERR_RTRUNC, "Received message truncated" },
78         { IPQ_ERR_NLRECV, "Received error from netlink" },
79         { IPQ_ERR_SEND, "Failed to send netlink message" },
80         { IPQ_ERR_SUPP, "Operation not supported" },
81         { IPQ_ERR_RECVBUF, "Receive buffer size invalid" },
82         { IPQ_ERR_TIMEOUT, "Timeout"},
83         { IPQ_ERR_PROTOCOL, "Invalid protocol specified" }
84 };
85
86 static int ipq_errno = IPQ_ERR_NONE;
87
88 static ssize_t ipq_netlink_sendto(const struct ipq_handle *h,
89                                   const void *msg, size_t len);
90
91 static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h,
92                                     unsigned char *buf, size_t len,
93                                     int timeout);
94
95 static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h,
96                                    const struct msghdr *msg,
97                                    unsigned int flags);
98
99 static char *ipq_strerror(int errcode);
100
101 static ssize_t ipq_netlink_sendto(const struct ipq_handle *h,
102                                   const void *msg, size_t len)
103 {
104         int status = sendto(h->fd, msg, len, 0,
105                             (struct sockaddr *)&h->peer, sizeof(h->peer));
106         if (status < 0)
107                 ipq_errno = IPQ_ERR_SEND;
108         return status;
109 }
110
111 static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h,
112                                    const struct msghdr *msg,
113                                    unsigned int flags)
114 {
115         int status = sendmsg(h->fd, msg, flags);
116         if (status < 0)
117                 ipq_errno = IPQ_ERR_SEND;
118         return status;
119 }
120
121 static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h,
122                                     unsigned char *buf, size_t len,
123                                     int timeout)
124 {
125         int addrlen, status;
126         struct nlmsghdr *nlh;
127
128         if (len < sizeof(struct nlmsgerr)) {
129                 ipq_errno = IPQ_ERR_RECVBUF;
130                 return -1;
131         }
132         addrlen = sizeof(h->peer);
133
134         if (timeout != 0) {
135                 int ret;
136                 struct timeval tv;
137                 fd_set read_fds;
138                 
139                 if (timeout < 0) {
140                         /* non-block non-timeout */
141                         tv.tv_sec = 0;
142                         tv.tv_usec = 0;
143                 } else {
144                         tv.tv_sec = timeout / 1000000;
145                         tv.tv_usec = timeout % 1000000;
146                 }
147
148                 FD_ZERO(&read_fds);
149                 FD_SET(h->fd, &read_fds);
150                 ret = select(h->fd+1, &read_fds, NULL, NULL, &tv);
151                 if (ret < 0) {
152                         if (errno == EINTR) {
153                                 return 0;
154                         } else {
155                                 ipq_errno = IPQ_ERR_RECV;
156                                 return -1;
157                         }
158                 }
159                 if (!FD_ISSET(h->fd, &read_fds)) {
160                         ipq_errno = IPQ_ERR_TIMEOUT;
161                         return 0;
162                 }
163         }
164         status = recvfrom(h->fd, buf, len, 0,
165                               (struct sockaddr *)&h->peer, &addrlen);
166         if (status < 0) {
167                 ipq_errno = IPQ_ERR_RECV;
168                 return status;
169         }
170         if (addrlen != sizeof(h->peer)) {
171                 ipq_errno = IPQ_ERR_RECV;
172                 return -1;
173         }
174         if (h->peer.nl_pid != 0) {
175                 ipq_errno = IPQ_ERR_RECV;
176                 return -1;
177         }
178         if (status == 0) {
179                 ipq_errno = IPQ_ERR_NLEOF;
180                 return -1;
181         }
182         nlh = (struct nlmsghdr *)buf;
183         if (nlh->nlmsg_flags & MSG_TRUNC || nlh->nlmsg_len > status) {
184                 ipq_errno = IPQ_ERR_RTRUNC;
185                 return -1;
186         }
187         return status;
188 }
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         if (protocol == PF_INET)
220                 h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_FIREWALL);
221         else if (protocol == PF_INET6)
222                 h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_IP6_FW);
223         else {
224                 ipq_errno = IPQ_ERR_PROTOCOL;
225                 free(h);
226                 return NULL;
227         }
228         
229         if (h->fd == -1) {
230                 ipq_errno = IPQ_ERR_SOCKET;
231                 close(h->fd);
232                 free(h);
233                 return NULL;
234         }
235         memset(&h->local, 0, sizeof(struct sockaddr_nl));
236         h->local.nl_family = AF_NETLINK;
237         h->local.nl_pid = getpid();
238         h->local.nl_groups = 0;
239         status = bind(h->fd, (struct sockaddr *)&h->local, sizeof(h->local));
240         if (status == -1) {
241                 ipq_errno = IPQ_ERR_BIND;
242                 close(h->fd);
243                 free(h);
244                 return NULL;
245         }
246         memset(&h->peer, 0, sizeof(struct sockaddr_nl));
247         h->peer.nl_family = AF_NETLINK;
248         h->peer.nl_pid = 0;
249         h->peer.nl_groups = 0;
250         return h;
251 }
252
253 /*
254  * No error condition is checked here at this stage, but it may happen
255  * if/when reliable messaging is implemented.
256  */
257 int ipq_destroy_handle(struct ipq_handle *h)
258 {
259         if (h) {
260                 close(h->fd);
261                 free(h);
262         }
263         return 0;
264 }
265
266 int ipq_set_mode(const struct ipq_handle *h,
267                  u_int8_t mode, size_t range)
268 {
269         struct {
270                 struct nlmsghdr nlh;
271                 ipq_peer_msg_t pm;
272         } req;
273
274         memset(&req, 0, sizeof(req));
275         req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req));
276         req.nlh.nlmsg_flags = NLM_F_REQUEST;
277         req.nlh.nlmsg_type = IPQM_MODE;
278         req.nlh.nlmsg_pid = h->local.nl_pid;
279         req.pm.msg.mode.value = mode;
280         req.pm.msg.mode.range = range;
281         return ipq_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
282 }
283
284 /*
285  * timeout is in microseconds (1 second is 1000000 (1 million) microseconds)
286  *
287  */
288 ssize_t ipq_read(const struct ipq_handle *h,
289                  unsigned char *buf, size_t len, int timeout)
290 {
291         return ipq_netlink_recvfrom(h, buf, len, timeout);
292 }
293
294 int ipq_message_type(const unsigned char *buf)
295 {
296         return ((struct nlmsghdr*)buf)->nlmsg_type;
297 }
298
299 int ipq_get_msgerr(const unsigned char *buf)
300 {
301         struct nlmsghdr *h = (struct nlmsghdr *)buf;
302         struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
303         return -err->error;
304 }
305
306 ipq_packet_msg_t *ipq_get_packet(const unsigned char *buf)
307 {
308         return NLMSG_DATA((struct nlmsghdr *)(buf));
309 }
310
311 int ipq_set_verdict(const struct ipq_handle *h,
312                     ipq_id_t id,
313                     unsigned int verdict,
314                     size_t data_len,
315                     unsigned char *buf)
316 {
317         unsigned char nvecs;
318         size_t tlen;
319         struct nlmsghdr nlh;
320         ipq_peer_msg_t pm;
321         struct iovec iov[3];
322         struct msghdr msg;
323
324         memset(&nlh, 0, sizeof(nlh));
325         nlh.nlmsg_flags = NLM_F_REQUEST;
326         nlh.nlmsg_type = IPQM_VERDICT;
327         nlh.nlmsg_pid = h->local.nl_pid;
328         memset(&pm, 0, sizeof(pm));
329         pm.msg.verdict.value = verdict;
330         pm.msg.verdict.id = id;
331         pm.msg.verdict.data_len = data_len;
332         iov[0].iov_base = &nlh;
333         iov[0].iov_len = sizeof(nlh);
334         iov[1].iov_base = &pm;
335         iov[1].iov_len = sizeof(pm);
336         tlen = sizeof(nlh) + sizeof(pm);
337         nvecs = 2;
338         if (data_len && buf) {
339                 iov[2].iov_base = buf;
340                 iov[2].iov_len = data_len;
341                 tlen += data_len;
342                 nvecs++;
343         }
344         msg.msg_name = (void *)&h->peer;
345         msg.msg_namelen = sizeof(h->peer);
346         msg.msg_iov = iov;
347         msg.msg_iovlen = nvecs;
348         msg.msg_control = NULL;
349         msg.msg_controllen = 0;
350         msg.msg_flags = 0;
351         nlh.nlmsg_len = tlen;
352         return ipq_netlink_sendmsg(h, &msg, 0);
353 }
354
355 /* Not implemented yet */
356 int ipq_ctl(const struct ipq_handle *h, int request, ...)
357 {
358         return 1;
359 }
360
361 char *ipq_errstr(void)
362 {
363         return ipq_strerror(ipq_errno);
364 }
365
366 void ipq_perror(const char *s)
367 {
368         if (s)
369                 fputs(s, stderr);
370         else
371                 fputs("ERROR", stderr);
372         if (ipq_errno)
373                 fprintf(stderr, ": %s", ipq_errstr());
374         if (errno)
375                 fprintf(stderr, ": %s", strerror(errno));
376         fputc('\n', stderr);
377 }