netlink.h 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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. #include <linux/in6.h>
  7. /* ========================================================================
  8. * Netlink Messages and Attributes Interface (As Seen On TV)
  9. * ------------------------------------------------------------------------
  10. * Messages Interface
  11. * ------------------------------------------------------------------------
  12. *
  13. * Message Format:
  14. * <--- nlmsg_total_size(payload) --->
  15. * <-- nlmsg_msg_size(payload) ->
  16. * +----------+- - -+-------------+- - -+-------- - -
  17. * | nlmsghdr | Pad | Payload | Pad | nlmsghdr
  18. * +----------+- - -+-------------+- - -+-------- - -
  19. * nlmsg_data(nlh)---^ ^
  20. * nlmsg_next(nlh)-----------------------+
  21. *
  22. * Payload Format:
  23. * <---------------------- nlmsg_len(nlh) --------------------->
  24. * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) ->
  25. * +----------------------+- - -+--------------------------------+
  26. * | Family Header | Pad | Attributes |
  27. * +----------------------+- - -+--------------------------------+
  28. * nlmsg_attrdata(nlh, hdrlen)---^
  29. *
  30. * Data Structures:
  31. * struct nlmsghdr netlink message header
  32. *
  33. * Message Construction:
  34. * nlmsg_new() create a new netlink message
  35. * nlmsg_put() add a netlink message to an skb
  36. * nlmsg_put_answer() callback based nlmsg_put()
  37. * nlmsg_end() finalize netlink message
  38. * nlmsg_get_pos() return current position in message
  39. * nlmsg_trim() trim part of message
  40. * nlmsg_cancel() cancel message construction
  41. * nlmsg_free() free a netlink message
  42. *
  43. * Message Sending:
  44. * nlmsg_multicast() multicast message to several groups
  45. * nlmsg_unicast() unicast a message to a single socket
  46. * nlmsg_notify() send notification message
  47. *
  48. * Message Length Calculations:
  49. * nlmsg_msg_size(payload) length of message w/o padding
  50. * nlmsg_total_size(payload) length of message w/ padding
  51. * nlmsg_padlen(payload) length of padding at tail
  52. *
  53. * Message Payload Access:
  54. * nlmsg_data(nlh) head of message payload
  55. * nlmsg_len(nlh) length of message payload
  56. * nlmsg_attrdata(nlh, hdrlen) head of attributes data
  57. * nlmsg_attrlen(nlh, hdrlen) length of attributes data
  58. *
  59. * Message Parsing:
  60. * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes?
  61. * nlmsg_next(nlh, remaining) get next netlink message
  62. * nlmsg_parse() parse attributes of a message
  63. * nlmsg_find_attr() find an attribute in a message
  64. * nlmsg_for_each_msg() loop over all messages
  65. * nlmsg_validate() validate netlink message incl. attrs
  66. * nlmsg_for_each_attr() loop over all attributes
  67. *
  68. * Misc:
  69. * nlmsg_report() report back to application?
  70. *
  71. * ------------------------------------------------------------------------
  72. * Attributes Interface
  73. * ------------------------------------------------------------------------
  74. *
  75. * Attribute Format:
  76. * <------- nla_total_size(payload) ------->
  77. * <---- nla_attr_size(payload) ----->
  78. * +----------+- - -+- - - - - - - - - +- - -+-------- - -
  79. * | Header | Pad | Payload | Pad | Header
  80. * +----------+- - -+- - - - - - - - - +- - -+-------- - -
  81. * <- nla_len(nla) -> ^
  82. * nla_data(nla)----^ |
  83. * nla_next(nla)-----------------------------'
  84. *
  85. * Data Structures:
  86. * struct nlattr netlink attribute header
  87. *
  88. * Attribute Construction:
  89. * nla_reserve(skb, type, len) reserve room for an attribute
  90. * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr
  91. * nla_put(skb, type, len, data) add attribute to skb
  92. * nla_put_nohdr(skb, len, data) add attribute w/o hdr
  93. * nla_append(skb, len, data) append data to skb
  94. *
  95. * Attribute Construction for Basic Types:
  96. * nla_put_u8(skb, type, value) add u8 attribute to skb
  97. * nla_put_u16(skb, type, value) add u16 attribute to skb
  98. * nla_put_u32(skb, type, value) add u32 attribute to skb
  99. * nla_put_u64(skb, type, value) add u64 attribute to skb
  100. * nla_put_s8(skb, type, value) add s8 attribute to skb
  101. * nla_put_s16(skb, type, value) add s16 attribute to skb
  102. * nla_put_s32(skb, type, value) add s32 attribute to skb
  103. * nla_put_s64(skb, type, value) add s64 attribute to skb
  104. * nla_put_string(skb, type, str) add string attribute to skb
  105. * nla_put_flag(skb, type) add flag attribute to skb
  106. * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb
  107. * nla_put_in_addr(skb, type, addr) add IPv4 address attribute to skb
  108. * nla_put_in6_addr(skb, type, addr) add IPv6 address attribute to skb
  109. *
  110. * Nested Attributes Construction:
  111. * nla_nest_start(skb, type) start a nested attribute
  112. * nla_nest_end(skb, nla) finalize a nested attribute
  113. * nla_nest_cancel(skb, nla) cancel nested attribute construction
  114. *
  115. * Attribute Length Calculations:
  116. * nla_attr_size(payload) length of attribute w/o padding
  117. * nla_total_size(payload) length of attribute w/ padding
  118. * nla_padlen(payload) length of padding
  119. *
  120. * Attribute Payload Access:
  121. * nla_data(nla) head of attribute payload
  122. * nla_len(nla) length of attribute payload
  123. *
  124. * Attribute Payload Access for Basic Types:
  125. * nla_get_u8(nla) get payload for a u8 attribute
  126. * nla_get_u16(nla) get payload for a u16 attribute
  127. * nla_get_u32(nla) get payload for a u32 attribute
  128. * nla_get_u64(nla) get payload for a u64 attribute
  129. * nla_get_s8(nla) get payload for a s8 attribute
  130. * nla_get_s16(nla) get payload for a s16 attribute
  131. * nla_get_s32(nla) get payload for a s32 attribute
  132. * nla_get_s64(nla) get payload for a s64 attribute
  133. * nla_get_flag(nla) return 1 if flag is true
  134. * nla_get_msecs(nla) get payload for a msecs attribute
  135. *
  136. * Attribute Misc:
  137. * nla_memcpy(dest, nla, count) copy attribute into memory
  138. * nla_memcmp(nla, data, size) compare attribute with memory area
  139. * nla_strlcpy(dst, nla, size) copy attribute to a sized string
  140. * nla_strcmp(nla, str) compare attribute with string
  141. *
  142. * Attribute Parsing:
  143. * nla_ok(nla, remaining) does nla fit into remaining bytes?
  144. * nla_next(nla, remaining) get next netlink attribute
  145. * nla_validate() validate a stream of attributes
  146. * nla_validate_nested() validate a stream of nested attributes
  147. * nla_find() find attribute in stream of attributes
  148. * nla_find_nested() find attribute in nested attributes
  149. * nla_parse() parse and validate stream of attrs
  150. * nla_parse_nested() parse nested attribuets
  151. * nla_for_each_attr() loop over all attributes
  152. * nla_for_each_nested() loop over the nested attributes
  153. *=========================================================================
  154. */
  155. /**
  156. * Standard attribute types to specify validation policy
  157. */
  158. enum {
  159. NLA_UNSPEC,
  160. NLA_U8,
  161. NLA_U16,
  162. NLA_U32,
  163. NLA_U64,
  164. NLA_STRING,
  165. NLA_FLAG,
  166. NLA_MSECS,
  167. NLA_NESTED,
  168. NLA_NESTED_COMPAT,
  169. NLA_NUL_STRING,
  170. NLA_BINARY,
  171. NLA_S8,
  172. NLA_S16,
  173. NLA_S32,
  174. NLA_S64,
  175. __NLA_TYPE_MAX,
  176. };
  177. #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
  178. /**
  179. * struct nla_policy - attribute validation policy
  180. * @type: Type of attribute or NLA_UNSPEC
  181. * @len: Type specific length of payload
  182. *
  183. * Policies are defined as arrays of this struct, the array must be
  184. * accessible by attribute type up to the highest identifier to be expected.
  185. *
  186. * Meaning of `len' field:
  187. * NLA_STRING Maximum length of string
  188. * NLA_NUL_STRING Maximum length of string (excluding NUL)
  189. * NLA_FLAG Unused
  190. * NLA_BINARY Maximum length of attribute payload
  191. * NLA_NESTED Don't use `len' field -- length verification is
  192. * done by checking len of nested header (or empty)
  193. * NLA_NESTED_COMPAT Minimum length of structure payload
  194. * NLA_U8, NLA_U16,
  195. * NLA_U32, NLA_U64,
  196. * NLA_S8, NLA_S16,
  197. * NLA_S32, NLA_S64,
  198. * NLA_MSECS Leaving the length field zero will verify the
  199. * given type fits, using it verifies minimum length
  200. * just like "All other"
  201. * All other Minimum length of attribute payload
  202. *
  203. * Example:
  204. * static const struct nla_policy my_policy[ATTR_MAX+1] = {
  205. * [ATTR_FOO] = { .type = NLA_U16 },
  206. * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
  207. * [ATTR_BAZ] = { .len = sizeof(struct mystruct) },
  208. * };
  209. */
  210. struct nla_policy {
  211. u16 type;
  212. u16 len;
  213. };
  214. /**
  215. * struct nl_info - netlink source information
  216. * @nlh: Netlink message header of original request
  217. * @portid: Netlink PORTID of requesting application
  218. */
  219. struct nl_info {
  220. struct nlmsghdr *nlh;
  221. struct net *nl_net;
  222. u32 portid;
  223. };
  224. int netlink_rcv_skb(struct sk_buff *skb,
  225. int (*cb)(struct sk_buff *, struct nlmsghdr *));
  226. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  227. unsigned int group, int report, gfp_t flags);
  228. int nla_validate(const struct nlattr *head, int len, int maxtype,
  229. const struct nla_policy *policy);
  230. int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
  231. int len, const struct nla_policy *policy);
  232. int nla_policy_len(const struct nla_policy *, int);
  233. struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
  234. size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize);
  235. int nla_memcpy(void *dest, const struct nlattr *src, int count);
  236. int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
  237. int nla_strcmp(const struct nlattr *nla, const char *str);
  238. struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
  239. void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
  240. struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
  241. void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
  242. void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
  243. const void *data);
  244. void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
  245. int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
  246. int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
  247. int nla_append(struct sk_buff *skb, int attrlen, const void *data);
  248. /**************************************************************************
  249. * Netlink Messages
  250. **************************************************************************/
  251. /**
  252. * nlmsg_msg_size - length of netlink message not including padding
  253. * @payload: length of message payload
  254. */
  255. static inline int nlmsg_msg_size(int payload)
  256. {
  257. return NLMSG_HDRLEN + payload;
  258. }
  259. /**
  260. * nlmsg_total_size - length of netlink message including padding
  261. * @payload: length of message payload
  262. */
  263. static inline int nlmsg_total_size(int payload)
  264. {
  265. return NLMSG_ALIGN(nlmsg_msg_size(payload));
  266. }
  267. /**
  268. * nlmsg_padlen - length of padding at the message's tail
  269. * @payload: length of message payload
  270. */
  271. static inline int nlmsg_padlen(int payload)
  272. {
  273. return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
  274. }
  275. /**
  276. * nlmsg_data - head of message payload
  277. * @nlh: netlink message header
  278. */
  279. static inline void *nlmsg_data(const struct nlmsghdr *nlh)
  280. {
  281. return (unsigned char *) nlh + NLMSG_HDRLEN;
  282. }
  283. /**
  284. * nlmsg_len - length of message payload
  285. * @nlh: netlink message header
  286. */
  287. static inline int nlmsg_len(const struct nlmsghdr *nlh)
  288. {
  289. return nlh->nlmsg_len - NLMSG_HDRLEN;
  290. }
  291. /**
  292. * nlmsg_attrdata - head of attributes data
  293. * @nlh: netlink message header
  294. * @hdrlen: length of family specific header
  295. */
  296. static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
  297. int hdrlen)
  298. {
  299. unsigned char *data = nlmsg_data(nlh);
  300. return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
  301. }
  302. /**
  303. * nlmsg_attrlen - length of attributes data
  304. * @nlh: netlink message header
  305. * @hdrlen: length of family specific header
  306. */
  307. static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
  308. {
  309. return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
  310. }
  311. /**
  312. * nlmsg_ok - check if the netlink message fits into the remaining bytes
  313. * @nlh: netlink message header
  314. * @remaining: number of bytes remaining in message stream
  315. */
  316. static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
  317. {
  318. return (remaining >= (int) sizeof(struct nlmsghdr) &&
  319. nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
  320. nlh->nlmsg_len <= remaining);
  321. }
  322. /**
  323. * nlmsg_next - next netlink message in message stream
  324. * @nlh: netlink message header
  325. * @remaining: number of bytes remaining in message stream
  326. *
  327. * Returns the next netlink message in the message stream and
  328. * decrements remaining by the size of the current message.
  329. */
  330. static inline struct nlmsghdr *
  331. nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
  332. {
  333. int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
  334. *remaining -= totlen;
  335. return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
  336. }
  337. /**
  338. * nlmsg_parse - parse attributes of a netlink message
  339. * @nlh: netlink message header
  340. * @hdrlen: length of family specific header
  341. * @tb: destination array with maxtype+1 elements
  342. * @maxtype: maximum attribute type to be expected
  343. * @policy: validation policy
  344. *
  345. * See nla_parse()
  346. */
  347. static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
  348. struct nlattr *tb[], int maxtype,
  349. const struct nla_policy *policy)
  350. {
  351. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  352. return -EINVAL;
  353. return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
  354. nlmsg_attrlen(nlh, hdrlen), policy);
  355. }
  356. /**
  357. * nlmsg_find_attr - find a specific attribute in a netlink message
  358. * @nlh: netlink message header
  359. * @hdrlen: length of familiy specific header
  360. * @attrtype: type of attribute to look for
  361. *
  362. * Returns the first attribute which matches the specified type.
  363. */
  364. static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
  365. int hdrlen, int attrtype)
  366. {
  367. return nla_find(nlmsg_attrdata(nlh, hdrlen),
  368. nlmsg_attrlen(nlh, hdrlen), attrtype);
  369. }
  370. /**
  371. * nlmsg_validate - validate a netlink message including attributes
  372. * @nlh: netlinket message header
  373. * @hdrlen: length of familiy specific header
  374. * @maxtype: maximum attribute type to be expected
  375. * @policy: validation policy
  376. */
  377. static inline int nlmsg_validate(const struct nlmsghdr *nlh,
  378. int hdrlen, int maxtype,
  379. const struct nla_policy *policy)
  380. {
  381. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  382. return -EINVAL;
  383. return nla_validate(nlmsg_attrdata(nlh, hdrlen),
  384. nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
  385. }
  386. /**
  387. * nlmsg_report - need to report back to application?
  388. * @nlh: netlink message header
  389. *
  390. * Returns 1 if a report back to the application is requested.
  391. */
  392. static inline int nlmsg_report(const struct nlmsghdr *nlh)
  393. {
  394. return !!(nlh->nlmsg_flags & NLM_F_ECHO);
  395. }
  396. /**
  397. * nlmsg_for_each_attr - iterate over a stream of attributes
  398. * @pos: loop counter, set to current attribute
  399. * @nlh: netlink message header
  400. * @hdrlen: length of familiy specific header
  401. * @rem: initialized to len, holds bytes currently remaining in stream
  402. */
  403. #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
  404. nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
  405. nlmsg_attrlen(nlh, hdrlen), rem)
  406. /**
  407. * nlmsg_put - Add a new netlink message to an skb
  408. * @skb: socket buffer to store message in
  409. * @portid: netlink PORTID of requesting application
  410. * @seq: sequence number of message
  411. * @type: message type
  412. * @payload: length of message payload
  413. * @flags: message flags
  414. *
  415. * Returns NULL if the tailroom of the skb is insufficient to store
  416. * the message header and payload.
  417. */
  418. static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  419. int type, int payload, int flags)
  420. {
  421. if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
  422. return NULL;
  423. return __nlmsg_put(skb, portid, seq, type, payload, flags);
  424. }
  425. /**
  426. * nlmsg_put_answer - Add a new callback based netlink message to an skb
  427. * @skb: socket buffer to store message in
  428. * @cb: netlink callback
  429. * @type: message type
  430. * @payload: length of message payload
  431. * @flags: message flags
  432. *
  433. * Returns NULL if the tailroom of the skb is insufficient to store
  434. * the message header and payload.
  435. */
  436. static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
  437. struct netlink_callback *cb,
  438. int type, int payload,
  439. int flags)
  440. {
  441. return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  442. type, payload, flags);
  443. }
  444. /**
  445. * nlmsg_new - Allocate a new netlink message
  446. * @payload: size of the message payload
  447. * @flags: the type of memory to allocate.
  448. *
  449. * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
  450. * and a good default is needed.
  451. */
  452. static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
  453. {
  454. return alloc_skb(nlmsg_total_size(payload), flags);
  455. }
  456. /**
  457. * nlmsg_end - Finalize a netlink message
  458. * @skb: socket buffer the message is stored in
  459. * @nlh: netlink message header
  460. *
  461. * Corrects the netlink message header to include the appeneded
  462. * attributes. Only necessary if attributes have been added to
  463. * the message.
  464. */
  465. static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
  466. {
  467. nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
  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_put_in_addr - Add an IPv4 address netlink attribute to a socket
  886. * buffer
  887. * @skb: socket buffer to add attribute to
  888. * @attrtype: attribute type
  889. * @addr: IPv4 address
  890. */
  891. static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
  892. __be32 addr)
  893. {
  894. return nla_put_be32(skb, attrtype, addr);
  895. }
  896. /**
  897. * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
  898. * buffer
  899. * @skb: socket buffer to add attribute to
  900. * @attrtype: attribute type
  901. * @addr: IPv6 address
  902. */
  903. static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
  904. const struct in6_addr *addr)
  905. {
  906. return nla_put(skb, attrtype, sizeof(*addr), addr);
  907. }
  908. /**
  909. * nla_get_u32 - return payload of u32 attribute
  910. * @nla: u32 netlink attribute
  911. */
  912. static inline u32 nla_get_u32(const struct nlattr *nla)
  913. {
  914. return *(u32 *) nla_data(nla);
  915. }
  916. /**
  917. * nla_get_be32 - return payload of __be32 attribute
  918. * @nla: __be32 netlink attribute
  919. */
  920. static inline __be32 nla_get_be32(const struct nlattr *nla)
  921. {
  922. return *(__be32 *) nla_data(nla);
  923. }
  924. /**
  925. * nla_get_le32 - return payload of __le32 attribute
  926. * @nla: __le32 netlink attribute
  927. */
  928. static inline __le32 nla_get_le32(const struct nlattr *nla)
  929. {
  930. return *(__le32 *) nla_data(nla);
  931. }
  932. /**
  933. * nla_get_u16 - return payload of u16 attribute
  934. * @nla: u16 netlink attribute
  935. */
  936. static inline u16 nla_get_u16(const struct nlattr *nla)
  937. {
  938. return *(u16 *) nla_data(nla);
  939. }
  940. /**
  941. * nla_get_be16 - return payload of __be16 attribute
  942. * @nla: __be16 netlink attribute
  943. */
  944. static inline __be16 nla_get_be16(const struct nlattr *nla)
  945. {
  946. return *(__be16 *) nla_data(nla);
  947. }
  948. /**
  949. * nla_get_le16 - return payload of __le16 attribute
  950. * @nla: __le16 netlink attribute
  951. */
  952. static inline __le16 nla_get_le16(const struct nlattr *nla)
  953. {
  954. return *(__le16 *) nla_data(nla);
  955. }
  956. /**
  957. * nla_get_u8 - return payload of u8 attribute
  958. * @nla: u8 netlink attribute
  959. */
  960. static inline u8 nla_get_u8(const struct nlattr *nla)
  961. {
  962. return *(u8 *) nla_data(nla);
  963. }
  964. /**
  965. * nla_get_u64 - return payload of u64 attribute
  966. * @nla: u64 netlink attribute
  967. */
  968. static inline u64 nla_get_u64(const struct nlattr *nla)
  969. {
  970. u64 tmp;
  971. nla_memcpy(&tmp, nla, sizeof(tmp));
  972. return tmp;
  973. }
  974. /**
  975. * nla_get_be64 - return payload of __be64 attribute
  976. * @nla: __be64 netlink attribute
  977. */
  978. static inline __be64 nla_get_be64(const struct nlattr *nla)
  979. {
  980. __be64 tmp;
  981. nla_memcpy(&tmp, nla, sizeof(tmp));
  982. return tmp;
  983. }
  984. /**
  985. * nla_get_le64 - return payload of __le64 attribute
  986. * @nla: __le64 netlink attribute
  987. */
  988. static inline __le64 nla_get_le64(const struct nlattr *nla)
  989. {
  990. return *(__le64 *) nla_data(nla);
  991. }
  992. /**
  993. * nla_get_s32 - return payload of s32 attribute
  994. * @nla: s32 netlink attribute
  995. */
  996. static inline s32 nla_get_s32(const struct nlattr *nla)
  997. {
  998. return *(s32 *) nla_data(nla);
  999. }
  1000. /**
  1001. * nla_get_s16 - return payload of s16 attribute
  1002. * @nla: s16 netlink attribute
  1003. */
  1004. static inline s16 nla_get_s16(const struct nlattr *nla)
  1005. {
  1006. return *(s16 *) nla_data(nla);
  1007. }
  1008. /**
  1009. * nla_get_s8 - return payload of s8 attribute
  1010. * @nla: s8 netlink attribute
  1011. */
  1012. static inline s8 nla_get_s8(const struct nlattr *nla)
  1013. {
  1014. return *(s8 *) nla_data(nla);
  1015. }
  1016. /**
  1017. * nla_get_s64 - return payload of s64 attribute
  1018. * @nla: s64 netlink attribute
  1019. */
  1020. static inline s64 nla_get_s64(const struct nlattr *nla)
  1021. {
  1022. s64 tmp;
  1023. nla_memcpy(&tmp, nla, sizeof(tmp));
  1024. return tmp;
  1025. }
  1026. /**
  1027. * nla_get_flag - return payload of flag attribute
  1028. * @nla: flag netlink attribute
  1029. */
  1030. static inline int nla_get_flag(const struct nlattr *nla)
  1031. {
  1032. return !!nla;
  1033. }
  1034. /**
  1035. * nla_get_msecs - return payload of msecs attribute
  1036. * @nla: msecs netlink attribute
  1037. *
  1038. * Returns the number of milliseconds in jiffies.
  1039. */
  1040. static inline unsigned long nla_get_msecs(const struct nlattr *nla)
  1041. {
  1042. u64 msecs = nla_get_u64(nla);
  1043. return msecs_to_jiffies((unsigned long) msecs);
  1044. }
  1045. /**
  1046. * nla_get_in_addr - return payload of IPv4 address attribute
  1047. * @nla: IPv4 address netlink attribute
  1048. */
  1049. static inline __be32 nla_get_in_addr(const struct nlattr *nla)
  1050. {
  1051. return *(__be32 *) nla_data(nla);
  1052. }
  1053. /**
  1054. * nla_get_in6_addr - return payload of IPv6 address attribute
  1055. * @nla: IPv6 address netlink attribute
  1056. */
  1057. static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla)
  1058. {
  1059. struct in6_addr tmp;
  1060. nla_memcpy(&tmp, nla, sizeof(tmp));
  1061. return tmp;
  1062. }
  1063. /**
  1064. * nla_nest_start - Start a new level of nested attributes
  1065. * @skb: socket buffer to add attributes to
  1066. * @attrtype: attribute type of container
  1067. *
  1068. * Returns the container attribute
  1069. */
  1070. static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
  1071. {
  1072. struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
  1073. if (nla_put(skb, attrtype, 0, NULL) < 0)
  1074. return NULL;
  1075. return start;
  1076. }
  1077. /**
  1078. * nla_nest_end - Finalize nesting of attributes
  1079. * @skb: socket buffer the attributes are stored in
  1080. * @start: container attribute
  1081. *
  1082. * Corrects the container attribute header to include the all
  1083. * appeneded attributes.
  1084. *
  1085. * Returns the total data length of the skb.
  1086. */
  1087. static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
  1088. {
  1089. start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
  1090. return skb->len;
  1091. }
  1092. /**
  1093. * nla_nest_cancel - Cancel nesting of attributes
  1094. * @skb: socket buffer the message is stored in
  1095. * @start: container attribute
  1096. *
  1097. * Removes the container attribute and including all nested
  1098. * attributes. Returns -EMSGSIZE
  1099. */
  1100. static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
  1101. {
  1102. nlmsg_trim(skb, start);
  1103. }
  1104. /**
  1105. * nla_validate_nested - Validate a stream of nested attributes
  1106. * @start: container attribute
  1107. * @maxtype: maximum attribute type to be expected
  1108. * @policy: validation policy
  1109. *
  1110. * Validates all attributes in the nested attribute stream against the
  1111. * specified policy. Attributes with a type exceeding maxtype will be
  1112. * ignored. See documenation of struct nla_policy for more details.
  1113. *
  1114. * Returns 0 on success or a negative error code.
  1115. */
  1116. static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
  1117. const struct nla_policy *policy)
  1118. {
  1119. return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
  1120. }
  1121. /**
  1122. * nla_for_each_attr - iterate over a stream of attributes
  1123. * @pos: loop counter, set to current attribute
  1124. * @head: head of attribute stream
  1125. * @len: length of attribute stream
  1126. * @rem: initialized to len, holds bytes currently remaining in stream
  1127. */
  1128. #define nla_for_each_attr(pos, head, len, rem) \
  1129. for (pos = head, rem = len; \
  1130. nla_ok(pos, rem); \
  1131. pos = nla_next(pos, &(rem)))
  1132. /**
  1133. * nla_for_each_nested - iterate over nested attributes
  1134. * @pos: loop counter, set to current attribute
  1135. * @nla: attribute containing the nested attributes
  1136. * @rem: initialized to len, holds bytes currently remaining in stream
  1137. */
  1138. #define nla_for_each_nested(pos, nla, rem) \
  1139. nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
  1140. /**
  1141. * nla_is_last - Test if attribute is last in stream
  1142. * @nla: attribute to test
  1143. * @rem: bytes remaining in stream
  1144. */
  1145. static inline bool nla_is_last(const struct nlattr *nla, int rem)
  1146. {
  1147. return nla->nla_len == rem;
  1148. }
  1149. #endif