netlink.h 33 KB

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