GNU Linux-libre 4.19.264-gnu1
[releases.git] / net / netfilter / nf_conntrack_broadcast.c
1 /*
2  *      broadcast connection tracking helper
3  *
4  *      (c) 2005 Patrick McHardy <kaber@trash.net>
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License
8  *      as published by the Free Software Foundation; either version
9  *      2 of the License, or (at your option) any later version.
10  */
11
12 #include <linux/module.h>
13 #include <linux/ip.h>
14 #include <net/route.h>
15 #include <linux/inetdevice.h>
16 #include <linux/skbuff.h>
17
18 #include <net/netfilter/nf_conntrack.h>
19 #include <net/netfilter/nf_conntrack_helper.h>
20 #include <net/netfilter/nf_conntrack_expect.h>
21
22 int nf_conntrack_broadcast_help(struct sk_buff *skb,
23                                 struct nf_conn *ct,
24                                 enum ip_conntrack_info ctinfo,
25                                 unsigned int timeout)
26 {
27         struct nf_conntrack_expect *exp;
28         struct iphdr *iph = ip_hdr(skb);
29         struct rtable *rt = skb_rtable(skb);
30         struct in_device *in_dev;
31         struct nf_conn_help *help = nfct_help(ct);
32         __be32 mask = 0;
33
34         /* we're only interested in locally generated packets */
35         if (skb->sk == NULL || !net_eq(nf_ct_net(ct), sock_net(skb->sk)))
36                 goto out;
37         if (rt == NULL || !(rt->rt_flags & RTCF_BROADCAST))
38                 goto out;
39         if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
40                 goto out;
41
42         in_dev = __in_dev_get_rcu(rt->dst.dev);
43         if (in_dev != NULL) {
44                 for_primary_ifa(in_dev) {
45                         if (ifa->ifa_broadcast == iph->daddr) {
46                                 mask = ifa->ifa_mask;
47                                 break;
48                         }
49                 } endfor_ifa(in_dev);
50         }
51
52         if (mask == 0)
53                 goto out;
54
55         exp = nf_ct_expect_alloc(ct);
56         if (exp == NULL)
57                 goto out;
58
59         exp->tuple                = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
60         exp->tuple.src.u.udp.port = help->helper->tuple.src.u.udp.port;
61
62         exp->mask.src.u3.ip       = mask;
63         exp->mask.src.u.udp.port  = htons(0xFFFF);
64
65         exp->expectfn             = NULL;
66         exp->flags                = NF_CT_EXPECT_PERMANENT;
67         exp->class                = NF_CT_EXPECT_CLASS_DEFAULT;
68         exp->helper               = NULL;
69
70         nf_ct_expect_related(exp);
71         nf_ct_expect_put(exp);
72
73         nf_ct_refresh(ct, skb, timeout * HZ);
74 out:
75         return NF_ACCEPT;
76 }
77 EXPORT_SYMBOL_GPL(nf_conntrack_broadcast_help);
78
79 MODULE_LICENSE("GPL");