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