xref: /openbmc/linux/net/netlink/af_netlink.h (revision da314c9923fed553a007785a901fd395b7eb6c19)
10f29c768SAndrey Vagin #ifndef _AF_NETLINK_H
20f29c768SAndrey Vagin #define _AF_NETLINK_H
30f29c768SAndrey Vagin 
4e341694eSThomas Graf #include <linux/rhashtable.h>
5ee1c2442SJohannes Berg #include <linux/atomic.h>
60f29c768SAndrey Vagin #include <net/sock.h>
70f29c768SAndrey Vagin 
80f29c768SAndrey Vagin #define NLGRPSZ(x)	(ALIGN(x, sizeof(unsigned long) * 8) / 8)
90f29c768SAndrey Vagin #define NLGRPLONGS(x)	(NLGRPSZ(x)/sizeof(unsigned long))
100f29c768SAndrey Vagin 
11ccdfcc39SPatrick McHardy struct netlink_ring {
12ccdfcc39SPatrick McHardy 	void			**pg_vec;
13ccdfcc39SPatrick McHardy 	unsigned int		head;
14ccdfcc39SPatrick McHardy 	unsigned int		frames_per_block;
15ccdfcc39SPatrick McHardy 	unsigned int		frame_size;
16ccdfcc39SPatrick McHardy 	unsigned int		frame_max;
17ccdfcc39SPatrick McHardy 
18ccdfcc39SPatrick McHardy 	unsigned int		pg_vec_order;
19ccdfcc39SPatrick McHardy 	unsigned int		pg_vec_pages;
20ccdfcc39SPatrick McHardy 	unsigned int		pg_vec_len;
21ccdfcc39SPatrick McHardy 
22ccdfcc39SPatrick McHardy 	atomic_t		pending;
23ccdfcc39SPatrick McHardy };
24ccdfcc39SPatrick McHardy 
250f29c768SAndrey Vagin struct netlink_sock {
260f29c768SAndrey Vagin 	/* struct sock has to be the first member of netlink_sock */
270f29c768SAndrey Vagin 	struct sock		sk;
280f29c768SAndrey Vagin 	u32			portid;
290f29c768SAndrey Vagin 	u32			dst_portid;
300f29c768SAndrey Vagin 	u32			dst_group;
310f29c768SAndrey Vagin 	u32			flags;
320f29c768SAndrey Vagin 	u32			subscriptions;
330f29c768SAndrey Vagin 	u32			ngroups;
340f29c768SAndrey Vagin 	unsigned long		*groups;
350f29c768SAndrey Vagin 	unsigned long		state;
369063e21fSEric Dumazet 	size_t			max_recvmsg_len;
370f29c768SAndrey Vagin 	wait_queue_head_t	wait;
38*da314c99SHerbert Xu 	bool			bound;
3916b304f3SPravin B Shelar 	bool			cb_running;
4016b304f3SPravin B Shelar 	struct netlink_callback	cb;
410f29c768SAndrey Vagin 	struct mutex		*cb_mutex;
420f29c768SAndrey Vagin 	struct mutex		cb_def_mutex;
430f29c768SAndrey Vagin 	void			(*netlink_rcv)(struct sk_buff *skb);
44023e2cfaSJohannes Berg 	int			(*netlink_bind)(struct net *net, int group);
45023e2cfaSJohannes Berg 	void			(*netlink_unbind)(struct net *net, int group);
460f29c768SAndrey Vagin 	struct module		*module;
47ccdfcc39SPatrick McHardy #ifdef CONFIG_NETLINK_MMAP
48ccdfcc39SPatrick McHardy 	struct mutex		pg_vec_lock;
49ccdfcc39SPatrick McHardy 	struct netlink_ring	rx_ring;
50ccdfcc39SPatrick McHardy 	struct netlink_ring	tx_ring;
51ccdfcc39SPatrick McHardy 	atomic_t		mapped;
52ccdfcc39SPatrick McHardy #endif /* CONFIG_NETLINK_MMAP */
53e341694eSThomas Graf 
54e341694eSThomas Graf 	struct rhash_head	node;
5521e4902aSThomas Graf 	struct rcu_head		rcu;
560f29c768SAndrey Vagin };
570f29c768SAndrey Vagin 
580f29c768SAndrey Vagin static inline struct netlink_sock *nlk_sk(struct sock *sk)
590f29c768SAndrey Vagin {
600f29c768SAndrey Vagin 	return container_of(sk, struct netlink_sock, sk);
610f29c768SAndrey Vagin }
620f29c768SAndrey Vagin 
631853c949SDaniel Borkmann static inline bool netlink_skb_is_mmaped(const struct sk_buff *skb)
641853c949SDaniel Borkmann {
651853c949SDaniel Borkmann #ifdef CONFIG_NETLINK_MMAP
661853c949SDaniel Borkmann 	return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED;
671853c949SDaniel Borkmann #else
681853c949SDaniel Borkmann 	return false;
691853c949SDaniel Borkmann #endif /* CONFIG_NETLINK_MMAP */
701853c949SDaniel Borkmann }
711853c949SDaniel Borkmann 
720f29c768SAndrey Vagin struct netlink_table {
73e341694eSThomas Graf 	struct rhashtable	hash;
740f29c768SAndrey Vagin 	struct hlist_head	mc_list;
750f29c768SAndrey Vagin 	struct listeners __rcu	*listeners;
760f29c768SAndrey Vagin 	unsigned int		flags;
770f29c768SAndrey Vagin 	unsigned int		groups;
780f29c768SAndrey Vagin 	struct mutex		*cb_mutex;
790f29c768SAndrey Vagin 	struct module		*module;
80023e2cfaSJohannes Berg 	int			(*bind)(struct net *net, int group);
81023e2cfaSJohannes Berg 	void			(*unbind)(struct net *net, int group);
82da12c90eSGao feng 	bool			(*compare)(struct net *net, struct sock *sock);
830f29c768SAndrey Vagin 	int			registered;
840f29c768SAndrey Vagin };
850f29c768SAndrey Vagin 
860f29c768SAndrey Vagin extern struct netlink_table *nl_table;
870f29c768SAndrey Vagin extern rwlock_t nl_table_lock;
880f29c768SAndrey Vagin 
890f29c768SAndrey Vagin #endif
90