Skip to content

Commit 3654654

Browse files
Jan Engelhardtdavem330
authored andcommitted
netlink: let nlmsg and nla functions take pointer-to-const args
The changed functions do not modify the NL messages and/or attributes at all. They should use const (similar to strchr), so that callers which have a const nlmsg/nlattr around can make use of them without casting. While at it, constify a data array. Signed-off-by: Jan Engelhardt <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 9d82ca9 commit 3654654

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

include/net/netlink.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,15 @@ extern int nlmsg_notify(struct sock *sk, struct sk_buff *skb,
225225
u32 pid, unsigned int group, int report,
226226
gfp_t flags);
227227

228-
extern int nla_validate(struct nlattr *head, int len, int maxtype,
228+
extern int nla_validate(const struct nlattr *head,
229+
int len, int maxtype,
229230
const struct nla_policy *policy);
230-
extern int nla_parse(struct nlattr *tb[], int maxtype,
231-
struct nlattr *head, int len,
231+
extern int nla_parse(struct nlattr **tb, int maxtype,
232+
const struct nlattr *head, int len,
232233
const struct nla_policy *policy);
233234
extern int nla_policy_len(const struct nla_policy *, int);
234-
extern struct nlattr * nla_find(struct nlattr *head, int len, int attrtype);
235+
extern struct nlattr * nla_find(const struct nlattr *head,
236+
int len, int attrtype);
235237
extern size_t nla_strlcpy(char *dst, const struct nlattr *nla,
236238
size_t dstsize);
237239
extern int nla_memcpy(void *dest, const struct nlattr *src, int count);
@@ -346,7 +348,8 @@ static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
346348
* Returns the next netlink message in the message stream and
347349
* decrements remaining by the size of the current message.
348350
*/
349-
static inline struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
351+
static inline struct nlmsghdr *
352+
nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
350353
{
351354
int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
352355

@@ -398,7 +401,8 @@ static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
398401
* @maxtype: maximum attribute type to be expected
399402
* @policy: validation policy
400403
*/
401-
static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
404+
static inline int nlmsg_validate(const struct nlmsghdr *nlh,
405+
int hdrlen, int maxtype,
402406
const struct nla_policy *policy)
403407
{
404408
if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
@@ -727,7 +731,8 @@ static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
727731
*
728732
* Returns the first attribute which matches the specified type.
729733
*/
730-
static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype)
734+
static inline struct nlattr *
735+
nla_find_nested(const struct nlattr *nla, int attrtype)
731736
{
732737
return nla_find(nla_data(nla), nla_len(nla), attrtype);
733738
}
@@ -1032,7 +1037,7 @@ static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
10321037
*
10331038
* Returns 0 on success or a negative error code.
10341039
*/
1035-
static inline int nla_validate_nested(struct nlattr *start, int maxtype,
1040+
static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
10361041
const struct nla_policy *policy)
10371042
{
10381043
return nla_validate(nla_data(start), nla_len(start), maxtype, policy);

lib/nlattr.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
#include <linux/types.h>
1616
#include <net/netlink.h>
1717

18-
static u16 nla_attr_minlen[NLA_TYPE_MAX+1] __read_mostly = {
18+
static const u16 nla_attr_minlen[NLA_TYPE_MAX+1] = {
1919
[NLA_U8] = sizeof(u8),
2020
[NLA_U16] = sizeof(u16),
2121
[NLA_U32] = sizeof(u32),
2222
[NLA_U64] = sizeof(u64),
2323
[NLA_NESTED] = NLA_HDRLEN,
2424
};
2525

26-
static int validate_nla(struct nlattr *nla, int maxtype,
26+
static int validate_nla(const struct nlattr *nla, int maxtype,
2727
const struct nla_policy *policy)
2828
{
2929
const struct nla_policy *pt;
@@ -115,10 +115,10 @@ static int validate_nla(struct nlattr *nla, int maxtype,
115115
*
116116
* Returns 0 on success or a negative error code.
117117
*/
118-
int nla_validate(struct nlattr *head, int len, int maxtype,
118+
int nla_validate(const struct nlattr *head, int len, int maxtype,
119119
const struct nla_policy *policy)
120120
{
121-
struct nlattr *nla;
121+
const struct nlattr *nla;
122122
int rem, err;
123123

124124
nla_for_each_attr(nla, head, len, rem) {
@@ -173,10 +173,10 @@ nla_policy_len(const struct nla_policy *p, int n)
173173
*
174174
* Returns 0 on success or a negative error code.
175175
*/
176-
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
177-
const struct nla_policy *policy)
176+
int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
177+
int len, const struct nla_policy *policy)
178178
{
179-
struct nlattr *nla;
179+
const struct nlattr *nla;
180180
int rem, err;
181181

182182
memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
@@ -191,7 +191,7 @@ int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
191191
goto errout;
192192
}
193193

194-
tb[type] = nla;
194+
tb[type] = (struct nlattr *)nla;
195195
}
196196
}
197197

@@ -212,14 +212,14 @@ int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
212212
*
213213
* Returns the first attribute in the stream matching the specified type.
214214
*/
215-
struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
215+
struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
216216
{
217-
struct nlattr *nla;
217+
const struct nlattr *nla;
218218
int rem;
219219

220220
nla_for_each_attr(nla, head, len, rem)
221221
if (nla_type(nla) == attrtype)
222-
return nla;
222+
return (struct nlattr *)nla;
223223

224224
return NULL;
225225
}

0 commit comments

Comments
 (0)