PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/user/libgenl.c

https://bitbucket.org/cloudfounders/drbd-8.4
C | 847 lines | 520 code | 114 blank | 213 comment | 114 complexity | 07697f438916eaa7ecf6dad021f71510 MD5 | raw file
  1. #include "libgenl.h"
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <time.h>
  5. #include <poll.h>
  6. int genl_join_mc_group(struct genl_sock *s, const char *name) {
  7. int g_id;
  8. int i;
  9. BUG_ON(!s || !s->s_family);
  10. for (i = 0; i < 32; i++) {
  11. if (!s->s_family->mc_groups[i].id)
  12. continue;
  13. if (strcmp(s->s_family->mc_groups[i].name, name))
  14. continue;
  15. g_id = s->s_family->mc_groups[i].id;
  16. return setsockopt(s->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
  17. &g_id, sizeof(g_id));
  18. }
  19. return -2;
  20. }
  21. #define DO_OR_LOG_AND_FAIL(x) \
  22. do { \
  23. int err = x; \
  24. if (err) { \
  25. dbg(1, "%s failed: %d %s\n", \
  26. #x, err, strerror(errno)); \
  27. goto fail; \
  28. } \
  29. } while(0)
  30. static struct genl_sock *genl_connect(__u32 nl_groups)
  31. {
  32. struct genl_sock *s = calloc(1, sizeof(*s));
  33. socklen_t sock_len;
  34. int bsz = 2 << 10;
  35. if (!s)
  36. return NULL;
  37. /* autobind; kernel is responsible to give us something unique
  38. * in bind() below. */
  39. s->s_local.nl_pid = 0;
  40. s->s_local.nl_family = AF_NETLINK;
  41. /*
  42. * If we want to receive multicast traffic on this socket, kernels
  43. * before v2.6.23-rc1 require us to indicate which multicast groups we
  44. * are interested in in nl_groups.
  45. */
  46. s->s_local.nl_groups = nl_groups;
  47. s->s_peer.nl_family = AF_NETLINK;
  48. /* start with some sane sequence number */
  49. s->s_seq_expect = s->s_seq_next = time(0);
  50. s->s_fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_GENERIC);
  51. if (s->s_fd == -1)
  52. goto fail;
  53. sock_len = sizeof(s->s_local);
  54. DO_OR_LOG_AND_FAIL(setsockopt(s->s_fd, SOL_SOCKET, SO_SNDBUF, &bsz, sizeof(bsz)));
  55. DO_OR_LOG_AND_FAIL(setsockopt(s->s_fd, SOL_SOCKET, SO_RCVBUF, &bsz, sizeof(bsz)));
  56. DO_OR_LOG_AND_FAIL(bind(s->s_fd, (struct sockaddr*) &s->s_local, sizeof(s->s_local)));
  57. DO_OR_LOG_AND_FAIL(getsockname(s->s_fd, (struct sockaddr*) &s->s_local, &sock_len));
  58. dbg(3, "bound socket to nl_pid:%u, my pid:%u, len:%u, sizeof:%u\n",
  59. s->s_local.nl_pid, getpid(),
  60. (unsigned)sock_len, (unsigned)sizeof(s->s_local));
  61. return s;
  62. fail:
  63. free(s);
  64. return NULL;
  65. }
  66. #undef DO_OR_LOG_AND_FAIL
  67. static int do_send(int fd, const void *buf, int len)
  68. {
  69. int c;
  70. while ((c = write(fd, buf, len)) < len) {
  71. if (c == -1) {
  72. if (errno == EINTR)
  73. continue;
  74. return -1;
  75. }
  76. buf += c;
  77. len -= c;
  78. }
  79. return 0;
  80. }
  81. int genl_send(struct genl_sock *s, struct msg_buff *msg)
  82. {
  83. struct nlmsghdr *n = (struct nlmsghdr *)msg->data;
  84. struct genlmsghdr *g;
  85. n->nlmsg_len = msg->tail - msg->data;
  86. n->nlmsg_flags |= NLM_F_REQUEST;
  87. n->nlmsg_seq = s->s_seq_expect = s->s_seq_next++;
  88. n->nlmsg_pid = s->s_local.nl_pid;
  89. g = nlmsg_data(n);
  90. dbg(3, "sending %smessage, pid:%u seq:%u, g.cmd/version:%u/%u",
  91. n->nlmsg_type == GENL_ID_CTRL ? "ctrl " : "",
  92. n->nlmsg_pid, n->nlmsg_seq, g->cmd, g->version);
  93. return do_send(s->s_fd, msg->data, n->nlmsg_len);
  94. }
  95. /* "inspired" by libnl nl_recv()
  96. * You pass in one iovec, which may contain pre-allocated buffer space,
  97. * obtained by malloc(). It will be realloc()ed on demand.
  98. * Caller is responsible for free()ing it up on return,
  99. * regardless of return code.
  100. */
  101. int genl_recv_timeout(struct genl_sock *s, struct iovec *iov, int timeout_ms)
  102. {
  103. struct sockaddr_nl addr;
  104. struct pollfd pfd;
  105. int flags;
  106. struct msghdr msg = {
  107. .msg_name = &addr,
  108. .msg_namelen = sizeof(struct sockaddr_nl),
  109. .msg_iov = iov,
  110. .msg_iovlen = 1,
  111. .msg_control = NULL,
  112. .msg_controllen = 0,
  113. .msg_flags = 0,
  114. };
  115. int n;
  116. if (!iov->iov_len) {
  117. iov->iov_len = 8192;
  118. iov->iov_base = malloc(iov->iov_len);
  119. }
  120. flags = MSG_PEEK;
  121. retry:
  122. pfd.fd = s->s_fd;
  123. pfd.events = POLLIN;
  124. if ((poll(&pfd, 1, timeout_ms) != 1) || !(pfd.revents & POLLIN))
  125. return 0; /* which is E_RCV_TIMEDOUT */
  126. /* for most cases this method will memcopy twice, as the default buffer
  127. * is large enough. But for those few other cases, we now have a
  128. * chance to realloc before the rest of the datagram is discarded.
  129. */
  130. n = recvmsg(s->s_fd, &msg, flags);
  131. if (!n)
  132. return 0;
  133. else if (n < 0) {
  134. if (errno == EINTR) {
  135. dbg(3, "recvmsg() returned EINTR, retrying\n");
  136. goto retry;
  137. } else if (errno == EAGAIN) {
  138. dbg(3, "recvmsg() returned EAGAIN, aborting\n");
  139. return 0;
  140. } else
  141. return -E_RCV_FAILED;
  142. }
  143. if (iov->iov_len < (unsigned)n ||
  144. msg.msg_flags & MSG_TRUNC) {
  145. /* Provided buffer is not long enough, enlarge it
  146. * and try again. */
  147. iov->iov_len *= 2;
  148. iov->iov_base = realloc(iov->iov_base, iov->iov_len);
  149. goto retry;
  150. } else if (flags != 0) {
  151. /* Buffer is big enough, do the actual reading */
  152. flags = 0;
  153. goto retry;
  154. }
  155. if (msg.msg_namelen != sizeof(struct sockaddr_nl))
  156. return -E_RCV_NO_SOURCE_ADDR;
  157. if (addr.nl_pid != 0) {
  158. dbg(3, "ignoring message from sender pid %u != 0\n",
  159. addr.nl_pid);
  160. goto retry;
  161. }
  162. return n;
  163. }
  164. /* Note that one datagram may contain multiple netlink messages
  165. * (e.g. for a dump response). This only checks the _first_ message,
  166. * caller has to iterate over multiple messages with nlmsg_for_each_msg()
  167. * when necessary. */
  168. int genl_recv_msgs(struct genl_sock *s, struct iovec *iov, char **err_desc, int timeout_ms)
  169. {
  170. struct nlmsghdr *nlh;
  171. int c = genl_recv_timeout(s, iov, timeout_ms);
  172. if (c <= 0) {
  173. if (err_desc)
  174. *err_desc = (c == -E_RCV_TIMEDOUT)
  175. ? "timed out waiting for reply"
  176. : (c == -E_RCV_NO_SOURCE_ADDR)
  177. ? "no source address!"
  178. : "failed to receive netlink reply";
  179. return c;
  180. }
  181. nlh = (struct nlmsghdr*)iov->iov_base;
  182. if (!nlmsg_ok(nlh, c)) {
  183. if (err_desc)
  184. *err_desc = "truncated message in netlink reply";
  185. return -E_RCV_MSG_TRUNC;
  186. }
  187. if (s->s_seq_expect && nlh->nlmsg_seq != s->s_seq_expect) {
  188. if (err_desc)
  189. *err_desc = "sequence mismatch in netlink reply";
  190. return -E_RCV_SEQ_MISMATCH;
  191. }
  192. if (nlh->nlmsg_type == NLMSG_NOOP ||
  193. nlh->nlmsg_type == NLMSG_OVERRUN) {
  194. if (err_desc)
  195. *err_desc = "unexpected message type in reply";
  196. return -E_RCV_UNEXPECTED_TYPE;
  197. }
  198. if (nlh->nlmsg_type == NLMSG_DONE)
  199. return -E_RCV_NLMSG_DONE;
  200. if (nlh->nlmsg_type == NLMSG_ERROR) {
  201. struct nlmsgerr *e = nlmsg_data(nlh);
  202. errno = -e->error;
  203. if (!errno)
  204. /* happens if you request NLM_F_ACK */
  205. dbg(3, "got a positive ACK message for seq:%u",
  206. s->s_seq_expect);
  207. else {
  208. dbg(3, "got a NACK message for seq:%u, error:%d",
  209. s->s_seq_expect, e->error);
  210. if (err_desc)
  211. *err_desc = strerror(errno);
  212. }
  213. return -E_RCV_ERROR_REPLY;
  214. }
  215. /* good reply message(s) */
  216. dbg(3, "received a good message for seq:%u", s->s_seq_expect);
  217. return c;
  218. }
  219. static struct genl_family genl_ctrl = {
  220. .id = GENL_ID_CTRL,
  221. .name = "nlctrl",
  222. .version = 0x2,
  223. .maxattr = CTRL_ATTR_MAX,
  224. };
  225. struct genl_sock *genl_connect_to_family(struct genl_family *family)
  226. {
  227. struct genl_sock *s = NULL;
  228. struct msg_buff *msg;
  229. struct nlmsghdr *nlh;
  230. struct nlattr *nla;
  231. struct iovec iov = { .iov_len = 0 };
  232. int rem;
  233. BUG_ON(!family);
  234. BUG_ON(!strlen(family->name));
  235. msg = msg_new(DEFAULT_MSG_SIZE);
  236. if (!msg) {
  237. dbg(1, "could not allocate genl message");
  238. goto out;
  239. }
  240. s = genl_connect(family->nl_groups);
  241. if (!s) {
  242. dbg(1, "error creating netlink socket");
  243. goto out;
  244. }
  245. genlmsg_put(msg, &genl_ctrl, 0, CTRL_CMD_GETFAMILY);
  246. nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family->name);
  247. if (genl_send(s, msg)) {
  248. dbg(1, "failed to send netlink message");
  249. free(s);
  250. s = NULL;
  251. goto out;
  252. }
  253. if (genl_recv_msgs(s, &iov, NULL, 3000) <= 0) {
  254. close(s->s_fd);
  255. free(s);
  256. s = NULL;
  257. goto out;
  258. }
  259. nlh = (struct nlmsghdr*)iov.iov_base;
  260. nla_for_each_attr(nla, nlmsg_attrdata(nlh, GENL_HDRLEN),
  261. nlmsg_attrlen(nlh, GENL_HDRLEN), rem) {
  262. switch (nla_type(nla)) {
  263. case CTRL_ATTR_FAMILY_ID:
  264. family->id = nla_get_u16(nla);
  265. dbg(2, "'%s' genl family id: %d", family->name, family->id);
  266. break;
  267. case CTRL_ATTR_FAMILY_NAME:
  268. break;
  269. #ifdef HAVE_CTRL_ATTR_VERSION
  270. case CTRL_ATTR_VERSION:
  271. family->version = nla_get_u32(nla);
  272. dbg(2, "'%s' genl family version: %d", family->name, family->version);
  273. break;
  274. #endif
  275. #ifdef HAVE_CTRL_ATTR_HDRSIZE
  276. case CTRL_ATTR_HDRSIZE:
  277. family->hdrsize = nla_get_u32(nla);
  278. dbg(2, "'%s' genl family hdrsize: %d", family->name, family->hdrsize);
  279. break;
  280. #endif
  281. #ifdef HAVE_CTRL_ATTR_MCAST_GROUPS
  282. case CTRL_ATTR_MCAST_GROUPS:
  283. {
  284. static struct nla_policy policy[] = {
  285. [CTRL_ATTR_MCAST_GRP_NAME] = { .type = NLA_NUL_STRING, .len = GENL_NAMSIZ },
  286. [CTRL_ATTR_MCAST_GRP_ID] = { .type = NLA_U32 },
  287. };
  288. struct nlattr *ntb[__CTRL_ATTR_MCAST_GRP_MAX];
  289. struct nlattr *idx;
  290. int tmp;
  291. int i = 0;
  292. nla_for_each_nested(idx, nla, tmp) {
  293. BUG_ON(i >= 32);
  294. nla_parse_nested(ntb, CTRL_ATTR_MCAST_GRP_MAX, idx, policy);
  295. if (ntb[CTRL_ATTR_MCAST_GRP_NAME] &&
  296. ntb[CTRL_ATTR_MCAST_GRP_ID]) {
  297. struct genl_multicast_group *grp = &family->mc_groups[i++];
  298. grp->id = nla_get_u32(ntb[CTRL_ATTR_MCAST_GRP_ID]);
  299. nla_strlcpy(grp->name, ntb[CTRL_ATTR_MCAST_GRP_NAME],
  300. sizeof(grp->name));
  301. dbg(2, "'%s'-'%s' multicast group found (id: %u)\n",
  302. family->name, grp->name, grp->id);
  303. }
  304. }
  305. break;
  306. };
  307. #endif
  308. default: ;
  309. }
  310. }
  311. if (!family->id)
  312. dbg(1, "genl family '%s' not found", family->name);
  313. else
  314. s->s_family = family;
  315. out:
  316. free(iov.iov_base);
  317. msg_free(msg);
  318. return s;
  319. }
  320. /*
  321. * Stripped down copy from linux-2.6.32/lib/nlattr.c
  322. * skb -> "msg_buff"
  323. * - Lars Ellenberg
  324. *
  325. * NETLINK Netlink attributes
  326. *
  327. * Authors: Thomas Graf <tgraf@suug.ch>
  328. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  329. */
  330. #include <string.h>
  331. #include <linux/types.h>
  332. static __u16 nla_attr_minlen[NLA_TYPE_MAX+1] __read_mostly = {
  333. [NLA_U8] = sizeof(__u8),
  334. [NLA_U16] = sizeof(__u16),
  335. [NLA_U32] = sizeof(__u32),
  336. [NLA_U64] = sizeof(__u64),
  337. [NLA_NESTED] = NLA_HDRLEN,
  338. };
  339. static int validate_nla(struct nlattr *nla, int maxtype,
  340. const struct nla_policy *policy)
  341. {
  342. const struct nla_policy *pt;
  343. int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
  344. if (type <= 0 || type > maxtype)
  345. return 0;
  346. pt = &policy[type];
  347. BUG_ON(pt->type > NLA_TYPE_MAX);
  348. switch (pt->type) {
  349. case NLA_FLAG:
  350. if (attrlen > 0)
  351. return -ERANGE;
  352. break;
  353. case NLA_NUL_STRING:
  354. if (pt->len)
  355. minlen = min_t(int, attrlen, pt->len + 1);
  356. else
  357. minlen = attrlen;
  358. if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
  359. return -EINVAL;
  360. /* fall through */
  361. case NLA_STRING:
  362. if (attrlen < 1)
  363. return -ERANGE;
  364. if (pt->len) {
  365. char *buf = nla_data(nla);
  366. if (buf[attrlen - 1] == '\0')
  367. attrlen--;
  368. if (attrlen > pt->len)
  369. return -ERANGE;
  370. }
  371. break;
  372. case NLA_BINARY:
  373. if (pt->len && attrlen > pt->len)
  374. return -ERANGE;
  375. break;
  376. case NLA_NESTED_COMPAT:
  377. if (attrlen < pt->len)
  378. return -ERANGE;
  379. if (attrlen < NLA_ALIGN(pt->len))
  380. break;
  381. if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
  382. return -ERANGE;
  383. nla = nla_data(nla) + NLA_ALIGN(pt->len);
  384. if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
  385. return -ERANGE;
  386. break;
  387. case NLA_NESTED:
  388. /* a nested attributes is allowed to be empty; if its not,
  389. * it must have a size of at least NLA_HDRLEN.
  390. */
  391. if (attrlen == 0)
  392. break;
  393. default:
  394. if (pt->len)
  395. minlen = pt->len;
  396. else if (pt->type != NLA_UNSPEC)
  397. minlen = nla_attr_minlen[pt->type];
  398. if (attrlen < minlen)
  399. return -ERANGE;
  400. }
  401. return 0;
  402. }
  403. /**
  404. * nla_validate - Validate a stream of attributes
  405. * @head: head of attribute stream
  406. * @len: length of attribute stream
  407. * @maxtype: maximum attribute type to be expected
  408. * @policy: validation policy
  409. *
  410. * Validates all attributes in the specified attribute stream against the
  411. * specified policy. Attributes with a type exceeding maxtype will be
  412. * ignored. See documenation of struct nla_policy for more details.
  413. *
  414. * Returns 0 on success or a negative error code.
  415. */
  416. int nla_validate(struct nlattr *head, int len, int maxtype,
  417. const struct nla_policy *policy)
  418. {
  419. struct nlattr *nla;
  420. int rem, err;
  421. nla_for_each_attr(nla, head, len, rem) {
  422. err = validate_nla(nla, maxtype, policy);
  423. if (err < 0)
  424. goto errout;
  425. }
  426. err = 0;
  427. errout:
  428. return err;
  429. }
  430. /**
  431. * nla_policy_len - Determin the max. length of a policy
  432. * @policy: policy to use
  433. * @n: number of policies
  434. *
  435. * Determines the max. length of the policy. It is currently used
  436. * to allocated Netlink buffers roughly the size of the actual
  437. * message.
  438. *
  439. * Returns 0 on success or a negative error code.
  440. */
  441. int
  442. nla_policy_len(const struct nla_policy *p, int n)
  443. {
  444. int i, len = 0;
  445. for (i = 0; i < n; i++, p++) {
  446. if (p->len)
  447. len += nla_total_size(p->len);
  448. else if (nla_attr_minlen[p->type])
  449. len += nla_total_size(nla_attr_minlen[p->type]);
  450. }
  451. return len;
  452. }
  453. /**
  454. * nla_parse - Parse a stream of attributes into a tb buffer
  455. * @tb: destination array with maxtype+1 elements
  456. * @maxtype: maximum attribute type to be expected
  457. * @head: head of attribute stream
  458. * @len: length of attribute stream
  459. * @policy: validation policy
  460. *
  461. * Parses a stream of attributes and stores a pointer to each attribute in
  462. * the tb array accessable via the attribute type. Attributes with a type
  463. * exceeding maxtype will be silently ignored for backwards compatibility
  464. * reasons. policy may be set to NULL if no validation is required.
  465. *
  466. * Returns 0 on success or a negative error code.
  467. */
  468. int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
  469. const struct nla_policy *policy)
  470. {
  471. struct nlattr *nla;
  472. int rem, err;
  473. memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
  474. nla_for_each_attr(nla, head, len, rem) {
  475. __u16 type = nla_type(nla);
  476. if (type > 0 && type <= maxtype) {
  477. if (policy) {
  478. err = validate_nla(nla, maxtype, policy);
  479. if (err < 0)
  480. goto errout;
  481. }
  482. tb[type] = nla;
  483. }
  484. }
  485. if (unlikely(rem > 0))
  486. dbg(1, "netlink: %d bytes leftover after parsing "
  487. "attributes.\n", rem);
  488. err = 0;
  489. errout:
  490. if (err)
  491. dbg(1, "netlink: policy violation t:%d[%x] e:%d\n",
  492. nla_type(nla), nla->nla_type, err);
  493. return err;
  494. }
  495. /**
  496. * nla_find - Find a specific attribute in a stream of attributes
  497. * @head: head of attribute stream
  498. * @len: length of attribute stream
  499. * @attrtype: type of attribute to look for
  500. *
  501. * Returns the first attribute in the stream matching the specified type.
  502. */
  503. struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
  504. {
  505. struct nlattr *nla;
  506. int rem;
  507. nla_for_each_attr(nla, head, len, rem)
  508. if (nla_type(nla) == attrtype)
  509. return nla;
  510. return NULL;
  511. }
  512. /**
  513. * nla_strlcpy - Copy string attribute payload into a sized buffer
  514. * @dst: where to copy the string to
  515. * @nla: attribute to copy the string from
  516. * @dstsize: size of destination buffer
  517. *
  518. * Copies at most dstsize - 1 bytes into the destination buffer.
  519. * The result is always a valid NUL-terminated string. Unlike
  520. * strlcpy the destination buffer is always padded out.
  521. *
  522. * Returns the length of the source buffer.
  523. */
  524. size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
  525. {
  526. size_t srclen = nla_len(nla);
  527. char *src = nla_data(nla);
  528. if (srclen > 0 && src[srclen - 1] == '\0')
  529. srclen--;
  530. if (dstsize > 0) {
  531. size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
  532. memset(dst, 0, dstsize);
  533. memcpy(dst, src, len);
  534. }
  535. return srclen;
  536. }
  537. /**
  538. * nla_memcpy - Copy a netlink attribute into another memory area
  539. * @dest: where to copy to memcpy
  540. * @src: netlink attribute to copy from
  541. * @count: size of the destination area
  542. *
  543. * Note: The number of bytes copied is limited by the length of
  544. * attribute's payload. memcpy
  545. *
  546. * Returns the number of bytes copied.
  547. */
  548. int nla_memcpy(void *dest, const struct nlattr *src, int count)
  549. {
  550. int minlen = min_t(int, count, nla_len(src));
  551. memcpy(dest, nla_data(src), minlen);
  552. return minlen;
  553. }
  554. /**
  555. * nla_memcmp - Compare an attribute with sized memory area
  556. * @nla: netlink attribute
  557. * @data: memory area
  558. * @size: size of memory area
  559. */
  560. int nla_memcmp(const struct nlattr *nla, const void *data,
  561. size_t size)
  562. {
  563. int d = nla_len(nla) - size;
  564. if (d == 0)
  565. d = memcmp(nla_data(nla), data, size);
  566. return d;
  567. }
  568. /**
  569. * nla_strcmp - Compare a string attribute against a string
  570. * @nla: netlink string attribute
  571. * @str: another string
  572. */
  573. int nla_strcmp(const struct nlattr *nla, const char *str)
  574. {
  575. int len = strlen(str) + 1;
  576. int d = nla_len(nla) - len;
  577. if (d == 0)
  578. d = memcmp(nla_data(nla), str, len);
  579. return d;
  580. }
  581. /**
  582. * __nla_reserve - reserve room for attribute on the msg
  583. * @msg: message buffer to reserve room on
  584. * @attrtype: attribute type
  585. * @attrlen: length of attribute payload
  586. *
  587. * Adds a netlink attribute header to a message buffer and reserves
  588. * room for the payload but does not copy it.
  589. *
  590. * The caller is responsible to ensure that the msg provides enough
  591. * tailroom for the attribute header and payload.
  592. */
  593. struct nlattr *__nla_reserve(struct msg_buff *msg, int attrtype, int attrlen)
  594. {
  595. struct nlattr *nla;
  596. nla = (struct nlattr *) msg_put(msg, nla_total_size(attrlen));
  597. nla->nla_type = attrtype;
  598. nla->nla_len = nla_attr_size(attrlen);
  599. memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
  600. return nla;
  601. }
  602. /**
  603. * __nla_reserve_nohdr - reserve room for attribute without header
  604. * @msg: message buffer to reserve room on
  605. * @attrlen: length of attribute payload
  606. *
  607. * Reserves room for attribute payload without a header.
  608. *
  609. * The caller is responsible to ensure that the msg provides enough
  610. * tailroom for the payload.
  611. */
  612. void *__nla_reserve_nohdr(struct msg_buff *msg, int attrlen)
  613. {
  614. void *start;
  615. start = msg_put(msg, NLA_ALIGN(attrlen));
  616. memset(start, 0, NLA_ALIGN(attrlen));
  617. return start;
  618. }
  619. /**
  620. * nla_reserve - reserve room for attribute on the msg
  621. * @msg: message buffer to reserve room on
  622. * @attrtype: attribute type
  623. * @attrlen: length of attribute payload
  624. *
  625. * Adds a netlink attribute header to a message buffer and reserves
  626. * room for the payload but does not copy it.
  627. *
  628. * Returns NULL if the tailroom of the msg is insufficient to store
  629. * the attribute header and payload.
  630. */
  631. struct nlattr *nla_reserve(struct msg_buff *msg, int attrtype, int attrlen)
  632. {
  633. if (unlikely(msg_tailroom(msg) < nla_total_size(attrlen)))
  634. return NULL;
  635. return __nla_reserve(msg, attrtype, attrlen);
  636. }
  637. /**
  638. * nla_reserve_nohdr - reserve room for attribute without header
  639. * @msg: message buffer to reserve room on
  640. * @attrlen: length of attribute payload
  641. *
  642. * Reserves room for attribute payload without a header.
  643. *
  644. * Returns NULL if the tailroom of the msg is insufficient to store
  645. * the attribute payload.
  646. */
  647. void *nla_reserve_nohdr(struct msg_buff *msg, int attrlen)
  648. {
  649. if (unlikely(msg_tailroom(msg) < NLA_ALIGN(attrlen)))
  650. return NULL;
  651. return __nla_reserve_nohdr(msg, attrlen);
  652. }
  653. /**
  654. * __nla_put - Add a netlink attribute to a message buffer
  655. * @msg: message buffer to add attribute to
  656. * @attrtype: attribute type
  657. * @attrlen: length of attribute payload
  658. * @data: head of attribute payload
  659. *
  660. * The caller is responsible to ensure that the msg provides enough
  661. * tailroom for the attribute header and payload.
  662. */
  663. void __nla_put(struct msg_buff *msg, int attrtype, int attrlen,
  664. const void *data)
  665. {
  666. struct nlattr *nla;
  667. nla = __nla_reserve(msg, attrtype, attrlen);
  668. memcpy(nla_data(nla), data, attrlen);
  669. }
  670. /**
  671. * __nla_put_nohdr - Add a netlink attribute without header
  672. * @msg: message buffer to add attribute to
  673. * @attrlen: length of attribute payload
  674. * @data: head of attribute payload
  675. *
  676. * The caller is responsible to ensure that the msg provides enough
  677. * tailroom for the attribute payload.
  678. */
  679. void __nla_put_nohdr(struct msg_buff *msg, int attrlen, const void *data)
  680. {
  681. void *start;
  682. start = __nla_reserve_nohdr(msg, attrlen);
  683. memcpy(start, data, attrlen);
  684. }
  685. /**
  686. * nla_put - Add a netlink attribute to a message buffer
  687. * @msg: message buffer to add attribute to
  688. * @attrtype: attribute type
  689. * @attrlen: length of attribute payload
  690. * @data: head of attribute payload
  691. *
  692. * Returns -EMSGSIZE if the tailroom of the msg is insufficient to store
  693. * the attribute header and payload.
  694. */
  695. int nla_put(struct msg_buff *msg, int attrtype, int attrlen, const void *data)
  696. {
  697. if (unlikely(msg_tailroom(msg) < nla_total_size(attrlen)))
  698. return -EMSGSIZE;
  699. __nla_put(msg, attrtype, attrlen, data);
  700. return 0;
  701. }
  702. /**
  703. * nla_put_nohdr - Add a netlink attribute without header
  704. * @msg: message buffer to add attribute to
  705. * @attrlen: length of attribute payload
  706. * @data: head of attribute payload
  707. *
  708. * Returns -EMSGSIZE if the tailroom of the msg is insufficient to store
  709. * the attribute payload.
  710. */
  711. int nla_put_nohdr(struct msg_buff *msg, int attrlen, const void *data)
  712. {
  713. if (unlikely(msg_tailroom(msg) < NLA_ALIGN(attrlen)))
  714. return -EMSGSIZE;
  715. __nla_put_nohdr(msg, attrlen, data);
  716. return 0;
  717. }
  718. /**
  719. * nla_append - Add a netlink attribute without header or padding
  720. * @msg: message buffer to add attribute to
  721. * @attrlen: length of attribute payload
  722. * @data: head of attribute payload
  723. *
  724. * Returns -EMSGSIZE if the tailroom of the msg is insufficient to store
  725. * the attribute payload.
  726. */
  727. int nla_append(struct msg_buff *msg, int attrlen, const void *data)
  728. {
  729. if (unlikely(msg_tailroom(msg) < NLA_ALIGN(attrlen)))
  730. return -EMSGSIZE;
  731. memcpy(msg_put(msg, attrlen), data, attrlen);
  732. return 0;
  733. }