1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef __NET_GENERIC_NETLINK_H 3#define __NET_GENERIC_NETLINK_H 4 5#include <linux/genetlink.h> 6#include <net/netlink.h> 7#include <net/net_namespace.h> 8 9#define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN) 10 11/** 12 * struct genl_multicast_group - generic netlink multicast group 13 * @name: name of the multicast group, names are per-family 14 * @cap_sys_admin: whether %CAP_SYS_ADMIN is required for binding 15 */ 16struct genl_multicast_group { 17 char name[GENL_NAMSIZ]; 18 u8 flags; 19 u8 cap_sys_admin:1; 20}; 21 22struct genl_ops; 23struct genl_info; 24 25/** 26 * struct genl_family - generic netlink family 27 * @id: protocol family identifier (private) 28 * @hdrsize: length of user specific header in bytes 29 * @name: name of family 30 * @version: protocol version 31 * @maxattr: maximum number of attributes supported 32 * @policy: netlink policy 33 * @netnsok: set to true if the family can handle network 34 * namespaces and should be presented in all of them 35 * @parallel_ops: operations can be called in parallel and aren't 36 * synchronized by the core genetlink code 37 * @pre_doit: called before an operation's doit callback, it may 38 * do additional, common, filtering and return an error 39 * @post_doit: called after an operation's doit callback, it may 40 * undo operations done by pre_doit, for example release locks 41 * @mcgrps: multicast groups used by this family 42 * @n_mcgrps: number of multicast groups 43 * @mcgrp_offset: starting number of multicast group IDs in this family 44 * (private) 45 * @ops: the operations supported by this family 46 * @n_ops: number of operations supported by this family 47 * @small_ops: the small-struct operations supported by this family 48 * @n_small_ops: number of small-struct operations supported by this family 49 */ 50struct genl_family { 51 int id; /* private */ 52 unsigned int hdrsize; 53 char name[GENL_NAMSIZ]; 54 unsigned int version; 55 unsigned int maxattr; 56 unsigned int mcgrp_offset; /* private */ 57 u8 netnsok:1; 58 u8 parallel_ops:1; 59 u8 n_ops; 60 u8 n_small_ops; 61 u8 n_mcgrps; 62 const struct nla_policy *policy; 63 int (*pre_doit)(const struct genl_ops *ops, 64 struct sk_buff *skb, 65 struct genl_info *info); 66 void (*post_doit)(const struct genl_ops *ops, 67 struct sk_buff *skb, 68 struct genl_info *info); 69 const struct genl_ops * ops; 70 const struct genl_small_ops *small_ops; 71 const struct genl_multicast_group *mcgrps; 72 struct module *module; 73}; 74 75/** 76 * struct genl_info - receiving information 77 * @snd_seq: sending sequence number 78 * @snd_portid: netlink portid of sender 79 * @nlhdr: netlink message header 80 * @genlhdr: generic netlink message header 81 * @userhdr: user specific header 82 * @attrs: netlink attributes 83 * @_net: network namespace 84 * @user_ptr: user pointers 85 * @extack: extended ACK report struct 86 */ 87struct genl_info { 88 u32 snd_seq; 89 u32 snd_portid; 90 struct nlmsghdr * nlhdr; 91 struct genlmsghdr * genlhdr; 92 void * userhdr; 93 struct nlattr ** attrs; 94 possible_net_t _net; 95 void * user_ptr[2]; 96 struct netlink_ext_ack *extack; 97}; 98 99static inline struct net *genl_info_net(struct genl_info *info) 100{ 101 return read_pnet(&info->_net); 102} 103 104static inline void genl_info_net_set(struct genl_info *info, struct net *net) 105{ 106 write_pnet(&info->_net, net); 107} 108 109#define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg) 110 111enum genl_validate_flags { 112 GENL_DONT_VALIDATE_STRICT = BIT(0), 113 GENL_DONT_VALIDATE_DUMP = BIT(1), 114 GENL_DONT_VALIDATE_DUMP_STRICT = BIT(2), 115}; 116 117/** 118 * struct genl_small_ops - generic netlink operations (small version) 119 * @cmd: command identifier 120 * @internal_flags: flags used by the family 121 * @flags: flags 122 * @validate: validation flags from enum genl_validate_flags 123 * @doit: standard command callback 124 * @dumpit: callback for dumpers 125 * 126 * This is a cut-down version of struct genl_ops for users who don't need 127 * most of the ancillary infra and want to save space. 128 */ 129struct genl_small_ops { 130 int (*doit)(struct sk_buff *skb, struct genl_info *info); 131 int (*dumpit)(struct sk_buff *skb, struct netlink_callback *cb); 132 u8 cmd; 133 u8 internal_flags; 134 u8 flags; 135 u8 validate; 136}; 137 138/** 139 * struct genl_ops - generic netlink operations 140 * @cmd: command identifier 141 * @internal_flags: flags used by the family 142 * @flags: flags 143 * @maxattr: maximum number of attributes supported 144 * @policy: netlink policy (takes precedence over family policy) 145 * @validate: validation flags from enum genl_validate_flags 146 * @doit: standard command callback 147 * @start: start callback for dumps 148 * @dumpit: callback for dumpers 149 * @done: completion callback for dumps 150 */ 151struct genl_ops { 152 int (*doit)(struct sk_buff *skb, 153 struct genl_info *info); 154 int (*start)(struct netlink_callback *cb); 155 int (*dumpit)(struct sk_buff *skb, 156 struct netlink_callback *cb); 157 int (*done)(struct netlink_callback *cb); 158 const struct nla_policy *policy; 159 unsigned int maxattr; 160 u8 cmd; 161 u8 internal_flags; 162 u8 flags; 163 u8 validate; 164}; 165 166/** 167 * struct genl_info - info that is available during dumpit op call 168 * @family: generic netlink family - for internal genl code usage 169 * @ops: generic netlink ops - for internal genl code usage 170 * @attrs: netlink attributes 171 */ 172struct genl_dumpit_info { 173 const struct genl_family *family; 174 struct genl_ops op; 175 struct nlattr **attrs; 176}; 177 178static inline const struct genl_dumpit_info * 179genl_dumpit_info(struct netlink_callback *cb) 180{ 181 return cb->data; 182} 183 184int genl_register_family(struct genl_family *family); 185int genl_unregister_family(const struct genl_family *family); 186void genl_notify(const struct genl_family *family, struct sk_buff *skb, 187 struct genl_info *info, u32 group, gfp_t flags); 188 189void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, 190 const struct genl_family *family, int flags, u8 cmd); 191 192/** 193 * genlmsg_nlhdr - Obtain netlink header from user specified header 194 * @user_hdr: user header as returned from genlmsg_put() 195 * 196 * Returns pointer to netlink header. 197 */ 198static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr) 199{ 200 return (struct nlmsghdr *)((char *)user_hdr - 201 GENL_HDRLEN - 202 NLMSG_HDRLEN); 203} 204 205/** 206 * genlmsg_parse_deprecated - parse attributes of a genetlink message 207 * @nlh: netlink message header 208 * @family: genetlink message family 209 * @tb: destination array with maxtype+1 elements 210 * @maxtype: maximum attribute type to be expected 211 * @policy: validation policy 212 * @extack: extended ACK report struct 213 */ 214static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh, 215 const struct genl_family *family, 216 struct nlattr *tb[], int maxtype, 217 const struct nla_policy *policy, 218 struct netlink_ext_ack *extack) 219{ 220 return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype, 221 policy, NL_VALIDATE_LIBERAL, extack); 222} 223 224/** 225 * genlmsg_parse - parse attributes of a genetlink message 226 * @nlh: netlink message header 227 * @family: genetlink message family 228 * @tb: destination array with maxtype+1 elements 229 * @maxtype: maximum attribute type to be expected 230 * @policy: validation policy 231 * @extack: extended ACK report struct 232 */ 233static inline int genlmsg_parse(const struct nlmsghdr *nlh, 234 const struct genl_family *family, 235 struct nlattr *tb[], int maxtype, 236 const struct nla_policy *policy, 237 struct netlink_ext_ack *extack) 238{ 239 return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype, 240 policy, NL_VALIDATE_STRICT, extack); 241} 242 243/** 244 * genl_dump_check_consistent - check if sequence is consistent and advertise if not 245 * @cb: netlink callback structure that stores the sequence number 246 * @user_hdr: user header as returned from genlmsg_put() 247 * 248 * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it 249 * simpler to use with generic netlink. 250 */ 251static inline void genl_dump_check_consistent(struct netlink_callback *cb, 252 void *user_hdr) 253{ 254 nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr)); 255} 256 257/** 258 * genlmsg_put_reply - Add generic netlink header to a reply message 259 * @skb: socket buffer holding the message 260 * @info: receiver info 261 * @family: generic netlink family 262 * @flags: netlink message flags 263 * @cmd: generic netlink command 264 * 265 * Returns pointer to user specific header 266 */ 267static inline void *genlmsg_put_reply(struct sk_buff *skb, 268 struct genl_info *info, 269 const struct genl_family *family, 270 int flags, u8 cmd) 271{ 272 return genlmsg_put(skb, info->snd_portid, info->snd_seq, family, 273 flags, cmd); 274} 275 276/** 277 * genlmsg_end - Finalize a generic netlink message 278 * @skb: socket buffer the message is stored in 279 * @hdr: user specific header 280 */ 281static inline void genlmsg_end(struct sk_buff *skb, void *hdr) 282{ 283 nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 284} 285 286/** 287 * genlmsg_cancel - Cancel construction of a generic netlink message 288 * @skb: socket buffer the message is stored in 289 * @hdr: generic netlink message header 290 */ 291static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr) 292{ 293 if (hdr) 294 nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); 295} 296 297/** 298 * genlmsg_multicast_netns - multicast a netlink message to a specific netns 299 * @family: the generic netlink family 300 * @net: the net namespace 301 * @skb: netlink message as socket buffer 302 * @portid: own netlink portid to avoid sending to yourself 303 * @group: offset of multicast group in groups array 304 * @flags: allocation flags 305 */ 306static inline int genlmsg_multicast_netns(const struct genl_family *family, 307 struct net *net, struct sk_buff *skb, 308 u32 portid, unsigned int group, gfp_t flags) 309{ 310 if (WARN_ON_ONCE(group >= family->n_mcgrps)) 311 return -EINVAL; 312 group = family->mcgrp_offset + group; 313 return nlmsg_multicast(net->genl_sock, skb, portid, group, flags); 314} 315 316/** 317 * genlmsg_multicast - multicast a netlink message to the default netns 318 * @family: the generic netlink family 319 * @skb: netlink message as socket buffer 320 * @portid: own netlink portid to avoid sending to yourself 321 * @group: offset of multicast group in groups array 322 * @flags: allocation flags 323 */ 324static inline int genlmsg_multicast(const struct genl_family *family, 325 struct sk_buff *skb, u32 portid, 326 unsigned int group, gfp_t flags) 327{ 328 return genlmsg_multicast_netns(family, &init_net, skb, 329 portid, group, flags); 330} 331 332/** 333 * genlmsg_multicast_allns - multicast a netlink message to all net namespaces 334 * @family: the generic netlink family 335 * @skb: netlink message as socket buffer 336 * @portid: own netlink portid to avoid sending to yourself 337 * @group: offset of multicast group in groups array 338 * @flags: allocation flags 339 * 340 * This function must hold the RTNL or rcu_read_lock(). 341 */ 342int genlmsg_multicast_allns(const struct genl_family *family, 343 struct sk_buff *skb, u32 portid, 344 unsigned int group, gfp_t flags); 345 346/** 347 * genlmsg_unicast - unicast a netlink message 348 * @skb: netlink message as socket buffer 349 * @portid: netlink portid of the destination socket 350 */ 351static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid) 352{ 353 return nlmsg_unicast(net->genl_sock, skb, portid); 354} 355 356/** 357 * genlmsg_reply - reply to a request 358 * @skb: netlink message to be sent back 359 * @info: receiver information 360 */ 361static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info) 362{ 363 return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid); 364} 365 366/** 367 * gennlmsg_data - head of message payload 368 * @gnlh: genetlink message header 369 */ 370static inline void *genlmsg_data(const struct genlmsghdr *gnlh) 371{ 372 return ((unsigned char *) gnlh + GENL_HDRLEN); 373} 374 375/** 376 * genlmsg_len - length of message payload 377 * @gnlh: genetlink message header 378 */ 379static inline int genlmsg_len(const struct genlmsghdr *gnlh) 380{ 381 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh - 382 NLMSG_HDRLEN); 383 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN); 384} 385 386/** 387 * genlmsg_msg_size - length of genetlink message not including padding 388 * @payload: length of message payload 389 */ 390static inline int genlmsg_msg_size(int payload) 391{ 392 return GENL_HDRLEN + payload; 393} 394 395/** 396 * genlmsg_total_size - length of genetlink message including padding 397 * @payload: length of message payload 398 */ 399static inline int genlmsg_total_size(int payload) 400{ 401 return NLMSG_ALIGN(genlmsg_msg_size(payload)); 402} 403 404/** 405 * genlmsg_new - Allocate a new generic netlink message 406 * @payload: size of the message payload 407 * @flags: the type of memory to allocate. 408 */ 409static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags) 410{ 411 return nlmsg_new(genlmsg_total_size(payload), flags); 412} 413 414/** 415 * genl_set_err - report error to genetlink broadcast listeners 416 * @family: the generic netlink family 417 * @net: the network namespace to report the error to 418 * @portid: the PORTID of a process that we want to skip (if any) 419 * @group: the broadcast group that will notice the error 420 * (this is the offset of the multicast group in the groups array) 421 * @code: error code, must be negative (as usual in kernelspace) 422 * 423 * This function returns the number of broadcast listeners that have set the 424 * NETLINK_RECV_NO_ENOBUFS socket option. 425 */ 426static inline int genl_set_err(const struct genl_family *family, 427 struct net *net, u32 portid, 428 u32 group, int code) 429{ 430 if (WARN_ON_ONCE(group >= family->n_mcgrps)) 431 return -EINVAL; 432 group = family->mcgrp_offset + group; 433 return netlink_set_err(net->genl_sock, portid, group, code); 434} 435 436static inline int genl_has_listeners(const struct genl_family *family, 437 struct net *net, unsigned int group) 438{ 439 if (WARN_ON_ONCE(group >= family->n_mcgrps)) 440 return -EINVAL; 441 group = family->mcgrp_offset + group; 442 return netlink_has_listeners(net->genl_sock, group); 443} 444#endif /* __NET_GENERIC_NETLINK_H */ 445