netlink.h 31 KB

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