genetlink.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_GENERIC_NETLINK_H
  3. #define __NET_GENERIC_NETLINK_H
  4. #include <linux/net.h>
  5. #include <net/netlink.h>
  6. #include <net/net_namespace.h>
  7. #include <uapi/linux/genetlink.h>
  8. #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
  9. /* Non-parallel generic netlink requests are serialized by a global lock. */
  10. void genl_lock(void);
  11. void genl_unlock(void);
  12. #define MODULE_ALIAS_GENL_FAMILY(family) \
  13. MODULE_ALIAS_NET_PF_PROTO_NAME(PF_NETLINK, NETLINK_GENERIC, "-family-" family)
  14. /* Binding to multicast group requires %CAP_NET_ADMIN */
  15. #define GENL_MCAST_CAP_NET_ADMIN BIT(0)
  16. /* Binding to multicast group requires %CAP_SYS_ADMIN */
  17. #define GENL_MCAST_CAP_SYS_ADMIN BIT(1)
  18. /**
  19. * struct genl_multicast_group - generic netlink multicast group
  20. * @name: name of the multicast group, names are per-family
  21. * @flags: GENL_MCAST_* flags
  22. */
  23. struct genl_multicast_group {
  24. char name[GENL_NAMSIZ];
  25. u8 flags;
  26. };
  27. struct genl_split_ops;
  28. struct genl_info;
  29. /**
  30. * struct genl_family - generic netlink family
  31. * @hdrsize: length of user specific header in bytes
  32. * @name: name of family
  33. * @version: protocol version
  34. * @maxattr: maximum number of attributes supported
  35. * @policy: netlink policy
  36. * @netnsok: set to true if the family can handle network
  37. * namespaces and should be presented in all of them
  38. * @parallel_ops: operations can be called in parallel and aren't
  39. * synchronized by the core genetlink code
  40. * @pre_doit: called before an operation's doit callback, it may
  41. * do additional, common, filtering and return an error
  42. * @post_doit: called after an operation's doit callback, it may
  43. * undo operations done by pre_doit, for example release locks
  44. * @bind: called when family multicast group is added to a netlink socket
  45. * @unbind: called when family multicast group is removed from a netlink socket
  46. * @module: pointer to the owning module (set to THIS_MODULE)
  47. * @mcgrps: multicast groups used by this family
  48. * @n_mcgrps: number of multicast groups
  49. * @resv_start_op: first operation for which reserved fields of the header
  50. * can be validated and policies are required (see below);
  51. * new families should leave this field at zero
  52. * @ops: the operations supported by this family
  53. * @n_ops: number of operations supported by this family
  54. * @small_ops: the small-struct operations supported by this family
  55. * @n_small_ops: number of small-struct operations supported by this family
  56. * @split_ops: the split do/dump form of operation definition
  57. * @n_split_ops: number of entries in @split_ops, note that with split do/dump
  58. * ops the number of entries is not the same as number of commands
  59. * @sock_priv_size: the size of per-socket private memory
  60. * @sock_priv_init: the per-socket private memory initializer
  61. * @sock_priv_destroy: the per-socket private memory destructor
  62. *
  63. * Attribute policies (the combination of @policy and @maxattr fields)
  64. * can be attached at the family level or at the operation level.
  65. * If both are present the per-operation policy takes precedence.
  66. * For operations before @resv_start_op lack of policy means that the core
  67. * will perform no attribute parsing or validation. For newer operations
  68. * if policy is not provided core will reject all TLV attributes.
  69. */
  70. struct genl_family {
  71. unsigned int hdrsize;
  72. char name[GENL_NAMSIZ];
  73. unsigned int version;
  74. unsigned int maxattr;
  75. u8 netnsok:1;
  76. u8 parallel_ops:1;
  77. u8 n_ops;
  78. u8 n_small_ops;
  79. u8 n_split_ops;
  80. u8 n_mcgrps;
  81. u8 resv_start_op;
  82. const struct nla_policy *policy;
  83. int (*pre_doit)(const struct genl_split_ops *ops,
  84. struct sk_buff *skb,
  85. struct genl_info *info);
  86. void (*post_doit)(const struct genl_split_ops *ops,
  87. struct sk_buff *skb,
  88. struct genl_info *info);
  89. int (*bind)(int mcgrp);
  90. void (*unbind)(int mcgrp);
  91. const struct genl_ops * ops;
  92. const struct genl_small_ops *small_ops;
  93. const struct genl_split_ops *split_ops;
  94. const struct genl_multicast_group *mcgrps;
  95. struct module *module;
  96. size_t sock_priv_size;
  97. void (*sock_priv_init)(void *priv);
  98. void (*sock_priv_destroy)(void *priv);
  99. /* private: internal use only */
  100. /* protocol family identifier */
  101. int id;
  102. /* starting number of multicast group IDs in this family */
  103. unsigned int mcgrp_offset;
  104. /* list of per-socket privs */
  105. struct xarray *sock_privs;
  106. };
  107. /**
  108. * struct genl_info - receiving information
  109. * @snd_seq: sending sequence number
  110. * @snd_portid: netlink portid of sender
  111. * @family: generic netlink family
  112. * @nlhdr: netlink message header
  113. * @genlhdr: generic netlink message header
  114. * @attrs: netlink attributes
  115. * @_net: network namespace
  116. * @ctx: storage space for the use by the family
  117. * @user_ptr: user pointers (deprecated, use ctx instead)
  118. * @extack: extended ACK report struct
  119. */
  120. struct genl_info {
  121. u32 snd_seq;
  122. u32 snd_portid;
  123. const struct genl_family *family;
  124. const struct nlmsghdr * nlhdr;
  125. struct genlmsghdr * genlhdr;
  126. struct nlattr ** attrs;
  127. possible_net_t _net;
  128. union {
  129. u8 ctx[NETLINK_CTX_SIZE];
  130. void * user_ptr[2];
  131. };
  132. struct netlink_ext_ack *extack;
  133. };
  134. static inline struct net *genl_info_net(const struct genl_info *info)
  135. {
  136. return read_pnet(&info->_net);
  137. }
  138. static inline void genl_info_net_set(struct genl_info *info, struct net *net)
  139. {
  140. write_pnet(&info->_net, net);
  141. }
  142. static inline void *genl_info_userhdr(const struct genl_info *info)
  143. {
  144. return (u8 *)info->genlhdr + GENL_HDRLEN;
  145. }
  146. #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
  147. #define GENL_SET_ERR_MSG_FMT(info, msg, args...) \
  148. NL_SET_ERR_MSG_FMT((info)->extack, msg, ##args)
  149. /* Report that a root attribute is missing */
  150. #define GENL_REQ_ATTR_CHECK(info, attr) ({ \
  151. const struct genl_info *__info = (info); \
  152. \
  153. NL_REQ_ATTR_CHECK(__info->extack, NULL, __info->attrs, (attr)); \
  154. })
  155. enum genl_validate_flags {
  156. GENL_DONT_VALIDATE_STRICT = BIT(0),
  157. GENL_DONT_VALIDATE_DUMP = BIT(1),
  158. GENL_DONT_VALIDATE_DUMP_STRICT = BIT(2),
  159. };
  160. /**
  161. * struct genl_small_ops - generic netlink operations (small version)
  162. * @cmd: command identifier
  163. * @internal_flags: flags used by the family
  164. * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
  165. * @validate: validation flags from enum genl_validate_flags
  166. * @doit: standard command callback
  167. * @dumpit: callback for dumpers
  168. *
  169. * This is a cut-down version of struct genl_ops for users who don't need
  170. * most of the ancillary infra and want to save space.
  171. */
  172. struct genl_small_ops {
  173. int (*doit)(struct sk_buff *skb, struct genl_info *info);
  174. int (*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
  175. u8 cmd;
  176. u8 internal_flags;
  177. u8 flags;
  178. u8 validate;
  179. };
  180. /**
  181. * struct genl_ops - generic netlink operations
  182. * @cmd: command identifier
  183. * @internal_flags: flags used by the family
  184. * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
  185. * @maxattr: maximum number of attributes supported
  186. * @policy: netlink policy (takes precedence over family policy)
  187. * @validate: validation flags from enum genl_validate_flags
  188. * @doit: standard command callback
  189. * @start: start callback for dumps
  190. * @dumpit: callback for dumpers
  191. * @done: completion callback for dumps
  192. */
  193. struct genl_ops {
  194. int (*doit)(struct sk_buff *skb,
  195. struct genl_info *info);
  196. int (*start)(struct netlink_callback *cb);
  197. int (*dumpit)(struct sk_buff *skb,
  198. struct netlink_callback *cb);
  199. int (*done)(struct netlink_callback *cb);
  200. const struct nla_policy *policy;
  201. unsigned int maxattr;
  202. u8 cmd;
  203. u8 internal_flags;
  204. u8 flags;
  205. u8 validate;
  206. };
  207. /**
  208. * struct genl_split_ops - generic netlink operations (do/dump split version)
  209. * @cmd: command identifier
  210. * @internal_flags: flags used by the family
  211. * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
  212. * @validate: validation flags from enum genl_validate_flags
  213. * @policy: netlink policy (takes precedence over family policy)
  214. * @maxattr: maximum number of attributes supported
  215. *
  216. * Do callbacks:
  217. * @pre_doit: called before an operation's @doit callback, it may
  218. * do additional, common, filtering and return an error
  219. * @doit: standard command callback
  220. * @post_doit: called after an operation's @doit callback, it may
  221. * undo operations done by pre_doit, for example release locks
  222. *
  223. * Dump callbacks:
  224. * @start: start callback for dumps
  225. * @dumpit: callback for dumpers
  226. * @done: completion callback for dumps
  227. *
  228. * Do callbacks can be used if %GENL_CMD_CAP_DO is set in @flags.
  229. * Dump callbacks can be used if %GENL_CMD_CAP_DUMP is set in @flags.
  230. * Exactly one of those flags must be set.
  231. */
  232. struct genl_split_ops {
  233. union {
  234. struct {
  235. int (*pre_doit)(const struct genl_split_ops *ops,
  236. struct sk_buff *skb,
  237. struct genl_info *info);
  238. int (*doit)(struct sk_buff *skb,
  239. struct genl_info *info);
  240. void (*post_doit)(const struct genl_split_ops *ops,
  241. struct sk_buff *skb,
  242. struct genl_info *info);
  243. };
  244. struct {
  245. int (*start)(struct netlink_callback *cb);
  246. int (*dumpit)(struct sk_buff *skb,
  247. struct netlink_callback *cb);
  248. int (*done)(struct netlink_callback *cb);
  249. };
  250. };
  251. const struct nla_policy *policy;
  252. unsigned int maxattr;
  253. u8 cmd;
  254. u8 internal_flags;
  255. u8 flags;
  256. u8 validate;
  257. };
  258. /**
  259. * struct genl_dumpit_info - info that is available during dumpit op call
  260. * @op: generic netlink ops - for internal genl code usage
  261. * @attrs: netlink attributes
  262. * @info: struct genl_info describing the request
  263. */
  264. struct genl_dumpit_info {
  265. struct genl_split_ops op;
  266. struct genl_info info;
  267. };
  268. static inline const struct genl_dumpit_info *
  269. genl_dumpit_info(struct netlink_callback *cb)
  270. {
  271. return cb->data;
  272. }
  273. static inline const struct genl_info *
  274. genl_info_dump(struct netlink_callback *cb)
  275. {
  276. return &genl_dumpit_info(cb)->info;
  277. }
  278. /**
  279. * genl_info_init_ntf() - initialize genl_info for notifications
  280. * @info: genl_info struct to set up
  281. * @family: pointer to the genetlink family
  282. * @cmd: command to be used in the notification
  283. *
  284. * Initialize a locally declared struct genl_info to pass to various APIs.
  285. * Intended to be used when creating notifications.
  286. */
  287. static inline void
  288. genl_info_init_ntf(struct genl_info *info, const struct genl_family *family,
  289. u8 cmd)
  290. {
  291. struct genlmsghdr *hdr = (void *) &info->user_ptr[0];
  292. memset(info, 0, sizeof(*info));
  293. info->family = family;
  294. info->genlhdr = hdr;
  295. hdr->cmd = cmd;
  296. }
  297. static inline bool genl_info_is_ntf(const struct genl_info *info)
  298. {
  299. return !info->nlhdr;
  300. }
  301. void *__genl_sk_priv_get(struct genl_family *family, struct sock *sk);
  302. void *genl_sk_priv_get(struct genl_family *family, struct sock *sk);
  303. int genl_register_family(struct genl_family *family);
  304. int genl_unregister_family(const struct genl_family *family);
  305. void genl_notify(const struct genl_family *family, struct sk_buff *skb,
  306. struct genl_info *info, u32 group, gfp_t flags);
  307. void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  308. const struct genl_family *family, int flags, u8 cmd);
  309. static inline void *
  310. __genlmsg_iput(struct sk_buff *skb, const struct genl_info *info, int flags)
  311. {
  312. return genlmsg_put(skb, info->snd_portid, info->snd_seq, info->family,
  313. flags, info->genlhdr->cmd);
  314. }
  315. /**
  316. * genlmsg_iput - start genetlink message based on genl_info
  317. * @skb: skb in which message header will be placed
  318. * @info: genl_info as provided to do/dump handlers
  319. *
  320. * Convenience wrapper which starts a genetlink message based on
  321. * information in user request. @info should be either the struct passed
  322. * by genetlink core to do/dump handlers (when constructing replies to
  323. * such requests) or a struct initialized by genl_info_init_ntf()
  324. * when constructing notifications.
  325. *
  326. * Returns: pointer to new genetlink header.
  327. */
  328. static inline void *
  329. genlmsg_iput(struct sk_buff *skb, const struct genl_info *info)
  330. {
  331. return __genlmsg_iput(skb, info, 0);
  332. }
  333. /**
  334. * genlmsg_nlhdr - Obtain netlink header from user specified header
  335. * @user_hdr: user header as returned from genlmsg_put()
  336. *
  337. * Returns: pointer to netlink header.
  338. */
  339. static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
  340. {
  341. return (struct nlmsghdr *)((char *)user_hdr -
  342. GENL_HDRLEN -
  343. NLMSG_HDRLEN);
  344. }
  345. /**
  346. * genlmsg_parse_deprecated - parse attributes of a genetlink message
  347. * @nlh: netlink message header
  348. * @family: genetlink message family
  349. * @tb: destination array with maxtype+1 elements
  350. * @maxtype: maximum attribute type to be expected
  351. * @policy: validation policy
  352. * @extack: extended ACK report struct
  353. */
  354. static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
  355. const struct genl_family *family,
  356. struct nlattr *tb[], int maxtype,
  357. const struct nla_policy *policy,
  358. struct netlink_ext_ack *extack)
  359. {
  360. return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
  361. policy, NL_VALIDATE_LIBERAL, extack);
  362. }
  363. /**
  364. * genlmsg_parse - parse attributes of a genetlink message
  365. * @nlh: netlink message header
  366. * @family: genetlink message family
  367. * @tb: destination array with maxtype+1 elements
  368. * @maxtype: maximum attribute type to be expected
  369. * @policy: validation policy
  370. * @extack: extended ACK report struct
  371. */
  372. static inline int genlmsg_parse(const struct nlmsghdr *nlh,
  373. const struct genl_family *family,
  374. struct nlattr *tb[], int maxtype,
  375. const struct nla_policy *policy,
  376. struct netlink_ext_ack *extack)
  377. {
  378. return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
  379. policy, NL_VALIDATE_STRICT, extack);
  380. }
  381. /**
  382. * genl_dump_check_consistent - check if sequence is consistent and advertise if not
  383. * @cb: netlink callback structure that stores the sequence number
  384. * @user_hdr: user header as returned from genlmsg_put()
  385. *
  386. * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
  387. * simpler to use with generic netlink.
  388. */
  389. static inline void genl_dump_check_consistent(struct netlink_callback *cb,
  390. void *user_hdr)
  391. {
  392. nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
  393. }
  394. /**
  395. * genlmsg_put_reply - Add generic netlink header to a reply message
  396. * @skb: socket buffer holding the message
  397. * @info: receiver info
  398. * @family: generic netlink family
  399. * @flags: netlink message flags
  400. * @cmd: generic netlink command
  401. *
  402. * Returns: pointer to user specific header
  403. */
  404. static inline void *genlmsg_put_reply(struct sk_buff *skb,
  405. struct genl_info *info,
  406. const struct genl_family *family,
  407. int flags, u8 cmd)
  408. {
  409. return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
  410. flags, cmd);
  411. }
  412. /**
  413. * genlmsg_end - Finalize a generic netlink message
  414. * @skb: socket buffer the message is stored in
  415. * @hdr: user specific header
  416. */
  417. static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
  418. {
  419. nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  420. }
  421. /**
  422. * genlmsg_cancel - Cancel construction of a generic netlink message
  423. * @skb: socket buffer the message is stored in
  424. * @hdr: generic netlink message header
  425. */
  426. static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
  427. {
  428. if (hdr)
  429. nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  430. }
  431. /**
  432. * genlmsg_multicast_netns_filtered - multicast a netlink message
  433. * to a specific netns with filter
  434. * function
  435. * @family: the generic netlink family
  436. * @net: the net namespace
  437. * @skb: netlink message as socket buffer
  438. * @portid: own netlink portid to avoid sending to yourself
  439. * @group: offset of multicast group in groups array
  440. * @flags: allocation flags
  441. * @filter: filter function
  442. * @filter_data: filter function private data
  443. *
  444. * Return: 0 on success, negative error code for failure.
  445. */
  446. static inline int
  447. genlmsg_multicast_netns_filtered(const struct genl_family *family,
  448. struct net *net, struct sk_buff *skb,
  449. u32 portid, unsigned int group, gfp_t flags,
  450. netlink_filter_fn filter,
  451. void *filter_data)
  452. {
  453. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  454. return -EINVAL;
  455. group = family->mcgrp_offset + group;
  456. return nlmsg_multicast_filtered(net->genl_sock, skb, portid, group,
  457. flags, filter, filter_data);
  458. }
  459. /**
  460. * genlmsg_multicast_netns - multicast a netlink message to a specific netns
  461. * @family: the generic netlink family
  462. * @net: the net namespace
  463. * @skb: netlink message as socket buffer
  464. * @portid: own netlink portid to avoid sending to yourself
  465. * @group: offset of multicast group in groups array
  466. * @flags: allocation flags
  467. */
  468. static inline int genlmsg_multicast_netns(const struct genl_family *family,
  469. struct net *net, struct sk_buff *skb,
  470. u32 portid, unsigned int group, gfp_t flags)
  471. {
  472. return genlmsg_multicast_netns_filtered(family, net, skb, portid,
  473. group, flags, NULL, NULL);
  474. }
  475. /**
  476. * genlmsg_multicast - multicast a netlink message to the default netns
  477. * @family: the generic netlink family
  478. * @skb: netlink message as socket buffer
  479. * @portid: own netlink portid to avoid sending to yourself
  480. * @group: offset of multicast group in groups array
  481. * @flags: allocation flags
  482. */
  483. static inline int genlmsg_multicast(const struct genl_family *family,
  484. struct sk_buff *skb, u32 portid,
  485. unsigned int group, gfp_t flags)
  486. {
  487. return genlmsg_multicast_netns(family, &init_net, skb,
  488. portid, group, flags);
  489. }
  490. /**
  491. * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
  492. * @family: the generic netlink family
  493. * @skb: netlink message as socket buffer
  494. * @portid: own netlink portid to avoid sending to yourself
  495. * @group: offset of multicast group in groups array
  496. *
  497. * This function must hold the RTNL or rcu_read_lock().
  498. */
  499. int genlmsg_multicast_allns(const struct genl_family *family,
  500. struct sk_buff *skb, u32 portid,
  501. unsigned int group);
  502. /**
  503. * genlmsg_unicast - unicast a netlink message
  504. * @net: network namespace to look up @portid in
  505. * @skb: netlink message as socket buffer
  506. * @portid: netlink portid of the destination socket
  507. */
  508. static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
  509. {
  510. return nlmsg_unicast(net->genl_sock, skb, portid);
  511. }
  512. /**
  513. * genlmsg_reply - reply to a request
  514. * @skb: netlink message to be sent back
  515. * @info: receiver information
  516. */
  517. static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
  518. {
  519. return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
  520. }
  521. /**
  522. * genlmsg_data - head of message payload
  523. * @gnlh: genetlink message header
  524. */
  525. static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
  526. {
  527. return ((unsigned char *) gnlh + GENL_HDRLEN);
  528. }
  529. /**
  530. * genlmsg_len - length of message payload
  531. * @gnlh: genetlink message header
  532. */
  533. static inline int genlmsg_len(const struct genlmsghdr *gnlh)
  534. {
  535. struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
  536. NLMSG_HDRLEN);
  537. return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
  538. }
  539. /**
  540. * genlmsg_msg_size - length of genetlink message not including padding
  541. * @payload: length of message payload
  542. */
  543. static inline int genlmsg_msg_size(int payload)
  544. {
  545. return GENL_HDRLEN + payload;
  546. }
  547. /**
  548. * genlmsg_total_size - length of genetlink message including padding
  549. * @payload: length of message payload
  550. */
  551. static inline int genlmsg_total_size(int payload)
  552. {
  553. return NLMSG_ALIGN(genlmsg_msg_size(payload));
  554. }
  555. /**
  556. * genlmsg_new - Allocate a new generic netlink message
  557. * @payload: size of the message payload
  558. * @flags: the type of memory to allocate.
  559. */
  560. static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
  561. {
  562. return nlmsg_new(genlmsg_total_size(payload), flags);
  563. }
  564. /**
  565. * genl_set_err - report error to genetlink broadcast listeners
  566. * @family: the generic netlink family
  567. * @net: the network namespace to report the error to
  568. * @portid: the PORTID of a process that we want to skip (if any)
  569. * @group: the broadcast group that will notice the error
  570. * (this is the offset of the multicast group in the groups array)
  571. * @code: error code, must be negative (as usual in kernelspace)
  572. *
  573. * This function returns the number of broadcast listeners that have set the
  574. * NETLINK_RECV_NO_ENOBUFS socket option.
  575. */
  576. static inline int genl_set_err(const struct genl_family *family,
  577. struct net *net, u32 portid,
  578. u32 group, int code)
  579. {
  580. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  581. return -EINVAL;
  582. group = family->mcgrp_offset + group;
  583. return netlink_set_err(net->genl_sock, portid, group, code);
  584. }
  585. static inline int genl_has_listeners(const struct genl_family *family,
  586. struct net *net, unsigned int group)
  587. {
  588. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  589. return -EINVAL;
  590. group = family->mcgrp_offset + group;
  591. return netlink_has_listeners(net->genl_sock, group);
  592. }
  593. #endif /* __NET_GENERIC_NETLINK_H */