netlink.h 32 KB

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