Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
93557f53 JO |
2 | /* |
3 | * broadcast connection tracking helper | |
4 | * | |
5 | * (c) 2005 Patrick McHardy <kaber@trash.net> | |
93557f53 JO |
6 | */ |
7 | ||
8 | #include <linux/module.h> | |
9 | #include <linux/ip.h> | |
10 | #include <net/route.h> | |
11 | #include <linux/inetdevice.h> | |
12 | #include <linux/skbuff.h> | |
13 | ||
14 | #include <net/netfilter/nf_conntrack.h> | |
15 | #include <net/netfilter/nf_conntrack_helper.h> | |
16 | #include <net/netfilter/nf_conntrack_expect.h> | |
17 | ||
18 | int nf_conntrack_broadcast_help(struct sk_buff *skb, | |
93557f53 JO |
19 | struct nf_conn *ct, |
20 | enum ip_conntrack_info ctinfo, | |
21 | unsigned int timeout) | |
22 | { | |
e14575fa | 23 | const struct nf_conntrack_helper *helper; |
93557f53 JO |
24 | struct nf_conntrack_expect *exp; |
25 | struct iphdr *iph = ip_hdr(skb); | |
26 | struct rtable *rt = skb_rtable(skb); | |
27 | struct in_device *in_dev; | |
28 | struct nf_conn_help *help = nfct_help(ct); | |
29 | __be32 mask = 0; | |
30 | ||
31 | /* we're only interested in locally generated packets */ | |
f5646501 | 32 | if (skb->sk == NULL || !net_eq(nf_ct_net(ct), sock_net(skb->sk))) |
93557f53 JO |
33 | goto out; |
34 | if (rt == NULL || !(rt->rt_flags & RTCF_BROADCAST)) | |
35 | goto out; | |
36 | if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) | |
37 | goto out; | |
38 | ||
93557f53 JO |
39 | in_dev = __in_dev_get_rcu(rt->dst.dev); |
40 | if (in_dev != NULL) { | |
b8d19572 FW |
41 | const struct in_ifaddr *ifa; |
42 | ||
43 | in_dev_for_each_ifa_rcu(ifa, in_dev) { | |
44 | if (ifa->ifa_flags & IFA_F_SECONDARY) | |
45 | continue; | |
46 | ||
93557f53 JO |
47 | if (ifa->ifa_broadcast == iph->daddr) { |
48 | mask = ifa->ifa_mask; | |
49 | break; | |
50 | } | |
b8d19572 | 51 | } |
93557f53 | 52 | } |
93557f53 JO |
53 | |
54 | if (mask == 0) | |
55 | goto out; | |
56 | ||
57 | exp = nf_ct_expect_alloc(ct); | |
58 | if (exp == NULL) | |
59 | goto out; | |
60 | ||
61 | exp->tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple; | |
e14575fa FW |
62 | |
63 | helper = rcu_dereference(help->helper); | |
64 | if (helper) | |
65 | exp->tuple.src.u.udp.port = helper->tuple.src.u.udp.port; | |
93557f53 JO |
66 | |
67 | exp->mask.src.u3.ip = mask; | |
68 | exp->mask.src.u.udp.port = htons(0xFFFF); | |
69 | ||
70 | exp->expectfn = NULL; | |
71 | exp->flags = NF_CT_EXPECT_PERMANENT; | |
72 | exp->class = NF_CT_EXPECT_CLASS_DEFAULT; | |
73 | exp->helper = NULL; | |
74 | ||
3c00fb0b | 75 | nf_ct_expect_related(exp, 0); |
93557f53 JO |
76 | nf_ct_expect_put(exp); |
77 | ||
78 | nf_ct_refresh(ct, skb, timeout * HZ); | |
79 | out: | |
80 | return NF_ACCEPT; | |
81 | } | |
82 | EXPORT_SYMBOL_GPL(nf_conntrack_broadcast_help); | |
83 | ||
84 | MODULE_LICENSE("GPL"); |