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