netlink.h 74 KB

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