Added match logic for local UDP port. Starting to add logic for remote UDP port.

This commit is contained in:
Pedro Alvarez 2019-05-31 15:31:03 +01:00 committed by Andre Puschmann
parent e2ca02bbe4
commit 326aef4431
2 changed files with 130 additions and 39 deletions

View File

@ -22,6 +22,7 @@
#include "srsue/hdr/stack/upper/tft_packet_filter.h"
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/udp.h>
namespace srsue {
@ -100,6 +101,11 @@ bool tft_packet_filter_t::match(const srslte::unique_byte_buffer_t& pdu)
return false;
}
// Check Ports/Port Range
if (!match_port(pdu)) {
return false;
}
return true;
}
@ -183,15 +189,29 @@ bool tft_packet_filter_t::match_port(const srslte::unique_byte_buffer_t& pdu)
{
struct iphdr* ip_pkt = (struct iphdr*)pdu->msg;
struct ipv6hdr* ip6_pkt = (struct ipv6hdr*)pdu->msg;
struct udphdr* udp_pkt;
// LOCAL_PORT_RANGE_FLAG
// SINGLE_REMOTE_PORT_FLAG
// REMOTE_PORT_RANGE_FLAG
if (ip_pkt->version == 4) {
switch (ip_pkt->protocol) {
case UDP_PROTOCOL:
printf("UDP protocol");
printf("UDP protocol\n");
udp_pkt = (struct udphdr*)&pdu->msg[ip_pkt->ihl * 4];
printf("%d\n", ntohs(udp_pkt->source));
if (active_filters & SINGLE_LOCAL_PORT_FLAG) {
if (udp_pkt->source != single_local_port) {
return false;
}
}
break;
case TCP_PROTOCOL:
printf("TCP protocol");
printf("TCP protocol\n");
break;
default:
printf("Unhandled protocol");
printf("Unhandled protocol\n");
return false;
}
}

View File

@ -38,54 +38,121 @@
using namespace srsue;
using namespace srslte;
int tft_filter_test()
// IP test message 1
// Protocol UDP
// Source port 2222, Destination port 2001
uint8_t ip_tst_message1[] = {
0x45, 0x00, 0x00, 0x5c, 0x22, 0xa1, 0x40, 0x00, 0x40, 0x11, 0x19, 0xee, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00,
0x01, 0x08, 0xae, 0x07, 0xd1, 0x00, 0x48, 0xfe, 0x5b, 0xd8, 0xf8, 0xd5, 0x4d, 0x9a, 0x9d, 0x26, 0xc7, 0xbd, 0xb4,
0xcc, 0x90, 0xe0, 0x21, 0x0b, 0x07, 0x74, 0x00, 0xcb, 0x2b, 0xf8, 0x09, 0xa1, 0x55, 0xa8, 0xf8, 0xfc, 0x93, 0xee,
0x4c, 0x67, 0x60, 0xb6, 0xa0, 0x1c, 0x79, 0x29, 0x45, 0x59, 0x96, 0xe6, 0x9b, 0x70, 0xc7, 0x34, 0xb0, 0x2f, 0xf5,
0x0e, 0x0f, 0xcb, 0x45, 0xf1, 0xae, 0x97, 0x46, 0x0c, 0xbe, 0x9f, 0xd7, 0xfa, 0xe5, 0xec, 0x99};
uint32_t ip_message_len1 = sizeof(ip_tst_message1);
// IP test message 2
// Protocol UDP
// Source port 8000, Destination Port 2001
uint8_t ip_tst_message2[] = {
0x45, 0x00, 0x00, 0x5c, 0x1c, 0x0e, 0x40, 0x00, 0x40, 0x11, 0x20, 0x81, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00,
0x01, 0x1f, 0x40, 0x07, 0xd1, 0x00, 0x48, 0xfe, 0x5b, 0xb8, 0x1a, 0x56, 0x0d, 0xd2, 0xa3, 0xf9, 0x11, 0xd5, 0x56,
0xb6, 0x95, 0x60, 0x07, 0x2d, 0x95, 0xe2, 0x53, 0x6b, 0x8f, 0x90, 0xb5, 0x48, 0xd1, 0x71, 0x24, 0xe8, 0x6e, 0x2d,
0x56, 0xec, 0xf1, 0xe5, 0x85, 0xa5, 0x79, 0xc6, 0x5c, 0x90, 0xd6, 0x72, 0x87, 0x20, 0x99, 0x94, 0xfa, 0x82, 0x0d,
0x2a, 0x2c, 0xdf, 0x02, 0x60, 0xef, 0x80, 0x07, 0xe6, 0xe1, 0xef, 0x4f, 0x40, 0x9a, 0x0a, 0xbc};
uint32_t ip_message_len2 = sizeof(ip_tst_message2);
int tft_filter_test_single_local_port()
{
srslte::log_filter log1("NAS");
srslte::log_filter log1("TFT");
log1.set_level(srslte::LOG_LEVEL_DEBUG);
log1.set_hex_limit(128);
srslte::byte_buffer_pool *pool = srslte::byte_buffer_pool::get_instance();
srslte::unique_byte_buffer_t tst_msg, out_msg;
tst_msg = allocate_unique_buffer(*pool);
out_msg = allocate_unique_buffer(*pool);
srslte::unique_byte_buffer_t ip_msg1, ip_msg2;
ip_msg1 = allocate_unique_buffer(*pool);
ip_msg2 = allocate_unique_buffer(*pool);
LIBLTE_MME_ACTIVATE_DEDICATED_EPS_BEARER_CONTEXT_REQUEST_MSG_STRUCT ded_bearer_req;
// Filter length: 3 bytes
// Filter type: Single local port
// Local port: 2222
uint8_t filter_message[3];
filter_message[0] = SINGLE_LOCAL_PORT_TYPE;
srslte::uint16_to_uint8(2222, &filter_message[1]);
LIBLTE_ERROR_ENUM err;
uint8_t nas_message[] = {0x27, 0x35, 0x11, 0xfd, 0xf6, 0x07, 0x62, 0x00, 0xc5, 0x05, 0x01, 0x01, 0x07, 0x21,
0x31, 0x00, 0x03, 0x40, 0x08, 0xae, 0x5d, 0x02, 0x00, 0xc2, 0x81, 0x34, 0x01, 0x4d};
uint32_t nas_message_len = sizeof(nas_message);
// Unpack Activate Dedicated EPS bearer context setup request
tst_msg->N_bytes = nas_message_len;
memcpy(tst_msg->msg, nas_message, nas_message_len);
log1.info_hex(tst_msg->msg, tst_msg->N_bytes, "NAS Activate Dedicated EPS Bearer Context Request original message\n");
// Set IP test message
ip_msg1->N_bytes = ip_message_len1;
memcpy(ip_msg1->msg, ip_tst_message1, ip_message_len1);
log1.info_hex(ip_msg1->msg, ip_msg1->N_bytes, "IP test message\n");
// Test message type and protocol discriminator
uint8_t pd, msg_type;
liblte_mme_parse_msg_header((LIBLTE_BYTE_MSG_STRUCT*)tst_msg.get(), &pd, &msg_type);
TESTASSERT(msg_type == LIBLTE_MME_MSG_TYPE_ACTIVATE_DEDICATED_EPS_BEARER_CONTEXT_REQUEST);
// Unpack message
err = liblte_mme_unpack_activate_dedicated_eps_bearer_context_request_msg((LIBLTE_BYTE_MSG_STRUCT*)tst_msg.get(),
&ded_bearer_req);
TESTASSERT(err == LIBLTE_SUCCESS);
// Traffic flow template
TESTASSERT(ded_bearer_req.tft.tft_op_code == LIBLTE_MME_TFT_OPERATION_CODE_CREATE_NEW_TFT);
TESTASSERT(ded_bearer_req.tft.parameter_list_size == 0);
TESTASSERT(ded_bearer_req.tft.packet_filter_list_size == 1);
TESTASSERT(ded_bearer_req.tft.packet_filter_list[0].dir == LIBLTE_MME_TFT_PACKET_FILTER_DIRECTION_BIDIRECTIONAL);
TESTASSERT(ded_bearer_req.tft.packet_filter_list[0].id == 1); // FIXME Should this be 2?
TESTASSERT(ded_bearer_req.tft.packet_filter_list[0].eval_precedence == 0);
TESTASSERT(ded_bearer_req.tft.packet_filter_list[0].filter_size == 3);
TESTASSERT(ded_bearer_req.tft.packet_filter_list[0].filter[0] ==
LIBLTE_MME_TFT_PACKET_FILTER_COMPONENT_TYPE_ID_SINGLE_LOCAL_PORT_TYPE);
// Set IP test message
ip_msg2->N_bytes = ip_message_len2;
memcpy(ip_msg2->msg, ip_tst_message2, ip_message_len1);
log1.info_hex(ip_msg2->msg, ip_msg2->N_bytes, "IP test message\n");
// Packet filter
LIBLTE_MME_PACKET_FILTER_STRUCT packet_filter;
packet_filter.dir = LIBLTE_MME_TFT_PACKET_FILTER_DIRECTION_BIDIRECTIONAL;
packet_filter.id = 1;
packet_filter.eval_precedence = 0;
packet_filter.filter_size = 3;
memcpy(packet_filter.filter, filter_message, 3);
srsue::tft_packet_filter_t filter(ded_bearer_req.tft.packet_filter_list[0]);
srsue::tft_packet_filter_t filter(packet_filter);
// Check filter
TESTASSERT(filter.active_filters == SINGLE_LOCAL_PORT_FLAG);
TESTASSERT(filter.single_local_port == ntohs(2222));
TESTASSERT(filter.match(ip_msg1));
TESTASSERT(!filter.match(ip_msg2));
printf("Test NAS Activate Dedicated EPS Bearer Context Request successfull\n");
return 0;
}
int tft_filter_test_single_remote_port()
{
srslte::log_filter log1("TFT");
log1.set_level(srslte::LOG_LEVEL_DEBUG);
log1.set_hex_limit(128);
srslte::byte_buffer_pool *pool = srslte::byte_buffer_pool::get_instance();
srslte::unique_byte_buffer_t ip_msg1, ip_msg2;
ip_msg1 = allocate_unique_buffer(*pool);
ip_msg2 = allocate_unique_buffer(*pool);
// Filter length: 3 bytes
// Filter type: Single local port
// Local port: 2222
uint8_t filter_message[3];
filter_message[0] = SINGLE_REMOTE_PORT_TYPE;
srslte::uint16_to_uint8(2001, &filter_message[1]);
// Set IP test message
ip_msg1->N_bytes = ip_message_len1;
memcpy(ip_msg1->msg, ip_tst_message1, ip_message_len1);
log1.info_hex(ip_msg1->msg, ip_msg1->N_bytes, "IP test message\n");
// Set IP test message
ip_msg2->N_bytes = ip_message_len2;
memcpy(ip_msg2->msg, ip_tst_message2, ip_message_len1);
log1.info_hex(ip_msg2->msg, ip_msg2->N_bytes, "IP test message\n");
// Packet filter
LIBLTE_MME_PACKET_FILTER_STRUCT packet_filter;
packet_filter.dir = LIBLTE_MME_TFT_PACKET_FILTER_DIRECTION_BIDIRECTIONAL;
packet_filter.id = 1;
packet_filter.eval_precedence = 0;
packet_filter.filter_size = 3;
memcpy(packet_filter.filter, filter_message, 3);
srsue::tft_packet_filter_t filter(packet_filter);
// Check filter
TESTASSERT(filter.match(ip_msg1));
TESTASSERT(!filter.match(ip_msg2));
printf("Test NAS Activate Dedicated EPS Bearer Context Request successfull\n");
return 0;
@ -93,7 +160,11 @@ int tft_filter_test()
int main(int argc, char **argv)
{
if (tft_filter_test()) {
srslte::byte_buffer_pool::get_instance();
if (tft_filter_test_single_local_port()) {
return -1;
}
if (tft_filter_test_single_remote_port()) {
return -1;
}
srslte::byte_buffer_pool::cleanup();