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.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/ctype.h>
13 #include <linux/skbuff.h>
14 #include <linux/inet.h>
16 #include <linux/udp.h>
17 #include <linux/netfilter.h>
19 #include <net/netfilter/nf_conntrack.h>
20 #include <net/netfilter/nf_conntrack_core.h>
21 #include <net/netfilter/nf_conntrack_expect.h>
22 #include <net/netfilter/nf_conntrack_helper.h>
23 #include <linux/netfilter/nf_conntrack_sip.h>
25 MODULE_LICENSE("GPL");
26 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
27 MODULE_DESCRIPTION("SIP connection tracking helper");
28 MODULE_ALIAS("ip_conntrack_sip");
31 static unsigned short ports[MAX_PORTS];
32 static unsigned int ports_c;
33 module_param_array(ports, ushort, &ports_c, 0400);
34 MODULE_PARM_DESC(ports, "port numbers of SIP servers");
36 static unsigned int sip_timeout __read_mostly = SIP_TIMEOUT;
37 module_param(sip_timeout, uint, 0600);
38 MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session");
40 static int sip_direct_signalling __read_mostly = 1;
41 module_param(sip_direct_signalling, int, 0600);
42 MODULE_PARM_DESC(sip_direct_signalling, "expect incoming calls from registrar "
45 static int sip_direct_media __read_mostly = 1;
46 module_param(sip_direct_media, int, 0600);
47 MODULE_PARM_DESC(sip_direct_media, "Expect Media streams between signalling "
48 "endpoints only (default 1)");
50 unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb,
52 unsigned int *datalen) __read_mostly;
53 EXPORT_SYMBOL_GPL(nf_nat_sip_hook);
55 unsigned int (*nf_nat_sip_expect_hook)(struct sk_buff *skb,
57 unsigned int *datalen,
58 struct nf_conntrack_expect *exp,
59 unsigned int matchoff,
60 unsigned int matchlen) __read_mostly;
61 EXPORT_SYMBOL_GPL(nf_nat_sip_expect_hook);
63 unsigned int (*nf_nat_sdp_addr_hook)(struct sk_buff *skb,
66 unsigned int *datalen,
67 enum sdp_header_types type,
68 enum sdp_header_types term,
69 const union nf_inet_addr *addr)
71 EXPORT_SYMBOL_GPL(nf_nat_sdp_addr_hook);
73 unsigned int (*nf_nat_sdp_session_hook)(struct sk_buff *skb,
76 unsigned int *datalen,
77 const union nf_inet_addr *addr)
79 EXPORT_SYMBOL_GPL(nf_nat_sdp_session_hook);
81 unsigned int (*nf_nat_sdp_media_hook)(struct sk_buff *skb,
83 unsigned int *datalen,
84 struct nf_conntrack_expect *rtp_exp,
85 struct nf_conntrack_expect *rtcp_exp,
86 unsigned int mediaoff,
87 unsigned int medialen,
88 union nf_inet_addr *rtp_addr)
90 EXPORT_SYMBOL_GPL(nf_nat_sdp_media_hook);
92 static int string_len(const struct nf_conn *ct, const char *dptr,
93 const char *limit, int *shift)
97 while (dptr < limit && isalpha(*dptr)) {
104 static int digits_len(const struct nf_conn *ct, const char *dptr,
105 const char *limit, int *shift)
108 while (dptr < limit && isdigit(*dptr)) {
115 static int parse_addr(const struct nf_conn *ct, const char *cp,
116 const char **endp, union nf_inet_addr *addr,
120 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
125 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
128 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
134 if (ret == 0 || end == cp)
141 /* skip ip address. returns its length. */
142 static int epaddr_len(const struct nf_conn *ct, const char *dptr,
143 const char *limit, int *shift)
145 union nf_inet_addr addr;
146 const char *aux = dptr;
148 if (!parse_addr(ct, dptr, &dptr, &addr, limit)) {
149 pr_debug("ip: %s parse failed.!\n", dptr);
156 dptr += digits_len(ct, dptr, limit, shift);
161 /* get address length, skiping user info. */
162 static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
163 const char *limit, int *shift)
165 const char *start = dptr;
168 /* Search for @, but stop at the end of the line.
169 * We are inside a sip: URI, so we don't need to worry about
170 * continuation lines. */
171 while (dptr < limit &&
172 *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
177 if (dptr < limit && *dptr == '@') {
185 return epaddr_len(ct, dptr, limit, shift);
188 /* Parse a SIP request line of the form:
190 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
192 * and return the offset and length of the address contained in the Request-URI.
194 int ct_sip_parse_request(const struct nf_conn *ct,
195 const char *dptr, unsigned int datalen,
196 unsigned int *matchoff, unsigned int *matchlen,
197 union nf_inet_addr *addr, __be16 *port)
199 const char *start = dptr, *limit = dptr + datalen, *end;
204 /* Skip method and following whitespace */
205 mlen = string_len(ct, dptr, limit, NULL);
213 limit -= strlen("sip:");
214 for (; dptr < limit; dptr++) {
215 if (*dptr == '\r' || *dptr == '\n')
217 if (strnicmp(dptr, "sip:", strlen("sip:")) == 0)
220 if (!skp_epaddr_len(ct, dptr, limit, &shift))
224 if (!parse_addr(ct, dptr, &end, addr, limit))
226 if (end < limit && *end == ':') {
228 p = simple_strtoul(end, (char **)&end, 10);
229 if (p < 1024 || p > 65535)
233 *port = htons(SIP_PORT);
237 *matchoff = dptr - start;
238 *matchlen = end - dptr;
241 EXPORT_SYMBOL_GPL(ct_sip_parse_request);
243 /* SIP header parsing: SIP headers are located at the beginning of a line, but
244 * may span several lines, in which case the continuation lines begin with a
245 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
246 * CRLF, RFC 3261 allows only CRLF, we support both.
248 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
249 * whitespace and the values. Whitespace in this context means any amount of
250 * tabs, spaces and continuation lines, which are treated as a single whitespace
253 * Some headers may appear multiple times. A comma seperated list of values is
254 * equivalent to multiple headers.
256 static const struct sip_header ct_sip_hdrs[] = {
257 [SIP_HDR_CSEQ] = SIP_HDR("CSeq", NULL, NULL, digits_len),
258 [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len),
259 [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len),
260 [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len),
261 [SIP_HDR_VIA] = SIP_HDR("Via", "v", "UDP ", epaddr_len),
262 [SIP_HDR_EXPIRES] = SIP_HDR("Expires", NULL, NULL, digits_len),
263 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len),
266 static const char *sip_follow_continuation(const char *dptr, const char *limit)
268 /* Walk past newline */
272 /* Skip '\n' in CR LF */
273 if (*(dptr - 1) == '\r' && *dptr == '\n') {
278 /* Continuation line? */
279 if (*dptr != ' ' && *dptr != '\t')
282 /* skip leading whitespace */
283 for (; dptr < limit; dptr++) {
284 if (*dptr != ' ' && *dptr != '\t')
290 static const char *sip_skip_whitespace(const char *dptr, const char *limit)
292 for (; dptr < limit; dptr++) {
295 if (*dptr != '\r' && *dptr != '\n')
297 dptr = sip_follow_continuation(dptr, limit);
304 /* Search within a SIP header value, dealing with continuation lines */
305 static const char *ct_sip_header_search(const char *dptr, const char *limit,
306 const char *needle, unsigned int len)
308 for (limit -= len; dptr < limit; dptr++) {
309 if (*dptr == '\r' || *dptr == '\n') {
310 dptr = sip_follow_continuation(dptr, limit);
316 if (strnicmp(dptr, needle, len) == 0)
322 int ct_sip_get_header(const struct nf_conn *ct, const char *dptr,
323 unsigned int dataoff, unsigned int datalen,
324 enum sip_header_types type,
325 unsigned int *matchoff, unsigned int *matchlen)
327 const struct sip_header *hdr = &ct_sip_hdrs[type];
328 const char *start = dptr, *limit = dptr + datalen;
331 for (dptr += dataoff; dptr < limit; dptr++) {
332 /* Find beginning of line */
333 if (*dptr != '\r' && *dptr != '\n')
337 if (*(dptr - 1) == '\r' && *dptr == '\n') {
342 /* Skip continuation lines */
343 if (*dptr == ' ' || *dptr == '\t')
346 /* Find header. Compact headers must be followed by a
347 * non-alphabetic character to avoid mismatches. */
348 if (limit - dptr >= hdr->len &&
349 strnicmp(dptr, hdr->name, hdr->len) == 0)
351 else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
352 strnicmp(dptr, hdr->cname, hdr->clen) == 0 &&
353 !isalpha(*(dptr + hdr->clen + 1)))
358 /* Find and skip colon */
359 dptr = sip_skip_whitespace(dptr, limit);
362 if (*dptr != ':' || ++dptr >= limit)
365 /* Skip whitespace after colon */
366 dptr = sip_skip_whitespace(dptr, limit);
370 *matchoff = dptr - start;
372 dptr = ct_sip_header_search(dptr, limit, hdr->search,
379 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
382 *matchoff = dptr - start + shift;
387 EXPORT_SYMBOL_GPL(ct_sip_get_header);
389 /* Get next header field in a list of comma seperated values */
390 static int ct_sip_next_header(const struct nf_conn *ct, const char *dptr,
391 unsigned int dataoff, unsigned int datalen,
392 enum sip_header_types type,
393 unsigned int *matchoff, unsigned int *matchlen)
395 const struct sip_header *hdr = &ct_sip_hdrs[type];
396 const char *start = dptr, *limit = dptr + datalen;
401 dptr = ct_sip_header_search(dptr, limit, ",", strlen(","));
405 dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen);
410 *matchoff = dptr - start;
411 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
418 /* Walk through headers until a parsable one is found or no header of the
419 * given type is left. */
420 static int ct_sip_walk_headers(const struct nf_conn *ct, const char *dptr,
421 unsigned int dataoff, unsigned int datalen,
422 enum sip_header_types type, int *in_header,
423 unsigned int *matchoff, unsigned int *matchlen)
427 if (in_header && *in_header) {
429 ret = ct_sip_next_header(ct, dptr, dataoff, datalen,
430 type, matchoff, matchlen);
435 dataoff += *matchoff;
441 ret = ct_sip_get_header(ct, dptr, dataoff, datalen,
442 type, matchoff, matchlen);
447 dataoff += *matchoff;
455 /* Locate a SIP header, parse the URI and return the offset and length of
456 * the address as well as the address and port themselves. A stream of
457 * headers can be parsed by handing in a non-NULL datalen and in_header
460 int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
461 unsigned int *dataoff, unsigned int datalen,
462 enum sip_header_types type, int *in_header,
463 unsigned int *matchoff, unsigned int *matchlen,
464 union nf_inet_addr *addr, __be16 *port)
466 const char *c, *limit = dptr + datalen;
470 ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
471 type, in_header, matchoff, matchlen);
476 if (!parse_addr(ct, dptr + *matchoff, &c, addr, limit))
480 p = simple_strtoul(c, (char **)&c, 10);
481 if (p < 1024 || p > 65535)
485 *port = htons(SIP_PORT);
491 EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri);
493 /* Parse address from header parameter and return address, offset and length */
494 int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr,
495 unsigned int dataoff, unsigned int datalen,
497 unsigned int *matchoff, unsigned int *matchlen,
498 union nf_inet_addr *addr)
500 const char *limit = dptr + datalen;
501 const char *start, *end;
503 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
505 limit = dptr + datalen;
507 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
511 start += strlen(name);
512 if (!parse_addr(ct, start, &end, addr, limit))
514 *matchoff = start - dptr;
515 *matchlen = end - start;
518 EXPORT_SYMBOL_GPL(ct_sip_parse_address_param);
520 /* Parse numerical header parameter and return value, offset and length */
521 int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr,
522 unsigned int dataoff, unsigned int datalen,
524 unsigned int *matchoff, unsigned int *matchlen,
527 const char *limit = dptr + datalen;
531 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
533 limit = dptr + datalen;
535 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
539 start += strlen(name);
540 *val = simple_strtoul(start, &end, 0);
543 if (matchoff && matchlen) {
544 *matchoff = start - dptr;
545 *matchlen = end - start;
549 EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param);
551 /* SDP header parsing: a SDP session description contains an ordered set of
552 * headers, starting with a section containing general session parameters,
553 * optionally followed by multiple media descriptions.
555 * SDP headers always start at the beginning of a line. According to RFC 2327:
556 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
557 * be tolerant and also accept records terminated with a single newline
558 * character". We handle both cases.
560 static const struct sip_header ct_sdp_hdrs[] = {
561 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
562 [SDP_HDR_OWNER_IP4] = SDP_HDR("o=", "IN IP4 ", epaddr_len),
563 [SDP_HDR_CONNECTION_IP4] = SDP_HDR("c=", "IN IP4 ", epaddr_len),
564 [SDP_HDR_OWNER_IP6] = SDP_HDR("o=", "IN IP6 ", epaddr_len),
565 [SDP_HDR_CONNECTION_IP6] = SDP_HDR("c=", "IN IP6 ", epaddr_len),
566 [SDP_HDR_MEDIA] = SDP_HDR("m=", "audio ", digits_len),
569 /* Linear string search within SDP header values */
570 static const char *ct_sdp_header_search(const char *dptr, const char *limit,
571 const char *needle, unsigned int len)
573 for (limit -= len; dptr < limit; dptr++) {
574 if (*dptr == '\r' || *dptr == '\n')
576 if (strncmp(dptr, needle, len) == 0)
582 /* Locate a SDP header (optionally a substring within the header value),
583 * optionally stopping at the first occurence of the term header, parse
584 * it and return the offset and length of the data we're interested in.
586 int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr,
587 unsigned int dataoff, unsigned int datalen,
588 enum sdp_header_types type,
589 enum sdp_header_types term,
590 unsigned int *matchoff, unsigned int *matchlen)
592 const struct sip_header *hdr = &ct_sdp_hdrs[type];
593 const struct sip_header *thdr = &ct_sdp_hdrs[term];
594 const char *start = dptr, *limit = dptr + datalen;
597 for (dptr += dataoff; dptr < limit; dptr++) {
598 /* Find beginning of line */
599 if (*dptr != '\r' && *dptr != '\n')
603 if (*(dptr - 1) == '\r' && *dptr == '\n') {
608 if (term != SDP_HDR_UNSPEC &&
609 limit - dptr >= thdr->len &&
610 strnicmp(dptr, thdr->name, thdr->len) == 0)
612 else if (limit - dptr >= hdr->len &&
613 strnicmp(dptr, hdr->name, hdr->len) == 0)
618 *matchoff = dptr - start;
620 dptr = ct_sdp_header_search(dptr, limit, hdr->search,
627 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
630 *matchoff = dptr - start + shift;
635 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header);
637 static int ct_sip_parse_sdp_addr(const struct nf_conn *ct, const char *dptr,
638 unsigned int dataoff, unsigned int datalen,
639 enum sdp_header_types type,
640 enum sdp_header_types term,
641 unsigned int *matchoff, unsigned int *matchlen,
642 union nf_inet_addr *addr)
646 ret = ct_sip_get_sdp_header(ct, dptr, dataoff, datalen, type, term,
651 if (!parse_addr(ct, dptr + *matchoff, NULL, addr,
652 dptr + *matchoff + *matchlen))
657 static int refresh_signalling_expectation(struct nf_conn *ct,
658 union nf_inet_addr *addr,
660 unsigned int expires)
662 struct nf_conn_help *help = nfct_help(ct);
663 struct nf_conntrack_expect *exp;
664 struct hlist_node *n, *next;
667 spin_lock_bh(&nf_conntrack_lock);
668 hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) {
669 if (exp->class != SIP_EXPECT_SIGNALLING ||
670 !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) ||
671 exp->tuple.dst.u.udp.port != port)
673 if (!del_timer(&exp->timeout))
675 exp->flags &= ~NF_CT_EXPECT_INACTIVE;
676 exp->timeout.expires = jiffies + expires * HZ;
677 add_timer(&exp->timeout);
681 spin_unlock_bh(&nf_conntrack_lock);
685 static void flush_expectations(struct nf_conn *ct, bool media)
687 struct nf_conn_help *help = nfct_help(ct);
688 struct nf_conntrack_expect *exp;
689 struct hlist_node *n, *next;
691 spin_lock_bh(&nf_conntrack_lock);
692 hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) {
693 if ((exp->class != SIP_EXPECT_SIGNALLING) ^ media)
695 if (!del_timer(&exp->timeout))
697 nf_ct_unlink_expect(exp);
698 nf_ct_expect_put(exp);
702 spin_unlock_bh(&nf_conntrack_lock);
705 static int set_expected_rtp_rtcp(struct sk_buff *skb,
706 const char **dptr, unsigned int *datalen,
707 union nf_inet_addr *daddr, __be16 port,
708 unsigned int mediaoff, unsigned int medialen)
710 struct nf_conntrack_expect *exp, *rtp_exp, *rtcp_exp;
711 enum ip_conntrack_info ctinfo;
712 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
713 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
714 union nf_inet_addr *saddr;
715 struct nf_conntrack_tuple tuple;
716 int family = ct->tuplehash[!dir].tuple.src.l3num;
717 int skip_expect = 0, ret = NF_DROP;
719 __be16 rtp_port, rtcp_port;
720 typeof(nf_nat_sdp_media_hook) nf_nat_sdp_media;
723 if (sip_direct_media) {
724 if (!nf_inet_addr_cmp(daddr, &ct->tuplehash[dir].tuple.src.u3))
726 saddr = &ct->tuplehash[!dir].tuple.src.u3;
729 /* We need to check whether the registration exists before attempting
730 * to register it since we can see the same media description multiple
731 * times on different connections in case multiple endpoints receive
734 memset(&tuple, 0, sizeof(tuple));
736 tuple.src.u3 = *saddr;
737 tuple.src.l3num = family;
738 tuple.dst.protonum = IPPROTO_UDP;
739 tuple.dst.u3 = *daddr;
740 tuple.dst.u.udp.port = port;
743 exp = __nf_ct_expect_find(&tuple);
744 if (exp && exp->master != ct &&
745 nfct_help(exp->master)->helper == nfct_help(ct)->helper &&
746 exp->class == SIP_EXPECT_AUDIO)
753 base_port = ntohs(tuple.dst.u.udp.port) & ~1;
754 rtp_port = htons(base_port);
755 rtcp_port = htons(base_port + 1);
757 rtp_exp = nf_ct_expect_alloc(ct);
760 nf_ct_expect_init(rtp_exp, SIP_EXPECT_AUDIO, family, saddr, daddr,
761 IPPROTO_UDP, NULL, &rtp_port);
763 rtcp_exp = nf_ct_expect_alloc(ct);
764 if (rtcp_exp == NULL)
766 nf_ct_expect_init(rtcp_exp, SIP_EXPECT_AUDIO, family, saddr, daddr,
767 IPPROTO_UDP, NULL, &rtcp_port);
769 nf_nat_sdp_media = rcu_dereference(nf_nat_sdp_media_hook);
770 if (nf_nat_sdp_media && ct->status & IPS_NAT_MASK)
771 ret = nf_nat_sdp_media(skb, dptr, datalen, rtp_exp, rtcp_exp,
772 mediaoff, medialen, daddr);
774 if (nf_ct_expect_related(rtp_exp) == 0) {
775 if (nf_ct_expect_related(rtcp_exp) != 0)
776 nf_ct_unexpect_related(rtp_exp);
781 nf_ct_expect_put(rtcp_exp);
783 nf_ct_expect_put(rtp_exp);
788 static int process_sdp(struct sk_buff *skb,
789 const char **dptr, unsigned int *datalen,
792 enum ip_conntrack_info ctinfo;
793 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
794 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
795 unsigned int matchoff, matchlen;
796 unsigned int mediaoff, medialen;
798 unsigned int caddr_len, maddr_len;
799 union nf_inet_addr caddr, maddr, rtp_addr;
801 enum sdp_header_types c_hdr;
803 typeof(nf_nat_sdp_addr_hook) nf_nat_sdp_addr;
804 typeof(nf_nat_sdp_session_hook) nf_nat_sdp_session;
806 c_hdr = family == AF_INET ? SDP_HDR_CONNECTION_IP4 :
807 SDP_HDR_CONNECTION_IP6;
809 /* Find beginning of session description */
810 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
811 SDP_HDR_VERSION, SDP_HDR_UNSPEC,
812 &matchoff, &matchlen) <= 0)
816 /* The connection information is contained in the session description
817 * and/or once per media description. The first media description marks
818 * the end of the session description. */
820 if (ct_sip_parse_sdp_addr(ct, *dptr, sdpoff, *datalen,
821 c_hdr, SDP_HDR_MEDIA,
822 &matchoff, &matchlen, &caddr) > 0)
823 caddr_len = matchlen;
825 if (ct_sip_get_sdp_header(ct, *dptr, sdpoff, *datalen,
826 SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
827 &mediaoff, &medialen) <= 0)
830 port = simple_strtoul(*dptr + mediaoff, NULL, 10);
831 if (port < 1024 || port > 65535)
834 /* The media description overrides the session description. */
836 if (ct_sip_parse_sdp_addr(ct, *dptr, mediaoff, *datalen,
837 c_hdr, SDP_HDR_MEDIA,
838 &matchoff, &matchlen, &maddr) > 0) {
839 maddr_len = matchlen;
840 memcpy(&rtp_addr, &maddr, sizeof(rtp_addr));
841 } else if (caddr_len)
842 memcpy(&rtp_addr, &caddr, sizeof(rtp_addr));
846 ret = set_expected_rtp_rtcp(skb, dptr, datalen, &rtp_addr, htons(port),
848 if (ret != NF_ACCEPT)
851 /* Update media connection address if present */
853 nf_nat_sdp_addr = rcu_dereference(nf_nat_sdp_addr_hook);
854 if (nf_nat_sdp_addr && ct->status & IPS_NAT_MASK) {
855 ret = nf_nat_sdp_addr(skb, dptr, mediaoff, datalen,
856 c_hdr, SDP_HDR_MEDIA, &rtp_addr);
857 if (ret != NF_ACCEPT)
862 /* Update session connection and owner addresses */
863 nf_nat_sdp_session = rcu_dereference(nf_nat_sdp_session_hook);
864 if (nf_nat_sdp_session && ct->status & IPS_NAT_MASK)
865 ret = nf_nat_sdp_session(skb, dptr, sdpoff, datalen, &rtp_addr);
869 static int process_invite_response(struct sk_buff *skb,
870 const char **dptr, unsigned int *datalen,
871 unsigned int cseq, unsigned int code)
873 enum ip_conntrack_info ctinfo;
874 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
876 if ((code >= 100 && code <= 199) ||
877 (code >= 200 && code <= 299))
878 return process_sdp(skb, dptr, datalen, cseq);
880 flush_expectations(ct, true);
885 static int process_update_response(struct sk_buff *skb,
886 const char **dptr, unsigned int *datalen,
887 unsigned int cseq, unsigned int code)
889 enum ip_conntrack_info ctinfo;
890 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
892 if ((code >= 100 && code <= 199) ||
893 (code >= 200 && code <= 299))
894 return process_sdp(skb, dptr, datalen, cseq);
896 flush_expectations(ct, true);
901 static int process_prack_response(struct sk_buff *skb,
902 const char **dptr, unsigned int *datalen,
903 unsigned int cseq, unsigned int code)
905 enum ip_conntrack_info ctinfo;
906 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
908 if ((code >= 100 && code <= 199) ||
909 (code >= 200 && code <= 299))
910 return process_sdp(skb, dptr, datalen, cseq);
912 flush_expectations(ct, true);
917 static int process_bye_request(struct sk_buff *skb,
918 const char **dptr, unsigned int *datalen,
921 enum ip_conntrack_info ctinfo;
922 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
924 flush_expectations(ct, true);
928 /* Parse a REGISTER request and create a permanent expectation for incoming
929 * signalling connections. The expectation is marked inactive and is activated
930 * when receiving a response indicating success from the registrar.
932 static int process_register_request(struct sk_buff *skb,
933 const char **dptr, unsigned int *datalen,
936 enum ip_conntrack_info ctinfo;
937 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
938 struct nf_conn_help *help = nfct_help(ct);
939 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
940 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
941 unsigned int matchoff, matchlen;
942 struct nf_conntrack_expect *exp;
943 union nf_inet_addr *saddr, daddr;
945 unsigned int expires = 0;
947 typeof(nf_nat_sip_expect_hook) nf_nat_sip_expect;
949 /* Expected connections can not register again. */
950 if (ct->status & IPS_EXPECTED)
953 /* We must check the expiration time: a value of zero signals the
954 * registrar to release the binding. We'll remove our expectation
955 * when receiving the new bindings in the response, but we don't
956 * want to create new ones.
958 * The expiration time may be contained in Expires: header, the
959 * Contact: header parameters or the URI parameters.
961 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
962 &matchoff, &matchlen) > 0)
963 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
965 ret = ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
966 SIP_HDR_CONTACT, NULL,
967 &matchoff, &matchlen, &daddr, &port);
973 /* We don't support third-party registrations */
974 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr))
977 if (ct_sip_parse_numerical_param(ct, *dptr,
978 matchoff + matchlen, *datalen,
979 "expires=", NULL, NULL, &expires) < 0)
987 exp = nf_ct_expect_alloc(ct);
992 if (sip_direct_signalling)
993 saddr = &ct->tuplehash[!dir].tuple.src.u3;
995 nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, family, saddr, &daddr,
996 IPPROTO_UDP, NULL, &port);
997 exp->timeout.expires = sip_timeout * HZ;
998 exp->helper = nfct_help(ct)->helper;
999 exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE;
1001 nf_nat_sip_expect = rcu_dereference(nf_nat_sip_expect_hook);
1002 if (nf_nat_sip_expect && ct->status & IPS_NAT_MASK)
1003 ret = nf_nat_sip_expect(skb, dptr, datalen, exp,
1004 matchoff, matchlen);
1006 if (nf_ct_expect_related(exp) != 0)
1011 nf_ct_expect_put(exp);
1014 if (ret == NF_ACCEPT)
1015 help->help.ct_sip_info.register_cseq = cseq;
1019 static int process_register_response(struct sk_buff *skb,
1020 const char **dptr, unsigned int *datalen,
1021 unsigned int cseq, unsigned int code)
1023 enum ip_conntrack_info ctinfo;
1024 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1025 struct nf_conn_help *help = nfct_help(ct);
1026 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1027 union nf_inet_addr addr;
1029 unsigned int matchoff, matchlen, dataoff = 0;
1030 unsigned int expires = 0;
1031 int in_contact = 0, ret;
1033 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1034 * they have received a final response from the registrar for the
1035 * previous one or the previous REGISTER request has timed out".
1037 * However, some servers fail to detect retransmissions and send late
1038 * responses, so we store the sequence number of the last valid
1039 * request and compare it here.
1041 if (help->help.ct_sip_info.register_cseq != cseq)
1044 if (code >= 100 && code <= 199)
1046 if (code < 200 || code > 299)
1049 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1050 &matchoff, &matchlen) > 0)
1051 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1054 unsigned int c_expires = expires;
1056 ret = ct_sip_parse_header_uri(ct, *dptr, &dataoff, *datalen,
1057 SIP_HDR_CONTACT, &in_contact,
1058 &matchoff, &matchlen,
1065 /* We don't support third-party registrations */
1066 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr))
1069 ret = ct_sip_parse_numerical_param(ct, *dptr,
1070 matchoff + matchlen,
1071 *datalen, "expires=",
1072 NULL, NULL, &c_expires);
1077 if (refresh_signalling_expectation(ct, &addr, port, c_expires))
1082 flush_expectations(ct, false);
1086 static const struct sip_handler sip_handlers[] = {
1087 SIP_HANDLER("INVITE", process_sdp, process_invite_response),
1088 SIP_HANDLER("UPDATE", process_sdp, process_update_response),
1089 SIP_HANDLER("ACK", process_sdp, NULL),
1090 SIP_HANDLER("PRACK", process_sdp, process_prack_response),
1091 SIP_HANDLER("BYE", process_bye_request, NULL),
1092 SIP_HANDLER("REGISTER", process_register_request, process_register_response),
1095 static int process_sip_response(struct sk_buff *skb,
1096 const char **dptr, unsigned int *datalen)
1098 static const struct sip_handler *handler;
1099 enum ip_conntrack_info ctinfo;
1100 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1101 unsigned int matchoff, matchlen;
1102 unsigned int code, cseq, dataoff, i;
1104 if (*datalen < strlen("SIP/2.0 200"))
1106 code = simple_strtoul(*dptr + strlen("SIP/2.0 "), NULL, 10);
1110 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
1111 &matchoff, &matchlen) <= 0)
1113 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
1116 dataoff = matchoff + matchlen + 1;
1118 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
1119 handler = &sip_handlers[i];
1120 if (handler->response == NULL)
1122 if (*datalen < dataoff + handler->len ||
1123 strnicmp(*dptr + dataoff, handler->method, handler->len))
1125 return handler->response(skb, dptr, datalen, cseq, code);
1130 static int process_sip_request(struct sk_buff *skb,
1131 const char **dptr, unsigned int *datalen)
1133 static const struct sip_handler *handler;
1134 enum ip_conntrack_info ctinfo;
1135 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1136 unsigned int matchoff, matchlen;
1137 unsigned int cseq, i;
1139 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
1140 handler = &sip_handlers[i];
1141 if (handler->request == NULL)
1143 if (*datalen < handler->len ||
1144 strnicmp(*dptr, handler->method, handler->len))
1147 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
1148 &matchoff, &matchlen) <= 0)
1150 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
1154 return handler->request(skb, dptr, datalen, cseq);
1159 static int sip_help(struct sk_buff *skb,
1160 unsigned int protoff,
1162 enum ip_conntrack_info ctinfo)
1164 unsigned int dataoff, datalen;
1167 typeof(nf_nat_sip_hook) nf_nat_sip;
1170 dataoff = protoff + sizeof(struct udphdr);
1171 if (dataoff >= skb->len)
1174 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1176 if (!skb_is_nonlinear(skb))
1177 dptr = skb->data + dataoff;
1179 pr_debug("Copy of skbuff not supported yet.\n");
1183 datalen = skb->len - dataoff;
1184 if (datalen < strlen("SIP/2.0 200"))
1187 if (strnicmp(dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1188 ret = process_sip_request(skb, &dptr, &datalen);
1190 ret = process_sip_response(skb, &dptr, &datalen);
1192 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
1193 nf_nat_sip = rcu_dereference(nf_nat_sip_hook);
1194 if (nf_nat_sip && !nf_nat_sip(skb, &dptr, &datalen))
1201 static struct nf_conntrack_helper sip[MAX_PORTS][2] __read_mostly;
1202 static char sip_names[MAX_PORTS][2][sizeof("sip-65535")] __read_mostly;
1204 static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = {
1205 [SIP_EXPECT_SIGNALLING] = {
1209 [SIP_EXPECT_AUDIO] = {
1210 .max_expected = 2 * IP_CT_DIR_MAX,
1215 static void nf_conntrack_sip_fini(void)
1219 for (i = 0; i < ports_c; i++) {
1220 for (j = 0; j < 2; j++) {
1221 if (sip[i][j].me == NULL)
1223 nf_conntrack_helper_unregister(&sip[i][j]);
1228 static int __init nf_conntrack_sip_init(void)
1234 ports[ports_c++] = SIP_PORT;
1236 for (i = 0; i < ports_c; i++) {
1237 memset(&sip[i], 0, sizeof(sip[i]));
1239 sip[i][0].tuple.src.l3num = AF_INET;
1240 sip[i][1].tuple.src.l3num = AF_INET6;
1241 for (j = 0; j < 2; j++) {
1242 sip[i][j].tuple.dst.protonum = IPPROTO_UDP;
1243 sip[i][j].tuple.src.u.udp.port = htons(ports[i]);
1244 sip[i][j].expect_policy = sip_exp_policy;
1245 sip[i][j].expect_class_max = SIP_EXPECT_MAX;
1246 sip[i][j].me = THIS_MODULE;
1247 sip[i][j].help = sip_help;
1249 tmpname = &sip_names[i][j][0];
1250 if (ports[i] == SIP_PORT)
1251 sprintf(tmpname, "sip");
1253 sprintf(tmpname, "sip-%u", i);
1254 sip[i][j].name = tmpname;
1256 pr_debug("port #%u: %u\n", i, ports[i]);
1258 ret = nf_conntrack_helper_register(&sip[i][j]);
1260 printk("nf_ct_sip: failed to register helper "
1261 "for pf: %u port: %u\n",
1262 sip[i][j].tuple.src.l3num, ports[i]);
1263 nf_conntrack_sip_fini();
1271 module_init(nf_conntrack_sip_init);
1272 module_exit(nf_conntrack_sip_fini);