1 /* SPDX-License-Identifier: LGPL-2.1 */ 2 3 /* 4 * NETLINK Netlink attributes 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation version 2.1 9 * of the License. 10 * 11 * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch> 12 */ 13 14 #ifndef __NLATTR_H 15 #define __NLATTR_H 16 17 #include <stdint.h> 18 #include <linux/netlink.h> 19 /* avoid multiple definition of netlink features */ 20 #define __LINUX_NETLINK_H 21 22 /** 23 * Standard attribute types to specify validation policy 24 */ 25 enum { 26 LIBBPF_NLA_UNSPEC, /**< Unspecified type, binary data chunk */ 27 LIBBPF_NLA_U8, /**< 8 bit integer */ 28 LIBBPF_NLA_U16, /**< 16 bit integer */ 29 LIBBPF_NLA_U32, /**< 32 bit integer */ 30 LIBBPF_NLA_U64, /**< 64 bit integer */ 31 LIBBPF_NLA_STRING, /**< NUL terminated character string */ 32 LIBBPF_NLA_FLAG, /**< Flag */ 33 LIBBPF_NLA_MSECS, /**< Micro seconds (64bit) */ 34 LIBBPF_NLA_NESTED, /**< Nested attributes */ 35 __LIBBPF_NLA_TYPE_MAX, 36 }; 37 38 #define LIBBPF_NLA_TYPE_MAX (__LIBBPF_NLA_TYPE_MAX - 1) 39 40 /** 41 * @ingroup attr 42 * Attribute validation policy. 43 * 44 * See section @core_doc{core_attr_parse,Attribute Parsing} for more details. 45 */ 46 struct libbpf_nla_policy { 47 /** Type of attribute or LIBBPF_NLA_UNSPEC */ 48 uint16_t type; 49 50 /** Minimal length of payload required */ 51 uint16_t minlen; 52 53 /** Maximal length of payload allowed */ 54 uint16_t maxlen; 55 }; 56 57 /** 58 * @ingroup attr 59 * Iterate over a stream of attributes 60 * @arg pos loop counter, set to current attribute 61 * @arg head head of attribute stream 62 * @arg len length of attribute stream 63 * @arg rem initialized to len, holds bytes currently remaining in stream 64 */ 65 #define libbpf_nla_for_each_attr(pos, head, len, rem) \ 66 for (pos = head, rem = len; \ 67 nla_ok(pos, rem); \ 68 pos = nla_next(pos, &(rem))) 69 70 /** 71 * libbpf_nla_data - head of payload 72 * @nla: netlink attribute 73 */ 74 static inline void *libbpf_nla_data(const struct nlattr *nla) 75 { 76 return (char *) nla + NLA_HDRLEN; 77 } 78 79 static inline uint8_t libbpf_nla_getattr_u8(const struct nlattr *nla) 80 { 81 return *(uint8_t *)libbpf_nla_data(nla); 82 } 83 84 static inline uint32_t libbpf_nla_getattr_u32(const struct nlattr *nla) 85 { 86 return *(uint32_t *)libbpf_nla_data(nla); 87 } 88 89 static inline const char *libbpf_nla_getattr_str(const struct nlattr *nla) 90 { 91 return (const char *)libbpf_nla_data(nla); 92 } 93 94 /** 95 * libbpf_nla_len - length of payload 96 * @nla: netlink attribute 97 */ 98 static inline int libbpf_nla_len(const struct nlattr *nla) 99 { 100 return nla->nla_len - NLA_HDRLEN; 101 } 102 103 int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, 104 int len, struct libbpf_nla_policy *policy); 105 int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype, 106 struct nlattr *nla, 107 struct libbpf_nla_policy *policy); 108 109 int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh); 110 111 #endif /* __NLATTR_H */ 112