]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ipq_linux/libipq.c
Добавление исходников
[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 int ipq_get_msgerr(const unsigned char *buf)
327 {
328     struct nlmsghdr *h = (struct nlmsghdr *)buf;
329     struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
330     return -err->error;
331 }
332 //-----------------------------------------------------------------------------
333 ipq_packet_msg_t *ipq_get_packet(const unsigned char *buf)
334 {
335     return(ipq_packet_msg_t *)(NLMSG_DATA((struct nlmsghdr *)(buf)));
336 }
337 //-----------------------------------------------------------------------------
338 int ipq_set_verdict(const struct ipq_handle *h,
339                     ipq_id_t id,
340                     unsigned int verdict,
341                     size_t data_len,
342                     unsigned char *buf)
343 {
344     unsigned char nvecs;
345     size_t tlen;
346     struct nlmsghdr nlh;
347     ipq_peer_msg_t pm;
348     struct iovec iov[3];
349     struct msghdr msg;
350
351     memset(&nlh, 0, sizeof(nlh));
352     nlh.nlmsg_flags = NLM_F_REQUEST;
353     nlh.nlmsg_type = IPQM_VERDICT;
354     nlh.nlmsg_pid = h->local.nl_pid;
355     memset(&pm, 0, sizeof(pm));
356     pm.msg.verdict.value = verdict;
357     pm.msg.verdict.id = id;
358     pm.msg.verdict.data_len = data_len;
359     iov[0].iov_base = &nlh;
360     iov[0].iov_len = sizeof(nlh);
361     iov[1].iov_base = &pm;
362     iov[1].iov_len = sizeof(pm);
363     tlen = sizeof(nlh) + sizeof(pm);
364     nvecs = 2;
365     if (data_len && buf)
366         {
367         iov[2].iov_base = buf;
368         iov[2].iov_len = data_len;
369         tlen += data_len;
370         nvecs++;
371         }
372     msg.msg_name = (void *)&h->peer;
373     msg.msg_namelen = sizeof(h->peer);
374     msg.msg_iov = iov;
375     msg.msg_iovlen = nvecs;
376     msg.msg_control = NULL;
377     msg.msg_controllen = 0;
378     msg.msg_flags = 0;
379     nlh.nlmsg_len = tlen;
380     return ipq_netlink_sendmsg(h, &msg, 0);
381 }
382 //-----------------------------------------------------------------------------
383 /* Not implemented yet */
384 int ipq_ctl(const struct ipq_handle __attribute__((unused)) * handle, int __attribute__((unused)) request, ...)
385 {
386     return 1;
387 }
388 //-----------------------------------------------------------------------------
389 /*char *ipq_errstr(void)
390 {
391     return ipq_strerror(ipq_errno);
392 }*/
393 //-----------------------------------------------------------------------------
394 /*void ipq_perror(const char *s)
395 {
396     if (s)
397         fputs(s, stderr);
398     else
399         fputs("ERROR", stderr);
400     if (ipq_errno)
401         fprintf(stderr, ": %s", ipq_errstr());
402     if (errno)
403         fprintf(stderr, ": %s", strerror(errno));
404     fputc('\n', stderr);
405 }*/
406 //-----------------------------------------------------------------------------
407
408