blob: 2ef7590279ec0476ab23764840567a6f2b38935a [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
Dmitry Shmidt64f17e82011-08-10 15:25:51 -070069int nla_padlen(int payload)
70{
71 return NLA_ALIGN(payload) - payload;
72}
73
Frank Makered6b39c2011-05-23 21:14:58 -070074/* Start a new level of nested attributes. */
75struct nlattr *nla_nest_start(struct nl_msg *msg, int attrtype)
76{
Dmitry Shmidt64f17e82011-08-10 15:25:51 -070077 struct nlattr *start = (struct nlattr *)nlmsg_tail(msg->nm_nlh);
78 int rc;
Frank Makered6b39c2011-05-23 21:14:58 -070079
Dmitry Shmidt64f17e82011-08-10 15:25:51 -070080 rc = nla_put(msg, attrtype, 0, NULL);
81 if (rc < 0)
Frank Makered6b39c2011-05-23 21:14:58 -070082 return NULL;
83
Dmitry Shmidt64f17e82011-08-10 15:25:51 -070084 return start;
Frank Makered6b39c2011-05-23 21:14:58 -070085}
86
87/* Finalize nesting of attributes. */
88int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
89{
Dmitry Shmidt64f17e82011-08-10 15:25:51 -070090 /* Set attribute size */
91 start->nla_len = (unsigned char *)nlmsg_tail(nlmsg_hdr(msg)) -
92 (unsigned char *)start;
Frank Makered6b39c2011-05-23 21:14:58 -070093 return 0;
94}
95
96/* Return next attribute in a stream of attributes. */
97struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
98{
99 struct nlattr *next_nla = NULL;
100 if (nla->nla_len >= sizeof(struct nlattr) &&
101 nla->nla_len <= *remaining){
102 next_nla = (struct nlattr *) \
103 ((char *) nla + NLA_ALIGN(nla->nla_len));
104 *remaining = *remaining - NLA_ALIGN(nla->nla_len);
105 }
106
107 return next_nla;
108
109}
110
111/* Check if the attribute header and payload can be accessed safely. */
112int nla_ok(const struct nlattr *nla, int remaining)
113{
114 return remaining > 0 &&
115 nla->nla_len >= sizeof(struct nlattr) &&
116 sizeof(struct nlattr) <= (unsigned int) remaining &&
117 nla->nla_len <= remaining;
118}
119
120/* Create attribute index based on a stream of attributes. */
121/* NOTE: Policy not used ! */
122int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
123 int len, struct nla_policy *policy)
124{
125 struct nlattr *pos;
126 int rem;
127
128 /* First clear table */
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700129 memset(tb, 0, (maxtype + 1) * sizeof(struct nlattr *));
Frank Makered6b39c2011-05-23 21:14:58 -0700130
131 nla_for_each_attr(pos, head, len, rem) {
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700132 int type = nla_type(pos);
Frank Makered6b39c2011-05-23 21:14:58 -0700133
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700134 if ((type <= maxtype) && (type != 0))
Frank Makered6b39c2011-05-23 21:14:58 -0700135 tb[type] = pos;
Frank Makered6b39c2011-05-23 21:14:58 -0700136 }
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
Jouni Malinen831cbc92012-08-09 14:30:42 -0700165/* Add 8 bit integer attribute to netlink message. */
166int nla_put_u8(struct nl_msg *msg, int attrtype, uint8_t value)
167{
168 return nla_put(msg, attrtype, sizeof(uint8_t), &value);
169}
170
171/* Add 16 bit integer attribute to netlink message. */
172int nla_put_u16(struct nl_msg *msg, int attrtype, uint16_t value)
173{
174 return nla_put(msg, attrtype, sizeof(uint16_t), &value);
175}
176
177/* Add 32 bit integer attribute to netlink message. */
178int nla_put_u32(struct nl_msg *msg, int attrtype, uint32_t value)
179{
180 return nla_put(msg, attrtype, sizeof(uint32_t), &value);
181}
182
183/* Add 64 bit integer attribute to netlink message. */
184int nla_put_u64(struct nl_msg *msg, int attrtype, uint64_t value)
185{
186 return nla_put(msg, attrtype, sizeof(uint64_t), &value);
187}
Frank Makered6b39c2011-05-23 21:14:58 -0700188
189/* Add nested attributes to netlink message. */
190/* Takes the attributes found in the nested message and appends them
191 * to the message msg nested in a container of the type attrtype. The
192 * nested message may not have a family specific header */
193int nla_put_nested(struct nl_msg *msg, int attrtype, struct nl_msg *nested)
194{
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700195 int rc;
Frank Makered6b39c2011-05-23 21:14:58 -0700196
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700197 rc = nla_put(msg, attrtype, nlmsg_attrlen(nlmsg_hdr(nested), 0),
198 nlmsg_attrdata(nlmsg_hdr(nested), 0));
Frank Makered6b39c2011-05-23 21:14:58 -0700199 return rc;
200
201}
202
203/* Return type of the attribute. */
204int nla_type(const struct nlattr *nla)
205{
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700206 return (int)nla->nla_type & NLA_TYPE_MASK;
Frank Makered6b39c2011-05-23 21:14:58 -0700207}
208
209/* Reserves room for an attribute in specified netlink message and fills
210 * in the attribute header (type,length). Return NULL if insufficient space */
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700211struct nlattr *nla_reserve(struct nl_msg *msg, int attrtype, int data_len)
Frank Makered6b39c2011-05-23 21:14:58 -0700212{
213
214 struct nlattr *nla;
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700215 const unsigned int NEW_SIZE = NLMSG_ALIGN(msg->nm_nlh->nlmsg_len) +
216 NLA_ALIGN(NLA_HDRLEN + data_len);
Frank Makered6b39c2011-05-23 21:14:58 -0700217
218 /* Check enough space for attribute */
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700219 if (NEW_SIZE > msg->nm_size)
220 return NULL;
Frank Makered6b39c2011-05-23 21:14:58 -0700221
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700222 nla = (struct nlattr *)nlmsg_tail(msg->nm_nlh);
223 nla->nla_type = attrtype;
224 nla->nla_len = NLA_HDRLEN + data_len;
225 memset((unsigned char *)nla + nla->nla_len, 0, nla_padlen(data_len));
226 msg->nm_nlh->nlmsg_len = NEW_SIZE;
Frank Makered6b39c2011-05-23 21:14:58 -0700227 return nla;
Frank Makered6b39c2011-05-23 21:14:58 -0700228}
229
230/* Copy attribute payload to another memory area. */
231int nla_memcpy(void *dest, struct nlattr *src, int count)
232{
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700233 if (!src || !dest)
Frank Makered6b39c2011-05-23 21:14:58 -0700234 return 0;
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700235 if (count > nla_len(src))
236 count = nla_len(src);
237 memcpy(dest, nla_data(src), count);
238 return count;
Frank Makered6b39c2011-05-23 21:14:58 -0700239}