1 /* SIP extension for IP connection tracking.
3 * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar>
4 * based on RR's ip_conntrack_ftp.c and other modules.
5 * (C) 2007 United Security Providers
6 * (C) 2007, 2008 Patrick McHardy <kaber@trash.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/ctype.h>
17 #include <linux/skbuff.h>
18 #include <linux/inet.h>
20 #include <linux/udp.h>
21 #include <linux/tcp.h>
22 #include <linux/netfilter.h>
24 #include <net/route.h>
25 #include <net/ip6_route.h>
26 #include <net/netfilter/nf_conntrack.h>
27 #include <net/netfilter/nf_conntrack_core.h>
28 #include <net/netfilter/nf_conntrack_expect.h>
29 #include <net/netfilter/nf_conntrack_helper.h>
30 #include <net/netfilter/nf_conntrack_zones.h>
31 #include <linux/netfilter/nf_conntrack_sip.h>
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
35 MODULE_DESCRIPTION("SIP connection tracking helper");
36 MODULE_ALIAS("ip_conntrack_sip");
37 MODULE_ALIAS_NFCT_HELPER("sip");
40 static unsigned short ports
[MAX_PORTS
];
41 static unsigned int ports_c
;
42 module_param_array(ports
, ushort
, &ports_c
, 0400);
43 MODULE_PARM_DESC(ports
, "port numbers of SIP servers");
45 static unsigned int sip_timeout __read_mostly
= SIP_TIMEOUT
;
46 module_param(sip_timeout
, uint
, 0600);
47 MODULE_PARM_DESC(sip_timeout
, "timeout for the master SIP session");
49 static int sip_direct_signalling __read_mostly
= 1;
50 module_param(sip_direct_signalling
, int, 0600);
51 MODULE_PARM_DESC(sip_direct_signalling
, "expect incoming calls from registrar "
54 static int sip_direct_media __read_mostly
= 1;
55 module_param(sip_direct_media
, int, 0600);
56 MODULE_PARM_DESC(sip_direct_media
, "Expect Media streams between signalling "
57 "endpoints only (default 1)");
59 static int sip_external_media __read_mostly
= 0;
60 module_param(sip_external_media
, int, 0600);
61 MODULE_PARM_DESC(sip_external_media
, "Expect Media streams between external "
62 "endpoints (default 0)");
64 const struct nf_nat_sip_hooks
*nf_nat_sip_hooks
;
65 EXPORT_SYMBOL_GPL(nf_nat_sip_hooks
);
67 static int string_len(const struct nf_conn
*ct
, const char *dptr
,
68 const char *limit
, int *shift
)
72 while (dptr
< limit
&& isalpha(*dptr
)) {
79 static int digits_len(const struct nf_conn
*ct
, const char *dptr
,
80 const char *limit
, int *shift
)
83 while (dptr
< limit
&& isdigit(*dptr
)) {
90 static int iswordc(const char c
)
92 if (isalnum(c
) || c
== '!' || c
== '"' || c
== '%' ||
93 (c
>= '(' && c
<= '+') || c
== ':' || c
== '<' || c
== '>' ||
94 c
== '?' || (c
>= '[' && c
<= ']') || c
== '_' || c
== '`' ||
95 c
== '{' || c
== '}' || c
== '~' || (c
>= '-' && c
<= '/') ||
101 static int word_len(const char *dptr
, const char *limit
)
104 while (dptr
< limit
&& iswordc(*dptr
)) {
111 static int callid_len(const struct nf_conn
*ct
, const char *dptr
,
112 const char *limit
, int *shift
)
116 len
= word_len(dptr
, limit
);
118 if (!len
|| dptr
== limit
|| *dptr
!= '@')
123 domain_len
= word_len(dptr
, limit
);
126 return len
+ domain_len
;
129 /* get media type + port length */
130 static int media_len(const struct nf_conn
*ct
, const char *dptr
,
131 const char *limit
, int *shift
)
133 int len
= string_len(ct
, dptr
, limit
, shift
);
136 if (dptr
>= limit
|| *dptr
!= ' ')
141 return len
+ digits_len(ct
, dptr
, limit
, shift
);
144 static int sip_parse_addr(const struct nf_conn
*ct
, const char *cp
,
145 const char **endp
, union nf_inet_addr
*addr
,
146 const char *limit
, bool delim
)
154 memset(addr
, 0, sizeof(*addr
));
155 switch (nf_ct_l3num(ct
)) {
157 ret
= in4_pton(cp
, limit
- cp
, (u8
*)&addr
->ip
, -1, &end
);
162 if (cp
< limit
&& *cp
== '[')
167 ret
= in6_pton(cp
, limit
- cp
, (u8
*)&addr
->ip6
, -1, &end
);
171 if (end
< limit
&& *end
== ']')
185 /* skip ip address. returns its length. */
186 static int epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
187 const char *limit
, int *shift
)
189 union nf_inet_addr addr
;
190 const char *aux
= dptr
;
192 if (!sip_parse_addr(ct
, dptr
, &dptr
, &addr
, limit
, true)) {
193 pr_debug("ip: %s parse failed.!\n", dptr
);
200 dptr
+= digits_len(ct
, dptr
, limit
, shift
);
205 /* get address length, skiping user info. */
206 static int skp_epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
207 const char *limit
, int *shift
)
209 const char *start
= dptr
;
212 /* Search for @, but stop at the end of the line.
213 * We are inside a sip: URI, so we don't need to worry about
214 * continuation lines. */
215 while (dptr
< limit
&&
216 *dptr
!= '@' && *dptr
!= '\r' && *dptr
!= '\n') {
221 if (dptr
< limit
&& *dptr
== '@') {
229 return epaddr_len(ct
, dptr
, limit
, shift
);
232 /* Parse a SIP request line of the form:
234 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
236 * and return the offset and length of the address contained in the Request-URI.
238 int ct_sip_parse_request(const struct nf_conn
*ct
,
239 const char *dptr
, unsigned int datalen
,
240 unsigned int *matchoff
, unsigned int *matchlen
,
241 union nf_inet_addr
*addr
, __be16
*port
)
243 const char *start
= dptr
, *limit
= dptr
+ datalen
, *end
;
248 /* Skip method and following whitespace */
249 mlen
= string_len(ct
, dptr
, limit
, NULL
);
257 for (; dptr
< limit
- strlen("sip:"); dptr
++) {
258 if (*dptr
== '\r' || *dptr
== '\n')
260 if (strncasecmp(dptr
, "sip:", strlen("sip:")) == 0) {
261 dptr
+= strlen("sip:");
265 if (!skp_epaddr_len(ct
, dptr
, limit
, &shift
))
269 if (!sip_parse_addr(ct
, dptr
, &end
, addr
, limit
, true))
271 if (end
< limit
&& *end
== ':') {
273 p
= simple_strtoul(end
, (char **)&end
, 10);
274 if (p
< 1024 || p
> 65535)
278 *port
= htons(SIP_PORT
);
282 *matchoff
= dptr
- start
;
283 *matchlen
= end
- dptr
;
286 EXPORT_SYMBOL_GPL(ct_sip_parse_request
);
288 /* SIP header parsing: SIP headers are located at the beginning of a line, but
289 * may span several lines, in which case the continuation lines begin with a
290 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
291 * CRLF, RFC 3261 allows only CRLF, we support both.
293 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
294 * whitespace and the values. Whitespace in this context means any amount of
295 * tabs, spaces and continuation lines, which are treated as a single whitespace
298 * Some headers may appear multiple times. A comma separated list of values is
299 * equivalent to multiple headers.
301 static const struct sip_header ct_sip_hdrs
[] = {
302 [SIP_HDR_CSEQ
] = SIP_HDR("CSeq", NULL
, NULL
, digits_len
),
303 [SIP_HDR_FROM
] = SIP_HDR("From", "f", "sip:", skp_epaddr_len
),
304 [SIP_HDR_TO
] = SIP_HDR("To", "t", "sip:", skp_epaddr_len
),
305 [SIP_HDR_CONTACT
] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len
),
306 [SIP_HDR_VIA_UDP
] = SIP_HDR("Via", "v", "UDP ", epaddr_len
),
307 [SIP_HDR_VIA_TCP
] = SIP_HDR("Via", "v", "TCP ", epaddr_len
),
308 [SIP_HDR_EXPIRES
] = SIP_HDR("Expires", NULL
, NULL
, digits_len
),
309 [SIP_HDR_CONTENT_LENGTH
] = SIP_HDR("Content-Length", "l", NULL
, digits_len
),
310 [SIP_HDR_CALL_ID
] = SIP_HDR("Call-Id", "i", NULL
, callid_len
),
313 static const char *sip_follow_continuation(const char *dptr
, const char *limit
)
315 /* Walk past newline */
319 /* Skip '\n' in CR LF */
320 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
325 /* Continuation line? */
326 if (*dptr
!= ' ' && *dptr
!= '\t')
329 /* skip leading whitespace */
330 for (; dptr
< limit
; dptr
++) {
331 if (*dptr
!= ' ' && *dptr
!= '\t')
337 static const char *sip_skip_whitespace(const char *dptr
, const char *limit
)
339 for (; dptr
< limit
; dptr
++) {
340 if (*dptr
== ' ' || *dptr
== '\t')
342 if (*dptr
!= '\r' && *dptr
!= '\n')
344 dptr
= sip_follow_continuation(dptr
, limit
);
350 /* Search within a SIP header value, dealing with continuation lines */
351 static const char *ct_sip_header_search(const char *dptr
, const char *limit
,
352 const char *needle
, unsigned int len
)
354 for (limit
-= len
; dptr
< limit
; dptr
++) {
355 if (*dptr
== '\r' || *dptr
== '\n') {
356 dptr
= sip_follow_continuation(dptr
, limit
);
362 if (strncasecmp(dptr
, needle
, len
) == 0)
368 int ct_sip_get_header(const struct nf_conn
*ct
, const char *dptr
,
369 unsigned int dataoff
, unsigned int datalen
,
370 enum sip_header_types type
,
371 unsigned int *matchoff
, unsigned int *matchlen
)
373 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
374 const char *start
= dptr
, *limit
= dptr
+ datalen
;
377 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
378 /* Find beginning of line */
379 if (*dptr
!= '\r' && *dptr
!= '\n')
383 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
388 /* Skip continuation lines */
389 if (*dptr
== ' ' || *dptr
== '\t')
392 /* Find header. Compact headers must be followed by a
393 * non-alphabetic character to avoid mismatches. */
394 if (limit
- dptr
>= hdr
->len
&&
395 strncasecmp(dptr
, hdr
->name
, hdr
->len
) == 0)
397 else if (hdr
->cname
&& limit
- dptr
>= hdr
->clen
+ 1 &&
398 strncasecmp(dptr
, hdr
->cname
, hdr
->clen
) == 0 &&
399 !isalpha(*(dptr
+ hdr
->clen
)))
404 /* Find and skip colon */
405 dptr
= sip_skip_whitespace(dptr
, limit
);
408 if (*dptr
!= ':' || ++dptr
>= limit
)
411 /* Skip whitespace after colon */
412 dptr
= sip_skip_whitespace(dptr
, limit
);
416 *matchoff
= dptr
- start
;
418 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
,
425 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
428 *matchoff
= dptr
- start
+ shift
;
433 EXPORT_SYMBOL_GPL(ct_sip_get_header
);
435 /* Get next header field in a list of comma separated values */
436 static int ct_sip_next_header(const struct nf_conn
*ct
, const char *dptr
,
437 unsigned int dataoff
, unsigned int datalen
,
438 enum sip_header_types type
,
439 unsigned int *matchoff
, unsigned int *matchlen
)
441 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
442 const char *start
= dptr
, *limit
= dptr
+ datalen
;
447 dptr
= ct_sip_header_search(dptr
, limit
, ",", strlen(","));
451 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
, hdr
->slen
);
456 *matchoff
= dptr
- start
;
457 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
464 /* Walk through headers until a parsable one is found or no header of the
465 * given type is left. */
466 static int ct_sip_walk_headers(const struct nf_conn
*ct
, const char *dptr
,
467 unsigned int dataoff
, unsigned int datalen
,
468 enum sip_header_types type
, int *in_header
,
469 unsigned int *matchoff
, unsigned int *matchlen
)
473 if (in_header
&& *in_header
) {
475 ret
= ct_sip_next_header(ct
, dptr
, dataoff
, datalen
,
476 type
, matchoff
, matchlen
);
481 dataoff
+= *matchoff
;
487 ret
= ct_sip_get_header(ct
, dptr
, dataoff
, datalen
,
488 type
, matchoff
, matchlen
);
493 dataoff
+= *matchoff
;
501 /* Locate a SIP header, parse the URI and return the offset and length of
502 * the address as well as the address and port themselves. A stream of
503 * headers can be parsed by handing in a non-NULL datalen and in_header
506 int ct_sip_parse_header_uri(const struct nf_conn
*ct
, const char *dptr
,
507 unsigned int *dataoff
, unsigned int datalen
,
508 enum sip_header_types type
, int *in_header
,
509 unsigned int *matchoff
, unsigned int *matchlen
,
510 union nf_inet_addr
*addr
, __be16
*port
)
512 const char *c
, *limit
= dptr
+ datalen
;
516 ret
= ct_sip_walk_headers(ct
, dptr
, dataoff
? *dataoff
: 0, datalen
,
517 type
, in_header
, matchoff
, matchlen
);
522 if (!sip_parse_addr(ct
, dptr
+ *matchoff
, &c
, addr
, limit
, true))
526 p
= simple_strtoul(c
, (char **)&c
, 10);
527 if (p
< 1024 || p
> 65535)
531 *port
= htons(SIP_PORT
);
537 EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri
);
539 static int ct_sip_parse_param(const struct nf_conn
*ct
, const char *dptr
,
540 unsigned int dataoff
, unsigned int datalen
,
542 unsigned int *matchoff
, unsigned int *matchlen
)
544 const char *limit
= dptr
+ datalen
;
548 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
550 limit
= dptr
+ datalen
;
552 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
555 start
+= strlen(name
);
557 end
= ct_sip_header_search(start
, limit
, ";", strlen(";"));
561 *matchoff
= start
- dptr
;
562 *matchlen
= end
- start
;
566 /* Parse address from header parameter and return address, offset and length */
567 int ct_sip_parse_address_param(const struct nf_conn
*ct
, const char *dptr
,
568 unsigned int dataoff
, unsigned int datalen
,
570 unsigned int *matchoff
, unsigned int *matchlen
,
571 union nf_inet_addr
*addr
, bool delim
)
573 const char *limit
= dptr
+ datalen
;
574 const char *start
, *end
;
576 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
578 limit
= dptr
+ datalen
;
580 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
584 start
+= strlen(name
);
585 if (!sip_parse_addr(ct
, start
, &end
, addr
, limit
, delim
))
587 *matchoff
= start
- dptr
;
588 *matchlen
= end
- start
;
591 EXPORT_SYMBOL_GPL(ct_sip_parse_address_param
);
593 /* Parse numerical header parameter and return value, offset and length */
594 int ct_sip_parse_numerical_param(const struct nf_conn
*ct
, const char *dptr
,
595 unsigned int dataoff
, unsigned int datalen
,
597 unsigned int *matchoff
, unsigned int *matchlen
,
600 const char *limit
= dptr
+ datalen
;
604 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
606 limit
= dptr
+ datalen
;
608 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
612 start
+= strlen(name
);
613 *val
= simple_strtoul(start
, &end
, 0);
616 if (matchoff
&& matchlen
) {
617 *matchoff
= start
- dptr
;
618 *matchlen
= end
- start
;
622 EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param
);
624 static int ct_sip_parse_transport(struct nf_conn
*ct
, const char *dptr
,
625 unsigned int dataoff
, unsigned int datalen
,
628 unsigned int matchoff
, matchlen
;
630 if (ct_sip_parse_param(ct
, dptr
, dataoff
, datalen
, "transport=",
631 &matchoff
, &matchlen
)) {
632 if (!strncasecmp(dptr
+ matchoff
, "TCP", strlen("TCP")))
633 *proto
= IPPROTO_TCP
;
634 else if (!strncasecmp(dptr
+ matchoff
, "UDP", strlen("UDP")))
635 *proto
= IPPROTO_UDP
;
639 if (*proto
!= nf_ct_protonum(ct
))
642 *proto
= nf_ct_protonum(ct
);
647 static int sdp_parse_addr(const struct nf_conn
*ct
, const char *cp
,
648 const char **endp
, union nf_inet_addr
*addr
,
654 memset(addr
, 0, sizeof(*addr
));
655 switch (nf_ct_l3num(ct
)) {
657 ret
= in4_pton(cp
, limit
- cp
, (u8
*)&addr
->ip
, -1, &end
);
660 ret
= in6_pton(cp
, limit
- cp
, (u8
*)&addr
->ip6
, -1, &end
);
673 /* skip ip address. returns its length. */
674 static int sdp_addr_len(const struct nf_conn
*ct
, const char *dptr
,
675 const char *limit
, int *shift
)
677 union nf_inet_addr addr
;
678 const char *aux
= dptr
;
680 if (!sdp_parse_addr(ct
, dptr
, &dptr
, &addr
, limit
)) {
681 pr_debug("ip: %s parse failed.!\n", dptr
);
688 /* SDP header parsing: a SDP session description contains an ordered set of
689 * headers, starting with a section containing general session parameters,
690 * optionally followed by multiple media descriptions.
692 * SDP headers always start at the beginning of a line. According to RFC 2327:
693 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
694 * be tolerant and also accept records terminated with a single newline
695 * character". We handle both cases.
697 static const struct sip_header ct_sdp_hdrs_v4
[] = {
698 [SDP_HDR_VERSION
] = SDP_HDR("v=", NULL
, digits_len
),
699 [SDP_HDR_OWNER
] = SDP_HDR("o=", "IN IP4 ", sdp_addr_len
),
700 [SDP_HDR_CONNECTION
] = SDP_HDR("c=", "IN IP4 ", sdp_addr_len
),
701 [SDP_HDR_MEDIA
] = SDP_HDR("m=", NULL
, media_len
),
704 static const struct sip_header ct_sdp_hdrs_v6
[] = {
705 [SDP_HDR_VERSION
] = SDP_HDR("v=", NULL
, digits_len
),
706 [SDP_HDR_OWNER
] = SDP_HDR("o=", "IN IP6 ", sdp_addr_len
),
707 [SDP_HDR_CONNECTION
] = SDP_HDR("c=", "IN IP6 ", sdp_addr_len
),
708 [SDP_HDR_MEDIA
] = SDP_HDR("m=", NULL
, media_len
),
711 /* Linear string search within SDP header values */
712 static const char *ct_sdp_header_search(const char *dptr
, const char *limit
,
713 const char *needle
, unsigned int len
)
715 for (limit
-= len
; dptr
< limit
; dptr
++) {
716 if (*dptr
== '\r' || *dptr
== '\n')
718 if (strncmp(dptr
, needle
, len
) == 0)
724 /* Locate a SDP header (optionally a substring within the header value),
725 * optionally stopping at the first occurrence of the term header, parse
726 * it and return the offset and length of the data we're interested in.
728 int ct_sip_get_sdp_header(const struct nf_conn
*ct
, const char *dptr
,
729 unsigned int dataoff
, unsigned int datalen
,
730 enum sdp_header_types type
,
731 enum sdp_header_types term
,
732 unsigned int *matchoff
, unsigned int *matchlen
)
734 const struct sip_header
*hdrs
, *hdr
, *thdr
;
735 const char *start
= dptr
, *limit
= dptr
+ datalen
;
738 hdrs
= nf_ct_l3num(ct
) == NFPROTO_IPV4
? ct_sdp_hdrs_v4
: ct_sdp_hdrs_v6
;
742 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
743 /* Find beginning of line */
744 if (*dptr
!= '\r' && *dptr
!= '\n')
748 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
753 if (term
!= SDP_HDR_UNSPEC
&&
754 limit
- dptr
>= thdr
->len
&&
755 strncasecmp(dptr
, thdr
->name
, thdr
->len
) == 0)
757 else if (limit
- dptr
>= hdr
->len
&&
758 strncasecmp(dptr
, hdr
->name
, hdr
->len
) == 0)
763 *matchoff
= dptr
- start
;
765 dptr
= ct_sdp_header_search(dptr
, limit
, hdr
->search
,
772 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
775 *matchoff
= dptr
- start
+ shift
;
780 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header
);
782 static int ct_sip_parse_sdp_addr(const struct nf_conn
*ct
, const char *dptr
,
783 unsigned int dataoff
, unsigned int datalen
,
784 enum sdp_header_types type
,
785 enum sdp_header_types term
,
786 unsigned int *matchoff
, unsigned int *matchlen
,
787 union nf_inet_addr
*addr
)
791 ret
= ct_sip_get_sdp_header(ct
, dptr
, dataoff
, datalen
, type
, term
,
796 if (!sdp_parse_addr(ct
, dptr
+ *matchoff
, NULL
, addr
,
797 dptr
+ *matchoff
+ *matchlen
))
802 static int refresh_signalling_expectation(struct nf_conn
*ct
,
803 union nf_inet_addr
*addr
,
804 u8 proto
, __be16 port
,
805 unsigned int expires
)
807 struct nf_conn_help
*help
= nfct_help(ct
);
808 struct nf_conntrack_expect
*exp
;
809 struct hlist_node
*next
;
812 spin_lock_bh(&nf_conntrack_expect_lock
);
813 hlist_for_each_entry_safe(exp
, next
, &help
->expectations
, lnode
) {
814 if (exp
->class != SIP_EXPECT_SIGNALLING
||
815 !nf_inet_addr_cmp(&exp
->tuple
.dst
.u3
, addr
) ||
816 exp
->tuple
.dst
.protonum
!= proto
||
817 exp
->tuple
.dst
.u
.udp
.port
!= port
)
819 if (mod_timer_pending(&exp
->timeout
, jiffies
+ expires
* HZ
)) {
820 exp
->flags
&= ~NF_CT_EXPECT_INACTIVE
;
825 spin_unlock_bh(&nf_conntrack_expect_lock
);
829 static void flush_expectations(struct nf_conn
*ct
, bool media
)
831 struct nf_conn_help
*help
= nfct_help(ct
);
832 struct nf_conntrack_expect
*exp
;
833 struct hlist_node
*next
;
835 spin_lock_bh(&nf_conntrack_expect_lock
);
836 hlist_for_each_entry_safe(exp
, next
, &help
->expectations
, lnode
) {
837 if ((exp
->class != SIP_EXPECT_SIGNALLING
) ^ media
)
839 if (!nf_ct_remove_expect(exp
))
844 spin_unlock_bh(&nf_conntrack_expect_lock
);
847 static int set_expected_rtp_rtcp(struct sk_buff
*skb
, unsigned int protoff
,
848 unsigned int dataoff
,
849 const char **dptr
, unsigned int *datalen
,
850 union nf_inet_addr
*daddr
, __be16 port
,
851 enum sip_expectation_classes
class,
852 unsigned int mediaoff
, unsigned int medialen
)
854 struct nf_conntrack_expect
*exp
, *rtp_exp
, *rtcp_exp
;
855 enum ip_conntrack_info ctinfo
;
856 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
857 struct net
*net
= nf_ct_net(ct
);
858 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
859 union nf_inet_addr
*saddr
;
860 struct nf_conntrack_tuple tuple
;
861 int direct_rtp
= 0, skip_expect
= 0, ret
= NF_DROP
;
863 __be16 rtp_port
, rtcp_port
;
864 const struct nf_nat_sip_hooks
*hooks
;
867 if (sip_direct_media
) {
868 if (!nf_inet_addr_cmp(daddr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
))
870 saddr
= &ct
->tuplehash
[!dir
].tuple
.src
.u3
;
871 } else if (sip_external_media
) {
872 struct net_device
*dev
= skb_dst(skb
)->dev
;
873 struct net
*net
= dev_net(dev
);
875 struct flowi4 fl4
= {};
876 #if IS_ENABLED(CONFIG_IPV6)
877 struct flowi6 fl6
= {};
879 struct dst_entry
*dst
= NULL
;
881 switch (nf_ct_l3num(ct
)) {
883 fl4
.daddr
= daddr
->ip
;
884 rt
= ip_route_output_key(net
, &fl4
);
889 #if IS_ENABLED(CONFIG_IPV6)
891 fl6
.daddr
= daddr
->in6
;
892 dst
= ip6_route_output(net
, NULL
, &fl6
);
901 /* Don't predict any conntracks when media endpoint is reachable
902 * through the same interface as the signalling peer.
904 if (dst
&& dst
->dev
== dev
)
908 /* We need to check whether the registration exists before attempting
909 * to register it since we can see the same media description multiple
910 * times on different connections in case multiple endpoints receive
913 * RTP optimization: if we find a matching media channel expectation
914 * and both the expectation and this connection are SNATed, we assume
915 * both sides can reach each other directly and use the final
916 * destination address from the expectation. We still need to keep
917 * the NATed expectations for media that might arrive from the
918 * outside, and additionally need to expect the direct RTP stream
919 * in case it passes through us even without NAT.
921 memset(&tuple
, 0, sizeof(tuple
));
923 tuple
.src
.u3
= *saddr
;
924 tuple
.src
.l3num
= nf_ct_l3num(ct
);
925 tuple
.dst
.protonum
= IPPROTO_UDP
;
926 tuple
.dst
.u3
= *daddr
;
927 tuple
.dst
.u
.udp
.port
= port
;
930 exp
= __nf_ct_expect_find(net
, nf_ct_zone(ct
), &tuple
);
932 if (!exp
|| exp
->master
== ct
||
933 nfct_help(exp
->master
)->helper
!= nfct_help(ct
)->helper
||
936 #ifdef CONFIG_NF_NAT_NEEDED
938 (!nf_inet_addr_cmp(&exp
->saved_addr
, &exp
->tuple
.dst
.u3
) ||
939 exp
->saved_proto
.udp
.port
!= exp
->tuple
.dst
.u
.udp
.port
) &&
940 ct
->status
& IPS_NAT_MASK
) {
941 *daddr
= exp
->saved_addr
;
942 tuple
.dst
.u3
= exp
->saved_addr
;
943 tuple
.dst
.u
.udp
.port
= exp
->saved_proto
.udp
.port
;
948 } while (!skip_expect
);
950 base_port
= ntohs(tuple
.dst
.u
.udp
.port
) & ~1;
951 rtp_port
= htons(base_port
);
952 rtcp_port
= htons(base_port
+ 1);
955 hooks
= rcu_dereference(nf_nat_sip_hooks
);
957 !hooks
->sdp_port(skb
, protoff
, dataoff
, dptr
, datalen
,
958 mediaoff
, medialen
, ntohs(rtp_port
)))
965 rtp_exp
= nf_ct_expect_alloc(ct
);
968 nf_ct_expect_init(rtp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
969 IPPROTO_UDP
, NULL
, &rtp_port
);
971 rtcp_exp
= nf_ct_expect_alloc(ct
);
972 if (rtcp_exp
== NULL
)
974 nf_ct_expect_init(rtcp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
975 IPPROTO_UDP
, NULL
, &rtcp_port
);
977 hooks
= rcu_dereference(nf_nat_sip_hooks
);
978 if (hooks
&& ct
->status
& IPS_NAT_MASK
&& !direct_rtp
)
979 ret
= hooks
->sdp_media(skb
, protoff
, dataoff
, dptr
,
980 datalen
, rtp_exp
, rtcp_exp
,
981 mediaoff
, medialen
, daddr
);
983 /* -EALREADY handling works around end-points that send
984 * SDP messages with identical port but different media type,
985 * we pretend expectation was set up.
987 int errp
= nf_ct_expect_related(rtp_exp
);
989 if (errp
== 0 || errp
== -EALREADY
) {
990 int errcp
= nf_ct_expect_related(rtcp_exp
);
992 if (errcp
== 0 || errcp
== -EALREADY
)
995 nf_ct_unexpect_related(rtp_exp
);
998 nf_ct_expect_put(rtcp_exp
);
1000 nf_ct_expect_put(rtp_exp
);
1005 static const struct sdp_media_type sdp_media_types
[] = {
1006 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO
),
1007 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO
),
1008 SDP_MEDIA_TYPE("image ", SIP_EXPECT_IMAGE
),
1011 static const struct sdp_media_type
*sdp_media_type(const char *dptr
,
1012 unsigned int matchoff
,
1013 unsigned int matchlen
)
1015 const struct sdp_media_type
*t
;
1018 for (i
= 0; i
< ARRAY_SIZE(sdp_media_types
); i
++) {
1019 t
= &sdp_media_types
[i
];
1020 if (matchlen
< t
->len
||
1021 strncmp(dptr
+ matchoff
, t
->name
, t
->len
))
1028 static int process_sdp(struct sk_buff
*skb
, unsigned int protoff
,
1029 unsigned int dataoff
,
1030 const char **dptr
, unsigned int *datalen
,
1033 enum ip_conntrack_info ctinfo
;
1034 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1035 unsigned int matchoff
, matchlen
;
1036 unsigned int mediaoff
, medialen
;
1037 unsigned int sdpoff
;
1038 unsigned int caddr_len
, maddr_len
;
1040 union nf_inet_addr caddr
, maddr
, rtp_addr
;
1041 const struct nf_nat_sip_hooks
*hooks
;
1043 const struct sdp_media_type
*t
;
1044 int ret
= NF_ACCEPT
;
1046 hooks
= rcu_dereference(nf_nat_sip_hooks
);
1048 /* Find beginning of session description */
1049 if (ct_sip_get_sdp_header(ct
, *dptr
, 0, *datalen
,
1050 SDP_HDR_VERSION
, SDP_HDR_UNSPEC
,
1051 &matchoff
, &matchlen
) <= 0)
1055 /* The connection information is contained in the session description
1056 * and/or once per media description. The first media description marks
1057 * the end of the session description. */
1059 if (ct_sip_parse_sdp_addr(ct
, *dptr
, sdpoff
, *datalen
,
1060 SDP_HDR_CONNECTION
, SDP_HDR_MEDIA
,
1061 &matchoff
, &matchlen
, &caddr
) > 0)
1062 caddr_len
= matchlen
;
1065 for (i
= 0; i
< ARRAY_SIZE(sdp_media_types
); ) {
1066 if (ct_sip_get_sdp_header(ct
, *dptr
, mediaoff
, *datalen
,
1067 SDP_HDR_MEDIA
, SDP_HDR_UNSPEC
,
1068 &mediaoff
, &medialen
) <= 0)
1071 /* Get media type and port number. A media port value of zero
1072 * indicates an inactive stream. */
1073 t
= sdp_media_type(*dptr
, mediaoff
, medialen
);
1075 mediaoff
+= medialen
;
1081 port
= simple_strtoul(*dptr
+ mediaoff
, NULL
, 10);
1084 if (port
< 1024 || port
> 65535) {
1085 nf_ct_helper_log(skb
, ct
, "wrong port %u", port
);
1089 /* The media description overrides the session description. */
1091 if (ct_sip_parse_sdp_addr(ct
, *dptr
, mediaoff
, *datalen
,
1092 SDP_HDR_CONNECTION
, SDP_HDR_MEDIA
,
1093 &matchoff
, &matchlen
, &maddr
) > 0) {
1094 maddr_len
= matchlen
;
1095 memcpy(&rtp_addr
, &maddr
, sizeof(rtp_addr
));
1096 } else if (caddr_len
)
1097 memcpy(&rtp_addr
, &caddr
, sizeof(rtp_addr
));
1099 nf_ct_helper_log(skb
, ct
, "cannot parse SDP message");
1103 ret
= set_expected_rtp_rtcp(skb
, protoff
, dataoff
,
1105 &rtp_addr
, htons(port
), t
->class,
1106 mediaoff
, medialen
);
1107 if (ret
!= NF_ACCEPT
) {
1108 nf_ct_helper_log(skb
, ct
,
1109 "cannot add expectation for voice");
1113 /* Update media connection address if present */
1114 if (maddr_len
&& hooks
&& ct
->status
& IPS_NAT_MASK
) {
1115 ret
= hooks
->sdp_addr(skb
, protoff
, dataoff
,
1116 dptr
, datalen
, mediaoff
,
1120 if (ret
!= NF_ACCEPT
) {
1121 nf_ct_helper_log(skb
, ct
, "cannot mangle SDP");
1128 /* Update session connection and owner addresses */
1129 hooks
= rcu_dereference(nf_nat_sip_hooks
);
1130 if (hooks
&& ct
->status
& IPS_NAT_MASK
)
1131 ret
= hooks
->sdp_session(skb
, protoff
, dataoff
,
1132 dptr
, datalen
, sdpoff
,
1137 static int process_invite_response(struct sk_buff
*skb
, unsigned int protoff
,
1138 unsigned int dataoff
,
1139 const char **dptr
, unsigned int *datalen
,
1140 unsigned int cseq
, unsigned int code
)
1142 enum ip_conntrack_info ctinfo
;
1143 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1144 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1146 if ((code
>= 100 && code
<= 199) ||
1147 (code
>= 200 && code
<= 299))
1148 return process_sdp(skb
, protoff
, dataoff
, dptr
, datalen
, cseq
);
1149 else if (ct_sip_info
->invite_cseq
== cseq
)
1150 flush_expectations(ct
, true);
1154 static int process_update_response(struct sk_buff
*skb
, unsigned int protoff
,
1155 unsigned int dataoff
,
1156 const char **dptr
, unsigned int *datalen
,
1157 unsigned int cseq
, unsigned int code
)
1159 enum ip_conntrack_info ctinfo
;
1160 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1161 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1163 if ((code
>= 100 && code
<= 199) ||
1164 (code
>= 200 && code
<= 299))
1165 return process_sdp(skb
, protoff
, dataoff
, dptr
, datalen
, cseq
);
1166 else if (ct_sip_info
->invite_cseq
== cseq
)
1167 flush_expectations(ct
, true);
1171 static int process_prack_response(struct sk_buff
*skb
, unsigned int protoff
,
1172 unsigned int dataoff
,
1173 const char **dptr
, unsigned int *datalen
,
1174 unsigned int cseq
, unsigned int code
)
1176 enum ip_conntrack_info ctinfo
;
1177 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1178 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1180 if ((code
>= 100 && code
<= 199) ||
1181 (code
>= 200 && code
<= 299))
1182 return process_sdp(skb
, protoff
, dataoff
, dptr
, datalen
, cseq
);
1183 else if (ct_sip_info
->invite_cseq
== cseq
)
1184 flush_expectations(ct
, true);
1188 static int process_invite_request(struct sk_buff
*skb
, unsigned int protoff
,
1189 unsigned int dataoff
,
1190 const char **dptr
, unsigned int *datalen
,
1193 enum ip_conntrack_info ctinfo
;
1194 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1195 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1198 flush_expectations(ct
, true);
1199 ret
= process_sdp(skb
, protoff
, dataoff
, dptr
, datalen
, cseq
);
1200 if (ret
== NF_ACCEPT
)
1201 ct_sip_info
->invite_cseq
= cseq
;
1205 static int process_bye_request(struct sk_buff
*skb
, unsigned int protoff
,
1206 unsigned int dataoff
,
1207 const char **dptr
, unsigned int *datalen
,
1210 enum ip_conntrack_info ctinfo
;
1211 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1213 flush_expectations(ct
, true);
1217 /* Parse a REGISTER request and create a permanent expectation for incoming
1218 * signalling connections. The expectation is marked inactive and is activated
1219 * when receiving a response indicating success from the registrar.
1221 static int process_register_request(struct sk_buff
*skb
, unsigned int protoff
,
1222 unsigned int dataoff
,
1223 const char **dptr
, unsigned int *datalen
,
1226 enum ip_conntrack_info ctinfo
;
1227 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1228 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1229 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
1230 unsigned int matchoff
, matchlen
;
1231 struct nf_conntrack_expect
*exp
;
1232 union nf_inet_addr
*saddr
, daddr
;
1233 const struct nf_nat_sip_hooks
*hooks
;
1236 unsigned int expires
= 0;
1239 /* Expected connections can not register again. */
1240 if (ct
->status
& IPS_EXPECTED
)
1243 /* We must check the expiration time: a value of zero signals the
1244 * registrar to release the binding. We'll remove our expectation
1245 * when receiving the new bindings in the response, but we don't
1246 * want to create new ones.
1248 * The expiration time may be contained in Expires: header, the
1249 * Contact: header parameters or the URI parameters.
1251 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_EXPIRES
,
1252 &matchoff
, &matchlen
) > 0)
1253 expires
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1255 ret
= ct_sip_parse_header_uri(ct
, *dptr
, NULL
, *datalen
,
1256 SIP_HDR_CONTACT
, NULL
,
1257 &matchoff
, &matchlen
, &daddr
, &port
);
1259 nf_ct_helper_log(skb
, ct
, "cannot parse contact");
1261 } else if (ret
== 0)
1264 /* We don't support third-party registrations */
1265 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
, &daddr
))
1268 if (ct_sip_parse_transport(ct
, *dptr
, matchoff
+ matchlen
, *datalen
,
1272 if (ct_sip_parse_numerical_param(ct
, *dptr
,
1273 matchoff
+ matchlen
, *datalen
,
1274 "expires=", NULL
, NULL
, &expires
) < 0) {
1275 nf_ct_helper_log(skb
, ct
, "cannot parse expires");
1284 exp
= nf_ct_expect_alloc(ct
);
1286 nf_ct_helper_log(skb
, ct
, "cannot alloc expectation");
1291 if (sip_direct_signalling
)
1292 saddr
= &ct
->tuplehash
[!dir
].tuple
.src
.u3
;
1294 nf_ct_expect_init(exp
, SIP_EXPECT_SIGNALLING
, nf_ct_l3num(ct
),
1295 saddr
, &daddr
, proto
, NULL
, &port
);
1296 exp
->timeout
.expires
= sip_timeout
* HZ
;
1297 exp
->helper
= nfct_help(ct
)->helper
;
1298 exp
->flags
= NF_CT_EXPECT_PERMANENT
| NF_CT_EXPECT_INACTIVE
;
1300 hooks
= rcu_dereference(nf_nat_sip_hooks
);
1301 if (hooks
&& ct
->status
& IPS_NAT_MASK
)
1302 ret
= hooks
->expect(skb
, protoff
, dataoff
, dptr
, datalen
,
1303 exp
, matchoff
, matchlen
);
1305 if (nf_ct_expect_related(exp
) != 0) {
1306 nf_ct_helper_log(skb
, ct
, "cannot add expectation");
1311 nf_ct_expect_put(exp
);
1314 if (ret
== NF_ACCEPT
)
1315 ct_sip_info
->register_cseq
= cseq
;
1319 static int process_register_response(struct sk_buff
*skb
, unsigned int protoff
,
1320 unsigned int dataoff
,
1321 const char **dptr
, unsigned int *datalen
,
1322 unsigned int cseq
, unsigned int code
)
1324 enum ip_conntrack_info ctinfo
;
1325 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1326 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1327 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
1328 union nf_inet_addr addr
;
1331 unsigned int matchoff
, matchlen
, coff
= 0;
1332 unsigned int expires
= 0;
1333 int in_contact
= 0, ret
;
1335 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1336 * they have received a final response from the registrar for the
1337 * previous one or the previous REGISTER request has timed out".
1339 * However, some servers fail to detect retransmissions and send late
1340 * responses, so we store the sequence number of the last valid
1341 * request and compare it here.
1343 if (ct_sip_info
->register_cseq
!= cseq
)
1346 if (code
>= 100 && code
<= 199)
1348 if (code
< 200 || code
> 299)
1351 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_EXPIRES
,
1352 &matchoff
, &matchlen
) > 0)
1353 expires
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1356 unsigned int c_expires
= expires
;
1358 ret
= ct_sip_parse_header_uri(ct
, *dptr
, &coff
, *datalen
,
1359 SIP_HDR_CONTACT
, &in_contact
,
1360 &matchoff
, &matchlen
,
1363 nf_ct_helper_log(skb
, ct
, "cannot parse contact");
1365 } else if (ret
== 0)
1368 /* We don't support third-party registrations */
1369 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.dst
.u3
, &addr
))
1372 if (ct_sip_parse_transport(ct
, *dptr
, matchoff
+ matchlen
,
1373 *datalen
, &proto
) == 0)
1376 ret
= ct_sip_parse_numerical_param(ct
, *dptr
,
1377 matchoff
+ matchlen
,
1378 *datalen
, "expires=",
1379 NULL
, NULL
, &c_expires
);
1381 nf_ct_helper_log(skb
, ct
, "cannot parse expires");
1386 if (refresh_signalling_expectation(ct
, &addr
, proto
, port
,
1392 flush_expectations(ct
, false);
1396 static const struct sip_handler sip_handlers
[] = {
1397 SIP_HANDLER("INVITE", process_invite_request
, process_invite_response
),
1398 SIP_HANDLER("UPDATE", process_sdp
, process_update_response
),
1399 SIP_HANDLER("ACK", process_sdp
, NULL
),
1400 SIP_HANDLER("PRACK", process_sdp
, process_prack_response
),
1401 SIP_HANDLER("BYE", process_bye_request
, NULL
),
1402 SIP_HANDLER("REGISTER", process_register_request
, process_register_response
),
1405 static int process_sip_response(struct sk_buff
*skb
, unsigned int protoff
,
1406 unsigned int dataoff
,
1407 const char **dptr
, unsigned int *datalen
)
1409 enum ip_conntrack_info ctinfo
;
1410 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1411 unsigned int matchoff
, matchlen
, matchend
;
1412 unsigned int code
, cseq
, i
;
1414 if (*datalen
< strlen("SIP/2.0 200"))
1416 code
= simple_strtoul(*dptr
+ strlen("SIP/2.0 "), NULL
, 10);
1418 nf_ct_helper_log(skb
, ct
, "cannot get code");
1422 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_CSEQ
,
1423 &matchoff
, &matchlen
) <= 0) {
1424 nf_ct_helper_log(skb
, ct
, "cannot parse cseq");
1427 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1428 if (!cseq
&& *(*dptr
+ matchoff
) != '0') {
1429 nf_ct_helper_log(skb
, ct
, "cannot get cseq");
1432 matchend
= matchoff
+ matchlen
+ 1;
1434 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1435 const struct sip_handler
*handler
;
1437 handler
= &sip_handlers
[i
];
1438 if (handler
->response
== NULL
)
1440 if (*datalen
< matchend
+ handler
->len
||
1441 strncasecmp(*dptr
+ matchend
, handler
->method
, handler
->len
))
1443 return handler
->response(skb
, protoff
, dataoff
, dptr
, datalen
,
1449 static int process_sip_request(struct sk_buff
*skb
, unsigned int protoff
,
1450 unsigned int dataoff
,
1451 const char **dptr
, unsigned int *datalen
)
1453 enum ip_conntrack_info ctinfo
;
1454 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1455 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1456 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
1457 unsigned int matchoff
, matchlen
;
1458 unsigned int cseq
, i
;
1459 union nf_inet_addr addr
;
1462 /* Many Cisco IP phones use a high source port for SIP requests, but
1463 * listen for the response on port 5060. If we are the local
1464 * router for one of these phones, save the port number from the
1465 * Via: header so that nf_nat_sip can redirect the responses to
1468 if (ct_sip_parse_header_uri(ct
, *dptr
, NULL
, *datalen
,
1469 SIP_HDR_VIA_UDP
, NULL
, &matchoff
,
1470 &matchlen
, &addr
, &port
) > 0 &&
1471 port
!= ct
->tuplehash
[dir
].tuple
.src
.u
.udp
.port
&&
1472 nf_inet_addr_cmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
))
1473 ct_sip_info
->forced_dport
= port
;
1475 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1476 const struct sip_handler
*handler
;
1478 handler
= &sip_handlers
[i
];
1479 if (handler
->request
== NULL
)
1481 if (*datalen
< handler
->len
+ 2 ||
1482 strncasecmp(*dptr
, handler
->method
, handler
->len
))
1484 if ((*dptr
)[handler
->len
] != ' ' ||
1485 !isalpha((*dptr
)[handler
->len
+1]))
1488 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_CSEQ
,
1489 &matchoff
, &matchlen
) <= 0) {
1490 nf_ct_helper_log(skb
, ct
, "cannot parse cseq");
1493 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1494 if (!cseq
&& *(*dptr
+ matchoff
) != '0') {
1495 nf_ct_helper_log(skb
, ct
, "cannot get cseq");
1499 return handler
->request(skb
, protoff
, dataoff
, dptr
, datalen
,
1505 static int process_sip_msg(struct sk_buff
*skb
, struct nf_conn
*ct
,
1506 unsigned int protoff
, unsigned int dataoff
,
1507 const char **dptr
, unsigned int *datalen
)
1509 const struct nf_nat_sip_hooks
*hooks
;
1512 if (strncasecmp(*dptr
, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1513 ret
= process_sip_request(skb
, protoff
, dataoff
, dptr
, datalen
);
1515 ret
= process_sip_response(skb
, protoff
, dataoff
, dptr
, datalen
);
1517 if (ret
== NF_ACCEPT
&& ct
->status
& IPS_NAT_MASK
) {
1518 hooks
= rcu_dereference(nf_nat_sip_hooks
);
1519 if (hooks
&& !hooks
->msg(skb
, protoff
, dataoff
,
1521 nf_ct_helper_log(skb
, ct
, "cannot NAT SIP message");
1529 static int sip_help_tcp(struct sk_buff
*skb
, unsigned int protoff
,
1530 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1532 struct tcphdr
*th
, _tcph
;
1533 unsigned int dataoff
, datalen
;
1534 unsigned int matchoff
, matchlen
, clen
;
1535 unsigned int msglen
, origlen
;
1536 const char *dptr
, *end
;
1537 s16 diff
, tdiff
= 0;
1538 int ret
= NF_ACCEPT
;
1541 if (ctinfo
!= IP_CT_ESTABLISHED
&&
1542 ctinfo
!= IP_CT_ESTABLISHED_REPLY
)
1546 th
= skb_header_pointer(skb
, protoff
, sizeof(_tcph
), &_tcph
);
1549 dataoff
= protoff
+ th
->doff
* 4;
1550 if (dataoff
>= skb
->len
)
1553 nf_ct_refresh(ct
, skb
, sip_timeout
* HZ
);
1555 if (unlikely(skb_linearize(skb
)))
1558 dptr
= skb
->data
+ dataoff
;
1559 datalen
= skb
->len
- dataoff
;
1560 if (datalen
< strlen("SIP/2.0 200"))
1564 if (ct_sip_get_header(ct
, dptr
, 0, datalen
,
1565 SIP_HDR_CONTENT_LENGTH
,
1566 &matchoff
, &matchlen
) <= 0)
1569 clen
= simple_strtoul(dptr
+ matchoff
, (char **)&end
, 10);
1570 if (dptr
+ matchoff
== end
)
1574 for (; end
+ strlen("\r\n\r\n") <= dptr
+ datalen
; end
++) {
1575 if (end
[0] == '\r' && end
[1] == '\n' &&
1576 end
[2] == '\r' && end
[3] == '\n') {
1583 end
+= strlen("\r\n\r\n") + clen
;
1585 msglen
= origlen
= end
- dptr
;
1586 if (msglen
> datalen
)
1589 ret
= process_sip_msg(skb
, ct
, protoff
, dataoff
,
1591 /* process_sip_* functions report why this packet is dropped */
1592 if (ret
!= NF_ACCEPT
)
1594 diff
= msglen
- origlen
;
1599 datalen
= datalen
+ diff
- msglen
;
1602 if (ret
== NF_ACCEPT
&& ct
->status
& IPS_NAT_MASK
) {
1603 const struct nf_nat_sip_hooks
*hooks
;
1605 hooks
= rcu_dereference(nf_nat_sip_hooks
);
1607 hooks
->seq_adjust(skb
, protoff
, tdiff
);
1613 static int sip_help_udp(struct sk_buff
*skb
, unsigned int protoff
,
1614 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1616 unsigned int dataoff
, datalen
;
1620 dataoff
= protoff
+ sizeof(struct udphdr
);
1621 if (dataoff
>= skb
->len
)
1624 nf_ct_refresh(ct
, skb
, sip_timeout
* HZ
);
1626 if (unlikely(skb_linearize(skb
)))
1629 dptr
= skb
->data
+ dataoff
;
1630 datalen
= skb
->len
- dataoff
;
1631 if (datalen
< strlen("SIP/2.0 200"))
1634 return process_sip_msg(skb
, ct
, protoff
, dataoff
, &dptr
, &datalen
);
1637 static struct nf_conntrack_helper sip
[MAX_PORTS
* 4] __read_mostly
;
1639 static const struct nf_conntrack_expect_policy sip_exp_policy
[SIP_EXPECT_MAX
+ 1] = {
1640 [SIP_EXPECT_SIGNALLING
] = {
1641 .name
= "signalling",
1645 [SIP_EXPECT_AUDIO
] = {
1647 .max_expected
= 2 * IP_CT_DIR_MAX
,
1650 [SIP_EXPECT_VIDEO
] = {
1652 .max_expected
= 2 * IP_CT_DIR_MAX
,
1655 [SIP_EXPECT_IMAGE
] = {
1657 .max_expected
= IP_CT_DIR_MAX
,
1662 static void __exit
nf_conntrack_sip_fini(void)
1664 nf_conntrack_helpers_unregister(sip
, ports_c
* 4);
1667 static int __init
nf_conntrack_sip_init(void)
1671 NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_sip_master
));
1674 ports
[ports_c
++] = SIP_PORT
;
1676 for (i
= 0; i
< ports_c
; i
++) {
1677 nf_ct_helper_init(&sip
[4 * i
], AF_INET
, IPPROTO_UDP
, "sip",
1678 SIP_PORT
, ports
[i
], i
, sip_exp_policy
,
1679 SIP_EXPECT_MAX
, sip_help_udp
,
1681 nf_ct_helper_init(&sip
[4 * i
+ 1], AF_INET
, IPPROTO_TCP
, "sip",
1682 SIP_PORT
, ports
[i
], i
, sip_exp_policy
,
1683 SIP_EXPECT_MAX
, sip_help_tcp
,
1685 nf_ct_helper_init(&sip
[4 * i
+ 2], AF_INET6
, IPPROTO_UDP
, "sip",
1686 SIP_PORT
, ports
[i
], i
, sip_exp_policy
,
1687 SIP_EXPECT_MAX
, sip_help_udp
,
1689 nf_ct_helper_init(&sip
[4 * i
+ 3], AF_INET6
, IPPROTO_TCP
, "sip",
1690 SIP_PORT
, ports
[i
], i
, sip_exp_policy
,
1691 SIP_EXPECT_MAX
, sip_help_tcp
,
1695 ret
= nf_conntrack_helpers_register(sip
, ports_c
* 4);
1697 pr_err("failed to register helpers\n");
1703 module_init(nf_conntrack_sip_init
);
1704 module_exit(nf_conntrack_sip_fini
);