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_info {
11 	struct net		*net;
12 	struct sock		*sk;
13 	const struct nlmsghdr	*nlh;
14 	const struct nfgenmsg	*nfmsg;
15 	struct netlink_ext_ack	*extack;
16 };
17 
18 enum nfnl_callback_type {
19 	NFNL_CB_UNSPEC	= 0,
20 	NFNL_CB_MUTEX,
21 	NFNL_CB_RCU,
22 	NFNL_CB_BATCH,
23 };
24 
25 struct nfnl_callback {
26 	int (*call)(struct sk_buff *skb, const struct nfnl_info *info,
27 		    const struct nlattr * const cda[]);
28 	const struct nla_policy	*policy;
29 	enum nfnl_callback_type	type;
30 	__u16			attr_count;
31 };
32 
33 enum nfnl_abort_action {
34 	NFNL_ABORT_NONE		= 0,
35 	NFNL_ABORT_AUTOLOAD,
36 	NFNL_ABORT_VALIDATE,
37 };
38 
39 struct nfnetlink_subsystem {
40 	const char *name;
41 	__u8 subsys_id;			/* nfnetlink subsystem ID */
42 	__u8 cb_count;			/* number of callbacks */
43 	const struct nfnl_callback *cb;	/* callback for individual types */
44 	struct module *owner;
45 	int (*commit)(struct net *net, struct sk_buff *skb);
46 	int (*abort)(struct net *net, struct sk_buff *skb,
47 		     enum nfnl_abort_action action);
48 	void (*cleanup)(struct net *net);
49 	bool (*valid_genid)(struct net *net, u32 genid);
50 };
51 
52 int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n);
53 int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n);
54 
55 int nfnetlink_has_listeners(struct net *net, unsigned int group);
56 int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid,
57 		   unsigned int group, int echo, gfp_t flags);
58 int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error);
59 int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid);
60 void nfnetlink_broadcast(struct net *net, struct sk_buff *skb, __u32 portid,
61 			 __u32 group, gfp_t allocation);
62 
nfnl_msg_type(u8 subsys,u8 msg_type)63 static inline u16 nfnl_msg_type(u8 subsys, u8 msg_type)
64 {
65 	return subsys << 8 | msg_type;
66 }
67 
nfnl_fill_hdr(struct nlmsghdr * nlh,u8 family,u8 version,__be16 res_id)68 static inline void nfnl_fill_hdr(struct nlmsghdr *nlh, u8 family, u8 version,
69 				 __be16 res_id)
70 {
71 	struct nfgenmsg *nfmsg;
72 
73 	nfmsg = nlmsg_data(nlh);
74 	nfmsg->nfgen_family = family;
75 	nfmsg->version = version;
76 	nfmsg->res_id = res_id;
77 }
78 
nfnl_msg_put(struct sk_buff * skb,u32 portid,u32 seq,int type,int flags,u8 family,u8 version,__be16 res_id)79 static inline struct nlmsghdr *nfnl_msg_put(struct sk_buff *skb, u32 portid,
80 					    u32 seq, int type, int flags,
81 					    u8 family, u8 version,
82 					    __be16 res_id)
83 {
84 	struct nlmsghdr *nlh;
85 
86 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), flags);
87 	if (!nlh)
88 		return NULL;
89 
90 	nfnl_fill_hdr(nlh, family, version, res_id);
91 
92 	return nlh;
93 }
94 
95 void nfnl_lock(__u8 subsys_id);
96 void nfnl_unlock(__u8 subsys_id);
97 #ifdef CONFIG_PROVE_LOCKING
98 bool lockdep_nfnl_is_held(__u8 subsys_id);
99 #else
lockdep_nfnl_is_held(__u8 subsys_id)100 static inline bool lockdep_nfnl_is_held(__u8 subsys_id)
101 {
102 	return true;
103 }
104 #endif /* CONFIG_PROVE_LOCKING */
105 
106 #define MODULE_ALIAS_NFNL_SUBSYS(subsys) \
107 	MODULE_ALIAS("nfnetlink-subsys-" __stringify(subsys))
108 
109 #endif	/* _NFNETLINK_H */
110