genetlink.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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, not 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. * @user_ptr: user pointers
  117. * @extack: extended ACK report struct
  118. */
  119. struct genl_info {
  120. u32 snd_seq;
  121. u32 snd_portid;
  122. const struct genl_family *family;
  123. const struct nlmsghdr * nlhdr;
  124. struct genlmsghdr * genlhdr;
  125. struct nlattr ** attrs;
  126. possible_net_t _net;
  127. void * user_ptr[2];
  128. struct netlink_ext_ack *extack;
  129. };
  130. static inline struct net *genl_info_net(const struct genl_info *info)
  131. {
  132. return read_pnet(&info->_net);
  133. }
  134. static inline void genl_info_net_set(struct genl_info *info, struct net *net)
  135. {
  136. write_pnet(&info->_net, net);
  137. }
  138. static inline void *genl_info_userhdr(const struct genl_info *info)
  139. {
  140. return (u8 *)info->genlhdr + GENL_HDRLEN;
  141. }
  142. #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
  143. #define GENL_SET_ERR_MSG_FMT(info, msg, args...) \
  144. NL_SET_ERR_MSG_FMT((info)->extack, msg, ##args)
  145. /* Report that a root attribute is missing */
  146. #define GENL_REQ_ATTR_CHECK(info, attr) ({ \
  147. const struct genl_info *__info = (info); \
  148. \
  149. NL_REQ_ATTR_CHECK(__info->extack, NULL, __info->attrs, (attr)); \
  150. })
  151. enum genl_validate_flags {
  152. GENL_DONT_VALIDATE_STRICT = BIT(0),
  153. GENL_DONT_VALIDATE_DUMP = BIT(1),
  154. GENL_DONT_VALIDATE_DUMP_STRICT = BIT(2),
  155. };
  156. /**
  157. * struct genl_small_ops - generic netlink operations (small version)
  158. * @cmd: command identifier
  159. * @internal_flags: flags used by the family
  160. * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
  161. * @validate: validation flags from enum genl_validate_flags
  162. * @doit: standard command callback
  163. * @dumpit: callback for dumpers
  164. *
  165. * This is a cut-down version of struct genl_ops for users who don't need
  166. * most of the ancillary infra and want to save space.
  167. */
  168. struct genl_small_ops {
  169. int (*doit)(struct sk_buff *skb, struct genl_info *info);
  170. int (*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
  171. u8 cmd;
  172. u8 internal_flags;
  173. u8 flags;
  174. u8 validate;
  175. };
  176. /**
  177. * struct genl_ops - generic netlink operations
  178. * @cmd: command identifier
  179. * @internal_flags: flags used by the family
  180. * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
  181. * @maxattr: maximum number of attributes supported
  182. * @policy: netlink policy (takes precedence over family policy)
  183. * @validate: validation flags from enum genl_validate_flags
  184. * @doit: standard command callback
  185. * @start: start callback for dumps
  186. * @dumpit: callback for dumpers
  187. * @done: completion callback for dumps
  188. */
  189. struct genl_ops {
  190. int (*doit)(struct sk_buff *skb,
  191. struct genl_info *info);
  192. int (*start)(struct netlink_callback *cb);
  193. int (*dumpit)(struct sk_buff *skb,
  194. struct netlink_callback *cb);
  195. int (*done)(struct netlink_callback *cb);
  196. const struct nla_policy *policy;
  197. unsigned int maxattr;
  198. u8 cmd;
  199. u8 internal_flags;
  200. u8 flags;
  201. u8 validate;
  202. };
  203. /**
  204. * struct genl_split_ops - generic netlink operations (do/dump split version)
  205. * @cmd: command identifier
  206. * @internal_flags: flags used by the family
  207. * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
  208. * @validate: validation flags from enum genl_validate_flags
  209. * @policy: netlink policy (takes precedence over family policy)
  210. * @maxattr: maximum number of attributes supported
  211. *
  212. * Do callbacks:
  213. * @pre_doit: called before an operation's @doit callback, it may
  214. * do additional, common, filtering and return an error
  215. * @doit: standard command callback
  216. * @post_doit: called after an operation's @doit callback, it may
  217. * undo operations done by pre_doit, for example release locks
  218. *
  219. * Dump callbacks:
  220. * @start: start callback for dumps
  221. * @dumpit: callback for dumpers
  222. * @done: completion callback for dumps
  223. *
  224. * Do callbacks can be used if %GENL_CMD_CAP_DO is set in @flags.
  225. * Dump callbacks can be used if %GENL_CMD_CAP_DUMP is set in @flags.
  226. * Exactly one of those flags must be set.
  227. */
  228. struct genl_split_ops {
  229. union {
  230. struct {
  231. int (*pre_doit)(const struct genl_split_ops *ops,
  232. struct sk_buff *skb,
  233. struct genl_info *info);
  234. int (*doit)(struct sk_buff *skb,
  235. struct genl_info *info);
  236. void (*post_doit)(const struct genl_split_ops *ops,
  237. struct sk_buff *skb,
  238. struct genl_info *info);
  239. };
  240. struct {
  241. int (*start)(struct netlink_callback *cb);
  242. int (*dumpit)(struct sk_buff *skb,
  243. struct netlink_callback *cb);
  244. int (*done)(struct netlink_callback *cb);
  245. };
  246. };
  247. const struct nla_policy *policy;
  248. unsigned int maxattr;
  249. u8 cmd;
  250. u8 internal_flags;
  251. u8 flags;
  252. u8 validate;
  253. };
  254. /**
  255. * struct genl_dumpit_info - info that is available during dumpit op call
  256. * @op: generic netlink ops - for internal genl code usage
  257. * @attrs: netlink attributes
  258. * @info: struct genl_info describing the request
  259. */
  260. struct genl_dumpit_info {
  261. struct genl_split_ops op;
  262. struct genl_info info;
  263. };
  264. static inline const struct genl_dumpit_info *
  265. genl_dumpit_info(struct netlink_callback *cb)
  266. {
  267. return cb->data;
  268. }
  269. static inline const struct genl_info *
  270. genl_info_dump(struct netlink_callback *cb)
  271. {
  272. return &genl_dumpit_info(cb)->info;
  273. }
  274. /**
  275. * genl_info_init_ntf() - initialize genl_info for notifications
  276. * @info: genl_info struct to set up
  277. * @family: pointer to the genetlink family
  278. * @cmd: command to be used in the notification
  279. *
  280. * Initialize a locally declared struct genl_info to pass to various APIs.
  281. * Intended to be used when creating notifications.
  282. */
  283. static inline void
  284. genl_info_init_ntf(struct genl_info *info, const struct genl_family *family,
  285. u8 cmd)
  286. {
  287. struct genlmsghdr *hdr = (void *) &info->user_ptr[0];
  288. memset(info, 0, sizeof(*info));
  289. info->family = family;
  290. info->genlhdr = hdr;
  291. hdr->cmd = cmd;
  292. }
  293. static inline bool genl_info_is_ntf(const struct genl_info *info)
  294. {
  295. return !info->nlhdr;
  296. }
  297. void *__genl_sk_priv_get(struct genl_family *family, struct sock *sk);
  298. void *genl_sk_priv_get(struct genl_family *family, struct sock *sk);
  299. int genl_register_family(struct genl_family *family);
  300. int genl_unregister_family(const struct genl_family *family);
  301. void genl_notify(const struct genl_family *family, struct sk_buff *skb,
  302. struct genl_info *info, u32 group, gfp_t flags);
  303. void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  304. const struct genl_family *family, int flags, u8 cmd);
  305. static inline void *
  306. __genlmsg_iput(struct sk_buff *skb, const struct genl_info *info, int flags)
  307. {
  308. return genlmsg_put(skb, info->snd_portid, info->snd_seq, info->family,
  309. flags, info->genlhdr->cmd);
  310. }
  311. /**
  312. * genlmsg_iput - start genetlink message based on genl_info
  313. * @skb: skb in which message header will be placed
  314. * @info: genl_info as provided to do/dump handlers
  315. *
  316. * Convenience wrapper which starts a genetlink message based on
  317. * information in user request. @info should be either the struct passed
  318. * by genetlink core to do/dump handlers (when constructing replies to
  319. * such requests) or a struct initialized by genl_info_init_ntf()
  320. * when constructing notifications.
  321. *
  322. * Returns pointer to new genetlink header.
  323. */
  324. static inline void *
  325. genlmsg_iput(struct sk_buff *skb, const struct genl_info *info)
  326. {
  327. return __genlmsg_iput(skb, info, 0);
  328. }
  329. /**
  330. * genlmsg_nlhdr - Obtain netlink header from user specified header
  331. * @user_hdr: user header as returned from genlmsg_put()
  332. *
  333. * Returns pointer to netlink header.
  334. */
  335. static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
  336. {
  337. return (struct nlmsghdr *)((char *)user_hdr -
  338. GENL_HDRLEN -
  339. NLMSG_HDRLEN);
  340. }
  341. /**
  342. * genlmsg_parse_deprecated - parse attributes of a genetlink message
  343. * @nlh: netlink message header
  344. * @family: genetlink message family
  345. * @tb: destination array with maxtype+1 elements
  346. * @maxtype: maximum attribute type to be expected
  347. * @policy: validation policy
  348. * @extack: extended ACK report struct
  349. */
  350. static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
  351. const struct genl_family *family,
  352. struct nlattr *tb[], int maxtype,
  353. const struct nla_policy *policy,
  354. struct netlink_ext_ack *extack)
  355. {
  356. return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
  357. policy, NL_VALIDATE_LIBERAL, extack);
  358. }
  359. /**
  360. * genlmsg_parse - parse attributes of a genetlink message
  361. * @nlh: netlink message header
  362. * @family: genetlink message family
  363. * @tb: destination array with maxtype+1 elements
  364. * @maxtype: maximum attribute type to be expected
  365. * @policy: validation policy
  366. * @extack: extended ACK report struct
  367. */
  368. static inline int genlmsg_parse(const struct nlmsghdr *nlh,
  369. const struct genl_family *family,
  370. struct nlattr *tb[], int maxtype,
  371. const struct nla_policy *policy,
  372. struct netlink_ext_ack *extack)
  373. {
  374. return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
  375. policy, NL_VALIDATE_STRICT, extack);
  376. }
  377. /**
  378. * genl_dump_check_consistent - check if sequence is consistent and advertise if not
  379. * @cb: netlink callback structure that stores the sequence number
  380. * @user_hdr: user header as returned from genlmsg_put()
  381. *
  382. * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
  383. * simpler to use with generic netlink.
  384. */
  385. static inline void genl_dump_check_consistent(struct netlink_callback *cb,
  386. void *user_hdr)
  387. {
  388. nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
  389. }
  390. /**
  391. * genlmsg_put_reply - Add generic netlink header to a reply message
  392. * @skb: socket buffer holding the message
  393. * @info: receiver info
  394. * @family: generic netlink family
  395. * @flags: netlink message flags
  396. * @cmd: generic netlink command
  397. *
  398. * Returns pointer to user specific header
  399. */
  400. static inline void *genlmsg_put_reply(struct sk_buff *skb,
  401. struct genl_info *info,
  402. const struct genl_family *family,
  403. int flags, u8 cmd)
  404. {
  405. return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
  406. flags, cmd);
  407. }
  408. /**
  409. * genlmsg_end - Finalize a generic netlink message
  410. * @skb: socket buffer the message is stored in
  411. * @hdr: user specific header
  412. */
  413. static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
  414. {
  415. nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  416. }
  417. /**
  418. * genlmsg_cancel - Cancel construction of a generic netlink message
  419. * @skb: socket buffer the message is stored in
  420. * @hdr: generic netlink message header
  421. */
  422. static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
  423. {
  424. if (hdr)
  425. nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  426. }
  427. /**
  428. * genlmsg_multicast_netns_filtered - multicast a netlink message
  429. * to a specific netns with filter
  430. * function
  431. * @family: the generic netlink family
  432. * @net: the net namespace
  433. * @skb: netlink message as socket buffer
  434. * @portid: own netlink portid to avoid sending to yourself
  435. * @group: offset of multicast group in groups array
  436. * @flags: allocation flags
  437. * @filter: filter function
  438. * @filter_data: filter function private data
  439. *
  440. * Return: 0 on success, negative error code for failure.
  441. */
  442. static inline int
  443. genlmsg_multicast_netns_filtered(const struct genl_family *family,
  444. struct net *net, struct sk_buff *skb,
  445. u32 portid, unsigned int group, gfp_t flags,
  446. netlink_filter_fn filter,
  447. void *filter_data)
  448. {
  449. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  450. return -EINVAL;
  451. group = family->mcgrp_offset + group;
  452. return nlmsg_multicast_filtered(net->genl_sock, skb, portid, group,
  453. flags, filter, filter_data);
  454. }
  455. /**
  456. * genlmsg_multicast_netns - multicast a netlink message to a specific netns
  457. * @family: the generic netlink family
  458. * @net: the net namespace
  459. * @skb: netlink message as socket buffer
  460. * @portid: own netlink portid to avoid sending to yourself
  461. * @group: offset of multicast group in groups array
  462. * @flags: allocation flags
  463. */
  464. static inline int genlmsg_multicast_netns(const struct genl_family *family,
  465. struct net *net, struct sk_buff *skb,
  466. u32 portid, unsigned int group, gfp_t flags)
  467. {
  468. return genlmsg_multicast_netns_filtered(family, net, skb, portid,
  469. group, flags, NULL, NULL);
  470. }
  471. /**
  472. * genlmsg_multicast - multicast a netlink message to the default netns
  473. * @family: the generic netlink family
  474. * @skb: netlink message as socket buffer
  475. * @portid: own netlink portid to avoid sending to yourself
  476. * @group: offset of multicast group in groups array
  477. * @flags: allocation flags
  478. */
  479. static inline int genlmsg_multicast(const struct genl_family *family,
  480. struct sk_buff *skb, u32 portid,
  481. unsigned int group, gfp_t flags)
  482. {
  483. return genlmsg_multicast_netns(family, &init_net, skb,
  484. portid, group, flags);
  485. }
  486. /**
  487. * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
  488. * @family: the generic netlink family
  489. * @skb: netlink message as socket buffer
  490. * @portid: own netlink portid to avoid sending to yourself
  491. * @group: offset of multicast group in groups array
  492. *
  493. * This function must hold the RTNL or rcu_read_lock().
  494. */
  495. int genlmsg_multicast_allns(const struct genl_family *family,
  496. struct sk_buff *skb, u32 portid,
  497. unsigned int group);
  498. /**
  499. * genlmsg_unicast - unicast a netlink message
  500. * @net: network namespace to look up @portid in
  501. * @skb: netlink message as socket buffer
  502. * @portid: netlink portid of the destination socket
  503. */
  504. static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
  505. {
  506. return nlmsg_unicast(net->genl_sock, skb, portid);
  507. }
  508. /**
  509. * genlmsg_reply - reply to a request
  510. * @skb: netlink message to be sent back
  511. * @info: receiver information
  512. */
  513. static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
  514. {
  515. return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
  516. }
  517. /**
  518. * genlmsg_data - head of message payload
  519. * @gnlh: genetlink message header
  520. */
  521. static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
  522. {
  523. return ((unsigned char *) gnlh + GENL_HDRLEN);
  524. }
  525. /**
  526. * genlmsg_len - length of message payload
  527. * @gnlh: genetlink message header
  528. */
  529. static inline int genlmsg_len(const struct genlmsghdr *gnlh)
  530. {
  531. struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
  532. NLMSG_HDRLEN);
  533. return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
  534. }
  535. /**
  536. * genlmsg_msg_size - length of genetlink message not including padding
  537. * @payload: length of message payload
  538. */
  539. static inline int genlmsg_msg_size(int payload)
  540. {
  541. return GENL_HDRLEN + payload;
  542. }
  543. /**
  544. * genlmsg_total_size - length of genetlink message including padding
  545. * @payload: length of message payload
  546. */
  547. static inline int genlmsg_total_size(int payload)
  548. {
  549. return NLMSG_ALIGN(genlmsg_msg_size(payload));
  550. }
  551. /**
  552. * genlmsg_new - Allocate a new generic netlink message
  553. * @payload: size of the message payload
  554. * @flags: the type of memory to allocate.
  555. */
  556. static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
  557. {
  558. return nlmsg_new(genlmsg_total_size(payload), flags);
  559. }
  560. /**
  561. * genl_set_err - report error to genetlink broadcast listeners
  562. * @family: the generic netlink family
  563. * @net: the network namespace to report the error to
  564. * @portid: the PORTID of a process that we want to skip (if any)
  565. * @group: the broadcast group that will notice the error
  566. * (this is the offset of the multicast group in the groups array)
  567. * @code: error code, must be negative (as usual in kernelspace)
  568. *
  569. * This function returns the number of broadcast listeners that have set the
  570. * NETLINK_RECV_NO_ENOBUFS socket option.
  571. */
  572. static inline int genl_set_err(const struct genl_family *family,
  573. struct net *net, u32 portid,
  574. u32 group, int code)
  575. {
  576. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  577. return -EINVAL;
  578. group = family->mcgrp_offset + group;
  579. return netlink_set_err(net->genl_sock, portid, group, code);
  580. }
  581. static inline int genl_has_listeners(const struct genl_family *family,
  582. struct net *net, unsigned int group)
  583. {
  584. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  585. return -EINVAL;
  586. group = family->mcgrp_offset + group;
  587. return netlink_has_listeners(net->genl_sock, group);
  588. }
  589. #endif /* __NET_GENERIC_NETLINK_H */