netlink.h 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. /* temporary variables to work around GCC PR81715 with asan-stack=1 */
  691. u8 tmp = value;
  692. return nla_put(skb, attrtype, sizeof(u8), &tmp);
  693. }
  694. /**
  695. * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
  696. * @skb: socket buffer to add attribute to
  697. * @attrtype: attribute type
  698. * @value: numeric value
  699. */
  700. static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
  701. {
  702. u16 tmp = value;
  703. return nla_put(skb, attrtype, sizeof(u16), &tmp);
  704. }
  705. /**
  706. * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer
  707. * @skb: socket buffer to add attribute to
  708. * @attrtype: attribute type
  709. * @value: numeric value
  710. */
  711. static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
  712. {
  713. __be16 tmp = value;
  714. return nla_put(skb, attrtype, sizeof(__be16), &tmp);
  715. }
  716. /**
  717. * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
  718. * @skb: socket buffer to add attribute to
  719. * @attrtype: attribute type
  720. * @value: numeric value
  721. */
  722. static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
  723. {
  724. __be16 tmp = value;
  725. return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
  726. }
  727. /**
  728. * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
  729. * @skb: socket buffer to add attribute to
  730. * @attrtype: attribute type
  731. * @value: numeric value
  732. */
  733. static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
  734. {
  735. __le16 tmp = value;
  736. return nla_put(skb, attrtype, sizeof(__le16), &tmp);
  737. }
  738. /**
  739. * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
  740. * @skb: socket buffer to add attribute to
  741. * @attrtype: attribute type
  742. * @value: numeric value
  743. */
  744. static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
  745. {
  746. u32 tmp = value;
  747. return nla_put(skb, attrtype, sizeof(u32), &tmp);
  748. }
  749. /**
  750. * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
  751. * @skb: socket buffer to add attribute to
  752. * @attrtype: attribute type
  753. * @value: numeric value
  754. */
  755. static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
  756. {
  757. __be32 tmp = value;
  758. return nla_put(skb, attrtype, sizeof(__be32), &tmp);
  759. }
  760. /**
  761. * nla_put_net32 - Add 32-bit network byte order 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_net32(struct sk_buff *skb, int attrtype, __be32 value)
  767. {
  768. __be32 tmp = value;
  769. return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
  770. }
  771. /**
  772. * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer
  773. * @skb: socket buffer to add attribute to
  774. * @attrtype: attribute type
  775. * @value: numeric value
  776. */
  777. static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
  778. {
  779. __le32 tmp = value;
  780. return nla_put(skb, attrtype, sizeof(__le32), &tmp);
  781. }
  782. /**
  783. * nla_put_u64 - Add a u64 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_u64(struct sk_buff *skb, int attrtype, u64 value)
  789. {
  790. u64 tmp = value;
  791. return nla_put(skb, attrtype, sizeof(u64), &tmp);
  792. }
  793. /**
  794. * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer
  795. * @skb: socket buffer to add attribute to
  796. * @attrtype: attribute type
  797. * @value: numeric value
  798. */
  799. static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value)
  800. {
  801. __be64 tmp = value;
  802. return nla_put(skb, attrtype, sizeof(__be64), &tmp);
  803. }
  804. /**
  805. * nla_put_net64 - Add 64-bit network byte order netlink attribute to a socket buffer
  806. * @skb: socket buffer to add attribute to
  807. * @attrtype: attribute type
  808. * @value: numeric value
  809. */
  810. static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value)
  811. {
  812. __be64 tmp = value;
  813. return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
  814. }
  815. /**
  816. * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer
  817. * @skb: socket buffer to add attribute to
  818. * @attrtype: attribute type
  819. * @value: numeric value
  820. */
  821. static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value)
  822. {
  823. __le64 tmp = value;
  824. return nla_put(skb, attrtype, sizeof(__le64), &tmp);
  825. }
  826. /**
  827. * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
  828. * @skb: socket buffer to add attribute to
  829. * @attrtype: attribute type
  830. * @value: numeric value
  831. */
  832. static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
  833. {
  834. s8 tmp = value;
  835. return nla_put(skb, attrtype, sizeof(s8), &tmp);
  836. }
  837. /**
  838. * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
  839. * @skb: socket buffer to add attribute to
  840. * @attrtype: attribute type
  841. * @value: numeric value
  842. */
  843. static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
  844. {
  845. s16 tmp = value;
  846. return nla_put(skb, attrtype, sizeof(s16), &tmp);
  847. }
  848. /**
  849. * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
  850. * @skb: socket buffer to add attribute to
  851. * @attrtype: attribute type
  852. * @value: numeric value
  853. */
  854. static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
  855. {
  856. s32 tmp = value;
  857. return nla_put(skb, attrtype, sizeof(s32), &tmp);
  858. }
  859. /**
  860. * nla_put_s64 - Add a s64 netlink attribute to a socket buffer
  861. * @skb: socket buffer to add attribute to
  862. * @attrtype: attribute type
  863. * @value: numeric value
  864. */
  865. static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value)
  866. {
  867. s64 tmp = value;
  868. return nla_put(skb, attrtype, sizeof(s64), &tmp);
  869. }
  870. /**
  871. * nla_put_string - Add a string netlink attribute to a socket buffer
  872. * @skb: socket buffer to add attribute to
  873. * @attrtype: attribute type
  874. * @str: NUL terminated string
  875. */
  876. static inline int nla_put_string(struct sk_buff *skb, int attrtype,
  877. const char *str)
  878. {
  879. return nla_put(skb, attrtype, strlen(str) + 1, str);
  880. }
  881. /**
  882. * nla_put_flag - Add a flag netlink attribute to a socket buffer
  883. * @skb: socket buffer to add attribute to
  884. * @attrtype: attribute type
  885. */
  886. static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
  887. {
  888. return nla_put(skb, attrtype, 0, NULL);
  889. }
  890. /**
  891. * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
  892. * @skb: socket buffer to add attribute to
  893. * @attrtype: attribute type
  894. * @njiffies: number of jiffies to convert to msecs
  895. */
  896. static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
  897. unsigned long njiffies)
  898. {
  899. u64 tmp = jiffies_to_msecs(njiffies);
  900. return nla_put(skb, attrtype, sizeof(u64), &tmp);
  901. }
  902. /**
  903. * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket
  904. * buffer
  905. * @skb: socket buffer to add attribute to
  906. * @attrtype: attribute type
  907. * @addr: IPv4 address
  908. */
  909. static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
  910. __be32 addr)
  911. {
  912. __be32 tmp = addr;
  913. return nla_put_be32(skb, attrtype, tmp);
  914. }
  915. /**
  916. * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
  917. * buffer
  918. * @skb: socket buffer to add attribute to
  919. * @attrtype: attribute type
  920. * @addr: IPv6 address
  921. */
  922. static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
  923. const struct in6_addr *addr)
  924. {
  925. return nla_put(skb, attrtype, sizeof(*addr), addr);
  926. }
  927. /**
  928. * nla_get_u32 - return payload of u32 attribute
  929. * @nla: u32 netlink attribute
  930. */
  931. static inline u32 nla_get_u32(const struct nlattr *nla)
  932. {
  933. return *(u32 *) nla_data(nla);
  934. }
  935. /**
  936. * nla_get_be32 - return payload of __be32 attribute
  937. * @nla: __be32 netlink attribute
  938. */
  939. static inline __be32 nla_get_be32(const struct nlattr *nla)
  940. {
  941. return *(__be32 *) nla_data(nla);
  942. }
  943. /**
  944. * nla_get_le32 - return payload of __le32 attribute
  945. * @nla: __le32 netlink attribute
  946. */
  947. static inline __le32 nla_get_le32(const struct nlattr *nla)
  948. {
  949. return *(__le32 *) nla_data(nla);
  950. }
  951. /**
  952. * nla_get_u16 - return payload of u16 attribute
  953. * @nla: u16 netlink attribute
  954. */
  955. static inline u16 nla_get_u16(const struct nlattr *nla)
  956. {
  957. return *(u16 *) nla_data(nla);
  958. }
  959. /**
  960. * nla_get_be16 - return payload of __be16 attribute
  961. * @nla: __be16 netlink attribute
  962. */
  963. static inline __be16 nla_get_be16(const struct nlattr *nla)
  964. {
  965. return *(__be16 *) nla_data(nla);
  966. }
  967. /**
  968. * nla_get_le16 - return payload of __le16 attribute
  969. * @nla: __le16 netlink attribute
  970. */
  971. static inline __le16 nla_get_le16(const struct nlattr *nla)
  972. {
  973. return *(__le16 *) nla_data(nla);
  974. }
  975. /**
  976. * nla_get_u8 - return payload of u8 attribute
  977. * @nla: u8 netlink attribute
  978. */
  979. static inline u8 nla_get_u8(const struct nlattr *nla)
  980. {
  981. return *(u8 *) nla_data(nla);
  982. }
  983. /**
  984. * nla_get_u64 - return payload of u64 attribute
  985. * @nla: u64 netlink attribute
  986. */
  987. static inline u64 nla_get_u64(const struct nlattr *nla)
  988. {
  989. u64 tmp;
  990. nla_memcpy(&tmp, nla, sizeof(tmp));
  991. return tmp;
  992. }
  993. /**
  994. * nla_get_be64 - return payload of __be64 attribute
  995. * @nla: __be64 netlink attribute
  996. */
  997. static inline __be64 nla_get_be64(const struct nlattr *nla)
  998. {
  999. __be64 tmp;
  1000. nla_memcpy(&tmp, nla, sizeof(tmp));
  1001. return tmp;
  1002. }
  1003. /**
  1004. * nla_get_le64 - return payload of __le64 attribute
  1005. * @nla: __le64 netlink attribute
  1006. */
  1007. static inline __le64 nla_get_le64(const struct nlattr *nla)
  1008. {
  1009. return *(__le64 *) nla_data(nla);
  1010. }
  1011. /**
  1012. * nla_get_s32 - return payload of s32 attribute
  1013. * @nla: s32 netlink attribute
  1014. */
  1015. static inline s32 nla_get_s32(const struct nlattr *nla)
  1016. {
  1017. return *(s32 *) nla_data(nla);
  1018. }
  1019. /**
  1020. * nla_get_s16 - return payload of s16 attribute
  1021. * @nla: s16 netlink attribute
  1022. */
  1023. static inline s16 nla_get_s16(const struct nlattr *nla)
  1024. {
  1025. return *(s16 *) nla_data(nla);
  1026. }
  1027. /**
  1028. * nla_get_s8 - return payload of s8 attribute
  1029. * @nla: s8 netlink attribute
  1030. */
  1031. static inline s8 nla_get_s8(const struct nlattr *nla)
  1032. {
  1033. return *(s8 *) nla_data(nla);
  1034. }
  1035. /**
  1036. * nla_get_s64 - return payload of s64 attribute
  1037. * @nla: s64 netlink attribute
  1038. */
  1039. static inline s64 nla_get_s64(const struct nlattr *nla)
  1040. {
  1041. s64 tmp;
  1042. nla_memcpy(&tmp, nla, sizeof(tmp));
  1043. return tmp;
  1044. }
  1045. /**
  1046. * nla_get_flag - return payload of flag attribute
  1047. * @nla: flag netlink attribute
  1048. */
  1049. static inline int nla_get_flag(const struct nlattr *nla)
  1050. {
  1051. return !!nla;
  1052. }
  1053. /**
  1054. * nla_get_msecs - return payload of msecs attribute
  1055. * @nla: msecs netlink attribute
  1056. *
  1057. * Returns the number of milliseconds in jiffies.
  1058. */
  1059. static inline unsigned long nla_get_msecs(const struct nlattr *nla)
  1060. {
  1061. u64 msecs = nla_get_u64(nla);
  1062. return msecs_to_jiffies((unsigned long) msecs);
  1063. }
  1064. /**
  1065. * nla_get_in_addr - return payload of IPv4 address attribute
  1066. * @nla: IPv4 address netlink attribute
  1067. */
  1068. static inline __be32 nla_get_in_addr(const struct nlattr *nla)
  1069. {
  1070. return *(__be32 *) nla_data(nla);
  1071. }
  1072. /**
  1073. * nla_get_in6_addr - return payload of IPv6 address attribute
  1074. * @nla: IPv6 address netlink attribute
  1075. */
  1076. static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla)
  1077. {
  1078. struct in6_addr tmp;
  1079. nla_memcpy(&tmp, nla, sizeof(tmp));
  1080. return tmp;
  1081. }
  1082. /**
  1083. * nla_nest_start - Start a new level of nested attributes
  1084. * @skb: socket buffer to add attributes to
  1085. * @attrtype: attribute type of container
  1086. *
  1087. * Returns the container attribute
  1088. */
  1089. static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
  1090. {
  1091. struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
  1092. if (nla_put(skb, attrtype, 0, NULL) < 0)
  1093. return NULL;
  1094. return start;
  1095. }
  1096. /**
  1097. * nla_nest_end - Finalize nesting of attributes
  1098. * @skb: socket buffer the attributes are stored in
  1099. * @start: container attribute
  1100. *
  1101. * Corrects the container attribute header to include the all
  1102. * appeneded attributes.
  1103. *
  1104. * Returns the total data length of the skb.
  1105. */
  1106. static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
  1107. {
  1108. start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
  1109. return skb->len;
  1110. }
  1111. /**
  1112. * nla_nest_cancel - Cancel nesting of attributes
  1113. * @skb: socket buffer the message is stored in
  1114. * @start: container attribute
  1115. *
  1116. * Removes the container attribute and including all nested
  1117. * attributes. Returns -EMSGSIZE
  1118. */
  1119. static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
  1120. {
  1121. nlmsg_trim(skb, start);
  1122. }
  1123. /**
  1124. * nla_validate_nested - Validate a stream of nested attributes
  1125. * @start: container attribute
  1126. * @maxtype: maximum attribute type to be expected
  1127. * @policy: validation policy
  1128. *
  1129. * Validates all attributes in the nested attribute stream against the
  1130. * specified policy. Attributes with a type exceeding maxtype will be
  1131. * ignored. See documenation of struct nla_policy for more details.
  1132. *
  1133. * Returns 0 on success or a negative error code.
  1134. */
  1135. static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
  1136. const struct nla_policy *policy)
  1137. {
  1138. return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
  1139. }
  1140. /**
  1141. * nla_for_each_attr - iterate over a stream of attributes
  1142. * @pos: loop counter, set to current attribute
  1143. * @head: head of attribute stream
  1144. * @len: length of attribute stream
  1145. * @rem: initialized to len, holds bytes currently remaining in stream
  1146. */
  1147. #define nla_for_each_attr(pos, head, len, rem) \
  1148. for (pos = head, rem = len; \
  1149. nla_ok(pos, rem); \
  1150. pos = nla_next(pos, &(rem)))
  1151. /**
  1152. * nla_for_each_nested - iterate over nested attributes
  1153. * @pos: loop counter, set to current attribute
  1154. * @nla: attribute containing the nested attributes
  1155. * @rem: initialized to len, holds bytes currently remaining in stream
  1156. */
  1157. #define nla_for_each_nested(pos, nla, rem) \
  1158. nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
  1159. /**
  1160. * nla_is_last - Test if attribute is last in stream
  1161. * @nla: attribute to test
  1162. * @rem: bytes remaining in stream
  1163. */
  1164. static inline bool nla_is_last(const struct nlattr *nla, int rem)
  1165. {
  1166. return nla->nla_len == rem;
  1167. }
  1168. #endif