1 /*
2  * Header for use in defining a given L4 protocol for connection tracking.
3  *
4  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
5  *	- generalized L3 protocol dependent part.
6  *
7  * Derived from include/linux/netfiter_ipv4/ip_conntrack_protcol.h
8  */
9 
10 #ifndef _NF_CONNTRACK_L4PROTO_H
11 #define _NF_CONNTRACK_L4PROTO_H
12 #include <linux/netlink.h>
13 #include <net/netlink.h>
14 #include <net/netfilter/nf_conntrack.h>
15 #include <net/netns/generic.h>
16 
17 struct seq_file;
18 
19 struct nf_conntrack_l4proto {
20 	/* L3 Protocol number. */
21 	u_int16_t l3proto;
22 
23 	/* L4 Protocol number. */
24 	u_int8_t l4proto;
25 
26 	/* Resolve clashes on insertion races. */
27 	bool allow_clash;
28 
29 	/* Try to fill in the third arg: dataoff is offset past network protocol
30            hdr.  Return true if possible. */
31 	bool (*pkt_to_tuple)(const struct sk_buff *skb, unsigned int dataoff,
32 			     struct net *net, struct nf_conntrack_tuple *tuple);
33 
34 	/* Invert the per-proto part of the tuple: ie. turn xmit into reply.
35 	 * Some packets can't be inverted: return 0 in that case.
36 	 */
37 	bool (*invert_tuple)(struct nf_conntrack_tuple *inverse,
38 			     const struct nf_conntrack_tuple *orig);
39 
40 	/* Returns verdict for packet, or -1 for invalid. */
41 	int (*packet)(struct nf_conn *ct,
42 		      const struct sk_buff *skb,
43 		      unsigned int dataoff,
44 		      enum ip_conntrack_info ctinfo,
45 		      u_int8_t pf,
46 		      unsigned int hooknum,
47 		      unsigned int *timeouts);
48 
49 	/* Called when a new connection for this protocol found;
50 	 * returns TRUE if it's OK.  If so, packet() called next. */
51 	bool (*new)(struct nf_conn *ct, const struct sk_buff *skb,
52 		    unsigned int dataoff, unsigned int *timeouts);
53 
54 	/* Called when a conntrack entry is destroyed */
55 	void (*destroy)(struct nf_conn *ct);
56 
57 	int (*error)(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
58 		     unsigned int dataoff, enum ip_conntrack_info *ctinfo,
59 		     u_int8_t pf, unsigned int hooknum);
60 
61 	/* Print out the per-protocol part of the tuple. Return like seq_* */
62 	void (*print_tuple)(struct seq_file *s,
63 			    const struct nf_conntrack_tuple *);
64 
65 	/* Print out the private part of the conntrack. */
66 	void (*print_conntrack)(struct seq_file *s, struct nf_conn *);
67 
68 	/* Return the array of timeouts for this protocol. */
69 	unsigned int *(*get_timeouts)(struct net *net);
70 
71 	/* convert protoinfo to nfnetink attributes */
72 	int (*to_nlattr)(struct sk_buff *skb, struct nlattr *nla,
73 			 struct nf_conn *ct);
74 	/* Calculate protoinfo nlattr size */
75 	int (*nlattr_size)(void);
76 
77 	/* convert nfnetlink attributes to protoinfo */
78 	int (*from_nlattr)(struct nlattr *tb[], struct nf_conn *ct);
79 
80 	int (*tuple_to_nlattr)(struct sk_buff *skb,
81 			       const struct nf_conntrack_tuple *t);
82 	/* Calculate tuple nlattr size */
83 	int (*nlattr_tuple_size)(void);
84 	int (*nlattr_to_tuple)(struct nlattr *tb[],
85 			       struct nf_conntrack_tuple *t);
86 	const struct nla_policy *nla_policy;
87 
88 	size_t nla_size;
89 
90 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
91 	struct {
92 		size_t obj_size;
93 		int (*nlattr_to_obj)(struct nlattr *tb[],
94 				     struct net *net, void *data);
95 		int (*obj_to_nlattr)(struct sk_buff *skb, const void *data);
96 
97 		unsigned int nlattr_max;
98 		const struct nla_policy *nla_policy;
99 	} ctnl_timeout;
100 #endif
101 	int	*net_id;
102 	/* Init l4proto pernet data */
103 	int (*init_net)(struct net *net, u_int16_t proto);
104 
105 	/* Return the per-net protocol part. */
106 	struct nf_proto_net *(*get_net_proto)(struct net *net);
107 
108 	/* Protocol name */
109 	const char *name;
110 
111 	/* Module (if any) which this is connected to. */
112 	struct module *me;
113 };
114 
115 /* Existing built-in generic protocol */
116 extern struct nf_conntrack_l4proto nf_conntrack_l4proto_generic;
117 
118 #define MAX_NF_CT_PROTO 256
119 
120 struct nf_conntrack_l4proto *__nf_ct_l4proto_find(u_int16_t l3proto,
121 						  u_int8_t l4proto);
122 
123 struct nf_conntrack_l4proto *nf_ct_l4proto_find_get(u_int16_t l3proto,
124 						    u_int8_t l4proto);
125 void nf_ct_l4proto_put(struct nf_conntrack_l4proto *p);
126 
127 /* Protocol pernet registration. */
128 int nf_ct_l4proto_pernet_register(struct net *net,
129 				  struct nf_conntrack_l4proto *proto);
130 void nf_ct_l4proto_pernet_unregister(struct net *net,
131 				     struct nf_conntrack_l4proto *proto);
132 
133 /* Protocol global registration. */
134 int nf_ct_l4proto_register(struct nf_conntrack_l4proto *proto);
135 void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto *proto);
136 
137 static inline void nf_ct_kfree_compat_sysctl_table(struct nf_proto_net *pn)
138 {
139 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
140 	kfree(pn->ctl_compat_table);
141 	pn->ctl_compat_table = NULL;
142 #endif
143 }
144 
145 /* Generic netlink helpers */
146 int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
147 			       const struct nf_conntrack_tuple *tuple);
148 int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
149 			       struct nf_conntrack_tuple *t);
150 int nf_ct_port_nlattr_tuple_size(void);
151 extern const struct nla_policy nf_ct_port_nla_policy[];
152 
153 #ifdef CONFIG_SYSCTL
154 #define LOG_INVALID(net, proto)				\
155 	((net)->ct.sysctl_log_invalid == (proto) ||	\
156 	 (net)->ct.sysctl_log_invalid == IPPROTO_RAW)
157 #else
158 static inline int LOG_INVALID(struct net *net, int proto) { return 0; }
159 #endif /* CONFIG_SYSCTL */
160 
161 #endif /*_NF_CONNTRACK_PROTOCOL_H*/
162