netlink.h 25 KB

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