Skip to content

Commit 379e71d

Browse files
rluboskartben
authored andcommitted
net: connection: Cleanup packet input processing
Part of the socket matching criteria for AF_PACKET family took place inside conn_raw_socket() function, and some of it was redundant with what already was checked in net_conn_packet_input(). Moreover, if the packet cloning for packet socket failed for whatever reason, the packet was reported as NET_DROP, which was confusing. Finally, conn_raw_socket() updated network stats, which didn't really work as net stats are only collected for UDP/TCP protocols and not for L2 level protocols. Therefore, cleanup the processing by: * Moving all socket matching criteria into net_conn_packet_input() for clarity, * Drop unneeded net stats functions, * Clarify NET_DROP strategy for packet socket input. net_conn_packet_input() should only be responsible for delivering packets to respective packet sockets, it should not decide whether to drop the packet or not - it's L2/L3 processing code responsibility. Therefore, assume this function forwards packet for further processing by default, and only allow small optimization to return NET_OK if the packet socket was really the only endpoint in the system. * And finally, since now conn_raw_socket() responsibility was to clone the packet for the respective socket, and was almost identical to a corresponding function for raw IP sockets, unify the two functions. Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
1 parent 32f3ce3 commit 379e71d

File tree

2 files changed

+40
-94
lines changed

2 files changed

+40
-94
lines changed

subsys/net/ip/connection.c

Lines changed: 39 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -590,69 +590,41 @@ static bool is_iface_matching(struct net_conn *conn, struct net_pkt *pkt)
590590
return (net_pkt_iface(pkt) == net_context_get_iface(conn->context));
591591
}
592592

