nlattr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * NETLINK Netlink attributes
  3. *
  4. * Authors: Thomas Graf <tgraf@suug.ch>
  5. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  6. */
  7. #include <linux/export.h>
  8. #include <linux/kernel.h>
  9. #include <linux/errno.h>
  10. #include <linux/jiffies.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/string.h>
  14. #include <linux/types.h>
  15. #include <net/netlink.h>
  16. static const u16 nla_attr_minlen[NLA_TYPE_MAX+1] = {
  17. [NLA_U8] = sizeof(u8),
  18. [NLA_U16] = sizeof(u16),
  19. [NLA_U32] = sizeof(u32),
  20. [NLA_U64] = sizeof(u64),
  21. [NLA_MSECS] = sizeof(u64),
  22. [NLA_NESTED] = NLA_HDRLEN,
  23. [NLA_S8] = sizeof(s8),
  24. [NLA_S16] = sizeof(s16),
  25. [NLA_S32] = sizeof(s32),
  26. [NLA_S64] = sizeof(s64),
  27. };
  28. static int validate_nla(const struct nlattr *nla, int maxtype,
  29. const struct nla_policy *policy)
  30. {
  31. const struct nla_policy *pt;
  32. int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
  33. if (type <= 0 || type > maxtype)
  34. return 0;
  35. pt = &policy[type];
  36. BUG_ON(pt->type > NLA_TYPE_MAX);
  37. switch (pt->type) {
  38. case NLA_FLAG:
  39. if (attrlen > 0)
  40. return -ERANGE;
  41. break;
  42. case NLA_NUL_STRING:
  43. if (pt->len)
  44. minlen = min_t(int, attrlen, pt->len + 1);
  45. else
  46. minlen = attrlen;
  47. if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
  48. return -EINVAL;
  49. /* fall through */
  50. case NLA_STRING:
  51. if (attrlen < 1)
  52. return -ERANGE;
  53. if (pt->len) {
  54. char *buf = nla_data(nla);
  55. if (buf[attrlen - 1] == '\0')
  56. attrlen--;
  57. if (attrlen > pt->len)
  58. return -ERANGE;
  59. }
  60. break;
  61. case NLA_BINARY:
  62. if (pt->len && attrlen > pt->len)
  63. return -ERANGE;
  64. break;
  65. case NLA_NESTED_COMPAT:
  66. if (attrlen < pt->len)
  67. return -ERANGE;
  68. if (attrlen < NLA_ALIGN(pt->len))
  69. break;
  70. if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
  71. return -ERANGE;
  72. nla = nla_data(nla) + NLA_ALIGN(pt->len);
  73. if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
  74. return -ERANGE;
  75. break;
  76. case NLA_NESTED:
  77. /* a nested attributes is allowed to be empty; if its not,
  78. * it must have a size of at least NLA_HDRLEN.
  79. */
  80. if (attrlen == 0)
  81. break;
  82. default:
  83. if (pt->len)
  84. minlen = pt->len;
  85. else if (pt->type != NLA_UNSPEC)
  86. minlen = nla_attr_minlen[pt->type];
  87. if (attrlen < minlen)
  88. return -ERANGE;
  89. }
  90. return 0;
  91. }
  92. /**
  93. * nla_validate - Validate a stream of attributes
  94. * @head: head of attribute stream
  95. * @len: length of attribute stream
  96. * @maxtype: maximum attribute type to be expected
  97. * @policy: validation policy
  98. *
  99. * Validates all attributes in the specified attribute stream against the
  100. * specified policy. Attributes with a type exceeding maxtype will be
  101. * ignored. See documenation of struct nla_policy for more details.
  102. *
  103. * Returns 0 on success or a negative error code.
  104. */
  105. int nla_validate(const struct nlattr *head, int len, int maxtype,
  106. const struct nla_policy *policy)
  107. {
  108. const struct nlattr *nla;
  109. int rem, err;
  110. nla_for_each_attr(nla, head, len, rem) {
  111. err = validate_nla(nla, maxtype, policy);
  112. if (err < 0)
  113. goto errout;
  114. }
  115. err = 0;
  116. errout:
  117. return err;
  118. }
  119. EXPORT_SYMBOL(nla_validate);
  120. /**
  121. * nla_policy_len - Determin the max. length of a policy
  122. * @policy: policy to use
  123. * @n: number of policies
  124. *
  125. * Determines the max. length of the policy. It is currently used
  126. * to allocated Netlink buffers roughly the size of the actual
  127. * message.
  128. *
  129. * Returns 0 on success or a negative error code.
  130. */
  131. int
  132. nla_policy_len(const struct nla_policy *p, int n)
  133. {
  134. int i, len = 0;
  135. for (i = 0; i < n; i++, p++) {
  136. if (p->len)
  137. len += nla_total_size(p->len);
  138. else if (nla_attr_minlen[p->type])
  139. len += nla_total_size(nla_attr_minlen[p->type]);
  140. }
  141. return len;
  142. }
  143. EXPORT_SYMBOL(nla_policy_len);
  144. /**
  145. * nla_parse - Parse a stream of attributes into a tb buffer
  146. * @tb: destination array with maxtype+1 elements
  147. * @maxtype: maximum attribute type to be expected
  148. * @head: head of attribute stream
  149. * @len: length of attribute stream
  150. * @policy: validation policy
  151. *
  152. * Parses a stream of attributes and stores a pointer to each attribute in
  153. * the tb array accessible via the attribute type. Attributes with a type
  154. * exceeding maxtype will be silently ignored for backwards compatibility
  155. * reasons. policy may be set to NULL if no validation is required.
  156. *
  157. * Returns 0 on success or a negative error code.
  158. */
  159. int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
  160. int len, const struct nla_policy *policy)
  161. {
  162. const struct nlattr *nla;
  163. int rem, err;
  164. memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
  165. nla_for_each_attr(nla, head, len, rem) {
  166. u16 type = nla_type(nla);
  167. if (type > 0 && type <= maxtype) {
  168. if (policy) {
  169. err = validate_nla(nla, maxtype, policy);
  170. if (err < 0)
  171. goto errout;
  172. }
  173. tb[type] = (struct nlattr *)nla;
  174. }
  175. }
  176. if (unlikely(rem > 0))
  177. pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
  178. rem, current->comm);
  179. err = 0;
  180. errout:
  181. return err;
  182. }
  183. EXPORT_SYMBOL(nla_parse);
  184. /**
  185. * nla_find - Find a specific attribute in a stream of attributes
  186. * @head: head of attribute stream
  187. * @len: length of attribute stream
  188. * @attrtype: type of attribute to look for
  189. *
  190. * Returns the first attribute in the stream matching the specified type.
  191. */
  192. struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
  193. {
  194. const struct nlattr *nla;
  195. int rem;
  196. nla_for_each_attr(nla, head, len, rem)
  197. if (nla_type(nla) == attrtype)
  198. return (struct nlattr *)nla;
  199. return NULL;
  200. }
  201. EXPORT_SYMBOL(nla_find);
  202. /**
  203. * nla_strlcpy - Copy string attribute payload into a sized buffer
  204. * @dst: where to copy the string to
  205. * @nla: attribute to copy the string from
  206. * @dstsize: size of destination buffer
  207. *
  208. * Copies at most dstsize - 1 bytes into the destination buffer.
  209. * The result is always a valid NUL-terminated string. Unlike
  210. * strlcpy the destination buffer is always padded out.
  211. *
  212. * Returns the length of the source buffer.
  213. */
  214. size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
  215. {
  216. size_t srclen = nla_len(nla);
  217. char *src = nla_data(nla);
  218. if (srclen > 0 && src[srclen - 1] == '\0')
  219. srclen--;
  220. if (dstsize > 0) {
  221. size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
  222. memset(dst, 0, dstsize);
  223. memcpy(dst, src, len);
  224. }
  225. return srclen;
  226. }
  227. EXPORT_SYMBOL(nla_strlcpy);
  228. /**
  229. * nla_memcpy - Copy a netlink attribute into another memory area
  230. * @dest: where to copy to memcpy
  231. * @src: netlink attribute to copy from
  232. * @count: size of the destination area
  233. *
  234. * Note: The number of bytes copied is limited by the length of
  235. * attribute's payload. memcpy
  236. *
  237. * Returns the number of bytes copied.
  238. */
  239. int nla_memcpy(void *dest, const struct nlattr *src, int count)
  240. {
  241. int minlen = min_t(int, count, nla_len(src));
  242. memcpy(dest, nla_data(src), minlen);
  243. return minlen;
  244. }
  245. EXPORT_SYMBOL(nla_memcpy);
  246. /**
  247. * nla_memcmp - Compare an attribute with sized memory area
  248. * @nla: netlink attribute
  249. * @data: memory area
  250. * @size: size of memory area
  251. */
  252. int nla_memcmp(const struct nlattr *nla, const void *data,
  253. size_t size)
  254. {
  255. int d = nla_len(nla) - size;
  256. if (d == 0)
  257. d = memcmp(nla_data(nla), data, size);
  258. return d;
  259. }
  260. EXPORT_SYMBOL(nla_memcmp);
  261. /**
  262. * nla_strcmp - Compare a string attribute against a string
  263. * @nla: netlink string attribute
  264. * @str: another string
  265. */
  266. int nla_strcmp(const struct nlattr *nla, const char *str)
  267. {
  268. int len = strlen(str);
  269. char *buf = nla_data(nla);
  270. int attrlen = nla_len(nla);
  271. int d;
  272. if (attrlen > 0 && buf[attrlen - 1] == '\0')
  273. attrlen--;
  274. d = attrlen - len;
  275. if (d == 0)
  276. d = memcmp(nla_data(nla), str, len);
  277. return d;
  278. }
  279. EXPORT_SYMBOL(nla_strcmp);
  280. #ifdef CONFIG_NET
  281. /**
  282. * __nla_reserve - reserve room for attribute on the skb
  283. * @skb: socket buffer to reserve room on
  284. * @attrtype: attribute type
  285. * @attrlen: length of attribute payload
  286. *
  287. * Adds a netlink attribute header to a socket buffer and reserves
  288. * room for the payload but does not copy it.
  289. *
  290. * The caller is responsible to ensure that the skb provides enough
  291. * tailroom for the attribute header and payload.
  292. */
  293. struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
  294. {
  295. struct nlattr *nla;
  296. nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
  297. nla->nla_type = attrtype;
  298. nla->nla_len = nla_attr_size(attrlen);
  299. memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
  300. return nla;
  301. }
  302. EXPORT_SYMBOL(__nla_reserve);
  303. /**
  304. * __nla_reserve_nohdr - reserve room for attribute without header
  305. * @skb: socket buffer to reserve room on
  306. * @attrlen: length of attribute payload
  307. *
  308. * Reserves room for attribute payload without a header.
  309. *
  310. * The caller is responsible to ensure that the skb provides enough
  311. * tailroom for the payload.
  312. */
  313. void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
  314. {
  315. void *start;
  316. start = skb_put(skb, NLA_ALIGN(attrlen));
  317. memset(start, 0, NLA_ALIGN(attrlen));
  318. return start;
  319. }
  320. EXPORT_SYMBOL(__nla_reserve_nohdr);
  321. /**
  322. * nla_reserve - reserve room for attribute on the skb
  323. * @skb: socket buffer to reserve room on
  324. * @attrtype: attribute type
  325. * @attrlen: length of attribute payload
  326. *
  327. * Adds a netlink attribute header to a socket buffer and reserves
  328. * room for the payload but does not copy it.
  329. *
  330. * Returns NULL if the tailroom of the skb is insufficient to store
  331. * the attribute header and payload.
  332. */
  333. struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
  334. {
  335. if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
  336. return NULL;
  337. return __nla_reserve(skb, attrtype, attrlen);
  338. }
  339. EXPORT_SYMBOL(nla_reserve);
  340. /**
  341. * nla_reserve_nohdr - reserve room for attribute without header
  342. * @skb: socket buffer to reserve room on
  343. * @attrlen: length of attribute payload
  344. *
  345. * Reserves room for attribute payload without a header.
  346. *
  347. * Returns NULL if the tailroom of the skb is insufficient to store
  348. * the attribute payload.
  349. */
  350. void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
  351. {
  352. if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
  353. return NULL;
  354. return __nla_reserve_nohdr(skb, attrlen);
  355. }
  356. EXPORT_SYMBOL(nla_reserve_nohdr);
  357. /**
  358. * __nla_put - Add a netlink attribute to a socket buffer
  359. * @skb: socket buffer to add attribute to
  360. * @attrtype: attribute type
  361. * @attrlen: length of attribute payload
  362. * @data: head of attribute payload
  363. *
  364. * The caller is responsible to ensure that the skb provides enough
  365. * tailroom for the attribute header and payload.
  366. */
  367. void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
  368. const void *data)
  369. {
  370. struct nlattr *nla;
  371. nla = __nla_reserve(skb, attrtype, attrlen);
  372. memcpy(nla_data(nla), data, attrlen);
  373. }
  374. EXPORT_SYMBOL(__nla_put);
  375. /**
  376. * __nla_put_nohdr - Add a netlink attribute without header
  377. * @skb: socket buffer to add attribute to
  378. * @attrlen: length of attribute payload
  379. * @data: head of attribute payload
  380. *
  381. * The caller is responsible to ensure that the skb provides enough
  382. * tailroom for the attribute payload.
  383. */
  384. void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
  385. {
  386. void *start;
  387. start = __nla_reserve_nohdr(skb, attrlen);
  388. memcpy(start, data, attrlen);
  389. }
  390. EXPORT_SYMBOL(__nla_put_nohdr);
  391. /**
  392. * nla_put - Add a netlink attribute to a socket buffer
  393. * @skb: socket buffer to add attribute to
  394. * @attrtype: attribute type
  395. * @attrlen: length of attribute payload
  396. * @data: head of attribute payload
  397. *
  398. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  399. * the attribute header and payload.
  400. */
  401. int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
  402. {
  403. if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
  404. return -EMSGSIZE;
  405. __nla_put(skb, attrtype, attrlen, data);
  406. return 0;
  407. }
  408. EXPORT_SYMBOL(nla_put);
  409. /**
  410. * nla_put_nohdr - Add a netlink attribute without header
  411. * @skb: socket buffer to add attribute to
  412. * @attrlen: length of attribute payload
  413. * @data: head of attribute payload
  414. *
  415. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  416. * the attribute payload.
  417. */
  418. int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
  419. {
  420. if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
  421. return -EMSGSIZE;
  422. __nla_put_nohdr(skb, attrlen, data);
  423. return 0;
  424. }
  425. EXPORT_SYMBOL(nla_put_nohdr);
  426. /**
  427. * nla_append - Add a netlink attribute without header or padding
  428. * @skb: socket buffer to add attribute to
  429. * @attrlen: length of attribute payload
  430. * @data: head of attribute payload
  431. *
  432. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  433. * the attribute payload.
  434. */
  435. int nla_append(struct sk_buff *skb, int attrlen, const void *data)
  436. {
  437. if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
  438. return -EMSGSIZE;
  439. memcpy(skb_put(skb, attrlen), data, attrlen);
  440. return 0;
  441. }
  442. EXPORT_SYMBOL(nla_append);
  443. #endif