netlink.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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_find() find attribute in stream of attributes
  148. * nla_find_nested() find attribute in nested attributes
  149. * nla_parse() parse and validate stream of attrs
  150. * nla_parse_nested() parse nested attribuets
  151. * nla_for_each_attr() loop over all attributes
  152. *=========================================================================
  153. */
  154. /**
  155. * Standard attribute types to specify validation policy
  156. */
  157. enum {
  158. NLA_UNSPEC,
  159. NLA_U8,
  160. NLA_U16,
  161. NLA_U32,
  162. NLA_U64,
  163. NLA_STRING,
  164. NLA_FLAG,
  165. NLA_MSECS,
  166. NLA_NESTED,
  167. __NLA_TYPE_MAX,
  168. };
  169. #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
  170. /**
  171. * struct nla_policy - attribute validation policy
  172. * @type: Type of attribute or NLA_UNSPEC
  173. * @minlen: Minimal length of payload required to be available
  174. *
  175. * Policies are defined as arrays of this struct, the array must be
  176. * accessible by attribute type up to the highest identifier to be expected.
  177. *
  178. * Example:
  179. * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
  180. * [ATTR_FOO] = { .type = NLA_U16 },
  181. * [ATTR_BAR] = { .type = NLA_STRING },
  182. * [ATTR_BAZ] = { .minlen = sizeof(struct mystruct) },
  183. * };
  184. */
  185. struct nla_policy {
  186. u16 type;
  187. u16 minlen;
  188. };
  189. extern void netlink_run_queue(struct sock *sk, unsigned int *qlen,
  190. int (*cb)(struct sk_buff *,
  191. struct nlmsghdr *, int *));
  192. extern void netlink_queue_skip(struct nlmsghdr *nlh,
  193. struct sk_buff *skb);
  194. extern int nlmsg_notify(struct sock *sk, struct sk_buff *skb,
  195. u32 pid, unsigned int group, int report,
  196. gfp_t flags);
  197. extern int nla_validate(struct nlattr *head, int len, int maxtype,
  198. struct nla_policy *policy);
  199. extern int nla_parse(struct nlattr *tb[], int maxtype,
  200. struct nlattr *head, int len,
  201. struct nla_policy *policy);
  202. extern struct nlattr * nla_find(struct nlattr *head, int len, int attrtype);
  203. extern size_t nla_strlcpy(char *dst, const struct nlattr *nla,
  204. size_t dstsize);
  205. extern int nla_memcpy(void *dest, struct nlattr *src, int count);
  206. extern int nla_memcmp(const struct nlattr *nla, const void *data,
  207. size_t size);
  208. extern int nla_strcmp(const struct nlattr *nla, const char *str);
  209. extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype,
  210. int attrlen);
  211. extern void * __nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
  212. extern struct nlattr * nla_reserve(struct sk_buff *skb, int attrtype,
  213. int attrlen);
  214. extern void * nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
  215. extern void __nla_put(struct sk_buff *skb, int attrtype,
  216. int attrlen, const void *data);
  217. extern void __nla_put_nohdr(struct sk_buff *skb, int attrlen,
  218. const void *data);
  219. extern int nla_put(struct sk_buff *skb, int attrtype,
  220. int attrlen, const void *data);
  221. extern int nla_put_nohdr(struct sk_buff *skb, int attrlen,
  222. const void *data);
  223. /**************************************************************************
  224. * Netlink Messages
  225. **************************************************************************/
  226. /**
  227. * nlmsg_msg_size - length of netlink message not including padding
  228. * @payload: length of message payload
  229. */
  230. static inline int nlmsg_msg_size(int payload)
  231. {
  232. return NLMSG_HDRLEN + payload;
  233. }
  234. /**
  235. * nlmsg_total_size - length of netlink message including padding
  236. * @payload: length of message payload
  237. */
  238. static inline int nlmsg_total_size(int payload)
  239. {
  240. return NLMSG_ALIGN(nlmsg_msg_size(payload));
  241. }
  242. /**
  243. * nlmsg_padlen - length of padding at the message's tail
  244. * @payload: length of message payload
  245. */
  246. static inline int nlmsg_padlen(int payload)
  247. {
  248. return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
  249. }
  250. /**
  251. * nlmsg_data - head of message payload
  252. * @nlh: netlink messsage header
  253. */
  254. static inline void *nlmsg_data(const struct nlmsghdr *nlh)
  255. {
  256. return (unsigned char *) nlh + NLMSG_HDRLEN;
  257. }
  258. /**
  259. * nlmsg_len - length of message payload
  260. * @nlh: netlink message header
  261. */
  262. static inline int nlmsg_len(const struct nlmsghdr *nlh)
  263. {
  264. return nlh->nlmsg_len - NLMSG_HDRLEN;
  265. }
  266. /**
  267. * nlmsg_attrdata - head of attributes data
  268. * @nlh: netlink message header
  269. * @hdrlen: length of family specific header
  270. */
  271. static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
  272. int hdrlen)
  273. {
  274. unsigned char *data = nlmsg_data(nlh);
  275. return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
  276. }
  277. /**
  278. * nlmsg_attrlen - length of attributes data
  279. * @nlh: netlink message header
  280. * @hdrlen: length of family specific header
  281. */
  282. static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
  283. {
  284. return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
  285. }
  286. /**
  287. * nlmsg_ok - check if the netlink message fits into the remaining bytes
  288. * @nlh: netlink message header
  289. * @remaining: number of bytes remaining in message stream
  290. */
  291. static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
  292. {
  293. return (remaining >= sizeof(struct nlmsghdr) &&
  294. nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
  295. nlh->nlmsg_len <= remaining);
  296. }
  297. /**
  298. * nlmsg_next - next netlink message in message stream
  299. * @nlh: netlink message header
  300. * @remaining: number of bytes remaining in message stream
  301. *
  302. * Returns the next netlink message in the message stream and
  303. * decrements remaining by the size of the current message.
  304. */
  305. static inline struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
  306. {
  307. int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
  308. *remaining -= totlen;
  309. return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
  310. }
  311. /**
  312. * nlmsg_parse - parse attributes of a netlink message
  313. * @nlh: netlink message header
  314. * @hdrlen: length of family specific header
  315. * @tb: destination array with maxtype+1 elements
  316. * @maxtype: maximum attribute type to be expected
  317. * @policy: validation policy
  318. *
  319. * See nla_parse()
  320. */
  321. static inline int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen,
  322. struct nlattr *tb[], int maxtype,
  323. struct nla_policy *policy)
  324. {
  325. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  326. return -EINVAL;
  327. return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
  328. nlmsg_attrlen(nlh, hdrlen), policy);
  329. }
  330. /**
  331. * nlmsg_find_attr - find a specific attribute in a netlink message
  332. * @nlh: netlink message header
  333. * @hdrlen: length of familiy specific header
  334. * @attrtype: type of attribute to look for
  335. *
  336. * Returns the first attribute which matches the specified type.
  337. */
  338. static inline struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh,
  339. int hdrlen, int attrtype)
  340. {
  341. return nla_find(nlmsg_attrdata(nlh, hdrlen),
  342. nlmsg_attrlen(nlh, hdrlen), attrtype);
  343. }
  344. /**
  345. * nlmsg_validate - validate a netlink message including attributes
  346. * @nlh: netlinket message header
  347. * @hdrlen: length of familiy specific header
  348. * @maxtype: maximum attribute type to be expected
  349. * @policy: validation policy
  350. */
  351. static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
  352. struct nla_policy *policy)
  353. {
  354. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  355. return -EINVAL;
  356. return nla_validate(nlmsg_attrdata(nlh, hdrlen),
  357. nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
  358. }
  359. /**
  360. * nlmsg_report - need to report back to application?
  361. * @nlh: netlink message header
  362. *
  363. * Returns 1 if a report back to the application is requested.
  364. */
  365. static inline int nlmsg_report(struct nlmsghdr *nlh)
  366. {
  367. return !!(nlh->nlmsg_flags & NLM_F_ECHO);
  368. }
  369. /**
  370. * nlmsg_for_each_attr - iterate over a stream of attributes
  371. * @pos: loop counter, set to current attribute
  372. * @nlh: netlink message header
  373. * @hdrlen: length of familiy specific header
  374. * @rem: initialized to len, holds bytes currently remaining in stream
  375. */
  376. #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
  377. nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
  378. nlmsg_attrlen(nlh, hdrlen), rem)
  379. #if 0
  380. /* FIXME: Enable once all users have been converted */
  381. /**
  382. * __nlmsg_put - Add a new netlink message to an skb
  383. * @skb: socket buffer to store message in
  384. * @pid: netlink process id
  385. * @seq: sequence number of message
  386. * @type: message type
  387. * @payload: length of message payload
  388. * @flags: message flags
  389. *
  390. * The caller is responsible to ensure that the skb provides enough
  391. * tailroom for both the netlink header and payload.
  392. */
  393. static inline struct nlmsghdr *__nlmsg_put(struct sk_buff *skb, u32 pid,
  394. u32 seq, int type, int payload,
  395. int flags)
  396. {
  397. struct nlmsghdr *nlh;
  398. nlh = (struct nlmsghdr *) skb_put(skb, nlmsg_total_size(payload));
  399. nlh->nlmsg_type = type;
  400. nlh->nlmsg_len = nlmsg_msg_size(payload);
  401. nlh->nlmsg_flags = flags;
  402. nlh->nlmsg_pid = pid;
  403. nlh->nlmsg_seq = seq;
  404. memset((unsigned char *) nlmsg_data(nlh) + payload, 0,
  405. nlmsg_padlen(payload));
  406. return nlh;
  407. }
  408. #endif
  409. /**
  410. * nlmsg_put - Add a new netlink message to an skb
  411. * @skb: socket buffer to store message in
  412. * @pid: netlink process id
  413. * @seq: sequence number of message
  414. * @type: message type
  415. * @payload: length of message payload
  416. * @flags: message flags
  417. *
  418. * Returns NULL if the tailroom of the skb is insufficient to store
  419. * the message header and payload.
  420. */
  421. static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
  422. int type, int payload, int flags)
  423. {
  424. if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
  425. return NULL;
  426. return __nlmsg_put(skb, pid, seq, type, payload, flags);
  427. }
  428. /**
  429. * nlmsg_put_answer - Add a new callback based netlink message to an skb
  430. * @skb: socket buffer to store message in
  431. * @cb: netlink callback
  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_answer(struct sk_buff *skb,
  440. struct netlink_callback *cb,
  441. int type, int payload,
  442. int flags)
  443. {
  444. return nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  445. type, payload, flags);
  446. }
  447. /**
  448. * nlmsg_new - Allocate a new netlink message
  449. * @size: maximum size of message
  450. * @flags: the type of memory to allocate.
  451. *
  452. * Use NLMSG_GOODSIZE if size isn't know and you need a good default size.
  453. */
  454. static inline struct sk_buff *nlmsg_new(int size, gfp_t flags)
  455. {
  456. return alloc_skb(size, flags);
  457. }
  458. /**
  459. * nlmsg_end - Finalize a netlink message
  460. * @skb: socket buffer the message is stored in
  461. * @nlh: netlink message header
  462. *
  463. * Corrects the netlink message header to include the appeneded
  464. * attributes. Only necessary if attributes have been added to
  465. * the message.
  466. *
  467. * Returns the total data length of the skb.
  468. */
  469. static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
  470. {
  471. nlh->nlmsg_len = skb->tail - (unsigned char *) nlh;
  472. return skb->len;
  473. }
  474. /**
  475. * nlmsg_get_pos - return current position in netlink message
  476. * @skb: socket buffer the message is stored in
  477. *
  478. * Returns a pointer to the current tail of the message.
  479. */
  480. static inline void *nlmsg_get_pos(struct sk_buff *skb)
  481. {
  482. return skb->tail;
  483. }
  484. /**
  485. * nlmsg_trim - Trim message to a mark
  486. * @skb: socket buffer the message is stored in
  487. * @mark: mark to trim to
  488. *
  489. * Trims the message to the provided mark. Returns -1.
  490. */
  491. static inline int nlmsg_trim(struct sk_buff *skb, void *mark)
  492. {
  493. if (mark)
  494. skb_trim(skb, (unsigned char *) mark - skb->data);
  495. return -1;
  496. }
  497. /**
  498. * nlmsg_cancel - Cancel construction of a netlink message
  499. * @skb: socket buffer the message is stored in
  500. * @nlh: netlink message header
  501. *
  502. * Removes the complete netlink message including all
  503. * attributes from the socket buffer again. Returns -1.
  504. */
  505. static inline int nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
  506. {
  507. return nlmsg_trim(skb, nlh);
  508. }
  509. /**
  510. * nlmsg_free - free a netlink message
  511. * @skb: socket buffer of netlink message
  512. */
  513. static inline void nlmsg_free(struct sk_buff *skb)
  514. {
  515. kfree_skb(skb);
  516. }
  517. /**
  518. * nlmsg_multicast - multicast a netlink message
  519. * @sk: netlink socket to spread messages to
  520. * @skb: netlink message as socket buffer
  521. * @pid: own netlink pid to avoid sending to yourself
  522. * @group: multicast group id
  523. * @flags: allocation flags
  524. */
  525. static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
  526. u32 pid, unsigned int group, gfp_t flags)
  527. {
  528. int err;
  529. NETLINK_CB(skb).dst_group = group;
  530. err = netlink_broadcast(sk, skb, pid, group, flags);
  531. if (err > 0)
  532. err = 0;
  533. return err;
  534. }
  535. /**
  536. * nlmsg_unicast - unicast a netlink message
  537. * @sk: netlink socket to spread message to
  538. * @skb: netlink message as socket buffer
  539. * @pid: netlink pid of the destination socket
  540. */
  541. static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid)
  542. {
  543. int err;
  544. err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
  545. if (err > 0)
  546. err = 0;
  547. return err;
  548. }
  549. /**
  550. * nlmsg_for_each_msg - iterate over a stream of messages
  551. * @pos: loop counter, set to current message
  552. * @head: head of message stream
  553. * @len: length of message stream
  554. * @rem: initialized to len, holds bytes currently remaining in stream
  555. */
  556. #define nlmsg_for_each_msg(pos, head, len, rem) \
  557. for (pos = head, rem = len; \
  558. nlmsg_ok(pos, rem); \
  559. pos = nlmsg_next(pos, &(rem)))
  560. /**************************************************************************
  561. * Netlink Attributes
  562. **************************************************************************/
  563. /**
  564. * nla_attr_size - length of attribute not including padding
  565. * @payload: length of payload
  566. */
  567. static inline int nla_attr_size(int payload)
  568. {
  569. return NLA_HDRLEN + payload;
  570. }
  571. /**
  572. * nla_total_size - total length of attribute including padding
  573. * @payload: length of payload
  574. */
  575. static inline int nla_total_size(int payload)
  576. {
  577. return NLA_ALIGN(nla_attr_size(payload));
  578. }
  579. /**
  580. * nla_padlen - length of padding at the tail of attribute
  581. * @payload: length of payload
  582. */
  583. static inline int nla_padlen(int payload)
  584. {
  585. return nla_total_size(payload) - nla_attr_size(payload);
  586. }
  587. /**
  588. * nla_data - head of payload
  589. * @nla: netlink attribute
  590. */
  591. static inline void *nla_data(const struct nlattr *nla)
  592. {
  593. return (char *) nla + NLA_HDRLEN;
  594. }
  595. /**
  596. * nla_len - length of payload
  597. * @nla: netlink attribute
  598. */
  599. static inline int nla_len(const struct nlattr *nla)
  600. {
  601. return nla->nla_len - NLA_HDRLEN;
  602. }
  603. /**
  604. * nla_ok - check if the netlink attribute fits into the remaining bytes
  605. * @nla: netlink attribute
  606. * @remaining: number of bytes remaining in attribute stream
  607. */
  608. static inline int nla_ok(const struct nlattr *nla, int remaining)
  609. {
  610. return remaining >= sizeof(*nla) &&
  611. nla->nla_len >= sizeof(*nla) &&
  612. nla->nla_len <= remaining;
  613. }
  614. /**
  615. * nla_next - next netlink attribte in attribute stream
  616. * @nla: netlink attribute
  617. * @remaining: number of bytes remaining in attribute stream
  618. *
  619. * Returns the next netlink attribute in the attribute stream and
  620. * decrements remaining by the size of the current attribute.
  621. */
  622. static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
  623. {
  624. int totlen = NLA_ALIGN(nla->nla_len);
  625. *remaining -= totlen;
  626. return (struct nlattr *) ((char *) nla + totlen);
  627. }
  628. /**
  629. * nla_find_nested - find attribute in a set of nested attributes
  630. * @nla: attribute containing the nested attributes
  631. * @attrtype: type of attribute to look for
  632. *
  633. * Returns the first attribute which matches the specified type.
  634. */
  635. static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype)
  636. {
  637. return nla_find(nla_data(nla), nla_len(nla), attrtype);
  638. }
  639. /**
  640. * nla_parse_nested - parse nested attributes
  641. * @tb: destination array with maxtype+1 elements
  642. * @maxtype: maximum attribute type to be expected
  643. * @nla: attribute containing the nested attributes
  644. * @policy: validation policy
  645. *
  646. * See nla_parse()
  647. */
  648. static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
  649. struct nlattr *nla,
  650. struct nla_policy *policy)
  651. {
  652. return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
  653. }
  654. /**
  655. * nla_put_u8 - Add a u16 netlink attribute to a socket buffer
  656. * @skb: socket buffer to add attribute to
  657. * @attrtype: attribute type
  658. * @value: numeric value
  659. */
  660. static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
  661. {
  662. return nla_put(skb, attrtype, sizeof(u8), &value);
  663. }
  664. /**
  665. * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
  666. * @skb: socket buffer to add attribute to
  667. * @attrtype: attribute type
  668. * @value: numeric value
  669. */
  670. static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
  671. {
  672. return nla_put(skb, attrtype, sizeof(u16), &value);
  673. }
  674. /**
  675. * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
  676. * @skb: socket buffer to add attribute to
  677. * @attrtype: attribute type
  678. * @value: numeric value
  679. */
  680. static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
  681. {
  682. return nla_put(skb, attrtype, sizeof(u32), &value);
  683. }
  684. /**
  685. * nla_put_64 - Add a u64 netlink attribute to a socket buffer
  686. * @skb: socket buffer to add attribute to
  687. * @attrtype: attribute type
  688. * @value: numeric value
  689. */
  690. static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
  691. {
  692. return nla_put(skb, attrtype, sizeof(u64), &value);
  693. }
  694. /**
  695. * nla_put_string - Add a string netlink attribute to a socket buffer
  696. * @skb: socket buffer to add attribute to
  697. * @attrtype: attribute type
  698. * @str: NUL terminated string
  699. */
  700. static inline int nla_put_string(struct sk_buff *skb, int attrtype,
  701. const char *str)
  702. {
  703. return nla_put(skb, attrtype, strlen(str) + 1, str);
  704. }
  705. /**
  706. * nla_put_flag - Add a flag netlink attribute to a socket buffer
  707. * @skb: socket buffer to add attribute to
  708. * @attrtype: attribute type
  709. */
  710. static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
  711. {
  712. return nla_put(skb, attrtype, 0, NULL);
  713. }
  714. /**
  715. * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
  716. * @skb: socket buffer to add attribute to
  717. * @attrtype: attribute type
  718. * @jiffies: number of msecs in jiffies
  719. */
  720. static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
  721. unsigned long jiffies)
  722. {
  723. u64 tmp = jiffies_to_msecs(jiffies);
  724. return nla_put(skb, attrtype, sizeof(u64), &tmp);
  725. }
  726. #define NLA_PUT(skb, attrtype, attrlen, data) \
  727. do { \
  728. if (nla_put(skb, attrtype, attrlen, data) < 0) \
  729. goto nla_put_failure; \
  730. } while(0)
  731. #define NLA_PUT_TYPE(skb, type, attrtype, value) \
  732. do { \
  733. type __tmp = value; \
  734. NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \
  735. } while(0)
  736. #define NLA_PUT_U8(skb, attrtype, value) \
  737. NLA_PUT_TYPE(skb, u8, attrtype, value)
  738. #define NLA_PUT_U16(skb, attrtype, value) \
  739. NLA_PUT_TYPE(skb, u16, attrtype, value)
  740. #define NLA_PUT_U32(skb, attrtype, value) \
  741. NLA_PUT_TYPE(skb, u32, attrtype, value)
  742. #define NLA_PUT_U64(skb, attrtype, value) \
  743. NLA_PUT_TYPE(skb, u64, attrtype, value)
  744. #define NLA_PUT_STRING(skb, attrtype, value) \
  745. NLA_PUT(skb, attrtype, strlen(value) + 1, value)
  746. #define NLA_PUT_FLAG(skb, attrtype, value) \
  747. NLA_PUT(skb, attrtype, 0, NULL)
  748. #define NLA_PUT_MSECS(skb, attrtype, jiffies) \
  749. NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
  750. /**
  751. * nla_get_u32 - return payload of u32 attribute
  752. * @nla: u32 netlink attribute
  753. */
  754. static inline u32 nla_get_u32(struct nlattr *nla)
  755. {
  756. return *(u32 *) nla_data(nla);
  757. }
  758. /**
  759. * nla_get_u16 - return payload of u16 attribute
  760. * @nla: u16 netlink attribute
  761. */
  762. static inline u16 nla_get_u16(struct nlattr *nla)
  763. {
  764. return *(u16 *) nla_data(nla);
  765. }
  766. /**
  767. * nla_get_u8 - return payload of u8 attribute
  768. * @nla: u8 netlink attribute
  769. */
  770. static inline u8 nla_get_u8(struct nlattr *nla)
  771. {
  772. return *(u8 *) nla_data(nla);
  773. }
  774. /**
  775. * nla_get_u64 - return payload of u64 attribute
  776. * @nla: u64 netlink attribute
  777. */
  778. static inline u64 nla_get_u64(struct nlattr *nla)
  779. {
  780. u64 tmp;
  781. nla_memcpy(&tmp, nla, sizeof(tmp));
  782. return tmp;
  783. }
  784. /**
  785. * nla_get_flag - return payload of flag attribute
  786. * @nla: flag netlink attribute
  787. */
  788. static inline int nla_get_flag(struct nlattr *nla)
  789. {
  790. return !!nla;
  791. }
  792. /**
  793. * nla_get_msecs - return payload of msecs attribute
  794. * @nla: msecs netlink attribute
  795. *
  796. * Returns the number of milliseconds in jiffies.
  797. */
  798. static inline unsigned long nla_get_msecs(struct nlattr *nla)
  799. {
  800. u64 msecs = nla_get_u64(nla);
  801. return msecs_to_jiffies((unsigned long) msecs);
  802. }
  803. /**
  804. * nla_nest_start - Start a new level of nested attributes
  805. * @skb: socket buffer to add attributes to
  806. * @attrtype: attribute type of container
  807. *
  808. * Returns the container attribute
  809. */
  810. static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
  811. {
  812. struct nlattr *start = (struct nlattr *) skb->tail;
  813. if (nla_put(skb, attrtype, 0, NULL) < 0)
  814. return NULL;
  815. return start;
  816. }
  817. /**
  818. * nla_nest_end - Finalize nesting of attributes
  819. * @skb: socket buffer the attribtues are stored in
  820. * @start: container attribute
  821. *
  822. * Corrects the container attribute header to include the all
  823. * appeneded attributes.
  824. *
  825. * Returns the total data length of the skb.
  826. */
  827. static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
  828. {
  829. start->nla_len = skb->tail - (unsigned char *) start;
  830. return skb->len;
  831. }
  832. /**
  833. * nla_nest_cancel - Cancel nesting of attributes
  834. * @skb: socket buffer the message is stored in
  835. * @start: container attribute
  836. *
  837. * Removes the container attribute and including all nested
  838. * attributes. Returns -1.
  839. */
  840. static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
  841. {
  842. return nlmsg_trim(skb, start);
  843. }
  844. /**
  845. * nla_for_each_attr - iterate over a stream of attributes
  846. * @pos: loop counter, set to current attribute
  847. * @head: head of attribute stream
  848. * @len: length of attribute stream
  849. * @rem: initialized to len, holds bytes currently remaining in stream
  850. */
  851. #define nla_for_each_attr(pos, head, len, rem) \
  852. for (pos = head, rem = len; \
  853. nla_ok(pos, rem); \
  854. pos = nla_next(pos, &(rem)))
  855. /**
  856. * nla_for_each_nested - iterate over nested attributes
  857. * @pos: loop counter, set to current attribute
  858. * @nla: attribute containing the nested attributes
  859. * @rem: initialized to len, holds bytes currently remaining in stream
  860. */
  861. #define nla_for_each_nested(pos, nla, rem) \
  862. nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
  863. #endif