netlink.h 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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,
  104. * padattr) add s64 attribute to skb
  105. * nla_put_string(skb, type, str) add string attribute to skb
  106. * nla_put_flag(skb, type) add flag attribute to skb
  107. * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb
  108. * nla_put_in_addr(skb, type, addr) add IPv4 address attribute to skb
  109. * nla_put_in6_addr(skb, type, addr) add IPv6 address attribute to skb
  110. *
  111. * Nested Attributes Construction:
  112. * nla_nest_start(skb, type) start a nested attribute
  113. * nla_nest_end(skb, nla) finalize a nested attribute
  114. * nla_nest_cancel(skb, nla) cancel nested attribute construction
  115. *
  116. * Attribute Length Calculations:
  117. * nla_attr_size(payload) length of attribute w/o padding
  118. * nla_total_size(payload) length of attribute w/ padding
  119. * nla_padlen(payload) length of padding
  120. *
  121. * Attribute Payload Access:
  122. * nla_data(nla) head of attribute payload
  123. * nla_len(nla) length of attribute payload
  124. *
  125. * Attribute Payload Access for Basic Types:
  126. * nla_get_u8(nla) get payload for a u8 attribute
  127. * nla_get_u16(nla) get payload for a u16 attribute
  128. * nla_get_u32(nla) get payload for a u32 attribute
  129. * nla_get_u64(nla) get payload for a u64 attribute
  130. * nla_get_s8(nla) get payload for a s8 attribute
  131. * nla_get_s16(nla) get payload for a s16 attribute
  132. * nla_get_s32(nla) get payload for a s32 attribute
  133. * nla_get_s64(nla) get payload for a s64 attribute
  134. * nla_get_flag(nla) return 1 if flag is true
  135. * nla_get_msecs(nla) get payload for a msecs attribute
  136. *
  137. * Attribute Misc:
  138. * nla_memcpy(dest, nla, count) copy attribute into memory
  139. * nla_memcmp(nla, data, size) compare attribute with memory area
  140. * nla_strlcpy(dst, nla, size) copy attribute to a sized string
  141. * nla_strcmp(nla, str) compare attribute with string
  142. *
  143. * Attribute Parsing:
  144. * nla_ok(nla, remaining) does nla fit into remaining bytes?
  145. * nla_next(nla, remaining) get next netlink attribute
  146. * nla_validate() validate a stream of attributes
  147. * nla_validate_nested() validate a stream of nested attributes
  148. * nla_find() find attribute in stream of attributes
  149. * nla_find_nested() find attribute in nested attributes
  150. * nla_parse() parse and validate stream of attrs
  151. * nla_parse_nested() parse nested attribuets
  152. * nla_for_each_attr() loop over all attributes
  153. * nla_for_each_nested() loop over the nested attributes
  154. *=========================================================================
  155. */
  156. /**
  157. * Standard attribute types to specify validation policy
  158. */
  159. enum {
  160. NLA_UNSPEC,
  161. NLA_U8,
  162. NLA_U16,
  163. NLA_U32,
  164. NLA_U64,
  165. NLA_STRING,
  166. NLA_FLAG,
  167. NLA_MSECS,
  168. NLA_NESTED,
  169. NLA_NESTED_COMPAT,
  170. NLA_NUL_STRING,
  171. NLA_BINARY,
  172. NLA_S8,
  173. NLA_S16,
  174. NLA_S32,
  175. NLA_S64,
  176. __NLA_TYPE_MAX,
  177. };
  178. #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
  179. /**
  180. * struct nla_policy - attribute validation policy
  181. * @type: Type of attribute or NLA_UNSPEC
  182. * @len: Type specific length of payload
  183. *
  184. * Policies are defined as arrays of this struct, the array must be
  185. * accessible by attribute type up to the highest identifier to be expected.
  186. *
  187. * Meaning of `len' field:
  188. * NLA_STRING Maximum length of string
  189. * NLA_NUL_STRING Maximum length of string (excluding NUL)
  190. * NLA_FLAG Unused
  191. * NLA_BINARY Maximum length of attribute payload
  192. * NLA_NESTED Don't use `len' field -- length verification is
  193. * done by checking len of nested header (or empty)
  194. * NLA_NESTED_COMPAT Minimum length of structure payload
  195. * NLA_U8, NLA_U16,
  196. * NLA_U32, NLA_U64,
  197. * NLA_S8, NLA_S16,
  198. * NLA_S32, NLA_S64,
  199. * NLA_MSECS Leaving the length field zero will verify the
  200. * given type fits, using it verifies minimum length
  201. * just like "All other"
  202. * All other Minimum length of attribute payload
  203. *
  204. * Example:
  205. * static const struct nla_policy my_policy[ATTR_MAX+1] = {
  206. * [ATTR_FOO] = { .type = NLA_U16 },
  207. * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
  208. * [ATTR_BAZ] = { .len = sizeof(struct mystruct) },
  209. * };
  210. */
  211. struct nla_policy {
  212. u16 type;
  213. u16 len;
  214. };
  215. /**
  216. * struct nl_info - netlink source information
  217. * @nlh: Netlink message header of original request
  218. * @portid: Netlink PORTID of requesting application
  219. */
  220. struct nl_info {
  221. struct nlmsghdr *nlh;
  222. struct net *nl_net;
  223. u32 portid;
  224. };
  225. int netlink_rcv_skb(struct sk_buff *skb,
  226. int (*cb)(struct sk_buff *, struct nlmsghdr *));
  227. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  228. unsigned int group, int report, gfp_t flags);
  229. int nla_validate(const struct nlattr *head, int len, int maxtype,
  230. const struct nla_policy *policy);
  231. int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
  232. int len, const struct nla_policy *policy);
  233. int nla_policy_len(const struct nla_policy *, int);
  234. struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
  235. size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize);
  236. int nla_memcpy(void *dest, const struct nlattr *src, int count);
  237. int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
  238. int nla_strcmp(const struct nlattr *nla, const char *str);
  239. struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
  240. struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
  241. int attrlen, int padattr);
  242. void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
  243. struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
  244. struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype,
  245. int attrlen, int padattr);
  246. void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
  247. void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
  248. const void *data);
  249. void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
  250. const void *data, int padattr);
  251. void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
  252. int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
  253. int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
  254. const void *data, int padattr);
  255. int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
  256. int nla_append(struct sk_buff *skb, int attrlen, const void *data);
  257. /**************************************************************************
  258. * Netlink Messages
  259. **************************************************************************/
  260. /**
  261. * nlmsg_msg_size - length of netlink message not including padding
  262. * @payload: length of message payload
  263. */
  264. static inline int nlmsg_msg_size(int payload)
  265. {
  266. return NLMSG_HDRLEN + payload;
  267. }
  268. /**
  269. * nlmsg_total_size - length of netlink message including padding
  270. * @payload: length of message payload
  271. */
  272. static inline int nlmsg_total_size(int payload)
  273. {
  274. return NLMSG_ALIGN(nlmsg_msg_size(payload));
  275. }
  276. /**
  277. * nlmsg_padlen - length of padding at the message's tail
  278. * @payload: length of message payload
  279. */
  280. static inline int nlmsg_padlen(int payload)
  281. {
  282. return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
  283. }
  284. /**
  285. * nlmsg_data - head of message payload
  286. * @nlh: netlink message header
  287. */
  288. static inline void *nlmsg_data(const struct nlmsghdr *nlh)
  289. {
  290. return (unsigned char *) nlh + NLMSG_HDRLEN;
  291. }
  292. /**
  293. * nlmsg_len - length of message payload
  294. * @nlh: netlink message header
  295. */
  296. static inline int nlmsg_len(const struct nlmsghdr *nlh)
  297. {
  298. return nlh->nlmsg_len - NLMSG_HDRLEN;
  299. }
  300. /**
  301. * nlmsg_attrdata - head of attributes data
  302. * @nlh: netlink message header
  303. * @hdrlen: length of family specific header
  304. */
  305. static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
  306. int hdrlen)
  307. {
  308. unsigned char *data = nlmsg_data(nlh);
  309. return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
  310. }
  311. /**
  312. * nlmsg_attrlen - length of attributes data
  313. * @nlh: netlink message header
  314. * @hdrlen: length of family specific header
  315. */
  316. static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
  317. {
  318. return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
  319. }
  320. /**
  321. * nlmsg_ok - check if the netlink message fits into the remaining bytes
  322. * @nlh: netlink message header
  323. * @remaining: number of bytes remaining in message stream
  324. */
  325. static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
  326. {
  327. return (remaining >= (int) sizeof(struct nlmsghdr) &&
  328. nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
  329. nlh->nlmsg_len <= remaining);
  330. }
  331. /**
  332. * nlmsg_next - next netlink message in message stream
  333. * @nlh: netlink message header
  334. * @remaining: number of bytes remaining in message stream
  335. *
  336. * Returns the next netlink message in the message stream and
  337. * decrements remaining by the size of the current message.
  338. */
  339. static inline struct nlmsghdr *
  340. nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
  341. {
  342. int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
  343. *remaining -= totlen;
  344. return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
  345. }
  346. /**
  347. * nlmsg_parse - parse attributes of a netlink message
  348. * @nlh: netlink message header
  349. * @hdrlen: length of family specific header
  350. * @tb: destination array with maxtype+1 elements
  351. * @maxtype: maximum attribute type to be expected
  352. * @policy: validation policy
  353. *
  354. * See nla_parse()
  355. */
  356. static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
  357. struct nlattr *tb[], int maxtype,
  358. const struct nla_policy *policy)
  359. {
  360. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  361. return -EINVAL;
  362. return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
  363. nlmsg_attrlen(nlh, hdrlen), policy);
  364. }
  365. /**
  366. * nlmsg_find_attr - find a specific attribute in a netlink message
  367. * @nlh: netlink message header
  368. * @hdrlen: length of familiy specific header
  369. * @attrtype: type of attribute to look for
  370. *
  371. * Returns the first attribute which matches the specified type.
  372. */
  373. static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
  374. int hdrlen, int attrtype)
  375. {
  376. return nla_find(nlmsg_attrdata(nlh, hdrlen),
  377. nlmsg_attrlen(nlh, hdrlen), attrtype);
  378. }
  379. /**
  380. * nlmsg_validate - validate a netlink message including attributes
  381. * @nlh: netlinket message header
  382. * @hdrlen: length of familiy specific header
  383. * @maxtype: maximum attribute type to be expected
  384. * @policy: validation policy
  385. */
  386. static inline int nlmsg_validate(const struct nlmsghdr *nlh,
  387. int hdrlen, int maxtype,
  388. const struct nla_policy *policy)
  389. {
  390. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  391. return -EINVAL;
  392. return nla_validate(nlmsg_attrdata(nlh, hdrlen),
  393. nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
  394. }
  395. /**
  396. * nlmsg_report - need to report back to application?
  397. * @nlh: netlink message header
  398. *
  399. * Returns 1 if a report back to the application is requested.
  400. */
  401. static inline int nlmsg_report(const struct nlmsghdr *nlh)
  402. {
  403. return !!(nlh->nlmsg_flags & NLM_F_ECHO);
  404. }
  405. /**
  406. * nlmsg_for_each_attr - iterate over a stream of attributes
  407. * @pos: loop counter, set to current attribute
  408. * @nlh: netlink message header
  409. * @hdrlen: length of familiy specific header
  410. * @rem: initialized to len, holds bytes currently remaining in stream
  411. */
  412. #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
  413. nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
  414. nlmsg_attrlen(nlh, hdrlen), rem)
  415. /**
  416. * nlmsg_put - Add a new netlink message to an skb
  417. * @skb: socket buffer to store message in
  418. * @portid: netlink PORTID of requesting application
  419. * @seq: sequence number of message
  420. * @type: message type
  421. * @payload: length of message payload
  422. * @flags: message flags
  423. *
  424. * Returns NULL if the tailroom of the skb is insufficient to store
  425. * the message header and payload.
  426. */
  427. static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  428. int type, int payload, int flags)
  429. {
  430. if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
  431. return NULL;
  432. return __nlmsg_put(skb, portid, seq, type, payload, flags);
  433. }
  434. /**
  435. * nlmsg_put_answer - Add a new callback based netlink message to an skb
  436. * @skb: socket buffer to store message in
  437. * @cb: netlink callback
  438. * @type: message type
  439. * @payload: length of message payload
  440. * @flags: message flags
  441. *
  442. * Returns NULL if the tailroom of the skb is insufficient to store
  443. * the message header and payload.
  444. */
  445. static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
  446. struct netlink_callback *cb,
  447. int type, int payload,
  448. int flags)
  449. {
  450. return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  451. type, payload, flags);
  452. }
  453. /**
  454. * nlmsg_new - Allocate a new netlink message
  455. * @payload: size of the message payload
  456. * @flags: the type of memory to allocate.
  457. *
  458. * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
  459. * and a good default is needed.
  460. */
  461. static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
  462. {
  463. return alloc_skb(nlmsg_total_size(payload), flags);
  464. }
  465. /**
  466. * nlmsg_end - Finalize a netlink message
  467. * @skb: socket buffer the message is stored in
  468. * @nlh: netlink message header
  469. *
  470. * Corrects the netlink message header to include the appeneded
  471. * attributes. Only necessary if attributes have been added to
  472. * the message.
  473. */
  474. static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
  475. {
  476. nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
  477. }
  478. /**
  479. * nlmsg_get_pos - return current position in netlink message
  480. * @skb: socket buffer the message is stored in
  481. *
  482. * Returns a pointer to the current tail of the message.
  483. */
  484. static inline void *nlmsg_get_pos(struct sk_buff *skb)
  485. {
  486. return skb_tail_pointer(skb);
  487. }
  488. /**
  489. * nlmsg_trim - Trim message to a mark
  490. * @skb: socket buffer the message is stored in
  491. * @mark: mark to trim to
  492. *
  493. * Trims the message to the provided mark.
  494. */
  495. static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
  496. {
  497. if (mark) {
  498. WARN_ON((unsigned char *) mark < skb->data);
  499. skb_trim(skb, (unsigned char *) mark - skb->data);
  500. }
  501. }
  502. /**
  503. * nlmsg_cancel - Cancel construction of a netlink message
  504. * @skb: socket buffer the message is stored in
  505. * @nlh: netlink message header
  506. *
  507. * Removes the complete netlink message including all
  508. * attributes from the socket buffer again.
  509. */
  510. static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
  511. {
  512. nlmsg_trim(skb, nlh);
  513. }
  514. /**
  515. * nlmsg_free - free a netlink message
  516. * @skb: socket buffer of netlink message
  517. */
  518. static inline void nlmsg_free(struct sk_buff *skb)
  519. {
  520. kfree_skb(skb);
  521. }
  522. /**
  523. * nlmsg_multicast - multicast a netlink message
  524. * @sk: netlink socket to spread messages to
  525. * @skb: netlink message as socket buffer
  526. * @portid: own netlink portid to avoid sending to yourself
  527. * @group: multicast group id
  528. * @flags: allocation flags
  529. */
  530. static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
  531. u32 portid, unsigned int group, gfp_t flags)
  532. {
  533. int err;
  534. NETLINK_CB(skb).dst_group = group;
  535. err = netlink_broadcast(sk, skb, portid, group, flags);
  536. if (err > 0)
  537. err = 0;
  538. return err;
  539. }
  540. /**
  541. * nlmsg_unicast - unicast a netlink message
  542. * @sk: netlink socket to spread message to
  543. * @skb: netlink message as socket buffer
  544. * @portid: netlink portid of the destination socket
  545. */
  546. static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
  547. {
  548. int err;
  549. err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
  550. if (err > 0)
  551. err = 0;
  552. return err;
  553. }
  554. /**
  555. * nlmsg_for_each_msg - iterate over a stream of messages
  556. * @pos: loop counter, set to current message
  557. * @head: head of message stream
  558. * @len: length of message stream
  559. * @rem: initialized to len, holds bytes currently remaining in stream
  560. */
  561. #define nlmsg_for_each_msg(pos, head, len, rem) \
  562. for (pos = head, rem = len; \
  563. nlmsg_ok(pos, rem); \
  564. pos = nlmsg_next(pos, &(rem)))
  565. /**
  566. * nl_dump_check_consistent - check if sequence is consistent and advertise if not
  567. * @cb: netlink callback structure that stores the sequence number
  568. * @nlh: netlink message header to write the flag to
  569. *
  570. * This function checks if the sequence (generation) number changed during dump
  571. * and if it did, advertises it in the netlink message header.
  572. *
  573. * The correct way to use it is to set cb->seq to the generation counter when
  574. * all locks for dumping have been acquired, and then call this function for
  575. * each message that is generated.
  576. *
  577. * Note that due to initialisation concerns, 0 is an invalid sequence number
  578. * and must not be used by code that uses this functionality.
  579. */
  580. static inline void
  581. nl_dump_check_consistent(struct netlink_callback *cb,
  582. struct nlmsghdr *nlh)
  583. {
  584. if (cb->prev_seq && cb->seq != cb->prev_seq)
  585. nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
  586. cb->prev_seq = cb->seq;
  587. }
  588. /**************************************************************************
  589. * Netlink Attributes
  590. **************************************************************************/
  591. /**
  592. * nla_attr_size - length of attribute not including padding
  593. * @payload: length of payload
  594. */
  595. static inline int nla_attr_size(int payload)
  596. {
  597. return NLA_HDRLEN + payload;
  598. }
  599. /**
  600. * nla_total_size - total length of attribute including padding
  601. * @payload: length of payload
  602. */
  603. static inline int nla_total_size(int payload)
  604. {
  605. return NLA_ALIGN(nla_attr_size(payload));
  606. }
  607. /**
  608. * nla_padlen - length of padding at the tail of attribute
  609. * @payload: length of payload
  610. */
  611. static inline int nla_padlen(int payload)
  612. {
  613. return nla_total_size(payload) - nla_attr_size(payload);
  614. }
  615. /**
  616. * nla_type - attribute type
  617. * @nla: netlink attribute
  618. */
  619. static inline int nla_type(const struct nlattr *nla)
  620. {
  621. return nla->nla_type & NLA_TYPE_MASK;
  622. }
  623. /**
  624. * nla_data - head of payload
  625. * @nla: netlink attribute
  626. */
  627. static inline void *nla_data(const struct nlattr *nla)
  628. {
  629. return (char *) nla + NLA_HDRLEN;
  630. }
  631. /**
  632. * nla_len - length of payload
  633. * @nla: netlink attribute
  634. */
  635. static inline int nla_len(const struct nlattr *nla)
  636. {
  637. return nla->nla_len - NLA_HDRLEN;
  638. }
  639. /**
  640. * nla_ok - check if the netlink attribute fits into the remaining bytes
  641. * @nla: netlink attribute
  642. * @remaining: number of bytes remaining in attribute stream
  643. */
  644. static inline int nla_ok(const struct nlattr *nla, int remaining)
  645. {
  646. return remaining >= (int) sizeof(*nla) &&
  647. nla->nla_len >= sizeof(*nla) &&
  648. nla->nla_len <= remaining;
  649. }
  650. /**
  651. * nla_next - next netlink attribute in attribute stream
  652. * @nla: netlink attribute
  653. * @remaining: number of bytes remaining in attribute stream
  654. *
  655. * Returns the next netlink attribute in the attribute stream and
  656. * decrements remaining by the size of the current attribute.
  657. */
  658. static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
  659. {
  660. int totlen = NLA_ALIGN(nla->nla_len);
  661. *remaining -= totlen;
  662. return (struct nlattr *) ((char *) nla + totlen);
  663. }
  664. /**
  665. * nla_find_nested - find attribute in a set of nested attributes
  666. * @nla: attribute containing the nested attributes
  667. * @attrtype: type of attribute to look for
  668. *
  669. * Returns the first attribute which matches the specified type.
  670. */
  671. static inline struct nlattr *
  672. nla_find_nested(const struct nlattr *nla, int attrtype)
  673. {
  674. return nla_find(nla_data(nla), nla_len(nla), attrtype);
  675. }
  676. /**
  677. * nla_parse_nested - parse nested attributes
  678. * @tb: destination array with maxtype+1 elements
  679. * @maxtype: maximum attribute type to be expected
  680. * @nla: attribute containing the nested attributes
  681. * @policy: validation policy
  682. *
  683. * See nla_parse()
  684. */
  685. static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
  686. const struct nlattr *nla,
  687. const struct nla_policy *policy)
  688. {
  689. return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
  690. }
  691. /**
  692. * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
  693. * @skb: socket buffer to add attribute to
  694. * @attrtype: attribute type
  695. * @value: numeric value
  696. */
  697. static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
  698. {
  699. return nla_put(skb, attrtype, sizeof(u8), &value);
  700. }
  701. /**
  702. * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
  703. * @skb: socket buffer to add attribute to
  704. * @attrtype: attribute type
  705. * @value: numeric value
  706. */
  707. static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
  708. {
  709. return nla_put(skb, attrtype, sizeof(u16), &value);
  710. }
  711. /**
  712. * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer
  713. * @skb: socket buffer to add attribute to
  714. * @attrtype: attribute type
  715. * @value: numeric value
  716. */
  717. static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
  718. {
  719. return nla_put(skb, attrtype, sizeof(__be16), &value);
  720. }
  721. /**
  722. * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
  723. * @skb: socket buffer to add attribute to
  724. * @attrtype: attribute type
  725. * @value: numeric value
  726. */
  727. static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
  728. {
  729. return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, value);
  730. }
  731. /**
  732. * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
  733. * @skb: socket buffer to add attribute to
  734. * @attrtype: attribute type
  735. * @value: numeric value
  736. */
  737. static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
  738. {
  739. return nla_put(skb, attrtype, sizeof(__le16), &value);
  740. }
  741. /**
  742. * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
  743. * @skb: socket buffer to add attribute to
  744. * @attrtype: attribute type
  745. * @value: numeric value
  746. */
  747. static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
  748. {
  749. return nla_put(skb, attrtype, sizeof(u32), &value);
  750. }
  751. /**
  752. * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
  753. * @skb: socket buffer to add attribute to
  754. * @attrtype: attribute type
  755. * @value: numeric value
  756. */
  757. static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
  758. {
  759. return nla_put(skb, attrtype, sizeof(__be32), &value);
  760. }
  761. /**
  762. * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer
  763. * @skb: socket buffer to add attribute to
  764. * @attrtype: attribute type
  765. * @value: numeric value
  766. */
  767. static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
  768. {
  769. return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, value);
  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. return nla_put(skb, attrtype, sizeof(__le32), &value);
  780. }
  781. /**
  782. * nla_put_u64 - Add a u64 netlink attribute to a socket buffer
  783. * @skb: socket buffer to add attribute to
  784. * @attrtype: attribute type
  785. * @value: numeric value
  786. */
  787. static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
  788. {
  789. return nla_put(skb, attrtype, sizeof(u64), &value);
  790. }
  791. /**
  792. * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it
  793. * @skb: socket buffer to add attribute to
  794. * @attrtype: attribute type
  795. * @value: numeric value
  796. * @padattr: attribute type for the padding
  797. */
  798. static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value,
  799. int padattr)
  800. {
  801. return nla_put_64bit(skb, attrtype, sizeof(__be64), &value, padattr);
  802. }
  803. /**
  804. * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it
  805. * @skb: socket buffer to add attribute to
  806. * @attrtype: attribute type
  807. * @value: numeric value
  808. * @padattr: attribute type for the padding
  809. */
  810. static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value,
  811. int padattr)
  812. {
  813. return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, value,
  814. padattr);
  815. }
  816. /**
  817. * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it
  818. * @skb: socket buffer to add attribute to
  819. * @attrtype: attribute type
  820. * @value: numeric value
  821. * @padattr: attribute type for the padding
  822. */
  823. static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value,
  824. int padattr)
  825. {
  826. return nla_put_64bit(skb, attrtype, sizeof(__le64), &value, padattr);
  827. }
  828. /**
  829. * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
  830. * @skb: socket buffer to add attribute to
  831. * @attrtype: attribute type
  832. * @value: numeric value
  833. */
  834. static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
  835. {
  836. return nla_put(skb, attrtype, sizeof(s8), &value);
  837. }
  838. /**
  839. * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
  840. * @skb: socket buffer to add attribute to
  841. * @attrtype: attribute type
  842. * @value: numeric value
  843. */
  844. static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
  845. {
  846. return nla_put(skb, attrtype, sizeof(s16), &value);
  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. return nla_put(skb, attrtype, sizeof(s32), &value);
  857. }
  858. /**
  859. * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it
  860. * @skb: socket buffer to add attribute to
  861. * @attrtype: attribute type
  862. * @value: numeric value
  863. * @padattr: attribute type for the padding
  864. */
  865. static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value,
  866. int padattr)
  867. {
  868. return nla_put_64bit(skb, attrtype, sizeof(s64), &value, padattr);
  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. return nla_put_be32(skb, attrtype, addr);
  913. }
  914. /**
  915. * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
  916. * buffer
  917. * @skb: socket buffer to add attribute to
  918. * @attrtype: attribute type
  919. * @addr: IPv6 address
  920. */
  921. static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
  922. const struct in6_addr *addr)
  923. {
  924. return nla_put(skb, attrtype, sizeof(*addr), addr);
  925. }
  926. /**
  927. * nla_get_u32 - return payload of u32 attribute
  928. * @nla: u32 netlink attribute
  929. */
  930. static inline u32 nla_get_u32(const struct nlattr *nla)
  931. {
  932. return *(u32 *) nla_data(nla);
  933. }
  934. /**
  935. * nla_get_be32 - return payload of __be32 attribute
  936. * @nla: __be32 netlink attribute
  937. */
  938. static inline __be32 nla_get_be32(const struct nlattr *nla)
  939. {
  940. return *(__be32 *) nla_data(nla);
  941. }
  942. /**
  943. * nla_get_le32 - return payload of __le32 attribute
  944. * @nla: __le32 netlink attribute
  945. */
  946. static inline __le32 nla_get_le32(const struct nlattr *nla)
  947. {
  948. return *(__le32 *) nla_data(nla);
  949. }
  950. /**
  951. * nla_get_u16 - return payload of u16 attribute
  952. * @nla: u16 netlink attribute
  953. */
  954. static inline u16 nla_get_u16(const struct nlattr *nla)
  955. {
  956. return *(u16 *) nla_data(nla);
  957. }
  958. /**
  959. * nla_get_be16 - return payload of __be16 attribute
  960. * @nla: __be16 netlink attribute
  961. */
  962. static inline __be16 nla_get_be16(const struct nlattr *nla)
  963. {
  964. return *(__be16 *) nla_data(nla);
  965. }
  966. /**
  967. * nla_get_le16 - return payload of __le16 attribute
  968. * @nla: __le16 netlink attribute
  969. */
  970. static inline __le16 nla_get_le16(const struct nlattr *nla)
  971. {
  972. return *(__le16 *) nla_data(nla);
  973. }
  974. /**
  975. * nla_get_u8 - return payload of u8 attribute
  976. * @nla: u8 netlink attribute
  977. */
  978. static inline u8 nla_get_u8(const struct nlattr *nla)
  979. {
  980. return *(u8 *) nla_data(nla);
  981. }
  982. /**
  983. * nla_get_u64 - return payload of u64 attribute
  984. * @nla: u64 netlink attribute
  985. */
  986. static inline u64 nla_get_u64(const struct nlattr *nla)
  987. {
  988. u64 tmp;
  989. nla_memcpy(&tmp, nla, sizeof(tmp));
  990. return tmp;
  991. }
  992. /**
  993. * nla_get_be64 - return payload of __be64 attribute
  994. * @nla: __be64 netlink attribute
  995. */
  996. static inline __be64 nla_get_be64(const struct nlattr *nla)
  997. {
  998. __be64 tmp;
  999. nla_memcpy(&tmp, nla, sizeof(tmp));
  1000. return tmp;
  1001. }
  1002. /**
  1003. * nla_get_le64 - return payload of __le64 attribute
  1004. * @nla: __le64 netlink attribute
  1005. */
  1006. static inline __le64 nla_get_le64(const struct nlattr *nla)
  1007. {
  1008. return *(__le64 *) nla_data(nla);
  1009. }
  1010. /**
  1011. * nla_get_s32 - return payload of s32 attribute
  1012. * @nla: s32 netlink attribute
  1013. */
  1014. static inline s32 nla_get_s32(const struct nlattr *nla)
  1015. {
  1016. return *(s32 *) nla_data(nla);
  1017. }
  1018. /**
  1019. * nla_get_s16 - return payload of s16 attribute
  1020. * @nla: s16 netlink attribute
  1021. */
  1022. static inline s16 nla_get_s16(const struct nlattr *nla)
  1023. {
  1024. return *(s16 *) nla_data(nla);
  1025. }
  1026. /**
  1027. * nla_get_s8 - return payload of s8 attribute
  1028. * @nla: s8 netlink attribute
  1029. */
  1030. static inline s8 nla_get_s8(const struct nlattr *nla)
  1031. {
  1032. return *(s8 *) nla_data(nla);
  1033. }
  1034. /**
  1035. * nla_get_s64 - return payload of s64 attribute
  1036. * @nla: s64 netlink attribute
  1037. */
  1038. static inline s64 nla_get_s64(const struct nlattr *nla)
  1039. {
  1040. s64 tmp;
  1041. nla_memcpy(&tmp, nla, sizeof(tmp));
  1042. return tmp;
  1043. }
  1044. /**
  1045. * nla_get_flag - return payload of flag attribute
  1046. * @nla: flag netlink attribute
  1047. */
  1048. static inline int nla_get_flag(const struct nlattr *nla)
  1049. {
  1050. return !!nla;
  1051. }
  1052. /**
  1053. * nla_get_msecs - return payload of msecs attribute
  1054. * @nla: msecs netlink attribute
  1055. *
  1056. * Returns the number of milliseconds in jiffies.
  1057. */
  1058. static inline unsigned long nla_get_msecs(const struct nlattr *nla)
  1059. {
  1060. u64 msecs = nla_get_u64(nla);
  1061. return msecs_to_jiffies((unsigned long) msecs);
  1062. }
  1063. /**
  1064. * nla_get_in_addr - return payload of IPv4 address attribute
  1065. * @nla: IPv4 address netlink attribute
  1066. */
  1067. static inline __be32 nla_get_in_addr(const struct nlattr *nla)
  1068. {
  1069. return *(__be32 *) nla_data(nla);
  1070. }
  1071. /**
  1072. * nla_get_in6_addr - return payload of IPv6 address attribute
  1073. * @nla: IPv6 address netlink attribute
  1074. */
  1075. static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla)
  1076. {
  1077. struct in6_addr tmp;
  1078. nla_memcpy(&tmp, nla, sizeof(tmp));
  1079. return tmp;
  1080. }
  1081. /**
  1082. * nla_nest_start - Start a new level of nested attributes
  1083. * @skb: socket buffer to add attributes to
  1084. * @attrtype: attribute type of container
  1085. *
  1086. * Returns the container attribute
  1087. */
  1088. static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
  1089. {
  1090. struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
  1091. if (nla_put(skb, attrtype, 0, NULL) < 0)
  1092. return NULL;
  1093. return start;
  1094. }
  1095. /**
  1096. * nla_nest_end - Finalize nesting of attributes
  1097. * @skb: socket buffer the attributes are stored in
  1098. * @start: container attribute
  1099. *
  1100. * Corrects the container attribute header to include the all
  1101. * appeneded attributes.
  1102. *
  1103. * Returns the total data length of the skb.
  1104. */
  1105. static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
  1106. {
  1107. start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
  1108. return skb->len;
  1109. }
  1110. /**
  1111. * nla_nest_cancel - Cancel nesting of attributes
  1112. * @skb: socket buffer the message is stored in
  1113. * @start: container attribute
  1114. *
  1115. * Removes the container attribute and including all nested
  1116. * attributes. Returns -EMSGSIZE
  1117. */
  1118. static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
  1119. {
  1120. nlmsg_trim(skb, start);
  1121. }
  1122. /**
  1123. * nla_validate_nested - Validate a stream of nested attributes
  1124. * @start: container attribute
  1125. * @maxtype: maximum attribute type to be expected
  1126. * @policy: validation policy
  1127. *
  1128. * Validates all attributes in the nested attribute stream against the
  1129. * specified policy. Attributes with a type exceeding maxtype will be
  1130. * ignored. See documenation of struct nla_policy for more details.
  1131. *
  1132. * Returns 0 on success or a negative error code.
  1133. */
  1134. static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
  1135. const struct nla_policy *policy)
  1136. {
  1137. return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
  1138. }
  1139. /**
  1140. * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute
  1141. * @skb: socket buffer the message is stored in
  1142. *
  1143. * Return true if padding is needed to align the next attribute (nla_data()) to
  1144. * a 64-bit aligned area.
  1145. */
  1146. static inline bool nla_need_padding_for_64bit(struct sk_buff *skb)
  1147. {
  1148. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1149. /* The nlattr header is 4 bytes in size, that's why we test
  1150. * if the skb->data _is_ aligned. A NOP attribute, plus
  1151. * nlattr header for next attribute, will make nla_data()
  1152. * 8-byte aligned.
  1153. */
  1154. if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
  1155. return true;
  1156. #endif
  1157. return false;
  1158. }
  1159. /**
  1160. * nla_align_64bit - 64-bit align the nla_data() of next attribute
  1161. * @skb: socket buffer the message is stored in
  1162. * @padattr: attribute type for the padding
  1163. *
  1164. * Conditionally emit a padding netlink attribute in order to make
  1165. * the next attribute we emit have a 64-bit aligned nla_data() area.
  1166. * This will only be done in architectures which do not have
  1167. * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
  1168. *
  1169. * Returns zero on success or a negative error code.
  1170. */
  1171. static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
  1172. {
  1173. if (nla_need_padding_for_64bit(skb) &&
  1174. !nla_reserve(skb, padattr, 0))
  1175. return -EMSGSIZE;
  1176. return 0;
  1177. }
  1178. /**
  1179. * nla_total_size_64bit - total length of attribute including padding
  1180. * @payload: length of payload
  1181. */
  1182. static inline int nla_total_size_64bit(int payload)
  1183. {
  1184. return NLA_ALIGN(nla_attr_size(payload))
  1185. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1186. + NLA_ALIGN(nla_attr_size(0))
  1187. #endif
  1188. ;
  1189. }
  1190. /**
  1191. * nla_for_each_attr - iterate over a stream of attributes
  1192. * @pos: loop counter, set to current attribute
  1193. * @head: head of attribute stream
  1194. * @len: length of attribute stream
  1195. * @rem: initialized to len, holds bytes currently remaining in stream
  1196. */
  1197. #define nla_for_each_attr(pos, head, len, rem) \
  1198. for (pos = head, rem = len; \
  1199. nla_ok(pos, rem); \
  1200. pos = nla_next(pos, &(rem)))
  1201. /**
  1202. * nla_for_each_nested - iterate over nested attributes
  1203. * @pos: loop counter, set to current attribute
  1204. * @nla: attribute containing the nested attributes
  1205. * @rem: initialized to len, holds bytes currently remaining in stream
  1206. */
  1207. #define nla_for_each_nested(pos, nla, rem) \
  1208. nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
  1209. /**
  1210. * nla_is_last - Test if attribute is last in stream
  1211. * @nla: attribute to test
  1212. * @rem: bytes remaining in stream
  1213. */
  1214. static inline bool nla_is_last(const struct nlattr *nla, int rem)
  1215. {
  1216. return nla->nla_len == rem;
  1217. }
  1218. #endif