netlink.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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. * 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 struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
  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(struct nlattr *head, int len, int maxtype,
  221. const struct nla_policy *policy);
  222. extern int nla_parse(struct nlattr *tb[], int maxtype,
  223. struct nlattr *head, int len,
  224. const struct nla_policy *policy);
  225. extern struct nlattr * nla_find(struct nlattr *head, int len, int attrtype);
  226. extern size_t nla_strlcpy(char *dst, const struct nlattr *nla,
  227. size_t dstsize);
  228. extern int nla_memcpy(void *dest, struct nlattr *src, int count);
  229. extern int nla_memcmp(const struct nlattr *nla, const void *data,
  230. size_t size);
  231. extern int nla_strcmp(const struct nlattr *nla, const char *str);
  232. extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype,
  233. int attrlen);
  234. extern void * __nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
  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 void __nla_put(struct sk_buff *skb, int attrtype,
  239. int attrlen, const void *data);
  240. extern void __nla_put_nohdr(struct sk_buff *skb, int attrlen,
  241. const void *data);
  242. extern int nla_put(struct sk_buff *skb, int attrtype,
  243. int attrlen, const void *data);
  244. extern int nla_put_nohdr(struct sk_buff *skb, int attrlen,
  245. const void *data);
  246. extern int nla_append(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.
  516. */
  517. static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
  518. {
  519. if (mark)
  520. skb_trim(skb, (unsigned char *) mark - skb->data);
  521. }
  522. /**
  523. * nlmsg_cancel - Cancel construction of a netlink message
  524. * @skb: socket buffer the message is stored in
  525. * @nlh: netlink message header
  526. *
  527. * Removes the complete netlink message including all
  528. * attributes from the socket buffer again.
  529. */
  530. static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
  531. {
  532. nlmsg_trim(skb, nlh);
  533. }
  534. /**
  535. * nlmsg_free - free a netlink message
  536. * @skb: socket buffer of netlink message
  537. */
  538. static inline void nlmsg_free(struct sk_buff *skb)
  539. {
  540. kfree_skb(skb);
  541. }
  542. /**
  543. * nlmsg_multicast - multicast a netlink message
  544. * @sk: netlink socket to spread messages to
  545. * @skb: netlink message as socket buffer
  546. * @pid: own netlink pid to avoid sending to yourself
  547. * @group: multicast group id
  548. * @flags: allocation flags
  549. */
  550. static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
  551. u32 pid, unsigned int group, gfp_t flags)
  552. {
  553. int err;
  554. NETLINK_CB(skb).dst_group = group;
  555. err = netlink_broadcast(sk, skb, pid, group, flags);
  556. if (err > 0)
  557. err = 0;
  558. return err;
  559. }
  560. /**
  561. * nlmsg_unicast - unicast a netlink message
  562. * @sk: netlink socket to spread message to
  563. * @skb: netlink message as socket buffer
  564. * @pid: netlink pid of the destination socket
  565. */
  566. static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid)
  567. {
  568. int err;
  569. err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
  570. if (err > 0)
  571. err = 0;
  572. return err;
  573. }
  574. /**
  575. * nlmsg_for_each_msg - iterate over a stream of messages
  576. * @pos: loop counter, set to current message
  577. * @head: head of message stream
  578. * @len: length of message stream
  579. * @rem: initialized to len, holds bytes currently remaining in stream
  580. */
  581. #define nlmsg_for_each_msg(pos, head, len, rem) \
  582. for (pos = head, rem = len; \
  583. nlmsg_ok(pos, rem); \
  584. pos = nlmsg_next(pos, &(rem)))
  585. /**************************************************************************
  586. * Netlink Attributes
  587. **************************************************************************/
  588. /**
  589. * nla_attr_size - length of attribute not including padding
  590. * @payload: length of payload
  591. */
  592. static inline int nla_attr_size(int payload)
  593. {
  594. return NLA_HDRLEN + payload;
  595. }
  596. /**
  597. * nla_total_size - total length of attribute including padding
  598. * @payload: length of payload
  599. */
  600. static inline int nla_total_size(int payload)
  601. {
  602. return NLA_ALIGN(nla_attr_size(payload));
  603. }
  604. /**
  605. * nla_padlen - length of padding at the tail of attribute
  606. * @payload: length of payload
  607. */
  608. static inline int nla_padlen(int payload)
  609. {
  610. return nla_total_size(payload) - nla_attr_size(payload);
  611. }
  612. /**
  613. * nla_type - attribute type
  614. * @nla: netlink attribute
  615. */
  616. static inline int nla_type(const struct nlattr *nla)
  617. {
  618. return nla->nla_type & NLA_TYPE_MASK;
  619. }
  620. /**
  621. * nla_data - head of payload
  622. * @nla: netlink attribute
  623. */
  624. static inline void *nla_data(const struct nlattr *nla)
  625. {
  626. return (char *) nla + NLA_HDRLEN;
  627. }
  628. /**
  629. * nla_len - length of payload
  630. * @nla: netlink attribute
  631. */
  632. static inline int nla_len(const struct nlattr *nla)
  633. {
  634. return nla->nla_len - NLA_HDRLEN;
  635. }
  636. /**
  637. * nla_ok - check if the netlink attribute fits into the remaining bytes
  638. * @nla: netlink attribute
  639. * @remaining: number of bytes remaining in attribute stream
  640. */
  641. static inline int nla_ok(const struct nlattr *nla, int remaining)
  642. {
  643. return remaining >= (int) sizeof(*nla) &&
  644. nla->nla_len >= sizeof(*nla) &&
  645. nla->nla_len <= remaining;
  646. }
  647. /**
  648. * nla_next - next netlink attribute in attribute stream
  649. * @nla: netlink attribute
  650. * @remaining: number of bytes remaining in attribute stream
  651. *
  652. * Returns the next netlink attribute in the attribute stream and
  653. * decrements remaining by the size of the current attribute.
  654. */
  655. static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
  656. {
  657. int totlen = NLA_ALIGN(nla->nla_len);
  658. *remaining -= totlen;
  659. return (struct nlattr *) ((char *) nla + totlen);
  660. }
  661. /**
  662. * nla_find_nested - find attribute in a set of nested attributes
  663. * @nla: attribute containing the nested attributes
  664. * @attrtype: type of attribute to look for
  665. *
  666. * Returns the first attribute which matches the specified type.
  667. */
  668. static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype)
  669. {
  670. return nla_find(nla_data(nla), nla_len(nla), attrtype);
  671. }
  672. /**
  673. * nla_parse_nested - parse nested attributes
  674. * @tb: destination array with maxtype+1 elements
  675. * @maxtype: maximum attribute type to be expected
  676. * @nla: attribute containing the nested attributes
  677. * @policy: validation policy
  678. *
  679. * See nla_parse()
  680. */
  681. static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
  682. struct nlattr *nla,
  683. const struct nla_policy *policy)
  684. {
  685. return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
  686. }
  687. /**
  688. * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
  689. * @skb: socket buffer to add attribute to
  690. * @attrtype: attribute type
  691. * @value: numeric value
  692. */
  693. static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
  694. {
  695. return nla_put(skb, attrtype, sizeof(u8), &value);
  696. }
  697. /**
  698. * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
  699. * @skb: socket buffer to add attribute to
  700. * @attrtype: attribute type
  701. * @value: numeric value
  702. */
  703. static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
  704. {
  705. return nla_put(skb, attrtype, sizeof(u16), &value);
  706. }
  707. /**
  708. * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
  709. * @skb: socket buffer to add attribute to
  710. * @attrtype: attribute type
  711. * @value: numeric value
  712. */
  713. static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
  714. {
  715. return nla_put(skb, attrtype, sizeof(u32), &value);
  716. }
  717. /**
  718. * nla_put_64 - Add a u64 netlink attribute to a socket buffer
  719. * @skb: socket buffer to add attribute to
  720. * @attrtype: attribute type
  721. * @value: numeric value
  722. */
  723. static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
  724. {
  725. return nla_put(skb, attrtype, sizeof(u64), &value);
  726. }
  727. /**
  728. * nla_put_string - Add a string netlink attribute to a socket buffer
  729. * @skb: socket buffer to add attribute to
  730. * @attrtype: attribute type
  731. * @str: NUL terminated string
  732. */
  733. static inline int nla_put_string(struct sk_buff *skb, int attrtype,
  734. const char *str)
  735. {
  736. return nla_put(skb, attrtype, strlen(str) + 1, str);
  737. }
  738. /**
  739. * nla_put_flag - Add a flag netlink attribute to a socket buffer
  740. * @skb: socket buffer to add attribute to
  741. * @attrtype: attribute type
  742. */
  743. static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
  744. {
  745. return nla_put(skb, attrtype, 0, NULL);
  746. }
  747. /**
  748. * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
  749. * @skb: socket buffer to add attribute to
  750. * @attrtype: attribute type
  751. * @jiffies: number of msecs in jiffies
  752. */
  753. static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
  754. unsigned long jiffies)
  755. {
  756. u64 tmp = jiffies_to_msecs(jiffies);
  757. return nla_put(skb, attrtype, sizeof(u64), &tmp);
  758. }
  759. #define NLA_PUT(skb, attrtype, attrlen, data) \
  760. do { \
  761. if (unlikely(nla_put(skb, attrtype, attrlen, data) < 0)) \
  762. goto nla_put_failure; \
  763. } while(0)
  764. #define NLA_PUT_TYPE(skb, type, attrtype, value) \
  765. do { \
  766. type __tmp = value; \
  767. NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \
  768. } while(0)
  769. #define NLA_PUT_U8(skb, attrtype, value) \
  770. NLA_PUT_TYPE(skb, u8, attrtype, value)
  771. #define NLA_PUT_U16(skb, attrtype, value) \
  772. NLA_PUT_TYPE(skb, u16, attrtype, value)
  773. #define NLA_PUT_LE16(skb, attrtype, value) \
  774. NLA_PUT_TYPE(skb, __le16, attrtype, value)
  775. #define NLA_PUT_BE16(skb, attrtype, value) \
  776. NLA_PUT_TYPE(skb, __be16, attrtype, value)
  777. #define NLA_PUT_U32(skb, attrtype, value) \
  778. NLA_PUT_TYPE(skb, u32, attrtype, value)
  779. #define NLA_PUT_BE32(skb, attrtype, value) \
  780. NLA_PUT_TYPE(skb, __be32, attrtype, value)
  781. #define NLA_PUT_U64(skb, attrtype, value) \
  782. NLA_PUT_TYPE(skb, u64, attrtype, value)
  783. #define NLA_PUT_BE64(skb, attrtype, value) \
  784. NLA_PUT_TYPE(skb, __be64, attrtype, value)
  785. #define NLA_PUT_STRING(skb, attrtype, value) \
  786. NLA_PUT(skb, attrtype, strlen(value) + 1, value)
  787. #define NLA_PUT_FLAG(skb, attrtype) \
  788. NLA_PUT(skb, attrtype, 0, NULL)
  789. #define NLA_PUT_MSECS(skb, attrtype, jiffies) \
  790. NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
  791. /**
  792. * nla_get_u32 - return payload of u32 attribute
  793. * @nla: u32 netlink attribute
  794. */
  795. static inline u32 nla_get_u32(struct nlattr *nla)
  796. {
  797. return *(u32 *) nla_data(nla);
  798. }
  799. /**
  800. * nla_get_be32 - return payload of __be32 attribute
  801. * @nla: __be32 netlink attribute
  802. */
  803. static inline __be32 nla_get_be32(struct nlattr *nla)
  804. {
  805. return *(__be32 *) nla_data(nla);
  806. }
  807. /**
  808. * nla_get_u16 - return payload of u16 attribute
  809. * @nla: u16 netlink attribute
  810. */
  811. static inline u16 nla_get_u16(struct nlattr *nla)
  812. {
  813. return *(u16 *) nla_data(nla);
  814. }
  815. /**
  816. * nla_get_be16 - return payload of __be16 attribute
  817. * @nla: __be16 netlink attribute
  818. */
  819. static inline __be16 nla_get_be16(struct nlattr *nla)
  820. {
  821. return *(__be16 *) nla_data(nla);
  822. }
  823. /**
  824. * nla_get_le16 - return payload of __le16 attribute
  825. * @nla: __le16 netlink attribute
  826. */
  827. static inline __le16 nla_get_le16(struct nlattr *nla)
  828. {
  829. return *(__le16 *) nla_data(nla);
  830. }
  831. /**
  832. * nla_get_u8 - return payload of u8 attribute
  833. * @nla: u8 netlink attribute
  834. */
  835. static inline u8 nla_get_u8(struct nlattr *nla)
  836. {
  837. return *(u8 *) nla_data(nla);
  838. }
  839. /**
  840. * nla_get_u64 - return payload of u64 attribute
  841. * @nla: u64 netlink attribute
  842. */
  843. static inline u64 nla_get_u64(struct nlattr *nla)
  844. {
  845. u64 tmp;
  846. nla_memcpy(&tmp, nla, sizeof(tmp));
  847. return tmp;
  848. }
  849. /**
  850. * nla_get_flag - return payload of flag attribute
  851. * @nla: flag netlink attribute
  852. */
  853. static inline int nla_get_flag(struct nlattr *nla)
  854. {
  855. return !!nla;
  856. }
  857. /**
  858. * nla_get_msecs - return payload of msecs attribute
  859. * @nla: msecs netlink attribute
  860. *
  861. * Returns the number of milliseconds in jiffies.
  862. */
  863. static inline unsigned long nla_get_msecs(struct nlattr *nla)
  864. {
  865. u64 msecs = nla_get_u64(nla);
  866. return msecs_to_jiffies((unsigned long) msecs);
  867. }
  868. /**
  869. * nla_nest_start - Start a new level of nested attributes
  870. * @skb: socket buffer to add attributes to
  871. * @attrtype: attribute type of container
  872. *
  873. * Returns the container attribute
  874. */
  875. static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
  876. {
  877. struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
  878. if (nla_put(skb, attrtype, 0, NULL) < 0)
  879. return NULL;
  880. return start;
  881. }
  882. /**
  883. * nla_nest_end - Finalize nesting of attributes
  884. * @skb: socket buffer the attributes are stored in
  885. * @start: container attribute
  886. *
  887. * Corrects the container attribute header to include the all
  888. * appeneded attributes.
  889. *
  890. * Returns the total data length of the skb.
  891. */
  892. static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
  893. {
  894. start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
  895. return skb->len;
  896. }
  897. /**
  898. * nla_nest_cancel - Cancel nesting of attributes
  899. * @skb: socket buffer the message is stored in
  900. * @start: container attribute
  901. *
  902. * Removes the container attribute and including all nested
  903. * attributes. Returns -EMSGSIZE
  904. */
  905. static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
  906. {
  907. nlmsg_trim(skb, start);
  908. }
  909. /**
  910. * nla_validate_nested - Validate a stream of nested attributes
  911. * @start: container attribute
  912. * @maxtype: maximum attribute type to be expected
  913. * @policy: validation policy
  914. *
  915. * Validates all attributes in the nested attribute stream against the
  916. * specified policy. Attributes with a type exceeding maxtype will be
  917. * ignored. See documenation of struct nla_policy for more details.
  918. *
  919. * Returns 0 on success or a negative error code.
  920. */
  921. static inline int nla_validate_nested(struct nlattr *start, int maxtype,
  922. const struct nla_policy *policy)
  923. {
  924. return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
  925. }
  926. /**
  927. * nla_for_each_attr - iterate over a stream of attributes
  928. * @pos: loop counter, set to current attribute
  929. * @head: head of attribute stream
  930. * @len: length of attribute stream
  931. * @rem: initialized to len, holds bytes currently remaining in stream
  932. */
  933. #define nla_for_each_attr(pos, head, len, rem) \
  934. for (pos = head, rem = len; \
  935. nla_ok(pos, rem); \
  936. pos = nla_next(pos, &(rem)))
  937. /**
  938. * nla_for_each_nested - iterate over nested attributes
  939. * @pos: loop counter, set to current attribute
  940. * @nla: attribute containing the nested attributes
  941. * @rem: initialized to len, holds bytes currently remaining in stream
  942. */
  943. #define nla_for_each_nested(pos, nla, rem) \
  944. nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
  945. #endif