netlink.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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 int nla_validate(struct nlattr *head, int len, int maxtype,
  181. struct nla_policy *policy);
  182. extern int nla_parse(struct nlattr *tb[], int maxtype,
  183. struct nlattr *head, int len,
  184. struct nla_policy *policy);
  185. extern struct nlattr * nla_find(struct nlattr *head, int len, int attrtype);
  186. extern size_t nla_strlcpy(char *dst, const struct nlattr *nla,
  187. size_t dstsize);
  188. extern int nla_memcpy(void *dest, struct nlattr *src, int count);
  189. extern int nla_memcmp(const struct nlattr *nla, const void *data,
  190. size_t size);
  191. extern int nla_strcmp(const struct nlattr *nla, const char *str);
  192. extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype,
  193. int attrlen);
  194. extern struct nlattr * nla_reserve(struct sk_buff *skb, int attrtype,
  195. int attrlen);
  196. extern void __nla_put(struct sk_buff *skb, int attrtype,
  197. int attrlen, const void *data);
  198. extern int nla_put(struct sk_buff *skb, int attrtype,
  199. int attrlen, const void *data);
  200. /**************************************************************************
  201. * Netlink Messages
  202. **************************************************************************/
  203. /**
  204. * nlmsg_msg_size - length of netlink message not including padding
  205. * @payload: length of message payload
  206. */
  207. static inline int nlmsg_msg_size(int payload)
  208. {
  209. return NLMSG_HDRLEN + payload;
  210. }
  211. /**
  212. * nlmsg_total_size - length of netlink message including padding
  213. * @payload: length of message payload
  214. */
  215. static inline int nlmsg_total_size(int payload)
  216. {
  217. return NLMSG_ALIGN(nlmsg_msg_size(payload));
  218. }
  219. /**
  220. * nlmsg_padlen - length of padding at the message's tail
  221. * @payload: length of message payload
  222. */
  223. static inline int nlmsg_padlen(int payload)
  224. {
  225. return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
  226. }
  227. /**
  228. * nlmsg_data - head of message payload
  229. * @nlh: netlink messsage header
  230. */
  231. static inline void *nlmsg_data(const struct nlmsghdr *nlh)
  232. {
  233. return (unsigned char *) nlh + NLMSG_HDRLEN;
  234. }
  235. /**
  236. * nlmsg_len - length of message payload
  237. * @nlh: netlink message header
  238. */
  239. static inline int nlmsg_len(const struct nlmsghdr *nlh)
  240. {
  241. return nlh->nlmsg_len - NLMSG_HDRLEN;
  242. }
  243. /**
  244. * nlmsg_attrdata - head of attributes data
  245. * @nlh: netlink message header
  246. * @hdrlen: length of family specific header
  247. */
  248. static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
  249. int hdrlen)
  250. {
  251. unsigned char *data = nlmsg_data(nlh);
  252. return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
  253. }
  254. /**
  255. * nlmsg_attrlen - length of attributes data
  256. * @nlh: netlink message header
  257. * @hdrlen: length of family specific header
  258. */
  259. static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
  260. {
  261. return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
  262. }
  263. /**
  264. * nlmsg_ok - check if the netlink message fits into the remaining bytes
  265. * @nlh: netlink message header
  266. * @remaining: number of bytes remaining in message stream
  267. */
  268. static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
  269. {
  270. return (remaining >= sizeof(struct nlmsghdr) &&
  271. nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
  272. nlh->nlmsg_len <= remaining);
  273. }
  274. /**
  275. * nlmsg_next - next netlink message in message stream
  276. * @nlh: netlink message header
  277. * @remaining: number of bytes remaining in message stream
  278. *
  279. * Returns the next netlink message in the message stream and
  280. * decrements remaining by the size of the current message.
  281. */
  282. static inline struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
  283. {
  284. int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
  285. *remaining -= totlen;
  286. return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
  287. }
  288. /**
  289. * nlmsg_parse - parse attributes of a netlink message
  290. * @nlh: netlink message header
  291. * @hdrlen: length of family specific header
  292. * @tb: destination array with maxtype+1 elements
  293. * @maxtype: maximum attribute type to be expected
  294. * @policy: validation policy
  295. *
  296. * See nla_parse()
  297. */
  298. static inline int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen,
  299. struct nlattr *tb[], int maxtype,
  300. struct nla_policy *policy)
  301. {
  302. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  303. return -EINVAL;
  304. return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
  305. nlmsg_attrlen(nlh, hdrlen), policy);
  306. }
  307. /**
  308. * nlmsg_find_attr - find a specific attribute in a netlink message
  309. * @nlh: netlink message header
  310. * @hdrlen: length of familiy specific header
  311. * @attrtype: type of attribute to look for
  312. *
  313. * Returns the first attribute which matches the specified type.
  314. */
  315. static inline struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh,
  316. int hdrlen, int attrtype)
  317. {
  318. return nla_find(nlmsg_attrdata(nlh, hdrlen),
  319. nlmsg_attrlen(nlh, hdrlen), attrtype);
  320. }
  321. /**
  322. * nlmsg_validate - validate a netlink message including attributes
  323. * @nlh: netlinket message header
  324. * @hdrlen: length of familiy specific header
  325. * @maxtype: maximum attribute type to be expected
  326. * @policy: validation policy
  327. */
  328. static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
  329. struct nla_policy *policy)
  330. {
  331. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  332. return -EINVAL;
  333. return nla_validate(nlmsg_attrdata(nlh, hdrlen),
  334. nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
  335. }
  336. /**
  337. * nlmsg_for_each_attr - iterate over a stream of attributes
  338. * @pos: loop counter, set to current attribute
  339. * @nlh: netlink message header
  340. * @hdrlen: length of familiy specific header
  341. * @rem: initialized to len, holds bytes currently remaining in stream
  342. */
  343. #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
  344. nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
  345. nlmsg_attrlen(nlh, hdrlen), rem)
  346. #if 0
  347. /* FIXME: Enable once all users have been converted */
  348. /**
  349. * __nlmsg_put - Add a new netlink message to an skb
  350. * @skb: socket buffer to store message in
  351. * @pid: netlink process id
  352. * @seq: sequence number of message
  353. * @type: message type
  354. * @payload: length of message payload
  355. * @flags: message flags
  356. *
  357. * The caller is responsible to ensure that the skb provides enough
  358. * tailroom for both the netlink header and payload.
  359. */
  360. static inline struct nlmsghdr *__nlmsg_put(struct sk_buff *skb, u32 pid,
  361. u32 seq, int type, int payload,
  362. int flags)
  363. {
  364. struct nlmsghdr *nlh;
  365. nlh = (struct nlmsghdr *) skb_put(skb, nlmsg_total_size(payload));
  366. nlh->nlmsg_type = type;
  367. nlh->nlmsg_len = nlmsg_msg_size(payload);
  368. nlh->nlmsg_flags = flags;
  369. nlh->nlmsg_pid = pid;
  370. nlh->nlmsg_seq = seq;
  371. memset((unsigned char *) nlmsg_data(nlh) + payload, 0,
  372. nlmsg_padlen(payload));
  373. return nlh;
  374. }
  375. #endif
  376. /**
  377. * nlmsg_put - Add a new netlink message to an skb
  378. * @skb: socket buffer to store message in
  379. * @pid: netlink process id
  380. * @seq: sequence number of message
  381. * @type: message type
  382. * @payload: length of message payload
  383. * @flags: message flags
  384. *
  385. * Returns NULL if the tailroom of the skb is insufficient to store
  386. * the message header and payload.
  387. */
  388. static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
  389. int type, int payload, int flags)
  390. {
  391. if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
  392. return NULL;
  393. return __nlmsg_put(skb, pid, seq, type, payload, flags);
  394. }
  395. /**
  396. * nlmsg_put_answer - Add a new callback based netlink message to an skb
  397. * @skb: socket buffer to store message in
  398. * @cb: netlink callback
  399. * @type: message type
  400. * @payload: length of message payload
  401. * @flags: message flags
  402. *
  403. * Returns NULL if the tailroom of the skb is insufficient to store
  404. * the message header and payload.
  405. */
  406. static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
  407. struct netlink_callback *cb,
  408. int type, int payload,
  409. int flags)
  410. {
  411. return nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  412. type, payload, flags);
  413. }
  414. /**
  415. * nlmsg_new - Allocate a new netlink message
  416. * @size: maximum size of message
  417. *
  418. * Use NLMSG_GOODSIZE if size isn't know and you need a good default size.
  419. */
  420. static inline struct sk_buff *nlmsg_new(int size)
  421. {
  422. return alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  423. }
  424. /**
  425. * nlmsg_end - Finalize a netlink message
  426. * @skb: socket buffer the message is stored in
  427. * @nlh: netlink message header
  428. *
  429. * Corrects the netlink message header to include the appeneded
  430. * attributes. Only necessary if attributes have been added to
  431. * the message.
  432. *
  433. * Returns the total data length of the skb.
  434. */
  435. static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
  436. {
  437. nlh->nlmsg_len = skb->tail - (unsigned char *) nlh;
  438. return skb->len;
  439. }
  440. /**
  441. * nlmsg_cancel - Cancel construction of a netlink message
  442. * @skb: socket buffer the message is stored in
  443. * @nlh: netlink message header
  444. *
  445. * Removes the complete netlink message including all
  446. * attributes from the socket buffer again. Returns -1.
  447. */
  448. static inline int nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
  449. {
  450. skb_trim(skb, (unsigned char *) nlh - skb->data);
  451. return -1;
  452. }
  453. /**
  454. * nlmsg_free - free a netlink message
  455. * @skb: socket buffer of netlink message
  456. */
  457. static inline void nlmsg_free(struct sk_buff *skb)
  458. {
  459. kfree_skb(skb);
  460. }
  461. /**
  462. * nlmsg_multicast - multicast a netlink message
  463. * @sk: netlink socket to spread messages to
  464. * @skb: netlink message as socket buffer
  465. * @pid: own netlink pid to avoid sending to yourself
  466. * @group: multicast group id
  467. */
  468. static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
  469. u32 pid, unsigned int group)
  470. {
  471. int err;
  472. NETLINK_CB(skb).dst_group = group;
  473. err = netlink_broadcast(sk, skb, pid, group, GFP_KERNEL);
  474. if (err > 0)
  475. err = 0;
  476. return err;
  477. }
  478. /**
  479. * nlmsg_unicast - unicast a netlink message
  480. * @sk: netlink socket to spread message to
  481. * @skb: netlink message as socket buffer
  482. * @pid: netlink pid of the destination socket
  483. */
  484. static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid)
  485. {
  486. int err;
  487. err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
  488. if (err > 0)
  489. err = 0;
  490. return err;
  491. }
  492. /**
  493. * nlmsg_for_each_msg - iterate over a stream of messages
  494. * @pos: loop counter, set to current message
  495. * @head: head of message stream
  496. * @len: length of message stream
  497. * @rem: initialized to len, holds bytes currently remaining in stream
  498. */
  499. #define nlmsg_for_each_msg(pos, head, len, rem) \
  500. for (pos = head, rem = len; \
  501. nlmsg_ok(pos, rem); \
  502. pos = nlmsg_next(pos, &(rem)))
  503. /**************************************************************************
  504. * Netlink Attributes
  505. **************************************************************************/
  506. /**
  507. * nla_attr_size - length of attribute not including padding
  508. * @payload: length of payload
  509. */
  510. static inline int nla_attr_size(int payload)
  511. {
  512. return NLA_HDRLEN + payload;
  513. }
  514. /**
  515. * nla_total_size - total length of attribute including padding
  516. * @payload: length of payload
  517. */
  518. static inline int nla_total_size(int payload)
  519. {
  520. return NLA_ALIGN(nla_attr_size(payload));
  521. }
  522. /**
  523. * nla_padlen - length of padding at the tail of attribute
  524. * @payload: length of payload
  525. */
  526. static inline int nla_padlen(int payload)
  527. {
  528. return nla_total_size(payload) - nla_attr_size(payload);
  529. }
  530. /**
  531. * nla_data - head of payload
  532. * @nla: netlink attribute
  533. */
  534. static inline void *nla_data(const struct nlattr *nla)
  535. {
  536. return (char *) nla + NLA_HDRLEN;
  537. }
  538. /**
  539. * nla_len - length of payload
  540. * @nla: netlink attribute
  541. */
  542. static inline int nla_len(const struct nlattr *nla)
  543. {
  544. return nla->nla_len - NLA_HDRLEN;
  545. }
  546. /**
  547. * nla_ok - check if the netlink attribute fits into the remaining bytes
  548. * @nla: netlink attribute
  549. * @remaining: number of bytes remaining in attribute stream
  550. */
  551. static inline int nla_ok(const struct nlattr *nla, int remaining)
  552. {
  553. return remaining >= sizeof(*nla) &&
  554. nla->nla_len >= sizeof(*nla) &&
  555. nla->nla_len <= remaining;
  556. }
  557. /**
  558. * nla_next - next netlink attribte in attribute stream
  559. * @nla: netlink attribute
  560. * @remaining: number of bytes remaining in attribute stream
  561. *
  562. * Returns the next netlink attribute in the attribute stream and
  563. * decrements remaining by the size of the current attribute.
  564. */
  565. static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
  566. {
  567. int totlen = NLA_ALIGN(nla->nla_len);
  568. *remaining -= totlen;
  569. return (struct nlattr *) ((char *) nla + totlen);
  570. }
  571. /**
  572. * nla_parse_nested - parse nested attributes
  573. * @tb: destination array with maxtype+1 elements
  574. * @maxtype: maximum attribute type to be expected
  575. * @nla: attribute containing the nested attributes
  576. * @policy: validation policy
  577. *
  578. * See nla_parse()
  579. */
  580. static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
  581. struct nlattr *nla,
  582. struct nla_policy *policy)
  583. {
  584. return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
  585. }
  586. /**
  587. * nla_put_u8 - Add a u16 netlink attribute to a socket buffer
  588. * @skb: socket buffer to add attribute to
  589. * @attrtype: attribute type
  590. * @value: numeric value
  591. */
  592. static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
  593. {
  594. return nla_put(skb, attrtype, sizeof(u8), &value);
  595. }
  596. /**
  597. * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
  598. * @skb: socket buffer to add attribute to
  599. * @attrtype: attribute type
  600. * @value: numeric value
  601. */
  602. static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
  603. {
  604. return nla_put(skb, attrtype, sizeof(u16), &value);
  605. }
  606. /**
  607. * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
  608. * @skb: socket buffer to add attribute to
  609. * @attrtype: attribute type
  610. * @value: numeric value
  611. */
  612. static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
  613. {
  614. return nla_put(skb, attrtype, sizeof(u32), &value);
  615. }
  616. /**
  617. * nla_put_64 - Add a u64 netlink attribute to a socket buffer
  618. * @skb: socket buffer to add attribute to
  619. * @attrtype: attribute type
  620. * @value: numeric value
  621. */
  622. static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
  623. {
  624. return nla_put(skb, attrtype, sizeof(u64), &value);
  625. }
  626. /**
  627. * nla_put_string - Add a string netlink attribute to a socket buffer
  628. * @skb: socket buffer to add attribute to
  629. * @attrtype: attribute type
  630. * @str: NUL terminated string
  631. */
  632. static inline int nla_put_string(struct sk_buff *skb, int attrtype,
  633. const char *str)
  634. {
  635. return nla_put(skb, attrtype, strlen(str) + 1, str);
  636. }
  637. /**
  638. * nla_put_flag - Add a flag netlink attribute to a socket buffer
  639. * @skb: socket buffer to add attribute to
  640. * @attrtype: attribute type
  641. */
  642. static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
  643. {
  644. return nla_put(skb, attrtype, 0, NULL);
  645. }
  646. /**
  647. * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
  648. * @skb: socket buffer to add attribute to
  649. * @attrtype: attribute type
  650. * @jiffies: number of msecs in jiffies
  651. */
  652. static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
  653. unsigned long jiffies)
  654. {
  655. u64 tmp = jiffies_to_msecs(jiffies);
  656. return nla_put(skb, attrtype, sizeof(u64), &tmp);
  657. }
  658. #define NLA_PUT(skb, attrtype, attrlen, data) \
  659. do { \
  660. if (nla_put(skb, attrtype, attrlen, data) < 0) \
  661. goto nla_put_failure; \
  662. } while(0)
  663. #define NLA_PUT_TYPE(skb, type, attrtype, value) \
  664. do { \
  665. type __tmp = value; \
  666. NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \
  667. } while(0)
  668. #define NLA_PUT_U8(skb, attrtype, value) \
  669. NLA_PUT_TYPE(skb, u8, attrtype, value)
  670. #define NLA_PUT_U16(skb, attrtype, value) \
  671. NLA_PUT_TYPE(skb, u16, attrtype, value)
  672. #define NLA_PUT_U32(skb, attrtype, value) \
  673. NLA_PUT_TYPE(skb, u32, attrtype, value)
  674. #define NLA_PUT_U64(skb, attrtype, value) \
  675. NLA_PUT_TYPE(skb, u64, attrtype, value)
  676. #define NLA_PUT_STRING(skb, attrtype, value) \
  677. NLA_PUT(skb, attrtype, strlen(value) + 1, value)
  678. #define NLA_PUT_FLAG(skb, attrtype, value) \
  679. NLA_PUT(skb, attrtype, 0, NULL)
  680. #define NLA_PUT_MSECS(skb, attrtype, jiffies) \
  681. NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
  682. /**
  683. * nla_get_u32 - return payload of u32 attribute
  684. * @nla: u32 netlink attribute
  685. */
  686. static inline u32 nla_get_u32(struct nlattr *nla)
  687. {
  688. return *(u32 *) nla_data(nla);
  689. }
  690. /**
  691. * nla_get_u16 - return payload of u16 attribute
  692. * @nla: u16 netlink attribute
  693. */
  694. static inline u16 nla_get_u16(struct nlattr *nla)
  695. {
  696. return *(u16 *) nla_data(nla);
  697. }
  698. /**
  699. * nla_get_u8 - return payload of u8 attribute
  700. * @nla: u8 netlink attribute
  701. */
  702. static inline u8 nla_get_u8(struct nlattr *nla)
  703. {
  704. return *(u8 *) nla_data(nla);
  705. }
  706. /**
  707. * nla_get_u64 - return payload of u64 attribute
  708. * @nla: u64 netlink attribute
  709. */
  710. static inline u64 nla_get_u64(struct nlattr *nla)
  711. {
  712. u64 tmp;
  713. nla_memcpy(&tmp, nla, sizeof(tmp));
  714. return tmp;
  715. }
  716. /**
  717. * nla_get_flag - return payload of flag attribute
  718. * @nla: flag netlink attribute
  719. */
  720. static inline int nla_get_flag(struct nlattr *nla)
  721. {
  722. return !!nla;
  723. }
  724. /**
  725. * nla_get_msecs - return payload of msecs attribute
  726. * @nla: msecs netlink attribute
  727. *
  728. * Returns the number of milliseconds in jiffies.
  729. */
  730. static inline unsigned long nla_get_msecs(struct nlattr *nla)
  731. {
  732. u64 msecs = nla_get_u64(nla);
  733. return msecs_to_jiffies((unsigned long) msecs);
  734. }
  735. /**
  736. * nla_nest_start - Start a new level of nested attributes
  737. * @skb: socket buffer to add attributes to
  738. * @attrtype: attribute type of container
  739. *
  740. * Returns the container attribute
  741. */
  742. static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
  743. {
  744. struct nlattr *start = (struct nlattr *) skb->tail;
  745. if (nla_put(skb, attrtype, 0, NULL) < 0)
  746. return NULL;
  747. return start;
  748. }
  749. /**
  750. * nla_nest_end - Finalize nesting of attributes
  751. * @skb: socket buffer the attribtues are stored in
  752. * @start: container attribute
  753. *
  754. * Corrects the container attribute header to include the all
  755. * appeneded attributes.
  756. *
  757. * Returns the total data length of the skb.
  758. */
  759. static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
  760. {
  761. start->nla_len = skb->tail - (unsigned char *) start;
  762. return skb->len;
  763. }
  764. /**
  765. * nla_nest_cancel - Cancel nesting of attributes
  766. * @skb: socket buffer the message is stored in
  767. * @start: container attribute
  768. *
  769. * Removes the container attribute and including all nested
  770. * attributes. Returns -1.
  771. */
  772. static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
  773. {
  774. if (start)
  775. skb_trim(skb, (unsigned char *) start - skb->data);
  776. return -1;
  777. }
  778. /**
  779. * nla_for_each_attr - iterate over a stream of attributes
  780. * @pos: loop counter, set to current attribute
  781. * @head: head of attribute stream
  782. * @len: length of attribute stream
  783. * @rem: initialized to len, holds bytes currently remaining in stream
  784. */
  785. #define nla_for_each_attr(pos, head, len, rem) \
  786. for (pos = head, rem = len; \
  787. nla_ok(pos, rem); \
  788. pos = nla_next(pos, &(rem)))
  789. #endif