netlink.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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 process id
  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. skb_trim(skb, (unsigned char *) mark - skb->data);
  490. }
  491. /**
  492. * nlmsg_cancel - Cancel construction of a netlink message
  493. * @skb: socket buffer the message is stored in
  494. * @nlh: netlink message header
  495. *
  496. * Removes the complete netlink message including all
  497. * attributes from the socket buffer again.
  498. */
  499. static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
  500. {
  501. nlmsg_trim(skb, nlh);
  502. }
  503. /**
  504. * nlmsg_free - free a netlink message
  505. * @skb: socket buffer of netlink message
  506. */
  507. static inline void nlmsg_free(struct sk_buff *skb)
  508. {
  509. kfree_skb(skb);
  510. }
  511. /**
  512. * nlmsg_multicast - multicast a netlink message
  513. * @sk: netlink socket to spread messages to
  514. * @skb: netlink message as socket buffer
  515. * @portid: own netlink portid to avoid sending to yourself
  516. * @group: multicast group id
  517. * @flags: allocation flags
  518. */
  519. static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
  520. u32 portid, unsigned int group, gfp_t flags)
  521. {
  522. int err;
  523. NETLINK_CB(skb).dst_group = group;
  524. err = netlink_broadcast(sk, skb, portid, group, flags);
  525. if (err > 0)
  526. err = 0;
  527. return err;
  528. }
  529. /**
  530. * nlmsg_unicast - unicast a netlink message
  531. * @sk: netlink socket to spread message to
  532. * @skb: netlink message as socket buffer
  533. * @portid: netlink portid of the destination socket
  534. */
  535. static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
  536. {
  537. int err;
  538. err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
  539. if (err > 0)
  540. err = 0;
  541. return err;
  542. }
  543. /**
  544. * nlmsg_for_each_msg - iterate over a stream of messages
  545. * @pos: loop counter, set to current message
  546. * @head: head of message stream
  547. * @len: length of message stream
  548. * @rem: initialized to len, holds bytes currently remaining in stream
  549. */
  550. #define nlmsg_for_each_msg(pos, head, len, rem) \
  551. for (pos = head, rem = len; \
  552. nlmsg_ok(pos, rem); \
  553. pos = nlmsg_next(pos, &(rem)))
  554. /**
  555. * nl_dump_check_consistent - check if sequence is consistent and advertise if not
  556. * @cb: netlink callback structure that stores the sequence number
  557. * @nlh: netlink message header to write the flag to
  558. *
  559. * This function checks if the sequence (generation) number changed during dump
  560. * and if it did, advertises it in the netlink message header.
  561. *
  562. * The correct way to use it is to set cb->seq to the generation counter when
  563. * all locks for dumping have been acquired, and then call this function for
  564. * each message that is generated.
  565. *
  566. * Note that due to initialisation concerns, 0 is an invalid sequence number
  567. * and must not be used by code that uses this functionality.
  568. */
  569. static inline void
  570. nl_dump_check_consistent(struct netlink_callback *cb,
  571. struct nlmsghdr *nlh)
  572. {
  573. if (cb->prev_seq && cb->seq != cb->prev_seq)
  574. nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
  575. cb->prev_seq = cb->seq;
  576. }
  577. /**************************************************************************
  578. * Netlink Attributes
  579. **************************************************************************/
  580. /**
  581. * nla_attr_size - length of attribute not including padding
  582. * @payload: length of payload
  583. */
  584. static inline int nla_attr_size(int payload)
  585. {
  586. return NLA_HDRLEN + payload;
  587. }
  588. /**
  589. * nla_total_size - total length of attribute including padding
  590. * @payload: length of payload
  591. */
  592. static inline int nla_total_size(int payload)
  593. {
  594. return NLA_ALIGN(nla_attr_size(payload));
  595. }
  596. /**
  597. * nla_padlen - length of padding at the tail of attribute
  598. * @payload: length of payload
  599. */
  600. static inline int nla_padlen(int payload)
  601. {
  602. return nla_total_size(payload) - nla_attr_size(payload);
  603. }
  604. /**
  605. * nla_type - attribute type
  606. * @nla: netlink attribute
  607. */
  608. static inline int nla_type(const struct nlattr *nla)
  609. {
  610. return nla->nla_type & NLA_TYPE_MASK;
  611. }
  612. /**
  613. * nla_data - head of payload
  614. * @nla: netlink attribute
  615. */
  616. static inline void *nla_data(const struct nlattr *nla)
  617. {
  618. return (char *) nla + NLA_HDRLEN;
  619. }
  620. /**
  621. * nla_len - length of payload
  622. * @nla: netlink attribute
  623. */
  624. static inline int nla_len(const struct nlattr *nla)
  625. {
  626. return nla->nla_len - NLA_HDRLEN;
  627. }
  628. /**
  629. * nla_ok - check if the netlink attribute fits into the remaining bytes
  630. * @nla: netlink attribute
  631. * @remaining: number of bytes remaining in attribute stream
  632. */
  633. static inline int nla_ok(const struct nlattr *nla, int remaining)
  634. {
  635. return remaining >= (int) sizeof(*nla) &&
  636. nla->nla_len >= sizeof(*nla) &&
  637. nla->nla_len <= remaining;
  638. }
  639. /**
  640. * nla_next - next netlink attribute in attribute stream
  641. * @nla: netlink attribute
  642. * @remaining: number of bytes remaining in attribute stream
  643. *
  644. * Returns the next netlink attribute in the attribute stream and
  645. * decrements remaining by the size of the current attribute.
  646. */
  647. static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
  648. {
  649. int totlen = NLA_ALIGN(nla->nla_len);
  650. *remaining -= totlen;
  651. return (struct nlattr *) ((char *) nla + totlen);
  652. }
  653. /**
  654. * nla_find_nested - find attribute in a set of nested attributes
  655. * @nla: attribute containing the nested attributes
  656. * @attrtype: type of attribute to look for
  657. *
  658. * Returns the first attribute which matches the specified type.
  659. */
  660. static inline struct nlattr *
  661. nla_find_nested(const struct nlattr *nla, int attrtype)
  662. {
  663. return nla_find(nla_data(nla), nla_len(nla), attrtype);
  664. }
  665. /**
  666. * nla_parse_nested - parse nested attributes
  667. * @tb: destination array with maxtype+1 elements
  668. * @maxtype: maximum attribute type to be expected
  669. * @nla: attribute containing the nested attributes
  670. * @policy: validation policy
  671. *
  672. * See nla_parse()
  673. */
  674. static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
  675. const struct nlattr *nla,
  676. const struct nla_policy *policy)
  677. {
  678. return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
  679. }
  680. /**
  681. * nla_put_u8 - Add a u8 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_u8(struct sk_buff *skb, int attrtype, u8 value)
  687. {
  688. return nla_put(skb, attrtype, sizeof(u8), &value);
  689. }
  690. /**
  691. * nla_put_u16 - Add a u16 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_u16(struct sk_buff *skb, int attrtype, u16 value)
  697. {
  698. return nla_put(skb, attrtype, sizeof(u16), &value);
  699. }
  700. /**
  701. * nla_put_be16 - Add a __be16 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_be16(struct sk_buff *skb, int attrtype, __be16 value)
  707. {
  708. return nla_put(skb, attrtype, sizeof(__be16), &value);
  709. }
  710. /**
  711. * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
  712. * @skb: socket buffer to add attribute to
  713. * @attrtype: attribute type
  714. * @value: numeric value
  715. */
  716. static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
  717. {
  718. return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, value);
  719. }
  720. /**
  721. * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
  722. * @skb: socket buffer to add attribute to
  723. * @attrtype: attribute type
  724. * @value: numeric value
  725. */
  726. static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
  727. {
  728. return nla_put(skb, attrtype, sizeof(__le16), &value);
  729. }
  730. /**
  731. * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
  732. * @skb: socket buffer to add attribute to
  733. * @attrtype: attribute type
  734. * @value: numeric value
  735. */
  736. static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
  737. {
  738. return nla_put(skb, attrtype, sizeof(u32), &value);
  739. }
  740. /**
  741. * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
  742. * @skb: socket buffer to add attribute to
  743. * @attrtype: attribute type
  744. * @value: numeric value
  745. */
  746. static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
  747. {
  748. return nla_put(skb, attrtype, sizeof(__be32), &value);
  749. }
  750. /**
  751. * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer
  752. * @skb: socket buffer to add attribute to
  753. * @attrtype: attribute type
  754. * @value: numeric value
  755. */
  756. static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
  757. {
  758. return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, value);
  759. }
  760. /**
  761. * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer
  762. * @skb: socket buffer to add attribute to
  763. * @attrtype: attribute type
  764. * @value: numeric value
  765. */
  766. static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
  767. {
  768. return nla_put(skb, attrtype, sizeof(__le32), &value);
  769. }
  770. /**
  771. * nla_put_u64 - Add a u64 netlink attribute to a socket buffer
  772. * @skb: socket buffer to add attribute to
  773. * @attrtype: attribute type
  774. * @value: numeric value
  775. */
  776. static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
  777. {
  778. return nla_put(skb, attrtype, sizeof(u64), &value);
  779. }
  780. /**
  781. * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer
  782. * @skb: socket buffer to add attribute to
  783. * @attrtype: attribute type
  784. * @value: numeric value
  785. */
  786. static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value)
  787. {
  788. return nla_put(skb, attrtype, sizeof(__be64), &value);
  789. }
  790. /**
  791. * nla_put_net64 - Add 64-bit network byte order netlink attribute to a socket buffer
  792. * @skb: socket buffer to add attribute to
  793. * @attrtype: attribute type
  794. * @value: numeric value
  795. */
  796. static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value)
  797. {
  798. return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, value);
  799. }
  800. /**
  801. * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer
  802. * @skb: socket buffer to add attribute to
  803. * @attrtype: attribute type
  804. * @value: numeric value
  805. */
  806. static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value)
  807. {
  808. return nla_put(skb, attrtype, sizeof(__le64), &value);
  809. }
  810. /**
  811. * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
  812. * @skb: socket buffer to add attribute to
  813. * @attrtype: attribute type
  814. * @value: numeric value
  815. */
  816. static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
  817. {
  818. return nla_put(skb, attrtype, sizeof(s8), &value);
  819. }
  820. /**
  821. * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
  822. * @skb: socket buffer to add attribute to
  823. * @attrtype: attribute type
  824. * @value: numeric value
  825. */
  826. static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
  827. {
  828. return nla_put(skb, attrtype, sizeof(s16), &value);
  829. }
  830. /**
  831. * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
  832. * @skb: socket buffer to add attribute to
  833. * @attrtype: attribute type
  834. * @value: numeric value
  835. */
  836. static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
  837. {
  838. return nla_put(skb, attrtype, sizeof(s32), &value);
  839. }
  840. /**
  841. * nla_put_s64 - Add a s64 netlink attribute to a socket buffer
  842. * @skb: socket buffer to add attribute to
  843. * @attrtype: attribute type
  844. * @value: numeric value
  845. */
  846. static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value)
  847. {
  848. return nla_put(skb, attrtype, sizeof(s64), &value);
  849. }
  850. /**
  851. * nla_put_string - Add a string netlink attribute to a socket buffer
  852. * @skb: socket buffer to add attribute to
  853. * @attrtype: attribute type
  854. * @str: NUL terminated string
  855. */
  856. static inline int nla_put_string(struct sk_buff *skb, int attrtype,
  857. const char *str)
  858. {
  859. return nla_put(skb, attrtype, strlen(str) + 1, str);
  860. }
  861. /**
  862. * nla_put_flag - Add a flag netlink attribute to a socket buffer
  863. * @skb: socket buffer to add attribute to
  864. * @attrtype: attribute type
  865. */
  866. static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
  867. {
  868. return nla_put(skb, attrtype, 0, NULL);
  869. }
  870. /**
  871. * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
  872. * @skb: socket buffer to add attribute to
  873. * @attrtype: attribute type
  874. * @jiffies: number of msecs in jiffies
  875. */
  876. static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
  877. unsigned long jiffies)
  878. {
  879. u64 tmp = jiffies_to_msecs(jiffies);
  880. return nla_put(skb, attrtype, sizeof(u64), &tmp);
  881. }
  882. /**
  883. * nla_get_u32 - return payload of u32 attribute
  884. * @nla: u32 netlink attribute
  885. */
  886. static inline u32 nla_get_u32(const struct nlattr *nla)
  887. {
  888. return *(u32 *) nla_data(nla);
  889. }
  890. /**
  891. * nla_get_be32 - return payload of __be32 attribute
  892. * @nla: __be32 netlink attribute
  893. */
  894. static inline __be32 nla_get_be32(const struct nlattr *nla)
  895. {
  896. return *(__be32 *) nla_data(nla);
  897. }
  898. /**
  899. * nla_get_u16 - return payload of u16 attribute
  900. * @nla: u16 netlink attribute
  901. */
  902. static inline u16 nla_get_u16(const struct nlattr *nla)
  903. {
  904. return *(u16 *) nla_data(nla);
  905. }
  906. /**
  907. * nla_get_be16 - return payload of __be16 attribute
  908. * @nla: __be16 netlink attribute
  909. */
  910. static inline __be16 nla_get_be16(const struct nlattr *nla)
  911. {
  912. return *(__be16 *) nla_data(nla);
  913. }
  914. /**
  915. * nla_get_le16 - return payload of __le16 attribute
  916. * @nla: __le16 netlink attribute
  917. */
  918. static inline __le16 nla_get_le16(const struct nlattr *nla)
  919. {
  920. return *(__le16 *) nla_data(nla);
  921. }
  922. /**
  923. * nla_get_u8 - return payload of u8 attribute
  924. * @nla: u8 netlink attribute
  925. */
  926. static inline u8 nla_get_u8(const struct nlattr *nla)
  927. {
  928. return *(u8 *) nla_data(nla);
  929. }
  930. /**
  931. * nla_get_u64 - return payload of u64 attribute
  932. * @nla: u64 netlink attribute
  933. */
  934. static inline u64 nla_get_u64(const struct nlattr *nla)
  935. {
  936. u64 tmp;
  937. nla_memcpy(&tmp, nla, sizeof(tmp));
  938. return tmp;
  939. }
  940. /**
  941. * nla_get_be64 - return payload of __be64 attribute
  942. * @nla: __be64 netlink attribute
  943. */
  944. static inline __be64 nla_get_be64(const struct nlattr *nla)
  945. {
  946. __be64 tmp;
  947. nla_memcpy(&tmp, nla, sizeof(tmp));
  948. return tmp;
  949. }
  950. /**
  951. * nla_get_s32 - return payload of s32 attribute
  952. * @nla: s32 netlink attribute
  953. */
  954. static inline s32 nla_get_s32(const struct nlattr *nla)
  955. {
  956. return *(s32 *) nla_data(nla);
  957. }
  958. /**
  959. * nla_get_s16 - return payload of s16 attribute
  960. * @nla: s16 netlink attribute
  961. */
  962. static inline s16 nla_get_s16(const struct nlattr *nla)
  963. {
  964. return *(s16 *) nla_data(nla);
  965. }
  966. /**
  967. * nla_get_s8 - return payload of s8 attribute
  968. * @nla: s8 netlink attribute
  969. */
  970. static inline s8 nla_get_s8(const struct nlattr *nla)
  971. {
  972. return *(s8 *) nla_data(nla);
  973. }
  974. /**
  975. * nla_get_s64 - return payload of s64 attribute
  976. * @nla: s64 netlink attribute
  977. */
  978. static inline s64 nla_get_s64(const struct nlattr *nla)
  979. {
  980. s64 tmp;
  981. nla_memcpy(&tmp, nla, sizeof(tmp));
  982. return tmp;
  983. }
  984. /**
  985. * nla_get_flag - return payload of flag attribute
  986. * @nla: flag netlink attribute
  987. */
  988. static inline int nla_get_flag(const struct nlattr *nla)
  989. {
  990. return !!nla;
  991. }
  992. /**
  993. * nla_get_msecs - return payload of msecs attribute
  994. * @nla: msecs netlink attribute
  995. *
  996. * Returns the number of milliseconds in jiffies.
  997. */
  998. static inline unsigned long nla_get_msecs(const struct nlattr *nla)
  999. {
  1000. u64 msecs = nla_get_u64(nla);
  1001. return msecs_to_jiffies((unsigned long) msecs);
  1002. }
  1003. /**
  1004. * nla_nest_start - Start a new level of nested attributes
  1005. * @skb: socket buffer to add attributes to
  1006. * @attrtype: attribute type of container
  1007. *
  1008. * Returns the container attribute
  1009. */
  1010. static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
  1011. {
  1012. struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
  1013. if (nla_put(skb, attrtype, 0, NULL) < 0)
  1014. return NULL;
  1015. return start;
  1016. }
  1017. /**
  1018. * nla_nest_end - Finalize nesting of attributes
  1019. * @skb: socket buffer the attributes are stored in
  1020. * @start: container attribute
  1021. *
  1022. * Corrects the container attribute header to include the all
  1023. * appeneded attributes.
  1024. *
  1025. * Returns the total data length of the skb.
  1026. */
  1027. static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
  1028. {
  1029. start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
  1030. return skb->len;
  1031. }
  1032. /**
  1033. * nla_nest_cancel - Cancel nesting of attributes
  1034. * @skb: socket buffer the message is stored in
  1035. * @start: container attribute
  1036. *
  1037. * Removes the container attribute and including all nested
  1038. * attributes. Returns -EMSGSIZE
  1039. */
  1040. static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
  1041. {
  1042. nlmsg_trim(skb, start);
  1043. }
  1044. /**
  1045. * nla_validate_nested - Validate a stream of nested attributes
  1046. * @start: container attribute
  1047. * @maxtype: maximum attribute type to be expected
  1048. * @policy: validation policy
  1049. *
  1050. * Validates all attributes in the nested attribute stream against the
  1051. * specified policy. Attributes with a type exceeding maxtype will be
  1052. * ignored. See documenation of struct nla_policy for more details.
  1053. *
  1054. * Returns 0 on success or a negative error code.
  1055. */
  1056. static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
  1057. const struct nla_policy *policy)
  1058. {
  1059. return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
  1060. }
  1061. /**
  1062. * nla_for_each_attr - iterate over a stream of attributes
  1063. * @pos: loop counter, set to current attribute
  1064. * @head: head of attribute stream
  1065. * @len: length of attribute stream
  1066. * @rem: initialized to len, holds bytes currently remaining in stream
  1067. */
  1068. #define nla_for_each_attr(pos, head, len, rem) \
  1069. for (pos = head, rem = len; \
  1070. nla_ok(pos, rem); \
  1071. pos = nla_next(pos, &(rem)))
  1072. /**
  1073. * nla_for_each_nested - iterate over nested attributes
  1074. * @pos: loop counter, set to current attribute
  1075. * @nla: attribute containing the nested attributes
  1076. * @rem: initialized to len, holds bytes currently remaining in stream
  1077. */
  1078. #define nla_for_each_nested(pos, nla, rem) \
  1079. nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
  1080. #endif