2 * libipulog.c, $Revision: 5371 $
4 * netfilter ULOG userspace library.
6 * (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
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
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.
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
21 * This library is still under development, so be aware of sudden interface
24 * $Id: libipulog.c 5371 2005-05-04 01:22:18Z laforge $
32 #include <libipulog/libipulog.h>
34 #include <proper/prop.h>
41 struct sockaddr_nl local;
42 struct sockaddr_nl peer;
43 struct nlmsghdr* last_nlhdr;
49 int ipulog_errno = IPULOG_ERR_NONE;
51 struct ipulog_errmap_t
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" },
71 ipulog_netlink_recvfrom(const struct ipulog_handle *h,
72 unsigned char *buf, size_t len)
78 if (len < sizeof(struct nlmsgerr)) {
79 ipulog_errno = IPULOG_ERR_RECVBUF;
82 addrlen = sizeof(h->peer);
83 status = recvfrom(h->fd, buf, len, 0, (struct sockaddr *)&h->peer,
86 ipulog_errno = IPULOG_ERR_RECV;
89 if (addrlen != sizeof (h->peer)) {
90 ipulog_errno = IPULOG_ERR_RECV;
93 if (h->peer.nl_pid != 0) {
94 ipulog_errno = IPULOG_ERR_RECV;
98 ipulog_errno = IPULOG_ERR_NLEOF;
101 nlh = (struct nlmsghdr *)buf;
102 if (nlh->nlmsg_flags & MSG_TRUNC || status > len) {
103 ipulog_errno = IPULOG_ERR_TRUNC;
111 char *ipulog_strerror(int errcode)
113 if (errcode < 0 || errcode > IPULOG_MAXERR)
114 errcode = IPULOG_ERR_IMPL;
115 return ipulog_errmap[errcode].message;
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)
121 if (group < 1 || group > 32)
123 ipulog_errno = IPULOG_ERR_INVGR;
126 return (1 << (group - 1));
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)
133 struct ipulog_handle *h;
136 h = (struct ipulog_handle *) malloc(sizeof(struct ipulog_handle));
139 ipulog_errno = IPULOG_ERR_HANDLE;
142 memset(h, 0, sizeof(struct ipulog_handle));
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));
153 ipulog_errno = IPULOG_ERR_SOCKET;
159 h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_NFLOG);
162 ipulog_errno = IPULOG_ERR_SOCKET;
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));
174 ipulog_errno = IPULOG_ERR_BIND;
180 memset(&h->peer, 0, sizeof(struct sockaddr_nl));
181 h->peer.nl_family = AF_NETLINK;
183 h->peer.nl_groups = gmask;
185 status = setsockopt(h->fd, SOL_SOCKET, SO_RCVBUF, &rcvbufsize,
189 ipulog_errno = IPULOG_ERR_RECVBUF;
198 /* destroy a ipulog handle */
199 void ipulog_destroy_handle(struct ipulog_handle *h)
206 int ipulog_set_mode()
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)
215 return ipulog_netlink_recvfrom(h, buf, len);
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,
224 struct nlmsghdr *nlh;
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;
234 if (!h->last_nlhdr) {
235 /* fist message in buffer */
236 nlh = (struct nlmsghdr *) buf;
237 if (!NLMSG_OK(nlh, len)) {
239 ipulog_errno = IPULOG_ERR_INVNL;
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;
252 /* calculate remaining lenght from lasthdr to end of buffer */
254 ((unsigned char *)h->last_nlhdr - buf));
255 nlh = NLMSG_NEXT(h->last_nlhdr, remain_len);
260 return NLMSG_DATA(nlh);
263 /* print a human readable description of the last error to stderr */
264 void ipulog_perror(const char *s)
269 fputs("ERROR", stderr);
271 fprintf(stderr, ": %s", ipulog_strerror(ipulog_errno));
273 fprintf(stderr, ": %s", strerror(errno));