diff options
Diffstat (limited to 'include/net/netlink.h')
-rw-r--r-- | include/net/netlink.h | 154 |
1 files changed, 138 insertions, 16 deletions
diff --git a/include/net/netlink.h b/include/net/netlink.h index 640c26a90cf..ce5cba19c39 100644 --- a/include/net/netlink.h +++ b/include/net/netlink.h | |||
@@ -35,12 +35,15 @@ | |||
35 | * nlmsg_put() add a netlink message to an skb | 35 | * nlmsg_put() add a netlink message to an skb |
36 | * nlmsg_put_answer() callback based nlmsg_put() | 36 | * nlmsg_put_answer() callback based nlmsg_put() |
37 | * nlmsg_end() finanlize netlink message | 37 | * nlmsg_end() finanlize netlink message |
38 | * nlmsg_get_pos() return current position in message | ||
39 | * nlmsg_trim() trim part of message | ||
38 | * nlmsg_cancel() cancel message construction | 40 | * nlmsg_cancel() cancel message construction |
39 | * nlmsg_free() free a netlink message | 41 | * nlmsg_free() free a netlink message |
40 | * | 42 | * |
41 | * Message Sending: | 43 | * Message Sending: |
42 | * nlmsg_multicast() multicast message to several groups | 44 | * nlmsg_multicast() multicast message to several groups |
43 | * nlmsg_unicast() unicast a message to a single socket | 45 | * nlmsg_unicast() unicast a message to a single socket |
46 | * nlmsg_notify() send notification message | ||
44 | * | 47 | * |
45 | * Message Length Calculations: | 48 | * Message Length Calculations: |
46 | * nlmsg_msg_size(payload) length of message w/o padding | 49 | * nlmsg_msg_size(payload) length of message w/o padding |
@@ -62,6 +65,9 @@ | |||
62 | * nlmsg_validate() validate netlink message incl. attrs | 65 | * nlmsg_validate() validate netlink message incl. attrs |
63 | * nlmsg_for_each_attr() loop over all attributes | 66 | * nlmsg_for_each_attr() loop over all attributes |
64 | * | 67 | * |
68 | * Misc: | ||
69 | * nlmsg_report() report back to application? | ||
70 | * | ||
65 | * ------------------------------------------------------------------------ | 71 | * ------------------------------------------------------------------------ |
66 | * Attributes Interface | 72 | * Attributes Interface |
67 | * ------------------------------------------------------------------------ | 73 | * ------------------------------------------------------------------------ |
@@ -80,8 +86,10 @@ | |||
80 | * struct nlattr netlink attribtue header | 86 | * struct nlattr netlink attribtue header |
81 | * | 87 | * |
82 | * Attribute Construction: | 88 | * Attribute Construction: |
83 | * nla_reserve(skb, type, len) reserve skb tailroom for an attribute | 89 | * nla_reserve(skb, type, len) reserve room for an attribute |
90 | * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr | ||
84 | * nla_put(skb, type, len, data) add attribute to skb | 91 | * nla_put(skb, type, len, data) add attribute to skb |
92 | * nla_put_nohdr(skb, len, data) add attribute w/o hdr | ||
85 | * | 93 | * |
86 | * Attribute Construction for Basic Types: | 94 | * Attribute Construction for Basic Types: |
87 | * nla_put_u8(skb, type, value) add u8 attribute to skb | 95 | * nla_put_u8(skb, type, value) add u8 attribute to skb |
@@ -138,10 +146,13 @@ | |||
138 | * nla_ok(nla, remaining) does nla fit into remaining bytes? | 146 | * nla_ok(nla, remaining) does nla fit into remaining bytes? |
139 | * nla_next(nla, remaining) get next netlink attribute | 147 | * nla_next(nla, remaining) get next netlink attribute |
140 | * nla_validate() validate a stream of attributes | 148 | * nla_validate() validate a stream of attributes |
149 | * nla_validate_nested() validate a stream of nested attributes | ||
141 | * nla_find() find attribute in stream of attributes | 150 | * nla_find() find attribute in stream of attributes |
151 | * nla_find_nested() find attribute in nested attributes | ||
142 | * nla_parse() parse and validate stream of attrs | 152 | * nla_parse() parse and validate stream of attrs |
143 | * nla_parse_nested() parse nested attribuets | 153 | * nla_parse_nested() parse nested attribuets |
144 | * nla_for_each_attr() loop over all attributes | 154 | * nla_for_each_attr() loop over all attributes |
155 | * nla_for_each_nested() loop over the nested attributes | ||
145 | *========================================================================= | 156 | *========================================================================= |
146 | */ | 157 | */ |
147 | 158 | ||
@@ -158,6 +169,7 @@ enum { | |||
158 | NLA_FLAG, | 169 | NLA_FLAG, |
159 | NLA_MSECS, | 170 | NLA_MSECS, |
160 | NLA_NESTED, | 171 | NLA_NESTED, |
172 | NLA_NUL_STRING, | ||
161 | __NLA_TYPE_MAX, | 173 | __NLA_TYPE_MAX, |
162 | }; | 174 | }; |
163 | 175 | ||
@@ -166,21 +178,37 @@ enum { | |||
166 | /** | 178 | /** |
167 | * struct nla_policy - attribute validation policy | 179 | * struct nla_policy - attribute validation policy |
168 | * @type: Type of attribute or NLA_UNSPEC | 180 | * @type: Type of attribute or NLA_UNSPEC |
169 | * @minlen: Minimal length of payload required to be available | 181 | * @len: Type specific length of payload |
170 | * | 182 | * |
171 | * Policies are defined as arrays of this struct, the array must be | 183 | * Policies are defined as arrays of this struct, the array must be |
172 | * accessible by attribute type up to the highest identifier to be expected. | 184 | * accessible by attribute type up to the highest identifier to be expected. |
173 | * | 185 | * |
186 | * Meaning of `len' field: | ||
187 | * NLA_STRING Maximum length of string | ||
188 | * NLA_NUL_STRING Maximum length of string (excluding NUL) | ||
189 | * NLA_FLAG Unused | ||
190 | * All other Exact length of attribute payload | ||
191 | * | ||
174 | * Example: | 192 | * Example: |
175 | * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = { | 193 | * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = { |
176 | * [ATTR_FOO] = { .type = NLA_U16 }, | 194 | * [ATTR_FOO] = { .type = NLA_U16 }, |
177 | * [ATTR_BAR] = { .type = NLA_STRING }, | 195 | * [ATTR_BAR] = { .type = NLA_STRING, len = BARSIZ }, |
178 | * [ATTR_BAZ] = { .minlen = sizeof(struct mystruct) }, | 196 | * [ATTR_BAZ] = { .len = sizeof(struct mystruct) }, |
179 | * }; | 197 | * }; |
180 | */ | 198 | */ |
181 | struct nla_policy { | 199 | struct nla_policy { |
182 | u16 type; | 200 | u16 type; |
183 | u16 minlen; | 201 | u16 len; |
202 | }; | ||
203 | |||
204 | /** | ||
205 | * struct nl_info - netlink source information | ||
206 | * @nlh: Netlink message header of original request | ||
207 | * @pid: Netlink PID of requesting application | ||
208 | */ | ||
209 | struct nl_info { | ||
210 | struct nlmsghdr *nlh; | ||
211 | u32 pid; | ||
184 | }; | 212 | }; |
185 | 213 | ||
186 | extern void netlink_run_queue(struct sock *sk, unsigned int *qlen, | 214 | extern void netlink_run_queue(struct sock *sk, unsigned int *qlen, |
@@ -188,6 +216,9 @@ extern void netlink_run_queue(struct sock *sk, unsigned int *qlen, | |||
188 | struct nlmsghdr *, int *)); | 216 | struct nlmsghdr *, int *)); |
189 | extern void netlink_queue_skip(struct nlmsghdr *nlh, | 217 | extern void netlink_queue_skip(struct nlmsghdr *nlh, |
190 | struct sk_buff *skb); | 218 | struct sk_buff *skb); |
219 | extern int nlmsg_notify(struct sock *sk, struct sk_buff *skb, | ||
220 | u32 pid, unsigned int group, int report, | ||
221 | gfp_t flags); | ||
191 | 222 | ||
192 | extern int nla_validate(struct nlattr *head, int len, int maxtype, | 223 | extern int nla_validate(struct nlattr *head, int len, int maxtype, |
193 | struct nla_policy *policy); | 224 | struct nla_policy *policy); |
@@ -203,12 +234,18 @@ extern int nla_memcmp(const struct nlattr *nla, const void *data, | |||
203 | extern int nla_strcmp(const struct nlattr *nla, const char *str); | 234 | extern int nla_strcmp(const struct nlattr *nla, const char *str); |
204 | extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype, | 235 | extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype, |
205 | int attrlen); | 236 | int attrlen); |
237 | extern void * __nla_reserve_nohdr(struct sk_buff *skb, int attrlen); | ||
206 | extern struct nlattr * nla_reserve(struct sk_buff *skb, int attrtype, | 238 | extern struct nlattr * nla_reserve(struct sk_buff *skb, int attrtype, |
207 | int attrlen); | 239 | int attrlen); |
240 | extern void * nla_reserve_nohdr(struct sk_buff *skb, int attrlen); | ||
208 | extern void __nla_put(struct sk_buff *skb, int attrtype, | 241 | extern void __nla_put(struct sk_buff *skb, int attrtype, |
209 | int attrlen, const void *data); | 242 | int attrlen, const void *data); |
243 | extern void __nla_put_nohdr(struct sk_buff *skb, int attrlen, | ||
244 | const void *data); | ||
210 | extern int nla_put(struct sk_buff *skb, int attrtype, | 245 | extern int nla_put(struct sk_buff *skb, int attrtype, |
211 | int attrlen, const void *data); | 246 | int attrlen, const void *data); |
247 | extern int nla_put_nohdr(struct sk_buff *skb, int attrlen, | ||
248 | const void *data); | ||
212 | 249 | ||
213 | /************************************************************************** | 250 | /************************************************************************** |
214 | * Netlink Messages | 251 | * Netlink Messages |
@@ -364,6 +401,17 @@ static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype, | |||
364 | } | 401 | } |
365 | 402 | ||
366 | /** | 403 | /** |
404 | * nlmsg_report - need to report back to application? | ||
405 | * @nlh: netlink message header | ||
406 | * | ||
407 | * Returns 1 if a report back to the application is requested. | ||
408 | */ | ||
409 | static inline int nlmsg_report(struct nlmsghdr *nlh) | ||
410 | { | ||
411 | return !!(nlh->nlmsg_flags & NLM_F_ECHO); | ||
412 | } | ||
413 | |||
414 | /** | ||
367 | * nlmsg_for_each_attr - iterate over a stream of attributes | 415 | * nlmsg_for_each_attr - iterate over a stream of attributes |
368 | * @pos: loop counter, set to current attribute | 416 | * @pos: loop counter, set to current attribute |
369 | * @nlh: netlink message header | 417 | * @nlh: netlink message header |
@@ -453,12 +501,13 @@ static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb, | |||
453 | /** | 501 | /** |
454 | * nlmsg_new - Allocate a new netlink message | 502 | * nlmsg_new - Allocate a new netlink message |
455 | * @size: maximum size of message | 503 | * @size: maximum size of message |
504 | * @flags: the type of memory to allocate. | ||
456 | * | 505 | * |
457 | * Use NLMSG_GOODSIZE if size isn't know and you need a good default size. | 506 | * Use NLMSG_GOODSIZE if size isn't know and you need a good default size. |
458 | */ | 507 | */ |
459 | static inline struct sk_buff *nlmsg_new(int size) | 508 | static inline struct sk_buff *nlmsg_new(int size, gfp_t flags) |
460 | { | 509 | { |
461 | return alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | 510 | return alloc_skb(size, flags); |
462 | } | 511 | } |
463 | 512 | ||
464 | /** | 513 | /** |
@@ -480,6 +529,32 @@ static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh) | |||
480 | } | 529 | } |
481 | 530 | ||
482 | /** | 531 | /** |
532 | * nlmsg_get_pos - return current position in netlink message | ||
533 | * @skb: socket buffer the message is stored in | ||
534 | * | ||
535 | * Returns a pointer to the current tail of the message. | ||
536 | */ | ||
537 | static inline void *nlmsg_get_pos(struct sk_buff *skb) | ||
538 | { | ||
539 | return skb->tail; | ||
540 | } | ||
541 | |||
542 | /** | ||
543 | * nlmsg_trim - Trim message to a mark | ||
544 | * @skb: socket buffer the message is stored in | ||
545 | * @mark: mark to trim to | ||
546 | * | ||
547 | * Trims the message to the provided mark. Returns -1. | ||
548 | */ | ||
549 | static inline int nlmsg_trim(struct sk_buff *skb, void *mark) | ||
550 | { | ||
551 | if (mark) | ||
552 | skb_trim(skb, (unsigned char *) mark - skb->data); | ||
553 | |||
554 | return -1; | ||
555 | } | ||
556 | |||
557 | /** | ||
483 | * nlmsg_cancel - Cancel construction of a netlink message | 558 | * nlmsg_cancel - Cancel construction of a netlink message |
484 | * @skb: socket buffer the message is stored in | 559 | * @skb: socket buffer the message is stored in |
485 | * @nlh: netlink message header | 560 | * @nlh: netlink message header |
@@ -489,9 +564,7 @@ static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh) | |||
489 | */ | 564 | */ |
490 | static inline int nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh) | 565 | static inline int nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh) |
491 | { | 566 | { |
492 | skb_trim(skb, (unsigned char *) nlh - skb->data); | 567 | return nlmsg_trim(skb, nlh); |
493 | |||
494 | return -1; | ||
495 | } | 568 | } |
496 | 569 | ||
497 | /** | 570 | /** |
@@ -509,15 +582,16 @@ static inline void nlmsg_free(struct sk_buff *skb) | |||
509 | * @skb: netlink message as socket buffer | 582 | * @skb: netlink message as socket buffer |
510 | * @pid: own netlink pid to avoid sending to yourself | 583 | * @pid: own netlink pid to avoid sending to yourself |
511 | * @group: multicast group id | 584 | * @group: multicast group id |
585 | * @flags: allocation flags | ||
512 | */ | 586 | */ |
513 | static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, | 587 | static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, |
514 | u32 pid, unsigned int group) | 588 | u32 pid, unsigned int group, gfp_t flags) |
515 | { | 589 | { |
516 | int err; | 590 | int err; |
517 | 591 | ||
518 | NETLINK_CB(skb).dst_group = group; | 592 | NETLINK_CB(skb).dst_group = group; |
519 | 593 | ||
520 | err = netlink_broadcast(sk, skb, pid, group, GFP_KERNEL); | 594 | err = netlink_broadcast(sk, skb, pid, group, flags); |
521 | if (err > 0) | 595 | if (err > 0) |
522 | err = 0; | 596 | err = 0; |
523 | 597 | ||
@@ -631,6 +705,18 @@ static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining) | |||
631 | } | 705 | } |
632 | 706 | ||
633 | /** | 707 | /** |
708 | * nla_find_nested - find attribute in a set of nested attributes | ||
709 | * @nla: attribute containing the nested attributes | ||
710 | * @attrtype: type of attribute to look for | ||
711 | * | ||
712 | * Returns the first attribute which matches the specified type. | ||
713 | */ | ||
714 | static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype) | ||
715 | { | ||
716 | return nla_find(nla_data(nla), nla_len(nla), attrtype); | ||
717 | } | ||
718 | |||
719 | /** | ||
634 | * nla_parse_nested - parse nested attributes | 720 | * nla_parse_nested - parse nested attributes |
635 | * @tb: destination array with maxtype+1 elements | 721 | * @tb: destination array with maxtype+1 elements |
636 | * @maxtype: maximum attribute type to be expected | 722 | * @maxtype: maximum attribute type to be expected |
@@ -745,13 +831,16 @@ static inline int nla_put_msecs(struct sk_buff *skb, int attrtype, | |||
745 | #define NLA_PUT_U32(skb, attrtype, value) \ | 831 | #define NLA_PUT_U32(skb, attrtype, value) \ |
746 | NLA_PUT_TYPE(skb, u32, attrtype, value) | 832 | NLA_PUT_TYPE(skb, u32, attrtype, value) |
747 | 833 | ||
834 | #define NLA_PUT_BE32(skb, attrtype, value) \ | ||
835 | NLA_PUT_TYPE(skb, __be32, attrtype, value) | ||
836 | |||
748 | #define NLA_PUT_U64(skb, attrtype, value) \ | 837 | #define NLA_PUT_U64(skb, attrtype, value) \ |
749 | NLA_PUT_TYPE(skb, u64, attrtype, value) | 838 | NLA_PUT_TYPE(skb, u64, attrtype, value) |
750 | 839 | ||
751 | #define NLA_PUT_STRING(skb, attrtype, value) \ | 840 | #define NLA_PUT_STRING(skb, attrtype, value) \ |
752 | NLA_PUT(skb, attrtype, strlen(value) + 1, value) | 841 | NLA_PUT(skb, attrtype, strlen(value) + 1, value) |
753 | 842 | ||
754 | #define NLA_PUT_FLAG(skb, attrtype, value) \ | 843 | #define NLA_PUT_FLAG(skb, attrtype) \ |
755 | NLA_PUT(skb, attrtype, 0, NULL) | 844 | NLA_PUT(skb, attrtype, 0, NULL) |
756 | 845 | ||
757 | #define NLA_PUT_MSECS(skb, attrtype, jiffies) \ | 846 | #define NLA_PUT_MSECS(skb, attrtype, jiffies) \ |
@@ -767,6 +856,15 @@ static inline u32 nla_get_u32(struct nlattr *nla) | |||
767 | } | 856 | } |
768 | 857 | ||
769 | /** | 858 | /** |
859 | * nla_get_be32 - return payload of __be32 attribute | ||
860 | * @nla: __be32 netlink attribute | ||
861 | */ | ||
862 | static inline __be32 nla_get_be32(struct nlattr *nla) | ||
863 | { | ||
864 | return *(__be32 *) nla_data(nla); | ||
865 | } | ||
866 | |||
867 | /** | ||
770 | * nla_get_u16 - return payload of u16 attribute | 868 | * nla_get_u16 - return payload of u16 attribute |
771 | * @nla: u16 netlink attribute | 869 | * @nla: u16 netlink attribute |
772 | */ | 870 | */ |
@@ -862,10 +960,25 @@ static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start) | |||
862 | */ | 960 | */ |
863 | static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) | 961 | static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) |
864 | { | 962 | { |
865 | if (start) | 963 | return nlmsg_trim(skb, start); |
866 | skb_trim(skb, (unsigned char *) start - skb->data); | 964 | } |
867 | 965 | ||
868 | return -1; | 966 | /** |
967 | * nla_validate_nested - Validate a stream of nested attributes | ||
968 | * @start: container attribute | ||
969 | * @maxtype: maximum attribute type to be expected | ||
970 | * @policy: validation policy | ||
971 | * | ||
972 | * Validates all attributes in the nested attribute stream against the | ||
973 | * specified policy. Attributes with a type exceeding maxtype will be | ||
974 | * ignored. See documenation of struct nla_policy for more details. | ||
975 | * | ||
976 | * Returns 0 on success or a negative error code. | ||
977 | */ | ||
978 | static inline int nla_validate_nested(struct nlattr *start, int maxtype, | ||
979 | struct nla_policy *policy) | ||
980 | { | ||
981 | return nla_validate(nla_data(start), nla_len(start), maxtype, policy); | ||
869 | } | 982 | } |
870 | 983 | ||
871 | /** | 984 | /** |
@@ -880,4 +993,13 @@ static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) | |||
880 | nla_ok(pos, rem); \ | 993 | nla_ok(pos, rem); \ |
881 | pos = nla_next(pos, &(rem))) | 994 | pos = nla_next(pos, &(rem))) |
882 | 995 | ||
996 | /** | ||
997 | * nla_for_each_nested - iterate over nested attributes | ||
998 | * @pos: loop counter, set to current attribute | ||
999 | * @nla: attribute containing the nested attributes | ||
1000 | * @rem: initialized to len, holds bytes currently remaining in stream | ||
1001 | */ | ||
1002 | #define nla_for_each_nested(pos, nla, rem) \ | ||
1003 | nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem) | ||
1004 | |||
883 | #endif | 1005 | #endif |