xref: /linux-6.15/include/linux/netlink.h (revision 8fdff1dc)
1 #ifndef __LINUX_NETLINK_H
2 #define __LINUX_NETLINK_H
3 
4 
5 #include <linux/capability.h>
6 #include <linux/skbuff.h>
7 #include <linux/export.h>
8 #include <net/scm.h>
9 #include <uapi/linux/netlink.h>
10 
11 struct net;
12 
13 static inline struct nlmsghdr *nlmsg_hdr(const struct sk_buff *skb)
14 {
15 	return (struct nlmsghdr *)skb->data;
16 }
17 
18 struct netlink_skb_parms {
19 	struct scm_creds	creds;		/* Skb credentials	*/
20 	__u32			portid;
21 	__u32			dst_group;
22 	struct sock		*ssk;
23 };
24 
25 #define NETLINK_CB(skb)		(*(struct netlink_skb_parms*)&((skb)->cb))
26 #define NETLINK_CREDS(skb)	(&NETLINK_CB((skb)).creds)
27 
28 
29 extern void netlink_table_grab(void);
30 extern void netlink_table_ungrab(void);
31 
32 #define NL_CFG_F_NONROOT_RECV	(1 << 0)
33 #define NL_CFG_F_NONROOT_SEND	(1 << 1)
34 
35 /* optional Netlink kernel configuration parameters */
36 struct netlink_kernel_cfg {
37 	unsigned int	groups;
38 	unsigned int	flags;
39 	void		(*input)(struct sk_buff *skb);
40 	struct mutex	*cb_mutex;
41 	void		(*bind)(int group);
42 };
43 
44 extern struct sock *__netlink_kernel_create(struct net *net, int unit,
45 					    struct module *module,
46 					    struct netlink_kernel_cfg *cfg);
47 static inline struct sock *
48 netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg)
49 {
50 	return __netlink_kernel_create(net, unit, THIS_MODULE, cfg);
51 }
52 
53 extern void netlink_kernel_release(struct sock *sk);
54 extern int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
55 extern int netlink_change_ngroups(struct sock *sk, unsigned int groups);
56 extern void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
57 extern void netlink_clear_multicast_users(struct sock *sk, unsigned int group);
58 extern void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err);
59 extern int netlink_has_listeners(struct sock *sk, unsigned int group);
60 extern int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
61 extern int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
62 			     __u32 group, gfp_t allocation);
63 extern int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb,
64 	__u32 portid, __u32 group, gfp_t allocation,
65 	int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
66 	void *filter_data);
67 extern int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
68 extern int netlink_register_notifier(struct notifier_block *nb);
69 extern int netlink_unregister_notifier(struct notifier_block *nb);
70 
71 /* finegrained unicast helpers: */
72 struct sock *netlink_getsockbyfilp(struct file *filp);
73 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
74 		      long *timeo, struct sock *ssk);
75 void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
76 int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
77 
78 /*
79  *	skb should fit one page. This choice is good for headerless malloc.
80  *	But we should limit to 8K so that userspace does not have to
81  *	use enormous buffer sizes on recvmsg() calls just to avoid
82  *	MSG_TRUNC when PAGE_SIZE is very large.
83  */
84 #if PAGE_SIZE < 8192UL
85 #define NLMSG_GOODSIZE	SKB_WITH_OVERHEAD(PAGE_SIZE)
86 #else
87 #define NLMSG_GOODSIZE	SKB_WITH_OVERHEAD(8192UL)
88 #endif
89 
90 #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
91 
92 
93 struct netlink_callback {
94 	struct sk_buff		*skb;
95 	const struct nlmsghdr	*nlh;
96 	int			(*dump)(struct sk_buff * skb,
97 					struct netlink_callback *cb);
98 	int			(*done)(struct netlink_callback *cb);
99 	void			*data;
100 	/* the module that dump function belong to */
101 	struct module		*module;
102 	u16			family;
103 	u16			min_dump_alloc;
104 	unsigned int		prev_seq, seq;
105 	long			args[6];
106 };
107 
108 struct netlink_notify {
109 	struct net *net;
110 	int portid;
111 	int protocol;
112 };
113 
114 struct nlmsghdr *
115 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
116 
117 struct netlink_dump_control {
118 	int (*dump)(struct sk_buff *skb, struct netlink_callback *);
119 	int (*done)(struct netlink_callback *);
120 	void *data;
121 	struct module *module;
122 	u16 min_dump_alloc;
123 };
124 
125 extern int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
126 				const struct nlmsghdr *nlh,
127 				struct netlink_dump_control *control);
128 static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
129 				     const struct nlmsghdr *nlh,
130 				     struct netlink_dump_control *control)
131 {
132 	if (!control->module)
133 		control->module = THIS_MODULE;
134 
135 	return __netlink_dump_start(ssk, skb, nlh, control);
136 }
137 
138 #endif	/* __LINUX_NETLINK_H */
139