replaced most pthread_mutex/cond_t by wrapped mutex/condvar_t variant
[strongswan.git] / src / charon / plugins / kernel_netlink / kernel_netlink_shared.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include <sys/socket.h>
19 #include <linux/netlink.h>
20 #include <linux/rtnetlink.h>
21 #include <errno.h>
22 #include <unistd.h>
23
24 #include "kernel_netlink_shared.h"
25
26 #include <daemon.h>
27 #include <utils/mutex.h>
28
29 typedef struct private_netlink_socket_t private_netlink_socket_t;
30
31 /**
32 * Private variables and functions of netlink_socket_t class.
33 */
34 struct private_netlink_socket_t {
35 /**
36 * public part of the netlink_socket_t object.
37 */
38 netlink_socket_t public;
39
40 /**
41 * mutex to lock access to netlink socket
42 */
43 mutex_t *mutex;
44
45 /**
46 * current sequence number for netlink request
47 */
48 int seq;
49
50 /**
51 * netlink socket
52 */
53 int socket;
54 };
55
56 /**
57 * Implementation of netlink_socket_t.send
58 */
59 static status_t netlink_send(private_netlink_socket_t *this, struct nlmsghdr *in,
60 struct nlmsghdr **out, size_t *out_len)
61 {
62 int len, addr_len;
63 struct sockaddr_nl addr;
64 chunk_t result = chunk_empty, tmp;
65 struct nlmsghdr *msg, peek;
66
67 this->mutex->lock(this->mutex);
68
69 in->nlmsg_seq = ++this->seq;
70 in->nlmsg_pid = getpid();
71
72 memset(&addr, 0, sizeof(addr));
73 addr.nl_family = AF_NETLINK;
74 addr.nl_pid = 0;
75 addr.nl_groups = 0;
76
77 while (TRUE)
78 {
79 len = sendto(this->socket, in, in->nlmsg_len, 0,
80 (struct sockaddr*)&addr, sizeof(addr));
81
82 if (len != in->nlmsg_len)
83 {
84 if (errno == EINTR)
85 {
86 /* interrupted, try again */
87 continue;
88 }
89 this->mutex->unlock(this->mutex);
90 DBG1(DBG_KNL, "error sending to netlink socket: %s", strerror(errno));
91 return FAILED;
92 }
93 break;
94 }
95
96 while (TRUE)
97 {
98 char buf[4096];
99 tmp.len = sizeof(buf);
100 tmp.ptr = buf;
101 msg = (struct nlmsghdr*)tmp.ptr;
102
103 memset(&addr, 0, sizeof(addr));
104 addr.nl_family = AF_NETLINK;
105 addr.nl_pid = getpid();
106 addr.nl_groups = 0;
107 addr_len = sizeof(addr);
108
109 len = recvfrom(this->socket, tmp.ptr, tmp.len, 0,
110 (struct sockaddr*)&addr, &addr_len);
111
112 if (len < 0)
113 {
114 if (errno == EINTR)
115 {
116 DBG1(DBG_KNL, "got interrupted");
117 /* interrupted, try again */
118 continue;
119 }
120 DBG1(DBG_KNL, "error reading from netlink socket: %s", strerror(errno));
121 this->mutex->unlock(this->mutex);
122 free(result.ptr);
123 return FAILED;
124 }
125 if (!NLMSG_OK(msg, len))
126 {
127 DBG1(DBG_KNL, "received corrupted netlink message");
128 this->mutex->unlock(this->mutex);
129 free(result.ptr);
130 return FAILED;
131 }
132 if (msg->nlmsg_seq != this->seq)
133 {
134 DBG1(DBG_KNL, "received invalid netlink sequence number");
135 if (msg->nlmsg_seq < this->seq)
136 {
137 continue;
138 }
139 this->mutex->unlock(this->mutex);
140 free(result.ptr);
141 return FAILED;
142 }
143
144 tmp.len = len;
145 result.ptr = realloc(result.ptr, result.len + tmp.len);
146 memcpy(result.ptr + result.len, tmp.ptr, tmp.len);
147 result.len += tmp.len;
148
149 /* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
150 * numbers to detect multi header messages */
151 len = recvfrom(this->socket, &peek, sizeof(peek), MSG_PEEK | MSG_DONTWAIT,
152 (struct sockaddr*)&addr, &addr_len);
153
154 if (len == sizeof(peek) && peek.nlmsg_seq == this->seq)
155 {
156 /* seems to be multipart */
157 continue;
158 }
159 break;
160 }
161
162 *out_len = result.len;
163 *out = (struct nlmsghdr*)result.ptr;
164
165 this->mutex->unlock(this->mutex);
166
167 return SUCCESS;
168 }
169
170 /**
171 * Implementation of netlink_socket_t.send_ack.
172 */
173 static status_t netlink_send_ack(private_netlink_socket_t *this, struct nlmsghdr *in)
174 {
175 struct nlmsghdr *out, *hdr;
176 size_t len;
177
178 if (netlink_send(this, in, &out, &len) != SUCCESS)
179 {
180 return FAILED;
181 }
182 hdr = out;
183 while (NLMSG_OK(hdr, len))
184 {
185 switch (hdr->nlmsg_type)
186 {
187 case NLMSG_ERROR:
188 {
189 struct nlmsgerr* err = (struct nlmsgerr*)NLMSG_DATA(hdr);
190
191 if (err->error)
192 {
193 if (-err->error == EEXIST)
194 { /* do not report existing routes */
195 free(out);
196 return ALREADY_DONE;
197 }
198 DBG1(DBG_KNL, "received netlink error: %s (%d)",
199 strerror(-err->error), -err->error);
200 free(out);
201 return FAILED;
202 }
203 free(out);
204 return SUCCESS;
205 }
206 default:
207 hdr = NLMSG_NEXT(hdr, len);
208 continue;
209 case NLMSG_DONE:
210 break;
211 }
212 break;
213 }
214 DBG1(DBG_KNL, "netlink request not acknowledged");
215 free(out);
216 return FAILED;
217 }
218
219 /**
220 * Implementation of netlink_socket_t.destroy.
221 */
222 static void destroy(private_netlink_socket_t *this)
223 {
224 close(this->socket);
225 this->mutex->destroy(this->mutex);
226 free(this);
227 }
228
229 /**
230 * Described in header.
231 */
232 netlink_socket_t *netlink_socket_create(int protocol) {
233 private_netlink_socket_t *this = malloc_thing(private_netlink_socket_t);
234 struct sockaddr_nl addr;
235
236 /* public functions */
237 this->public.send = (status_t(*)(netlink_socket_t*,struct nlmsghdr*, struct nlmsghdr**, size_t*))netlink_send;
238 this->public.send_ack = (status_t(*)(netlink_socket_t*,struct nlmsghdr*))netlink_send_ack;
239 this->public.destroy = (void(*)(netlink_socket_t*))destroy;
240
241 /* private members */
242 this->seq = 200;
243 this->mutex = mutex_create(MUTEX_DEFAULT);
244
245 memset(&addr, 0, sizeof(addr));
246 addr.nl_family = AF_NETLINK;
247
248 this->socket = socket(AF_NETLINK, SOCK_RAW, protocol);
249 if (this->socket <= 0)
250 {
251 charon->kill(charon, "unable to create netlink socket");
252 }
253
254 addr.nl_groups = 0;
255 if (bind(this->socket, (struct sockaddr*)&addr, sizeof(addr)))
256 {
257 charon->kill(charon, "unable to bind netlink socket");
258 }
259
260 return &this->public;
261 }
262
263 /**
264 * Described in header.
265 */
266 void netlink_add_attribute(struct nlmsghdr *hdr, int rta_type, chunk_t data,
267 size_t buflen)
268 {
269 struct rtattr *rta;
270
271 if (NLMSG_ALIGN(hdr->nlmsg_len) + RTA_ALIGN(data.len) > buflen)
272 {
273 DBG1(DBG_KNL, "unable to add attribute, buffer too small");
274 return;
275 }
276
277 rta = (struct rtattr*)(((char*)hdr) + NLMSG_ALIGN(hdr->nlmsg_len));
278 rta->rta_type = rta_type;
279 rta->rta_len = RTA_LENGTH(data.len);
280 memcpy(RTA_DATA(rta), data.ptr, data.len);
281 hdr->nlmsg_len = NLMSG_ALIGN(hdr->nlmsg_len) + rta->rta_len;
282 }