netlink.c revision d99fe5ec960b5eae96585d5ceb59fe287fe0c5e7
127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi/*
227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * Copyright (C) 2011 The Android Open Source Project
327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi *
427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * Licensed under the Apache License, Version 2.0 (the "License");
527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * you may not use this file except in compliance with the License.
627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * You may obtain a copy of the License at
727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi *
827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi *	http://www.apache.org/licenses/LICENSE-2.0
927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi *
1027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * Unless required by applicable law or agreed to in writing, software
1127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * distributed under the License is distributed on an "AS IS" BASIS,
1227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * See the License for the specific language governing permissions and
1427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * limitations under the License.
1527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi */
1627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
1727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi/* NOTICE: This is a clean room re-implementation of libnl */
182272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi
1927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <errno.h>
2027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <string.h>
2127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <unistd.h>
2227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <fcntl.h>
232272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi#include <sys/socket.h>
242272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi#include "netlink-types.h"
252272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi
262272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi#define NL_BUFFER_SZ (32768U)
272272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi
282272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi/* Checks message for completeness and sends it out */
292272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimiint nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
302272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi{
312272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi	struct nlmsghdr *nlh = msg->nm_nlh;
322272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi	struct timeval tv;
3327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
3427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi	if (!nlh) {
3527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi		int errsv = errno;
3627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi		fprintf(stderr, "Netlink message header is NULL!\n");
3727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi		return -errsv;
3827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi	}
3927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
402272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi	/* Complete the nl_msg header */
412272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi	if (gettimeofday(&tv, NULL))
422272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi		nlh->nlmsg_seq = 1;
432272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi	else
442272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi		nlh->nlmsg_seq = (int) tv.tv_sec;
452272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi	nlh->nlmsg_pid = sk->s_local.nl_pid;
4627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi	nlh->nlmsg_flags |= NLM_F_REQUEST | NLM_F_ACK;
4727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
4827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi	return nl_send(sk, msg);
4927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
5027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
5127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi/* Receives a netlink message, allocates a buffer in *buf and stores
5227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * the message content. The peer's netlink address is stored in
5327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * *nla. The caller is responsible for freeing the buffer allocated in
5427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * *buf if a positive value is returned. Interrupted system calls are
5527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * handled by repeating the read. The input buffer size is determined
5627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * by peeking before the actual read is done */
5727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiint nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla, \
5827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi	unsigned char **buf, struct ucred **creds)
5927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi{
6027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi	int rc = -1;
6127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi	int sk_flags;
6227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi	int RECV_BUF_SIZE = getpagesize();
6327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi	int errsv;
64	struct iovec recvmsg_iov;
65	struct msghdr msg;
66
67	/* Allocate buffer */
68	*buf = (unsigned char *) malloc(RECV_BUF_SIZE);
69	if (!(*buf)) {
70		rc = -ENOMEM;
71		goto fail;
72	}
73
74	/* Prepare to receive message */
75	recvmsg_iov.iov_base = *buf;
76	recvmsg_iov.iov_len = RECV_BUF_SIZE;
77
78	msg.msg_name = (void *) &sk->s_peer;
79	msg.msg_namelen = sizeof(sk->s_peer);
80	msg.msg_iov = &recvmsg_iov;
81	msg.msg_iovlen = 1;
82	msg.msg_control = NULL;
83	msg.msg_controllen = 0;
84	msg.msg_flags = 0;
85
86	/* Make non blocking and then restore previous setting */
87	sk_flags = fcntl(sk->s_fd, F_GETFL, 0);
88	fcntl(sk->s_fd, F_SETFL, O_NONBLOCK);
89	rc = recvmsg(sk->s_fd, &msg, 0);
90	errsv = errno;
91	fcntl(sk->s_fd, F_SETFL, sk_flags);
92
93	if (rc < 0) {
94		rc = -errsv;
95		free(*buf);
96		*buf = NULL;
97	}
98
99fail:
100	return rc;
101}
102
103/* Receive a set of messages from a netlink socket */
104/* NOTE: Does not currently support callback replacements!!! */
105int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
106{
107	struct sockaddr_nl nla;
108	struct ucred *creds;
109
110	int rc, cb_rc = NL_OK, done = 0;
111
112	do {
113		unsigned char *buf;
114		int i, rem, flags;
115		struct nlmsghdr *nlh;
116		struct nlmsgerr *nlme;
117		struct nl_msg *msg;
118
119		done = 0;
120		rc = nl_recv(sk, &nla, &buf, &creds);
121		if (rc < 0)
122			break;
123
124		nlmsg_for_each_msg(nlh, (struct nlmsghdr *) buf, rc, rem) {
125
126			if (rc <= 0 || cb_rc == NL_STOP)
127				break;
128
129			/* Check for callbacks */
130
131			msg = (struct nl_msg *) malloc(sizeof(struct nl_msg));
132			memset(msg, 0, sizeof(*msg));
133			msg->nm_nlh = nlh;
134
135			/* Check netlink message type */
136
137			switch (msg->nm_nlh->nlmsg_type) {
138			case NLMSG_ERROR:	  /* Used for ACK too */
139				/* Certainly we should be doing some
140				 * checking here to make sure this
141				 * message is intended for us */
142				nlme = nlmsg_data(msg->nm_nlh);
143				if (nlme->error == 0)
144					msg->nm_nlh->nlmsg_flags |= NLM_F_ACK;
145
146				rc = nlme->error;
147				cb_rc = cb->cb_err(&nla, nlme, cb->cb_err_arg);
148				nlme = NULL;
149				break;
150
151			case NLMSG_DONE:
152				done = 1;
153
154			case NLMSG_OVERRUN:
155			case NLMSG_NOOP:
156			default:
157				break;
158			};
159
160			for (i = 0; i <= NL_CB_TYPE_MAX; i++) {
161
162				if (cb->cb_set[i]) {
163					switch (i) {
164					case NL_CB_VALID:
165						if (rc > 0)
166							cb_rc = cb->cb_set[i](msg, cb->cb_args[i]);
167						break;
168
169					case NL_CB_FINISH:
170						if ((msg->nm_nlh->nlmsg_flags & NLM_F_MULTI) &&
171							(msg->nm_nlh->nlmsg_type & NLMSG_DONE))
172							cb_rc = cb->cb_set[i](msg, cb->cb_args[i]);
173
174						break;
175
176					case NL_CB_ACK:
177						if (msg->nm_nlh->nlmsg_flags & NLM_F_ACK)
178							cb_rc = cb->cb_set[i](msg, cb->cb_args[i]);
179
180						break;
181					default:
182						break;
183					}
184				}
185			}
186
187			free(msg);
188			if (done)
189				break;
190		}
191		free(buf);
192		buf = NULL;
193
194		if (done)
195			break;
196	} while (rc > 0 && cb_rc != NL_STOP);
197
198success:
199fail:
200	return rc;
201}
202
203/* Send raw data over netlink socket */
204int nl_send(struct nl_sock *sk, struct nl_msg *msg)
205{
206	struct nlmsghdr *nlh = nlmsg_hdr(msg);
207	struct iovec msg_iov;
208
209	/* Create IO vector with Netlink message */
210	msg_iov.iov_base = nlh;
211	msg_iov.iov_len = nlh->nlmsg_len;
212
213	return nl_send_iovec(sk, msg, &msg_iov, 1);
214}
215
216/* Send netlink message */
217int nl_send_iovec(struct nl_sock *sk, struct nl_msg *msg,
218		   struct iovec *iov, unsigned iovlen)
219{
220	int rc;
221
222	/* Socket message */
223	struct msghdr mh = {
224		.msg_name = (void *) &sk->s_peer,
225		.msg_namelen = sizeof(sk->s_peer),
226		.msg_iov = iov,
227		.msg_iovlen = iovlen,
228		.msg_control = NULL,
229		.msg_controllen = 0,
230		.msg_flags = 0
231	};
232
233	/* Send message and verify sent */
234	rc = nl_sendmsg(sk, (struct nl_msg *) &mh, 0);
235	if (rc < 0)
236		fprintf(stderr, "Error sending netlink message: %d\n", errno);
237	return rc;
238
239}
240
241/* Send netlink message with control over sendmsg() message header */
242int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
243{
244	return sendmsg(sk->s_fd, (struct msghdr *) msg, (int) hdr);
245}
246
247/* Create and connect netlink socket */
248int nl_connect(struct nl_sock *sk, int protocol)
249{
250	struct sockaddr addr;
251	socklen_t addrlen;
252	int rc;
253
254	/* Create RX socket */
255	sk->s_fd = socket(PF_NETLINK, SOCK_RAW, protocol);
256	if (sk->s_fd < 0)
257		return -errno;
258
259	/* Set size of RX and TX buffers */
260	if (nl_socket_set_buffer_size(sk, NL_BUFFER_SZ, NL_BUFFER_SZ) < 0)
261		return -errno;
262
263	/* Bind RX socket */
264	rc = bind(sk->s_fd, (struct sockaddr *)&sk->s_local, \
265		sizeof(sk->s_local));
266	if (rc < 0)
267		return -errno;
268	addrlen = sizeof(addr);
269	getsockname(sk->s_fd, &addr, &addrlen);
270
271	return 0;
272
273}
274