]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - include/net/netfilter/nf_conntrack_l4proto.h
netfilter: conntrack: handle builtin l4proto packet functions via direct calls
[thirdparty/kernel/stable.git] / include / net / netfilter / nf_conntrack_l4proto.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Header for use in defining a given L4 protocol for connection tracking.
4 *
5 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
6 * - generalized L3 protocol dependent part.
7 *
8 * Derived from include/linux/netfiter_ipv4/ip_conntrack_protcol.h
9 */
10
11 #ifndef _NF_CONNTRACK_L4PROTO_H
12 #define _NF_CONNTRACK_L4PROTO_H
13 #include <linux/netlink.h>
14 #include <net/netlink.h>
15 #include <net/netfilter/nf_conntrack.h>
16 #include <net/netns/generic.h>
17
18 struct seq_file;
19
20 struct nf_conntrack_l4proto {
21 /* L4 Protocol number. */
22 u_int8_t l4proto;
23
24 /* Resolve clashes on insertion races. */
25 bool allow_clash;
26
27 /* protoinfo nlattr size, closes a hole */
28 u16 nlattr_size;
29
30 /* Try to fill in the third arg: dataoff is offset past network protocol
31 hdr. Return true if possible. */
32 bool (*pkt_to_tuple)(const struct sk_buff *skb, unsigned int dataoff,
33 struct net *net, struct nf_conntrack_tuple *tuple);
34
35 /* Invert the per-proto part of the tuple: ie. turn xmit into reply.
36 * Only used by icmp, most protocols use a generic version.
37 */
38 bool (*invert_tuple)(struct nf_conntrack_tuple *inverse,
39 const struct nf_conntrack_tuple *orig);
40
41 /* Returns verdict for packet, or -1 for invalid. */
42 int (*packet)(struct nf_conn *ct,
43 struct sk_buff *skb,
44 unsigned int dataoff,
45 enum ip_conntrack_info ctinfo,
46 const struct nf_hook_state *state);
47
48 /* Called when a conntrack entry is destroyed */
49 void (*destroy)(struct nf_conn *ct);
50
51 /* called by gc worker if table is full */
52 bool (*can_early_drop)(const struct nf_conn *ct);
53
54 /* convert protoinfo to nfnetink attributes */
55 int (*to_nlattr)(struct sk_buff *skb, struct nlattr *nla,
56 struct nf_conn *ct);
57
58 /* convert nfnetlink attributes to protoinfo */
59 int (*from_nlattr)(struct nlattr *tb[], struct nf_conn *ct);
60
61 int (*tuple_to_nlattr)(struct sk_buff *skb,
62 const struct nf_conntrack_tuple *t);
63 /* Calculate tuple nlattr size */
64 unsigned int (*nlattr_tuple_size)(void);
65 int (*nlattr_to_tuple)(struct nlattr *tb[],
66 struct nf_conntrack_tuple *t);
67 const struct nla_policy *nla_policy;
68
69 struct {
70 int (*nlattr_to_obj)(struct nlattr *tb[],
71 struct net *net, void *data);
72 int (*obj_to_nlattr)(struct sk_buff *skb, const void *data);
73
74 u16 obj_size;
75 u16 nlattr_max;
76 const struct nla_policy *nla_policy;
77 } ctnl_timeout;
78 #ifdef CONFIG_NF_CONNTRACK_PROCFS
79 /* Print out the private part of the conntrack. */
80 void (*print_conntrack)(struct seq_file *s, struct nf_conn *);
81 #endif
82 unsigned int *net_id;
83 /* Init l4proto pernet data */
84 int (*init_net)(struct net *net);
85
86 /* Return the per-net protocol part. */
87 struct nf_proto_net *(*get_net_proto)(struct net *net);
88
89 /* Module (if any) which this is connected to. */
90 struct module *me;
91 };
92
93 int nf_conntrack_icmpv4_error(struct nf_conn *tmpl,
94 struct sk_buff *skb,
95 unsigned int dataoff,
96 const struct nf_hook_state *state);
97
98 int nf_conntrack_icmpv6_error(struct nf_conn *tmpl,
99 struct sk_buff *skb,
100 unsigned int dataoff,
101 const struct nf_hook_state *state);
102
103 int nf_conntrack_icmp_packet(struct nf_conn *ct,
104 struct sk_buff *skb,
105 enum ip_conntrack_info ctinfo,
106 const struct nf_hook_state *state);
107
108 int nf_conntrack_icmpv6_packet(struct nf_conn *ct,
109 struct sk_buff *skb,
110 enum ip_conntrack_info ctinfo,
111 const struct nf_hook_state *state);
112
113 int nf_conntrack_udp_packet(struct nf_conn *ct,
114 struct sk_buff *skb,
115 unsigned int dataoff,
116 enum ip_conntrack_info ctinfo,
117 const struct nf_hook_state *state);
118 int nf_conntrack_udplite_packet(struct nf_conn *ct,
119 struct sk_buff *skb,
120 unsigned int dataoff,
121 enum ip_conntrack_info ctinfo,
122 const struct nf_hook_state *state);
123 int nf_conntrack_tcp_packet(struct nf_conn *ct,
124 struct sk_buff *skb,
125 unsigned int dataoff,
126 enum ip_conntrack_info ctinfo,
127 const struct nf_hook_state *state);
128 int nf_conntrack_dccp_packet(struct nf_conn *ct,
129 struct sk_buff *skb,
130 unsigned int dataoff,
131 enum ip_conntrack_info ctinfo,
132 const struct nf_hook_state *state);
133 int nf_conntrack_sctp_packet(struct nf_conn *ct,
134 struct sk_buff *skb,
135 unsigned int dataoff,
136 enum ip_conntrack_info ctinfo,
137 const struct nf_hook_state *state);
138
139 /* Existing built-in generic protocol */
140 extern const struct nf_conntrack_l4proto nf_conntrack_l4proto_generic;
141
142 #define MAX_NF_CT_PROTO IPPROTO_UDPLITE
143
144 const struct nf_conntrack_l4proto *__nf_ct_l4proto_find(u8 l4proto);
145
146 const struct nf_conntrack_l4proto *nf_ct_l4proto_find_get(u8 l4proto);
147 void nf_ct_l4proto_put(const struct nf_conntrack_l4proto *p);
148
149 /* Protocol pernet registration. */
150 int nf_ct_l4proto_pernet_register_one(struct net *net,
151 const struct nf_conntrack_l4proto *proto);
152 void nf_ct_l4proto_pernet_unregister_one(struct net *net,
153 const struct nf_conntrack_l4proto *proto);
154 int nf_ct_l4proto_pernet_register(struct net *net,
155 const struct nf_conntrack_l4proto *const proto[],
156 unsigned int num_proto);
157 void nf_ct_l4proto_pernet_unregister(struct net *net,
158 const struct nf_conntrack_l4proto *const proto[],
159 unsigned int num_proto);
160
161 /* Protocol global registration. */
162 int nf_ct_l4proto_register_one(const struct nf_conntrack_l4proto *proto);
163 void nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *proto);
164
165 /* Generic netlink helpers */
166 int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
167 const struct nf_conntrack_tuple *tuple);
168 int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
169 struct nf_conntrack_tuple *t);
170 unsigned int nf_ct_port_nlattr_tuple_size(void);
171 extern const struct nla_policy nf_ct_port_nla_policy[];
172
173 #ifdef CONFIG_SYSCTL
174 __printf(3, 4) __cold
175 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb,
176 const struct nf_conn *ct,
177 const char *fmt, ...);
178 __printf(5, 6) __cold
179 void nf_l4proto_log_invalid(const struct sk_buff *skb,
180 struct net *net,
181 u16 pf, u8 protonum,
182 const char *fmt, ...);
183 #else
184 static inline __printf(5, 6) __cold
185 void nf_l4proto_log_invalid(const struct sk_buff *skb, struct net *net,
186 u16 pf, u8 protonum, const char *fmt, ...) {}
187 static inline __printf(3, 4) __cold
188 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb,
189 const struct nf_conn *ct,
190 const char *fmt, ...) { }
191 #endif /* CONFIG_SYSCTL */
192
193 static inline struct nf_generic_net *nf_generic_pernet(struct net *net)
194 {
195 return &net->ct.nf_ct_proto.generic;
196 }
197
198 static inline struct nf_tcp_net *nf_tcp_pernet(struct net *net)
199 {
200 return &net->ct.nf_ct_proto.tcp;
201 }
202
203 static inline struct nf_udp_net *nf_udp_pernet(struct net *net)
204 {
205 return &net->ct.nf_ct_proto.udp;
206 }
207
208 static inline struct nf_icmp_net *nf_icmp_pernet(struct net *net)
209 {
210 return &net->ct.nf_ct_proto.icmp;
211 }
212
213 static inline struct nf_icmp_net *nf_icmpv6_pernet(struct net *net)
214 {
215 return &net->ct.nf_ct_proto.icmpv6;
216 }
217
218 #ifdef CONFIG_NF_CT_PROTO_DCCP
219 static inline struct nf_dccp_net *nf_dccp_pernet(struct net *net)
220 {
221 return &net->ct.nf_ct_proto.dccp;
222 }
223 #endif
224
225 #ifdef CONFIG_NF_CT_PROTO_SCTP
226 static inline struct nf_sctp_net *nf_sctp_pernet(struct net *net)
227 {
228 return &net->ct.nf_ct_proto.sctp;
229 }
230 #endif
231
232 #endif /*_NF_CONNTRACK_PROTOCOL_H*/