nlattr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. /**
  120. * nla_policy_len - Determin the max. length of a policy
  121. * @policy: policy to use
  122. * @n: number of policies
  123. *
  124. * Determines the max. length of the policy. It is currently used
  125. * to allocated Netlink buffers roughly the size of the actual
  126. * message.
  127. *
  128. * Returns 0 on success or a negative error code.
  129. */
  130. int
  131. nla_policy_len(const struct nla_policy *p, int n)
  132. {
  133. int i, len = 0;
  134. for (i = 0; i < n; i++, p++) {
  135. if (p->len)
  136. len += nla_total_size(p->len);
  137. else if (nla_attr_minlen[p->type])
  138. len += nla_total_size(nla_attr_minlen[p->type]);
  139. }
  140. return len;
  141. }
  142. /**
  143. * nla_parse - Parse a stream of attributes into a tb buffer
  144. * @tb: destination array with maxtype+1 elements
  145. * @maxtype: maximum attribute type to be expected
  146. * @head: head of attribute stream
  147. * @len: length of attribute stream
  148. * @policy: validation policy
  149. *
  150. * Parses a stream of attributes and stores a pointer to each attribute in
  151. * the tb array accessible via the attribute type. Attributes with a type
  152. * exceeding maxtype will be silently ignored for backwards compatibility
  153. * reasons. policy may be set to NULL if no validation is required.
  154. *
  155. * Returns 0 on success or a negative error code.
  156. */
  157. int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
  158. int len, const struct nla_policy *policy)
  159. {
  160. const struct nlattr *nla;
  161. int rem, err;
  162. memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
  163. nla_for_each_attr(nla, head, len, rem) {
  164. u16 type = nla_type(nla);
  165. if (type > 0 && type <= maxtype) {
  166. if (policy) {
  167. err = validate_nla(nla, maxtype, policy);
  168. if (err < 0)
  169. goto errout;
  170. }
  171. tb[type] = (struct nlattr *)nla;
  172. }
  173. }
  174. if (unlikely(rem > 0))
  175. printk(KERN_WARNING "netlink: %d bytes leftover after parsing "
  176. "attributes.\n", rem);
  177. err = 0;
  178. errout:
  179. return err;
  180. }
  181. /**
  182. * nla_find - Find a specific attribute in a stream of attributes
  183. * @head: head of attribute stream
  184. * @len: length of attribute stream
  185. * @attrtype: type of attribute to look for
  186. *
  187. * Returns the first attribute in the stream matching the specified type.
  188. */
  189. struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
  190. {
  191. const struct nlattr *nla;
  192. int rem;
  193. nla_for_each_attr(nla, head, len, rem)
  194. if (nla_type(nla) == attrtype)
  195. return (struct nlattr *)nla;
  196. return NULL;
  197. }
  198. /**
  199. * nla_strlcpy - Copy string attribute payload into a sized buffer
  200. * @dst: where to copy the string to
  201. * @nla: attribute to copy the string from
  202. * @dstsize: size of destination buffer
  203. *
  204. * Copies at most dstsize - 1 bytes into the destination buffer.
  205. * The result is always a valid NUL-terminated string. Unlike
  206. * strlcpy the destination buffer is always padded out.
  207. *
  208. * Returns the length of the source buffer.
  209. */
  210. size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
  211. {
  212. size_t srclen = nla_len(nla);
  213. char *src = nla_data(nla);
  214. if (srclen > 0 && src[srclen - 1] == '\0')
  215. srclen--;
  216. if (dstsize > 0) {
  217. size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
  218. memset(dst, 0, dstsize);
  219. memcpy(dst, src, len);
  220. }
  221. return srclen;
  222. }
  223. /**
  224. * nla_memcpy - Copy a netlink attribute into another memory area
  225. * @dest: where to copy to memcpy
  226. * @src: netlink attribute to copy from
  227. * @count: size of the destination area
  228. *
  229. * Note: The number of bytes copied is limited by the length of
  230. * attribute's payload. memcpy
  231. *
  232. * Returns the number of bytes copied.
  233. */
  234. int nla_memcpy(void *dest, const struct nlattr *src, int count)
  235. {
  236. int minlen = min_t(int, count, nla_len(src));
  237. memcpy(dest, nla_data(src), minlen);
  238. return minlen;
  239. }
  240. /**
  241. * nla_memcmp - Compare an attribute with sized memory area
  242. * @nla: netlink attribute
  243. * @data: memory area
  244. * @size: size of memory area
  245. */
  246. int nla_memcmp(const struct nlattr *nla, const void *data,
  247. size_t size)
  248. {
  249. int d = nla_len(nla) - size;
  250. if (d == 0)
  251. d = memcmp(nla_data(nla), data, size);
  252. return d;
  253. }
  254. /**
  255. * nla_strcmp - Compare a string attribute against a string
  256. * @nla: netlink string attribute
  257. * @str: another string
  258. */
  259. int nla_strcmp(const struct nlattr *nla, const char *str)
  260. {
  261. int len = strlen(str) + 1;
  262. int d = nla_len(nla) - len;
  263. if (d == 0)
  264. d = memcmp(nla_data(nla), str, len);
  265. return d;
  266. }
  267. #ifdef CONFIG_NET
  268. /**
  269. * __nla_reserve - reserve room for attribute on the skb
  270. * @skb: socket buffer to reserve room on
  271. * @attrtype: attribute type
  272. * @attrlen: length of attribute payload
  273. *
  274. * Adds a netlink attribute header to a socket buffer and reserves
  275. * room for the payload but does not copy it.
  276. *
  277. * The caller is responsible to ensure that the skb provides enough
  278. * tailroom for the attribute header and payload.
  279. */
  280. struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
  281. {
  282. struct nlattr *nla;
  283. nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
  284. nla->nla_type = attrtype;
  285. nla->nla_len = nla_attr_size(attrlen);
  286. memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
  287. return nla;
  288. }
  289. EXPORT_SYMBOL(__nla_reserve);
  290. /**
  291. * __nla_reserve_nohdr - reserve room for attribute without header
  292. * @skb: socket buffer to reserve room on
  293. * @attrlen: length of attribute payload
  294. *
  295. * Reserves room for attribute payload without a header.
  296. *
  297. * The caller is responsible to ensure that the skb provides enough
  298. * tailroom for the payload.
  299. */
  300. void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
  301. {
  302. void *start;
  303. start = skb_put(skb, NLA_ALIGN(attrlen));
  304. memset(start, 0, NLA_ALIGN(attrlen));
  305. return start;
  306. }
  307. EXPORT_SYMBOL(__nla_reserve_nohdr);
  308. /**
  309. * nla_reserve - reserve room for attribute on the skb
  310. * @skb: socket buffer to reserve room on
  311. * @attrtype: attribute type
  312. * @attrlen: length of attribute payload
  313. *
  314. * Adds a netlink attribute header to a socket buffer and reserves
  315. * room for the payload but does not copy it.
  316. *
  317. * Returns NULL if the tailroom of the skb is insufficient to store
  318. * the attribute header and payload.
  319. */
  320. struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
  321. {
  322. if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
  323. return NULL;
  324. return __nla_reserve(skb, attrtype, attrlen);
  325. }
  326. EXPORT_SYMBOL(nla_reserve);
  327. /**
  328. * nla_reserve_nohdr - reserve room for attribute without header
  329. * @skb: socket buffer to reserve room on
  330. * @attrlen: length of attribute payload
  331. *
  332. * Reserves room for attribute payload without a header.
  333. *
  334. * Returns NULL if the tailroom of the skb is insufficient to store
  335. * the attribute payload.
  336. */
  337. void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
  338. {
  339. if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
  340. return NULL;
  341. return __nla_reserve_nohdr(skb, attrlen);
  342. }
  343. EXPORT_SYMBOL(nla_reserve_nohdr);
  344. /**
  345. * __nla_put - Add a netlink attribute to a socket buffer
  346. * @skb: socket buffer to add attribute to
  347. * @attrtype: attribute type
  348. * @attrlen: length of attribute payload
  349. * @data: head of attribute payload
  350. *
  351. * The caller is responsible to ensure that the skb provides enough
  352. * tailroom for the attribute header and payload.
  353. */
  354. void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
  355. const void *data)
  356. {
  357. struct nlattr *nla;
  358. nla = __nla_reserve(skb, attrtype, attrlen);
  359. memcpy(nla_data(nla), data, attrlen);
  360. }
  361. EXPORT_SYMBOL(__nla_put);
  362. /**
  363. * __nla_put_nohdr - Add a netlink attribute without header
  364. * @skb: socket buffer to add attribute to
  365. * @attrlen: length of attribute payload
  366. * @data: head of attribute payload
  367. *
  368. * The caller is responsible to ensure that the skb provides enough
  369. * tailroom for the attribute payload.
  370. */
  371. void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
  372. {
  373. void *start;
  374. start = __nla_reserve_nohdr(skb, attrlen);
  375. memcpy(start, data, attrlen);
  376. }
  377. EXPORT_SYMBOL(__nla_put_nohdr);
  378. /**
  379. * nla_put - Add a netlink attribute to a socket buffer
  380. * @skb: socket buffer to add attribute to
  381. * @attrtype: attribute type
  382. * @attrlen: length of attribute payload
  383. * @data: head of attribute payload
  384. *
  385. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  386. * the attribute header and payload.
  387. */
  388. int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
  389. {
  390. if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
  391. return -EMSGSIZE;
  392. __nla_put(skb, attrtype, attrlen, data);
  393. return 0;
  394. }
  395. EXPORT_SYMBOL(nla_put);
  396. /**
  397. * nla_put_nohdr - Add a netlink attribute without header
  398. * @skb: socket buffer to add attribute to
  399. * @attrlen: length of attribute payload
  400. * @data: head of attribute payload
  401. *
  402. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  403. * the attribute payload.
  404. */
  405. int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
  406. {
  407. if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
  408. return -EMSGSIZE;
  409. __nla_put_nohdr(skb, attrlen, data);
  410. return 0;
  411. }
  412. EXPORT_SYMBOL(nla_put_nohdr);
  413. /**
  414. * nla_append - Add a netlink attribute without header or padding
  415. * @skb: socket buffer to add attribute to
  416. * @attrlen: length of attribute payload
  417. * @data: head of attribute payload
  418. *
  419. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  420. * the attribute payload.
  421. */
  422. int nla_append(struct sk_buff *skb, int attrlen, const void *data)
  423. {
  424. if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
  425. return -EMSGSIZE;
  426. memcpy(skb_put(skb, attrlen), data, attrlen);
  427. return 0;
  428. }
  429. EXPORT_SYMBOL(nla_append);
  430. #endif
  431. EXPORT_SYMBOL(nla_validate);
  432. EXPORT_SYMBOL(nla_policy_len);
  433. EXPORT_SYMBOL(nla_parse);
  434. EXPORT_SYMBOL(nla_find);
  435. EXPORT_SYMBOL(nla_strlcpy);
  436. EXPORT_SYMBOL(nla_memcpy);
  437. EXPORT_SYMBOL(nla_memcmp);
  438. EXPORT_SYMBOL(nla_strcmp);