1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Header for use in defining a given L4 protocol for connection tracking.
5 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
6 * - generalized L3 protocol dependent part.
8 * Derived from include/linux/netfiter_ipv4/ip_conntrack_protcol.h
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>
20 struct nf_conntrack_l4proto {
21 /* L3 Protocol number. */
24 /* L4 Protocol number. */
27 /* Resolve clashes on insertion races. */
30 /* protoinfo nlattr size, closes a hole */
33 /* Try to fill in the third arg: dataoff is offset past network protocol
34 hdr. Return true if possible. */
35 bool (*pkt_to_tuple)(const struct sk_buff *skb, unsigned int dataoff,
36 struct net *net, struct nf_conntrack_tuple *tuple);
38 /* Invert the per-proto part of the tuple: ie. turn xmit into reply.
39 * Only used by icmp, most protocols use a generic version.
41 bool (*invert_tuple)(struct nf_conntrack_tuple *inverse,
42 const struct nf_conntrack_tuple *orig);
44 /* Returns verdict for packet, or -1 for invalid. */
45 int (*packet)(struct nf_conn *ct,
46 const struct sk_buff *skb,
48 enum ip_conntrack_info ctinfo);
50 /* Called when a new connection for this protocol found;
51 * returns TRUE if it's OK. If so, packet() called next. */
52 bool (*new)(struct nf_conn *ct, const struct sk_buff *skb,
53 unsigned int dataoff);
55 /* Called when a conntrack entry is destroyed */
56 void (*destroy)(struct nf_conn *ct);
58 int (*error)(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
60 u_int8_t pf, unsigned int hooknum);
62 /* called by gc worker if table is full */
63 bool (*can_early_drop)(const struct nf_conn *ct);
65 /* convert protoinfo to nfnetink attributes */
66 int (*to_nlattr)(struct sk_buff *skb, struct nlattr *nla,
69 /* convert nfnetlink attributes to protoinfo */
70 int (*from_nlattr)(struct nlattr *tb[], struct nf_conn *ct);
72 int (*tuple_to_nlattr)(struct sk_buff *skb,
73 const struct nf_conntrack_tuple *t);
74 /* Calculate tuple nlattr size */
75 unsigned int (*nlattr_tuple_size)(void);
76 int (*nlattr_to_tuple)(struct nlattr *tb[],
77 struct nf_conntrack_tuple *t);
78 const struct nla_policy *nla_policy;
81 int (*nlattr_to_obj)(struct nlattr *tb[],
82 struct net *net, void *data);
83 int (*obj_to_nlattr)(struct sk_buff *skb, const void *data);
87 const struct nla_policy *nla_policy;
89 #ifdef CONFIG_NF_CONNTRACK_PROCFS
90 /* Print out the private part of the conntrack. */
91 void (*print_conntrack)(struct seq_file *s, struct nf_conn *);
94 /* Init l4proto pernet data */
95 int (*init_net)(struct net *net, u_int16_t proto);
97 /* Return the per-net protocol part. */
98 struct nf_proto_net *(*get_net_proto)(struct net *net);
100 /* Module (if any) which this is connected to. */
104 /* Existing built-in generic protocol */
105 extern const struct nf_conntrack_l4proto nf_conntrack_l4proto_generic;
107 #define MAX_NF_CT_PROTO 256
109 const struct nf_conntrack_l4proto *__nf_ct_l4proto_find(u_int16_t l3proto,
112 const struct nf_conntrack_l4proto *nf_ct_l4proto_find_get(u_int16_t l3proto,
114 void nf_ct_l4proto_put(const struct nf_conntrack_l4proto *p);
116 /* Protocol pernet registration. */
117 int nf_ct_l4proto_pernet_register_one(struct net *net,
118 const struct nf_conntrack_l4proto *proto);
119 void nf_ct_l4proto_pernet_unregister_one(struct net *net,
120 const struct nf_conntrack_l4proto *proto);
121 int nf_ct_l4proto_pernet_register(struct net *net,
122 const struct nf_conntrack_l4proto *const proto[],
123 unsigned int num_proto);
124 void nf_ct_l4proto_pernet_unregister(struct net *net,
125 const struct nf_conntrack_l4proto *const proto[],
126 unsigned int num_proto);
128 /* Protocol global registration. */
129 int nf_ct_l4proto_register_one(const struct nf_conntrack_l4proto *proto);
130 void nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *proto);
132 /* Generic netlink helpers */
133 int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
134 const struct nf_conntrack_tuple *tuple);
135 int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
136 struct nf_conntrack_tuple *t);
137 unsigned int nf_ct_port_nlattr_tuple_size(void);
138 extern const struct nla_policy nf_ct_port_nla_policy[];
141 __printf(3, 4) __cold
142 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb,
143 const struct nf_conn *ct,
144 const char *fmt, ...);
145 __printf(5, 6) __cold
146 void nf_l4proto_log_invalid(const struct sk_buff *skb,
149 const char *fmt, ...);
151 static inline __printf(5, 6) __cold
152 void nf_l4proto_log_invalid(const struct sk_buff *skb, struct net *net,
153 u16 pf, u8 protonum, const char *fmt, ...) {}
154 static inline __printf(3, 4) __cold
155 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb,
156 const struct nf_conn *ct,
157 const char *fmt, ...) { }
158 #endif /* CONFIG_SYSCTL */
160 #endif /*_NF_CONNTRACK_PROTOCOL_H*/