13 #include <linux/module.h>
14 #include <linux/ctype.h>
18 #include <linux/udp.h>
19 #include <linux/tcp.h>
20 #include <linux/netfilter.h>
37 static unsigned int ports_c;
47 MODULE_PARM_DESC(sip_direct_signalling,
"expect incoming calls from registrar "
52 MODULE_PARM_DESC(sip_direct_media,
"Expect Media streams between signalling "
53 "endpoints only (default 1)");
56 unsigned int dataoff,
const char **dptr,
70 unsigned int matchoff,
79 enum sdp_header_types
type,
80 enum sdp_header_types term,
89 unsigned int matchoff,
90 unsigned int matchlen,
105 unsigned int dataoff,
110 unsigned int mediaoff,
111 unsigned int medialen,
116 static int string_len(
const struct nf_conn *
ct,
const char *dptr,
117 const char *
limit,
int *shift)
121 while (dptr < limit &&
isalpha(*dptr)) {
128 static int digits_len(
const struct nf_conn *ct,
const char *dptr,
129 const char *limit,
int *shift)
132 while (dptr < limit &&
isdigit(*dptr)) {
139 static int iswordc(
const char c)
141 if (
isalnum(c) || c ==
'!' || c ==
'"' || c ==
'%' ||
142 (c >=
'(' && c <=
'/') || c ==
':' || c ==
'<' || c ==
'>' ||
143 c ==
'?' || (c >=
'[' && c <=
']') || c ==
'_' || c ==
'`' ||
144 c ==
'{' || c ==
'}' || c ==
'~')
149 static int word_len(
const char *dptr,
const char *limit)
152 while (dptr < limit && iswordc(*dptr)) {
159 static int callid_len(
const struct nf_conn *ct,
const char *dptr,
160 const char *limit,
int *shift)
164 len = word_len(dptr, limit);
166 if (!len || dptr == limit || *dptr !=
'@')
171 domain_len = word_len(dptr, limit);
174 return len + domain_len;
178 static int media_len(
const struct nf_conn *ct,
const char *dptr,
179 const char *limit,
int *shift)
181 int len = string_len(ct, dptr, limit, shift);
184 if (dptr >= limit || *dptr !=
' ')
189 return len + digits_len(ct, dptr, limit, shift);
192 static int sip_parse_addr(
const struct nf_conn *ct,
const char *
cp,
194 const char *limit,
bool delim)
202 memset(addr, 0,
sizeof(*addr));
203 switch (nf_ct_l3num(ct)) {
205 ret =
in4_pton(cp, limit - cp, (
u8 *)&addr->
ip, -1, &end);
210 if (cp < limit && *cp ==
'[')
219 if (end < limit && *end ==
']')
234 static int epaddr_len(
const struct nf_conn *ct,
const char *dptr,
235 const char *limit,
int *shift)
238 const char *
aux = dptr;
240 if (!sip_parse_addr(ct, dptr, &dptr, &addr, limit,
true)) {
241 pr_debug(
"ip: %s parse failed.!\n", dptr);
248 dptr += digits_len(ct, dptr, limit, shift);
254 static int skp_epaddr_len(
const struct nf_conn *ct,
const char *dptr,
255 const char *limit,
int *shift)
257 const char *
start = dptr;
263 while (dptr < limit &&
264 *dptr !=
'@' && *dptr !=
'\r' && *dptr !=
'\n') {
269 if (dptr < limit && *dptr ==
'@') {
277 return epaddr_len(ct, dptr, limit, shift);
287 const char *dptr,
unsigned int datalen,
288 unsigned int *matchoff,
unsigned int *matchlen,
291 const char *start = dptr, *limit = dptr +
datalen, *
end;
297 mlen = string_len(ct, dptr, limit,
NULL);
305 for (; dptr < limit -
strlen(
"sip:"); dptr++) {
306 if (*dptr ==
'\r' || *dptr ==
'\n')
313 if (!skp_epaddr_len(ct, dptr, limit, &shift))
317 if (!sip_parse_addr(ct, dptr, &end, addr, limit,
true))
319 if (end < limit && *end ==
':') {
322 if (p < 1024 || p > 65535)
326 *port =
htons(SIP_PORT);
330 *matchoff = dptr -
start;
331 *matchlen = end - dptr;
349 static const struct sip_header ct_sip_hdrs[] = {
350 [SIP_HDR_CSEQ] = SIP_HDR(
"CSeq",
NULL,
NULL, digits_len),
351 [SIP_HDR_FROM] = SIP_HDR(
"From",
"f",
"sip:", skp_epaddr_len),
352 [SIP_HDR_TO] = SIP_HDR(
"To",
"t",
"sip:", skp_epaddr_len),
353 [SIP_HDR_CONTACT] = SIP_HDR(
"Contact",
"m",
"sip:", skp_epaddr_len),
354 [SIP_HDR_VIA_UDP] = SIP_HDR(
"Via",
"v",
"UDP ", epaddr_len),
355 [SIP_HDR_VIA_TCP] = SIP_HDR(
"Via",
"v",
"TCP ", epaddr_len),
356 [SIP_HDR_EXPIRES] = SIP_HDR(
"Expires",
NULL,
NULL, digits_len),
357 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR(
"Content-Length",
"l",
NULL, digits_len),
358 [SIP_HDR_CALL_ID] = SIP_HDR(
"Call-Id",
"i",
NULL, callid_len),
361 static const char *sip_follow_continuation(
const char *dptr,
const char *limit)
368 if (*(dptr - 1) ==
'\r' && *dptr ==
'\n') {
374 if (*dptr !=
' ' && *dptr !=
'\t')
378 for (; dptr <
limit; dptr++) {
379 if (*dptr !=
' ' && *dptr !=
'\t')
385 static const char *sip_skip_whitespace(
const char *dptr,
const char *limit)
387 for (; dptr <
limit; dptr++) {
390 if (*dptr !=
'\r' && *dptr !=
'\n')
392 dptr = sip_follow_continuation(dptr, limit);
400 static const char *ct_sip_header_search(
const char *dptr,
const char *limit,
401 const char *needle,
unsigned int len)
403 for (limit -= len; dptr <
limit; dptr++) {
404 if (*dptr ==
'\r' || *dptr ==
'\n') {
405 dptr = sip_follow_continuation(dptr, limit);
411 if (
strnicmp(dptr, needle, len) == 0)
418 unsigned int dataoff,
unsigned int datalen,
419 enum sip_header_types
type,
420 unsigned int *matchoff,
unsigned int *matchlen)
422 const struct sip_header *
hdr = &ct_sip_hdrs[
type];
423 const char *start = dptr, *limit = dptr +
datalen;
426 for (dptr += dataoff; dptr <
limit; dptr++) {
428 if (*dptr !=
'\r' && *dptr !=
'\n')
432 if (*(dptr - 1) ==
'\r' && *dptr ==
'\n') {
438 if (*dptr ==
' ' || *dptr ==
'\t')
443 if (limit - dptr >= hdr->len &&
444 strnicmp(dptr, hdr->name, hdr->len) == 0)
446 else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
447 strnicmp(dptr, hdr->cname, hdr->clen) == 0 &&
454 dptr = sip_skip_whitespace(dptr, limit);
457 if (*dptr !=
':' || ++dptr >= limit)
461 dptr = sip_skip_whitespace(dptr, limit);
465 *matchoff = dptr -
start;
467 dptr = ct_sip_header_search(dptr, limit, hdr->search,
474 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
477 *matchoff = dptr - start + shift;
485 static int ct_sip_next_header(
const struct nf_conn *ct,
const char *dptr,
486 unsigned int dataoff,
unsigned int datalen,
487 enum sip_header_types
type,
488 unsigned int *matchoff,
unsigned int *matchlen)
490 const struct sip_header *
hdr = &ct_sip_hdrs[
type];
491 const char *start = dptr, *limit = dptr +
datalen;
496 dptr = ct_sip_header_search(dptr, limit,
",",
strlen(
","));
500 dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen);
505 *matchoff = dptr -
start;
506 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
515 static int ct_sip_walk_headers(
const struct nf_conn *ct,
const char *dptr,
516 unsigned int dataoff,
unsigned int datalen,
517 enum sip_header_types type,
int *in_header,
518 unsigned int *matchoff,
unsigned int *matchlen)
522 if (in_header && *in_header) {
524 ret = ct_sip_next_header(ct, dptr, dataoff, datalen,
525 type, matchoff, matchlen);
530 dataoff += *matchoff;
537 type, matchoff, matchlen);
542 dataoff += *matchoff;
556 unsigned int *dataoff,
unsigned int datalen,
557 enum sip_header_types type,
int *in_header,
558 unsigned int *matchoff,
unsigned int *matchlen,
561 const char *
c, *limit = dptr +
datalen;
565 ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
566 type, in_header, matchoff, matchlen);
571 if (!sip_parse_addr(ct, dptr + *matchoff, &c, addr, limit,
true))
576 if (p < 1024 || p > 65535)
580 *port =
htons(SIP_PORT);
588 static int ct_sip_parse_param(
const struct nf_conn *ct,
const char *dptr,
589 unsigned int dataoff,
unsigned int datalen,
591 unsigned int *matchoff,
unsigned int *matchlen)
593 const char *limit = dptr +
datalen;
597 limit = ct_sip_header_search(dptr + dataoff, limit,
",",
strlen(
","));
601 start = ct_sip_header_search(dptr + dataoff, limit, name,
strlen(name));
606 end = ct_sip_header_search(start, limit,
";",
strlen(
";"));
610 *matchoff = start - dptr;
611 *matchlen = end -
start;
617 unsigned int dataoff,
unsigned int datalen,
619 unsigned int *matchoff,
unsigned int *matchlen,
622 const char *limit = dptr +
datalen;
625 limit = ct_sip_header_search(dptr + dataoff, limit,
",",
strlen(
","));
629 start = ct_sip_header_search(dptr + dataoff, limit, name,
strlen(name));
634 if (!sip_parse_addr(ct, start, &end, addr, limit, delim))
636 *matchoff = start - dptr;
637 *matchlen = end -
start;
644 unsigned int dataoff,
unsigned int datalen,
646 unsigned int *matchoff,
unsigned int *matchlen,
649 const char *limit = dptr +
datalen;
653 limit = ct_sip_header_search(dptr + dataoff, limit,
",",
strlen(
","));
657 start = ct_sip_header_search(dptr + dataoff, limit, name,
strlen(name));
665 if (matchoff && matchlen) {
666 *matchoff = start - dptr;
667 *matchlen = end -
start;
673 static int ct_sip_parse_transport(
struct nf_conn *ct,
const char *dptr,
674 unsigned int dataoff,
unsigned int datalen,
677 unsigned int matchoff, matchlen;
679 if (ct_sip_parse_param(ct, dptr, dataoff, datalen,
"transport=",
680 &matchoff, &matchlen)) {
688 if (*proto != nf_ct_protonum(ct))
691 *proto = nf_ct_protonum(ct);
696 static int sdp_parse_addr(
const struct nf_conn *ct,
const char *cp,
703 memset(addr, 0,
sizeof(*addr));
704 switch (nf_ct_l3num(ct)) {
706 ret =
in4_pton(cp, limit - cp, (
u8 *)&addr->
ip, -1, &end);
723 static int sdp_addr_len(
const struct nf_conn *ct,
const char *dptr,
724 const char *limit,
int *shift)
727 const char *aux = dptr;
729 if (!sdp_parse_addr(ct, dptr, &dptr, &addr, limit)) {
730 pr_debug(
"ip: %s parse failed.!\n", dptr);
746 static const struct sip_header ct_sdp_hdrs_v4[] = {
747 [SDP_HDR_VERSION] = SDP_HDR(
"v=",
NULL, digits_len),
748 [SDP_HDR_OWNER] = SDP_HDR(
"o=",
"IN IP4 ", sdp_addr_len),
749 [SDP_HDR_CONNECTION] = SDP_HDR(
"c=",
"IN IP4 ", sdp_addr_len),
750 [SDP_HDR_MEDIA] = SDP_HDR(
"m=",
NULL, media_len),
753 static const struct sip_header ct_sdp_hdrs_v6[] = {
754 [SDP_HDR_VERSION] = SDP_HDR(
"v=",
NULL, digits_len),
755 [SDP_HDR_OWNER] = SDP_HDR(
"o=",
"IN IP6 ", sdp_addr_len),
756 [SDP_HDR_CONNECTION] = SDP_HDR(
"c=",
"IN IP6 ", sdp_addr_len),
757 [SDP_HDR_MEDIA] = SDP_HDR(
"m=",
NULL, media_len),
761 static const char *ct_sdp_header_search(
const char *dptr,
const char *limit,
762 const char *needle,
unsigned int len)
764 for (limit -= len; dptr <
limit; dptr++) {
765 if (*dptr ==
'\r' || *dptr ==
'\n')
767 if (
strncmp(dptr, needle, len) == 0)
778 unsigned int dataoff,
unsigned int datalen,
779 enum sdp_header_types type,
780 enum sdp_header_types term,
781 unsigned int *matchoff,
unsigned int *matchlen)
783 const struct sip_header *hdrs, *
hdr, *
thdr;
784 const char *start = dptr, *limit = dptr +
datalen;
787 hdrs = nf_ct_l3num(ct) ==
NFPROTO_IPV4 ? ct_sdp_hdrs_v4 : ct_sdp_hdrs_v6;
791 for (dptr += dataoff; dptr <
limit; dptr++) {
793 if (*dptr !=
'\r' && *dptr !=
'\n')
797 if (*(dptr - 1) ==
'\r' && *dptr ==
'\n') {
802 if (term != SDP_HDR_UNSPEC &&
803 limit - dptr >= thdr->len &&
804 strnicmp(dptr, thdr->name, thdr->len) == 0)
806 else if (limit - dptr >= hdr->len &&
807 strnicmp(dptr, hdr->name, hdr->len) == 0)
812 *matchoff = dptr -
start;
814 dptr = ct_sdp_header_search(dptr, limit, hdr->search,
821 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
824 *matchoff = dptr - start + shift;
831 static int ct_sip_parse_sdp_addr(
const struct nf_conn *ct,
const char *dptr,
832 unsigned int dataoff,
unsigned int datalen,
833 enum sdp_header_types type,
834 enum sdp_header_types term,
835 unsigned int *matchoff,
unsigned int *matchlen,
845 if (!sdp_parse_addr(ct, dptr + *matchoff,
NULL, addr,
846 dptr + *matchoff + *matchlen))
851 static int refresh_signalling_expectation(
struct nf_conn *ct,
854 unsigned int expires)
861 spin_lock_bh(&nf_conntrack_lock);
863 if (exp->
class != SIP_EXPECT_SIGNALLING ||
864 !nf_inet_addr_cmp(&exp->
tuple.dst.u3, addr) ||
865 exp->
tuple.dst.protonum != proto ||
866 exp->
tuple.dst.u.udp.port != port)
876 spin_unlock_bh(&nf_conntrack_lock);
880 static void flush_expectations(
struct nf_conn *ct,
bool media)
886 spin_lock_bh(&nf_conntrack_lock);
888 if ((exp->
class != SIP_EXPECT_SIGNALLING) ^ media)
892 nf_ct_unlink_expect(exp);
897 spin_unlock_bh(&nf_conntrack_lock);
900 static int set_expected_rtp_rtcp(
struct sk_buff *skb,
unsigned int protoff,
901 unsigned int dataoff,
902 const char **dptr,
unsigned int *datalen,
904 enum sip_expectation_classes
class,
905 unsigned int mediaoff,
unsigned int medialen)
909 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
910 struct net *
net = nf_ct_net(ct);
914 int direct_rtp = 0, skip_expect = 0, ret =
NF_DROP;
916 __be16 rtp_port, rtcp_port;
921 if (sip_direct_media) {
922 if (!nf_inet_addr_cmp(daddr, &ct->
tuplehash[dir].tuple.src.u3))
940 memset(&tuple, 0,
sizeof(tuple));
942 tuple.src.u3 = *
saddr;
943 tuple.src.l3num = nf_ct_l3num(ct);
945 tuple.dst.u3 = *
daddr;
946 tuple.dst.u.udp.port =
port;
952 if (!exp || exp->
master == ct ||
953 nfct_help(exp->
master)->helper != nfct_help(ct)->helper ||
956 #ifdef CONFIG_NF_NAT_NEEDED
958 (!nf_inet_addr_cmp(&exp->saved_addr, &exp->
tuple.dst.u3) ||
959 exp->saved_proto.udp.port != exp->
tuple.dst.u.udp.port) &&
961 *daddr = exp->saved_addr;
962 tuple.dst.u3 = exp->saved_addr;
963 tuple.dst.u.udp.port = exp->saved_proto.udp.port;
968 }
while (!skip_expect);
971 base_port =
ntohs(tuple.dst.u.udp.port) & ~1;
972 rtp_port =
htons(base_port);
973 rtcp_port =
htons(base_port + 1);
977 if (nf_nat_sdp_port &&
978 !nf_nat_sdp_port(skb, protoff, dataoff, dptr, datalen,
979 mediaoff, medialen,
ntohs(rtp_port)))
993 if (rtcp_exp == NULL)
1000 ret = nf_nat_sdp_media(skb, protoff, dataoff, dptr, datalen,
1002 mediaoff, medialen, daddr);
1004 if (nf_ct_expect_related(rtp_exp) == 0) {
1005 if (nf_ct_expect_related(rtcp_exp) != 0)
1018 static const struct sdp_media_type sdp_media_types[] = {
1019 SDP_MEDIA_TYPE(
"audio ", SIP_EXPECT_AUDIO),
1020 SDP_MEDIA_TYPE(
"video ", SIP_EXPECT_VIDEO),
1021 SDP_MEDIA_TYPE(
"image ", SIP_EXPECT_IMAGE),
1024 static const struct sdp_media_type *sdp_media_type(
const char *dptr,
1025 unsigned int matchoff,
1026 unsigned int matchlen)
1028 const struct sdp_media_type *
t;
1031 for (i = 0; i <
ARRAY_SIZE(sdp_media_types); i++) {
1032 t = &sdp_media_types[
i];
1033 if (matchlen < t->len ||
1034 strncmp(dptr + matchoff, t->name, t->len))
1041 static int process_sdp(
struct sk_buff *skb,
unsigned int protoff,
1042 unsigned int dataoff,
1043 const char **dptr,
unsigned int *datalen,
1047 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1048 unsigned int matchoff, matchlen;
1049 unsigned int mediaoff, medialen;
1050 unsigned int sdpoff;
1051 unsigned int caddr_len, maddr_len;
1055 const struct sdp_media_type *
t;
1064 SDP_HDR_VERSION, SDP_HDR_UNSPEC,
1065 &matchoff, &matchlen) <= 0)
1073 if (ct_sip_parse_sdp_addr(ct, *dptr, sdpoff, *datalen,
1074 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
1075 &matchoff, &matchlen, &caddr) > 0)
1076 caddr_len = matchlen;
1081 SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
1082 &mediaoff, &medialen) <= 0)
1087 t = sdp_media_type(*dptr, mediaoff, medialen);
1089 mediaoff += medialen;
1103 if (ct_sip_parse_sdp_addr(ct, *dptr, mediaoff, *datalen,
1104 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
1105 &matchoff, &matchlen, &maddr) > 0) {
1106 maddr_len = matchlen;
1107 memcpy(&rtp_addr, &maddr,
sizeof(rtp_addr));
1108 }
else if (caddr_len)
1109 memcpy(&rtp_addr, &caddr,
sizeof(rtp_addr));
1113 ret = set_expected_rtp_rtcp(skb, protoff, dataoff,
1115 &rtp_addr,
htons(port), t->class,
1116 mediaoff, medialen);
1117 if (ret != NF_ACCEPT)
1121 if (maddr_len && nf_nat_sdp_addr && ct->status &
IPS_NAT_MASK) {
1122 ret = nf_nat_sdp_addr(skb, protoff, dataoff,
1123 dptr, datalen, mediaoff,
1124 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
1126 if (ret != NF_ACCEPT)
1135 ret = nf_nat_sdp_session(skb, protoff, dataoff,
1136 dptr, datalen, sdpoff, &rtp_addr);
1140 static int process_invite_response(
struct sk_buff *skb,
unsigned int protoff,
1141 unsigned int dataoff,
1142 const char **dptr,
unsigned int *datalen,
1143 unsigned int cseq,
unsigned int code)
1146 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1147 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1149 if ((code >= 100 && code <= 199) ||
1150 (code >= 200 && code <= 299))
1151 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
1152 else if (ct_sip_info->invite_cseq == cseq)
1153 flush_expectations(ct,
true);
1157 static int process_update_response(
struct sk_buff *skb,
unsigned int protoff,
1158 unsigned int dataoff,
1159 const char **dptr,
unsigned int *datalen,
1160 unsigned int cseq,
unsigned int code)
1163 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1164 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1166 if ((code >= 100 && code <= 199) ||
1167 (code >= 200 && code <= 299))
1168 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
1169 else if (ct_sip_info->invite_cseq == cseq)
1170 flush_expectations(ct,
true);
1174 static int process_prack_response(
struct sk_buff *skb,
unsigned int protoff,
1175 unsigned int dataoff,
1176 const char **dptr,
unsigned int *datalen,
1177 unsigned int cseq,
unsigned int code)
1180 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1181 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1183 if ((code >= 100 && code <= 199) ||
1184 (code >= 200 && code <= 299))
1185 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
1186 else if (ct_sip_info->invite_cseq == cseq)
1187 flush_expectations(ct,
true);
1191 static int process_invite_request(
struct sk_buff *skb,
unsigned int protoff,
1192 unsigned int dataoff,
1193 const char **dptr,
unsigned int *datalen,
1197 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1198 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1201 flush_expectations(ct,
true);
1202 ret = process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
1203 if (ret == NF_ACCEPT)
1204 ct_sip_info->invite_cseq = cseq;
1208 static int process_bye_request(
struct sk_buff *skb,
unsigned int protoff,
1209 unsigned int dataoff,
1210 const char **dptr,
unsigned int *datalen,
1214 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1216 flush_expectations(ct,
true);
1224 static int process_register_request(
struct sk_buff *skb,
unsigned int protoff,
1225 unsigned int dataoff,
1226 const char **dptr,
unsigned int *datalen,
1230 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1231 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1233 unsigned int matchoff, matchlen;
1238 unsigned int expires = 0;
1255 &matchoff, &matchlen) > 0)
1259 SIP_HDR_CONTACT, NULL,
1260 &matchoff, &matchlen, &daddr, &port);
1267 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.
src.u3, &daddr))
1270 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen, *datalen,
1275 matchoff + matchlen, *datalen,
1276 "expires=", NULL, NULL, &expires) < 0)
1289 if (sip_direct_signalling)
1290 saddr = &ct->
tuplehash[!dir].tuple.src.u3;
1293 saddr, &daddr, proto, NULL, &port);
1294 exp->
timeout.expires = sip_timeout *
HZ;
1295 exp->
helper = nfct_help(ct)->helper;
1300 ret = nf_nat_sip_expect(skb, protoff, dataoff, dptr, datalen,
1301 exp, matchoff, matchlen);
1303 if (nf_ct_expect_related(exp) != 0)
1311 if (ret == NF_ACCEPT)
1312 ct_sip_info->register_cseq = cseq;
1316 static int process_register_response(
struct sk_buff *skb,
unsigned int protoff,
1317 unsigned int dataoff,
1318 const char **dptr,
unsigned int *datalen,
1319 unsigned int cseq,
unsigned int code)
1322 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1323 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1328 unsigned int matchoff, matchlen, coff = 0;
1329 unsigned int expires = 0;
1330 int in_contact = 0,
ret;
1340 if (ct_sip_info->register_cseq != cseq)
1343 if (code >= 100 && code <= 199)
1349 &matchoff, &matchlen) > 0)
1353 unsigned int c_expires = expires;
1356 SIP_HDR_CONTACT, &in_contact,
1357 &matchoff, &matchlen,
1365 if (!nf_inet_addr_cmp(&ct->
tuplehash[dir].tuple.dst.u3, &addr))
1368 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen,
1369 *datalen, &proto) == 0)
1373 matchoff + matchlen,
1374 *datalen,
"expires=",
1375 NULL, NULL, &c_expires);
1380 if (refresh_signalling_expectation(ct, &addr, proto, port,
1386 flush_expectations(ct,
false);
1390 static const struct sip_handler sip_handlers[] = {
1391 SIP_HANDLER(
"INVITE", process_invite_request, process_invite_response),
1392 SIP_HANDLER(
"UPDATE", process_sdp, process_update_response),
1393 SIP_HANDLER(
"ACK", process_sdp, NULL),
1394 SIP_HANDLER(
"PRACK", process_sdp, process_prack_response),
1395 SIP_HANDLER(
"BYE", process_bye_request, NULL),
1396 SIP_HANDLER(
"REGISTER", process_register_request, process_register_response),
1399 static int process_sip_response(
struct sk_buff *skb,
unsigned int protoff,
1400 unsigned int dataoff,
1401 const char **dptr,
unsigned int *datalen)
1404 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1405 unsigned int matchoff, matchlen, matchend;
1406 unsigned int code, cseq,
i;
1408 if (*datalen <
strlen(
"SIP/2.0 200"))
1415 &matchoff, &matchlen) <= 0)
1420 matchend = matchoff + matchlen + 1;
1422 for (i = 0; i <
ARRAY_SIZE(sip_handlers); i++) {
1423 const struct sip_handler *
handler;
1425 handler = &sip_handlers[
i];
1426 if (handler->response == NULL)
1428 if (*datalen < matchend + handler->len ||
1429 strnicmp(*dptr + matchend, handler->method, handler->len))
1431 return handler->response(skb, protoff, dataoff, dptr, datalen,
1437 static int process_sip_request(
struct sk_buff *skb,
unsigned int protoff,
1438 unsigned int dataoff,
1439 const char **dptr,
unsigned int *datalen)
1442 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1443 unsigned int matchoff, matchlen;
1444 unsigned int cseq,
i;
1446 for (i = 0; i <
ARRAY_SIZE(sip_handlers); i++) {
1447 const struct sip_handler *handler;
1449 handler = &sip_handlers[
i];
1450 if (handler->request == NULL)
1452 if (*datalen < handler->len ||
1453 strnicmp(*dptr, handler->method, handler->len))
1457 &matchoff, &matchlen) <= 0)
1463 return handler->request(skb, protoff, dataoff, dptr, datalen,
1469 static int process_sip_msg(
struct sk_buff *skb,
struct nf_conn *ct,
1470 unsigned int protoff,
unsigned int dataoff,
1471 const char **dptr,
unsigned int *datalen)
1477 ret = process_sip_request(skb, protoff, dataoff, dptr, datalen);
1479 ret = process_sip_response(skb, protoff, dataoff, dptr, datalen);
1483 if (nf_nat_sip && !nf_nat_sip(skb, protoff, dataoff,
1491 static int sip_help_tcp(
struct sk_buff *skb,
unsigned int protoff,
1495 unsigned int dataoff,
datalen;
1496 unsigned int matchoff, matchlen,
clen;
1497 unsigned int msglen, origlen;
1498 const char *dptr, *
end;
1499 s16 diff, tdiff = 0;
1509 th = skb_header_pointer(skb, protoff,
sizeof(_tcph), &_tcph);
1512 dataoff = protoff + th->doff * 4;
1513 if (dataoff >= skb->len)
1516 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1521 dptr = skb->
data + dataoff;
1522 datalen = skb->len - dataoff;
1523 if (datalen <
strlen("SIP/2.0 200"))
1528 SIP_HDR_CONTENT_LENGTH,
1529 &matchoff, &matchlen) <= 0)
1533 if (dptr + matchoff == end)
1538 if (end[0] ==
'\r' && end[1] ==
'\n' &&
1539 end[2] ==
'\r' && end[3] ==
'\n') {
1548 msglen = origlen = end - dptr;
1549 if (msglen > datalen)
1552 ret = process_sip_msg(skb, ct, protoff, dataoff,
1554 if (ret != NF_ACCEPT)
1556 diff = msglen - origlen;
1561 datalen = datalen + diff -
msglen;
1564 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
1566 if (nf_nat_sip_seq_adjust)
1567 nf_nat_sip_seq_adjust(skb, protoff, tdiff);
1573 static int sip_help_udp(
struct sk_buff *skb,
unsigned int protoff,
1576 unsigned int dataoff,
datalen;
1580 dataoff = protoff +
sizeof(
struct udphdr);
1581 if (dataoff >= skb->
len)
1584 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1589 dptr = skb->
data + dataoff;
1590 datalen = skb->
len - dataoff;
1591 if (datalen <
strlen(
"SIP/2.0 200"))
1594 return process_sip_msg(skb, ct, protoff, dataoff, &dptr, &datalen);
1600 [SIP_EXPECT_SIGNALLING] = {
1601 .name =
"signalling",
1605 [SIP_EXPECT_AUDIO] = {
1610 [SIP_EXPECT_VIDEO] = {
1615 [SIP_EXPECT_IMAGE] = {
1622 static void nf_conntrack_sip_fini(
void)
1626 for (i = 0; i < ports_c; i++) {
1628 if (sip[i][j].me == NULL)
1635 static int __init nf_conntrack_sip_init(
void)
1640 ports[ports_c++] = SIP_PORT;
1642 for (i = 0; i < ports_c; i++) {
1643 memset(&sip[i], 0,
sizeof(sip[i]));
1645 sip[
i][0].tuple.src.l3num =
AF_INET;
1647 sip[
i][0].help = sip_help_udp;
1648 sip[
i][1].tuple.src.l3num =
AF_INET;
1650 sip[
i][1].help = sip_help_tcp;
1654 sip[
i][2].help = sip_help_udp;
1657 sip[
i][3].help = sip_help_tcp;
1660 sip[
i][
j].data_len =
sizeof(
struct nf_ct_sip_master);
1661 sip[
i][
j].tuple.src.u.udp.port =
htons(ports[i]);
1662 sip[
i][
j].expect_policy = sip_exp_policy;
1663 sip[
i][
j].expect_class_max = SIP_EXPECT_MAX;
1666 if (ports[i] == SIP_PORT)
1667 sprintf(sip[i][j].name,
"sip");
1669 sprintf(sip[i][j].name,
"sip-%u", i);
1671 pr_debug(
"port #%u: %u\n", i, ports[i]);
1676 " helper for pf: %u port: %u\n",
1677 sip[i][j].tuple.src.l3num, ports[i]);
1678 nf_conntrack_sip_fini();