kernel-netlink: Add a simple send message test querying available links
[strongswan.git] / src / libhydra / 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
16 #include <sys/socket.h>
17 #include <linux/netlink.h>
18 #include <linux/rtnetlink.h>
19 #include <errno.h>
20 #include <unistd.h>
21
22 #include "kernel_netlink_shared.h"
23
24 #include <utils/debug.h>
25 #include <threading/mutex.h>
26
27 typedef struct private_netlink_socket_t private_netlink_socket_t;
28
29 /**
30 * Private variables and functions of netlink_socket_t class.
31 */
32 struct private_netlink_socket_t {
33 /**
34 * public part of the netlink_socket_t object.
35 */
36 netlink_socket_t public;
37
38 /**
39 * mutex to lock access to netlink socket
40 */
41 mutex_t *mutex;
42
43 /**
44 * current sequence number for netlink request
45 */
46 int seq;
47
48 /**
49 * netlink socket
50 */
51 int socket;
52
53 /**
54 * Enum names for Netlink messages
55 */
56 enum_name_t *names;
57 };
58
59 /**
60 * Imported from kernel_netlink_ipsec.c
61 */
62 extern enum_name_t *xfrm_msg_names;
63
64 METHOD(netlink_socket_t, netlink_send, status_t,
65 private_netlink_socket_t *this, struct nlmsghdr *in, struct nlmsghdr **out,
66 size_t *out_len)
67 {
68 union {
69 struct nlmsghdr hdr;
70 u_char bytes[4096];
71 } response;
72 struct sockaddr_nl addr;
73 chunk_t result = chunk_empty;
74 int len;
75
76 this->mutex->lock(this->mutex);
77
78 in->nlmsg_seq = ++this->seq;
79 in->nlmsg_pid = getpid();
80
81 memset(&addr, 0, sizeof(addr));
82 addr.nl_family = AF_NETLINK;
83 addr.nl_pid = 0;
84 addr.nl_groups = 0;
85
86 if (this->names)
87 {
88 DBG3(DBG_KNL, "sending %N: %b",
89 this->names, in->nlmsg_type, in, in->nlmsg_len);
90 }
91 while (TRUE)
92 {
93 len = sendto(this->socket, in, in->nlmsg_len, 0,
94 (struct sockaddr*)&addr, sizeof(addr));
95
96 if (len != in->nlmsg_len)
97 {
98 if (errno == EINTR)
99 {
100 /* interrupted, try again */
101 continue;
102 }
103 this->mutex->unlock(this->mutex);
104 DBG1(DBG_KNL, "error sending to netlink socket: %s", strerror(errno));
105 return FAILED;
106 }
107 break;
108 }
109
110 while (TRUE)
111 {
112 len = recv(this->socket, &response, sizeof(response), 0);
113 if (len < 0)
114 {
115 if (errno == EINTR)
116 {
117 DBG1(DBG_KNL, "got interrupted");
118 /* interrupted, try again */
119 continue;
120 }
121 DBG1(DBG_KNL, "error reading from netlink socket: %s", strerror(errno));
122 this->mutex->unlock(this->mutex);
123 free(result.ptr);
124 return FAILED;
125 }
126 if (!NLMSG_OK(&response.hdr, len))
127 {
128 DBG1(DBG_KNL, "received corrupted netlink message");
129 this->mutex->unlock(this->mutex);
130 free(result.ptr);
131 return FAILED;
132 }
133 if (response.hdr.nlmsg_seq != this->seq)
134 {
135 DBG1(DBG_KNL, "received invalid netlink sequence number");
136 if (response.hdr.nlmsg_seq < this->seq)
137 {
138 continue;
139 }
140 this->mutex->unlock(this->mutex);
141 free(result.ptr);
142 return FAILED;
143 }
144
145 result = chunk_cat("mc", result, chunk_create(response.bytes, len));
146
147 /* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
148 * numbers to detect multi header messages */
149 len = recv(this->socket, &response.hdr, sizeof(response.hdr),
150 MSG_PEEK | MSG_DONTWAIT);
151 if (len == sizeof(response.hdr) && response.hdr.nlmsg_seq == this->seq)
152 {
153 /* seems to be multipart */
154 continue;
155 }
156 break;
157 }
158
159 *out_len = result.len;
160 *out = (struct nlmsghdr*)result.ptr;
161
162 this->mutex->unlock(this->mutex);
163
164 return SUCCESS;
165 }
166
167 METHOD(netlink_socket_t, netlink_send_ack, status_t,
168 private_netlink_socket_t *this, struct nlmsghdr *in)
169 {
170 struct nlmsghdr *out, *hdr;
171 size_t len;
172
173 if (netlink_send(this, in, &out, &len) != SUCCESS)
174 {
175 return FAILED;
176 }
177 hdr = out;
178 while (NLMSG_OK(hdr, len))
179 {
180 switch (hdr->nlmsg_type)
181 {
182 case NLMSG_ERROR:
183 {
184 struct nlmsgerr* err = NLMSG_DATA(hdr);
185
186 if (err->error)
187 {
188 if (-err->error == EEXIST)
189 { /* do not report existing routes */
190 free(out);
191 return ALREADY_DONE;
192 }
193 if (-err->error == ESRCH)
194 { /* do not report missing entries */
195 free(out);
196 return NOT_FOUND;
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 METHOD(netlink_socket_t, destroy, void,
220 private_netlink_socket_t *this)
221 {
222 if (this->socket != -1)
223 {
224 close(this->socket);
225 }
226 this->mutex->destroy(this->mutex);
227 free(this);
228 }
229
230 /**
231 * Described in header.
232 */
233 netlink_socket_t *netlink_socket_create(int protocol, enum_name_t *names)
234 {
235 private_netlink_socket_t *this;
236 struct sockaddr_nl addr = {
237 .nl_family = AF_NETLINK,
238 };
239
240 INIT(this,
241 .public = {
242 .send = _netlink_send,
243 .send_ack = _netlink_send_ack,
244 .destroy = _destroy,
245 },
246 .seq = 200,
247 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
248 .socket = socket(AF_NETLINK, SOCK_RAW, protocol),
249 .names = names,
250 );
251
252 if (this->socket == -1)
253 {
254 DBG1(DBG_KNL, "unable to create netlink socket");
255 destroy(this);
256 return NULL;
257 }
258 if (bind(this->socket, (struct sockaddr*)&addr, sizeof(addr)))
259 {
260 DBG1(DBG_KNL, "unable to bind netlink socket");
261 destroy(this);
262 return NULL;
263 }
264
265 return &this->public;
266 }
267
268 /**
269 * Described in header.
270 */
271 void netlink_add_attribute(struct nlmsghdr *hdr, int rta_type, chunk_t data,
272 size_t buflen)
273 {
274 struct rtattr *rta;
275
276 if (NLMSG_ALIGN(hdr->nlmsg_len) + RTA_LENGTH(data.len) > buflen)
277 {
278 DBG1(DBG_KNL, "unable to add attribute, buffer too small");
279 return;
280 }
281
282 rta = (struct rtattr*)(((char*)hdr) + NLMSG_ALIGN(hdr->nlmsg_len));
283 rta->rta_type = rta_type;
284 rta->rta_len = RTA_LENGTH(data.len);
285 memcpy(RTA_DATA(rta), data.ptr, data.len);
286 hdr->nlmsg_len = NLMSG_ALIGN(hdr->nlmsg_len) + rta->rta_len;
287 }
288
289 /**
290 * Described in header.
291 */
292 void* netlink_reserve(struct nlmsghdr *hdr, int buflen, int type, int len)
293 {
294 struct rtattr *rta;
295
296 if (NLMSG_ALIGN(hdr->nlmsg_len) + RTA_LENGTH(len) > buflen)
297 {
298 DBG1(DBG_KNL, "unable to add attribute, buffer too small");
299 return NULL;
300 }
301
302 rta = ((void*)hdr) + NLMSG_ALIGN(hdr->nlmsg_len);
303 rta->rta_type = type;
304 rta->rta_len = RTA_LENGTH(len);
305 hdr->nlmsg_len = NLMSG_ALIGN(hdr->nlmsg_len) + rta->rta_len;
306
307 return RTA_DATA(rta);
308 }