Importing all of DRL, including ulogd and all of its files.
[distributedratelimiting.git] / libipulog / libipulog.c
1 /* 
2  * libipulog.c, $Revision: 5371 $
3  *
4  * netfilter ULOG userspace library.
5  *
6  * (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 
10  *  as published by the Free Software Foundation
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * This library is still under development, so be aware of sudden interface
22  * changes
23  *
24  * $Id: libipulog.c 5371 2005-05-04 01:22:18Z laforge $
25  */
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <net/if.h>
32 #include <libipulog/libipulog.h>
33 #if HAVE_LIBPROPER
34 #include <proper/prop.h>
35 #endif
36
37 struct ipulog_handle
38 {
39         int fd;
40         u_int8_t blocking;
41         struct sockaddr_nl local;
42         struct sockaddr_nl peer;
43         struct nlmsghdr* last_nlhdr;
44 };
45
46 /* internal */
47
48
49 int ipulog_errno = IPULOG_ERR_NONE;
50
51 struct ipulog_errmap_t 
52 {
53         int errcode;
54         char *message;
55 } ipulog_errmap[] = 
56 {
57         { IPULOG_ERR_NONE, "No error" },
58         { IPULOG_ERR_IMPL, "Not implemented yet" },
59         { IPULOG_ERR_HANDLE, "Unable to create netlink handle" },
60         { IPULOG_ERR_SOCKET, "Unable to create netlink socket" },
61         { IPULOG_ERR_BIND, "Unable to bind netlink socket" },
62         { IPULOG_ERR_RECVBUF, "Receive buffer size invalid" },
63         { IPULOG_ERR_RECV, "Error during netlink receive" },
64         { IPULOG_ERR_NLEOF, "Received EOF on netlink socket" },
65         { IPULOG_ERR_TRUNC, "Receive message truncated" },
66         { IPULOG_ERR_INVGR, "Invalid group specified" },
67         { IPULOG_ERR_INVNL, "Invalid netlink message" },
68 };
69
70 static ssize_t 
71 ipulog_netlink_recvfrom(const struct ipulog_handle *h,
72                         unsigned char *buf, size_t len)
73 {
74         socklen_t addrlen;
75         int status;
76         struct nlmsghdr *nlh;
77         
78         if (len < sizeof(struct nlmsgerr)) {
79                 ipulog_errno = IPULOG_ERR_RECVBUF;
80                 return -1; 
81         }
82         addrlen = sizeof(h->peer);
83         status = recvfrom(h->fd, buf, len, 0, (struct sockaddr *)&h->peer,      
84                         &addrlen);
85         if (status < 0) {
86                 ipulog_errno = IPULOG_ERR_RECV;
87                 return status;
88         }
89         if (addrlen != sizeof (h->peer)) {
90                 ipulog_errno = IPULOG_ERR_RECV;
91                 return -1;
92         }       
93         if (h->peer.nl_pid != 0) {
94                 ipulog_errno = IPULOG_ERR_RECV;
95                 return -1;
96         }
97         if (status == 0) {
98                 ipulog_errno = IPULOG_ERR_NLEOF;
99                 return -1;
100         }
101         nlh = (struct nlmsghdr *)buf;
102         if (nlh->nlmsg_flags & MSG_TRUNC || status > len) {
103                 ipulog_errno = IPULOG_ERR_TRUNC;
104                 return -1;
105         }
106         return status;
107 }
108
109 /* public */
110
111 char *ipulog_strerror(int errcode)
112 {
113         if (errcode < 0 || errcode > IPULOG_MAXERR)
114                 errcode = IPULOG_ERR_IMPL;
115         return ipulog_errmap[errcode].message;
116 }
117
118 /* convert a netlink group (1-32) to a group_mask suitable for create_handle */
119 u_int32_t ipulog_group2gmask(u_int32_t group)
120 {
121         if (group < 1 || group > 32)
122         {
123                 ipulog_errno = IPULOG_ERR_INVGR;
124                 return 0;
125         }
126         return (1 << (group - 1));
127 }
128
129 /* create a ipulog handle for the reception of packets sent to gmask */
130 struct ipulog_handle *ipulog_create_handle(u_int32_t gmask, 
131                                            u_int32_t rcvbufsize)
132 {
133         struct ipulog_handle *h;
134         int status;
135
136         h = (struct ipulog_handle *) malloc(sizeof(struct ipulog_handle));
137         if (h == NULL)
138         {
139                 ipulog_errno = IPULOG_ERR_HANDLE;
140                 return NULL;
141         }
142         memset(h, 0, sizeof(struct ipulog_handle));
143 #if HAVE_LIBPROPER
144         /* Create and bind privileged netlink socket through Proper */
145         memset(&h->local, 0, sizeof(struct sockaddr_nl));
146         h->local.nl_family = AF_NETLINK;
147         h->local.nl_pid = getpid();
148         h->local.nl_groups = gmask;
149         h->fd = prop_create_socket(PF_NETLINK, SOCK_RAW, NETLINK_NFLOG,
150                                    (struct sockaddr *)&h->local, sizeof(h->local));
151         if (h->fd < 0)
152         {
153                 ipulog_errno = IPULOG_ERR_SOCKET;
154                 close(h->fd);
155                 free(h);
156                 return NULL;
157         }
158 #else
159         h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_NFLOG);
160         if (h->fd == -1)
161         {
162                 ipulog_errno = IPULOG_ERR_SOCKET;
163                 close(h->fd);
164                 free(h);
165                 return NULL;
166         }
167         memset(&h->local, 0, sizeof(struct sockaddr_nl));
168         h->local.nl_family = AF_NETLINK;
169         h->local.nl_pid = getpid();
170         h->local.nl_groups = gmask;
171         status = bind(h->fd, (struct sockaddr *)&h->local, sizeof(h->local));
172         if (status == -1)
173         {
174                 ipulog_errno = IPULOG_ERR_BIND;
175                 close(h->fd);
176                 free(h);
177                 return NULL;
178         }
179 #endif
180         memset(&h->peer, 0, sizeof(struct sockaddr_nl));
181         h->peer.nl_family = AF_NETLINK;
182         h->peer.nl_pid = 0;
183         h->peer.nl_groups = gmask;
184
185         status = setsockopt(h->fd, SOL_SOCKET, SO_RCVBUF, &rcvbufsize,
186                             sizeof(rcvbufsize));
187         if (status == -1)
188         {
189                 ipulog_errno = IPULOG_ERR_RECVBUF;
190                 close(h->fd);
191                 free(h);
192                 return NULL;
193         }
194
195         return h;
196
197
198 /* destroy a ipulog handle */
199 void ipulog_destroy_handle(struct ipulog_handle *h)
200 {
201         close(h->fd);
202         free(h);
203 }
204
205 #if 0
206 int ipulog_set_mode()
207 {
208 }
209 #endif
210
211 /* do a BLOCKING read on an ipulog handle */
212 ssize_t ipulog_read(struct ipulog_handle *h, unsigned char *buf,
213                     size_t len, int timeout)
214 {
215         return ipulog_netlink_recvfrom(h, buf, len);
216 }
217
218 /* get a pointer to the actual start of the ipulog packet,
219    use this to strip netlink header */
220 ulog_packet_msg_t *ipulog_get_packet(struct ipulog_handle *h,
221                                      const unsigned char *buf, 
222                                      size_t len)
223 {
224         struct nlmsghdr *nlh;
225         size_t remain_len;
226
227         /* if last header in handle not inside this buffer,
228          * drop reference to last header */
229         if ((unsigned char *)h->last_nlhdr > (buf + len) || 
230             (unsigned char *)h->last_nlhdr < buf) {
231                 h->last_nlhdr = NULL;
232         }
233         
234         if (!h->last_nlhdr) {
235                 /* fist message in buffer */
236                 nlh = (struct nlmsghdr *) buf;
237                 if (!NLMSG_OK(nlh, len)) {
238                         /* ERROR */
239                         ipulog_errno = IPULOG_ERR_INVNL;
240                         return NULL;
241                 }
242         } else {
243                 /* we are in n-th part of multilink message */
244                 if (h->last_nlhdr->nlmsg_type == NLMSG_DONE ||
245                     !(h->last_nlhdr->nlmsg_flags & NLM_F_MULTI)) {
246                         /* if last part in multilink message, 
247                          * or no multipart message at all: return */
248                         h->last_nlhdr = NULL;
249                         return NULL;
250                 }
251
252                 /* calculate remaining lenght from lasthdr to end of buffer */
253                 remain_len = (len - 
254                                 ((unsigned char *)h->last_nlhdr - buf));
255                 nlh = NLMSG_NEXT(h->last_nlhdr, remain_len);
256         }
257
258         h->last_nlhdr = nlh;
259
260         return NLMSG_DATA(nlh);
261 }
262
263 /* print a human readable description of the last error to stderr */
264 void ipulog_perror(const char *s)
265 {
266         if (s)
267                 fputs(s, stderr);
268         else
269                 fputs("ERROR", stderr);
270         if (ipulog_errno)
271                 fprintf(stderr, ": %s", ipulog_strerror(ipulog_errno));
272         if (errno)
273                 fprintf(stderr, ": %s", strerror(errno));
274         fputc('\n', stderr);
275 }
276