]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ipq_linux/libipq.c
Lots of stylistic fixes.
[stg.git] / projects / stargazer / plugins / capture / ipq_linux / 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.h"
35
36 /****************************************************************************
37  *
38  * Private interface
39  *
40  ****************************************************************************/
41
42 enum
43     {
44     IPQ_ERR_NONE = 0,
45     IPQ_ERR_IMPL,
46     IPQ_ERR_HANDLE,
47     IPQ_ERR_SOCKET,
48     IPQ_ERR_BIND,
49     IPQ_ERR_BUFFER,
50     IPQ_ERR_RECV,
51     IPQ_ERR_NLEOF,
52     IPQ_ERR_ADDRLEN,
53     IPQ_ERR_STRUNC,
54     IPQ_ERR_RTRUNC,
55     IPQ_ERR_NLRECV,
56     IPQ_ERR_SEND,
57     IPQ_ERR_SUPP,
58     IPQ_ERR_RECVBUF,
59     IPQ_ERR_TIMEOUT,
60     IPQ_ERR_PROTOCOL
61     };
62 #define IPQ_MAXERR IPQ_ERR_PROTOCOL
63
64 struct ipq_errmap_t
65     {
66     int errcode;
67     char *message;
68     } ipq_errmap[] = {
69     { IPQ_ERR_NONE, "Unknown error"},
70     { IPQ_ERR_IMPL, "Implementation error"},
71     { IPQ_ERR_HANDLE, "Unable to create netlink handle"},
72     { IPQ_ERR_SOCKET, "Unable to create netlink socket"},
73     { IPQ_ERR_BIND, "Unable to bind netlink socket"},
74     { IPQ_ERR_BUFFER, "Unable to allocate buffer"},
75     { IPQ_ERR_RECV, "Failed to receive netlink message"},
76     { IPQ_ERR_NLEOF, "Received EOF on netlink socket"},
77     { IPQ_ERR_ADDRLEN, "Invalid peer address length"},
78     { IPQ_ERR_STRUNC, "Sent message truncated"},
79     { IPQ_ERR_RTRUNC, "Received message truncated"},
80     { IPQ_ERR_NLRECV, "Received error from netlink"},
81     { IPQ_ERR_SEND, "Failed to send netlink message"},
82     { IPQ_ERR_SUPP, "Operation not supported"},
83     { IPQ_ERR_RECVBUF, "Receive buffer size invalid"},
84     { IPQ_ERR_TIMEOUT, "Timeout"},
85     { IPQ_ERR_PROTOCOL, "Invalid protocol specified"}
86 };
87
88 static int ipq_errno = IPQ_ERR_NONE;
89
90 static ssize_t ipq_netlink_sendto(const struct ipq_handle *h, 
91                                   const void *msg, size_t len);
92
93 static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h,
94                                     unsigned char *buf, size_t len,
95                                     int timeout);
96
97 static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h,
98                                    const struct msghdr *msg,
99                                    unsigned int flags);
100
101 //static char *ipq_strerror(int errcode);
102 //-----------------------------------------------------------------------------
103 static ssize_t ipq_netlink_sendto(const struct ipq_handle *h,
104                                   const void *msg, size_t len)
105 {
106     int status = sendto(h->fd, msg, len, 0,
107                         (struct sockaddr *)&h->peer, sizeof(h->peer));
108     if (status < 0)
109         ipq_errno = IPQ_ERR_SEND;
110     return status;
111 }
112 //-----------------------------------------------------------------------------
113 static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h,
114                                    const struct msghdr *msg,
115                                    unsigned int flags)
116 {
117     int status = sendmsg(h->fd, msg, flags);
118     if (status < 0)
119         ipq_errno = IPQ_ERR_SEND;
120     return status;
121 }
122 //-----------------------------------------------------------------------------
123 static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h,
124                                     unsigned char *buf, size_t len,
125                                     int timeout)
126 {
127     socklen_t addrlen;
128     int status;
129     struct nlmsghdr *nlh;
130
131     if (len < sizeof(struct nlmsgerr))
132         {
133         ipq_errno = IPQ_ERR_RECVBUF;
134         return -1;
135         }
136     addrlen = sizeof(h->peer);
137
138     if (timeout != 0)
139         {
140         int ret;
141         struct timeval tv;
142         fd_set read_fds;
143
144         if (timeout < 0)
145             {
146             /* non-block non-timeout */
147             tv.tv_sec = 0;
148             tv.tv_usec = 0;
149             }
150         else
151             {
152             tv.tv_sec = timeout / 1000000;
153             tv.tv_usec = timeout % 1000000;
154             }
155
156         FD_ZERO(&read_fds);
157         FD_SET(h->fd, &read_fds);
158         ret = select(h->fd+1, &read_fds, NULL, NULL, &tv);
159         if (ret < 0)
160             {
161             if (errno == EINTR)
162                 {
163                 return 0;
164                 }
165             else
166                 {
167                 ipq_errno = IPQ_ERR_RECV;
168                 return -1;
169                 }
170             }
171         if (!FD_ISSET(h->fd, &read_fds))
172             {
173             ipq_errno = IPQ_ERR_TIMEOUT;
174             return 0;
175             }
176         }
177     status = recvfrom(h->fd, buf, len, 0,
178                       (struct sockaddr *)&h->peer, &addrlen);
179     if (status < 0)
180         {
181         ipq_errno = IPQ_ERR_RECV;
182         return status;
183         }
184     if (addrlen != sizeof(h->peer))
185         {
186         ipq_errno = IPQ_ERR_RECV;
187         return -1;
188         }
189     if (h->peer.nl_pid != 0)
190         {
191         ipq_errno = IPQ_ERR_RECV;
192         return -1;
193         }
194     if (status == 0)
195         {
196         ipq_errno = IPQ_ERR_NLEOF;
197         return -1;
198         }
199     nlh = (struct nlmsghdr *)buf;
200     if (nlh->nlmsg_flags & MSG_TRUNC || (int)nlh->nlmsg_len > status)
201         {
202         ipq_errno = IPQ_ERR_RTRUNC;
203         return -1;
204         }
205     return status;
206 }
207 //-----------------------------------------------------------------------------
208 static char *ipq_strerror(int errcode)
209 {
210     if (errcode < 0 || errcode > IPQ_MAXERR)
211         errcode = IPQ_ERR_IMPL;
212     return ipq_errmap[errcode].message;
213 }
214
215 /****************************************************************************
216  *
217  * Public interface
218  *
219  ****************************************************************************/
220
221 /*
222  * Create and initialise an ipq handle.
223  */
224 struct ipq_handle *ipq_create_handle(u_int32_t __attribute__((unused)) flags, u_int32_t protocol)
225     {
226     int status;
227     struct ipq_handle *h;
228
229     h = (struct ipq_handle *)malloc(sizeof(struct ipq_handle));
230     if (h == NULL)
231         {
232         ipq_errno = IPQ_ERR_HANDLE;
233         return NULL;
234         }
235
236     memset(h, 0, sizeof(struct ipq_handle));
237
238     if (protocol == PF_INET)
239         h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_FIREWALL);
240     else if (protocol == PF_INET6)
241         h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_IP6_FW);
242     else
243         {
244         ipq_errno = IPQ_ERR_PROTOCOL;
245         free(h);
246         return NULL;
247         }
248
249     if (h->fd == -1)
250         {
251         ipq_errno = IPQ_ERR_SOCKET;
252         close(h->fd);
253         free(h);
254         return NULL;
255         }
256     memset(&h->local, 0, sizeof(struct sockaddr_nl));
257     h->local.nl_family = AF_NETLINK;
258     h->local.nl_pid = getpid();
259     h->local.nl_groups = 0;
260     status = bind(h->fd, (struct sockaddr *)&h->local, sizeof(h->local));
261     if (status == -1)
262         {
263         ipq_errno = IPQ_ERR_BIND;
264         close(h->fd);
265         free(h);
266         return NULL;
267         }
268     memset(&h->peer, 0, sizeof(struct sockaddr_nl));
269     h->peer.nl_family = AF_NETLINK;
270     h->peer.nl_pid = 0;
271     h->peer.nl_groups = 0;
272     return h;
273     }
274 //-----------------------------------------------------------------------------
275 /*
276  * No error condition is checked here at this stage, but it may happen
277  * if/when reliable messaging is implemented.
278  */
279 int ipq_destroy_handle(struct ipq_handle *h)
280 {
281     if (h)
282         {
283         close(h->fd);
284         free(h);
285         }
286     return 0;
287 }
288 //-----------------------------------------------------------------------------
289 int ipq_set_mode(const struct ipq_handle *h,
290                  u_int8_t mode, size_t range)
291 {
292     #define FAKE_ARRAY_SIZE 16
293     struct
294         {
295         struct nlmsghdr nlh;
296         ipq_peer_msg_t pm;
297         char s[FAKE_ARRAY_SIZE];
298         } req;
299     
300     memset(&req, 0, sizeof(req));
301     req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req)-FAKE_ARRAY_SIZE);
302     req.nlh.nlmsg_flags = NLM_F_REQUEST;
303     req.nlh.nlmsg_type = IPQM_MODE;
304     req.nlh.nlmsg_pid = h->local.nl_pid;
305     req.pm.msg.mode.value = mode;
306     req.pm.msg.mode.range = range;
307     return ipq_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
308     //return ipq_netlink_sendto(h, (void *)&req, sizeof(req));
309 }
310 //-----------------------------------------------------------------------------
311 /*
312  * timeout is in microseconds (1 second is 1000000 (1 million) microseconds)
313  *
314  */
315 ssize_t ipq_read(const struct ipq_handle *h,
316                  unsigned char *buf, size_t len, int timeout)
317 {
318     return ipq_netlink_recvfrom(h, buf, len, timeout);
319 }
320 //-----------------------------------------------------------------------------
321 int ipq_message_type(const unsigned char *buf)
322 {
323     return((struct nlmsghdr*)buf)->nlmsg_type;
324 }
325 //-----------------------------------------------------------------------------
326 ipq_packet_msg_t *ipq_get_packet(const unsigned char *buf)
327 {
328     return(ipq_packet_msg_t *)(NLMSG_DATA((struct nlmsghdr *)(buf)));
329 }
330 //-----------------------------------------------------------------------------
331 int ipq_set_verdict(const struct ipq_handle *h,
332                     ipq_id_t id,
333                     unsigned int verdict,
334                     size_t data_len,
335                     unsigned char *buf)
336 {
337     unsigned char nvecs;
338     size_t tlen;
339     struct nlmsghdr nlh;
340     ipq_peer_msg_t pm;
341     struct iovec iov[3];
342     struct msghdr msg;
343
344     memset(&nlh, 0, sizeof(nlh));
345     nlh.nlmsg_flags = NLM_F_REQUEST;
346     nlh.nlmsg_type = IPQM_VERDICT;
347     nlh.nlmsg_pid = h->local.nl_pid;
348     memset(&pm, 0, sizeof(pm));
349     pm.msg.verdict.value = verdict;
350     pm.msg.verdict.id = id;
351     pm.msg.verdict.data_len = data_len;
352     iov[0].iov_base = &nlh;
353     iov[0].iov_len = sizeof(nlh);
354     iov[1].iov_base = &pm;
355     iov[1].iov_len = sizeof(pm);
356     tlen = sizeof(nlh) + sizeof(pm);
357     nvecs = 2;
358     if (data_len && buf)
359         {
360         iov[2].iov_base = buf;
361         iov[2].iov_len = data_len;
362         tlen += data_len;
363         nvecs++;
364         }
365     msg.msg_name = (void *)&h->peer;
366     msg.msg_namelen = sizeof(h->peer);
367     msg.msg_iov = iov;
368     msg.msg_iovlen = nvecs;
369     msg.msg_control = NULL;
370     msg.msg_controllen = 0;
371     msg.msg_flags = 0;
372     nlh.nlmsg_len = tlen;
373     return ipq_netlink_sendmsg(h, &msg, 0);
374 }
375 //-----------------------------------------------------------------------------
376 char *ipq_errstr(void)
377 {
378     return ipq_strerror(ipq_errno);
379 }
380 //-----------------------------------------------------------------------------
381 /*void ipq_perror(const char *s)
382 {
383     if (s)
384         fputs(s, stderr);
385     else
386         fputs("ERROR", stderr);
387     if (ipq_errno)
388         fprintf(stderr, ": %s", ipq_errstr());
389     if (errno)
390         fprintf(stderr, ": %s", strerror(errno));
391     fputc('\n', stderr);
392 }*/
393 //-----------------------------------------------------------------------------
394
395