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 dataoff
,
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
,
66 unsigned int *datalen
,
67 struct nf_conntrack_expect
*exp
,
68 unsigned int matchoff
,
69 unsigned int matchlen
) __read_mostly
;
70 EXPORT_SYMBOL_GPL(nf_nat_sip_expect_hook
);
72 unsigned int (*nf_nat_sdp_addr_hook
)(struct sk_buff
*skb
, unsigned int dataoff
,
74 unsigned int *datalen
,
76 enum sdp_header_types type
,
77 enum sdp_header_types term
,
78 const union nf_inet_addr
*addr
)
80 EXPORT_SYMBOL_GPL(nf_nat_sdp_addr_hook
);
82 unsigned int (*nf_nat_sdp_port_hook
)(struct sk_buff
*skb
, unsigned int dataoff
,
84 unsigned int *datalen
,
85 unsigned int matchoff
,
86 unsigned int matchlen
,
87 u_int16_t port
) __read_mostly
;
88 EXPORT_SYMBOL_GPL(nf_nat_sdp_port_hook
);
90 unsigned int (*nf_nat_sdp_session_hook
)(struct sk_buff
*skb
,
93 unsigned int *datalen
,
95 const union nf_inet_addr
*addr
)
97 EXPORT_SYMBOL_GPL(nf_nat_sdp_session_hook
);
99 unsigned int (*nf_nat_sdp_media_hook
)(struct sk_buff
*skb
, unsigned int dataoff
,
101 unsigned int *datalen
,
102 struct nf_conntrack_expect
*rtp_exp
,
103 struct nf_conntrack_expect
*rtcp_exp
,
104 unsigned int mediaoff
,
105 unsigned int medialen
,
106 union nf_inet_addr
*rtp_addr
)
108 EXPORT_SYMBOL_GPL(nf_nat_sdp_media_hook
);
110 static int string_len(const struct nf_conn
*ct
, const char *dptr
,
111 const char *limit
, int *shift
)
115 while (dptr
< limit
&& isalpha(*dptr
)) {
122 static int digits_len(const struct nf_conn
*ct
, const char *dptr
,
123 const char *limit
, int *shift
)
126 while (dptr
< limit
&& isdigit(*dptr
)) {
133 static int iswordc(const char c
)
135 if (isalnum(c
) || c
== '!' || c
== '"' || c
== '%' ||
136 (c
>= '(' && c
<= '/') || c
== ':' || c
== '<' || c
== '>' ||
137 c
== '?' || (c
>= '[' && c
<= ']') || c
== '_' || c
== '`' ||
138 c
== '{' || c
== '}' || c
== '~')
143 static int word_len(const char *dptr
, const char *limit
)
146 while (dptr
< limit
&& iswordc(*dptr
)) {
153 static int callid_len(const struct nf_conn
*ct
, const char *dptr
,
154 const char *limit
, int *shift
)
158 len
= word_len(dptr
, limit
);
160 if (!len
|| dptr
== limit
|| *dptr
!= '@')
165 domain_len
= word_len(dptr
, limit
);
168 return len
+ domain_len
;
171 /* get media type + port length */
172 static int media_len(const struct nf_conn
*ct
, const char *dptr
,
173 const char *limit
, int *shift
)
175 int len
= string_len(ct
, dptr
, limit
, shift
);
178 if (dptr
>= limit
|| *dptr
!= ' ')
183 return len
+ digits_len(ct
, dptr
, limit
, shift
);
186 static int sip_parse_addr(const struct nf_conn
*ct
, const char *cp
,
187 const char **endp
, union nf_inet_addr
*addr
,
188 const char *limit
, bool delim
)
196 memset(addr
, 0, sizeof(*addr
));
197 switch (nf_ct_l3num(ct
)) {
199 ret
= in4_pton(cp
, limit
- cp
, (u8
*)&addr
->ip
, -1, &end
);
204 if (cp
< limit
&& *cp
== '[')
209 ret
= in6_pton(cp
, limit
- cp
, (u8
*)&addr
->ip6
, -1, &end
);
213 if (end
< limit
&& *end
== ']')
227 /* skip ip address. returns its length. */
228 static int epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
229 const char *limit
, int *shift
)
231 union nf_inet_addr addr
;
232 const char *aux
= dptr
;
234 if (!sip_parse_addr(ct
, dptr
, &dptr
, &addr
, limit
, true)) {
235 pr_debug("ip: %s parse failed.!\n", dptr
);
242 dptr
+= digits_len(ct
, dptr
, limit
, shift
);
247 /* get address length, skiping user info. */
248 static int skp_epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
249 const char *limit
, int *shift
)
251 const char *start
= dptr
;
254 /* Search for @, but stop at the end of the line.
255 * We are inside a sip: URI, so we don't need to worry about
256 * continuation lines. */
257 while (dptr
< limit
&&
258 *dptr
!= '@' && *dptr
!= '\r' && *dptr
!= '\n') {
263 if (dptr
< limit
&& *dptr
== '@') {
271 return epaddr_len(ct
, dptr
, limit
, shift
);
274 /* Parse a SIP request line of the form:
276 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
278 * and return the offset and length of the address contained in the Request-URI.
280 int ct_sip_parse_request(const struct nf_conn
*ct
,
281 const char *dptr
, unsigned int datalen
,
282 unsigned int *matchoff
, unsigned int *matchlen
,
283 union nf_inet_addr
*addr
, __be16
*port
)
285 const char *start
= dptr
, *limit
= dptr
+ datalen
, *end
;
290 /* Skip method and following whitespace */
291 mlen
= string_len(ct
, dptr
, limit
, NULL
);
299 for (; dptr
< limit
- strlen("sip:"); dptr
++) {
300 if (*dptr
== '\r' || *dptr
== '\n')
302 if (strnicmp(dptr
, "sip:", strlen("sip:")) == 0) {
303 dptr
+= strlen("sip:");
307 if (!skp_epaddr_len(ct
, dptr
, limit
, &shift
))
311 if (!sip_parse_addr(ct
, dptr
, &end
, addr
, limit
, true))
313 if (end
< limit
&& *end
== ':') {
315 p
= simple_strtoul(end
, (char **)&end
, 10);
316 if (p
< 1024 || p
> 65535)
320 *port
= htons(SIP_PORT
);
324 *matchoff
= dptr
- start
;
325 *matchlen
= end
- dptr
;
328 EXPORT_SYMBOL_GPL(ct_sip_parse_request
);
330 /* SIP header parsing: SIP headers are located at the beginning of a line, but
331 * may span several lines, in which case the continuation lines begin with a
332 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
333 * CRLF, RFC 3261 allows only CRLF, we support both.
335 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
336 * whitespace and the values. Whitespace in this context means any amount of
337 * tabs, spaces and continuation lines, which are treated as a single whitespace
340 * Some headers may appear multiple times. A comma separated list of values is
341 * equivalent to multiple headers.
343 static const struct sip_header ct_sip_hdrs
[] = {
344 [SIP_HDR_CSEQ
] = SIP_HDR("CSeq", NULL
, NULL
, digits_len
),
345 [SIP_HDR_FROM
] = SIP_HDR("From", "f", "sip:", skp_epaddr_len
),
346 [SIP_HDR_TO
] = SIP_HDR("To", "t", "sip:", skp_epaddr_len
),
347 [SIP_HDR_CONTACT
] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len
),
348 [SIP_HDR_VIA_UDP
] = SIP_HDR("Via", "v", "UDP ", epaddr_len
),
349 [SIP_HDR_VIA_TCP
] = SIP_HDR("Via", "v", "TCP ", epaddr_len
),
350 [SIP_HDR_EXPIRES
] = SIP_HDR("Expires", NULL
, NULL
, digits_len
),
351 [SIP_HDR_CONTENT_LENGTH
] = SIP_HDR("Content-Length", "l", NULL
, digits_len
),
352 [SIP_HDR_CALL_ID
] = SIP_HDR("Call-Id", "i", NULL
, callid_len
),
355 static const char *sip_follow_continuation(const char *dptr
, const char *limit
)
357 /* Walk past newline */
361 /* Skip '\n' in CR LF */
362 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
367 /* Continuation line? */
368 if (*dptr
!= ' ' && *dptr
!= '\t')
371 /* skip leading whitespace */
372 for (; dptr
< limit
; dptr
++) {
373 if (*dptr
!= ' ' && *dptr
!= '\t')
379 static const char *sip_skip_whitespace(const char *dptr
, const char *limit
)
381 for (; dptr
< limit
; dptr
++) {
384 if (*dptr
!= '\r' && *dptr
!= '\n')
386 dptr
= sip_follow_continuation(dptr
, limit
);
393 /* Search within a SIP header value, dealing with continuation lines */
394 static const char *ct_sip_header_search(const char *dptr
, const char *limit
,
395 const char *needle
, unsigned int len
)
397 for (limit
-= len
; dptr
< limit
; dptr
++) {
398 if (*dptr
== '\r' || *dptr
== '\n') {
399 dptr
= sip_follow_continuation(dptr
, limit
);
405 if (strnicmp(dptr
, needle
, len
) == 0)
411 int ct_sip_get_header(const struct nf_conn
*ct
, const char *dptr
,
412 unsigned int dataoff
, unsigned int datalen
,
413 enum sip_header_types type
,
414 unsigned int *matchoff
, unsigned int *matchlen
)
416 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
417 const char *start
= dptr
, *limit
= dptr
+ datalen
;
420 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
421 /* Find beginning of line */
422 if (*dptr
!= '\r' && *dptr
!= '\n')
426 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
431 /* Skip continuation lines */
432 if (*dptr
== ' ' || *dptr
== '\t')
435 /* Find header. Compact headers must be followed by a
436 * non-alphabetic character to avoid mismatches. */
437 if (limit
- dptr
>= hdr
->len
&&
438 strnicmp(dptr
, hdr
->name
, hdr
->len
) == 0)
440 else if (hdr
->cname
&& limit
- dptr
>= hdr
->clen
+ 1 &&
441 strnicmp(dptr
, hdr
->cname
, hdr
->clen
) == 0 &&
442 !isalpha(*(dptr
+ hdr
->clen
)))
447 /* Find and skip colon */
448 dptr
= sip_skip_whitespace(dptr
, limit
);
451 if (*dptr
!= ':' || ++dptr
>= limit
)
454 /* Skip whitespace after colon */
455 dptr
= sip_skip_whitespace(dptr
, limit
);
459 *matchoff
= dptr
- start
;
461 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
,
468 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
471 *matchoff
= dptr
- start
+ shift
;
476 EXPORT_SYMBOL_GPL(ct_sip_get_header
);
478 /* Get next header field in a list of comma separated values */
479 static int ct_sip_next_header(const struct nf_conn
*ct
, const char *dptr
,
480 unsigned int dataoff
, unsigned int datalen
,
481 enum sip_header_types type
,
482 unsigned int *matchoff
, unsigned int *matchlen
)
484 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
485 const char *start
= dptr
, *limit
= dptr
+ datalen
;
490 dptr
= ct_sip_header_search(dptr
, limit
, ",", strlen(","));
494 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
, hdr
->slen
);
499 *matchoff
= dptr
- start
;
500 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
507 /* Walk through headers until a parsable one is found or no header of the
508 * given type is left. */
509 static int ct_sip_walk_headers(const struct nf_conn
*ct
, const char *dptr
,
510 unsigned int dataoff
, unsigned int datalen
,
511 enum sip_header_types type
, int *in_header
,
512 unsigned int *matchoff
, unsigned int *matchlen
)
516 if (in_header
&& *in_header
) {
518 ret
= ct_sip_next_header(ct
, dptr
, dataoff
, datalen
,
519 type
, matchoff
, matchlen
);
524 dataoff
+= *matchoff
;
530 ret
= ct_sip_get_header(ct
, dptr
, dataoff
, datalen
,
531 type
, matchoff
, matchlen
);
536 dataoff
+= *matchoff
;
544 /* Locate a SIP header, parse the URI and return the offset and length of
545 * the address as well as the address and port themselves. A stream of
546 * headers can be parsed by handing in a non-NULL datalen and in_header
549 int ct_sip_parse_header_uri(const struct nf_conn
*ct
, const char *dptr
,
550 unsigned int *dataoff
, unsigned int datalen
,
551 enum sip_header_types type
, int *in_header
,
552 unsigned int *matchoff
, unsigned int *matchlen
,
553 union nf_inet_addr
*addr
, __be16
*port
)
555 const char *c
, *limit
= dptr
+ datalen
;
559 ret
= ct_sip_walk_headers(ct
, dptr
, dataoff
? *dataoff
: 0, datalen
,
560 type
, in_header
, matchoff
, matchlen
);
565 if (!sip_parse_addr(ct
, dptr
+ *matchoff
, &c
, addr
, limit
, true))
569 p
= simple_strtoul(c
, (char **)&c
, 10);
570 if (p
< 1024 || p
> 65535)
574 *port
= htons(SIP_PORT
);
580 EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri
);
582 static int ct_sip_parse_param(const struct nf_conn
*ct
, const char *dptr
,
583 unsigned int dataoff
, unsigned int datalen
,
585 unsigned int *matchoff
, unsigned int *matchlen
)
587 const char *limit
= dptr
+ datalen
;
591 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
593 limit
= dptr
+ datalen
;
595 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
598 start
+= strlen(name
);
600 end
= ct_sip_header_search(start
, limit
, ";", strlen(";"));
604 *matchoff
= start
- dptr
;
605 *matchlen
= end
- start
;
609 /* Parse address from header parameter and return address, offset and length */
610 int ct_sip_parse_address_param(const struct nf_conn
*ct
, const char *dptr
,
611 unsigned int dataoff
, unsigned int datalen
,
613 unsigned int *matchoff
, unsigned int *matchlen
,
614 union nf_inet_addr
*addr
, bool delim
)
616 const char *limit
= dptr
+ datalen
;
617 const char *start
, *end
;
619 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
621 limit
= dptr
+ datalen
;
623 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
627 start
+= strlen(name
);
628 if (!sip_parse_addr(ct
, start
, &end
, addr
, limit
, delim
))
630 *matchoff
= start
- dptr
;
631 *matchlen
= end
- start
;
634 EXPORT_SYMBOL_GPL(ct_sip_parse_address_param
);
636 /* Parse numerical header parameter and return value, offset and length */
637 int ct_sip_parse_numerical_param(const struct nf_conn
*ct
, const char *dptr
,
638 unsigned int dataoff
, unsigned int datalen
,
640 unsigned int *matchoff
, unsigned int *matchlen
,
643 const char *limit
= dptr
+ datalen
;
647 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
649 limit
= dptr
+ datalen
;
651 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
655 start
+= strlen(name
);
656 *val
= simple_strtoul(start
, &end
, 0);
659 if (matchoff
&& matchlen
) {
660 *matchoff
= start
- dptr
;
661 *matchlen
= end
- start
;
665 EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param
);
667 static int ct_sip_parse_transport(struct nf_conn
*ct
, const char *dptr
,
668 unsigned int dataoff
, unsigned int datalen
,
671 unsigned int matchoff
, matchlen
;
673 if (ct_sip_parse_param(ct
, dptr
, dataoff
, datalen
, "transport=",
674 &matchoff
, &matchlen
)) {
675 if (!strnicmp(dptr
+ matchoff
, "TCP", strlen("TCP")))
676 *proto
= IPPROTO_TCP
;
677 else if (!strnicmp(dptr
+ matchoff
, "UDP", strlen("UDP")))
678 *proto
= IPPROTO_UDP
;
682 if (*proto
!= nf_ct_protonum(ct
))
685 *proto
= nf_ct_protonum(ct
);
690 static int sdp_parse_addr(const struct nf_conn
*ct
, const char *cp
,
691 const char **endp
, union nf_inet_addr
*addr
,
697 memset(addr
, 0, sizeof(*addr
));
698 switch (nf_ct_l3num(ct
)) {
700 ret
= in4_pton(cp
, limit
- cp
, (u8
*)&addr
->ip
, -1, &end
);
703 ret
= in6_pton(cp
, limit
- cp
, (u8
*)&addr
->ip6
, -1, &end
);
716 /* skip ip address. returns its length. */
717 static int sdp_addr_len(const struct nf_conn
*ct
, const char *dptr
,
718 const char *limit
, int *shift
)
720 union nf_inet_addr addr
;
721 const char *aux
= dptr
;
723 if (!sdp_parse_addr(ct
, dptr
, &dptr
, &addr
, limit
)) {
724 pr_debug("ip: %s parse failed.!\n", dptr
);
731 /* SDP header parsing: a SDP session description contains an ordered set of
732 * headers, starting with a section containing general session parameters,
733 * optionally followed by multiple media descriptions.
735 * SDP headers always start at the beginning of a line. According to RFC 2327:
736 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
737 * be tolerant and also accept records terminated with a single newline
738 * character". We handle both cases.
740 static const struct sip_header ct_sdp_hdrs
[] = {
741 [SDP_HDR_VERSION
] = SDP_HDR("v=", NULL
, digits_len
),
742 [SDP_HDR_OWNER_IP4
] = SDP_HDR("o=", "IN IP4 ", sdp_addr_len
),
743 [SDP_HDR_CONNECTION_IP4
] = SDP_HDR("c=", "IN IP4 ", sdp_addr_len
),
744 [SDP_HDR_OWNER_IP6
] = SDP_HDR("o=", "IN IP6 ", sdp_addr_len
),
745 [SDP_HDR_CONNECTION_IP6
] = SDP_HDR("c=", "IN IP6 ", sdp_addr_len
),
746 [SDP_HDR_MEDIA
] = SDP_HDR("m=", NULL
, media_len
),
749 /* Linear string search within SDP header values */
750 static const char *ct_sdp_header_search(const char *dptr
, const char *limit
,
751 const char *needle
, unsigned int len
)
753 for (limit
-= len
; dptr
< limit
; dptr
++) {
754 if (*dptr
== '\r' || *dptr
== '\n')
756 if (strncmp(dptr
, needle
, len
) == 0)
762 /* Locate a SDP header (optionally a substring within the header value),
763 * optionally stopping at the first occurrence of the term header, parse
764 * it and return the offset and length of the data we're interested in.
766 int ct_sip_get_sdp_header(const struct nf_conn
*ct
, const char *dptr
,
767 unsigned int dataoff
, unsigned int datalen
,
768 enum sdp_header_types type
,
769 enum sdp_header_types term
,
770 unsigned int *matchoff
, unsigned int *matchlen
)
772 const struct sip_header
*hdr
= &ct_sdp_hdrs
[type
];
773 const struct sip_header
*thdr
= &ct_sdp_hdrs
[term
];
774 const char *start
= dptr
, *limit
= dptr
+ datalen
;
777 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
778 /* Find beginning of line */
779 if (*dptr
!= '\r' && *dptr
!= '\n')
783 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
788 if (term
!= SDP_HDR_UNSPEC
&&
789 limit
- dptr
>= thdr
->len
&&
790 strnicmp(dptr
, thdr
->name
, thdr
->len
) == 0)
792 else if (limit
- dptr
>= hdr
->len
&&
793 strnicmp(dptr
, hdr
->name
, hdr
->len
) == 0)
798 *matchoff
= dptr
- start
;
800 dptr
= ct_sdp_header_search(dptr
, limit
, hdr
->search
,
807 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
810 *matchoff
= dptr
- start
+ shift
;
815 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header
);
817 static int ct_sip_parse_sdp_addr(const struct nf_conn
*ct
, const char *dptr
,
818 unsigned int dataoff
, unsigned int datalen
,
819 enum sdp_header_types type
,
820 enum sdp_header_types term
,
821 unsigned int *matchoff
, unsigned int *matchlen
,
822 union nf_inet_addr
*addr
)
826 ret
= ct_sip_get_sdp_header(ct
, dptr
, dataoff
, datalen
, type
, term
,
831 if (!sdp_parse_addr(ct
, dptr
+ *matchoff
, NULL
, addr
,
832 dptr
+ *matchoff
+ *matchlen
))
837 static int refresh_signalling_expectation(struct nf_conn
*ct
,
838 union nf_inet_addr
*addr
,
839 u8 proto
, __be16 port
,
840 unsigned int expires
)
842 struct nf_conn_help
*help
= nfct_help(ct
);
843 struct nf_conntrack_expect
*exp
;
844 struct hlist_node
*n
, *next
;
847 spin_lock_bh(&nf_conntrack_lock
);
848 hlist_for_each_entry_safe(exp
, n
, next
, &help
->expectations
, lnode
) {
849 if (exp
->class != SIP_EXPECT_SIGNALLING
||
850 !nf_inet_addr_cmp(&exp
->tuple
.dst
.u3
, addr
) ||
851 exp
->tuple
.dst
.protonum
!= proto
||
852 exp
->tuple
.dst
.u
.udp
.port
!= port
)
854 if (!del_timer(&exp
->timeout
))
856 exp
->flags
&= ~NF_CT_EXPECT_INACTIVE
;
857 exp
->timeout
.expires
= jiffies
+ expires
* HZ
;
858 add_timer(&exp
->timeout
);
862 spin_unlock_bh(&nf_conntrack_lock
);
866 static void flush_expectations(struct nf_conn
*ct
, bool media
)
868 struct nf_conn_help
*help
= nfct_help(ct
);
869 struct nf_conntrack_expect
*exp
;
870 struct hlist_node
*n
, *next
;
872 spin_lock_bh(&nf_conntrack_lock
);
873 hlist_for_each_entry_safe(exp
, n
, next
, &help
->expectations
, lnode
) {
874 if ((exp
->class != SIP_EXPECT_SIGNALLING
) ^ media
)
876 if (!del_timer(&exp
->timeout
))
878 nf_ct_unlink_expect(exp
);
879 nf_ct_expect_put(exp
);
883 spin_unlock_bh(&nf_conntrack_lock
);
886 static int set_expected_rtp_rtcp(struct sk_buff
*skb
, unsigned int dataoff
,
887 const char **dptr
, unsigned int *datalen
,
888 union nf_inet_addr
*daddr
, __be16 port
,
889 enum sip_expectation_classes
class,
890 unsigned int mediaoff
, unsigned int medialen
)
892 struct nf_conntrack_expect
*exp
, *rtp_exp
, *rtcp_exp
;
893 enum ip_conntrack_info ctinfo
;
894 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
895 struct net
*net
= nf_ct_net(ct
);
896 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
897 union nf_inet_addr
*saddr
;
898 struct nf_conntrack_tuple tuple
;
899 int direct_rtp
= 0, skip_expect
= 0, ret
= NF_DROP
;
901 __be16 rtp_port
, rtcp_port
;
902 typeof(nf_nat_sdp_port_hook
) nf_nat_sdp_port
;
903 typeof(nf_nat_sdp_media_hook
) nf_nat_sdp_media
;
906 if (sip_direct_media
) {
907 if (!nf_inet_addr_cmp(daddr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
))
909 saddr
= &ct
->tuplehash
[!dir
].tuple
.src
.u3
;
912 /* We need to check whether the registration exists before attempting
913 * to register it since we can see the same media description multiple
914 * times on different connections in case multiple endpoints receive
917 * RTP optimization: if we find a matching media channel expectation
918 * and both the expectation and this connection are SNATed, we assume
919 * both sides can reach each other directly and use the final
920 * destination address from the expectation. We still need to keep
921 * the NATed expectations for media that might arrive from the
922 * outside, and additionally need to expect the direct RTP stream
923 * in case it passes through us even without NAT.
925 memset(&tuple
, 0, sizeof(tuple
));
927 tuple
.src
.u3
= *saddr
;
928 tuple
.src
.l3num
= nf_ct_l3num(ct
);
929 tuple
.dst
.protonum
= IPPROTO_UDP
;
930 tuple
.dst
.u3
= *daddr
;
931 tuple
.dst
.u
.udp
.port
= port
;
935 exp
= __nf_ct_expect_find(net
, nf_ct_zone(ct
), &tuple
);
937 if (!exp
|| exp
->master
== ct
||
938 nfct_help(exp
->master
)->helper
!= nfct_help(ct
)->helper
||
941 #ifdef CONFIG_NF_NAT_NEEDED
942 if (exp
->tuple
.src
.l3num
== AF_INET
&& !direct_rtp
&&
943 (exp
->saved_ip
!= exp
->tuple
.dst
.u3
.ip
||
944 exp
->saved_proto
.udp
.port
!= exp
->tuple
.dst
.u
.udp
.port
) &&
945 ct
->status
& IPS_NAT_MASK
) {
946 daddr
->ip
= exp
->saved_ip
;
947 tuple
.dst
.u3
.ip
= exp
->saved_ip
;
948 tuple
.dst
.u
.udp
.port
= exp
->saved_proto
.udp
.port
;
953 } while (!skip_expect
);
956 base_port
= ntohs(tuple
.dst
.u
.udp
.port
) & ~1;
957 rtp_port
= htons(base_port
);
958 rtcp_port
= htons(base_port
+ 1);
961 nf_nat_sdp_port
= rcu_dereference(nf_nat_sdp_port_hook
);
962 if (nf_nat_sdp_port
&&
963 !nf_nat_sdp_port(skb
, dataoff
, dptr
, datalen
,
964 mediaoff
, medialen
, ntohs(rtp_port
)))
971 rtp_exp
= nf_ct_expect_alloc(ct
);
974 nf_ct_expect_init(rtp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
975 IPPROTO_UDP
, NULL
, &rtp_port
);
977 rtcp_exp
= nf_ct_expect_alloc(ct
);
978 if (rtcp_exp
== NULL
)
980 nf_ct_expect_init(rtcp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
981 IPPROTO_UDP
, NULL
, &rtcp_port
);
983 nf_nat_sdp_media
= rcu_dereference(nf_nat_sdp_media_hook
);
984 if (nf_nat_sdp_media
&& ct
->status
& IPS_NAT_MASK
&& !direct_rtp
)
985 ret
= nf_nat_sdp_media(skb
, dataoff
, dptr
, datalen
,
987 mediaoff
, medialen
, daddr
);
989 if (nf_ct_expect_related(rtp_exp
) == 0) {
990 if (nf_ct_expect_related(rtcp_exp
) != 0)
991 nf_ct_unexpect_related(rtp_exp
);
996 nf_ct_expect_put(rtcp_exp
);
998 nf_ct_expect_put(rtp_exp
);
1003 static const struct sdp_media_type sdp_media_types
[] = {
1004 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO
),
1005 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO
),
1006 SDP_MEDIA_TYPE("image ", SIP_EXPECT_IMAGE
),
1009 static const struct sdp_media_type
*sdp_media_type(const char *dptr
,
1010 unsigned int matchoff
,
1011 unsigned int matchlen
)
1013 const struct sdp_media_type
*t
;
1016 for (i
= 0; i
< ARRAY_SIZE(sdp_media_types
); i
++) {
1017 t
= &sdp_media_types
[i
];
1018 if (matchlen
< t
->len
||
1019 strncmp(dptr
+ matchoff
, t
->name
, t
->len
))
1026 static int process_sdp(struct sk_buff
*skb
, unsigned int dataoff
,
1027 const char **dptr
, unsigned int *datalen
,
1030 enum ip_conntrack_info ctinfo
;
1031 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1032 unsigned int matchoff
, matchlen
;
1033 unsigned int mediaoff
, medialen
;
1034 unsigned int sdpoff
;
1035 unsigned int caddr_len
, maddr_len
;
1037 union nf_inet_addr caddr
, maddr
, rtp_addr
;
1039 enum sdp_header_types c_hdr
;
1040 const struct sdp_media_type
*t
;
1041 int ret
= NF_ACCEPT
;
1042 typeof(nf_nat_sdp_addr_hook
) nf_nat_sdp_addr
;
1043 typeof(nf_nat_sdp_session_hook
) nf_nat_sdp_session
;
1045 nf_nat_sdp_addr
= rcu_dereference(nf_nat_sdp_addr_hook
);
1046 c_hdr
= nf_ct_l3num(ct
) == AF_INET
? SDP_HDR_CONNECTION_IP4
:
1047 SDP_HDR_CONNECTION_IP6
;
1049 /* Find beginning of session description */
1050 if (ct_sip_get_sdp_header(ct
, *dptr
, 0, *datalen
,
1051 SDP_HDR_VERSION
, SDP_HDR_UNSPEC
,
1052 &matchoff
, &matchlen
) <= 0)
1056 /* The connection information is contained in the session description
1057 * and/or once per media description. The first media description marks
1058 * the end of the session description. */
1060 if (ct_sip_parse_sdp_addr(ct
, *dptr
, sdpoff
, *datalen
,
1061 c_hdr
, SDP_HDR_MEDIA
,
1062 &matchoff
, &matchlen
, &caddr
) > 0)
1063 caddr_len
= matchlen
;
1066 for (i
= 0; i
< ARRAY_SIZE(sdp_media_types
); ) {
1067 if (ct_sip_get_sdp_header(ct
, *dptr
, mediaoff
, *datalen
,
1068 SDP_HDR_MEDIA
, SDP_HDR_UNSPEC
,
1069 &mediaoff
, &medialen
) <= 0)
1072 /* Get media type and port number. A media port value of zero
1073 * indicates an inactive stream. */
1074 t
= sdp_media_type(*dptr
, mediaoff
, medialen
);
1076 mediaoff
+= medialen
;
1082 port
= simple_strtoul(*dptr
+ mediaoff
, NULL
, 10);
1085 if (port
< 1024 || port
> 65535)
1088 /* The media description overrides the session description. */
1090 if (ct_sip_parse_sdp_addr(ct
, *dptr
, mediaoff
, *datalen
,
1091 c_hdr
, SDP_HDR_MEDIA
,
1092 &matchoff
, &matchlen
, &maddr
) > 0) {
1093 maddr_len
= matchlen
;
1094 memcpy(&rtp_addr
, &maddr
, sizeof(rtp_addr
));
1095 } else if (caddr_len
)
1096 memcpy(&rtp_addr
, &caddr
, sizeof(rtp_addr
));
1100 ret
= set_expected_rtp_rtcp(skb
, dataoff
, dptr
, datalen
,
1101 &rtp_addr
, htons(port
), t
->class,
1102 mediaoff
, medialen
);
1103 if (ret
!= NF_ACCEPT
)
1106 /* Update media connection address if present */
1107 if (maddr_len
&& nf_nat_sdp_addr
&& ct
->status
& IPS_NAT_MASK
) {
1108 ret
= nf_nat_sdp_addr(skb
, dataoff
, dptr
, datalen
,
1109 mediaoff
, c_hdr
, SDP_HDR_MEDIA
,
1111 if (ret
!= NF_ACCEPT
)
1117 /* Update session connection and owner addresses */
1118 nf_nat_sdp_session
= rcu_dereference(nf_nat_sdp_session_hook
);
1119 if (nf_nat_sdp_session
&& ct
->status
& IPS_NAT_MASK
)
1120 ret
= nf_nat_sdp_session(skb
, dataoff
, dptr
, datalen
, sdpoff
,
1125 static int process_invite_response(struct sk_buff
*skb
, unsigned int dataoff
,
1126 const char **dptr
, unsigned int *datalen
,
1127 unsigned int cseq
, unsigned int code
)
1129 enum ip_conntrack_info ctinfo
;
1130 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1131 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1133 if ((code
>= 100 && code
<= 199) ||
1134 (code
>= 200 && code
<= 299))
1135 return process_sdp(skb
, dataoff
, dptr
, datalen
, cseq
);
1136 else if (ct_sip_info
->invite_cseq
== cseq
)
1137 flush_expectations(ct
, true);
1141 static int process_update_response(struct sk_buff
*skb
, unsigned int dataoff
,
1142 const char **dptr
, unsigned int *datalen
,
1143 unsigned int cseq
, unsigned int code
)
1145 enum ip_conntrack_info ctinfo
;
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
, dataoff
, dptr
, datalen
, cseq
);
1152 else if (ct_sip_info
->invite_cseq
== cseq
)
1153 flush_expectations(ct
, true);
1157 static int process_prack_response(struct sk_buff
*skb
, unsigned int dataoff
,
1158 const char **dptr
, unsigned int *datalen
,
1159 unsigned int cseq
, unsigned int code
)
1161 enum ip_conntrack_info ctinfo
;
1162 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1163 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1165 if ((code
>= 100 && code
<= 199) ||
1166 (code
>= 200 && code
<= 299))
1167 return process_sdp(skb
, dataoff
, dptr
, datalen
, cseq
);
1168 else if (ct_sip_info
->invite_cseq
== cseq
)
1169 flush_expectations(ct
, true);
1173 static int process_invite_request(struct sk_buff
*skb
, unsigned int dataoff
,
1174 const char **dptr
, unsigned int *datalen
,
1177 enum ip_conntrack_info ctinfo
;
1178 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1179 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1182 flush_expectations(ct
, true);
1183 ret
= process_sdp(skb
, dataoff
, dptr
, datalen
, cseq
);
1184 if (ret
== NF_ACCEPT
)
1185 ct_sip_info
->invite_cseq
= cseq
;
1189 static int process_bye_request(struct sk_buff
*skb
, 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
);
1196 flush_expectations(ct
, true);
1200 /* Parse a REGISTER request and create a permanent expectation for incoming
1201 * signalling connections. The expectation is marked inactive and is activated
1202 * when receiving a response indicating success from the registrar.
1204 static int process_register_request(struct sk_buff
*skb
, unsigned int dataoff
,
1205 const char **dptr
, unsigned int *datalen
,
1208 enum ip_conntrack_info ctinfo
;
1209 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1210 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1211 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
1212 unsigned int matchoff
, matchlen
;
1213 struct nf_conntrack_expect
*exp
;
1214 union nf_inet_addr
*saddr
, daddr
;
1217 unsigned int expires
= 0;
1219 typeof(nf_nat_sip_expect_hook
) nf_nat_sip_expect
;
1221 /* Expected connections can not register again. */
1222 if (ct
->status
& IPS_EXPECTED
)
1225 /* We must check the expiration time: a value of zero signals the
1226 * registrar to release the binding. We'll remove our expectation
1227 * when receiving the new bindings in the response, but we don't
1228 * want to create new ones.
1230 * The expiration time may be contained in Expires: header, the
1231 * Contact: header parameters or the URI parameters.
1233 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_EXPIRES
,
1234 &matchoff
, &matchlen
) > 0)
1235 expires
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1237 ret
= ct_sip_parse_header_uri(ct
, *dptr
, NULL
, *datalen
,
1238 SIP_HDR_CONTACT
, NULL
,
1239 &matchoff
, &matchlen
, &daddr
, &port
);
1245 /* We don't support third-party registrations */
1246 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
, &daddr
))
1249 if (ct_sip_parse_transport(ct
, *dptr
, matchoff
+ matchlen
, *datalen
,
1253 if (ct_sip_parse_numerical_param(ct
, *dptr
,
1254 matchoff
+ matchlen
, *datalen
,
1255 "expires=", NULL
, NULL
, &expires
) < 0)
1263 exp
= nf_ct_expect_alloc(ct
);
1268 if (sip_direct_signalling
)
1269 saddr
= &ct
->tuplehash
[!dir
].tuple
.src
.u3
;
1271 nf_ct_expect_init(exp
, SIP_EXPECT_SIGNALLING
, nf_ct_l3num(ct
),
1272 saddr
, &daddr
, proto
, NULL
, &port
);
1273 exp
->timeout
.expires
= sip_timeout
* HZ
;
1274 exp
->helper
= nfct_help(ct
)->helper
;
1275 exp
->flags
= NF_CT_EXPECT_PERMANENT
| NF_CT_EXPECT_INACTIVE
;
1277 nf_nat_sip_expect
= rcu_dereference(nf_nat_sip_expect_hook
);
1278 if (nf_nat_sip_expect
&& ct
->status
& IPS_NAT_MASK
)
1279 ret
= nf_nat_sip_expect(skb
, dataoff
, dptr
, datalen
, exp
,
1280 matchoff
, matchlen
);
1282 if (nf_ct_expect_related(exp
) != 0)
1287 nf_ct_expect_put(exp
);
1290 if (ret
== NF_ACCEPT
)
1291 ct_sip_info
->register_cseq
= cseq
;
1295 static int process_register_response(struct sk_buff
*skb
, unsigned int dataoff
,
1296 const char **dptr
, unsigned int *datalen
,
1297 unsigned int cseq
, unsigned int code
)
1299 enum ip_conntrack_info ctinfo
;
1300 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1301 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1302 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
1303 union nf_inet_addr addr
;
1306 unsigned int matchoff
, matchlen
, coff
= 0;
1307 unsigned int expires
= 0;
1308 int in_contact
= 0, ret
;
1310 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1311 * they have received a final response from the registrar for the
1312 * previous one or the previous REGISTER request has timed out".
1314 * However, some servers fail to detect retransmissions and send late
1315 * responses, so we store the sequence number of the last valid
1316 * request and compare it here.
1318 if (ct_sip_info
->register_cseq
!= cseq
)
1321 if (code
>= 100 && code
<= 199)
1323 if (code
< 200 || code
> 299)
1326 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_EXPIRES
,
1327 &matchoff
, &matchlen
) > 0)
1328 expires
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1331 unsigned int c_expires
= expires
;
1333 ret
= ct_sip_parse_header_uri(ct
, *dptr
, &coff
, *datalen
,
1334 SIP_HDR_CONTACT
, &in_contact
,
1335 &matchoff
, &matchlen
,
1342 /* We don't support third-party registrations */
1343 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.dst
.u3
, &addr
))
1346 if (ct_sip_parse_transport(ct
, *dptr
, matchoff
+ matchlen
,
1347 *datalen
, &proto
) == 0)
1350 ret
= ct_sip_parse_numerical_param(ct
, *dptr
,
1351 matchoff
+ matchlen
,
1352 *datalen
, "expires=",
1353 NULL
, NULL
, &c_expires
);
1358 if (refresh_signalling_expectation(ct
, &addr
, proto
, port
,
1364 flush_expectations(ct
, false);
1368 static const struct sip_handler sip_handlers
[] = {
1369 SIP_HANDLER("INVITE", process_invite_request
, process_invite_response
),
1370 SIP_HANDLER("UPDATE", process_sdp
, process_update_response
),
1371 SIP_HANDLER("ACK", process_sdp
, NULL
),
1372 SIP_HANDLER("PRACK", process_sdp
, process_prack_response
),
1373 SIP_HANDLER("BYE", process_bye_request
, NULL
),
1374 SIP_HANDLER("REGISTER", process_register_request
, process_register_response
),
1377 static int process_sip_response(struct sk_buff
*skb
, unsigned int dataoff
,
1378 const char **dptr
, unsigned int *datalen
)
1380 enum ip_conntrack_info ctinfo
;
1381 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1382 unsigned int matchoff
, matchlen
, matchend
;
1383 unsigned int code
, cseq
, i
;
1385 if (*datalen
< strlen("SIP/2.0 200"))
1387 code
= simple_strtoul(*dptr
+ strlen("SIP/2.0 "), NULL
, 10);
1391 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_CSEQ
,
1392 &matchoff
, &matchlen
) <= 0)
1394 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1397 matchend
= matchoff
+ matchlen
+ 1;
1399 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1400 const struct sip_handler
*handler
;
1402 handler
= &sip_handlers
[i
];
1403 if (handler
->response
== NULL
)
1405 if (*datalen
< matchend
+ handler
->len
||
1406 strnicmp(*dptr
+ matchend
, handler
->method
, handler
->len
))
1408 return handler
->response(skb
, dataoff
, dptr
, datalen
,
1414 static int process_sip_request(struct sk_buff
*skb
, unsigned int dataoff
,
1415 const char **dptr
, unsigned int *datalen
)
1417 enum ip_conntrack_info ctinfo
;
1418 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1419 unsigned int matchoff
, matchlen
;
1420 unsigned int cseq
, i
;
1422 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1423 const struct sip_handler
*handler
;
1425 handler
= &sip_handlers
[i
];
1426 if (handler
->request
== NULL
)
1428 if (*datalen
< handler
->len
||
1429 strnicmp(*dptr
, handler
->method
, handler
->len
))
1432 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_CSEQ
,
1433 &matchoff
, &matchlen
) <= 0)
1435 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1439 return handler
->request(skb
, dataoff
, dptr
, datalen
, cseq
);
1444 static int process_sip_msg(struct sk_buff
*skb
, struct nf_conn
*ct
,
1445 unsigned int dataoff
, const char **dptr
,
1446 unsigned int *datalen
)
1448 typeof(nf_nat_sip_hook
) nf_nat_sip
;
1451 if (strnicmp(*dptr
, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1452 ret
= process_sip_request(skb
, dataoff
, dptr
, datalen
);
1454 ret
= process_sip_response(skb
, dataoff
, dptr
, datalen
);
1456 if (ret
== NF_ACCEPT
&& ct
->status
& IPS_NAT_MASK
) {
1457 nf_nat_sip
= rcu_dereference(nf_nat_sip_hook
);
1458 if (nf_nat_sip
&& !nf_nat_sip(skb
, dataoff
, dptr
, datalen
))
1465 static int sip_help_tcp(struct sk_buff
*skb
, unsigned int protoff
,
1466 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1468 struct tcphdr
*th
, _tcph
;
1469 unsigned int dataoff
, datalen
;
1470 unsigned int matchoff
, matchlen
, clen
;
1471 unsigned int msglen
, origlen
;
1472 const char *dptr
, *end
;
1473 s16 diff
, tdiff
= 0;
1474 int ret
= NF_ACCEPT
;
1476 typeof(nf_nat_sip_seq_adjust_hook
) nf_nat_sip_seq_adjust
;
1478 if (ctinfo
!= IP_CT_ESTABLISHED
&&
1479 ctinfo
!= IP_CT_ESTABLISHED_REPLY
)
1483 th
= skb_header_pointer(skb
, protoff
, sizeof(_tcph
), &_tcph
);
1486 dataoff
= protoff
+ th
->doff
* 4;
1487 if (dataoff
>= skb
->len
)
1490 nf_ct_refresh(ct
, skb
, sip_timeout
* HZ
);
1492 if (unlikely(skb_linearize(skb
)))
1495 dptr
= skb
->data
+ dataoff
;
1496 datalen
= skb
->len
- dataoff
;
1497 if (datalen
< strlen("SIP/2.0 200"))
1501 if (ct_sip_get_header(ct
, dptr
, 0, datalen
,
1502 SIP_HDR_CONTENT_LENGTH
,
1503 &matchoff
, &matchlen
) <= 0)
1506 clen
= simple_strtoul(dptr
+ matchoff
, (char **)&end
, 10);
1507 if (dptr
+ matchoff
== end
)
1511 for (; end
+ strlen("\r\n\r\n") <= dptr
+ datalen
; end
++) {
1512 if (end
[0] == '\r' && end
[1] == '\n' &&
1513 end
[2] == '\r' && end
[3] == '\n') {
1520 end
+= strlen("\r\n\r\n") + clen
;
1522 msglen
= origlen
= end
- dptr
;
1523 if (msglen
> datalen
)
1526 ret
= process_sip_msg(skb
, ct
, dataoff
, &dptr
, &msglen
);
1527 if (ret
!= NF_ACCEPT
)
1529 diff
= msglen
- origlen
;
1534 datalen
= datalen
+ diff
- msglen
;
1537 if (ret
== NF_ACCEPT
&& ct
->status
& IPS_NAT_MASK
) {
1538 nf_nat_sip_seq_adjust
= rcu_dereference(nf_nat_sip_seq_adjust_hook
);
1539 if (nf_nat_sip_seq_adjust
)
1540 nf_nat_sip_seq_adjust(skb
, tdiff
);
1546 static int sip_help_udp(struct sk_buff
*skb
, unsigned int protoff
,
1547 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1549 unsigned int dataoff
, datalen
;
1553 dataoff
= protoff
+ sizeof(struct udphdr
);
1554 if (dataoff
>= skb
->len
)
1557 nf_ct_refresh(ct
, skb
, sip_timeout
* HZ
);
1559 if (unlikely(skb_linearize(skb
)))
1562 dptr
= skb
->data
+ dataoff
;
1563 datalen
= skb
->len
- dataoff
;
1564 if (datalen
< strlen("SIP/2.0 200"))
1567 return process_sip_msg(skb
, ct
, dataoff
, &dptr
, &datalen
);
1570 static struct nf_conntrack_helper sip
[MAX_PORTS
][4] __read_mostly
;
1572 static const struct nf_conntrack_expect_policy sip_exp_policy
[SIP_EXPECT_MAX
+ 1] = {
1573 [SIP_EXPECT_SIGNALLING
] = {
1574 .name
= "signalling",
1578 [SIP_EXPECT_AUDIO
] = {
1580 .max_expected
= 2 * IP_CT_DIR_MAX
,
1583 [SIP_EXPECT_VIDEO
] = {
1585 .max_expected
= 2 * IP_CT_DIR_MAX
,
1588 [SIP_EXPECT_IMAGE
] = {
1590 .max_expected
= IP_CT_DIR_MAX
,
1595 static void nf_conntrack_sip_fini(void)
1599 for (i
= 0; i
< ports_c
; i
++) {
1600 for (j
= 0; j
< ARRAY_SIZE(sip
[i
]); j
++) {
1601 if (sip
[i
][j
].me
== NULL
)
1603 nf_conntrack_helper_unregister(&sip
[i
][j
]);
1608 static int __init
nf_conntrack_sip_init(void)
1613 ports
[ports_c
++] = SIP_PORT
;
1615 for (i
= 0; i
< ports_c
; i
++) {
1616 memset(&sip
[i
], 0, sizeof(sip
[i
]));
1618 sip
[i
][0].tuple
.src
.l3num
= AF_INET
;
1619 sip
[i
][0].tuple
.dst
.protonum
= IPPROTO_UDP
;
1620 sip
[i
][0].help
= sip_help_udp
;
1621 sip
[i
][1].tuple
.src
.l3num
= AF_INET
;
1622 sip
[i
][1].tuple
.dst
.protonum
= IPPROTO_TCP
;
1623 sip
[i
][1].help
= sip_help_tcp
;
1625 sip
[i
][2].tuple
.src
.l3num
= AF_INET6
;
1626 sip
[i
][2].tuple
.dst
.protonum
= IPPROTO_UDP
;
1627 sip
[i
][2].help
= sip_help_udp
;
1628 sip
[i
][3].tuple
.src
.l3num
= AF_INET6
;
1629 sip
[i
][3].tuple
.dst
.protonum
= IPPROTO_TCP
;
1630 sip
[i
][3].help
= sip_help_tcp
;
1632 for (j
= 0; j
< ARRAY_SIZE(sip
[i
]); j
++) {
1633 sip
[i
][j
].data_len
= sizeof(struct nf_ct_sip_master
);
1634 sip
[i
][j
].tuple
.src
.u
.udp
.port
= htons(ports
[i
]);
1635 sip
[i
][j
].expect_policy
= sip_exp_policy
;
1636 sip
[i
][j
].expect_class_max
= SIP_EXPECT_MAX
;
1637 sip
[i
][j
].me
= THIS_MODULE
;
1639 if (ports
[i
] == SIP_PORT
)
1640 sprintf(sip
[i
][j
].name
, "sip");
1642 sprintf(sip
[i
][j
].name
, "sip-%u", i
);
1644 pr_debug("port #%u: %u\n", i
, ports
[i
]);
1646 ret
= nf_conntrack_helper_register(&sip
[i
][j
]);
1648 printk(KERN_ERR
"nf_ct_sip: failed to register"
1649 " helper for pf: %u port: %u\n",
1650 sip
[i
][j
].tuple
.src
.l3num
, ports
[i
]);
1651 nf_conntrack_sip_fini();
1659 module_init(nf_conntrack_sip_init
);
1660 module_exit(nf_conntrack_sip_fini
);