Skip to content
Permalink
Browse files

net/connection: Final cleanup

- renaming functions to better names
- reordering functions place (register, then unregister for instance)
- centralizing logs to relevant place

Fixes #8722

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
  • Loading branch information...
tbursztyka authored and jukkar committed Apr 25, 2019
1 parent d652a1e commit 9a6e3012dafc75d453c1e7b93620be1593cb10b7
Showing with 120 additions and 133 deletions.
  1. +120 −133 subsys/net/ip/connection.c
@@ -56,6 +56,60 @@ static struct net_conn conns[CONFIG_NET_MAX_CONN];
static sys_slist_t conn_unused;
static sys_slist_t conn_used;

#if (CONFIG_NET_CONN_LOG_LEVEL >= LOG_LEVEL_DBG)
static inline
void conn_register_debug(struct net_conn *conn,
u16_t remote_port, u16_t local_port)
{
char dst[NET_IPV6_ADDR_LEN];
char src[NET_IPV6_ADDR_LEN];

if (conn->flags & NET_CONN_REMOTE_ADDR_SET) {
if (IS_ENABLED(CONFIG_NET_IPV6) &&
conn->family == AF_INET6) {
snprintk(dst, sizeof(dst), "%s",
log_strdup(net_sprint_ipv6_addr(
&net_sin6(&conn->remote_addr)->sin6_addr)));
} else if (IS_ENABLED(CONFIG_NET_IPV4) &&
conn->family == AF_INET) {
snprintk(dst, sizeof(dst), "%s",
log_strdup(net_sprint_ipv4_addr(
&net_sin(&conn->remote_addr)->sin_addr)));
} else {
snprintk(dst, sizeof(dst), "%s", "?");
}
} else {
snprintk(dst, sizeof(dst), "%s", "-");
}

if (conn->flags & NET_CONN_LOCAL_ADDR_SET) {
if (IS_ENABLED(CONFIG_NET_IPV6) &&
conn->family == AF_INET6) {
snprintk(src, sizeof(src), "%s",
log_strdup(net_sprint_ipv6_addr(
&net_sin6(&conn->local_addr)->sin6_addr)));
} else if (IS_ENABLED(CONFIG_NET_IPV4) &&
conn->family == AF_INET) {
snprintk(src, sizeof(src), "%s",
log_strdup(net_sprint_ipv4_addr(
&net_sin(&conn->local_addr)->sin_addr)));
} else {
snprintk(src, sizeof(src), "%s", "?");
}
} else {
snprintk(src, sizeof(src), "%s", "-");
}

NET_DBG("[%p/%d/%u/0x%02x] remote %s/%u ",
conn, conn->proto, conn->family, conn->flags,
log_strdup(dst), remote_port);
NET_DBG(" local %s/%u cb %p ud %p",
log_strdup(src), local_port, conn->cb, conn->user_data);
}
#else
#define conn_register_debug(...)
#endif /* (CONFIG_NET_CONN_LOG_LEVEL >= LOG_LEVEL_DBG) */

static struct net_conn *conn_get_unused(void)
{
sys_snode_t *node;
@@ -84,102 +138,8 @@ static void conn_set_unused(struct net_conn *conn)
sys_slist_prepend(&conn_unused, &conn->node);
}

int net_conn_unregister(struct net_conn_handle *handle)
{
struct net_conn *conn = (struct net_conn *)handle;

if (conn < &conns[0] || conn > &conns[CONFIG_NET_MAX_CONN]) {
return -EINVAL;
}

if (!(conn->flags & NET_CONN_IN_USE)) {
return -ENOENT;
}

NET_DBG("Connection handler %p removed", conn);

sys_slist_find_and_remove(&conn_used, &conn->node);

conn_set_unused(conn);

return 0;
}

