netlink.h 28 KB

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