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