Skip to content

Commit 307694f

Browse files
rluboskartben
authored andcommitted
net: sockets: Remove support for AF_PACKET/IPPROTO_RAW combination
IPPROTO_RAW is not a valid protocol type for AF_PACKET sockets, which should only use IEEE 802.3 protocol numbers. Therefore remove support for this type of sockets. As an alternative, users can use AF_PACKET/SOCK_DGRAM or AF_INET(6)/SOCK_RAW, depending on the actual use case. Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
1 parent 403d921 commit 307694f

File tree

9 files changed

+28
-266
lines changed

9 files changed

+28
-266
lines changed

subsys/net/ip/connection.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ enum net_verdict net_conn_input(struct net_pkt *pkt,
698698
return NET_DROP;
699699
}
700700
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET) && pkt_family == AF_PACKET) {
701-
if (proto != ETH_P_ALL && proto != IPPROTO_RAW) {
701+
if (proto != ETH_P_ALL) {
702702
return NET_DROP;
703703
}
704704
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_CAN) && pkt_family == AF_CAN) {
@@ -787,12 +787,12 @@ enum net_verdict net_conn_input(struct net_pkt *pkt,
787787
/* Is the candidate connection matching the packet's protocol within the family? */
788788
if (conn->proto != proto) {
789789
/* For packet socket data, the proto is set to ETH_P_ALL
790-
* or IPPROTO_RAW but the listener might have a specific
791-
* protocol set. This is ok and let the packet pass this
792-
* check in this case.
790+
* but the listener might have a specific protocol set.
791+
* This is ok and let the packet pass this check in this
792+
* case.
793793
*/
794794
if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET) && pkt_family == AF_PACKET) {
795-
if (proto != ETH_P_ALL && proto != IPPROTO_RAW) {
795+
if (proto != ETH_P_ALL) {
796796
continue; /* wrong protocol */
797797
}
798798
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_INET_RAW) && raw_ip_pkt) {
@@ -813,17 +813,15 @@ enum net_verdict net_conn_input(struct net_pkt *pkt,
813813
* targets AF_PACKET sockets.
814814
*
815815
* All AF_PACKET connections will receive the packet if
816-
* their socket type and - in case of IPPROTO - protocol
817-
* also matches.
816+
* their socket type and protocol also matches.
818817
*/
819-
if (proto == ETH_P_ALL) {
820-
/* We shall continue with ETH_P_ALL to IPPROTO_RAW: */
818+
if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET_DGRAM) &&
819+
(proto == ETH_P_ALL) && !net_pkt_is_l2_processed(pkt)) {
820+
/* We shall continue with ETH_P_ALL to AF_PACKET/SOCK_DGRAM: */
821821
raw_pkt_continue = true;
822822
}
823823

824-
/* With IPPROTO_RAW deliver only if protocol match: */
825-
if ((proto == ETH_P_ALL && conn->proto != IPPROTO_RAW) ||
826-
conn->proto == proto) {
824+
if (proto == ETH_P_ALL) {
827825
enum net_verdict ret = conn_raw_socket(pkt, conn, proto);
828826

829827
if (ret == NET_DROP) {

subsys/net/ip/net_context.c

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,53 +2666,25 @@ static int context_sendto(struct net_context *context,
26662666

26672667
net_pkt_cursor_init(pkt);
26682668

2669-
if (net_context_get_proto(context) == IPPROTO_RAW) {
2670-
char type = (NET_IPV6_HDR(pkt)->vtc & 0xf0);
2671-
struct sockaddr_ll *ll_addr;
2672-
2673-
/* Set the family to pkt if detected */
2674-
switch (type) {
2675-
case 0x60:
2676-
net_pkt_set_family(pkt, AF_INET6);
2677-
net_pkt_set_ll_proto_type(pkt, NET_ETH_PTYPE_IPV6);
2678-
break;
2679-
case 0x40:
2680-
net_pkt_set_family(pkt, AF_INET);
2681-
net_pkt_set_ll_proto_type(pkt, NET_ETH_PTYPE_IP);
2682-
break;
2683-
default:
2684-
/* Not IP traffic, let it go forward as it is */
2685-
ll_addr = (struct sockaddr_ll *)dst_addr;
2686-
2687-
net_pkt_set_ll_proto_type(pkt,
2688-
ntohs(ll_addr->sll_protocol));
2689-
break;
2690-
}
2669+
struct sockaddr_ll_ptr *ll_src_addr;
2670+
struct sockaddr_ll *ll_dst_addr;
26912671

2692-
/* Pass to L2: */
2693-
ret = net_try_send_data(pkt, timeout);
2694-
} else {
2695-
struct sockaddr_ll_ptr *ll_src_addr;
2696-
struct sockaddr_ll *ll_dst_addr;
2697-
2698-
/* The destination address is set in remote for this
2699-
* socket type.
2700-
*/
2701-
ll_dst_addr = (struct sockaddr_ll *)&context->remote;
2702-
ll_src_addr = (struct sockaddr_ll_ptr *)&context->local;
2672+
/* The destination address is set in remote for this
2673+
* socket type.
2674+
*/
2675+
ll_dst_addr = (struct sockaddr_ll *)&context->remote;
2676+
ll_src_addr = (struct sockaddr_ll_ptr *)&context->local;
27032677

2704-
(void)net_linkaddr_set(net_pkt_lladdr_dst(pkt),
2705-
ll_dst_addr->sll_addr,
2706-
sizeof(struct net_eth_addr));
2707-
(void)net_linkaddr_set(net_pkt_lladdr_src(pkt),
2708-
ll_src_addr->sll_addr,
2709-
sizeof(struct net_eth_addr));
2678+
(void)net_linkaddr_set(net_pkt_lladdr_dst(pkt),
2679+
ll_dst_addr->sll_addr,
2680+
sizeof(struct net_eth_addr));
2681+
(void)net_linkaddr_set(net_pkt_lladdr_src(pkt),
2682+
ll_src_addr->sll_addr,
2683+
sizeof(struct net_eth_addr));
27102684

2711-
net_pkt_set_ll_proto_type(pkt,
2712-
ntohs(ll_dst_addr->sll_protocol));
2685+
net_pkt_set_ll_proto_type(pkt, ntohs(ll_dst_addr->sll_protocol));
27132686

2714-
net_if_try_queue_tx(net_pkt_iface(pkt), pkt, timeout);
2715-
}
2687+
net_if_try_queue_tx(net_pkt_iface(pkt), pkt, timeout);
27162688
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_CAN) && family == AF_CAN &&
27172689
net_context_get_proto(context) == CAN_RAW) {
27182690
ret = context_write_data(pkt, buf, len, msghdr);

subsys/net/ip/net_core.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,6 @@ static inline enum net_verdict process_data(struct net_pkt *pkt,
127127

128128
if (IS_ENABLED(CONFIG_NET_IP) && (family == AF_INET || family == AF_INET6 ||
129129
family == AF_UNSPEC || family == AF_PACKET)) {
130-
/* L2 processed, now we can pass IPPROTO_RAW to packet socket:
131-
*/
132-
ret = net_packet_socket_input(pkt, IPPROTO_RAW);
133-
if (ret != NET_CONTINUE) {
134-
return ret;
135-
}
136-
137130
/* IP version and header length. */
138131
uint8_t vtc_vhl = NET_IPV6_HDR(pkt)->vtc & 0xf0;
139132

subsys/net/ip/net_if.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,6 @@ enum net_verdict net_if_try_send_data(struct net_if *iface, struct net_pkt *pkt,
505505
}
506506
#endif
507507

508-
/* Bypass the IP stack with SOCK_RAW/IPPROTO_RAW sockets */
509-
if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET) &&
510-
context && net_context_get_type(context) == SOCK_RAW &&
511-
net_context_get_proto(context) == IPPROTO_RAW) {
512-
goto done;
513-
}
514-
515508
/* Bypass the IP stack with AF_INET(6)/SOCK_RAW */
516509
if (context && net_context_get_type(context) == SOCK_RAW &&
517510
(net_context_get_family(context) == AF_INET ||

subsys/net/lib/sockets/sockets_packet.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ static int zpacket_socket(int family, int type, int proto)
5151
return -1;
5252
}
5353

54-
if (proto == 0) {
55-
if (type == SOCK_RAW) {
56-
proto = IPPROTO_RAW;
57-
}
58-
} else {
54+
if (proto != 0) {
5955
/* For example in Linux, the protocol parameter can be given
6056
* as htons(ETH_P_ALL) to receive all the network packets.
6157
* So convert the proto field back to host byte order so that
@@ -492,11 +488,10 @@ static bool packet_is_supported(int family, int type, int proto)
492488
proto = ntohs(proto);
493489
return proto == ETH_P_ALL
494490
|| proto == ETH_P_ECAT
495-
|| proto == ETH_P_IEEE802154
496-
|| proto == IPPROTO_RAW;
491+
|| proto == ETH_P_IEEE802154;
497492

498493
case SOCK_DGRAM:
499-
return proto > 0;
494+
return true;
500495

501496
default:
502497
return false;

tests/net/socket/af_packet_ipproto_raw/CMakeLists.txt

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/net/socket/af_packet_ipproto_raw/prj.conf

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/net/socket/af_packet_ipproto_raw/src/main.c

Lines changed: 0 additions & 146 deletions
This file was deleted.

tests/net/socket/af_packet_ipproto_raw/testcase.yaml

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)