xref: /linux-6.15/include/linux/netlink.h (revision a8b0ca17)
1 #ifndef __LINUX_NETLINK_H
2 #define __LINUX_NETLINK_H
3 
4 #include <linux/socket.h> /* for sa_family_t */
5 #include <linux/types.h>
6 
7 #define NETLINK_ROUTE		0	/* Routing/device hook				*/
8 #define NETLINK_UNUSED		1	/* Unused number				*/
9 #define NETLINK_USERSOCK	2	/* Reserved for user mode socket protocols 	*/
10 #define NETLINK_FIREWALL	3	/* Firewalling hook				*/
11 #define NETLINK_INET_DIAG	4	/* INET socket monitoring			*/
12 #define NETLINK_NFLOG		5	/* netfilter/iptables ULOG */
13 #define NETLINK_XFRM		6	/* ipsec */
14 #define NETLINK_SELINUX		7	/* SELinux event notifications */
15 #define NETLINK_ISCSI		8	/* Open-iSCSI */
16 #define NETLINK_AUDIT		9	/* auditing */
17 #define NETLINK_FIB_LOOKUP	10
18 #define NETLINK_CONNECTOR	11
19 #define NETLINK_NETFILTER	12	/* netfilter subsystem */
20 #define NETLINK_IP6_FW		13
21 #define NETLINK_DNRTMSG		14	/* DECnet routing messages */
22 #define NETLINK_KOBJECT_UEVENT	15	/* Kernel messages to userspace */
23 #define NETLINK_GENERIC		16
24 /* leave room for NETLINK_DM (DM Events) */
25 #define NETLINK_SCSITRANSPORT	18	/* SCSI Transports */
26 #define NETLINK_ECRYPTFS	19
27 #define NETLINK_RDMA		20
28 
29 #define MAX_LINKS 32
30 
31 struct sockaddr_nl {
32 	sa_family_t	nl_family;	/* AF_NETLINK	*/
33 	unsigned short	nl_pad;		/* zero		*/
34 	__u32		nl_pid;		/* port ID	*/
35        	__u32		nl_groups;	/* multicast groups mask */
36 };
37 
38 struct nlmsghdr {
39 	__u32		nlmsg_len;	/* Length of message including header */
40 	__u16		nlmsg_type;	/* Message content */
41 	__u16		nlmsg_flags;	/* Additional flags */
42 	__u32		nlmsg_seq;	/* Sequence number */
43 	__u32		nlmsg_pid;	/* Sending process port ID */
44 };
45 
46 /* Flags values */
47 
48 #define NLM_F_REQUEST		1	/* It is request message. 	*/
49 #define NLM_F_MULTI		2	/* Multipart message, terminated by NLMSG_DONE */
50 #define NLM_F_ACK		4	/* Reply with ack, with zero or error code */
51 #define NLM_F_ECHO		8	/* Echo this request 		*/
52 
53 /* Modifiers to GET request */
54 #define NLM_F_ROOT	0x100	/* specify tree	root	*/
55 #define NLM_F_MATCH	0x200	/* return all matching	*/
56 #define NLM_F_ATOMIC	0x400	/* atomic GET		*/
57 #define NLM_F_DUMP	(NLM_F_ROOT|NLM_F_MATCH)
58 
59 /* Modifiers to NEW request */
60 #define NLM_F_REPLACE	0x100	/* Override existing		*/
61 #define NLM_F_EXCL	0x200	/* Do not touch, if it exists	*/
62 #define NLM_F_CREATE	0x400	/* Create, if it does not exist	*/
63 #define NLM_F_APPEND	0x800	/* Add to end of list		*/
64 
65 /*
66    4.4BSD ADD		NLM_F_CREATE|NLM_F_EXCL
67    4.4BSD CHANGE	NLM_F_REPLACE
68 
69    True CHANGE		NLM_F_CREATE|NLM_F_REPLACE
70    Append		NLM_F_CREATE
71    Check		NLM_F_EXCL
72  */
73 
74 #define NLMSG_ALIGNTO	4U
75 #define NLMSG_ALIGN(len) ( ((len)+NLMSG_ALIGNTO-1) & ~(NLMSG_ALIGNTO-1) )
76 #define NLMSG_HDRLEN	 ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
77 #define NLMSG_LENGTH(len) ((len)+NLMSG_ALIGN(NLMSG_HDRLEN))
78 #define NLMSG_SPACE(len) NLMSG_ALIGN(NLMSG_LENGTH(len))
79 #define NLMSG_DATA(nlh)  ((void*)(((char*)nlh) + NLMSG_LENGTH(0)))
80 #define NLMSG_NEXT(nlh,len)	 ((len) -= NLMSG_ALIGN((nlh)->nlmsg_len), \
81 				  (struct nlmsghdr*)(((char*)(nlh)) + NLMSG_ALIGN((nlh)->nlmsg_len)))
82 #define NLMSG_OK(nlh,len) ((len) >= (int)sizeof(struct nlmsghdr) && \
83 			   (nlh)->nlmsg_len >= sizeof(struct nlmsghdr) && \
84 			   (nlh)->nlmsg_len <= (len))
85 #define NLMSG_PAYLOAD(nlh,len) ((nlh)->nlmsg_len - NLMSG_SPACE((len)))
86 
87 #define NLMSG_NOOP		0x1	/* Nothing.		*/
88 #define NLMSG_ERROR		0x2	/* Error		*/
89 #define NLMSG_DONE		0x3	/* End of a dump	*/
90 #define NLMSG_OVERRUN		0x4	/* Data lost		*/
91 
92 #define NLMSG_MIN_TYPE		0x10	/* < 0x10: reserved control messages */
93 
94 struct nlmsgerr {
95 	int		error;
96 	struct nlmsghdr msg;
97 };
98 
99 #define NETLINK_ADD_MEMBERSHIP	1
100 #define NETLINK_DROP_MEMBERSHIP	2
101 #define NETLINK_PKTINFO		3
102 #define NETLINK_BROADCAST_ERROR	4
103 #define NETLINK_NO_ENOBUFS	5
104 
105 struct nl_pktinfo {
106 	__u32	group;
107 };
108 
109 #define NET_MAJOR 36		/* Major 36 is reserved for networking 						*/
110 
111 enum {
112 	NETLINK_UNCONNECTED = 0,
113 	NETLINK_CONNECTED,
114 };
115 
116 /*
117  *  <------- NLA_HDRLEN ------> <-- NLA_ALIGN(payload)-->
118  * +---------------------+- - -+- - - - - - - - - -+- - -+
119  * |        Header       | Pad |     Payload       | Pad |
120  * |   (struct nlattr)   | ing |                   | ing |
121  * +---------------------+- - -+- - - - - - - - - -+- - -+
122  *  <-------------- nlattr->nla_len -------------->
123  */
124 
125 struct nlattr {
126 	__u16           nla_len;
127 	__u16           nla_type;
128 };
129 
130 /*
131  * nla_type (16 bits)
132  * +---+---+-------------------------------+
133  * | N | O | Attribute Type                |
134  * +---+---+-------------------------------+
135  * N := Carries nested attributes
136  * O := Payload stored in network byte order
137  *
138  * Note: The N and O flag are mutually exclusive.
139  */
140 #define NLA_F_NESTED		(1 << 15)
141 #define NLA_F_NET_BYTEORDER	(1 << 14)
142 #define NLA_TYPE_MASK		~(NLA_F_NESTED | NLA_F_NET_BYTEORDER)
143 
144 #define NLA_ALIGNTO		4
145 #define NLA_ALIGN(len)		(((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
146 #define NLA_HDRLEN		((int) NLA_ALIGN(sizeof(struct nlattr)))
147 
148 #ifdef __KERNEL__
149 
150 #include <linux/capability.h>
151 #include <linux/skbuff.h>
152 
153 struct net;
154 
155 static inline struct nlmsghdr *nlmsg_hdr(const struct sk_buff *skb)
156 {
157 	return (struct nlmsghdr *)skb->data;
158 }
159 
160 struct netlink_skb_parms {
161 	struct ucred		creds;		/* Skb credentials	*/
162 	__u32			pid;
163 	__u32			dst_group;
164 };
165 
166 #define NETLINK_CB(skb)		(*(struct netlink_skb_parms*)&((skb)->cb))
167 #define NETLINK_CREDS(skb)	(&NETLINK_CB((skb)).creds)
168 
169 
170 extern void netlink_table_grab(void);
171 extern void netlink_table_ungrab(void);
172 
173 extern struct sock *netlink_kernel_create(struct net *net,
174 					  int unit,unsigned int groups,
175 					  void (*input)(struct sk_buff *skb),
176 					  struct mutex *cb_mutex,
177 					  struct module *module);
178 extern void netlink_kernel_release(struct sock *sk);
179 extern int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
180 extern int netlink_change_ngroups(struct sock *sk, unsigned int groups);
181 extern void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
182 extern void netlink_clear_multicast_users(struct sock *sk, unsigned int group);
183 extern void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err);
184 extern int netlink_has_listeners(struct sock *sk, unsigned int group);
185 extern int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 pid, int nonblock);
186 extern int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 pid,
187 			     __u32 group, gfp_t allocation);
188 extern int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb,
189 	__u32 pid, __u32 group, gfp_t allocation,
190 	int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
191 	void *filter_data);
192 extern int netlink_set_err(struct sock *ssk, __u32 pid, __u32 group, int code);
193 extern int netlink_register_notifier(struct notifier_block *nb);
194 extern int netlink_unregister_notifier(struct notifier_block *nb);
195 
196 /* finegrained unicast helpers: */
197 struct sock *netlink_getsockbyfilp(struct file *filp);
198 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
199 		      long *timeo, struct sock *ssk);
200 void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
201 int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
202 
203 /*
204  *	skb should fit one page. This choice is good for headerless malloc.
205  *	But we should limit to 8K so that userspace does not have to
206  *	use enormous buffer sizes on recvmsg() calls just to avoid
207  *	MSG_TRUNC when PAGE_SIZE is very large.
208  */
209 #if PAGE_SIZE < 8192UL
210 #define NLMSG_GOODSIZE	SKB_WITH_OVERHEAD(PAGE_SIZE)
211 #else
212 #define NLMSG_GOODSIZE	SKB_WITH_OVERHEAD(8192UL)
213 #endif
214 
215 #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
216 
217 
218 struct netlink_callback {
219 	struct sk_buff		*skb;
220 	const struct nlmsghdr	*nlh;
221 	int			(*dump)(struct sk_buff * skb,
222 					struct netlink_callback *cb);
223 	int			(*done)(struct netlink_callback *cb);
224 	int			family;
225 	long			args[6];
226 };
227 
228 struct netlink_notify {
229 	struct net *net;
230 	int pid;
231 	int protocol;
232 };
233 
234 static __inline__ struct nlmsghdr *
235 __nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq, int type, int len, int flags)
236 {
237 	struct nlmsghdr *nlh;
238 	int size = NLMSG_LENGTH(len);
239 
240 	nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
241 	nlh->nlmsg_type = type;
242 	nlh->nlmsg_len = size;
243 	nlh->nlmsg_flags = flags;
244 	nlh->nlmsg_pid = pid;
245 	nlh->nlmsg_seq = seq;
246 	if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
247 		memset(NLMSG_DATA(nlh) + len, 0, NLMSG_ALIGN(size) - size);
248 	return nlh;
249 }
250 
251 #define NLMSG_NEW(skb, pid, seq, type, len, flags) \
252 ({	if (unlikely(skb_tailroom(skb) < (int)NLMSG_SPACE(len))) \
253 		goto nlmsg_failure; \
254 	__nlmsg_put(skb, pid, seq, type, len, flags); })
255 
256 #define NLMSG_PUT(skb, pid, seq, type, len) \
257 	NLMSG_NEW(skb, pid, seq, type, len, 0)
258 
259 extern int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
260 			      const struct nlmsghdr *nlh,
261 			      int (*dump)(struct sk_buff *skb, struct netlink_callback*),
262 			      int (*done)(struct netlink_callback*));
263 
264 
265 #define NL_NONROOT_RECV 0x1
266 #define NL_NONROOT_SEND 0x2
267 extern void netlink_set_nonroot(int protocol, unsigned flag);
268 
269 #endif /* __KERNEL__ */
270 
271 #endif	/* __LINUX_NETLINK_H */
272