netlink.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. #ifndef __NET_NETLINK_H
  2. #define __NET_NETLINK_H
  3. #include <linux/types.h>
  4. #include <linux/netlink.h>
  5. #include <linux/jiffies.h>
  6. /* ========================================================================
  7. * Netlink Messages and Attributes Interface (As Seen On TV)
  8. * ------------------------------------------------------------------------
  9. * Messages Interface
  10. * ------------------------------------------------------------------------
  11. *
  12. * Message Format:
  13. * <--- nlmsg_total_size(payload) --->
  14. * <-- nlmsg_msg_size(payload) ->
  15. * +----------+- - -+-------------+- - -+-------- - -
  16. * | nlmsghdr | Pad | Payload | Pad | nlmsghdr
  17. * +----------+- - -+-------------+- - -+-------- - -
  18. * nlmsg_data(nlh)---^ ^
  19. * nlmsg_next(nlh)-----------------------+
  20. *
  21. * Payload Format:
  22. * <---------------------- nlmsg_len(nlh) --------------------->
  23. * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) ->
  24. * +----------------------+- - -+--------------------------------+
  25. * | Family Header | Pad | Attributes |
  26. * +----------------------+- - -+--------------------------------+
  27. * nlmsg_attrdata(nlh, hdrlen)---^
  28. *
  29. * Data Structures:
  30. * struct nlmsghdr netlink message header
  31. *
  32. * Message Construction:
  33. * nlmsg_new() create a new netlink message
  34. * nlmsg_put() add a netlink message to an skb
  35. * nlmsg_put_answer() callback based nlmsg_put()
  36. * nlmsg_end() finalize netlink message
  37. * nlmsg_get_pos() return current position in message
  38. * nlmsg_trim() trim part of message
  39. * nlmsg_cancel() cancel message construction
  40. * nlmsg_free() free a netlink message
  41. *
  42. * Message Sending:
  43. * nlmsg_multicast() multicast message to several groups
  44. * nlmsg_unicast() unicast a message to a single socket
  45. * nlmsg_notify() send notification message
  46. *
  47. * Message Length Calculations:
  48. * nlmsg_msg_size(payload) length of message w/o padding
  49. * nlmsg_total_size(payload) length of message w/ padding
  50. * nlmsg_padlen(payload) length of padding at tail
  51. *
  52. * Message Payload Access:
  53. * nlmsg_data(nlh) head of message payload
  54. * nlmsg_len(nlh) length of message payload
  55. * nlmsg_attrdata(nlh, hdrlen) head of attributes data
  56. * nlmsg_attrlen(nlh, hdrlen) length of attributes data
  57. *
  58. * Message Parsing:
  59. * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes?
  60. * nlmsg_next(nlh, remaining) get next netlink message
  61. * nlmsg_parse() parse attributes of a message
  62. * nlmsg_find_attr() find an attribute in a message
  63. * nlmsg_for_each_msg() loop over all messages
  64. * nlmsg_validate() validate netlink message incl. attrs
  65. * nlmsg_for_each_attr() loop over all attributes
  66. *
  67. * Misc:
  68. * nlmsg_report() report back to application?
  69. *
  70. * ------------------------------------------------------------------------
  71. * Attributes Interface
  72. * ------------------------------------------------------------------------
  73. *
  74. * Attribute Format:
  75. * <------- nla_total_size(payload) ------->
  76. * <---- nla_attr_size(payload) ----->
  77. * +----------+- - -+- - - - - - - - - +- - -+-------- - -
  78. * | Header | Pad | Payload | Pad | Header
  79. * +----------+- - -+- - - - - - - - - +- - -+-------- - -
  80. * <- nla_len(nla) -> ^
  81. * nla_data(nla)----^ |
  82. * nla_next(nla)-----------------------------'
  83. *
  84. * Data Structures:
  85. * struct nlattr netlink attribute header
  86. *
  87. * Attribute Construction:
  88. * nla_reserve(skb, type, len) reserve room for an attribute
  89. * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr
  90. * nla_put(skb, type, len, data) add attribute to skb
  91. * nla_put_nohdr(skb, len, data) add attribute w/o hdr
  92. * nla_append(skb, len, data) append data to skb
  93. *
  94. * Attribute Construction for Basic Types:
  95. * nla_put_u8(skb, type, value) add u8 attribute to skb
  96. * nla_put_u16(skb, type, value) add u16 attribute to skb
  97. * nla_put_u32(skb, type, value) add u32 attribute to skb
  98. * nla_put_u64(skb, type, value) add u64 attribute to skb
  99. * nla_put_s8(skb, type, value) add s8 attribute to skb
  100. * nla_put_s16(skb, type, value) add s16 attribute to skb
  101. * nla_put_s32(skb, type, value) add s32 attribute to skb
  102. * nla_put_s64(skb, type, value) add s64 attribute to skb
  103. * nla_put_string(skb, type, str) add string attribute to skb
  104. * nla_put_flag(skb, type) add flag attribute to skb
  105. * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb
  106. *
  107. * Nested Attributes Construction:
  108. * nla_nest_start(skb, type) start a nested attribute
  109. * nla_nest_end(skb, nla) finalize a nested attribute
  110. * nla_nest_cancel(skb, nla) cancel nested attribute construction
  111. *
  112. * Attribute Length Calculations:
  113. * nla_attr_size(payload) length of attribute w/o padding
  114. * nla_total_size(payload) length of attribute w/ padding
  115. * nla_padlen(payload) length of padding
  116. *
  117. * Attribute Payload Access:
  118. * nla_data(nla) head of attribute payload
  119. * nla_len(nla) length of attribute payload
  120. *
  121. * Attribute Payload Access for Basic Types:
  122. * nla_get_u8(nla) get payload for a u8 attribute
  123. * nla_get_u16(nla) get payload for a u16 attribute
  124. * nla_get_u32(nla) get payload for a u32 attribute
  125. * nla_get_u64(nla) get payload for a u64 attribute
  126. * nla_get_s8(nla) get payload for a s8 attribute
  127. * nla_get_s16(nla) get payload for a s16 attribute
  128. * nla_get_s32(nla) get payload for a s32 attribute
  129. * nla_get_s64(nla) get payload for a s64 attribute
  130. * nla_get_flag(nla) return 1 if flag is true
  131. * nla_get_msecs(nla) get payload for a msecs attribute
  132. *
  133. * Attribute Misc:
  134. * nla_memcpy(dest, nla, count) copy attribute into memory
  135. * nla_memcmp(nla, data, size) compare attribute with memory area
  136. * nla_strlcpy(dst, nla, size) copy attribute to a sized string
  137. * nla_strcmp(nla, str) compare attribute with string
  138. *
  139. * Attribute Parsing:
  140. * nla_ok(nla, remaining) does nla fit into remaining bytes?
  141. * nla_next(nla, remaining) get next netlink attribute
  142. * nla_validate() validate a stream of attributes
  143. * nla_validate_nested() validate a stream of nested 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. * nla_for_each_nested() loop over the nested attributes
  150. *=========================================================================
  151. */
  152. /**
  153. * Standard attribute types to specify validation policy
  154. */
  155. enum {
  156. NLA_UNSPEC,
  157. NLA_U8,
  158. NLA_U16,
  159. NLA_U32,
  160. NLA_U64,
  161. NLA_STRING,
  162. NLA_FLAG,
  163. NLA_MSECS,
  164. NLA_NESTED,
  165. NLA_NESTED_COMPAT,
  166. NLA_NUL_STRING,
  167. NLA_BINARY,
  168. NLA_S8,
  169. NLA_S16,
  170. NLA_S32,
  171. NLA_S64,
  172. __NLA_TYPE_MAX,
  173. };
  174. #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
  175. /**
  176. * struct nla_policy - attribute validation policy
  177. * @type: Type of attribute or NLA_UNSPEC
  178. * @len: Type specific length of payload
  179. *
  180. * Policies are defined as arrays of this struct, the array must be
  181. * accessible by attribute type up to the highest identifier to be expected.
  182. *
  183. * Meaning of `len' field:
  184. * NLA_STRING Maximum length of string
  185. * NLA_NUL_STRING Maximum length of string (excluding NUL)
  186. * NLA_FLAG Unused
  187. * NLA_BINARY Maximum length of attribute payload
  188. * NLA_NESTED Don't use `len' field -- length verification is
  189. * done by checking len of nested header (or empty)
  190. * NLA_NESTED_COMPAT Minimum length of structure payload
  191. * NLA_U8, NLA_U16,
  192. * NLA_U32, NLA_U64,
  193. * NLA_S8, NLA_S16,
  194. * NLA_S32, NLA_S64,
  195. * NLA_MSECS Leaving the length field zero will verify the
  196. * given type fits, using it verifies minimum length
  197. * just like "All other"
  198. * All other Minimum length of attribute payload
  199. *
  200. * Example:
  201. * static const struct nla_policy my_policy[ATTR_MAX+1] = {
  202. * [ATTR_FOO] = { .type = NLA_U16 },
  203. * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
  204. * [ATTR_BAZ] = { .len = sizeof(struct mystruct) },
  205. * };
  206. */
  207. struct nla_policy {
  208. u16 type;
  209. u16 len;
  210. };
  211. /**
  212. * struct nl_info - netlink source information
  213. * @nlh: Netlink message header of original request
  214. * @portid: Netlink PORTID of requesting application
  215. */
  216. struct nl_info {
  217. struct nlmsghdr *nlh;
  218. struct net *nl_net;
  219. u32 portid;
  220. };
  221. int netlink_rcv_skb(struct sk_buff *skb,
  222. int (*cb)(struct sk_buff *, struct nlmsghdr *));
  223. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  224. unsigned int group, int report, gfp_t flags);
  225. int nla_validate(const struct nlattr *head, int len, int maxtype,
  226. const struct nla_policy *policy);
  227. int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
  228. int len, const struct nla_policy *policy);
  229. int nla_policy_len(const struct nla_policy *, int);
  230. struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
  231. size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize);
  232. int nla_memcpy(void *dest, const struct nlattr *src, int count);
  233. int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
  234. int nla_strcmp(const struct nlattr *nla, const char *str);
  235. struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
  236. void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
  237. struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
  238. void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
  239. void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
  240. const void *data);
  241. void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
  242. int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
  243. int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
  244. int nla_append(struct sk_buff *skb, int attrlen, const void *data);
  245. /**************************************************************************
  246. * Netlink Messages
  247. **************************************************************************/
  248. /**
  249. * nlmsg_msg_size - length of netlink message not including padding
  250. * @payload: length of message payload
  251. */
  252. static inline int nlmsg_msg_size(int payload)
  253. {
  254. return NLMSG_HDRLEN + payload;
  255. }
  256. /**
  257. * nlmsg_total_size - length of netlink message including padding
  258. * @payload: length of message payload
  259. */
  260. static inline int nlmsg_total_size(int payload)
  261. {
  262. return NLMSG_ALIGN(nlmsg_msg_size(payload));
  263. }
  264. /**
  265. * nlmsg_padlen - length of padding at the message's tail
  266. * @payload: length of message payload
  267. */
  268. static inline int nlmsg_padlen(int payload)
  269. {
  270. return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
  271. }
  272. /**
  273. * nlmsg_data - head of message payload
  274. * @nlh: netlink message header
  275. */
  276. static inline void *nlmsg_data(const struct nlmsghdr *nlh)
  277. {
  278. return (unsigned char *) nlh + NLMSG_HDRLEN;
  279. }
  280. /**
  281. * nlmsg_len - length of message payload
  282. * @nlh: netlink message header
  283. */
  284. static inline int nlmsg_len(const struct nlmsghdr *nlh)
  285. {
  286. return nlh->nlmsg_len - NLMSG_HDRLEN;
  287. }
  288. /**
  289. * nlmsg_attrdata - head of attributes data
  290. * @nlh: netlink message header
  291. * @hdrlen: length of family specific header
  292. */
  293. static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
  294. int hdrlen)
  295. {
  296. unsigned char *data = nlmsg_data(nlh);
  297. return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
  298. }
  299. /**
  300. * nlmsg_attrlen - length of attributes data
  301. * @nlh: netlink message header
  302. * @hdrlen: length of family specific header
  303. */
  304. static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
  305. {
  306. return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
  307. }
  308. /**
  309. * nlmsg_ok - check if the netlink message fits into the remaining bytes
  310. * @nlh: netlink message header
  311. * @remaining: number of bytes remaining in message stream
  312. */
  313. static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
  314. {
  315. return (remaining >= (int) sizeof(struct nlmsghdr) &&
  316. nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
  317. nlh->nlmsg_len <= remaining);
  318. }
  319. /**
  320. * nlmsg_next - next netlink message in message stream
  321. * @nlh: netlink message header
  322. * @remaining: number of bytes remaining in message stream
  323. *
  324. * Returns the next netlink message in the message stream and
  325. * decrements remaining by the size of the current message.
  326. */
  327. static inline struct nlmsghdr *
  328. nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
  329. {
  330. int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
  331. *remaining -= totlen;
  332. return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
  333. }
  334. /**
  335. * nlmsg_parse - parse attributes of a netlink message
  336. * @nlh: netlink message header
  337. * @hdrlen: length of family specific header
  338. * @tb: destination array with maxtype+1 elements
  339. * @maxtype: maximum attribute type to be expected
  340. * @policy: validation policy
  341. *
  342. * See nla_parse()
  343. */
  344. static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
  345. struct nlattr *tb[], int maxtype,
  346. const struct nla_policy *policy)
  347. {
  348. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  349. return -EINVAL;
  350. return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
  351. nlmsg_attrlen(nlh, hdrlen), policy);
  352. }
  353. /**
  354. * nlmsg_find_attr - find a specific attribute in a netlink message
  355. * @nlh: netlink message header
  356. * @hdrlen: length of familiy specific header
  357. * @attrtype: type of attribute to look for
  358. *
  359. * Returns the first attribute which matches the specified type.
  360. */
  361. static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
  362. int hdrlen, int attrtype)
  363. {
  364. return nla_find(nlmsg_attrdata(nlh, hdrlen),
  365. nlmsg_attrlen(nlh, hdrlen), attrtype);
  366. }
  367. /**
  368. * nlmsg_validate - validate a netlink message including attributes
  369. * @nlh: netlinket message header
  370. * @hdrlen: length of familiy specific header
  371. * @maxtype: maximum attribute type to be expected
  372. * @policy: validation policy
  373. */
  374. static inline int nlmsg_validate(const struct nlmsghdr *nlh,
  375. int hdrlen, int maxtype,
  376. const struct nla_policy *policy)
  377. {
  378. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  379. return -EINVAL;
  380. return nla_validate(nlmsg_attrdata(nlh, hdrlen),
  381. nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
  382. }
  383. /**
  384. * nlmsg_report - need to report back to application?
  385. * @nlh: netlink message header
  386. *
  387. * Returns 1 if a report back to the application is requested.
  388. */
  389. static inline int nlmsg_report(const struct nlmsghdr *nlh)
  390. {
  391. return !!(nlh->nlmsg_flags & NLM_F_ECHO);
  392. }
  393. /**
  394. * nlmsg_for_each_attr - iterate over a stream of attributes
  395. * @pos: loop counter, set to current attribute
  396. * @nlh: netlink message header
  397. * @hdrlen: length of familiy specific header
  398. * @rem: initialized to len, holds bytes currently remaining in stream
  399. */
  400. #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
  401. nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
  402. nlmsg_attrlen(nlh, hdrlen), rem)
  403. /**
  404. * nlmsg_put - Add a new netlink message to an skb
  405. * @skb: socket buffer to store message in
  406. * @portid: netlink PORTID of requesting application
  407. * @seq: sequence number of message
  408. * @type: message type
  409. * @payload: length of message payload
  410. * @flags: message flags
  411. *
  412. * Returns NULL if the tailroom of the skb is insufficient to store
  413. * the message header and payload.
  414. */
  415. static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  416. int type, int payload, int flags)
  417. {
  418. if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
  419. return NULL;
  420. return __nlmsg_put(skb, portid, seq, type, payload, flags);
  421. }
  422. /**
  423. * nlmsg_put_answer - Add a new callback based netlink message to an skb
  424. * @skb: socket buffer to store message in
  425. * @cb: netlink callback
  426. * @type: message type
  427. * @payload: length of message payload
  428. * @flags: message flags
  429. *
  430. * Returns NULL if the tailroom of the skb is insufficient to store
  431. * the message header and payload.
  432. */
  433. static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
  434. struct netlink_callback *cb,
  435. int type, int payload,
  436. int flags)
  437. {
  438. return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  439. type, payload, flags);
  440. }
  441. /**
  442. * nlmsg_new - Allocate a new netlink message
  443. * @payload: size of the message payload
  444. * @flags: the type of memory to allocate.
  445. *
  446. * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
  447. * and a good default is needed.
  448. */
  449. static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
  450. {
  451. return alloc_skb(nlmsg_total_size(payload), flags);
  452. }
  453. /**
  454. * nlmsg_end - Finalize a netlink message
  455. * @skb: socket buffer the message is stored in
  456. * @nlh: netlink message header
  457. *
  458. * Corrects the netlink message header to include the appeneded
  459. * attributes. Only necessary if attributes have been added to
  460. * the message.
  461. *
  462. * Returns the total data length of the skb.
  463. */
  464. static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
  465. {
  466. nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
  467. return skb->len;
  468. }
  469. /**
  470. * nlmsg_get_pos - return current position in netlink message
  471. * @skb: socket buffer the message is stored in
  472. *
  473. * Returns a pointer to the current tail of the message.
  474. */
  475. static inline void *nlmsg_get_pos(struct sk_buff *skb)
  476. {
  477. return skb_tail_pointer(skb);
  478. }
  479. /**
  480. * nlmsg_trim - Trim message to a mark
  481. * @skb: socket buffer the message is stored in
  482. * @mark: mark to trim to
  483. *
  484. * Trims the message to the provided mark.
  485. */
  486. static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
  487. {
  488. if (mark) {
  489. WARN_ON((unsigned char *) mark < skb->data);
  490. skb_trim(skb, (unsigned char *) mark - skb->data);
  491. }
  492. }
  493. /**
  494. * nlmsg_cancel - Cancel construction of a netlink message
  495. * @skb: socket buffer the message is stored in
  496. * @nlh: netlink message header
  497. *
  498. * Removes the complete netlink message including all
  499. * attributes from the socket buffer again.
  500. */
  501. static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
  502. {
  503. nlmsg_trim(skb, nlh);
  504. }
  505. /**
  506. * nlmsg_free - free a netlink message
  507. * @skb: socket buffer of netlink message
  508. */
  509. static inline void nlmsg_free(struct sk_buff *skb)
  510. {
  511. kfree_skb(skb);
  512. }
  513. /**
  514. * nlmsg_multicast - multicast a netlink message
  515. * @sk: netlink socket to spread messages to
  516. * @skb: netlink message as socket buffer
  517. * @portid: own netlink portid to avoid sending to yourself
  518. * @group: multicast group id
  519. * @flags: allocation flags
  520. */
  521. static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
  522. u32 portid, unsigned int group, gfp_t flags)
  523. {
  524. int err;
  525. NETLINK_CB(skb).dst_group = group;
  526. err = netlink_broadcast(sk, skb, portid, group, flags);
  527. if (err > 0)
  528. err = 0;
  529. return err;
  530. }
  531. /**
  532. * nlmsg_unicast - unicast a netlink message
  533. * @sk: netlink socket to spread message to
  534. * @skb: netlink message as socket buffer
  535. * @portid: netlink portid of the destination socket
  536. */
  537. static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
  538. {
  539. int err;
  540. err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
  541. if (err > 0)
  542. err = 0;
  543. return err;
  544. }
  545. /**
  546. * nlmsg_for_each_msg - iterate over a stream of messages
  547. * @pos: loop counter, set to current message
  548. * @head: head of message stream
  549. * @len: length of message stream
  550. * @rem: initialized to len, holds bytes currently remaining in stream
  551. */
  552. #define nlmsg_for_each_msg(pos, head, len, rem) \
  553. for (pos = head, rem = len; \
  554. nlmsg_ok(pos, rem); \
  555. pos = nlmsg_next(pos, &(rem)))
  556. /**
  557. * nl_dump_check_consistent - check if sequence is consistent and advertise if not
  558. * @cb: netlink callback structure that stores the sequence number
  559. * @nlh: netlink message header to write the flag to
  560. *
  561. * This function checks if the sequence (generation) number changed during dump
  562. * and if it did, advertises it in the netlink message header.
  563. *
  564. * The correct way to use it is to set cb->seq to the generation counter when
  565. * all locks for dumping have been acquired, and then call this function for
  566. * each message that is generated.
  567. *
  568. * Note that due to initialisation concerns, 0 is an invalid sequence number
  569. * and must not be used by code that uses this functionality.
  570. */
  571. static inline void
  572. nl_dump_check_consistent(struct netlink_callback *cb,
  573. struct nlmsghdr *nlh)
  574. {
  575. if (cb->prev_seq && cb->seq != cb->prev_seq)
  576. nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
  577. cb->prev_seq = cb->seq;
  578. }
  579. /**************************************************************************
  580. * Netlink Attributes
  581. **************************************************************************/
  582. /**
  583. * nla_attr_size - length of attribute not including padding
  584. * @payload: length of payload
  585. */
  586. static inline int nla_attr_size(int payload)
  587. {
  588. return NLA_HDRLEN + payload;
  589. }
  590. /**
  591. * nla_total_size - total length of attribute including padding
  592. * @payload: length of payload
  593. */
  594. static inline int nla_total_size(int payload)
  595. {
  596. return NLA_ALIGN(nla_attr_size(payload));
  597. }
  598. /**
  599. * nla_padlen - length of padding at the tail of attribute
  600. * @payload: length of payload
  601. */
  602. static inline int nla_padlen(int payload)
  603. {
  604. return nla_total_size(payload) - nla_attr_size(payload);
  605. }
  606. /**
  607. * nla_type - attribute type
  608. * @nla: netlink attribute
  609. */
  610. static inline int nla_type(const struct nlattr *nla)
  611. {
  612. return nla->nla_type & NLA_TYPE_MASK;
  613. }
  614. /**
  615. * nla_data - head of payload
  616. * @nla: netlink attribute
  617. */
  618. static inline void *nla_data(const struct nlattr *nla)
  619. {
  620. return (char *) nla + NLA_HDRLEN;
  621. }
  622. /**
  623. * nla_len - length of payload
  624. * @nla: netlink attribute
  625. */
  626. static inline int nla_len(const struct nlattr *nla)
  627. {
  628. return nla->nla_len - NLA_HDRLEN;
  629. }
  630. /**
  631. * nla_ok - check if the netlink attribute fits into the remaining bytes
  632. * @nla: netlink attribute
  633. * @remaining: number of bytes remaining in attribute stream
  634. */
  635. static inline int nla_ok(const struct nlattr *nla, int remaining)
  636. {
  637. return remaining >= (int) sizeof(*nla) &&
  638. nla->nla_len >= sizeof(*nla) &&
  639. nla->nla_len <= remaining;
  640. }
  641. /**
  642. * nla_next - next netlink attribute in attribute stream
  643. * @nla: netlink attribute
  644. * @remaining: number of bytes remaining in attribute stream
  645. *
  646. * Returns the next netlink attribute in the attribute stream and
  647. * decrements remaining by the size of the current attribute.
  648. */
  649. static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
  650. {
  651. int totlen = NLA_ALIGN(nla->nla_len);
  652. *remaining -= totlen;
  653. return (struct nlattr *) ((char *) nla + totlen);
  654. }
  655. /**
  656. * nla_find_nested - find attribute in a set of nested attributes
  657. * @nla: attribute containing the nested attributes
  658. * @attrtype: type of attribute to look for
  659. *
  660. * Returns the first attribute which matches the specified type.
  661. */
  662. static inline struct nlattr *
  663. nla_find_nested(const struct nlattr *nla, int attrtype)
  664. {
  665. return nla_find(nla_data(nla), nla_len(nla), attrtype);
  666. }
  667. /**
  668. * nla_parse_nested - parse nested attributes
  669. * @tb: destination array with maxtype+1 elements
  670. * @maxtype: maximum attribute type to be expected
  671. * @nla: attribute containing the nested attributes
  672. * @policy: validation policy
  673. *
  674. * See nla_parse()
  675. */
  676. static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
  677. const struct nlattr *nla,
  678. const struct nla_policy *policy)
  679. {
  680. return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
  681. }
  682. /**
  683. * nla_put_u8 - Add a u8 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_u8(struct sk_buff *skb, int attrtype, u8 value)
  689. {
  690. return nla_put(skb, attrtype, sizeof(u8), &value);
  691. }
  692. /**
  693. * nla_put_u16 - Add a u16 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_u16(struct sk_buff *skb, int attrtype, u16 value)
  699. {
  700. return nla_put(skb, attrtype, sizeof(u16), &value);
  701. }
  702. /**
  703. * nla_put_be16 - Add a __be16 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_be16(struct sk_buff *skb, int attrtype, __be16 value)
  709. {
  710. return nla_put(skb, attrtype, sizeof(__be16), &value);
  711. }
  712. /**
  713. * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
  714. * @skb: socket buffer to add attribute to
  715. * @attrtype: attribute type
  716. * @value: numeric value
  717. */
  718. static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
  719. {
  720. return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, value);
  721. }
  722. /**
  723. * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
  724. * @skb: socket buffer to add attribute to
  725. * @attrtype: attribute type
  726. * @value: numeric value
  727. */
  728. static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
  729. {
  730. return nla_put(skb, attrtype, sizeof(__le16), &value);
  731. }
  732. /**
  733. * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
  734. * @skb: socket buffer to add attribute to
  735. * @attrtype: attribute type
  736. * @value: numeric value
  737. */
  738. static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
  739. {
  740. return nla_put(skb, attrtype, sizeof(u32), &value);
  741. }
  742. /**
  743. * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
  744. * @skb: socket buffer to add attribute to
  745. * @attrtype: attribute type
  746. * @value: numeric value
  747. */
  748. static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
  749. {
  750. return nla_put(skb, attrtype, sizeof(__be32), &value);
  751. }
  752. /**
  753. * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer
  754. * @skb: socket buffer to add attribute to
  755. * @attrtype: attribute type
  756. * @value: numeric value
  757. */
  758. static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
  759. {
  760. return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, value);
  761. }
  762. /**
  763. * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer
  764. * @skb: socket buffer to add attribute to
  765. * @attrtype: attribute type
  766. * @value: numeric value
  767. */
  768. static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
  769. {
  770. return nla_put(skb, attrtype, sizeof(__le32), &value);
  771. }
  772. /**
  773. * nla_put_u64 - Add a u64 netlink attribute to a socket buffer
  774. * @skb: socket buffer to add attribute to
  775. * @attrtype: attribute type
  776. * @value: numeric value
  777. */
  778. static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
  779. {
  780. return nla_put(skb, attrtype, sizeof(u64), &value);
  781. }
  782. /**
  783. * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer
  784. * @skb: socket buffer to add attribute to
  785. * @attrtype: attribute type
  786. * @value: numeric value
  787. */
  788. static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value)
  789. {
  790. return nla_put(skb, attrtype, sizeof(__be64), &value);
  791. }
  792. /**
  793. * nla_put_net64 - Add 64-bit network byte order netlink attribute to a socket buffer
  794. * @skb: socket buffer to add attribute to
  795. * @attrtype: attribute type
  796. * @value: numeric value
  797. */
  798. static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value)
  799. {
  800. return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, value);
  801. }
  802. /**
  803. * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer
  804. * @skb: socket buffer to add attribute to
  805. * @attrtype: attribute type
  806. * @value: numeric value
  807. */
  808. static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value)
  809. {
  810. return nla_put(skb, attrtype, sizeof(__le64), &value);
  811. }
  812. /**
  813. * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
  814. * @skb: socket buffer to add attribute to
  815. * @attrtype: attribute type
  816. * @value: numeric value
  817. */
  818. static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
  819. {
  820. return nla_put(skb, attrtype, sizeof(s8), &value);
  821. }
  822. /**
  823. * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
  824. * @skb: socket buffer to add attribute to
  825. * @attrtype: attribute type
  826. * @value: numeric value
  827. */
  828. static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
  829. {
  830. return nla_put(skb, attrtype, sizeof(s16), &value);
  831. }
  832. /**
  833. * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
  834. * @skb: socket buffer to add attribute to
  835. * @attrtype: attribute type
  836. * @value: numeric value
  837. */
  838. static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
  839. {
  840. return nla_put(skb, attrtype, sizeof(s32), &value);
  841. }
  842. /**
  843. * nla_put_s64 - Add a s64 netlink attribute to a socket buffer
  844. * @skb: socket buffer to add attribute to
  845. * @attrtype: attribute type
  846. * @value: numeric value
  847. */
  848. static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value)
  849. {
  850. return nla_put(skb, attrtype, sizeof(s64), &value);
  851. }
  852. /**
  853. * nla_put_string - Add a string netlink attribute to a socket buffer
  854. * @skb: socket buffer to add attribute to
  855. * @attrtype: attribute type
  856. * @str: NUL terminated string
  857. */
  858. static inline int nla_put_string(struct sk_buff *skb, int attrtype,
  859. const char *str)
  860. {
  861. return nla_put(skb, attrtype, strlen(str) + 1, str);
  862. }
  863. /**
  864. * nla_put_flag - Add a flag netlink attribute to a socket buffer
  865. * @skb: socket buffer to add attribute to
  866. * @attrtype: attribute type
  867. */
  868. static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
  869. {
  870. return nla_put(skb, attrtype, 0, NULL);
  871. }
  872. /**
  873. * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
  874. * @skb: socket buffer to add attribute to
  875. * @attrtype: attribute type
  876. * @njiffies: number of jiffies to convert to msecs
  877. */
  878. static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
  879. unsigned long njiffies)
  880. {
  881. u64 tmp = jiffies_to_msecs(njiffies);
  882. return nla_put(skb, attrtype, sizeof(u64), &tmp);
  883. }
  884. /**
  885. * nla_get_u32 - return payload of u32 attribute
  886. * @nla: u32 netlink attribute
  887. */
  888. static inline u32 nla_get_u32(const struct nlattr *nla)
  889. {
  890. return *(u32 *) nla_data(nla);
  891. }
  892. /**
  893. * nla_get_be32 - return payload of __be32 attribute
  894. * @nla: __be32 netlink attribute
  895. */
  896. static inline __be32 nla_get_be32(const struct nlattr *nla)
  897. {
  898. return *(__be32 *) nla_data(nla);
  899. }
  900. /**
  901. * nla_get_u16 - return payload of u16 attribute
  902. * @nla: u16 netlink attribute
  903. */
  904. static inline u16 nla_get_u16(const struct nlattr *nla)
  905. {
  906. return *(u16 *) nla_data(nla);
  907. }
  908. /**
  909. * nla_get_be16 - return payload of __be16 attribute
  910. * @nla: __be16 netlink attribute
  911. */
  912. static inline __be16 nla_get_be16(const struct nlattr *nla)
  913. {
  914. return *(__be16 *) nla_data(nla);
  915. }
  916. /**
  917. * nla_get_le16 - return payload of __le16 attribute
  918. * @nla: __le16 netlink attribute
  919. */
  920. static inline __le16 nla_get_le16(const struct nlattr *nla)
  921. {
  922. return *(__le16 *) nla_data(nla);
  923. }
  924. /**
  925. * nla_get_u8 - return payload of u8 attribute
  926. * @nla: u8 netlink attribute
  927. */
  928. static inline u8 nla_get_u8(const struct nlattr *nla)
  929. {
  930. return *(u8 *) nla_data(nla);
  931. }
  932. /**
  933. * nla_get_u64 - return payload of u64 attribute
  934. * @nla: u64 netlink attribute
  935. */
  936. static inline u64 nla_get_u64(const struct nlattr *nla)
  937. {
  938. u64 tmp;
  939. nla_memcpy(&tmp, nla, sizeof(tmp));
  940. return tmp;
  941. }
  942. /**
  943. * nla_get_be64 - return payload of __be64 attribute
  944. * @nla: __be64 netlink attribute
  945. */
  946. static inline __be64 nla_get_be64(const struct nlattr *nla)
  947. {
  948. __be64 tmp;
  949. nla_memcpy(&tmp, nla, sizeof(tmp));
  950. return tmp;
  951. }
  952. /**
  953. * nla_get_s32 - return payload of s32 attribute
  954. * @nla: s32 netlink attribute
  955. */
  956. static inline s32 nla_get_s32(const struct nlattr *nla)
  957. {
  958. return *(s32 *) nla_data(nla);
  959. }
  960. /**
  961. * nla_get_s16 - return payload of s16 attribute
  962. * @nla: s16 netlink attribute
  963. */
  964. static inline s16 nla_get_s16(const struct nlattr *nla)
  965. {
  966. return *(s16 *) nla_data(nla);
  967. }
  968. /**
  969. * nla_get_s8 - return payload of s8 attribute
  970. * @nla: s8 netlink attribute
  971. */
  972. static inline s8 nla_get_s8(const struct nlattr *nla)
  973. {
  974. return *(s8 *) nla_data(nla);
  975. }
  976. /**
  977. * nla_get_s64 - return payload of s64 attribute
  978. * @nla: s64 netlink attribute
  979. */
  980. static inline s64 nla_get_s64(const struct nlattr *nla)
  981. {
  982. s64 tmp;
  983. nla_memcpy(&tmp, nla, sizeof(tmp));
  984. return tmp;
  985. }
  986. /**
  987. * nla_get_flag - return payload of flag attribute
  988. * @nla: flag netlink attribute
  989. */
  990. static inline int nla_get_flag(const struct nlattr *nla)
  991. {
  992. return !!nla;
  993. }
  994. /**
  995. * nla_get_msecs - return payload of msecs attribute
  996. * @nla: msecs netlink attribute
  997. *
  998. * Returns the number of milliseconds in jiffies.
  999. */
  1000. static inline unsigned long nla_get_msecs(const struct nlattr *nla)
  1001. {
  1002. u64 msecs = nla_get_u64(nla);
  1003. return msecs_to_jiffies((unsigned long) msecs);
  1004. }
  1005. /**
  1006. * nla_nest_start - Start a new level of nested attributes
  1007. * @skb: socket buffer to add attributes to
  1008. * @attrtype: attribute type of container
  1009. *
  1010. * Returns the container attribute
  1011. */
  1012. static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
  1013. {
  1014. struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
  1015. if (nla_put(skb, attrtype, 0, NULL) < 0)
  1016. return NULL;
  1017. return start;
  1018. }
  1019. /**
  1020. * nla_nest_end - Finalize nesting of attributes
  1021. * @skb: socket buffer the attributes are stored in
  1022. * @start: container attribute
  1023. *
  1024. * Corrects the container attribute header to include the all
  1025. * appeneded attributes.
  1026. *
  1027. * Returns the total data length of the skb.
  1028. */
  1029. static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
  1030. {
  1031. start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
  1032. return skb->len;
  1033. }
  1034. /**
  1035. * nla_nest_cancel - Cancel nesting of attributes
  1036. * @skb: socket buffer the message is stored in
  1037. * @start: container attribute
  1038. *
  1039. * Removes the container attribute and including all nested
  1040. * attributes. Returns -EMSGSIZE
  1041. */
  1042. static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
  1043. {
  1044. nlmsg_trim(skb, start);
  1045. }
  1046. /**
  1047. * nla_validate_nested - Validate a stream of nested attributes
  1048. * @start: container attribute
  1049. * @maxtype: maximum attribute type to be expected
  1050. * @policy: validation policy
  1051. *
  1052. * Validates all attributes in the nested attribute stream against the
  1053. * specified policy. Attributes with a type exceeding maxtype will be
  1054. * ignored. See documenation of struct nla_policy for more details.
  1055. *
  1056. * Returns 0 on success or a negative error code.
  1057. */
  1058. static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
  1059. const struct nla_policy *policy)
  1060. {
  1061. return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
  1062. }
  1063. /**
  1064. * nla_for_each_attr - iterate over a stream of attributes
  1065. * @pos: loop counter, set to current attribute
  1066. * @head: head of attribute stream
  1067. * @len: length of attribute stream
  1068. * @rem: initialized to len, holds bytes currently remaining in stream
  1069. */
  1070. #define nla_for_each_attr(pos, head, len, rem) \
  1071. for (pos = head, rem = len; \
  1072. nla_ok(pos, rem); \
  1073. pos = nla_next(pos, &(rem)))
  1074. /**
  1075. * nla_for_each_nested - iterate over nested attributes
  1076. * @pos: loop counter, set to current attribute
  1077. * @nla: attribute containing the nested attributes
  1078. * @rem: initialized to len, holds bytes currently remaining in stream
  1079. */
  1080. #define nla_for_each_nested(pos, nla, rem) \
  1081. nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
  1082. /**
  1083. * nla_is_last - Test if attribute is last in stream
  1084. * @nla: attribute to test
  1085. * @rem: bytes remaining in stream
  1086. */
  1087. static inline bool nla_is_last(const struct nlattr *nla, int rem)
  1088. {
  1089. return nla->nla_len == rem;
  1090. }
  1091. #endif