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