Blame view
net/netfilter/nfnetlink.c
16.5 KB
f9e815b37 [NETFITLER]: Add ... |
1 2 3 4 5 |
/* Netfilter messages via netlink socket. Allows for user space * protocol helpers and general trouble making from userspace. * * (C) 2001 by Jay Schulist <jschlst@samba.org>, * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org> |
8c4d4e8b5 netfilter: nfnetl... |
6 |
* (C) 2005-2017 by Pablo Neira Ayuso <pablo@netfilter.org> |
f9e815b37 [NETFITLER]: Add ... |
7 8 9 10 11 12 13 14 15 |
* * Initial netfilter messages via netlink development funded and * generally made possible by Network Robots, Inc. (www.networkrobots.com) * * Further development of this code funded by Astaro AG (http://www.astaro.com) * * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. */ |
f9e815b37 [NETFITLER]: Add ... |
16 17 18 19 |
#include <linux/module.h> #include <linux/types.h> #include <linux/socket.h> #include <linux/kernel.h> |
f9e815b37 [NETFITLER]: Add ... |
20 21 22 |
#include <linux/string.h> #include <linux/sockios.h> #include <linux/net.h> |
f9e815b37 [NETFITLER]: Add ... |
23 |
#include <linux/skbuff.h> |
7c0f6ba68 Replace <asm/uacc... |
24 |
#include <linux/uaccess.h> |
f9e815b37 [NETFITLER]: Add ... |
25 26 |
#include <net/sock.h> #include <linux/init.h> |
8a3d4c361 netfilter: nf_tab... |
27 |
#include <linux/sched/signal.h> |
f9e815b37 [NETFITLER]: Add ... |
28 |
|
573ce260b net-next: replace... |
29 |
#include <net/netlink.h> |
f9e815b37 [NETFITLER]: Add ... |
30 31 32 |
#include <linux/netfilter/nfnetlink.h> MODULE_LICENSE("GPL"); |
4fdb3bb72 [NETLINK]: Add pr... |
33 34 |
MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER); |
4cacc3951 netfilter: Add MO... |
35 |
MODULE_DESCRIPTION("Netfilter messages via netlink socket"); |
f9e815b37 [NETFITLER]: Add ... |
36 |
|
9c55d3b54 nfnetlink: add nf... |
37 38 39 |
#define nfnl_dereference_protected(id) \ rcu_dereference_protected(table[(id)].subsys, \ lockdep_nfnl_is_held((id))) |
7b7744e2a netfilter: nfnetl... |
40 |
#define NFNL_MAX_ATTR_COUNT 32 |
c14b78e7d netfilter: nfnetl... |
41 42 43 44 |
static struct { struct mutex mutex; const struct nfnetlink_subsystem __rcu *subsys; } table[NFNL_SUBSYS_COUNT]; |
f9e815b37 [NETFITLER]: Add ... |
45 |
|
ab6c41eef netfilter: nfnetl... |
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
static struct lock_class_key nfnl_lockdep_keys[NFNL_SUBSYS_COUNT]; static const char *const nfnl_lockdep_names[NFNL_SUBSYS_COUNT] = { [NFNL_SUBSYS_NONE] = "nfnl_subsys_none", [NFNL_SUBSYS_CTNETLINK] = "nfnl_subsys_ctnetlink", [NFNL_SUBSYS_CTNETLINK_EXP] = "nfnl_subsys_ctnetlink_exp", [NFNL_SUBSYS_QUEUE] = "nfnl_subsys_queue", [NFNL_SUBSYS_ULOG] = "nfnl_subsys_ulog", [NFNL_SUBSYS_OSF] = "nfnl_subsys_osf", [NFNL_SUBSYS_IPSET] = "nfnl_subsys_ipset", [NFNL_SUBSYS_ACCT] = "nfnl_subsys_acct", [NFNL_SUBSYS_CTNETLINK_TIMEOUT] = "nfnl_subsys_cttimeout", [NFNL_SUBSYS_CTHELPER] = "nfnl_subsys_cthelper", [NFNL_SUBSYS_NFTABLES] = "nfnl_subsys_nftables", [NFNL_SUBSYS_NFT_COMPAT] = "nfnl_subsys_nftcompat", }; |
03292745b netlink: add nlk-... |
62 63 64 65 66 67 68 |
static const int nfnl_group2type[NFNLGRP_MAX+1] = { [NFNLGRP_CONNTRACK_NEW] = NFNL_SUBSYS_CTNETLINK, [NFNLGRP_CONNTRACK_UPDATE] = NFNL_SUBSYS_CTNETLINK, [NFNLGRP_CONNTRACK_DESTROY] = NFNL_SUBSYS_CTNETLINK, [NFNLGRP_CONNTRACK_EXP_NEW] = NFNL_SUBSYS_CTNETLINK_EXP, [NFNLGRP_CONNTRACK_EXP_UPDATE] = NFNL_SUBSYS_CTNETLINK_EXP, [NFNLGRP_CONNTRACK_EXP_DESTROY] = NFNL_SUBSYS_CTNETLINK_EXP, |
97840cb67 netfilter: nfnetl... |
69 70 |
[NFNLGRP_NFTABLES] = NFNL_SUBSYS_NFTABLES, [NFNLGRP_ACCT_QUOTA] = NFNL_SUBSYS_ACCT, |
33d5a7b14 netfilter: nf_tab... |
71 |
[NFNLGRP_NFTRACE] = NFNL_SUBSYS_NFTABLES, |
03292745b netlink: add nlk-... |
72 |
}; |
c14b78e7d netfilter: nfnetl... |
73 |
void nfnl_lock(__u8 subsys_id) |
f9e815b37 [NETFITLER]: Add ... |
74 |
{ |
c14b78e7d netfilter: nfnetl... |
75 |
mutex_lock(&table[subsys_id].mutex); |
f9e815b37 [NETFITLER]: Add ... |
76 |
} |
e6a7d3c04 netfilter: ctnetl... |
77 |
EXPORT_SYMBOL_GPL(nfnl_lock); |
f9e815b37 [NETFITLER]: Add ... |
78 |
|
c14b78e7d netfilter: nfnetl... |
79 |
void nfnl_unlock(__u8 subsys_id) |
a3c5029cf [NETFILTER]: nfne... |
80 |
{ |
c14b78e7d netfilter: nfnetl... |
81 |
mutex_unlock(&table[subsys_id].mutex); |
f9e815b37 [NETFITLER]: Add ... |
82 |
} |
e6a7d3c04 netfilter: ctnetl... |
83 |
EXPORT_SYMBOL_GPL(nfnl_unlock); |
f9e815b37 [NETFITLER]: Add ... |
84 |
|
0eb5db7ad netfilter: nfnetl... |
85 |
#ifdef CONFIG_PROVE_LOCKING |
875e08294 net/nfnetlink: lo... |
86 |
bool lockdep_nfnl_is_held(u8 subsys_id) |
0eb5db7ad netfilter: nfnetl... |
87 88 89 90 91 |
{ return lockdep_is_held(&table[subsys_id].mutex); } EXPORT_SYMBOL_GPL(lockdep_nfnl_is_held); #endif |
7c8d4cb41 [NETFILTER]: nfne... |
92 |
int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n) |
f9e815b37 [NETFITLER]: Add ... |
93 |
{ |
7b7744e2a netfilter: nfnetl... |
94 95 96 97 98 99 |
u8 cb_id; /* Sanity-check attr_count size to avoid stack buffer overflow. */ for (cb_id = 0; cb_id < n->cb_count; cb_id++) if (WARN_ON(n->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT)) return -EINVAL; |
c14b78e7d netfilter: nfnetl... |
100 101 102 |
nfnl_lock(n->subsys_id); if (table[n->subsys_id].subsys) { nfnl_unlock(n->subsys_id); |
0ab43f849 [NETFILTER]: Core... |
103 104 |
return -EBUSY; } |
c14b78e7d netfilter: nfnetl... |
105 106 |
rcu_assign_pointer(table[n->subsys_id].subsys, n); nfnl_unlock(n->subsys_id); |
f9e815b37 [NETFITLER]: Add ... |
107 108 109 |
return 0; } |
f4bc177f0 [NETFILTER]: nfne... |
110 |
EXPORT_SYMBOL_GPL(nfnetlink_subsys_register); |
f9e815b37 [NETFITLER]: Add ... |
111 |
|
7c8d4cb41 [NETFILTER]: nfne... |
112 |
int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n) |
f9e815b37 [NETFITLER]: Add ... |
113 |
{ |
c14b78e7d netfilter: nfnetl... |
114 115 116 |
nfnl_lock(n->subsys_id); table[n->subsys_id].subsys = NULL; nfnl_unlock(n->subsys_id); |
6b75e3e8d netfilter: nfnetl... |
117 |
synchronize_rcu(); |
f9e815b37 [NETFITLER]: Add ... |
118 119 |
return 0; } |
f4bc177f0 [NETFILTER]: nfne... |
120 |
EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister); |
f9e815b37 [NETFITLER]: Add ... |
121 |
|
b745d0358 netfilter: nfnetl... |
122 |
static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u16 type) |
f9e815b37 [NETFITLER]: Add ... |
123 |
{ |
b745d0358 netfilter: nfnetl... |
124 |
u8 subsys_id = NFNL_SUBSYS_ID(type); |
f9e815b37 [NETFITLER]: Add ... |
125 |
|
ac0f1d989 [NETFILTER]: nfne... |
126 |
if (subsys_id >= NFNL_SUBSYS_COUNT) |
f9e815b37 [NETFITLER]: Add ... |
127 |
return NULL; |
c14b78e7d netfilter: nfnetl... |
128 |
return rcu_dereference(table[subsys_id].subsys); |
f9e815b37 [NETFITLER]: Add ... |
129 |
} |
7c8d4cb41 [NETFILTER]: nfne... |
130 |
static inline const struct nfnl_callback * |
b745d0358 netfilter: nfnetl... |
131 |
nfnetlink_find_client(u16 type, const struct nfnetlink_subsystem *ss) |
f9e815b37 [NETFITLER]: Add ... |
132 |
{ |
b745d0358 netfilter: nfnetl... |
133 |
u8 cb_id = NFNL_MSG_TYPE(type); |
601e68e10 [NETFILTER]: Fix ... |
134 |
|
67ca39660 [NETFILTER]: nfne... |
135 |
if (cb_id >= ss->cb_count) |
f9e815b37 [NETFITLER]: Add ... |
136 |
return NULL; |
f9e815b37 [NETFITLER]: Add ... |
137 138 139 |
return &ss->cb[cb_id]; } |
cd8c20b65 netfilter: nfnetl... |
140 |
int nfnetlink_has_listeners(struct net *net, unsigned int group) |
a24276924 [NETFILTER]: ctne... |
141 |
{ |
cd8c20b65 netfilter: nfnetl... |
142 |
return netlink_has_listeners(net->nfnl, group); |
a24276924 [NETFILTER]: ctne... |
143 144 |
} EXPORT_SYMBOL_GPL(nfnetlink_has_listeners); |
ec464e5dc netfilter: rename... |
145 |
int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid, |
95c961747 net: cleanup unsi... |
146 |
unsigned int group, int echo, gfp_t flags) |
f9e815b37 [NETFITLER]: Add ... |
147 |
{ |
ec464e5dc netfilter: rename... |
148 |
return nlmsg_notify(net->nfnl, skb, portid, group, echo, flags); |
f9e815b37 [NETFITLER]: Add ... |
149 |
} |
f4bc177f0 [NETFILTER]: nfne... |
150 |
EXPORT_SYMBOL_GPL(nfnetlink_send); |
f9e815b37 [NETFITLER]: Add ... |
151 |
|
ec464e5dc netfilter: rename... |
152 |
int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error) |
dd5b6ce6f nefilter: nfnetli... |
153 |
{ |
ec464e5dc netfilter: rename... |
154 |
return netlink_set_err(net->nfnl, portid, group, error); |
dd5b6ce6f nefilter: nfnetli... |
155 156 |
} EXPORT_SYMBOL_GPL(nfnetlink_set_err); |
ee9211835 netfilter: nfnetl... |
157 |
int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid) |
f9e815b37 [NETFITLER]: Add ... |
158 |
{ |
ee9211835 netfilter: nfnetl... |
159 160 161 162 163 164 165 |
int err; err = nlmsg_unicast(net->nfnl, skb, portid); if (err == -EAGAIN) err = -ENOBUFS; return err; |
f9e815b37 [NETFITLER]: Add ... |
166 |
} |
f4bc177f0 [NETFILTER]: nfne... |
167 |
EXPORT_SYMBOL_GPL(nfnetlink_unicast); |
f9e815b37 [NETFITLER]: Add ... |
168 169 |
/* Process one complete nfnetlink message. */ |
2d4bc9336 netlink: extended... |
170 171 |
static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, struct netlink_ext_ack *extack) |
f9e815b37 [NETFITLER]: Add ... |
172 |
{ |
cd8c20b65 netfilter: nfnetl... |
173 |
struct net *net = sock_net(skb->sk); |
7c8d4cb41 [NETFILTER]: nfne... |
174 175 |
const struct nfnl_callback *nc; const struct nfnetlink_subsystem *ss; |
1d00a4eb4 [NETLINK]: Remove... |
176 |
int type, err; |
f9e815b37 [NETFITLER]: Add ... |
177 |
|
f9e815b37 [NETFITLER]: Add ... |
178 |
/* All the messages must at least contain nfgenmsg */ |
573ce260b net-next: replace... |
179 |
if (nlmsg_len(nlh) < sizeof(struct nfgenmsg)) |
f9e815b37 [NETFITLER]: Add ... |
180 |
return 0; |
f9e815b37 [NETFITLER]: Add ... |
181 182 |
type = nlh->nlmsg_type; |
e6a7d3c04 netfilter: ctnetl... |
183 |
replay: |
6b75e3e8d netfilter: nfnetl... |
184 |
rcu_read_lock(); |
f9e815b37 [NETFITLER]: Add ... |
185 |
ss = nfnetlink_get_subsys(type); |
0ab43f849 [NETFILTER]: Core... |
186 |
if (!ss) { |
95a5afca4 net: Remove CONFI... |
187 |
#ifdef CONFIG_MODULES |
6b75e3e8d netfilter: nfnetl... |
188 |
rcu_read_unlock(); |
37d2e7a20 [NETFILTER] nfnet... |
189 |
request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type)); |
6b75e3e8d netfilter: nfnetl... |
190 |
rcu_read_lock(); |
37d2e7a20 [NETFILTER] nfnet... |
191 |
ss = nfnetlink_get_subsys(type); |
0ab43f849 [NETFILTER]: Core... |
192 193 |
if (!ss) #endif |
6b75e3e8d netfilter: nfnetl... |
194 195 |
{ rcu_read_unlock(); |
1d00a4eb4 [NETLINK]: Remove... |
196 |
return -EINVAL; |
6b75e3e8d netfilter: nfnetl... |
197 |
} |
0ab43f849 [NETFILTER]: Core... |
198 |
} |
f9e815b37 [NETFITLER]: Add ... |
199 200 |
nc = nfnetlink_find_client(type, ss); |
6b75e3e8d netfilter: nfnetl... |
201 202 |
if (!nc) { rcu_read_unlock(); |
1d00a4eb4 [NETLINK]: Remove... |
203 |
return -EINVAL; |
6b75e3e8d netfilter: nfnetl... |
204 |
} |
f9e815b37 [NETFITLER]: Add ... |
205 |
|
f9e815b37 [NETFITLER]: Add ... |
206 |
{ |
573ce260b net-next: replace... |
207 |
int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); |
b745d0358 netfilter: nfnetl... |
208 |
u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type); |
7b7744e2a netfilter: nfnetl... |
209 |
struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1]; |
f49c857ff netfilter: nfnetl... |
210 211 |
struct nlattr *attr = (void *)nlh + min_len; int attrlen = nlh->nlmsg_len - min_len; |
c14b78e7d netfilter: nfnetl... |
212 |
__u8 subsys_id = NFNL_SUBSYS_ID(type); |
f49c857ff netfilter: nfnetl... |
213 |
|
7b7744e2a netfilter: nfnetl... |
214 215 216 217 218 |
/* Sanity-check NFNL_MAX_ATTR_COUNT */ if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) { rcu_read_unlock(); return -ENOMEM; } |
8cb081746 netlink: make val... |
219 220 221 |
err = nla_parse_deprecated(cda, ss->cb[cb_id].attr_count, attr, attrlen, ss->cb[cb_id].policy, extack); |
4009e1885 netfilter: nfnetl... |
222 223 |
if (err < 0) { rcu_read_unlock(); |
f49c857ff netfilter: nfnetl... |
224 |
return err; |
4009e1885 netfilter: nfnetl... |
225 |
} |
601e68e10 [NETFILTER]: Fix ... |
226 |
|
6b75e3e8d netfilter: nfnetl... |
227 |
if (nc->call_rcu) { |
7b8002a15 netfilter: nfnetl... |
228 |
err = nc->call_rcu(net, net->nfnl, skb, nlh, |
04ba724b6 netfilter: nfnetl... |
229 230 |
(const struct nlattr **)cda, extack); |
6b75e3e8d netfilter: nfnetl... |
231 232 233 |
rcu_read_unlock(); } else { rcu_read_unlock(); |
c14b78e7d netfilter: nfnetl... |
234 |
nfnl_lock(subsys_id); |
9c55d3b54 nfnetlink: add nf... |
235 |
if (nfnl_dereference_protected(subsys_id) != ss || |
6b75e3e8d netfilter: nfnetl... |
236 237 |
nfnetlink_find_client(type, ss) != nc) err = -EAGAIN; |
59560a38a netfilter: nfnetl... |
238 |
else if (nc->call) |
7b8002a15 netfilter: nfnetl... |
239 |
err = nc->call(net, net->nfnl, skb, nlh, |
04ba724b6 netfilter: nfnetl... |
240 241 |
(const struct nlattr **)cda, extack); |
59560a38a netfilter: nfnetl... |
242 243 |
else err = -EINVAL; |
c14b78e7d netfilter: nfnetl... |
244 |
nfnl_unlock(subsys_id); |
6b75e3e8d netfilter: nfnetl... |
245 |
} |
e6a7d3c04 netfilter: ctnetl... |
246 247 248 |
if (err == -EAGAIN) goto replay; return err; |
f9e815b37 [NETFITLER]: Add ... |
249 |
} |
f9e815b37 [NETFITLER]: Add ... |
250 |
} |
cbb8125eb netfilter: nfnetl... |
251 252 253 254 |
struct nfnl_err { struct list_head head; struct nlmsghdr *nlh; int err; |
04ba724b6 netfilter: nfnetl... |
255 |
struct netlink_ext_ack extack; |
cbb8125eb netfilter: nfnetl... |
256 |
}; |
04ba724b6 netfilter: nfnetl... |
257 258 |
static int nfnl_err_add(struct list_head *list, struct nlmsghdr *nlh, int err, const struct netlink_ext_ack *extack) |
cbb8125eb netfilter: nfnetl... |
259 260 261 262 263 264 265 266 267 |
{ struct nfnl_err *nfnl_err; nfnl_err = kmalloc(sizeof(struct nfnl_err), GFP_KERNEL); if (nfnl_err == NULL) return -ENOMEM; nfnl_err->nlh = nlh; nfnl_err->err = err; |
04ba724b6 netfilter: nfnetl... |
268 |
nfnl_err->extack = *extack; |
cbb8125eb netfilter: nfnetl... |
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
list_add_tail(&nfnl_err->head, list); return 0; } static void nfnl_err_del(struct nfnl_err *nfnl_err) { list_del(&nfnl_err->head); kfree(nfnl_err); } static void nfnl_err_reset(struct list_head *err_list) { struct nfnl_err *nfnl_err, *next; list_for_each_entry_safe(nfnl_err, next, err_list, head) nfnl_err_del(nfnl_err); } static void nfnl_err_deliver(struct list_head *err_list, struct sk_buff *skb) { struct nfnl_err *nfnl_err, *next; list_for_each_entry_safe(nfnl_err, next, err_list, head) { |
04ba724b6 netfilter: nfnetl... |
293 294 |
netlink_ack(skb, nfnl_err->nlh, nfnl_err->err, &nfnl_err->extack); |
cbb8125eb netfilter: nfnetl... |
295 296 297 |
nfnl_err_del(nfnl_err); } } |
6742b9e31 netfilter: nfnetl... |
298 299 300 301 302 |
enum { NFNL_BATCH_FAILURE = (1 << 0), NFNL_BATCH_DONE = (1 << 1), NFNL_BATCH_REPLAY = (1 << 2), }; |
0628b123c netfilter: nfnetl... |
303 |
static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, |
8c4d4e8b5 netfilter: nfnetl... |
304 |
u16 subsys_id, u32 genid) |
0628b123c netfilter: nfnetl... |
305 |
{ |
0f8162326 netfilter: nfnetl... |
306 |
struct sk_buff *oskb = skb; |
0628b123c netfilter: nfnetl... |
307 308 309 |
struct net *net = sock_net(skb->sk); const struct nfnetlink_subsystem *ss; const struct nfnl_callback *nc; |
04ba724b6 netfilter: nfnetl... |
310 |
struct netlink_ext_ack extack; |
4eba8b78e netfilter: nfnetl... |
311 |
LIST_HEAD(err_list); |
6742b9e31 netfilter: nfnetl... |
312 |
u32 status; |
0628b123c netfilter: nfnetl... |
313 314 315 |
int err; if (subsys_id >= NFNL_SUBSYS_COUNT) |
2d4bc9336 netlink: extended... |
316 |
return netlink_ack(skb, nlh, -EINVAL, NULL); |
0628b123c netfilter: nfnetl... |
317 |
replay: |
6742b9e31 netfilter: nfnetl... |
318 |
status = 0; |
c0391b6ab netfilter: nf_tab... |
319 |
replay_abort: |
0f8162326 netfilter: nfnetl... |
320 321 |
skb = netlink_skb_clone(oskb, GFP_KERNEL); if (!skb) |
2d4bc9336 netlink: extended... |
322 |
return netlink_ack(oskb, nlh, -ENOMEM, NULL); |
0628b123c netfilter: nfnetl... |
323 |
|
0628b123c netfilter: nfnetl... |
324 |
nfnl_lock(subsys_id); |
9c55d3b54 nfnetlink: add nf... |
325 |
ss = nfnl_dereference_protected(subsys_id); |
0628b123c netfilter: nfnetl... |
326 327 328 329 330 |
if (!ss) { #ifdef CONFIG_MODULES nfnl_unlock(subsys_id); request_module("nfnetlink-subsys-%d", subsys_id); nfnl_lock(subsys_id); |
9c55d3b54 nfnetlink: add nf... |
331 |
ss = nfnl_dereference_protected(subsys_id); |
0628b123c netfilter: nfnetl... |
332 333 334 335 |
if (!ss) #endif { nfnl_unlock(subsys_id); |
2d4bc9336 netlink: extended... |
336 |
netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL); |
0f8162326 netfilter: nfnetl... |
337 |
return kfree_skb(skb); |
0628b123c netfilter: nfnetl... |
338 339 |
} } |
ca2f18be7 netfilter: nf_tab... |
340 |
if (!ss->valid_genid || !ss->commit || !ss->abort) { |
0628b123c netfilter: nfnetl... |
341 |
nfnl_unlock(subsys_id); |
2d4bc9336 netlink: extended... |
342 |
netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL); |
ecd15dd7e netfilter: nfnetl... |
343 |
return kfree_skb(skb); |
0628b123c netfilter: nfnetl... |
344 |
} |
be2ab5b4d netfilter: nf_tab... |
345 346 347 348 349 |
if (!try_module_get(ss->owner)) { nfnl_unlock(subsys_id); netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL); return kfree_skb(skb); } |
ca2f18be7 netfilter: nf_tab... |
350 |
if (!ss->valid_genid(net, genid)) { |
be2ab5b4d netfilter: nf_tab... |
351 |
module_put(ss->owner); |
8c4d4e8b5 netfilter: nfnetl... |
352 |
nfnl_unlock(subsys_id); |
2d4bc9336 netlink: extended... |
353 |
netlink_ack(oskb, nlh, -ERESTART, NULL); |
8c4d4e8b5 netfilter: nfnetl... |
354 355 |
return kfree_skb(skb); } |
f102d66b3 netfilter: nf_tab... |
356 |
nfnl_unlock(subsys_id); |
0628b123c netfilter: nfnetl... |
357 358 |
while (skb->len >= nlmsg_total_size(0)) { int msglen, type; |
8a3d4c361 netfilter: nf_tab... |
359 360 361 362 363 364 |
if (fatal_signal_pending(current)) { nfnl_err_reset(&err_list); err = -EINTR; status = NFNL_BATCH_FAILURE; goto done; } |
04ba724b6 netfilter: nfnetl... |
365 |
memset(&extack, 0, sizeof(extack)); |
0628b123c netfilter: nfnetl... |
366 367 |
nlh = nlmsg_hdr(skb); err = 0; |
c58d6c936 netfilter: nfnetl... |
368 369 370 371 372 373 |
if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len || nlmsg_len(nlh) < sizeof(struct nfgenmsg)) { nfnl_err_reset(&err_list); status |= NFNL_BATCH_FAILURE; goto done; |
0628b123c netfilter: nfnetl... |
374 375 376 377 378 379 380 381 382 383 384 |
} /* Only requests are handled by the kernel */ if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) { err = -EINVAL; goto ack; } type = nlh->nlmsg_type; if (type == NFNL_MSG_BATCH_BEGIN) { /* Malformed: Batch begin twice */ |
cbb8125eb netfilter: nfnetl... |
385 |
nfnl_err_reset(&err_list); |
6742b9e31 netfilter: nfnetl... |
386 |
status |= NFNL_BATCH_FAILURE; |
0628b123c netfilter: nfnetl... |
387 388 |
goto done; } else if (type == NFNL_MSG_BATCH_END) { |
6742b9e31 netfilter: nfnetl... |
389 |
status |= NFNL_BATCH_DONE; |
0628b123c netfilter: nfnetl... |
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
goto done; } else if (type < NLMSG_MIN_TYPE) { err = -EINVAL; goto ack; } /* We only accept a batch with messages for the same * subsystem. */ if (NFNL_SUBSYS_ID(type) != subsys_id) { err = -EINVAL; goto ack; } nc = nfnetlink_find_client(type, ss); if (!nc) { err = -EINVAL; goto ack; } { int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); |
b745d0358 netfilter: nfnetl... |
412 |
u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type); |
7b7744e2a netfilter: nfnetl... |
413 |
struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1]; |
0628b123c netfilter: nfnetl... |
414 415 |
struct nlattr *attr = (void *)nlh + min_len; int attrlen = nlh->nlmsg_len - min_len; |
7b7744e2a netfilter: nfnetl... |
416 417 418 419 420 |
/* Sanity-check NFTA_MAX_ATTR */ if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) { err = -ENOMEM; goto ack; } |
8cb081746 netlink: make val... |
421 422 423 424 |
err = nla_parse_deprecated(cda, ss->cb[cb_id].attr_count, attr, attrlen, ss->cb[cb_id].policy, NULL); |
0628b123c netfilter: nfnetl... |
425 426 427 428 |
if (err < 0) goto ack; if (nc->call_batch) { |
633c9a840 netfilter: nfnetl... |
429 |
err = nc->call_batch(net, net->nfnl, skb, nlh, |
04ba724b6 netfilter: nfnetl... |
430 431 |
(const struct nlattr **)cda, &extack); |
0628b123c netfilter: nfnetl... |
432 433 434 435 436 437 438 |
} /* The lock was released to autoload some module, we * have to abort and start from scratch using the * original skb. */ if (err == -EAGAIN) { |
6742b9e31 netfilter: nfnetl... |
439 |
status |= NFNL_BATCH_REPLAY; |
71ad00c50 netfilter: nf_tab... |
440 |
goto done; |
0628b123c netfilter: nfnetl... |
441 442 443 444 |
} } ack: if (nlh->nlmsg_flags & NLM_F_ACK || err) { |
cbb8125eb netfilter: nfnetl... |
445 446 447 448 |
/* Errors are delivered once the full batch has been * processed, this avoids that the same error is * reported several times when replaying the batch. */ |
04ba724b6 netfilter: nfnetl... |
449 |
if (nfnl_err_add(&err_list, nlh, err, &extack) < 0) { |
cbb8125eb netfilter: nfnetl... |
450 451 452 453 454 |
/* We failed to enqueue an error, reset the * list of errors and send OOM to userspace * pointing to the batch header. */ nfnl_err_reset(&err_list); |
2d4bc9336 netlink: extended... |
455 456 |
netlink_ack(oskb, nlmsg_hdr(oskb), -ENOMEM, NULL); |
6742b9e31 netfilter: nfnetl... |
457 |
status |= NFNL_BATCH_FAILURE; |
cbb8125eb netfilter: nfnetl... |
458 459 |
goto done; } |
0628b123c netfilter: nfnetl... |
460 461 462 463 |
/* We don't stop processing the batch on errors, thus, * userspace gets all the errors that the batch * triggers. */ |
0628b123c netfilter: nfnetl... |
464 |
if (err) |
6742b9e31 netfilter: nfnetl... |
465 |
status |= NFNL_BATCH_FAILURE; |
0628b123c netfilter: nfnetl... |
466 |
} |
71ad00c50 netfilter: nf_tab... |
467 |
|
0628b123c netfilter: nfnetl... |
468 469 470 471 472 473 |
msglen = NLMSG_ALIGN(nlh->nlmsg_len); if (msglen > skb->len) msglen = skb->len; skb_pull(skb, msglen); } done: |
6742b9e31 netfilter: nfnetl... |
474 |
if (status & NFNL_BATCH_REPLAY) { |
c0391b6ab netfilter: nf_tab... |
475 |
ss->abort(net, oskb, NFNL_ABORT_AUTOLOAD); |
6742b9e31 netfilter: nfnetl... |
476 |
nfnl_err_reset(&err_list); |
6742b9e31 netfilter: nfnetl... |
477 |
kfree_skb(skb); |
be2ab5b4d netfilter: nf_tab... |
478 |
module_put(ss->owner); |
6742b9e31 netfilter: nfnetl... |
479 480 |
goto replay; } else if (status == NFNL_BATCH_DONE) { |
003087911 netfilter: nfnetl... |
481 482 483 484 485 |
err = ss->commit(net, oskb); if (err == -EAGAIN) { status |= NFNL_BATCH_REPLAY; goto done; } else if (err) { |
c0391b6ab netfilter: nf_tab... |
486 |
ss->abort(net, oskb, NFNL_ABORT_NONE); |
003087911 netfilter: nfnetl... |
487 488 |
netlink_ack(oskb, nlmsg_hdr(oskb), err, NULL); } |
6742b9e31 netfilter: nfnetl... |
489 |
} else { |
c0391b6ab netfilter: nf_tab... |
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
enum nfnl_abort_action abort_action; if (status & NFNL_BATCH_FAILURE) abort_action = NFNL_ABORT_NONE; else abort_action = NFNL_ABORT_VALIDATE; err = ss->abort(net, oskb, abort_action); if (err == -EAGAIN) { nfnl_err_reset(&err_list); kfree_skb(skb); module_put(ss->owner); status |= NFNL_BATCH_FAILURE; goto replay_abort; } |
6742b9e31 netfilter: nfnetl... |
505 |
} |
a654de8fd netfilter: nf_tab... |
506 507 |
if (ss->cleanup) ss->cleanup(net); |
0628b123c netfilter: nfnetl... |
508 |
|
cbb8125eb netfilter: nfnetl... |
509 |
nfnl_err_deliver(&err_list, oskb); |
0f8162326 netfilter: nfnetl... |
510 |
kfree_skb(skb); |
be2ab5b4d netfilter: nf_tab... |
511 |
module_put(ss->owner); |
0628b123c netfilter: nfnetl... |
512 |
} |
8c4d4e8b5 netfilter: nfnetl... |
513 514 515 |
static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = { [NFNL_BATCH_GENID] = { .type = NLA_U32 }, }; |
48656835c netfilter: nfnetl... |
516 |
static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh) |
f9e815b37 [NETFITLER]: Add ... |
517 |
{ |
8c4d4e8b5 netfilter: nfnetl... |
518 519 520 521 |
int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); struct nlattr *attr = (void *)nlh + min_len; struct nlattr *cda[NFNL_BATCH_MAX + 1]; int attrlen = nlh->nlmsg_len - min_len; |
48656835c netfilter: nfnetl... |
522 |
struct nfgenmsg *nfgenmsg; |
8c4d4e8b5 netfilter: nfnetl... |
523 524 |
int msglen, err; u32 gen_id = 0; |
b745d0358 netfilter: nfnetl... |
525 |
u16 res_id; |
0628b123c netfilter: nfnetl... |
526 |
|
48656835c netfilter: nfnetl... |
527 528 529 |
msglen = NLMSG_ALIGN(nlh->nlmsg_len); if (msglen > skb->len) msglen = skb->len; |
f55ce7b02 netfilter: nfnetl... |
530 |
if (skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg)) |
48656835c netfilter: nfnetl... |
531 |
return; |
8cb081746 netlink: make val... |
532 533 |
err = nla_parse_deprecated(cda, NFNL_BATCH_MAX, attr, attrlen, nfnl_batch_policy, NULL); |
8c4d4e8b5 netfilter: nfnetl... |
534 |
if (err < 0) { |
2d4bc9336 netlink: extended... |
535 |
netlink_ack(skb, nlh, err, NULL); |
8c4d4e8b5 netfilter: nfnetl... |
536 537 538 539 |
return; } if (cda[NFNL_BATCH_GENID]) gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID])); |
48656835c netfilter: nfnetl... |
540 541 542 543 544 545 546 |
nfgenmsg = nlmsg_data(nlh); skb_pull(skb, msglen); /* Work around old nft using host byte order */ if (nfgenmsg->res_id == NFNL_SUBSYS_NFTABLES) res_id = NFNL_SUBSYS_NFTABLES; else res_id = ntohs(nfgenmsg->res_id); |
8c4d4e8b5 netfilter: nfnetl... |
547 |
nfnetlink_rcv_batch(skb, nlh, res_id, gen_id); |
48656835c netfilter: nfnetl... |
548 549 550 551 552 |
} static void nfnetlink_rcv(struct sk_buff *skb) { struct nlmsghdr *nlh = nlmsg_hdr(skb); |
f55ce7b02 netfilter: nfnetl... |
553 554 |
if (skb->len < NLMSG_HDRLEN || nlh->nlmsg_len < NLMSG_HDRLEN || |
0628b123c netfilter: nfnetl... |
555 556 |
skb->len < nlh->nlmsg_len) return; |
90f62cf30 net: Use netlink_... |
557 |
if (!netlink_net_capable(skb, CAP_NET_ADMIN)) { |
2d4bc9336 netlink: extended... |
558 |
netlink_ack(skb, nlh, -EPERM, NULL); |
cdbe7c2d6 nfnetlink: do not... |
559 560 |
return; } |
48656835c netfilter: nfnetl... |
561 562 563 |
if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN) nfnetlink_rcv_skb_batch(skb, nlh); else |
d4ef38354 netfilter: Remove... |
564 |
netlink_rcv_skb(skb, nfnetlink_rcv_msg); |
f9e815b37 [NETFITLER]: Add ... |
565 |
} |
03292745b netlink: add nlk-... |
566 |
#ifdef CONFIG_MODULES |
023e2cfa3 netlink/genetlink... |
567 |
static int nfnetlink_bind(struct net *net, int group) |
03292745b netlink: add nlk-... |
568 569 |
{ const struct nfnetlink_subsystem *ss; |
97840cb67 netfilter: nfnetl... |
570 571 572 |
int type; if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX) |
62924af24 netfilter: nfnetl... |
573 |
return 0; |
97840cb67 netfilter: nfnetl... |
574 575 |
type = nfnl_group2type[group]; |
03292745b netlink: add nlk-... |
576 577 |
rcu_read_lock(); |
dbc3617f4 netfilter: nfnetl... |
578 |
ss = nfnetlink_get_subsys(type << 8); |
03292745b netlink: add nlk-... |
579 |
rcu_read_unlock(); |
bfe4bc71c netlink: simplify... |
580 |
if (!ss) |
1b0890cd6 netfilter: nfnetl... |
581 |
request_module_nowait("nfnetlink-subsys-%d", type); |
4f5209005 netlink: have net... |
582 |
return 0; |
03292745b netlink: add nlk-... |
583 584 |
} #endif |
cd8c20b65 netfilter: nfnetl... |
585 |
static int __net_init nfnetlink_net_init(struct net *net) |
f9e815b37 [NETFITLER]: Add ... |
586 |
{ |
cd8c20b65 netfilter: nfnetl... |
587 |
struct sock *nfnl; |
a31f2d17b netlink: add netl... |
588 589 590 |
struct netlink_kernel_cfg cfg = { .groups = NFNLGRP_MAX, .input = nfnetlink_rcv, |
03292745b netlink: add nlk-... |
591 592 593 |
#ifdef CONFIG_MODULES .bind = nfnetlink_bind, #endif |
a31f2d17b netlink: add netl... |
594 |
}; |
cd8c20b65 netfilter: nfnetl... |
595 |
|
9f00d9776 netlink: hide str... |
596 |
nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg); |
cd8c20b65 netfilter: nfnetl... |
597 598 599 |
if (!nfnl) return -ENOMEM; net->nfnl_stash = nfnl; |
cf778b00e net: reintroduce ... |
600 |
rcu_assign_pointer(net->nfnl, nfnl); |
cd8c20b65 netfilter: nfnetl... |
601 |
return 0; |
f9e815b37 [NETFITLER]: Add ... |
602 |
} |
cd8c20b65 netfilter: nfnetl... |
603 |
static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list) |
f9e815b37 [NETFITLER]: Add ... |
604 |
{ |
cd8c20b65 netfilter: nfnetl... |
605 |
struct net *net; |
f9e815b37 [NETFITLER]: Add ... |
606 |
|
cd8c20b65 netfilter: nfnetl... |
607 |
list_for_each_entry(net, net_exit_list, exit_list) |
a9b3cd7f3 rcu: convert uses... |
608 |
RCU_INIT_POINTER(net->nfnl, NULL); |
cd8c20b65 netfilter: nfnetl... |
609 610 611 612 |
synchronize_net(); list_for_each_entry(net, net_exit_list, exit_list) netlink_kernel_release(net->nfnl_stash); } |
f9e815b37 [NETFITLER]: Add ... |
613 |
|
cd8c20b65 netfilter: nfnetl... |
614 615 616 617 618 619 620 |
static struct pernet_operations nfnetlink_net_ops = { .init = nfnetlink_net_init, .exit_batch = nfnetlink_net_exit_batch, }; static int __init nfnetlink_init(void) { |
c14b78e7d netfilter: nfnetl... |
621 |
int i; |
97840cb67 netfilter: nfnetl... |
622 623 |
for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++) BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE); |
c14b78e7d netfilter: nfnetl... |
624 |
for (i=0; i<NFNL_SUBSYS_COUNT; i++) |
ab6c41eef netfilter: nfnetl... |
625 |
__mutex_init(&table[i].mutex, nfnl_lockdep_names[i], &nfnl_lockdep_keys[i]); |
c14b78e7d netfilter: nfnetl... |
626 |
|
cd8c20b65 netfilter: nfnetl... |
627 |
return register_pernet_subsys(&nfnetlink_net_ops); |
f9e815b37 [NETFITLER]: Add ... |
628 |
} |
cd8c20b65 netfilter: nfnetl... |
629 630 |
static void __exit nfnetlink_exit(void) { |
cd8c20b65 netfilter: nfnetl... |
631 632 |
unregister_pernet_subsys(&nfnetlink_net_ops); } |
f9e815b37 [NETFITLER]: Add ... |
633 634 |
module_init(nfnetlink_init); module_exit(nfnetlink_exit); |