netlink.h 39 KB

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