diff options
| -rw-r--r-- | include/linux/netlink.h | 24 | ||||
| -rw-r--r-- | include/net/netlink.h | 877 | ||||
| -rw-r--r-- | net/netlink/Makefile | 2 | ||||
| -rw-r--r-- | net/netlink/attr.c | 328 |
4 files changed, 1229 insertions, 2 deletions
diff --git a/include/linux/netlink.h b/include/linux/netlink.h index ba25ca874c20..6a2ccf78a356 100644 --- a/include/linux/netlink.h +++ b/include/linux/netlink.h | |||
| @@ -71,7 +71,8 @@ struct nlmsghdr | |||
| 71 | 71 | ||
| 72 | #define NLMSG_ALIGNTO 4 | 72 | #define NLMSG_ALIGNTO 4 |
| 73 | #define NLMSG_ALIGN(len) ( ((len)+NLMSG_ALIGNTO-1) & ~(NLMSG_ALIGNTO-1) ) | 73 | #define NLMSG_ALIGN(len) ( ((len)+NLMSG_ALIGNTO-1) & ~(NLMSG_ALIGNTO-1) ) |
| 74 | #define NLMSG_LENGTH(len) ((len)+NLMSG_ALIGN(sizeof(struct nlmsghdr))) | 74 | #define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr))) |
| 75 | #define NLMSG_LENGTH(len) ((len)+NLMSG_ALIGN(NLMSG_HDRLEN)) | ||
| 75 | #define NLMSG_SPACE(len) NLMSG_ALIGN(NLMSG_LENGTH(len)) | 76 | #define NLMSG_SPACE(len) NLMSG_ALIGN(NLMSG_LENGTH(len)) |
| 76 | #define NLMSG_DATA(nlh) ((void*)(((char*)nlh) + NLMSG_LENGTH(0))) | 77 | #define NLMSG_DATA(nlh) ((void*)(((char*)nlh) + NLMSG_LENGTH(0))) |
| 77 | #define NLMSG_NEXT(nlh,len) ((len) -= NLMSG_ALIGN((nlh)->nlmsg_len), \ | 78 | #define NLMSG_NEXT(nlh,len) ((len) -= NLMSG_ALIGN((nlh)->nlmsg_len), \ |
| @@ -86,6 +87,8 @@ struct nlmsghdr | |||
| 86 | #define NLMSG_DONE 0x3 /* End of a dump */ | 87 | #define NLMSG_DONE 0x3 /* End of a dump */ |
| 87 | #define NLMSG_OVERRUN 0x4 /* Data lost */ | 88 | #define NLMSG_OVERRUN 0x4 /* Data lost */ |
| 88 | 89 | ||
| 90 | #define NLMSG_MIN_TYPE 0x10 /* < 0x10: reserved control messages */ | ||
| 91 | |||
| 89 | struct nlmsgerr | 92 | struct nlmsgerr |
| 90 | { | 93 | { |
| 91 | int error; | 94 | int error; |
| @@ -108,6 +111,25 @@ enum { | |||
| 108 | NETLINK_CONNECTED, | 111 | NETLINK_CONNECTED, |
| 109 | }; | 112 | }; |
| 110 | 113 | ||
| 114 | /* | ||
| 115 | * <------- NLA_HDRLEN ------> <-- NLA_ALIGN(payload)--> | ||
| 116 | * +---------------------+- - -+- - - - - - - - - -+- - -+ | ||
| 117 | * | Header | Pad | Payload | Pad | | ||
| 118 | * | (struct nlattr) | ing | | ing | | ||
| 119 | * +---------------------+- - -+- - - - - - - - - -+- - -+ | ||
| 120 | * <-------------- nlattr->nla_len --------------> | ||
| 121 | */ | ||
| 122 | |||
| 123 | struct nlattr | ||
| 124 | { | ||
| 125 | __u16 nla_len; | ||
| 126 | __u16 nla_type; | ||
| 127 | }; | ||
| 128 | |||
| 129 | #define NLA_ALIGNTO 4 | ||
| 130 | #define NLA_ALIGN(len) (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1)) | ||
| 131 | #define NLA_HDRLEN ((int) NLA_ALIGN(sizeof(struct nlattr))) | ||
| 132 | |||
| 111 | #ifdef __KERNEL__ | 133 | #ifdef __KERNEL__ |
| 112 | 134 | ||
| 113 | #include <linux/capability.h> | 135 | #include <linux/capability.h> |
diff --git a/include/net/netlink.h b/include/net/netlink.h new file mode 100644 index 000000000000..c99e22db9632 --- /dev/null +++ b/include/net/netlink.h | |||
| @@ -0,0 +1,877 @@ | |||
| 1 | #ifndef __NET_NETLINK_H | ||
| 2 | #define __NET_NETLINK_H | ||
| 3 | |||
| 4 | #include <linux/types.h> | ||
| 5 | #include <linux/netlink.h> | ||
| 6 | |||
| 7 | /* ======================================================================== | ||
| 8 | * Netlink Messages and Attributes Interface (As Seen On TV) | ||
| 9 | * ------------------------------------------------------------------------ | ||
| 10 | * Messages Interface | ||
| 11 | * ------------------------------------------------------------------------ | ||
| 12 | * | ||
| 13 | * Message Format: | ||
| 14 | * <--- nlmsg_total_size(payload) ---> | ||
| 15 | * <-- nlmsg_msg_size(payload) -> | ||
| 16 | * +----------+- - -+-------------+- - -+-------- - - | ||
| 17 | * | nlmsghdr | Pad | Payload | Pad | nlmsghdr | ||
| 18 | * +----------+- - -+-------------+- - -+-------- - - | ||
| 19 | * nlmsg_data(nlh)---^ ^ | ||
| 20 | * nlmsg_next(nlh)-----------------------+ | ||
| 21 | * | ||
| 22 | * Payload Format: | ||
| 23 | * <---------------------- nlmsg_len(nlh) ---------------------> | ||
| 24 | * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) -> | ||
| 25 | * +----------------------+- - -+--------------------------------+ | ||
| 26 | * | Family Header | Pad | Attributes | | ||
| 27 | * +----------------------+- - -+--------------------------------+ | ||
| 28 | * nlmsg_attrdata(nlh, hdrlen)---^ | ||
| 29 | * | ||
| 30 | * Data Structures: | ||
| 31 | * struct nlmsghdr netlink message header | ||
| 32 | * | ||
| 33 | * Message Construction: | ||
| 34 | * nlmsg_new() create a new netlink message | ||
| 35 | * nlmsg_put() add a netlink message to an skb | ||
| 36 | * nlmsg_put_answer() callback based nlmsg_put() | ||
| 37 | * nlmsg_end() finanlize netlink message | ||
| 38 | * nlmsg_cancel() cancel message construction | ||
| 39 | * nlmsg_free() free a netlink message | ||
| 40 | * | ||
| 41 | * Message Sending: | ||
| 42 | * nlmsg_multicast() multicast message to several groups | ||
| 43 | * nlmsg_unicast() unicast a message to a single socket | ||
| 44 | * | ||
| 45 | * Message Length Calculations: | ||
| 46 | * nlmsg_msg_size(payload) length of message w/o padding | ||
| 47 | * nlmsg_total_size(payload) length of message w/ padding | ||
| 48 | * nlmsg_padlen(payload) length of padding at tail | ||
| 49 | * | ||
| 50 | * Message Payload Access: | ||
| 51 | * nlmsg_data(nlh) head of message payload | ||
| 52 | * nlmsg_len(nlh) length of message payload | ||
| 53 | * nlmsg_attrdata(nlh, hdrlen) head of attributes data | ||
| 54 | * nlmsg_attrlen(nlh, hdrlen) length of attributes data | ||
| 55 | * | ||
| 56 | * Message Parsing: | ||
| 57 | * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes? | ||
| 58 | * nlmsg_next(nlh, remaining) get next netlink message | ||
| 59 | * nlmsg_parse() parse attributes of a message | ||
| 60 | * nlmsg_find_attr() find an attribute in a message | ||
| 61 | * nlmsg_for_each_msg() loop over all messages | ||
| 62 | * nlmsg_validate() validate netlink message incl. attrs | ||
| 63 | * nlmsg_for_each_attr() loop over all attributes | ||
| 64 | * | ||
| 65 | * ------------------------------------------------------------------------ | ||
| 66 | * Attributes Interface | ||
| 67 | * ------------------------------------------------------------------------ | ||
| 68 | * | ||
| 69 | * Attribute Format: | ||
| 70 | * <------- nla_total_size(payload) -------> | ||
| 71 | * <---- nla_attr_size(payload) -----> | ||
| 72 | * +----------+- - -+- - - - - - - - - +- - -+-------- - - | ||
| 73 | * | Header | Pad | Payload | Pad | Header | ||
| 74 | * +----------+- - -+- - - - - - - - - +- - -+-------- - - | ||
| 75 | * <- nla_len(nla) -> ^ | ||
| 76 | * nla_data(nla)----^ | | ||
| 77 | * nla_next(nla)-----------------------------' | ||
| 78 | * | ||
| 79 | * Data Structures: | ||
| 80 | * struct nlattr netlink attribtue header | ||
| 81 | * | ||
| 82 | * Attribute Construction: | ||
| 83 | * nla_reserve(skb, type, len) reserve skb tailroom for an attribute | ||
| 84 | * nla_put(skb, type, len, data) add attribute to skb | ||
| 85 | * | ||
| 86 | * Attribute Construction for Basic Types: | ||
| 87 | * nla_put_u8(skb, type, value) add u8 attribute to skb | ||
| 88 | * nla_put_u16(skb, type, value) add u16 attribute to skb | ||
| 89 | * nla_put_u32(skb, type, value) add u32 attribute to skb | ||
| 90 | * nla_put_u64(skb, type, value) add u64 attribute to skb | ||
| 91 | * nla_put_string(skb, type, str) add string attribute to skb | ||
| 92 | * nla_put_flag(skb, type) add flag attribute to skb | ||
| 93 | * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb | ||
| 94 | * | ||
| 95 | * Exceptions Based Attribute Construction: | ||
| 96 | * NLA_PUT(skb, type, len, data) add attribute to skb | ||
| 97 | * NLA_PUT_U8(skb, type, value) add u8 attribute to skb | ||
| 98 | * NLA_PUT_U16(skb, type, value) add u16 attribute to skb | ||
| 99 | * NLA_PUT_U32(skb, type, value) add u32 attribute to skb | ||
| 100 | * NLA_PUT_U64(skb, type, value) add u64 attribute to skb | ||
| 101 | * NLA_PUT_STRING(skb, type, str) add string attribute to skb | ||
| 102 | * NLA_PUT_FLAG(skb, type) add flag attribute to skb | ||
| 103 | * NLA_PUT_MSECS(skb, type, jiffies) add msecs attribute to skb | ||
| 104 | * | ||
| 105 | * The meaning of these functions is equal to their lower case | ||
| 106 | * variants but they jump to the label nla_put_failure in case | ||
| 107 | * of a failure. | ||
| 108 | * | ||
| 109 | * Nested Attributes Construction: | ||
| 110 | * nla_nest_start(skb, type) start a nested attribute | ||
| 111 | * nla_nest_end(skb, nla) finalize a nested attribute | ||
| 112 | * nla_nest_cancel(skb, nla) cancel nested attribute construction | ||
| 113 | * | ||
| 114 | * Attribute Length Calculations: | ||
| 115 | * nla_attr_size(payload) length of attribute w/o padding | ||
| 116 | * nla_total_size(payload) length of attribute w/ padding | ||
| 117 | * nla_padlen(payload) length of padding | ||
| 118 | * | ||
| 119 | * Attribute Payload Access: | ||
| 120 | * nla_data(nla) head of attribute payload | ||
| 121 | * nla_len(nla) length of attribute payload | ||
| 122 | * | ||
| 123 | * Attribute Payload Access for Basic Types: | ||
| 124 | * nla_get_u8(nla) get payload for a u8 attribute | ||
| 125 | * nla_get_u16(nla) get payload for a u16 attribute | ||
| 126 | * nla_get_u32(nla) get payload for a u32 attribute | ||
| 127 | * nla_get_u64(nla) get payload for a u64 attribute | ||
| 128 | * nla_get_flag(nla) return 1 if flag is true | ||
| 129 | * nla_get_msecs(nla) get payload for a msecs attribute | ||
| 130 | * | ||
| 131 | * Attribute Misc: | ||
| 132 | * nla_memcpy(dest, nla, count) copy attribute into memory | ||
| 133 | * nla_memcmp(nla, data, size) compare attribute with memory area | ||
| 134 | * nla_strlcpy(dst, nla, size) copy attribute to a sized string | ||
| 135 | * nla_strcmp(nla, str) compare attribute with string | ||
| 136 | * | ||
| 137 | * Attribute Parsing: | ||
| 138 | * nla_ok(nla, remaining) does nla fit into remaining bytes? | ||
| 139 | * nla_next(nla, remaining) get next netlink attribute | ||
| 140 | * nla_validate() validate a stream of attributes | ||
| 141 | * nla_find() find attribute in stream of attributes | ||
| 142 | * nla_parse() parse and validate stream of attrs | ||
| 143 | * nla_parse_nested() parse nested attribuets | ||
| 144 | * nla_for_each_attr() loop over all attributes | ||
| 145 | *========================================================================= | ||
| 146 | */ | ||
| 147 | |||
| 148 | /** | ||
| 149 | * Standard attribute types to specify validation policy | ||
| 150 | */ | ||
| 151 | enum { | ||
| 152 | NLA_UNSPEC, | ||
| 153 | NLA_U8, | ||
| 154 | NLA_U16, | ||
| 155 | NLA_U32, | ||
| 156 | NLA_U64, | ||
| 157 | NLA_STRING, | ||
| 158 | NLA_FLAG, | ||
| 159 | NLA_MSECS, | ||
| 160 | NLA_NESTED, | ||
| 161 | __NLA_TYPE_MAX, | ||
| 162 | }; | ||
| 163 | |||
| 164 | #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) | ||
| 165 | |||
| 166 | /** | ||
| 167 | * struct nla_policy - attribute validation policy | ||
| 168 | * @type: Type of attribute or NLA_UNSPEC | ||
| 169 | * @minlen: Minimal length of payload required to be available | ||
| 170 | * | ||
| 171 | * 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. | ||
| 173 | * | ||
| 174 | * Example: | ||
| 175 | * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = { | ||
| 176 | * [ATTR_FOO] = { .type = NLA_U16 }, | ||
| 177 | * [ATTR_BAR] = { .type = NLA_STRING }, | ||
| 178 | * [ATTR_BAZ] = { .minlen = sizeof(struct mystruct) }, | ||
| 179 | * }; | ||
| 180 | */ | ||
| 181 | struct nla_policy { | ||
| 182 | u16 type; | ||
| 183 | u16 minlen; | ||
| 184 | }; | ||
| 185 | |||
| 186 | extern int nla_validate(struct nlattr *head, int len, int maxtype, | ||
| 187 | struct nla_policy *policy); | ||
| 188 | extern int nla_parse(struct nlattr *tb[], int maxtype, | ||
| 189 | struct nlattr *head, int len, | ||
| 190 | struct nla_policy *policy); | ||
| 191 | extern struct nlattr * nla_find(struct nlattr *head, int len, int attrtype); | ||
| 192 | extern size_t nla_strlcpy(char *dst, const struct nlattr *nla, | ||
| 193 | size_t dstsize); | ||
| 194 | extern int nla_memcpy(void *dest, struct nlattr *src, int count); | ||
| 195 | extern int nla_memcmp(const struct nlattr *nla, const void *data, | ||
| 196 | size_t size); | ||
| 197 | extern int nla_strcmp(const struct nlattr *nla, const char *str); | ||
| 198 | extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype, | ||
| 199 | int attrlen); | ||
| 200 | extern struct nlattr * nla_reserve(struct sk_buff *skb, int attrtype, | ||
| 201 | int attrlen); | ||
| 202 | extern void __nla_put(struct sk_buff *skb, int attrtype, | ||
| 203 | int attrlen, const void *data); | ||
| 204 | extern int nla_put(struct sk_buff *skb, int attrtype, | ||
| 205 | int attrlen, const void *data); | ||
| 206 | |||
| 207 | /************************************************************************** | ||
| 208 | * Netlink Messages | ||
| 209 | **************************************************************************/ | ||
| 210 | |||
| 211 | /** | ||
| 212 | * nlmsg_msg_size - length of netlink message not including padding | ||
| 213 | * @payload: length of message payload | ||
| 214 | */ | ||
| 215 | static inline int nlmsg_msg_size(int payload) | ||
| 216 | { | ||
| 217 | return NLMSG_HDRLEN + payload; | ||
| 218 | } | ||
| 219 | |||
| 220 | /** | ||
| 221 | * nlmsg_total_size - length of netlink message including padding | ||
| 222 | * @payload: length of message payload | ||
| 223 | */ | ||
| 224 | static inline int nlmsg_total_size(int payload) | ||
| 225 | { | ||
| 226 | return NLMSG_ALIGN(nlmsg_msg_size(payload)); | ||
| 227 | } | ||
| 228 | |||
| 229 | /** | ||
| 230 | * nlmsg_padlen - length of padding at the message's tail | ||
| 231 | * @payload: length of message payload | ||
| 232 | */ | ||
| 233 | static inline int nlmsg_padlen(int payload) | ||
| 234 | { | ||
| 235 | return nlmsg_total_size(payload) - nlmsg_msg_size(payload); | ||
| 236 | } | ||
| 237 | |||
| 238 | /** | ||
| 239 | * nlmsg_data - head of message payload | ||
| 240 | * @nlh: netlink messsage header | ||
| 241 | */ | ||
| 242 | static inline void *nlmsg_data(const struct nlmsghdr *nlh) | ||
| 243 | { | ||
| 244 | return (unsigned char *) nlh + NLMSG_HDRLEN; | ||
| 245 | } | ||
| 246 | |||
| 247 | /** | ||
| 248 | * nlmsg_len - length of message payload | ||
| 249 | * @nlh: netlink message header | ||
| 250 | */ | ||
| 251 | static inline int nlmsg_len(const struct nlmsghdr *nlh) | ||
| 252 | { | ||
| 253 | return nlh->nlmsg_len - NLMSG_HDRLEN; | ||
| 254 | } | ||
| 255 | |||
| 256 | /** | ||
| 257 | * nlmsg_attrdata - head of attributes data | ||
| 258 | * @nlh: netlink message header | ||
| 259 | * @hdrlen: length of family specific header | ||
| 260 | */ | ||
| 261 | static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, | ||
| 262 | int hdrlen) | ||
| 263 | { | ||
| 264 | unsigned char *data = nlmsg_data(nlh); | ||
| 265 | return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen)); | ||
| 266 | } | ||
| 267 | |||
| 268 | /** | ||
| 269 | * nlmsg_attrlen - length of attributes data | ||
| 270 | * @nlh: netlink message header | ||
| 271 | * @hdrlen: length of family specific header | ||
| 272 | */ | ||
| 273 | static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen) | ||
| 274 | { | ||
| 275 | return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen); | ||
| 276 | } | ||
| 277 | |||
| 278 | /** | ||
| 279 | * nlmsg_ok - check if the netlink message fits into the remaining bytes | ||
| 280 | * @nlh: netlink message header | ||
| 281 | * @remaining: number of bytes remaining in message stream | ||
| 282 | */ | ||
| 283 | static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining) | ||
| 284 | { | ||
| 285 | return (remaining >= sizeof(struct nlmsghdr) && | ||
| 286 | nlh->nlmsg_len >= sizeof(struct nlmsghdr) && | ||
| 287 | nlh->nlmsg_len <= remaining); | ||
| 288 | } | ||
| 289 | |||
| 290 | /** | ||
| 291 | * nlmsg_next - next netlink message in message stream | ||
| 292 | * @nlh: netlink message header | ||
| 293 | * @remaining: number of bytes remaining in message stream | ||
| 294 | * | ||
| 295 | * Returns the next netlink message in the message stream and | ||
| 296 | * decrements remaining by the size of the current message. | ||
| 297 | */ | ||
| 298 | static inline struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining) | ||
| 299 | { | ||
| 300 | int totlen = NLMSG_ALIGN(nlh->nlmsg_len); | ||
| 301 | |||
| 302 | *remaining -= totlen; | ||
| 303 | |||
| 304 | return (struct nlmsghdr *) ((unsigned char *) nlh + totlen); | ||
| 305 | } | ||
| 306 | |||
| 307 | /** | ||
| 308 | * nlmsg_parse - parse attributes of a netlink message | ||
| 309 | * @nlh: netlink message header | ||
| 310 | * @hdrlen: length of family specific header | ||
| 311 | * @tb: destination array with maxtype+1 elements | ||
| 312 | * @maxtype: maximum attribute type to be expected | ||
| 313 | * @policy: validation policy | ||
| 314 | * | ||
| 315 | * See nla_parse() | ||
| 316 | */ | ||
| 317 | static inline int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, | ||
| 318 | struct nlattr *tb[], int maxtype, | ||
| 319 | struct nla_policy *policy) | ||
| 320 | { | ||
| 321 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) | ||
| 322 | return -EINVAL; | ||
| 323 | |||
| 324 | return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), | ||
| 325 | nlmsg_attrlen(nlh, hdrlen), policy); | ||
| 326 | } | ||
| 327 | |||
| 328 | /** | ||
| 329 | * nlmsg_find_attr - find a specific attribute in a netlink message | ||
| 330 | * @nlh: netlink message header | ||
| 331 | * @hdrlen: length of familiy specific header | ||
| 332 | * @attrtype: type of attribute to look for | ||
| 333 | * | ||
| 334 | * Returns the first attribute which matches the specified type. | ||
| 335 | */ | ||
| 336 | static inline struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh, | ||
| 337 | int hdrlen, int attrtype) | ||
| 338 | { | ||
| 339 | return nla_find(nlmsg_attrdata(nlh, hdrlen), | ||
| 340 | nlmsg_attrlen(nlh, hdrlen), attrtype); | ||
| 341 | } | ||
| 342 | |||
| 343 | /** | ||
| 344 | * nlmsg_validate - validate a netlink message including attributes | ||
| 345 | * @nlh: netlinket message header | ||
| 346 | * @hdrlen: length of familiy specific header | ||
| 347 | * @maxtype: maximum attribute type to be expected | ||
| 348 | * @policy: validation policy | ||
| 349 | */ | ||
| 350 | static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype, | ||
| 351 | struct nla_policy *policy) | ||
| 352 | { | ||
| 353 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) | ||
| 354 | return -EINVAL; | ||
| 355 | |||
| 356 | return nla_validate(nlmsg_attrdata(nlh, hdrlen), | ||
| 357 | nlmsg_attrlen(nlh, hdrlen), maxtype, policy); | ||
| 358 | } | ||
| 359 | |||
| 360 | /** | ||
| 361 | * nlmsg_for_each_attr - iterate over a stream of attributes | ||
| 362 | * @pos: loop counter, set to current attribute | ||
| 363 | * @nlh: netlink message header | ||
| 364 | * @hdrlen: length of familiy specific header | ||
| 365 | * @rem: initialized to len, holds bytes currently remaining in stream | ||
| 366 | */ | ||
| 367 | #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \ | ||
| 368 | nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \ | ||
| 369 | nlmsg_attrlen(nlh, hdrlen), rem) | ||
| 370 | |||
| 371 | #if 0 | ||
| 372 | /* FIXME: Enable once all users have been converted */ | ||
| 373 | |||
| 374 | /** | ||
| 375 | * __nlmsg_put - Add a new netlink message to an skb | ||
| 376 | * @skb: socket buffer to store message in | ||
| 377 | * @pid: netlink process id | ||
| 378 | * @seq: sequence number of message | ||
| 379 | * @type: message type | ||
| 380 | * @payload: length of message payload | ||
| 381 | * @flags: message flags | ||
| 382 | * | ||
| 383 | * The caller is responsible to ensure that the skb provides enough | ||
| 384 | * tailroom for both the netlink header and payload. | ||
| 385 | */ | ||
| 386 | static inline struct nlmsghdr *__nlmsg_put(struct sk_buff *skb, u32 pid, | ||
| 387 | u32 seq, int type, int payload, | ||
| 388 | int flags) | ||
| 389 | { | ||
| 390 | struct nlmsghdr *nlh; | ||
| 391 | |||
| 392 | nlh = (struct nlmsghdr *) skb_put(skb, nlmsg_total_size(payload)); | ||
| 393 | nlh->nlmsg_type = type; | ||
| 394 | nlh->nlmsg_len = nlmsg_msg_size(payload); | ||
| 395 | nlh->nlmsg_flags = flags; | ||
| 396 | nlh->nlmsg_pid = pid; | ||
| 397 | nlh->nlmsg_seq = seq; | ||
| 398 | |||
| 399 | memset((unsigned char *) nlmsg_data(nlh) + payload, 0, | ||
| 400 | nlmsg_padlen(payload)); | ||
| 401 | |||
| 402 | return nlh; | ||
| 403 | } | ||
| 404 | #endif | ||
| 405 | |||
| 406 | /** | ||
| 407 | * nlmsg_put - Add a new netlink message to an skb | ||
| 408 | * @skb: socket buffer to store message in | ||
| 409 | * @pid: netlink process id | ||
| 410 | * @seq: sequence number of message | ||
| 411 | * @type: message type | ||
| 412 | * @payload: length of message payload | ||
| 413 | * @flags: message flags | ||
| 414 | * | ||
| 415 | * Returns NULL if the tailroom of the skb is insufficient to store | ||
| 416 | * the message header and payload. | ||
| 417 | */ | ||
| 418 | static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq, | ||
| 419 | int type, int payload, int flags) | ||
| 420 | { | ||
| 421 | if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload))) | ||
| 422 | return NULL; | ||
| 423 | |||
| 424 | return __nlmsg_put(skb, pid, seq, type, payload, flags); | ||
| 425 | } | ||
| 426 | |||
| 427 | /** | ||
| 428 | * nlmsg_put_answer - Add a new callback based netlink message to an skb | ||
| 429 | * @skb: socket buffer to store message in | ||
| 430 | * @cb: netlink callback | ||
| 431 | * @type: message type | ||
| 432 | * @payload: length of message payload | ||
| 433 | * @flags: message flags | ||
| 434 | * | ||
| 435 | * Returns NULL if the tailroom of the skb is insufficient to store | ||
| 436 | * the message header and payload. | ||
| 437 | */ | ||
| 438 | static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb, | ||
| 439 | struct netlink_callback *cb, | ||
| 440 | int type, int payload, | ||
| 441 | int flags) | ||
| 442 | { | ||
| 443 | return nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, | ||
| 444 | type, payload, flags); | ||
| 445 | } | ||
| 446 | |||
| 447 | /** | ||
| 448 | * nlmsg_new - Allocate a new netlink message | ||
| 449 | * @size: maximum size of message | ||
| 450 | * | ||
| 451 | * Use NLMSG_GOODSIZE if size isn't know and you need a good default size. | ||
| 452 | */ | ||
| 453 | static inline struct sk_buff *nlmsg_new(int size) | ||
| 454 | { | ||
| 455 | return alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | ||
| 456 | } | ||
| 457 | |||
| 458 | /** | ||
| 459 | * nlmsg_end - Finalize a netlink message | ||
| 460 | * @skb: socket buffer the message is stored in | ||
| 461 | * @nlh: netlink message header | ||
| 462 | * | ||
| 463 | * Corrects the netlink message header to include the appeneded | ||
| 464 | * attributes. Only necessary if attributes have been added to | ||
| 465 | * the message. | ||
| 466 | * | ||
| 467 | * Returns the total data length of the skb. | ||
| 468 | */ | ||
| 469 | static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh) | ||
| 470 | { | ||
| 471 | nlh->nlmsg_len = skb->tail - (unsigned char *) nlh; | ||
| 472 | |||
| 473 | return skb->len; | ||
| 474 | } | ||
| 475 | |||
| 476 | /** | ||
| 477 | * nlmsg_cancel - Cancel construction of a netlink message | ||
| 478 | * @skb: socket buffer the message is stored in | ||
| 479 | * @nlh: netlink message header | ||
| 480 | * | ||
| 481 | * Removes the complete netlink message including all | ||
| 482 | * attributes from the socket buffer again. Returns -1. | ||
| 483 | */ | ||
| 484 | static inline int nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh) | ||
| 485 | { | ||
| 486 | skb_trim(skb, (unsigned char *) nlh - skb->data); | ||
| 487 | |||
| 488 | return -1; | ||
| 489 | } | ||
| 490 | |||
| 491 | /** | ||
| 492 | * nlmsg_free - free a netlink message | ||
| 493 | * @skb: socket buffer of netlink message | ||
| 494 | */ | ||
| 495 | static inline void nlmsg_free(struct sk_buff *skb) | ||
| 496 | { | ||
| 497 | kfree_skb(skb); | ||
| 498 | } | ||
| 499 | |||
| 500 | /** | ||
| 501 | * nlmsg_multicast - multicast a netlink message | ||
| 502 | * @sk: netlink socket to spread messages to | ||
| 503 | * @skb: netlink message as socket buffer | ||
| 504 | * @pid: own netlink pid to avoid sending to yourself | ||
| 505 | * @group: multicast group id | ||
| 506 | */ | ||
| 507 | static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, | ||
| 508 | u32 pid, unsigned int group) | ||
| 509 | { | ||
| 510 | int err; | ||
| 511 | |||
| 512 | NETLINK_CB(skb).dst_group = group; | ||
| 513 | |||
| 514 | err = netlink_broadcast(sk, skb, pid, group, GFP_KERNEL); | ||
| 515 | if (err > 0) | ||
| 516 | err = 0; | ||
| 517 | |||
| 518 | return err; | ||
| 519 | } | ||
| 520 | |||
| 521 | /** | ||
| 522 | * nlmsg_unicast - unicast a netlink message | ||
| 523 | * @sk: netlink socket to spread message to | ||
| 524 | * @skb: netlink message as socket buffer | ||
| 525 | * @pid: netlink pid of the destination socket | ||
| 526 | */ | ||
| 527 | static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid) | ||
| 528 | { | ||
| 529 | int err; | ||
| 530 | |||
| 531 | err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT); | ||
| 532 | if (err > 0) | ||
| 533 | err = 0; | ||
| 534 | |||
| 535 | return err; | ||
| 536 | } | ||
| 537 | |||
| 538 | /** | ||
| 539 | * nlmsg_for_each_msg - iterate over a stream of messages | ||
| 540 | * @pos: loop counter, set to current message | ||
| 541 | * @head: head of message stream | ||
| 542 | * @len: length of message stream | ||
| 543 | * @rem: initialized to len, holds bytes currently remaining in stream | ||
| 544 | */ | ||
| 545 | #define nlmsg_for_each_msg(pos, head, len, rem) \ | ||
| 546 | for (pos = head, rem = len; \ | ||
| 547 | nlmsg_ok(pos, rem); \ | ||
| 548 | pos = nlmsg_next(pos, &(rem))) | ||
| 549 | |||
| 550 | /************************************************************************** | ||
| 551 | * Netlink Attributes | ||
| 552 | **************************************************************************/ | ||
| 553 | |||
| 554 | /** | ||
| 555 | * nla_attr_size - length of attribute not including padding | ||
| 556 | * @payload: length of payload | ||
| 557 | */ | ||
| 558 | static inline int nla_attr_size(int payload) | ||
| 559 | { | ||
| 560 | return NLA_HDRLEN + payload; | ||
| 561 | } | ||
| 562 | |||
| 563 | /** | ||
| 564 | * nla_total_size - total length of attribute including padding | ||
| 565 | * @payload: length of payload | ||
| 566 | */ | ||
| 567 | static inline int nla_total_size(int payload) | ||
| 568 | { | ||
| 569 | return NLA_ALIGN(nla_attr_size(payload)); | ||
| 570 | } | ||
| 571 | |||
| 572 | /** | ||
| 573 | * nla_padlen - length of padding at the tail of attribute | ||
| 574 | * @payload: length of payload | ||
| 575 | */ | ||
| 576 | static inline int nla_padlen(int payload) | ||
| 577 | { | ||
| 578 | return nla_total_size(payload) - nla_attr_size(payload); | ||
| 579 | } | ||
| 580 | |||
| 581 | /** | ||
| 582 | * nla_data - head of payload | ||
| 583 | * @nla: netlink attribute | ||
| 584 | */ | ||
| 585 | static inline void *nla_data(const struct nlattr *nla) | ||
| 586 | { | ||
| 587 | return (char *) nla + NLA_HDRLEN; | ||
| 588 | } | ||
| 589 | |||
| 590 | /** | ||
| 591 | * nla_len - length of payload | ||
| 592 | * @nla: netlink attribute | ||
| 593 | */ | ||
| 594 | static inline int nla_len(const struct nlattr *nla) | ||
| 595 | { | ||
| 596 | return nla->nla_len - NLA_HDRLEN; | ||
| 597 | } | ||
| 598 | |||
| 599 | /** | ||
| 600 | * nla_ok - check if the netlink attribute fits into the remaining bytes | ||
| 601 | * @nla: netlink attribute | ||
| 602 | * @remaining: number of bytes remaining in attribute stream | ||
| 603 | */ | ||
| 604 | static inline int nla_ok(const struct nlattr *nla, int remaining) | ||
| 605 | { | ||
| 606 | return remaining >= sizeof(*nla) && | ||
| 607 | nla->nla_len >= sizeof(*nla) && | ||
| 608 | nla->nla_len <= remaining; | ||
| 609 | } | ||
| 610 | |||
| 611 | /** | ||
| 612 | * nla_next - next netlink attribte in attribute stream | ||
| 613 | * @nla: netlink attribute | ||
| 614 | * @remaining: number of bytes remaining in attribute stream | ||
| 615 | * | ||
| 616 | * Returns the next netlink attribute in the attribute stream and | ||
| 617 | * decrements remaining by the size of the current attribute. | ||
| 618 | */ | ||
| 619 | static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining) | ||
| 620 | { | ||
| 621 | int totlen = NLA_ALIGN(nla->nla_len); | ||
| 622 | |||
| 623 | *remaining -= totlen; | ||
| 624 | return (struct nlattr *) ((char *) nla + totlen); | ||
| 625 | } | ||
| 626 | |||
| 627 | /** | ||
| 628 | * nla_parse_nested - parse nested attributes | ||
| 629 | * @tb: destination array with maxtype+1 elements | ||
| 630 | * @maxtype: maximum attribute type to be expected | ||
| 631 | * @nla: attribute containing the nested attributes | ||
| 632 | * @policy: validation policy | ||
| 633 | * | ||
| 634 | * See nla_parse() | ||
| 635 | */ | ||
| 636 | static inline int nla_parse_nested(struct nlattr *tb[], int maxtype, | ||
| 637 | struct nlattr *nla, | ||
| 638 | struct nla_policy *policy) | ||
| 639 | { | ||
| 640 | return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy); | ||
| 641 | } | ||
| 642 | /** | ||
| 643 | * nla_put_u8 - Add a u16 netlink attribute to a socket buffer | ||
| 644 | * @skb: socket buffer to add attribute to | ||
| 645 | * @attrtype: attribute type | ||
| 646 | * @value: numeric value | ||
| 647 | */ | ||
| 648 | static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value) | ||
| 649 | { | ||
| 650 | return nla_put(skb, attrtype, sizeof(u8), &value); | ||
| 651 | } | ||
| 652 | |||
| 653 | /** | ||
| 654 | * nla_put_u16 - Add a u16 netlink attribute to a socket buffer | ||
| 655 | * @skb: socket buffer to add attribute to | ||
| 656 | * @attrtype: attribute type | ||
| 657 | * @value: numeric value | ||
| 658 | */ | ||
| 659 | static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value) | ||
| 660 | { | ||
| 661 | return nla_put(skb, attrtype, sizeof(u16), &value); | ||
| 662 | } | ||
| 663 | |||
| 664 | /** | ||
| 665 | * nla_put_u32 - Add a u32 netlink attribute to a socket buffer | ||
| 666 | * @skb: socket buffer to add attribute to | ||
| 667 | * @attrtype: attribute type | ||
| 668 | * @value: numeric value | ||
| 669 | */ | ||
| 670 | static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value) | ||
| 671 | { | ||
| 672 | return nla_put(skb, attrtype, sizeof(u32), &value); | ||
| 673 | } | ||
| 674 | |||
| 675 | /** | ||
| 676 | * nla_put_64 - Add a u64 netlink attribute to a socket buffer | ||
| 677 | * @skb: socket buffer to add attribute to | ||
| 678 | * @attrtype: attribute type | ||
| 679 | * @value: numeric value | ||
| 680 | */ | ||
| 681 | static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value) | ||
| 682 | { | ||
| 683 | return nla_put(skb, attrtype, sizeof(u64), &value); | ||
| 684 | } | ||
| 685 | |||
| 686 | /** | ||
| 687 | * nla_put_string - Add a string netlink attribute to a socket buffer | ||
| 688 | * @skb: socket buffer to add attribute to | ||
| 689 | * @attrtype: attribute type | ||
| 690 | * @str: NUL terminated string | ||
| 691 | */ | ||
| 692 | static inline int nla_put_string(struct sk_buff *skb, int attrtype, | ||
| 693 | const char *str) | ||
| 694 | { | ||
| 695 | return nla_put(skb, attrtype, strlen(str) + 1, str); | ||
| 696 | } | ||
| 697 | |||
| 698 | /** | ||
| 699 | * nla_put_flag - Add a flag netlink attribute to a socket buffer | ||
| 700 | * @skb: socket buffer to add attribute to | ||
| 701 | * @attrtype: attribute type | ||
| 702 | */ | ||
| 703 | static inline int nla_put_flag(struct sk_buff *skb, int attrtype) | ||
| 704 | { | ||
| 705 | return nla_put(skb, attrtype, 0, NULL); | ||
| 706 | } | ||
| 707 | |||
| 708 | /** | ||
| 709 | * nla_put_msecs - Add a msecs netlink attribute to a socket buffer | ||
| 710 | * @skb: socket buffer to add attribute to | ||
| 711 | * @attrtype: attribute type | ||
| 712 | * @jiffies: number of msecs in jiffies | ||
| 713 | */ | ||
| 714 | static inline int nla_put_msecs(struct sk_buff *skb, int attrtype, | ||
| 715 | unsigned long jiffies) | ||
| 716 | { | ||
| 717 | u64 tmp = jiffies_to_msecs(jiffies); | ||
| 718 | return nla_put(skb, attrtype, sizeof(u64), &tmp); | ||
| 719 | } | ||
| 720 | |||
| 721 | #define NLA_PUT(skb, attrtype, attrlen, data) \ | ||
| 722 | do { \ | ||
| 723 | if (nla_put(skb, attrtype, attrlen, data) < 0) \ | ||
| 724 | goto nla_put_failure; \ | ||
| 725 | } while(0) | ||
| 726 | |||
| 727 | #define NLA_PUT_TYPE(skb, type, attrtype, value) \ | ||
| 728 | do { \ | ||
| 729 | type __tmp = value; \ | ||
| 730 | NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \ | ||
| 731 | } while(0) | ||
| 732 | |||
| 733 | #define NLA_PUT_U8(skb, attrtype, value) \ | ||
| 734 | NLA_PUT_TYPE(skb, u8, attrtype, value) | ||
| 735 | |||
| 736 | #define NLA_PUT_U16(skb, attrtype, value) \ | ||
| 737 | NLA_PUT_TYPE(skb, u16, attrtype, value) | ||
| 738 | |||
| 739 | #define NLA_PUT_U32(skb, attrtype, value) \ | ||
| 740 | NLA_PUT_TYPE(skb, u32, attrtype, value) | ||
| 741 | |||
| 742 | #define NLA_PUT_U64(skb, attrtype, value) \ | ||
| 743 | NLA_PUT_TYPE(skb, u64, attrtype, value) | ||
| 744 | |||
| 745 | #define NLA_PUT_STRING(skb, attrtype, value) \ | ||
| 746 | NLA_PUT(skb, attrtype, strlen(value) + 1, value) | ||
| 747 | |||
| 748 | #define NLA_PUT_FLAG(skb, attrtype, value) \ | ||
| 749 | NLA_PUT(skb, attrtype, 0, NULL) | ||
| 750 | |||
| 751 | #define NLA_PUT_MSECS(skb, attrtype, jiffies) \ | ||
| 752 | NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies)) | ||
| 753 | |||
| 754 | /** | ||
| 755 | * nla_get_u32 - return payload of u32 attribute | ||
| 756 | * @nla: u32 netlink attribute | ||
| 757 | */ | ||
| 758 | static inline u32 nla_get_u32(struct nlattr *nla) | ||
| 759 | { | ||
| 760 | return *(u32 *) nla_data(nla); | ||
| 761 | } | ||
| 762 | |||
| 763 | /** | ||
| 764 | * nla_get_u16 - return payload of u16 attribute | ||
| 765 | * @nla: u16 netlink attribute | ||
| 766 | */ | ||
| 767 | static inline u16 nla_get_u16(struct nlattr *nla) | ||
| 768 | { | ||
| 769 | return *(u16 *) nla_data(nla); | ||
| 770 | } | ||
| 771 | |||
| 772 | /** | ||
| 773 | * nla_get_u8 - return payload of u8 attribute | ||
| 774 | * @nla: u8 netlink attribute | ||
| 775 | */ | ||
| 776 | static inline u8 nla_get_u8(struct nlattr *nla) | ||
| 777 | { | ||
| 778 | return *(u8 *) nla_data(nla); | ||
| 779 | } | ||
| 780 | |||
| 781 | /** | ||
| 782 | * nla_get_u64 - return payload of u64 attribute | ||
| 783 | * @nla: u64 netlink attribute | ||
| 784 | */ | ||
| 785 | static inline u64 nla_get_u64(struct nlattr *nla) | ||
| 786 | { | ||
| 787 | u64 tmp; | ||
| 788 | |||
| 789 | nla_memcpy(&tmp, nla, sizeof(tmp)); | ||
| 790 | |||
| 791 | return tmp; | ||
| 792 | } | ||
| 793 | |||
| 794 | /** | ||
| 795 | * nla_get_flag - return payload of flag attribute | ||
| 796 | * @nla: flag netlink attribute | ||
| 797 | */ | ||
| 798 | static inline int nla_get_flag(struct nlattr *nla) | ||
| 799 | { | ||
| 800 | return !!nla; | ||
| 801 | } | ||
| 802 | |||
| 803 | /** | ||
| 804 | * nla_get_msecs - return payload of msecs attribute | ||
| 805 | * @nla: msecs netlink attribute | ||
| 806 | * | ||
| 807 | * Returns the number of milliseconds in jiffies. | ||
| 808 | */ | ||
| 809 | static inline unsigned long nla_get_msecs(struct nlattr *nla) | ||
| 810 | { | ||
| 811 | u64 msecs = nla_get_u64(nla); | ||
| 812 | |||
| 813 | return msecs_to_jiffies((unsigned long) msecs); | ||
| 814 | } | ||
| 815 | |||
| 816 | /** | ||
| 817 | * nla_nest_start - Start a new level of nested attributes | ||
| 818 | * @skb: socket buffer to add attributes to | ||
| 819 | * @attrtype: attribute type of container | ||
| 820 | * | ||
| 821 | * Returns the container attribute | ||
| 822 | */ | ||
| 823 | static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) | ||
| 824 | { | ||
| 825 | struct nlattr *start = (struct nlattr *) skb->tail; | ||
| 826 | |||
| 827 | if (nla_put(skb, attrtype, 0, NULL) < 0) | ||
| 828 | return NULL; | ||
| 829 | |||
| 830 | return start; | ||
| 831 | } | ||
| 832 | |||
| 833 | /** | ||
| 834 | * nla_nest_end - Finalize nesting of attributes | ||
| 835 | * @skb: socket buffer the attribtues are stored in | ||
| 836 | * @start: container attribute | ||
| 837 | * | ||
| 838 | * Corrects the container attribute header to include the all | ||
| 839 | * appeneded attributes. | ||
| 840 | * | ||
| 841 | * Returns the total data length of the skb. | ||
| 842 | */ | ||
| 843 | static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start) | ||
| 844 | { | ||
| 845 | start->nla_len = skb->tail - (unsigned char *) start; | ||
| 846 | return skb->len; | ||
| 847 | } | ||
| 848 | |||
| 849 | /** | ||
| 850 | * nla_nest_cancel - Cancel nesting of attributes | ||
| 851 | * @skb: socket buffer the message is stored in | ||
| 852 | * @start: container attribute | ||
| 853 | * | ||
| 854 | * Removes the container attribute and including all nested | ||
| 855 | * attributes. Returns -1. | ||
| 856 | */ | ||
| 857 | static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) | ||
| 858 | { | ||
| 859 | if (start) | ||
| 860 | skb_trim(skb, (unsigned char *) start - skb->data); | ||
| 861 | |||
| 862 | return -1; | ||
| 863 | } | ||
| 864 | |||
| 865 | /** | ||
| 866 | * nla_for_each_attr - iterate over a stream of attributes | ||
| 867 | * @pos: loop counter, set to current attribute | ||
| 868 | * @head: head of attribute stream | ||
| 869 | * @len: length of attribute stream | ||
| 870 | * @rem: initialized to len, holds bytes currently remaining in stream | ||
| 871 | */ | ||
| 872 | #define nla_for_each_attr(pos, head, len, rem) \ | ||
| 873 | for (pos = head, rem = len; \ | ||
| 874 | nla_ok(pos, rem); \ | ||
| 875 | pos = nla_next(pos, &(rem))) | ||
| 876 | |||
| 877 | #endif | ||
diff --git a/net/netlink/Makefile b/net/netlink/Makefile index 39d9c2dcd03c..68179015bd39 100644 --- a/net/netlink/Makefile +++ b/net/netlink/Makefile | |||
| @@ -2,4 +2,4 @@ | |||
| 2 | # Makefile for the netlink driver. | 2 | # Makefile for the netlink driver. |
| 3 | # | 3 | # |
| 4 | 4 | ||
| 5 | obj-y := af_netlink.o | 5 | obj-y := af_netlink.o attr.o |
diff --git a/net/netlink/attr.c b/net/netlink/attr.c new file mode 100644 index 000000000000..fffef4ab276f --- /dev/null +++ b/net/netlink/attr.c | |||
| @@ -0,0 +1,328 @@ | |||
| 1 | /* | ||
| 2 | * NETLINK Netlink attributes | ||
| 3 | * | ||
| 4 | * Authors: Thomas Graf <tgraf@suug.ch> | ||
| 5 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <linux/config.h> | ||
| 9 | #include <linux/module.h> | ||
| 10 | #include <linux/kernel.h> | ||
| 11 | #include <linux/errno.h> | ||
| 12 | #include <linux/jiffies.h> | ||
| 13 | #include <linux/netdevice.h> | ||
| 14 | #include <linux/skbuff.h> | ||
| 15 | #include <linux/string.h> | ||
| 16 | #include <linux/types.h> | ||
| 17 | #include <net/netlink.h> | ||
| 18 | |||
| 19 | static u16 nla_attr_minlen[NLA_TYPE_MAX+1] __read_mostly = { | ||
| 20 | [NLA_U8] = sizeof(u8), | ||
| 21 | [NLA_U16] = sizeof(u16), | ||
| 22 | [NLA_U32] = sizeof(u32), | ||
| 23 | [NLA_U64] = sizeof(u64), | ||
| 24 | [NLA_STRING] = 1, | ||
| 25 | [NLA_NESTED] = NLA_HDRLEN, | ||
| 26 | }; | ||
| 27 | |||
| 28 | static int validate_nla(struct nlattr *nla, int maxtype, | ||
| 29 | struct nla_policy *policy) | ||
| 30 | { | ||
| 31 | struct nla_policy *pt; | ||
| 32 | int minlen = 0; | ||
| 33 | |||
| 34 | if (nla->nla_type <= 0 || nla->nla_type > maxtype) | ||
| 35 | return 0; | ||
| 36 | |||
| 37 | pt = &policy[nla->nla_type]; | ||
| 38 | |||
| 39 | BUG_ON(pt->type > NLA_TYPE_MAX); | ||
| 40 | |||
| 41 | if (pt->minlen) | ||
| 42 | minlen = pt->minlen; | ||
| 43 | else if (pt->type != NLA_UNSPEC) | ||
| 44 | minlen = nla_attr_minlen[pt->type]; | ||
| 45 | |||
| 46 | if (pt->type == NLA_FLAG && nla_len(nla) > 0) | ||
| 47 | return -ERANGE; | ||
| 48 | |||
| 49 | if (nla_len(nla) < minlen) | ||
| 50 | return -ERANGE; | ||
| 51 | |||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | /** | ||
| 56 | * nla_validate - Validate a stream of attributes | ||
| 57 | * @head: head of attribute stream | ||
| 58 | * @len: length of attribute stream | ||
| 59 | * @maxtype: maximum attribute type to be expected | ||
| 60 | * @policy: validation policy | ||
| 61 | * | ||
| 62 | * Validates all attributes in the specified attribute stream against the | ||
| 63 | * specified policy. Attributes with a type exceeding maxtype will be | ||
| 64 | * ignored. See documenation of struct nla_policy for more details. | ||
| 65 | * | ||
| 66 | * Returns 0 on success or a negative error code. | ||
| 67 | */ | ||
| 68 | int nla_validate(struct nlattr *head, int len, int maxtype, | ||
| 69 | struct nla_policy *policy) | ||
| 70 | { | ||
| 71 | struct nlattr *nla; | ||
| 72 | int rem, err; | ||
| 73 | |||
| 74 | nla_for_each_attr(nla, head, len, rem) { | ||
| 75 | err = validate_nla(nla, maxtype, policy); | ||
| 76 | if (err < 0) | ||
| 77 | goto errout; | ||
| 78 | } | ||
| 79 | |||
| 80 | err = 0; | ||
| 81 | errout: | ||
| 82 | return err; | ||
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * nla_parse - Parse a stream of attributes into a tb buffer | ||
| 87 | * @tb: destination array with maxtype+1 elements | ||
| 88 | * @maxtype: maximum attribute type to be expected | ||
| 89 | * @head: head of attribute stream | ||
| 90 | * @len: length of attribute stream | ||
| 91 | * | ||
| 92 | * Parses a stream of attributes and stores a pointer to each attribute in | ||
| 93 | * the tb array accessable via the attribute type. Attributes with a type | ||
| 94 | * exceeding maxtype will be silently ignored for backwards compatibility | ||
| 95 | * reasons. policy may be set to NULL if no validation is required. | ||
| 96 | * | ||
| 97 | * Returns 0 on success or a negative error code. | ||
| 98 | */ | ||
| 99 | int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, | ||
| 100 | struct nla_policy *policy) | ||
| 101 | { | ||
| 102 | struct nlattr *nla; | ||
| 103 | int rem, err; | ||
| 104 | |||
| 105 | memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); | ||
| 106 | |||
| 107 | nla_for_each_attr(nla, head, len, rem) { | ||
| 108 | u16 type = nla->nla_type; | ||
| 109 | |||
| 110 | if (type > 0 && type <= maxtype) { | ||
| 111 | if (policy) { | ||
| 112 | err = validate_nla(nla, maxtype, policy); | ||
| 113 | if (err < 0) | ||
| 114 | goto errout; | ||
| 115 | } | ||
| 116 | |||
| 117 | tb[type] = nla; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | if (unlikely(rem > 0)) | ||
| 122 | printk(KERN_WARNING "netlink: %d bytes leftover after parsing " | ||
| 123 | "attributes.\n", rem); | ||
| 124 | |||
| 125 | err = 0; | ||
| 126 | errout: | ||
| 127 | return err; | ||
| 128 | } | ||
| 129 | |||
| 130 | /** | ||
| 131 | * nla_find - Find a specific attribute in a stream of attributes | ||
| 132 | * @head: head of attribute stream | ||
| 133 | * @len: length of attribute stream | ||
| 134 | * @attrtype: type of attribute to look for | ||
| 135 | * | ||
| 136 | * Returns the first attribute in the stream matching the specified type. | ||
| 137 | */ | ||
| 138 | struct nlattr *nla_find(struct nlattr *head, int len, int attrtype) | ||
| 139 | { | ||
| 140 | struct nlattr *nla; | ||
| 141 | int rem; | ||
| 142 | |||
| 143 | nla_for_each_attr(nla, head, len, rem) | ||
| 144 | if (nla->nla_type == attrtype) | ||
| 145 | return nla; | ||
| 146 | |||
| 147 | return NULL; | ||
| 148 | } | ||
| 149 | |||
| 150 | /** | ||
| 151 | * nla_strlcpy - Copy string attribute payload into a sized buffer | ||
| 152 | * @dst: where to copy the string to | ||
| 153 | * @src: attribute to copy the string from | ||
| 154 | * @dstsize: size of destination buffer | ||
| 155 | * | ||
| 156 | * Copies at most dstsize - 1 bytes into the destination buffer. | ||
| 157 | * The result is always a valid NUL-terminated string. Unlike | ||
| 158 | * strlcpy the destination buffer is always padded out. | ||
| 159 | * | ||
| 160 | * Returns the length of the source buffer. | ||
| 161 | */ | ||
| 162 | size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize) | ||
| 163 | { | ||
| 164 | size_t srclen = nla_len(nla); | ||
| 165 | char *src = nla_data(nla); | ||
| 166 | |||
| 167 | if (srclen > 0 && src[srclen - 1] == '\0') | ||
| 168 | srclen--; | ||
| 169 | |||
| 170 | if (dstsize > 0) { | ||
| 171 | size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen; | ||
| 172 | |||
| 173 | memset(dst, 0, dstsize); | ||
| 174 | memcpy(dst, src, len); | ||
| 175 | } | ||
| 176 | |||
| 177 | return srclen; | ||
| 178 | } | ||
| 179 | |||
| 180 | /** | ||
| 181 | * nla_memcpy - Copy a netlink attribute into another memory area | ||
| 182 | * @dest: where to copy to memcpy | ||
| 183 | * @src: netlink attribute to copy from | ||
| 184 | * @count: size of the destination area | ||
| 185 | * | ||
| 186 | * Note: The number of bytes copied is limited by the length of | ||
| 187 | * attribute's payload. memcpy | ||
| 188 | * | ||
| 189 | * Returns the number of bytes copied. | ||
| 190 | */ | ||
| 191 | int nla_memcpy(void *dest, struct nlattr *src, int count) | ||
| 192 | { | ||
| 193 | int minlen = min_t(int, count, nla_len(src)); | ||
| 194 | |||
| 195 | memcpy(dest, nla_data(src), minlen); | ||
| 196 | |||
| 197 | return minlen; | ||
| 198 | } | ||
| 199 | |||
| 200 | /** | ||
| 201 | * nla_memcmp - Compare an attribute with sized memory area | ||
| 202 | * @nla: netlink attribute | ||
| 203 | * @data: memory area | ||
| 204 | * @size: size of memory area | ||
| 205 | */ | ||
| 206 | int nla_memcmp(const struct nlattr *nla, const void *data, | ||
| 207 | size_t size) | ||
| 208 | { | ||
| 209 | int d = nla_len(nla) - size; | ||
| 210 | |||
| 211 | if (d == 0) | ||
| 212 | d = memcmp(nla_data(nla), data, size); | ||
| 213 | |||
| 214 | return d; | ||
| 215 | } | ||
| 216 | |||
| 217 | /** | ||
| 218 | * nla_strcmp - Compare a string attribute against a string | ||
| 219 | * @nla: netlink string attribute | ||
| 220 | * @str: another string | ||
| 221 | */ | ||
| 222 | int nla_strcmp(const struct nlattr *nla, const char *str) | ||
| 223 | { | ||
| 224 | int len = strlen(str) + 1; | ||
| 225 | int d = nla_len(nla) - len; | ||
| 226 | |||
| 227 | if (d == 0) | ||
| 228 | d = memcmp(nla_data(nla), str, len); | ||
| 229 | |||
| 230 | return d; | ||
| 231 | } | ||
| 232 | |||
| 233 | /** | ||
| 234 | * __nla_reserve - reserve room for attribute on the skb | ||
| 235 | * @skb: socket buffer to reserve room on | ||
| 236 | * @attrtype: attribute type | ||
| 237 | * @attrlen: length of attribute payload | ||
| 238 | * | ||
| 239 | * Adds a netlink attribute header to a socket buffer and reserves | ||
| 240 | * room for the payload but does not copy it. | ||
| 241 | * | ||
| 242 | * The caller is responsible to ensure that the skb provides enough | ||
| 243 | * tailroom for the attribute header and payload. | ||
| 244 | */ | ||
| 245 | struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) | ||
| 246 | { | ||
| 247 | struct nlattr *nla; | ||
| 248 | |||
| 249 | nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen)); | ||
| 250 | nla->nla_type = attrtype; | ||
| 251 | nla->nla_len = nla_attr_size(attrlen); | ||
| 252 | |||
| 253 | memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen)); | ||
| 254 | |||
| 255 | return nla; | ||
| 256 | } | ||
| 257 | |||
| 258 | /** | ||
| 259 | * nla_reserve - reserve room for attribute on the skb | ||
| 260 | * @skb: socket buffer to reserve room on | ||
| 261 | * @attrtype: attribute type | ||
| 262 | * @attrlen: length of attribute payload | ||
| 263 | * | ||
| 264 | * Adds a netlink attribute header to a socket buffer and reserves | ||
| 265 | * room for the payload but does not copy it. | ||
| 266 | * | ||
| 267 | * Returns NULL if the tailroom of the skb is insufficient to store | ||
| 268 | * the attribute header and payload. | ||
| 269 | */ | ||
| 270 | struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) | ||
| 271 | { | ||
| 272 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) | ||
| 273 | return NULL; | ||
| 274 | |||
| 275 | return __nla_reserve(skb, attrtype, attrlen); | ||
| 276 | } | ||
| 277 | |||
| 278 | /** | ||
| 279 | * __nla_put - Add a netlink attribute to a socket buffer | ||
| 280 | * @skb: socket buffer to add attribute to | ||
| 281 | * @attrtype: attribute type | ||
| 282 | * @attrlen: length of attribute payload | ||
| 283 | * @data: head of attribute payload | ||
| 284 | * | ||
| 285 | * The caller is responsible to ensure that the skb provides enough | ||
| 286 | * tailroom for the attribute header and payload. | ||
| 287 | */ | ||
| 288 | void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, | ||
| 289 | const void *data) | ||
| 290 | { | ||
| 291 | struct nlattr *nla; | ||
| 292 | |||
| 293 | nla = __nla_reserve(skb, attrtype, attrlen); | ||
| 294 | memcpy(nla_data(nla), data, attrlen); | ||
| 295 | } | ||
| 296 | |||
| 297 | |||
| 298 | /** | ||
| 299 | * nla_put - Add a netlink attribute to a socket buffer | ||
| 300 | * @skb: socket buffer to add attribute to | ||
| 301 | * @attrtype: attribute type | ||
| 302 | * @attrlen: length of attribute payload | ||
| 303 | * @data: head of attribute payload | ||
| 304 | * | ||
| 305 | * Returns -1 if the tailroom of the skb is insufficient to store | ||
| 306 | * the attribute header and payload. | ||
| 307 | */ | ||
| 308 | int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data) | ||
| 309 | { | ||
| 310 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) | ||
| 311 | return -1; | ||
| 312 | |||
| 313 | __nla_put(skb, attrtype, attrlen, data); | ||
| 314 | return 0; | ||
| 315 | } | ||
| 316 | |||
| 317 | |||
| 318 | EXPORT_SYMBOL(nla_validate); | ||
| 319 | EXPORT_SYMBOL(nla_parse); | ||
| 320 | EXPORT_SYMBOL(nla_find); | ||
| 321 | EXPORT_SYMBOL(nla_strlcpy); | ||
| 322 | EXPORT_SYMBOL(__nla_reserve); | ||
| 323 | EXPORT_SYMBOL(nla_reserve); | ||
| 324 | EXPORT_SYMBOL(__nla_put); | ||
| 325 | EXPORT_SYMBOL(nla_put); | ||
| 326 | EXPORT_SYMBOL(nla_memcpy); | ||
| 327 | EXPORT_SYMBOL(nla_memcmp); | ||
| 328 | EXPORT_SYMBOL(nla_strcmp); | ||
