Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 1 | /* SIP extension for NAT alteration. |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2 | * |
| 3 | * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar> |
| 4 | * based on RR's ip_nat_ftp.c and other modules. |
Patrick McHardy | f49e1aa | 2008-03-25 20:27:05 -0700 | [diff] [blame] | 5 | * (C) 2007 United Security Providers |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 6 | * (C) 2007, 2008, 2011, 2012 Patrick McHardy <kaber@trash.net> |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 7 | * |
| 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. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/skbuff.h> |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 15 | #include <linux/inet.h> |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 16 | #include <linux/udp.h> |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 17 | #include <linux/tcp.h> |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 18 | |
| 19 | #include <net/netfilter/nf_nat.h> |
| 20 | #include <net/netfilter/nf_nat_helper.h> |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 21 | #include <net/netfilter/nf_conntrack_helper.h> |
| 22 | #include <net/netfilter/nf_conntrack_expect.h> |
Patrick McHardy | 41d73ec | 2013-08-27 08:50:12 +0200 | [diff] [blame] | 23 | #include <net/netfilter/nf_conntrack_seqadj.h> |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 24 | #include <linux/netfilter/nf_conntrack_sip.h> |
| 25 | |
| 26 | MODULE_LICENSE("GPL"); |
| 27 | MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>"); |
| 28 | MODULE_DESCRIPTION("SIP NAT helper"); |
| 29 | MODULE_ALIAS("ip_nat_sip"); |
| 30 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 31 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 32 | static unsigned int mangle_packet(struct sk_buff *skb, unsigned int protoff, |
| 33 | unsigned int dataoff, |
Patrick McHardy | 2a6cfb2 | 2008-03-25 20:16:54 -0700 | [diff] [blame] | 34 | const char **dptr, unsigned int *datalen, |
| 35 | unsigned int matchoff, unsigned int matchlen, |
| 36 | const char *buffer, unsigned int buflen) |
| 37 | { |
| 38 | enum ip_conntrack_info ctinfo; |
| 39 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 40 | struct tcphdr *th; |
| 41 | unsigned int baseoff; |
Patrick McHardy | 2a6cfb2 | 2008-03-25 20:16:54 -0700 | [diff] [blame] | 42 | |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 43 | if (nf_ct_protonum(ct) == IPPROTO_TCP) { |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 44 | th = (struct tcphdr *)(skb->data + protoff); |
| 45 | baseoff = protoff + th->doff * 4; |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 46 | matchoff += dataoff - baseoff; |
| 47 | |
| 48 | if (!__nf_nat_mangle_tcp_packet(skb, ct, ctinfo, |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 49 | protoff, matchoff, matchlen, |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 50 | buffer, buflen, false)) |
| 51 | return 0; |
| 52 | } else { |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 53 | baseoff = protoff + sizeof(struct udphdr); |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 54 | matchoff += dataoff - baseoff; |
| 55 | |
| 56 | if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo, |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 57 | protoff, matchoff, matchlen, |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 58 | buffer, buflen)) |
| 59 | return 0; |
| 60 | } |
Patrick McHardy | 2a6cfb2 | 2008-03-25 20:16:54 -0700 | [diff] [blame] | 61 | |
| 62 | /* Reload data pointer and adjust datalen value */ |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 63 | *dptr = skb->data + dataoff; |
Patrick McHardy | 2a6cfb2 | 2008-03-25 20:16:54 -0700 | [diff] [blame] | 64 | *datalen += buflen - matchlen; |
| 65 | return 1; |
| 66 | } |
| 67 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 68 | static int sip_sprintf_addr(const struct nf_conn *ct, char *buffer, |
| 69 | const union nf_inet_addr *addr, bool delim) |
| 70 | { |
| 71 | if (nf_ct_l3num(ct) == NFPROTO_IPV4) |
| 72 | return sprintf(buffer, "%pI4", &addr->ip); |
| 73 | else { |
| 74 | if (delim) |
| 75 | return sprintf(buffer, "[%pI6c]", &addr->ip6); |
| 76 | else |
| 77 | return sprintf(buffer, "%pI6c", &addr->ip6); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static int sip_sprintf_addr_port(const struct nf_conn *ct, char *buffer, |
| 82 | const union nf_inet_addr *addr, u16 port) |
| 83 | { |
| 84 | if (nf_ct_l3num(ct) == NFPROTO_IPV4) |
| 85 | return sprintf(buffer, "%pI4:%u", &addr->ip, port); |
| 86 | else |
| 87 | return sprintf(buffer, "[%pI6c]:%u", &addr->ip6, port); |
| 88 | } |
| 89 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 90 | static int map_addr(struct sk_buff *skb, unsigned int protoff, |
| 91 | unsigned int dataoff, |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 92 | const char **dptr, unsigned int *datalen, |
| 93 | unsigned int matchoff, unsigned int matchlen, |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 94 | union nf_inet_addr *addr, __be16 port) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 95 | { |
Patrick McHardy | 212440a | 2008-03-25 20:17:13 -0700 | [diff] [blame] | 96 | enum ip_conntrack_info ctinfo; |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 97 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 98 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
Kevin Cernekee | 7266507 | 2012-12-17 18:33:58 +0000 | [diff] [blame] | 99 | struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct); |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 100 | char buffer[INET6_ADDRSTRLEN + sizeof("[]:nnnnn")]; |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 101 | unsigned int buflen; |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 102 | union nf_inet_addr newaddr; |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 103 | __be16 newport; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 104 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 105 | if (nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, addr) && |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 106 | ct->tuplehash[dir].tuple.src.u.udp.port == port) { |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 107 | newaddr = ct->tuplehash[!dir].tuple.dst.u3; |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 108 | newport = ct->tuplehash[!dir].tuple.dst.u.udp.port; |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 109 | } else if (nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, addr) && |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 110 | ct->tuplehash[dir].tuple.dst.u.udp.port == port) { |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 111 | newaddr = ct->tuplehash[!dir].tuple.src.u3; |
Kevin Cernekee | 7266507 | 2012-12-17 18:33:58 +0000 | [diff] [blame] | 112 | newport = ct_sip_info->forced_dport ? : |
| 113 | ct->tuplehash[!dir].tuple.src.u.udp.port; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 114 | } else |
| 115 | return 1; |
| 116 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 117 | if (nf_inet_addr_cmp(&newaddr, addr) && newport == port) |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 118 | return 1; |
| 119 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 120 | buflen = sip_sprintf_addr_port(ct, buffer, &newaddr, ntohs(newport)); |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 121 | return mangle_packet(skb, protoff, dataoff, dptr, datalen, |
| 122 | matchoff, matchlen, buffer, buflen); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 125 | static int map_sip_addr(struct sk_buff *skb, unsigned int protoff, |
| 126 | unsigned int dataoff, |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 127 | const char **dptr, unsigned int *datalen, |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 128 | enum sip_header_types type) |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 129 | { |
| 130 | enum ip_conntrack_info ctinfo; |
| 131 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
| 132 | unsigned int matchlen, matchoff; |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 133 | union nf_inet_addr addr; |
| 134 | __be16 port; |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 135 | |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 136 | if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen, type, NULL, |
| 137 | &matchoff, &matchlen, &addr, &port) <= 0) |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 138 | return 1; |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 139 | return map_addr(skb, protoff, dataoff, dptr, datalen, |
| 140 | matchoff, matchlen, &addr, port); |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 143 | static unsigned int nf_nat_sip(struct sk_buff *skb, unsigned int protoff, |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 144 | unsigned int dataoff, |
Patrick McHardy | 2a6cfb2 | 2008-03-25 20:16:54 -0700 | [diff] [blame] | 145 | const char **dptr, unsigned int *datalen) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 146 | { |
Patrick McHardy | 212440a | 2008-03-25 20:17:13 -0700 | [diff] [blame] | 147 | enum ip_conntrack_info ctinfo; |
| 148 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 149 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
Kevin Cernekee | 7266507 | 2012-12-17 18:33:58 +0000 | [diff] [blame] | 150 | struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct); |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 151 | unsigned int coff, matchoff, matchlen; |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 152 | enum sip_header_types hdr; |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 153 | union nf_inet_addr addr; |
| 154 | __be16 port; |
Patrick McHardy | c978cd3 | 2008-03-25 20:24:57 -0700 | [diff] [blame] | 155 | int request, in_header; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 156 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 157 | /* Basic rules: requests and responses. */ |
Patrick McHardy | 779382e | 2008-03-25 20:17:36 -0700 | [diff] [blame] | 158 | if (strnicmp(*dptr, "SIP/2.0", strlen("SIP/2.0")) != 0) { |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 159 | if (ct_sip_parse_request(ct, *dptr, *datalen, |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 160 | &matchoff, &matchlen, |
| 161 | &addr, &port) > 0 && |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 162 | !map_addr(skb, protoff, dataoff, dptr, datalen, |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 163 | matchoff, matchlen, &addr, port)) { |
| 164 | nf_ct_helper_log(skb, ct, "cannot mangle SIP message"); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 165 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 166 | } |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 167 | request = 1; |
| 168 | } else |
| 169 | request = 0; |
| 170 | |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 171 | if (nf_ct_protonum(ct) == IPPROTO_TCP) |
| 172 | hdr = SIP_HDR_VIA_TCP; |
| 173 | else |
| 174 | hdr = SIP_HDR_VIA_UDP; |
| 175 | |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 176 | /* Translate topmost Via header and parameters */ |
| 177 | if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen, |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 178 | hdr, NULL, &matchoff, &matchlen, |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 179 | &addr, &port) > 0) { |
Patrick McHardy | f22eb25 | 2012-08-09 10:08:47 +0000 | [diff] [blame] | 180 | unsigned int olen, matchend, poff, plen, buflen, n; |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 181 | char buffer[INET6_ADDRSTRLEN + sizeof("[]:nnnnn")]; |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 182 | |
| 183 | /* We're only interested in headers related to this |
| 184 | * connection */ |
| 185 | if (request) { |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 186 | if (!nf_inet_addr_cmp(&addr, |
| 187 | &ct->tuplehash[dir].tuple.src.u3) || |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 188 | port != ct->tuplehash[dir].tuple.src.u.udp.port) |
| 189 | goto next; |
| 190 | } else { |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 191 | if (!nf_inet_addr_cmp(&addr, |
| 192 | &ct->tuplehash[dir].tuple.dst.u3) || |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 193 | port != ct->tuplehash[dir].tuple.dst.u.udp.port) |
| 194 | goto next; |
| 195 | } |
| 196 | |
Patrick McHardy | f22eb25 | 2012-08-09 10:08:47 +0000 | [diff] [blame] | 197 | olen = *datalen; |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 198 | if (!map_addr(skb, protoff, dataoff, dptr, datalen, |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 199 | matchoff, matchlen, &addr, port)) { |
| 200 | nf_ct_helper_log(skb, ct, "cannot mangle Via header"); |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 201 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 202 | } |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 203 | |
Patrick McHardy | f22eb25 | 2012-08-09 10:08:47 +0000 | [diff] [blame] | 204 | matchend = matchoff + matchlen + *datalen - olen; |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 205 | |
| 206 | /* The maddr= parameter (RFC 2361) specifies where to send |
| 207 | * the reply. */ |
| 208 | if (ct_sip_parse_address_param(ct, *dptr, matchend, *datalen, |
| 209 | "maddr=", &poff, &plen, |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 210 | &addr, true) > 0 && |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 211 | nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.src.u3) && |
| 212 | !nf_inet_addr_cmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3)) { |
| 213 | buflen = sip_sprintf_addr(ct, buffer, |
| 214 | &ct->tuplehash[!dir].tuple.dst.u3, |
| 215 | true); |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 216 | if (!mangle_packet(skb, protoff, dataoff, dptr, datalen, |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 217 | poff, plen, buffer, buflen)) { |
| 218 | nf_ct_helper_log(skb, ct, "cannot mangle maddr"); |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 219 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 220 | } |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* The received= parameter (RFC 2361) contains the address |
| 224 | * from which the server received the request. */ |
| 225 | if (ct_sip_parse_address_param(ct, *dptr, matchend, *datalen, |
| 226 | "received=", &poff, &plen, |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 227 | &addr, false) > 0 && |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 228 | nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.dst.u3) && |
| 229 | !nf_inet_addr_cmp(&addr, &ct->tuplehash[!dir].tuple.src.u3)) { |
| 230 | buflen = sip_sprintf_addr(ct, buffer, |
| 231 | &ct->tuplehash[!dir].tuple.src.u3, |
| 232 | false); |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 233 | if (!mangle_packet(skb, protoff, dataoff, dptr, datalen, |
Balazs Peter Odor | 5aed938 | 2013-06-22 19:24:43 +0200 | [diff] [blame] | 234 | poff, plen, buffer, buflen)) { |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 235 | nf_ct_helper_log(skb, ct, "cannot mangle received"); |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 236 | return NF_DROP; |
Balazs Peter Odor | 5aed938 | 2013-06-22 19:24:43 +0200 | [diff] [blame] | 237 | } |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* The rport= parameter (RFC 3581) contains the port number |
| 241 | * from which the server received the request. */ |
| 242 | if (ct_sip_parse_numerical_param(ct, *dptr, matchend, *datalen, |
| 243 | "rport=", &poff, &plen, |
| 244 | &n) > 0 && |
| 245 | htons(n) == ct->tuplehash[dir].tuple.dst.u.udp.port && |
| 246 | htons(n) != ct->tuplehash[!dir].tuple.src.u.udp.port) { |
| 247 | __be16 p = ct->tuplehash[!dir].tuple.src.u.udp.port; |
| 248 | buflen = sprintf(buffer, "%u", ntohs(p)); |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 249 | if (!mangle_packet(skb, protoff, dataoff, dptr, datalen, |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 250 | poff, plen, buffer, buflen)) { |
| 251 | nf_ct_helper_log(skb, ct, "cannot mangle rport"); |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 252 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 253 | } |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 254 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 257 | next: |
Patrick McHardy | c978cd3 | 2008-03-25 20:24:57 -0700 | [diff] [blame] | 258 | /* Translate Contact headers */ |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 259 | coff = 0; |
Patrick McHardy | c978cd3 | 2008-03-25 20:24:57 -0700 | [diff] [blame] | 260 | in_header = 0; |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 261 | while (ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen, |
Patrick McHardy | c978cd3 | 2008-03-25 20:24:57 -0700 | [diff] [blame] | 262 | SIP_HDR_CONTACT, &in_header, |
| 263 | &matchoff, &matchlen, |
| 264 | &addr, &port) > 0) { |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 265 | if (!map_addr(skb, protoff, dataoff, dptr, datalen, |
| 266 | matchoff, matchlen, |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 267 | &addr, port)) { |
| 268 | nf_ct_helper_log(skb, ct, "cannot mangle contact"); |
Patrick McHardy | c978cd3 | 2008-03-25 20:24:57 -0700 | [diff] [blame] | 269 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 270 | } |
Patrick McHardy | c978cd3 | 2008-03-25 20:24:57 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 273 | if (!map_sip_addr(skb, protoff, dataoff, dptr, datalen, SIP_HDR_FROM) || |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 274 | !map_sip_addr(skb, protoff, dataoff, dptr, datalen, SIP_HDR_TO)) { |
| 275 | nf_ct_helper_log(skb, ct, "cannot mangle SIP from/to"); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 276 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 277 | } |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 278 | |
Kevin Cernekee | 7266507 | 2012-12-17 18:33:58 +0000 | [diff] [blame] | 279 | /* Mangle destination port for Cisco phones, then fix up checksums */ |
| 280 | if (dir == IP_CT_DIR_REPLY && ct_sip_info->forced_dport) { |
| 281 | struct udphdr *uh; |
| 282 | |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 283 | if (!skb_make_writable(skb, skb->len)) { |
| 284 | nf_ct_helper_log(skb, ct, "cannot mangle packet"); |
Kevin Cernekee | 7266507 | 2012-12-17 18:33:58 +0000 | [diff] [blame] | 285 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 286 | } |
Kevin Cernekee | 7266507 | 2012-12-17 18:33:58 +0000 | [diff] [blame] | 287 | |
| 288 | uh = (void *)skb->data + protoff; |
| 289 | uh->dest = ct_sip_info->forced_dport; |
| 290 | |
| 291 | if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo, protoff, |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 292 | 0, 0, NULL, 0)) { |
| 293 | nf_ct_helper_log(skb, ct, "cannot mangle packet"); |
Kevin Cernekee | 7266507 | 2012-12-17 18:33:58 +0000 | [diff] [blame] | 294 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 295 | } |
Kevin Cernekee | 7266507 | 2012-12-17 18:33:58 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 298 | return NF_ACCEPT; |
| 299 | } |
| 300 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 301 | static void nf_nat_sip_seq_adjust(struct sk_buff *skb, unsigned int protoff, |
| 302 | s16 off) |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 303 | { |
| 304 | enum ip_conntrack_info ctinfo; |
| 305 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
| 306 | const struct tcphdr *th; |
| 307 | |
| 308 | if (nf_ct_protonum(ct) != IPPROTO_TCP || off == 0) |
| 309 | return; |
| 310 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 311 | th = (struct tcphdr *)(skb->data + protoff); |
Patrick McHardy | 41d73ec | 2013-08-27 08:50:12 +0200 | [diff] [blame] | 312 | nf_ct_seqadj_set(ct, ctinfo, th->seq, off); |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 315 | /* Handles expected signalling connections and media streams */ |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 316 | static void nf_nat_sip_expected(struct nf_conn *ct, |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 317 | struct nf_conntrack_expect *exp) |
| 318 | { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 319 | struct nf_nat_range range; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 320 | |
| 321 | /* This must be a fresh one. */ |
| 322 | BUG_ON(ct->status & IPS_NAT_DONE_MASK); |
| 323 | |
| 324 | /* For DST manip, map port here to where it's expected. */ |
Patrick McHardy | cbc9f2f | 2011-12-23 13:59:49 +0100 | [diff] [blame] | 325 | range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 326 | range.min_proto = range.max_proto = exp->saved_proto; |
| 327 | range.min_addr = range.max_addr = exp->saved_addr; |
Patrick McHardy | cbc9f2f | 2011-12-23 13:59:49 +0100 | [diff] [blame] | 328 | nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 329 | |
| 330 | /* Change src to where master sends to, but only if the connection |
| 331 | * actually came from the same source. */ |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 332 | if (nf_inet_addr_cmp(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3, |
| 333 | &ct->master->tuplehash[exp->dir].tuple.src.u3)) { |
Patrick McHardy | cbc9f2f | 2011-12-23 13:59:49 +0100 | [diff] [blame] | 334 | range.flags = NF_NAT_RANGE_MAP_IPS; |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 335 | range.min_addr = range.max_addr |
| 336 | = ct->master->tuplehash[!exp->dir].tuple.dst.u3; |
Patrick McHardy | cbc9f2f | 2011-12-23 13:59:49 +0100 | [diff] [blame] | 337 | nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 341 | static unsigned int nf_nat_sip_expect(struct sk_buff *skb, unsigned int protoff, |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 342 | unsigned int dataoff, |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 343 | const char **dptr, unsigned int *datalen, |
| 344 | struct nf_conntrack_expect *exp, |
| 345 | unsigned int matchoff, |
| 346 | unsigned int matchlen) |
| 347 | { |
| 348 | enum ip_conntrack_info ctinfo; |
| 349 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
| 350 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
Kevin Cernekee | 7266507 | 2012-12-17 18:33:58 +0000 | [diff] [blame] | 351 | struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct); |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 352 | union nf_inet_addr newaddr; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 353 | u_int16_t port; |
Kevin Cernekee | 7266507 | 2012-12-17 18:33:58 +0000 | [diff] [blame] | 354 | __be16 srcport; |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 355 | char buffer[INET6_ADDRSTRLEN + sizeof("[]:nnnnn")]; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 356 | unsigned int buflen; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 357 | |
| 358 | /* Connection will come from reply */ |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 359 | if (nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, |
| 360 | &ct->tuplehash[!dir].tuple.dst.u3)) |
| 361 | newaddr = exp->tuple.dst.u3; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 362 | else |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 363 | newaddr = ct->tuplehash[!dir].tuple.dst.u3; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 364 | |
| 365 | /* If the signalling port matches the connection's source port in the |
| 366 | * original direction, try to use the destination port in the opposite |
| 367 | * direction. */ |
Kevin Cernekee | 7266507 | 2012-12-17 18:33:58 +0000 | [diff] [blame] | 368 | srcport = ct_sip_info->forced_dport ? : |
| 369 | ct->tuplehash[dir].tuple.src.u.udp.port; |
| 370 | if (exp->tuple.dst.u.udp.port == srcport) |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 371 | port = ntohs(ct->tuplehash[!dir].tuple.dst.u.udp.port); |
| 372 | else |
| 373 | port = ntohs(exp->tuple.dst.u.udp.port); |
| 374 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 375 | exp->saved_addr = exp->tuple.dst.u3; |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 376 | exp->tuple.dst.u3 = newaddr; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 377 | exp->saved_proto.udp.port = exp->tuple.dst.u.udp.port; |
| 378 | exp->dir = !dir; |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 379 | exp->expectfn = nf_nat_sip_expected; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 380 | |
| 381 | for (; port != 0; port++) { |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 382 | int ret; |
| 383 | |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 384 | exp->tuple.dst.u.udp.port = htons(port); |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 385 | ret = nf_ct_expect_related(exp); |
| 386 | if (ret == 0) |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 387 | break; |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 388 | else if (ret != -EBUSY) { |
| 389 | port = 0; |
| 390 | break; |
| 391 | } |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 394 | if (port == 0) { |
| 395 | nf_ct_helper_log(skb, ct, "all ports in use for SIP"); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 396 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 397 | } |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 398 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 399 | if (!nf_inet_addr_cmp(&exp->tuple.dst.u3, &exp->saved_addr) || |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 400 | exp->tuple.dst.u.udp.port != exp->saved_proto.udp.port) { |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 401 | buflen = sip_sprintf_addr_port(ct, buffer, &newaddr, port); |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 402 | if (!mangle_packet(skb, protoff, dataoff, dptr, datalen, |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 403 | matchoff, matchlen, buffer, buflen)) { |
| 404 | nf_ct_helper_log(skb, ct, "cannot mangle packet"); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 405 | goto err; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 406 | } |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 407 | } |
| 408 | return NF_ACCEPT; |
| 409 | |
| 410 | err: |
| 411 | nf_ct_unexpect_related(exp); |
| 412 | return NF_DROP; |
| 413 | } |
| 414 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 415 | static int mangle_content_len(struct sk_buff *skb, unsigned int protoff, |
| 416 | unsigned int dataoff, |
Patrick McHardy | 2a6cfb2 | 2008-03-25 20:16:54 -0700 | [diff] [blame] | 417 | const char **dptr, unsigned int *datalen) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 418 | { |
Patrick McHardy | 212440a | 2008-03-25 20:17:13 -0700 | [diff] [blame] | 419 | enum ip_conntrack_info ctinfo; |
| 420 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 2a6cfb2 | 2008-03-25 20:16:54 -0700 | [diff] [blame] | 421 | unsigned int matchoff, matchlen; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 422 | char buffer[sizeof("65536")]; |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 423 | int buflen, c_len; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 424 | |
Joe Perches | e00ccd4 | 2007-12-20 14:05:03 -0800 | [diff] [blame] | 425 | /* Get actual SDP length */ |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 426 | if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen, |
| 427 | SDP_HDR_VERSION, SDP_HDR_UNSPEC, |
| 428 | &matchoff, &matchlen) <= 0) |
| 429 | return 0; |
| 430 | c_len = *datalen - matchoff + strlen("v="); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 431 | |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 432 | /* Now, update SDP length */ |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 433 | if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CONTENT_LENGTH, |
| 434 | &matchoff, &matchlen) <= 0) |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 435 | return 0; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 436 | |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 437 | buflen = sprintf(buffer, "%u", c_len); |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 438 | return mangle_packet(skb, protoff, dataoff, dptr, datalen, |
| 439 | matchoff, matchlen, buffer, buflen); |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 440 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 441 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 442 | static int mangle_sdp_packet(struct sk_buff *skb, unsigned int protoff, |
| 443 | unsigned int dataoff, |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 444 | const char **dptr, unsigned int *datalen, |
| 445 | unsigned int sdpoff, |
Herbert Xu | c71529e | 2008-07-21 10:03:23 -0700 | [diff] [blame] | 446 | enum sdp_header_types type, |
| 447 | enum sdp_header_types term, |
| 448 | char *buffer, int buflen) |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 449 | { |
| 450 | enum ip_conntrack_info ctinfo; |
| 451 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
| 452 | unsigned int matchlen, matchoff; |
| 453 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 454 | if (ct_sip_get_sdp_header(ct, *dptr, sdpoff, *datalen, type, term, |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 455 | &matchoff, &matchlen) <= 0) |
Herbert Xu | c71529e | 2008-07-21 10:03:23 -0700 | [diff] [blame] | 456 | return -ENOENT; |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 457 | return mangle_packet(skb, protoff, dataoff, dptr, datalen, |
| 458 | matchoff, matchlen, buffer, buflen) ? 0 : -EINVAL; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 461 | static unsigned int nf_nat_sdp_addr(struct sk_buff *skb, unsigned int protoff, |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 462 | unsigned int dataoff, |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 463 | const char **dptr, unsigned int *datalen, |
| 464 | unsigned int sdpoff, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 465 | enum sdp_header_types type, |
| 466 | enum sdp_header_types term, |
| 467 | const union nf_inet_addr *addr) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 468 | { |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 469 | enum ip_conntrack_info ctinfo; |
| 470 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
| 471 | char buffer[INET6_ADDRSTRLEN]; |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 472 | unsigned int buflen; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 473 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 474 | buflen = sip_sprintf_addr(ct, buffer, addr, false); |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 475 | if (mangle_sdp_packet(skb, protoff, dataoff, dptr, datalen, |
| 476 | sdpoff, type, term, buffer, buflen)) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 477 | return 0; |
| 478 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 479 | return mangle_content_len(skb, protoff, dataoff, dptr, datalen); |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 482 | static unsigned int nf_nat_sdp_port(struct sk_buff *skb, unsigned int protoff, |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 483 | unsigned int dataoff, |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 484 | const char **dptr, unsigned int *datalen, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 485 | unsigned int matchoff, |
| 486 | unsigned int matchlen, |
| 487 | u_int16_t port) |
| 488 | { |
| 489 | char buffer[sizeof("nnnnn")]; |
| 490 | unsigned int buflen; |
| 491 | |
| 492 | buflen = sprintf(buffer, "%u", port); |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 493 | if (!mangle_packet(skb, protoff, dataoff, dptr, datalen, |
| 494 | matchoff, matchlen, buffer, buflen)) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 495 | return 0; |
| 496 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 497 | return mangle_content_len(skb, protoff, dataoff, dptr, datalen); |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 500 | static unsigned int nf_nat_sdp_session(struct sk_buff *skb, unsigned int protoff, |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 501 | unsigned int dataoff, |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 502 | const char **dptr, unsigned int *datalen, |
| 503 | unsigned int sdpoff, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 504 | const union nf_inet_addr *addr) |
| 505 | { |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 506 | enum ip_conntrack_info ctinfo; |
| 507 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
| 508 | char buffer[INET6_ADDRSTRLEN]; |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 509 | unsigned int buflen; |
| 510 | |
| 511 | /* Mangle session description owner and contact addresses */ |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 512 | buflen = sip_sprintf_addr(ct, buffer, addr, false); |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 513 | if (mangle_sdp_packet(skb, protoff, dataoff, dptr, datalen, sdpoff, |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 514 | SDP_HDR_OWNER, SDP_HDR_MEDIA, buffer, buflen)) |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 515 | return 0; |
| 516 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 517 | switch (mangle_sdp_packet(skb, protoff, dataoff, dptr, datalen, sdpoff, |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 518 | SDP_HDR_CONNECTION, SDP_HDR_MEDIA, |
Herbert Xu | c71529e | 2008-07-21 10:03:23 -0700 | [diff] [blame] | 519 | buffer, buflen)) { |
| 520 | case 0: |
| 521 | /* |
| 522 | * RFC 2327: |
| 523 | * |
| 524 | * Session description |
| 525 | * |
| 526 | * c=* (connection information - not required if included in all media) |
| 527 | */ |
| 528 | case -ENOENT: |
| 529 | break; |
| 530 | default: |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 531 | return 0; |
Herbert Xu | c71529e | 2008-07-21 10:03:23 -0700 | [diff] [blame] | 532 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 533 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 534 | return mangle_content_len(skb, protoff, dataoff, dptr, datalen); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* So, this packet has hit the connection tracking matching code. |
| 538 | Mangle it, and change the expectation to match the new version. */ |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 539 | static unsigned int nf_nat_sdp_media(struct sk_buff *skb, unsigned int protoff, |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 540 | unsigned int dataoff, |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 541 | const char **dptr, unsigned int *datalen, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 542 | struct nf_conntrack_expect *rtp_exp, |
| 543 | struct nf_conntrack_expect *rtcp_exp, |
| 544 | unsigned int mediaoff, |
| 545 | unsigned int medialen, |
| 546 | union nf_inet_addr *rtp_addr) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 547 | { |
Patrick McHardy | 212440a | 2008-03-25 20:17:13 -0700 | [diff] [blame] | 548 | enum ip_conntrack_info ctinfo; |
| 549 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 550 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 551 | u_int16_t port; |
| 552 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 553 | /* Connection will come from reply */ |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 554 | if (nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, |
| 555 | &ct->tuplehash[!dir].tuple.dst.u3)) |
| 556 | *rtp_addr = rtp_exp->tuple.dst.u3; |
Jerome Borsboom | f4a607b | 2007-07-07 22:19:48 -0700 | [diff] [blame] | 557 | else |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 558 | *rtp_addr = ct->tuplehash[!dir].tuple.dst.u3; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 559 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 560 | rtp_exp->saved_addr = rtp_exp->tuple.dst.u3; |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 561 | rtp_exp->tuple.dst.u3 = *rtp_addr; |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 562 | rtp_exp->saved_proto.udp.port = rtp_exp->tuple.dst.u.udp.port; |
| 563 | rtp_exp->dir = !dir; |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 564 | rtp_exp->expectfn = nf_nat_sip_expected; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 565 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 566 | rtcp_exp->saved_addr = rtcp_exp->tuple.dst.u3; |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 567 | rtcp_exp->tuple.dst.u3 = *rtp_addr; |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 568 | rtcp_exp->saved_proto.udp.port = rtcp_exp->tuple.dst.u.udp.port; |
| 569 | rtcp_exp->dir = !dir; |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 570 | rtcp_exp->expectfn = nf_nat_sip_expected; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 571 | |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 572 | /* Try to get same pair of ports: if not, try to change them. */ |
| 573 | for (port = ntohs(rtp_exp->tuple.dst.u.udp.port); |
| 574 | port != 0; port += 2) { |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 575 | int ret; |
| 576 | |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 577 | rtp_exp->tuple.dst.u.udp.port = htons(port); |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 578 | ret = nf_ct_expect_related(rtp_exp); |
| 579 | if (ret == -EBUSY) |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 580 | continue; |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 581 | else if (ret < 0) { |
| 582 | port = 0; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 583 | break; |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 584 | } |
| 585 | rtcp_exp->tuple.dst.u.udp.port = htons(port + 1); |
| 586 | ret = nf_ct_expect_related(rtcp_exp); |
| 587 | if (ret == 0) |
| 588 | break; |
Pablo Neira Ayuso | 3f509c6 | 2012-08-29 15:24:09 +0000 | [diff] [blame] | 589 | else if (ret == -EBUSY) { |
| 590 | nf_ct_unexpect_related(rtp_exp); |
| 591 | continue; |
| 592 | } else if (ret < 0) { |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 593 | nf_ct_unexpect_related(rtp_exp); |
| 594 | port = 0; |
| 595 | break; |
| 596 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 599 | if (port == 0) { |
| 600 | nf_ct_helper_log(skb, ct, "all ports in use for SDP media"); |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 601 | goto err1; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 602 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 603 | |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 604 | /* Update media port. */ |
| 605 | if (rtp_exp->tuple.dst.u.udp.port != rtp_exp->saved_proto.udp.port && |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 606 | !nf_nat_sdp_port(skb, protoff, dataoff, dptr, datalen, |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 607 | mediaoff, medialen, port)) { |
| 608 | nf_ct_helper_log(skb, ct, "cannot mangle SDP message"); |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 609 | goto err2; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 610 | } |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 611 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 612 | return NF_ACCEPT; |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 613 | |
| 614 | err2: |
| 615 | nf_ct_unexpect_related(rtp_exp); |
| 616 | nf_ct_unexpect_related(rtcp_exp); |
| 617 | err1: |
| 618 | return NF_DROP; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Pablo Neira Ayuso | 544d5c7 | 2012-02-05 03:44:51 +0100 | [diff] [blame] | 621 | static struct nf_ct_helper_expectfn sip_nat = { |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 622 | .name = "sip", |
| 623 | .expectfn = nf_nat_sip_expected, |
Pablo Neira Ayuso | 544d5c7 | 2012-02-05 03:44:51 +0100 | [diff] [blame] | 624 | }; |
| 625 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 626 | static void __exit nf_nat_sip_fini(void) |
| 627 | { |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 628 | RCU_INIT_POINTER(nf_nat_sip_hooks, NULL); |
| 629 | |
Pablo Neira Ayuso | 544d5c7 | 2012-02-05 03:44:51 +0100 | [diff] [blame] | 630 | nf_ct_helper_expectfn_unregister(&sip_nat); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 631 | synchronize_rcu(); |
| 632 | } |
| 633 | |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 634 | static const struct nf_nat_sip_hooks sip_hooks = { |
| 635 | .msg = nf_nat_sip, |
| 636 | .seq_adjust = nf_nat_sip_seq_adjust, |
| 637 | .expect = nf_nat_sip_expect, |
| 638 | .sdp_addr = nf_nat_sdp_addr, |
| 639 | .sdp_port = nf_nat_sdp_port, |
| 640 | .sdp_session = nf_nat_sdp_session, |
| 641 | .sdp_media = nf_nat_sdp_media, |
| 642 | }; |
| 643 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 644 | static int __init nf_nat_sip_init(void) |
| 645 | { |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 646 | BUG_ON(nf_nat_sip_hooks != NULL); |
| 647 | RCU_INIT_POINTER(nf_nat_sip_hooks, &sip_hooks); |
Pablo Neira Ayuso | 544d5c7 | 2012-02-05 03:44:51 +0100 | [diff] [blame] | 648 | nf_ct_helper_expectfn_register(&sip_nat); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | module_init(nf_nat_sip_init); |
| 653 | module_exit(nf_nat_sip_fini); |