net: introduce NET_IP config to efficiently mark conditional code

The code contained several repeated composite IPv4/v6 and UDP/TCP
preprocessor statements that can be simplified by introducing a hidden
NET_IP preprocessor constant that captures what probably is actually
"meant" by this code.

While we were on it we also used the new constant to further isolate
IP-specific code from non-IP specific generics.

Signed-off-by: Florian Grandel <jerico.dev@gmail.com>
diff --git a/include/zephyr/net/net_if.h b/include/zephyr/net/net_if.h
index e95c006..94fe533 100644
--- a/include/zephyr/net/net_if.h
+++ b/include/zephyr/net/net_if.h
@@ -371,8 +371,10 @@
  * @brief IP and other configuration related data for network interface.
  */
 struct net_if_config {
+#if defined(CONFIG_NET_IP)
 	/** IP address configuration setting */
 	struct net_if_ip ip;
+#endif
 
 #if defined(CONFIG_NET_DHCPV4) && defined(CONFIG_NET_NATIVE_IPV4)
 	struct net_if_dhcpv4 dhcpv4;
@@ -631,7 +633,7 @@
  *
  * @param iface Network interface
  *
- * @return True if IP offlining is active, false otherwise.
+ * @return True if IP offloading is active, false otherwise.
  */
 static inline bool net_if_is_ip_offloaded(struct net_if *iface)
 {
@@ -2250,6 +2252,12 @@
 	void (*init)(struct net_if *iface);
 };
 
+#if defined(CONFIG_NET_IP)
+#define NET_IF_IP_INIT .ip = {},
+#else
+#define NET_IF_IP_INIT
+#endif
+
 #if defined(CONFIG_NET_DHCPV4) && defined(CONFIG_NET_NATIVE_IPV4)
 #define NET_IF_DHCPV4_INIT .dhcpv4.state = NET_DHCPV4_DISABLED,
 #else
@@ -2258,8 +2266,7 @@
 
 #define NET_IF_CONFIG_INIT				\
 	.config = {					\
-		.ip = {					\
-		},					\
+		NET_IF_IP_INIT				\
 		NET_IF_DHCPV4_INIT			\
 	}
 
diff --git a/include/zephyr/net/net_pkt.h b/include/zephyr/net/net_pkt.h
index 858a573..a0e2c8e 100644
--- a/include/zephyr/net/net_pkt.h
+++ b/include/zephyr/net/net_pkt.h
@@ -131,7 +131,9 @@
 	sys_snode_t next;
 #endif
 
+#if defined(CONFIG_NET_IP)
 	uint8_t ip_hdr_len;	/* pre-filled in order to avoid func call */
+#endif
 
 	uint8_t overwrite  : 1;	/* Is packet content being overwritten? */
 
@@ -155,21 +157,22 @@
 	uint8_t forwarding : 1;	/* Are we forwarding this pkt
 				 * Used only if defined(CONFIG_NET_ROUTE)
 				 */
-	uint8_t family     : 3;	/* IPv4 vs IPv6 */
+	uint8_t family     : 3;	/* Address family, see net_ip.h */
 
 	union {
+#if defined(CONFIG_NET_IPV4_AUTO)
 		uint8_t ipv4_auto_arp_msg : 1; /* Is this pkt IPv4 autoconf ARP
-					     * message. Used only if
-					     * defined(CONFIG_NET_IPV4_AUTO).
+					     * message.
 					     * Note: family needs to be
 					     * AF_INET.
 					     */
+#endif
+#if defined(CONFIG_NET_LLDP)
 		uint8_t lldp_pkt          : 1; /* Is this pkt an LLDP message.
-					     * Used only if
-					     * defined(CONFIG_NET_LLDP).
 					     * Note: family needs to be
 					     * AF_UNSPEC.
 					     */
+#endif
 		uint8_t ppp_msg           : 1; /* This is a PPP message */
 	};
 
@@ -194,12 +197,17 @@
 				   * processed by the L2
 				   */
 
+#if defined(CONFIG_NET_IP)
 	union {
 		/* IPv6 hop limit or IPv4 ttl for this network packet.
 		 * The value is shared between IPv6 and IPv4.
 		 */
+#if defined(CONFIG_NET_IPV6)
 		uint8_t ipv6_hop_limit;
+#endif
+#if defined(CONFIG_NET_IPV4)
 		uint8_t ipv4_ttl;
+#endif
 	};
 
 	union {
@@ -210,6 +218,7 @@
 		uint16_t ipv6_ext_len; /* length of extension headers */
 #endif
 	};
+#endif /* CONFIG_NET_IP */
 
 	/** Network packet priority, can be left out in which case packet
 	 * is not prioritised.
@@ -387,12 +396,18 @@
 
 static inline uint8_t net_pkt_ip_hdr_len(struct net_pkt *pkt)
 {
+#if defined(CONFIG_NET_IP)
 	return pkt->ip_hdr_len;
+#else
+	return 0;
+#endif
 }
 
 static inline void net_pkt_set_ip_hdr_len(struct net_pkt *pkt, uint8_t len)
 {
+#if defined(CONFIG_NET_IP)
 	pkt->ip_hdr_len = len;
+#endif
 }
 
 static inline uint8_t net_pkt_sent(struct net_pkt *pkt)
diff --git a/subsys/net/Kconfig b/subsys/net/Kconfig
index 1b1bd93..0018075 100644
--- a/subsys/net/Kconfig
+++ b/subsys/net/Kconfig
@@ -60,12 +60,13 @@
 endif # NET_BUF
 
 config NETWORKING
-	bool "Link layer and IP networking support"
+	bool "Link layer and networking (including IP)"
 	select NET_BUF
 	select POLL
 	select ENTROPY_GENERATOR
 	help
-	  This option enabled generic link layer and IP networking support.
+	  This option enables generic link layer and networking support
+	  and is the basis for higher level protocols (e.g. IP protocols).
 
 if NETWORKING
 
diff --git a/subsys/net/ip/CMakeLists.txt b/subsys/net/ip/CMakeLists.txt
index 337e9a8..2778604 100644
--- a/subsys/net/ip/CMakeLists.txt
+++ b/subsys/net/ip/CMakeLists.txt
@@ -23,6 +23,7 @@
 zephyr_library_sources(net_context.c)
 zephyr_library_sources(net_pkt.c)
 zephyr_library_sources(net_tc.c)
+zephyr_library_sources_ifdef(CONFIG_NET_IP           connection.c)
 zephyr_library_sources_ifdef(CONFIG_NET_6LO          6lo.c)
 zephyr_library_sources_ifdef(CONFIG_NET_DHCPV4       dhcpv4.c)
 zephyr_library_sources_ifdef(CONFIG_NET_IPV4_AUTO    ipv4_autoconf.c)
@@ -34,10 +35,10 @@
 zephyr_library_sources_ifdef(CONFIG_NET_IPV6_FRAGMENT     ipv6_fragment.c)
 zephyr_library_sources_ifdef(CONFIG_NET_ROUTE        route.c)
 zephyr_library_sources_ifdef(CONFIG_NET_STATISTICS   net_stats.c)
-zephyr_library_sources_ifdef(CONFIG_NET_TCP          connection.c tcp.c)
+zephyr_library_sources_ifdef(CONFIG_NET_TCP          tcp.c)
 zephyr_library_sources_ifdef(CONFIG_NET_TEST_PROTOCOL           tp.c)
 zephyr_library_sources_ifdef(CONFIG_NET_TRICKLE      trickle.c)
-zephyr_library_sources_ifdef(CONFIG_NET_UDP          connection.c udp.c)
+zephyr_library_sources_ifdef(CONFIG_NET_UDP          udp.c)
 zephyr_library_sources_ifdef(CONFIG_NET_SOCKETS_PACKET  connection.c
                                                         packet_socket.c)
 zephyr_library_sources_ifdef(CONFIG_NET_SOCKETS_CAN  connection.c
diff --git a/subsys/net/ip/Kconfig b/subsys/net/ip/Kconfig
index d4a59fe..0db4b37 100644
--- a/subsys/net/ip/Kconfig
+++ b/subsys/net/ip/Kconfig
@@ -6,8 +6,13 @@
 
 menu "IP stack"
 
+# Hidden option enabled whenever an IP stack is available.
+config NET_IP
+	bool
+	default y if NET_IPV6 || NET_IPV4
+
 config NET_NATIVE
-	bool "Native IP stack"
+	bool "Native network stack support"
 	default y
 	help
 	  Enables Zephyr native IP stack. If you disable this, then
@@ -335,6 +340,7 @@
 
 config NET_TCP
 	bool "TCP"
+	depends on NET_IP
 	help
 	  The value depends on your network needs.
 
@@ -502,6 +508,7 @@
 config NET_UDP
 	bool "UDP"
 	default y
+	depends on NET_IP
 	help
 	  The value depends on your network needs.
 
diff --git a/subsys/net/ip/connection.h b/subsys/net/ip/connection.h
index b938275..81106a9 100644
--- a/subsys/net/ip/connection.h
+++ b/subsys/net/ip/connection.h
@@ -31,9 +31,12 @@
 struct net_conn_handle;
 
 /**
- * @brief Function that is called by connection subsystem when UDP/TCP
- * packet is received and which matches local and remote IP address
- * and port.
+ * @brief Function that is called by connection subsystem when a
+ * net packet is received which matches local and remote address
+ * (and port in case of TCP/UDP packets).
+ *
+ * The arguments ip_hdr and proto_hdr are NULL in case of non-IP
+ * protocols.
  */
 typedef enum net_verdict (*net_conn_cb_t)(struct net_conn *conn,
 					  struct net_pkt *pkt,
@@ -51,13 +54,13 @@
 	/** Internal slist node */
 	sys_snode_t node;
 
-	/** Remote IP address */
+	/** Remote socket address */
 	struct sockaddr remote_addr;
 
-	/** Local IP address */
+	/** Local socket address */
 	struct sockaddr local_addr;
 
-	/** Callback to be called when matching UDP packet is received */
+	/** Callback to be called when matching net packet is received */
 	net_conn_cb_t cb;
 
 	/** A pointer to the net_context corresponding to the connection.
@@ -79,16 +82,17 @@
 };
 
 /**
- * @brief Register a callback to be called when UDP/TCP packet
+ * @brief Register a callback to be called when a net packet
  * is received corresponding to received packet.
  *
- * @param proto Protocol for the connection (UDP or TCP or SOCK_RAW)
- * @param family Protocol family (AF_INET or AF_INET6 or AF_PACKET)
+ * @param proto Protocol for the connection (depends on the protocol
+ *              family, e.g. UDP/TCP in the case of AF_INET/AF_INET6)
+ * @param family Protocol family (AF_*)
  * @param remote_addr Remote address of the connection end point.
  * @param local_addr Local address of the connection end point.
  * @param remote_port Remote port of the connection end point.
  * @param local_port Local port of the connection end point.
- * @param cb Callback to be called
+ * @param cb Callback to be called when a matching net pkt is received
  * @param context net_context structure related to the connection.
  * @param user_data User data supplied by caller.
  * @param handle Connection handle that can be used when unregistering
@@ -173,8 +177,7 @@
  * the received packet. If corresponding IP protocol support is
  * disabled, the function will always return NET_DROP.
  */
-#if defined(CONFIG_NET_UDP) || defined(CONFIG_NET_TCP) || \
-	defined(CONFIG_NET_SOCKETS_PACKET) || defined(CONFIG_NET_SOCKETS_CAN)
+#if defined(CONFIG_NET_IP) || defined(CONFIG_NET_SOCKETS_PACKET) || defined(CONFIG_NET_SOCKETS_CAN)
 enum net_verdict net_conn_input(struct net_pkt *pkt,
 				union net_ip_header *ip_hdr,
 				uint8_t proto,
@@ -187,7 +190,7 @@
 {
 	return NET_DROP;
 }
-#endif /* CONFIG_NET_UDP || CONFIG_NET_TCP  || CONFIG_NET_SOCKETS_PACKET */
+#endif /* CONFIG_NET_IP  || CONFIG_NET_SOCKETS_PACKET */
 
 /**
  * @typedef net_conn_foreach_cb_t
diff --git a/subsys/net/ip/net_shell.c b/subsys/net/ip/net_shell.c
index c37dce1..810552e 100644
--- a/subsys/net/ip/net_shell.c
+++ b/subsys/net/ip/net_shell.c
@@ -347,7 +347,7 @@
 #if defined(CONFIG_NET_VLAN)
 	struct ethernet_context *eth_ctx;
 #endif
-#if defined(CONFIG_NET_IPV4) || defined(CONFIG_NET_IPV6)
+#if defined(CONFIG_NET_IP)
 	struct net_if_addr *unicast;
 	struct net_if_mcast_addr *mcast;
 #endif
@@ -356,7 +356,7 @@
 	int ret;
 #endif
 	const char *extra;
-#if defined(CONFIG_NET_IPV4) || defined(CONFIG_NET_IPV6)
+#if defined(CONFIG_NET_IP)
 	int i, count;
 #endif
 
@@ -3835,7 +3835,7 @@
 	return 0;
 }
 
-#if defined(CONFIG_NET_IPV6) || defined(CONFIG_NET_IPV4)
+#if defined(CONFIG_NET_IP)
 
 K_SEM_DEFINE(ping_timeout, 0, 1);
 static const struct shell *shell_for_ping;
@@ -4108,7 +4108,7 @@
 
 	return res;
 }
-#endif /* CONFIG_NET_IPV6 || CONFIG_NET_IPV4 */
+#endif /* CONFIG_NET_IP */
 
 static int cmd_net_ping(const struct shell *shell, size_t argc, char *argv[])
 {
diff --git a/subsys/net/ip/utils.c b/subsys/net/ip/utils.c
index 5897ac3..dbff096 100644
--- a/subsys/net/ip/utils.c
+++ b/subsys/net/ip/utils.c
@@ -551,6 +551,7 @@
 	return sum;
 }
 
+#if defined(CONFIG_NET_IP)
 uint16_t net_calc_chksum(struct net_pkt *pkt, uint8_t proto)
 {
 	size_t len = 0U;
@@ -598,6 +599,7 @@
 
 	return ~sum;
 }
+#endif
 
 #if defined(CONFIG_NET_IPV4)
 uint16_t net_calc_chksum_ipv4(struct net_pkt *pkt)
@@ -626,7 +628,7 @@
 }
 #endif /* CONFIG_NET_IPV4_IGMP */
 
-#if defined(CONFIG_NET_IPV6) || defined(CONFIG_NET_IPV4)
+#if defined(CONFIG_NET_IP)
 static bool convert_port(const char *buf, uint16_t *port)
 {
 	unsigned long tmp;
@@ -643,7 +645,7 @@
 
 	return true;
 }
-#endif /* CONFIG_NET_IPV6 || CONFIG_NET_IPV4 */
+#endif /* CONFIG_NET_IP */
 
 #if defined(CONFIG_NET_IPV6)
 static bool parse_ipv6(const char *str, size_t str_len,
diff --git a/subsys/net/lib/sockets/getaddrinfo.c b/subsys/net/lib/sockets/getaddrinfo.c
index 0755412..ed9b1e2 100644
--- a/subsys/net/lib/sockets/getaddrinfo.c
+++ b/subsys/net/lib/sockets/getaddrinfo.c
@@ -20,8 +20,7 @@
 #include <zephyr/net/socket_offload.h>
 #include <zephyr/syscall_handler.h>
 
-#if defined(CONFIG_DNS_RESOLVER) || \
-	defined(CONFIG_NET_IPV6) || defined(CONFIG_NET_IPV4)
+#if defined(CONFIG_DNS_RESOLVER) || defined(CONFIG_NET_IP)
 #define ANY_RESOLVER
 
 #if defined(CONFIG_DNS_RESOLVER_AI_MAX_ENTRIES)
@@ -320,7 +319,7 @@
 
 #endif /* defined(CONFIG_DNS_RESOLVER) */
 
-#if defined(CONFIG_NET_IPV6) || defined(CONFIG_NET_IPV4)
+#if defined(CONFIG_NET_IP)
 static int try_resolve_literal_addr(const char *host, const char *service,
 				    const struct zsock_addrinfo *hints,
 				    struct zsock_addrinfo *res)
@@ -396,7 +395,7 @@
 
 	return 0;
 }
-#endif /* defined(CONFIG_NET_IPV6) || defined(CONFIG_NET_IPV4) */
+#endif /* CONFIG_NET_IP */
 
 int zsock_getaddrinfo(const char *host, const char *service,
 		      const struct zsock_addrinfo *hints,
@@ -415,7 +414,7 @@
 	}
 #endif
 
-#if defined(CONFIG_NET_IPV6) || defined(CONFIG_NET_IPV4)
+#if defined(CONFIG_NET_IP)
 	/* Resolve literal address even if DNS is not available */
 	if (ret) {
 		ret = try_resolve_literal_addr(host, service, hints, *res);