netlink.h 38 KB

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