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 #include <linux/module.h>
14 #include <linux/ctype.h>
15 #include <linux/skbuff.h>
16 #include <linux/inet.h>
18 #include <linux/udp.h>
19 #include <linux/tcp.h>
20 #include <linux/netfilter.h>
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_conntrack_core.h>
24 #include <net/netfilter/nf_conntrack_expect.h>
25 #include <net/netfilter/nf_conntrack_helper.h>
26 #include <net/netfilter/nf_conntrack_zones.h>
27 #include <linux/netfilter/nf_conntrack_sip.h>
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
31 MODULE_DESCRIPTION("SIP connection tracking helper");
32 MODULE_ALIAS("ip_conntrack_sip");
33 MODULE_ALIAS_NFCT_HELPER("sip");
36 static unsigned short ports
[MAX_PORTS
];
37 static unsigned int ports_c
;
38 module_param_array(ports
, ushort
, &ports_c
, 0400);
39 MODULE_PARM_DESC(ports
, "port numbers of SIP servers");
41 static unsigned int sip_timeout __read_mostly
= SIP_TIMEOUT
;
42 module_param(sip_timeout
, uint
, 0600);
43 MODULE_PARM_DESC(sip_timeout
, "timeout for the master SIP session");
45 static int sip_direct_signalling __read_mostly
= 1;
46 module_param(sip_direct_signalling
, int, 0600);
47 MODULE_PARM_DESC(sip_direct_signalling
, "expect incoming calls from registrar "
50 static int sip_direct_media __read_mostly
= 1;
51 module_param(sip_direct_media
, int, 0600);
52 MODULE_PARM_DESC(sip_direct_media
, "Expect Media streams between signalling "
53 "endpoints only (default 1)");
55 unsigned int (*nf_nat_sip_hook
)(struct sk_buff
*skb
, unsigned int protoff
,
56 unsigned int dataoff
, const char **dptr
,
57 unsigned int *datalen
) __read_mostly
;
58 EXPORT_SYMBOL_GPL(nf_nat_sip_hook
);
60 void (*nf_nat_sip_seq_adjust_hook
)(struct sk_buff
*skb
, s16 off
) __read_mostly
;
61 EXPORT_SYMBOL_GPL(nf_nat_sip_seq_adjust_hook
);
63 unsigned int (*nf_nat_sip_expect_hook
)(struct sk_buff
*skb
,
67 unsigned int *datalen
,
68 struct nf_conntrack_expect
*exp
,
69 unsigned int matchoff
,
70 unsigned int matchlen
) __read_mostly
;
71 EXPORT_SYMBOL_GPL(nf_nat_sip_expect_hook
);
73 unsigned int (*nf_nat_sdp_addr_hook
)(struct sk_buff
*skb
, unsigned int protoff
,
76 unsigned int *datalen
,
78 enum sdp_header_types type
,
79 enum sdp_header_types term
,
80 const union nf_inet_addr
*addr
)
82 EXPORT_SYMBOL_GPL(nf_nat_sdp_addr_hook
);
84 unsigned int (*nf_nat_sdp_port_hook
)(struct sk_buff
*skb
, unsigned int protoff
,
87 unsigned int *datalen
,
88 unsigned int matchoff
,
89 unsigned int matchlen
,
90 u_int16_t port
) __read_mostly
;
91 EXPORT_SYMBOL_GPL(nf_nat_sdp_port_hook
);
93 unsigned int (*nf_nat_sdp_session_hook
)(struct sk_buff
*skb
,
97 unsigned int *datalen
,
99 const union nf_inet_addr
*addr
)
101 EXPORT_SYMBOL_GPL(nf_nat_sdp_session_hook
);
103 unsigned int (*nf_nat_sdp_media_hook
)(struct sk_buff
*skb
, unsigned int protoff
,
104 unsigned int dataoff
,
106 unsigned int *datalen
,
107 struct nf_conntrack_expect
*rtp_exp
,
108 struct nf_conntrack_expect
*rtcp_exp
,
109 unsigned int mediaoff
,
110 unsigned int medialen
,
111 union nf_inet_addr
*rtp_addr
)
113 EXPORT_SYMBOL_GPL(nf_nat_sdp_media_hook
);
115 static int string_len(const struct nf_conn
*ct
, const char *dptr
,
116 const char *limit
, int *shift
)
120 while (dptr
< limit
&& isalpha(*dptr
)) {
127 static int digits_len(const struct nf_conn
*ct
, const char *dptr
,
128 const char *limit
, int *shift
)
131 while (dptr
< limit
&& isdigit(*dptr
)) {
138 static int iswordc(const char c
)
140 if (isalnum(c
) || c
== '!' || c
== '"' || c
== '%' ||
141 (c
>= '(' && c
<= '/') || c
== ':' || c
== '<' || c
== '>' ||
142 c
== '?' || (c
>= '[' && c
<= ']') || c
== '_' || c
== '`' ||
143 c
== '{' || c
== '}' || c
== '~')
148 static int word_len(const char *dptr
, const char *limit
)
151 while (dptr
< limit
&& iswordc(*dptr
)) {
158 static int callid_len(const struct nf_conn
*ct
, const char *dptr
,
159 const char *limit
, int *shift
)
163 len
= word_len(dptr
, limit
);
165 if (!len
|| dptr
== limit
|| *dptr
!= '@')
170 domain_len
= word_len(dptr
, limit
);
173 return len
+ domain_len
;
176 /* get media type + port length */
177 static int media_len(const struct nf_conn
*ct
, const char *dptr
,
178 const char *limit
, int *shift
)
180 int len
= string_len(ct
, dptr
, limit
, shift
);
183 if (dptr
>= limit
|| *dptr
!= ' ')
188 return len
+ digits_len(ct
, dptr
, limit
, shift
);
191 static int sip_parse_addr(const struct nf_conn
*ct
, const char *cp
,
192 const char **endp
, union nf_inet_addr
*addr
,
193 const char *limit
, bool delim
)
201 memset(addr
, 0, sizeof(*addr
));
202 switch (nf_ct_l3num(ct
)) {
204 ret
= in4_pton(cp
, limit
- cp
, (u8
*)&addr
->ip
, -1, &end
);
209 if (cp
< limit
&& *cp
== '[')
214 ret
= in6_pton(cp
, limit
- cp
, (u8
*)&addr
->ip6
, -1, &end
);
218 if (end
< limit
&& *end
== ']')
232 /* skip ip address. returns its length. */
233 static int epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
234 const char *limit
, int *shift
)
236 union nf_inet_addr addr
;
237 const char *aux
= dptr
;
239 if (!sip_parse_addr(ct
, dptr
, &dptr
, &addr
, limit
, true)) {
240 pr_debug("ip: %s parse failed.!\n", dptr
);
247 dptr
+= digits_len(ct
, dptr
, limit
, shift
);
252 /* get address length, skiping user info. */
253 static int skp_epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
254 const char *limit
, int *shift
)
256 const char *start
= dptr
;
259 /* Search for @, but stop at the end of the line.
260 * We are inside a sip: URI, so we don't need to worry about
261 * continuation lines. */
262 while (dptr
< limit
&&
263 *dptr
!= '@' && *dptr
!= '\r' && *dptr
!= '\n') {
268 if (dptr
< limit
&& *dptr
== '@') {
276 return epaddr_len(ct
, dptr
, limit
, shift
);
279 /* Parse a SIP request line of the form:
281 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
283 * and return the offset and length of the address contained in the Request-URI.
285 int ct_sip_parse_request(const struct nf_conn
*ct
,
286 const char *dptr
, unsigned int datalen
,
287 unsigned int *matchoff
, unsigned int *matchlen
,
288 union nf_inet_addr
*addr
, __be16
*port
)
290 const char *start
= dptr
, *limit
= dptr
+ datalen
, *end
;
295 /* Skip method and following whitespace */
296 mlen
= string_len(ct
, dptr
, limit
, NULL
);
304 for (; dptr
< limit
- strlen("sip:"); dptr
++) {
305 if (*dptr
== '\r' || *dptr
== '\n')
307 if (strnicmp(dptr
, "sip:", strlen("sip:")) == 0) {
308 dptr
+= strlen("sip:");
312 if (!skp_epaddr_len(ct
, dptr
, limit
, &shift
))
316 if (!sip_parse_addr(ct
, dptr
, &end
, addr
, limit
, true))
318 if (end
< limit
&& *end
== ':') {
320 p
= simple_strtoul(end
, (char **)&end
, 10);
321 if (p
< 1024 || p
> 65535)
325 *port
= htons(SIP_PORT
);
329 *matchoff
= dptr
- start
;
330 *matchlen
= end
- dptr
;
333 EXPORT_SYMBOL_GPL(ct_sip_parse_request
);
335 /* SIP header parsing: SIP headers are located at the beginning of a line, but
336 * may span several lines, in which case the continuation lines begin with a
337 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
338 * CRLF, RFC 3261 allows only CRLF, we support both.
340 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
341 * whitespace and the values. Whitespace in this context means any amount of
342 * tabs, spaces and continuation lines, which are treated as a single whitespace
345 * Some headers may appear multiple times. A comma separated list of values is
346 * equivalent to multiple headers.
348 static const struct sip_header ct_sip_hdrs
[] = {
349 [SIP_HDR_CSEQ
] = SIP_HDR("CSeq", NULL
, NULL
, digits_len
),
350 [SIP_HDR_FROM
] = SIP_HDR("From", "f", "sip:", skp_epaddr_len
),
351 [SIP_HDR_TO
] = SIP_HDR("To", "t", "sip:", skp_epaddr_len
),
352 [SIP_HDR_CONTACT
] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len
),
353 [SIP_HDR_VIA_UDP
] = SIP_HDR("Via", "v", "UDP ", epaddr_len
),
354 [SIP_HDR_VIA_TCP
] = SIP_HDR("Via", "v", "TCP ", epaddr_len
),
355 [SIP_HDR_EXPIRES
] = SIP_HDR("Expires", NULL
, NULL
, digits_len
),
356 [SIP_HDR_CONTENT_LENGTH
] = SIP_HDR("Content-Length", "l", NULL
, digits_len
),
357 [SIP_HDR_CALL_ID
] = SIP_HDR("Call-Id", "i", NULL
, callid_len
),
360 static const char *sip_follow_continuation(const char *dptr
, const char *limit
)
362 /* Walk past newline */
366 /* Skip '\n' in CR LF */
367 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
372 /* Continuation line? */
373 if (*dptr
!= ' ' && *dptr
!= '\t')
376 /* skip leading whitespace */
377 for (; dptr
< limit
; dptr
++) {
378 if (*dptr
!= ' ' && *dptr
!= '\t')
384 static const char *sip_skip_whitespace(const char *dptr
, const char *limit
)
386 for (; dptr
< limit
; dptr
++) {
389 if (*dptr
!= '\r' && *dptr
!= '\n')
391 dptr
= sip_follow_continuation(dptr
, limit
);
398 /* Search within a SIP header value, dealing with continuation lines */
399 static const char *ct_sip_header_search(const char *dptr
, const char *limit
,
400 const char *needle
, unsigned int len
)
402 for (limit
-= len
; dptr
< limit
; dptr
++) {
403 if (*dptr
== '\r' || *dptr
== '\n') {
404 dptr
= sip_follow_continuation(dptr
, limit
);
410 if (strnicmp(dptr
, needle
, len
) == 0)
416 int ct_sip_get_header(const struct nf_conn
*ct
, const char *dptr
,
417 unsigned int dataoff
, unsigned int datalen
,
418 enum sip_header_types type
,
419 unsigned int *matchoff
, unsigned int *matchlen
)
421 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
422 const char *start
= dptr
, *limit
= dptr
+ datalen
;
425 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
426 /* Find beginning of line */
427 if (*dptr
!= '\r' && *dptr
!= '\n')
431 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
436 /* Skip continuation lines */
437 if (*dptr
== ' ' || *dptr
== '\t')
440 /* Find header. Compact headers must be followed by a
441 * non-alphabetic character to avoid mismatches. */
442 if (limit
- dptr
>= hdr
->len
&&
443 strnicmp(dptr
, hdr
->name
, hdr
->len
) == 0)
445 else if (hdr
->cname
&& limit
- dptr
>= hdr
->clen
+ 1 &&
446 strnicmp(dptr
, hdr
->cname
, hdr
->clen
) == 0 &&
447 !isalpha(*(dptr
+ hdr
->clen
)))
452 /* Find and skip colon */
453 dptr
= sip_skip_whitespace(dptr
, limit
);
456 if (*dptr
!= ':' || ++dptr
>= limit
)
459 /* Skip whitespace after colon */
460 dptr
= sip_skip_whitespace(dptr
, limit
);
464 *matchoff
= dptr
- start
;
466 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
,
473 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
476 *matchoff
= dptr
- start
+ shift
;
481 EXPORT_SYMBOL_GPL(ct_sip_get_header
);
483 /* Get next header field in a list of comma separated values */
484 static int ct_sip_next_header(const struct nf_conn
*ct
, const char *dptr
,
485 unsigned int dataoff
, unsigned int datalen
,
486 enum sip_header_types type
,
487 unsigned int *matchoff
, unsigned int *matchlen
)
489 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
490 const char *start
= dptr
, *limit
= dptr
+ datalen
;
495 dptr
= ct_sip_header_search(dptr
, limit
, ",", strlen(","));
499 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
, hdr
->slen
);
504 *matchoff
= dptr
- start
;
505 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
512 /* Walk through headers until a parsable one is found or no header of the
513 * given type is left. */
514 static int ct_sip_walk_headers(const struct nf_conn
*ct
, const char *dptr
,
515 unsigned int dataoff
, unsigned int datalen
,
516 enum sip_header_types type
, int *in_header
,
517 unsigned int *matchoff
, unsigned int *matchlen
)
521 if (in_header
&& *in_header
) {
523 ret
= ct_sip_next_header(ct
, dptr
, dataoff
, datalen
,
524 type
, matchoff
, matchlen
);
529 dataoff
+= *matchoff
;
535 ret
= ct_sip_get_header(ct
, dptr
, dataoff
, datalen
,
536 type
, matchoff
, matchlen
);
541 dataoff
+= *matchoff
;
549 /* Locate a SIP header, parse the URI and return the offset and length of
550 * the address as well as the address and port themselves. A stream of
551 * headers can be parsed by handing in a non-NULL datalen and in_header
554 int ct_sip_parse_header_uri(const struct nf_conn
*ct
, const char *dptr
,
555 unsigned int *dataoff
, unsigned int datalen
,
556 enum sip_header_types type
, int *in_header
,
557 unsigned int *matchoff
, unsigned int *matchlen
,
558 union nf_inet_addr
*addr
, __be16
*port
)
560 const char *c
, *limit
= dptr
+ datalen
;
564 ret
= ct_sip_walk_headers(ct
, dptr
, dataoff
? *dataoff
: 0, datalen
,
565 type
, in_header
, matchoff
, matchlen
);
570 if (!sip_parse_addr(ct
, dptr
+ *matchoff
, &c
, addr
, limit
, true))
574 p
= simple_strtoul(c
, (char **)&c
, 10);
575 if (p
< 1024 || p
> 65535)
579 *port
= htons(SIP_PORT
);
585 EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri
);
587 static int ct_sip_parse_param(const struct nf_conn
*ct
, const char *dptr
,
588 unsigned int dataoff
, unsigned int datalen
,
590 unsigned int *matchoff
, unsigned int *matchlen
)
592 const char *limit
= dptr
+ datalen
;
596 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
598 limit
= dptr
+ datalen
;
600 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
603 start
+= strlen(name
);
605 end
= ct_sip_header_search(start
, limit
, ";", strlen(";"));
609 *matchoff
= start
- dptr
;
610 *matchlen
= end
- start
;
614 /* Parse address from header parameter and return address, offset and length */
615 int ct_sip_parse_address_param(const struct nf_conn
*ct
, const char *dptr
,
616 unsigned int dataoff
, unsigned int datalen
,
618 unsigned int *matchoff
, unsigned int *matchlen
,
619 union nf_inet_addr
*addr
, bool delim
)
621 const char *limit
= dptr
+ datalen
;
622 const char *start
, *end
;
624 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
626 limit
= dptr
+ datalen
;
628 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
632 start
+= strlen(name
);
633 if (!sip_parse_addr(ct
, start
, &end
, addr
, limit
, delim
))
635 *matchoff
= start
- dptr
;
636 *matchlen
= end
- start
;
639 EXPORT_SYMBOL_GPL(ct_sip_parse_address_param
);
641 /* Parse numerical header parameter and return value, offset and length */
642 int ct_sip_parse_numerical_param(const struct nf_conn
*ct
, const char *dptr
,
643 unsigned int dataoff
, unsigned int datalen
,
645 unsigned int *matchoff
, unsigned int *matchlen
,
648 const char *limit
= dptr
+ datalen
;
652 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
654 limit
= dptr
+ datalen
;
656 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
660 start
+= strlen(name
);
661 *val
= simple_strtoul(start
, &end
, 0);
664 if (matchoff
&& matchlen
) {
665 *matchoff
= start
- dptr
;
666 *matchlen
= end
- start
;
670 EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param
);
672 static int ct_sip_parse_transport(struct nf_conn
*ct
, const char *dptr
,
673 unsigned int dataoff
, unsigned int datalen
,
676 unsigned int matchoff
, matchlen
;
678 if (ct_sip_parse_param(ct
, dptr
, dataoff
, datalen
, "transport=",
679 &matchoff
, &matchlen
)) {
680 if (!strnicmp(dptr
+ matchoff
, "TCP", strlen("TCP")))
681 *proto
= IPPROTO_TCP
;
682 else if (!strnicmp(dptr
+ matchoff
, "UDP", strlen("UDP")))
683 *proto
= IPPROTO_UDP
;
687 if (*proto
!= nf_ct_protonum(ct
))
690 *proto
= nf_ct_protonum(ct
);
695 static int sdp_parse_addr(const struct nf_conn
*ct
, const char *cp
,
696 const char **endp
, union nf_inet_addr
*addr
,
702 memset(addr
, 0, sizeof(*addr
));
703 switch (nf_ct_l3num(ct
)) {
705 ret
= in4_pton(cp
, limit
- cp
, (u8
*)&addr
->ip
, -1, &end
);
708 ret
= in6_pton(cp
, limit
- cp
, (u8
*)&addr
->ip6
, -1, &end
);
721 /* skip ip address. returns its length. */
722 static int sdp_addr_len(const struct nf_conn
*ct
, const char *dptr
,
723 const char *limit
, int *shift
)
725 union nf_inet_addr addr
;
726 const char *aux
= dptr
;
728 if (!sdp_parse_addr(ct
, dptr
, &dptr
, &addr
, limit
)) {
729 pr_debug("ip: %s parse failed.!\n", dptr
);
736 /* SDP header parsing: a SDP session description contains an ordered set of
737 * headers, starting with a section containing general session parameters,
738 * optionally followed by multiple media descriptions.
740 * SDP headers always start at the beginning of a line. According to RFC 2327:
741 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
742 * be tolerant and also accept records terminated with a single newline
743 * character". We handle both cases.
745 static const struct sip_header ct_sdp_hdrs
[] = {
746 [SDP_HDR_VERSION
] = SDP_HDR("v=", NULL
, digits_len
),
747 [SDP_HDR_OWNER_IP4
] = SDP_HDR("o=", "IN IP4 ", sdp_addr_len
),
748 [SDP_HDR_CONNECTION_IP4
] = SDP_HDR("c=", "IN IP4 ", sdp_addr_len
),
749 [SDP_HDR_OWNER_IP6
] = SDP_HDR("o=", "IN IP6 ", sdp_addr_len
),
750 [SDP_HDR_CONNECTION_IP6
] = SDP_HDR("c=", "IN IP6 ", sdp_addr_len
),
751 [SDP_HDR_MEDIA
] = SDP_HDR("m=", NULL
, media_len
),
754 /* Linear string search within SDP header values */
755 static const char *ct_sdp_header_search(const char *dptr
, const char *limit
,
756 const char *needle
, unsigned int len
)
758 for (limit
-= len
; dptr
< limit
; dptr
++) {
759 if (*dptr
== '\r' || *dptr
== '\n')
761 if (strncmp(dptr
, needle
, len
) == 0)
767 /* Locate a SDP header (optionally a substring within the header value),
768 * optionally stopping at the first occurrence of the term header, parse
769 * it and return the offset and length of the data we're interested in.
771 int ct_sip_get_sdp_header(const struct nf_conn
*ct
, const char *dptr
,
772 unsigned int dataoff
, unsigned int datalen
,
773 enum sdp_header_types type
,
774 enum sdp_header_types term
,
775 unsigned int *matchoff
, unsigned int *matchlen
)
777 const struct sip_header
*hdr
= &ct_sdp_hdrs
[type
];
778 const struct sip_header
*thdr
= &ct_sdp_hdrs
[term
];
779 const char *start
= dptr
, *limit
= dptr
+ datalen
;
782 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
783 /* Find beginning of line */
784 if (*dptr
!= '\r' && *dptr
!= '\n')
788 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
793 if (term
!= SDP_HDR_UNSPEC
&&
794 limit
- dptr
>= thdr
->len
&&
795 strnicmp(dptr
, thdr
->name
, thdr
->len
) == 0)
797 else if (limit
- dptr
>= hdr
->len
&&
798 strnicmp(dptr
, hdr
->name
, hdr
->len
) == 0)
803 *matchoff
= dptr
- start
;
805 dptr
= ct_sdp_header_search(dptr
, limit
, hdr
->search
,
812 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
815 *matchoff
= dptr
- start
+ shift
;
820 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header
);
822 static int ct_sip_parse_sdp_addr(const struct nf_conn
*ct
, const char *dptr
,
823 unsigned int dataoff
, unsigned int datalen
,
824 enum sdp_header_types type
,
825 enum sdp_header_types term
,
826 unsigned int *matchoff
, unsigned int *matchlen
,
827 union nf_inet_addr
*addr
)
831 ret
= ct_sip_get_sdp_header(ct
, dptr
, dataoff
, datalen
, type
, term
,
836 if (!sdp_parse_addr(ct
, dptr
+ *matchoff
, NULL
, addr
,
837 dptr
+ *matchoff
+ *matchlen
))
842 static int refresh_signalling_expectation(struct nf_conn
*ct
,
843 union nf_inet_addr
*addr
,
844 u8 proto
, __be16 port
,
845 unsigned int expires
)
847 struct nf_conn_help
*help
= nfct_help(ct
);
848 struct nf_conntrack_expect
*exp
;
849 struct hlist_node
*n
, *next
;
852 spin_lock_bh(&nf_conntrack_lock
);
853 hlist_for_each_entry_safe(exp
, n
, next
, &help
->expectations
, lnode
) {
854 if (exp
->class != SIP_EXPECT_SIGNALLING
||
855 !nf_inet_addr_cmp(&exp
->tuple
.dst
.u3
, addr
) ||
856 exp
->tuple
.dst
.protonum
!= proto
||
857 exp
->tuple
.dst
.u
.udp
.port
!= port
)
859 if (!del_timer(&exp
->timeout
))
861 exp
->flags
&= ~NF_CT_EXPECT_INACTIVE
;
862 exp
->timeout
.expires
= jiffies
+ expires
* HZ
;
863 add_timer(&exp
->timeout
);
867 spin_unlock_bh(&nf_conntrack_lock
);
871 static void flush_expectations(struct nf_conn
*ct
, bool media
)
873 struct nf_conn_help
*help
= nfct_help(ct
);
874 struct nf_conntrack_expect
*exp
;
875 struct hlist_node
*n
, *next
;
877 spin_lock_bh(&nf_conntrack_lock
);
878 hlist_for_each_entry_safe(exp
, n
, next
, &help
->expectations
, lnode
) {
879 if ((exp
->class != SIP_EXPECT_SIGNALLING
) ^ media
)
881 if (!del_timer(&exp
->timeout
))
883 nf_ct_unlink_expect(exp
);
884 nf_ct_expect_put(exp
);
888 spin_unlock_bh(&nf_conntrack_lock
);
891 static int set_expected_rtp_rtcp(struct sk_buff
*skb
, unsigned int protoff
,
892 unsigned int dataoff
,
893 const char **dptr
, unsigned int *datalen
,
894 union nf_inet_addr
*daddr
, __be16 port
,
895 enum sip_expectation_classes
class,
896 unsigned int mediaoff
, unsigned int medialen
)
898 struct nf_conntrack_expect
*exp
, *rtp_exp
, *rtcp_exp
;
899 enum ip_conntrack_info ctinfo
;
900 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
901 struct net
*net
= nf_ct_net(ct
);
902 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
903 union nf_inet_addr
*saddr
;
904 struct nf_conntrack_tuple tuple
;
905 int direct_rtp
= 0, skip_expect
= 0, ret
= NF_DROP
;
907 __be16 rtp_port
, rtcp_port
;
908 typeof(nf_nat_sdp_port_hook
) nf_nat_sdp_port
;
909 typeof(nf_nat_sdp_media_hook
) nf_nat_sdp_media
;
912 if (sip_direct_media
) {
913 if (!nf_inet_addr_cmp(daddr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
))
915 saddr
= &ct
->tuplehash
[!dir
].tuple
.src
.u3
;
918 /* We need to check whether the registration exists before attempting
919 * to register it since we can see the same media description multiple
920 * times on different connections in case multiple endpoints receive
923 * RTP optimization: if we find a matching media channel expectation
924 * and both the expectation and this connection are SNATed, we assume
925 * both sides can reach each other directly and use the final
926 * destination address from the expectation. We still need to keep
927 * the NATed expectations for media that might arrive from the
928 * outside, and additionally need to expect the direct RTP stream
929 * in case it passes through us even without NAT.
931 memset(&tuple
, 0, sizeof(tuple
));
933 tuple
.src
.u3
= *saddr
;
934 tuple
.src
.l3num
= nf_ct_l3num(ct
);
935 tuple
.dst
.protonum
= IPPROTO_UDP
;
936 tuple
.dst
.u3
= *daddr
;
937 tuple
.dst
.u
.udp
.port
= port
;
941 exp
= __nf_ct_expect_find(net
, nf_ct_zone(ct
), &tuple
);
943 if (!exp
|| exp
->master
== ct
||
944 nfct_help(exp
->master
)->helper
!= nfct_help(ct
)->helper
||
947 #ifdef CONFIG_NF_NAT_NEEDED
948 if (exp
->tuple
.src
.l3num
== AF_INET
&& !direct_rtp
&&
949 (exp
->saved_addr
.ip
!= exp
->tuple
.dst
.u3
.ip
||
950 exp
->saved_proto
.udp
.port
!= exp
->tuple
.dst
.u
.udp
.port
) &&
951 ct
->status
& IPS_NAT_MASK
) {
952 daddr
->ip
= exp
->saved_addr
.ip
;
953 tuple
.dst
.u3
.ip
= exp
->saved_addr
.ip
;
954 tuple
.dst
.u
.udp
.port
= exp
->saved_proto
.udp
.port
;
959 } while (!skip_expect
);
962 base_port
= ntohs(tuple
.dst
.u
.udp
.port
) & ~1;
963 rtp_port
= htons(base_port
);
964 rtcp_port
= htons(base_port
+ 1);
967 nf_nat_sdp_port
= rcu_dereference(nf_nat_sdp_port_hook
);
968 if (nf_nat_sdp_port
&&
969 !nf_nat_sdp_port(skb
, protoff
, dataoff
, dptr
, datalen
,
970 mediaoff
, medialen
, ntohs(rtp_port
)))
977 rtp_exp
= nf_ct_expect_alloc(ct
);
980 nf_ct_expect_init(rtp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
981 IPPROTO_UDP
, NULL
, &rtp_port
);
983 rtcp_exp
= nf_ct_expect_alloc(ct
);
984 if (rtcp_exp
== NULL
)
986 nf_ct_expect_init(rtcp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
987 IPPROTO_UDP
, NULL
, &rtcp_port
);
989 nf_nat_sdp_media
= rcu_dereference(nf_nat_sdp_media_hook
);
990 if (nf_nat_sdp_media
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
991 ct
->status
& IPS_NAT_MASK
&& !direct_rtp
)
992 ret
= nf_nat_sdp_media(skb
, protoff
, dataoff
, dptr
, datalen
,
994 mediaoff
, medialen
, daddr
);
996 if (nf_ct_expect_related(rtp_exp
) == 0) {
997 if (nf_ct_expect_related(rtcp_exp
) != 0)
998 nf_ct_unexpect_related(rtp_exp
);
1003 nf_ct_expect_put(rtcp_exp
);
1005 nf_ct_expect_put(rtp_exp
);
1010 static const struct sdp_media_type sdp_media_types
[] = {
1011 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO
),
1012 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO
),
1013 SDP_MEDIA_TYPE("image ", SIP_EXPECT_IMAGE
),
1016 static const struct sdp_media_type
*sdp_media_type(const char *dptr
,
1017 unsigned int matchoff
,
1018 unsigned int matchlen
)
1020 const struct sdp_media_type
*t
;
1023 for (i
= 0; i
< ARRAY_SIZE(sdp_media_types
); i
++) {
1024 t
= &sdp_media_types
[i
];
1025 if (matchlen
< t
->len
||
1026 strncmp(dptr
+ matchoff
, t
->name
, t
->len
))
1033 static int process_sdp(struct sk_buff
*skb
, unsigned int protoff
,
1034 unsigned int dataoff
,
1035 const char **dptr
, unsigned int *datalen
,
1038 enum ip_conntrack_info ctinfo
;
1039 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1040 unsigned int matchoff
, matchlen
;
1041 unsigned int mediaoff
, medialen
;
1042 unsigned int sdpoff
;
1043 unsigned int caddr_len
, maddr_len
;
1045 union nf_inet_addr caddr
, maddr
, rtp_addr
;
1047 enum sdp_header_types c_hdr
;
1048 const struct sdp_media_type
*t
;
1049 int ret
= NF_ACCEPT
;
1050 typeof(nf_nat_sdp_addr_hook
) nf_nat_sdp_addr
;
1051 typeof(nf_nat_sdp_session_hook
) nf_nat_sdp_session
;
1053 nf_nat_sdp_addr
= rcu_dereference(nf_nat_sdp_addr_hook
);
1054 c_hdr
= nf_ct_l3num(ct
) == AF_INET
? SDP_HDR_CONNECTION_IP4
:
1055 SDP_HDR_CONNECTION_IP6
;
1057 /* Find beginning of session description */
1058 if (ct_sip_get_sdp_header(ct
, *dptr
, 0, *datalen
,
1059 SDP_HDR_VERSION
, SDP_HDR_UNSPEC
,
1060 &matchoff
, &matchlen
) <= 0)
1064 /* The connection information is contained in the session description
1065 * and/or once per media description. The first media description marks
1066 * the end of the session description. */
1068 if (ct_sip_parse_sdp_addr(ct
, *dptr
, sdpoff
, *datalen
,
1069 c_hdr
, SDP_HDR_MEDIA
,
1070 &matchoff
, &matchlen
, &caddr
) > 0)
1071 caddr_len
= matchlen
;
1074 for (i
= 0; i
< ARRAY_SIZE(sdp_media_types
); ) {
1075 if (ct_sip_get_sdp_header(ct
, *dptr
, mediaoff
, *datalen
,
1076 SDP_HDR_MEDIA
, SDP_HDR_UNSPEC
,
1077 &mediaoff
, &medialen
) <= 0)
1080 /* Get media type and port number. A media port value of zero
1081 * indicates an inactive stream. */
1082 t
= sdp_media_type(*dptr
, mediaoff
, medialen
);
1084 mediaoff
+= medialen
;
1090 port
= simple_strtoul(*dptr
+ mediaoff
, NULL
, 10);
1093 if (port
< 1024 || port
> 65535)
1096 /* The media description overrides the session description. */
1098 if (ct_sip_parse_sdp_addr(ct
, *dptr
, mediaoff
, *datalen
,
1099 c_hdr
, SDP_HDR_MEDIA
,
1100 &matchoff
, &matchlen
, &maddr
) > 0) {
1101 maddr_len
= matchlen
;
1102 memcpy(&rtp_addr
, &maddr
, sizeof(rtp_addr
));
1103 } else if (caddr_len
)
1104 memcpy(&rtp_addr
, &caddr
, sizeof(rtp_addr
));
1108 ret
= set_expected_rtp_rtcp(skb
, protoff
, dataoff
,
1110 &rtp_addr
, htons(port
), t
->class,
1111 mediaoff
, medialen
);
1112 if (ret
!= NF_ACCEPT
)
1115 /* Update media connection address if present */
1116 if (maddr_len
&& nf_nat_sdp_addr
&&
1117 nf_ct_l3num(ct
) == NFPROTO_IPV4
&& ct
->status
& IPS_NAT_MASK
) {
1118 ret
= nf_nat_sdp_addr(skb
, protoff
, dataoff
,
1120 mediaoff
, c_hdr
, SDP_HDR_MEDIA
,
1122 if (ret
!= NF_ACCEPT
)
1128 /* Update session connection and owner addresses */
1129 nf_nat_sdp_session
= rcu_dereference(nf_nat_sdp_session_hook
);
1130 if (nf_nat_sdp_session
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1131 ct
->status
& IPS_NAT_MASK
)
1132 ret
= nf_nat_sdp_session(skb
, protoff
, dataoff
,
1133 dptr
, datalen
, sdpoff
, &rtp_addr
);
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
;
1235 unsigned int expires
= 0;
1237 typeof(nf_nat_sip_expect_hook
) nf_nat_sip_expect
;
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
);
1263 /* We don't support third-party registrations */
1264 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
, &daddr
))
1267 if (ct_sip_parse_transport(ct
, *dptr
, matchoff
+ matchlen
, *datalen
,
1271 if (ct_sip_parse_numerical_param(ct
, *dptr
,
1272 matchoff
+ matchlen
, *datalen
,
1273 "expires=", NULL
, NULL
, &expires
) < 0)
1281 exp
= nf_ct_expect_alloc(ct
);
1286 if (sip_direct_signalling
)
1287 saddr
= &ct
->tuplehash
[!dir
].tuple
.src
.u3
;
1289 nf_ct_expect_init(exp
, SIP_EXPECT_SIGNALLING
, nf_ct_l3num(ct
),
1290 saddr
, &daddr
, proto
, NULL
, &port
);
1291 exp
->timeout
.expires
= sip_timeout
* HZ
;
1292 exp
->helper
= nfct_help(ct
)->helper
;
1293 exp
->flags
= NF_CT_EXPECT_PERMANENT
| NF_CT_EXPECT_INACTIVE
;
1295 nf_nat_sip_expect
= rcu_dereference(nf_nat_sip_expect_hook
);
1296 if (nf_nat_sip_expect
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1297 ct
->status
& IPS_NAT_MASK
)
1298 ret
= nf_nat_sip_expect(skb
, protoff
, dataoff
, dptr
, datalen
,
1299 exp
, matchoff
, matchlen
);
1301 if (nf_ct_expect_related(exp
) != 0)
1306 nf_ct_expect_put(exp
);
1309 if (ret
== NF_ACCEPT
)
1310 ct_sip_info
->register_cseq
= cseq
;
1314 static int process_register_response(struct sk_buff
*skb
, unsigned int protoff
,
1315 unsigned int dataoff
,
1316 const char **dptr
, unsigned int *datalen
,
1317 unsigned int cseq
, unsigned int code
)
1319 enum ip_conntrack_info ctinfo
;
1320 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1321 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1322 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
1323 union nf_inet_addr addr
;
1326 unsigned int matchoff
, matchlen
, coff
= 0;
1327 unsigned int expires
= 0;
1328 int in_contact
= 0, ret
;
1330 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1331 * they have received a final response from the registrar for the
1332 * previous one or the previous REGISTER request has timed out".
1334 * However, some servers fail to detect retransmissions and send late
1335 * responses, so we store the sequence number of the last valid
1336 * request and compare it here.
1338 if (ct_sip_info
->register_cseq
!= cseq
)
1341 if (code
>= 100 && code
<= 199)
1343 if (code
< 200 || code
> 299)
1346 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_EXPIRES
,
1347 &matchoff
, &matchlen
) > 0)
1348 expires
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1351 unsigned int c_expires
= expires
;
1353 ret
= ct_sip_parse_header_uri(ct
, *dptr
, &coff
, *datalen
,
1354 SIP_HDR_CONTACT
, &in_contact
,
1355 &matchoff
, &matchlen
,
1362 /* We don't support third-party registrations */
1363 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.dst
.u3
, &addr
))
1366 if (ct_sip_parse_transport(ct
, *dptr
, matchoff
+ matchlen
,
1367 *datalen
, &proto
) == 0)
1370 ret
= ct_sip_parse_numerical_param(ct
, *dptr
,
1371 matchoff
+ matchlen
,
1372 *datalen
, "expires=",
1373 NULL
, NULL
, &c_expires
);
1378 if (refresh_signalling_expectation(ct
, &addr
, proto
, port
,
1384 flush_expectations(ct
, false);
1388 static const struct sip_handler sip_handlers
[] = {
1389 SIP_HANDLER("INVITE", process_invite_request
, process_invite_response
),
1390 SIP_HANDLER("UPDATE", process_sdp
, process_update_response
),
1391 SIP_HANDLER("ACK", process_sdp
, NULL
),
1392 SIP_HANDLER("PRACK", process_sdp
, process_prack_response
),
1393 SIP_HANDLER("BYE", process_bye_request
, NULL
),
1394 SIP_HANDLER("REGISTER", process_register_request
, process_register_response
),
1397 static int process_sip_response(struct sk_buff
*skb
, unsigned int protoff
,
1398 unsigned int dataoff
,
1399 const char **dptr
, unsigned int *datalen
)
1401 enum ip_conntrack_info ctinfo
;
1402 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1403 unsigned int matchoff
, matchlen
, matchend
;
1404 unsigned int code
, cseq
, i
;
1406 if (*datalen
< strlen("SIP/2.0 200"))
1408 code
= simple_strtoul(*dptr
+ strlen("SIP/2.0 "), NULL
, 10);
1412 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_CSEQ
,
1413 &matchoff
, &matchlen
) <= 0)
1415 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1418 matchend
= matchoff
+ matchlen
+ 1;
1420 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1421 const struct sip_handler
*handler
;
1423 handler
= &sip_handlers
[i
];
1424 if (handler
->response
== NULL
)
1426 if (*datalen
< matchend
+ handler
->len
||
1427 strnicmp(*dptr
+ matchend
, handler
->method
, handler
->len
))
1429 return handler
->response(skb
, protoff
, dataoff
, dptr
, datalen
,
1435 static int process_sip_request(struct sk_buff
*skb
, unsigned int protoff
,
1436 unsigned int dataoff
,
1437 const char **dptr
, unsigned int *datalen
)
1439 enum ip_conntrack_info ctinfo
;
1440 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1441 unsigned int matchoff
, matchlen
;
1442 unsigned int cseq
, i
;
1444 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1445 const struct sip_handler
*handler
;
1447 handler
= &sip_handlers
[i
];
1448 if (handler
->request
== NULL
)
1450 if (*datalen
< handler
->len
||
1451 strnicmp(*dptr
, handler
->method
, handler
->len
))
1454 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_CSEQ
,
1455 &matchoff
, &matchlen
) <= 0)
1457 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1461 return handler
->request(skb
, protoff
, dataoff
, dptr
, datalen
,
1467 static int process_sip_msg(struct sk_buff
*skb
, struct nf_conn
*ct
,
1468 unsigned int protoff
, unsigned int dataoff
,
1469 const char **dptr
, unsigned int *datalen
)
1471 typeof(nf_nat_sip_hook
) nf_nat_sip
;
1474 if (strnicmp(*dptr
, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1475 ret
= process_sip_request(skb
, protoff
, dataoff
, dptr
, datalen
);
1477 ret
= process_sip_response(skb
, protoff
, dataoff
, dptr
, datalen
);
1479 if (ret
== NF_ACCEPT
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1480 ct
->status
& IPS_NAT_MASK
) {
1481 nf_nat_sip
= rcu_dereference(nf_nat_sip_hook
);
1482 if (nf_nat_sip
&& !nf_nat_sip(skb
, protoff
, dataoff
,
1490 static int sip_help_tcp(struct sk_buff
*skb
, unsigned int protoff
,
1491 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1493 struct tcphdr
*th
, _tcph
;
1494 unsigned int dataoff
, datalen
;
1495 unsigned int matchoff
, matchlen
, clen
;
1496 unsigned int msglen
, origlen
;
1497 const char *dptr
, *end
;
1498 s16 diff
, tdiff
= 0;
1499 int ret
= NF_ACCEPT
;
1501 typeof(nf_nat_sip_seq_adjust_hook
) nf_nat_sip_seq_adjust
;
1503 if (ctinfo
!= IP_CT_ESTABLISHED
&&
1504 ctinfo
!= IP_CT_ESTABLISHED_REPLY
)
1508 th
= skb_header_pointer(skb
, protoff
, sizeof(_tcph
), &_tcph
);
1511 dataoff
= protoff
+ th
->doff
* 4;
1512 if (dataoff
>= skb
->len
)
1515 nf_ct_refresh(ct
, skb
, sip_timeout
* HZ
);
1517 if (unlikely(skb_linearize(skb
)))
1520 dptr
= skb
->data
+ dataoff
;
1521 datalen
= skb
->len
- dataoff
;
1522 if (datalen
< strlen("SIP/2.0 200"))
1526 if (ct_sip_get_header(ct
, dptr
, 0, datalen
,
1527 SIP_HDR_CONTENT_LENGTH
,
1528 &matchoff
, &matchlen
) <= 0)
1531 clen
= simple_strtoul(dptr
+ matchoff
, (char **)&end
, 10);
1532 if (dptr
+ matchoff
== end
)
1536 for (; end
+ strlen("\r\n\r\n") <= dptr
+ datalen
; end
++) {
1537 if (end
[0] == '\r' && end
[1] == '\n' &&
1538 end
[2] == '\r' && end
[3] == '\n') {
1545 end
+= strlen("\r\n\r\n") + clen
;
1547 msglen
= origlen
= end
- dptr
;
1548 if (msglen
> datalen
)
1551 ret
= process_sip_msg(skb
, ct
, protoff
, dataoff
,
1553 if (ret
!= NF_ACCEPT
)
1555 diff
= msglen
- origlen
;
1560 datalen
= datalen
+ diff
- msglen
;
1563 if (ret
== NF_ACCEPT
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1564 ct
->status
& IPS_NAT_MASK
) {
1565 nf_nat_sip_seq_adjust
= rcu_dereference(nf_nat_sip_seq_adjust_hook
);
1566 if (nf_nat_sip_seq_adjust
)
1567 nf_nat_sip_seq_adjust(skb
, tdiff
);
1573 static int sip_help_udp(struct sk_buff
*skb
, unsigned int protoff
,
1574 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
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
);
1586 if (unlikely(skb_linearize(skb
)))
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
);
1597 static struct nf_conntrack_helper sip
[MAX_PORTS
][4] __read_mostly
;
1599 static const struct nf_conntrack_expect_policy sip_exp_policy
[SIP_EXPECT_MAX
+ 1] = {
1600 [SIP_EXPECT_SIGNALLING
] = {
1601 .name
= "signalling",
1605 [SIP_EXPECT_AUDIO
] = {
1607 .max_expected
= 2 * IP_CT_DIR_MAX
,
1610 [SIP_EXPECT_VIDEO
] = {
1612 .max_expected
= 2 * IP_CT_DIR_MAX
,
1615 [SIP_EXPECT_IMAGE
] = {
1617 .max_expected
= IP_CT_DIR_MAX
,
1622 static void nf_conntrack_sip_fini(void)
1626 for (i
= 0; i
< ports_c
; i
++) {
1627 for (j
= 0; j
< ARRAY_SIZE(sip
[i
]); j
++) {
1628 if (sip
[i
][j
].me
== NULL
)
1630 nf_conntrack_helper_unregister(&sip
[i
][j
]);
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
;
1646 sip
[i
][0].tuple
.dst
.protonum
= IPPROTO_UDP
;
1647 sip
[i
][0].help
= sip_help_udp
;
1648 sip
[i
][1].tuple
.src
.l3num
= AF_INET
;
1649 sip
[i
][1].tuple
.dst
.protonum
= IPPROTO_TCP
;
1650 sip
[i
][1].help
= sip_help_tcp
;
1652 sip
[i
][2].tuple
.src
.l3num
= AF_INET6
;
1653 sip
[i
][2].tuple
.dst
.protonum
= IPPROTO_UDP
;
1654 sip
[i
][2].help
= sip_help_udp
;
1655 sip
[i
][3].tuple
.src
.l3num
= AF_INET6
;
1656 sip
[i
][3].tuple
.dst
.protonum
= IPPROTO_TCP
;
1657 sip
[i
][3].help
= sip_help_tcp
;
1659 for (j
= 0; j
< ARRAY_SIZE(sip
[i
]); j
++) {
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
;
1664 sip
[i
][j
].me
= THIS_MODULE
;
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
]);
1673 ret
= nf_conntrack_helper_register(&sip
[i
][j
]);
1675 printk(KERN_ERR
"nf_ct_sip: failed to register"
1676 " helper for pf: %u port: %u\n",
1677 sip
[i
][j
].tuple
.src
.l3num
, ports
[i
]);
1678 nf_conntrack_sip_fini();
1686 module_init(nf_conntrack_sip_init
);
1687 module_exit(nf_conntrack_sip_fini
);