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 	/** Connection type */
78 	enum net_sock_type type;
79 
80 	/** Protocol family */
81 	uint8_t family;
82 
83 	/** Flags for the connection */
84 	uint8_t flags;
85 
86 	/** Is v4-mapping-to-v6 enabled for this connection */
87 	uint8_t v6only : 1;
88 };
89 
90 /**
91  * @brief Register a callback to be called when a net packet
92  * is received corresponding to received packet.
93  *
94  * @param proto Protocol for the connection (depends on the protocol
95  *              family, e.g. UDP/TCP in the case of AF_INET/AF_INET6)
96  * @param type Connection type (SOCK_STREAM/DGRAM/RAW)
97  * @param family Protocol family (AF_*)
98  * @param remote_addr Remote address of the connection end point.
99  * @param local_addr Local address of the connection end point.
100  * @param remote_port Remote port of the connection end point.
101  * @param local_port Local port of the connection end point.
102  * @param cb Callback to be called when a matching net pkt is received
103  * @param context net_context structure related to the connection.
104  * @param user_data User data supplied by caller.
105  * @param handle Connection handle that can be used when unregistering
106  *
107  * @return Return 0 if the registration succeed, <0 otherwise.
108  */
109 #if defined(CONFIG_NET_NATIVE)
110 int net_conn_register(uint16_t proto, enum net_sock_type type, uint8_t family,
111 		      const struct sockaddr *remote_addr,
112 		      const struct sockaddr *local_addr,
113 		      uint16_t remote_port,
114 		      uint16_t local_port,
115 		      struct net_context *context,
116 		      net_conn_cb_t cb,
117 		      void *user_data,
118 		      struct net_conn_handle **handle);
119 #else
net_conn_register(uint16_t proto,enum net_sock_type type,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)120 static inline int net_conn_register(uint16_t proto, enum net_sock_type type,
121 				    uint8_t family,
122 				    const struct sockaddr *remote_addr,
123 				    const struct sockaddr *local_addr,
124 				    uint16_t remote_port,
125 				    uint16_t local_port,
126 				    struct net_context *context,
127 				    net_conn_cb_t cb,
128 				    void *user_data,
129 				    struct net_conn_handle **handle)
130 {
131 	ARG_UNUSED(proto);
132 	ARG_UNUSED(family);
133 	ARG_UNUSED(remote_addr);
134 	ARG_UNUSED(local_addr);
135 	ARG_UNUSED(remote_port);
136 	ARG_UNUSED(local_port);
137 	ARG_UNUSED(cb);
138 	ARG_UNUSED(context);
139 	ARG_UNUSED(user_data);
140 	ARG_UNUSED(handle);
141 
142 	return -ENOTSUP;
143 }
144 #endif
145 
146 /**
147  * @brief Unregister connection handler.
148  *
149  * @param handle Handle from registering.
150  *
151  * @return Return 0 if the unregistration succeed, <0 otherwise.
152  */
153 #if defined(CONFIG_NET_NATIVE)
154 int net_conn_unregister(struct net_conn_handle *handle);
155 #else
net_conn_unregister(struct net_conn_handle * handle)156 static inline int net_conn_unregister(struct net_conn_handle *handle)
157 {
158 	ARG_UNUSED(handle);
159 
160 	return -ENOTSUP;
161 }
162 #endif
163 
164 /**
165  * @brief Update the callback, user data, remote address, and port
166  * for a registered connection handle.
167  *
168  * @param handle A handle registered with net_conn_register()
169  * @param cb Callback to be called
170  * @param user_data User data supplied by caller.
171  * @param remote_addr Remote address
172  * @param remote_port Remote port
173  * @param local_addr Local address
174  * @param local_port Local port
175  *
176  * @return Return 0 if the change succeed, <0 otherwise.
177  */
178 int net_conn_update(struct net_conn_handle *handle,
179 		    net_conn_cb_t cb,
180 		    void *user_data,
181 		    const struct sockaddr *remote_addr,
182 		    uint16_t remote_port,
183 		    const struct sockaddr *local_addr,
184 		    uint16_t local_port);
185 
186 /**
187  * @brief Called by net_core.c when a network packet is received
188  *        (before L3 processing).
189  *
190  * @param pkt Network packet holding received data
191  * @param proto LL protocol for the connection
192  *
193  * @return NET_OK if the packet was consumed, NET_CONTINUE if the packet should
194  * be processed further in the stack.
195  */
196 enum net_verdict net_conn_packet_input(struct net_pkt *pkt, uint16_t proto);
197 
198 /**
199  * @brief Called by net_core.c when an IP packet is received
200  *        (before L4 processing).
201  *
202  * @param pkt Network packet holding received data
203  * @param ip_hdr A pointer to the IP header within the packet
204  * @param proto L4 protocol for the connection
205  *
206  * @return NET_CONTINUE if the packet should be further processed in the stack.
207  */
208 enum net_verdict net_conn_raw_ip_input(struct net_pkt *pkt,
209 				       union net_ip_header *ip_hdr,
210 				       uint8_t proto);
211 
212 /**
213  * @brief Called by net_core.c when a CAN packet is received.
214  *
215  * @param pkt Network packet holding received data
216  * @param proto Protocol for the connection
217  *
218  * @return NET_OK if the packet was consumed, NET_DROP if the packet parsing
219  * failed and the packet should be discarded.
220  */
221 enum net_verdict net_conn_can_input(struct net_pkt *pkt, uint8_t proto);
222 
223 /**
224  * @brief Called by net_core.c when a network packet is received (after L4
225  *        processing).
226  *
227  * @param pkt Network packet holding received data
228  * @param ip_hdr A pointer to the IP header within the packet
229  * @param proto Protocol for the connection
230  * @param proto_hdr A pointer to the L4 protocol header within the packet
231  *
232  * @return NET_OK if the packet was consumed, NET_DROP if
233  * the packet parsing failed and the caller should handle
234  * the received packet. If corresponding IP protocol support is
235  * disabled, the function will always return NET_DROP.
236  */
237 #if defined(CONFIG_NET_IP) || defined(CONFIG_NET_CONNECTION_SOCKETS)
238 enum net_verdict net_conn_input(struct net_pkt *pkt,
239 				union net_ip_header *ip_hdr,
240 				uint8_t proto,
241 				union net_proto_header *proto_hdr);
242 #else
net_conn_input(struct net_pkt * pkt,union net_ip_header * ip_hdr,uint8_t proto,union net_proto_header * proto_hdr)243 static inline enum net_verdict net_conn_input(struct net_pkt *pkt,
244 					      union net_ip_header *ip_hdr,
245 					      uint8_t proto,
246 					      union net_proto_header *proto_hdr)
247 {
248 	return NET_DROP;
249 }
250 #endif /* CONFIG_NET_IP || CONFIG_NET_CONNECTION_SOCKETS */
251 
252 /**
253  * @typedef net_conn_foreach_cb_t
254  * @brief Callback used while iterating over network connection
255  * handlers.
256  *
257  * @param conn A valid pointer on current network connection handler.
258  * @param user_data A valid pointer on some user data or NULL
259  */
260 typedef void (*net_conn_foreach_cb_t)(struct net_conn *conn, void *user_data);
261 
262 /**
263  * @brief Go through all the network connection handlers and call callback
264  * for each network connection handler.
265  *
266  * @param cb User supplied callback function to call.
267  * @param user_data User specified data.
268  */
269 void net_conn_foreach(net_conn_foreach_cb_t cb, void *user_data);
270 
271 #if defined(CONFIG_NET_NATIVE)
272 void net_conn_init(void);
273 #else
274 #define net_conn_init(...)
275 #endif
276 
277 #ifdef __cplusplus
278 }
279 #endif
280 
281 #endif /* __CONNECTION_H */
282