zephyr/subsys/net/ip/packet_socket.c

52 lines
1 KiB
C
Raw Permalink Normal View History

/** @file
* @brief Packet Sockets related functions
*/
/*
* Copyright (c) 2019 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(net_sockets_raw, CONFIG_NET_SOCKETS_LOG_LEVEL);
#include <errno.h>
#include <zephyr/net/net_pkt.h>
#include <zephyr/net/net_context.h>
#include <zephyr/net/ethernet.h>
#include <zephyr/net/dsa.h>
#include "connection.h"
#include "packet_socket.h"
enum net_verdict net_packet_socket_input(struct net_pkt *pkt, uint8_t proto)
{
sa_family_t orig_family;
enum net_verdict net_verdict;
#if defined(CONFIG_NET_DSA)
/*
* For DSA the master port is not supporting raw packets. Only the
* lan1..3 are working with them.
*/
if (dsa_is_port_master(net_pkt_iface(pkt))) {
return NET_CONTINUE;
}
#endif
orig_family = net_pkt_family(pkt);
net_pkt_set_family(pkt, AF_PACKET);
net_verdict = net_conn_input(pkt, NULL, proto, NULL);
net_pkt_set_family(pkt, orig_family);
net: core: clean up inbound packet handling The net_core:process_data() and connection:net_conn_input() methods are the central network packet reception pipeline which: 1) guide network packets through all network layers, 2) decode, validate and filter packages along the way and 3) distribute packages to connections/sockets on all layers. This code seems to have grown complex and rather cluttered over time as all protocols, layers and socket implementations meet there in one single place. The code also reveals its origin as a pure IP stack which makes it hard to introduce non-IP protocols and their supporting socket infrastructure in a modularized way. For an outside contributor it seems almost impossible to add another protocol, protocol layer, filter rule or socket implementation without breaking things. This change doesn't try to solve all issues at once. It focuses exclusively on aspects that maintain backwards compatibility: * Improve modularization and encapsulation on implementation level by disentangling code that mixes up layers, protocols and socket implementations. * Make IP just one protocol among others by removing assymmetry in protocol handling logic and introduce preprocessor markup so that IP-specific code can be eliminated by the preprocessor if not needed. * Use preprocessor markup to delineate hook points for future modularization or expansion without introducing structural changes (as this would almost certainly break the API). * Reduce cyclomatic complexity, use positive rather than negative logic, improve variable naming, replace if/elseif/else blocks with switches, reduce variable span, introduce inline comments where code does not speak for itself, etc. as much as possible to make the code overall more human-friendly. Background: These are preparative steps for the introduction of IEEE 802.15.RAW sockets, DGRAM sockets and sockets bound to PAN IDs and device addresses similar to what the Linux kernel does. Signed-off-by: Florian Grandel <jerico.dev@gmail.com>
2022-08-06 00:12:51 +02:00
if (net_verdict == NET_DROP) {
return NET_CONTINUE;
net: core: clean up inbound packet handling The net_core:process_data() and connection:net_conn_input() methods are the central network packet reception pipeline which: 1) guide network packets through all network layers, 2) decode, validate and filter packages along the way and 3) distribute packages to connections/sockets on all layers. This code seems to have grown complex and rather cluttered over time as all protocols, layers and socket implementations meet there in one single place. The code also reveals its origin as a pure IP stack which makes it hard to introduce non-IP protocols and their supporting socket infrastructure in a modularized way. For an outside contributor it seems almost impossible to add another protocol, protocol layer, filter rule or socket implementation without breaking things. This change doesn't try to solve all issues at once. It focuses exclusively on aspects that maintain backwards compatibility: * Improve modularization and encapsulation on implementation level by disentangling code that mixes up layers, protocols and socket implementations. * Make IP just one protocol among others by removing assymmetry in protocol handling logic and introduce preprocessor markup so that IP-specific code can be eliminated by the preprocessor if not needed. * Use preprocessor markup to delineate hook points for future modularization or expansion without introducing structural changes (as this would almost certainly break the API). * Reduce cyclomatic complexity, use positive rather than negative logic, improve variable naming, replace if/elseif/else blocks with switches, reduce variable span, introduce inline comments where code does not speak for itself, etc. as much as possible to make the code overall more human-friendly. Background: These are preparative steps for the introduction of IEEE 802.15.RAW sockets, DGRAM sockets and sockets bound to PAN IDs and device addresses similar to what the Linux kernel does. Signed-off-by: Florian Grandel <jerico.dev@gmail.com>
2022-08-06 00:12:51 +02:00
} else {
return net_verdict;
}
}