• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 enum nfnl_abort_action {
28 	NFNL_ABORT_NONE		= 0,
29 	NFNL_ABORT_AUTOLOAD,
30 	NFNL_ABORT_VALIDATE,
31 };
32 
33 struct nfnetlink_subsystem {
34 	const char *name;
35 	__u8 subsys_id;			/* nfnetlink subsystem ID */
36 	__u8 cb_count;			/* number of callbacks */
37 	const struct nfnl_callback *cb;	/* callback for individual types */
38 	struct module *owner;
39 	int (*commit)(struct net *net, struct sk_buff *skb);
40 	int (*abort)(struct net *net, struct sk_buff *skb,
41 		     enum nfnl_abort_action action);
42 	bool (*valid_genid)(struct net *net, u32 genid);
43 };
44 
45 int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n);
46 int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n);
47 
48 int nfnetlink_has_listeners(struct net *net, unsigned int group);
49 int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid,
50 		   unsigned int group, int echo, gfp_t flags);
51 int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error);
52 int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid);
53 
nfnl_msg_type(u8 subsys,u8 msg_type)54 static inline u16 nfnl_msg_type(u8 subsys, u8 msg_type)
55 {
56 	return subsys << 8 | msg_type;
57 }
58 
nfnl_fill_hdr(struct nlmsghdr * nlh,u8 family,u8 version,__be16 res_id)59 static inline void nfnl_fill_hdr(struct nlmsghdr *nlh, u8 family, u8 version,
60 				 __be16 res_id)
61 {
62 	struct nfgenmsg *nfmsg;
63 
64 	nfmsg = nlmsg_data(nlh);
65 	nfmsg->nfgen_family = family;
66 	nfmsg->version = version;
67 	nfmsg->res_id = res_id;
68 }
69 
nfnl_msg_put(struct sk_buff * skb,u32 portid,u32 seq,int type,int flags,u8 family,u8 version,__be16 res_id)70 static inline struct nlmsghdr *nfnl_msg_put(struct sk_buff *skb, u32 portid,
71 					    u32 seq, int type, int flags,
72 					    u8 family, u8 version,
73 					    __be16 res_id)
74 {
75 	struct nlmsghdr *nlh;
76 
77 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), flags);
78 	if (!nlh)
79 		return NULL;
80 
81 	nfnl_fill_hdr(nlh, family, version, res_id);
82 
83 	return nlh;
84 }
85 
86 void nfnl_lock(__u8 subsys_id);
87 void nfnl_unlock(__u8 subsys_id);
88 #ifdef CONFIG_PROVE_LOCKING
89 bool lockdep_nfnl_is_held(__u8 subsys_id);
90 #else
lockdep_nfnl_is_held(__u8 subsys_id)91 static inline bool lockdep_nfnl_is_held(__u8 subsys_id)
92 {
93 	return true;
94 }
95 #endif /* CONFIG_PROVE_LOCKING */
96 
97 #define MODULE_ALIAS_NFNL_SUBSYS(subsys) \
98 	MODULE_ALIAS("nfnetlink-subsys-" __stringify(subsys))
99 
100 #endif	/* _NFNETLINK_H */
101