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