netlink.h 28 KB

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