| 1 | |
| 2 | |
| 3 | * protocol helpers and general trouble making from userspace.
|
| 4 | *
|
| 5 | * (C) 2001 by Jay Schulist <[email protected]>,
|
| 6 | * (C) 2002-2005 by Harald Welte <[email protected]>
|
| 7 | * (C) 2005-2017 by Pablo Neira Ayuso <[email protected]>
|
| 8 | *
|
| 9 | * Initial netfilter messages via netlink development funded and
|
| 10 | * generally made possible by Network Robots, Inc. (www.networkrobots.com)
|
| 11 | *
|
| 12 | * Further development of this code funded by Astaro AG (http://www.astaro.com)
|
| 13 |
|
| 14 |
|
| 15 | #include <linux/module.h>
|
| 16 | #include <linux/types.h>
|
| 17 | #include <linux/socket.h>
|
| 18 | #include <linux/kernel.h>
|
| 19 | #include <linux/string.h>
|
| 20 | #include <linux/sockios.h>
|
| 21 | #include <linux/net.h>
|
| 22 | #include <linux/skbuff.h>
|
| 23 | #include <linux/uaccess.h>
|
| 24 | #include <net/sock.h>
|
| 25 | #include <linux/init.h>
|
| 26 | #include <linux/sched/signal.h>
|
| 27 |
|
| 28 | #include <net/netlink.h>
|
| 29 | #include <net/netns/generic.h>
|
| 30 | #include <linux/netfilter.h>
|
| 31 | #include <linux/netfilter/nfnetlink.h>
|
| 32 |
|
| 33 | MODULE_LICENSE("GPL");
|
| 34 | MODULE_AUTHOR("Harald Welte <[email protected]>");
|
| 35 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
|
| 36 | MODULE_DESCRIPTION("Netfilter messages via netlink socket");
|
| 37 |
|
| 38 | #define nfnl_dereference_protected(id) \
|
| 39 | rcu_dereference_protected(table[(id)].subsys, \
|
| 40 | lockdep_nfnl_is_held((id)))
|
| 41 |
|
| 42 | #define NFNL_MAX_ATTR_COUNT 32
|
| 43 |
|
| 44 | static unsigned int nfnetlink_pernet_id __read_mostly;
|
| 45 |
|
| 46 | #ifdef CONFIG_NF_CONNTRACK_EVENTS
|
| 47 | static DEFINE_SPINLOCK(nfnl_grp_active_lock);
|
| 48 | #endif
|
| 49 |
|
| 50 | struct nfnl_net {
|
| 51 | struct sock *nfnl;
|
| 52 | };
|
| 53 |
|
| 54 | static struct {
|
| 55 | struct mutex mutex;
|
| 56 | const struct nfnetlink_subsystem __rcu *subsys;
|
| 57 | } table[NFNL_SUBSYS_COUNT];
|
| 58 |
|
| 59 | static struct lock_class_key nfnl_lockdep_keys[NFNL_SUBSYS_COUNT];
|
| 60 |
|
| 61 | static const char *const nfnl_lockdep_names[NFNL_SUBSYS_COUNT] = {
|
| 62 | [NFNL_SUBSYS_NONE] = "nfnl_subsys_none",
|
| 63 | [NFNL_SUBSYS_CTNETLINK] = "nfnl_subsys_ctnetlink",
|
| 64 | [NFNL_SUBSYS_CTNETLINK_EXP] = "nfnl_subsys_ctnetlink_exp",
|
| 65 | [NFNL_SUBSYS_QUEUE] = "nfnl_subsys_queue",
|
| 66 | [NFNL_SUBSYS_ULOG] = "nfnl_subsys_ulog",
|
| 67 | [NFNL_SUBSYS_OSF] = "nfnl_subsys_osf",
|
| 68 | [NFNL_SUBSYS_IPSET] = "nfnl_subsys_ipset",
|
| 69 | [NFNL_SUBSYS_ACCT] = "nfnl_subsys_acct",
|
| 70 | [NFNL_SUBSYS_CTNETLINK_TIMEOUT] = "nfnl_subsys_cttimeout",
|
| 71 | [NFNL_SUBSYS_CTHELPER] = "nfnl_subsys_cthelper",
|
| 72 | [NFNL_SUBSYS_NFTABLES] = "nfnl_subsys_nftables",
|
| 73 | [NFNL_SUBSYS_NFT_COMPAT] = "nfnl_subsys_nftcompat",
|
| 74 | [NFNL_SUBSYS_HOOK] = "nfnl_subsys_hook",
|
| 75 | };
|
| 76 |
|
| 77 | static const int nfnl_group2type[NFNLGRP_MAX+1] = {
|
| 78 | [NFNLGRP_CONNTRACK_NEW] = NFNL_SUBSYS_CTNETLINK,
|
| 79 | [NFNLGRP_CONNTRACK_UPDATE] = NFNL_SUBSYS_CTNETLINK,
|
| 80 | [NFNLGRP_CONNTRACK_DESTROY] = NFNL_SUBSYS_CTNETLINK,
|
| 81 | [NFNLGRP_CONNTRACK_EXP_NEW] = NFNL_SUBSYS_CTNETLINK_EXP,
|
| 82 | [NFNLGRP_CONNTRACK_EXP_UPDATE] = NFNL_SUBSYS_CTNETLINK_EXP,
|
| 83 | [NFNLGRP_CONNTRACK_EXP_DESTROY] = NFNL_SUBSYS_CTNETLINK_EXP,
|
| 84 | [NFNLGRP_NFTABLES] = NFNL_SUBSYS_NFTABLES,
|
| 85 | [NFNLGRP_ACCT_QUOTA] = NFNL_SUBSYS_ACCT,
|
| 86 | [NFNLGRP_NFTRACE] = NFNL_SUBSYS_NFTABLES,
|
| 87 | };
|
| 88 |
|
| 89 | static struct nfnl_net *nfnl_pernet(struct net *net)
|
| 90 | {
|
| 91 | return net_generic(net, nfnetlink_pernet_id);
|
| 92 | }
|
| 93 |
|
| 94 | void nfnl_lock(__u8 subsys_id)
|
| 95 | {
|
| 96 | mutex_lock(&table[subsys_id].mutex);
|
| 97 | }
|
| 98 | EXPORT_SYMBOL_GPL(nfnl_lock);
|
| 99 |
|
| 100 | void nfnl_unlock(__u8 subsys_id)
|
| 101 | {
|
| 102 | mutex_unlock(&table[subsys_id].mutex);
|
| 103 | }
|
| 104 | EXPORT_SYMBOL_GPL(nfnl_unlock);
|
| 105 |
|
| 106 | #ifdef CONFIG_PROVE_LOCKING
|
| 107 | bool lockdep_nfnl_is_held(u8 subsys_id)
|
| 108 | {
|
| 109 | return lockdep_is_held(&table[subsys_id].mutex);
|
| 110 | }
|
| 111 | EXPORT_SYMBOL_GPL(lockdep_nfnl_is_held);
|
| 112 | #endif
|
| 113 |
|
| 114 | int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
|
| 115 | {
|
| 116 | u8 cb_id;
|
| 117 |
|
| 118 |
|
| 119 | for (cb_id = 0; cb_id < n->cb_count; cb_id++)
|
| 120 | if (WARN_ON(n->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT))
|
| 121 | return -EINVAL;
|
| 122 |
|
| 123 | nfnl_lock(n->subsys_id);
|
| 124 | if (table[n->subsys_id].subsys) {
|
| 125 | nfnl_unlock(n->subsys_id);
|
| 126 | return -EBUSY;
|
| 127 | }
|
| 128 | rcu_assign_pointer(table[n->subsys_id].subsys, n);
|
| 129 | nfnl_unlock(n->subsys_id);
|
| 130 |
|
| 131 | return 0;
|
| 132 | }
|
| 133 | EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
|
| 134 |
|
| 135 | int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
|
| 136 | {
|
| 137 | nfnl_lock(n->subsys_id);
|
| 138 | table[n->subsys_id].subsys = NULL;
|
| 139 | nfnl_unlock(n->subsys_id);
|
| 140 | synchronize_rcu();
|
| 141 | return 0;
|
| 142 | }
|
| 143 | EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
|
| 144 |
|
| 145 | static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u16 type)
|
| 146 | {
|
| 147 | u8 subsys_id = NFNL_SUBSYS_ID(type);
|
| 148 |
|
| 149 | if (subsys_id >= NFNL_SUBSYS_COUNT)
|
| 150 | return NULL;
|
| 151 |
|
| 152 | return rcu_dereference(table[subsys_id].subsys);
|
| 153 | }
|
| 154 |
|
| 155 | static inline const struct nfnl_callback *
|
| 156 | nfnetlink_find_client(u16 type, const struct nfnetlink_subsystem *ss)
|
| 157 | {
|
| 158 | u8 cb_id = NFNL_MSG_TYPE(type);
|
| 159 |
|
| 160 | if (cb_id >= ss->cb_count)
|
| 161 | return NULL;
|
| 162 |
|
| 163 | return &ss->cb[cb_id];
|
| 164 | }
|
| 165 |
|
| 166 | int nfnetlink_has_listeners(struct net *net, unsigned int group)
|
| 167 | {
|
| 168 | struct nfnl_net *nfnlnet = nfnl_pernet(net);
|
| 169 |
|
| 170 | return netlink_has_listeners(nfnlnet->nfnl, group);
|
| 171 | }
|
| 172 | EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
|
| 173 |
|
| 174 | int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid,
|
| 175 | unsigned int group, int echo, gfp_t flags)
|
| 176 | {
|
| 177 | struct nfnl_net *nfnlnet = nfnl_pernet(net);
|
| 178 |
|
| 179 | return nlmsg_notify(nfnlnet->nfnl, skb, portid, group, echo, flags);
|
| 180 | }
|
| 181 | EXPORT_SYMBOL_GPL(nfnetlink_send);
|
| 182 |
|
| 183 | int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error)
|
| 184 | {
|
| 185 | struct nfnl_net *nfnlnet = nfnl_pernet(net);
|
| 186 |
|
| 187 | return netlink_set_err(nfnlnet->nfnl, portid, group, error);
|
| 188 | }
|
| 189 | EXPORT_SYMBOL_GPL(nfnetlink_set_err);
|
| 190 |
|
| 191 | int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid)
|
| 192 | {
|
| 193 | struct nfnl_net *nfnlnet = nfnl_pernet(net);
|
| 194 | int err;
|
| 195 |
|
| 196 | err = nlmsg_unicast(nfnlnet->nfnl, skb, portid);
|
| 197 | if (err == -EAGAIN)
|
| 198 | err = -ENOBUFS;
|
| 199 |
|
| 200 | return err;
|
| 201 | }
|
| 202 | EXPORT_SYMBOL_GPL(nfnetlink_unicast);
|
| 203 |
|
| 204 | void nfnetlink_broadcast(struct net *net, struct sk_buff *skb, __u32 portid,
|
| 205 | __u32 group, gfp_t allocation)
|
| 206 | {
|
| 207 | struct nfnl_net *nfnlnet = nfnl_pernet(net);
|
| 208 |
|
| 209 | netlink_broadcast(nfnlnet->nfnl, skb, portid, group, allocation);
|
| 210 | }
|
| 211 | EXPORT_SYMBOL_GPL(nfnetlink_broadcast);
|
| 212 |
|
| 213 |
|
| 214 | static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
|
| 215 | struct netlink_ext_ack *extack)
|
| 216 | {
|
| 217 | struct net *net = sock_net(skb->sk);
|
| 218 | const struct nfnl_callback *nc;
|
| 219 | const struct nfnetlink_subsystem *ss;
|
| 220 | int type, err;
|
| 221 |
|
| 222 |
|
| 223 | if (nlmsg_len(nlh) < sizeof(struct nfgenmsg))
|
| 224 | return 0;
|
| 225 |
|
| 226 | type = nlh->nlmsg_type;
|
| 227 | replay:
|
| 228 | rcu_read_lock();
|
| 229 |
|
| 230 | ss = nfnetlink_get_subsys(type);
|
| 231 | if (!ss) {
|
| 232 | #ifdef CONFIG_MODULES
|
| 233 | rcu_read_unlock();
|
| 234 | request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
|
| 235 | rcu_read_lock();
|
| 236 | ss = nfnetlink_get_subsys(type);
|
| 237 | if (!ss)
|
| 238 | #endif
|
| 239 | {
|
| 240 | rcu_read_unlock();
|
| 241 | return -EINVAL;
|
| 242 | }
|
| 243 | }
|
| 244 |
|
| 245 | nc = nfnetlink_find_client(type, ss);
|
| 246 | if (!nc) {
|
| 247 | rcu_read_unlock();
|
| 248 | return -EINVAL;
|
| 249 | }
|
| 250 |
|
| 251 | {
|
| 252 | int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
|
| 253 | struct nfnl_net *nfnlnet = nfnl_pernet(net);
|
| 254 | u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
|
| 255 | struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
|
| 256 | struct nlattr *attr = (void *)nlh + min_len;
|
| 257 | int attrlen = nlh->nlmsg_len - min_len;
|
| 258 | __u8 subsys_id = NFNL_SUBSYS_ID(type);
|
| 259 | struct nfnl_info info = {
|
| 260 | .net = net,
|
| 261 | .sk = nfnlnet->nfnl,
|
| 262 | .nlh = nlh,
|
| 263 | .nfmsg = nlmsg_data(nlh),
|
| 264 | .extack = extack,
|
| 265 | };
|
| 266 |
|
| 267 |
|
| 268 | if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
|
| 269 | rcu_read_unlock();
|
| 270 | return -ENOMEM;
|
| 271 | }
|
| 272 |
|
| 273 | err = nla_parse_deprecated(cda, ss->cb[cb_id].attr_count,
|
| 274 | attr, attrlen,
|
| 275 | ss->cb[cb_id].policy, extack);
|
| 276 | if (err < 0) {
|
| 277 | rcu_read_unlock();
|
| 278 | return err;
|
| 279 | }
|
| 280 |
|
| 281 | if (!nc->call) {
|
| 282 | rcu_read_unlock();
|
| 283 | return -EINVAL;
|
| 284 | }
|
| 285 |
|
| 286 | switch (nc->type) {
|
| 287 | case NFNL_CB_RCU:
|
| 288 | err = nc->call(skb, &info, (const struct nlattr **)cda);
|
| 289 | rcu_read_unlock();
|
| 290 | break;
|
| 291 | case NFNL_CB_MUTEX:
|
| 292 | rcu_read_unlock();
|
| 293 | nfnl_lock(subsys_id);
|
| 294 | if (nfnl_dereference_protected(subsys_id) != ss ||
|
| 295 | nfnetlink_find_client(type, ss) != nc) {
|
| 296 | nfnl_unlock(subsys_id);
|
| 297 | err = -EAGAIN;
|
| 298 | break;
|
| 299 | }
|
| 300 | err = nc->call(skb, &info, (const struct nlattr **)cda);
|
| 301 | nfnl_unlock(subsys_id);
|
| 302 | break;
|
| 303 | default:
|
| 304 | rcu_read_unlock();
|
| 305 | err = -EINVAL;
|
| 306 | break;
|
| 307 | }
|
| 308 | if (err == -EAGAIN)
|
| 309 | goto replay;
|
| 310 | return err;
|
| 311 | }
|
| 312 | }
|
| 313 |
|
| 314 | struct nfnl_err {
|
| 315 | struct list_head head;
|
| 316 | struct nlmsghdr *nlh;
|
| 317 | int err;
|
| 318 | struct netlink_ext_ack extack;
|
| 319 | };
|
| 320 |
|
| 321 | static int nfnl_err_add(struct list_head *list, struct nlmsghdr *nlh, int err,
|
| 322 | const struct netlink_ext_ack *extack)
|
| 323 | {
|
| 324 | struct nfnl_err *nfnl_err;
|
| 325 |
|
| 326 | nfnl_err = kmalloc_obj(struct nfnl_err);
|
| 327 | if (nfnl_err == NULL)
|
| 328 | return -ENOMEM;
|
| 329 |
|
| 330 | nfnl_err->nlh = nlh;
|
| 331 | nfnl_err->err = err;
|
| 332 | nfnl_err->extack = *extack;
|
| 333 | list_add_tail(&nfnl_err->head, list);
|
| 334 |
|
| 335 | return 0;
|
| 336 | }
|
| 337 |
|
| 338 | static void nfnl_err_del(struct nfnl_err *nfnl_err)
|
| 339 | {
|
| 340 | list_del(&nfnl_err->head);
|
| 341 | kfree(nfnl_err);
|
| 342 | }
|
| 343 |
|
| 344 | static void nfnl_err_reset(struct list_head *err_list)
|
| 345 | {
|
| 346 | struct nfnl_err *nfnl_err, *next;
|
| 347 |
|
| 348 | list_for_each_entry_safe(nfnl_err, next, err_list, head)
|
| 349 | nfnl_err_del(nfnl_err);
|
| 350 | }
|
| 351 |
|
| 352 | static void nfnl_err_deliver(struct list_head *err_list, struct sk_buff *skb)
|
| 353 | {
|
| 354 | struct nfnl_err *nfnl_err, *next;
|
| 355 |
|
| 356 | list_for_each_entry_safe(nfnl_err, next, err_list, head) {
|
| 357 | netlink_ack(skb, nfnl_err->nlh, nfnl_err->err,
|
| 358 | &nfnl_err->extack);
|
| 359 | nfnl_err_del(nfnl_err);
|
| 360 | }
|
| 361 | }
|
| 362 |
|
| 363 | enum {
|
| 364 | NFNL_BATCH_FAILURE = (1 << 0),
|
| 365 | NFNL_BATCH_DONE = (1 << 1),
|
| 366 | NFNL_BATCH_REPLAY = (1 << 2),
|
| 367 | };
|
| 368 |
|
| 369 | static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
|
| 370 | u16 subsys_id, u32 genid)
|
| 371 | {
|
| 372 | struct sk_buff *oskb = skb;
|
| 373 | struct net *net = sock_net(skb->sk);
|
| 374 | const struct nfnetlink_subsystem *ss;
|
| 375 | const struct nfnl_callback *nc;
|
| 376 | struct netlink_ext_ack extack;
|
| 377 | struct nlmsghdr *onlh = nlh;
|
| 378 | LIST_HEAD(err_list);
|
| 379 | u32 status;
|
| 380 | int err;
|
| 381 |
|
| 382 | if (subsys_id >= NFNL_SUBSYS_COUNT)
|
| 383 | return netlink_ack(skb, nlh, -EINVAL, NULL);
|
| 384 | replay:
|
| 385 | status = 0;
|
| 386 | replay_abort:
|
| 387 | skb = netlink_skb_clone(oskb, GFP_KERNEL);
|
| 388 | nlh = onlh;
|
| 389 | if (!skb)
|
| 390 | return netlink_ack(oskb, nlh, -ENOMEM, NULL);
|
| 391 |
|
| 392 | nfnl_lock(subsys_id);
|
| 393 | ss = nfnl_dereference_protected(subsys_id);
|
| 394 | if (!ss) {
|
| 395 | #ifdef CONFIG_MODULES
|
| 396 | nfnl_unlock(subsys_id);
|
| 397 | request_module("nfnetlink-subsys-%d", subsys_id);
|
| 398 | nfnl_lock(subsys_id);
|
| 399 | ss = nfnl_dereference_protected(subsys_id);
|
| 400 | if (!ss)
|
| 401 | #endif
|
| 402 | {
|
| 403 | nfnl_unlock(subsys_id);
|
| 404 | netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
|
| 405 | return consume_skb(skb);
|
| 406 | }
|
| 407 | }
|
| 408 |
|
| 409 | if (!ss->valid_genid || !ss->commit || !ss->abort) {
|
| 410 | nfnl_unlock(subsys_id);
|
| 411 | netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
|
| 412 | return consume_skb(skb);
|
| 413 | }
|
| 414 |
|
| 415 | if (!try_module_get(ss->owner)) {
|
| 416 | nfnl_unlock(subsys_id);
|
| 417 | netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
|
| 418 | return consume_skb(skb);
|
| 419 | }
|
| 420 |
|
| 421 | if (!ss->valid_genid(net, genid)) {
|
| 422 | module_put(ss->owner);
|
| 423 | nfnl_unlock(subsys_id);
|
| 424 | netlink_ack(oskb, nlh, -ERESTART, NULL);
|
| 425 | return consume_skb(skb);
|
| 426 | }
|
| 427 |
|
| 428 | nfnl_unlock(subsys_id);
|
| 429 |
|
| 430 | if (nlh->nlmsg_flags & NLM_F_ACK) {
|
| 431 | memset(&extack, 0, sizeof(extack));
|
| 432 | nfnl_err_add(&err_list, nlh, 0, &extack);
|
| 433 | }
|
| 434 |
|
| 435 | while (skb->len >= nlmsg_total_size(0)) {
|
| 436 | int msglen, type;
|
| 437 |
|
| 438 | if (fatal_signal_pending(current)) {
|
| 439 | nfnl_err_reset(&err_list);
|
| 440 | err = -EINTR;
|
| 441 | status = NFNL_BATCH_FAILURE;
|
| 442 | goto done;
|
| 443 | }
|
| 444 |
|
| 445 | memset(&extack, 0, sizeof(extack));
|
| 446 | nlh = nlmsg_hdr(skb);
|
| 447 | err = 0;
|
| 448 |
|
| 449 | if (nlh->nlmsg_len < NLMSG_HDRLEN ||
|
| 450 | skb->len < nlh->nlmsg_len ||
|
| 451 | nlmsg_len(nlh) < sizeof(struct nfgenmsg)) {
|
| 452 | nfnl_err_reset(&err_list);
|
| 453 | status |= NFNL_BATCH_FAILURE;
|
| 454 | goto done;
|
| 455 | }
|
| 456 |
|
| 457 |
|
| 458 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
|
| 459 | err = -EINVAL;
|
| 460 | goto ack;
|
| 461 | }
|
| 462 |
|
| 463 | type = nlh->nlmsg_type;
|
| 464 | if (type == NFNL_MSG_BATCH_BEGIN) {
|
| 465 |
|
| 466 | nfnl_err_reset(&err_list);
|
| 467 | status |= NFNL_BATCH_FAILURE;
|
| 468 | goto done;
|
| 469 | } else if (type == NFNL_MSG_BATCH_END) {
|
| 470 | status |= NFNL_BATCH_DONE;
|
| 471 | goto done;
|
| 472 | } else if (type < NLMSG_MIN_TYPE) {
|
| 473 | err = -EINVAL;
|
| 474 | goto ack;
|
| 475 | }
|
| 476 |
|
| 477 | |
| 478 | * subsystem.
|
| 479 |
|
| 480 | if (NFNL_SUBSYS_ID(type) != subsys_id) {
|
| 481 | err = -EINVAL;
|
| 482 | goto ack;
|
| 483 | }
|
| 484 |
|
| 485 | nc = nfnetlink_find_client(type, ss);
|
| 486 | if (!nc) {
|
| 487 | err = -EINVAL;
|
| 488 | goto ack;
|
| 489 | }
|
| 490 |
|
| 491 | if (nc->type != NFNL_CB_BATCH) {
|
| 492 | err = -EINVAL;
|
| 493 | goto ack;
|
| 494 | }
|
| 495 |
|
| 496 | {
|
| 497 | int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
|
| 498 | struct nfnl_net *nfnlnet = nfnl_pernet(net);
|
| 499 | struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
|
| 500 | struct nlattr *attr = (void *)nlh + min_len;
|
| 501 | u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
|
| 502 | int attrlen = nlh->nlmsg_len - min_len;
|
| 503 | struct nfnl_info info = {
|
| 504 | .net = net,
|
| 505 | .sk = nfnlnet->nfnl,
|
| 506 | .nlh = nlh,
|
| 507 | .nfmsg = nlmsg_data(nlh),
|
| 508 | .extack = &extack,
|
| 509 | };
|
| 510 |
|
| 511 |
|
| 512 | if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
|
| 513 | err = -ENOMEM;
|
| 514 | goto ack;
|
| 515 | }
|
| 516 |
|
| 517 | err = nla_parse_deprecated(cda,
|
| 518 | ss->cb[cb_id].attr_count,
|
| 519 | attr, attrlen,
|
| 520 | ss->cb[cb_id].policy, &extack);
|
| 521 | if (err < 0)
|
| 522 | goto ack;
|
| 523 |
|
| 524 | err = nc->call(skb, &info, (const struct nlattr **)cda);
|
| 525 |
|
| 526 | |
| 527 | * have to abort and start from scratch using the
|
| 528 | * original skb.
|
| 529 |
|
| 530 | if (err == -EAGAIN) {
|
| 531 | status |= NFNL_BATCH_REPLAY;
|
| 532 | goto done;
|
| 533 | }
|
| 534 | }
|
| 535 | ack:
|
| 536 | if (nlh->nlmsg_flags & NLM_F_ACK || err) {
|
| 537 | |
| 538 | * processed, this avoids that the same error is
|
| 539 | * reported several times when replaying the batch.
|
| 540 |
|
| 541 | if (err == -ENOMEM ||
|
| 542 | nfnl_err_add(&err_list, nlh, err, &extack) < 0) {
|
| 543 | |
| 544 | * list of errors and send OOM to userspace
|
| 545 | * pointing to the batch header.
|
| 546 |
|
| 547 | nfnl_err_reset(&err_list);
|
| 548 | netlink_ack(oskb, nlmsg_hdr(oskb), -ENOMEM,
|
| 549 | NULL);
|
| 550 | status |= NFNL_BATCH_FAILURE;
|
| 551 | goto done;
|
| 552 | }
|
| 553 | |
| 554 | * userspace gets all the errors that the batch
|
| 555 | * triggers.
|
| 556 |
|
| 557 | if (err)
|
| 558 | status |= NFNL_BATCH_FAILURE;
|
| 559 | }
|
| 560 |
|
| 561 | msglen = NLMSG_ALIGN(nlh->nlmsg_len);
|
| 562 | if (msglen > skb->len)
|
| 563 | msglen = skb->len;
|
| 564 | skb_pull(skb, msglen);
|
| 565 | }
|
| 566 | done:
|
| 567 | if (status & NFNL_BATCH_REPLAY) {
|
| 568 | ss->abort(net, oskb, NFNL_ABORT_AUTOLOAD);
|
| 569 | nfnl_err_reset(&err_list);
|
| 570 | consume_skb(skb);
|
| 571 | module_put(ss->owner);
|
| 572 | goto replay;
|
| 573 | } else if (status == NFNL_BATCH_DONE) {
|
| 574 | err = ss->commit(net, oskb);
|
| 575 | if (err == -EAGAIN) {
|
| 576 | status |= NFNL_BATCH_REPLAY;
|
| 577 | goto done;
|
| 578 | } else if (err) {
|
| 579 | ss->abort(net, oskb, NFNL_ABORT_NONE);
|
| 580 | netlink_ack(oskb, nlmsg_hdr(oskb), err, NULL);
|
| 581 | } else if (nlh->nlmsg_flags & NLM_F_ACK) {
|
| 582 | memset(&extack, 0, sizeof(extack));
|
| 583 | nfnl_err_add(&err_list, nlh, 0, &extack);
|
| 584 | }
|
| 585 | } else {
|
| 586 | enum nfnl_abort_action abort_action;
|
| 587 |
|
| 588 | if (status & NFNL_BATCH_FAILURE)
|
| 589 | abort_action = NFNL_ABORT_NONE;
|
| 590 | else
|
| 591 | abort_action = NFNL_ABORT_VALIDATE;
|
| 592 |
|
| 593 | err = ss->abort(net, oskb, abort_action);
|
| 594 | if (err == -EAGAIN) {
|
| 595 | nfnl_err_reset(&err_list);
|
| 596 | consume_skb(skb);
|
| 597 | module_put(ss->owner);
|
| 598 | status |= NFNL_BATCH_FAILURE;
|
| 599 | goto replay_abort;
|
| 600 | }
|
| 601 | }
|
| 602 |
|
| 603 | nfnl_err_deliver(&err_list, oskb);
|
| 604 | consume_skb(skb);
|
| 605 | module_put(ss->owner);
|
| 606 | }
|
| 607 |
|
| 608 | static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = {
|
| 609 | [NFNL_BATCH_GENID] = { .type = NLA_U32 },
|
| 610 | };
|
| 611 |
|
| 612 | static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)
|
| 613 | {
|
| 614 | int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
|
| 615 | struct nlattr *attr = (void *)nlh + min_len;
|
| 616 | struct nlattr *cda[NFNL_BATCH_MAX + 1];
|
| 617 | int attrlen = nlh->nlmsg_len - min_len;
|
| 618 | struct nfgenmsg *nfgenmsg;
|
| 619 | int msglen, err;
|
| 620 | u32 gen_id = 0;
|
| 621 | u16 res_id;
|
| 622 |
|
| 623 | msglen = NLMSG_ALIGN(nlh->nlmsg_len);
|
| 624 | if (msglen > skb->len)
|
| 625 | msglen = skb->len;
|
| 626 |
|
| 627 | if (skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))
|
| 628 | return;
|
| 629 |
|
| 630 | err = nla_parse_deprecated(cda, NFNL_BATCH_MAX, attr, attrlen,
|
| 631 | nfnl_batch_policy, NULL);
|
| 632 | if (err < 0) {
|
| 633 | netlink_ack(skb, nlh, err, NULL);
|
| 634 | return;
|
| 635 | }
|
| 636 | if (cda[NFNL_BATCH_GENID])
|
| 637 | gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID]));
|
| 638 |
|
| 639 | nfgenmsg = nlmsg_data(nlh);
|
| 640 | skb_pull(skb, msglen);
|
| 641 |
|
| 642 | if (nfgenmsg->res_id == (__force __be16)NFNL_SUBSYS_NFTABLES)
|
| 643 | res_id = NFNL_SUBSYS_NFTABLES;
|
| 644 | else
|
| 645 | res_id = ntohs(nfgenmsg->res_id);
|
| 646 |
|
| 647 | nfnetlink_rcv_batch(skb, nlh, res_id, gen_id);
|
| 648 | }
|
| 649 |
|
| 650 | static void nfnetlink_rcv(struct sk_buff *skb)
|
| 651 | {
|
| 652 | struct nlmsghdr *nlh = nlmsg_hdr(skb);
|
| 653 |
|
| 654 | if (skb->len < NLMSG_HDRLEN ||
|
| 655 | nlh->nlmsg_len < NLMSG_HDRLEN ||
|
| 656 | skb->len < nlh->nlmsg_len)
|
| 657 | return;
|
| 658 |
|
| 659 | if (!netlink_net_capable(skb, CAP_NET_ADMIN)) {
|
| 660 | netlink_ack(skb, nlh, -EPERM, NULL);
|
| 661 | return;
|
| 662 | }
|
| 663 |
|
| 664 | if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN)
|
| 665 | nfnetlink_rcv_skb_batch(skb, nlh);
|
| 666 | else
|
| 667 | netlink_rcv_skb(skb, nfnetlink_rcv_msg);
|
| 668 | }
|
| 669 |
|
| 670 | static void nfnetlink_bind_event(struct net *net, unsigned int group)
|
| 671 | {
|
| 672 | #ifdef CONFIG_NF_CONNTRACK_EVENTS
|
| 673 | int type, group_bit;
|
| 674 | u8 v;
|
| 675 |
|
| 676 | |
| 677 | * The other groups are not relevant and can be ignored.
|
| 678 |
|
| 679 | if (group >= 8)
|
| 680 | return;
|
| 681 |
|
| 682 | type = nfnl_group2type[group];
|
| 683 |
|
| 684 | switch (type) {
|
| 685 | case NFNL_SUBSYS_CTNETLINK:
|
| 686 | break;
|
| 687 | case NFNL_SUBSYS_CTNETLINK_EXP:
|
| 688 | break;
|
| 689 | default:
|
| 690 | return;
|
| 691 | }
|
| 692 |
|
| 693 | group_bit = (1 << group);
|
| 694 |
|
| 695 | spin_lock(&nfnl_grp_active_lock);
|
| 696 | v = READ_ONCE(nf_ctnetlink_has_listener);
|
| 697 | if ((v & group_bit) == 0) {
|
| 698 | v |= group_bit;
|
| 699 |
|
| 700 |
|
| 701 | WRITE_ONCE(nf_ctnetlink_has_listener, v);
|
| 702 | }
|
| 703 |
|
| 704 | spin_unlock(&nfnl_grp_active_lock);
|
| 705 | #endif
|
| 706 | }
|
| 707 |
|
| 708 | static int nfnetlink_bind(struct net *net, int group)
|
| 709 | {
|
| 710 | const struct nfnetlink_subsystem *ss;
|
| 711 | int type;
|
| 712 |
|
| 713 | if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX)
|
| 714 | return 0;
|
| 715 |
|
| 716 | type = nfnl_group2type[group];
|
| 717 |
|
| 718 | rcu_read_lock();
|
| 719 | ss = nfnetlink_get_subsys(type << 8);
|
| 720 | rcu_read_unlock();
|
| 721 | if (!ss)
|
| 722 | request_module_nowait("nfnetlink-subsys-%d", type);
|
| 723 |
|
| 724 | nfnetlink_bind_event(net, group);
|
| 725 | return 0;
|
| 726 | }
|
| 727 |
|
| 728 | static void nfnetlink_unbind(struct net *net, int group)
|
| 729 | {
|
| 730 | #ifdef CONFIG_NF_CONNTRACK_EVENTS
|
| 731 | int type, group_bit;
|
| 732 |
|
| 733 | if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX)
|
| 734 | return;
|
| 735 |
|
| 736 | type = nfnl_group2type[group];
|
| 737 |
|
| 738 | switch (type) {
|
| 739 | case NFNL_SUBSYS_CTNETLINK:
|
| 740 | break;
|
| 741 | case NFNL_SUBSYS_CTNETLINK_EXP:
|
| 742 | break;
|
| 743 | default:
|
| 744 | return;
|
| 745 | }
|
| 746 |
|
| 747 |
|
| 748 | if (group >= 8)
|
| 749 | return;
|
| 750 |
|
| 751 | group_bit = (1 << group);
|
| 752 |
|
| 753 | spin_lock(&nfnl_grp_active_lock);
|
| 754 | if (!nfnetlink_has_listeners(net, group)) {
|
| 755 | u8 v = READ_ONCE(nf_ctnetlink_has_listener);
|
| 756 |
|
| 757 | v &= ~group_bit;
|
| 758 |
|
| 759 |
|
| 760 | WRITE_ONCE(nf_ctnetlink_has_listener, v);
|
| 761 | }
|
| 762 | spin_unlock(&nfnl_grp_active_lock);
|
| 763 | #endif
|
| 764 | }
|
| 765 |
|
| 766 | static int __net_init nfnetlink_net_init(struct net *net)
|
| 767 | {
|
| 768 | struct nfnl_net *nfnlnet = nfnl_pernet(net);
|
| 769 | struct netlink_kernel_cfg cfg = {
|
| 770 | .groups = NFNLGRP_MAX,
|
| 771 | .input = nfnetlink_rcv,
|
| 772 | .bind = nfnetlink_bind,
|
| 773 | .unbind = nfnetlink_unbind,
|
| 774 | };
|
| 775 |
|
| 776 | nfnlnet->nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg);
|
| 777 | if (!nfnlnet->nfnl)
|
| 778 | return -ENOMEM;
|
| 779 | return 0;
|
| 780 | }
|
| 781 |
|
| 782 | static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
|
| 783 | {
|
| 784 | struct nfnl_net *nfnlnet;
|
| 785 | struct net *net;
|
| 786 |
|
| 787 | list_for_each_entry(net, net_exit_list, exit_list) {
|
| 788 | nfnlnet = nfnl_pernet(net);
|
| 789 |
|
| 790 | netlink_kernel_release(nfnlnet->nfnl);
|
| 791 | }
|
| 792 | }
|
| 793 |
|
| 794 | static struct pernet_operations nfnetlink_net_ops = {
|
| 795 | .init = nfnetlink_net_init,
|
| 796 | .exit_batch = nfnetlink_net_exit_batch,
|
| 797 | .id = &nfnetlink_pernet_id,
|
| 798 | .size = sizeof(struct nfnl_net),
|
| 799 | };
|
| 800 |
|
| 801 | static int __init nfnetlink_init(void)
|
| 802 | {
|
| 803 | int i;
|
| 804 |
|
| 805 | for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++)
|
| 806 | BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE);
|
| 807 |
|
| 808 | for (i=0; i<NFNL_SUBSYS_COUNT; i++)
|
| 809 | __mutex_init(&table[i].mutex, nfnl_lockdep_names[i], &nfnl_lockdep_keys[i]);
|
| 810 |
|
| 811 | return register_pernet_subsys(&nfnetlink_net_ops);
|
| 812 | }
|
| 813 |
|
| 814 | static void __exit nfnetlink_exit(void)
|
| 815 | {
|
| 816 | unregister_pernet_subsys(&nfnetlink_net_ops);
|
| 817 | }
|
| 818 | module_init(nfnetlink_init);
|
| 819 | module_exit(nfnetlink_exit);
|