.. | .. |
---|
172 | 172 | NLA_FLAG, |
---|
173 | 173 | NLA_MSECS, |
---|
174 | 174 | NLA_NESTED, |
---|
175 | | - NLA_NESTED_COMPAT, |
---|
| 175 | + NLA_NESTED_ARRAY, |
---|
176 | 176 | NLA_NUL_STRING, |
---|
177 | 177 | NLA_BINARY, |
---|
178 | 178 | NLA_S8, |
---|
.. | .. |
---|
180 | 180 | NLA_S32, |
---|
181 | 181 | NLA_S64, |
---|
182 | 182 | NLA_BITFIELD32, |
---|
| 183 | + NLA_REJECT, |
---|
183 | 184 | __NLA_TYPE_MAX, |
---|
184 | 185 | }; |
---|
185 | 186 | |
---|
186 | 187 | #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) |
---|
187 | 188 | |
---|
| 189 | +struct netlink_range_validation { |
---|
| 190 | + u64 min, max; |
---|
| 191 | +}; |
---|
| 192 | + |
---|
| 193 | +struct netlink_range_validation_signed { |
---|
| 194 | + s64 min, max; |
---|
| 195 | +}; |
---|
| 196 | + |
---|
| 197 | +enum nla_policy_validation { |
---|
| 198 | + NLA_VALIDATE_NONE, |
---|
| 199 | + NLA_VALIDATE_RANGE, |
---|
| 200 | + NLA_VALIDATE_RANGE_WARN_TOO_LONG, |
---|
| 201 | + NLA_VALIDATE_MIN, |
---|
| 202 | + NLA_VALIDATE_MAX, |
---|
| 203 | + NLA_VALIDATE_MASK, |
---|
| 204 | + NLA_VALIDATE_RANGE_PTR, |
---|
| 205 | + NLA_VALIDATE_FUNCTION, |
---|
| 206 | +}; |
---|
| 207 | + |
---|
188 | 208 | /** |
---|
189 | 209 | * struct nla_policy - attribute validation policy |
---|
190 | 210 | * @type: Type of attribute or NLA_UNSPEC |
---|
| 211 | + * @validation_type: type of attribute validation done in addition to |
---|
| 212 | + * type-specific validation (e.g. range, function call), see |
---|
| 213 | + * &enum nla_policy_validation |
---|
191 | 214 | * @len: Type specific length of payload |
---|
192 | 215 | * |
---|
193 | 216 | * Policies are defined as arrays of this struct, the array must be |
---|
.. | .. |
---|
198 | 221 | * NLA_NUL_STRING Maximum length of string (excluding NUL) |
---|
199 | 222 | * NLA_FLAG Unused |
---|
200 | 223 | * NLA_BINARY Maximum length of attribute payload |
---|
201 | | - * NLA_NESTED Don't use `len' field -- length verification is |
---|
202 | | - * done by checking len of nested header (or empty) |
---|
203 | | - * NLA_NESTED_COMPAT Minimum length of structure payload |
---|
| 224 | + * (but see also below with the validation type) |
---|
| 225 | + * NLA_NESTED, |
---|
| 226 | + * NLA_NESTED_ARRAY Length verification is done by checking len of |
---|
| 227 | + * nested header (or empty); len field is used if |
---|
| 228 | + * nested_policy is also used, for the max attr |
---|
| 229 | + * number in the nested policy. |
---|
204 | 230 | * NLA_U8, NLA_U16, |
---|
205 | 231 | * NLA_U32, NLA_U64, |
---|
206 | 232 | * NLA_S8, NLA_S16, |
---|
.. | .. |
---|
208 | 234 | * NLA_MSECS Leaving the length field zero will verify the |
---|
209 | 235 | * given type fits, using it verifies minimum length |
---|
210 | 236 | * just like "All other" |
---|
211 | | - * NLA_BITFIELD32 A 32-bit bitmap/bitselector attribute |
---|
| 237 | + * NLA_BITFIELD32 Unused |
---|
| 238 | + * NLA_REJECT Unused |
---|
212 | 239 | * All other Minimum length of attribute payload |
---|
213 | 240 | * |
---|
| 241 | + * Meaning of validation union: |
---|
| 242 | + * NLA_BITFIELD32 This is a 32-bit bitmap/bitselector attribute and |
---|
| 243 | + * `bitfield32_valid' is the u32 value of valid flags |
---|
| 244 | + * NLA_REJECT This attribute is always rejected and `reject_message' |
---|
| 245 | + * may point to a string to report as the error instead |
---|
| 246 | + * of the generic one in extended ACK. |
---|
| 247 | + * NLA_NESTED `nested_policy' to a nested policy to validate, must |
---|
| 248 | + * also set `len' to the max attribute number. Use the |
---|
| 249 | + * provided NLA_POLICY_NESTED() macro. |
---|
| 250 | + * Note that nla_parse() will validate, but of course not |
---|
| 251 | + * parse, the nested sub-policies. |
---|
| 252 | + * NLA_NESTED_ARRAY `nested_policy' points to a nested policy to validate, |
---|
| 253 | + * must also set `len' to the max attribute number. Use |
---|
| 254 | + * the provided NLA_POLICY_NESTED_ARRAY() macro. |
---|
| 255 | + * The difference to NLA_NESTED is the structure: |
---|
| 256 | + * NLA_NESTED has the nested attributes directly inside |
---|
| 257 | + * while an array has the nested attributes at another |
---|
| 258 | + * level down and the attribute types directly in the |
---|
| 259 | + * nesting don't matter. |
---|
| 260 | + * NLA_U8, |
---|
| 261 | + * NLA_U16, |
---|
| 262 | + * NLA_U32, |
---|
| 263 | + * NLA_U64, |
---|
| 264 | + * NLA_S8, |
---|
| 265 | + * NLA_S16, |
---|
| 266 | + * NLA_S32, |
---|
| 267 | + * NLA_S64 The `min' and `max' fields are used depending on the |
---|
| 268 | + * validation_type field, if that is min/max/range then |
---|
| 269 | + * the min, max or both are used (respectively) to check |
---|
| 270 | + * the value of the integer attribute. |
---|
| 271 | + * Note that in the interest of code simplicity and |
---|
| 272 | + * struct size both limits are s16, so you cannot |
---|
| 273 | + * enforce a range that doesn't fall within the range |
---|
| 274 | + * of s16 - do that as usual in the code instead. |
---|
| 275 | + * Use the NLA_POLICY_MIN(), NLA_POLICY_MAX() and |
---|
| 276 | + * NLA_POLICY_RANGE() macros. |
---|
| 277 | + * NLA_U8, |
---|
| 278 | + * NLA_U16, |
---|
| 279 | + * NLA_U32, |
---|
| 280 | + * NLA_U64 If the validation_type field instead is set to |
---|
| 281 | + * NLA_VALIDATE_RANGE_PTR, `range' must be a pointer |
---|
| 282 | + * to a struct netlink_range_validation that indicates |
---|
| 283 | + * the min/max values. |
---|
| 284 | + * Use NLA_POLICY_FULL_RANGE(). |
---|
| 285 | + * NLA_S8, |
---|
| 286 | + * NLA_S16, |
---|
| 287 | + * NLA_S32, |
---|
| 288 | + * NLA_S64 If the validation_type field instead is set to |
---|
| 289 | + * NLA_VALIDATE_RANGE_PTR, `range_signed' must be a |
---|
| 290 | + * pointer to a struct netlink_range_validation_signed |
---|
| 291 | + * that indicates the min/max values. |
---|
| 292 | + * Use NLA_POLICY_FULL_RANGE_SIGNED(). |
---|
| 293 | + * |
---|
| 294 | + * NLA_BINARY If the validation type is like the ones for integers |
---|
| 295 | + * above, then the min/max length (not value like for |
---|
| 296 | + * integers) of the attribute is enforced. |
---|
| 297 | + * |
---|
| 298 | + * All other Unused - but note that it's a union |
---|
| 299 | + * |
---|
| 300 | + * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN: |
---|
| 301 | + * NLA_BINARY Validation function called for the attribute. |
---|
| 302 | + * All other Unused - but note that it's a union |
---|
| 303 | + * |
---|
214 | 304 | * Example: |
---|
| 305 | + * |
---|
| 306 | + * static const u32 myvalidflags = 0xff231023; |
---|
| 307 | + * |
---|
215 | 308 | * static const struct nla_policy my_policy[ATTR_MAX+1] = { |
---|
216 | 309 | * [ATTR_FOO] = { .type = NLA_U16 }, |
---|
217 | 310 | * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ }, |
---|
218 | | - * [ATTR_BAZ] = { .len = sizeof(struct mystruct) }, |
---|
219 | | - * [ATTR_GOO] = { .type = NLA_BITFIELD32, .validation_data = &myvalidflags }, |
---|
| 311 | + * [ATTR_BAZ] = NLA_POLICY_EXACT_LEN(sizeof(struct mystruct)), |
---|
| 312 | + * [ATTR_GOO] = NLA_POLICY_BITFIELD32(myvalidflags), |
---|
220 | 313 | * }; |
---|
221 | 314 | */ |
---|
222 | 315 | struct nla_policy { |
---|
223 | | - u16 type; |
---|
| 316 | + u8 type; |
---|
| 317 | + u8 validation_type; |
---|
224 | 318 | u16 len; |
---|
225 | | - void *validation_data; |
---|
| 319 | + union { |
---|
| 320 | + const u32 bitfield32_valid; |
---|
| 321 | + const u32 mask; |
---|
| 322 | + const char *reject_message; |
---|
| 323 | + const struct nla_policy *nested_policy; |
---|
| 324 | + struct netlink_range_validation *range; |
---|
| 325 | + struct netlink_range_validation_signed *range_signed; |
---|
| 326 | + struct { |
---|
| 327 | + s16 min, max; |
---|
| 328 | + }; |
---|
| 329 | + int (*validate)(const struct nlattr *attr, |
---|
| 330 | + struct netlink_ext_ack *extack); |
---|
| 331 | + /* This entry is special, and used for the attribute at index 0 |
---|
| 332 | + * only, and specifies special data about the policy, namely it |
---|
| 333 | + * specifies the "boundary type" where strict length validation |
---|
| 334 | + * starts for any attribute types >= this value, also, strict |
---|
| 335 | + * nesting validation starts here. |
---|
| 336 | + * |
---|
| 337 | + * Additionally, it means that NLA_UNSPEC is actually NLA_REJECT |
---|
| 338 | + * for any types >= this, so need to use NLA_POLICY_MIN_LEN() to |
---|
| 339 | + * get the previous pure { .len = xyz } behaviour. The advantage |
---|
| 340 | + * of this is that types not specified in the policy will be |
---|
| 341 | + * rejected. |
---|
| 342 | + * |
---|
| 343 | + * For completely new families it should be set to 1 so that the |
---|
| 344 | + * validation is enforced for all attributes. For existing ones |
---|
| 345 | + * it should be set at least when new attributes are added to |
---|
| 346 | + * the enum used by the policy, and be set to the new value that |
---|
| 347 | + * was added to enforce strict validation from thereon. |
---|
| 348 | + */ |
---|
| 349 | + u16 strict_start_type; |
---|
| 350 | + }; |
---|
226 | 351 | }; |
---|
| 352 | + |
---|
| 353 | +#define NLA_POLICY_ETH_ADDR NLA_POLICY_EXACT_LEN(ETH_ALEN) |
---|
| 354 | +#define NLA_POLICY_ETH_ADDR_COMPAT NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN) |
---|
| 355 | + |
---|
| 356 | +#define _NLA_POLICY_NESTED(maxattr, policy) \ |
---|
| 357 | + { .type = NLA_NESTED, .nested_policy = policy, .len = maxattr } |
---|
| 358 | +#define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \ |
---|
| 359 | + { .type = NLA_NESTED_ARRAY, .nested_policy = policy, .len = maxattr } |
---|
| 360 | +#define NLA_POLICY_NESTED(policy) \ |
---|
| 361 | + _NLA_POLICY_NESTED(ARRAY_SIZE(policy) - 1, policy) |
---|
| 362 | +#define NLA_POLICY_NESTED_ARRAY(policy) \ |
---|
| 363 | + _NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy) |
---|
| 364 | +#define NLA_POLICY_BITFIELD32(valid) \ |
---|
| 365 | + { .type = NLA_BITFIELD32, .bitfield32_valid = valid } |
---|
| 366 | + |
---|
| 367 | +#define __NLA_IS_UINT_TYPE(tp) \ |
---|
| 368 | + (tp == NLA_U8 || tp == NLA_U16 || tp == NLA_U32 || tp == NLA_U64) |
---|
| 369 | +#define __NLA_IS_SINT_TYPE(tp) \ |
---|
| 370 | + (tp == NLA_S8 || tp == NLA_S16 || tp == NLA_S32 || tp == NLA_S64) |
---|
| 371 | + |
---|
| 372 | +#define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition)) |
---|
| 373 | +#define NLA_ENSURE_UINT_TYPE(tp) \ |
---|
| 374 | + (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp)) + tp) |
---|
| 375 | +#define NLA_ENSURE_UINT_OR_BINARY_TYPE(tp) \ |
---|
| 376 | + (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) || \ |
---|
| 377 | + tp == NLA_MSECS || \ |
---|
| 378 | + tp == NLA_BINARY) + tp) |
---|
| 379 | +#define NLA_ENSURE_SINT_TYPE(tp) \ |
---|
| 380 | + (__NLA_ENSURE(__NLA_IS_SINT_TYPE(tp)) + tp) |
---|
| 381 | +#define NLA_ENSURE_INT_OR_BINARY_TYPE(tp) \ |
---|
| 382 | + (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) || \ |
---|
| 383 | + __NLA_IS_SINT_TYPE(tp) || \ |
---|
| 384 | + tp == NLA_MSECS || \ |
---|
| 385 | + tp == NLA_BINARY) + tp) |
---|
| 386 | +#define NLA_ENSURE_NO_VALIDATION_PTR(tp) \ |
---|
| 387 | + (__NLA_ENSURE(tp != NLA_BITFIELD32 && \ |
---|
| 388 | + tp != NLA_REJECT && \ |
---|
| 389 | + tp != NLA_NESTED && \ |
---|
| 390 | + tp != NLA_NESTED_ARRAY) + tp) |
---|
| 391 | + |
---|
| 392 | +#define NLA_POLICY_RANGE(tp, _min, _max) { \ |
---|
| 393 | + .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \ |
---|
| 394 | + .validation_type = NLA_VALIDATE_RANGE, \ |
---|
| 395 | + .min = _min, \ |
---|
| 396 | + .max = _max \ |
---|
| 397 | +} |
---|
| 398 | + |
---|
| 399 | +#define NLA_POLICY_FULL_RANGE(tp, _range) { \ |
---|
| 400 | + .type = NLA_ENSURE_UINT_OR_BINARY_TYPE(tp), \ |
---|
| 401 | + .validation_type = NLA_VALIDATE_RANGE_PTR, \ |
---|
| 402 | + .range = _range, \ |
---|
| 403 | +} |
---|
| 404 | + |
---|
| 405 | +#define NLA_POLICY_FULL_RANGE_SIGNED(tp, _range) { \ |
---|
| 406 | + .type = NLA_ENSURE_SINT_TYPE(tp), \ |
---|
| 407 | + .validation_type = NLA_VALIDATE_RANGE_PTR, \ |
---|
| 408 | + .range_signed = _range, \ |
---|
| 409 | +} |
---|
| 410 | + |
---|
| 411 | +#define NLA_POLICY_MIN(tp, _min) { \ |
---|
| 412 | + .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \ |
---|
| 413 | + .validation_type = NLA_VALIDATE_MIN, \ |
---|
| 414 | + .min = _min, \ |
---|
| 415 | +} |
---|
| 416 | + |
---|
| 417 | +#define NLA_POLICY_MAX(tp, _max) { \ |
---|
| 418 | + .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \ |
---|
| 419 | + .validation_type = NLA_VALIDATE_MAX, \ |
---|
| 420 | + .max = _max, \ |
---|
| 421 | +} |
---|
| 422 | + |
---|
| 423 | +#define NLA_POLICY_MASK(tp, _mask) { \ |
---|
| 424 | + .type = NLA_ENSURE_UINT_TYPE(tp), \ |
---|
| 425 | + .validation_type = NLA_VALIDATE_MASK, \ |
---|
| 426 | + .mask = _mask, \ |
---|
| 427 | +} |
---|
| 428 | + |
---|
| 429 | +#define NLA_POLICY_VALIDATE_FN(tp, fn, ...) { \ |
---|
| 430 | + .type = NLA_ENSURE_NO_VALIDATION_PTR(tp), \ |
---|
| 431 | + .validation_type = NLA_VALIDATE_FUNCTION, \ |
---|
| 432 | + .validate = fn, \ |
---|
| 433 | + .len = __VA_ARGS__ + 0, \ |
---|
| 434 | +} |
---|
| 435 | + |
---|
| 436 | +#define NLA_POLICY_EXACT_LEN(_len) NLA_POLICY_RANGE(NLA_BINARY, _len, _len) |
---|
| 437 | +#define NLA_POLICY_EXACT_LEN_WARN(_len) { \ |
---|
| 438 | + .type = NLA_BINARY, \ |
---|
| 439 | + .validation_type = NLA_VALIDATE_RANGE_WARN_TOO_LONG, \ |
---|
| 440 | + .min = _len, \ |
---|
| 441 | + .max = _len \ |
---|
| 442 | +} |
---|
| 443 | +#define NLA_POLICY_MIN_LEN(_len) NLA_POLICY_MIN(NLA_BINARY, _len) |
---|
227 | 444 | |
---|
228 | 445 | /** |
---|
229 | 446 | * struct nl_info - netlink source information |
---|
230 | 447 | * @nlh: Netlink message header of original request |
---|
| 448 | + * @nl_net: Network namespace |
---|
231 | 449 | * @portid: Netlink PORTID of requesting application |
---|
| 450 | + * @skip_notify: Skip netlink notifications to user space |
---|
| 451 | + * @skip_notify_kernel: Skip selected in-kernel notifications |
---|
232 | 452 | */ |
---|
233 | 453 | struct nl_info { |
---|
234 | 454 | struct nlmsghdr *nlh; |
---|
235 | 455 | struct net *nl_net; |
---|
236 | 456 | u32 portid; |
---|
237 | | - bool skip_notify; |
---|
| 457 | + u8 skip_notify:1, |
---|
| 458 | + skip_notify_kernel:1; |
---|
238 | 459 | }; |
---|
| 460 | + |
---|
| 461 | +/** |
---|
| 462 | + * enum netlink_validation - netlink message/attribute validation levels |
---|
| 463 | + * @NL_VALIDATE_LIBERAL: Old-style "be liberal" validation, not caring about |
---|
| 464 | + * extra data at the end of the message, attributes being longer than |
---|
| 465 | + * they should be, or unknown attributes being present. |
---|
| 466 | + * @NL_VALIDATE_TRAILING: Reject junk data encountered after attribute parsing. |
---|
| 467 | + * @NL_VALIDATE_MAXTYPE: Reject attributes > max type; Together with _TRAILING |
---|
| 468 | + * this is equivalent to the old nla_parse_strict()/nlmsg_parse_strict(). |
---|
| 469 | + * @NL_VALIDATE_UNSPEC: Reject attributes with NLA_UNSPEC in the policy. |
---|
| 470 | + * This can safely be set by the kernel when the given policy has no |
---|
| 471 | + * NLA_UNSPEC anymore, and can thus be used to ensure policy entries |
---|
| 472 | + * are enforced going forward. |
---|
| 473 | + * @NL_VALIDATE_STRICT_ATTRS: strict attribute policy parsing (e.g. |
---|
| 474 | + * U8, U16, U32 must have exact size, etc.) |
---|
| 475 | + * @NL_VALIDATE_NESTED: Check that NLA_F_NESTED is set for NLA_NESTED(_ARRAY) |
---|
| 476 | + * and unset for other policies. |
---|
| 477 | + */ |
---|
| 478 | +enum netlink_validation { |
---|
| 479 | + NL_VALIDATE_LIBERAL = 0, |
---|
| 480 | + NL_VALIDATE_TRAILING = BIT(0), |
---|
| 481 | + NL_VALIDATE_MAXTYPE = BIT(1), |
---|
| 482 | + NL_VALIDATE_UNSPEC = BIT(2), |
---|
| 483 | + NL_VALIDATE_STRICT_ATTRS = BIT(3), |
---|
| 484 | + NL_VALIDATE_NESTED = BIT(4), |
---|
| 485 | +}; |
---|
| 486 | + |
---|
| 487 | +#define NL_VALIDATE_DEPRECATED_STRICT (NL_VALIDATE_TRAILING |\ |
---|
| 488 | + NL_VALIDATE_MAXTYPE) |
---|
| 489 | +#define NL_VALIDATE_STRICT (NL_VALIDATE_TRAILING |\ |
---|
| 490 | + NL_VALIDATE_MAXTYPE |\ |
---|
| 491 | + NL_VALIDATE_UNSPEC |\ |
---|
| 492 | + NL_VALIDATE_STRICT_ATTRS |\ |
---|
| 493 | + NL_VALIDATE_NESTED) |
---|
239 | 494 | |
---|
240 | 495 | int netlink_rcv_skb(struct sk_buff *skb, |
---|
241 | 496 | int (*cb)(struct sk_buff *, struct nlmsghdr *, |
---|
.. | .. |
---|
243 | 498 | int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, |
---|
244 | 499 | unsigned int group, int report, gfp_t flags); |
---|
245 | 500 | |
---|
246 | | -int nla_validate(const struct nlattr *head, int len, int maxtype, |
---|
247 | | - const struct nla_policy *policy, |
---|
248 | | - struct netlink_ext_ack *extack); |
---|
249 | | -int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, |
---|
250 | | - int len, const struct nla_policy *policy, |
---|
251 | | - struct netlink_ext_ack *extack); |
---|
| 501 | +int __nla_validate(const struct nlattr *head, int len, int maxtype, |
---|
| 502 | + const struct nla_policy *policy, unsigned int validate, |
---|
| 503 | + struct netlink_ext_ack *extack); |
---|
| 504 | +int __nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, |
---|
| 505 | + int len, const struct nla_policy *policy, unsigned int validate, |
---|
| 506 | + struct netlink_ext_ack *extack); |
---|
252 | 507 | int nla_policy_len(const struct nla_policy *, int); |
---|
253 | 508 | struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype); |
---|
254 | 509 | size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize); |
---|
.. | .. |
---|
377 | 632 | } |
---|
378 | 633 | |
---|
379 | 634 | /** |
---|
380 | | - * nlmsg_parse - parse attributes of a netlink message |
---|
| 635 | + * nla_parse - Parse a stream of attributes into a tb buffer |
---|
| 636 | + * @tb: destination array with maxtype+1 elements |
---|
| 637 | + * @maxtype: maximum attribute type to be expected |
---|
| 638 | + * @head: head of attribute stream |
---|
| 639 | + * @len: length of attribute stream |
---|
| 640 | + * @policy: validation policy |
---|
| 641 | + * @extack: extended ACK pointer |
---|
| 642 | + * |
---|
| 643 | + * Parses a stream of attributes and stores a pointer to each attribute in |
---|
| 644 | + * the tb array accessible via the attribute type. Attributes with a type |
---|
| 645 | + * exceeding maxtype will be rejected, policy must be specified, attributes |
---|
| 646 | + * will be validated in the strictest way possible. |
---|
| 647 | + * |
---|
| 648 | + * Returns 0 on success or a negative error code. |
---|
| 649 | + */ |
---|
| 650 | +static inline int nla_parse(struct nlattr **tb, int maxtype, |
---|
| 651 | + const struct nlattr *head, int len, |
---|
| 652 | + const struct nla_policy *policy, |
---|
| 653 | + struct netlink_ext_ack *extack) |
---|
| 654 | +{ |
---|
| 655 | + return __nla_parse(tb, maxtype, head, len, policy, |
---|
| 656 | + NL_VALIDATE_STRICT, extack); |
---|
| 657 | +} |
---|
| 658 | + |
---|
| 659 | +/** |
---|
| 660 | + * nla_parse_deprecated - Parse a stream of attributes into a tb buffer |
---|
| 661 | + * @tb: destination array with maxtype+1 elements |
---|
| 662 | + * @maxtype: maximum attribute type to be expected |
---|
| 663 | + * @head: head of attribute stream |
---|
| 664 | + * @len: length of attribute stream |
---|
| 665 | + * @policy: validation policy |
---|
| 666 | + * @extack: extended ACK pointer |
---|
| 667 | + * |
---|
| 668 | + * Parses a stream of attributes and stores a pointer to each attribute in |
---|
| 669 | + * the tb array accessible via the attribute type. Attributes with a type |
---|
| 670 | + * exceeding maxtype will be ignored and attributes from the policy are not |
---|
| 671 | + * always strictly validated (only for new attributes). |
---|
| 672 | + * |
---|
| 673 | + * Returns 0 on success or a negative error code. |
---|
| 674 | + */ |
---|
| 675 | +static inline int nla_parse_deprecated(struct nlattr **tb, int maxtype, |
---|
| 676 | + const struct nlattr *head, int len, |
---|
| 677 | + const struct nla_policy *policy, |
---|
| 678 | + struct netlink_ext_ack *extack) |
---|
| 679 | +{ |
---|
| 680 | + return __nla_parse(tb, maxtype, head, len, policy, |
---|
| 681 | + NL_VALIDATE_LIBERAL, extack); |
---|
| 682 | +} |
---|
| 683 | + |
---|
| 684 | +/** |
---|
| 685 | + * nla_parse_deprecated_strict - Parse a stream of attributes into a tb buffer |
---|
| 686 | + * @tb: destination array with maxtype+1 elements |
---|
| 687 | + * @maxtype: maximum attribute type to be expected |
---|
| 688 | + * @head: head of attribute stream |
---|
| 689 | + * @len: length of attribute stream |
---|
| 690 | + * @policy: validation policy |
---|
| 691 | + * @extack: extended ACK pointer |
---|
| 692 | + * |
---|
| 693 | + * Parses a stream of attributes and stores a pointer to each attribute in |
---|
| 694 | + * the tb array accessible via the attribute type. Attributes with a type |
---|
| 695 | + * exceeding maxtype will be rejected as well as trailing data, but the |
---|
| 696 | + * policy is not completely strictly validated (only for new attributes). |
---|
| 697 | + * |
---|
| 698 | + * Returns 0 on success or a negative error code. |
---|
| 699 | + */ |
---|
| 700 | +static inline int nla_parse_deprecated_strict(struct nlattr **tb, int maxtype, |
---|
| 701 | + const struct nlattr *head, |
---|
| 702 | + int len, |
---|
| 703 | + const struct nla_policy *policy, |
---|
| 704 | + struct netlink_ext_ack *extack) |
---|
| 705 | +{ |
---|
| 706 | + return __nla_parse(tb, maxtype, head, len, policy, |
---|
| 707 | + NL_VALIDATE_DEPRECATED_STRICT, extack); |
---|
| 708 | +} |
---|
| 709 | + |
---|
| 710 | +/** |
---|
| 711 | + * __nlmsg_parse - parse attributes of a netlink message |
---|
381 | 712 | * @nlh: netlink message header |
---|
382 | 713 | * @hdrlen: length of family specific header |
---|
383 | 714 | * @tb: destination array with maxtype+1 elements |
---|
384 | 715 | * @maxtype: maximum attribute type to be expected |
---|
385 | 716 | * @policy: validation policy |
---|
| 717 | + * @validate: validation strictness |
---|
| 718 | + * @extack: extended ACK report struct |
---|
| 719 | + * |
---|
| 720 | + * See nla_parse() |
---|
| 721 | + */ |
---|
| 722 | +static inline int __nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen, |
---|
| 723 | + struct nlattr *tb[], int maxtype, |
---|
| 724 | + const struct nla_policy *policy, |
---|
| 725 | + unsigned int validate, |
---|
| 726 | + struct netlink_ext_ack *extack) |
---|
| 727 | +{ |
---|
| 728 | + if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) { |
---|
| 729 | + NL_SET_ERR_MSG(extack, "Invalid header length"); |
---|
| 730 | + return -EINVAL; |
---|
| 731 | + } |
---|
| 732 | + |
---|
| 733 | + return __nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), |
---|
| 734 | + nlmsg_attrlen(nlh, hdrlen), policy, validate, |
---|
| 735 | + extack); |
---|
| 736 | +} |
---|
| 737 | + |
---|
| 738 | +/** |
---|
| 739 | + * nlmsg_parse - parse attributes of a netlink message |
---|
| 740 | + * @nlh: netlink message header |
---|
| 741 | + * @hdrlen: length of family specific header |
---|
| 742 | + * @tb: destination array with maxtype+1 elements |
---|
| 743 | + * @maxtype: maximum attribute type to be expected |
---|
386 | 744 | * @extack: extended ACK report struct |
---|
387 | 745 | * |
---|
388 | 746 | * See nla_parse() |
---|
.. | .. |
---|
392 | 750 | const struct nla_policy *policy, |
---|
393 | 751 | struct netlink_ext_ack *extack) |
---|
394 | 752 | { |
---|
395 | | - if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) |
---|
396 | | - return -EINVAL; |
---|
| 753 | + return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy, |
---|
| 754 | + NL_VALIDATE_STRICT, extack); |
---|
| 755 | +} |
---|
397 | 756 | |
---|
398 | | - return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), |
---|
399 | | - nlmsg_attrlen(nlh, hdrlen), policy, extack); |
---|
| 757 | +/** |
---|
| 758 | + * nlmsg_parse_deprecated - parse attributes of a netlink message |
---|
| 759 | + * @nlh: netlink message header |
---|
| 760 | + * @hdrlen: length of family specific header |
---|
| 761 | + * @tb: destination array with maxtype+1 elements |
---|
| 762 | + * @maxtype: maximum attribute type to be expected |
---|
| 763 | + * @extack: extended ACK report struct |
---|
| 764 | + * |
---|
| 765 | + * See nla_parse_deprecated() |
---|
| 766 | + */ |
---|
| 767 | +static inline int nlmsg_parse_deprecated(const struct nlmsghdr *nlh, int hdrlen, |
---|
| 768 | + struct nlattr *tb[], int maxtype, |
---|
| 769 | + const struct nla_policy *policy, |
---|
| 770 | + struct netlink_ext_ack *extack) |
---|
| 771 | +{ |
---|
| 772 | + return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy, |
---|
| 773 | + NL_VALIDATE_LIBERAL, extack); |
---|
| 774 | +} |
---|
| 775 | + |
---|
| 776 | +/** |
---|
| 777 | + * nlmsg_parse_deprecated_strict - parse attributes of a netlink message |
---|
| 778 | + * @nlh: netlink message header |
---|
| 779 | + * @hdrlen: length of family specific header |
---|
| 780 | + * @tb: destination array with maxtype+1 elements |
---|
| 781 | + * @maxtype: maximum attribute type to be expected |
---|
| 782 | + * @extack: extended ACK report struct |
---|
| 783 | + * |
---|
| 784 | + * See nla_parse_deprecated_strict() |
---|
| 785 | + */ |
---|
| 786 | +static inline int |
---|
| 787 | +nlmsg_parse_deprecated_strict(const struct nlmsghdr *nlh, int hdrlen, |
---|
| 788 | + struct nlattr *tb[], int maxtype, |
---|
| 789 | + const struct nla_policy *policy, |
---|
| 790 | + struct netlink_ext_ack *extack) |
---|
| 791 | +{ |
---|
| 792 | + return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy, |
---|
| 793 | + NL_VALIDATE_DEPRECATED_STRICT, extack); |
---|
400 | 794 | } |
---|
401 | 795 | |
---|
402 | 796 | /** |
---|
.. | .. |
---|
415 | 809 | } |
---|
416 | 810 | |
---|
417 | 811 | /** |
---|
418 | | - * nlmsg_validate - validate a netlink message including attributes |
---|
| 812 | + * nla_validate_deprecated - Validate a stream of attributes |
---|
| 813 | + * @head: head of attribute stream |
---|
| 814 | + * @len: length of attribute stream |
---|
| 815 | + * @maxtype: maximum attribute type to be expected |
---|
| 816 | + * @policy: validation policy |
---|
| 817 | + * @validate: validation strictness |
---|
| 818 | + * @extack: extended ACK report struct |
---|
| 819 | + * |
---|
| 820 | + * Validates all attributes in the specified attribute stream against the |
---|
| 821 | + * specified policy. Validation is done in liberal mode. |
---|
| 822 | + * See documenation of struct nla_policy for more details. |
---|
| 823 | + * |
---|
| 824 | + * Returns 0 on success or a negative error code. |
---|
| 825 | + */ |
---|
| 826 | +static inline int nla_validate_deprecated(const struct nlattr *head, int len, |
---|
| 827 | + int maxtype, |
---|
| 828 | + const struct nla_policy *policy, |
---|
| 829 | + struct netlink_ext_ack *extack) |
---|
| 830 | +{ |
---|
| 831 | + return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_LIBERAL, |
---|
| 832 | + extack); |
---|
| 833 | +} |
---|
| 834 | + |
---|
| 835 | +/** |
---|
| 836 | + * nla_validate - Validate a stream of attributes |
---|
| 837 | + * @head: head of attribute stream |
---|
| 838 | + * @len: length of attribute stream |
---|
| 839 | + * @maxtype: maximum attribute type to be expected |
---|
| 840 | + * @policy: validation policy |
---|
| 841 | + * @extack: extended ACK report struct |
---|
| 842 | + * |
---|
| 843 | + * Validates all attributes in the specified attribute stream against the |
---|
| 844 | + * specified policy. Validation is done in strict mode. |
---|
| 845 | + * See documenation of struct nla_policy for more details. |
---|
| 846 | + * |
---|
| 847 | + * Returns 0 on success or a negative error code. |
---|
| 848 | + */ |
---|
| 849 | +static inline int nla_validate(const struct nlattr *head, int len, int maxtype, |
---|
| 850 | + const struct nla_policy *policy, |
---|
| 851 | + struct netlink_ext_ack *extack) |
---|
| 852 | +{ |
---|
| 853 | + return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_STRICT, |
---|
| 854 | + extack); |
---|
| 855 | +} |
---|
| 856 | + |
---|
| 857 | +/** |
---|
| 858 | + * nlmsg_validate_deprecated - validate a netlink message including attributes |
---|
419 | 859 | * @nlh: netlinket message header |
---|
420 | 860 | * @hdrlen: length of familiy specific header |
---|
421 | 861 | * @maxtype: maximum attribute type to be expected |
---|
422 | 862 | * @policy: validation policy |
---|
423 | 863 | * @extack: extended ACK report struct |
---|
424 | 864 | */ |
---|
425 | | -static inline int nlmsg_validate(const struct nlmsghdr *nlh, |
---|
426 | | - int hdrlen, int maxtype, |
---|
427 | | - const struct nla_policy *policy, |
---|
428 | | - struct netlink_ext_ack *extack) |
---|
| 865 | +static inline int nlmsg_validate_deprecated(const struct nlmsghdr *nlh, |
---|
| 866 | + int hdrlen, int maxtype, |
---|
| 867 | + const struct nla_policy *policy, |
---|
| 868 | + struct netlink_ext_ack *extack) |
---|
429 | 869 | { |
---|
430 | 870 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) |
---|
431 | 871 | return -EINVAL; |
---|
432 | 872 | |
---|
433 | | - return nla_validate(nlmsg_attrdata(nlh, hdrlen), |
---|
434 | | - nlmsg_attrlen(nlh, hdrlen), maxtype, policy, |
---|
435 | | - extack); |
---|
| 873 | + return __nla_validate(nlmsg_attrdata(nlh, hdrlen), |
---|
| 874 | + nlmsg_attrlen(nlh, hdrlen), maxtype, |
---|
| 875 | + policy, NL_VALIDATE_LIBERAL, extack); |
---|
436 | 876 | } |
---|
| 877 | + |
---|
| 878 | + |
---|
437 | 879 | |
---|
438 | 880 | /** |
---|
439 | 881 | * nlmsg_report - need to report back to application? |
---|
.. | .. |
---|
762 | 1204 | const struct nla_policy *policy, |
---|
763 | 1205 | struct netlink_ext_ack *extack) |
---|
764 | 1206 | { |
---|
765 | | - return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy, |
---|
766 | | - extack); |
---|
| 1207 | + if (!(nla->nla_type & NLA_F_NESTED)) { |
---|
| 1208 | + NL_SET_ERR_MSG_ATTR(extack, nla, "NLA_F_NESTED is missing"); |
---|
| 1209 | + return -EINVAL; |
---|
| 1210 | + } |
---|
| 1211 | + |
---|
| 1212 | + return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy, |
---|
| 1213 | + NL_VALIDATE_STRICT, extack); |
---|
| 1214 | +} |
---|
| 1215 | + |
---|
| 1216 | +/** |
---|
| 1217 | + * nla_parse_nested_deprecated - parse nested attributes |
---|
| 1218 | + * @tb: destination array with maxtype+1 elements |
---|
| 1219 | + * @maxtype: maximum attribute type to be expected |
---|
| 1220 | + * @nla: attribute containing the nested attributes |
---|
| 1221 | + * @policy: validation policy |
---|
| 1222 | + * @extack: extended ACK report struct |
---|
| 1223 | + * |
---|
| 1224 | + * See nla_parse_deprecated() |
---|
| 1225 | + */ |
---|
| 1226 | +static inline int nla_parse_nested_deprecated(struct nlattr *tb[], int maxtype, |
---|
| 1227 | + const struct nlattr *nla, |
---|
| 1228 | + const struct nla_policy *policy, |
---|
| 1229 | + struct netlink_ext_ack *extack) |
---|
| 1230 | +{ |
---|
| 1231 | + return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy, |
---|
| 1232 | + NL_VALIDATE_LIBERAL, extack); |
---|
767 | 1233 | } |
---|
768 | 1234 | |
---|
769 | 1235 | /** |
---|
.. | .. |
---|
1065 | 1531 | } |
---|
1066 | 1532 | |
---|
1067 | 1533 | /** |
---|
| 1534 | + * nla_put_bitfield32 - Add a bitfield32 netlink attribute to a socket buffer |
---|
| 1535 | + * @skb: socket buffer to add attribute to |
---|
| 1536 | + * @attrtype: attribute type |
---|
| 1537 | + * @value: value carrying bits |
---|
| 1538 | + * @selector: selector of valid bits |
---|
| 1539 | + */ |
---|
| 1540 | +static inline int nla_put_bitfield32(struct sk_buff *skb, int attrtype, |
---|
| 1541 | + __u32 value, __u32 selector) |
---|
| 1542 | +{ |
---|
| 1543 | + struct nla_bitfield32 tmp = { value, selector, }; |
---|
| 1544 | + |
---|
| 1545 | + return nla_put(skb, attrtype, sizeof(tmp), &tmp); |
---|
| 1546 | +} |
---|
| 1547 | + |
---|
| 1548 | +/** |
---|
1068 | 1549 | * nla_get_u32 - return payload of u32 attribute |
---|
1069 | 1550 | * @nla: u32 netlink attribute |
---|
1070 | 1551 | */ |
---|
.. | .. |
---|
1268 | 1749 | } |
---|
1269 | 1750 | |
---|
1270 | 1751 | /** |
---|
1271 | | - * nla_nest_start - Start a new level of nested attributes |
---|
| 1752 | + * nla_nest_start_noflag - Start a new level of nested attributes |
---|
1272 | 1753 | * @skb: socket buffer to add attributes to |
---|
1273 | 1754 | * @attrtype: attribute type of container |
---|
1274 | 1755 | * |
---|
1275 | | - * Returns the container attribute |
---|
| 1756 | + * This function exists for backward compatibility to use in APIs which never |
---|
| 1757 | + * marked their nest attributes with NLA_F_NESTED flag. New APIs should use |
---|
| 1758 | + * nla_nest_start() which sets the flag. |
---|
| 1759 | + * |
---|
| 1760 | + * Returns the container attribute or NULL on error |
---|
1276 | 1761 | */ |
---|
1277 | | -static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) |
---|
| 1762 | +static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb, |
---|
| 1763 | + int attrtype) |
---|
1278 | 1764 | { |
---|
1279 | 1765 | struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb); |
---|
1280 | 1766 | |
---|
.. | .. |
---|
1282 | 1768 | return NULL; |
---|
1283 | 1769 | |
---|
1284 | 1770 | return start; |
---|
| 1771 | +} |
---|
| 1772 | + |
---|
| 1773 | +/** |
---|
| 1774 | + * nla_nest_start - Start a new level of nested attributes, with NLA_F_NESTED |
---|
| 1775 | + * @skb: socket buffer to add attributes to |
---|
| 1776 | + * @attrtype: attribute type of container |
---|
| 1777 | + * |
---|
| 1778 | + * Unlike nla_nest_start_noflag(), mark the nest attribute with NLA_F_NESTED |
---|
| 1779 | + * flag. This is the preferred function to use in new code. |
---|
| 1780 | + * |
---|
| 1781 | + * Returns the container attribute or NULL on error |
---|
| 1782 | + */ |
---|
| 1783 | +static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) |
---|
| 1784 | +{ |
---|
| 1785 | + return nla_nest_start_noflag(skb, attrtype | NLA_F_NESTED); |
---|
1285 | 1786 | } |
---|
1286 | 1787 | |
---|
1287 | 1788 | /** |
---|
.. | .. |
---|
1314 | 1815 | } |
---|
1315 | 1816 | |
---|
1316 | 1817 | /** |
---|
1317 | | - * nla_validate_nested - Validate a stream of nested attributes |
---|
| 1818 | + * __nla_validate_nested - Validate a stream of nested attributes |
---|
1318 | 1819 | * @start: container attribute |
---|
1319 | 1820 | * @maxtype: maximum attribute type to be expected |
---|
1320 | 1821 | * @policy: validation policy |
---|
| 1822 | + * @validate: validation strictness |
---|
1321 | 1823 | * @extack: extended ACK report struct |
---|
1322 | 1824 | * |
---|
1323 | 1825 | * Validates all attributes in the nested attribute stream against the |
---|
.. | .. |
---|
1326 | 1828 | * |
---|
1327 | 1829 | * Returns 0 on success or a negative error code. |
---|
1328 | 1830 | */ |
---|
1329 | | -static inline int nla_validate_nested(const struct nlattr *start, int maxtype, |
---|
1330 | | - const struct nla_policy *policy, |
---|
1331 | | - struct netlink_ext_ack *extack) |
---|
| 1831 | +static inline int __nla_validate_nested(const struct nlattr *start, int maxtype, |
---|
| 1832 | + const struct nla_policy *policy, |
---|
| 1833 | + unsigned int validate, |
---|
| 1834 | + struct netlink_ext_ack *extack) |
---|
1332 | 1835 | { |
---|
1333 | | - return nla_validate(nla_data(start), nla_len(start), maxtype, policy, |
---|
1334 | | - extack); |
---|
| 1836 | + return __nla_validate(nla_data(start), nla_len(start), maxtype, policy, |
---|
| 1837 | + validate, extack); |
---|
| 1838 | +} |
---|
| 1839 | + |
---|
| 1840 | +static inline int |
---|
| 1841 | +nla_validate_nested(const struct nlattr *start, int maxtype, |
---|
| 1842 | + const struct nla_policy *policy, |
---|
| 1843 | + struct netlink_ext_ack *extack) |
---|
| 1844 | +{ |
---|
| 1845 | + return __nla_validate_nested(start, maxtype, policy, |
---|
| 1846 | + NL_VALIDATE_STRICT, extack); |
---|
| 1847 | +} |
---|
| 1848 | + |
---|
| 1849 | +static inline int |
---|
| 1850 | +nla_validate_nested_deprecated(const struct nlattr *start, int maxtype, |
---|
| 1851 | + const struct nla_policy *policy, |
---|
| 1852 | + struct netlink_ext_ack *extack) |
---|
| 1853 | +{ |
---|
| 1854 | + return __nla_validate_nested(start, maxtype, policy, |
---|
| 1855 | + NL_VALIDATE_LIBERAL, extack); |
---|
1335 | 1856 | } |
---|
1336 | 1857 | |
---|
1337 | 1858 | /** |
---|
.. | .. |
---|
1420 | 1941 | return nla->nla_len == rem; |
---|
1421 | 1942 | } |
---|
1422 | 1943 | |
---|
| 1944 | +void nla_get_range_unsigned(const struct nla_policy *pt, |
---|
| 1945 | + struct netlink_range_validation *range); |
---|
| 1946 | +void nla_get_range_signed(const struct nla_policy *pt, |
---|
| 1947 | + struct netlink_range_validation_signed *range); |
---|
| 1948 | + |
---|
| 1949 | +struct netlink_policy_dump_state; |
---|
| 1950 | + |
---|
| 1951 | +int netlink_policy_dump_add_policy(struct netlink_policy_dump_state **pstate, |
---|
| 1952 | + const struct nla_policy *policy, |
---|
| 1953 | + unsigned int maxtype); |
---|
| 1954 | +int netlink_policy_dump_get_policy_idx(struct netlink_policy_dump_state *state, |
---|
| 1955 | + const struct nla_policy *policy, |
---|
| 1956 | + unsigned int maxtype); |
---|
| 1957 | +bool netlink_policy_dump_loop(struct netlink_policy_dump_state *state); |
---|
| 1958 | +int netlink_policy_dump_write(struct sk_buff *skb, |
---|
| 1959 | + struct netlink_policy_dump_state *state); |
---|
| 1960 | +int netlink_policy_dump_attr_size_estimate(const struct nla_policy *pt); |
---|
| 1961 | +int netlink_policy_dump_write_attr(struct sk_buff *skb, |
---|
| 1962 | + const struct nla_policy *pt, |
---|
| 1963 | + int nestattr); |
---|
| 1964 | +void netlink_policy_dump_free(struct netlink_policy_dump_state *state); |
---|
| 1965 | + |
---|
1423 | 1966 | #endif |
---|