593-
#if defined(CONFIG_NET_SOCKETS_PACKET)
594-
static enum net_verdict conn_raw_socket(struct net_pkt *pkt,
595-
struct net_conn *conn, uint8_t proto)
593+
#if defined(CONFIG_NET_SOCKETS_PACKET) || defined(CONFIG_NET_SOCKETS_INET_RAW)
594+
static void conn_raw_socket_deliver(struct net_pkt *pkt, struct net_conn *conn,
595+
bool is_ip)
596596
{
597-
enum net_sock_type type = net_context_get_type(conn->context);
598-
599-
if (proto == ETH_P_ALL) {
600-
if ((type == SOCK_DGRAM && !net_pkt_is_l2_processed(pkt)) ||
601-
(type == SOCK_RAW && net_pkt_is_l2_processed(pkt))) {
602-
return NET_CONTINUE;
603-
}
604-
}
605-
606-
/*
607-
* After l2 processed only deliver protocol matched pkt,
608-
* unless the connection protocol is all packets
609-
*/
610-
if (type == SOCK_DGRAM && net_pkt_is_l2_processed(pkt) &&
611-
conn->proto != ETH_P_ALL &&
612-
conn->proto != net_pkt_ll_proto_type(pkt)) {
613-
return NET_CONTINUE;
614-
}
615-
616-
if (!(conn->flags & NET_CONN_LOCAL_ADDR_SET)) {
617-
return NET_CONTINUE;
618-
}
619-
620-
struct net_if *pkt_iface = net_pkt_iface(pkt);
621-
struct sockaddr_ll *local;
622597
struct net_pkt *raw_pkt;
598+
struct net_pkt_cursor cur;
623599

624-
local = (struct sockaddr_ll *)&conn->local_addr;
625-
626-
if (local->sll_ifindex != net_if_get_by_iface(pkt_iface)) {
627-
return NET_CONTINUE;
628-
}
600+
net_pkt_cursor_backup(pkt, &cur);
601+
net_pkt_cursor_init(pkt);
629602

630-
NET_DBG("[%p] raw match found cb %p ud %p", conn, conn->cb,
631-
conn->user_data);
603+
NET_DBG("[%p] raw%s match found cb %p ud %p", conn, is_ip ? " IP" : "",
604+
conn->cb, conn->user_data);
632605

633606
raw_pkt = net_pkt_clone(pkt, K_MSEC(CONFIG_NET_CONN_PACKET_CLONE_TIMEOUT));
634-
if (!raw_pkt) {
635-
net_stats_update_per_proto_drop(pkt_iface, proto);
636-
NET_WARN("pkt cloning failed, pkt %p dropped", pkt);
637-
return NET_DROP;
607+
if (raw_pkt == NULL) {
608+
NET_WARN("pkt cloning failed, pkt %p not delivered", pkt);
609+
goto out;
638610
}
639611

640612
if (conn->cb(conn, raw_pkt, NULL, NULL, conn->user_data) == NET_DROP) {
641-
net_stats_update_per_proto_drop(pkt_iface, proto);
642613
net_pkt_unref(raw_pkt);
643-
} else {
644-
net_stats_update_per_proto_recv(pkt_iface, proto);
645614
}
646615

647-
return NET_OK;
616+
out:
617+
net_pkt_cursor_restore(pkt, &cur);
648618
}
619+
#endif /* defined(CONFIG_NET_SOCKETS_PACKET) || defined(CONFIG_NET_SOCKETS_INET_RAW) */
649620

621+
#if defined(CONFIG_NET_SOCKETS_PACKET)
650622
enum net_verdict net_conn_packet_input(struct net_pkt *pkt, uint16_t proto)
651623
{
652-
bool raw_pkt_delivered = false;
624+
bool raw_sock_found = false;
653625
bool raw_pkt_continue = false;
626+
struct sockaddr_ll *local;
654627
struct net_conn *conn;
655-
enum net_verdict ret;
656628

657629
/* Only accept input with AF_PACKET family. */
658630
if (net_pkt_family(pkt) != AF_PACKET) {
@@ -685,7 +657,11 @@ enum net_verdict net_conn_packet_input(struct net_pkt *pkt, uint16_t proto)
685657
* with this packet regardless the result.
686658
*/
687659
raw_pkt_continue = true;
688-
continue;
660+
continue; /* L2 not processed yet */
661+
}
662+
663+
if (conn->type == SOCK_RAW && net_pkt_is_l2_processed(pkt)) {
664+
continue; /* L2 already processed */
689665
}
690666

691667
if (conn->proto == 0) {
@@ -703,36 +679,34 @@ enum net_verdict net_conn_packet_input(struct net_pkt *pkt, uint16_t proto)
703679

704680
/* Apply protocol-specific matching criteria... */
705681

706-
ret = conn_raw_socket(pkt, conn, proto);
707-
if (ret == NET_DROP) {
708-
k_mutex_unlock(&conn_lock);
709-
goto drop;
710-
} else if (ret == NET_OK) {
711-
raw_pkt_delivered = true;
682+
if (!(conn->flags & NET_CONN_LOCAL_ADDR_SET)) {
683+
continue;
712684
}
713-
}
714685

715-
k_mutex_unlock(&conn_lock);
686+
local = (struct sockaddr_ll *)&conn->local_addr;
687+
if (local->sll_ifindex != net_if_get_by_iface(net_pkt_iface(pkt))) {
688+
continue;
689+
}
716690

717-
if (raw_pkt_continue) {
718-
/* When there is open connection different than AF_PACKET this
719-
* packet shall be also handled in the upper net stack layers.
720-
*/
721-
return NET_CONTINUE;
691+
conn_raw_socket_deliver(pkt, conn, false);
692+
693+
raw_sock_found = true;
722694
}
723695

724-
if (raw_pkt_delivered) {
696+
k_mutex_unlock(&conn_lock);
697+
698+
if (!raw_pkt_continue && raw_sock_found) {
725699
/* As one or more raw socket packets have already been delivered
726700
* in the loop above, report NET_OK.
727701
*/
728702
net_pkt_unref(pkt);
729703
return NET_OK;
730704
}
731705

732-
drop:
733-
net_stats_update_per_proto_drop(net_pkt_iface(pkt), proto);
734-
735-
return NET_DROP;
706+
/* When there is open connection different than AF_PACKET this
707+
* packet shall be also handled in the upper net stack layers.
708+
*/
709+
return NET_CONTINUE;
736710
}
737711
#else
738712
enum net_verdict net_conn_packet_input(struct net_pkt *pkt, uint16_t proto)
@@ -745,30 +719,6 @@ enum net_verdict net_conn_packet_input(struct net_pkt *pkt, uint16_t proto)
745719
#endif /* defined(CONFIG_NET_SOCKETS_PACKET) */
746720

747721
#if defined(CONFIG_NET_SOCKETS_INET_RAW)
748-
static void conn_raw_ip_socket(struct net_pkt *pkt, struct net_conn *conn)
749-
{
750-
struct net_pkt *raw_pkt;
751-
struct net_pkt_cursor cur;
752-
753-
net_pkt_cursor_backup(pkt, &cur);
754-
net_pkt_cursor_init(pkt);
755-
756-
NET_DBG("[%p] raw IP match found cb %p ud %p", conn, conn->cb,
757-
conn->user_data);
758-
759-
raw_pkt = net_pkt_clone(pkt, K_MSEC(CONFIG_NET_CONN_PACKET_CLONE_TIMEOUT));
760-
if (raw_pkt == NULL) {
761-
goto out;
762-
}
763-
764-
if (conn->cb(conn, raw_pkt, NULL, NULL, conn->user_data) == NET_DROP) {
765-
net_pkt_unref(raw_pkt);
766-
}
767-
768-
out:
769-
net_pkt_cursor_restore(pkt, &cur);
770-
}
771-
772722
enum net_verdict net_conn_raw_ip_input(struct net_pkt *pkt,
773723
union net_ip_header *ip_hdr,
774724
uint8_t proto)
@@ -810,7 +760,7 @@ enum net_verdict net_conn_raw_ip_input(struct net_pkt *pkt,
810760
continue; /* wrong local address */
811761
}
812762

813-
conn_raw_ip_socket(pkt, conn);
763+
conn_raw_socket_deliver(pkt, conn, true);
814764
}
815765

816766
k_mutex_unlock(&conn_lock);

subsys/net/ip/packet_socket.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,5 @@ enum net_verdict net_packet_socket_input(struct net_pkt *pkt, uint16_t proto)
4343

4444
net_pkt_set_family(pkt, orig_family);
4545

46-
if (net_verdict == NET_DROP) {
47-
return NET_CONTINUE;
48-
} else {
49-
return net_verdict;
50-
}
46+
return net_verdict;
5147
}

0 commit comments

Comments
 (0)