1 /** @file
2  @brief Generic connection handling.
3 
4  This is not to be included by the application.
5  */
6 
7 /*
8  * Copyright (c) 2016 Intel Corporation
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  */
12 
13 #ifndef __CONNECTION_H
14 #define __CONNECTION_H
15 
16 #include <zephyr/types.h>
17 
18 #include <zephyr/sys/util.h>
19 
20 #include <zephyr/net/net_context.h>
21 #include <zephyr/net/net_core.h>
22 #include <zephyr/net/net_ip.h>
23 #include <zephyr/net/net_pkt.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 struct net_conn;
30 
31 struct net_conn_handle;
32 
33 /**
34  * @brief Function that is called by connection subsystem when a
35  * net packet is received which matches local and remote address
36  * (and port in case of TCP/UDP packets).
37  *
38  * The arguments ip_hdr and proto_hdr are NULL in case of non-IP
39  * protocols.
40  */
41 typedef enum net_verdict (*net_conn_cb_t)(struct net_conn *conn,
42 					  struct net_pkt *pkt,
43 					  union net_ip_header *ip_hdr,
44 					  union net_proto_header *proto_hdr,
45 					  void *user_data);
46 
47 /**
48  * @brief Information about a connection in the system.
49  *
50  * Stores the connection information.
51  *
52  */
53 struct net_conn {
54 	/** Internal slist node */
55 	sys_snode_t node;
56 
57 	/** Remote socket address */
58 	struct sockaddr remote_addr;
59 
60 	/** Local socket address */
61 	struct sockaddr local_addr;
62 
63 	/** Callback to be called when matching net packet is received */
64 	net_conn_cb_t cb;
65 
66 	/** A pointer to the net_context corresponding to the connection.
67 	 *  Can be NULL if no net_context is associated.
68 	 */
69 	struct net_context *context;
70 
71 	/** Possible user to pass to the callback */
72 	void *user_data;
73 
74 	/** Connection protocol */
75 	uint16_t proto;
76 
77 	/** Protocol family */
78 	uint8_t family;
79 
80 	/** Flags for the connection */
81 	uint8_t flags;
82 
83 	/** Is v4-mapping-to-v6 enabled for this connection */
84 	uint8_t v6only : 1;
85 };
86 
87 /**
88  * @brief Register a callback to be called when a net packet
89  * is received corresponding to received packet.
90  *
91  * @param proto Protocol for the connection (depends on the protocol
92  *              family, e.g. UDP/TCP in the case of AF_INET/AF_INET6)
93  * @param family Protocol family (AF_*)
94  * @param remote_addr Remote address of the connection end point.
95  * @param local_addr Local address of the connection end point.
96  * @param remote_port Remote port of the connection end point.
97  * @param local_port Local port of the connection end point.
98  * @param cb Callback to be called when a matching net pkt is received
99  * @param context net_context structure related to the connection.
100  * @param user_data User data supplied by caller.
101  * @param handle Connection handle that can be used when unregistering
102  *
103  * @return Return 0 if the registration succeed, <0 otherwise.
104  */
105 #if defined(CONFIG_NET_NATIVE)
106 int net_conn_register(uint16_t proto, uint8_t family,
107 		      const struct sockaddr *remote_addr,
108 		      const struct sockaddr *local_addr,
109 		      uint16_t remote_port,
110 		      uint16_t local_port,
111 		      struct net_context *context,
112 		      net_conn_cb_t cb,
113 		      void *user_data,
114 		      struct net_conn_handle **handle);
115 #else
net_conn_register(uint16_t proto,uint8_t family,const struct sockaddr * remote_addr,const struct sockaddr * local_addr,uint16_t remote_port,uint16_t local_port,struct net_context * context,net_conn_cb_t cb,void * user_data,struct net_conn_handle ** handle)116 static inline int net_conn_register(uint16_t proto, uint8_t family,
117 				    const struct sockaddr *remote_addr,
118 				    const struct sockaddr *local_addr,
119 				    uint16_t remote_port,
120 				    uint16_t local_port,
121 				    struct net_context *context,
122 				    net_conn_cb_t cb,
123 				    void *user_data,
124 				    struct net_conn_handle **handle)
125 {
126 	ARG_UNUSED(proto);
127 	ARG_UNUSED(family);
128 	ARG_UNUSED(remote_addr);
129 	ARG_UNUSED(local_addr);
130 	ARG_UNUSED(remote_port);
131 	ARG_UNUSED(local_port);
132 	ARG_UNUSED(cb);
133 	ARG_UNUSED(context);
134 	ARG_UNUSED(user_data);
135 	ARG_UNUSED(handle);
136 
137 	return -ENOTSUP;
138 }
139 #endif
140 
141 /**
142  * @brief Unregister connection handler.
143  *
144  * @param handle Handle from registering.
145  *
146  * @return Return 0 if the unregistration succeed, <0 otherwise.
147  */
148 #if defined(CONFIG_NET_NATIVE)
149 int net_conn_unregister(struct net_conn_handle *handle);
150 #else
net_conn_unregister(struct net_conn_handle * handle)151 static inline int net_conn_unregister(struct net_conn_handle *handle)
152 {
153 	ARG_UNUSED(handle);
154 
155 	return -ENOTSUP;
156 }
157 #endif
158 
159 /**
160  * @brief Change the callback and user_data for a registered connection
161  * handle.
162  *
163  * @param handle A handle registered with net_conn_register()
164  * @param cb Callback to be called
165  * @param user_data User data supplied by caller.
166  *
167  * @return Return 0 if the the change succeed, <0 otherwise.
168  */
169 int net_conn_change_callback(struct net_conn_handle *handle,
170 			     net_conn_cb_t cb, void *user_data);
171 
172 /**
173  * @brief Called by net_core.c when a network packet is received.
174  *
175  * @param pkt Network packet holding received data
176  * @param proto Protocol for the connection
177  *
178  * @return NET_OK if the packet was consumed, NET_DROP if
179  * the packet parsing failed and the caller should handle
180  * the received packet. If corresponding IP protocol support is
181  * disabled, the function will always return NET_DROP.
182  */
183 #if defined(CONFIG_NET_IP) || defined(CONFIG_NET_CONNECTION_SOCKETS)
184 enum net_verdict net_conn_input(struct net_pkt *pkt,
185 				union net_ip_header *ip_hdr,
186 				uint8_t proto,
187 				union net_proto_header *proto_hdr);
188 #else
net_conn_input(struct net_pkt * pkt,union net_ip_header * ip_hdr,uint8_t proto,union net_proto_header * proto_hdr)189 static inline enum net_verdict net_conn_input(struct net_pkt *pkt,
190 					      union net_ip_header *ip_hdr,
191 					      uint8_t proto,
192 					      union net_proto_header *proto_hdr)
193 {
194 	return NET_DROP;
195 }
196 #endif /* CONFIG_NET_IP || CONFIG_NET_CONNECTION_SOCKETS */
197 
198 /**
199  * @typedef net_conn_foreach_cb_t
200  * @brief Callback used while iterating over network connection
201  * handlers.
202  *
203  * @param conn A valid pointer on current network connection handler.
204  * @param user_data A valid pointer on some user data or NULL
205  */
206 typedef void (*net_conn_foreach_cb_t)(struct net_conn *conn, void *user_data);
207 
208 /**
209  * @brief Go through all the network connection handlers and call callback
210  * for each network connection handler.
211  *
212  * @param cb User supplied callback function to call.
213  * @param user_data User specified data.
214  */
215 void net_conn_foreach(net_conn_foreach_cb_t cb, void *user_data);
216 
217 #if defined(CONFIG_NET_NATIVE)
218 void net_conn_init(void);
219 #else
220 #define net_conn_init(...)
221 #endif
222 
223 #ifdef __cplusplus
224 }
225 #endif
226 
227 #endif /* __CONNECTION_H */
228