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 	/* L3 Protocol number. */
22 	u_int16_t l3proto;
23 
24 	/* L4 Protocol number. */
25 	u_int8_t l4proto;
26 
27 	/* Resolve clashes on insertion races. */
28 	bool allow_clash;
29 
30 	/* protoinfo nlattr size, closes a hole */
31 	u16 nlattr_size;
32 
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);
37 
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.
40 	 */
41 	bool (*invert_tuple)(struct nf_conntrack_tuple *inverse,
42 			     const struct nf_conntrack_tuple *orig);
43 
44 	/* Returns verdict for packet, or -1 for invalid. */
45 	int (*packet)(struct nf_conn *ct,
46 		      const struct sk_buff *skb,
47 		      unsigned int dataoff,
48 		      enum ip_conntrack_info ctinfo);
49 
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);
54 
55 	/* Called when a conntrack entry is destroyed */
56 	void (*destroy)(struct nf_conn *ct);
57 
58 	int (*error)(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
59 		     unsigned int dataoff,
60 		     u_int8_t pf, unsigned int hooknum);
61 
62 	/* called by gc worker if table is full */
63 	bool (*can_early_drop)(const struct nf_conn *ct);
64 
65 	/* convert protoinfo to nfnetink attributes */
66 	int (*to_nlattr)(struct sk_buff *skb, struct nlattr *nla,
67 			 struct nf_conn *ct);
68 
69 	/* convert nfnetlink attributes to protoinfo */
70 	int (*from_nlattr)(struct nlattr *tb[], struct nf_conn *ct);
71 
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;
79 
80 	struct {
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);
84 
85 		u16 obj_size;
86 		u16 nlattr_max;
87 		const struct nla_policy *nla_policy;
88 	} ctnl_timeout;
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 *);
92 #endif
93 	unsigned int	*net_id;
94 	/* Init l4proto pernet data */
95 	int (*init_net)(struct net *net, u_int16_t proto);
96 
97 	/* Return the per-net protocol part. */
98 	struct nf_proto_net *(*get_net_proto)(struct net *net);
99 
100 	/* Module (if any) which this is connected to. */
101 	struct module *me;
102 };
103 
104 /* Existing built-in generic protocol */
105 extern const struct nf_conntrack_l4proto nf_conntrack_l4proto_generic;
106 
107 #define MAX_NF_CT_PROTO 256
108 
109 const struct nf_conntrack_l4proto *__nf_ct_l4proto_find(u_int16_t l3proto,
110 						  u_int8_t l4proto);
111 
112 const struct nf_conntrack_l4proto *nf_ct_l4proto_find_get(u_int16_t l3proto,
113 						    u_int8_t l4proto);
114 void nf_ct_l4proto_put(const struct nf_conntrack_l4proto *p);
115 
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);
127 
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);
131 
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[];
139 
140 #ifdef CONFIG_SYSCTL
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,
147 			    struct net *net,
148 			    u16 pf, u8 protonum,
149 			    const char *fmt, ...);
150 #else
151 static inline __printf(5, 6) __cold
nf_l4proto_log_invalid(const struct sk_buff * skb,struct net * net,u16 pf,u8 protonum,const char * fmt,...)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
nf_ct_l4proto_log_invalid(const struct sk_buff * skb,const struct nf_conn * ct,const char * fmt,...)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 */
159 
160 #endif /*_NF_CONNTRACK_PROTOCOL_H*/
161