netlink.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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_s8(skb, type, value) add s8 attribute to skb
  100. * nla_put_s16(skb, type, value) add s16 attribute to skb
  101. * nla_put_s32(skb, type, value) add s32 attribute to skb
  102. * nla_put_s64(skb, type, value) add s64 attribute to skb
  103. * nla_put_string(skb, type, str) add string attribute to skb
  104. * nla_put_flag(skb, type) add flag attribute to skb
  105. * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb
  106. *
  107. * Nested Attributes Construction:
  108. * nla_nest_start(skb, type) start a nested attribute
  109. * nla_nest_end(skb, nla) finalize a nested attribute
  110. * nla_nest_cancel(skb, nla) cancel nested attribute construction
  111. *
  112. * Attribute Length Calculations:
  113. * nla_attr_size(payload) length of attribute w/o padding
  114. * nla_total_size(payload) length of attribute w/ padding
  115. * nla_padlen(payload) length of padding
  116. *
  117. * Attribute Payload Access:
  118. * nla_data(nla) head of attribute payload
  119. * nla_len(nla) length of attribute payload
  120. *
  121. * Attribute Payload Access for Basic Types:
  122. * nla_get_u8(nla) get payload for a u8 attribute
  123. * nla_get_u16(nla) get payload for a u16 attribute
  124. * nla_get_u32(nla) get payload for a u32 attribute
  125. * nla_get_u64(nla) get payload for a u64 attribute
  126. * nla_get_s8(nla) get payload for a s8 attribute
  127. * nla_get_s16(nla) get payload for a s16 attribute
  128. * nla_get_s32(nla) get payload for a s32 attribute
  129. * nla_get_s64(nla) get payload for a s64 attribute
  130. * nla_get_flag(nla) return 1 if flag is true
  131. * nla_get_msecs(nla) get payload for a msecs attribute
  132. *
  133. * Attribute Misc:
  134. * nla_memcpy(dest, nla, count) copy attribute into memory
  135. * nla_memcmp(nla, data, size) compare attribute with memory area
  136. * nla_strlcpy(dst, nla, size) copy attribute to a sized string
  137. * nla_strcmp(nla, str) compare attribute with string
  138. *
  139. * Attribute Parsing:
  140. * nla_ok(nla, remaining) does nla fit into remaining bytes?
  141. * nla_next(nla, remaining) get next netlink attribute
  142. * nla_validate() validate a stream of attributes
  143. * nla_validate_nested() validate a stream of nested attributes
  144. * nla_find() find attribute in stream of attributes
  145. * nla_find_nested() find attribute in nested attributes
  146. * nla_parse() parse and validate stream of attrs
  147. * nla_parse_nested() parse nested attribuets
  148. * nla_for_each_attr() loop over all attributes
  149. * nla_for_each_nested() loop over the nested attributes
  150. *=========================================================================
  151. */
  152. /**
  153. * Standard attribute types to specify validation policy
  154. */
  155. enum {
  156. NLA_UNSPEC,
  157. NLA_U8,
  158. NLA_U16,
  159. NLA_U32,
  160. NLA_U64,
  161. NLA_STRING,
  162. NLA_FLAG,
  163. NLA_MSECS,
  164. NLA_NESTED,
  165. NLA_NESTED_COMPAT,
  166. NLA_NUL_STRING,
  167. NLA_BINARY,
  168. NLA_S8,
  169. NLA_S16,
  170. NLA_S32,
  171. NLA_S64,
  172. __NLA_TYPE_MAX,
  173. };
  174. #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
  175. /**
  176. * struct nla_policy - attribute validation policy
  177. * @type: Type of attribute or NLA_UNSPEC
  178. * @len: Type specific length of payload
  179. *
  180. * Policies are defined as arrays of this struct, the array must be
  181. * accessible by attribute type up to the highest identifier to be expected.
  182. *
  183. * Meaning of `len' field:
  184. * NLA_STRING Maximum length of string
  185. * NLA_NUL_STRING Maximum length of string (excluding NUL)
  186. * NLA_FLAG Unused
  187. * NLA_BINARY Maximum length of attribute payload
  188. * NLA_NESTED Don't use `len' field -- length verification is
  189. * done by checking len of nested header (or empty)
  190. * NLA_NESTED_COMPAT Minimum length of structure payload
  191. * NLA_U8, NLA_U16,
  192. * NLA_U32, NLA_U64,
  193. * NLA_S8, NLA_S16,
  194. * NLA_S32, NLA_S64,
  195. * NLA_MSECS Leaving the length field zero will verify the
  196. * given type fits, using it verifies minimum length
  197. * just like "All other"
  198. * All other Minimum length of attribute payload
  199. *
  200. * Example:
  201. * static const struct nla_policy my_policy[ATTR_MAX+1] = {
  202. * [ATTR_FOO] = { .type = NLA_U16 },
  203. * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
  204. * [ATTR_BAZ] = { .len = sizeof(struct mystruct) },
  205. * };
  206. */
  207. struct nla_policy {
  208. u16 type;
  209. u16 len;
  210. };
  211. /**
  212. * struct nl_info - netlink source information
  213. * @nlh: Netlink message header of original request
  214. * @portid: Netlink PORTID of requesting application
  215. */
  216. struct nl_info {
  217. struct nlmsghdr *nlh;
  218. struct net *nl_net;
  219. u32 portid;
  220. };
  221. int netlink_rcv_skb(struct sk_buff *skb,
  222. int (*cb)(struct sk_buff *, struct nlmsghdr *));
  223. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  224. unsigned int group, int report, gfp_t flags);
  225. int nla_validate(const struct nlattr *head, int len, int maxtype,
  226. const struct nla_policy *policy);
  227. int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
  228. int len, const struct nla_policy *policy);
  229. int nla_policy_len(const struct nla_policy *, int);
  230. struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
  231. size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize);
  232. int nla_memcpy(void *dest, const struct nlattr *src, int count);
  233. int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
  234. int nla_strcmp(const struct nlattr *nla, const char *str);
  235. struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
  236. void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
  237. struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
  238. void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
  239. void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
  240. const void *data);
  241. void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
  242. int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
  243. int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
  244. int nla_append(struct sk_buff *skb, int attrlen, const void *data);
  245. /**************************************************************************
  246. * Netlink Messages
  247. **************************************************************************/
  248. /**
  249. * nlmsg_msg_size - length of netlink message not including padding
  250. * @payload: length of message payload
  251. */
  252. static inline int nlmsg_msg_size(int payload)
  253. {
  254. return NLMSG_HDRLEN + payload;
  255. }
  256. /**
  257. * nlmsg_total_size - length of netlink message including padding
  258. * @payload: length of message payload
  259. */
  260. static inline int nlmsg_total_size(int payload)
  261. {
  262. return NLMSG_ALIGN(nlmsg_msg_size(payload));
  263. }
  264. /**
  265. * nlmsg_padlen - length of padding at the message's tail
  266. * @payload: length of message payload
  267. */
  268. static inline int nlmsg_padlen(int payload)
  269. {
  270. return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
  271. }
  272. /**
  273. * nlmsg_data - head of message payload
  274. * @nlh: netlink message header
  275. */
  276. static inline void *nlmsg_data(const struct nlmsghdr *nlh)
  277. {
  278. return (unsigned char *) nlh + NLMSG_HDRLEN;
  279. }
  280. /**
  281. * nlmsg_len - length of message payload
  282. * @nlh: netlink message header
  283. */
  284. static inline int nlmsg_len(const struct nlmsghdr *nlh)
  285. {
  286. return nlh->nlmsg_len - NLMSG_HDRLEN;
  287. }
  288. /**
  289. * nlmsg_attrdata - head of attributes data
  290. * @nlh: netlink message header
  291. * @hdrlen: length of family specific header
  292. */
  293. static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
  294. int hdrlen)
  295. {
  296. unsigned char *data = nlmsg_data(nlh);
  297. return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
  298. }
  299. /**
  300. * nlmsg_attrlen - length of attributes data
  301. * @nlh: netlink message header
  302. * @hdrlen: length of family specific header
  303. */
  304. static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
  305. {
  306. return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
  307. }
  308. /**
  309. * nlmsg_ok - check if the netlink message fits into the remaining bytes
  310. * @nlh: netlink message header
  311. * @remaining: number of bytes remaining in message stream
  312. */
  313. static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
  314. {
  315. return (remaining >= (int) sizeof(struct nlmsghdr) &&
  316. nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
  317. nlh->nlmsg_len <= remaining);
  318. }
  319. /**
  320. * nlmsg_next - next netlink message in message stream
  321. * @nlh: netlink message header
  322. * @remaining: number of bytes remaining in message stream
  323. *
  324. * Returns the next netlink message in the message stream and
  325. * decrements remaining by the size of the current message.
  326. */
  327. static inline struct nlmsghdr *
  328. nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
  329. {
  330. int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
  331. *remaining -= totlen;
  332. return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
  333. }
  334. /**
  335. * nlmsg_parse - parse attributes of a netlink message
  336. * @nlh: netlink message header
  337. * @hdrlen: length of family specific header
  338. * @tb: destination array with maxtype+1 elements
  339. * @maxtype: maximum attribute type to be expected
  340. * @policy: validation policy
  341. *
  342. * See nla_parse()
  343. */
  344. static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
  345. struct nlattr *tb[], int maxtype,
  346. const struct nla_policy *policy)
  347. {
  348. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  349. return -EINVAL;
  350. return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
  351. nlmsg_attrlen(nlh, hdrlen), policy);
  352. }
  353. /**
  354. * nlmsg_find_attr - find a specific attribute in a netlink message
  355. * @nlh: netlink message header
  356. * @hdrlen: length of familiy specific header
  357. * @attrtype: type of attribute to look for
  358. *
  359. * Returns the first attribute which matches the specified type.
  360. */
  361. static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
  362. int hdrlen, int attrtype)
  363. {
  364. return nla_find(nlmsg_attrdata(nlh, hdrlen),
  365. nlmsg_attrlen(nlh, hdrlen), attrtype);
  366. }
  367. /**
  368. * nlmsg_validate - validate a netlink message including attributes
  369. * @nlh: netlinket message header
  370. * @hdrlen: length of familiy specific header
  371. * @maxtype: maximum attribute type to be expected
  372. * @policy: validation policy
  373. */
  374. static inline int nlmsg_validate(const struct nlmsghdr *nlh,
  375. int hdrlen, int maxtype,
  376. const struct nla_policy *policy)
  377. {
  378. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  379. return -EINVAL;
  380. return nla_validate(nlmsg_attrdata(nlh, hdrlen),
  381. nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
  382. }
  383. /**
  384. * nlmsg_report - need to report back to application?
  385. * @nlh: netlink message header
  386. *
  387. * Returns 1 if a report back to the application is requested.
  388. */
  389. static inline int nlmsg_report(const struct nlmsghdr *nlh)
  390. {
  391. return !!(nlh->nlmsg_flags & NLM_F_ECHO);
  392. }
  393. /**
  394. * nlmsg_for_each_attr - iterate over a stream of attributes
  395. * @pos: loop counter, set to current attribute
  396. * @nlh: netlink message header
  397. * @hdrlen: length of familiy specific header
  398. * @rem: initialized to len, holds bytes currently remaining in stream
  399. */
  400. #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
  401. nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
  402. nlmsg_attrlen(nlh, hdrlen), rem)
  403. /**
  404. * nlmsg_put - Add a new netlink message to an skb
  405. * @skb: socket buffer to store message in
  406. * @portid: netlink PORTID of requesting application
  407. * @seq: sequence number of message
  408. * @type: message type
  409. * @payload: length of message payload
  410. * @flags: message flags
  411. *
  412. * Returns NULL if the tailroom of the skb is insufficient to store
  413. * the message header and payload.
  414. */
  415. static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  416. int type, int payload, int flags)
  417. {
  418. if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
  419. return NULL;
  420. return __nlmsg_put(skb, portid, seq, type, payload, flags);
  421. }
  422. /**
  423. * nlmsg_put_answer - Add a new callback based netlink message to an skb
  424. * @skb: socket buffer to store message in
  425. * @cb: netlink callback
  426. * @type: message type
  427. * @payload: length of message payload
  428. * @flags: message flags
  429. *
  430. * Returns NULL if the tailroom of the skb is insufficient to store
  431. * the message header and payload.
  432. */
  433. static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
  434. struct netlink_callback *cb,
  435. int type, int payload,
  436. int flags)
  437. {
  438. return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  439. type, payload, flags);
  440. }
  441. /**
  442. * nlmsg_new - Allocate a new netlink message
  443. * @payload: size of the message payload
  444. * @flags: the type of memory to allocate.
  445. *
  446. * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
  447. * and a good default is needed.
  448. */
  449. static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
  450. {
  451. return alloc_skb(nlmsg_total_size(payload), flags);
  452. }
  453. /**
  454. * nlmsg_end - Finalize a netlink message
  455. * @skb: socket buffer the message is stored in
  456. * @nlh: netlink message header
  457. *
  458. * Corrects the netlink message header to include the appeneded
  459. * attributes. Only necessary if attributes have been added to
  460. * the message.
  461. */
  462. static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
  463. {
  464. nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
  465. }
  466. /**
  467. * nlmsg_get_pos - return current position in netlink message
  468. * @skb: socket buffer the message is stored in
  469. *
  470. * Returns a pointer to the current tail of the message.
  471. */
  472. static inline void *nlmsg_get_pos(struct sk_buff *skb)
  473. {
  474. return skb_tail_pointer(skb);
  475. }
  476. /**
  477. * nlmsg_trim - Trim message to a mark
  478. * @skb: socket buffer the message is stored in
  479. * @mark: mark to trim to
  480. *
  481. * Trims the message to the provided mark.
  482. */
  483. static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
  484. {
  485. if (mark) {
  486. WARN_ON((unsigned char *) mark < skb->data);
  487. skb_trim(skb, (unsigned char *) mark - skb->data);
  488. }
  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. * @portid: own netlink portid 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 portid, unsigned int group, gfp_t flags)
  520. {
  521. int err;
  522. NETLINK_CB(skb).dst_group = group;
  523. err = netlink_broadcast(sk, skb, portid, 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. * @portid: netlink portid of the destination socket
  533. */
  534. static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
  535. {
  536. int err;
  537. err = netlink_unicast(sk, skb, portid, 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_s8 - Add a s8 netlink attribute to a socket buffer
  811. * @skb: socket buffer to add attribute to
  812. * @attrtype: attribute type
  813. * @value: numeric value
  814. */
  815. static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
  816. {
  817. return nla_put(skb, attrtype, sizeof(s8), &value);
  818. }
  819. /**
  820. * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
  821. * @skb: socket buffer to add attribute to
  822. * @attrtype: attribute type
  823. * @value: numeric value
  824. */
  825. static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
  826. {
  827. return nla_put(skb, attrtype, sizeof(s16), &value);
  828. }
  829. /**
  830. * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
  831. * @skb: socket buffer to add attribute to
  832. * @attrtype: attribute type
  833. * @value: numeric value
  834. */
  835. static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
  836. {
  837. return nla_put(skb, attrtype, sizeof(s32), &value);
  838. }
  839. /**
  840. * nla_put_s64 - Add a s64 netlink attribute to a socket buffer
  841. * @skb: socket buffer to add attribute to
  842. * @attrtype: attribute type
  843. * @value: numeric value
  844. */
  845. static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value)
  846. {
  847. return nla_put(skb, attrtype, sizeof(s64), &value);
  848. }
  849. /**
  850. * nla_put_string - Add a string netlink attribute to a socket buffer
  851. * @skb: socket buffer to add attribute to
  852. * @attrtype: attribute type
  853. * @str: NUL terminated string
  854. */
  855. static inline int nla_put_string(struct sk_buff *skb, int attrtype,
  856. const char *str)
  857. {
  858. return nla_put(skb, attrtype, strlen(str) + 1, str);
  859. }
  860. /**
  861. * nla_put_flag - Add a flag netlink attribute to a socket buffer
  862. * @skb: socket buffer to add attribute to
  863. * @attrtype: attribute type
  864. */
  865. static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
  866. {
  867. return nla_put(skb, attrtype, 0, NULL);
  868. }
  869. /**
  870. * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
  871. * @skb: socket buffer to add attribute to
  872. * @attrtype: attribute type
  873. * @njiffies: number of jiffies to convert to msecs
  874. */
  875. static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
  876. unsigned long njiffies)
  877. {
  878. u64 tmp = jiffies_to_msecs(njiffies);
  879. return nla_put(skb, attrtype, sizeof(u64), &tmp);
  880. }
  881. /**
  882. * nla_get_u32 - return payload of u32 attribute
  883. * @nla: u32 netlink attribute
  884. */
  885. static inline u32 nla_get_u32(const struct nlattr *nla)
  886. {
  887. return *(u32 *) nla_data(nla);
  888. }
  889. /**
  890. * nla_get_be32 - return payload of __be32 attribute
  891. * @nla: __be32 netlink attribute
  892. */
  893. static inline __be32 nla_get_be32(const struct nlattr *nla)
  894. {
  895. return *(__be32 *) nla_data(nla);
  896. }
  897. /**
  898. * nla_get_u16 - return payload of u16 attribute
  899. * @nla: u16 netlink attribute
  900. */
  901. static inline u16 nla_get_u16(const struct nlattr *nla)
  902. {
  903. return *(u16 *) nla_data(nla);
  904. }
  905. /**
  906. * nla_get_be16 - return payload of __be16 attribute
  907. * @nla: __be16 netlink attribute
  908. */
  909. static inline __be16 nla_get_be16(const struct nlattr *nla)
  910. {
  911. return *(__be16 *) nla_data(nla);
  912. }
  913. /**
  914. * nla_get_le16 - return payload of __le16 attribute
  915. * @nla: __le16 netlink attribute
  916. */
  917. static inline __le16 nla_get_le16(const struct nlattr *nla)
  918. {
  919. return *(__le16 *) nla_data(nla);
  920. }
  921. /**
  922. * nla_get_u8 - return payload of u8 attribute
  923. * @nla: u8 netlink attribute
  924. */
  925. static inline u8 nla_get_u8(const struct nlattr *nla)
  926. {
  927. return *(u8 *) nla_data(nla);
  928. }
  929. /**
  930. * nla_get_u64 - return payload of u64 attribute
  931. * @nla: u64 netlink attribute
  932. */
  933. static inline u64 nla_get_u64(const struct nlattr *nla)
  934. {
  935. u64 tmp;
  936. nla_memcpy(&tmp, nla, sizeof(tmp));
  937. return tmp;
  938. }
  939. /**
  940. * nla_get_be64 - return payload of __be64 attribute
  941. * @nla: __be64 netlink attribute
  942. */
  943. static inline __be64 nla_get_be64(const struct nlattr *nla)
  944. {
  945. __be64 tmp;
  946. nla_memcpy(&tmp, nla, sizeof(tmp));
  947. return tmp;
  948. }
  949. /**
  950. * nla_get_s32 - return payload of s32 attribute
  951. * @nla: s32 netlink attribute
  952. */
  953. static inline s32 nla_get_s32(const struct nlattr *nla)
  954. {
  955. return *(s32 *) nla_data(nla);
  956. }
  957. /**
  958. * nla_get_s16 - return payload of s16 attribute
  959. * @nla: s16 netlink attribute
  960. */
  961. static inline s16 nla_get_s16(const struct nlattr *nla)
  962. {
  963. return *(s16 *) nla_data(nla);
  964. }
  965. /**
  966. * nla_get_s8 - return payload of s8 attribute
  967. * @nla: s8 netlink attribute
  968. */
  969. static inline s8 nla_get_s8(const struct nlattr *nla)
  970. {
  971. return *(s8 *) nla_data(nla);
  972. }
  973. /**
  974. * nla_get_s64 - return payload of s64 attribute
  975. * @nla: s64 netlink attribute
  976. */
  977. static inline s64 nla_get_s64(const struct nlattr *nla)
  978. {
  979. s64 tmp;
  980. nla_memcpy(&tmp, nla, sizeof(tmp));
  981. return tmp;
  982. }
  983. /**
  984. * nla_get_flag - return payload of flag attribute
  985. * @nla: flag netlink attribute
  986. */
  987. static inline int nla_get_flag(const struct nlattr *nla)
  988. {
  989. return !!nla;
  990. }
  991. /**
  992. * nla_get_msecs - return payload of msecs attribute
  993. * @nla: msecs netlink attribute
  994. *
  995. * Returns the number of milliseconds in jiffies.
  996. */
  997. static inline unsigned long nla_get_msecs(const struct nlattr *nla)
  998. {
  999. u64 msecs = nla_get_u64(nla);
  1000. return msecs_to_jiffies((unsigned long) msecs);
  1001. }
  1002. /**
  1003. * nla_nest_start - Start a new level of nested attributes
  1004. * @skb: socket buffer to add attributes to
  1005. * @attrtype: attribute type of container
  1006. *
  1007. * Returns the container attribute
  1008. */
  1009. static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
  1010. {
  1011. struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
  1012. if (nla_put(skb, attrtype, 0, NULL) < 0)
  1013. return NULL;
  1014. return start;
  1015. }
  1016. /**
  1017. * nla_nest_end - Finalize nesting of attributes
  1018. * @skb: socket buffer the attributes are stored in
  1019. * @start: container attribute
  1020. *
  1021. * Corrects the container attribute header to include the all
  1022. * appeneded attributes.
  1023. *
  1024. * Returns the total data length of the skb.
  1025. */
  1026. static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
  1027. {
  1028. start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
  1029. return skb->len;
  1030. }
  1031. /**
  1032. * nla_nest_cancel - Cancel nesting of attributes
  1033. * @skb: socket buffer the message is stored in
  1034. * @start: container attribute
  1035. *
  1036. * Removes the container attribute and including all nested
  1037. * attributes. Returns -EMSGSIZE
  1038. */
  1039. static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
  1040. {
  1041. nlmsg_trim(skb, start);
  1042. }
  1043. /**
  1044. * nla_validate_nested - Validate a stream of nested attributes
  1045. * @start: container attribute
  1046. * @maxtype: maximum attribute type to be expected
  1047. * @policy: validation policy
  1048. *
  1049. * Validates all attributes in the nested attribute stream against the
  1050. * specified policy. Attributes with a type exceeding maxtype will be
  1051. * ignored. See documenation of struct nla_policy for more details.
  1052. *
  1053. * Returns 0 on success or a negative error code.
  1054. */
  1055. static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
  1056. const struct nla_policy *policy)
  1057. {
  1058. return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
  1059. }
  1060. /**
  1061. * nla_for_each_attr - iterate over a stream of attributes
  1062. * @pos: loop counter, set to current attribute
  1063. * @head: head of attribute stream
  1064. * @len: length of attribute stream
  1065. * @rem: initialized to len, holds bytes currently remaining in stream
  1066. */
  1067. #define nla_for_each_attr(pos, head, len, rem) \
  1068. for (pos = head, rem = len; \
  1069. nla_ok(pos, rem); \
  1070. pos = nla_next(pos, &(rem)))
  1071. /**
  1072. * nla_for_each_nested - iterate over nested attributes
  1073. * @pos: loop counter, set to current attribute
  1074. * @nla: attribute containing the nested attributes
  1075. * @rem: initialized to len, holds bytes currently remaining in stream
  1076. */
  1077. #define nla_for_each_nested(pos, nla, rem) \
  1078. nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
  1079. /**
  1080. * nla_is_last - Test if attribute is last in stream
  1081. * @nla: attribute to test
  1082. * @rem: bytes remaining in stream
  1083. */
  1084. static inline bool nla_is_last(const struct nlattr *nla, int rem)
  1085. {
  1086. return nla->nla_len == rem;
  1087. }
  1088. #endif