netlink.h 65 KB

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