netlink.h 32 KB

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