netlink.h 32 KB

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