1 2 /* (C) 1999-2001 Paul `Rusty' Russell 3 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> 4 * (C) 2006-2012 Patrick McHardy <kaber@trash.net> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10 11 #include <linux/types.h> 12 #include <linux/ip.h> 13 #include <linux/netfilter.h> 14 #include <linux/module.h> 15 #include <linux/skbuff.h> 16 #include <linux/icmp.h> 17 #include <linux/sysctl.h> 18 #include <net/route.h> 19 #include <net/ip.h> 20 21 #include <linux/netfilter_ipv4.h> 22 #include <net/netfilter/nf_conntrack.h> 23 #include <net/netfilter/nf_conntrack_helper.h> 24 #include <net/netfilter/nf_conntrack_l4proto.h> 25 #include <net/netfilter/nf_conntrack_l3proto.h> 26 #include <net/netfilter/nf_conntrack_zones.h> 27 #include <net/netfilter/nf_conntrack_core.h> 28 #include <net/netfilter/nf_conntrack_seqadj.h> 29 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h> 30 #include <net/netfilter/nf_nat_helper.h> 31 #include <net/netfilter/ipv4/nf_defrag_ipv4.h> 32 #include <net/netfilter/nf_log.h> 33 34 static int conntrack4_net_id __read_mostly; 35 static DEFINE_MUTEX(register_ipv4_hooks); 36 37 struct conntrack4_net { 38 unsigned int users; 39 }; 40 41 static bool ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff, 42 struct nf_conntrack_tuple *tuple) 43 { 44 const __be32 *ap; 45 __be32 _addrs[2]; 46 ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr), 47 sizeof(u_int32_t) * 2, _addrs); 48 if (ap == NULL) 49 return false; 50 51 tuple->src.u3.ip = ap[0]; 52 tuple->dst.u3.ip = ap[1]; 53 54 return true; 55 } 56 57 static bool ipv4_invert_tuple(struct nf_conntrack_tuple *tuple, 58 const struct nf_conntrack_tuple *orig) 59 { 60 tuple->src.u3.ip = orig->dst.u3.ip; 61 tuple->dst.u3.ip = orig->src.u3.ip; 62 63 return true; 64 } 65 66 static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff, 67 unsigned int *dataoff, u_int8_t *protonum) 68 { 69 const struct iphdr *iph; 70 struct iphdr _iph; 71 72 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph); 73 if (iph == NULL) 74 return -NF_ACCEPT; 75 76 /* Conntrack defragments packets, we might still see fragments 77 * inside ICMP packets though. */ 78 if (iph->frag_off & htons(IP_OFFSET)) 79 return -NF_ACCEPT; 80 81 *dataoff = nhoff + (iph->ihl << 2); 82 *protonum = iph->protocol; 83 84 /* Check bogus IP headers */ 85 if (*dataoff > skb->len) { 86 pr_debug("nf_conntrack_ipv4: bogus IPv4 packet: " 87 "nhoff %u, ihl %u, skblen %u\n", 88 nhoff, iph->ihl << 2, skb->len); 89 return -NF_ACCEPT; 90 } 91 92 return NF_ACCEPT; 93 } 94 95 static unsigned int ipv4_helper(void *priv, 96 struct sk_buff *skb, 97 const struct nf_hook_state *state) 98 { 99 struct nf_conn *ct; 100 enum ip_conntrack_info ctinfo; 101 const struct nf_conn_help *help; 102 const struct nf_conntrack_helper *helper; 103 104 /* This is where we call the helper: as the packet goes out. */ 105 ct = nf_ct_get(skb, &ctinfo); 106 if (!ct || ctinfo == IP_CT_RELATED_REPLY) 107 return NF_ACCEPT; 108 109 help = nfct_help(ct); 110 if (!help) 111 return NF_ACCEPT; 112 113 /* rcu_read_lock()ed by nf_hook_thresh */ 114 helper = rcu_dereference(help->helper); 115 if (!helper) 116 return NF_ACCEPT; 117 118 return helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb), 119 ct, ctinfo); 120 } 121 122 static unsigned int ipv4_confirm(void *priv, 123 struct sk_buff *skb, 124 const struct nf_hook_state *state) 125 { 126 struct nf_conn *ct; 127 enum ip_conntrack_info ctinfo; 128 129 ct = nf_ct_get(skb, &ctinfo); 130 if (!ct || ctinfo == IP_CT_RELATED_REPLY) 131 goto out; 132 133 /* adjust seqs for loopback traffic only in outgoing direction */ 134 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) && 135 !nf_is_loopback_packet(skb)) { 136 if (!nf_ct_seq_adjust(skb, ct, ctinfo, ip_hdrlen(skb))) { 137 NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop); 138 return NF_DROP; 139 } 140 } 141 out: 142 /* We've seen it coming out the other side: confirm it */ 143 return nf_conntrack_confirm(skb); 144 } 145 146 static unsigned int ipv4_conntrack_in(void *priv, 147 struct sk_buff *skb, 148 const struct nf_hook_state *state) 149 { 150 return nf_conntrack_in(state->net, PF_INET, state->hook, skb); 151 } 152 153 static unsigned int ipv4_conntrack_local(void *priv, 154 struct sk_buff *skb, 155 const struct nf_hook_state *state) 156 { 157 if (ip_is_fragment(ip_hdr(skb))) { /* IP_NODEFRAG setsockopt set */ 158 enum ip_conntrack_info ctinfo; 159 struct nf_conn *tmpl; 160 161 tmpl = nf_ct_get(skb, &ctinfo); 162 if (tmpl && nf_ct_is_template(tmpl)) { 163 /* when skipping ct, clear templates to avoid fooling 164 * later targets/matches 165 */ 166 skb->_nfct = 0; 167 nf_ct_put(tmpl); 168 } 169 return NF_ACCEPT; 170 } 171 172 return nf_conntrack_in(state->net, PF_INET, state->hook, skb); 173 } 174 175 /* Connection tracking may drop packets, but never alters them, so 176 make it the first hook. */ 177 static const struct nf_hook_ops ipv4_conntrack_ops[] = { 178 { 179 .hook = ipv4_conntrack_in, 180 .pf = NFPROTO_IPV4, 181 .hooknum = NF_INET_PRE_ROUTING, 182 .priority = NF_IP_PRI_CONNTRACK, 183 }, 184 { 185 .hook = ipv4_conntrack_local, 186 .pf = NFPROTO_IPV4, 187 .hooknum = NF_INET_LOCAL_OUT, 188 .priority = NF_IP_PRI_CONNTRACK, 189 }, 190 { 191 .hook = ipv4_helper, 192 .pf = NFPROTO_IPV4, 193 .hooknum = NF_INET_POST_ROUTING, 194 .priority = NF_IP_PRI_CONNTRACK_HELPER, 195 }, 196 { 197 .hook = ipv4_confirm, 198 .pf = NFPROTO_IPV4, 199 .hooknum = NF_INET_POST_ROUTING, 200 .priority = NF_IP_PRI_CONNTRACK_CONFIRM, 201 }, 202 { 203 .hook = ipv4_helper, 204 .pf = NFPROTO_IPV4, 205 .hooknum = NF_INET_LOCAL_IN, 206 .priority = NF_IP_PRI_CONNTRACK_HELPER, 207 }, 208 { 209 .hook = ipv4_confirm, 210 .pf = NFPROTO_IPV4, 211 .hooknum = NF_INET_LOCAL_IN, 212 .priority = NF_IP_PRI_CONNTRACK_CONFIRM, 213 }, 214 }; 215 216 /* Fast function for those who don't want to parse /proc (and I don't 217 blame them). */ 218 /* Reversing the socket's dst/src point of view gives us the reply 219 mapping. */ 220 static int 221 getorigdst(struct sock *sk, int optval, void __user *user, int *len) 222 { 223 const struct inet_sock *inet = inet_sk(sk); 224 const struct nf_conntrack_tuple_hash *h; 225 struct nf_conntrack_tuple tuple; 226 227 memset(&tuple, 0, sizeof(tuple)); 228 229 lock_sock(sk); 230 tuple.src.u3.ip = inet->inet_rcv_saddr; 231 tuple.src.u.tcp.port = inet->inet_sport; 232 tuple.dst.u3.ip = inet->inet_daddr; 233 tuple.dst.u.tcp.port = inet->inet_dport; 234 tuple.src.l3num = PF_INET; 235 tuple.dst.protonum = sk->sk_protocol; 236 release_sock(sk); 237 238 /* We only do TCP and SCTP at the moment: is there a better way? */ 239 if (tuple.dst.protonum != IPPROTO_TCP && 240 tuple.dst.protonum != IPPROTO_SCTP) { 241 pr_debug("SO_ORIGINAL_DST: Not a TCP/SCTP socket\n"); 242 return -ENOPROTOOPT; 243 } 244 245 if ((unsigned int) *len < sizeof(struct sockaddr_in)) { 246 pr_debug("SO_ORIGINAL_DST: len %d not %zu\n", 247 *len, sizeof(struct sockaddr_in)); 248 return -EINVAL; 249 } 250 251 h = nf_conntrack_find_get(sock_net(sk), &nf_ct_zone_dflt, &tuple); 252 if (h) { 253 struct sockaddr_in sin; 254 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); 255 256 sin.sin_family = AF_INET; 257 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL] 258 .tuple.dst.u.tcp.port; 259 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL] 260 .tuple.dst.u3.ip; 261 memset(sin.sin_zero, 0, sizeof(sin.sin_zero)); 262 263 pr_debug("SO_ORIGINAL_DST: %pI4 %u\n", 264 &sin.sin_addr.s_addr, ntohs(sin.sin_port)); 265 nf_ct_put(ct); 266 if (copy_to_user(user, &sin, sizeof(sin)) != 0) 267 return -EFAULT; 268 else 269 return 0; 270 } 271 pr_debug("SO_ORIGINAL_DST: Can't find %pI4/%u-%pI4/%u.\n", 272 &tuple.src.u3.ip, ntohs(tuple.src.u.tcp.port), 273 &tuple.dst.u3.ip, ntohs(tuple.dst.u.tcp.port)); 274 return -ENOENT; 275 } 276 277 #if IS_ENABLED(CONFIG_NF_CT_NETLINK) 278 279 #include <linux/netfilter/nfnetlink.h> 280 #include <linux/netfilter/nfnetlink_conntrack.h> 281 282 static int ipv4_tuple_to_nlattr(struct sk_buff *skb, 283 const struct nf_conntrack_tuple *tuple) 284 { 285 if (nla_put_in_addr(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) || 286 nla_put_in_addr(skb, CTA_IP_V4_DST, tuple->dst.u3.ip)) 287 goto nla_put_failure; 288 return 0; 289 290 nla_put_failure: 291 return -1; 292 } 293 294 static const struct nla_policy ipv4_nla_policy[CTA_IP_MAX+1] = { 295 [CTA_IP_V4_SRC] = { .type = NLA_U32 }, 296 [CTA_IP_V4_DST] = { .type = NLA_U32 }, 297 }; 298 299 static int ipv4_nlattr_to_tuple(struct nlattr *tb[], 300 struct nf_conntrack_tuple *t) 301 { 302 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST]) 303 return -EINVAL; 304 305 t->src.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_SRC]); 306 t->dst.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_DST]); 307 308 return 0; 309 } 310 #endif 311 312 static struct nf_sockopt_ops so_getorigdst = { 313 .pf = PF_INET, 314 .get_optmin = SO_ORIGINAL_DST, 315 .get_optmax = SO_ORIGINAL_DST+1, 316 .get = getorigdst, 317 .owner = THIS_MODULE, 318 }; 319 320 static int ipv4_hooks_register(struct net *net) 321 { 322 struct conntrack4_net *cnet = net_generic(net, conntrack4_net_id); 323 int err = 0; 324 325 mutex_lock(®ister_ipv4_hooks); 326 327 cnet->users++; 328 if (cnet->users > 1) 329 goto out_unlock; 330 331 err = nf_defrag_ipv4_enable(net); 332 if (err) { 333 cnet->users = 0; 334 goto out_unlock; 335 } 336 337 err = nf_register_net_hooks(net, ipv4_conntrack_ops, 338 ARRAY_SIZE(ipv4_conntrack_ops)); 339 340 if (err) 341 cnet->users = 0; 342 out_unlock: 343 mutex_unlock(®ister_ipv4_hooks); 344 return err; 345 } 346 347 static void ipv4_hooks_unregister(struct net *net) 348 { 349 struct conntrack4_net *cnet = net_generic(net, conntrack4_net_id); 350 351 mutex_lock(®ister_ipv4_hooks); 352 if (cnet->users && (--cnet->users == 0)) 353 nf_unregister_net_hooks(net, ipv4_conntrack_ops, 354 ARRAY_SIZE(ipv4_conntrack_ops)); 355 mutex_unlock(®ister_ipv4_hooks); 356 } 357 358 const struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 = { 359 .l3proto = PF_INET, 360 .pkt_to_tuple = ipv4_pkt_to_tuple, 361 .invert_tuple = ipv4_invert_tuple, 362 .get_l4proto = ipv4_get_l4proto, 363 #if IS_ENABLED(CONFIG_NF_CT_NETLINK) 364 .tuple_to_nlattr = ipv4_tuple_to_nlattr, 365 .nlattr_to_tuple = ipv4_nlattr_to_tuple, 366 .nla_policy = ipv4_nla_policy, 367 .nla_size = NLA_ALIGN(NLA_HDRLEN + sizeof(u32)) + /* CTA_IP_V4_SRC */ 368 NLA_ALIGN(NLA_HDRLEN + sizeof(u32)), /* CTA_IP_V4_DST */ 369 #endif 370 .net_ns_get = ipv4_hooks_register, 371 .net_ns_put = ipv4_hooks_unregister, 372 .me = THIS_MODULE, 373 }; 374 375 module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint, 376 &nf_conntrack_htable_size, 0600); 377 378 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET)); 379 MODULE_ALIAS("ip_conntrack"); 380 MODULE_LICENSE("GPL"); 381 382 static const struct nf_conntrack_l4proto * const builtin_l4proto4[] = { 383 &nf_conntrack_l4proto_tcp4, 384 &nf_conntrack_l4proto_udp4, 385 &nf_conntrack_l4proto_icmp, 386 #ifdef CONFIG_NF_CT_PROTO_DCCP 387 &nf_conntrack_l4proto_dccp4, 388 #endif 389 #ifdef CONFIG_NF_CT_PROTO_SCTP 390 &nf_conntrack_l4proto_sctp4, 391 #endif 392 #ifdef CONFIG_NF_CT_PROTO_UDPLITE 393 &nf_conntrack_l4proto_udplite4, 394 #endif 395 }; 396 397 static int ipv4_net_init(struct net *net) 398 { 399 return nf_ct_l4proto_pernet_register(net, builtin_l4proto4, 400 ARRAY_SIZE(builtin_l4proto4)); 401 } 402 403 static void ipv4_net_exit(struct net *net) 404 { 405 nf_ct_l4proto_pernet_unregister(net, builtin_l4proto4, 406 ARRAY_SIZE(builtin_l4proto4)); 407 } 408 409 static struct pernet_operations ipv4_net_ops = { 410 .init = ipv4_net_init, 411 .exit = ipv4_net_exit, 412 .id = &conntrack4_net_id, 413 .size = sizeof(struct conntrack4_net), 414 }; 415 416 static int __init nf_conntrack_l3proto_ipv4_init(void) 417 { 418 int ret = 0; 419 420 need_conntrack(); 421 422 #if IS_ENABLED(CONFIG_NF_CT_NETLINK) 423 if (WARN_ON(nla_policy_len(ipv4_nla_policy, CTA_IP_MAX + 1) != 424 nf_conntrack_l3proto_ipv4.nla_size)) 425 return -EINVAL; 426 #endif 427 ret = nf_register_sockopt(&so_getorigdst); 428 if (ret < 0) { 429 pr_err("Unable to register netfilter socket option\n"); 430 return ret; 431 } 432 433 ret = register_pernet_subsys(&ipv4_net_ops); 434 if (ret < 0) { 435 pr_err("nf_conntrack_ipv4: can't register pernet ops\n"); 436 goto cleanup_sockopt; 437 } 438 439 ret = nf_ct_l4proto_register(builtin_l4proto4, 440 ARRAY_SIZE(builtin_l4proto4)); 441 if (ret < 0) 442 goto cleanup_pernet; 443 444 ret = nf_ct_l3proto_register(&nf_conntrack_l3proto_ipv4); 445 if (ret < 0) { 446 pr_err("nf_conntrack_ipv4: can't register ipv4 proto.\n"); 447 goto cleanup_l4proto; 448 } 449 450 return ret; 451 cleanup_l4proto: 452 nf_ct_l4proto_unregister(builtin_l4proto4, 453 ARRAY_SIZE(builtin_l4proto4)); 454 cleanup_pernet: 455 unregister_pernet_subsys(&ipv4_net_ops); 456 cleanup_sockopt: 457 nf_unregister_sockopt(&so_getorigdst); 458 return ret; 459 } 460 461 static void __exit nf_conntrack_l3proto_ipv4_fini(void) 462 { 463 synchronize_net(); 464 nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv4); 465 nf_ct_l4proto_unregister(builtin_l4proto4, 466 ARRAY_SIZE(builtin_l4proto4)); 467 unregister_pernet_subsys(&ipv4_net_ops); 468 nf_unregister_sockopt(&so_getorigdst); 469 } 470 471 module_init(nf_conntrack_l3proto_ipv4_init); 472 module_exit(nf_conntrack_l3proto_ipv4_fini); 473
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.