]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/nl.c
2e97efcca4a447f5f582a904ea100c8b52064fa9
[mirror_lxc.git] / src / lxc / nl.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library 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 GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23 #include <sys/socket.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <linux/netlink.h>
31 #include <linux/rtnetlink.h>
32
33 #include "nl.h"
34
35 #define NLMSG_TAIL(nmsg) \
36 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
37
38 extern size_t nlmsg_len(const struct nlmsg *nlmsg)
39 {
40 return nlmsg->nlmsghdr->nlmsg_len - NLMSG_HDRLEN;
41 }
42
43 extern void *nlmsg_data(struct nlmsg *nlmsg)
44 {
45 char *data = ((char *)nlmsg) + NLMSG_HDRLEN;
46 if (!nlmsg_len(nlmsg))
47 return NULL;
48 return data;
49 }
50
51 static int nla_put(struct nlmsg *nlmsg, int attr,
52 const void *data, size_t len)
53 {
54 struct rtattr *rta;
55 size_t rtalen = RTA_LENGTH(len);
56 size_t tlen = NLMSG_ALIGN(nlmsg->nlmsghdr->nlmsg_len) + RTA_ALIGN(rtalen);
57
58 if (tlen > nlmsg->cap)
59 return -ENOMEM;
60
61 rta = NLMSG_TAIL(nlmsg->nlmsghdr);
62 rta->rta_type = attr;
63 rta->rta_len = rtalen;
64 if (data && len)
65 memcpy(RTA_DATA(rta), data, len);
66 nlmsg->nlmsghdr->nlmsg_len = tlen;
67 return 0;
68 }
69
70 extern int nla_put_buffer(struct nlmsg *nlmsg, int attr,
71 const void *data, size_t size)
72 {
73 return nla_put(nlmsg, attr, data, size);
74 }
75
76 extern int nla_put_string(struct nlmsg *nlmsg, int attr, const char *string)
77 {
78 return nla_put(nlmsg, attr, string, strlen(string) + 1);
79 }
80
81 extern int nla_put_u32(struct nlmsg *nlmsg, int attr, int value)
82 {
83 return nla_put(nlmsg, attr, &value, sizeof(value));
84 }
85
86 extern int nla_put_u16(struct nlmsg *nlmsg, int attr, unsigned short value)
87 {
88 return nla_put(nlmsg, attr, &value, 2);
89 }
90
91 extern int nla_put_attr(struct nlmsg *nlmsg, int attr)
92 {
93 return nla_put(nlmsg, attr, NULL, 0);
94 }
95
96 struct rtattr *nla_begin_nested(struct nlmsg *nlmsg, int attr)
97 {
98 struct rtattr *rtattr = NLMSG_TAIL(nlmsg->nlmsghdr);
99
100 if (nla_put_attr(nlmsg, attr))
101 return NULL;
102
103 return rtattr;
104 }
105
106 void nla_end_nested(struct nlmsg *nlmsg, struct rtattr *attr)
107 {
108 attr->rta_len = (void *)NLMSG_TAIL(nlmsg->nlmsghdr) - (void *)attr;
109 }
110
111 extern struct nlmsg *nlmsg_alloc(size_t size)
112 {
113 struct nlmsg *nlmsg;
114 size_t len = NLMSG_HDRLEN + NLMSG_ALIGN(size);
115
116 nlmsg = (struct nlmsg *)malloc(sizeof(struct nlmsg));
117 if (!nlmsg)
118 return NULL;
119
120 nlmsg->nlmsghdr = (struct nlmsghdr *)malloc(len);
121 if (!nlmsg->nlmsghdr)
122 goto errout;
123
124 memset(nlmsg->nlmsghdr, 0, len);
125 nlmsg->cap = len;
126 nlmsg->nlmsghdr->nlmsg_len = NLMSG_HDRLEN;
127
128 return nlmsg;
129 errout:
130 free(nlmsg);
131 return NULL;
132 }
133
134 extern void *nlmsg_reserve(struct nlmsg *nlmsg, size_t len)
135 {
136 void *buf;
137 size_t nlmsg_len = nlmsg->nlmsghdr->nlmsg_len;
138 size_t tlen = NLMSG_ALIGN(len);
139
140 if (nlmsg_len + tlen > nlmsg->cap)
141 return NULL;
142
143 buf = ((char *)(nlmsg->nlmsghdr)) + nlmsg_len;
144 nlmsg->nlmsghdr->nlmsg_len += tlen;
145
146 if (tlen > len)
147 memset(buf + len, 0, tlen - len);
148
149 return buf;
150 }
151
152 extern struct nlmsg *nlmsg_alloc_reserve(size_t size)
153 {
154 struct nlmsg *nlmsg;
155
156 nlmsg = nlmsg_alloc(size);
157 if (!nlmsg)
158 return NULL;
159
160 /* Just set message length to cap directly. */
161 nlmsg->nlmsghdr->nlmsg_len = nlmsg->cap;
162 return nlmsg;
163 }
164
165 extern void nlmsg_free(struct nlmsg *nlmsg)
166 {
167 if (!nlmsg)
168 return;
169
170 free(nlmsg->nlmsghdr);
171 free(nlmsg);
172 }
173
174 extern int __netlink_recv(struct nl_handler *handler, struct nlmsghdr *nlmsghdr)
175 {
176 int ret;
177 struct sockaddr_nl nladdr;
178 struct iovec iov = {
179 .iov_base = nlmsghdr,
180 .iov_len = nlmsghdr->nlmsg_len,
181 };
182
183 struct msghdr msg = {
184 .msg_name = &nladdr,
185 .msg_namelen = sizeof(nladdr),
186 .msg_iov = &iov,
187 .msg_iovlen = 1,
188 };
189
190 memset(&nladdr, 0, sizeof(nladdr));
191 nladdr.nl_family = AF_NETLINK;
192 nladdr.nl_pid = 0;
193 nladdr.nl_groups = 0;
194
195 again:
196 ret = recvmsg(handler->fd, &msg, 0);
197 if (ret < 0) {
198 if (errno == EINTR)
199 goto again;
200
201 return -1;
202 }
203
204 if (!ret)
205 return 0;
206
207 if (msg.msg_flags & MSG_TRUNC && (ret == nlmsghdr->nlmsg_len))
208 return -EMSGSIZE;
209
210 return ret;
211 }
212
213 extern int netlink_rcv(struct nl_handler *handler, struct nlmsg *answer)
214 {
215 return __netlink_recv(handler, answer->nlmsghdr);
216 }
217
218 extern int __netlink_send(struct nl_handler *handler, struct nlmsghdr *nlmsghdr)
219 {
220 int ret;
221 struct sockaddr_nl nladdr;
222 struct iovec iov = {
223 .iov_base = nlmsghdr,
224 .iov_len = nlmsghdr->nlmsg_len,
225 };
226 struct msghdr msg = {
227 .msg_name = &nladdr,
228 .msg_namelen = sizeof(nladdr),
229 .msg_iov = &iov,
230 .msg_iovlen = 1,
231 };
232
233 memset(&nladdr, 0, sizeof(nladdr));
234 nladdr.nl_family = AF_NETLINK;
235 nladdr.nl_pid = 0;
236 nladdr.nl_groups = 0;
237
238 ret = sendmsg(handler->fd, &msg, MSG_NOSIGNAL);
239 if (ret < 0)
240 return -1;
241
242 return ret;
243 }
244
245 extern int netlink_send(struct nl_handler *handler, struct nlmsg *nlmsg)
246 {
247 return __netlink_send(handler, nlmsg->nlmsghdr);
248 }
249
250 extern int __netlink_transaction(struct nl_handler *handler,
251 struct nlmsghdr *request,
252 struct nlmsghdr *answer)
253 {
254 int ret;
255
256 ret = __netlink_send(handler, request);
257 if (ret < 0)
258 return ret;
259
260 ret = __netlink_recv(handler, answer);
261 if (ret < 0)
262 return ret;
263
264 if (answer->nlmsg_type == NLMSG_ERROR) {
265 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(answer);
266 return err->error;
267 }
268
269 return 0;
270 }
271
272 extern int netlink_transaction(struct nl_handler *handler,
273 struct nlmsg *request, struct nlmsg *answer)
274 {
275 return __netlink_transaction(handler, request->nlmsghdr,
276 answer->nlmsghdr);
277 }
278
279 extern int netlink_open(struct nl_handler *handler, int protocol)
280 {
281 socklen_t socklen;
282 int sndbuf = 32768;
283 int rcvbuf = 32768;
284 int err;
285
286 memset(handler, 0, sizeof(*handler));
287
288 handler->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
289 if (handler->fd < 0)
290 return -errno;
291
292 if (setsockopt(handler->fd, SOL_SOCKET, SO_SNDBUF,
293 &sndbuf, sizeof(sndbuf)) < 0)
294 goto err_with_errno;
295
296 if (setsockopt(handler->fd, SOL_SOCKET, SO_RCVBUF,
297 &rcvbuf,sizeof(rcvbuf)) < 0)
298 goto err_with_errno;
299
300 memset(&handler->local, 0, sizeof(handler->local));
301 handler->local.nl_family = AF_NETLINK;
302 handler->local.nl_groups = 0;
303
304 if (bind(handler->fd, (struct sockaddr*)&handler->local,
305 sizeof(handler->local)) < 0)
306 goto err_with_errno;
307
308 socklen = sizeof(handler->local);
309 if (getsockname(handler->fd, (struct sockaddr*)&handler->local,
310 &socklen) < 0)
311 goto err_with_errno;
312
313 if (socklen != sizeof(handler->local)) {
314 err = -EINVAL;
315 goto errclose;
316 }
317
318 if (handler->local.nl_family != AF_NETLINK) {
319 err = -EINVAL;
320 goto errclose;
321 }
322
323 handler->seq = time(NULL);
324
325 return 0;
326 err_with_errno:
327 err = -errno;
328 errclose:
329 close(handler->fd);
330 return err;
331 }
332
333 extern int netlink_close(struct nl_handler *handler)
334 {
335 close(handler->fd);
336 handler->fd = -1;
337 return 0;
338 }
339