Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 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-2005 by Harald Welte <laforge@gnumonks.org> |
| 6 | * (C) 2003 by Patrick Mchardy <kaber@trash.net> |
| 7 | * (C) 2005 by Pablo Neira Ayuso <pablo@eurodev.net> |
| 8 | * |
| 9 | * I've reworked this stuff to use attributes instead of conntrack |
| 10 | * structures. 5.44 am. I need more tea. --pablo 05/07/11. |
| 11 | * |
| 12 | * Initial connection tracking via netlink development funded and |
| 13 | * generally made possible by Network Robots, Inc. (www.networkrobots.com) |
| 14 | * |
| 15 | * Further development of this code funded by Astaro AG (http://www.astaro.com) |
| 16 | * |
| 17 | * This software may be used and distributed according to the terms |
| 18 | * of the GNU General Public License, incorporated herein by reference. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/timer.h> |
| 26 | #include <linux/skbuff.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/netlink.h> |
| 29 | #include <linux/spinlock.h> |
| 30 | #include <linux/notifier.h> |
| 31 | #include <linux/rtnetlink.h> |
| 32 | |
| 33 | #include <linux/netfilter.h> |
| 34 | #include <linux/netfilter_ipv4.h> |
| 35 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 36 | #include <linux/netfilter_ipv4/ip_conntrack.h> |
| 37 | #include <linux/netfilter_ipv4/ip_conntrack_core.h> |
| 38 | #include <linux/netfilter_ipv4/ip_conntrack_helper.h> |
| 39 | #include <linux/netfilter_ipv4/ip_conntrack_protocol.h> |
| 40 | #include <linux/netfilter_ipv4/ip_nat_protocol.h> |
| 41 | |
| 42 | #include <linux/netfilter/nfnetlink.h> |
| 43 | #include <linux/netfilter/nfnetlink_conntrack.h> |
| 44 | |
| 45 | MODULE_LICENSE("GPL"); |
| 46 | |
| 47 | static char __initdata version[] = "0.90"; |
| 48 | |
| 49 | #if 0 |
| 50 | #define DEBUGP printk |
| 51 | #else |
| 52 | #define DEBUGP(format, args...) |
| 53 | #endif |
| 54 | |
| 55 | |
| 56 | static inline int |
| 57 | ctnetlink_dump_tuples_proto(struct sk_buff *skb, |
| 58 | const struct ip_conntrack_tuple *tuple) |
| 59 | { |
| 60 | struct ip_conntrack_protocol *proto; |
| 61 | |
| 62 | NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum); |
| 63 | |
| 64 | proto = ip_conntrack_proto_find_get(tuple->dst.protonum); |
| 65 | if (proto && proto->tuple_to_nfattr) |
| 66 | return proto->tuple_to_nfattr(skb, tuple); |
| 67 | |
| 68 | return 0; |
| 69 | |
| 70 | nfattr_failure: |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | static inline int |
| 75 | ctnetlink_dump_tuples(struct sk_buff *skb, |
| 76 | const struct ip_conntrack_tuple *tuple) |
| 77 | { |
| 78 | struct nfattr *nest_parms; |
| 79 | |
| 80 | nest_parms = NFA_NEST(skb, CTA_TUPLE_IP); |
| 81 | NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t), &tuple->src.ip); |
| 82 | NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t), &tuple->dst.ip); |
| 83 | NFA_NEST_END(skb, nest_parms); |
| 84 | |
| 85 | nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO); |
| 86 | ctnetlink_dump_tuples_proto(skb, tuple); |
| 87 | NFA_NEST_END(skb, nest_parms); |
| 88 | |
| 89 | return 0; |
| 90 | |
| 91 | nfattr_failure: |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | static inline int |
| 96 | ctnetlink_dump_status(struct sk_buff *skb, const struct ip_conntrack *ct) |
| 97 | { |
| 98 | u_int32_t status = htonl((u_int32_t) ct->status); |
| 99 | NFA_PUT(skb, CTA_STATUS, sizeof(status), &status); |
| 100 | return 0; |
| 101 | |
| 102 | nfattr_failure: |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | static inline int |
| 107 | ctnetlink_dump_timeout(struct sk_buff *skb, const struct ip_conntrack *ct) |
| 108 | { |
| 109 | long timeout_l = ct->timeout.expires - jiffies; |
| 110 | u_int32_t timeout; |
| 111 | |
| 112 | if (timeout_l < 0) |
| 113 | timeout = 0; |
| 114 | else |
| 115 | timeout = htonl(timeout_l / HZ); |
| 116 | |
| 117 | NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout); |
| 118 | return 0; |
| 119 | |
| 120 | nfattr_failure: |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | static inline int |
| 125 | ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct ip_conntrack *ct) |
| 126 | { |
| 127 | struct ip_conntrack_protocol *proto = ip_conntrack_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum); |
| 128 | |
| 129 | struct nfattr *nest_proto; |
| 130 | int ret; |
| 131 | |
| 132 | if (!proto || !proto->to_nfattr) |
| 133 | return 0; |
| 134 | |
| 135 | nest_proto = NFA_NEST(skb, CTA_PROTOINFO); |
| 136 | |
| 137 | ret = proto->to_nfattr(skb, nest_proto, ct); |
| 138 | |
| 139 | ip_conntrack_proto_put(proto); |
| 140 | |
| 141 | NFA_NEST_END(skb, nest_proto); |
| 142 | |
| 143 | return ret; |
| 144 | |
| 145 | nfattr_failure: |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | static inline int |
| 150 | ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct ip_conntrack *ct) |
| 151 | { |
| 152 | struct nfattr *nest_helper; |
| 153 | |
| 154 | if (!ct->helper) |
| 155 | return 0; |
| 156 | |
| 157 | nest_helper = NFA_NEST(skb, CTA_HELP); |
| 158 | NFA_PUT(skb, CTA_HELP_NAME, CTA_HELP_MAXNAMESIZE, &ct->helper->name); |
| 159 | |
| 160 | if (ct->helper->to_nfattr) |
| 161 | ct->helper->to_nfattr(skb, ct); |
| 162 | |
| 163 | NFA_NEST_END(skb, nest_helper); |
| 164 | |
| 165 | return 0; |
| 166 | |
| 167 | nfattr_failure: |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | #ifdef CONFIG_IP_NF_CT_ACCT |
| 172 | static inline int |
| 173 | ctnetlink_dump_counters(struct sk_buff *skb, const struct ip_conntrack *ct, |
| 174 | enum ip_conntrack_dir dir) |
| 175 | { |
| 176 | enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; |
| 177 | struct nfattr *nest_count = NFA_NEST(skb, type); |
| 178 | u_int64_t tmp; |
| 179 | |
| 180 | tmp = cpu_to_be64(ct->counters[dir].packets); |
| 181 | NFA_PUT(skb, CTA_COUNTERS_PACKETS, sizeof(u_int64_t), &tmp); |
| 182 | |
| 183 | tmp = cpu_to_be64(ct->counters[dir].bytes); |
| 184 | NFA_PUT(skb, CTA_COUNTERS_BYTES, sizeof(u_int64_t), &tmp); |
| 185 | |
| 186 | NFA_NEST_END(skb, nest_count); |
| 187 | |
| 188 | return 0; |
| 189 | |
| 190 | nfattr_failure: |
| 191 | return -1; |
| 192 | } |
| 193 | #else |
| 194 | #define ctnetlink_dump_counters(a, b, c) (0) |
| 195 | #endif |
| 196 | |
| 197 | #ifdef CONFIG_IP_NF_CONNTRACK_MARK |
| 198 | static inline int |
| 199 | ctnetlink_dump_mark(struct sk_buff *skb, const struct ip_conntrack *ct) |
| 200 | { |
| 201 | u_int32_t mark = htonl(ct->mark); |
| 202 | |
| 203 | NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark); |
| 204 | return 0; |
| 205 | |
| 206 | nfattr_failure: |
| 207 | return -1; |
| 208 | } |
| 209 | #else |
| 210 | #define ctnetlink_dump_mark(a, b) (0) |
| 211 | #endif |
| 212 | |
| 213 | static inline int |
| 214 | ctnetlink_dump_id(struct sk_buff *skb, const struct ip_conntrack *ct) |
| 215 | { |
| 216 | u_int32_t id = htonl(ct->id); |
| 217 | NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id); |
| 218 | return 0; |
| 219 | |
| 220 | nfattr_failure: |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | static inline int |
| 225 | ctnetlink_dump_use(struct sk_buff *skb, const struct ip_conntrack *ct) |
| 226 | { |
| 227 | unsigned int use = htonl(atomic_read(&ct->ct_general.use)); |
| 228 | |
| 229 | NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use); |
| 230 | return 0; |
| 231 | |
| 232 | nfattr_failure: |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple) |
| 237 | |
| 238 | static int |
| 239 | ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, |
| 240 | int event, int nowait, |
| 241 | const struct ip_conntrack *ct) |
| 242 | { |
| 243 | struct nlmsghdr *nlh; |
| 244 | struct nfgenmsg *nfmsg; |
| 245 | struct nfattr *nest_parms; |
| 246 | unsigned char *b; |
| 247 | |
| 248 | b = skb->tail; |
| 249 | |
| 250 | event |= NFNL_SUBSYS_CTNETLINK << 8; |
| 251 | nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg)); |
| 252 | nfmsg = NLMSG_DATA(nlh); |
| 253 | |
| 254 | nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0; |
| 255 | nfmsg->nfgen_family = AF_INET; |
| 256 | nfmsg->version = NFNETLINK_V0; |
| 257 | nfmsg->res_id = 0; |
| 258 | |
| 259 | nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG); |
| 260 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
| 261 | goto nfattr_failure; |
| 262 | NFA_NEST_END(skb, nest_parms); |
| 263 | |
| 264 | nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY); |
| 265 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0) |
| 266 | goto nfattr_failure; |
| 267 | NFA_NEST_END(skb, nest_parms); |
| 268 | |
| 269 | if (ctnetlink_dump_status(skb, ct) < 0 || |
| 270 | ctnetlink_dump_timeout(skb, ct) < 0 || |
| 271 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || |
| 272 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 || |
| 273 | ctnetlink_dump_protoinfo(skb, ct) < 0 || |
| 274 | ctnetlink_dump_helpinfo(skb, ct) < 0 || |
| 275 | ctnetlink_dump_mark(skb, ct) < 0 || |
| 276 | ctnetlink_dump_id(skb, ct) < 0 || |
| 277 | ctnetlink_dump_use(skb, ct) < 0) |
| 278 | goto nfattr_failure; |
| 279 | |
| 280 | nlh->nlmsg_len = skb->tail - b; |
| 281 | return skb->len; |
| 282 | |
| 283 | nlmsg_failure: |
| 284 | nfattr_failure: |
| 285 | skb_trim(skb, b - skb->data); |
| 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS |
| 290 | static int ctnetlink_conntrack_event(struct notifier_block *this, |
| 291 | unsigned long events, void *ptr) |
| 292 | { |
| 293 | struct nlmsghdr *nlh; |
| 294 | struct nfgenmsg *nfmsg; |
| 295 | struct nfattr *nest_parms; |
| 296 | struct ip_conntrack *ct = (struct ip_conntrack *)ptr; |
| 297 | struct sk_buff *skb; |
| 298 | unsigned int type; |
| 299 | unsigned char *b; |
| 300 | unsigned int flags = 0, groups; |
| 301 | |
| 302 | /* ignore our fake conntrack entry */ |
| 303 | if (ct == &ip_conntrack_untracked) |
| 304 | return NOTIFY_DONE; |
| 305 | |
| 306 | if (events & IPCT_DESTROY) { |
| 307 | type = IPCTNL_MSG_CT_DELETE; |
| 308 | groups = NF_NETLINK_CONNTRACK_DESTROY; |
| 309 | goto alloc_skb; |
| 310 | } |
| 311 | if (events & (IPCT_NEW | IPCT_RELATED)) { |
| 312 | type = IPCTNL_MSG_CT_NEW; |
| 313 | flags = NLM_F_CREATE|NLM_F_EXCL; |
| 314 | /* dump everything */ |
| 315 | events = ~0UL; |
| 316 | groups = NF_NETLINK_CONNTRACK_NEW; |
| 317 | goto alloc_skb; |
| 318 | } |
| 319 | if (events & (IPCT_STATUS | |
| 320 | IPCT_PROTOINFO | |
| 321 | IPCT_HELPER | |
| 322 | IPCT_HELPINFO | |
| 323 | IPCT_NATINFO)) { |
| 324 | type = IPCTNL_MSG_CT_NEW; |
| 325 | groups = NF_NETLINK_CONNTRACK_UPDATE; |
| 326 | goto alloc_skb; |
| 327 | } |
| 328 | |
| 329 | return NOTIFY_DONE; |
| 330 | |
| 331 | alloc_skb: |
| 332 | /* FIXME: Check if there are any listeners before, don't hurt performance */ |
| 333 | |
| 334 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
| 335 | if (!skb) |
| 336 | return NOTIFY_DONE; |
| 337 | |
| 338 | b = skb->tail; |
| 339 | |
| 340 | type |= NFNL_SUBSYS_CTNETLINK << 8; |
| 341 | nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg)); |
| 342 | nfmsg = NLMSG_DATA(nlh); |
| 343 | |
| 344 | nlh->nlmsg_flags = flags; |
| 345 | nfmsg->nfgen_family = AF_INET; |
| 346 | nfmsg->version = NFNETLINK_V0; |
| 347 | nfmsg->res_id = 0; |
| 348 | |
| 349 | nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG); |
| 350 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
| 351 | goto nfattr_failure; |
| 352 | NFA_NEST_END(skb, nest_parms); |
| 353 | |
| 354 | nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY); |
| 355 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0) |
| 356 | goto nfattr_failure; |
| 357 | NFA_NEST_END(skb, nest_parms); |
| 358 | |
| 359 | /* NAT stuff is now a status flag */ |
| 360 | if ((events & IPCT_STATUS || events & IPCT_NATINFO) |
| 361 | && ctnetlink_dump_status(skb, ct) < 0) |
| 362 | goto nfattr_failure; |
| 363 | if (events & IPCT_REFRESH |
| 364 | && ctnetlink_dump_timeout(skb, ct) < 0) |
| 365 | goto nfattr_failure; |
| 366 | if (events & IPCT_PROTOINFO |
| 367 | && ctnetlink_dump_protoinfo(skb, ct) < 0) |
| 368 | goto nfattr_failure; |
| 369 | if (events & IPCT_HELPINFO |
| 370 | && ctnetlink_dump_helpinfo(skb, ct) < 0) |
| 371 | goto nfattr_failure; |
| 372 | |
| 373 | if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || |
| 374 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0) |
| 375 | goto nfattr_failure; |
| 376 | |
| 377 | nlh->nlmsg_len = skb->tail - b; |
| 378 | nfnetlink_send(skb, 0, groups, 0); |
| 379 | return NOTIFY_DONE; |
| 380 | |
| 381 | nlmsg_failure: |
| 382 | nfattr_failure: |
| 383 | kfree_skb(skb); |
| 384 | return NOTIFY_DONE; |
| 385 | } |
| 386 | #endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */ |
| 387 | |
| 388 | static int ctnetlink_done(struct netlink_callback *cb) |
| 389 | { |
| 390 | DEBUGP("entered %s\n", __FUNCTION__); |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static int |
| 395 | ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) |
| 396 | { |
| 397 | struct ip_conntrack *ct = NULL; |
| 398 | struct ip_conntrack_tuple_hash *h; |
| 399 | struct list_head *i; |
| 400 | u_int32_t *id = (u_int32_t *) &cb->args[1]; |
| 401 | |
| 402 | DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__, |
| 403 | cb->args[0], *id); |
| 404 | |
| 405 | read_lock_bh(&ip_conntrack_lock); |
| 406 | for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) { |
| 407 | list_for_each(i, &ip_conntrack_hash[cb->args[0]]) { |
| 408 | h = (struct ip_conntrack_tuple_hash *) i; |
| 409 | if (DIRECTION(h) != IP_CT_DIR_ORIGINAL) |
| 410 | continue; |
| 411 | ct = tuplehash_to_ctrack(h); |
| 412 | if (ct->id <= *id) |
| 413 | continue; |
| 414 | if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid, |
| 415 | cb->nlh->nlmsg_seq, |
| 416 | IPCTNL_MSG_CT_NEW, |
| 417 | 1, ct) < 0) |
| 418 | goto out; |
| 419 | *id = ct->id; |
| 420 | } |
| 421 | } |
| 422 | out: |
| 423 | read_unlock_bh(&ip_conntrack_lock); |
| 424 | |
| 425 | DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id); |
| 426 | |
| 427 | return skb->len; |
| 428 | } |
| 429 | |
| 430 | #ifdef CONFIG_IP_NF_CT_ACCT |
| 431 | static int |
| 432 | ctnetlink_dump_table_w(struct sk_buff *skb, struct netlink_callback *cb) |
| 433 | { |
| 434 | struct ip_conntrack *ct = NULL; |
| 435 | struct ip_conntrack_tuple_hash *h; |
| 436 | struct list_head *i; |
| 437 | u_int32_t *id = (u_int32_t *) &cb->args[1]; |
| 438 | |
| 439 | DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__, |
| 440 | cb->args[0], *id); |
| 441 | |
| 442 | write_lock_bh(&ip_conntrack_lock); |
| 443 | for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) { |
| 444 | list_for_each(i, &ip_conntrack_hash[cb->args[0]]) { |
| 445 | h = (struct ip_conntrack_tuple_hash *) i; |
| 446 | if (DIRECTION(h) != IP_CT_DIR_ORIGINAL) |
| 447 | continue; |
| 448 | ct = tuplehash_to_ctrack(h); |
| 449 | if (ct->id <= *id) |
| 450 | continue; |
| 451 | if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid, |
| 452 | cb->nlh->nlmsg_seq, |
| 453 | IPCTNL_MSG_CT_NEW, |
| 454 | 1, ct) < 0) |
| 455 | goto out; |
| 456 | *id = ct->id; |
| 457 | |
| 458 | memset(&ct->counters, 0, sizeof(ct->counters)); |
| 459 | } |
| 460 | } |
| 461 | out: |
| 462 | write_unlock_bh(&ip_conntrack_lock); |
| 463 | |
| 464 | DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id); |
| 465 | |
| 466 | return skb->len; |
| 467 | } |
| 468 | #endif |
| 469 | |
| 470 | static const int cta_min_ip[CTA_IP_MAX] = { |
| 471 | [CTA_IP_V4_SRC-1] = sizeof(u_int32_t), |
| 472 | [CTA_IP_V4_DST-1] = sizeof(u_int32_t), |
| 473 | }; |
| 474 | |
| 475 | static inline int |
| 476 | ctnetlink_parse_tuple_ip(struct nfattr *attr, struct ip_conntrack_tuple *tuple) |
| 477 | { |
| 478 | struct nfattr *tb[CTA_IP_MAX]; |
| 479 | |
| 480 | DEBUGP("entered %s\n", __FUNCTION__); |
| 481 | |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 482 | |
| 483 | if (nfattr_parse_nested(tb, CTA_IP_MAX, attr) < 0) |
| 484 | goto nfattr_failure; |
| 485 | |
| 486 | if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip)) |
| 487 | return -EINVAL; |
| 488 | |
| 489 | if (!tb[CTA_IP_V4_SRC-1]) |
| 490 | return -EINVAL; |
| 491 | tuple->src.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]); |
| 492 | |
| 493 | if (!tb[CTA_IP_V4_DST-1]) |
| 494 | return -EINVAL; |
| 495 | tuple->dst.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]); |
| 496 | |
| 497 | DEBUGP("leaving\n"); |
| 498 | |
| 499 | return 0; |
| 500 | |
| 501 | nfattr_failure: |
| 502 | return -1; |
| 503 | } |
| 504 | |
| 505 | static const int cta_min_proto[CTA_PROTO_MAX] = { |
| 506 | [CTA_PROTO_NUM-1] = sizeof(u_int16_t), |
| 507 | [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t), |
| 508 | [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t), |
| 509 | [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t), |
| 510 | [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t), |
| 511 | [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t), |
| 512 | }; |
| 513 | |
| 514 | static inline int |
| 515 | ctnetlink_parse_tuple_proto(struct nfattr *attr, |
| 516 | struct ip_conntrack_tuple *tuple) |
| 517 | { |
| 518 | struct nfattr *tb[CTA_PROTO_MAX]; |
| 519 | struct ip_conntrack_protocol *proto; |
| 520 | int ret = 0; |
| 521 | |
| 522 | DEBUGP("entered %s\n", __FUNCTION__); |
| 523 | |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 524 | if (nfattr_parse_nested(tb, CTA_PROTO_MAX, attr) < 0) |
| 525 | goto nfattr_failure; |
| 526 | |
| 527 | if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto)) |
| 528 | return -EINVAL; |
| 529 | |
| 530 | if (!tb[CTA_PROTO_NUM-1]) |
| 531 | return -EINVAL; |
| 532 | tuple->dst.protonum = *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]); |
| 533 | |
| 534 | proto = ip_conntrack_proto_find_get(tuple->dst.protonum); |
| 535 | |
| 536 | if (likely(proto && proto->nfattr_to_tuple)) { |
| 537 | ret = proto->nfattr_to_tuple(tb, tuple); |
| 538 | ip_conntrack_proto_put(proto); |
| 539 | } |
| 540 | |
| 541 | return ret; |
| 542 | |
| 543 | nfattr_failure: |
| 544 | return -1; |
| 545 | } |
| 546 | |
| 547 | static inline int |
| 548 | ctnetlink_parse_tuple(struct nfattr *cda[], struct ip_conntrack_tuple *tuple, |
| 549 | enum ctattr_tuple type) |
| 550 | { |
| 551 | struct nfattr *tb[CTA_TUPLE_MAX]; |
| 552 | int err; |
| 553 | |
| 554 | DEBUGP("entered %s\n", __FUNCTION__); |
| 555 | |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 556 | memset(tuple, 0, sizeof(*tuple)); |
| 557 | |
| 558 | if (nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]) < 0) |
| 559 | goto nfattr_failure; |
| 560 | |
| 561 | if (!tb[CTA_TUPLE_IP-1]) |
| 562 | return -EINVAL; |
| 563 | |
| 564 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple); |
| 565 | if (err < 0) |
| 566 | return err; |
| 567 | |
| 568 | if (!tb[CTA_TUPLE_PROTO-1]) |
| 569 | return -EINVAL; |
| 570 | |
| 571 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple); |
| 572 | if (err < 0) |
| 573 | return err; |
| 574 | |
| 575 | /* orig and expect tuples get DIR_ORIGINAL */ |
| 576 | if (type == CTA_TUPLE_REPLY) |
| 577 | tuple->dst.dir = IP_CT_DIR_REPLY; |
| 578 | else |
| 579 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; |
| 580 | |
| 581 | DUMP_TUPLE(tuple); |
| 582 | |
| 583 | DEBUGP("leaving\n"); |
| 584 | |
| 585 | return 0; |
| 586 | |
| 587 | nfattr_failure: |
| 588 | return -1; |
| 589 | } |
| 590 | |
| 591 | #ifdef CONFIG_IP_NF_NAT_NEEDED |
| 592 | static const int cta_min_protonat[CTA_PROTONAT_MAX] = { |
| 593 | [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t), |
| 594 | [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t), |
| 595 | }; |
| 596 | |
| 597 | static int ctnetlink_parse_nat_proto(struct nfattr *attr, |
| 598 | const struct ip_conntrack *ct, |
| 599 | struct ip_nat_range *range) |
| 600 | { |
| 601 | struct nfattr *tb[CTA_PROTONAT_MAX]; |
| 602 | struct ip_nat_protocol *npt; |
| 603 | |
| 604 | DEBUGP("entered %s\n", __FUNCTION__); |
| 605 | |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 606 | if (nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr) < 0) |
| 607 | goto nfattr_failure; |
| 608 | |
| 609 | if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat)) |
| 610 | goto nfattr_failure; |
| 611 | |
| 612 | npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum); |
| 613 | if (!npt) |
| 614 | return 0; |
| 615 | |
| 616 | if (!npt->nfattr_to_range) { |
| 617 | ip_nat_proto_put(npt); |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */ |
| 622 | if (npt->nfattr_to_range(tb, range) > 0) |
| 623 | range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED; |
| 624 | |
| 625 | ip_nat_proto_put(npt); |
| 626 | |
| 627 | DEBUGP("leaving\n"); |
| 628 | return 0; |
| 629 | |
| 630 | nfattr_failure: |
| 631 | return -1; |
| 632 | } |
| 633 | |
| 634 | static inline int |
| 635 | ctnetlink_parse_nat(struct nfattr *cda[], |
| 636 | const struct ip_conntrack *ct, struct ip_nat_range *range) |
| 637 | { |
| 638 | struct nfattr *tb[CTA_NAT_MAX]; |
| 639 | int err; |
| 640 | |
| 641 | DEBUGP("entered %s\n", __FUNCTION__); |
| 642 | |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 643 | memset(range, 0, sizeof(*range)); |
| 644 | |
| 645 | if (nfattr_parse_nested(tb, CTA_NAT_MAX, cda[CTA_NAT-1]) < 0) |
| 646 | goto nfattr_failure; |
| 647 | |
| 648 | if (tb[CTA_NAT_MINIP-1]) |
| 649 | range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]); |
| 650 | |
| 651 | if (!tb[CTA_NAT_MAXIP-1]) |
| 652 | range->max_ip = range->min_ip; |
| 653 | else |
| 654 | range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]); |
| 655 | |
| 656 | if (range->min_ip) |
| 657 | range->flags |= IP_NAT_RANGE_MAP_IPS; |
| 658 | |
| 659 | if (!tb[CTA_NAT_PROTO-1]) |
| 660 | return 0; |
| 661 | |
| 662 | err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range); |
| 663 | if (err < 0) |
| 664 | return err; |
| 665 | |
| 666 | DEBUGP("leaving\n"); |
| 667 | return 0; |
| 668 | |
| 669 | nfattr_failure: |
| 670 | return -1; |
| 671 | } |
| 672 | #endif |
| 673 | |
| 674 | static inline int |
| 675 | ctnetlink_parse_help(struct nfattr *attr, char **helper_name) |
| 676 | { |
| 677 | struct nfattr *tb[CTA_HELP_MAX]; |
| 678 | |
| 679 | DEBUGP("entered %s\n", __FUNCTION__); |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 680 | |
| 681 | if (nfattr_parse_nested(tb, CTA_HELP_MAX, attr) < 0) |
| 682 | goto nfattr_failure; |
| 683 | |
| 684 | if (!tb[CTA_HELP_NAME-1]) |
| 685 | return -EINVAL; |
| 686 | |
| 687 | *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]); |
| 688 | |
| 689 | return 0; |
| 690 | |
| 691 | nfattr_failure: |
| 692 | return -1; |
| 693 | } |
| 694 | |
| 695 | static int |
| 696 | ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, |
| 697 | struct nlmsghdr *nlh, struct nfattr *cda[], int *errp) |
| 698 | { |
| 699 | struct ip_conntrack_tuple_hash *h; |
| 700 | struct ip_conntrack_tuple tuple; |
| 701 | struct ip_conntrack *ct; |
| 702 | int err = 0; |
| 703 | |
| 704 | DEBUGP("entered %s\n", __FUNCTION__); |
| 705 | |
| 706 | if (cda[CTA_TUPLE_ORIG-1]) |
| 707 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG); |
| 708 | else if (cda[CTA_TUPLE_REPLY-1]) |
| 709 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY); |
| 710 | else { |
| 711 | /* Flush the whole table */ |
| 712 | ip_conntrack_flush(); |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | if (err < 0) |
| 717 | return err; |
| 718 | |
| 719 | h = ip_conntrack_find_get(&tuple, NULL); |
| 720 | if (!h) { |
| 721 | DEBUGP("tuple not found in conntrack hash\n"); |
| 722 | return -ENOENT; |
| 723 | } |
| 724 | |
| 725 | ct = tuplehash_to_ctrack(h); |
| 726 | |
| 727 | if (cda[CTA_ID-1]) { |
| 728 | u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1])); |
| 729 | if (ct->id != id) { |
| 730 | ip_conntrack_put(ct); |
| 731 | return -ENOENT; |
| 732 | } |
| 733 | } |
| 734 | if (del_timer(&ct->timeout)) { |
| 735 | ip_conntrack_put(ct); |
| 736 | ct->timeout.function((unsigned long)ct); |
| 737 | return 0; |
| 738 | } |
| 739 | ip_conntrack_put(ct); |
| 740 | DEBUGP("leaving\n"); |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | static int |
| 746 | ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, |
| 747 | struct nlmsghdr *nlh, struct nfattr *cda[], int *errp) |
| 748 | { |
| 749 | struct ip_conntrack_tuple_hash *h; |
| 750 | struct ip_conntrack_tuple tuple; |
| 751 | struct ip_conntrack *ct; |
| 752 | struct sk_buff *skb2 = NULL; |
| 753 | int err = 0; |
| 754 | |
| 755 | DEBUGP("entered %s\n", __FUNCTION__); |
| 756 | |
| 757 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
| 758 | struct nfgenmsg *msg = NLMSG_DATA(nlh); |
| 759 | u32 rlen; |
| 760 | |
| 761 | if (msg->nfgen_family != AF_INET) |
| 762 | return -EAFNOSUPPORT; |
| 763 | |
| 764 | if (NFNL_MSG_TYPE(nlh->nlmsg_type) == |
| 765 | IPCTNL_MSG_CT_GET_CTRZERO) { |
| 766 | #ifdef CONFIG_IP_NF_CT_ACCT |
| 767 | if ((*errp = netlink_dump_start(ctnl, skb, nlh, |
| 768 | ctnetlink_dump_table_w, |
| 769 | ctnetlink_done)) != 0) |
| 770 | return -EINVAL; |
| 771 | #else |
| 772 | return -ENOTSUPP; |
| 773 | #endif |
| 774 | } else { |
| 775 | if ((*errp = netlink_dump_start(ctnl, skb, nlh, |
| 776 | ctnetlink_dump_table, |
| 777 | ctnetlink_done)) != 0) |
| 778 | return -EINVAL; |
| 779 | } |
| 780 | |
| 781 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 782 | if (rlen > skb->len) |
| 783 | rlen = skb->len; |
| 784 | skb_pull(skb, rlen); |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | if (cda[CTA_TUPLE_ORIG-1]) |
| 789 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG); |
| 790 | else if (cda[CTA_TUPLE_REPLY-1]) |
| 791 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY); |
| 792 | else |
| 793 | return -EINVAL; |
| 794 | |
| 795 | if (err < 0) |
| 796 | return err; |
| 797 | |
| 798 | h = ip_conntrack_find_get(&tuple, NULL); |
| 799 | if (!h) { |
| 800 | DEBUGP("tuple not found in conntrack hash"); |
| 801 | return -ENOENT; |
| 802 | } |
| 803 | DEBUGP("tuple found\n"); |
| 804 | ct = tuplehash_to_ctrack(h); |
| 805 | |
| 806 | err = -ENOMEM; |
| 807 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
| 808 | if (!skb2) { |
| 809 | ip_conntrack_put(ct); |
| 810 | return -ENOMEM; |
| 811 | } |
| 812 | NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid; |
| 813 | |
| 814 | err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, |
| 815 | IPCTNL_MSG_CT_NEW, 1, ct); |
| 816 | ip_conntrack_put(ct); |
| 817 | if (err <= 0) |
| 818 | goto out; |
| 819 | |
| 820 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); |
| 821 | if (err < 0) |
| 822 | goto out; |
| 823 | |
| 824 | DEBUGP("leaving\n"); |
| 825 | return 0; |
| 826 | |
| 827 | out: |
| 828 | if (skb2) |
| 829 | kfree_skb(skb2); |
| 830 | return -1; |
| 831 | } |
| 832 | |
| 833 | static inline int |
| 834 | ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[]) |
| 835 | { |
| 836 | unsigned long d, status = *(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]); |
| 837 | d = ct->status ^ status; |
| 838 | |
| 839 | if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING)) |
| 840 | /* unchangeable */ |
| 841 | return -EINVAL; |
| 842 | |
| 843 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) |
| 844 | /* SEEN_REPLY bit can only be set */ |
| 845 | return -EINVAL; |
| 846 | |
| 847 | |
| 848 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) |
| 849 | /* ASSURED bit can only be set */ |
| 850 | return -EINVAL; |
| 851 | |
| 852 | if (cda[CTA_NAT-1]) { |
| 853 | #ifndef CONFIG_IP_NF_NAT_NEEDED |
| 854 | return -EINVAL; |
| 855 | #else |
| 856 | unsigned int hooknum; |
| 857 | struct ip_nat_range range; |
| 858 | |
| 859 | if (ctnetlink_parse_nat(cda, ct, &range) < 0) |
| 860 | return -EINVAL; |
| 861 | |
| 862 | DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n", |
| 863 | NIPQUAD(range.min_ip), NIPQUAD(range.max_ip), |
| 864 | htons(range.min.all), htons(range.max.all)); |
| 865 | |
| 866 | /* This is tricky but it works. ip_nat_setup_info needs the |
| 867 | * hook number as parameter, so let's do the correct |
| 868 | * conversion and run away */ |
| 869 | if (status & IPS_SRC_NAT_DONE) |
| 870 | hooknum = NF_IP_POST_ROUTING; /* IP_NAT_MANIP_SRC */ |
| 871 | else if (status & IPS_DST_NAT_DONE) |
| 872 | hooknum = NF_IP_PRE_ROUTING; /* IP_NAT_MANIP_DST */ |
| 873 | else |
| 874 | return -EINVAL; /* Missing NAT flags */ |
| 875 | |
| 876 | DEBUGP("NAT status: %lu\n", |
| 877 | status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK)); |
| 878 | |
| 879 | if (ip_nat_initialized(ct, hooknum)) |
| 880 | return -EEXIST; |
| 881 | ip_nat_setup_info(ct, &range, hooknum); |
| 882 | |
| 883 | DEBUGP("NAT status after setup_info: %lu\n", |
| 884 | ct->status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK)); |
| 885 | #endif |
| 886 | } |
| 887 | |
| 888 | /* Be careful here, modifying NAT bits can screw up things, |
| 889 | * so don't let users modify them directly if they don't pass |
| 890 | * ip_nat_range. */ |
| 891 | ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK); |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | |
| 896 | static inline int |
| 897 | ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[]) |
| 898 | { |
| 899 | struct ip_conntrack_helper *helper; |
| 900 | char *helpname; |
| 901 | int err; |
| 902 | |
| 903 | DEBUGP("entered %s\n", __FUNCTION__); |
| 904 | |
| 905 | /* don't change helper of sibling connections */ |
| 906 | if (ct->master) |
| 907 | return -EINVAL; |
| 908 | |
| 909 | err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname); |
| 910 | if (err < 0) |
| 911 | return err; |
| 912 | |
| 913 | helper = __ip_conntrack_helper_find_byname(helpname); |
| 914 | if (!helper) { |
| 915 | if (!strcmp(helpname, "")) |
| 916 | helper = NULL; |
| 917 | else |
| 918 | return -EINVAL; |
| 919 | } |
| 920 | |
| 921 | if (ct->helper) { |
| 922 | if (!helper) { |
| 923 | /* we had a helper before ... */ |
| 924 | ip_ct_remove_expectations(ct); |
| 925 | ct->helper = NULL; |
| 926 | } else { |
| 927 | /* need to zero data of old helper */ |
| 928 | memset(&ct->help, 0, sizeof(ct->help)); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | ct->helper = helper; |
| 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | static inline int |
| 938 | ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[]) |
| 939 | { |
| 940 | u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1])); |
| 941 | |
| 942 | if (!del_timer(&ct->timeout)) |
| 943 | return -ETIME; |
| 944 | |
| 945 | ct->timeout.expires = jiffies + timeout * HZ; |
| 946 | add_timer(&ct->timeout); |
| 947 | |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | static int |
| 952 | ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[]) |
| 953 | { |
| 954 | int err; |
| 955 | |
| 956 | DEBUGP("entered %s\n", __FUNCTION__); |
| 957 | |
| 958 | if (cda[CTA_HELP-1]) { |
| 959 | err = ctnetlink_change_helper(ct, cda); |
| 960 | if (err < 0) |
| 961 | return err; |
| 962 | } |
| 963 | |
| 964 | if (cda[CTA_TIMEOUT-1]) { |
| 965 | err = ctnetlink_change_timeout(ct, cda); |
| 966 | if (err < 0) |
| 967 | return err; |
| 968 | } |
| 969 | |
| 970 | if (cda[CTA_STATUS-1]) { |
| 971 | err = ctnetlink_change_status(ct, cda); |
| 972 | if (err < 0) |
| 973 | return err; |
| 974 | } |
| 975 | |
| 976 | DEBUGP("all done\n"); |
| 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | static int |
| 981 | ctnetlink_create_conntrack(struct nfattr *cda[], |
| 982 | struct ip_conntrack_tuple *otuple, |
| 983 | struct ip_conntrack_tuple *rtuple) |
| 984 | { |
| 985 | struct ip_conntrack *ct; |
| 986 | int err = -EINVAL; |
| 987 | |
| 988 | DEBUGP("entered %s\n", __FUNCTION__); |
| 989 | |
| 990 | ct = ip_conntrack_alloc(otuple, rtuple); |
| 991 | if (ct == NULL || IS_ERR(ct)) |
| 992 | return -ENOMEM; |
| 993 | |
| 994 | if (!cda[CTA_TIMEOUT-1]) |
| 995 | goto err; |
| 996 | ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1])); |
| 997 | |
| 998 | ct->timeout.expires = jiffies + ct->timeout.expires * HZ; |
| 999 | ct->status |= IPS_CONFIRMED; |
| 1000 | |
| 1001 | err = ctnetlink_change_status(ct, cda); |
| 1002 | if (err < 0) |
| 1003 | goto err; |
| 1004 | |
| 1005 | ct->helper = ip_conntrack_helper_find_get(rtuple); |
| 1006 | |
| 1007 | add_timer(&ct->timeout); |
| 1008 | ip_conntrack_hash_insert(ct); |
| 1009 | |
| 1010 | if (ct->helper) |
| 1011 | ip_conntrack_helper_put(ct->helper); |
| 1012 | |
| 1013 | DEBUGP("conntrack with id %u inserted\n", ct->id); |
| 1014 | return 0; |
| 1015 | |
| 1016 | err: |
| 1017 | ip_conntrack_free(ct); |
| 1018 | return err; |
| 1019 | } |
| 1020 | |
| 1021 | static int |
| 1022 | ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, |
| 1023 | struct nlmsghdr *nlh, struct nfattr *cda[], int *errp) |
| 1024 | { |
| 1025 | struct ip_conntrack_tuple otuple, rtuple; |
| 1026 | struct ip_conntrack_tuple_hash *h = NULL; |
| 1027 | int err = 0; |
| 1028 | |
| 1029 | DEBUGP("entered %s\n", __FUNCTION__); |
| 1030 | |
| 1031 | if (cda[CTA_TUPLE_ORIG-1]) { |
| 1032 | err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG); |
| 1033 | if (err < 0) |
| 1034 | return err; |
| 1035 | } |
| 1036 | |
| 1037 | if (cda[CTA_TUPLE_REPLY-1]) { |
| 1038 | err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY); |
| 1039 | if (err < 0) |
| 1040 | return err; |
| 1041 | } |
| 1042 | |
| 1043 | write_lock_bh(&ip_conntrack_lock); |
| 1044 | if (cda[CTA_TUPLE_ORIG-1]) |
| 1045 | h = __ip_conntrack_find(&otuple, NULL); |
| 1046 | else if (cda[CTA_TUPLE_REPLY-1]) |
| 1047 | h = __ip_conntrack_find(&rtuple, NULL); |
| 1048 | |
| 1049 | if (h == NULL) { |
| 1050 | write_unlock_bh(&ip_conntrack_lock); |
| 1051 | DEBUGP("no such conntrack, create new\n"); |
| 1052 | err = -ENOENT; |
| 1053 | if (nlh->nlmsg_flags & NLM_F_CREATE) |
| 1054 | err = ctnetlink_create_conntrack(cda, &otuple, &rtuple); |
Pablo Neira | 88aa042 | 2005-08-09 20:02:55 -0700 | [diff] [blame] | 1055 | return err; |
| 1056 | } |
| 1057 | /* implicit 'else' */ |
| 1058 | |
| 1059 | /* we only allow nat config for new conntracks */ |
| 1060 | if (cda[CTA_NAT-1]) { |
| 1061 | err = -EINVAL; |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1062 | goto out_unlock; |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /* We manipulate the conntrack inside the global conntrack table lock, |
| 1066 | * so there's no need to increase the refcount */ |
| 1067 | DEBUGP("conntrack found\n"); |
| 1068 | err = -EEXIST; |
| 1069 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) |
| 1070 | err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda); |
| 1071 | |
| 1072 | out_unlock: |
| 1073 | write_unlock_bh(&ip_conntrack_lock); |
| 1074 | return err; |
| 1075 | } |
| 1076 | |
| 1077 | /*********************************************************************** |
| 1078 | * EXPECT |
| 1079 | ***********************************************************************/ |
| 1080 | |
| 1081 | static inline int |
| 1082 | ctnetlink_exp_dump_tuple(struct sk_buff *skb, |
| 1083 | const struct ip_conntrack_tuple *tuple, |
| 1084 | enum ctattr_expect type) |
| 1085 | { |
| 1086 | struct nfattr *nest_parms = NFA_NEST(skb, type); |
| 1087 | |
| 1088 | if (ctnetlink_dump_tuples(skb, tuple) < 0) |
| 1089 | goto nfattr_failure; |
| 1090 | |
| 1091 | NFA_NEST_END(skb, nest_parms); |
| 1092 | |
| 1093 | return 0; |
| 1094 | |
| 1095 | nfattr_failure: |
| 1096 | return -1; |
| 1097 | } |
| 1098 | |
| 1099 | static inline int |
| 1100 | ctnetlink_exp_dump_expect(struct sk_buff *skb, |
| 1101 | const struct ip_conntrack_expect *exp) |
| 1102 | { |
Harald Welte | 1444fc5 | 2005-08-09 20:04:07 -0700 | [diff] [blame^] | 1103 | struct ip_conntrack *master = exp->master; |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1104 | u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ); |
| 1105 | u_int32_t id = htonl(exp->id); |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1106 | |
| 1107 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) |
| 1108 | goto nfattr_failure; |
| 1109 | if (ctnetlink_exp_dump_tuple(skb, &exp->mask, CTA_EXPECT_MASK) < 0) |
| 1110 | goto nfattr_failure; |
Harald Welte | 1444fc5 | 2005-08-09 20:04:07 -0700 | [diff] [blame^] | 1111 | if (ctnetlink_exp_dump_tuple(skb, |
| 1112 | &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
| 1113 | CTA_EXPECT_MASTER) < 0) |
| 1114 | goto nfattr_failure; |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1115 | |
| 1116 | NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout); |
| 1117 | NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id); |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1118 | |
| 1119 | return 0; |
| 1120 | |
| 1121 | nfattr_failure: |
| 1122 | return -1; |
| 1123 | } |
| 1124 | |
| 1125 | static int |
| 1126 | ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq, |
| 1127 | int event, |
| 1128 | int nowait, |
| 1129 | const struct ip_conntrack_expect *exp) |
| 1130 | { |
| 1131 | struct nlmsghdr *nlh; |
| 1132 | struct nfgenmsg *nfmsg; |
| 1133 | unsigned char *b; |
| 1134 | |
| 1135 | b = skb->tail; |
| 1136 | |
| 1137 | event |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
| 1138 | nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg)); |
| 1139 | nfmsg = NLMSG_DATA(nlh); |
| 1140 | |
| 1141 | nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0; |
| 1142 | nfmsg->nfgen_family = AF_INET; |
| 1143 | nfmsg->version = NFNETLINK_V0; |
| 1144 | nfmsg->res_id = 0; |
| 1145 | |
| 1146 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
| 1147 | goto nfattr_failure; |
| 1148 | |
| 1149 | nlh->nlmsg_len = skb->tail - b; |
| 1150 | return skb->len; |
| 1151 | |
| 1152 | nlmsg_failure: |
| 1153 | nfattr_failure: |
| 1154 | skb_trim(skb, b - skb->data); |
| 1155 | return -1; |
| 1156 | } |
| 1157 | |
| 1158 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS |
| 1159 | static int ctnetlink_expect_event(struct notifier_block *this, |
| 1160 | unsigned long events, void *ptr) |
| 1161 | { |
| 1162 | struct nlmsghdr *nlh; |
| 1163 | struct nfgenmsg *nfmsg; |
| 1164 | struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr; |
| 1165 | struct sk_buff *skb; |
| 1166 | unsigned int type; |
| 1167 | unsigned char *b; |
| 1168 | int flags = 0; |
| 1169 | u16 proto; |
| 1170 | |
| 1171 | if (events & IPEXP_NEW) { |
| 1172 | type = IPCTNL_MSG_EXP_NEW; |
| 1173 | flags = NLM_F_CREATE|NLM_F_EXCL; |
| 1174 | } else |
| 1175 | return NOTIFY_DONE; |
| 1176 | |
| 1177 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
| 1178 | if (!skb) |
| 1179 | return NOTIFY_DONE; |
| 1180 | |
| 1181 | b = skb->tail; |
| 1182 | |
| 1183 | type |= NFNL_SUBSYS_CTNETLINK << 8; |
| 1184 | nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg)); |
| 1185 | nfmsg = NLMSG_DATA(nlh); |
| 1186 | |
| 1187 | nlh->nlmsg_flags = flags; |
| 1188 | nfmsg->nfgen_family = AF_INET; |
| 1189 | nfmsg->version = NFNETLINK_V0; |
| 1190 | nfmsg->res_id = 0; |
| 1191 | |
| 1192 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
| 1193 | goto nfattr_failure; |
| 1194 | |
| 1195 | nlh->nlmsg_len = skb->tail - b; |
| 1196 | proto = exp->tuple.dst.protonum; |
| 1197 | nfnetlink_send(skb, 0, NF_NETLINK_CONNTRACK_EXP_NEW, 0); |
| 1198 | return NOTIFY_DONE; |
| 1199 | |
| 1200 | nlmsg_failure: |
| 1201 | nfattr_failure: |
| 1202 | kfree_skb(skb); |
| 1203 | return NOTIFY_DONE; |
| 1204 | } |
| 1205 | #endif |
| 1206 | |
| 1207 | static int |
| 1208 | ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) |
| 1209 | { |
| 1210 | struct ip_conntrack_expect *exp = NULL; |
| 1211 | struct list_head *i; |
| 1212 | u_int32_t *id = (u_int32_t *) &cb->args[0]; |
| 1213 | |
| 1214 | DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id); |
| 1215 | |
| 1216 | read_lock_bh(&ip_conntrack_lock); |
| 1217 | list_for_each(i, &ip_conntrack_expect_list) { |
| 1218 | exp = (struct ip_conntrack_expect *) i; |
| 1219 | if (exp->id <= *id) |
| 1220 | continue; |
| 1221 | if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid, |
| 1222 | cb->nlh->nlmsg_seq, |
| 1223 | IPCTNL_MSG_EXP_NEW, |
| 1224 | 1, exp) < 0) |
| 1225 | goto out; |
| 1226 | *id = exp->id; |
| 1227 | } |
| 1228 | out: |
| 1229 | read_unlock_bh(&ip_conntrack_lock); |
| 1230 | |
| 1231 | DEBUGP("leaving, last id=%llu\n", *id); |
| 1232 | |
| 1233 | return skb->len; |
| 1234 | } |
| 1235 | |
| 1236 | static int |
| 1237 | ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, |
| 1238 | struct nlmsghdr *nlh, struct nfattr *cda[], int *errp) |
| 1239 | { |
| 1240 | struct ip_conntrack_tuple tuple; |
| 1241 | struct ip_conntrack_expect *exp; |
| 1242 | struct sk_buff *skb2; |
| 1243 | int err = 0; |
| 1244 | |
| 1245 | DEBUGP("entered %s\n", __FUNCTION__); |
| 1246 | |
| 1247 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
| 1248 | struct nfgenmsg *msg = NLMSG_DATA(nlh); |
| 1249 | u32 rlen; |
| 1250 | |
| 1251 | if (msg->nfgen_family != AF_INET) |
| 1252 | return -EAFNOSUPPORT; |
| 1253 | |
| 1254 | if ((*errp = netlink_dump_start(ctnl, skb, nlh, |
| 1255 | ctnetlink_exp_dump_table, |
| 1256 | ctnetlink_done)) != 0) |
| 1257 | return -EINVAL; |
| 1258 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 1259 | if (rlen > skb->len) |
| 1260 | rlen = skb->len; |
| 1261 | skb_pull(skb, rlen); |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
Harald Welte | 1444fc5 | 2005-08-09 20:04:07 -0700 | [diff] [blame^] | 1265 | if (cda[CTA_EXPECT_MASTER-1]) |
| 1266 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER); |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1267 | else |
| 1268 | return -EINVAL; |
| 1269 | |
| 1270 | if (err < 0) |
| 1271 | return err; |
| 1272 | |
| 1273 | exp = ip_conntrack_expect_find_get(&tuple); |
| 1274 | if (!exp) |
| 1275 | return -ENOENT; |
| 1276 | |
| 1277 | err = -ENOMEM; |
| 1278 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1279 | if (!skb2) |
| 1280 | goto out; |
| 1281 | NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid; |
| 1282 | |
| 1283 | err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid, |
| 1284 | nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, |
| 1285 | 1, exp); |
| 1286 | if (err <= 0) |
| 1287 | goto out; |
| 1288 | |
| 1289 | ip_conntrack_expect_put(exp); |
| 1290 | |
| 1291 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); |
| 1292 | if (err < 0) |
| 1293 | goto free; |
| 1294 | |
| 1295 | return err; |
| 1296 | |
| 1297 | out: |
| 1298 | ip_conntrack_expect_put(exp); |
| 1299 | free: |
| 1300 | if (skb2) |
| 1301 | kfree_skb(skb2); |
| 1302 | return err; |
| 1303 | } |
| 1304 | |
| 1305 | static int |
| 1306 | ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, |
| 1307 | struct nlmsghdr *nlh, struct nfattr *cda[], int *errp) |
| 1308 | { |
| 1309 | struct ip_conntrack_expect *exp, *tmp; |
| 1310 | struct ip_conntrack_tuple tuple; |
| 1311 | struct ip_conntrack_helper *h; |
| 1312 | int err; |
| 1313 | |
Harald Welte | 1444fc5 | 2005-08-09 20:04:07 -0700 | [diff] [blame^] | 1314 | if (cda[CTA_EXPECT_TUPLE-1]) { |
| 1315 | /* delete a single expect by tuple */ |
| 1316 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE); |
| 1317 | if (err < 0) |
| 1318 | return err; |
| 1319 | |
| 1320 | /* bump usage count to 2 */ |
| 1321 | exp = ip_conntrack_expect_find_get(&tuple); |
| 1322 | if (!exp) |
| 1323 | return -ENOENT; |
| 1324 | |
| 1325 | if (cda[CTA_EXPECT_ID-1]) { |
| 1326 | u_int32_t id = |
| 1327 | *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]); |
| 1328 | if (exp->id != ntohl(id)) { |
| 1329 | ip_conntrack_expect_put(exp); |
| 1330 | return -ENOENT; |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | /* after list removal, usage count == 1 */ |
| 1335 | ip_conntrack_unexpect_related(exp); |
| 1336 | /* have to put what we 'get' above. |
| 1337 | * after this line usage count == 0 */ |
| 1338 | ip_conntrack_expect_put(exp); |
| 1339 | } else if (cda[CTA_EXPECT_HELP_NAME-1]) { |
| 1340 | char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]); |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1341 | |
| 1342 | /* delete all expectations for this helper */ |
| 1343 | write_lock_bh(&ip_conntrack_lock); |
| 1344 | h = __ip_conntrack_helper_find_byname(name); |
| 1345 | if (!h) { |
| 1346 | write_unlock_bh(&ip_conntrack_lock); |
| 1347 | return -EINVAL; |
| 1348 | } |
| 1349 | list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list, |
| 1350 | list) { |
| 1351 | if (exp->master->helper == h |
| 1352 | && del_timer(&exp->timeout)) |
| 1353 | __ip_ct_expect_unlink_destroy(exp); |
| 1354 | } |
| 1355 | write_unlock(&ip_conntrack_lock); |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1356 | } else { |
| 1357 | /* This basically means we have to flush everything*/ |
| 1358 | write_lock_bh(&ip_conntrack_lock); |
| 1359 | list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list, |
| 1360 | list) { |
| 1361 | if (del_timer(&exp->timeout)) |
| 1362 | __ip_ct_expect_unlink_destroy(exp); |
| 1363 | } |
| 1364 | write_unlock_bh(&ip_conntrack_lock); |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1367 | return 0; |
| 1368 | } |
| 1369 | static int |
| 1370 | ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[]) |
| 1371 | { |
| 1372 | return -EOPNOTSUPP; |
| 1373 | } |
| 1374 | |
| 1375 | static int |
| 1376 | ctnetlink_create_expect(struct nfattr *cda[]) |
| 1377 | { |
| 1378 | struct ip_conntrack_tuple tuple, mask, master_tuple; |
| 1379 | struct ip_conntrack_tuple_hash *h = NULL; |
| 1380 | struct ip_conntrack_expect *exp; |
| 1381 | struct ip_conntrack *ct; |
| 1382 | int err = 0; |
| 1383 | |
| 1384 | DEBUGP("entered %s\n", __FUNCTION__); |
| 1385 | |
Harald Welte | 1444fc5 | 2005-08-09 20:04:07 -0700 | [diff] [blame^] | 1386 | /* caller guarantees that those three CTA_EXPECT_* exist */ |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1387 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE); |
| 1388 | if (err < 0) |
| 1389 | return err; |
Harald Welte | bd9a26b | 2005-08-09 20:03:22 -0700 | [diff] [blame] | 1390 | err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK); |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1391 | if (err < 0) |
| 1392 | return err; |
Harald Welte | 1444fc5 | 2005-08-09 20:04:07 -0700 | [diff] [blame^] | 1393 | err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER); |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1394 | if (err < 0) |
| 1395 | return err; |
| 1396 | |
| 1397 | /* Look for master conntrack of this expectation */ |
| 1398 | h = ip_conntrack_find_get(&master_tuple, NULL); |
| 1399 | if (!h) |
| 1400 | return -ENOENT; |
| 1401 | ct = tuplehash_to_ctrack(h); |
| 1402 | |
| 1403 | if (!ct->helper) { |
| 1404 | /* such conntrack hasn't got any helper, abort */ |
| 1405 | err = -EINVAL; |
| 1406 | goto out; |
| 1407 | } |
| 1408 | |
| 1409 | exp = ip_conntrack_expect_alloc(ct); |
| 1410 | if (!exp) { |
| 1411 | err = -ENOMEM; |
| 1412 | goto out; |
| 1413 | } |
| 1414 | |
| 1415 | exp->expectfn = NULL; |
| 1416 | exp->master = ct; |
| 1417 | memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple)); |
| 1418 | memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple)); |
| 1419 | |
| 1420 | err = ip_conntrack_expect_related(exp); |
| 1421 | ip_conntrack_expect_put(exp); |
| 1422 | |
| 1423 | out: |
| 1424 | ip_conntrack_put(tuplehash_to_ctrack(h)); |
| 1425 | return err; |
| 1426 | } |
| 1427 | |
| 1428 | static int |
| 1429 | ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb, |
| 1430 | struct nlmsghdr *nlh, struct nfattr *cda[], int *errp) |
| 1431 | { |
| 1432 | struct ip_conntrack_tuple tuple; |
| 1433 | struct ip_conntrack_expect *exp; |
| 1434 | int err = 0; |
| 1435 | |
| 1436 | DEBUGP("entered %s\n", __FUNCTION__); |
| 1437 | |
Harald Welte | 1444fc5 | 2005-08-09 20:04:07 -0700 | [diff] [blame^] | 1438 | if (!cda[CTA_EXPECT_TUPLE-1] |
| 1439 | || !cda[CTA_EXPECT_MASK-1] |
| 1440 | || !cda[CTA_EXPECT_MASTER-1]) |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1441 | return -EINVAL; |
| 1442 | |
| 1443 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE); |
| 1444 | if (err < 0) |
| 1445 | return err; |
| 1446 | |
| 1447 | write_lock_bh(&ip_conntrack_lock); |
| 1448 | exp = __ip_conntrack_expect_find(&tuple); |
| 1449 | |
| 1450 | if (!exp) { |
| 1451 | write_unlock_bh(&ip_conntrack_lock); |
| 1452 | err = -ENOENT; |
| 1453 | if (nlh->nlmsg_flags & NLM_F_CREATE) |
| 1454 | err = ctnetlink_create_expect(cda); |
| 1455 | return err; |
| 1456 | } |
| 1457 | |
| 1458 | err = -EEXIST; |
| 1459 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) |
| 1460 | err = ctnetlink_change_expect(exp, cda); |
| 1461 | write_unlock_bh(&ip_conntrack_lock); |
| 1462 | |
| 1463 | DEBUGP("leaving\n"); |
| 1464 | |
| 1465 | return err; |
| 1466 | } |
| 1467 | |
| 1468 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS |
| 1469 | static struct notifier_block ctnl_notifier = { |
| 1470 | .notifier_call = ctnetlink_conntrack_event, |
| 1471 | }; |
| 1472 | |
| 1473 | static struct notifier_block ctnl_notifier_exp = { |
| 1474 | .notifier_call = ctnetlink_expect_event, |
| 1475 | }; |
| 1476 | #endif |
| 1477 | |
| 1478 | static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { |
| 1479 | [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack, |
Harald Welte | 927ccbc | 2005-08-09 20:03:40 -0700 | [diff] [blame] | 1480 | .attr_count = CTA_MAX, |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1481 | .cap_required = CAP_NET_ADMIN }, |
| 1482 | [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack, |
Harald Welte | 927ccbc | 2005-08-09 20:03:40 -0700 | [diff] [blame] | 1483 | .attr_count = CTA_MAX, |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1484 | .cap_required = CAP_NET_ADMIN }, |
| 1485 | [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack, |
Harald Welte | 927ccbc | 2005-08-09 20:03:40 -0700 | [diff] [blame] | 1486 | .attr_count = CTA_MAX, |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1487 | .cap_required = CAP_NET_ADMIN }, |
| 1488 | [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack, |
Harald Welte | 927ccbc | 2005-08-09 20:03:40 -0700 | [diff] [blame] | 1489 | .attr_count = CTA_MAX, |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1490 | .cap_required = CAP_NET_ADMIN }, |
| 1491 | }; |
| 1492 | |
| 1493 | static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_MAX] = { |
| 1494 | [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect, |
Harald Welte | 927ccbc | 2005-08-09 20:03:40 -0700 | [diff] [blame] | 1495 | .attr_count = CTA_EXPECT_MAX, |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1496 | .cap_required = CAP_NET_ADMIN }, |
| 1497 | [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect, |
Harald Welte | 927ccbc | 2005-08-09 20:03:40 -0700 | [diff] [blame] | 1498 | .attr_count = CTA_EXPECT_MAX, |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1499 | .cap_required = CAP_NET_ADMIN }, |
| 1500 | [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect, |
Harald Welte | 927ccbc | 2005-08-09 20:03:40 -0700 | [diff] [blame] | 1501 | .attr_count = CTA_EXPECT_MAX, |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1502 | .cap_required = CAP_NET_ADMIN }, |
| 1503 | }; |
| 1504 | |
| 1505 | static struct nfnetlink_subsystem ctnl_subsys = { |
| 1506 | .name = "conntrack", |
| 1507 | .subsys_id = NFNL_SUBSYS_CTNETLINK, |
| 1508 | .cb_count = IPCTNL_MSG_MAX, |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1509 | .cb = ctnl_cb, |
| 1510 | }; |
| 1511 | |
| 1512 | static struct nfnetlink_subsystem ctnl_exp_subsys = { |
| 1513 | .name = "conntrack_expect", |
| 1514 | .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, |
| 1515 | .cb_count = IPCTNL_MSG_EXP_MAX, |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 1516 | .cb = ctnl_exp_cb, |
| 1517 | }; |
| 1518 | |
| 1519 | static int __init ctnetlink_init(void) |
| 1520 | { |
| 1521 | int ret; |
| 1522 | |
| 1523 | printk("ctnetlink v%s: registering with nfnetlink.\n", version); |
| 1524 | ret = nfnetlink_subsys_register(&ctnl_subsys); |
| 1525 | if (ret < 0) { |
| 1526 | printk("ctnetlink_init: cannot register with nfnetlink.\n"); |
| 1527 | goto err_out; |
| 1528 | } |
| 1529 | |
| 1530 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); |
| 1531 | if (ret < 0) { |
| 1532 | printk("ctnetlink_init: cannot register exp with nfnetlink.\n"); |
| 1533 | goto err_unreg_subsys; |
| 1534 | } |
| 1535 | |
| 1536 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS |
| 1537 | ret = ip_conntrack_register_notifier(&ctnl_notifier); |
| 1538 | if (ret < 0) { |
| 1539 | printk("ctnetlink_init: cannot register notifier.\n"); |
| 1540 | goto err_unreg_exp_subsys; |
| 1541 | } |
| 1542 | |
| 1543 | ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp); |
| 1544 | if (ret < 0) { |
| 1545 | printk("ctnetlink_init: cannot expect register notifier.\n"); |
| 1546 | goto err_unreg_notifier; |
| 1547 | } |
| 1548 | #endif |
| 1549 | |
| 1550 | return 0; |
| 1551 | |
| 1552 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS |
| 1553 | err_unreg_notifier: |
| 1554 | ip_conntrack_unregister_notifier(&ctnl_notifier); |
| 1555 | err_unreg_exp_subsys: |
| 1556 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); |
| 1557 | #endif |
| 1558 | err_unreg_subsys: |
| 1559 | nfnetlink_subsys_unregister(&ctnl_subsys); |
| 1560 | err_out: |
| 1561 | return ret; |
| 1562 | } |
| 1563 | |
| 1564 | static void __exit ctnetlink_exit(void) |
| 1565 | { |
| 1566 | printk("ctnetlink: unregistering from nfnetlink.\n"); |
| 1567 | |
| 1568 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS |
| 1569 | ip_conntrack_unregister_notifier(&ctnl_notifier_exp); |
| 1570 | ip_conntrack_unregister_notifier(&ctnl_notifier); |
| 1571 | #endif |
| 1572 | |
| 1573 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); |
| 1574 | nfnetlink_subsys_unregister(&ctnl_subsys); |
| 1575 | return; |
| 1576 | } |
| 1577 | |
| 1578 | module_init(ctnetlink_init); |
| 1579 | module_exit(ctnetlink_exit); |