4 * IPQ userspace library.
6 * Please note that this library is still developmental, and there may
9 * Author: James Morris <jmorris@intercode.com.au>
11 * 07-11-2001 Modified by Fernando Anton to add support for IPv6.
13 * Copyright (c) 2000-2001 Netfilter Core Team
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.
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.
32 #include <sys/types.h>
36 /****************************************************************************
40 ****************************************************************************/
62 #define IPQ_MAXERR IPQ_ERR_PROTOCOL
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"}
88 static int ipq_errno = IPQ_ERR_NONE;
90 static ssize_t ipq_netlink_sendto(const struct ipq_handle *h,
91 const void *msg, size_t len);
93 static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h,
94 unsigned char *buf, size_t len,
97 static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h,
98 const struct msghdr *msg,
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)
106 int status = sendto(h->fd, msg, len, 0,
107 (struct sockaddr *)&h->peer, sizeof(h->peer));
109 ipq_errno = IPQ_ERR_SEND;
112 //-----------------------------------------------------------------------------
113 static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h,
114 const struct msghdr *msg,
117 int status = sendmsg(h->fd, msg, flags);
119 ipq_errno = IPQ_ERR_SEND;
122 //-----------------------------------------------------------------------------
123 static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h,
124 unsigned char *buf, size_t len,
129 struct nlmsghdr *nlh;
131 if (len < sizeof(struct nlmsgerr))
133 ipq_errno = IPQ_ERR_RECVBUF;
136 addrlen = sizeof(h->peer);
146 /* non-block non-timeout */
152 tv.tv_sec = timeout / 1000000;
153 tv.tv_usec = timeout % 1000000;
157 FD_SET(h->fd, &read_fds);
158 ret = select(h->fd+1, &read_fds, NULL, NULL, &tv);
167 ipq_errno = IPQ_ERR_RECV;
171 if (!FD_ISSET(h->fd, &read_fds))
173 ipq_errno = IPQ_ERR_TIMEOUT;
177 status = recvfrom(h->fd, buf, len, 0,
178 (struct sockaddr *)&h->peer, &addrlen);
181 ipq_errno = IPQ_ERR_RECV;
184 if (addrlen != sizeof(h->peer))
186 ipq_errno = IPQ_ERR_RECV;
189 if (h->peer.nl_pid != 0)
191 ipq_errno = IPQ_ERR_RECV;
196 ipq_errno = IPQ_ERR_NLEOF;
199 nlh = (struct nlmsghdr *)buf;
200 if (nlh->nlmsg_flags & MSG_TRUNC || (int)nlh->nlmsg_len > status)
202 ipq_errno = IPQ_ERR_RTRUNC;
207 //-----------------------------------------------------------------------------
208 /*static char *ipq_strerror(int errcode)
210 if (errcode < 0 || errcode > IPQ_MAXERR)
211 errcode = IPQ_ERR_IMPL;
212 return ipq_errmap[errcode].message;
215 /****************************************************************************
219 ****************************************************************************/
222 * Create and initialise an ipq handle.
224 struct ipq_handle *ipq_create_handle(u_int32_t __attribute__((unused)) flags, u_int32_t protocol)
227 struct ipq_handle *h;
229 h = (struct ipq_handle *)malloc(sizeof(struct ipq_handle));
232 ipq_errno = IPQ_ERR_HANDLE;
236 memset(h, 0, sizeof(struct ipq_handle));
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);
244 ipq_errno = IPQ_ERR_PROTOCOL;
251 ipq_errno = IPQ_ERR_SOCKET;
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));
263 ipq_errno = IPQ_ERR_BIND;
268 memset(&h->peer, 0, sizeof(struct sockaddr_nl));
269 h->peer.nl_family = AF_NETLINK;
271 h->peer.nl_groups = 0;
274 //-----------------------------------------------------------------------------
276 * No error condition is checked here at this stage, but it may happen
277 * if/when reliable messaging is implemented.
279 int ipq_destroy_handle(struct ipq_handle *h)
288 //-----------------------------------------------------------------------------
289 int ipq_set_mode(const struct ipq_handle *h,
290 u_int8_t mode, size_t range)
292 #define FAKE_ARRAY_SIZE 16
297 char s[FAKE_ARRAY_SIZE];
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));
310 //-----------------------------------------------------------------------------
312 * timeout is in microseconds (1 second is 1000000 (1 million) microseconds)
315 ssize_t ipq_read(const struct ipq_handle *h,
316 unsigned char *buf, size_t len, int timeout)
318 return ipq_netlink_recvfrom(h, buf, len, timeout);
320 //-----------------------------------------------------------------------------
321 int ipq_message_type(const unsigned char *buf)
323 return((struct nlmsghdr*)buf)->nlmsg_type;
325 //-----------------------------------------------------------------------------
326 int ipq_get_msgerr(const unsigned char *buf)
328 struct nlmsghdr *h = (struct nlmsghdr *)buf;
329 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
332 //-----------------------------------------------------------------------------
333 ipq_packet_msg_t *ipq_get_packet(const unsigned char *buf)
335 return(ipq_packet_msg_t *)(NLMSG_DATA((struct nlmsghdr *)(buf)));
337 //-----------------------------------------------------------------------------
338 int ipq_set_verdict(const struct ipq_handle *h,
340 unsigned int verdict,
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 = ±
362 iov[1].iov_len = sizeof(pm);
363 tlen = sizeof(nlh) + sizeof(pm);
367 iov[2].iov_base = buf;
368 iov[2].iov_len = data_len;
372 msg.msg_name = (void *)&h->peer;
373 msg.msg_namelen = sizeof(h->peer);
375 msg.msg_iovlen = nvecs;
376 msg.msg_control = NULL;
377 msg.msg_controllen = 0;
379 nlh.nlmsg_len = tlen;
380 return ipq_netlink_sendmsg(h, &msg, 0);
382 //-----------------------------------------------------------------------------
383 /* Not implemented yet */
384 int ipq_ctl(const struct ipq_handle __attribute__((unused)) * handle, int __attribute__((unused)) request, ...)
388 //-----------------------------------------------------------------------------
389 /*char *ipq_errstr(void)
391 return ipq_strerror(ipq_errno);
393 //-----------------------------------------------------------------------------
394 /*void ipq_perror(const char *s)
399 fputs("ERROR", stderr);
401 fprintf(stderr, ": %s", ipq_errstr());
403 fprintf(stderr, ": %s", strerror(errno));
406 //-----------------------------------------------------------------------------