blob: c017f3ab0542c18c1e1740e3a6ecf88abb8070d9 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/*
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 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <unistd.h>
Rusty Russell7e53bf92000-03-20 07:03:28 +000025
Marc Bouchere6869a82000-03-20 06:03:29 +000026#include <libipq/libipq.h>
27
28/****************************************************************************
29 *
30 * Private interface
31 *
32 ****************************************************************************/
33
34enum {
35 IPQ_ERR_NONE = 0,
36 IPQ_ERR_IMPL,
37 IPQ_ERR_HANDLE,
38 IPQ_ERR_SOCKET,
39 IPQ_ERR_BIND,
40 IPQ_ERR_BUFFER,
41 IPQ_ERR_RECV,
42 IPQ_ERR_NLEOF,
43 IPQ_ERR_ADDRLEN,
44 IPQ_ERR_STRUNC,
45 IPQ_ERR_RTRUNC,
46 IPQ_ERR_NLRECV,
47 IPQ_ERR_SEND,
48 IPQ_ERR_SUPP,
49 IPQ_ERR_RECVBUF
50};
51#define IPQ_MAXERR IPQ_ERR_RECVBUF
52
53struct ipq_errmap_t {
54 int errcode;
55 char *message;
56} ipq_errmap[] = {
57 { IPQ_ERR_NONE, "Unknown error" },
58 { IPQ_ERR_IMPL, "Implementation error" },
59 { IPQ_ERR_HANDLE, "Unable to create netlink handle" },
60 { IPQ_ERR_SOCKET, "Unable to create netlink socket" },
61 { IPQ_ERR_BIND, "Unable to bind netlink socket" },
62 { IPQ_ERR_BUFFER, "Unable to allocate buffer" },
63 { IPQ_ERR_RECV, "Failed to receive netlink message" },
64 { IPQ_ERR_NLEOF, "Received EOF on netlink socket" },
65 { IPQ_ERR_ADDRLEN, "Invalid peer address length" },
66 { IPQ_ERR_STRUNC, "Sent message truncated" },
67 { IPQ_ERR_RTRUNC, "Received message truncated" },
68 { IPQ_ERR_NLRECV, "Received error from netlink" },
69 { IPQ_ERR_SEND, "Failed to send netlink message" },
70 { IPQ_ERR_SUPP, "Operation not supported" },
71 { IPQ_ERR_RECVBUF, "Receive buffer size invalid" }
72};
73
74static int ipq_errno = IPQ_ERR_NONE;
75
76static ssize_t ipq_netlink_sendto(const struct ipq_handle *h,
77 const void *msg, size_t len);
Rusty Russell7e53bf92000-03-20 07:03:28 +000078
Marc Bouchere6869a82000-03-20 06:03:29 +000079static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h,
80 unsigned char *buf, size_t len);
Rusty Russell7e53bf92000-03-20 07:03:28 +000081
Marc Bouchere6869a82000-03-20 06:03:29 +000082static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h,
83 const struct msghdr *msg,
84 unsigned int flags);
Rusty Russell7e53bf92000-03-20 07:03:28 +000085
Marc Bouchere6869a82000-03-20 06:03:29 +000086static char *ipq_strerror(int errcode);
87
88static ssize_t ipq_netlink_sendto(const struct ipq_handle *h,
89 const void *msg, size_t len)
90{
91 int status = sendto(h->fd, msg, len, 0,
92 (struct sockaddr *)&h->peer, sizeof(h->peer));
93 if (status < 0)
94 ipq_errno = IPQ_ERR_SEND;
Rusty Russell7e53bf92000-03-20 07:03:28 +000095 return status;
Marc Bouchere6869a82000-03-20 06:03:29 +000096}
97
98static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h,
99 const struct msghdr *msg,
100 unsigned int flags)
101{
102 int status = sendmsg(h->fd, msg, flags);
103 if (status < 0)
104 ipq_errno = IPQ_ERR_SEND;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000105 return status;
Marc Bouchere6869a82000-03-20 06:03:29 +0000106}
107
108static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h,
109 unsigned char *buf, size_t len)
110{
111 int addrlen, status;
112 struct nlmsghdr *nlh;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000113
Marc Bouchere6869a82000-03-20 06:03:29 +0000114 if (len < sizeof(struct nlmsgerr)) {
115 ipq_errno = IPQ_ERR_RECVBUF;
116 return -1;
117 }
118 addrlen = sizeof(h->peer);
119 status = recvfrom(h->fd, buf, len, 0,
120 (struct sockaddr *)&h->peer, &addrlen);
121 if (status < 0) {
122 ipq_errno = IPQ_ERR_RECV;
123 return status;
124 }
125 if (addrlen != sizeof(h->peer)) {
126 ipq_errno = IPQ_ERR_RECV;
127 return -1;
128 }
129 if (status == 0) {
130 ipq_errno = IPQ_ERR_NLEOF;
131 return -1;
132 }
133 nlh = (struct nlmsghdr *)buf;
134 if (nlh->nlmsg_flags & MSG_TRUNC || nlh->nlmsg_len > status) {
135 ipq_errno = IPQ_ERR_RTRUNC;
136 return -1;
137 }
138 return status;
139}
140
141static char *ipq_strerror(int errcode)
142{
143 if (errcode < 0 || errcode > IPQ_MAXERR)
144 errcode = IPQ_ERR_IMPL;
145 return ipq_errmap[errcode].message;
146}
147
148/****************************************************************************
149 *
150 * Public interface
151 *
152 ****************************************************************************/
153
Rusty Russell7e53bf92000-03-20 07:03:28 +0000154/*
Marc Bouchere6869a82000-03-20 06:03:29 +0000155 * Create and initialise an ipq handle.
156 * FIXME: implement flags.
157 */
158struct ipq_handle *ipq_create_handle(u_int32_t flags)
159{
160 int status;
161 struct ipq_handle *h;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000162
Marc Bouchere6869a82000-03-20 06:03:29 +0000163 h = (struct ipq_handle *)malloc(sizeof(struct ipq_handle));
164 if (h == NULL) {
165 ipq_errno = IPQ_ERR_HANDLE;
166 return NULL;
167 }
168 memset(h, 0, sizeof(struct ipq_handle));
169 h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_FIREWALL);
170 if (h->fd == -1) {
171 ipq_errno = IPQ_ERR_SOCKET;
172 close(h->fd);
173 free(h);
174 return NULL;
175 }
176 memset(&h->local, 0, sizeof(struct sockaddr_nl));
177 h->local.nl_family = AF_NETLINK;
178 h->local.nl_pid = getpid();
179 h->local.nl_groups = 0;
180 status = bind(h->fd, (struct sockaddr *)&h->local, sizeof(h->local));
181 if (status == -1) {
182 ipq_errno = IPQ_ERR_BIND;
183 close(h->fd);
184 free(h);
185 return NULL;
186 }
187 memset(&h->peer, 0, sizeof(struct sockaddr_nl));
188 h->peer.nl_family = AF_NETLINK;
189 h->peer.nl_pid = 0;
190 h->peer.nl_groups = 0;
191 return h;
192}
193
194/*
Rusty Russell7e53bf92000-03-20 07:03:28 +0000195 * No error condition is checked here at this stage, but it may happen
Marc Bouchere6869a82000-03-20 06:03:29 +0000196 * if/when reliable messaging is implemented.
197 */
198int ipq_destroy_handle(struct ipq_handle *h)
199{
200 if (h) {
201 close(h->fd);
202 free(h);
203 }
204 return 0;
205}
206
207int ipq_set_mode(const struct ipq_handle *h,
208 u_int8_t mode, size_t range)
209{
210 struct {
211 struct nlmsghdr nlh;
212 ipq_peer_msg_t pm;
213 } req;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000214
Marc Bouchere6869a82000-03-20 06:03:29 +0000215 memset(&req, 0, sizeof(req));
216 req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req));
217 req.nlh.nlmsg_flags = NLM_F_REQUEST;
218 req.nlh.nlmsg_type = IPQM_MODE;
219 req.nlh.nlmsg_pid = h->local.nl_pid;
220 req.pm.msg.mode.value = mode;
221 req.pm.msg.mode.range = range;
222 return ipq_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
223}
224
225/* Note: timeout is not yet implemented */
226ssize_t ipq_read(const struct ipq_handle *h,
227 unsigned char *buf, size_t len, int timeout)
228{
229 return ipq_netlink_recvfrom(h, buf, len);
230}
231
232int ipq_message_type(const unsigned char *buf)
233{
234 return ((struct nlmsghdr*)buf)->nlmsg_type;
235}
236
237int ipq_get_msgerr(const unsigned char *buf)
238{
239 struct nlmsghdr *h = (struct nlmsghdr *)buf;
240 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
241 return -err->error;
242}
243
244ipq_packet_msg_t *ipq_get_packet(const unsigned char *buf)
245{
246 return NLMSG_DATA((struct nlmsghdr *)(buf));
247}
248
249int ipq_set_verdict(const struct ipq_handle *h,
250 unsigned long id,
251 unsigned int verdict,
252 size_t data_len,
253 unsigned char *buf)
254{
255 unsigned char nvecs;
256 size_t tlen;
257 struct nlmsghdr nlh;
258 ipq_peer_msg_t pm;
259 struct iovec iov[3];
260 struct msghdr msg;
261
262 memset(&nlh, 0, sizeof(nlh));
263 nlh.nlmsg_flags = NLM_F_REQUEST;
264 nlh.nlmsg_type = IPQM_VERDICT;
265 nlh.nlmsg_pid = h->local.nl_pid;
266 memset(&pm, 0, sizeof(pm));
267 pm.msg.verdict.value = verdict;
268 pm.msg.verdict.id = id;
269 pm.msg.verdict.data_len = data_len;
270 iov[0].iov_base = &nlh;
271 iov[0].iov_len = sizeof(nlh);
272 iov[1].iov_base = &pm;
273 iov[1].iov_len = sizeof(pm);
274 tlen = sizeof(nlh) + sizeof(pm);
275 nvecs = 2;
276 if (data_len && buf) {
277 iov[2].iov_base = buf;
278 iov[2].iov_len = data_len;
279 tlen += data_len;
280 nvecs++;
281 }
282 msg.msg_name = (void *)&h->peer;
283 msg.msg_namelen = sizeof(h->peer);
284 msg.msg_iov = iov;
285 msg.msg_iovlen = nvecs;
286 msg.msg_control = NULL;
287 msg.msg_controllen = 0;
288 msg.msg_flags = 0;
289 nlh.nlmsg_len = tlen;
290 return ipq_netlink_sendmsg(h, &msg, 0);
291}
292
293/* Not implemented yet */
294int ipq_ctl(const struct ipq_handle *h, int request, ...)
295{
296 return 1;
297}
298
299void ipq_perror(const char *s)
300{
301 if (s)
302 fputs(s, stderr);
303 else
304 fputs("ERROR", stderr);
305 if (ipq_errno)
306 fprintf(stderr, ": %s", ipq_strerror(ipq_errno));
307 if (errno)
308 fprintf(stderr, ": %s", strerror(errno));
309 fputc('\n', stderr);
310}