attr.c revision ed6b39cc7746fabdd0d01c96afcf60b9544913d3
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *	http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* NOTICE: This is a clean room re-implementation of libnl */
18
19#include <errno.h>
20#include "netlink/netlink.h"
21#include "netlink/msg.h"
22#include "netlink/attr.h"
23#include "netlink-types.h"
24
25/* Return payload of string attribute. */
26char *nla_get_string(struct nlattr *nla)
27{
28	return (char *) nla_data(nla);
29}
30
31/* Return payload of 16 bit integer attribute. */
32uint16_t nla_get_u16(struct nlattr *nla)
33{
34	return *((uint16_t *) nla_data(nla));
35}
36
37/* Return payload of 32 bit integer attribute. */
38uint32_t nla_get_u32(struct nlattr *nla)
39{
40	return *((uint32_t *) nla_data(nla));
41}
42
43/* Return value of 8 bit integer attribute. */
44uint8_t nla_get_u8(struct nlattr *nla)
45{
46	return *((uint8_t *) nla_data(nla));
47}
48
49/* Return payload of uint64_t attribute. */
50uint64_t nla_get_u64(struct nlattr *nla)
51{
52	uint64_t tmp;
53	nla_memcpy(&tmp, nla, sizeof(tmp));
54	return tmp;
55}
56
57/* Head of payload */
58void *nla_data(const struct nlattr *nla)
59{
60	return (void *) ((char *) nla + NLA_HDRLEN);
61}
62
63/* Return length of the payload . */
64int nla_len(const struct nlattr *nla)
65{
66	return nla->nla_len - NLA_HDRLEN;
67}
68
69/* Start a new level of nested attributes. */
70struct nlattr *nla_nest_start(struct nl_msg *msg, int attrtype)
71{
72	if (!nla_put(msg, attrtype, 0, NULL)) {
73		/* Get ref to last (nested start) attr	*/
74		int padding;
75		struct nlattr *nla;
76
77		padding = nlmsg_padlen(nlmsg_datalen(nlmsg_hdr(msg)));
78		nla = (struct nlattr *) \
79			((char *) nlmsg_tail(msg->nm_nlh) - padding);
80		return nla;
81
82	} else
83		return NULL;
84
85}
86
87/* Finalize nesting of attributes. */
88int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
89{
90	start->nla_len = (unsigned char *) \
91		nlmsg_tail(nlmsg_hdr(msg)) - (unsigned char *)start;
92	return 0;
93}
94
95/* Return next attribute in a stream of attributes. */
96struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
97{
98	struct nlattr *next_nla = NULL;
99	if (nla->nla_len >= sizeof(struct nlattr) &&
100	   nla->nla_len <= *remaining){
101		next_nla = (struct nlattr *) \
102			((char *) nla + NLA_ALIGN(nla->nla_len));
103		*remaining = *remaining - NLA_ALIGN(nla->nla_len);
104	}
105
106	return next_nla;
107
108}
109
110/* Check if the attribute header and payload can be accessed safely. */
111int nla_ok(const struct nlattr *nla, int remaining)
112{
113	return remaining > 0 &&
114		nla->nla_len >= sizeof(struct nlattr) &&
115		sizeof(struct nlattr) <= (unsigned int) remaining &&
116		nla->nla_len <= remaining;
117}
118
119/* Create attribute index based on a stream of attributes. */
120/* NOTE: Policy not used ! */
121int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
122	int len, struct nla_policy *policy)
123{
124	struct nlattr *pos;
125	int rem;
126
127	/* First clear table */
128	memset(tb, 0, (maxtype+1) * sizeof(struct nlattr *));
129
130	nla_for_each_attr(pos, head, len, rem) {
131		const int type = nla_type(pos);
132
133		if (type <= maxtype)
134			tb[type] = pos;
135
136	}
137
138	return 0;
139}
140
141
142/* Create attribute index based on nested attribute. */
143int nla_parse_nested(struct nlattr *tb[], int maxtype,
144		struct nlattr *nla, struct nla_policy *policy)
145{
146	return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
147}
148
149
150/* Add a unspecific attribute to netlink message. */
151int nla_put(struct nl_msg *msg, int attrtype, int datalen, const void *data)
152{
153	struct nlattr *nla;
154
155	/* Reserve space and init nla header */
156	nla = nla_reserve(msg, attrtype, datalen);
157	if (nla)
158		memcpy(nla_data(nla), data, datalen);
159
160	return -errno;
161
162}
163
164
165/* Add nested attributes to netlink message. */
166/* Takes the attributes found in the nested message and appends them
167 * to the message msg nested in a container of the type attrtype. The
168 * nested message may not have a family specific header */
169int nla_put_nested(struct nl_msg *msg, int attrtype, struct nl_msg *nested)
170{
171	int rc = -1;
172	const int NO_HEADER = 0;
173
174	rc = nla_put(
175		msg,
176		attrtype,
177		nlmsg_attrlen(nlmsg_hdr(nested), NO_HEADER),
178		(const void *) nlmsg_attrdata(nlmsg_hdr(nested), NO_HEADER)
179		);
180	return rc;
181
182}
183
184/* Return type of the attribute. */
185int nla_type(const struct nlattr *nla)
186{
187	return (int) nla->nla_type;
188}
189
190/* Reserves room for an attribute in specified netlink message and fills
191 * in the attribute header (type,length). Return NULL if insufficient space */
192struct nlattr *nla_reserve(struct nl_msg * msg, int attrtype, int data_len)
193{
194
195	struct nlattr *nla;
196	const unsigned int NEW_SIZE = \
197		msg->nm_nlh->nlmsg_len + NLA_ALIGN(NLA_HDRLEN + data_len);
198
199	/* Check enough space for attribute */
200	if (NEW_SIZE <= msg->nm_size) {
201		const int fam_hdrlen = msg->nm_nlh->nlmsg_len - NLMSG_HDRLEN;
202		msg->nm_nlh->nlmsg_len = NEW_SIZE;
203		nla = nlmsg_attrdata(msg->nm_nlh, fam_hdrlen);
204		nla->nla_type = attrtype;
205		nla->nla_len = NLA_HDRLEN + data_len;
206	} else
207		goto fail;
208
209	return nla;
210fail:
211	return NULL;
212
213}
214
215/* Copy attribute payload to another memory area. */
216int nla_memcpy(void *dest, struct nlattr *src, int count)
217{
218	int rc;
219	void *ret_dest = memcpy(dest, nla_data(src), count);
220	if (!ret_dest)
221		return count;
222	else
223		return 0;
224}
225
226
227