]>
Commit | Line | Data |
---|---|---|
9fb9cbb1 YK |
1 | /* (C) 1999-2001 Paul `Rusty' Russell |
2 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
9fb9cbb1 YK |
7 | */ |
8 | ||
9 | #include <linux/types.h> | |
cd354f1a | 10 | #include <linux/jiffies.h> |
9fb9cbb1 YK |
11 | #include <linux/timer.h> |
12 | #include <linux/netfilter.h> | |
605dcad6 | 13 | #include <net/netfilter/nf_conntrack_l4proto.h> |
9fb9cbb1 | 14 | |
933a41e7 | 15 | static unsigned int nf_ct_generic_timeout __read_mostly = 600*HZ; |
9fb9cbb1 | 16 | |
db29a950 FW |
17 | static bool nf_generic_should_process(u8 proto) |
18 | { | |
19 | switch (proto) { | |
db29a950 FW |
20 | #ifdef CONFIG_NF_CT_PROTO_GRE_MODULE |
21 | case IPPROTO_GRE: | |
22 | return false; | |
db29a950 FW |
23 | #endif |
24 | default: | |
25 | return true; | |
26 | } | |
27 | } | |
28 | ||
15f585bd G |
29 | static inline struct nf_generic_net *generic_pernet(struct net *net) |
30 | { | |
31 | return &net->ct.nf_ct_proto.generic; | |
32 | } | |
33 | ||
09f263cd JE |
34 | static bool generic_pkt_to_tuple(const struct sk_buff *skb, |
35 | unsigned int dataoff, | |
a31f1adc | 36 | struct net *net, struct nf_conntrack_tuple *tuple) |
9fb9cbb1 YK |
37 | { |
38 | tuple->src.u.all = 0; | |
39 | tuple->dst.u.all = 0; | |
40 | ||
09f263cd | 41 | return true; |
9fb9cbb1 YK |
42 | } |
43 | ||
09f263cd JE |
44 | static bool generic_invert_tuple(struct nf_conntrack_tuple *tuple, |
45 | const struct nf_conntrack_tuple *orig) | |
9fb9cbb1 YK |
46 | { |
47 | tuple->src.u.all = 0; | |
48 | tuple->dst.u.all = 0; | |
49 | ||
09f263cd | 50 | return true; |
9fb9cbb1 YK |
51 | } |
52 | ||
2c8503f5 PNA |
53 | static unsigned int *generic_get_timeouts(struct net *net) |
54 | { | |
15f585bd | 55 | return &(generic_pernet(net)->timeout); |
2c8503f5 PNA |
56 | } |
57 | ||
9fb9cbb1 | 58 | /* Returns verdict for packet, or -1 for invalid. */ |
2c8503f5 PNA |
59 | static int generic_packet(struct nf_conn *ct, |
60 | const struct sk_buff *skb, | |
61 | unsigned int dataoff, | |
62 | enum ip_conntrack_info ctinfo, | |
2c8503f5 | 63 | unsigned int *timeout) |
9fb9cbb1 | 64 | { |
2c8503f5 | 65 | nf_ct_refresh_acct(ct, ctinfo, skb, *timeout); |
9fb9cbb1 YK |
66 | return NF_ACCEPT; |
67 | } | |
68 | ||
69 | /* Called when a new connection for this protocol found. */ | |
2c8503f5 PNA |
70 | static bool generic_new(struct nf_conn *ct, const struct sk_buff *skb, |
71 | unsigned int dataoff, unsigned int *timeouts) | |
9fb9cbb1 | 72 | { |
77966845 MRL |
73 | bool ret; |
74 | ||
75 | ret = nf_generic_should_process(nf_ct_protonum(ct)); | |
76 | if (!ret) | |
77 | pr_warn_once("conntrack: generic helper won't handle protocol %d. Please consider loading the specific helper module.\n", | |
78 | nf_ct_protonum(ct)); | |
79 | return ret; | |
9fb9cbb1 YK |
80 | } |
81 | ||
50978462 PNA |
82 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
83 | ||
84 | #include <linux/netfilter/nfnetlink.h> | |
85 | #include <linux/netfilter/nfnetlink_cttimeout.h> | |
86 | ||
8264deb8 G |
87 | static int generic_timeout_nlattr_to_obj(struct nlattr *tb[], |
88 | struct net *net, void *data) | |
50978462 PNA |
89 | { |
90 | unsigned int *timeout = data; | |
8264deb8 | 91 | struct nf_generic_net *gn = generic_pernet(net); |
50978462 PNA |
92 | |
93 | if (tb[CTA_TIMEOUT_GENERIC_TIMEOUT]) | |
94 | *timeout = | |
95 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_GENERIC_TIMEOUT])) * HZ; | |
96 | else { | |
97 | /* Set default generic timeout. */ | |
8264deb8 | 98 | *timeout = gn->timeout; |
50978462 PNA |
99 | } |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
104 | static int | |
105 | generic_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data) | |
106 | { | |
107 | const unsigned int *timeout = data; | |
108 | ||
f5776941 DM |
109 | if (nla_put_be32(skb, CTA_TIMEOUT_GENERIC_TIMEOUT, htonl(*timeout / HZ))) |
110 | goto nla_put_failure; | |
50978462 PNA |
111 | |
112 | return 0; | |
113 | ||
114 | nla_put_failure: | |
115 | return -ENOSPC; | |
116 | } | |
117 | ||
118 | static const struct nla_policy | |
119 | generic_timeout_nla_policy[CTA_TIMEOUT_GENERIC_MAX+1] = { | |
120 | [CTA_TIMEOUT_GENERIC_TIMEOUT] = { .type = NLA_U32 }, | |
121 | }; | |
122 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
123 | ||
933a41e7 | 124 | #ifdef CONFIG_SYSCTL |
933a41e7 PM |
125 | static struct ctl_table generic_sysctl_table[] = { |
126 | { | |
933a41e7 | 127 | .procname = "nf_conntrack_generic_timeout", |
933a41e7 PM |
128 | .maxlen = sizeof(unsigned int), |
129 | .mode = 0644, | |
6d9f239a | 130 | .proc_handler = proc_dointvec_jiffies, |
933a41e7 | 131 | }, |
f8572d8f | 132 | { } |
933a41e7 PM |
133 | }; |
134 | #endif /* CONFIG_SYSCTL */ | |
135 | ||
22ac0377 G |
136 | static int generic_kmemdup_sysctl_table(struct nf_proto_net *pn, |
137 | struct nf_generic_net *gn) | |
15f585bd | 138 | { |
15f585bd G |
139 | #ifdef CONFIG_SYSCTL |
140 | pn->ctl_table = kmemdup(generic_sysctl_table, | |
141 | sizeof(generic_sysctl_table), | |
142 | GFP_KERNEL); | |
143 | if (!pn->ctl_table) | |
144 | return -ENOMEM; | |
22ac0377 | 145 | |
15f585bd | 146 | pn->ctl_table[0].data = &gn->timeout; |
22ac0377 G |
147 | #endif |
148 | return 0; | |
149 | } | |
15f585bd | 150 | |
22ac0377 G |
151 | static int generic_init_net(struct net *net, u_int16_t proto) |
152 | { | |
22ac0377 G |
153 | struct nf_generic_net *gn = generic_pernet(net); |
154 | struct nf_proto_net *pn = &gn->pn; | |
155 | ||
156 | gn->timeout = nf_ct_generic_timeout; | |
157 | ||
adf05168 | 158 | return generic_kmemdup_sysctl_table(pn, gn); |
22ac0377 G |
159 | } |
160 | ||
08911475 PNA |
161 | static struct nf_proto_net *generic_get_net_proto(struct net *net) |
162 | { | |
163 | return &net->ct.nf_ct_proto.generic.pn; | |
164 | } | |
165 | ||
61075af5 | 166 | struct nf_conntrack_l4proto nf_conntrack_l4proto_generic __read_mostly = |
9fb9cbb1 YK |
167 | { |
168 | .l3proto = PF_UNSPEC, | |
fe2a7ce4 | 169 | .l4proto = 255, |
9fb9cbb1 YK |
170 | .pkt_to_tuple = generic_pkt_to_tuple, |
171 | .invert_tuple = generic_invert_tuple, | |
2c8503f5 PNA |
172 | .packet = generic_packet, |
173 | .get_timeouts = generic_get_timeouts, | |
174 | .new = generic_new, | |
50978462 PNA |
175 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
176 | .ctnl_timeout = { | |
177 | .nlattr_to_obj = generic_timeout_nlattr_to_obj, | |
178 | .obj_to_nlattr = generic_timeout_obj_to_nlattr, | |
179 | .nlattr_max = CTA_TIMEOUT_GENERIC_MAX, | |
180 | .obj_size = sizeof(unsigned int), | |
181 | .nla_policy = generic_timeout_nla_policy, | |
182 | }, | |
183 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
15f585bd | 184 | .init_net = generic_init_net, |
08911475 | 185 | .get_net_proto = generic_get_net_proto, |
9fb9cbb1 | 186 | }; |