netlink.h 29 KB

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