aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorPatrick McHardy <kaber@trash.net>2010-02-11 06:26:19 -0500
committerPatrick McHardy <kaber@trash.net>2010-02-11 06:26:19 -0500
commitf5b321bd37fbec9188feb1f721ab46a5ac0b35da (patch)
treee4899d8dcfdb4128505e4f246154934d18692295 /net
parent3b6b9fab42fe98358d70735cf98d43fc18dc79c9 (diff)
netfilter: nf_conntrack_sip: add TCP support
Add TCP support, which is mandated by RFC3261 for all SIP elements. SIP over TCP is similar to UDP, except that messages are delimited by Content-Length: headers and multiple messages may appear in one packet. Signed-off-by: Patrick McHardy <kaber@trash.net>
Diffstat (limited to 'net')
-rw-r--r--net/ipv4/netfilter/nf_nat_sip.c2
-rw-r--r--net/netfilter/nf_conntrack_sip.c205
2 files changed, 177 insertions, 30 deletions
diff --git a/net/ipv4/netfilter/nf_nat_sip.c b/net/ipv4/netfilter/nf_nat_sip.c
index 2454ea5abb79..b232e4040dc6 100644
--- a/net/ipv4/netfilter/nf_nat_sip.c
+++ b/net/ipv4/netfilter/nf_nat_sip.c
@@ -122,7 +122,7 @@ static unsigned int ip_nat_sip(struct sk_buff *skb, unsigned int dataoff,
122 122
123 /* Translate topmost Via header and parameters */ 123 /* Translate topmost Via header and parameters */
124 if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen, 124 if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
125 SIP_HDR_VIA, NULL, &matchoff, &matchlen, 125 SIP_HDR_VIA_UDP, NULL, &matchoff, &matchlen,
126 &addr, &port) > 0) { 126 &addr, &port) > 0) {
127 unsigned int matchend, poff, plen, buflen, n; 127 unsigned int matchend, poff, plen, buflen, n;
128 char buffer[sizeof("nnn.nnn.nnn.nnn:nnnnn")]; 128 char buffer[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
diff --git a/net/netfilter/nf_conntrack_sip.c b/net/netfilter/nf_conntrack_sip.c
index 0ec37d6a2df7..1cc75c5a822b 100644
--- a/net/netfilter/nf_conntrack_sip.c
+++ b/net/netfilter/nf_conntrack_sip.c
@@ -16,6 +16,7 @@
16#include <linux/inet.h> 16#include <linux/inet.h>
17#include <linux/in.h> 17#include <linux/in.h>
18#include <linux/udp.h> 18#include <linux/udp.h>
19#include <linux/tcp.h>
19#include <linux/netfilter.h> 20#include <linux/netfilter.h>
20 21
21#include <net/netfilter/nf_conntrack.h> 22#include <net/netfilter/nf_conntrack.h>
@@ -287,7 +288,8 @@ static const struct sip_header ct_sip_hdrs[] = {
287 [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len), 288 [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len),
288 [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len), 289 [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len),
289 [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len), 290 [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len),
290 [SIP_HDR_VIA] = SIP_HDR("Via", "v", "UDP ", epaddr_len), 291 [SIP_HDR_VIA_UDP] = SIP_HDR("Via", "v", "UDP ", epaddr_len),
292 [SIP_HDR_VIA_TCP] = SIP_HDR("Via", "v", "TCP ", epaddr_len),
291 [SIP_HDR_EXPIRES] = SIP_HDR("Expires", NULL, NULL, digits_len), 293 [SIP_HDR_EXPIRES] = SIP_HDR("Expires", NULL, NULL, digits_len),
292 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len), 294 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len),
293}; 295};
@@ -519,6 +521,33 @@ int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
519} 521}
520EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri); 522EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri);
521 523
524static int ct_sip_parse_param(const struct nf_conn *ct, const char *dptr,
525 unsigned int dataoff, unsigned int datalen,
526 const char *name,
527 unsigned int *matchoff, unsigned int *matchlen)
528{
529 const char *limit = dptr + datalen;
530 const char *start;
531 const char *end;
532
533 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
534 if (!limit)
535 limit = dptr + datalen;
536
537 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
538 if (!start)
539 return 0;
540 start += strlen(name);
541
542 end = ct_sip_header_search(start, limit, ";", strlen(";"));
543 if (!end)
544 end = limit;
545
546 *matchoff = start - dptr;
547 *matchlen = end - start;
548 return 1;
549}
550
522/* Parse address from header parameter and return address, offset and length */ 551/* Parse address from header parameter and return address, offset and length */
523int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr, 552int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr,
524 unsigned int dataoff, unsigned int datalen, 553 unsigned int dataoff, unsigned int datalen,
@@ -577,6 +606,29 @@ int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr,
577} 606}
578EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param); 607EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param);
579 608
609static int ct_sip_parse_transport(struct nf_conn *ct, const char *dptr,
610 unsigned int dataoff, unsigned int datalen,
611 u8 *proto)
612{
613 unsigned int matchoff, matchlen;
614
615 if (ct_sip_parse_param(ct, dptr, dataoff, datalen, "transport=",
616 &matchoff, &matchlen)) {
617 if (!strnicmp(dptr + matchoff, "TCP", strlen("TCP")))
618 *proto = IPPROTO_TCP;
619 else if (!strnicmp(dptr + matchoff, "UDP", strlen("UDP")))
620 *proto = IPPROTO_UDP;
621 else
622 return 0;
623
624 if (*proto != nf_ct_protonum(ct))
625 return 0;
626 } else
627 *proto = nf_ct_protonum(ct);
628
629 return 1;
630}
631
580/* SDP header parsing: a SDP session description contains an ordered set of 632/* SDP header parsing: a SDP session description contains an ordered set of
581 * headers, starting with a section containing general session parameters, 633 * headers, starting with a section containing general session parameters,
582 * optionally followed by multiple media descriptions. 634 * optionally followed by multiple media descriptions.
@@ -685,7 +737,7 @@ static int ct_sip_parse_sdp_addr(const struct nf_conn *ct, const char *dptr,
685 737
686static int refresh_signalling_expectation(struct nf_conn *ct, 738static int refresh_signalling_expectation(struct nf_conn *ct,
687 union nf_inet_addr *addr, 739 union nf_inet_addr *addr,
688 __be16 port, 740 u8 proto, __be16 port,
689 unsigned int expires) 741 unsigned int expires)
690{ 742{
691 struct nf_conn_help *help = nfct_help(ct); 743 struct nf_conn_help *help = nfct_help(ct);
@@ -697,6 +749,7 @@ static int refresh_signalling_expectation(struct nf_conn *ct,
697 hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) { 749 hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) {
698 if (exp->class != SIP_EXPECT_SIGNALLING || 750 if (exp->class != SIP_EXPECT_SIGNALLING ||
699 !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) || 751 !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) ||
752 exp->tuple.dst.protonum != proto ||
700 exp->tuple.dst.u.udp.port != port) 753 exp->tuple.dst.u.udp.port != port)
701 continue; 754 continue;
702 if (!del_timer(&exp->timeout)) 755 if (!del_timer(&exp->timeout))
@@ -1048,6 +1101,7 @@ static int process_register_request(struct sk_buff *skb, unsigned int dataoff,
1048 struct nf_conntrack_expect *exp; 1101 struct nf_conntrack_expect *exp;
1049 union nf_inet_addr *saddr, daddr; 1102 union nf_inet_addr *saddr, daddr;
1050 __be16 port; 1103 __be16 port;
1104 u8 proto;
1051 unsigned int expires = 0; 1105 unsigned int expires = 0;
1052 int ret; 1106 int ret;
1053 typeof(nf_nat_sip_expect_hook) nf_nat_sip_expect; 1107 typeof(nf_nat_sip_expect_hook) nf_nat_sip_expect;
@@ -1080,6 +1134,10 @@ static int process_register_request(struct sk_buff *skb, unsigned int dataoff,
1080 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr)) 1134 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr))
1081 return NF_ACCEPT; 1135 return NF_ACCEPT;
1082 1136
1137 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen, *datalen,
1138 &proto) == 0)
1139 return NF_ACCEPT;
1140
1083 if (ct_sip_parse_numerical_param(ct, *dptr, 1141 if (ct_sip_parse_numerical_param(ct, *dptr,
1084 matchoff + matchlen, *datalen, 1142 matchoff + matchlen, *datalen,
1085 "expires=", NULL, NULL, &expires) < 0) 1143 "expires=", NULL, NULL, &expires) < 0)
@@ -1099,7 +1157,7 @@ static int process_register_request(struct sk_buff *skb, unsigned int dataoff,
1099 saddr = &ct->tuplehash[!dir].tuple.src.u3; 1157 saddr = &ct->tuplehash[!dir].tuple.src.u3;
1100 1158
1101 nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, nf_ct_l3num(ct), 1159 nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, nf_ct_l3num(ct),
1102 saddr, &daddr, IPPROTO_UDP, NULL, &port); 1160 saddr, &daddr, proto, NULL, &port);
1103 exp->timeout.expires = sip_timeout * HZ; 1161 exp->timeout.expires = sip_timeout * HZ;
1104 exp->helper = nfct_help(ct)->helper; 1162 exp->helper = nfct_help(ct)->helper;
1105 exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE; 1163 exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE;
@@ -1132,6 +1190,7 @@ static int process_register_response(struct sk_buff *skb, unsigned int dataoff,
1132 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); 1190 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1133 union nf_inet_addr addr; 1191 union nf_inet_addr addr;
1134 __be16 port; 1192 __be16 port;
1193 u8 proto;
1135 unsigned int matchoff, matchlen, coff = 0; 1194 unsigned int matchoff, matchlen, coff = 0;
1136 unsigned int expires = 0; 1195 unsigned int expires = 0;
1137 int in_contact = 0, ret; 1196 int in_contact = 0, ret;
@@ -1172,6 +1231,10 @@ static int process_register_response(struct sk_buff *skb, unsigned int dataoff,
1172 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr)) 1231 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr))
1173 continue; 1232 continue;
1174 1233
1234 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen,
1235 *datalen, &proto) == 0)
1236 continue;
1237
1175 ret = ct_sip_parse_numerical_param(ct, *dptr, 1238 ret = ct_sip_parse_numerical_param(ct, *dptr,
1176 matchoff + matchlen, 1239 matchoff + matchlen,
1177 *datalen, "expires=", 1240 *datalen, "expires=",
@@ -1180,7 +1243,8 @@ static int process_register_response(struct sk_buff *skb, unsigned int dataoff,
1180 return NF_DROP; 1243 return NF_DROP;
1181 if (c_expires == 0) 1244 if (c_expires == 0)
1182 break; 1245 break;
1183 if (refresh_signalling_expectation(ct, &addr, port, c_expires)) 1246 if (refresh_signalling_expectation(ct, &addr, proto, port,
1247 c_expires))
1184 return NF_ACCEPT; 1248 return NF_ACCEPT;
1185 } 1249 }
1186 1250
@@ -1265,50 +1329,123 @@ static int process_sip_request(struct sk_buff *skb, unsigned int dataoff,
1265 return NF_ACCEPT; 1329 return NF_ACCEPT;
1266} 1330}
1267 1331
1268static int sip_help(struct sk_buff *skb, 1332static int process_sip_msg(struct sk_buff *skb, struct nf_conn *ct,
1269 unsigned int protoff, 1333 unsigned int dataoff, const char **dptr,
1270 struct nf_conn *ct, 1334 unsigned int *datalen)
1271 enum ip_conntrack_info ctinfo) 1335{
1336 typeof(nf_nat_sip_hook) nf_nat_sip;
1337 int ret;
1338
1339 if (strnicmp(*dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1340 ret = process_sip_request(skb, dataoff, dptr, datalen);
1341 else
1342 ret = process_sip_response(skb, dataoff, dptr, datalen);
1343
1344 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
1345 nf_nat_sip = rcu_dereference(nf_nat_sip_hook);
1346 if (nf_nat_sip && !nf_nat_sip(skb, dataoff, dptr, datalen))
1347 ret = NF_DROP;
1348 }
1349
1350 return ret;
1351}
1352
1353static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,
1354 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1272{ 1355{
1356 struct tcphdr *th, _tcph;
1273 unsigned int dataoff, datalen; 1357 unsigned int dataoff, datalen;
1274 const char *dptr; 1358 unsigned int matchoff, matchlen, clen;
1359 unsigned int msglen, origlen;
1360 const char *dptr, *end;
1361 s16 diff, tdiff = 0;
1275 int ret; 1362 int ret;
1276 typeof(nf_nat_sip_hook) nf_nat_sip; 1363
1364 if (ctinfo != IP_CT_ESTABLISHED &&
1365 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY)
1366 return NF_ACCEPT;
1277 1367
1278 /* No Data ? */ 1368 /* No Data ? */
1279 dataoff = protoff + sizeof(struct udphdr); 1369 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
1370 if (th == NULL)
1371 return NF_ACCEPT;
1372 dataoff = protoff + th->doff * 4;
1280 if (dataoff >= skb->len) 1373 if (dataoff >= skb->len)
1281 return NF_ACCEPT; 1374 return NF_ACCEPT;
1282 1375
1283 nf_ct_refresh(ct, skb, sip_timeout * HZ); 1376 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1284 1377
1285 if (!skb_is_nonlinear(skb)) 1378 if (skb_is_nonlinear(skb)) {
1286 dptr = skb->data + dataoff;
1287 else {
1288 pr_debug("Copy of skbuff not supported yet.\n"); 1379 pr_debug("Copy of skbuff not supported yet.\n");
1289 return NF_ACCEPT; 1380 return NF_ACCEPT;
1290 } 1381 }
1291 1382
1383 dptr = skb->data + dataoff;
1292 datalen = skb->len - dataoff; 1384 datalen = skb->len - dataoff;
1293 if (datalen < strlen("SIP/2.0 200")) 1385 if (datalen < strlen("SIP/2.0 200"))
1294 return NF_ACCEPT; 1386 return NF_ACCEPT;
1295 1387
1296 if (strnicmp(dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0) 1388 while (1) {
1297 ret = process_sip_request(skb, dataoff, &dptr, &datalen); 1389 if (ct_sip_get_header(ct, dptr, 0, datalen,
1298 else 1390 SIP_HDR_CONTENT_LENGTH,
1299 ret = process_sip_response(skb, dataoff, &dptr, &datalen); 1391 &matchoff, &matchlen) <= 0)
1392 break;
1300 1393
1301 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) { 1394 clen = simple_strtoul(dptr + matchoff, (char **)&end, 10);
1302 nf_nat_sip = rcu_dereference(nf_nat_sip_hook); 1395 if (dptr + matchoff == end)
1303 if (nf_nat_sip && !nf_nat_sip(skb, dataoff, &dptr, &datalen)) 1396 break;
1304 ret = NF_DROP; 1397
1398 if (end + strlen("\r\n\r\n") > dptr + datalen)
1399 break;
1400 if (end[0] != '\r' || end[1] != '\n' ||
1401 end[2] != '\r' || end[3] != '\n')
1402 break;
1403 end += strlen("\r\n\r\n") + clen;
1404
1405 msglen = origlen = end - dptr;
1406
1407 ret = process_sip_msg(skb, ct, dataoff, &dptr, &msglen);
1408 if (ret != NF_ACCEPT)
1409 break;
1410 diff = msglen - origlen;
1411 tdiff += diff;
1412
1413 dataoff += msglen;
1414 dptr += msglen;
1415 datalen = datalen + diff - msglen;
1305 } 1416 }
1306 1417
1307 return ret; 1418 return ret;
1308} 1419}
1309 1420
1310static struct nf_conntrack_helper sip[MAX_PORTS][2] __read_mostly; 1421static int sip_help_udp(struct sk_buff *skb, unsigned int protoff,
1311static char sip_names[MAX_PORTS][2][sizeof("sip-65535")] __read_mostly; 1422 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1423{
1424 unsigned int dataoff, datalen;
1425 const char *dptr;
1426
1427 /* No Data ? */
1428 dataoff = protoff + sizeof(struct udphdr);
1429 if (dataoff >= skb->len)
1430 return NF_ACCEPT;
1431
1432 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1433
1434 if (skb_is_nonlinear(skb)) {
1435 pr_debug("Copy of skbuff not supported yet.\n");
1436 return NF_ACCEPT;
1437 }
1438
1439 dptr = skb->data + dataoff;
1440 datalen = skb->len - dataoff;
1441 if (datalen < strlen("SIP/2.0 200"))
1442 return NF_ACCEPT;
1443
1444 return process_sip_msg(skb, ct, dataoff, &dptr, &datalen);
1445}
1446
1447static struct nf_conntrack_helper sip[MAX_PORTS][4] __read_mostly;
1448static char sip_names[MAX_PORTS][4][sizeof("sip-65535")] __read_mostly;
1312 1449
1313static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = { 1450static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = {
1314 [SIP_EXPECT_SIGNALLING] = { 1451 [SIP_EXPECT_SIGNALLING] = {
@@ -1333,7 +1470,7 @@ static void nf_conntrack_sip_fini(void)
1333 int i, j; 1470 int i, j;
1334 1471
1335 for (i = 0; i < ports_c; i++) { 1472 for (i = 0; i < ports_c; i++) {
1336 for (j = 0; j < 2; j++) { 1473 for (j = 0; j < ARRAY_SIZE(sip[i]); j++) {
1337 if (sip[i][j].me == NULL) 1474 if (sip[i][j].me == NULL)
1338 continue; 1475 continue;
1339 nf_conntrack_helper_unregister(&sip[i][j]); 1476 nf_conntrack_helper_unregister(&sip[i][j]);
@@ -1353,14 +1490,24 @@ static int __init nf_conntrack_sip_init(void)
1353 memset(&sip[i], 0, sizeof(sip[i])); 1490 memset(&sip[i], 0, sizeof(sip[i]));
1354 1491
1355 sip[i][0].tuple.src.l3num = AF_INET; 1492 sip[i][0].tuple.src.l3num = AF_INET;
1356 sip[i][1].tuple.src.l3num = AF_INET6; 1493 sip[i][0].tuple.dst.protonum = IPPROTO_UDP;
1357 for (j = 0; j < 2; j++) { 1494 sip[i][0].help = sip_help_udp;
1358 sip[i][j].tuple.dst.protonum = IPPROTO_UDP; 1495 sip[i][1].tuple.src.l3num = AF_INET;
1496 sip[i][1].tuple.dst.protonum = IPPROTO_TCP;
1497 sip[i][1].help = sip_help_tcp;
1498
1499 sip[i][2].tuple.src.l3num = AF_INET6;
1500 sip[i][2].tuple.dst.protonum = IPPROTO_UDP;
1501 sip[i][2].help = sip_help_udp;
1502 sip[i][3].tuple.src.l3num = AF_INET6;
1503 sip[i][3].tuple.dst.protonum = IPPROTO_TCP;
1504 sip[i][3].help = sip_help_tcp;
1505
1506 for (j = 0; j < ARRAY_SIZE(sip[i]); j++) {
1359 sip[i][j].tuple.src.u.udp.port = htons(ports[i]); 1507 sip[i][j].tuple.src.u.udp.port = htons(ports[i]);
1360 sip[i][j].expect_policy = sip_exp_policy; 1508 sip[i][j].expect_policy = sip_exp_policy;
1361 sip[i][j].expect_class_max = SIP_EXPECT_MAX; 1509 sip[i][j].expect_class_max = SIP_EXPECT_MAX;
1362 sip[i][j].me = THIS_MODULE; 1510 sip[i][j].me = THIS_MODULE;
1363 sip[i][j].help = sip_help;
1364 1511
1365 tmpname = &sip_names[i][j][0]; 1512 tmpname = &sip_names[i][j][0];
1366 if (ports[i] == SIP_PORT) 1513 if (ports[i] == SIP_PORT)