netlink.h 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_NETLINK_H
  3. #define __NET_NETLINK_H
  4. #include <linux/types.h>
  5. #include <linux/netlink.h>
  6. #include <linux/jiffies.h>
  7. #include <linux/in6.h>
  8. /* ========================================================================
  9. * Netlink Messages and Attributes Interface (As Seen On TV)
  10. * ------------------------------------------------------------------------
  11. * Messages Interface
  12. * ------------------------------------------------------------------------
  13. *
  14. * Message Format:
  15. * <--- nlmsg_total_size(payload) --->
  16. * <-- nlmsg_msg_size(payload) ->
  17. * +----------+- - -+-------------+- - -+-------- - -
  18. * | nlmsghdr | Pad | Payload | Pad | nlmsghdr
  19. * +----------+- - -+-------------+- - -+-------- - -
  20. * nlmsg_data(nlh)---^ ^
  21. * nlmsg_next(nlh)-----------------------+
  22. *
  23. * Payload Format:
  24. * <---------------------- nlmsg_len(nlh) --------------------->
  25. * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) ->
  26. * +----------------------+- - -+--------------------------------+
  27. * | Family Header | Pad | Attributes |
  28. * +----------------------+- - -+--------------------------------+
  29. * nlmsg_attrdata(nlh, hdrlen)---^
  30. *
  31. * Data Structures:
  32. * struct nlmsghdr netlink message header
  33. *
  34. * Message Construction:
  35. * nlmsg_new() create a new netlink message
  36. * nlmsg_put() add a netlink message to an skb
  37. * nlmsg_put_answer() callback based nlmsg_put()
  38. * nlmsg_end() finalize netlink message
  39. * nlmsg_get_pos() return current position in message
  40. * nlmsg_trim() trim part of message
  41. * nlmsg_cancel() cancel message construction
  42. * nlmsg_free() free a netlink message
  43. *
  44. * Message Sending:
  45. * nlmsg_multicast() multicast message to several groups
  46. * nlmsg_unicast() unicast a message to a single socket
  47. * nlmsg_notify() send notification message
  48. *
  49. * Message Length Calculations:
  50. * nlmsg_msg_size(payload) length of message w/o padding
  51. * nlmsg_total_size(payload) length of message w/ padding
  52. * nlmsg_padlen(payload) length of padding at tail
  53. *
  54. * Message Payload Access:
  55. * nlmsg_data(nlh) head of message payload
  56. * nlmsg_len(nlh) length of message payload
  57. * nlmsg_attrdata(nlh, hdrlen) head of attributes data
  58. * nlmsg_attrlen(nlh, hdrlen) length of attributes data
  59. *
  60. * Message Parsing:
  61. * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes?
  62. * nlmsg_next(nlh, remaining) get next netlink message
  63. * nlmsg_parse() parse attributes of a message
  64. * nlmsg_find_attr() find an attribute in a message
  65. * nlmsg_for_each_msg() loop over all messages
  66. * nlmsg_validate() validate netlink message incl. attrs
  67. * nlmsg_for_each_attr() loop over all attributes
  68. *
  69. * Misc:
  70. * nlmsg_report() report back to application?
  71. *
  72. * ------------------------------------------------------------------------
  73. * Attributes Interface
  74. * ------------------------------------------------------------------------
  75. *
  76. * Attribute Format:
  77. * <------- nla_total_size(payload) ------->
  78. * <---- nla_attr_size(payload) ----->
  79. * +----------+- - -+- - - - - - - - - +- - -+-------- - -
  80. * | Header | Pad | Payload | Pad | Header
  81. * +----------+- - -+- - - - - - - - - +- - -+-------- - -
  82. * <- nla_len(nla) -> ^
  83. * nla_data(nla)----^ |
  84. * nla_next(nla)-----------------------------'
  85. *
  86. * Data Structures:
  87. * struct nlattr netlink attribute header
  88. *
  89. * Attribute Construction:
  90. * nla_reserve(skb, type, len) reserve room for an attribute
  91. * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr
  92. * nla_put(skb, type, len, data) add attribute to skb
  93. * nla_put_nohdr(skb, len, data) add attribute w/o hdr
  94. * nla_append(skb, len, data) append data to skb
  95. *
  96. * Attribute Construction for Basic Types:
  97. * nla_put_u8(skb, type, value) add u8 attribute to skb
  98. * nla_put_u16(skb, type, value) add u16 attribute to skb
  99. * nla_put_u32(skb, type, value) add u32 attribute to skb
  100. * nla_put_u64_64bit(skb, type,
  101. * value, padattr) add u64 attribute to skb
  102. * nla_put_s8(skb, type, value) add s8 attribute to skb
  103. * nla_put_s16(skb, type, value) add s16 attribute to skb
  104. * nla_put_s32(skb, type, value) add s32 attribute to skb
  105. * nla_put_s64(skb, type, value,
  106. * padattr) add s64 attribute to skb
  107. * nla_put_string(skb, type, str) add string attribute to skb
  108. * nla_put_flag(skb, type) add flag attribute to skb
  109. * nla_put_msecs(skb, type, jiffies,
  110. * padattr) add msecs attribute to skb
  111. * nla_put_in_addr(skb, type, addr) add IPv4 address attribute to skb
  112. * nla_put_in6_addr(skb, type, addr) add IPv6 address attribute to skb
  113. *
  114. * Nested Attributes Construction:
  115. * nla_nest_start(skb, type) start a nested attribute
  116. * nla_nest_end(skb, nla) finalize a nested attribute
  117. * nla_nest_cancel(skb, nla) cancel nested attribute construction
  118. *
  119. * Attribute Length Calculations:
  120. * nla_attr_size(payload) length of attribute w/o padding
  121. * nla_total_size(payload) length of attribute w/ padding
  122. * nla_padlen(payload) length of padding
  123. *
  124. * Attribute Payload Access:
  125. * nla_data(nla) head of attribute payload
  126. * nla_len(nla) length of attribute payload
  127. *
  128. * Attribute Payload Access for Basic Types:
  129. * nla_get_u8(nla) get payload for a u8 attribute
  130. * nla_get_u16(nla) get payload for a u16 attribute
  131. * nla_get_u32(nla) get payload for a u32 attribute
  132. * nla_get_u64(nla) get payload for a u64 attribute
  133. * nla_get_s8(nla) get payload for a s8 attribute
  134. * nla_get_s16(nla) get payload for a s16 attribute
  135. * nla_get_s32(nla) get payload for a s32 attribute
  136. * nla_get_s64(nla) get payload for a s64 attribute
  137. * nla_get_flag(nla) return 1 if flag is true
  138. * nla_get_msecs(nla) get payload for a msecs attribute
  139. *
  140. * Attribute Misc:
  141. * nla_memcpy(dest, nla, count) copy attribute into memory
  142. * nla_memcmp(nla, data, size) compare attribute with memory area
  143. * nla_strscpy(dst, nla, size) copy attribute to a sized string
  144. * nla_strcmp(nla, str) compare attribute with string
  145. *
  146. * Attribute Parsing:
  147. * nla_ok(nla, remaining) does nla fit into remaining bytes?
  148. * nla_next(nla, remaining) get next netlink attribute
  149. * nla_validate() validate a stream of attributes
  150. * nla_validate_nested() validate a stream of nested attributes
  151. * nla_find() find attribute in stream of attributes
  152. * nla_find_nested() find attribute in nested attributes
  153. * nla_parse() parse and validate stream of attrs
  154. * nla_parse_nested() parse nested attributes
  155. * nla_for_each_attr() loop over all attributes
  156. * nla_for_each_nested() loop over the nested attributes
  157. *=========================================================================
  158. */
  159. /**
  160. * Standard attribute types to specify validation policy
  161. */
  162. enum {
  163. NLA_UNSPEC,
  164. NLA_U8,
  165. NLA_U16,
  166. NLA_U32,
  167. NLA_U64,
  168. NLA_STRING,
  169. NLA_FLAG,
  170. NLA_MSECS,
  171. NLA_NESTED,
  172. NLA_NESTED_ARRAY,
  173. NLA_NUL_STRING,
  174. NLA_BINARY,
  175. NLA_S8,
  176. NLA_S16,
  177. NLA_S32,
  178. NLA_S64,
  179. NLA_BITFIELD32,
  180. NLA_REJECT,
  181. NLA_BE16,
  182. NLA_BE32,
  183. __NLA_TYPE_MAX,
  184. };
  185. #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
  186. struct netlink_range_validation {
  187. u64 min, max;
  188. };
  189. struct netlink_range_validation_signed {
  190. s64 min, max;
  191. };
  192. enum nla_policy_validation {
  193. NLA_VALIDATE_NONE,
  194. NLA_VALIDATE_RANGE,
  195. NLA_VALIDATE_RANGE_WARN_TOO_LONG,
  196. NLA_VALIDATE_MIN,
  197. NLA_VALIDATE_MAX,
  198. NLA_VALIDATE_MASK,
  199. NLA_VALIDATE_RANGE_PTR,
  200. NLA_VALIDATE_FUNCTION,
  201. };
  202. /**
  203. * struct nla_policy - attribute validation policy
  204. * @type: Type of attribute or NLA_UNSPEC
  205. * @validation_type: type of attribute validation done in addition to
  206. * type-specific validation (e.g. range, function call), see
  207. * &enum nla_policy_validation
  208. * @len: Type specific length of payload
  209. *
  210. * Policies are defined as arrays of this struct, the array must be
  211. * accessible by attribute type up to the highest identifier to be expected.
  212. *
  213. * Meaning of `len' field:
  214. * NLA_STRING Maximum length of string
  215. * NLA_NUL_STRING Maximum length of string (excluding NUL)
  216. * NLA_FLAG Unused
  217. * NLA_BINARY Maximum length of attribute payload
  218. * (but see also below with the validation type)
  219. * NLA_NESTED,
  220. * NLA_NESTED_ARRAY Length verification is done by checking len of
  221. * nested header (or empty); len field is used if
  222. * nested_policy is also used, for the max attr
  223. * number in the nested policy.
  224. * NLA_U8, NLA_U16,
  225. * NLA_U32, NLA_U64,
  226. * NLA_S8, NLA_S16,
  227. * NLA_S32, NLA_S64,
  228. * NLA_BE16, NLA_BE32,
  229. * NLA_MSECS Leaving the length field zero will verify the
  230. * given type fits, using it verifies minimum length
  231. * just like "All other"
  232. * NLA_BITFIELD32 Unused
  233. * NLA_REJECT Unused
  234. * All other Minimum length of attribute payload
  235. *
  236. * Meaning of validation union:
  237. * NLA_BITFIELD32 This is a 32-bit bitmap/bitselector attribute and
  238. * `bitfield32_valid' is the u32 value of valid flags
  239. * NLA_REJECT This attribute is always rejected and `reject_message'
  240. * may point to a string to report as the error instead
  241. * of the generic one in extended ACK.
  242. * NLA_NESTED `nested_policy' to a nested policy to validate, must
  243. * also set `len' to the max attribute number. Use the
  244. * provided NLA_POLICY_NESTED() macro.
  245. * Note that nla_parse() will validate, but of course not
  246. * parse, the nested sub-policies.
  247. * NLA_NESTED_ARRAY `nested_policy' points to a nested policy to validate,
  248. * must also set `len' to the max attribute number. Use
  249. * the provided NLA_POLICY_NESTED_ARRAY() macro.
  250. * The difference to NLA_NESTED is the structure:
  251. * NLA_NESTED has the nested attributes directly inside
  252. * while an array has the nested attributes at another
  253. * level down and the attribute types directly in the
  254. * nesting don't matter.
  255. * NLA_U8,
  256. * NLA_U16,
  257. * NLA_U32,
  258. * NLA_U64,
  259. * NLA_BE16,
  260. * NLA_BE32,
  261. * NLA_S8,
  262. * NLA_S16,
  263. * NLA_S32,
  264. * NLA_S64 The `min' and `max' fields are used depending on the
  265. * validation_type field, if that is min/max/range then
  266. * the min, max or both are used (respectively) to check
  267. * the value of the integer attribute.
  268. * Note that in the interest of code simplicity and
  269. * struct size both limits are s16, so you cannot
  270. * enforce a range that doesn't fall within the range
  271. * of s16 - do that as usual in the code instead.
  272. * Use the NLA_POLICY_MIN(), NLA_POLICY_MAX() and
  273. * NLA_POLICY_RANGE() macros.
  274. * NLA_U8,
  275. * NLA_U16,
  276. * NLA_U32,
  277. * NLA_U64 If the validation_type field instead is set to
  278. * NLA_VALIDATE_RANGE_PTR, `range' must be a pointer
  279. * to a struct netlink_range_validation that indicates
  280. * the min/max values.
  281. * Use NLA_POLICY_FULL_RANGE().
  282. * NLA_S8,
  283. * NLA_S16,
  284. * NLA_S32,
  285. * NLA_S64 If the validation_type field instead is set to
  286. * NLA_VALIDATE_RANGE_PTR, `range_signed' must be a
  287. * pointer to a struct netlink_range_validation_signed
  288. * that indicates the min/max values.
  289. * Use NLA_POLICY_FULL_RANGE_SIGNED().
  290. *
  291. * NLA_BINARY If the validation type is like the ones for integers
  292. * above, then the min/max length (not value like for
  293. * integers) of the attribute is enforced.
  294. *
  295. * All other Unused - but note that it's a union
  296. *
  297. * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN:
  298. * NLA_BINARY Validation function called for the attribute.
  299. * All other Unused - but note that it's a union
  300. *
  301. * Example:
  302. *
  303. * static const u32 myvalidflags = 0xff231023;
  304. *
  305. * static const struct nla_policy my_policy[ATTR_MAX+1] = {
  306. * [ATTR_FOO] = { .type = NLA_U16 },
  307. * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
  308. * [ATTR_BAZ] = NLA_POLICY_EXACT_LEN(sizeof(struct mystruct)),
  309. * [ATTR_GOO] = NLA_POLICY_BITFIELD32(myvalidflags),
  310. * };
  311. */
  312. struct nla_policy {
  313. u8 type;
  314. u8 validation_type;
  315. u16 len;
  316. union {
  317. /**
  318. * @strict_start_type: first attribute to validate strictly
  319. *
  320. * This entry is special, and used for the attribute at index 0
  321. * only, and specifies special data about the policy, namely it
  322. * specifies the "boundary type" where strict length validation
  323. * starts for any attribute types >= this value, also, strict
  324. * nesting validation starts here.
  325. *
  326. * Additionally, it means that NLA_UNSPEC is actually NLA_REJECT
  327. * for any types >= this, so need to use NLA_POLICY_MIN_LEN() to
  328. * get the previous pure { .len = xyz } behaviour. The advantage
  329. * of this is that types not specified in the policy will be
  330. * rejected.
  331. *
  332. * For completely new families it should be set to 1 so that the
  333. * validation is enforced for all attributes. For existing ones
  334. * it should be set at least when new attributes are added to
  335. * the enum used by the policy, and be set to the new value that
  336. * was added to enforce strict validation from thereon.
  337. */
  338. u16 strict_start_type;
  339. /* private: use NLA_POLICY_*() to set */
  340. const u32 bitfield32_valid;
  341. const u32 mask;
  342. const char *reject_message;
  343. const struct nla_policy *nested_policy;
  344. struct netlink_range_validation *range;
  345. struct netlink_range_validation_signed *range_signed;
  346. struct {
  347. s16 min, max;
  348. };
  349. int (*validate)(const struct nlattr *attr,
  350. struct netlink_ext_ack *extack);
  351. };
  352. };
  353. #define NLA_POLICY_ETH_ADDR NLA_POLICY_EXACT_LEN(ETH_ALEN)
  354. #define NLA_POLICY_ETH_ADDR_COMPAT NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN)
  355. #define _NLA_POLICY_NESTED(maxattr, policy) \
  356. { .type = NLA_NESTED, .nested_policy = policy, .len = maxattr }
  357. #define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \
  358. { .type = NLA_NESTED_ARRAY, .nested_policy = policy, .len = maxattr }
  359. #define NLA_POLICY_NESTED(policy) \
  360. _NLA_POLICY_NESTED(ARRAY_SIZE(policy) - 1, policy)
  361. #define NLA_POLICY_NESTED_ARRAY(policy) \
  362. _NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy)
  363. #define NLA_POLICY_BITFIELD32(valid) \
  364. { .type = NLA_BITFIELD32, .bitfield32_valid = valid }
  365. #define __NLA_IS_UINT_TYPE(tp) \
  366. (tp == NLA_U8 || tp == NLA_U16 || tp == NLA_U32 || tp == NLA_U64)
  367. #define __NLA_IS_SINT_TYPE(tp) \
  368. (tp == NLA_S8 || tp == NLA_S16 || tp == NLA_S32 || tp == NLA_S64)
  369. #define __NLA_IS_BEINT_TYPE(tp) \
  370. (tp == NLA_BE16 || tp == NLA_BE32)
  371. #define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition))
  372. #define NLA_ENSURE_UINT_TYPE(tp) \
  373. (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp)) + tp)
  374. #define NLA_ENSURE_UINT_OR_BINARY_TYPE(tp) \
  375. (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) || \
  376. tp == NLA_MSECS || \
  377. tp == NLA_BINARY) + tp)
  378. #define NLA_ENSURE_SINT_TYPE(tp) \
  379. (__NLA_ENSURE(__NLA_IS_SINT_TYPE(tp)) + tp)
  380. #define NLA_ENSURE_INT_OR_BINARY_TYPE(tp) \
  381. (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) || \
  382. __NLA_IS_SINT_TYPE(tp) || \
  383. __NLA_IS_BEINT_TYPE(tp) || \
  384. tp == NLA_MSECS || \
  385. tp == NLA_BINARY) + tp)
  386. #define NLA_ENSURE_NO_VALIDATION_PTR(tp) \
  387. (__NLA_ENSURE(tp != NLA_BITFIELD32 && \
  388. tp != NLA_REJECT && \
  389. tp != NLA_NESTED && \
  390. tp != NLA_NESTED_ARRAY) + tp)
  391. #define NLA_ENSURE_BEINT_TYPE(tp) \
  392. (__NLA_ENSURE(__NLA_IS_BEINT_TYPE(tp)) + tp)
  393. #define NLA_POLICY_RANGE(tp, _min, _max) { \
  394. .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \
  395. .validation_type = NLA_VALIDATE_RANGE, \
  396. .min = _min, \
  397. .max = _max \
  398. }
  399. #define NLA_POLICY_FULL_RANGE(tp, _range) { \
  400. .type = NLA_ENSURE_UINT_OR_BINARY_TYPE(tp), \
  401. .validation_type = NLA_VALIDATE_RANGE_PTR, \
  402. .range = _range, \
  403. }
  404. #define NLA_POLICY_FULL_RANGE_SIGNED(tp, _range) { \
  405. .type = NLA_ENSURE_SINT_TYPE(tp), \
  406. .validation_type = NLA_VALIDATE_RANGE_PTR, \
  407. .range_signed = _range, \
  408. }
  409. #define NLA_POLICY_MIN(tp, _min) { \
  410. .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \
  411. .validation_type = NLA_VALIDATE_MIN, \
  412. .min = _min, \
  413. }
  414. #define NLA_POLICY_MAX(tp, _max) { \
  415. .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \
  416. .validation_type = NLA_VALIDATE_MAX, \
  417. .max = _max, \
  418. }
  419. #define NLA_POLICY_MASK(tp, _mask) { \
  420. .type = NLA_ENSURE_UINT_TYPE(tp), \
  421. .validation_type = NLA_VALIDATE_MASK, \
  422. .mask = _mask, \
  423. }
  424. #define NLA_POLICY_VALIDATE_FN(tp, fn, ...) { \
  425. .type = NLA_ENSURE_NO_VALIDATION_PTR(tp), \
  426. .validation_type = NLA_VALIDATE_FUNCTION, \
  427. .validate = fn, \
  428. .len = __VA_ARGS__ + 0, \
  429. }
  430. #define NLA_POLICY_EXACT_LEN(_len) NLA_POLICY_RANGE(NLA_BINARY, _len, _len)
  431. #define NLA_POLICY_EXACT_LEN_WARN(_len) { \
  432. .type = NLA_BINARY, \
  433. .validation_type = NLA_VALIDATE_RANGE_WARN_TOO_LONG, \
  434. .min = _len, \
  435. .max = _len \
  436. }
  437. #define NLA_POLICY_MIN_LEN(_len) NLA_POLICY_MIN(NLA_BINARY, _len)
  438. /**
  439. * struct nl_info - netlink source information
  440. * @nlh: Netlink message header of original request
  441. * @nl_net: Network namespace
  442. * @portid: Netlink PORTID of requesting application
  443. * @skip_notify: Skip netlink notifications to user space
  444. * @skip_notify_kernel: Skip selected in-kernel notifications
  445. */
  446. struct nl_info {
  447. struct nlmsghdr *nlh;
  448. struct net *nl_net;
  449. u32 portid;
  450. u8 skip_notify:1,
  451. skip_notify_kernel:1;
  452. };
  453. /**
  454. * enum netlink_validation - netlink message/attribute validation levels
  455. * @NL_VALIDATE_LIBERAL: Old-style "be liberal" validation, not caring about
  456. * extra data at the end of the message, attributes being longer than
  457. * they should be, or unknown attributes being present.
  458. * @NL_VALIDATE_TRAILING: Reject junk data encountered after attribute parsing.
  459. * @NL_VALIDATE_MAXTYPE: Reject attributes > max type; Together with _TRAILING
  460. * this is equivalent to the old nla_parse_strict()/nlmsg_parse_strict().
  461. * @NL_VALIDATE_UNSPEC: Reject attributes with NLA_UNSPEC in the policy.
  462. * This can safely be set by the kernel when the given policy has no
  463. * NLA_UNSPEC anymore, and can thus be used to ensure policy entries
  464. * are enforced going forward.
  465. * @NL_VALIDATE_STRICT_ATTRS: strict attribute policy parsing (e.g.
  466. * U8, U16, U32 must have exact size, etc.)
  467. * @NL_VALIDATE_NESTED: Check that NLA_F_NESTED is set for NLA_NESTED(_ARRAY)
  468. * and unset for other policies.
  469. */
  470. enum netlink_validation {
  471. NL_VALIDATE_LIBERAL = 0,
  472. NL_VALIDATE_TRAILING = BIT(0),
  473. NL_VALIDATE_MAXTYPE = BIT(1),
  474. NL_VALIDATE_UNSPEC = BIT(2),
  475. NL_VALIDATE_STRICT_ATTRS = BIT(3),
  476. NL_VALIDATE_NESTED = BIT(4),
  477. };
  478. #define NL_VALIDATE_DEPRECATED_STRICT (NL_VALIDATE_TRAILING |\
  479. NL_VALIDATE_MAXTYPE)
  480. #define NL_VALIDATE_STRICT (NL_VALIDATE_TRAILING |\
  481. NL_VALIDATE_MAXTYPE |\
  482. NL_VALIDATE_UNSPEC |\
  483. NL_VALIDATE_STRICT_ATTRS |\
  484. NL_VALIDATE_NESTED)
  485. int netlink_rcv_skb(struct sk_buff *skb,
  486. int (*cb)(struct sk_buff *, struct nlmsghdr *,
  487. struct netlink_ext_ack *));
  488. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  489. unsigned int group, int report, gfp_t flags);
  490. int __nla_validate(const struct nlattr *head, int len, int maxtype,
  491. const struct nla_policy *policy, unsigned int validate,
  492. struct netlink_ext_ack *extack);
  493. int __nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
  494. int len, const struct nla_policy *policy, unsigned int validate,
  495. struct netlink_ext_ack *extack);
  496. int nla_policy_len(const struct nla_policy *, int);
  497. struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
  498. ssize_t nla_strscpy(char *dst, const struct nlattr *nla, size_t dstsize);
  499. char *nla_strdup(const struct nlattr *nla, gfp_t flags);
  500. int nla_memcpy(void *dest, const struct nlattr *src, int count);
  501. int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
  502. int nla_strcmp(const struct nlattr *nla, const char *str);
  503. struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
  504. struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
  505. int attrlen, int padattr);
  506. void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
  507. struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
  508. struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype,
  509. int attrlen, int padattr);
  510. void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
  511. void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
  512. const void *data);
  513. void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
  514. const void *data, int padattr);
  515. void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
  516. int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
  517. int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
  518. const void *data, int padattr);
  519. int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
  520. int nla_append(struct sk_buff *skb, int attrlen, const void *data);
  521. /**************************************************************************
  522. * Netlink Messages
  523. **************************************************************************/
  524. /**
  525. * nlmsg_msg_size - length of netlink message not including padding
  526. * @payload: length of message payload
  527. */
  528. static inline int nlmsg_msg_size(int payload)
  529. {
  530. return NLMSG_HDRLEN + payload;
  531. }
  532. /**
  533. * nlmsg_total_size - length of netlink message including padding
  534. * @payload: length of message payload
  535. */
  536. static inline int nlmsg_total_size(int payload)
  537. {
  538. return NLMSG_ALIGN(nlmsg_msg_size(payload));
  539. }
  540. /**
  541. * nlmsg_padlen - length of padding at the message's tail
  542. * @payload: length of message payload
  543. */
  544. static inline int nlmsg_padlen(int payload)
  545. {
  546. return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
  547. }
  548. /**
  549. * nlmsg_data - head of message payload
  550. * @nlh: netlink message header
  551. */
  552. static inline void *nlmsg_data(const struct nlmsghdr *nlh)
  553. {
  554. return (unsigned char *) nlh + NLMSG_HDRLEN;
  555. }
  556. /**
  557. * nlmsg_len - length of message payload
  558. * @nlh: netlink message header
  559. */
  560. static inline int nlmsg_len(const struct nlmsghdr *nlh)
  561. {
  562. return nlh->nlmsg_len - NLMSG_HDRLEN;
  563. }
  564. /**
  565. * nlmsg_attrdata - head of attributes data
  566. * @nlh: netlink message header
  567. * @hdrlen: length of family specific header
  568. */
  569. static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
  570. int hdrlen)
  571. {
  572. unsigned char *data = nlmsg_data(nlh);
  573. return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
  574. }
  575. /**
  576. * nlmsg_attrlen - length of attributes data
  577. * @nlh: netlink message header
  578. * @hdrlen: length of family specific header
  579. */
  580. static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
  581. {
  582. return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
  583. }
  584. /**
  585. * nlmsg_ok - check if the netlink message fits into the remaining bytes
  586. * @nlh: netlink message header
  587. * @remaining: number of bytes remaining in message stream
  588. */
  589. static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
  590. {
  591. return (remaining >= (int) sizeof(struct nlmsghdr) &&
  592. nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
  593. nlh->nlmsg_len <= remaining);
  594. }
  595. /**
  596. * nlmsg_next - next netlink message in message stream
  597. * @nlh: netlink message header
  598. * @remaining: number of bytes remaining in message stream
  599. *
  600. * Returns the next netlink message in the message stream and
  601. * decrements remaining by the size of the current message.
  602. */
  603. static inline struct nlmsghdr *
  604. nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
  605. {
  606. int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
  607. *remaining -= totlen;
  608. return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
  609. }
  610. /**
  611. * nla_parse - Parse a stream of attributes into a tb buffer
  612. * @tb: destination array with maxtype+1 elements
  613. * @maxtype: maximum attribute type to be expected
  614. * @head: head of attribute stream
  615. * @len: length of attribute stream
  616. * @policy: validation policy
  617. * @extack: extended ACK pointer
  618. *
  619. * Parses a stream of attributes and stores a pointer to each attribute in
  620. * the tb array accessible via the attribute type. Attributes with a type
  621. * exceeding maxtype will be rejected, policy must be specified, attributes
  622. * will be validated in the strictest way possible.
  623. *
  624. * Returns 0 on success or a negative error code.
  625. */
  626. static inline int nla_parse(struct nlattr **tb, int maxtype,
  627. const struct nlattr *head, int len,
  628. const struct nla_policy *policy,
  629. struct netlink_ext_ack *extack)
  630. {
  631. return __nla_parse(tb, maxtype, head, len, policy,
  632. NL_VALIDATE_STRICT, extack);
  633. }
  634. /**
  635. * nla_parse_deprecated - Parse a stream of attributes into a tb buffer
  636. * @tb: destination array with maxtype+1 elements
  637. * @maxtype: maximum attribute type to be expected
  638. * @head: head of attribute stream
  639. * @len: length of attribute stream
  640. * @policy: validation policy
  641. * @extack: extended ACK pointer
  642. *
  643. * Parses a stream of attributes and stores a pointer to each attribute in
  644. * the tb array accessible via the attribute type. Attributes with a type
  645. * exceeding maxtype will be ignored and attributes from the policy are not
  646. * always strictly validated (only for new attributes).
  647. *
  648. * Returns 0 on success or a negative error code.
  649. */
  650. static inline int nla_parse_deprecated(struct nlattr **tb, int maxtype,
  651. const struct nlattr *head, int len,
  652. const struct nla_policy *policy,
  653. struct netlink_ext_ack *extack)
  654. {
  655. return __nla_parse(tb, maxtype, head, len, policy,
  656. NL_VALIDATE_LIBERAL, extack);
  657. }
  658. /**
  659. * nla_parse_deprecated_strict - Parse a stream of attributes into a tb buffer
  660. * @tb: destination array with maxtype+1 elements
  661. * @maxtype: maximum attribute type to be expected
  662. * @head: head of attribute stream
  663. * @len: length of attribute stream
  664. * @policy: validation policy
  665. * @extack: extended ACK pointer
  666. *
  667. * Parses a stream of attributes and stores a pointer to each attribute in
  668. * the tb array accessible via the attribute type. Attributes with a type
  669. * exceeding maxtype will be rejected as well as trailing data, but the
  670. * policy is not completely strictly validated (only for new attributes).
  671. *
  672. * Returns 0 on success or a negative error code.
  673. */
  674. static inline int nla_parse_deprecated_strict(struct nlattr **tb, int maxtype,
  675. const struct nlattr *head,
  676. int len,
  677. const struct nla_policy *policy,
  678. struct netlink_ext_ack *extack)
  679. {
  680. return __nla_parse(tb, maxtype, head, len, policy,
  681. NL_VALIDATE_DEPRECATED_STRICT, extack);
  682. }
  683. /**
  684. * __nlmsg_parse - parse attributes of a netlink message
  685. * @nlh: netlink message header
  686. * @hdrlen: length of family specific header
  687. * @tb: destination array with maxtype+1 elements
  688. * @maxtype: maximum attribute type to be expected
  689. * @policy: validation policy
  690. * @validate: validation strictness
  691. * @extack: extended ACK report struct
  692. *
  693. * See nla_parse()
  694. */
  695. static inline int __nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
  696. struct nlattr *tb[], int maxtype,
  697. const struct nla_policy *policy,
  698. unsigned int validate,
  699. struct netlink_ext_ack *extack)
  700. {
  701. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) {
  702. NL_SET_ERR_MSG(extack, "Invalid header length");
  703. return -EINVAL;
  704. }
  705. return __nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
  706. nlmsg_attrlen(nlh, hdrlen), policy, validate,
  707. extack);
  708. }
  709. /**
  710. * nlmsg_parse - parse attributes of a netlink message
  711. * @nlh: netlink message header
  712. * @hdrlen: length of family specific header
  713. * @tb: destination array with maxtype+1 elements
  714. * @maxtype: maximum attribute type to be expected
  715. * @policy: validation policy
  716. * @extack: extended ACK report struct
  717. *
  718. * See nla_parse()
  719. */
  720. static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
  721. struct nlattr *tb[], int maxtype,
  722. const struct nla_policy *policy,
  723. struct netlink_ext_ack *extack)
  724. {
  725. return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
  726. NL_VALIDATE_STRICT, extack);
  727. }
  728. /**
  729. * nlmsg_parse_deprecated - parse attributes of a netlink message
  730. * @nlh: netlink message header
  731. * @hdrlen: length of family specific header
  732. * @tb: destination array with maxtype+1 elements
  733. * @maxtype: maximum attribute type to be expected
  734. * @policy: validation policy
  735. * @extack: extended ACK report struct
  736. *
  737. * See nla_parse_deprecated()
  738. */
  739. static inline int nlmsg_parse_deprecated(const struct nlmsghdr *nlh, int hdrlen,
  740. struct nlattr *tb[], int maxtype,
  741. const struct nla_policy *policy,
  742. struct netlink_ext_ack *extack)
  743. {
  744. return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
  745. NL_VALIDATE_LIBERAL, extack);
  746. }
  747. /**
  748. * nlmsg_parse_deprecated_strict - parse attributes of a netlink message
  749. * @nlh: netlink message header
  750. * @hdrlen: length of family specific header
  751. * @tb: destination array with maxtype+1 elements
  752. * @maxtype: maximum attribute type to be expected
  753. * @policy: validation policy
  754. * @extack: extended ACK report struct
  755. *
  756. * See nla_parse_deprecated_strict()
  757. */
  758. static inline int
  759. nlmsg_parse_deprecated_strict(const struct nlmsghdr *nlh, int hdrlen,
  760. struct nlattr *tb[], int maxtype,
  761. const struct nla_policy *policy,
  762. struct netlink_ext_ack *extack)
  763. {
  764. return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
  765. NL_VALIDATE_DEPRECATED_STRICT, extack);
  766. }
  767. /**
  768. * nlmsg_find_attr - find a specific attribute in a netlink message
  769. * @nlh: netlink message header
  770. * @hdrlen: length of familiy specific header
  771. * @attrtype: type of attribute to look for
  772. *
  773. * Returns the first attribute which matches the specified type.
  774. */
  775. static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
  776. int hdrlen, int attrtype)
  777. {
  778. return nla_find(nlmsg_attrdata(nlh, hdrlen),
  779. nlmsg_attrlen(nlh, hdrlen), attrtype);
  780. }
  781. /**
  782. * nla_validate_deprecated - Validate a stream of attributes
  783. * @head: head of attribute stream
  784. * @len: length of attribute stream
  785. * @maxtype: maximum attribute type to be expected
  786. * @policy: validation policy
  787. * @extack: extended ACK report struct
  788. *
  789. * Validates all attributes in the specified attribute stream against the
  790. * specified policy. Validation is done in liberal mode.
  791. * See documenation of struct nla_policy for more details.
  792. *
  793. * Returns 0 on success or a negative error code.
  794. */
  795. static inline int nla_validate_deprecated(const struct nlattr *head, int len,
  796. int maxtype,
  797. const struct nla_policy *policy,
  798. struct netlink_ext_ack *extack)
  799. {
  800. return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_LIBERAL,
  801. extack);
  802. }
  803. /**
  804. * nla_validate - Validate a stream of attributes
  805. * @head: head of attribute stream
  806. * @len: length of attribute stream
  807. * @maxtype: maximum attribute type to be expected
  808. * @policy: validation policy
  809. * @extack: extended ACK report struct
  810. *
  811. * Validates all attributes in the specified attribute stream against the
  812. * specified policy. Validation is done in strict mode.
  813. * See documenation of struct nla_policy for more details.
  814. *
  815. * Returns 0 on success or a negative error code.
  816. */
  817. static inline int nla_validate(const struct nlattr *head, int len, int maxtype,
  818. const struct nla_policy *policy,
  819. struct netlink_ext_ack *extack)
  820. {
  821. return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_STRICT,
  822. extack);
  823. }
  824. /**
  825. * nlmsg_validate_deprecated - validate a netlink message including attributes
  826. * @nlh: netlinket message header
  827. * @hdrlen: length of familiy specific header
  828. * @maxtype: maximum attribute type to be expected
  829. * @policy: validation policy
  830. * @extack: extended ACK report struct
  831. */
  832. static inline int nlmsg_validate_deprecated(const struct nlmsghdr *nlh,
  833. int hdrlen, int maxtype,
  834. const struct nla_policy *policy,
  835. struct netlink_ext_ack *extack)
  836. {
  837. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  838. return -EINVAL;
  839. return __nla_validate(nlmsg_attrdata(nlh, hdrlen),
  840. nlmsg_attrlen(nlh, hdrlen), maxtype,
  841. policy, NL_VALIDATE_LIBERAL, extack);
  842. }
  843. /**
  844. * nlmsg_report - need to report back to application?
  845. * @nlh: netlink message header
  846. *
  847. * Returns 1 if a report back to the application is requested.
  848. */
  849. static inline int nlmsg_report(const struct nlmsghdr *nlh)
  850. {
  851. return nlh ? !!(nlh->nlmsg_flags & NLM_F_ECHO) : 0;
  852. }
  853. /**
  854. * nlmsg_for_each_attr - iterate over a stream of attributes
  855. * @pos: loop counter, set to current attribute
  856. * @nlh: netlink message header
  857. * @hdrlen: length of familiy specific header
  858. * @rem: initialized to len, holds bytes currently remaining in stream
  859. */
  860. #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
  861. nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
  862. nlmsg_attrlen(nlh, hdrlen), rem)
  863. /**
  864. * nlmsg_put - Add a new netlink message to an skb
  865. * @skb: socket buffer to store message in
  866. * @portid: netlink PORTID of requesting application
  867. * @seq: sequence number of message
  868. * @type: message type
  869. * @payload: length of message payload
  870. * @flags: message flags
  871. *
  872. * Returns NULL if the tailroom of the skb is insufficient to store
  873. * the message header and payload.
  874. */
  875. static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  876. int type, int payload, int flags)
  877. {
  878. if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
  879. return NULL;
  880. return __nlmsg_put(skb, portid, seq, type, payload, flags);
  881. }
  882. /**
  883. * nlmsg_append - Add more data to a nlmsg in a skb
  884. * @skb: socket buffer to store message in
  885. * @size: length of message payload
  886. *
  887. * Append data to an existing nlmsg, used when constructing a message
  888. * with multiple fixed-format headers (which is rare).
  889. * Returns NULL if the tailroom of the skb is insufficient to store
  890. * the extra payload.
  891. */
  892. static inline void *nlmsg_append(struct sk_buff *skb, u32 size)
  893. {
  894. if (unlikely(skb_tailroom(skb) < NLMSG_ALIGN(size)))
  895. return NULL;
  896. if (NLMSG_ALIGN(size) - size)
  897. memset(skb_tail_pointer(skb) + size, 0,
  898. NLMSG_ALIGN(size) - size);
  899. return __skb_put(skb, NLMSG_ALIGN(size));
  900. }
  901. /**
  902. * nlmsg_put_answer - Add a new callback based netlink message to an skb
  903. * @skb: socket buffer to store message in
  904. * @cb: netlink callback
  905. * @type: message type
  906. * @payload: length of message payload
  907. * @flags: message flags
  908. *
  909. * Returns NULL if the tailroom of the skb is insufficient to store
  910. * the message header and payload.
  911. */
  912. static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
  913. struct netlink_callback *cb,
  914. int type, int payload,
  915. int flags)
  916. {
  917. return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  918. type, payload, flags);
  919. }
  920. /**
  921. * nlmsg_new - Allocate a new netlink message
  922. * @payload: size of the message payload
  923. * @flags: the type of memory to allocate.
  924. *
  925. * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
  926. * and a good default is needed.
  927. */
  928. static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
  929. {
  930. return alloc_skb(nlmsg_total_size(payload), flags);
  931. }
  932. /**
  933. * nlmsg_end - Finalize a netlink message
  934. * @skb: socket buffer the message is stored in
  935. * @nlh: netlink message header
  936. *
  937. * Corrects the netlink message header to include the appeneded
  938. * attributes. Only necessary if attributes have been added to
  939. * the message.
  940. */
  941. static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
  942. {
  943. nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
  944. }
  945. /**
  946. * nlmsg_get_pos - return current position in netlink message
  947. * @skb: socket buffer the message is stored in
  948. *
  949. * Returns a pointer to the current tail of the message.
  950. */
  951. static inline void *nlmsg_get_pos(struct sk_buff *skb)
  952. {
  953. return skb_tail_pointer(skb);
  954. }
  955. /**
  956. * nlmsg_trim - Trim message to a mark
  957. * @skb: socket buffer the message is stored in
  958. * @mark: mark to trim to
  959. *
  960. * Trims the message to the provided mark.
  961. */
  962. static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
  963. {
  964. if (mark) {
  965. WARN_ON((unsigned char *) mark < skb->data);
  966. skb_trim(skb, (unsigned char *) mark - skb->data);
  967. }
  968. }
  969. /**
  970. * nlmsg_cancel - Cancel construction of a netlink message
  971. * @skb: socket buffer the message is stored in
  972. * @nlh: netlink message header
  973. *
  974. * Removes the complete netlink message including all
  975. * attributes from the socket buffer again.
  976. */
  977. static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
  978. {
  979. nlmsg_trim(skb, nlh);
  980. }
  981. /**
  982. * nlmsg_free - free a netlink message
  983. * @skb: socket buffer of netlink message
  984. */
  985. static inline void nlmsg_free(struct sk_buff *skb)
  986. {
  987. kfree_skb(skb);
  988. }
  989. /**
  990. * nlmsg_multicast - multicast a netlink message
  991. * @sk: netlink socket to spread messages to
  992. * @skb: netlink message as socket buffer
  993. * @portid: own netlink portid to avoid sending to yourself
  994. * @group: multicast group id
  995. * @flags: allocation flags
  996. */
  997. static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
  998. u32 portid, unsigned int group, gfp_t flags)
  999. {
  1000. int err;
  1001. NETLINK_CB(skb).dst_group = group;
  1002. err = netlink_broadcast(sk, skb, portid, group, flags);
  1003. if (err > 0)
  1004. err = 0;
  1005. return err;
  1006. }
  1007. /**
  1008. * nlmsg_unicast - unicast a netlink message
  1009. * @sk: netlink socket to spread message to
  1010. * @skb: netlink message as socket buffer
  1011. * @portid: netlink portid of the destination socket
  1012. */
  1013. static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
  1014. {
  1015. int err;
  1016. err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
  1017. if (err > 0)
  1018. err = 0;
  1019. return err;
  1020. }
  1021. /**
  1022. * nlmsg_for_each_msg - iterate over a stream of messages
  1023. * @pos: loop counter, set to current message
  1024. * @head: head of message stream
  1025. * @len: length of message stream
  1026. * @rem: initialized to len, holds bytes currently remaining in stream
  1027. */
  1028. #define nlmsg_for_each_msg(pos, head, len, rem) \
  1029. for (pos = head, rem = len; \
  1030. nlmsg_ok(pos, rem); \
  1031. pos = nlmsg_next(pos, &(rem)))
  1032. /**
  1033. * nl_dump_check_consistent - check if sequence is consistent and advertise if not
  1034. * @cb: netlink callback structure that stores the sequence number
  1035. * @nlh: netlink message header to write the flag to
  1036. *
  1037. * This function checks if the sequence (generation) number changed during dump
  1038. * and if it did, advertises it in the netlink message header.
  1039. *
  1040. * The correct way to use it is to set cb->seq to the generation counter when
  1041. * all locks for dumping have been acquired, and then call this function for
  1042. * each message that is generated.
  1043. *
  1044. * Note that due to initialisation concerns, 0 is an invalid sequence number
  1045. * and must not be used by code that uses this functionality.
  1046. */
  1047. static inline void
  1048. nl_dump_check_consistent(struct netlink_callback *cb,
  1049. struct nlmsghdr *nlh)
  1050. {
  1051. if (cb->prev_seq && cb->seq != cb->prev_seq)
  1052. nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
  1053. cb->prev_seq = cb->seq;
  1054. }
  1055. /**************************************************************************
  1056. * Netlink Attributes
  1057. **************************************************************************/
  1058. /**
  1059. * nla_attr_size - length of attribute not including padding
  1060. * @payload: length of payload
  1061. */
  1062. static inline int nla_attr_size(int payload)
  1063. {
  1064. return NLA_HDRLEN + payload;
  1065. }
  1066. /**
  1067. * nla_total_size - total length of attribute including padding
  1068. * @payload: length of payload
  1069. */
  1070. static inline int nla_total_size(int payload)
  1071. {
  1072. return NLA_ALIGN(nla_attr_size(payload));
  1073. }
  1074. /**
  1075. * nla_padlen - length of padding at the tail of attribute
  1076. * @payload: length of payload
  1077. */
  1078. static inline int nla_padlen(int payload)
  1079. {
  1080. return nla_total_size(payload) - nla_attr_size(payload);
  1081. }
  1082. /**
  1083. * nla_type - attribute type
  1084. * @nla: netlink attribute
  1085. */
  1086. static inline int nla_type(const struct nlattr *nla)
  1087. {
  1088. return nla->nla_type & NLA_TYPE_MASK;
  1089. }
  1090. /**
  1091. * nla_data - head of payload
  1092. * @nla: netlink attribute
  1093. */
  1094. static inline void *nla_data(const struct nlattr *nla)
  1095. {
  1096. return (char *) nla + NLA_HDRLEN;
  1097. }
  1098. /**
  1099. * nla_len - length of payload
  1100. * @nla: netlink attribute
  1101. */
  1102. static inline int nla_len(const struct nlattr *nla)
  1103. {
  1104. return nla->nla_len - NLA_HDRLEN;
  1105. }
  1106. /**
  1107. * nla_ok - check if the netlink attribute fits into the remaining bytes
  1108. * @nla: netlink attribute
  1109. * @remaining: number of bytes remaining in attribute stream
  1110. */
  1111. static inline int nla_ok(const struct nlattr *nla, int remaining)
  1112. {
  1113. return remaining >= (int) sizeof(*nla) &&
  1114. nla->nla_len >= sizeof(*nla) &&
  1115. nla->nla_len <= remaining;
  1116. }
  1117. /**
  1118. * nla_next - next netlink attribute in attribute stream
  1119. * @nla: netlink attribute
  1120. * @remaining: number of bytes remaining in attribute stream
  1121. *
  1122. * Returns the next netlink attribute in the attribute stream and
  1123. * decrements remaining by the size of the current attribute.
  1124. */
  1125. static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
  1126. {
  1127. unsigned int totlen = NLA_ALIGN(nla->nla_len);
  1128. *remaining -= totlen;
  1129. return (struct nlattr *) ((char *) nla + totlen);
  1130. }
  1131. /**
  1132. * nla_find_nested - find attribute in a set of nested attributes
  1133. * @nla: attribute containing the nested attributes
  1134. * @attrtype: type of attribute to look for
  1135. *
  1136. * Returns the first attribute which matches the specified type.
  1137. */
  1138. static inline struct nlattr *
  1139. nla_find_nested(const struct nlattr *nla, int attrtype)
  1140. {
  1141. return nla_find(nla_data(nla), nla_len(nla), attrtype);
  1142. }
  1143. /**
  1144. * nla_parse_nested - parse nested attributes
  1145. * @tb: destination array with maxtype+1 elements
  1146. * @maxtype: maximum attribute type to be expected
  1147. * @nla: attribute containing the nested attributes
  1148. * @policy: validation policy
  1149. * @extack: extended ACK report struct
  1150. *
  1151. * See nla_parse()
  1152. */
  1153. static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
  1154. const struct nlattr *nla,
  1155. const struct nla_policy *policy,
  1156. struct netlink_ext_ack *extack)
  1157. {
  1158. if (!(nla->nla_type & NLA_F_NESTED)) {
  1159. NL_SET_ERR_MSG_ATTR(extack, nla, "NLA_F_NESTED is missing");
  1160. return -EINVAL;
  1161. }
  1162. return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
  1163. NL_VALIDATE_STRICT, extack);
  1164. }
  1165. /**
  1166. * nla_parse_nested_deprecated - parse nested attributes
  1167. * @tb: destination array with maxtype+1 elements
  1168. * @maxtype: maximum attribute type to be expected
  1169. * @nla: attribute containing the nested attributes
  1170. * @policy: validation policy
  1171. * @extack: extended ACK report struct
  1172. *
  1173. * See nla_parse_deprecated()
  1174. */
  1175. static inline int nla_parse_nested_deprecated(struct nlattr *tb[], int maxtype,
  1176. const struct nlattr *nla,
  1177. const struct nla_policy *policy,
  1178. struct netlink_ext_ack *extack)
  1179. {
  1180. return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
  1181. NL_VALIDATE_LIBERAL, extack);
  1182. }
  1183. /**
  1184. * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
  1185. * @skb: socket buffer to add attribute to
  1186. * @attrtype: attribute type
  1187. * @value: numeric value
  1188. */
  1189. static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
  1190. {
  1191. /* temporary variables to work around GCC PR81715 with asan-stack=1 */
  1192. u8 tmp = value;
  1193. return nla_put(skb, attrtype, sizeof(u8), &tmp);
  1194. }
  1195. /**
  1196. * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
  1197. * @skb: socket buffer to add attribute to
  1198. * @attrtype: attribute type
  1199. * @value: numeric value
  1200. */
  1201. static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
  1202. {
  1203. u16 tmp = value;
  1204. return nla_put(skb, attrtype, sizeof(u16), &tmp);
  1205. }
  1206. /**
  1207. * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer
  1208. * @skb: socket buffer to add attribute to
  1209. * @attrtype: attribute type
  1210. * @value: numeric value
  1211. */
  1212. static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
  1213. {
  1214. __be16 tmp = value;
  1215. return nla_put(skb, attrtype, sizeof(__be16), &tmp);
  1216. }
  1217. /**
  1218. * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
  1219. * @skb: socket buffer to add attribute to
  1220. * @attrtype: attribute type
  1221. * @value: numeric value
  1222. */
  1223. static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
  1224. {
  1225. __be16 tmp = value;
  1226. return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
  1227. }
  1228. /**
  1229. * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
  1230. * @skb: socket buffer to add attribute to
  1231. * @attrtype: attribute type
  1232. * @value: numeric value
  1233. */
  1234. static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
  1235. {
  1236. __le16 tmp = value;
  1237. return nla_put(skb, attrtype, sizeof(__le16), &tmp);
  1238. }
  1239. /**
  1240. * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
  1241. * @skb: socket buffer to add attribute to
  1242. * @attrtype: attribute type
  1243. * @value: numeric value
  1244. */
  1245. static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
  1246. {
  1247. u32 tmp = value;
  1248. return nla_put(skb, attrtype, sizeof(u32), &tmp);
  1249. }
  1250. /**
  1251. * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
  1252. * @skb: socket buffer to add attribute to
  1253. * @attrtype: attribute type
  1254. * @value: numeric value
  1255. */
  1256. static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
  1257. {
  1258. __be32 tmp = value;
  1259. return nla_put(skb, attrtype, sizeof(__be32), &tmp);
  1260. }
  1261. /**
  1262. * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer
  1263. * @skb: socket buffer to add attribute to
  1264. * @attrtype: attribute type
  1265. * @value: numeric value
  1266. */
  1267. static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
  1268. {
  1269. __be32 tmp = value;
  1270. return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
  1271. }
  1272. /**
  1273. * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer
  1274. * @skb: socket buffer to add attribute to
  1275. * @attrtype: attribute type
  1276. * @value: numeric value
  1277. */
  1278. static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
  1279. {
  1280. __le32 tmp = value;
  1281. return nla_put(skb, attrtype, sizeof(__le32), &tmp);
  1282. }
  1283. /**
  1284. * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it
  1285. * @skb: socket buffer to add attribute to
  1286. * @attrtype: attribute type
  1287. * @value: numeric value
  1288. * @padattr: attribute type for the padding
  1289. */
  1290. static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype,
  1291. u64 value, int padattr)
  1292. {
  1293. u64 tmp = value;
  1294. return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
  1295. }
  1296. /**
  1297. * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it
  1298. * @skb: socket buffer to add attribute to
  1299. * @attrtype: attribute type
  1300. * @value: numeric value
  1301. * @padattr: attribute type for the padding
  1302. */
  1303. static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value,
  1304. int padattr)
  1305. {
  1306. __be64 tmp = value;
  1307. return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr);
  1308. }
  1309. /**
  1310. * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it
  1311. * @skb: socket buffer to add attribute to
  1312. * @attrtype: attribute type
  1313. * @value: numeric value
  1314. * @padattr: attribute type for the padding
  1315. */
  1316. static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value,
  1317. int padattr)
  1318. {
  1319. __be64 tmp = value;
  1320. return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp,
  1321. padattr);
  1322. }
  1323. /**
  1324. * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it
  1325. * @skb: socket buffer to add attribute to
  1326. * @attrtype: attribute type
  1327. * @value: numeric value
  1328. * @padattr: attribute type for the padding
  1329. */
  1330. static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value,
  1331. int padattr)
  1332. {
  1333. __le64 tmp = value;
  1334. return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr);
  1335. }
  1336. /**
  1337. * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
  1338. * @skb: socket buffer to add attribute to
  1339. * @attrtype: attribute type
  1340. * @value: numeric value
  1341. */
  1342. static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
  1343. {
  1344. s8 tmp = value;
  1345. return nla_put(skb, attrtype, sizeof(s8), &tmp);
  1346. }
  1347. /**
  1348. * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
  1349. * @skb: socket buffer to add attribute to
  1350. * @attrtype: attribute type
  1351. * @value: numeric value
  1352. */
  1353. static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
  1354. {
  1355. s16 tmp = value;
  1356. return nla_put(skb, attrtype, sizeof(s16), &tmp);
  1357. }
  1358. /**
  1359. * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
  1360. * @skb: socket buffer to add attribute to
  1361. * @attrtype: attribute type
  1362. * @value: numeric value
  1363. */
  1364. static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
  1365. {
  1366. s32 tmp = value;
  1367. return nla_put(skb, attrtype, sizeof(s32), &tmp);
  1368. }
  1369. /**
  1370. * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it
  1371. * @skb: socket buffer to add attribute to
  1372. * @attrtype: attribute type
  1373. * @value: numeric value
  1374. * @padattr: attribute type for the padding
  1375. */
  1376. static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value,
  1377. int padattr)
  1378. {
  1379. s64 tmp = value;
  1380. return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr);
  1381. }
  1382. /**
  1383. * nla_put_string - Add a string netlink attribute to a socket buffer
  1384. * @skb: socket buffer to add attribute to
  1385. * @attrtype: attribute type
  1386. * @str: NUL terminated string
  1387. */
  1388. static inline int nla_put_string(struct sk_buff *skb, int attrtype,
  1389. const char *str)
  1390. {
  1391. return nla_put(skb, attrtype, strlen(str) + 1, str);
  1392. }
  1393. /**
  1394. * nla_put_flag - Add a flag netlink attribute to a socket buffer
  1395. * @skb: socket buffer to add attribute to
  1396. * @attrtype: attribute type
  1397. */
  1398. static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
  1399. {
  1400. return nla_put(skb, attrtype, 0, NULL);
  1401. }
  1402. /**
  1403. * nla_put_msecs - Add a msecs netlink attribute to a skb and align it
  1404. * @skb: socket buffer to add attribute to
  1405. * @attrtype: attribute type
  1406. * @njiffies: number of jiffies to convert to msecs
  1407. * @padattr: attribute type for the padding
  1408. */
  1409. static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
  1410. unsigned long njiffies, int padattr)
  1411. {
  1412. u64 tmp = jiffies_to_msecs(njiffies);
  1413. return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
  1414. }
  1415. /**
  1416. * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket
  1417. * buffer
  1418. * @skb: socket buffer to add attribute to
  1419. * @attrtype: attribute type
  1420. * @addr: IPv4 address
  1421. */
  1422. static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
  1423. __be32 addr)
  1424. {
  1425. __be32 tmp = addr;
  1426. return nla_put_be32(skb, attrtype, tmp);
  1427. }
  1428. /**
  1429. * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
  1430. * buffer
  1431. * @skb: socket buffer to add attribute to
  1432. * @attrtype: attribute type
  1433. * @addr: IPv6 address
  1434. */
  1435. static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
  1436. const struct in6_addr *addr)
  1437. {
  1438. return nla_put(skb, attrtype, sizeof(*addr), addr);
  1439. }
  1440. /**
  1441. * nla_put_bitfield32 - Add a bitfield32 netlink attribute to a socket buffer
  1442. * @skb: socket buffer to add attribute to
  1443. * @attrtype: attribute type
  1444. * @value: value carrying bits
  1445. * @selector: selector of valid bits
  1446. */
  1447. static inline int nla_put_bitfield32(struct sk_buff *skb, int attrtype,
  1448. __u32 value, __u32 selector)
  1449. {
  1450. struct nla_bitfield32 tmp = { value, selector, };
  1451. return nla_put(skb, attrtype, sizeof(tmp), &tmp);
  1452. }
  1453. /**
  1454. * nla_get_u32 - return payload of u32 attribute
  1455. * @nla: u32 netlink attribute
  1456. */
  1457. static inline u32 nla_get_u32(const struct nlattr *nla)
  1458. {
  1459. return *(u32 *) nla_data(nla);
  1460. }
  1461. /**
  1462. * nla_get_be32 - return payload of __be32 attribute
  1463. * @nla: __be32 netlink attribute
  1464. */
  1465. static inline __be32 nla_get_be32(const struct nlattr *nla)
  1466. {
  1467. return *(__be32 *) nla_data(nla);
  1468. }
  1469. /**
  1470. * nla_get_le32 - return payload of __le32 attribute
  1471. * @nla: __le32 netlink attribute
  1472. */
  1473. static inline __le32 nla_get_le32(const struct nlattr *nla)
  1474. {
  1475. return *(__le32 *) nla_data(nla);
  1476. }
  1477. /**
  1478. * nla_get_u16 - return payload of u16 attribute
  1479. * @nla: u16 netlink attribute
  1480. */
  1481. static inline u16 nla_get_u16(const struct nlattr *nla)
  1482. {
  1483. return *(u16 *) nla_data(nla);
  1484. }
  1485. /**
  1486. * nla_get_be16 - return payload of __be16 attribute
  1487. * @nla: __be16 netlink attribute
  1488. */
  1489. static inline __be16 nla_get_be16(const struct nlattr *nla)
  1490. {
  1491. return *(__be16 *) nla_data(nla);
  1492. }
  1493. /**
  1494. * nla_get_le16 - return payload of __le16 attribute
  1495. * @nla: __le16 netlink attribute
  1496. */
  1497. static inline __le16 nla_get_le16(const struct nlattr *nla)
  1498. {
  1499. return *(__le16 *) nla_data(nla);
  1500. }
  1501. /**
  1502. * nla_get_u8 - return payload of u8 attribute
  1503. * @nla: u8 netlink attribute
  1504. */
  1505. static inline u8 nla_get_u8(const struct nlattr *nla)
  1506. {
  1507. return *(u8 *) nla_data(nla);
  1508. }
  1509. /**
  1510. * nla_get_u64 - return payload of u64 attribute
  1511. * @nla: u64 netlink attribute
  1512. */
  1513. static inline u64 nla_get_u64(const struct nlattr *nla)
  1514. {
  1515. u64 tmp;
  1516. nla_memcpy(&tmp, nla, sizeof(tmp));
  1517. return tmp;
  1518. }
  1519. /**
  1520. * nla_get_be64 - return payload of __be64 attribute
  1521. * @nla: __be64 netlink attribute
  1522. */
  1523. static inline __be64 nla_get_be64(const struct nlattr *nla)
  1524. {
  1525. __be64 tmp;
  1526. nla_memcpy(&tmp, nla, sizeof(tmp));
  1527. return tmp;
  1528. }
  1529. /**
  1530. * nla_get_le64 - return payload of __le64 attribute
  1531. * @nla: __le64 netlink attribute
  1532. */
  1533. static inline __le64 nla_get_le64(const struct nlattr *nla)
  1534. {
  1535. return *(__le64 *) nla_data(nla);
  1536. }
  1537. /**
  1538. * nla_get_s32 - return payload of s32 attribute
  1539. * @nla: s32 netlink attribute
  1540. */
  1541. static inline s32 nla_get_s32(const struct nlattr *nla)
  1542. {
  1543. return *(s32 *) nla_data(nla);
  1544. }
  1545. /**
  1546. * nla_get_s16 - return payload of s16 attribute
  1547. * @nla: s16 netlink attribute
  1548. */
  1549. static inline s16 nla_get_s16(const struct nlattr *nla)
  1550. {
  1551. return *(s16 *) nla_data(nla);
  1552. }
  1553. /**
  1554. * nla_get_s8 - return payload of s8 attribute
  1555. * @nla: s8 netlink attribute
  1556. */
  1557. static inline s8 nla_get_s8(const struct nlattr *nla)
  1558. {
  1559. return *(s8 *) nla_data(nla);
  1560. }
  1561. /**
  1562. * nla_get_s64 - return payload of s64 attribute
  1563. * @nla: s64 netlink attribute
  1564. */
  1565. static inline s64 nla_get_s64(const struct nlattr *nla)
  1566. {
  1567. s64 tmp;
  1568. nla_memcpy(&tmp, nla, sizeof(tmp));
  1569. return tmp;
  1570. }
  1571. /**
  1572. * nla_get_flag - return payload of flag attribute
  1573. * @nla: flag netlink attribute
  1574. */
  1575. static inline int nla_get_flag(const struct nlattr *nla)
  1576. {
  1577. return !!nla;
  1578. }
  1579. /**
  1580. * nla_get_msecs - return payload of msecs attribute
  1581. * @nla: msecs netlink attribute
  1582. *
  1583. * Returns the number of milliseconds in jiffies.
  1584. */
  1585. static inline unsigned long nla_get_msecs(const struct nlattr *nla)
  1586. {
  1587. u64 msecs = nla_get_u64(nla);
  1588. return msecs_to_jiffies((unsigned long) msecs);
  1589. }
  1590. /**
  1591. * nla_get_in_addr - return payload of IPv4 address attribute
  1592. * @nla: IPv4 address netlink attribute
  1593. */
  1594. static inline __be32 nla_get_in_addr(const struct nlattr *nla)
  1595. {
  1596. return *(__be32 *) nla_data(nla);
  1597. }
  1598. /**
  1599. * nla_get_in6_addr - return payload of IPv6 address attribute
  1600. * @nla: IPv6 address netlink attribute
  1601. */
  1602. static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla)
  1603. {
  1604. struct in6_addr tmp;
  1605. nla_memcpy(&tmp, nla, sizeof(tmp));
  1606. return tmp;
  1607. }
  1608. /**
  1609. * nla_get_bitfield32 - return payload of 32 bitfield attribute
  1610. * @nla: nla_bitfield32 attribute
  1611. */
  1612. static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla)
  1613. {
  1614. struct nla_bitfield32 tmp;
  1615. nla_memcpy(&tmp, nla, sizeof(tmp));
  1616. return tmp;
  1617. }
  1618. /**
  1619. * nla_memdup - duplicate attribute memory (kmemdup)
  1620. * @src: netlink attribute to duplicate from
  1621. * @gfp: GFP mask
  1622. */
  1623. static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp)
  1624. {
  1625. return kmemdup(nla_data(src), nla_len(src), gfp);
  1626. }
  1627. /**
  1628. * nla_nest_start_noflag - Start a new level of nested attributes
  1629. * @skb: socket buffer to add attributes to
  1630. * @attrtype: attribute type of container
  1631. *
  1632. * This function exists for backward compatibility to use in APIs which never
  1633. * marked their nest attributes with NLA_F_NESTED flag. New APIs should use
  1634. * nla_nest_start() which sets the flag.
  1635. *
  1636. * Returns the container attribute or NULL on error
  1637. */
  1638. static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb,
  1639. int attrtype)
  1640. {
  1641. struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
  1642. if (nla_put(skb, attrtype, 0, NULL) < 0)
  1643. return NULL;
  1644. return start;
  1645. }
  1646. /**
  1647. * nla_nest_start - Start a new level of nested attributes, with NLA_F_NESTED
  1648. * @skb: socket buffer to add attributes to
  1649. * @attrtype: attribute type of container
  1650. *
  1651. * Unlike nla_nest_start_noflag(), mark the nest attribute with NLA_F_NESTED
  1652. * flag. This is the preferred function to use in new code.
  1653. *
  1654. * Returns the container attribute or NULL on error
  1655. */
  1656. static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
  1657. {
  1658. return nla_nest_start_noflag(skb, attrtype | NLA_F_NESTED);
  1659. }
  1660. /**
  1661. * nla_nest_end - Finalize nesting of attributes
  1662. * @skb: socket buffer the attributes are stored in
  1663. * @start: container attribute
  1664. *
  1665. * Corrects the container attribute header to include the all
  1666. * appeneded attributes.
  1667. *
  1668. * Returns the total data length of the skb.
  1669. */
  1670. static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
  1671. {
  1672. start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
  1673. return skb->len;
  1674. }
  1675. /**
  1676. * nla_nest_cancel - Cancel nesting of attributes
  1677. * @skb: socket buffer the message is stored in
  1678. * @start: container attribute
  1679. *
  1680. * Removes the container attribute and including all nested
  1681. * attributes. Returns -EMSGSIZE
  1682. */
  1683. static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
  1684. {
  1685. nlmsg_trim(skb, start);
  1686. }
  1687. /**
  1688. * __nla_validate_nested - Validate a stream of nested attributes
  1689. * @start: container attribute
  1690. * @maxtype: maximum attribute type to be expected
  1691. * @policy: validation policy
  1692. * @validate: validation strictness
  1693. * @extack: extended ACK report struct
  1694. *
  1695. * Validates all attributes in the nested attribute stream against the
  1696. * specified policy. Attributes with a type exceeding maxtype will be
  1697. * ignored. See documenation of struct nla_policy for more details.
  1698. *
  1699. * Returns 0 on success or a negative error code.
  1700. */
  1701. static inline int __nla_validate_nested(const struct nlattr *start, int maxtype,
  1702. const struct nla_policy *policy,
  1703. unsigned int validate,
  1704. struct netlink_ext_ack *extack)
  1705. {
  1706. return __nla_validate(nla_data(start), nla_len(start), maxtype, policy,
  1707. validate, extack);
  1708. }
  1709. static inline int
  1710. nla_validate_nested(const struct nlattr *start, int maxtype,
  1711. const struct nla_policy *policy,
  1712. struct netlink_ext_ack *extack)
  1713. {
  1714. return __nla_validate_nested(start, maxtype, policy,
  1715. NL_VALIDATE_STRICT, extack);
  1716. }
  1717. static inline int
  1718. nla_validate_nested_deprecated(const struct nlattr *start, int maxtype,
  1719. const struct nla_policy *policy,
  1720. struct netlink_ext_ack *extack)
  1721. {
  1722. return __nla_validate_nested(start, maxtype, policy,
  1723. NL_VALIDATE_LIBERAL, extack);
  1724. }
  1725. /**
  1726. * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute
  1727. * @skb: socket buffer the message is stored in
  1728. *
  1729. * Return true if padding is needed to align the next attribute (nla_data()) to
  1730. * a 64-bit aligned area.
  1731. */
  1732. static inline bool nla_need_padding_for_64bit(struct sk_buff *skb)
  1733. {
  1734. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1735. /* The nlattr header is 4 bytes in size, that's why we test
  1736. * if the skb->data _is_ aligned. A NOP attribute, plus
  1737. * nlattr header for next attribute, will make nla_data()
  1738. * 8-byte aligned.
  1739. */
  1740. if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
  1741. return true;
  1742. #endif
  1743. return false;
  1744. }
  1745. /**
  1746. * nla_align_64bit - 64-bit align the nla_data() of next attribute
  1747. * @skb: socket buffer the message is stored in
  1748. * @padattr: attribute type for the padding
  1749. *
  1750. * Conditionally emit a padding netlink attribute in order to make
  1751. * the next attribute we emit have a 64-bit aligned nla_data() area.
  1752. * This will only be done in architectures which do not have
  1753. * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
  1754. *
  1755. * Returns zero on success or a negative error code.
  1756. */
  1757. static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
  1758. {
  1759. if (nla_need_padding_for_64bit(skb) &&
  1760. !nla_reserve(skb, padattr, 0))
  1761. return -EMSGSIZE;
  1762. return 0;
  1763. }
  1764. /**
  1765. * nla_total_size_64bit - total length of attribute including padding
  1766. * @payload: length of payload
  1767. */
  1768. static inline int nla_total_size_64bit(int payload)
  1769. {
  1770. return NLA_ALIGN(nla_attr_size(payload))
  1771. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1772. + NLA_ALIGN(nla_attr_size(0))
  1773. #endif
  1774. ;
  1775. }
  1776. /**
  1777. * nla_for_each_attr - iterate over a stream of attributes
  1778. * @pos: loop counter, set to current attribute
  1779. * @head: head of attribute stream
  1780. * @len: length of attribute stream
  1781. * @rem: initialized to len, holds bytes currently remaining in stream
  1782. */
  1783. #define nla_for_each_attr(pos, head, len, rem) \
  1784. for (pos = head, rem = len; \
  1785. nla_ok(pos, rem); \
  1786. pos = nla_next(pos, &(rem)))
  1787. /**
  1788. * nla_for_each_nested - iterate over nested attributes
  1789. * @pos: loop counter, set to current attribute
  1790. * @nla: attribute containing the nested attributes
  1791. * @rem: initialized to len, holds bytes currently remaining in stream
  1792. */
  1793. #define nla_for_each_nested(pos, nla, rem) \
  1794. nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
  1795. /**
  1796. * nla_is_last - Test if attribute is last in stream
  1797. * @nla: attribute to test
  1798. * @rem: bytes remaining in stream
  1799. */
  1800. static inline bool nla_is_last(const struct nlattr *nla, int rem)
  1801. {
  1802. return nla->nla_len == rem;
  1803. }
  1804. void nla_get_range_unsigned(const struct nla_policy *pt,
  1805. struct netlink_range_validation *range);
  1806. void nla_get_range_signed(const struct nla_policy *pt,
  1807. struct netlink_range_validation_signed *range);
  1808. struct netlink_policy_dump_state;
  1809. int netlink_policy_dump_add_policy(struct netlink_policy_dump_state **pstate,
  1810. const struct nla_policy *policy,
  1811. unsigned int maxtype);
  1812. int netlink_policy_dump_get_policy_idx(struct netlink_policy_dump_state *state,
  1813. const struct nla_policy *policy,
  1814. unsigned int maxtype);
  1815. bool netlink_policy_dump_loop(struct netlink_policy_dump_state *state);
  1816. int netlink_policy_dump_write(struct sk_buff *skb,
  1817. struct netlink_policy_dump_state *state);
  1818. int netlink_policy_dump_attr_size_estimate(const struct nla_policy *pt);
  1819. int netlink_policy_dump_write_attr(struct sk_buff *skb,
  1820. const struct nla_policy *pt,
  1821. int nestattr);
  1822. void netlink_policy_dump_free(struct netlink_policy_dump_state *state);
  1823. #endif