]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/nl.c
Merge pull request #1539 from brauner/2017-05-06/fix_abstract_unix_sockets
[mirror_lxc.git] / src / lxc / nl.c
CommitLineData
0ad19a3f 1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
0ad19a3f 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
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0ad19a3f 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>
f549edcc
GK
32
33#include "nl.h"
0ad19a3f 34
35#define NLMSG_TAIL(nmsg) \
36 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
37
38extern size_t nlmsg_len(const struct nlmsg *nlmsg)
39{
06f976ca 40 return nlmsg->nlmsghdr->nlmsg_len - NLMSG_HDRLEN;
0ad19a3f 41}
42
43extern void *nlmsg_data(struct nlmsg *nlmsg)
44{
06f976ca 45 char *data = ((char *)nlmsg) + NLMSG_HDRLEN;
0ad19a3f 46 if (!nlmsg_len(nlmsg))
47 return NULL;
48 return data;
49}
50
f79d43bb 51static int nla_put(struct nlmsg *nlmsg, int attr,
0ad19a3f 52 const void *data, size_t len)
53{
54 struct rtattr *rta;
55 size_t rtalen = RTA_LENGTH(len);
06f976ca 56 size_t tlen = NLMSG_ALIGN(nlmsg->nlmsghdr->nlmsg_len) + RTA_ALIGN(rtalen);
84760c11 57
06f976ca
SZ
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 memcpy(RTA_DATA(rta), data, len);
65 nlmsg->nlmsghdr->nlmsg_len = tlen;
0ad19a3f 66 return 0;
67}
68
f79d43bb 69extern int nla_put_buffer(struct nlmsg *nlmsg, int attr,
0ad19a3f 70 const void *data, size_t size)
71{
72 return nla_put(nlmsg, attr, data, size);
73}
74
75extern int nla_put_string(struct nlmsg *nlmsg, int attr, const char *string)
76{
d028235d 77 return nla_put(nlmsg, attr, string, strlen(string) + 1);
0ad19a3f 78}
79
80extern int nla_put_u32(struct nlmsg *nlmsg, int attr, int value)
81{
82 return nla_put(nlmsg, attr, &value, sizeof(value));
83}
84
7c11d57a 85extern int nla_put_u16(struct nlmsg *nlmsg, int attr, unsigned short value)
9ddaf3bf
JHS
86{
87 return nla_put(nlmsg, attr, &value, 2);
88}
89
0ad19a3f 90extern int nla_put_attr(struct nlmsg *nlmsg, int attr)
91{
92 return nla_put(nlmsg, attr, NULL, 0);
93}
94
95struct rtattr *nla_begin_nested(struct nlmsg *nlmsg, int attr)
96{
06f976ca 97 struct rtattr *rtattr = NLMSG_TAIL(nlmsg->nlmsghdr);
0ad19a3f 98
99 if (nla_put_attr(nlmsg, attr))
100 return NULL;
101
102 return rtattr;
103}
104
105void nla_end_nested(struct nlmsg *nlmsg, struct rtattr *attr)
106{
06f976ca 107 attr->rta_len = (void *)NLMSG_TAIL(nlmsg->nlmsghdr) - (void *)attr;
0ad19a3f 108}
109
110extern struct nlmsg *nlmsg_alloc(size_t size)
111{
112 struct nlmsg *nlmsg;
b5887164 113 size_t len = NLMSG_HDRLEN + NLMSG_ALIGN(size);
0ad19a3f 114
06f976ca 115 nlmsg = (struct nlmsg *)malloc(sizeof(struct nlmsg));
0ad19a3f 116 if (!nlmsg)
117 return NULL;
118
06f976ca
SZ
119 nlmsg->nlmsghdr = (struct nlmsghdr *)malloc(len);
120 if (!nlmsg->nlmsghdr)
121 goto errout;
122
123 memset(nlmsg->nlmsghdr, 0, len);
124 nlmsg->cap = len;
125 nlmsg->nlmsghdr->nlmsg_len = NLMSG_HDRLEN;
0ad19a3f 126
06f976ca
SZ
127 return nlmsg;
128errout:
129 free(nlmsg);
130 return NULL;
131}
132
133extern void *nlmsg_reserve(struct nlmsg *nlmsg, size_t len)
134{
135 void *buf;
136 size_t nlmsg_len = nlmsg->nlmsghdr->nlmsg_len;
137 size_t tlen = NLMSG_ALIGN(len);
138
139 if (nlmsg_len + tlen > nlmsg->cap)
140 return NULL;
141
142 buf = ((char *)(nlmsg->nlmsghdr)) + nlmsg_len;
143 nlmsg->nlmsghdr->nlmsg_len += tlen;
144
145 if (tlen > len)
146 memset(buf + len, 0, tlen - len);
147
148 return buf;
149}
150
151extern struct nlmsg *nlmsg_alloc_reserve(size_t size)
152{
153 struct nlmsg *nlmsg;
154
155 nlmsg = nlmsg_alloc(size);
156 if (!nlmsg)
157 return NULL;
158
159 // just set message length to cap directly
160 nlmsg->nlmsghdr->nlmsg_len = nlmsg->cap;
0ad19a3f 161 return nlmsg;
162}
163
164extern void nlmsg_free(struct nlmsg *nlmsg)
165{
06f976ca
SZ
166 if (!nlmsg)
167 return;
168
169 free(nlmsg->nlmsghdr);
0ad19a3f 170 free(nlmsg);
171}
172
173extern int netlink_rcv(struct nl_handler *handler, struct nlmsg *answer)
174{
175 int ret;
d028235d
SG
176 struct sockaddr_nl nladdr;
177 struct iovec iov = {
178 .iov_base = answer->nlmsghdr,
179 .iov_len = answer->nlmsghdr->nlmsg_len,
180 };
0ad19a3f 181
182 struct msghdr msg = {
d028235d
SG
183 .msg_name = &nladdr,
184 .msg_namelen = sizeof(nladdr),
185 .msg_iov = &iov,
186 .msg_iovlen = 1,
187 };
0ad19a3f 188
d028235d
SG
189 memset(&nladdr, 0, sizeof(nladdr));
190 nladdr.nl_family = AF_NETLINK;
191 nladdr.nl_pid = 0;
192 nladdr.nl_groups = 0;
0ad19a3f 193
194again:
195 ret = recvmsg(handler->fd, &msg, 0);
196 if (ret < 0) {
197 if (errno == EINTR)
198 goto again;
199 return -errno;
200 }
201
202 if (!ret)
203 return 0;
204
e239ff31 205 if (msg.msg_flags & MSG_TRUNC &&
06f976ca 206 ret == answer->nlmsghdr->nlmsg_len)
0ad19a3f 207 return -EMSGSIZE;
208
209 return ret;
210}
211
212extern int netlink_send(struct nl_handler *handler, struct nlmsg *nlmsg)
213{
d028235d
SG
214 struct sockaddr_nl nladdr;
215 struct iovec iov = {
216 .iov_base = nlmsg->nlmsghdr,
217 .iov_len = nlmsg->nlmsghdr->nlmsg_len,
218 };
0ad19a3f 219 struct msghdr msg = {
d028235d
SG
220 .msg_name = &nladdr,
221 .msg_namelen = sizeof(nladdr),
222 .msg_iov = &iov,
223 .msg_iovlen = 1,
224 };
0ad19a3f 225 int ret;
226
d028235d
SG
227 memset(&nladdr, 0, sizeof(nladdr));
228 nladdr.nl_family = AF_NETLINK;
229 nladdr.nl_pid = 0;
230 nladdr.nl_groups = 0;
0ad19a3f 231
232 ret = sendmsg(handler->fd, &msg, 0);
e239ff31 233 if (ret < 0)
0ad19a3f 234 return -errno;
0ad19a3f 235
236 return ret;
237}
238
9ddaf3bf
JHS
239#ifndef NLMSG_ERROR
240#define NLMSG_ERROR 0x2
241#endif
f79d43bb 242extern int netlink_transaction(struct nl_handler *handler,
0ad19a3f 243 struct nlmsg *request, struct nlmsg *answer)
244{
0ad19a3f 245 int ret;
246
247 ret = netlink_send(handler, request);
248 if (ret < 0)
249 return ret;
250
251 ret = netlink_rcv(handler, answer);
252 if (ret < 0)
253 return ret;
254
06f976ca
SZ
255 if (answer->nlmsghdr->nlmsg_type == NLMSG_ERROR) {
256 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(answer->nlmsghdr);
3cfc0f3a 257 return err->error;
0ad19a3f 258 }
e239ff31 259
0ad19a3f 260 return 0;
261}
262
263extern int netlink_open(struct nl_handler *handler, int protocol)
264{
265 socklen_t socklen;
d028235d
SG
266 int sndbuf = 32768;
267 int rcvbuf = 32768;
b73e5969 268 int err;
0ad19a3f 269
d028235d 270 memset(handler, 0, sizeof(*handler));
0ad19a3f 271
d028235d
SG
272 handler->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
273 if (handler->fd < 0)
274 return -errno;
0ad19a3f 275
d028235d 276 if (setsockopt(handler->fd, SOL_SOCKET, SO_SNDBUF,
0ad19a3f 277 &sndbuf, sizeof(sndbuf)) < 0)
b73e5969 278 goto err_with_errno;
0ad19a3f 279
d028235d 280 if (setsockopt(handler->fd, SOL_SOCKET, SO_RCVBUF,
0ad19a3f 281 &rcvbuf,sizeof(rcvbuf)) < 0)
b73e5969 282 goto err_with_errno;
0ad19a3f 283
d028235d
SG
284 memset(&handler->local, 0, sizeof(handler->local));
285 handler->local.nl_family = AF_NETLINK;
286 handler->local.nl_groups = 0;
0ad19a3f 287
d028235d 288 if (bind(handler->fd, (struct sockaddr*)&handler->local,
0ad19a3f 289 sizeof(handler->local)) < 0)
b73e5969 290 goto err_with_errno;
0ad19a3f 291
d028235d
SG
292 socklen = sizeof(handler->local);
293 if (getsockname(handler->fd, (struct sockaddr*)&handler->local,
0ad19a3f 294 &socklen) < 0)
b73e5969 295 goto err_with_errno;
0ad19a3f 296
b73e5969
WB
297 if (socklen != sizeof(handler->local)) {
298 err = -EINVAL;
299 goto errclose;
300 }
0ad19a3f 301
b73e5969
WB
302 if (handler->local.nl_family != AF_NETLINK) {
303 err = -EINVAL;
304 goto errclose;
305 }
0ad19a3f 306
307 handler->seq = time(NULL);
308
d028235d 309 return 0;
b73e5969
WB
310err_with_errno:
311 err = -errno;
312errclose:
313 close(handler->fd);
314 return err;
0ad19a3f 315}
316
317extern int netlink_close(struct nl_handler *handler)
318{
319 close(handler->fd);
320 handler->fd = -1;
321 return 0;
322}
323