netlink.h 29 KB

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