]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
net: Convert uses of typedef ctl_table to struct ctl_table
[mirror_ubuntu-bionic-kernel.git] / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4.c
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/ipv4/nf_conntrack_ipv4.h>
29 #include <net/netfilter/nf_nat_helper.h>
30 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
31 #include <net/netfilter/nf_log.h>
32
33 static bool ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
34 struct nf_conntrack_tuple *tuple)
35 {
36 const __be32 *ap;
37 __be32 _addrs[2];
38 ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr),
39 sizeof(u_int32_t) * 2, _addrs);
40 if (ap == NULL)
41 return false;
42
43 tuple->src.u3.ip = ap[0];
44 tuple->dst.u3.ip = ap[1];
45
46 return true;
47 }
48
49 static bool ipv4_invert_tuple(struct nf_conntrack_tuple *tuple,
50 const struct nf_conntrack_tuple *orig)
51 {
52 tuple->src.u3.ip = orig->dst.u3.ip;
53 tuple->dst.u3.ip = orig->src.u3.ip;
54
55 return true;
56 }
57
58 static int ipv4_print_tuple(struct seq_file *s,
59 const struct nf_conntrack_tuple *tuple)
60 {
61 return seq_printf(s, "src=%pI4 dst=%pI4 ",
62 &tuple->src.u3.ip, &tuple->dst.u3.ip);
63 }
64
65 static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
66 unsigned int *dataoff, u_int8_t *protonum)
67 {
68 const struct iphdr *iph;
69 struct iphdr _iph;
70
71 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
72 if (iph == NULL)
73 return -NF_ACCEPT;
74
75 /* Conntrack defragments packets, we might still see fragments
76 * inside ICMP packets though. */
77 if (iph->frag_off & htons(IP_OFFSET))
78 return -NF_ACCEPT;
79
80 *dataoff = nhoff + (iph->ihl << 2);
81 *protonum = iph->protocol;
82
83 /* Check bogus IP headers */
84 if (*dataoff > skb->len) {
85 pr_debug("nf_conntrack_ipv4: bogus IPv4 packet: "
86 "nhoff %u, ihl %u, skblen %u\n",
87 nhoff, iph->ihl << 2, skb->len);
88 return -NF_ACCEPT;
89 }
90
91 return NF_ACCEPT;
92 }
93
94 static unsigned int ipv4_helper(unsigned int hooknum,
95 struct sk_buff *skb,
96 const struct net_device *in,
97 const struct net_device *out,
98 int (*okfn)(struct sk_buff *))
99 {
100 struct nf_conn *ct;
101 enum ip_conntrack_info ctinfo;
102 const struct nf_conn_help *help;
103 const struct nf_conntrack_helper *helper;
104
105 /* This is where we call the helper: as the packet goes out. */
106 ct = nf_ct_get(skb, &ctinfo);
107 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
108 return NF_ACCEPT;
109
110 help = nfct_help(ct);
111 if (!help)
112 return NF_ACCEPT;
113
114 /* rcu_read_lock()ed by nf_hook_slow */
115 helper = rcu_dereference(help->helper);
116 if (!helper)
117 return NF_ACCEPT;
118
119 return helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb),
120 ct, ctinfo);
121 }
122
123 static unsigned int ipv4_confirm(unsigned int hooknum,
124 struct sk_buff *skb,
125 const struct net_device *in,
126 const struct net_device *out,
127 int (*okfn)(struct sk_buff *))
128 {
129 struct nf_conn *ct;
130 enum ip_conntrack_info ctinfo;
131
132 ct = nf_ct_get(skb, &ctinfo);
133 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
134 goto out;
135
136 /* adjust seqs for loopback traffic only in outgoing direction */
137 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
138 !nf_is_loopback_packet(skb)) {
139 typeof(nf_nat_seq_adjust_hook) seq_adjust;
140
141 seq_adjust = rcu_dereference(nf_nat_seq_adjust_hook);
142 if (!seq_adjust ||
143 !seq_adjust(skb, ct, ctinfo, ip_hdrlen(skb))) {
144 NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop);
145 return NF_DROP;
146 }
147 }
148 out:
149 /* We've seen it coming out the other side: confirm it */
150 return nf_conntrack_confirm(skb);
151 }
152
153 static unsigned int ipv4_conntrack_in(unsigned int hooknum,
154 struct sk_buff *skb,
155 const struct net_device *in,
156 const struct net_device *out,
157 int (*okfn)(struct sk_buff *))
158 {
159 return nf_conntrack_in(dev_net(in), PF_INET, hooknum, skb);
160 }
161
162 static unsigned int ipv4_conntrack_local(unsigned int hooknum,
163 struct sk_buff *skb,
164 const struct net_device *in,
165 const struct net_device *out,
166 int (*okfn)(struct sk_buff *))
167 {
168 /* root is playing with raw sockets. */
169 if (skb->len < sizeof(struct iphdr) ||
170 ip_hdrlen(skb) < sizeof(struct iphdr))
171 return NF_ACCEPT;
172 return nf_conntrack_in(dev_net(out), PF_INET, hooknum, skb);
173 }
174
175 /* Connection tracking may drop packets, but never alters them, so
176 make it the first hook. */
177 static struct nf_hook_ops ipv4_conntrack_ops[] __read_mostly = {
178 {
179 .hook = ipv4_conntrack_in,
180 .owner = THIS_MODULE,
181 .pf = NFPROTO_IPV4,
182 .hooknum = NF_INET_PRE_ROUTING,
183 .priority = NF_IP_PRI_CONNTRACK,
184 },
185 {
186 .hook = ipv4_conntrack_local,
187 .owner = THIS_MODULE,
188 .pf = NFPROTO_IPV4,
189 .hooknum = NF_INET_LOCAL_OUT,
190 .priority = NF_IP_PRI_CONNTRACK,
191 },
192 {
193 .hook = ipv4_helper,
194 .owner = THIS_MODULE,
195 .pf = NFPROTO_IPV4,
196 .hooknum = NF_INET_POST_ROUTING,
197 .priority = NF_IP_PRI_CONNTRACK_HELPER,
198 },
199 {
200 .hook = ipv4_confirm,
201 .owner = THIS_MODULE,
202 .pf = NFPROTO_IPV4,
203 .hooknum = NF_INET_POST_ROUTING,
204 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
205 },
206 {
207 .hook = ipv4_helper,
208 .owner = THIS_MODULE,
209 .pf = NFPROTO_IPV4,
210 .hooknum = NF_INET_LOCAL_IN,
211 .priority = NF_IP_PRI_CONNTRACK_HELPER,
212 },
213 {
214 .hook = ipv4_confirm,
215 .owner = THIS_MODULE,
216 .pf = NFPROTO_IPV4,
217 .hooknum = NF_INET_LOCAL_IN,
218 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
219 },
220 };
221
222 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
223 static int log_invalid_proto_min = 0;
224 static int log_invalid_proto_max = 255;
225
226 static struct ctl_table ip_ct_sysctl_table[] = {
227 {
228 .procname = "ip_conntrack_max",
229 .maxlen = sizeof(int),
230 .mode = 0644,
231 .proc_handler = proc_dointvec,
232 },
233 {
234 .procname = "ip_conntrack_count",
235 .maxlen = sizeof(int),
236 .mode = 0444,
237 .proc_handler = proc_dointvec,
238 },
239 {
240 .procname = "ip_conntrack_buckets",
241 .maxlen = sizeof(unsigned int),
242 .mode = 0444,
243 .proc_handler = proc_dointvec,
244 },
245 {
246 .procname = "ip_conntrack_checksum",
247 .maxlen = sizeof(int),
248 .mode = 0644,
249 .proc_handler = proc_dointvec,
250 },
251 {
252 .procname = "ip_conntrack_log_invalid",
253 .maxlen = sizeof(unsigned int),
254 .mode = 0644,
255 .proc_handler = proc_dointvec_minmax,
256 .extra1 = &log_invalid_proto_min,
257 .extra2 = &log_invalid_proto_max,
258 },
259 { }
260 };
261 #endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
262
263 /* Fast function for those who don't want to parse /proc (and I don't
264 blame them). */
265 /* Reversing the socket's dst/src point of view gives us the reply
266 mapping. */
267 static int
268 getorigdst(struct sock *sk, int optval, void __user *user, int *len)
269 {
270 const struct inet_sock *inet = inet_sk(sk);
271 const struct nf_conntrack_tuple_hash *h;
272 struct nf_conntrack_tuple tuple;
273
274 memset(&tuple, 0, sizeof(tuple));
275 tuple.src.u3.ip = inet->inet_rcv_saddr;
276 tuple.src.u.tcp.port = inet->inet_sport;
277 tuple.dst.u3.ip = inet->inet_daddr;
278 tuple.dst.u.tcp.port = inet->inet_dport;
279 tuple.src.l3num = PF_INET;
280 tuple.dst.protonum = sk->sk_protocol;
281
282 /* We only do TCP and SCTP at the moment: is there a better way? */
283 if (sk->sk_protocol != IPPROTO_TCP && sk->sk_protocol != IPPROTO_SCTP) {
284 pr_debug("SO_ORIGINAL_DST: Not a TCP/SCTP socket\n");
285 return -ENOPROTOOPT;
286 }
287
288 if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
289 pr_debug("SO_ORIGINAL_DST: len %d not %Zu\n",
290 *len, sizeof(struct sockaddr_in));
291 return -EINVAL;
292 }
293
294 h = nf_conntrack_find_get(sock_net(sk), NF_CT_DEFAULT_ZONE, &tuple);
295 if (h) {
296 struct sockaddr_in sin;
297 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
298
299 sin.sin_family = AF_INET;
300 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
301 .tuple.dst.u.tcp.port;
302 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
303 .tuple.dst.u3.ip;
304 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
305
306 pr_debug("SO_ORIGINAL_DST: %pI4 %u\n",
307 &sin.sin_addr.s_addr, ntohs(sin.sin_port));
308 nf_ct_put(ct);
309 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
310 return -EFAULT;
311 else
312 return 0;
313 }
314 pr_debug("SO_ORIGINAL_DST: Can't find %pI4/%u-%pI4/%u.\n",
315 &tuple.src.u3.ip, ntohs(tuple.src.u.tcp.port),
316 &tuple.dst.u3.ip, ntohs(tuple.dst.u.tcp.port));
317 return -ENOENT;
318 }
319
320 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
321
322 #include <linux/netfilter/nfnetlink.h>
323 #include <linux/netfilter/nfnetlink_conntrack.h>
324
325 static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
326 const struct nf_conntrack_tuple *tuple)
327 {
328 if (nla_put_be32(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) ||
329 nla_put_be32(skb, CTA_IP_V4_DST, tuple->dst.u3.ip))
330 goto nla_put_failure;
331 return 0;
332
333 nla_put_failure:
334 return -1;
335 }
336
337 static const struct nla_policy ipv4_nla_policy[CTA_IP_MAX+1] = {
338 [CTA_IP_V4_SRC] = { .type = NLA_U32 },
339 [CTA_IP_V4_DST] = { .type = NLA_U32 },
340 };
341
342 static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
343 struct nf_conntrack_tuple *t)
344 {
345 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
346 return -EINVAL;
347
348 t->src.u3.ip = nla_get_be32(tb[CTA_IP_V4_SRC]);
349 t->dst.u3.ip = nla_get_be32(tb[CTA_IP_V4_DST]);
350
351 return 0;
352 }
353
354 static int ipv4_nlattr_tuple_size(void)
355 {
356 return nla_policy_len(ipv4_nla_policy, CTA_IP_MAX + 1);
357 }
358 #endif
359
360 static struct nf_sockopt_ops so_getorigdst = {
361 .pf = PF_INET,
362 .get_optmin = SO_ORIGINAL_DST,
363 .get_optmax = SO_ORIGINAL_DST+1,
364 .get = &getorigdst,
365 .owner = THIS_MODULE,
366 };
367
368 static int ipv4_init_net(struct net *net)
369 {
370 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
371 struct nf_ip_net *in = &net->ct.nf_ct_proto;
372 in->ctl_table = kmemdup(ip_ct_sysctl_table,
373 sizeof(ip_ct_sysctl_table),
374 GFP_KERNEL);
375 if (!in->ctl_table)
376 return -ENOMEM;
377
378 in->ctl_table[0].data = &nf_conntrack_max;
379 in->ctl_table[1].data = &net->ct.count;
380 in->ctl_table[2].data = &net->ct.htable_size;
381 in->ctl_table[3].data = &net->ct.sysctl_checksum;
382 in->ctl_table[4].data = &net->ct.sysctl_log_invalid;
383 #endif
384 return 0;
385 }
386
387 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 __read_mostly = {
388 .l3proto = PF_INET,
389 .name = "ipv4",
390 .pkt_to_tuple = ipv4_pkt_to_tuple,
391 .invert_tuple = ipv4_invert_tuple,
392 .print_tuple = ipv4_print_tuple,
393 .get_l4proto = ipv4_get_l4proto,
394 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
395 .tuple_to_nlattr = ipv4_tuple_to_nlattr,
396 .nlattr_tuple_size = ipv4_nlattr_tuple_size,
397 .nlattr_to_tuple = ipv4_nlattr_to_tuple,
398 .nla_policy = ipv4_nla_policy,
399 #endif
400 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
401 .ctl_table_path = "net/ipv4/netfilter",
402 #endif
403 .init_net = ipv4_init_net,
404 .me = THIS_MODULE,
405 };
406
407 module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
408 &nf_conntrack_htable_size, 0600);
409
410 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
411 MODULE_ALIAS("ip_conntrack");
412 MODULE_LICENSE("GPL");
413
414 static int ipv4_net_init(struct net *net)
415 {
416 int ret = 0;
417
418 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_tcp4);
419 if (ret < 0) {
420 pr_err("nf_conntrack_tcp4: pernet registration failed\n");
421 goto out_tcp;
422 }
423 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udp4);
424 if (ret < 0) {
425 pr_err("nf_conntrack_udp4: pernet registration failed\n");
426 goto out_udp;
427 }
428 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_icmp);
429 if (ret < 0) {
430 pr_err("nf_conntrack_icmp4: pernet registration failed\n");
431 goto out_icmp;
432 }
433 ret = nf_ct_l3proto_pernet_register(net, &nf_conntrack_l3proto_ipv4);
434 if (ret < 0) {
435 pr_err("nf_conntrack_ipv4: pernet registration failed\n");
436 goto out_ipv4;
437 }
438 return 0;
439 out_ipv4:
440 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmp);
441 out_icmp:
442 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp4);
443 out_udp:
444 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp4);
445 out_tcp:
446 return ret;
447 }
448
449 static void ipv4_net_exit(struct net *net)
450 {
451 nf_ct_l3proto_pernet_unregister(net, &nf_conntrack_l3proto_ipv4);
452 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmp);
453 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp4);
454 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp4);
455 }
456
457 static struct pernet_operations ipv4_net_ops = {
458 .init = ipv4_net_init,
459 .exit = ipv4_net_exit,
460 };
461
462 static int __init nf_conntrack_l3proto_ipv4_init(void)
463 {
464 int ret = 0;
465
466 need_conntrack();
467 nf_defrag_ipv4_enable();
468
469 ret = nf_register_sockopt(&so_getorigdst);
470 if (ret < 0) {
471 printk(KERN_ERR "Unable to register netfilter socket option\n");
472 return ret;
473 }
474
475 ret = register_pernet_subsys(&ipv4_net_ops);
476 if (ret < 0) {
477 pr_err("nf_conntrack_ipv4: can't register pernet ops\n");
478 goto cleanup_sockopt;
479 }
480
481 ret = nf_register_hooks(ipv4_conntrack_ops,
482 ARRAY_SIZE(ipv4_conntrack_ops));
483 if (ret < 0) {
484 pr_err("nf_conntrack_ipv4: can't register hooks.\n");
485 goto cleanup_pernet;
486 }
487
488 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_tcp4);
489 if (ret < 0) {
490 pr_err("nf_conntrack_ipv4: can't register tcp4 proto.\n");
491 goto cleanup_hooks;
492 }
493
494 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udp4);
495 if (ret < 0) {
496 pr_err("nf_conntrack_ipv4: can't register udp4 proto.\n");
497 goto cleanup_tcp4;
498 }
499
500 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_icmp);
501 if (ret < 0) {
502 pr_err("nf_conntrack_ipv4: can't register icmpv4 proto.\n");
503 goto cleanup_udp4;
504 }
505
506 ret = nf_ct_l3proto_register(&nf_conntrack_l3proto_ipv4);
507 if (ret < 0) {
508 pr_err("nf_conntrack_ipv4: can't register ipv4 proto.\n");
509 goto cleanup_icmpv4;
510 }
511
512 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
513 ret = nf_conntrack_ipv4_compat_init();
514 if (ret < 0)
515 goto cleanup_proto;
516 #endif
517 return ret;
518 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
519 cleanup_proto:
520 nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
521 #endif
522 cleanup_icmpv4:
523 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmp);
524 cleanup_udp4:
525 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp4);
526 cleanup_tcp4:
527 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
528 cleanup_hooks:
529 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
530 cleanup_pernet:
531 unregister_pernet_subsys(&ipv4_net_ops);
532 cleanup_sockopt:
533 nf_unregister_sockopt(&so_getorigdst);
534 return ret;
535 }
536
537 static void __exit nf_conntrack_l3proto_ipv4_fini(void)
538 {
539 synchronize_net();
540 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
541 nf_conntrack_ipv4_compat_fini();
542 #endif
543 nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
544 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmp);
545 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp4);
546 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
547 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
548 unregister_pernet_subsys(&ipv4_net_ops);
549 nf_unregister_sockopt(&so_getorigdst);
550 }
551
552 module_init(nf_conntrack_l3proto_ipv4_init);
553 module_exit(nf_conntrack_l3proto_ipv4_fini);
554
555 void need_ipv4_conntrack(void)
556 {
557 return;
558 }
559 EXPORT_SYMBOL_GPL(need_ipv4_conntrack);