blob: d416350e2fe444f91fd261cbaa83121707b6d439 [file] [log] [blame]
Frank Makered6b39c2011-05-23 21:14:58 -07001/*
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{
Frank Maker69bc1792011-08-01 19:51:49 -070090 struct nlattr *container;
91
92 /* Adjust nested attribute container size */
93 container = (unsigned char *) start - sizeof(struct nlattr);
94 container->nla_len = (unsigned char *) \
95 nlmsg_tail(nlmsg_hdr(msg)) - (unsigned char *)container;
96
97 /* Fix attribute size */
Frank Makered6b39c2011-05-23 21:14:58 -070098 start->nla_len = (unsigned char *) \
99 nlmsg_tail(nlmsg_hdr(msg)) - (unsigned char *)start;
Frank Maker69bc1792011-08-01 19:51:49 -0700100
Frank Makered6b39c2011-05-23 21:14:58 -0700101 return 0;
102}
103
104/* Return next attribute in a stream of attributes. */
105struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
106{
107 struct nlattr *next_nla = NULL;
108 if (nla->nla_len >= sizeof(struct nlattr) &&
109 nla->nla_len <= *remaining){
110 next_nla = (struct nlattr *) \
111 ((char *) nla + NLA_ALIGN(nla->nla_len));
112 *remaining = *remaining - NLA_ALIGN(nla->nla_len);
113 }
114
115 return next_nla;
116
117}
118
119/* Check if the attribute header and payload can be accessed safely. */
120int nla_ok(const struct nlattr *nla, int remaining)
121{
122 return remaining > 0 &&
123 nla->nla_len >= sizeof(struct nlattr) &&
124 sizeof(struct nlattr) <= (unsigned int) remaining &&
125 nla->nla_len <= remaining;
126}
127
128/* Create attribute index based on a stream of attributes. */
129/* NOTE: Policy not used ! */
130int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
131 int len, struct nla_policy *policy)
132{
133 struct nlattr *pos;
134 int rem;
135
136 /* First clear table */
137 memset(tb, 0, (maxtype+1) * sizeof(struct nlattr *));
138
139 nla_for_each_attr(pos, head, len, rem) {
140 const int type = nla_type(pos);
141
142 if (type <= maxtype)
143 tb[type] = pos;
144
145 }
146
147 return 0;
148}
149
150
151/* Create attribute index based on nested attribute. */
152int nla_parse_nested(struct nlattr *tb[], int maxtype,
153 struct nlattr *nla, struct nla_policy *policy)
154{
155 return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
156}
157
158
159/* Add a unspecific attribute to netlink message. */
160int nla_put(struct nl_msg *msg, int attrtype, int datalen, const void *data)
161{
162 struct nlattr *nla;
163
164 /* Reserve space and init nla header */
165 nla = nla_reserve(msg, attrtype, datalen);
Frank Maker1b534832011-06-30 17:00:49 -0700166 if (nla) {
Frank Makered6b39c2011-05-23 21:14:58 -0700167 memcpy(nla_data(nla), data, datalen);
Frank Maker1b534832011-06-30 17:00:49 -0700168 return 0;
169 }
Frank Makered6b39c2011-05-23 21:14:58 -0700170
Frank Maker1b534832011-06-30 17:00:49 -0700171 return -EINVAL;
Frank Makered6b39c2011-05-23 21:14:58 -0700172
173}
174
175
176/* Add nested attributes to netlink message. */
177/* Takes the attributes found in the nested message and appends them
178 * to the message msg nested in a container of the type attrtype. The
179 * nested message may not have a family specific header */
180int nla_put_nested(struct nl_msg *msg, int attrtype, struct nl_msg *nested)
181{
182 int rc = -1;
183 const int NO_HEADER = 0;
184
185 rc = nla_put(
186 msg,
187 attrtype,
188 nlmsg_attrlen(nlmsg_hdr(nested), NO_HEADER),
189 (const void *) nlmsg_attrdata(nlmsg_hdr(nested), NO_HEADER)
190 );
191 return rc;
192
193}
194
195/* Return type of the attribute. */
196int nla_type(const struct nlattr *nla)
197{
198 return (int) nla->nla_type;
199}
200
201/* Reserves room for an attribute in specified netlink message and fills
202 * in the attribute header (type,length). Return NULL if insufficient space */
203struct nlattr *nla_reserve(struct nl_msg * msg, int attrtype, int data_len)
204{
205
206 struct nlattr *nla;
207 const unsigned int NEW_SIZE = \
208 msg->nm_nlh->nlmsg_len + NLA_ALIGN(NLA_HDRLEN + data_len);
209
210 /* Check enough space for attribute */
211 if (NEW_SIZE <= msg->nm_size) {
212 const int fam_hdrlen = msg->nm_nlh->nlmsg_len - NLMSG_HDRLEN;
213 msg->nm_nlh->nlmsg_len = NEW_SIZE;
214 nla = nlmsg_attrdata(msg->nm_nlh, fam_hdrlen);
215 nla->nla_type = attrtype;
216 nla->nla_len = NLA_HDRLEN + data_len;
217 } else
218 goto fail;
219
220 return nla;
221fail:
222 return NULL;
223
224}
225
226/* Copy attribute payload to another memory area. */
227int nla_memcpy(void *dest, struct nlattr *src, int count)
228{
229 int rc;
230 void *ret_dest = memcpy(dest, nla_data(src), count);
231 if (!ret_dest)
232 return count;
233 else
234 return 0;
235}
236
237