1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2 1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 2 3 /* 3 /* 4 * NETLINK Netlink attributes 4 * NETLINK Netlink attributes 5 * 5 * 6 * Copyright (c) 2003-2013 Thomas Graf <tgraf@ 6 * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch> 7 */ 7 */ 8 8 9 #ifndef __LIBBPF_NLATTR_H 9 #ifndef __LIBBPF_NLATTR_H 10 #define __LIBBPF_NLATTR_H 10 #define __LIBBPF_NLATTR_H 11 11 12 #include <stdint.h> 12 #include <stdint.h> 13 #include <string.h> 13 #include <string.h> 14 #include <errno.h> 14 #include <errno.h> 15 #include <linux/netlink.h> 15 #include <linux/netlink.h> 16 #include <linux/rtnetlink.h> 16 #include <linux/rtnetlink.h> 17 #include <linux/genetlink.h> << 18 17 19 /* avoid multiple definition of netlink featur 18 /* avoid multiple definition of netlink features */ 20 #define __LINUX_NETLINK_H 19 #define __LINUX_NETLINK_H 21 20 22 /** 21 /** 23 * Standard attribute types to specify validat 22 * Standard attribute types to specify validation policy 24 */ 23 */ 25 enum { 24 enum { 26 LIBBPF_NLA_UNSPEC, /**< Unspecifi 25 LIBBPF_NLA_UNSPEC, /**< Unspecified type, binary data chunk */ 27 LIBBPF_NLA_U8, /**< 8 bit int 26 LIBBPF_NLA_U8, /**< 8 bit integer */ 28 LIBBPF_NLA_U16, /**< 16 bit in 27 LIBBPF_NLA_U16, /**< 16 bit integer */ 29 LIBBPF_NLA_U32, /**< 32 bit in 28 LIBBPF_NLA_U32, /**< 32 bit integer */ 30 LIBBPF_NLA_U64, /**< 64 bit in 29 LIBBPF_NLA_U64, /**< 64 bit integer */ 31 LIBBPF_NLA_STRING, /**< NUL termi 30 LIBBPF_NLA_STRING, /**< NUL terminated character string */ 32 LIBBPF_NLA_FLAG, /**< Flag */ 31 LIBBPF_NLA_FLAG, /**< Flag */ 33 LIBBPF_NLA_MSECS, /**< Micro sec 32 LIBBPF_NLA_MSECS, /**< Micro seconds (64bit) */ 34 LIBBPF_NLA_NESTED, /**< Nested at 33 LIBBPF_NLA_NESTED, /**< Nested attributes */ 35 __LIBBPF_NLA_TYPE_MAX, 34 __LIBBPF_NLA_TYPE_MAX, 36 }; 35 }; 37 36 38 #define LIBBPF_NLA_TYPE_MAX (__LIBBPF_NLA_TYPE 37 #define LIBBPF_NLA_TYPE_MAX (__LIBBPF_NLA_TYPE_MAX - 1) 39 38 40 /** 39 /** 41 * @ingroup attr 40 * @ingroup attr 42 * Attribute validation policy. 41 * Attribute validation policy. 43 * 42 * 44 * See section @core_doc{core_attr_parse,Attri 43 * See section @core_doc{core_attr_parse,Attribute Parsing} for more details. 45 */ 44 */ 46 struct libbpf_nla_policy { 45 struct libbpf_nla_policy { 47 /** Type of attribute or LIBBPF_NLA_UN 46 /** Type of attribute or LIBBPF_NLA_UNSPEC */ 48 uint16_t type; 47 uint16_t type; 49 48 50 /** Minimal length of payload required 49 /** Minimal length of payload required */ 51 uint16_t minlen; 50 uint16_t minlen; 52 51 53 /** Maximal length of payload allowed 52 /** Maximal length of payload allowed */ 54 uint16_t maxlen; 53 uint16_t maxlen; 55 }; 54 }; 56 55 57 struct libbpf_nla_req { 56 struct libbpf_nla_req { 58 struct nlmsghdr nh; 57 struct nlmsghdr nh; 59 union { 58 union { 60 struct ifinfomsg ifinfo; 59 struct ifinfomsg ifinfo; 61 struct tcmsg tc; 60 struct tcmsg tc; 62 struct genlmsghdr gnl; << 63 }; 61 }; 64 char buf[128]; 62 char buf[128]; 65 }; 63 }; 66 64 67 /** 65 /** 68 * @ingroup attr 66 * @ingroup attr 69 * Iterate over a stream of attributes 67 * Iterate over a stream of attributes 70 * @arg pos loop counter, set to current a 68 * @arg pos loop counter, set to current attribute 71 * @arg head head of attribute stream 69 * @arg head head of attribute stream 72 * @arg len length of attribute stream 70 * @arg len length of attribute stream 73 * @arg rem initialized to len, holds byte 71 * @arg rem initialized to len, holds bytes currently remaining in stream 74 */ 72 */ 75 #define libbpf_nla_for_each_attr(pos, head, le 73 #define libbpf_nla_for_each_attr(pos, head, len, rem) \ 76 for (pos = head, rem = len; \ 74 for (pos = head, rem = len; \ 77 nla_ok(pos, rem); \ 75 nla_ok(pos, rem); \ 78 pos = nla_next(pos, &(rem))) 76 pos = nla_next(pos, &(rem))) 79 77 80 /** 78 /** 81 * libbpf_nla_data - head of payload 79 * libbpf_nla_data - head of payload 82 * @nla: netlink attribute 80 * @nla: netlink attribute 83 */ 81 */ 84 static inline void *libbpf_nla_data(const stru 82 static inline void *libbpf_nla_data(const struct nlattr *nla) 85 { 83 { 86 return (void *)nla + NLA_HDRLEN; 84 return (void *)nla + NLA_HDRLEN; 87 } 85 } 88 86 89 static inline uint8_t libbpf_nla_getattr_u8(co 87 static inline uint8_t libbpf_nla_getattr_u8(const struct nlattr *nla) 90 { 88 { 91 return *(uint8_t *)libbpf_nla_data(nla 89 return *(uint8_t *)libbpf_nla_data(nla); 92 } 90 } 93 91 94 static inline uint16_t libbpf_nla_getattr_u16( << 95 { << 96 return *(uint16_t *)libbpf_nla_data(nl << 97 } << 98 << 99 static inline uint32_t libbpf_nla_getattr_u32( 92 static inline uint32_t libbpf_nla_getattr_u32(const struct nlattr *nla) 100 { 93 { 101 return *(uint32_t *)libbpf_nla_data(nl 94 return *(uint32_t *)libbpf_nla_data(nla); 102 } << 103 << 104 static inline uint64_t libbpf_nla_getattr_u64( << 105 { << 106 return *(uint64_t *)libbpf_nla_data(nl << 107 } 95 } 108 96 109 static inline const char *libbpf_nla_getattr_s 97 static inline const char *libbpf_nla_getattr_str(const struct nlattr *nla) 110 { 98 { 111 return (const char *)libbpf_nla_data(n 99 return (const char *)libbpf_nla_data(nla); 112 } 100 } 113 101 114 /** 102 /** 115 * libbpf_nla_len - length of payload 103 * libbpf_nla_len - length of payload 116 * @nla: netlink attribute 104 * @nla: netlink attribute 117 */ 105 */ 118 static inline int libbpf_nla_len(const struct 106 static inline int libbpf_nla_len(const struct nlattr *nla) 119 { 107 { 120 return nla->nla_len - NLA_HDRLEN; 108 return nla->nla_len - NLA_HDRLEN; 121 } 109 } 122 110 123 int libbpf_nla_parse(struct nlattr *tb[], int 111 int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, 124 int len, struct libbpf_nl 112 int len, struct libbpf_nla_policy *policy); 125 int libbpf_nla_parse_nested(struct nlattr *tb[ 113 int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype, 126 struct nlattr *nla 114 struct nlattr *nla, 127 struct libbpf_nla_ 115 struct libbpf_nla_policy *policy); 128 116 129 int libbpf_nla_dump_errormsg(struct nlmsghdr * 117 int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh); 130 118 131 static inline struct nlattr *nla_data(struct n 119 static inline struct nlattr *nla_data(struct nlattr *nla) 132 { 120 { 133 return (struct nlattr *)((void *)nla + 121 return (struct nlattr *)((void *)nla + NLA_HDRLEN); 134 } 122 } 135 123 136 static inline struct nlattr *req_tail(struct l 124 static inline struct nlattr *req_tail(struct libbpf_nla_req *req) 137 { 125 { 138 return (struct nlattr *)((void *)req + 126 return (struct nlattr *)((void *)req + NLMSG_ALIGN(req->nh.nlmsg_len)); 139 } 127 } 140 128 141 static inline int nlattr_add(struct libbpf_nla 129 static inline int nlattr_add(struct libbpf_nla_req *req, int type, 142 const void *data, 130 const void *data, int len) 143 { 131 { 144 struct nlattr *nla; 132 struct nlattr *nla; 145 133 146 if (NLMSG_ALIGN(req->nh.nlmsg_len) + N 134 if (NLMSG_ALIGN(req->nh.nlmsg_len) + NLA_ALIGN(NLA_HDRLEN + len) > sizeof(*req)) 147 return -EMSGSIZE; 135 return -EMSGSIZE; 148 if (!!data != !!len) 136 if (!!data != !!len) 149 return -EINVAL; 137 return -EINVAL; 150 138 151 nla = req_tail(req); 139 nla = req_tail(req); 152 nla->nla_type = type; 140 nla->nla_type = type; 153 nla->nla_len = NLA_HDRLEN + len; 141 nla->nla_len = NLA_HDRLEN + len; 154 if (data) 142 if (data) 155 memcpy(nla_data(nla), data, le 143 memcpy(nla_data(nla), data, len); 156 req->nh.nlmsg_len = NLMSG_ALIGN(req->n 144 req->nh.nlmsg_len = NLMSG_ALIGN(req->nh.nlmsg_len) + NLA_ALIGN(nla->nla_len); 157 return 0; 145 return 0; 158 } 146 } 159 147 160 static inline struct nlattr *nlattr_begin_nest 148 static inline struct nlattr *nlattr_begin_nested(struct libbpf_nla_req *req, int type) 161 { 149 { 162 struct nlattr *tail; 150 struct nlattr *tail; 163 151 164 tail = req_tail(req); 152 tail = req_tail(req); 165 if (nlattr_add(req, type | NLA_F_NESTE 153 if (nlattr_add(req, type | NLA_F_NESTED, NULL, 0)) 166 return NULL; 154 return NULL; 167 return tail; 155 return tail; 168 } 156 } 169 157 170 static inline void nlattr_end_nested(struct li 158 static inline void nlattr_end_nested(struct libbpf_nla_req *req, 171 struct nl 159 struct nlattr *tail) 172 { 160 { 173 tail->nla_len = (void *)req_tail(req) 161 tail->nla_len = (void *)req_tail(req) - (void *)tail; 174 } 162 } 175 163 176 #endif /* __LIBBPF_NLATTR_H */ 164 #endif /* __LIBBPF_NLATTR_H */ 177 165
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.