| 1 | /* Connection tracking via netlink socket. Allows for user space |
| 2 | * protocol helpers and general trouble making from userspace. |
| 3 | * |
| 4 | * (C) 2001 by Jay Schulist <jschlst@samba.org> |
| 5 | * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org> |
| 6 | * (C) 2003 by Patrick Mchardy <kaber@trash.net> |
| 7 | * (C) 2005-2007 by Pablo Neira Ayuso <pablo@netfilter.org> |
| 8 | * |
| 9 | * Initial connection tracking 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 | * This software may be used and distributed according to the terms |
| 15 | * of the GNU General Public License, incorporated herein by reference. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/timer.h> |
| 23 | #include <linux/skbuff.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/netlink.h> |
| 26 | #include <linux/spinlock.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/notifier.h> |
| 29 | |
| 30 | #include <linux/netfilter.h> |
| 31 | #include <net/netlink.h> |
| 32 | #include <net/netfilter/nf_conntrack.h> |
| 33 | #include <net/netfilter/nf_conntrack_core.h> |
| 34 | #include <net/netfilter/nf_conntrack_expect.h> |
| 35 | #include <net/netfilter/nf_conntrack_helper.h> |
| 36 | #include <net/netfilter/nf_conntrack_l3proto.h> |
| 37 | #include <net/netfilter/nf_conntrack_l4proto.h> |
| 38 | #include <net/netfilter/nf_conntrack_tuple.h> |
| 39 | #ifdef CONFIG_NF_NAT_NEEDED |
| 40 | #include <net/netfilter/nf_nat_core.h> |
| 41 | #include <net/netfilter/nf_nat_protocol.h> |
| 42 | #endif |
| 43 | |
| 44 | #include <linux/netfilter/nfnetlink.h> |
| 45 | #include <linux/netfilter/nfnetlink_conntrack.h> |
| 46 | |
| 47 | MODULE_LICENSE("GPL"); |
| 48 | |
| 49 | static char __initdata version[] = "0.93"; |
| 50 | |
| 51 | static inline int |
| 52 | ctnetlink_dump_tuples_proto(struct sk_buff *skb, |
| 53 | const struct nf_conntrack_tuple *tuple, |
| 54 | struct nf_conntrack_l4proto *l4proto) |
| 55 | { |
| 56 | int ret = 0; |
| 57 | struct nlattr *nest_parms; |
| 58 | |
| 59 | nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED); |
| 60 | if (!nest_parms) |
| 61 | goto nla_put_failure; |
| 62 | NLA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum); |
| 63 | |
| 64 | if (likely(l4proto->tuple_to_nlattr)) |
| 65 | ret = l4proto->tuple_to_nlattr(skb, tuple); |
| 66 | |
| 67 | nla_nest_end(skb, nest_parms); |
| 68 | |
| 69 | return ret; |
| 70 | |
| 71 | nla_put_failure: |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | static inline int |
| 76 | ctnetlink_dump_tuples_ip(struct sk_buff *skb, |
| 77 | const struct nf_conntrack_tuple *tuple, |
| 78 | struct nf_conntrack_l3proto *l3proto) |
| 79 | { |
| 80 | int ret = 0; |
| 81 | struct nlattr *nest_parms; |
| 82 | |
| 83 | nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED); |
| 84 | if (!nest_parms) |
| 85 | goto nla_put_failure; |
| 86 | |
| 87 | if (likely(l3proto->tuple_to_nlattr)) |
| 88 | ret = l3proto->tuple_to_nlattr(skb, tuple); |
| 89 | |
| 90 | nla_nest_end(skb, nest_parms); |
| 91 | |
| 92 | return ret; |
| 93 | |
| 94 | nla_put_failure: |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | static inline int |
| 99 | ctnetlink_dump_tuples(struct sk_buff *skb, |
| 100 | const struct nf_conntrack_tuple *tuple) |
| 101 | { |
| 102 | int ret; |
| 103 | struct nf_conntrack_l3proto *l3proto; |
| 104 | struct nf_conntrack_l4proto *l4proto; |
| 105 | |
| 106 | l3proto = nf_ct_l3proto_find_get(tuple->src.l3num); |
| 107 | ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto); |
| 108 | nf_ct_l3proto_put(l3proto); |
| 109 | |
| 110 | if (unlikely(ret < 0)) |
| 111 | return ret; |
| 112 | |
| 113 | l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum); |
| 114 | ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto); |
| 115 | nf_ct_l4proto_put(l4proto); |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static inline int |
| 121 | ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct) |
| 122 | { |
| 123 | __be32 status = htonl((u_int32_t) ct->status); |
| 124 | NLA_PUT(skb, CTA_STATUS, sizeof(status), &status); |
| 125 | return 0; |
| 126 | |
| 127 | nla_put_failure: |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | static inline int |
| 132 | ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct) |
| 133 | { |
| 134 | long timeout_l = ct->timeout.expires - jiffies; |
| 135 | __be32 timeout; |
| 136 | |
| 137 | if (timeout_l < 0) |
| 138 | timeout = 0; |
| 139 | else |
| 140 | timeout = htonl(timeout_l / HZ); |
| 141 | |
| 142 | NLA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout); |
| 143 | return 0; |
| 144 | |
| 145 | nla_put_failure: |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | static inline int |
| 150 | ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct) |
| 151 | { |
| 152 | struct nf_conntrack_l4proto *l4proto = nf_ct_l4proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num, ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum); |
| 153 | struct nlattr *nest_proto; |
| 154 | int ret; |
| 155 | |
| 156 | if (!l4proto->to_nlattr) { |
| 157 | nf_ct_l4proto_put(l4proto); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED); |
| 162 | if (!nest_proto) |
| 163 | goto nla_put_failure; |
| 164 | |
| 165 | ret = l4proto->to_nlattr(skb, nest_proto, ct); |
| 166 | |
| 167 | nf_ct_l4proto_put(l4proto); |
| 168 | |
| 169 | nla_nest_end(skb, nest_proto); |
| 170 | |
| 171 | return ret; |
| 172 | |
| 173 | nla_put_failure: |
| 174 | nf_ct_l4proto_put(l4proto); |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | static inline int |
| 179 | ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct) |
| 180 | { |
| 181 | struct nlattr *nest_helper; |
| 182 | const struct nf_conn_help *help = nfct_help(ct); |
| 183 | struct nf_conntrack_helper *helper; |
| 184 | |
| 185 | if (!help) |
| 186 | return 0; |
| 187 | |
| 188 | rcu_read_lock(); |
| 189 | helper = rcu_dereference(help->helper); |
| 190 | if (!helper) |
| 191 | goto out; |
| 192 | |
| 193 | nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED); |
| 194 | if (!nest_helper) |
| 195 | goto nla_put_failure; |
| 196 | NLA_PUT(skb, CTA_HELP_NAME, strlen(helper->name), helper->name); |
| 197 | |
| 198 | if (helper->to_nlattr) |
| 199 | helper->to_nlattr(skb, ct); |
| 200 | |
| 201 | nla_nest_end(skb, nest_helper); |
| 202 | out: |
| 203 | rcu_read_unlock(); |
| 204 | return 0; |
| 205 | |
| 206 | nla_put_failure: |
| 207 | rcu_read_unlock(); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | #ifdef CONFIG_NF_CT_ACCT |
| 212 | static inline int |
| 213 | ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct, |
| 214 | enum ip_conntrack_dir dir) |
| 215 | { |
| 216 | enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; |
| 217 | struct nlattr *nest_count; |
| 218 | __be32 tmp; |
| 219 | |
| 220 | nest_count = nla_nest_start(skb, type | NLA_F_NESTED); |
| 221 | if (!nest_count) |
| 222 | goto nla_put_failure; |
| 223 | |
| 224 | tmp = htonl(ct->counters[dir].packets); |
| 225 | NLA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp); |
| 226 | |
| 227 | tmp = htonl(ct->counters[dir].bytes); |
| 228 | NLA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp); |
| 229 | |
| 230 | nla_nest_end(skb, nest_count); |
| 231 | |
| 232 | return 0; |
| 233 | |
| 234 | nla_put_failure: |
| 235 | return -1; |
| 236 | } |
| 237 | #else |
| 238 | #define ctnetlink_dump_counters(a, b, c) (0) |
| 239 | #endif |
| 240 | |
| 241 | #ifdef CONFIG_NF_CONNTRACK_MARK |
| 242 | static inline int |
| 243 | ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct) |
| 244 | { |
| 245 | __be32 mark = htonl(ct->mark); |
| 246 | |
| 247 | NLA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark); |
| 248 | return 0; |
| 249 | |
| 250 | nla_put_failure: |
| 251 | return -1; |
| 252 | } |
| 253 | #else |
| 254 | #define ctnetlink_dump_mark(a, b) (0) |
| 255 | #endif |
| 256 | |
| 257 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
| 258 | static inline int |
| 259 | ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct) |
| 260 | { |
| 261 | __be32 mark = htonl(ct->secmark); |
| 262 | |
| 263 | NLA_PUT(skb, CTA_SECMARK, sizeof(u_int32_t), &mark); |
| 264 | return 0; |
| 265 | |
| 266 | nla_put_failure: |
| 267 | return -1; |
| 268 | } |
| 269 | #else |
| 270 | #define ctnetlink_dump_secmark(a, b) (0) |
| 271 | #endif |
| 272 | |
| 273 | #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple) |
| 274 | |
| 275 | static inline int |
| 276 | ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct) |
| 277 | { |
| 278 | struct nlattr *nest_parms; |
| 279 | |
| 280 | if (!(ct->status & IPS_EXPECTED)) |
| 281 | return 0; |
| 282 | |
| 283 | nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED); |
| 284 | if (!nest_parms) |
| 285 | goto nla_put_failure; |
| 286 | if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0) |
| 287 | goto nla_put_failure; |
| 288 | nla_nest_end(skb, nest_parms); |
| 289 | |
| 290 | return 0; |
| 291 | |
| 292 | nla_put_failure: |
| 293 | return -1; |
| 294 | } |
| 295 | |
| 296 | #ifdef CONFIG_NF_NAT_NEEDED |
| 297 | static inline int |
| 298 | dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type) |
| 299 | { |
| 300 | __be32 tmp; |
| 301 | struct nlattr *nest_parms; |
| 302 | |
| 303 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); |
| 304 | if (!nest_parms) |
| 305 | goto nla_put_failure; |
| 306 | |
| 307 | tmp = htonl(natseq->correction_pos); |
| 308 | NLA_PUT(skb, CTA_NAT_SEQ_CORRECTION_POS, sizeof(tmp), &tmp); |
| 309 | tmp = htonl(natseq->offset_before); |
| 310 | NLA_PUT(skb, CTA_NAT_SEQ_OFFSET_BEFORE, sizeof(tmp), &tmp); |
| 311 | tmp = htonl(natseq->offset_after); |
| 312 | NLA_PUT(skb, CTA_NAT_SEQ_OFFSET_AFTER, sizeof(tmp), &tmp); |
| 313 | |
| 314 | nla_nest_end(skb, nest_parms); |
| 315 | |
| 316 | return 0; |
| 317 | |
| 318 | nla_put_failure: |
| 319 | return -1; |
| 320 | } |
| 321 | |
| 322 | static inline int |
| 323 | ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct) |
| 324 | { |
| 325 | struct nf_nat_seq *natseq; |
| 326 | struct nf_conn_nat *nat = nfct_nat(ct); |
| 327 | |
| 328 | if (!(ct->status & IPS_SEQ_ADJUST) || !nat) |
| 329 | return 0; |
| 330 | |
| 331 | natseq = &nat->seq[IP_CT_DIR_ORIGINAL]; |
| 332 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1) |
| 333 | return -1; |
| 334 | |
| 335 | natseq = &nat->seq[IP_CT_DIR_REPLY]; |
| 336 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1) |
| 337 | return -1; |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | #else |
| 342 | #define ctnetlink_dump_nat_seq_adj(a, b) (0) |
| 343 | #endif |
| 344 | |
| 345 | static inline int |
| 346 | ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct) |
| 347 | { |
| 348 | __be32 id = htonl((unsigned long)ct); |
| 349 | NLA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id); |
| 350 | return 0; |
| 351 | |
| 352 | nla_put_failure: |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | static inline int |
| 357 | ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct) |
| 358 | { |
| 359 | __be32 use = htonl(atomic_read(&ct->ct_general.use)); |
| 360 | |
| 361 | NLA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use); |
| 362 | return 0; |
| 363 | |
| 364 | nla_put_failure: |
| 365 | return -1; |
| 366 | } |
| 367 | |
| 368 | #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple) |
| 369 | |
| 370 | static int |
| 371 | ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, |
| 372 | int event, int nowait, |
| 373 | const struct nf_conn *ct) |
| 374 | { |
| 375 | struct nlmsghdr *nlh; |
| 376 | struct nfgenmsg *nfmsg; |
| 377 | struct nlattr *nest_parms; |
| 378 | unsigned char *b = skb_tail_pointer(skb); |
| 379 | |
| 380 | event |= NFNL_SUBSYS_CTNETLINK << 8; |
| 381 | nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg)); |
| 382 | nfmsg = NLMSG_DATA(nlh); |
| 383 | |
| 384 | nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0; |
| 385 | nfmsg->nfgen_family = |
| 386 | ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num; |
| 387 | nfmsg->version = NFNETLINK_V0; |
| 388 | nfmsg->res_id = 0; |
| 389 | |
| 390 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
| 391 | if (!nest_parms) |
| 392 | goto nla_put_failure; |
| 393 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
| 394 | goto nla_put_failure; |
| 395 | nla_nest_end(skb, nest_parms); |
| 396 | |
| 397 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
| 398 | if (!nest_parms) |
| 399 | goto nla_put_failure; |
| 400 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0) |
| 401 | goto nla_put_failure; |
| 402 | nla_nest_end(skb, nest_parms); |
| 403 | |
| 404 | if (ctnetlink_dump_status(skb, ct) < 0 || |
| 405 | ctnetlink_dump_timeout(skb, ct) < 0 || |
| 406 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || |
| 407 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 || |
| 408 | ctnetlink_dump_protoinfo(skb, ct) < 0 || |
| 409 | ctnetlink_dump_helpinfo(skb, ct) < 0 || |
| 410 | ctnetlink_dump_mark(skb, ct) < 0 || |
| 411 | ctnetlink_dump_secmark(skb, ct) < 0 || |
| 412 | ctnetlink_dump_id(skb, ct) < 0 || |
| 413 | ctnetlink_dump_use(skb, ct) < 0 || |
| 414 | ctnetlink_dump_master(skb, ct) < 0 || |
| 415 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) |
| 416 | goto nla_put_failure; |
| 417 | |
| 418 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
| 419 | return skb->len; |
| 420 | |
| 421 | nlmsg_failure: |
| 422 | nla_put_failure: |
| 423 | nlmsg_trim(skb, b); |
| 424 | return -1; |
| 425 | } |
| 426 | |
| 427 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
| 428 | static int ctnetlink_conntrack_event(struct notifier_block *this, |
| 429 | unsigned long events, void *ptr) |
| 430 | { |
| 431 | struct nlmsghdr *nlh; |
| 432 | struct nfgenmsg *nfmsg; |
| 433 | struct nlattr *nest_parms; |
| 434 | struct nf_conn *ct = (struct nf_conn *)ptr; |
| 435 | struct sk_buff *skb; |
| 436 | unsigned int type; |
| 437 | sk_buff_data_t b; |
| 438 | unsigned int flags = 0, group; |
| 439 | |
| 440 | /* ignore our fake conntrack entry */ |
| 441 | if (ct == &nf_conntrack_untracked) |
| 442 | return NOTIFY_DONE; |
| 443 | |
| 444 | if (events & IPCT_DESTROY) { |
| 445 | type = IPCTNL_MSG_CT_DELETE; |
| 446 | group = NFNLGRP_CONNTRACK_DESTROY; |
| 447 | } else if (events & (IPCT_NEW | IPCT_RELATED)) { |
| 448 | type = IPCTNL_MSG_CT_NEW; |
| 449 | flags = NLM_F_CREATE|NLM_F_EXCL; |
| 450 | group = NFNLGRP_CONNTRACK_NEW; |
| 451 | } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) { |
| 452 | type = IPCTNL_MSG_CT_NEW; |
| 453 | group = NFNLGRP_CONNTRACK_UPDATE; |
| 454 | } else |
| 455 | return NOTIFY_DONE; |
| 456 | |
| 457 | if (!nfnetlink_has_listeners(group)) |
| 458 | return NOTIFY_DONE; |
| 459 | |
| 460 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
| 461 | if (!skb) |
| 462 | return NOTIFY_DONE; |
| 463 | |
| 464 | b = skb->tail; |
| 465 | |
| 466 | type |= NFNL_SUBSYS_CTNETLINK << 8; |
| 467 | nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg)); |
| 468 | nfmsg = NLMSG_DATA(nlh); |
| 469 | |
| 470 | nlh->nlmsg_flags = flags; |
| 471 | nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num; |
| 472 | nfmsg->version = NFNETLINK_V0; |
| 473 | nfmsg->res_id = 0; |
| 474 | |
| 475 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
| 476 | if (!nest_parms) |
| 477 | goto nla_put_failure; |
| 478 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
| 479 | goto nla_put_failure; |
| 480 | nla_nest_end(skb, nest_parms); |
| 481 | |
| 482 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
| 483 | if (!nest_parms) |
| 484 | goto nla_put_failure; |
| 485 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0) |
| 486 | goto nla_put_failure; |
| 487 | nla_nest_end(skb, nest_parms); |
| 488 | |
| 489 | if (events & IPCT_DESTROY) { |
| 490 | if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || |
| 491 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0) |
| 492 | goto nla_put_failure; |
| 493 | } else { |
| 494 | if (ctnetlink_dump_status(skb, ct) < 0) |
| 495 | goto nla_put_failure; |
| 496 | |
| 497 | if (ctnetlink_dump_timeout(skb, ct) < 0) |
| 498 | goto nla_put_failure; |
| 499 | |
| 500 | if (events & IPCT_PROTOINFO |
| 501 | && ctnetlink_dump_protoinfo(skb, ct) < 0) |
| 502 | goto nla_put_failure; |
| 503 | |
| 504 | if ((events & IPCT_HELPER || nfct_help(ct)) |
| 505 | && ctnetlink_dump_helpinfo(skb, ct) < 0) |
| 506 | goto nla_put_failure; |
| 507 | |
| 508 | #ifdef CONFIG_NF_CONNTRACK_MARK |
| 509 | if ((events & IPCT_MARK || ct->mark) |
| 510 | && ctnetlink_dump_mark(skb, ct) < 0) |
| 511 | goto nla_put_failure; |
| 512 | #endif |
| 513 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
| 514 | if ((events & IPCT_SECMARK || ct->secmark) |
| 515 | && ctnetlink_dump_secmark(skb, ct) < 0) |
| 516 | goto nla_put_failure; |
| 517 | #endif |
| 518 | |
| 519 | if (events & IPCT_COUNTER_FILLING && |
| 520 | (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || |
| 521 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)) |
| 522 | goto nla_put_failure; |
| 523 | |
| 524 | if (events & IPCT_RELATED && |
| 525 | ctnetlink_dump_master(skb, ct) < 0) |
| 526 | goto nla_put_failure; |
| 527 | |
| 528 | if (events & IPCT_NATSEQADJ && |
| 529 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) |
| 530 | goto nla_put_failure; |
| 531 | } |
| 532 | |
| 533 | nlh->nlmsg_len = skb->tail - b; |
| 534 | nfnetlink_send(skb, 0, group, 0); |
| 535 | return NOTIFY_DONE; |
| 536 | |
| 537 | nlmsg_failure: |
| 538 | nla_put_failure: |
| 539 | kfree_skb(skb); |
| 540 | return NOTIFY_DONE; |
| 541 | } |
| 542 | #endif /* CONFIG_NF_CONNTRACK_EVENTS */ |
| 543 | |
| 544 | static int ctnetlink_done(struct netlink_callback *cb) |
| 545 | { |
| 546 | if (cb->args[1]) |
| 547 | nf_ct_put((struct nf_conn *)cb->args[1]); |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num |
| 552 | |
| 553 | static int |
| 554 | ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) |
| 555 | { |
| 556 | struct nf_conn *ct, *last; |
| 557 | struct nf_conntrack_tuple_hash *h; |
| 558 | struct hlist_node *n; |
| 559 | struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh); |
| 560 | u_int8_t l3proto = nfmsg->nfgen_family; |
| 561 | |
| 562 | read_lock_bh(&nf_conntrack_lock); |
| 563 | last = (struct nf_conn *)cb->args[1]; |
| 564 | for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) { |
| 565 | restart: |
| 566 | hlist_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]], |
| 567 | hnode) { |
| 568 | if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) |
| 569 | continue; |
| 570 | ct = nf_ct_tuplehash_to_ctrack(h); |
| 571 | /* Dump entries of a given L3 protocol number. |
| 572 | * If it is not specified, ie. l3proto == 0, |
| 573 | * then dump everything. */ |
| 574 | if (l3proto && L3PROTO(ct) != l3proto) |
| 575 | continue; |
| 576 | if (cb->args[1]) { |
| 577 | if (ct != last) |
| 578 | continue; |
| 579 | cb->args[1] = 0; |
| 580 | } |
| 581 | if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid, |
| 582 | cb->nlh->nlmsg_seq, |
| 583 | IPCTNL_MSG_CT_NEW, |
| 584 | 1, ct) < 0) { |
| 585 | nf_conntrack_get(&ct->ct_general); |
| 586 | cb->args[1] = (unsigned long)ct; |
| 587 | goto out; |
| 588 | } |
| 589 | #ifdef CONFIG_NF_CT_ACCT |
| 590 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == |
| 591 | IPCTNL_MSG_CT_GET_CTRZERO) |
| 592 | memset(&ct->counters, 0, sizeof(ct->counters)); |
| 593 | #endif |
| 594 | } |
| 595 | if (cb->args[1]) { |
| 596 | cb->args[1] = 0; |
| 597 | goto restart; |
| 598 | } |
| 599 | } |
| 600 | out: |
| 601 | read_unlock_bh(&nf_conntrack_lock); |
| 602 | if (last) |
| 603 | nf_ct_put(last); |
| 604 | |
| 605 | return skb->len; |
| 606 | } |
| 607 | |
| 608 | static inline int |
| 609 | ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple) |
| 610 | { |
| 611 | struct nlattr *tb[CTA_IP_MAX+1]; |
| 612 | struct nf_conntrack_l3proto *l3proto; |
| 613 | int ret = 0; |
| 614 | |
| 615 | nla_parse_nested(tb, CTA_IP_MAX, attr, NULL); |
| 616 | |
| 617 | l3proto = nf_ct_l3proto_find_get(tuple->src.l3num); |
| 618 | |
| 619 | if (likely(l3proto->nlattr_to_tuple)) { |
| 620 | ret = nla_validate_nested(attr, CTA_IP_MAX, |
| 621 | l3proto->nla_policy); |
| 622 | if (ret == 0) |
| 623 | ret = l3proto->nlattr_to_tuple(tb, tuple); |
| 624 | } |
| 625 | |
| 626 | nf_ct_l3proto_put(l3proto); |
| 627 | |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = { |
| 632 | [CTA_PROTO_NUM] = { .type = NLA_U8 }, |
| 633 | }; |
| 634 | |
| 635 | static inline int |
| 636 | ctnetlink_parse_tuple_proto(struct nlattr *attr, |
| 637 | struct nf_conntrack_tuple *tuple) |
| 638 | { |
| 639 | struct nlattr *tb[CTA_PROTO_MAX+1]; |
| 640 | struct nf_conntrack_l4proto *l4proto; |
| 641 | int ret = 0; |
| 642 | |
| 643 | ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy); |
| 644 | if (ret < 0) |
| 645 | return ret; |
| 646 | |
| 647 | if (!tb[CTA_PROTO_NUM]) |
| 648 | return -EINVAL; |
| 649 | tuple->dst.protonum = *(u_int8_t *)nla_data(tb[CTA_PROTO_NUM]); |
| 650 | |
| 651 | l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum); |
| 652 | |
| 653 | if (likely(l4proto->nlattr_to_tuple)) { |
| 654 | ret = nla_validate_nested(attr, CTA_PROTO_MAX, |
| 655 | l4proto->nla_policy); |
| 656 | if (ret == 0) |
| 657 | ret = l4proto->nlattr_to_tuple(tb, tuple); |
| 658 | } |
| 659 | |
| 660 | nf_ct_l4proto_put(l4proto); |
| 661 | |
| 662 | return ret; |
| 663 | } |
| 664 | |
| 665 | static inline int |
| 666 | ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple, |
| 667 | enum ctattr_tuple type, u_int8_t l3num) |
| 668 | { |
| 669 | struct nlattr *tb[CTA_TUPLE_MAX+1]; |
| 670 | int err; |
| 671 | |
| 672 | memset(tuple, 0, sizeof(*tuple)); |
| 673 | |
| 674 | nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL); |
| 675 | |
| 676 | if (!tb[CTA_TUPLE_IP]) |
| 677 | return -EINVAL; |
| 678 | |
| 679 | tuple->src.l3num = l3num; |
| 680 | |
| 681 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple); |
| 682 | if (err < 0) |
| 683 | return err; |
| 684 | |
| 685 | if (!tb[CTA_TUPLE_PROTO]) |
| 686 | return -EINVAL; |
| 687 | |
| 688 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple); |
| 689 | if (err < 0) |
| 690 | return err; |
| 691 | |
| 692 | /* orig and expect tuples get DIR_ORIGINAL */ |
| 693 | if (type == CTA_TUPLE_REPLY) |
| 694 | tuple->dst.dir = IP_CT_DIR_REPLY; |
| 695 | else |
| 696 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | #ifdef CONFIG_NF_NAT_NEEDED |
| 702 | static const struct nla_policy protonat_nla_policy[CTA_PROTONAT_MAX+1] = { |
| 703 | [CTA_PROTONAT_PORT_MIN] = { .type = NLA_U16 }, |
| 704 | [CTA_PROTONAT_PORT_MAX] = { .type = NLA_U16 }, |
| 705 | }; |
| 706 | |
| 707 | static int nfnetlink_parse_nat_proto(struct nlattr *attr, |
| 708 | const struct nf_conn *ct, |
| 709 | struct nf_nat_range *range) |
| 710 | { |
| 711 | struct nlattr *tb[CTA_PROTONAT_MAX+1]; |
| 712 | struct nf_nat_protocol *npt; |
| 713 | int err; |
| 714 | |
| 715 | err = nla_parse_nested(tb, CTA_PROTONAT_MAX, attr, protonat_nla_policy); |
| 716 | if (err < 0) |
| 717 | return err; |
| 718 | |
| 719 | npt = nf_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum); |
| 720 | |
| 721 | if (!npt->nlattr_to_range) { |
| 722 | nf_nat_proto_put(npt); |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | /* nlattr_to_range returns 1 if it parsed, 0 if not, neg. on error */ |
| 727 | if (npt->nlattr_to_range(tb, range) > 0) |
| 728 | range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED; |
| 729 | |
| 730 | nf_nat_proto_put(npt); |
| 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | static const struct nla_policy nat_nla_policy[CTA_NAT_MAX+1] = { |
| 736 | [CTA_NAT_MINIP] = { .type = NLA_U32 }, |
| 737 | [CTA_NAT_MAXIP] = { .type = NLA_U32 }, |
| 738 | }; |
| 739 | |
| 740 | static inline int |
| 741 | nfnetlink_parse_nat(struct nlattr *nat, |
| 742 | const struct nf_conn *ct, struct nf_nat_range *range) |
| 743 | { |
| 744 | struct nlattr *tb[CTA_NAT_MAX+1]; |
| 745 | int err; |
| 746 | |
| 747 | memset(range, 0, sizeof(*range)); |
| 748 | |
| 749 | err = nla_parse_nested(tb, CTA_NAT_MAX, nat, nat_nla_policy); |
| 750 | if (err < 0) |
| 751 | return err; |
| 752 | |
| 753 | if (tb[CTA_NAT_MINIP]) |
| 754 | range->min_ip = *(__be32 *)nla_data(tb[CTA_NAT_MINIP]); |
| 755 | |
| 756 | if (!tb[CTA_NAT_MAXIP]) |
| 757 | range->max_ip = range->min_ip; |
| 758 | else |
| 759 | range->max_ip = *(__be32 *)nla_data(tb[CTA_NAT_MAXIP]); |
| 760 | |
| 761 | if (range->min_ip) |
| 762 | range->flags |= IP_NAT_RANGE_MAP_IPS; |
| 763 | |
| 764 | if (!tb[CTA_NAT_PROTO]) |
| 765 | return 0; |
| 766 | |
| 767 | err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO], ct, range); |
| 768 | if (err < 0) |
| 769 | return err; |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | #endif |
| 774 | |
| 775 | static inline int |
| 776 | ctnetlink_parse_help(struct nlattr *attr, char **helper_name) |
| 777 | { |
| 778 | struct nlattr *tb[CTA_HELP_MAX+1]; |
| 779 | |
| 780 | nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL); |
| 781 | |
| 782 | if (!tb[CTA_HELP_NAME]) |
| 783 | return -EINVAL; |
| 784 | |
| 785 | *helper_name = nla_data(tb[CTA_HELP_NAME]); |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static const struct nla_policy ct_nla_policy[CTA_MAX+1] = { |
| 791 | [CTA_STATUS] = { .type = NLA_U32 }, |
| 792 | [CTA_TIMEOUT] = { .type = NLA_U32 }, |
| 793 | [CTA_MARK] = { .type = NLA_U32 }, |
| 794 | [CTA_USE] = { .type = NLA_U32 }, |
| 795 | [CTA_ID] = { .type = NLA_U32 }, |
| 796 | }; |
| 797 | |
| 798 | static int |
| 799 | ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, |
| 800 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
| 801 | { |
| 802 | struct nf_conntrack_tuple_hash *h; |
| 803 | struct nf_conntrack_tuple tuple; |
| 804 | struct nf_conn *ct; |
| 805 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); |
| 806 | u_int8_t u3 = nfmsg->nfgen_family; |
| 807 | int err = 0; |
| 808 | |
| 809 | if (cda[CTA_TUPLE_ORIG]) |
| 810 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
| 811 | else if (cda[CTA_TUPLE_REPLY]) |
| 812 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
| 813 | else { |
| 814 | /* Flush the whole table */ |
| 815 | nf_conntrack_flush(); |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | if (err < 0) |
| 820 | return err; |
| 821 | |
| 822 | h = nf_conntrack_find_get(&tuple); |
| 823 | if (!h) |
| 824 | return -ENOENT; |
| 825 | |
| 826 | ct = nf_ct_tuplehash_to_ctrack(h); |
| 827 | |
| 828 | if (cda[CTA_ID]) { |
| 829 | u_int32_t id = ntohl(*(__be32 *)nla_data(cda[CTA_ID])); |
| 830 | if (id != (u32)(unsigned long)ct) { |
| 831 | nf_ct_put(ct); |
| 832 | return -ENOENT; |
| 833 | } |
| 834 | } |
| 835 | if (del_timer(&ct->timeout)) |
| 836 | ct->timeout.function((unsigned long)ct); |
| 837 | |
| 838 | nf_ct_put(ct); |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | static int |
| 844 | ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, |
| 845 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
| 846 | { |
| 847 | struct nf_conntrack_tuple_hash *h; |
| 848 | struct nf_conntrack_tuple tuple; |
| 849 | struct nf_conn *ct; |
| 850 | struct sk_buff *skb2 = NULL; |
| 851 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); |
| 852 | u_int8_t u3 = nfmsg->nfgen_family; |
| 853 | int err = 0; |
| 854 | |
| 855 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
| 856 | #ifndef CONFIG_NF_CT_ACCT |
| 857 | if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO) |
| 858 | return -ENOTSUPP; |
| 859 | #endif |
| 860 | return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table, |
| 861 | ctnetlink_done); |
| 862 | } |
| 863 | |
| 864 | if (cda[CTA_TUPLE_ORIG]) |
| 865 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
| 866 | else if (cda[CTA_TUPLE_REPLY]) |
| 867 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
| 868 | else |
| 869 | return -EINVAL; |
| 870 | |
| 871 | if (err < 0) |
| 872 | return err; |
| 873 | |
| 874 | h = nf_conntrack_find_get(&tuple); |
| 875 | if (!h) |
| 876 | return -ENOENT; |
| 877 | |
| 878 | ct = nf_ct_tuplehash_to_ctrack(h); |
| 879 | |
| 880 | err = -ENOMEM; |
| 881 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 882 | if (!skb2) { |
| 883 | nf_ct_put(ct); |
| 884 | return -ENOMEM; |
| 885 | } |
| 886 | |
| 887 | err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, |
| 888 | IPCTNL_MSG_CT_NEW, 1, ct); |
| 889 | nf_ct_put(ct); |
| 890 | if (err <= 0) |
| 891 | goto free; |
| 892 | |
| 893 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); |
| 894 | if (err < 0) |
| 895 | goto out; |
| 896 | |
| 897 | return 0; |
| 898 | |
| 899 | free: |
| 900 | kfree_skb(skb2); |
| 901 | out: |
| 902 | return err; |
| 903 | } |
| 904 | |
| 905 | static inline int |
| 906 | ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[]) |
| 907 | { |
| 908 | unsigned long d; |
| 909 | unsigned int status = ntohl(*(__be32 *)nla_data(cda[CTA_STATUS])); |
| 910 | d = ct->status ^ status; |
| 911 | |
| 912 | if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING)) |
| 913 | /* unchangeable */ |
| 914 | return -EINVAL; |
| 915 | |
| 916 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) |
| 917 | /* SEEN_REPLY bit can only be set */ |
| 918 | return -EINVAL; |
| 919 | |
| 920 | |
| 921 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) |
| 922 | /* ASSURED bit can only be set */ |
| 923 | return -EINVAL; |
| 924 | |
| 925 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) { |
| 926 | #ifndef CONFIG_NF_NAT_NEEDED |
| 927 | return -EINVAL; |
| 928 | #else |
| 929 | struct nf_nat_range range; |
| 930 | |
| 931 | if (cda[CTA_NAT_DST]) { |
| 932 | if (nfnetlink_parse_nat(cda[CTA_NAT_DST], ct, |
| 933 | &range) < 0) |
| 934 | return -EINVAL; |
| 935 | if (nf_nat_initialized(ct, |
| 936 | HOOK2MANIP(NF_INET_PRE_ROUTING))) |
| 937 | return -EEXIST; |
| 938 | nf_nat_setup_info(ct, &range, NF_INET_PRE_ROUTING); |
| 939 | } |
| 940 | if (cda[CTA_NAT_SRC]) { |
| 941 | if (nfnetlink_parse_nat(cda[CTA_NAT_SRC], ct, |
| 942 | &range) < 0) |
| 943 | return -EINVAL; |
| 944 | if (nf_nat_initialized(ct, |
| 945 | HOOK2MANIP(NF_INET_POST_ROUTING))) |
| 946 | return -EEXIST; |
| 947 | nf_nat_setup_info(ct, &range, NF_INET_POST_ROUTING); |
| 948 | } |
| 949 | #endif |
| 950 | } |
| 951 | |
| 952 | /* Be careful here, modifying NAT bits can screw up things, |
| 953 | * so don't let users modify them directly if they don't pass |
| 954 | * nf_nat_range. */ |
| 955 | ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK); |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | |
| 960 | static inline int |
| 961 | ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[]) |
| 962 | { |
| 963 | struct nf_conntrack_helper *helper; |
| 964 | struct nf_conn_help *help = nfct_help(ct); |
| 965 | char *helpname; |
| 966 | int err; |
| 967 | |
| 968 | /* don't change helper of sibling connections */ |
| 969 | if (ct->master) |
| 970 | return -EINVAL; |
| 971 | |
| 972 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); |
| 973 | if (err < 0) |
| 974 | return err; |
| 975 | |
| 976 | if (!strcmp(helpname, "")) { |
| 977 | if (help && help->helper) { |
| 978 | /* we had a helper before ... */ |
| 979 | nf_ct_remove_expectations(ct); |
| 980 | rcu_assign_pointer(help->helper, NULL); |
| 981 | } |
| 982 | |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | helper = __nf_conntrack_helper_find_byname(helpname); |
| 987 | if (helper == NULL) |
| 988 | return -EINVAL; |
| 989 | |
| 990 | if (help) { |
| 991 | if (help->helper == helper) |
| 992 | return 0; |
| 993 | if (help->helper) |
| 994 | return -EBUSY; |
| 995 | /* need to zero data of old helper */ |
| 996 | memset(&help->help, 0, sizeof(help->help)); |
| 997 | } else { |
| 998 | help = nf_ct_helper_ext_add(ct, GFP_KERNEL); |
| 999 | if (help == NULL) |
| 1000 | return -ENOMEM; |
| 1001 | } |
| 1002 | |
| 1003 | rcu_assign_pointer(help->helper, helper); |
| 1004 | |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | static inline int |
| 1009 | ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[]) |
| 1010 | { |
| 1011 | u_int32_t timeout = ntohl(*(__be32 *)nla_data(cda[CTA_TIMEOUT])); |
| 1012 | |
| 1013 | if (!del_timer(&ct->timeout)) |
| 1014 | return -ETIME; |
| 1015 | |
| 1016 | ct->timeout.expires = jiffies + timeout * HZ; |
| 1017 | add_timer(&ct->timeout); |
| 1018 | |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | static inline int |
| 1023 | ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[]) |
| 1024 | { |
| 1025 | struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO]; |
| 1026 | struct nf_conntrack_l4proto *l4proto; |
| 1027 | u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum; |
| 1028 | u_int16_t l3num = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num; |
| 1029 | int err = 0; |
| 1030 | |
| 1031 | nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL); |
| 1032 | |
| 1033 | l4proto = nf_ct_l4proto_find_get(l3num, npt); |
| 1034 | |
| 1035 | if (l4proto->from_nlattr) |
| 1036 | err = l4proto->from_nlattr(tb, ct); |
| 1037 | nf_ct_l4proto_put(l4proto); |
| 1038 | |
| 1039 | return err; |
| 1040 | } |
| 1041 | |
| 1042 | #ifdef CONFIG_NF_NAT_NEEDED |
| 1043 | static inline int |
| 1044 | change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr) |
| 1045 | { |
| 1046 | struct nlattr *cda[CTA_NAT_SEQ_MAX+1]; |
| 1047 | |
| 1048 | nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL); |
| 1049 | |
| 1050 | if (!cda[CTA_NAT_SEQ_CORRECTION_POS]) |
| 1051 | return -EINVAL; |
| 1052 | |
| 1053 | natseq->correction_pos = |
| 1054 | ntohl(*(__be32 *)nla_data(cda[CTA_NAT_SEQ_CORRECTION_POS])); |
| 1055 | |
| 1056 | if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE]) |
| 1057 | return -EINVAL; |
| 1058 | |
| 1059 | natseq->offset_before = |
| 1060 | ntohl(*(__be32 *)nla_data(cda[CTA_NAT_SEQ_OFFSET_BEFORE])); |
| 1061 | |
| 1062 | if (!cda[CTA_NAT_SEQ_OFFSET_AFTER]) |
| 1063 | return -EINVAL; |
| 1064 | |
| 1065 | natseq->offset_after = |
| 1066 | ntohl(*(__be32 *)nla_data(cda[CTA_NAT_SEQ_OFFSET_AFTER])); |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | static int |
| 1072 | ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[]) |
| 1073 | { |
| 1074 | int ret = 0; |
| 1075 | struct nf_conn_nat *nat = nfct_nat(ct); |
| 1076 | |
| 1077 | if (!nat) |
| 1078 | return 0; |
| 1079 | |
| 1080 | if (cda[CTA_NAT_SEQ_ADJ_ORIG]) { |
| 1081 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL], |
| 1082 | cda[CTA_NAT_SEQ_ADJ_ORIG]); |
| 1083 | if (ret < 0) |
| 1084 | return ret; |
| 1085 | |
| 1086 | ct->status |= IPS_SEQ_ADJUST; |
| 1087 | } |
| 1088 | |
| 1089 | if (cda[CTA_NAT_SEQ_ADJ_REPLY]) { |
| 1090 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY], |
| 1091 | cda[CTA_NAT_SEQ_ADJ_REPLY]); |
| 1092 | if (ret < 0) |
| 1093 | return ret; |
| 1094 | |
| 1095 | ct->status |= IPS_SEQ_ADJUST; |
| 1096 | } |
| 1097 | |
| 1098 | return 0; |
| 1099 | } |
| 1100 | #endif |
| 1101 | |
| 1102 | static int |
| 1103 | ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[]) |
| 1104 | { |
| 1105 | int err; |
| 1106 | |
| 1107 | if (cda[CTA_HELP]) { |
| 1108 | err = ctnetlink_change_helper(ct, cda); |
| 1109 | if (err < 0) |
| 1110 | return err; |
| 1111 | } |
| 1112 | |
| 1113 | if (cda[CTA_TIMEOUT]) { |
| 1114 | err = ctnetlink_change_timeout(ct, cda); |
| 1115 | if (err < 0) |
| 1116 | return err; |
| 1117 | } |
| 1118 | |
| 1119 | if (cda[CTA_STATUS]) { |
| 1120 | err = ctnetlink_change_status(ct, cda); |
| 1121 | if (err < 0) |
| 1122 | return err; |
| 1123 | } |
| 1124 | |
| 1125 | if (cda[CTA_PROTOINFO]) { |
| 1126 | err = ctnetlink_change_protoinfo(ct, cda); |
| 1127 | if (err < 0) |
| 1128 | return err; |
| 1129 | } |
| 1130 | |
| 1131 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
| 1132 | if (cda[CTA_MARK]) |
| 1133 | ct->mark = ntohl(*(__be32 *)nla_data(cda[CTA_MARK])); |
| 1134 | #endif |
| 1135 | |
| 1136 | #ifdef CONFIG_NF_NAT_NEEDED |
| 1137 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { |
| 1138 | err = ctnetlink_change_nat_seq_adj(ct, cda); |
| 1139 | if (err < 0) |
| 1140 | return err; |
| 1141 | } |
| 1142 | #endif |
| 1143 | |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | static int |
| 1148 | ctnetlink_create_conntrack(struct nlattr *cda[], |
| 1149 | struct nf_conntrack_tuple *otuple, |
| 1150 | struct nf_conntrack_tuple *rtuple, |
| 1151 | struct nf_conn *master_ct) |
| 1152 | { |
| 1153 | struct nf_conn *ct; |
| 1154 | int err = -EINVAL; |
| 1155 | struct nf_conn_help *help; |
| 1156 | struct nf_conntrack_helper *helper; |
| 1157 | |
| 1158 | ct = nf_conntrack_alloc(otuple, rtuple); |
| 1159 | if (ct == NULL || IS_ERR(ct)) |
| 1160 | return -ENOMEM; |
| 1161 | |
| 1162 | if (!cda[CTA_TIMEOUT]) |
| 1163 | goto err; |
| 1164 | ct->timeout.expires = ntohl(*(__be32 *)nla_data(cda[CTA_TIMEOUT])); |
| 1165 | |
| 1166 | ct->timeout.expires = jiffies + ct->timeout.expires * HZ; |
| 1167 | ct->status |= IPS_CONFIRMED; |
| 1168 | |
| 1169 | if (cda[CTA_STATUS]) { |
| 1170 | err = ctnetlink_change_status(ct, cda); |
| 1171 | if (err < 0) |
| 1172 | goto err; |
| 1173 | } |
| 1174 | |
| 1175 | if (cda[CTA_PROTOINFO]) { |
| 1176 | err = ctnetlink_change_protoinfo(ct, cda); |
| 1177 | if (err < 0) |
| 1178 | goto err; |
| 1179 | } |
| 1180 | |
| 1181 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
| 1182 | if (cda[CTA_MARK]) |
| 1183 | ct->mark = ntohl(*(__be32 *)nla_data(cda[CTA_MARK])); |
| 1184 | #endif |
| 1185 | |
| 1186 | helper = nf_ct_helper_find_get(rtuple); |
| 1187 | if (helper) { |
| 1188 | help = nf_ct_helper_ext_add(ct, GFP_KERNEL); |
| 1189 | if (help == NULL) { |
| 1190 | nf_ct_helper_put(helper); |
| 1191 | err = -ENOMEM; |
| 1192 | goto err; |
| 1193 | } |
| 1194 | /* not in hash table yet so not strictly necessary */ |
| 1195 | rcu_assign_pointer(help->helper, helper); |
| 1196 | } |
| 1197 | |
| 1198 | /* setup master conntrack: this is a confirmed expectation */ |
| 1199 | if (master_ct) { |
| 1200 | __set_bit(IPS_EXPECTED_BIT, &ct->status); |
| 1201 | ct->master = master_ct; |
| 1202 | } |
| 1203 | |
| 1204 | add_timer(&ct->timeout); |
| 1205 | nf_conntrack_hash_insert(ct); |
| 1206 | |
| 1207 | if (helper) |
| 1208 | nf_ct_helper_put(helper); |
| 1209 | |
| 1210 | return 0; |
| 1211 | |
| 1212 | err: |
| 1213 | nf_conntrack_free(ct); |
| 1214 | return err; |
| 1215 | } |
| 1216 | |
| 1217 | static int |
| 1218 | ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, |
| 1219 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
| 1220 | { |
| 1221 | struct nf_conntrack_tuple otuple, rtuple; |
| 1222 | struct nf_conntrack_tuple_hash *h = NULL; |
| 1223 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); |
| 1224 | u_int8_t u3 = nfmsg->nfgen_family; |
| 1225 | int err = 0; |
| 1226 | |
| 1227 | if (cda[CTA_TUPLE_ORIG]) { |
| 1228 | err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3); |
| 1229 | if (err < 0) |
| 1230 | return err; |
| 1231 | } |
| 1232 | |
| 1233 | if (cda[CTA_TUPLE_REPLY]) { |
| 1234 | err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3); |
| 1235 | if (err < 0) |
| 1236 | return err; |
| 1237 | } |
| 1238 | |
| 1239 | write_lock_bh(&nf_conntrack_lock); |
| 1240 | if (cda[CTA_TUPLE_ORIG]) |
| 1241 | h = __nf_conntrack_find(&otuple, NULL); |
| 1242 | else if (cda[CTA_TUPLE_REPLY]) |
| 1243 | h = __nf_conntrack_find(&rtuple, NULL); |
| 1244 | |
| 1245 | if (h == NULL) { |
| 1246 | struct nf_conntrack_tuple master; |
| 1247 | struct nf_conntrack_tuple_hash *master_h = NULL; |
| 1248 | struct nf_conn *master_ct = NULL; |
| 1249 | |
| 1250 | if (cda[CTA_TUPLE_MASTER]) { |
| 1251 | err = ctnetlink_parse_tuple(cda, |
| 1252 | &master, |
| 1253 | CTA_TUPLE_MASTER, |
| 1254 | u3); |
| 1255 | if (err < 0) |
| 1256 | return err; |
| 1257 | |
| 1258 | master_h = __nf_conntrack_find(&master, NULL); |
| 1259 | if (master_h == NULL) { |
| 1260 | err = -ENOENT; |
| 1261 | goto out_unlock; |
| 1262 | } |
| 1263 | master_ct = nf_ct_tuplehash_to_ctrack(master_h); |
| 1264 | atomic_inc(&master_ct->ct_general.use); |
| 1265 | } |
| 1266 | |
| 1267 | write_unlock_bh(&nf_conntrack_lock); |
| 1268 | err = -ENOENT; |
| 1269 | if (nlh->nlmsg_flags & NLM_F_CREATE) |
| 1270 | err = ctnetlink_create_conntrack(cda, |
| 1271 | &otuple, |
| 1272 | &rtuple, |
| 1273 | master_ct); |
| 1274 | if (err < 0 && master_ct) |
| 1275 | nf_ct_put(master_ct); |
| 1276 | |
| 1277 | return err; |
| 1278 | } |
| 1279 | /* implicit 'else' */ |
| 1280 | |
| 1281 | /* We manipulate the conntrack inside the global conntrack table lock, |
| 1282 | * so there's no need to increase the refcount */ |
| 1283 | err = -EEXIST; |
| 1284 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) { |
| 1285 | /* we only allow nat config for new conntracks */ |
| 1286 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) { |
| 1287 | err = -EINVAL; |
| 1288 | goto out_unlock; |
| 1289 | } |
| 1290 | /* can't link an existing conntrack to a master */ |
| 1291 | if (cda[CTA_TUPLE_MASTER]) { |
| 1292 | err = -EINVAL; |
| 1293 | goto out_unlock; |
| 1294 | } |
| 1295 | err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h), |
| 1296 | cda); |
| 1297 | } |
| 1298 | |
| 1299 | out_unlock: |
| 1300 | write_unlock_bh(&nf_conntrack_lock); |
| 1301 | return err; |
| 1302 | } |
| 1303 | |
| 1304 | /*********************************************************************** |
| 1305 | * EXPECT |
| 1306 | ***********************************************************************/ |
| 1307 | |
| 1308 | static inline int |
| 1309 | ctnetlink_exp_dump_tuple(struct sk_buff *skb, |
| 1310 | const struct nf_conntrack_tuple *tuple, |
| 1311 | enum ctattr_expect type) |
| 1312 | { |
| 1313 | struct nlattr *nest_parms; |
| 1314 | |
| 1315 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); |
| 1316 | if (!nest_parms) |
| 1317 | goto nla_put_failure; |
| 1318 | if (ctnetlink_dump_tuples(skb, tuple) < 0) |
| 1319 | goto nla_put_failure; |
| 1320 | nla_nest_end(skb, nest_parms); |
| 1321 | |
| 1322 | return 0; |
| 1323 | |
| 1324 | nla_put_failure: |
| 1325 | return -1; |
| 1326 | } |
| 1327 | |
| 1328 | static inline int |
| 1329 | ctnetlink_exp_dump_mask(struct sk_buff *skb, |
| 1330 | const struct nf_conntrack_tuple *tuple, |
| 1331 | const struct nf_conntrack_tuple_mask *mask) |
| 1332 | { |
| 1333 | int ret; |
| 1334 | struct nf_conntrack_l3proto *l3proto; |
| 1335 | struct nf_conntrack_l4proto *l4proto; |
| 1336 | struct nf_conntrack_tuple m; |
| 1337 | struct nlattr *nest_parms; |
| 1338 | |
| 1339 | memset(&m, 0xFF, sizeof(m)); |
| 1340 | m.src.u.all = mask->src.u.all; |
| 1341 | memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3)); |
| 1342 | |
| 1343 | nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED); |
| 1344 | if (!nest_parms) |
| 1345 | goto nla_put_failure; |
| 1346 | |
| 1347 | l3proto = nf_ct_l3proto_find_get(tuple->src.l3num); |
| 1348 | ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto); |
| 1349 | nf_ct_l3proto_put(l3proto); |
| 1350 | |
| 1351 | if (unlikely(ret < 0)) |
| 1352 | goto nla_put_failure; |
| 1353 | |
| 1354 | l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum); |
| 1355 | ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto); |
| 1356 | nf_ct_l4proto_put(l4proto); |
| 1357 | if (unlikely(ret < 0)) |
| 1358 | goto nla_put_failure; |
| 1359 | |
| 1360 | nla_nest_end(skb, nest_parms); |
| 1361 | |
| 1362 | return 0; |
| 1363 | |
| 1364 | nla_put_failure: |
| 1365 | return -1; |
| 1366 | } |
| 1367 | |
| 1368 | static inline int |
| 1369 | ctnetlink_exp_dump_expect(struct sk_buff *skb, |
| 1370 | const struct nf_conntrack_expect *exp) |
| 1371 | { |
| 1372 | struct nf_conn *master = exp->master; |
| 1373 | __be32 timeout = htonl((exp->timeout.expires - jiffies) / HZ); |
| 1374 | __be32 id = htonl((unsigned long)exp); |
| 1375 | |
| 1376 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) |
| 1377 | goto nla_put_failure; |
| 1378 | if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0) |
| 1379 | goto nla_put_failure; |
| 1380 | if (ctnetlink_exp_dump_tuple(skb, |
| 1381 | &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
| 1382 | CTA_EXPECT_MASTER) < 0) |
| 1383 | goto nla_put_failure; |
| 1384 | |
| 1385 | NLA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout); |
| 1386 | NLA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id); |
| 1387 | |
| 1388 | return 0; |
| 1389 | |
| 1390 | nla_put_failure: |
| 1391 | return -1; |
| 1392 | } |
| 1393 | |
| 1394 | static int |
| 1395 | ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq, |
| 1396 | int event, |
| 1397 | int nowait, |
| 1398 | const struct nf_conntrack_expect *exp) |
| 1399 | { |
| 1400 | struct nlmsghdr *nlh; |
| 1401 | struct nfgenmsg *nfmsg; |
| 1402 | unsigned char *b = skb_tail_pointer(skb); |
| 1403 | |
| 1404 | event |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
| 1405 | nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg)); |
| 1406 | nfmsg = NLMSG_DATA(nlh); |
| 1407 | |
| 1408 | nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0; |
| 1409 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
| 1410 | nfmsg->version = NFNETLINK_V0; |
| 1411 | nfmsg->res_id = 0; |
| 1412 | |
| 1413 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
| 1414 | goto nla_put_failure; |
| 1415 | |
| 1416 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
| 1417 | return skb->len; |
| 1418 | |
| 1419 | nlmsg_failure: |
| 1420 | nla_put_failure: |
| 1421 | nlmsg_trim(skb, b); |
| 1422 | return -1; |
| 1423 | } |
| 1424 | |
| 1425 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
| 1426 | static int ctnetlink_expect_event(struct notifier_block *this, |
| 1427 | unsigned long events, void *ptr) |
| 1428 | { |
| 1429 | struct nlmsghdr *nlh; |
| 1430 | struct nfgenmsg *nfmsg; |
| 1431 | struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr; |
| 1432 | struct sk_buff *skb; |
| 1433 | unsigned int type; |
| 1434 | sk_buff_data_t b; |
| 1435 | int flags = 0; |
| 1436 | |
| 1437 | if (events & IPEXP_NEW) { |
| 1438 | type = IPCTNL_MSG_EXP_NEW; |
| 1439 | flags = NLM_F_CREATE|NLM_F_EXCL; |
| 1440 | } else |
| 1441 | return NOTIFY_DONE; |
| 1442 | |
| 1443 | if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW)) |
| 1444 | return NOTIFY_DONE; |
| 1445 | |
| 1446 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
| 1447 | if (!skb) |
| 1448 | return NOTIFY_DONE; |
| 1449 | |
| 1450 | b = skb->tail; |
| 1451 | |
| 1452 | type |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
| 1453 | nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg)); |
| 1454 | nfmsg = NLMSG_DATA(nlh); |
| 1455 | |
| 1456 | nlh->nlmsg_flags = flags; |
| 1457 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
| 1458 | nfmsg->version = NFNETLINK_V0; |
| 1459 | nfmsg->res_id = 0; |
| 1460 | |
| 1461 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
| 1462 | goto nla_put_failure; |
| 1463 | |
| 1464 | nlh->nlmsg_len = skb->tail - b; |
| 1465 | nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0); |
| 1466 | return NOTIFY_DONE; |
| 1467 | |
| 1468 | nlmsg_failure: |
| 1469 | nla_put_failure: |
| 1470 | kfree_skb(skb); |
| 1471 | return NOTIFY_DONE; |
| 1472 | } |
| 1473 | #endif |
| 1474 | static int ctnetlink_exp_done(struct netlink_callback *cb) |
| 1475 | { |
| 1476 | if (cb->args[1]) |
| 1477 | nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]); |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | static int |
| 1482 | ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) |
| 1483 | { |
| 1484 | struct nf_conntrack_expect *exp, *last; |
| 1485 | struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh); |
| 1486 | struct hlist_node *n; |
| 1487 | u_int8_t l3proto = nfmsg->nfgen_family; |
| 1488 | |
| 1489 | read_lock_bh(&nf_conntrack_lock); |
| 1490 | last = (struct nf_conntrack_expect *)cb->args[1]; |
| 1491 | for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) { |
| 1492 | restart: |
| 1493 | hlist_for_each_entry(exp, n, &nf_ct_expect_hash[cb->args[0]], |
| 1494 | hnode) { |
| 1495 | if (l3proto && exp->tuple.src.l3num != l3proto) |
| 1496 | continue; |
| 1497 | if (cb->args[1]) { |
| 1498 | if (exp != last) |
| 1499 | continue; |
| 1500 | cb->args[1] = 0; |
| 1501 | } |
| 1502 | if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid, |
| 1503 | cb->nlh->nlmsg_seq, |
| 1504 | IPCTNL_MSG_EXP_NEW, |
| 1505 | 1, exp) < 0) { |
| 1506 | atomic_inc(&exp->use); |
| 1507 | cb->args[1] = (unsigned long)exp; |
| 1508 | goto out; |
| 1509 | } |
| 1510 | } |
| 1511 | if (cb->args[1]) { |
| 1512 | cb->args[1] = 0; |
| 1513 | goto restart; |
| 1514 | } |
| 1515 | } |
| 1516 | out: |
| 1517 | read_unlock_bh(&nf_conntrack_lock); |
| 1518 | if (last) |
| 1519 | nf_ct_expect_put(last); |
| 1520 | |
| 1521 | return skb->len; |
| 1522 | } |
| 1523 | |
| 1524 | static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = { |
| 1525 | [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 }, |
| 1526 | [CTA_EXPECT_ID] = { .type = NLA_U32 }, |
| 1527 | }; |
| 1528 | |
| 1529 | static int |
| 1530 | ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, |
| 1531 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
| 1532 | { |
| 1533 | struct nf_conntrack_tuple tuple; |
| 1534 | struct nf_conntrack_expect *exp; |
| 1535 | struct sk_buff *skb2; |
| 1536 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); |
| 1537 | u_int8_t u3 = nfmsg->nfgen_family; |
| 1538 | int err = 0; |
| 1539 | |
| 1540 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
| 1541 | return netlink_dump_start(ctnl, skb, nlh, |
| 1542 | ctnetlink_exp_dump_table, |
| 1543 | ctnetlink_exp_done); |
| 1544 | } |
| 1545 | |
| 1546 | if (cda[CTA_EXPECT_MASTER]) |
| 1547 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3); |
| 1548 | else |
| 1549 | return -EINVAL; |
| 1550 | |
| 1551 | if (err < 0) |
| 1552 | return err; |
| 1553 | |
| 1554 | exp = nf_ct_expect_find_get(&tuple); |
| 1555 | if (!exp) |
| 1556 | return -ENOENT; |
| 1557 | |
| 1558 | if (cda[CTA_EXPECT_ID]) { |
| 1559 | __be32 id = *(__be32 *)nla_data(cda[CTA_EXPECT_ID]); |
| 1560 | if (ntohl(id) != (u32)(unsigned long)exp) { |
| 1561 | nf_ct_expect_put(exp); |
| 1562 | return -ENOENT; |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | err = -ENOMEM; |
| 1567 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1568 | if (!skb2) |
| 1569 | goto out; |
| 1570 | |
| 1571 | err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid, |
| 1572 | nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, |
| 1573 | 1, exp); |
| 1574 | if (err <= 0) |
| 1575 | goto free; |
| 1576 | |
| 1577 | nf_ct_expect_put(exp); |
| 1578 | |
| 1579 | return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); |
| 1580 | |
| 1581 | free: |
| 1582 | kfree_skb(skb2); |
| 1583 | out: |
| 1584 | nf_ct_expect_put(exp); |
| 1585 | return err; |
| 1586 | } |
| 1587 | |
| 1588 | static int |
| 1589 | ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, |
| 1590 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
| 1591 | { |
| 1592 | struct nf_conntrack_expect *exp; |
| 1593 | struct nf_conntrack_tuple tuple; |
| 1594 | struct nf_conntrack_helper *h; |
| 1595 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); |
| 1596 | struct hlist_node *n, *next; |
| 1597 | u_int8_t u3 = nfmsg->nfgen_family; |
| 1598 | unsigned int i; |
| 1599 | int err; |
| 1600 | |
| 1601 | if (cda[CTA_EXPECT_TUPLE]) { |
| 1602 | /* delete a single expect by tuple */ |
| 1603 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
| 1604 | if (err < 0) |
| 1605 | return err; |
| 1606 | |
| 1607 | /* bump usage count to 2 */ |
| 1608 | exp = nf_ct_expect_find_get(&tuple); |
| 1609 | if (!exp) |
| 1610 | return -ENOENT; |
| 1611 | |
| 1612 | if (cda[CTA_EXPECT_ID]) { |
| 1613 | __be32 id = *(__be32 *)nla_data(cda[CTA_EXPECT_ID]); |
| 1614 | if (ntohl(id) != (u32)(unsigned long)exp) { |
| 1615 | nf_ct_expect_put(exp); |
| 1616 | return -ENOENT; |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | /* after list removal, usage count == 1 */ |
| 1621 | nf_ct_unexpect_related(exp); |
| 1622 | /* have to put what we 'get' above. |
| 1623 | * after this line usage count == 0 */ |
| 1624 | nf_ct_expect_put(exp); |
| 1625 | } else if (cda[CTA_EXPECT_HELP_NAME]) { |
| 1626 | char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]); |
| 1627 | struct nf_conn_help *m_help; |
| 1628 | |
| 1629 | /* delete all expectations for this helper */ |
| 1630 | write_lock_bh(&nf_conntrack_lock); |
| 1631 | h = __nf_conntrack_helper_find_byname(name); |
| 1632 | if (!h) { |
| 1633 | write_unlock_bh(&nf_conntrack_lock); |
| 1634 | return -EINVAL; |
| 1635 | } |
| 1636 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
| 1637 | hlist_for_each_entry_safe(exp, n, next, |
| 1638 | &nf_ct_expect_hash[i], |
| 1639 | hnode) { |
| 1640 | m_help = nfct_help(exp->master); |
| 1641 | if (m_help->helper == h |
| 1642 | && del_timer(&exp->timeout)) { |
| 1643 | nf_ct_unlink_expect(exp); |
| 1644 | nf_ct_expect_put(exp); |
| 1645 | } |
| 1646 | } |
| 1647 | } |
| 1648 | write_unlock_bh(&nf_conntrack_lock); |
| 1649 | } else { |
| 1650 | /* This basically means we have to flush everything*/ |
| 1651 | write_lock_bh(&nf_conntrack_lock); |
| 1652 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
| 1653 | hlist_for_each_entry_safe(exp, n, next, |
| 1654 | &nf_ct_expect_hash[i], |
| 1655 | hnode) { |
| 1656 | if (del_timer(&exp->timeout)) { |
| 1657 | nf_ct_unlink_expect(exp); |
| 1658 | nf_ct_expect_put(exp); |
| 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | write_unlock_bh(&nf_conntrack_lock); |
| 1663 | } |
| 1664 | |
| 1665 | return 0; |
| 1666 | } |
| 1667 | static int |
| 1668 | ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[]) |
| 1669 | { |
| 1670 | return -EOPNOTSUPP; |
| 1671 | } |
| 1672 | |
| 1673 | static int |
| 1674 | ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3) |
| 1675 | { |
| 1676 | struct nf_conntrack_tuple tuple, mask, master_tuple; |
| 1677 | struct nf_conntrack_tuple_hash *h = NULL; |
| 1678 | struct nf_conntrack_expect *exp; |
| 1679 | struct nf_conn *ct; |
| 1680 | struct nf_conn_help *help; |
| 1681 | int err = 0; |
| 1682 | |
| 1683 | /* caller guarantees that those three CTA_EXPECT_* exist */ |
| 1684 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
| 1685 | if (err < 0) |
| 1686 | return err; |
| 1687 | err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3); |
| 1688 | if (err < 0) |
| 1689 | return err; |
| 1690 | err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3); |
| 1691 | if (err < 0) |
| 1692 | return err; |
| 1693 | |
| 1694 | /* Look for master conntrack of this expectation */ |
| 1695 | h = nf_conntrack_find_get(&master_tuple); |
| 1696 | if (!h) |
| 1697 | return -ENOENT; |
| 1698 | ct = nf_ct_tuplehash_to_ctrack(h); |
| 1699 | help = nfct_help(ct); |
| 1700 | |
| 1701 | if (!help || !help->helper) { |
| 1702 | /* such conntrack hasn't got any helper, abort */ |
| 1703 | err = -EINVAL; |
| 1704 | goto out; |
| 1705 | } |
| 1706 | |
| 1707 | exp = nf_ct_expect_alloc(ct); |
| 1708 | if (!exp) { |
| 1709 | err = -ENOMEM; |
| 1710 | goto out; |
| 1711 | } |
| 1712 | |
| 1713 | exp->expectfn = NULL; |
| 1714 | exp->flags = 0; |
| 1715 | exp->master = ct; |
| 1716 | exp->helper = NULL; |
| 1717 | memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple)); |
| 1718 | memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3)); |
| 1719 | exp->mask.src.u.all = mask.src.u.all; |
| 1720 | |
| 1721 | err = nf_ct_expect_related(exp); |
| 1722 | nf_ct_expect_put(exp); |
| 1723 | |
| 1724 | out: |
| 1725 | nf_ct_put(nf_ct_tuplehash_to_ctrack(h)); |
| 1726 | return err; |
| 1727 | } |
| 1728 | |
| 1729 | static int |
| 1730 | ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb, |
| 1731 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
| 1732 | { |
| 1733 | struct nf_conntrack_tuple tuple; |
| 1734 | struct nf_conntrack_expect *exp; |
| 1735 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); |
| 1736 | u_int8_t u3 = nfmsg->nfgen_family; |
| 1737 | int err = 0; |
| 1738 | |
| 1739 | if (!cda[CTA_EXPECT_TUPLE] |
| 1740 | || !cda[CTA_EXPECT_MASK] |
| 1741 | || !cda[CTA_EXPECT_MASTER]) |
| 1742 | return -EINVAL; |
| 1743 | |
| 1744 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
| 1745 | if (err < 0) |
| 1746 | return err; |
| 1747 | |
| 1748 | write_lock_bh(&nf_conntrack_lock); |
| 1749 | exp = __nf_ct_expect_find(&tuple); |
| 1750 | |
| 1751 | if (!exp) { |
| 1752 | write_unlock_bh(&nf_conntrack_lock); |
| 1753 | err = -ENOENT; |
| 1754 | if (nlh->nlmsg_flags & NLM_F_CREATE) |
| 1755 | err = ctnetlink_create_expect(cda, u3); |
| 1756 | return err; |
| 1757 | } |
| 1758 | |
| 1759 | err = -EEXIST; |
| 1760 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) |
| 1761 | err = ctnetlink_change_expect(exp, cda); |
| 1762 | write_unlock_bh(&nf_conntrack_lock); |
| 1763 | |
| 1764 | return err; |
| 1765 | } |
| 1766 | |
| 1767 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
| 1768 | static struct notifier_block ctnl_notifier = { |
| 1769 | .notifier_call = ctnetlink_conntrack_event, |
| 1770 | }; |
| 1771 | |
| 1772 | static struct notifier_block ctnl_notifier_exp = { |
| 1773 | .notifier_call = ctnetlink_expect_event, |
| 1774 | }; |
| 1775 | #endif |
| 1776 | |
| 1777 | static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { |
| 1778 | [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack, |
| 1779 | .attr_count = CTA_MAX, |
| 1780 | .policy = ct_nla_policy }, |
| 1781 | [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack, |
| 1782 | .attr_count = CTA_MAX, |
| 1783 | .policy = ct_nla_policy }, |
| 1784 | [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack, |
| 1785 | .attr_count = CTA_MAX, |
| 1786 | .policy = ct_nla_policy }, |
| 1787 | [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack, |
| 1788 | .attr_count = CTA_MAX, |
| 1789 | .policy = ct_nla_policy }, |
| 1790 | }; |
| 1791 | |
| 1792 | static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = { |
| 1793 | [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect, |
| 1794 | .attr_count = CTA_EXPECT_MAX, |
| 1795 | .policy = exp_nla_policy }, |
| 1796 | [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect, |
| 1797 | .attr_count = CTA_EXPECT_MAX, |
| 1798 | .policy = exp_nla_policy }, |
| 1799 | [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect, |
| 1800 | .attr_count = CTA_EXPECT_MAX, |
| 1801 | .policy = exp_nla_policy }, |
| 1802 | }; |
| 1803 | |
| 1804 | static const struct nfnetlink_subsystem ctnl_subsys = { |
| 1805 | .name = "conntrack", |
| 1806 | .subsys_id = NFNL_SUBSYS_CTNETLINK, |
| 1807 | .cb_count = IPCTNL_MSG_MAX, |
| 1808 | .cb = ctnl_cb, |
| 1809 | }; |
| 1810 | |
| 1811 | static const struct nfnetlink_subsystem ctnl_exp_subsys = { |
| 1812 | .name = "conntrack_expect", |
| 1813 | .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, |
| 1814 | .cb_count = IPCTNL_MSG_EXP_MAX, |
| 1815 | .cb = ctnl_exp_cb, |
| 1816 | }; |
| 1817 | |
| 1818 | MODULE_ALIAS("ip_conntrack_netlink"); |
| 1819 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); |
| 1820 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP); |
| 1821 | |
| 1822 | static int __init ctnetlink_init(void) |
| 1823 | { |
| 1824 | int ret; |
| 1825 | |
| 1826 | printk("ctnetlink v%s: registering with nfnetlink.\n", version); |
| 1827 | ret = nfnetlink_subsys_register(&ctnl_subsys); |
| 1828 | if (ret < 0) { |
| 1829 | printk("ctnetlink_init: cannot register with nfnetlink.\n"); |
| 1830 | goto err_out; |
| 1831 | } |
| 1832 | |
| 1833 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); |
| 1834 | if (ret < 0) { |
| 1835 | printk("ctnetlink_init: cannot register exp with nfnetlink.\n"); |
| 1836 | goto err_unreg_subsys; |
| 1837 | } |
| 1838 | |
| 1839 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
| 1840 | ret = nf_conntrack_register_notifier(&ctnl_notifier); |
| 1841 | if (ret < 0) { |
| 1842 | printk("ctnetlink_init: cannot register notifier.\n"); |
| 1843 | goto err_unreg_exp_subsys; |
| 1844 | } |
| 1845 | |
| 1846 | ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp); |
| 1847 | if (ret < 0) { |
| 1848 | printk("ctnetlink_init: cannot expect register notifier.\n"); |
| 1849 | goto err_unreg_notifier; |
| 1850 | } |
| 1851 | #endif |
| 1852 | |
| 1853 | return 0; |
| 1854 | |
| 1855 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
| 1856 | err_unreg_notifier: |
| 1857 | nf_conntrack_unregister_notifier(&ctnl_notifier); |
| 1858 | err_unreg_exp_subsys: |
| 1859 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); |
| 1860 | #endif |
| 1861 | err_unreg_subsys: |
| 1862 | nfnetlink_subsys_unregister(&ctnl_subsys); |
| 1863 | err_out: |
| 1864 | return ret; |
| 1865 | } |
| 1866 | |
| 1867 | static void __exit ctnetlink_exit(void) |
| 1868 | { |
| 1869 | printk("ctnetlink: unregistering from nfnetlink.\n"); |
| 1870 | |
| 1871 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
| 1872 | nf_ct_expect_unregister_notifier(&ctnl_notifier_exp); |
| 1873 | nf_conntrack_unregister_notifier(&ctnl_notifier); |
| 1874 | #endif |
| 1875 | |
| 1876 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); |
| 1877 | nfnetlink_subsys_unregister(&ctnl_subsys); |
| 1878 | return; |
| 1879 | } |
| 1880 | |
| 1881 | module_init(ctnetlink_init); |
| 1882 | module_exit(ctnetlink_exit); |