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/netfilter.h>
21 #include <net/netfilter/nf_conntrack.h>
22 #include <net/netfilter/nf_conntrack_core.h>
23 #include <net/netfilter/nf_conntrack_expect.h>
24 #include <net/netfilter/nf_conntrack_helper.h>
25 #include <linux/netfilter/nf_conntrack_sip.h>
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
29 MODULE_DESCRIPTION("SIP connection tracking helper");
30 MODULE_ALIAS("ip_conntrack_sip");
31 MODULE_ALIAS_NFCT_HELPER("sip");
34 static unsigned short ports[MAX_PORTS];
35 static unsigned int ports_c;
36 module_param_array(ports, ushort, &ports_c, 0400);
37 MODULE_PARM_DESC(ports, "port numbers of SIP servers");
39 static unsigned int sip_timeout __read_mostly = SIP_TIMEOUT;
40 module_param(sip_timeout, uint, 0600);
41 MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session");
43 static int sip_direct_signalling __read_mostly = 1;
44 module_param(sip_direct_signalling, int, 0600);
45 MODULE_PARM_DESC(sip_direct_signalling, "expect incoming calls from registrar "
48 static int sip_direct_media __read_mostly = 1;
49 module_param(sip_direct_media, int, 0600);
50 MODULE_PARM_DESC(sip_direct_media, "Expect Media streams between signalling "
51 "endpoints only (default 1)");
53 unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb,
55 unsigned int *datalen) __read_mostly;
56 EXPORT_SYMBOL_GPL(nf_nat_sip_hook);
58 unsigned int (*nf_nat_sip_expect_hook)(struct sk_buff *skb,
60 unsigned int *datalen,
61 struct nf_conntrack_expect *exp,
62 unsigned int matchoff,
63 unsigned int matchlen) __read_mostly;
64 EXPORT_SYMBOL_GPL(nf_nat_sip_expect_hook);
66 unsigned int (*nf_nat_sdp_addr_hook)(struct sk_buff *skb,
69 unsigned int *datalen,
70 enum sdp_header_types type,
71 enum sdp_header_types term,
72 const union nf_inet_addr *addr)
74 EXPORT_SYMBOL_GPL(nf_nat_sdp_addr_hook);
76 unsigned int (*nf_nat_sdp_port_hook)(struct sk_buff *skb,
78 unsigned int *datalen,
79 unsigned int matchoff,
80 unsigned int matchlen,
81 u_int16_t port) __read_mostly;
82 EXPORT_SYMBOL_GPL(nf_nat_sdp_port_hook);
84 unsigned int (*nf_nat_sdp_session_hook)(struct sk_buff *skb,
87 unsigned int *datalen,
88 const union nf_inet_addr *addr)
90 EXPORT_SYMBOL_GPL(nf_nat_sdp_session_hook);
92 unsigned int (*nf_nat_sdp_media_hook)(struct sk_buff *skb,
94 unsigned int *datalen,
95 struct nf_conntrack_expect *rtp_exp,
96 struct nf_conntrack_expect *rtcp_exp,
97 unsigned int mediaoff,
98 unsigned int medialen,
99 union nf_inet_addr *rtp_addr)
101 EXPORT_SYMBOL_GPL(nf_nat_sdp_media_hook);
103 static int string_len(const struct nf_conn *ct, const char *dptr,
104 const char *limit, int *shift)
108 while (dptr < limit && isalpha(*dptr)) {
115 static int digits_len(const struct nf_conn *ct, const char *dptr,
116 const char *limit, int *shift)
119 while (dptr < limit && isdigit(*dptr)) {
126 /* get media type + port length */
127 static int media_len(const struct nf_conn *ct, const char *dptr,
128 const char *limit, int *shift)
130 int len = string_len(ct, dptr, limit, shift);
133 if (dptr >= limit || *dptr != ' ')
138 return len + digits_len(ct, dptr, limit, shift);
141 static int parse_addr(const struct nf_conn *ct, const char *cp,
142 const char **endp, union nf_inet_addr *addr,
148 memset(addr, 0, sizeof(*addr));
149 switch (nf_ct_l3num(ct)) {
151 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
154 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
160 if (ret == 0 || end == cp)
167 /* skip ip address. returns its length. */
168 static int epaddr_len(const struct nf_conn *ct, const char *dptr,
169 const char *limit, int *shift)
171 union nf_inet_addr addr;
172 const char *aux = dptr;
174 if (!parse_addr(ct, dptr, &dptr, &addr, limit)) {
175 pr_debug("ip: %s parse failed.!\n", dptr);
182 dptr += digits_len(ct, dptr, limit, shift);
187 /* get address length, skiping user info. */
188 static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
189 const char *limit, int *shift)
191 const char *start = dptr;
194 /* Search for @, but stop at the end of the line.
195 * We are inside a sip: URI, so we don't need to worry about
196 * continuation lines. */
197 while (dptr < limit &&
198 *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
203 if (dptr < limit && *dptr == '@') {
211 return epaddr_len(ct, dptr, limit, shift);
214 /* Parse a SIP request line of the form:
216 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
218 * and return the offset and length of the address contained in the Request-URI.
220 int ct_sip_parse_request(const struct nf_conn *ct,
221 const char *dptr, unsigned int datalen,
222 unsigned int *matchoff, unsigned int *matchlen,
223 union nf_inet_addr *addr, __be16 *port)
225 const char *start = dptr, *limit = dptr + datalen, *end;
230 /* Skip method and following whitespace */
231 mlen = string_len(ct, dptr, limit, NULL);
239 limit -= strlen("sip:");
240 for (; dptr < limit; dptr++) {
241 if (*dptr == '\r' || *dptr == '\n')
243 if (strnicmp(dptr, "sip:", strlen("sip:")) == 0)
246 if (!skp_epaddr_len(ct, dptr, limit, &shift))
250 if (!parse_addr(ct, dptr, &end, addr, limit))
252 if (end < limit && *end == ':') {
254 p = simple_strtoul(end, (char **)&end, 10);
255 if (p < 1024 || p > 65535)
259 *port = htons(SIP_PORT);
263 *matchoff = dptr - start;
264 *matchlen = end - dptr;
267 EXPORT_SYMBOL_GPL(ct_sip_parse_request);
269 /* SIP header parsing: SIP headers are located at the beginning of a line, but
270 * may span several lines, in which case the continuation lines begin with a
271 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
272 * CRLF, RFC 3261 allows only CRLF, we support both.
274 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
275 * whitespace and the values. Whitespace in this context means any amount of
276 * tabs, spaces and continuation lines, which are treated as a single whitespace
279 * Some headers may appear multiple times. A comma seperated list of values is
280 * equivalent to multiple headers.
282 static const struct sip_header ct_sip_hdrs[] = {
283 [SIP_HDR_CSEQ] = SIP_HDR("CSeq", NULL, NULL, digits_len),
284 [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len),
285 [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len),
286 [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len),
287 [SIP_HDR_VIA] = SIP_HDR("Via", "v", "UDP ", epaddr_len),
288 [SIP_HDR_EXPIRES] = SIP_HDR("Expires", NULL, NULL, digits_len),
289 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len),
292 static const char *sip_follow_continuation(const char *dptr, const char *limit)
294 /* Walk past newline */
298 /* Skip '\n' in CR LF */
299 if (*(dptr - 1) == '\r' && *dptr == '\n') {
304 /* Continuation line? */
305 if (*dptr != ' ' && *dptr != '\t')
308 /* skip leading whitespace */
309 for (; dptr < limit; dptr++) {
310 if (*dptr != ' ' && *dptr != '\t')
316 static const char *sip_skip_whitespace(const char *dptr, const char *limit)
318 for (; dptr < limit; dptr++) {
321 if (*dptr != '\r' && *dptr != '\n')
323 dptr = sip_follow_continuation(dptr, limit);
330 /* Search within a SIP header value, dealing with continuation lines */
331 static const char *ct_sip_header_search(const char *dptr, const char *limit,
332 const char *needle, unsigned int len)
334 for (limit -= len; dptr < limit; dptr++) {
335 if (*dptr == '\r' || *dptr == '\n') {
336 dptr = sip_follow_continuation(dptr, limit);
342 if (strnicmp(dptr, needle, len) == 0)
348 int ct_sip_get_header(const struct nf_conn *ct, const char *dptr,
349 unsigned int dataoff, unsigned int datalen,
350 enum sip_header_types type,
351 unsigned int *matchoff, unsigned int *matchlen)
353 const struct sip_header *hdr = &ct_sip_hdrs[type];
354 const char *start = dptr, *limit = dptr + datalen;
357 for (dptr += dataoff; dptr < limit; dptr++) {
358 /* Find beginning of line */
359 if (*dptr != '\r' && *dptr != '\n')
363 if (*(dptr - 1) == '\r' && *dptr == '\n') {
368 /* Skip continuation lines */
369 if (*dptr == ' ' || *dptr == '\t')
372 /* Find header. Compact headers must be followed by a
373 * non-alphabetic character to avoid mismatches. */
374 if (limit - dptr >= hdr->len &&
375 strnicmp(dptr, hdr->name, hdr->len) == 0)
377 else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
378 strnicmp(dptr, hdr->cname, hdr->clen) == 0 &&
379 !isalpha(*(dptr + hdr->clen + 1)))
384 /* Find and skip colon */
385 dptr = sip_skip_whitespace(dptr, limit);
388 if (*dptr != ':' || ++dptr >= limit)
391 /* Skip whitespace after colon */
392 dptr = sip_skip_whitespace(dptr, limit);
396 *matchoff = dptr - start;
398 dptr = ct_sip_header_search(dptr, limit, hdr->search,
405 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
408 *matchoff = dptr - start + shift;
413 EXPORT_SYMBOL_GPL(ct_sip_get_header);
415 /* Get next header field in a list of comma seperated values */
416 static int ct_sip_next_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;
427 dptr = ct_sip_header_search(dptr, limit, ",", strlen(","));
431 dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen);
436 *matchoff = dptr - start;
437 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
444 /* Walk through headers until a parsable one is found or no header of the
445 * given type is left. */
446 static int ct_sip_walk_headers(const struct nf_conn *ct, const char *dptr,
447 unsigned int dataoff, unsigned int datalen,
448 enum sip_header_types type, int *in_header,
449 unsigned int *matchoff, unsigned int *matchlen)
453 if (in_header && *in_header) {
455 ret = ct_sip_next_header(ct, dptr, dataoff, datalen,
456 type, matchoff, matchlen);
461 dataoff += *matchoff;
467 ret = ct_sip_get_header(ct, dptr, dataoff, datalen,
468 type, matchoff, matchlen);
473 dataoff += *matchoff;
481 /* Locate a SIP header, parse the URI and return the offset and length of
482 * the address as well as the address and port themselves. A stream of
483 * headers can be parsed by handing in a non-NULL datalen and in_header
486 int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
487 unsigned int *dataoff, unsigned int datalen,
488 enum sip_header_types type, int *in_header,
489 unsigned int *matchoff, unsigned int *matchlen,
490 union nf_inet_addr *addr, __be16 *port)
492 const char *c, *limit = dptr + datalen;
496 ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
497 type, in_header, matchoff, matchlen);
502 if (!parse_addr(ct, dptr + *matchoff, &c, addr, limit))
506 p = simple_strtoul(c, (char **)&c, 10);
507 if (p < 1024 || p > 65535)
511 *port = htons(SIP_PORT);
517 EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri);
519 /* Parse address from header parameter and return address, offset and length */
520 int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr,
521 unsigned int dataoff, unsigned int datalen,
523 unsigned int *matchoff, unsigned int *matchlen,
524 union nf_inet_addr *addr)
526 const char *limit = dptr + datalen;
527 const char *start, *end;
529 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
531 limit = dptr + datalen;
533 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
537 start += strlen(name);
538 if (!parse_addr(ct, start, &end, addr, limit))
540 *matchoff = start - dptr;
541 *matchlen = end - start;
544 EXPORT_SYMBOL_GPL(ct_sip_parse_address_param);
546 /* Parse numerical header parameter and return value, offset and length */
547 int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr,
548 unsigned int dataoff, unsigned int datalen,
550 unsigned int *matchoff, unsigned int *matchlen,
553 const char *limit = dptr + datalen;
557 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
559 limit = dptr + datalen;
561 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
565 start += strlen(name);
566 *val = simple_strtoul(start, &end, 0);
569 if (matchoff && matchlen) {
570 *matchoff = start - dptr;
571 *matchlen = end - start;
575 EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param);
577 /* SDP header parsing: a SDP session description contains an ordered set of
578 * headers, starting with a section containing general session parameters,
579 * optionally followed by multiple media descriptions.
581 * SDP headers always start at the beginning of a line. According to RFC 2327:
582 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
583 * be tolerant and also accept records terminated with a single newline
584 * character". We handle both cases.
586 static const struct sip_header ct_sdp_hdrs[] = {
587 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
588 [SDP_HDR_OWNER_IP4] = SDP_HDR("o=", "IN IP4 ", epaddr_len),
589 [SDP_HDR_CONNECTION_IP4] = SDP_HDR("c=", "IN IP4 ", epaddr_len),
590 [SDP_HDR_OWNER_IP6] = SDP_HDR("o=", "IN IP6 ", epaddr_len),
591 [SDP_HDR_CONNECTION_IP6] = SDP_HDR("c=", "IN IP6 ", epaddr_len),
592 [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len),
595 /* Linear string search within SDP header values */
596 static const char *ct_sdp_header_search(const char *dptr, const char *limit,
597 const char *needle, unsigned int len)
599 for (limit -= len; dptr < limit; dptr++) {
600 if (*dptr == '\r' || *dptr == '\n')
602 if (strncmp(dptr, needle, len) == 0)
608 /* Locate a SDP header (optionally a substring within the header value),
609 * optionally stopping at the first occurence of the term header, parse
610 * it and return the offset and length of the data we're interested in.
612 int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr,
613 unsigned int dataoff, unsigned int datalen,
614 enum sdp_header_types type,
615 enum sdp_header_types term,
616 unsigned int *matchoff, unsigned int *matchlen)
618 const struct sip_header *hdr = &ct_sdp_hdrs[type];
619 const struct sip_header *thdr = &ct_sdp_hdrs[term];
620 const char *start = dptr, *limit = dptr + datalen;
623 for (dptr += dataoff; dptr < limit; dptr++) {
624 /* Find beginning of line */
625 if (*dptr != '\r' && *dptr != '\n')
629 if (*(dptr - 1) == '\r' && *dptr == '\n') {
634 if (term != SDP_HDR_UNSPEC &&
635 limit - dptr >= thdr->len &&
636 strnicmp(dptr, thdr->name, thdr->len) == 0)
638 else if (limit - dptr >= hdr->len &&
639 strnicmp(dptr, hdr->name, hdr->len) == 0)
644 *matchoff = dptr - start;
646 dptr = ct_sdp_header_search(dptr, limit, hdr->search,
653 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
656 *matchoff = dptr - start + shift;
661 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header);
663 static int ct_sip_parse_sdp_addr(const struct nf_conn *ct, const char *dptr,
664 unsigned int dataoff, unsigned int datalen,
665 enum sdp_header_types type,
666 enum sdp_header_types term,
667 unsigned int *matchoff, unsigned int *matchlen,
668 union nf_inet_addr *addr)
672 ret = ct_sip_get_sdp_header(ct, dptr, dataoff, datalen, type, term,
677 if (!parse_addr(ct, dptr + *matchoff, NULL, addr,
678 dptr + *matchoff + *matchlen))
683 static int refresh_signalling_expectation(struct nf_conn *ct,
684 union nf_inet_addr *addr,
686 unsigned int expires)
688 struct nf_conn_help *help = nfct_help(ct);
689 struct nf_conntrack_expect *exp;
690 struct hlist_node *n, *next;
693 spin_lock_bh(&nf_conntrack_lock);
694 hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) {
695 if (exp->class != SIP_EXPECT_SIGNALLING ||
696 !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) ||
697 exp->tuple.dst.u.udp.port != port)
699 if (!del_timer(&exp->timeout))
701 exp->flags &= ~NF_CT_EXPECT_INACTIVE;
702 exp->timeout.expires = jiffies + expires * HZ;
703 add_timer(&exp->timeout);
707 spin_unlock_bh(&nf_conntrack_lock);
711 static void flush_expectations(struct nf_conn *ct, bool media)
713 struct nf_conn_help *help = nfct_help(ct);
714 struct nf_conntrack_expect *exp;
715 struct hlist_node *n, *next;
717 spin_lock_bh(&nf_conntrack_lock);
718 hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) {
719 if ((exp->class != SIP_EXPECT_SIGNALLING) ^ media)
721 if (!del_timer(&exp->timeout))
723 nf_ct_unlink_expect(exp);
724 nf_ct_expect_put(exp);
728 spin_unlock_bh(&nf_conntrack_lock);
731 static int set_expected_rtp_rtcp(struct sk_buff *skb,
732 const char **dptr, unsigned int *datalen,
733 union nf_inet_addr *daddr, __be16 port,
734 enum sip_expectation_classes class,
735 unsigned int mediaoff, unsigned int medialen)
737 struct nf_conntrack_expect *exp, *rtp_exp, *rtcp_exp;
738 enum ip_conntrack_info ctinfo;
739 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
740 struct net *net = nf_ct_net(ct);
741 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
742 union nf_inet_addr *saddr;
743 struct nf_conntrack_tuple tuple;
744 int direct_rtp = 0, skip_expect = 0, ret = NF_DROP;
746 __be16 rtp_port, rtcp_port;
747 typeof(nf_nat_sdp_port_hook) nf_nat_sdp_port;
748 typeof(nf_nat_sdp_media_hook) nf_nat_sdp_media;
751 if (sip_direct_media) {
752 if (!nf_inet_addr_cmp(daddr, &ct->tuplehash[dir].tuple.src.u3))
754 saddr = &ct->tuplehash[!dir].tuple.src.u3;
757 /* We need to check whether the registration exists before attempting
758 * to register it since we can see the same media description multiple
759 * times on different connections in case multiple endpoints receive
762 * RTP optimization: if we find a matching media channel expectation
763 * and both the expectation and this connection are SNATed, we assume
764 * both sides can reach each other directly and use the final
765 * destination address from the expectation. We still need to keep
766 * the NATed expectations for media that might arrive from the
767 * outside, and additionally need to expect the direct RTP stream
768 * in case it passes through us even without NAT.
770 memset(&tuple, 0, sizeof(tuple));
772 tuple.src.u3 = *saddr;
773 tuple.src.l3num = nf_ct_l3num(ct);
774 tuple.dst.protonum = IPPROTO_UDP;
775 tuple.dst.u3 = *daddr;
776 tuple.dst.u.udp.port = port;
780 exp = __nf_ct_expect_find(net, &tuple);
782 if (!exp || exp->master == ct ||
783 nfct_help(exp->master)->helper != nfct_help(ct)->helper ||
786 #ifdef CONFIG_NF_NAT_NEEDED
787 if (exp->tuple.src.l3num == AF_INET && !direct_rtp &&
788 (exp->saved_ip != exp->tuple.dst.u3.ip ||
789 exp->saved_proto.udp.port != exp->tuple.dst.u.udp.port) &&
790 ct->status & IPS_NAT_MASK) {
791 daddr->ip = exp->saved_ip;
792 tuple.dst.u3.ip = exp->saved_ip;
793 tuple.dst.u.udp.port = exp->saved_proto.udp.port;
798 } while (!skip_expect);
801 base_port = ntohs(tuple.dst.u.udp.port) & ~1;
802 rtp_port = htons(base_port);
803 rtcp_port = htons(base_port + 1);
806 nf_nat_sdp_port = rcu_dereference(nf_nat_sdp_port_hook);
807 if (nf_nat_sdp_port &&
808 !nf_nat_sdp_port(skb, dptr, datalen,
809 mediaoff, medialen, ntohs(rtp_port)))
816 rtp_exp = nf_ct_expect_alloc(ct);
819 nf_ct_expect_init(rtp_exp, class, nf_ct_l3num(ct), saddr, daddr,
820 IPPROTO_UDP, NULL, &rtp_port);
822 rtcp_exp = nf_ct_expect_alloc(ct);
823 if (rtcp_exp == NULL)
825 nf_ct_expect_init(rtcp_exp, class, nf_ct_l3num(ct), saddr, daddr,
826 IPPROTO_UDP, NULL, &rtcp_port);
828 nf_nat_sdp_media = rcu_dereference(nf_nat_sdp_media_hook);
829 if (nf_nat_sdp_media && ct->status & IPS_NAT_MASK && !direct_rtp)
830 ret = nf_nat_sdp_media(skb, dptr, datalen, rtp_exp, rtcp_exp,
831 mediaoff, medialen, daddr);
833 if (nf_ct_expect_related(rtp_exp) == 0) {
834 if (nf_ct_expect_related(rtcp_exp) != 0)
835 nf_ct_unexpect_related(rtp_exp);
840 nf_ct_expect_put(rtcp_exp);
842 nf_ct_expect_put(rtp_exp);
847 static const struct sdp_media_type sdp_media_types[] = {
848 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO),
849 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO),
852 static const struct sdp_media_type *sdp_media_type(const char *dptr,
853 unsigned int matchoff,
854 unsigned int matchlen)
856 const struct sdp_media_type *t;
859 for (i = 0; i < ARRAY_SIZE(sdp_media_types); i++) {
860 t = &sdp_media_types[i];
861 if (matchlen < t->len ||
862 strncmp(dptr + matchoff, t->name, t->len))
869 static int process_sdp(struct sk_buff *skb,
870 const char **dptr, unsigned int *datalen,
873 enum ip_conntrack_info ctinfo;
874 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
875 struct nf_conn_help *help = nfct_help(ct);
876 unsigned int matchoff, matchlen;
877 unsigned int mediaoff, medialen;
879 unsigned int caddr_len, maddr_len;
881 union nf_inet_addr caddr, maddr, rtp_addr;
883 enum sdp_header_types c_hdr;
884 const struct sdp_media_type *t;
886 typeof(nf_nat_sdp_addr_hook) nf_nat_sdp_addr;
887 typeof(nf_nat_sdp_session_hook) nf_nat_sdp_session;
889 nf_nat_sdp_addr = rcu_dereference(nf_nat_sdp_addr_hook);
890 c_hdr = nf_ct_l3num(ct) == AF_INET ? SDP_HDR_CONNECTION_IP4 :
891 SDP_HDR_CONNECTION_IP6;
893 /* Find beginning of session description */
894 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
895 SDP_HDR_VERSION, SDP_HDR_UNSPEC,
896 &matchoff, &matchlen) <= 0)
900 /* The connection information is contained in the session description
901 * and/or once per media description. The first media description marks
902 * the end of the session description. */
904 if (ct_sip_parse_sdp_addr(ct, *dptr, sdpoff, *datalen,
905 c_hdr, SDP_HDR_MEDIA,
906 &matchoff, &matchlen, &caddr) > 0)
907 caddr_len = matchlen;
910 for (i = 0; i < ARRAY_SIZE(sdp_media_types); ) {
911 if (ct_sip_get_sdp_header(ct, *dptr, mediaoff, *datalen,
912 SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
913 &mediaoff, &medialen) <= 0)
916 /* Get media type and port number. A media port value of zero
917 * indicates an inactive stream. */
918 t = sdp_media_type(*dptr, mediaoff, medialen);
920 mediaoff += medialen;
926 port = simple_strtoul(*dptr + mediaoff, NULL, 10);
929 if (port < 1024 || port > 65535)
932 /* The media description overrides the session description. */
934 if (ct_sip_parse_sdp_addr(ct, *dptr, mediaoff, *datalen,
935 c_hdr, SDP_HDR_MEDIA,
936 &matchoff, &matchlen, &maddr) > 0) {
937 maddr_len = matchlen;
938 memcpy(&rtp_addr, &maddr, sizeof(rtp_addr));
939 } else if (caddr_len)
940 memcpy(&rtp_addr, &caddr, sizeof(rtp_addr));
944 ret = set_expected_rtp_rtcp(skb, dptr, datalen,
945 &rtp_addr, htons(port), t->class,
947 if (ret != NF_ACCEPT)
950 /* Update media connection address if present */
951 if (maddr_len && nf_nat_sdp_addr && ct->status & IPS_NAT_MASK) {
952 ret = nf_nat_sdp_addr(skb, dptr, mediaoff, datalen,
953 c_hdr, SDP_HDR_MEDIA, &rtp_addr);
954 if (ret != NF_ACCEPT)
960 /* Update session connection and owner addresses */
961 nf_nat_sdp_session = rcu_dereference(nf_nat_sdp_session_hook);
962 if (nf_nat_sdp_session && ct->status & IPS_NAT_MASK)
963 ret = nf_nat_sdp_session(skb, dptr, sdpoff, datalen, &rtp_addr);
965 if (ret == NF_ACCEPT && i > 0)
966 help->help.ct_sip_info.invite_cseq = cseq;
970 static int process_invite_response(struct sk_buff *skb,
971 const char **dptr, unsigned int *datalen,
972 unsigned int cseq, unsigned int code)
974 enum ip_conntrack_info ctinfo;
975 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
976 struct nf_conn_help *help = nfct_help(ct);
978 if ((code >= 100 && code <= 199) ||
979 (code >= 200 && code <= 299))
980 return process_sdp(skb, dptr, datalen, cseq);
981 else if (help->help.ct_sip_info.invite_cseq == cseq)
982 flush_expectations(ct, true);
986 static int process_update_response(struct sk_buff *skb,
987 const char **dptr, unsigned int *datalen,
988 unsigned int cseq, unsigned int code)
990 enum ip_conntrack_info ctinfo;
991 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
992 struct nf_conn_help *help = nfct_help(ct);
994 if ((code >= 100 && code <= 199) ||
995 (code >= 200 && code <= 299))
996 return process_sdp(skb, dptr, datalen, cseq);
997 else if (help->help.ct_sip_info.invite_cseq == cseq)
998 flush_expectations(ct, true);
1002 static int process_prack_response(struct sk_buff *skb,
1003 const char **dptr, unsigned int *datalen,
1004 unsigned int cseq, unsigned int code)
1006 enum ip_conntrack_info ctinfo;
1007 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1008 struct nf_conn_help *help = nfct_help(ct);
1010 if ((code >= 100 && code <= 199) ||
1011 (code >= 200 && code <= 299))
1012 return process_sdp(skb, dptr, datalen, cseq);
1013 else if (help->help.ct_sip_info.invite_cseq == cseq)
1014 flush_expectations(ct, true);
1018 static int process_bye_request(struct sk_buff *skb,
1019 const char **dptr, unsigned int *datalen,
1022 enum ip_conntrack_info ctinfo;
1023 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1025 flush_expectations(ct, true);
1029 /* Parse a REGISTER request and create a permanent expectation for incoming
1030 * signalling connections. The expectation is marked inactive and is activated
1031 * when receiving a response indicating success from the registrar.
1033 static int process_register_request(struct sk_buff *skb,
1034 const char **dptr, unsigned int *datalen,
1037 enum ip_conntrack_info ctinfo;
1038 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1039 struct nf_conn_help *help = nfct_help(ct);
1040 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1041 unsigned int matchoff, matchlen;
1042 struct nf_conntrack_expect *exp;
1043 union nf_inet_addr *saddr, daddr;
1045 unsigned int expires = 0;
1047 typeof(nf_nat_sip_expect_hook) nf_nat_sip_expect;
1049 /* Expected connections can not register again. */
1050 if (ct->status & IPS_EXPECTED)
1053 /* We must check the expiration time: a value of zero signals the
1054 * registrar to release the binding. We'll remove our expectation
1055 * when receiving the new bindings in the response, but we don't
1056 * want to create new ones.
1058 * The expiration time may be contained in Expires: header, the
1059 * Contact: header parameters or the URI parameters.
1061 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1062 &matchoff, &matchlen) > 0)
1063 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1065 ret = ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1066 SIP_HDR_CONTACT, NULL,
1067 &matchoff, &matchlen, &daddr, &port);
1073 /* We don't support third-party registrations */
1074 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr))
1077 if (ct_sip_parse_numerical_param(ct, *dptr,
1078 matchoff + matchlen, *datalen,
1079 "expires=", NULL, NULL, &expires) < 0)
1087 exp = nf_ct_expect_alloc(ct);
1092 if (sip_direct_signalling)
1093 saddr = &ct->tuplehash[!dir].tuple.src.u3;
1095 nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, nf_ct_l3num(ct),
1096 saddr, &daddr, IPPROTO_UDP, NULL, &port);
1097 exp->timeout.expires = sip_timeout * HZ;
1098 exp->helper = nfct_help(ct)->helper;
1099 exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE;
1101 nf_nat_sip_expect = rcu_dereference(nf_nat_sip_expect_hook);
1102 if (nf_nat_sip_expect && ct->status & IPS_NAT_MASK)
1103 ret = nf_nat_sip_expect(skb, dptr, datalen, exp,
1104 matchoff, matchlen);
1106 if (nf_ct_expect_related(exp) != 0)
1111 nf_ct_expect_put(exp);
1114 if (ret == NF_ACCEPT)
1115 help->help.ct_sip_info.register_cseq = cseq;
1119 static int process_register_response(struct sk_buff *skb,
1120 const char **dptr, unsigned int *datalen,
1121 unsigned int cseq, unsigned int code)
1123 enum ip_conntrack_info ctinfo;
1124 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1125 struct nf_conn_help *help = nfct_help(ct);
1126 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1127 union nf_inet_addr addr;
1129 unsigned int matchoff, matchlen, dataoff = 0;
1130 unsigned int expires = 0;
1131 int in_contact = 0, ret;
1133 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1134 * they have received a final response from the registrar for the
1135 * previous one or the previous REGISTER request has timed out".
1137 * However, some servers fail to detect retransmissions and send late
1138 * responses, so we store the sequence number of the last valid
1139 * request and compare it here.
1141 if (help->help.ct_sip_info.register_cseq != cseq)
1144 if (code >= 100 && code <= 199)
1146 if (code < 200 || code > 299)
1149 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1150 &matchoff, &matchlen) > 0)
1151 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1154 unsigned int c_expires = expires;
1156 ret = ct_sip_parse_header_uri(ct, *dptr, &dataoff, *datalen,
1157 SIP_HDR_CONTACT, &in_contact,
1158 &matchoff, &matchlen,
1165 /* We don't support third-party registrations */
1166 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr))
1169 ret = ct_sip_parse_numerical_param(ct, *dptr,
1170 matchoff + matchlen,
1171 *datalen, "expires=",
1172 NULL, NULL, &c_expires);
1177 if (refresh_signalling_expectation(ct, &addr, port, c_expires))
1182 flush_expectations(ct, false);
1186 static const struct sip_handler sip_handlers[] = {
1187 SIP_HANDLER("INVITE", process_sdp, process_invite_response),
1188 SIP_HANDLER("UPDATE", process_sdp, process_update_response),
1189 SIP_HANDLER("ACK", process_sdp, NULL),
1190 SIP_HANDLER("PRACK", process_sdp, process_prack_response),
1191 SIP_HANDLER("BYE", process_bye_request, NULL),
1192 SIP_HANDLER("REGISTER", process_register_request, process_register_response),
1195 static int process_sip_response(struct sk_buff *skb,
1196 const char **dptr, unsigned int *datalen)
1198 enum ip_conntrack_info ctinfo;
1199 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1200 unsigned int matchoff, matchlen;
1201 unsigned int code, cseq, dataoff, i;
1203 if (*datalen < strlen("SIP/2.0 200"))
1205 code = simple_strtoul(*dptr + strlen("SIP/2.0 "), NULL, 10);
1209 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
1210 &matchoff, &matchlen) <= 0)
1212 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
1215 dataoff = matchoff + matchlen + 1;
1217 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
1218 const struct sip_handler *handler;
1220 handler = &sip_handlers[i];
1221 if (handler->response == NULL)
1223 if (*datalen < dataoff + handler->len ||
1224 strnicmp(*dptr + dataoff, handler->method, handler->len))
1226 return handler->response(skb, dptr, datalen, cseq, code);
1231 static int process_sip_request(struct sk_buff *skb,
1232 const char **dptr, unsigned int *datalen)
1234 enum ip_conntrack_info ctinfo;
1235 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1236 unsigned int matchoff, matchlen;
1237 unsigned int cseq, i;
1239 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
1240 const struct sip_handler *handler;
1242 handler = &sip_handlers[i];
1243 if (handler->request == NULL)
1245 if (*datalen < handler->len ||
1246 strnicmp(*dptr, handler->method, handler->len))
1249 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
1250 &matchoff, &matchlen) <= 0)
1252 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
1256 return handler->request(skb, dptr, datalen, cseq);
1261 static int sip_help(struct sk_buff *skb,
1262 unsigned int protoff,
1264 enum ip_conntrack_info ctinfo)
1266 unsigned int dataoff, datalen;
1269 typeof(nf_nat_sip_hook) nf_nat_sip;
1272 dataoff = protoff + sizeof(struct udphdr);
1273 if (dataoff >= skb->len)
1276 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1278 if (!skb_is_nonlinear(skb))
1279 dptr = skb->data + dataoff;
1281 pr_debug("Copy of skbuff not supported yet.\n");
1285 datalen = skb->len - dataoff;
1286 if (datalen < strlen("SIP/2.0 200"))
1289 if (strnicmp(dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1290 ret = process_sip_request(skb, &dptr, &datalen);
1292 ret = process_sip_response(skb, &dptr, &datalen);
1294 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
1295 nf_nat_sip = rcu_dereference(nf_nat_sip_hook);
1296 if (nf_nat_sip && !nf_nat_sip(skb, &dptr, &datalen))
1303 static struct nf_conntrack_helper sip[MAX_PORTS][2] __read_mostly;
1304 static char sip_names[MAX_PORTS][2][sizeof("sip-65535")] __read_mostly;
1306 static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = {
1307 [SIP_EXPECT_SIGNALLING] = {
1311 [SIP_EXPECT_AUDIO] = {
1312 .max_expected = 2 * IP_CT_DIR_MAX,
1315 [SIP_EXPECT_VIDEO] = {
1316 .max_expected = 2 * IP_CT_DIR_MAX,
1321 static void nf_conntrack_sip_fini(void)
1325 for (i = 0; i < ports_c; i++) {
1326 for (j = 0; j < 2; j++) {
1327 if (sip[i][j].me == NULL)
1329 nf_conntrack_helper_unregister(&sip[i][j]);
1334 static int __init nf_conntrack_sip_init(void)
1340 ports[ports_c++] = SIP_PORT;
1342 for (i = 0; i < ports_c; i++) {
1343 memset(&sip[i], 0, sizeof(sip[i]));
1345 sip[i][0].tuple.src.l3num = AF_INET;
1346 sip[i][1].tuple.src.l3num = AF_INET6;
1347 for (j = 0; j < 2; j++) {
1348 sip[i][j].tuple.dst.protonum = IPPROTO_UDP;
1349 sip[i][j].tuple.src.u.udp.port = htons(ports[i]);
1350 sip[i][j].expect_policy = sip_exp_policy;
1351 sip[i][j].expect_class_max = SIP_EXPECT_MAX;
1352 sip[i][j].me = THIS_MODULE;
1353 sip[i][j].help = sip_help;
1355 tmpname = &sip_names[i][j][0];
1356 if (ports[i] == SIP_PORT)
1357 sprintf(tmpname, "sip");
1359 sprintf(tmpname, "sip-%u", i);
1360 sip[i][j].name = tmpname;
1362 pr_debug("port #%u: %u\n", i, ports[i]);
1364 ret = nf_conntrack_helper_register(&sip[i][j]);
1366 printk("nf_ct_sip: failed to register helper "
1367 "for pf: %u port: %u\n",
1368 sip[i][j].tuple.src.l3num, ports[i]);
1369 nf_conntrack_sip_fini();
1377 module_init(nf_conntrack_sip_init);
1378 module_exit(nf_conntrack_sip_fini);