netlink.h 40 KB

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