netlink.h 35 KB

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