]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/nl.c
licensing: Add missing headers and FSF address
[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_ALIGN(sizeof(struct nlmsghdr));
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
57 rta = NLMSG_TAIL(&nlmsg->nlmsghdr);
58 rta->rta_type = attr;
59 rta->rta_len = rtalen;
60 memcpy(RTA_DATA(rta), data, len);
61 nlmsg->nlmsghdr.nlmsg_len =
62 NLMSG_ALIGN(nlmsg->nlmsghdr.nlmsg_len) + RTA_ALIGN(rtalen);
63 return 0;
64 }
65
66 extern int nla_put_buffer(struct nlmsg *nlmsg, int attr,
67 const void *data, size_t size)
68 {
69 return nla_put(nlmsg, attr, data, size);
70 }
71
72 extern int nla_put_string(struct nlmsg *nlmsg, int attr, const char *string)
73 {
74 return nla_put(nlmsg, attr, string, strlen(string) + 1);
75 }
76
77 extern int nla_put_u32(struct nlmsg *nlmsg, int attr, int value)
78 {
79 return nla_put(nlmsg, attr, &value, sizeof(value));
80 }
81
82 extern int nla_put_u16(struct nlmsg *nlmsg, int attr, unsigned short value)
83 {
84 return nla_put(nlmsg, attr, &value, 2);
85 }
86
87 extern int nla_put_attr(struct nlmsg *nlmsg, int attr)
88 {
89 return nla_put(nlmsg, attr, NULL, 0);
90 }
91
92 struct rtattr *nla_begin_nested(struct nlmsg *nlmsg, int attr)
93 {
94 struct rtattr *rtattr = NLMSG_TAIL(&nlmsg->nlmsghdr);
95
96 if (nla_put_attr(nlmsg, attr))
97 return NULL;
98
99 return rtattr;
100 }
101
102 void nla_end_nested(struct nlmsg *nlmsg, struct rtattr *attr)
103 {
104 attr->rta_len = (void *)NLMSG_TAIL(&nlmsg->nlmsghdr) - (void *)attr;
105 }
106
107 extern struct nlmsg *nlmsg_alloc(size_t size)
108 {
109 struct nlmsg *nlmsg;
110 size_t len = NLMSG_ALIGN(size) + NLMSG_ALIGN(sizeof(struct nlmsghdr *));
111
112 nlmsg = (struct nlmsg *)malloc(len);
113 if (!nlmsg)
114 return NULL;
115
116 memset(nlmsg, 0, len);
117 nlmsg->nlmsghdr.nlmsg_len = NLMSG_ALIGN(size);
118
119 return nlmsg;
120 }
121
122 extern void nlmsg_free(struct nlmsg *nlmsg)
123 {
124 free(nlmsg);
125 }
126
127 extern int netlink_rcv(struct nl_handler *handler, struct nlmsg *answer)
128 {
129 int ret;
130 struct sockaddr_nl nladdr;
131 struct iovec iov = {
132 .iov_base = answer,
133 .iov_len = answer->nlmsghdr.nlmsg_len,
134 };
135
136 struct msghdr msg = {
137 .msg_name = &nladdr,
138 .msg_namelen = sizeof(nladdr),
139 .msg_iov = &iov,
140 .msg_iovlen = 1,
141 };
142
143 memset(&nladdr, 0, sizeof(nladdr));
144 nladdr.nl_family = AF_NETLINK;
145 nladdr.nl_pid = 0;
146 nladdr.nl_groups = 0;
147
148 again:
149 ret = recvmsg(handler->fd, &msg, 0);
150 if (ret < 0) {
151 if (errno == EINTR)
152 goto again;
153 return -errno;
154 }
155
156 if (!ret)
157 return 0;
158
159 if (msg.msg_flags & MSG_TRUNC &&
160 ret == answer->nlmsghdr.nlmsg_len)
161 return -EMSGSIZE;
162
163 return ret;
164 }
165
166 extern int netlink_send(struct nl_handler *handler, struct nlmsg *nlmsg)
167 {
168 struct sockaddr_nl nladdr;
169 struct iovec iov = {
170 .iov_base = (void*)nlmsg,
171 .iov_len = nlmsg->nlmsghdr.nlmsg_len,
172 };
173 struct msghdr msg = {
174 .msg_name = &nladdr,
175 .msg_namelen = sizeof(nladdr),
176 .msg_iov = &iov,
177 .msg_iovlen = 1,
178 };
179 int ret;
180
181 memset(&nladdr, 0, sizeof(nladdr));
182 nladdr.nl_family = AF_NETLINK;
183 nladdr.nl_pid = 0;
184 nladdr.nl_groups = 0;
185
186 ret = sendmsg(handler->fd, &msg, 0);
187 if (ret < 0)
188 return -errno;
189
190 return ret;
191 }
192
193 #ifndef NLMSG_ERROR
194 #define NLMSG_ERROR 0x2
195 #endif
196 extern int netlink_transaction(struct nl_handler *handler,
197 struct nlmsg *request, struct nlmsg *answer)
198 {
199 int ret;
200
201 ret = netlink_send(handler, request);
202 if (ret < 0)
203 return ret;
204
205 ret = netlink_rcv(handler, answer);
206 if (ret < 0)
207 return ret;
208
209 if (answer->nlmsghdr.nlmsg_type == NLMSG_ERROR) {
210 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(answer);
211 return err->error;
212 }
213
214 return 0;
215 }
216
217 extern int netlink_open(struct nl_handler *handler, int protocol)
218 {
219 socklen_t socklen;
220 int sndbuf = 32768;
221 int rcvbuf = 32768;
222
223 memset(handler, 0, sizeof(*handler));
224
225 handler->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
226 if (handler->fd < 0)
227 return -errno;
228
229 if (setsockopt(handler->fd, SOL_SOCKET, SO_SNDBUF,
230 &sndbuf, sizeof(sndbuf)) < 0)
231 return -errno;
232
233 if (setsockopt(handler->fd, SOL_SOCKET, SO_RCVBUF,
234 &rcvbuf,sizeof(rcvbuf)) < 0)
235 return -errno;
236
237 memset(&handler->local, 0, sizeof(handler->local));
238 handler->local.nl_family = AF_NETLINK;
239 handler->local.nl_groups = 0;
240
241 if (bind(handler->fd, (struct sockaddr*)&handler->local,
242 sizeof(handler->local)) < 0)
243 return -errno;
244
245 socklen = sizeof(handler->local);
246 if (getsockname(handler->fd, (struct sockaddr*)&handler->local,
247 &socklen) < 0)
248 return -errno;
249
250 if (socklen != sizeof(handler->local))
251 return -EINVAL;
252
253 if (handler->local.nl_family != AF_NETLINK)
254 return -EINVAL;
255
256 handler->seq = time(NULL);
257
258 return 0;
259 }
260
261 extern int netlink_close(struct nl_handler *handler)
262 {
263 close(handler->fd);
264 handler->fd = -1;
265 return 0;
266 }
267