Blame view
net/netfilter/nfnetlink.c
16.5 KB
f9e815b37
|
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
|
6 |
* (C) 2005-2017 by Pablo Neira Ayuso <pablo@netfilter.org> |
f9e815b37
|
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
|
16 17 18 19 |
#include <linux/module.h> #include <linux/types.h> #include <linux/socket.h> #include <linux/kernel.h> |
f9e815b37
|
20 21 22 |
#include <linux/string.h> #include <linux/sockios.h> #include <linux/net.h> |
f9e815b37
|
23 |
#include <linux/skbuff.h> |
7c0f6ba68
|
24 |
#include <linux/uaccess.h> |
f9e815b37
|
25 26 |
#include <net/sock.h> #include <linux/init.h> |
8a3d4c361
|
27 |
#include <linux/sched/signal.h> |
f9e815b37
|
28 |
|
573ce260b
|
29 |
#include <net/netlink.h> |
f9e815b37
|
30 31 32 |
#include <linux/netfilter/nfnetlink.h> MODULE_LICENSE("GPL"); |
4fdb3bb72
|
33 34 |
MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER); |
4cacc3951
|
35 |
MODULE_DESCRIPTION("Netfilter messages via netlink socket"); |
f9e815b37
|
36 |
|
9c55d3b54
|
37 38 39 |
#define nfnl_dereference_protected(id) \ rcu_dereference_protected(table[(id)].subsys, \ lockdep_nfnl_is_held((id))) |
7b7744e2a
|
40 |
#define NFNL_MAX_ATTR_COUNT 32 |
c14b78e7d
|
41 42 43 44 |
static struct { struct mutex mutex; const struct nfnetlink_subsystem __rcu *subsys; } table[NFNL_SUBSYS_COUNT]; |
f9e815b37
|
45 |
|
ab6c41eef
|
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
|
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
|
69 70 |
[NFNLGRP_NFTABLES] = NFNL_SUBSYS_NFTABLES, [NFNLGRP_ACCT_QUOTA] = NFNL_SUBSYS_ACCT, |
33d5a7b14
|
71 |
[NFNLGRP_NFTRACE] = NFNL_SUBSYS_NFTABLES, |
03292745b
|
72 |
}; |
c14b78e7d
|
73 |
void nfnl_lock(__u8 subsys_id) |
f9e815b37
|
74 |
{ |
c14b78e7d
|
75 |
mutex_lock(&table[subsys_id].mutex); |
f9e815b37
|
76 |
} |
e6a7d3c04
|
77 |
EXPORT_SYMBOL_GPL(nfnl_lock); |
f9e815b37
|
78 |
|
c14b78e7d
|
79 |
void nfnl_unlock(__u8 subsys_id) |
a3c5029cf
|
80 |
{ |
c14b78e7d
|
81 |
mutex_unlock(&table[subsys_id].mutex); |
f9e815b37
|
82 |
} |
e6a7d3c04
|
83 |
EXPORT_SYMBOL_GPL(nfnl_unlock); |
f9e815b37
|
84 |
|
0eb5db7ad
|
85 |
#ifdef CONFIG_PROVE_LOCKING |
875e08294
|
86 |
bool lockdep_nfnl_is_held(u8 subsys_id) |
0eb5db7ad
|
87 88 89 90 91 |
{ return lockdep_is_held(&table[subsys_id].mutex); } EXPORT_SYMBOL_GPL(lockdep_nfnl_is_held); #endif |
7c8d4cb41
|
92 |
int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n) |
f9e815b37
|
93 |
{ |
7b7744e2a
|
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
|
100 101 102 |
nfnl_lock(n->subsys_id); if (table[n->subsys_id].subsys) { nfnl_unlock(n->subsys_id); |
0ab43f849
|
103 104 |
return -EBUSY; } |
c14b78e7d
|
105 106 |
rcu_assign_pointer(table[n->subsys_id].subsys, n); nfnl_unlock(n->subsys_id); |
f9e815b37
|
107 108 109 |
return 0; } |
f4bc177f0
|
110 |
EXPORT_SYMBOL_GPL(nfnetlink_subsys_register); |
f9e815b37
|
111 |
|
7c8d4cb41
|
112 |
int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n) |
f9e815b37
|
113 |
{ |
c14b78e7d
|
114 115 116 |
nfnl_lock(n->subsys_id); table[n->subsys_id].subsys = NULL; nfnl_unlock(n->subsys_id); |
6b75e3e8d
|
117 |
synchronize_rcu(); |
f9e815b37
|
118 119 |
return 0; } |
f4bc177f0
|
120 |
EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister); |
f9e815b37
|
121 |
|
b745d0358
|
122 |
static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u16 type) |
f9e815b37
|
123 |
{ |
b745d0358
|
124 |
u8 subsys_id = NFNL_SUBSYS_ID(type); |
f9e815b37
|
125 |
|
ac0f1d989
|
126 |
if (subsys_id >= NFNL_SUBSYS_COUNT) |
f9e815b37
|
127 |
return NULL; |
c14b78e7d
|
128 |
return rcu_dereference(table[subsys_id].subsys); |
f9e815b37
|
129 |
} |
7c8d4cb41
|
130 |
static inline const struct nfnl_callback * |
b745d0358
|
131 |
nfnetlink_find_client(u16 type, const struct nfnetlink_subsystem *ss) |
f9e815b37
|
132 |
{ |
b745d0358
|
133 |
u8 cb_id = NFNL_MSG_TYPE(type); |
601e68e10
|
134 |
|
67ca39660
|
135 |
if (cb_id >= ss->cb_count) |
f9e815b37
|
136 |
return NULL; |
f9e815b37
|
137 138 139 |
return &ss->cb[cb_id]; } |
cd8c20b65
|
140 |
int nfnetlink_has_listeners(struct net *net, unsigned int group) |
a24276924
|
141 |
{ |
cd8c20b65
|
142 |
return netlink_has_listeners(net->nfnl, group); |
a24276924
|
143 144 |
} EXPORT_SYMBOL_GPL(nfnetlink_has_listeners); |
ec464e5dc
|
145 |
int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid, |
95c961747
|
146 |
unsigned int group, int echo, gfp_t flags) |
f9e815b37
|
147 |
{ |
ec464e5dc
|
148 |
return nlmsg_notify(net->nfnl, skb, portid, group, echo, flags); |
f9e815b37
|
149 |
} |
f4bc177f0
|
150 |
EXPORT_SYMBOL_GPL(nfnetlink_send); |
f9e815b37
|
151 |
|
ec464e5dc
|
152 |
int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error) |
dd5b6ce6f
|
153 |
{ |
ec464e5dc
|
154 |
return netlink_set_err(net->nfnl, portid, group, error); |
dd5b6ce6f
|
155 156 |
} EXPORT_SYMBOL_GPL(nfnetlink_set_err); |
ee9211835
|
157 |
int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid) |
f9e815b37
|
158 |
{ |
ee9211835
|
159 160 161 162 163 164 165 |
int err; err = nlmsg_unicast(net->nfnl, skb, portid); if (err == -EAGAIN) err = -ENOBUFS; return err; |
f9e815b37
|
166 |
} |
f4bc177f0
|
167 |
EXPORT_SYMBOL_GPL(nfnetlink_unicast); |
f9e815b37
|
168 169 |
/* Process one complete nfnetlink message. */ |
2d4bc9336
|
170 171 |
static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, struct netlink_ext_ack *extack) |
f9e815b37
|
172 |
{ |
cd8c20b65
|
173 |
struct net *net = sock_net(skb->sk); |
7c8d4cb41
|
174 175 |
const struct nfnl_callback *nc; const struct nfnetlink_subsystem *ss; |
1d00a4eb4
|
176 |
int type, err; |
f9e815b37
|
177 |
|
f9e815b37
|
178 |
/* All the messages must at least contain nfgenmsg */ |
573ce260b
|
179 |
if (nlmsg_len(nlh) < sizeof(struct nfgenmsg)) |
f9e815b37
|
180 |
return 0; |
f9e815b37
|
181 182 |
type = nlh->nlmsg_type; |
e6a7d3c04
|
183 |
replay: |
6b75e3e8d
|
184 |
rcu_read_lock(); |
f9e815b37
|
185 |
ss = nfnetlink_get_subsys(type); |
0ab43f849
|
186 |
if (!ss) { |
95a5afca4
|
187 |
#ifdef CONFIG_MODULES |
6b75e3e8d
|
188 |
rcu_read_unlock(); |
37d2e7a20
|
189 |
request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type)); |
6b75e3e8d
|
190 |
rcu_read_lock(); |
37d2e7a20
|
191 |
ss = nfnetlink_get_subsys(type); |
0ab43f849
|
192 193 |
if (!ss) #endif |
6b75e3e8d
|
194 195 |
{ rcu_read_unlock(); |
1d00a4eb4
|
196 |
return -EINVAL; |
6b75e3e8d
|
197 |
} |
0ab43f849
|
198 |
} |
f9e815b37
|
199 200 |
nc = nfnetlink_find_client(type, ss); |
6b75e3e8d
|
201 202 |
if (!nc) { rcu_read_unlock(); |
1d00a4eb4
|
203 |
return -EINVAL; |
6b75e3e8d
|
204 |
} |
f9e815b37
|
205 |
|
f9e815b37
|
206 |
{ |
573ce260b
|
207 |
int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); |
b745d0358
|
208 |
u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type); |
7b7744e2a
|
209 |
struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1]; |
f49c857ff
|
210 211 |
struct nlattr *attr = (void *)nlh + min_len; int attrlen = nlh->nlmsg_len - min_len; |
c14b78e7d
|
212 |
__u8 subsys_id = NFNL_SUBSYS_ID(type); |
f49c857ff
|
213 |
|
7b7744e2a
|
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
|
219 220 221 |
err = nla_parse_deprecated(cda, ss->cb[cb_id].attr_count, attr, attrlen, ss->cb[cb_id].policy, extack); |
4009e1885
|
222 223 |
if (err < 0) { rcu_read_unlock(); |
f49c857ff
|
224 |
return err; |
4009e1885
|
225 |
} |
601e68e10
|
226 |
|
6b75e3e8d
|
227 |
if (nc->call_rcu) { |
7b8002a15
|
228 |
err = nc->call_rcu(net, net->nfnl, skb, nlh, |
04ba724b6
|
229 230 |
(const struct nlattr **)cda, extack); |
6b75e3e8d
|
231 232 233 |
rcu_read_unlock(); } else { rcu_read_unlock(); |
c14b78e7d
|
234 |
nfnl_lock(subsys_id); |
9c55d3b54
|
235 |
if (nfnl_dereference_protected(subsys_id) != ss || |
6b75e3e8d
|
236 237 |
nfnetlink_find_client(type, ss) != nc) err = -EAGAIN; |
59560a38a
|
238 |
else if (nc->call) |
7b8002a15
|
239 |
err = nc->call(net, net->nfnl, skb, nlh, |
04ba724b6
|
240 241 |
(const struct nlattr **)cda, extack); |
59560a38a
|
242 243 |
else err = -EINVAL; |
c14b78e7d
|
244 |
nfnl_unlock(subsys_id); |
6b75e3e8d
|
245 |
} |
e6a7d3c04
|
246 247 248 |
if (err == -EAGAIN) goto replay; return err; |
f9e815b37
|
249 |
} |
f9e815b37
|
250 |
} |
cbb8125eb
|
251 252 253 254 |
struct nfnl_err { struct list_head head; struct nlmsghdr *nlh; int err; |
04ba724b6
|
255 |
struct netlink_ext_ack extack; |
cbb8125eb
|
256 |
}; |
04ba724b6
|
257 258 |
static int nfnl_err_add(struct list_head *list, struct nlmsghdr *nlh, int err, const struct netlink_ext_ack *extack) |
cbb8125eb
|
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
|
268 |
nfnl_err->extack = *extack; |
cbb8125eb
|
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
|
293 294 |
netlink_ack(skb, nfnl_err->nlh, nfnl_err->err, &nfnl_err->extack); |
cbb8125eb
|
295 296 297 |
nfnl_err_del(nfnl_err); } } |
6742b9e31
|
298 299 300 301 302 |
enum { NFNL_BATCH_FAILURE = (1 << 0), NFNL_BATCH_DONE = (1 << 1), NFNL_BATCH_REPLAY = (1 << 2), }; |
0628b123c
|
303 |
static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, |
8c4d4e8b5
|
304 |
u16 subsys_id, u32 genid) |
0628b123c
|
305 |
{ |
0f8162326
|
306 |
struct sk_buff *oskb = skb; |
0628b123c
|
307 308 309 |
struct net *net = sock_net(skb->sk); const struct nfnetlink_subsystem *ss; const struct nfnl_callback *nc; |
04ba724b6
|
310 |
struct netlink_ext_ack extack; |
4eba8b78e
|
311 |
LIST_HEAD(err_list); |
6742b9e31
|
312 |
u32 status; |
0628b123c
|
313 314 315 |
int err; if (subsys_id >= NFNL_SUBSYS_COUNT) |
2d4bc9336
|
316 |
return netlink_ack(skb, nlh, -EINVAL, NULL); |
0628b123c
|
317 |
replay: |
6742b9e31
|
318 |
status = 0; |
c0391b6ab
|
319 |
replay_abort: |
0f8162326
|
320 321 |
skb = netlink_skb_clone(oskb, GFP_KERNEL); if (!skb) |
2d4bc9336
|
322 |
return netlink_ack(oskb, nlh, -ENOMEM, NULL); |
0628b123c
|
323 |
|
0628b123c
|
324 |
nfnl_lock(subsys_id); |
9c55d3b54
|
325 |
ss = nfnl_dereference_protected(subsys_id); |
0628b123c
|
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
|
331 |
ss = nfnl_dereference_protected(subsys_id); |
0628b123c
|
332 333 334 335 |
if (!ss) #endif { nfnl_unlock(subsys_id); |
2d4bc9336
|
336 |
netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL); |
0f8162326
|
337 |
return kfree_skb(skb); |
0628b123c
|
338 339 |
} } |
ca2f18be7
|
340 |
if (!ss->valid_genid || !ss->commit || !ss->abort) { |
0628b123c
|
341 |
nfnl_unlock(subsys_id); |
2d4bc9336
|
342 |
netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL); |
ecd15dd7e
|
343 |
return kfree_skb(skb); |
0628b123c
|
344 |
} |
be2ab5b4d
|
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
|
350 |
if (!ss->valid_genid(net, genid)) { |
be2ab5b4d
|
351 |
module_put(ss->owner); |
8c4d4e8b5
|
352 |
nfnl_unlock(subsys_id); |
2d4bc9336
|
353 |
netlink_ack(oskb, nlh, -ERESTART, NULL); |
8c4d4e8b5
|
354 355 |
return kfree_skb(skb); } |
f102d66b3
|
356 |
nfnl_unlock(subsys_id); |
0628b123c
|
357 358 |
while (skb->len >= nlmsg_total_size(0)) { int msglen, type; |
8a3d4c361
|
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
|
365 |
memset(&extack, 0, sizeof(extack)); |
0628b123c
|
366 367 |
nlh = nlmsg_hdr(skb); err = 0; |
c58d6c936
|
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
|
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
|
385 |
nfnl_err_reset(&err_list); |
6742b9e31
|
386 |
status |= NFNL_BATCH_FAILURE; |
0628b123c
|
387 388 |
goto done; } else if (type == NFNL_MSG_BATCH_END) { |
6742b9e31
|
389 |
status |= NFNL_BATCH_DONE; |
0628b123c
|
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
|
412 |
u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type); |
7b7744e2a
|
413 |
struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1]; |
0628b123c
|
414 415 |
struct nlattr *attr = (void *)nlh + min_len; int attrlen = nlh->nlmsg_len - min_len; |
7b7744e2a
|
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
|
421 422 423 424 |
err = nla_parse_deprecated(cda, ss->cb[cb_id].attr_count, attr, attrlen, ss->cb[cb_id].policy, NULL); |
0628b123c
|
425 426 427 428 |
if (err < 0) goto ack; if (nc->call_batch) { |
633c9a840
|
429 |
err = nc->call_batch(net, net->nfnl, skb, nlh, |
04ba724b6
|
430 431 |
(const struct nlattr **)cda, &extack); |
0628b123c
|
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
|
439 |
status |= NFNL_BATCH_REPLAY; |
71ad00c50
|
440 |
goto done; |
0628b123c
|
441 442 443 444 |
} } ack: if (nlh->nlmsg_flags & NLM_F_ACK || err) { |
cbb8125eb
|
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
|
449 |
if (nfnl_err_add(&err_list, nlh, err, &extack) < 0) { |
cbb8125eb
|
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
|
455 456 |
netlink_ack(oskb, nlmsg_hdr(oskb), -ENOMEM, NULL); |
6742b9e31
|
457 |
status |= NFNL_BATCH_FAILURE; |
cbb8125eb
|
458 459 |
goto done; } |
0628b123c
|
460 461 462 463 |
/* We don't stop processing the batch on errors, thus, * userspace gets all the errors that the batch * triggers. */ |
0628b123c
|
464 |
if (err) |
6742b9e31
|
465 |
status |= NFNL_BATCH_FAILURE; |
0628b123c
|
466 |
} |
71ad00c50
|
467 |
|
0628b123c
|
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
|
474 |
if (status & NFNL_BATCH_REPLAY) { |
c0391b6ab
|
475 |
ss->abort(net, oskb, NFNL_ABORT_AUTOLOAD); |
6742b9e31
|
476 |
nfnl_err_reset(&err_list); |
6742b9e31
|
477 |
kfree_skb(skb); |
be2ab5b4d
|
478 |
module_put(ss->owner); |
6742b9e31
|
479 480 |
goto replay; } else if (status == NFNL_BATCH_DONE) { |
003087911
|
481 482 483 484 485 |
err = ss->commit(net, oskb); if (err == -EAGAIN) { status |= NFNL_BATCH_REPLAY; goto done; } else if (err) { |
c0391b6ab
|
486 |
ss->abort(net, oskb, NFNL_ABORT_NONE); |
003087911
|
487 488 |
netlink_ack(oskb, nlmsg_hdr(oskb), err, NULL); } |
6742b9e31
|
489 |
} else { |
c0391b6ab
|
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
|
505 |
} |
a654de8fd
|
506 507 |
if (ss->cleanup) ss->cleanup(net); |
0628b123c
|
508 |
|
cbb8125eb
|
509 |
nfnl_err_deliver(&err_list, oskb); |
0f8162326
|
510 |
kfree_skb(skb); |
be2ab5b4d
|
511 |
module_put(ss->owner); |
0628b123c
|
512 |
} |
8c4d4e8b5
|
513 514 515 |
static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = { [NFNL_BATCH_GENID] = { .type = NLA_U32 }, }; |
48656835c
|
516 |
static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh) |
f9e815b37
|
517 |
{ |
8c4d4e8b5
|
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
|
522 |
struct nfgenmsg *nfgenmsg; |
8c4d4e8b5
|
523 524 |
int msglen, err; u32 gen_id = 0; |
b745d0358
|
525 |
u16 res_id; |
0628b123c
|
526 |
|
48656835c
|
527 528 529 |
msglen = NLMSG_ALIGN(nlh->nlmsg_len); if (msglen > skb->len) msglen = skb->len; |
f55ce7b02
|
530 |
if (skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg)) |
48656835c
|
531 |
return; |
8cb081746
|
532 533 |
err = nla_parse_deprecated(cda, NFNL_BATCH_MAX, attr, attrlen, nfnl_batch_policy, NULL); |
8c4d4e8b5
|
534 |
if (err < 0) { |
2d4bc9336
|
535 |
netlink_ack(skb, nlh, err, NULL); |
8c4d4e8b5
|
536 537 538 539 |
return; } if (cda[NFNL_BATCH_GENID]) gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID])); |
48656835c
|
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
|
547 |
nfnetlink_rcv_batch(skb, nlh, res_id, gen_id); |
48656835c
|
548 549 550 551 552 |
} static void nfnetlink_rcv(struct sk_buff *skb) { struct nlmsghdr *nlh = nlmsg_hdr(skb); |
f55ce7b02
|
553 554 |
if (skb->len < NLMSG_HDRLEN || nlh->nlmsg_len < NLMSG_HDRLEN || |
0628b123c
|
555 556 |
skb->len < nlh->nlmsg_len) return; |
90f62cf30
|
557 |
if (!netlink_net_capable(skb, CAP_NET_ADMIN)) { |
2d4bc9336
|
558 |
netlink_ack(skb, nlh, -EPERM, NULL); |
cdbe7c2d6
|
559 560 |
return; } |
48656835c
|
561 562 563 |
if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN) nfnetlink_rcv_skb_batch(skb, nlh); else |
d4ef38354
|
564 |
netlink_rcv_skb(skb, nfnetlink_rcv_msg); |
f9e815b37
|
565 |
} |
03292745b
|
566 |
#ifdef CONFIG_MODULES |
023e2cfa3
|
567 |
static int nfnetlink_bind(struct net *net, int group) |
03292745b
|
568 569 |
{ const struct nfnetlink_subsystem *ss; |
97840cb67
|
570 571 572 |
int type; if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX) |
62924af24
|
573 |
return 0; |
97840cb67
|
574 575 |
type = nfnl_group2type[group]; |
03292745b
|
576 577 |
rcu_read_lock(); |
dbc3617f4
|
578 |
ss = nfnetlink_get_subsys(type << 8); |
03292745b
|
579 |
rcu_read_unlock(); |
bfe4bc71c
|
580 |
if (!ss) |
1b0890cd6
|
581 |
request_module_nowait("nfnetlink-subsys-%d", type); |
4f5209005
|
582 |
return 0; |
03292745b
|
583 584 |
} #endif |
cd8c20b65
|
585 |
static int __net_init nfnetlink_net_init(struct net *net) |
f9e815b37
|
586 |
{ |
cd8c20b65
|
587 |
struct sock *nfnl; |
a31f2d17b
|
588 589 590 |
struct netlink_kernel_cfg cfg = { .groups = NFNLGRP_MAX, .input = nfnetlink_rcv, |
03292745b
|
591 592 593 |
#ifdef CONFIG_MODULES .bind = nfnetlink_bind, #endif |
a31f2d17b
|
594 |
}; |
cd8c20b65
|
595 |
|
9f00d9776
|
596 |
nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg); |
cd8c20b65
|
597 598 599 |
if (!nfnl) return -ENOMEM; net->nfnl_stash = nfnl; |
cf778b00e
|
600 |
rcu_assign_pointer(net->nfnl, nfnl); |
cd8c20b65
|
601 |
return 0; |
f9e815b37
|
602 |
} |
cd8c20b65
|
603 |
static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list) |
f9e815b37
|
604 |
{ |
cd8c20b65
|
605 |
struct net *net; |
f9e815b37
|
606 |
|
cd8c20b65
|
607 |
list_for_each_entry(net, net_exit_list, exit_list) |
a9b3cd7f3
|
608 |
RCU_INIT_POINTER(net->nfnl, NULL); |
cd8c20b65
|
609 610 611 612 |
synchronize_net(); list_for_each_entry(net, net_exit_list, exit_list) netlink_kernel_release(net->nfnl_stash); } |
f9e815b37
|
613 |
|
cd8c20b65
|
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
|
621 |
int i; |
97840cb67
|
622 623 |
for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++) BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE); |
c14b78e7d
|
624 |
for (i=0; i<NFNL_SUBSYS_COUNT; i++) |
ab6c41eef
|
625 |
__mutex_init(&table[i].mutex, nfnl_lockdep_names[i], &nfnl_lockdep_keys[i]); |
c14b78e7d
|
626 |
|
cd8c20b65
|
627 |
return register_pernet_subsys(&nfnetlink_net_ops); |
f9e815b37
|
628 |
} |
cd8c20b65
|
629 630 |
static void __exit nfnetlink_exit(void) { |
cd8c20b65
|
631 632 |
unregister_pernet_subsys(&nfnetlink_net_ops); } |
f9e815b37
|
633 634 |
module_init(nfnetlink_init); module_exit(nfnetlink_exit); |