Skip to content

Commit

Permalink
netlink: let nlmsg and nla functions take pointer-to-const args
Browse files Browse the repository at this point in the history
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 <jengelh@medozas.de>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Jan Engelhardt authored and David S. Miller committed Nov 16, 2010
1 parent 9d82ca9 commit 3654654
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
21 changes: 13 additions & 8 deletions include/net/netlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,15 @@ extern int nlmsg_notify(struct sock *sk, struct sk_buff *skb,
u32 pid, unsigned int group, int report,
gfp_t flags);

extern int nla_validate(struct nlattr *head, int len, int maxtype,
extern int nla_validate(const struct nlattr *head,
int len, int maxtype,
const struct nla_policy *policy);
extern int nla_parse(struct nlattr *tb[], int maxtype,
struct nlattr *head, int len,
extern int nla_parse(struct nlattr **tb, int maxtype,
const struct nlattr *head, int len,
const struct nla_policy *policy);
extern int nla_policy_len(const struct nla_policy *, int);
extern struct nlattr * nla_find(struct nlattr *head, int len, int attrtype);
extern struct nlattr * nla_find(const struct nlattr *head,
int len, int attrtype);
extern size_t nla_strlcpy(char *dst, const struct nlattr *nla,
size_t dstsize);
extern int nla_memcpy(void *dest, const struct nlattr *src, int count);
Expand Down Expand Up @@ -346,7 +348,8 @@ static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
* Returns the next netlink message in the message stream and
* decrements remaining by the size of the current message.
*/
static inline struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
static inline struct nlmsghdr *
nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
{
int totlen = NLMSG_ALIGN(nlh->nlmsg_len);

Expand Down Expand Up @@ -398,7 +401,8 @@ static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
* @maxtype: maximum attribute type to be expected
* @policy: validation policy
*/
static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
static inline int nlmsg_validate(const struct nlmsghdr *nlh,
int hdrlen, int maxtype,
const struct nla_policy *policy)
{
if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Expand Down Expand Up @@ -727,7 +731,8 @@ static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
*
* Returns the first attribute which matches the specified type.
*/
static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype)
static inline struct nlattr *
nla_find_nested(const struct nlattr *nla, int attrtype)
{
return nla_find(nla_data(nla), nla_len(nla), attrtype);
}
Expand Down Expand Up @@ -1032,7 +1037,7 @@ static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
*
* Returns 0 on success or a negative error code.
*/
static inline int nla_validate_nested(struct nlattr *start, int maxtype,
static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
const struct nla_policy *policy)
{
return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
Expand Down
22 changes: 11 additions & 11 deletions lib/nlattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
#include <linux/types.h>
#include <net/netlink.h>

static u16 nla_attr_minlen[NLA_TYPE_MAX+1] __read_mostly = {
static const u16 nla_attr_minlen[NLA_TYPE_MAX+1] = {
[NLA_U8] = sizeof(u8),
[NLA_U16] = sizeof(u16),
[NLA_U32] = sizeof(u32),
[NLA_U64] = sizeof(u64),
[NLA_NESTED] = NLA_HDRLEN,
};

static int validate_nla(struct nlattr *nla, int maxtype,
static int validate_nla(const struct nlattr *nla, int maxtype,
const struct nla_policy *policy)
{
const struct nla_policy *pt;
Expand Down Expand Up @@ -115,10 +115,10 @@ static int validate_nla(struct nlattr *nla, int maxtype,
*
* Returns 0 on success or a negative error code.
*/
int nla_validate(struct nlattr *head, int len, int maxtype,
int nla_validate(const struct nlattr *head, int len, int maxtype,
const struct nla_policy *policy)
{
struct nlattr *nla;
const struct nlattr *nla;
int rem, err;

nla_for_each_attr(nla, head, len, rem) {
Expand Down Expand Up @@ -173,10 +173,10 @@ nla_policy_len(const struct nla_policy *p, int n)
*
* Returns 0 on success or a negative error code.
*/
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
const struct nla_policy *policy)
int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
int len, const struct nla_policy *policy)
{
struct nlattr *nla;
const struct nlattr *nla;
int rem, err;

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

tb[type] = nla;
tb[type] = (struct nlattr *)nla;
}
}

Expand All @@ -212,14 +212,14 @@ int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
*
* Returns the first attribute in the stream matching the specified type.
*/
struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
{
struct nlattr *nla;
const struct nlattr *nla;
int rem;

nla_for_each_attr(nla, head, len, rem)
if (nla_type(nla) == attrtype)
return nla;
return (struct nlattr *)nla;

return NULL;
}
Expand Down

0 comments on commit 3654654

Please sign in to comment.