xref: /openbmc/linux/net/netlink/af_netlink.h (revision f6d73b12ca9fd3b1c29a6a725cd751b972c740cf)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
20f29c768SAndrey Vagin #ifndef _AF_NETLINK_H
30f29c768SAndrey Vagin #define _AF_NETLINK_H
40f29c768SAndrey Vagin 
5e341694eSThomas Graf #include <linux/rhashtable.h>
6ee1c2442SJohannes Berg #include <linux/atomic.h>
70f29c768SAndrey Vagin #include <net/sock.h>
80f29c768SAndrey Vagin 
9457c79e5SAndrey Vagin /* flags */
108fe08d70SEric Dumazet enum {
118fe08d70SEric Dumazet 	NETLINK_F_KERNEL_SOCKET,
128fe08d70SEric Dumazet 	NETLINK_F_RECV_PKTINFO,
138fe08d70SEric Dumazet 	NETLINK_F_BROADCAST_SEND_ERROR,
148fe08d70SEric Dumazet 	NETLINK_F_RECV_NO_ENOBUFS,
158fe08d70SEric Dumazet 	NETLINK_F_LISTEN_ALL_NSID,
168fe08d70SEric Dumazet 	NETLINK_F_CAP_ACK,
178fe08d70SEric Dumazet 	NETLINK_F_EXT_ACK,
188fe08d70SEric Dumazet 	NETLINK_F_STRICT_CHK,
198fe08d70SEric Dumazet };
20457c79e5SAndrey Vagin 
210f29c768SAndrey Vagin #define NLGRPSZ(x)	(ALIGN(x, sizeof(unsigned long) * 8) / 8)
220f29c768SAndrey Vagin #define NLGRPLONGS(x)	(NLGRPSZ(x)/sizeof(unsigned long))
230f29c768SAndrey Vagin 
240f29c768SAndrey Vagin struct netlink_sock {
250f29c768SAndrey Vagin 	/* struct sock has to be the first member of netlink_sock */
260f29c768SAndrey Vagin 	struct sock		sk;
278fe08d70SEric Dumazet 	unsigned long		flags;
280f29c768SAndrey Vagin 	u32			portid;
290f29c768SAndrey Vagin 	u32			dst_portid;
300f29c768SAndrey Vagin 	u32			dst_group;
310f29c768SAndrey Vagin 	u32			subscriptions;
320f29c768SAndrey Vagin 	u32			ngroups;
330f29c768SAndrey Vagin 	unsigned long		*groups;
340f29c768SAndrey Vagin 	unsigned long		state;
359063e21fSEric Dumazet 	size_t			max_recvmsg_len;
360f29c768SAndrey Vagin 	wait_queue_head_t	wait;
37da314c99SHerbert Xu 	bool			bound;
3816b304f3SPravin B Shelar 	bool			cb_running;
390642840bSJason A. Donenfeld 	int			dump_done_errno;
4016b304f3SPravin B Shelar 	struct netlink_callback	cb;
41*c8c76f15SEric Dumazet 	struct mutex		nl_cb_mutex;
42*c8c76f15SEric Dumazet 
43*c8c76f15SEric Dumazet 	struct mutex		*dump_cb_mutex;
440f29c768SAndrey Vagin 	void			(*netlink_rcv)(struct sk_buff *skb);
45023e2cfaSJohannes Berg 	int			(*netlink_bind)(struct net *net, int group);
46023e2cfaSJohannes Berg 	void			(*netlink_unbind)(struct net *net, int group);
47a4c9a56eSAnjali Kulkarni 	void			(*netlink_release)(struct sock *sk,
48a4c9a56eSAnjali Kulkarni 						   unsigned long *groups);
490f29c768SAndrey Vagin 	struct module		*module;
50e341694eSThomas Graf 
51e341694eSThomas Graf 	struct rhash_head	node;
5221e4902aSThomas Graf 	struct rcu_head		rcu;
530f29c768SAndrey Vagin };
540f29c768SAndrey Vagin 
nlk_sk(struct sock * sk)550f29c768SAndrey Vagin static inline struct netlink_sock *nlk_sk(struct sock *sk)
560f29c768SAndrey Vagin {
570f29c768SAndrey Vagin 	return container_of(sk, struct netlink_sock, sk);
580f29c768SAndrey Vagin }
590f29c768SAndrey Vagin 
608fe08d70SEric Dumazet #define nlk_test_bit(nr, sk) test_bit(NETLINK_F_##nr, &nlk_sk(sk)->flags)
618fe08d70SEric Dumazet 
620f29c768SAndrey Vagin struct netlink_table {
63e341694eSThomas Graf 	struct rhashtable	hash;
640f29c768SAndrey Vagin 	struct hlist_head	mc_list;
650f29c768SAndrey Vagin 	struct listeners __rcu	*listeners;
660f29c768SAndrey Vagin 	unsigned int		flags;
670f29c768SAndrey Vagin 	unsigned int		groups;
680f29c768SAndrey Vagin 	struct mutex		*cb_mutex;
690f29c768SAndrey Vagin 	struct module		*module;
70023e2cfaSJohannes Berg 	int			(*bind)(struct net *net, int group);
71023e2cfaSJohannes Berg 	void			(*unbind)(struct net *net, int group);
72a4c9a56eSAnjali Kulkarni 	void                    (*release)(struct sock *sk,
73a4c9a56eSAnjali Kulkarni 					   unsigned long *groups);
740f29c768SAndrey Vagin 	int			registered;
750f29c768SAndrey Vagin };
760f29c768SAndrey Vagin 
770f29c768SAndrey Vagin extern struct netlink_table *nl_table;
780f29c768SAndrey Vagin extern rwlock_t nl_table_lock;
790f29c768SAndrey Vagin 
800f29c768SAndrey Vagin #endif
81