int net_conn_change_callback(struct net_conn_handle *handle,
net_conn_cb_t cb, void *user_data)
{
struct net_conn *conn = (struct net_conn *)handle;

if (conn < &conns[0] || conn > &conns[CONFIG_NET_MAX_CONN]) {
return -EINVAL;
}

if (!(conn->flags & NET_CONN_IN_USE)) {
return -ENOENT;
}

NET_DBG("[%zu] connection handler %p changed callback",
conn - conns, conn);

conn->cb = cb;
conn->user_data = user_data;

return 0;
}

static inline
void prepare_register_debug_print(char *dst, int dst_len,
char *src, int src_len,
const struct sockaddr *remote_addr,
const struct sockaddr *local_addr)
{
if (remote_addr && remote_addr->sa_family == AF_INET6) {
if (IS_ENABLED(CONFIG_NET_IPV6)) {
snprintk(dst, dst_len, "%s",
log_strdup(net_sprint_ipv6_addr(
&net_sin6(remote_addr)->sin6_addr)));
} else {
snprintk(dst, dst_len, "%s", "?");
}

} else if (remote_addr && remote_addr->sa_family == AF_INET) {
if (IS_ENABLED(CONFIG_NET_IPV4)) {
snprintk(dst, dst_len, "%s",
log_strdup(net_sprint_ipv4_addr(
&net_sin(remote_addr)->sin_addr)));
} else {
snprintk(dst, dst_len, "%s", "?");
}

} else {
snprintk(dst, dst_len, "%s", "-");
}

if (local_addr && local_addr->sa_family == AF_INET6) {
if (IS_ENABLED(CONFIG_NET_IPV6)) {
snprintk(src, src_len, "%s",
log_strdup(net_sprint_ipv6_addr(
&net_sin6(local_addr)->sin6_addr)));
} else {
snprintk(src, src_len, "%s", "?");
}

} else if (local_addr && local_addr->sa_family == AF_INET) {
if (IS_ENABLED(CONFIG_NET_IPV4)) {
snprintk(src, src_len, "%s",
log_strdup(net_sprint_ipv4_addr(
&net_sin(local_addr)->sin_addr)));
} else {
snprintk(src, src_len, "%s", "?");
}

} else {
snprintk(src, src_len, "%s", "-");
}
}

