netlink.h 32 KB

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