netlink.h 29 KB

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