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 |
| 6 | * (C) 2007, 2008 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> |
| 15 | #include <linux/ip.h> |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 16 | #include <net/ip.h> |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 17 | #include <linux/udp.h> |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 18 | #include <linux/tcp.h> |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 19 | |
| 20 | #include <net/netfilter/nf_nat.h> |
| 21 | #include <net/netfilter/nf_nat_helper.h> |
| 22 | #include <net/netfilter/nf_nat_rule.h> |
| 23 | #include <net/netfilter/nf_conntrack_helper.h> |
| 24 | #include <net/netfilter/nf_conntrack_expect.h> |
| 25 | #include <linux/netfilter/nf_conntrack_sip.h> |
| 26 | |
| 27 | MODULE_LICENSE("GPL"); |
| 28 | MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>"); |
| 29 | MODULE_DESCRIPTION("SIP NAT helper"); |
| 30 | MODULE_ALIAS("ip_nat_sip"); |
| 31 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 32 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 33 | static unsigned int mangle_packet(struct sk_buff *skb, 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) { |
| 44 | th = (struct tcphdr *)(skb->data + ip_hdrlen(skb)); |
| 45 | baseoff = ip_hdrlen(skb) + th->doff * 4; |
| 46 | matchoff += dataoff - baseoff; |
| 47 | |
| 48 | if (!__nf_nat_mangle_tcp_packet(skb, ct, ctinfo, |
| 49 | matchoff, matchlen, |
| 50 | buffer, buflen, false)) |
| 51 | return 0; |
| 52 | } else { |
| 53 | baseoff = ip_hdrlen(skb) + sizeof(struct udphdr); |
| 54 | matchoff += dataoff - baseoff; |
| 55 | |
| 56 | if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo, |
| 57 | matchoff, matchlen, |
| 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 | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 68 | static int map_addr(struct sk_buff *skb, unsigned int dataoff, |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 69 | const char **dptr, unsigned int *datalen, |
| 70 | unsigned int matchoff, unsigned int matchlen, |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 71 | union nf_inet_addr *addr, __be16 port) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 72 | { |
Patrick McHardy | 212440a | 2008-03-25 20:17:13 -0700 | [diff] [blame] | 73 | enum ip_conntrack_info ctinfo; |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 74 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 75 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 76 | char buffer[sizeof("nnn.nnn.nnn.nnn:nnnnn")]; |
| 77 | unsigned int buflen; |
| 78 | __be32 newaddr; |
| 79 | __be16 newport; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 80 | |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 81 | if (ct->tuplehash[dir].tuple.src.u3.ip == addr->ip && |
| 82 | ct->tuplehash[dir].tuple.src.u.udp.port == port) { |
| 83 | newaddr = ct->tuplehash[!dir].tuple.dst.u3.ip; |
| 84 | newport = ct->tuplehash[!dir].tuple.dst.u.udp.port; |
| 85 | } else if (ct->tuplehash[dir].tuple.dst.u3.ip == addr->ip && |
| 86 | ct->tuplehash[dir].tuple.dst.u.udp.port == port) { |
| 87 | newaddr = ct->tuplehash[!dir].tuple.src.u3.ip; |
| 88 | newport = ct->tuplehash[!dir].tuple.src.u.udp.port; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 89 | } else |
| 90 | return 1; |
| 91 | |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 92 | if (newaddr == addr->ip && newport == port) |
| 93 | return 1; |
| 94 | |
Harvey Harrison | cffee38 | 2008-10-31 00:53:08 -0700 | [diff] [blame] | 95 | buflen = sprintf(buffer, "%pI4:%u", &newaddr, ntohs(newport)); |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 96 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 97 | return mangle_packet(skb, dataoff, dptr, datalen, matchoff, matchlen, |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 98 | buffer, buflen); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 101 | static int map_sip_addr(struct sk_buff *skb, unsigned int dataoff, |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 102 | const char **dptr, unsigned int *datalen, |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 103 | enum sip_header_types type) |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 104 | { |
| 105 | enum ip_conntrack_info ctinfo; |
| 106 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
| 107 | unsigned int matchlen, matchoff; |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 108 | union nf_inet_addr addr; |
| 109 | __be16 port; |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 110 | |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 111 | if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen, type, NULL, |
| 112 | &matchoff, &matchlen, &addr, &port) <= 0) |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 113 | return 1; |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 114 | return map_addr(skb, dataoff, dptr, datalen, matchoff, matchlen, |
| 115 | &addr, port); |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 118 | static unsigned int ip_nat_sip(struct sk_buff *skb, unsigned int dataoff, |
Patrick McHardy | 2a6cfb2 | 2008-03-25 20:16:54 -0700 | [diff] [blame] | 119 | const char **dptr, unsigned int *datalen) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 120 | { |
Patrick McHardy | 212440a | 2008-03-25 20:17:13 -0700 | [diff] [blame] | 121 | enum ip_conntrack_info ctinfo; |
| 122 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 123 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 124 | unsigned int coff, matchoff, matchlen; |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 125 | enum sip_header_types hdr; |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 126 | union nf_inet_addr addr; |
| 127 | __be16 port; |
Patrick McHardy | c978cd3 | 2008-03-25 20:24:57 -0700 | [diff] [blame] | 128 | int request, in_header; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 129 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 130 | /* Basic rules: requests and responses. */ |
Patrick McHardy | 779382e | 2008-03-25 20:17:36 -0700 | [diff] [blame] | 131 | if (strnicmp(*dptr, "SIP/2.0", strlen("SIP/2.0")) != 0) { |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 132 | if (ct_sip_parse_request(ct, *dptr, *datalen, |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 133 | &matchoff, &matchlen, |
| 134 | &addr, &port) > 0 && |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 135 | !map_addr(skb, dataoff, dptr, datalen, matchoff, matchlen, |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 136 | &addr, port)) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 137 | return NF_DROP; |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 138 | request = 1; |
| 139 | } else |
| 140 | request = 0; |
| 141 | |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 142 | if (nf_ct_protonum(ct) == IPPROTO_TCP) |
| 143 | hdr = SIP_HDR_VIA_TCP; |
| 144 | else |
| 145 | hdr = SIP_HDR_VIA_UDP; |
| 146 | |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 147 | /* Translate topmost Via header and parameters */ |
| 148 | if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen, |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 149 | hdr, NULL, &matchoff, &matchlen, |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 150 | &addr, &port) > 0) { |
Patrick McHardy | f22eb25 | 2012-08-09 10:08:47 +0000 | [diff] [blame^] | 151 | unsigned int olen, matchend, poff, plen, buflen, n; |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 152 | char buffer[sizeof("nnn.nnn.nnn.nnn:nnnnn")]; |
| 153 | |
| 154 | /* We're only interested in headers related to this |
| 155 | * connection */ |
| 156 | if (request) { |
| 157 | if (addr.ip != ct->tuplehash[dir].tuple.src.u3.ip || |
| 158 | port != ct->tuplehash[dir].tuple.src.u.udp.port) |
| 159 | goto next; |
| 160 | } else { |
| 161 | if (addr.ip != ct->tuplehash[dir].tuple.dst.u3.ip || |
| 162 | port != ct->tuplehash[dir].tuple.dst.u.udp.port) |
| 163 | goto next; |
| 164 | } |
| 165 | |
Patrick McHardy | f22eb25 | 2012-08-09 10:08:47 +0000 | [diff] [blame^] | 166 | olen = *datalen; |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 167 | if (!map_addr(skb, dataoff, dptr, datalen, matchoff, matchlen, |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 168 | &addr, port)) |
| 169 | return NF_DROP; |
| 170 | |
Patrick McHardy | f22eb25 | 2012-08-09 10:08:47 +0000 | [diff] [blame^] | 171 | matchend = matchoff + matchlen + *datalen - olen; |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 172 | |
| 173 | /* The maddr= parameter (RFC 2361) specifies where to send |
| 174 | * the reply. */ |
| 175 | if (ct_sip_parse_address_param(ct, *dptr, matchend, *datalen, |
| 176 | "maddr=", &poff, &plen, |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 177 | &addr, true) > 0 && |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 178 | addr.ip == ct->tuplehash[dir].tuple.src.u3.ip && |
| 179 | addr.ip != ct->tuplehash[!dir].tuple.dst.u3.ip) { |
Harvey Harrison | cffee38 | 2008-10-31 00:53:08 -0700 | [diff] [blame] | 180 | buflen = sprintf(buffer, "%pI4", |
| 181 | &ct->tuplehash[!dir].tuple.dst.u3.ip); |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 182 | if (!mangle_packet(skb, dataoff, dptr, datalen, |
| 183 | poff, plen, buffer, buflen)) |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 184 | return NF_DROP; |
| 185 | } |
| 186 | |
| 187 | /* The received= parameter (RFC 2361) contains the address |
| 188 | * from which the server received the request. */ |
| 189 | if (ct_sip_parse_address_param(ct, *dptr, matchend, *datalen, |
| 190 | "received=", &poff, &plen, |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 191 | &addr, false) > 0 && |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 192 | addr.ip == ct->tuplehash[dir].tuple.dst.u3.ip && |
| 193 | addr.ip != ct->tuplehash[!dir].tuple.src.u3.ip) { |
Harvey Harrison | cffee38 | 2008-10-31 00:53:08 -0700 | [diff] [blame] | 194 | buflen = sprintf(buffer, "%pI4", |
| 195 | &ct->tuplehash[!dir].tuple.src.u3.ip); |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 196 | if (!mangle_packet(skb, dataoff, dptr, datalen, |
| 197 | poff, plen, buffer, buflen)) |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 198 | return NF_DROP; |
| 199 | } |
| 200 | |
| 201 | /* The rport= parameter (RFC 3581) contains the port number |
| 202 | * from which the server received the request. */ |
| 203 | if (ct_sip_parse_numerical_param(ct, *dptr, matchend, *datalen, |
| 204 | "rport=", &poff, &plen, |
| 205 | &n) > 0 && |
| 206 | htons(n) == ct->tuplehash[dir].tuple.dst.u.udp.port && |
| 207 | htons(n) != ct->tuplehash[!dir].tuple.src.u.udp.port) { |
| 208 | __be16 p = ct->tuplehash[!dir].tuple.src.u.udp.port; |
| 209 | buflen = sprintf(buffer, "%u", ntohs(p)); |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 210 | if (!mangle_packet(skb, dataoff, dptr, datalen, |
| 211 | poff, plen, buffer, buflen)) |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 212 | return NF_DROP; |
| 213 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Patrick McHardy | 720ac70 | 2008-03-25 20:24:41 -0700 | [diff] [blame] | 216 | next: |
Patrick McHardy | c978cd3 | 2008-03-25 20:24:57 -0700 | [diff] [blame] | 217 | /* Translate Contact headers */ |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 218 | coff = 0; |
Patrick McHardy | c978cd3 | 2008-03-25 20:24:57 -0700 | [diff] [blame] | 219 | in_header = 0; |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 220 | while (ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen, |
Patrick McHardy | c978cd3 | 2008-03-25 20:24:57 -0700 | [diff] [blame] | 221 | SIP_HDR_CONTACT, &in_header, |
| 222 | &matchoff, &matchlen, |
| 223 | &addr, &port) > 0) { |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 224 | if (!map_addr(skb, dataoff, dptr, datalen, matchoff, matchlen, |
Patrick McHardy | c978cd3 | 2008-03-25 20:24:57 -0700 | [diff] [blame] | 225 | &addr, port)) |
| 226 | return NF_DROP; |
| 227 | } |
| 228 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 229 | if (!map_sip_addr(skb, dataoff, dptr, datalen, SIP_HDR_FROM) || |
| 230 | !map_sip_addr(skb, dataoff, dptr, datalen, SIP_HDR_TO)) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 231 | return NF_DROP; |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 232 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 233 | return NF_ACCEPT; |
| 234 | } |
| 235 | |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 236 | static void ip_nat_sip_seq_adjust(struct sk_buff *skb, s16 off) |
| 237 | { |
| 238 | enum ip_conntrack_info ctinfo; |
| 239 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
| 240 | const struct tcphdr *th; |
| 241 | |
| 242 | if (nf_ct_protonum(ct) != IPPROTO_TCP || off == 0) |
| 243 | return; |
| 244 | |
| 245 | th = (struct tcphdr *)(skb->data + ip_hdrlen(skb)); |
| 246 | nf_nat_set_seq_adjust(ct, ctinfo, th->seq, off); |
| 247 | } |
| 248 | |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 249 | /* Handles expected signalling connections and media streams */ |
| 250 | static void ip_nat_sip_expected(struct nf_conn *ct, |
| 251 | struct nf_conntrack_expect *exp) |
| 252 | { |
Patrick McHardy | cbc9f2f | 2011-12-23 13:59:49 +0100 | [diff] [blame] | 253 | struct nf_nat_ipv4_range range; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 254 | |
| 255 | /* This must be a fresh one. */ |
| 256 | BUG_ON(ct->status & IPS_NAT_DONE_MASK); |
| 257 | |
| 258 | /* For DST manip, map port here to where it's expected. */ |
Patrick McHardy | cbc9f2f | 2011-12-23 13:59:49 +0100 | [diff] [blame] | 259 | range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 260 | range.min = range.max = exp->saved_proto; |
| 261 | range.min_ip = range.max_ip = exp->saved_ip; |
Patrick McHardy | cbc9f2f | 2011-12-23 13:59:49 +0100 | [diff] [blame] | 262 | nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 263 | |
| 264 | /* Change src to where master sends to, but only if the connection |
| 265 | * actually came from the same source. */ |
| 266 | if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip == |
| 267 | ct->master->tuplehash[exp->dir].tuple.src.u3.ip) { |
Patrick McHardy | cbc9f2f | 2011-12-23 13:59:49 +0100 | [diff] [blame] | 268 | range.flags = NF_NAT_RANGE_MAP_IPS; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 269 | range.min_ip = range.max_ip |
| 270 | = ct->master->tuplehash[!exp->dir].tuple.dst.u3.ip; |
Patrick McHardy | cbc9f2f | 2011-12-23 13:59:49 +0100 | [diff] [blame] | 271 | nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 275 | static unsigned int ip_nat_sip_expect(struct sk_buff *skb, unsigned int dataoff, |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 276 | const char **dptr, unsigned int *datalen, |
| 277 | struct nf_conntrack_expect *exp, |
| 278 | unsigned int matchoff, |
| 279 | unsigned int matchlen) |
| 280 | { |
| 281 | enum ip_conntrack_info ctinfo; |
| 282 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
| 283 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
| 284 | __be32 newip; |
| 285 | u_int16_t port; |
| 286 | char buffer[sizeof("nnn.nnn.nnn.nnn:nnnnn")]; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 287 | unsigned int buflen; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 288 | |
| 289 | /* Connection will come from reply */ |
| 290 | if (ct->tuplehash[dir].tuple.src.u3.ip == ct->tuplehash[!dir].tuple.dst.u3.ip) |
| 291 | newip = exp->tuple.dst.u3.ip; |
| 292 | else |
| 293 | newip = ct->tuplehash[!dir].tuple.dst.u3.ip; |
| 294 | |
| 295 | /* If the signalling port matches the connection's source port in the |
| 296 | * original direction, try to use the destination port in the opposite |
| 297 | * direction. */ |
| 298 | if (exp->tuple.dst.u.udp.port == |
| 299 | ct->tuplehash[dir].tuple.src.u.udp.port) |
| 300 | port = ntohs(ct->tuplehash[!dir].tuple.dst.u.udp.port); |
| 301 | else |
| 302 | port = ntohs(exp->tuple.dst.u.udp.port); |
| 303 | |
| 304 | exp->saved_ip = exp->tuple.dst.u3.ip; |
| 305 | exp->tuple.dst.u3.ip = newip; |
| 306 | exp->saved_proto.udp.port = exp->tuple.dst.u.udp.port; |
| 307 | exp->dir = !dir; |
| 308 | exp->expectfn = ip_nat_sip_expected; |
| 309 | |
| 310 | for (; port != 0; port++) { |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 311 | int ret; |
| 312 | |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 313 | exp->tuple.dst.u.udp.port = htons(port); |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 314 | ret = nf_ct_expect_related(exp); |
| 315 | if (ret == 0) |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 316 | break; |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 317 | else if (ret != -EBUSY) { |
| 318 | port = 0; |
| 319 | break; |
| 320 | } |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | if (port == 0) |
| 324 | return NF_DROP; |
| 325 | |
| 326 | if (exp->tuple.dst.u3.ip != exp->saved_ip || |
| 327 | exp->tuple.dst.u.udp.port != exp->saved_proto.udp.port) { |
Harvey Harrison | cffee38 | 2008-10-31 00:53:08 -0700 | [diff] [blame] | 328 | buflen = sprintf(buffer, "%pI4:%u", &newip, port); |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 329 | if (!mangle_packet(skb, dataoff, dptr, datalen, |
| 330 | matchoff, matchlen, buffer, buflen)) |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 331 | goto err; |
| 332 | } |
| 333 | return NF_ACCEPT; |
| 334 | |
| 335 | err: |
| 336 | nf_ct_unexpect_related(exp); |
| 337 | return NF_DROP; |
| 338 | } |
| 339 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 340 | static int mangle_content_len(struct sk_buff *skb, unsigned int dataoff, |
Patrick McHardy | 2a6cfb2 | 2008-03-25 20:16:54 -0700 | [diff] [blame] | 341 | const char **dptr, unsigned int *datalen) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 342 | { |
Patrick McHardy | 212440a | 2008-03-25 20:17:13 -0700 | [diff] [blame] | 343 | enum ip_conntrack_info ctinfo; |
| 344 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 2a6cfb2 | 2008-03-25 20:16:54 -0700 | [diff] [blame] | 345 | unsigned int matchoff, matchlen; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 346 | char buffer[sizeof("65536")]; |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 347 | int buflen, c_len; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 348 | |
Joe Perches | e00ccd4 | 2007-12-20 14:05:03 -0800 | [diff] [blame] | 349 | /* Get actual SDP length */ |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 350 | if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen, |
| 351 | SDP_HDR_VERSION, SDP_HDR_UNSPEC, |
| 352 | &matchoff, &matchlen) <= 0) |
| 353 | return 0; |
| 354 | c_len = *datalen - matchoff + strlen("v="); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 355 | |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 356 | /* Now, update SDP length */ |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 357 | if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CONTENT_LENGTH, |
| 358 | &matchoff, &matchlen) <= 0) |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 359 | return 0; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 360 | |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 361 | buflen = sprintf(buffer, "%u", c_len); |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 362 | return mangle_packet(skb, dataoff, dptr, datalen, matchoff, matchlen, |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 363 | buffer, buflen); |
| 364 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 365 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 366 | static int mangle_sdp_packet(struct sk_buff *skb, unsigned int dataoff, |
| 367 | const char **dptr, unsigned int *datalen, |
| 368 | unsigned int sdpoff, |
Herbert Xu | c71529e | 2008-07-21 10:03:23 -0700 | [diff] [blame] | 369 | enum sdp_header_types type, |
| 370 | enum sdp_header_types term, |
| 371 | char *buffer, int buflen) |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 372 | { |
| 373 | enum ip_conntrack_info ctinfo; |
| 374 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
| 375 | unsigned int matchlen, matchoff; |
| 376 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 377 | if (ct_sip_get_sdp_header(ct, *dptr, sdpoff, *datalen, type, term, |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 378 | &matchoff, &matchlen) <= 0) |
Herbert Xu | c71529e | 2008-07-21 10:03:23 -0700 | [diff] [blame] | 379 | return -ENOENT; |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 380 | return mangle_packet(skb, dataoff, dptr, datalen, matchoff, matchlen, |
Herbert Xu | c71529e | 2008-07-21 10:03:23 -0700 | [diff] [blame] | 381 | buffer, buflen) ? 0 : -EINVAL; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 384 | static unsigned int ip_nat_sdp_addr(struct sk_buff *skb, unsigned int dataoff, |
| 385 | const char **dptr, unsigned int *datalen, |
| 386 | unsigned int sdpoff, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 387 | enum sdp_header_types type, |
| 388 | enum sdp_header_types term, |
| 389 | const union nf_inet_addr *addr) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 390 | { |
| 391 | char buffer[sizeof("nnn.nnn.nnn.nnn")]; |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 392 | unsigned int buflen; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 393 | |
Harvey Harrison | cffee38 | 2008-10-31 00:53:08 -0700 | [diff] [blame] | 394 | buflen = sprintf(buffer, "%pI4", &addr->ip); |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 395 | if (mangle_sdp_packet(skb, dataoff, dptr, datalen, sdpoff, type, term, |
Herbert Xu | c71529e | 2008-07-21 10:03:23 -0700 | [diff] [blame] | 396 | buffer, buflen)) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 397 | return 0; |
| 398 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 399 | return mangle_content_len(skb, dataoff, dptr, datalen); |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 402 | static unsigned int ip_nat_sdp_port(struct sk_buff *skb, unsigned int dataoff, |
| 403 | const char **dptr, unsigned int *datalen, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 404 | unsigned int matchoff, |
| 405 | unsigned int matchlen, |
| 406 | u_int16_t port) |
| 407 | { |
| 408 | char buffer[sizeof("nnnnn")]; |
| 409 | unsigned int buflen; |
| 410 | |
| 411 | buflen = sprintf(buffer, "%u", port); |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 412 | if (!mangle_packet(skb, dataoff, dptr, datalen, matchoff, matchlen, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 413 | buffer, buflen)) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 414 | return 0; |
| 415 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 416 | return mangle_content_len(skb, dataoff, dptr, datalen); |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 419 | static unsigned int ip_nat_sdp_session(struct sk_buff *skb, unsigned int dataoff, |
| 420 | const char **dptr, unsigned int *datalen, |
| 421 | unsigned int sdpoff, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 422 | const union nf_inet_addr *addr) |
| 423 | { |
| 424 | char buffer[sizeof("nnn.nnn.nnn.nnn")]; |
| 425 | unsigned int buflen; |
| 426 | |
| 427 | /* Mangle session description owner and contact addresses */ |
Harvey Harrison | cffee38 | 2008-10-31 00:53:08 -0700 | [diff] [blame] | 428 | buflen = sprintf(buffer, "%pI4", &addr->ip); |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 429 | if (mangle_sdp_packet(skb, dataoff, dptr, datalen, sdpoff, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 430 | SDP_HDR_OWNER_IP4, SDP_HDR_MEDIA, |
| 431 | buffer, buflen)) |
| 432 | return 0; |
| 433 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 434 | switch (mangle_sdp_packet(skb, dataoff, dptr, datalen, sdpoff, |
Herbert Xu | c71529e | 2008-07-21 10:03:23 -0700 | [diff] [blame] | 435 | SDP_HDR_CONNECTION_IP4, SDP_HDR_MEDIA, |
| 436 | buffer, buflen)) { |
| 437 | case 0: |
| 438 | /* |
| 439 | * RFC 2327: |
| 440 | * |
| 441 | * Session description |
| 442 | * |
| 443 | * c=* (connection information - not required if included in all media) |
| 444 | */ |
| 445 | case -ENOENT: |
| 446 | break; |
| 447 | default: |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 448 | return 0; |
Herbert Xu | c71529e | 2008-07-21 10:03:23 -0700 | [diff] [blame] | 449 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 450 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 451 | return mangle_content_len(skb, dataoff, dptr, datalen); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* So, this packet has hit the connection tracking matching code. |
| 455 | Mangle it, and change the expectation to match the new version. */ |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 456 | static unsigned int ip_nat_sdp_media(struct sk_buff *skb, unsigned int dataoff, |
| 457 | const char **dptr, unsigned int *datalen, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 458 | struct nf_conntrack_expect *rtp_exp, |
| 459 | struct nf_conntrack_expect *rtcp_exp, |
| 460 | unsigned int mediaoff, |
| 461 | unsigned int medialen, |
| 462 | union nf_inet_addr *rtp_addr) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 463 | { |
Patrick McHardy | 212440a | 2008-03-25 20:17:13 -0700 | [diff] [blame] | 464 | enum ip_conntrack_info ctinfo; |
| 465 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 466 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 467 | u_int16_t port; |
| 468 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 469 | /* Connection will come from reply */ |
Jerome Borsboom | f4a607b | 2007-07-07 22:19:48 -0700 | [diff] [blame] | 470 | if (ct->tuplehash[dir].tuple.src.u3.ip == |
| 471 | ct->tuplehash[!dir].tuple.dst.u3.ip) |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 472 | rtp_addr->ip = rtp_exp->tuple.dst.u3.ip; |
Jerome Borsboom | f4a607b | 2007-07-07 22:19:48 -0700 | [diff] [blame] | 473 | else |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 474 | rtp_addr->ip = ct->tuplehash[!dir].tuple.dst.u3.ip; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 475 | |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 476 | rtp_exp->saved_ip = rtp_exp->tuple.dst.u3.ip; |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 477 | rtp_exp->tuple.dst.u3.ip = rtp_addr->ip; |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 478 | rtp_exp->saved_proto.udp.port = rtp_exp->tuple.dst.u.udp.port; |
| 479 | rtp_exp->dir = !dir; |
| 480 | rtp_exp->expectfn = ip_nat_sip_expected; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 481 | |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 482 | rtcp_exp->saved_ip = rtcp_exp->tuple.dst.u3.ip; |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 483 | rtcp_exp->tuple.dst.u3.ip = rtp_addr->ip; |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 484 | rtcp_exp->saved_proto.udp.port = rtcp_exp->tuple.dst.u.udp.port; |
| 485 | rtcp_exp->dir = !dir; |
| 486 | rtcp_exp->expectfn = ip_nat_sip_expected; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 487 | |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 488 | /* Try to get same pair of ports: if not, try to change them. */ |
| 489 | for (port = ntohs(rtp_exp->tuple.dst.u.udp.port); |
| 490 | port != 0; port += 2) { |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 491 | int ret; |
| 492 | |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 493 | rtp_exp->tuple.dst.u.udp.port = htons(port); |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 494 | ret = nf_ct_expect_related(rtp_exp); |
| 495 | if (ret == -EBUSY) |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 496 | continue; |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 497 | else if (ret < 0) { |
| 498 | port = 0; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 499 | break; |
Pablo Neira Ayuso | 5b92b61 | 2010-09-22 08:34:12 +0200 | [diff] [blame] | 500 | } |
| 501 | rtcp_exp->tuple.dst.u.udp.port = htons(port + 1); |
| 502 | ret = nf_ct_expect_related(rtcp_exp); |
| 503 | if (ret == 0) |
| 504 | break; |
| 505 | else if (ret != -EBUSY) { |
| 506 | nf_ct_unexpect_related(rtp_exp); |
| 507 | port = 0; |
| 508 | break; |
| 509 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | if (port == 0) |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 513 | goto err1; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 514 | |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 515 | /* Update media port. */ |
| 516 | if (rtp_exp->tuple.dst.u.udp.port != rtp_exp->saved_proto.udp.port && |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 517 | !ip_nat_sdp_port(skb, dataoff, dptr, datalen, |
| 518 | mediaoff, medialen, port)) |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 519 | goto err2; |
| 520 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 521 | return NF_ACCEPT; |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 522 | |
| 523 | err2: |
| 524 | nf_ct_unexpect_related(rtp_exp); |
| 525 | nf_ct_unexpect_related(rtcp_exp); |
| 526 | err1: |
| 527 | return NF_DROP; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Pablo Neira Ayuso | 544d5c7 | 2012-02-05 03:44:51 +0100 | [diff] [blame] | 530 | static struct nf_ct_helper_expectfn sip_nat = { |
| 531 | .name = "sip", |
| 532 | .expectfn = ip_nat_sip_expected, |
| 533 | }; |
| 534 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 535 | static void __exit nf_nat_sip_fini(void) |
| 536 | { |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 537 | RCU_INIT_POINTER(nf_nat_sip_hook, NULL); |
| 538 | RCU_INIT_POINTER(nf_nat_sip_seq_adjust_hook, NULL); |
| 539 | RCU_INIT_POINTER(nf_nat_sip_expect_hook, NULL); |
| 540 | RCU_INIT_POINTER(nf_nat_sdp_addr_hook, NULL); |
| 541 | RCU_INIT_POINTER(nf_nat_sdp_port_hook, NULL); |
| 542 | RCU_INIT_POINTER(nf_nat_sdp_session_hook, NULL); |
| 543 | RCU_INIT_POINTER(nf_nat_sdp_media_hook, NULL); |
Pablo Neira Ayuso | 544d5c7 | 2012-02-05 03:44:51 +0100 | [diff] [blame] | 544 | nf_ct_helper_expectfn_unregister(&sip_nat); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 545 | synchronize_rcu(); |
| 546 | } |
| 547 | |
| 548 | static int __init nf_nat_sip_init(void) |
| 549 | { |
Patrick McHardy | d1332e0 | 2007-11-05 20:43:30 -0800 | [diff] [blame] | 550 | BUG_ON(nf_nat_sip_hook != NULL); |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 551 | BUG_ON(nf_nat_sip_seq_adjust_hook != NULL); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 552 | BUG_ON(nf_nat_sip_expect_hook != NULL); |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 553 | BUG_ON(nf_nat_sdp_addr_hook != NULL); |
Patrick McHardy | c7f485a | 2008-03-25 20:26:43 -0700 | [diff] [blame] | 554 | BUG_ON(nf_nat_sdp_port_hook != NULL); |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 555 | BUG_ON(nf_nat_sdp_session_hook != NULL); |
| 556 | BUG_ON(nf_nat_sdp_media_hook != NULL); |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 557 | RCU_INIT_POINTER(nf_nat_sip_hook, ip_nat_sip); |
| 558 | RCU_INIT_POINTER(nf_nat_sip_seq_adjust_hook, ip_nat_sip_seq_adjust); |
| 559 | RCU_INIT_POINTER(nf_nat_sip_expect_hook, ip_nat_sip_expect); |
| 560 | RCU_INIT_POINTER(nf_nat_sdp_addr_hook, ip_nat_sdp_addr); |
| 561 | RCU_INIT_POINTER(nf_nat_sdp_port_hook, ip_nat_sdp_port); |
| 562 | RCU_INIT_POINTER(nf_nat_sdp_session_hook, ip_nat_sdp_session); |
| 563 | RCU_INIT_POINTER(nf_nat_sdp_media_hook, ip_nat_sdp_media); |
Pablo Neira Ayuso | 544d5c7 | 2012-02-05 03:44:51 +0100 | [diff] [blame] | 564 | nf_ct_helper_expectfn_register(&sip_nat); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | module_init(nf_nat_sip_init); |
| 569 | module_exit(nf_nat_sip_fini); |