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