1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NF_CONNTRACK_SIP_H__
3 #define __NF_CONNTRACK_SIP_H__
4 #ifdef __KERNEL__
5 
6 #include <net/netfilter/nf_conntrack_expect.h>
7 
8 #include <linux/types.h>
9 
10 #define SIP_PORT	5060
11 #define SIP_TIMEOUT	3600
12 
13 struct nf_ct_sip_master {
14 	unsigned int	register_cseq;
15 	unsigned int	invite_cseq;
16 	__be16		forced_dport;
17 };
18 
19 enum sip_expectation_classes {
20 	SIP_EXPECT_SIGNALLING,
21 	SIP_EXPECT_AUDIO,
22 	SIP_EXPECT_VIDEO,
23 	SIP_EXPECT_IMAGE,
24 	__SIP_EXPECT_MAX
25 };
26 #define SIP_EXPECT_MAX	(__SIP_EXPECT_MAX - 1)
27 
28 struct sdp_media_type {
29 	const char			*name;
30 	unsigned int			len;
31 	enum sip_expectation_classes	class;
32 };
33 
34 #define SDP_MEDIA_TYPE(__name, __class)					\
35 {									\
36 	.name	= (__name),						\
37 	.len	= sizeof(__name) - 1,					\
38 	.class	= (__class),						\
39 }
40 
41 struct sip_handler {
42 	const char	*method;
43 	unsigned int	len;
44 	int		(*request)(struct sk_buff *skb, unsigned int protoff,
45 				   unsigned int dataoff,
46 				   const char **dptr, unsigned int *datalen,
47 				   unsigned int cseq);
48 	int		(*response)(struct sk_buff *skb, unsigned int protoff,
49 				    unsigned int dataoff,
50 				    const char **dptr, unsigned int *datalen,
51 				    unsigned int cseq, unsigned int code);
52 };
53 
54 #define SIP_HANDLER(__method, __request, __response)			\
55 {									\
56 	.method		= (__method),					\
57 	.len		= sizeof(__method) - 1,				\
58 	.request	= (__request),					\
59 	.response	= (__response),					\
60 }
61 
62 struct sip_header {
63 	const char	*name;
64 	const char	*cname;
65 	const char	*search;
66 	unsigned int	len;
67 	unsigned int	clen;
68 	unsigned int	slen;
69 	int		(*match_len)(const struct nf_conn *ct,
70 				     const char *dptr, const char *limit,
71 				     int *shift);
72 };
73 
74 #define __SIP_HDR(__name, __cname, __search, __match)			\
75 {									\
76 	.name		= (__name),					\
77 	.len		= sizeof(__name) - 1,				\
78 	.cname		= (__cname),					\
79 	.clen		= (__cname) ? sizeof(__cname) - 1 : 0,		\
80 	.search		= (__search),					\
81 	.slen		= (__search) ? sizeof(__search) - 1 : 0,	\
82 	.match_len	= (__match),					\
83 }
84 
85 #define SIP_HDR(__name, __cname, __search, __match) \
86 	__SIP_HDR(__name, __cname, __search, __match)
87 
88 #define SDP_HDR(__name, __search, __match) \
89 	__SIP_HDR(__name, NULL, __search, __match)
90 
91 enum sip_header_types {
92 	SIP_HDR_CSEQ,
93 	SIP_HDR_FROM,
94 	SIP_HDR_TO,
95 	SIP_HDR_CONTACT,
96 	SIP_HDR_VIA_UDP,
97 	SIP_HDR_VIA_TCP,
98 	SIP_HDR_EXPIRES,
99 	SIP_HDR_CONTENT_LENGTH,
100 	SIP_HDR_CALL_ID,
101 };
102 
103 enum sdp_header_types {
104 	SDP_HDR_UNSPEC,
105 	SDP_HDR_VERSION,
106 	SDP_HDR_OWNER,
107 	SDP_HDR_CONNECTION,
108 	SDP_HDR_MEDIA,
109 };
110 
111 struct nf_nat_sip_hooks {
112 	unsigned int (*msg)(struct sk_buff *skb,
113 			    unsigned int protoff,
114 			    unsigned int dataoff,
115 			    const char **dptr,
116 			    unsigned int *datalen);
117 
118 	void (*seq_adjust)(struct sk_buff *skb,
119 			   unsigned int protoff, s16 off);
120 
121 	unsigned int (*expect)(struct sk_buff *skb,
122 			       unsigned int protoff,
123 			       unsigned int dataoff,
124 			       const char **dptr,
125 			       unsigned int *datalen,
126 			       struct nf_conntrack_expect *exp,
127 			       unsigned int matchoff,
128 			       unsigned int matchlen);
129 
130 	unsigned int (*sdp_addr)(struct sk_buff *skb,
131 				 unsigned int protoff,
132 				 unsigned int dataoff,
133 				 const char **dptr,
134 				 unsigned int *datalen,
135 				 unsigned int sdpoff,
136 				 enum sdp_header_types type,
137 				 enum sdp_header_types term,
138 				 const union nf_inet_addr *addr);
139 
140 	unsigned int (*sdp_port)(struct sk_buff *skb,
141 				 unsigned int protoff,
142 				 unsigned int dataoff,
143 				 const char **dptr,
144 				 unsigned int *datalen,
145 				 unsigned int matchoff,
146 				 unsigned int matchlen,
147 				 u_int16_t port);
148 
149 	unsigned int (*sdp_session)(struct sk_buff *skb,
150 				    unsigned int protoff,
151 				    unsigned int dataoff,
152 				    const char **dptr,
153 				    unsigned int *datalen,
154 				    unsigned int sdpoff,
155 				    const union nf_inet_addr *addr);
156 
157 	unsigned int (*sdp_media)(struct sk_buff *skb,
158 				  unsigned int protoff,
159 				  unsigned int dataoff,
160 				  const char **dptr,
161 				  unsigned int *datalen,
162 				  struct nf_conntrack_expect *rtp_exp,
163 				  struct nf_conntrack_expect *rtcp_exp,
164 				  unsigned int mediaoff,
165 				  unsigned int medialen,
166 				  union nf_inet_addr *rtp_addr);
167 };
168 extern const struct nf_nat_sip_hooks *nf_nat_sip_hooks;
169 
170 int ct_sip_parse_request(const struct nf_conn *ct, const char *dptr,
171 			 unsigned int datalen, unsigned int *matchoff,
172 			 unsigned int *matchlen, union nf_inet_addr *addr,
173 			 __be16 *port);
174 int ct_sip_get_header(const struct nf_conn *ct, const char *dptr,
175 		      unsigned int dataoff, unsigned int datalen,
176 		      enum sip_header_types type, unsigned int *matchoff,
177 		      unsigned int *matchlen);
178 int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
179 			    unsigned int *dataoff, unsigned int datalen,
180 			    enum sip_header_types type, int *in_header,
181 			    unsigned int *matchoff, unsigned int *matchlen,
182 			    union nf_inet_addr *addr, __be16 *port);
183 int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr,
184 			       unsigned int dataoff, unsigned int datalen,
185 			       const char *name, unsigned int *matchoff,
186 			       unsigned int *matchlen, union nf_inet_addr *addr,
187 			       bool delim);
188 int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr,
189 				 unsigned int off, unsigned int datalen,
190 				 const char *name, unsigned int *matchoff,
191 				 unsigned int *matchen, unsigned int *val);
192 
193 int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr,
194 			  unsigned int dataoff, unsigned int datalen,
195 			  enum sdp_header_types type,
196 			  enum sdp_header_types term,
197 			  unsigned int *matchoff, unsigned int *matchlen);
198 
199 #endif /* __KERNEL__ */
200 #endif /* __NF_CONNTRACK_SIP_H__ */
201