blob: 94676681bc90e6488bf359af9b94421965f89965 [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{
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);
Frank Maker1b534832011-06-30 17:00:49 -0700157 if (nla) {
Frank Makered6b39c2011-05-23 21:14:58 -0700158 memcpy(nla_data(nla), data, datalen);
Frank Maker1b534832011-06-30 17:00:49 -0700159 return 0;
160 }
Frank Makered6b39c2011-05-23 21:14:58 -0700161
Frank Maker1b534832011-06-30 17:00:49 -0700162 return -EINVAL;
Frank Makered6b39c2011-05-23 21:14:58 -0700163
164}
165
166
167/* Add nested attributes to netlink message. */
168/* Takes the attributes found in the nested message and appends them
169 * to the message msg nested in a container of the type attrtype. The
170 * nested message may not have a family specific header */
171int nla_put_nested(struct nl_msg *msg, int attrtype, struct nl_msg *nested)
172{
173 int rc = -1;
174 const int NO_HEADER = 0;
175
176 rc = nla_put(
177 msg,
178 attrtype,
179 nlmsg_attrlen(nlmsg_hdr(nested), NO_HEADER),
180 (const void *) nlmsg_attrdata(nlmsg_hdr(nested), NO_HEADER)
181 );
182 return rc;
183
184}
185
186/* Return type of the attribute. */
187int nla_type(const struct nlattr *nla)
188{
189 return (int) nla->nla_type;
190}
191
192/* Reserves room for an attribute in specified netlink message and fills
193 * in the attribute header (type,length). Return NULL if insufficient space */
194struct nlattr *nla_reserve(struct nl_msg * msg, int attrtype, int data_len)
195{
196
197 struct nlattr *nla;
198 const unsigned int NEW_SIZE = \
199 msg->nm_nlh->nlmsg_len + NLA_ALIGN(NLA_HDRLEN + data_len);
200
201 /* Check enough space for attribute */
202 if (NEW_SIZE <= msg->nm_size) {
203 const int fam_hdrlen = msg->nm_nlh->nlmsg_len - NLMSG_HDRLEN;
204 msg->nm_nlh->nlmsg_len = NEW_SIZE;
205 nla = nlmsg_attrdata(msg->nm_nlh, fam_hdrlen);
206 nla->nla_type = attrtype;
207 nla->nla_len = NLA_HDRLEN + data_len;
208 } else
209 goto fail;
210
211 return nla;
212fail:
213 return NULL;
214
215}
216
217/* Copy attribute payload to another memory area. */
218int nla_memcpy(void *dest, struct nlattr *src, int count)
219{
220 int rc;
221 void *ret_dest = memcpy(dest, nla_data(src), count);
222 if (!ret_dest)
223 return count;
224 else
225 return 0;
226}
227
228