1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NFNETLINK_H
3 #define _NFNETLINK_H
4 
5 #include <linux/netlink.h>
6 #include <linux/capability.h>
7 #include <net/netlink.h>
8 #include <uapi/linux/netfilter/nfnetlink.h>
9 
10 struct nfnl_callback {
11 	int (*call)(struct net *net, struct sock *nl, struct sk_buff *skb,
12 		    const struct nlmsghdr *nlh,
13 		    const struct nlattr * const cda[],
14 		    struct netlink_ext_ack *extack);
15 	int (*call_rcu)(struct net *net, struct sock *nl, struct sk_buff *skb,
16 			const struct nlmsghdr *nlh,
17 			const struct nlattr * const cda[],
18 			struct netlink_ext_ack *extack);
19 	int (*call_batch)(struct net *net, struct sock *nl, struct sk_buff *skb,
20 			  const struct nlmsghdr *nlh,
21 			  const struct nlattr * const cda[],
22 			  struct netlink_ext_ack *extack);
23 	const struct nla_policy *policy;	/* netlink attribute policy */
24 	const u_int16_t attr_count;		/* number of nlattr's */
25 };
26 
27 struct nfnetlink_subsystem {
28 	const char *name;
29 	__u8 subsys_id;			/* nfnetlink subsystem ID */
30 	__u8 cb_count;			/* number of callbacks */
31 	const struct nfnl_callback *cb;	/* callback for individual types */
32 	struct module *owner;
33 	int (*commit)(struct net *net, struct sk_buff *skb);
34 	int (*abort)(struct net *net, struct sk_buff *skb);
35 	void (*cleanup)(struct net *net);
36 	bool (*valid_genid)(struct net *net, u32 genid);
37 };
38 
39 int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n);
40 int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n);
41 
42 int nfnetlink_has_listeners(struct net *net, unsigned int group);
43 int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid,
44 		   unsigned int group, int echo, gfp_t flags);
45 int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error);
46 int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid,
47 		      int flags);
48 
nfnl_msg_type(u8 subsys,u8 msg_type)49 static inline u16 nfnl_msg_type(u8 subsys, u8 msg_type)
50 {
51 	return subsys << 8 | msg_type;
52 }
53 
54 void nfnl_lock(__u8 subsys_id);
55 void nfnl_unlock(__u8 subsys_id);
56 #ifdef CONFIG_PROVE_LOCKING
57 bool lockdep_nfnl_is_held(__u8 subsys_id);
58 #else
lockdep_nfnl_is_held(__u8 subsys_id)59 static inline bool lockdep_nfnl_is_held(__u8 subsys_id)
60 {
61 	return true;
62 }
63 #endif /* CONFIG_PROVE_LOCKING */
64 
65 /*
66  * nfnl_dereference - fetch RCU pointer when updates are prevented by subsys mutex
67  *
68  * @p: The pointer to read, prior to dereferencing
69  * @ss: The nfnetlink subsystem ID
70  *
71  * Return the value of the specified RCU-protected pointer, but omit
72  * the READ_ONCE(), because caller holds the NFNL subsystem mutex.
73  */
74 #define nfnl_dereference(p, ss)					\
75 	rcu_dereference_protected(p, lockdep_nfnl_is_held(ss))
76 
77 #define MODULE_ALIAS_NFNL_SUBSYS(subsys) \
78 	MODULE_ALIAS("nfnetlink-subsys-" __stringify(subsys))
79 
80 #endif	/* _NFNETLINK_H */
81