netlink.h 27 KB

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