Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 1 | #ifndef __NET_NETLINK_H |
| 2 | #define __NET_NETLINK_H |
| 3 | |
| 4 | #include <linux/types.h> |
| 5 | #include <linux/netlink.h> |
Al Viro | d7fe0f2 | 2006-12-03 23:15:30 -0500 | [diff] [blame] | 6 | #include <linux/jiffies.h> |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 7 | |
| 8 | /* ======================================================================== |
| 9 | * Netlink Messages and Attributes Interface (As Seen On TV) |
| 10 | * ------------------------------------------------------------------------ |
| 11 | * Messages Interface |
| 12 | * ------------------------------------------------------------------------ |
| 13 | * |
| 14 | * Message Format: |
| 15 | * <--- nlmsg_total_size(payload) ---> |
| 16 | * <-- nlmsg_msg_size(payload) -> |
| 17 | * +----------+- - -+-------------+- - -+-------- - - |
| 18 | * | nlmsghdr | Pad | Payload | Pad | nlmsghdr |
| 19 | * +----------+- - -+-------------+- - -+-------- - - |
| 20 | * nlmsg_data(nlh)---^ ^ |
| 21 | * nlmsg_next(nlh)-----------------------+ |
| 22 | * |
| 23 | * Payload Format: |
| 24 | * <---------------------- nlmsg_len(nlh) ---------------------> |
| 25 | * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) -> |
| 26 | * +----------------------+- - -+--------------------------------+ |
| 27 | * | Family Header | Pad | Attributes | |
| 28 | * +----------------------+- - -+--------------------------------+ |
| 29 | * nlmsg_attrdata(nlh, hdrlen)---^ |
| 30 | * |
| 31 | * Data Structures: |
| 32 | * struct nlmsghdr netlink message header |
| 33 | * |
| 34 | * Message Construction: |
| 35 | * nlmsg_new() create a new netlink message |
| 36 | * nlmsg_put() add a netlink message to an skb |
| 37 | * nlmsg_put_answer() callback based nlmsg_put() |
| 38 | * nlmsg_end() finanlize netlink message |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 39 | * nlmsg_get_pos() return current position in message |
| 40 | * nlmsg_trim() trim part of message |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 41 | * nlmsg_cancel() cancel message construction |
| 42 | * nlmsg_free() free a netlink message |
| 43 | * |
| 44 | * Message Sending: |
| 45 | * nlmsg_multicast() multicast message to several groups |
| 46 | * nlmsg_unicast() unicast a message to a single socket |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 47 | * nlmsg_notify() send notification message |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 48 | * |
| 49 | * Message Length Calculations: |
| 50 | * nlmsg_msg_size(payload) length of message w/o padding |
| 51 | * nlmsg_total_size(payload) length of message w/ padding |
| 52 | * nlmsg_padlen(payload) length of padding at tail |
| 53 | * |
| 54 | * Message Payload Access: |
| 55 | * nlmsg_data(nlh) head of message payload |
| 56 | * nlmsg_len(nlh) length of message payload |
| 57 | * nlmsg_attrdata(nlh, hdrlen) head of attributes data |
| 58 | * nlmsg_attrlen(nlh, hdrlen) length of attributes data |
| 59 | * |
| 60 | * Message Parsing: |
| 61 | * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes? |
| 62 | * nlmsg_next(nlh, remaining) get next netlink message |
| 63 | * nlmsg_parse() parse attributes of a message |
| 64 | * nlmsg_find_attr() find an attribute in a message |
| 65 | * nlmsg_for_each_msg() loop over all messages |
| 66 | * nlmsg_validate() validate netlink message incl. attrs |
| 67 | * nlmsg_for_each_attr() loop over all attributes |
| 68 | * |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 69 | * Misc: |
| 70 | * nlmsg_report() report back to application? |
| 71 | * |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 72 | * ------------------------------------------------------------------------ |
| 73 | * Attributes Interface |
| 74 | * ------------------------------------------------------------------------ |
| 75 | * |
| 76 | * Attribute Format: |
| 77 | * <------- nla_total_size(payload) -------> |
| 78 | * <---- nla_attr_size(payload) -----> |
| 79 | * +----------+- - -+- - - - - - - - - +- - -+-------- - - |
| 80 | * | Header | Pad | Payload | Pad | Header |
| 81 | * +----------+- - -+- - - - - - - - - +- - -+-------- - - |
| 82 | * <- nla_len(nla) -> ^ |
| 83 | * nla_data(nla)----^ | |
| 84 | * nla_next(nla)-----------------------------' |
| 85 | * |
| 86 | * Data Structures: |
| 87 | * struct nlattr netlink attribtue header |
| 88 | * |
| 89 | * Attribute Construction: |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 90 | * nla_reserve(skb, type, len) reserve room for an attribute |
| 91 | * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 92 | * nla_put(skb, type, len, data) add attribute to skb |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 93 | * nla_put_nohdr(skb, len, data) add attribute w/o hdr |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 94 | * |
| 95 | * Attribute Construction for Basic Types: |
| 96 | * nla_put_u8(skb, type, value) add u8 attribute to skb |
| 97 | * nla_put_u16(skb, type, value) add u16 attribute to skb |
| 98 | * nla_put_u32(skb, type, value) add u32 attribute to skb |
| 99 | * nla_put_u64(skb, type, value) add u64 attribute to skb |
| 100 | * nla_put_string(skb, type, str) add string attribute to skb |
| 101 | * nla_put_flag(skb, type) add flag attribute to skb |
| 102 | * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb |
| 103 | * |
| 104 | * Exceptions Based Attribute Construction: |
| 105 | * NLA_PUT(skb, type, len, data) add attribute to skb |
| 106 | * NLA_PUT_U8(skb, type, value) add u8 attribute to skb |
| 107 | * NLA_PUT_U16(skb, type, value) add u16 attribute to skb |
| 108 | * NLA_PUT_U32(skb, type, value) add u32 attribute to skb |
| 109 | * NLA_PUT_U64(skb, type, value) add u64 attribute to skb |
| 110 | * NLA_PUT_STRING(skb, type, str) add string attribute to skb |
| 111 | * NLA_PUT_FLAG(skb, type) add flag attribute to skb |
| 112 | * NLA_PUT_MSECS(skb, type, jiffies) add msecs attribute to skb |
| 113 | * |
| 114 | * The meaning of these functions is equal to their lower case |
| 115 | * variants but they jump to the label nla_put_failure in case |
| 116 | * of a failure. |
| 117 | * |
| 118 | * Nested Attributes Construction: |
| 119 | * nla_nest_start(skb, type) start a nested attribute |
| 120 | * nla_nest_end(skb, nla) finalize a nested attribute |
| 121 | * nla_nest_cancel(skb, nla) cancel nested attribute construction |
| 122 | * |
| 123 | * Attribute Length Calculations: |
| 124 | * nla_attr_size(payload) length of attribute w/o padding |
| 125 | * nla_total_size(payload) length of attribute w/ padding |
| 126 | * nla_padlen(payload) length of padding |
| 127 | * |
| 128 | * Attribute Payload Access: |
| 129 | * nla_data(nla) head of attribute payload |
| 130 | * nla_len(nla) length of attribute payload |
| 131 | * |
| 132 | * Attribute Payload Access for Basic Types: |
| 133 | * nla_get_u8(nla) get payload for a u8 attribute |
| 134 | * nla_get_u16(nla) get payload for a u16 attribute |
| 135 | * nla_get_u32(nla) get payload for a u32 attribute |
| 136 | * nla_get_u64(nla) get payload for a u64 attribute |
| 137 | * nla_get_flag(nla) return 1 if flag is true |
| 138 | * nla_get_msecs(nla) get payload for a msecs attribute |
| 139 | * |
| 140 | * Attribute Misc: |
| 141 | * nla_memcpy(dest, nla, count) copy attribute into memory |
| 142 | * nla_memcmp(nla, data, size) compare attribute with memory area |
| 143 | * nla_strlcpy(dst, nla, size) copy attribute to a sized string |
| 144 | * nla_strcmp(nla, str) compare attribute with string |
| 145 | * |
| 146 | * Attribute Parsing: |
| 147 | * nla_ok(nla, remaining) does nla fit into remaining bytes? |
| 148 | * nla_next(nla, remaining) get next netlink attribute |
| 149 | * nla_validate() validate a stream of attributes |
Paul Moore | 4fe5d5c | 2006-09-25 15:54:03 -0700 | [diff] [blame] | 150 | * nla_validate_nested() validate a stream of nested attributes |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 151 | * nla_find() find attribute in stream of attributes |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 152 | * nla_find_nested() find attribute in nested attributes |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 153 | * nla_parse() parse and validate stream of attrs |
| 154 | * nla_parse_nested() parse nested attribuets |
| 155 | * nla_for_each_attr() loop over all attributes |
Paul Moore | 22acb19 | 2006-09-25 15:53:37 -0700 | [diff] [blame] | 156 | * nla_for_each_nested() loop over the nested attributes |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 157 | *========================================================================= |
| 158 | */ |
| 159 | |
| 160 | /** |
| 161 | * Standard attribute types to specify validation policy |
| 162 | */ |
| 163 | enum { |
| 164 | NLA_UNSPEC, |
| 165 | NLA_U8, |
| 166 | NLA_U16, |
| 167 | NLA_U32, |
| 168 | NLA_U64, |
| 169 | NLA_STRING, |
| 170 | NLA_FLAG, |
| 171 | NLA_MSECS, |
| 172 | NLA_NESTED, |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 173 | NLA_NUL_STRING, |
Johannes Berg | d30045a | 2007-03-23 11:37:48 -0700 | [diff] [blame] | 174 | NLA_BINARY, |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 175 | __NLA_TYPE_MAX, |
| 176 | }; |
| 177 | |
| 178 | #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) |
| 179 | |
| 180 | /** |
| 181 | * struct nla_policy - attribute validation policy |
| 182 | * @type: Type of attribute or NLA_UNSPEC |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 183 | * @len: Type specific length of payload |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 184 | * |
| 185 | * Policies are defined as arrays of this struct, the array must be |
| 186 | * accessible by attribute type up to the highest identifier to be expected. |
| 187 | * |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 188 | * Meaning of `len' field: |
| 189 | * NLA_STRING Maximum length of string |
| 190 | * NLA_NUL_STRING Maximum length of string (excluding NUL) |
| 191 | * NLA_FLAG Unused |
Johannes Berg | d30045a | 2007-03-23 11:37:48 -0700 | [diff] [blame] | 192 | * NLA_BINARY Maximum length of attribute payload |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 193 | * All other Exact length of attribute payload |
| 194 | * |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 195 | * Example: |
| 196 | * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = { |
| 197 | * [ATTR_FOO] = { .type = NLA_U16 }, |
Johannes Berg | d30045a | 2007-03-23 11:37:48 -0700 | [diff] [blame] | 198 | * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ }, |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 199 | * [ATTR_BAZ] = { .len = sizeof(struct mystruct) }, |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 200 | * }; |
| 201 | */ |
| 202 | struct nla_policy { |
| 203 | u16 type; |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 204 | u16 len; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 205 | }; |
| 206 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 207 | /** |
| 208 | * struct nl_info - netlink source information |
| 209 | * @nlh: Netlink message header of original request |
| 210 | * @pid: Netlink PID of requesting application |
| 211 | */ |
| 212 | struct nl_info { |
| 213 | struct nlmsghdr *nlh; |
| 214 | u32 pid; |
| 215 | }; |
| 216 | |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 217 | extern void netlink_run_queue(struct sock *sk, unsigned int *qlen, |
| 218 | int (*cb)(struct sk_buff *, |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 219 | struct nlmsghdr *)); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 220 | extern int nlmsg_notify(struct sock *sk, struct sk_buff *skb, |
| 221 | u32 pid, unsigned int group, int report, |
| 222 | gfp_t flags); |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 223 | |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 224 | extern int nla_validate(struct nlattr *head, int len, int maxtype, |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 225 | const struct nla_policy *policy); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 226 | extern int nla_parse(struct nlattr *tb[], int maxtype, |
| 227 | struct nlattr *head, int len, |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 228 | const struct nla_policy *policy); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 229 | extern struct nlattr * nla_find(struct nlattr *head, int len, int attrtype); |
| 230 | extern size_t nla_strlcpy(char *dst, const struct nlattr *nla, |
| 231 | size_t dstsize); |
| 232 | extern int nla_memcpy(void *dest, struct nlattr *src, int count); |
| 233 | extern int nla_memcmp(const struct nlattr *nla, const void *data, |
| 234 | size_t size); |
| 235 | extern int nla_strcmp(const struct nlattr *nla, const char *str); |
| 236 | extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype, |
| 237 | int attrlen); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 238 | extern void * __nla_reserve_nohdr(struct sk_buff *skb, int attrlen); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 239 | extern struct nlattr * nla_reserve(struct sk_buff *skb, int attrtype, |
| 240 | int attrlen); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 241 | extern void * nla_reserve_nohdr(struct sk_buff *skb, int attrlen); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 242 | extern void __nla_put(struct sk_buff *skb, int attrtype, |
| 243 | int attrlen, const void *data); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 244 | extern void __nla_put_nohdr(struct sk_buff *skb, int attrlen, |
| 245 | const void *data); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 246 | extern int nla_put(struct sk_buff *skb, int attrtype, |
| 247 | int attrlen, const void *data); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 248 | extern int nla_put_nohdr(struct sk_buff *skb, int attrlen, |
| 249 | const void *data); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 250 | |
| 251 | /************************************************************************** |
| 252 | * Netlink Messages |
| 253 | **************************************************************************/ |
| 254 | |
| 255 | /** |
| 256 | * nlmsg_msg_size - length of netlink message not including padding |
| 257 | * @payload: length of message payload |
| 258 | */ |
| 259 | static inline int nlmsg_msg_size(int payload) |
| 260 | { |
| 261 | return NLMSG_HDRLEN + payload; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * nlmsg_total_size - length of netlink message including padding |
| 266 | * @payload: length of message payload |
| 267 | */ |
| 268 | static inline int nlmsg_total_size(int payload) |
| 269 | { |
| 270 | return NLMSG_ALIGN(nlmsg_msg_size(payload)); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * nlmsg_padlen - length of padding at the message's tail |
| 275 | * @payload: length of message payload |
| 276 | */ |
| 277 | static inline int nlmsg_padlen(int payload) |
| 278 | { |
| 279 | return nlmsg_total_size(payload) - nlmsg_msg_size(payload); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * nlmsg_data - head of message payload |
| 284 | * @nlh: netlink messsage header |
| 285 | */ |
| 286 | static inline void *nlmsg_data(const struct nlmsghdr *nlh) |
| 287 | { |
| 288 | return (unsigned char *) nlh + NLMSG_HDRLEN; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * nlmsg_len - length of message payload |
| 293 | * @nlh: netlink message header |
| 294 | */ |
| 295 | static inline int nlmsg_len(const struct nlmsghdr *nlh) |
| 296 | { |
| 297 | return nlh->nlmsg_len - NLMSG_HDRLEN; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * nlmsg_attrdata - head of attributes data |
| 302 | * @nlh: netlink message header |
| 303 | * @hdrlen: length of family specific header |
| 304 | */ |
| 305 | static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, |
| 306 | int hdrlen) |
| 307 | { |
| 308 | unsigned char *data = nlmsg_data(nlh); |
| 309 | return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen)); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * nlmsg_attrlen - length of attributes data |
| 314 | * @nlh: netlink message header |
| 315 | * @hdrlen: length of family specific header |
| 316 | */ |
| 317 | static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen) |
| 318 | { |
| 319 | return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * nlmsg_ok - check if the netlink message fits into the remaining bytes |
| 324 | * @nlh: netlink message header |
| 325 | * @remaining: number of bytes remaining in message stream |
| 326 | */ |
| 327 | static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining) |
| 328 | { |
| 329 | return (remaining >= sizeof(struct nlmsghdr) && |
| 330 | nlh->nlmsg_len >= sizeof(struct nlmsghdr) && |
| 331 | nlh->nlmsg_len <= remaining); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * nlmsg_next - next netlink message in message stream |
| 336 | * @nlh: netlink message header |
| 337 | * @remaining: number of bytes remaining in message stream |
| 338 | * |
| 339 | * Returns the next netlink message in the message stream and |
| 340 | * decrements remaining by the size of the current message. |
| 341 | */ |
| 342 | static inline struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining) |
| 343 | { |
| 344 | int totlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 345 | |
| 346 | *remaining -= totlen; |
| 347 | |
| 348 | return (struct nlmsghdr *) ((unsigned char *) nlh + totlen); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * nlmsg_parse - parse attributes of a netlink message |
| 353 | * @nlh: netlink message header |
| 354 | * @hdrlen: length of family specific header |
| 355 | * @tb: destination array with maxtype+1 elements |
| 356 | * @maxtype: maximum attribute type to be expected |
| 357 | * @policy: validation policy |
| 358 | * |
| 359 | * See nla_parse() |
| 360 | */ |
| 361 | static inline int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, |
| 362 | struct nlattr *tb[], int maxtype, |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 363 | const struct nla_policy *policy) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 364 | { |
| 365 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) |
| 366 | return -EINVAL; |
| 367 | |
| 368 | return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), |
| 369 | nlmsg_attrlen(nlh, hdrlen), policy); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * nlmsg_find_attr - find a specific attribute in a netlink message |
| 374 | * @nlh: netlink message header |
| 375 | * @hdrlen: length of familiy specific header |
| 376 | * @attrtype: type of attribute to look for |
| 377 | * |
| 378 | * Returns the first attribute which matches the specified type. |
| 379 | */ |
| 380 | static inline struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh, |
| 381 | int hdrlen, int attrtype) |
| 382 | { |
| 383 | return nla_find(nlmsg_attrdata(nlh, hdrlen), |
| 384 | nlmsg_attrlen(nlh, hdrlen), attrtype); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * nlmsg_validate - validate a netlink message including attributes |
| 389 | * @nlh: netlinket message header |
| 390 | * @hdrlen: length of familiy specific header |
| 391 | * @maxtype: maximum attribute type to be expected |
| 392 | * @policy: validation policy |
| 393 | */ |
| 394 | static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype, |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 395 | const struct nla_policy *policy) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 396 | { |
| 397 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) |
| 398 | return -EINVAL; |
| 399 | |
| 400 | return nla_validate(nlmsg_attrdata(nlh, hdrlen), |
| 401 | nlmsg_attrlen(nlh, hdrlen), maxtype, policy); |
| 402 | } |
| 403 | |
| 404 | /** |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 405 | * nlmsg_report - need to report back to application? |
| 406 | * @nlh: netlink message header |
| 407 | * |
| 408 | * Returns 1 if a report back to the application is requested. |
| 409 | */ |
| 410 | static inline int nlmsg_report(struct nlmsghdr *nlh) |
| 411 | { |
| 412 | return !!(nlh->nlmsg_flags & NLM_F_ECHO); |
| 413 | } |
| 414 | |
| 415 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 416 | * nlmsg_for_each_attr - iterate over a stream of attributes |
| 417 | * @pos: loop counter, set to current attribute |
| 418 | * @nlh: netlink message header |
| 419 | * @hdrlen: length of familiy specific header |
| 420 | * @rem: initialized to len, holds bytes currently remaining in stream |
| 421 | */ |
| 422 | #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \ |
| 423 | nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \ |
| 424 | nlmsg_attrlen(nlh, hdrlen), rem) |
| 425 | |
| 426 | #if 0 |
| 427 | /* FIXME: Enable once all users have been converted */ |
| 428 | |
| 429 | /** |
| 430 | * __nlmsg_put - Add a new netlink message to an skb |
| 431 | * @skb: socket buffer to store message in |
| 432 | * @pid: netlink process id |
| 433 | * @seq: sequence number of message |
| 434 | * @type: message type |
| 435 | * @payload: length of message payload |
| 436 | * @flags: message flags |
| 437 | * |
| 438 | * The caller is responsible to ensure that the skb provides enough |
| 439 | * tailroom for both the netlink header and payload. |
| 440 | */ |
| 441 | static inline struct nlmsghdr *__nlmsg_put(struct sk_buff *skb, u32 pid, |
| 442 | u32 seq, int type, int payload, |
| 443 | int flags) |
| 444 | { |
| 445 | struct nlmsghdr *nlh; |
| 446 | |
| 447 | nlh = (struct nlmsghdr *) skb_put(skb, nlmsg_total_size(payload)); |
| 448 | nlh->nlmsg_type = type; |
| 449 | nlh->nlmsg_len = nlmsg_msg_size(payload); |
| 450 | nlh->nlmsg_flags = flags; |
| 451 | nlh->nlmsg_pid = pid; |
| 452 | nlh->nlmsg_seq = seq; |
| 453 | |
| 454 | memset((unsigned char *) nlmsg_data(nlh) + payload, 0, |
| 455 | nlmsg_padlen(payload)); |
| 456 | |
| 457 | return nlh; |
| 458 | } |
| 459 | #endif |
| 460 | |
| 461 | /** |
| 462 | * nlmsg_put - Add a new netlink message to an skb |
| 463 | * @skb: socket buffer to store message in |
| 464 | * @pid: netlink process id |
| 465 | * @seq: sequence number of message |
| 466 | * @type: message type |
| 467 | * @payload: length of message payload |
| 468 | * @flags: message flags |
| 469 | * |
| 470 | * Returns NULL if the tailroom of the skb is insufficient to store |
| 471 | * the message header and payload. |
| 472 | */ |
| 473 | static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq, |
| 474 | int type, int payload, int flags) |
| 475 | { |
| 476 | if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload))) |
| 477 | return NULL; |
| 478 | |
| 479 | return __nlmsg_put(skb, pid, seq, type, payload, flags); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * nlmsg_put_answer - Add a new callback based netlink message to an skb |
| 484 | * @skb: socket buffer to store message in |
| 485 | * @cb: netlink callback |
| 486 | * @type: message type |
| 487 | * @payload: length of message payload |
| 488 | * @flags: message flags |
| 489 | * |
| 490 | * Returns NULL if the tailroom of the skb is insufficient to store |
| 491 | * the message header and payload. |
| 492 | */ |
| 493 | static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb, |
| 494 | struct netlink_callback *cb, |
| 495 | int type, int payload, |
| 496 | int flags) |
| 497 | { |
| 498 | return nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, |
| 499 | type, payload, flags); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * nlmsg_new - Allocate a new netlink message |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 504 | * @payload: size of the message payload |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 505 | * @flags: the type of memory to allocate. |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 506 | * |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 507 | * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known |
| 508 | * and a good default is needed. |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 509 | */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 510 | static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 511 | { |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 512 | return alloc_skb(nlmsg_total_size(payload), flags); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /** |
| 516 | * nlmsg_end - Finalize a netlink message |
| 517 | * @skb: socket buffer the message is stored in |
| 518 | * @nlh: netlink message header |
| 519 | * |
| 520 | * Corrects the netlink message header to include the appeneded |
| 521 | * attributes. Only necessary if attributes have been added to |
| 522 | * the message. |
| 523 | * |
| 524 | * Returns the total data length of the skb. |
| 525 | */ |
| 526 | static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 527 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 528 | nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 529 | |
| 530 | return skb->len; |
| 531 | } |
| 532 | |
| 533 | /** |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 534 | * nlmsg_get_pos - return current position in netlink message |
| 535 | * @skb: socket buffer the message is stored in |
| 536 | * |
| 537 | * Returns a pointer to the current tail of the message. |
| 538 | */ |
| 539 | static inline void *nlmsg_get_pos(struct sk_buff *skb) |
| 540 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 541 | return skb_tail_pointer(skb); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
| 545 | * nlmsg_trim - Trim message to a mark |
| 546 | * @skb: socket buffer the message is stored in |
| 547 | * @mark: mark to trim to |
| 548 | * |
| 549 | * Trims the message to the provided mark. Returns -1. |
| 550 | */ |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 551 | static inline int nlmsg_trim(struct sk_buff *skb, const void *mark) |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 552 | { |
| 553 | if (mark) |
| 554 | skb_trim(skb, (unsigned char *) mark - skb->data); |
| 555 | |
| 556 | return -1; |
| 557 | } |
| 558 | |
| 559 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 560 | * nlmsg_cancel - Cancel construction of a netlink message |
| 561 | * @skb: socket buffer the message is stored in |
| 562 | * @nlh: netlink message header |
| 563 | * |
| 564 | * Removes the complete netlink message including all |
| 565 | * attributes from the socket buffer again. Returns -1. |
| 566 | */ |
| 567 | static inline int nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 568 | { |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 569 | return nlmsg_trim(skb, nlh); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /** |
| 573 | * nlmsg_free - free a netlink message |
| 574 | * @skb: socket buffer of netlink message |
| 575 | */ |
| 576 | static inline void nlmsg_free(struct sk_buff *skb) |
| 577 | { |
| 578 | kfree_skb(skb); |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * nlmsg_multicast - multicast a netlink message |
| 583 | * @sk: netlink socket to spread messages to |
| 584 | * @skb: netlink message as socket buffer |
| 585 | * @pid: own netlink pid to avoid sending to yourself |
| 586 | * @group: multicast group id |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 587 | * @flags: allocation flags |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 588 | */ |
| 589 | static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 590 | u32 pid, unsigned int group, gfp_t flags) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 591 | { |
| 592 | int err; |
| 593 | |
| 594 | NETLINK_CB(skb).dst_group = group; |
| 595 | |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 596 | err = netlink_broadcast(sk, skb, pid, group, flags); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 597 | if (err > 0) |
| 598 | err = 0; |
| 599 | |
| 600 | return err; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * nlmsg_unicast - unicast a netlink message |
| 605 | * @sk: netlink socket to spread message to |
| 606 | * @skb: netlink message as socket buffer |
| 607 | * @pid: netlink pid of the destination socket |
| 608 | */ |
| 609 | static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid) |
| 610 | { |
| 611 | int err; |
| 612 | |
| 613 | err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT); |
| 614 | if (err > 0) |
| 615 | err = 0; |
| 616 | |
| 617 | return err; |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * nlmsg_for_each_msg - iterate over a stream of messages |
| 622 | * @pos: loop counter, set to current message |
| 623 | * @head: head of message stream |
| 624 | * @len: length of message stream |
| 625 | * @rem: initialized to len, holds bytes currently remaining in stream |
| 626 | */ |
| 627 | #define nlmsg_for_each_msg(pos, head, len, rem) \ |
| 628 | for (pos = head, rem = len; \ |
| 629 | nlmsg_ok(pos, rem); \ |
| 630 | pos = nlmsg_next(pos, &(rem))) |
| 631 | |
| 632 | /************************************************************************** |
| 633 | * Netlink Attributes |
| 634 | **************************************************************************/ |
| 635 | |
| 636 | /** |
| 637 | * nla_attr_size - length of attribute not including padding |
| 638 | * @payload: length of payload |
| 639 | */ |
| 640 | static inline int nla_attr_size(int payload) |
| 641 | { |
| 642 | return NLA_HDRLEN + payload; |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * nla_total_size - total length of attribute including padding |
| 647 | * @payload: length of payload |
| 648 | */ |
| 649 | static inline int nla_total_size(int payload) |
| 650 | { |
| 651 | return NLA_ALIGN(nla_attr_size(payload)); |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * nla_padlen - length of padding at the tail of attribute |
| 656 | * @payload: length of payload |
| 657 | */ |
| 658 | static inline int nla_padlen(int payload) |
| 659 | { |
| 660 | return nla_total_size(payload) - nla_attr_size(payload); |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * nla_data - head of payload |
| 665 | * @nla: netlink attribute |
| 666 | */ |
| 667 | static inline void *nla_data(const struct nlattr *nla) |
| 668 | { |
| 669 | return (char *) nla + NLA_HDRLEN; |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * nla_len - length of payload |
| 674 | * @nla: netlink attribute |
| 675 | */ |
| 676 | static inline int nla_len(const struct nlattr *nla) |
| 677 | { |
| 678 | return nla->nla_len - NLA_HDRLEN; |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * nla_ok - check if the netlink attribute fits into the remaining bytes |
| 683 | * @nla: netlink attribute |
| 684 | * @remaining: number of bytes remaining in attribute stream |
| 685 | */ |
| 686 | static inline int nla_ok(const struct nlattr *nla, int remaining) |
| 687 | { |
| 688 | return remaining >= sizeof(*nla) && |
| 689 | nla->nla_len >= sizeof(*nla) && |
| 690 | nla->nla_len <= remaining; |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * nla_next - next netlink attribte in attribute stream |
| 695 | * @nla: netlink attribute |
| 696 | * @remaining: number of bytes remaining in attribute stream |
| 697 | * |
| 698 | * Returns the next netlink attribute in the attribute stream and |
| 699 | * decrements remaining by the size of the current attribute. |
| 700 | */ |
| 701 | static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining) |
| 702 | { |
| 703 | int totlen = NLA_ALIGN(nla->nla_len); |
| 704 | |
| 705 | *remaining -= totlen; |
| 706 | return (struct nlattr *) ((char *) nla + totlen); |
| 707 | } |
| 708 | |
| 709 | /** |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 710 | * nla_find_nested - find attribute in a set of nested attributes |
| 711 | * @nla: attribute containing the nested attributes |
| 712 | * @attrtype: type of attribute to look for |
| 713 | * |
| 714 | * Returns the first attribute which matches the specified type. |
| 715 | */ |
| 716 | static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype) |
| 717 | { |
| 718 | return nla_find(nla_data(nla), nla_len(nla), attrtype); |
| 719 | } |
| 720 | |
| 721 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 722 | * nla_parse_nested - parse nested attributes |
| 723 | * @tb: destination array with maxtype+1 elements |
| 724 | * @maxtype: maximum attribute type to be expected |
| 725 | * @nla: attribute containing the nested attributes |
| 726 | * @policy: validation policy |
| 727 | * |
| 728 | * See nla_parse() |
| 729 | */ |
| 730 | static inline int nla_parse_nested(struct nlattr *tb[], int maxtype, |
| 731 | struct nlattr *nla, |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 732 | const struct nla_policy *policy) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 733 | { |
| 734 | return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy); |
| 735 | } |
| 736 | /** |
| 737 | * nla_put_u8 - Add a u16 netlink attribute to a socket buffer |
| 738 | * @skb: socket buffer to add attribute to |
| 739 | * @attrtype: attribute type |
| 740 | * @value: numeric value |
| 741 | */ |
| 742 | static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value) |
| 743 | { |
| 744 | return nla_put(skb, attrtype, sizeof(u8), &value); |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * nla_put_u16 - Add a u16 netlink attribute to a socket buffer |
| 749 | * @skb: socket buffer to add attribute to |
| 750 | * @attrtype: attribute type |
| 751 | * @value: numeric value |
| 752 | */ |
| 753 | static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value) |
| 754 | { |
| 755 | return nla_put(skb, attrtype, sizeof(u16), &value); |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * nla_put_u32 - Add a u32 netlink attribute to a socket buffer |
| 760 | * @skb: socket buffer to add attribute to |
| 761 | * @attrtype: attribute type |
| 762 | * @value: numeric value |
| 763 | */ |
| 764 | static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value) |
| 765 | { |
| 766 | return nla_put(skb, attrtype, sizeof(u32), &value); |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * nla_put_64 - Add a u64 netlink attribute to a socket buffer |
| 771 | * @skb: socket buffer to add attribute to |
| 772 | * @attrtype: attribute type |
| 773 | * @value: numeric value |
| 774 | */ |
| 775 | static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value) |
| 776 | { |
| 777 | return nla_put(skb, attrtype, sizeof(u64), &value); |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * nla_put_string - Add a string netlink attribute to a socket buffer |
| 782 | * @skb: socket buffer to add attribute to |
| 783 | * @attrtype: attribute type |
| 784 | * @str: NUL terminated string |
| 785 | */ |
| 786 | static inline int nla_put_string(struct sk_buff *skb, int attrtype, |
| 787 | const char *str) |
| 788 | { |
| 789 | return nla_put(skb, attrtype, strlen(str) + 1, str); |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * nla_put_flag - Add a flag netlink attribute to a socket buffer |
| 794 | * @skb: socket buffer to add attribute to |
| 795 | * @attrtype: attribute type |
| 796 | */ |
| 797 | static inline int nla_put_flag(struct sk_buff *skb, int attrtype) |
| 798 | { |
| 799 | return nla_put(skb, attrtype, 0, NULL); |
| 800 | } |
| 801 | |
| 802 | /** |
| 803 | * nla_put_msecs - Add a msecs netlink attribute to a socket buffer |
| 804 | * @skb: socket buffer to add attribute to |
| 805 | * @attrtype: attribute type |
| 806 | * @jiffies: number of msecs in jiffies |
| 807 | */ |
| 808 | static inline int nla_put_msecs(struct sk_buff *skb, int attrtype, |
| 809 | unsigned long jiffies) |
| 810 | { |
| 811 | u64 tmp = jiffies_to_msecs(jiffies); |
| 812 | return nla_put(skb, attrtype, sizeof(u64), &tmp); |
| 813 | } |
| 814 | |
| 815 | #define NLA_PUT(skb, attrtype, attrlen, data) \ |
| 816 | do { \ |
| 817 | if (nla_put(skb, attrtype, attrlen, data) < 0) \ |
| 818 | goto nla_put_failure; \ |
| 819 | } while(0) |
| 820 | |
| 821 | #define NLA_PUT_TYPE(skb, type, attrtype, value) \ |
| 822 | do { \ |
| 823 | type __tmp = value; \ |
| 824 | NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \ |
| 825 | } while(0) |
| 826 | |
| 827 | #define NLA_PUT_U8(skb, attrtype, value) \ |
| 828 | NLA_PUT_TYPE(skb, u8, attrtype, value) |
| 829 | |
| 830 | #define NLA_PUT_U16(skb, attrtype, value) \ |
| 831 | NLA_PUT_TYPE(skb, u16, attrtype, value) |
| 832 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 833 | #define NLA_PUT_LE16(skb, attrtype, value) \ |
| 834 | NLA_PUT_TYPE(skb, __le16, attrtype, value) |
| 835 | |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 836 | #define NLA_PUT_U32(skb, attrtype, value) \ |
| 837 | NLA_PUT_TYPE(skb, u32, attrtype, value) |
| 838 | |
Al Viro | 00012e5 | 2006-09-26 22:14:41 -0700 | [diff] [blame] | 839 | #define NLA_PUT_BE32(skb, attrtype, value) \ |
| 840 | NLA_PUT_TYPE(skb, __be32, attrtype, value) |
| 841 | |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 842 | #define NLA_PUT_U64(skb, attrtype, value) \ |
| 843 | NLA_PUT_TYPE(skb, u64, attrtype, value) |
| 844 | |
| 845 | #define NLA_PUT_STRING(skb, attrtype, value) \ |
| 846 | NLA_PUT(skb, attrtype, strlen(value) + 1, value) |
| 847 | |
Johannes Berg | ff5dfe7 | 2006-08-26 19:17:53 -0700 | [diff] [blame] | 848 | #define NLA_PUT_FLAG(skb, attrtype) \ |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 849 | NLA_PUT(skb, attrtype, 0, NULL) |
| 850 | |
| 851 | #define NLA_PUT_MSECS(skb, attrtype, jiffies) \ |
| 852 | NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies)) |
| 853 | |
| 854 | /** |
| 855 | * nla_get_u32 - return payload of u32 attribute |
| 856 | * @nla: u32 netlink attribute |
| 857 | */ |
| 858 | static inline u32 nla_get_u32(struct nlattr *nla) |
| 859 | { |
| 860 | return *(u32 *) nla_data(nla); |
| 861 | } |
| 862 | |
| 863 | /** |
Al Viro | 00012e5 | 2006-09-26 22:14:41 -0700 | [diff] [blame] | 864 | * nla_get_be32 - return payload of __be32 attribute |
| 865 | * @nla: __be32 netlink attribute |
| 866 | */ |
| 867 | static inline __be32 nla_get_be32(struct nlattr *nla) |
| 868 | { |
| 869 | return *(__be32 *) nla_data(nla); |
| 870 | } |
| 871 | |
| 872 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 873 | * nla_get_u16 - return payload of u16 attribute |
| 874 | * @nla: u16 netlink attribute |
| 875 | */ |
| 876 | static inline u16 nla_get_u16(struct nlattr *nla) |
| 877 | { |
| 878 | return *(u16 *) nla_data(nla); |
| 879 | } |
| 880 | |
| 881 | /** |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 882 | * nla_get_le16 - return payload of __le16 attribute |
| 883 | * @nla: __le16 netlink attribute |
| 884 | */ |
| 885 | static inline __le16 nla_get_le16(struct nlattr *nla) |
| 886 | { |
| 887 | return *(__le16 *) nla_data(nla); |
| 888 | } |
| 889 | |
| 890 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 891 | * nla_get_u8 - return payload of u8 attribute |
| 892 | * @nla: u8 netlink attribute |
| 893 | */ |
| 894 | static inline u8 nla_get_u8(struct nlattr *nla) |
| 895 | { |
| 896 | return *(u8 *) nla_data(nla); |
| 897 | } |
| 898 | |
| 899 | /** |
| 900 | * nla_get_u64 - return payload of u64 attribute |
| 901 | * @nla: u64 netlink attribute |
| 902 | */ |
| 903 | static inline u64 nla_get_u64(struct nlattr *nla) |
| 904 | { |
| 905 | u64 tmp; |
| 906 | |
| 907 | nla_memcpy(&tmp, nla, sizeof(tmp)); |
| 908 | |
| 909 | return tmp; |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | * nla_get_flag - return payload of flag attribute |
| 914 | * @nla: flag netlink attribute |
| 915 | */ |
| 916 | static inline int nla_get_flag(struct nlattr *nla) |
| 917 | { |
| 918 | return !!nla; |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * nla_get_msecs - return payload of msecs attribute |
| 923 | * @nla: msecs netlink attribute |
| 924 | * |
| 925 | * Returns the number of milliseconds in jiffies. |
| 926 | */ |
| 927 | static inline unsigned long nla_get_msecs(struct nlattr *nla) |
| 928 | { |
| 929 | u64 msecs = nla_get_u64(nla); |
| 930 | |
| 931 | return msecs_to_jiffies((unsigned long) msecs); |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * nla_nest_start - Start a new level of nested attributes |
| 936 | * @skb: socket buffer to add attributes to |
| 937 | * @attrtype: attribute type of container |
| 938 | * |
| 939 | * Returns the container attribute |
| 940 | */ |
| 941 | static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) |
| 942 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 943 | struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 944 | |
| 945 | if (nla_put(skb, attrtype, 0, NULL) < 0) |
| 946 | return NULL; |
| 947 | |
| 948 | return start; |
| 949 | } |
| 950 | |
| 951 | /** |
| 952 | * nla_nest_end - Finalize nesting of attributes |
| 953 | * @skb: socket buffer the attribtues are stored in |
| 954 | * @start: container attribute |
| 955 | * |
| 956 | * Corrects the container attribute header to include the all |
| 957 | * appeneded attributes. |
| 958 | * |
| 959 | * Returns the total data length of the skb. |
| 960 | */ |
| 961 | static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start) |
| 962 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 963 | start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 964 | return skb->len; |
| 965 | } |
| 966 | |
| 967 | /** |
| 968 | * nla_nest_cancel - Cancel nesting of attributes |
| 969 | * @skb: socket buffer the message is stored in |
| 970 | * @start: container attribute |
| 971 | * |
| 972 | * Removes the container attribute and including all nested |
| 973 | * attributes. Returns -1. |
| 974 | */ |
| 975 | static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) |
| 976 | { |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 977 | return nlmsg_trim(skb, start); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /** |
Paul Moore | 4fe5d5c | 2006-09-25 15:54:03 -0700 | [diff] [blame] | 981 | * nla_validate_nested - Validate a stream of nested attributes |
| 982 | * @start: container attribute |
| 983 | * @maxtype: maximum attribute type to be expected |
| 984 | * @policy: validation policy |
| 985 | * |
| 986 | * Validates all attributes in the nested attribute stream against the |
| 987 | * specified policy. Attributes with a type exceeding maxtype will be |
| 988 | * ignored. See documenation of struct nla_policy for more details. |
| 989 | * |
| 990 | * Returns 0 on success or a negative error code. |
| 991 | */ |
| 992 | static inline int nla_validate_nested(struct nlattr *start, int maxtype, |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 993 | const struct nla_policy *policy) |
Paul Moore | 4fe5d5c | 2006-09-25 15:54:03 -0700 | [diff] [blame] | 994 | { |
| 995 | return nla_validate(nla_data(start), nla_len(start), maxtype, policy); |
| 996 | } |
| 997 | |
| 998 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 999 | * nla_for_each_attr - iterate over a stream of attributes |
| 1000 | * @pos: loop counter, set to current attribute |
| 1001 | * @head: head of attribute stream |
| 1002 | * @len: length of attribute stream |
| 1003 | * @rem: initialized to len, holds bytes currently remaining in stream |
| 1004 | */ |
| 1005 | #define nla_for_each_attr(pos, head, len, rem) \ |
| 1006 | for (pos = head, rem = len; \ |
| 1007 | nla_ok(pos, rem); \ |
| 1008 | pos = nla_next(pos, &(rem))) |
| 1009 | |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 1010 | /** |
| 1011 | * nla_for_each_nested - iterate over nested attributes |
| 1012 | * @pos: loop counter, set to current attribute |
| 1013 | * @nla: attribute containing the nested attributes |
| 1014 | * @rem: initialized to len, holds bytes currently remaining in stream |
| 1015 | */ |
| 1016 | #define nla_for_each_nested(pos, nla, rem) \ |
| 1017 | nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem) |
| 1018 | |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 1019 | #endif |