/* Check if we already have identical connection handler installed. */
static struct net_conn *find_conn_handler(u16_t proto, u8_t family,
static struct net_conn *conn_find_handler(u16_t proto, u8_t family,
const struct sockaddr *remote_addr,
const struct sockaddr *local_addr,
u16_t remote_port,
@@ -288,7 +248,7 @@ int net_conn_register(u16_t proto, u8_t family,
struct net_conn *conn;
u8_t flags = 0U;

conn = find_conn_handler(proto, family, remote_addr, local_addr,
conn = conn_find_handler(proto, family, remote_addr, local_addr,
remote_port, local_port);
if (conn) {
NET_ERR("Identical connection handler %p already found.", conn);
@@ -377,40 +337,67 @@ int net_conn_register(u16_t proto, u8_t family,
conn->proto = proto;
conn->family = family;

if (CONFIG_NET_CONN_LOG_LEVEL >= LOG_LEVEL_DBG) {
char dst[NET_IPV6_ADDR_LEN];
char src[NET_IPV6_ADDR_LEN];

prepare_register_debug_print(dst, sizeof(dst),
src, sizeof(src),
remote_addr,
local_addr);

NET_DBG("[%p/%d/%u/%u/0x%02x] remote %p/%s/%u ", conn,
local_addr ? local_addr->sa_family : AF_UNSPEC,
proto, family, flags, remote_addr,
log_strdup(dst), remote_port);
NET_DBG(" local %p/%s/%u cb %p ud %p",
local_addr, log_strdup(src), local_port,
cb, user_data);
}

if (handle) {
*handle = (struct net_conn_handle *)conn;
}

conn_set_used(conn);

conn_register_debug(conn, remote_port, local_port);

return 0;
error:
conn_set_unused(conn);
return -EINVAL;
}

static bool check_addr(struct net_pkt *pkt,
union net_ip_header *ip_hdr,
struct sockaddr *addr,
bool is_remote)
int net_conn_unregister(struct net_conn_handle *handle)
{
struct net_conn *conn = (struct net_conn *)handle;

if (conn < &conns[0] || conn > &conns[CONFIG_NET_MAX_CONN]) {
return -EINVAL;
}

if (!(conn->flags & NET_CONN_IN_USE)) {
return -ENOENT;
}

NET_DBG("Connection handler %p removed", conn);

sys_slist_find_and_remove(&conn_used, &conn->node);

conn_set_unused(conn);

return 0;
}

int net_conn_change_callback(struct net_conn_handle *handle,
net_conn_cb_t cb, void *user_data)
{
struct net_conn *conn = (struct net_conn *)handle;

if (conn < &conns[0] || conn > &conns[CONFIG_NET_MAX_CONN]) {
return -EINVAL;
}

if (!(conn->flags & NET_CONN_IN_USE)) {
return -ENOENT;
}

NET_DBG("[%zu] connection handler %p changed callback",
conn - conns, conn);

conn->cb = cb;
conn->user_data = user_data;

return 0;
}

static bool conn_addr_cmp(struct net_pkt *pkt,
union net_ip_header *ip_hdr,
struct sockaddr *addr,
bool is_remote)
{
if (addr->sa_family != net_pkt_family(pkt)) {
return false;
@@ -458,7 +445,7 @@ static bool check_addr(struct net_pkt *pkt,
return true;
}

static inline void send_icmp_error(struct net_pkt *pkt)
static inline void conn_send_icmp_error(struct net_pkt *pkt)
{
if (IS_ENABLED(CONFIG_NET_IPV6) && net_pkt_family(pkt) == AF_INET6) {
net_icmpv6_send_error(pkt, NET_ICMPV6_DST_UNREACH,
@@ -470,14 +457,14 @@ static inline void send_icmp_error(struct net_pkt *pkt)
}
}

static bool is_valid_packet(struct net_pkt *pkt,
union net_ip_header *ip_hdr,
u16_t src_port,
u16_t dst_port)
static bool conn_are_end_points_valid(struct net_pkt *pkt,
union net_ip_header *ip_hdr,
u16_t src_port,
u16_t dst_port)
{
bool my_src_addr = false;

/* For AF_PACKET family, we are not not parsing headers. */
/* For AF_PACKET family, we are not parsing headers. */
if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET) &&
net_pkt_family(pkt) == AF_PACKET) {
return true;
@@ -542,8 +529,8 @@ enum net_verdict net_conn_input(struct net_pkt *pkt,
return NET_DROP;
}

if (!is_valid_packet(pkt, ip_hdr, src_port, dst_port)) {
NET_DBG("Dropping invalid packet");
if (!conn_are_end_points_valid(pkt, ip_hdr, src_port, dst_port)) {
NET_DBG("Dropping invalid src/dst end-points packet");
return NET_DROP;
}

@@ -582,17 +569,17 @@ enum net_verdict net_conn_input(struct net_pkt *pkt,
}

if (conn->flags & NET_CONN_REMOTE_ADDR_SET) {
if (!check_addr(pkt, ip_hdr,
&conn->remote_addr,
true)) {
if (!conn_addr_cmp(pkt, ip_hdr,
&conn->remote_addr,
true)) {
continue;
}
}

if (conn->flags & NET_CONN_LOCAL_ADDR_SET) {
if (!check_addr(pkt, ip_hdr,
&conn->local_addr,
false)) {
if (!conn_addr_cmp(pkt, ip_hdr,
&conn->local_addr,
false)) {
continue;
}
}
@@ -649,7 +636,7 @@ enum net_verdict net_conn_input(struct net_pkt *pkt,
net_pkt_family(pkt) == AF_PACKET) {
;
} else {
send_icmp_error(pkt);
conn_send_icmp_error(pkt);

if (IS_ENABLED(CONFIG_NET_TCP) && proto == IPPROTO_TCP) {
net_stats_update_tcp_seg_connrst(net_pkt_iface(pkt));

0 comments on commit 9a6e301

Please sign in to comment.
You can’t perform that action at this time.