~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/lib/bpf/nlattr.h

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /tools/lib/bpf/nlattr.h (Version linux-6.12-rc7) and /tools/lib/bpf/nlattr.h (Version linux-6.8.12)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php