• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "wm_config.h"
16 
17 #if TLS_CONFIG_SOCKET_RAW
18 
19 #include "tls_netconn.h"
20 #include "wm_debug.h"
21 #include <string.h>
22 #include "ip_addr.h"
23 #include "wm_mem.h"
24 
25 #define FOUR 4
26 
27 #define TCP_WRITE_FLAG_COPY 0x01
28 #define TCP_WRITE_FLAG_MORE 0x02
29 
30 struct tls_netconn *p_net_conn[TLS_MAX_NETCONN_NUM];
31 #if CONN_SEM_NOT_FREE
32 sys_sem_t conn_op_completed[TLS_MAX_NETCONN_NUM] = {NULL};
33 #endif
34 
35 static void net_tcp_err_cb(void *arg, err_t err);
36 static err_t net_tcp_poll_cb(void *arg, struct tcp_pcb *pcb);
37 static void net_free_socket(int socketno);
38 
39 u32 current_src_ip = 0;
tls_net_set_sourceip(u32 ipvalue)40 void tls_net_set_sourceip(u32 ipvalue)
41 {
42     current_src_ip = ipvalue;
43 }
44 
tls_net_get_sourceip(void)45 u32 tls_net_get_sourceip(void)
46 {
47     return current_src_ip;
48 }
49 
get_server_conn(struct tls_netconn * conn)50 struct tls_netconn *get_server_conn(struct tls_netconn *conn)
51 {
52     struct tls_netconn *server_conn = NULL;
53 
54     do {
55         server_conn = dl_list_first(&conn->list, struct tls_netconn, list);
56         conn = server_conn;
57     } while ((server_conn != NULL) && server_conn->client);
58 
59     return server_conn;
60 }
61 
net_alloc_socket(struct tls_netconn * conn)62 static struct tls_netconn *net_alloc_socket(struct tls_netconn *conn)
63 {
64     int sock = -1, i = 0, j = 0;
65     u32 cpu_sr;
66     struct tls_netconn *conn_t = NULL;
67 
68     for (i = 0; i < TLS_MAX_NETCONN_NUM; i++) {
69         if (p_net_conn[i] == NULL) {
70             sock = i;
71             break;
72         }
73     }
74 
75     if (i == TLS_MAX_NETCONN_NUM) {
76         TLS_DBGPRT_ERR("\nnet_alloc_socket error\n");
77         return NULL;
78     }
79 
80     if (conn != NULL) {
81         j = dl_list_len(&conn->list);
82         if (j >= FOUR) {
83             TLS_DBGPRT_ERR("list len > 4\n");
84             sock = -1;
85         }
86     }
87 
88     if (sock < 0) {
89         TLS_DBGPRT_ERR("sock < 0\n");
90         return NULL;
91     }
92     cpu_sr = tls_os_set_critical();
93     conn_t = tls_mem_alloc(sizeof(struct tls_netconn));
94     tls_os_release_critical(cpu_sr);
95     if (conn_t != NULL) {
96         p_net_conn[sock] = conn_t;
97         memset(conn_t, 0, sizeof(struct tls_netconn));
98         conn_t->used = true;
99         conn_t->state = NETCONN_STATE_NONE;
100         conn_t->skt_num = sock + 1;  /* TLS_MAX_NETCONN_NUM + */
101         dl_list_init(&conn_t->list);
102 #if CONN_SEM_NOT_FREE
103         TLS_DBGPRT_INFO("conn_op_completed[%d]=%x\n", sock, (u32)conn_op_completed[sock]);
104         if (conn_op_completed[sock] == NULL) {
105             if (sys_sem_new(&conn_op_completed[sock], 0) != ERR_OK) {
106                 net_free_socket(conn_t->skt_num);
107             }
108         }
109 #else
110         if (sys_sem_new(&conn_t->op_completed, 0) != ERR_OK) {
111             net_free_socket(conn_t->skt_num);
112         }
113 #endif
114     }
115     TLS_DBGPRT_INFO("net_alloc_socket conn ptr = 0x%x\n", (u32)conn_t);
116     return conn_t;
117 }
118 
net_free_socket(int socketno)119 static void net_free_socket(int socketno)
120 {
121     int index;
122     u32 cpu_sr;
123     struct tls_netconn *conn = NULL;
124 
125     conn = tls_net_get_socket(socketno);
126     if (conn == NULL || TRUE != conn->used) {
127         TLS_DBGPRT_ERR("\nconn=%x,used=%d\n", (u32)conn, conn->used);
128         return;
129     }
130     TLS_DBGPRT_INFO("conn ptr = 0x%x\n", (u32)conn);
131 #if !CONN_SEM_NOT_FREE
132     if (conn->op_completed != NULL) {
133         sys_sem_free(&conn->op_completed);
134     }
135 #endif
136     conn->used = false;
137     if (conn->client && conn->list.prev != NULL && conn->list.prev != &conn->list) {
138         TLS_DBGPRT_INFO("del from list.\n");
139         cpu_sr = tls_os_set_critical();
140         dl_list_del(&conn->list);
141         tls_os_release_critical(cpu_sr);
142     }
143     index = conn->skt_num - 1;    /* TLS_MAX_NETCONN_NUM */
144     if (conn->pcb.tcp) {
145         tcp_close(conn->pcb.tcp);
146         conn->pcb.tcp = NULL;
147     }
148     tls_mem_free(conn);
149     cpu_sr = tls_os_set_critical();
150     conn = NULL;
151     p_net_conn[index] = NULL;
152     tls_os_release_critical(cpu_sr);
153 }
154 
net_send_event_to_hostif(struct tls_netconn * conn,int event)155 static void net_send_event_to_hostif (struct tls_netconn *conn, int event)
156 {
157     struct tls_socket_desc *skt_desc = conn->skd;
158     TLS_DBGPRT_INFO("skt_desc->state_changed: 0x%x, event=%d\n", (u32)skt_desc->state_changed, event);
159     if (skt_desc->state_changed) {
160         skt_desc->state_changed(conn->skt_num, event, conn->state);
161     }
162 }
163 
net_tcp_close_connect(int socketno)164 static void net_tcp_close_connect(int socketno)
165 {
166 }
167 
net_tcp_poll_cb(void * arg,struct tcp_pcb * pcb)168 static err_t net_tcp_poll_cb(void *arg, struct tcp_pcb *pcb)
169 {
170     return ERR_OK;
171 }
172 
net_tcp_err_cb(void * arg,err_t err)173 static void net_tcp_err_cb(void *arg, err_t err)
174 {
175     struct tls_netconn *conn = NULL;
176     struct tcp_pcb *pcb = NULL;
177     int socketno = (int)arg;
178     u8 event = NET_EVENT_TCP_CONNECT_FAILED;
179 
180     conn = tls_net_get_socket(socketno);
181     if (conn == NULL || TRUE != conn->used) {
182         TLS_DBGPRT_ERR("\nconn=%x,used=%d\n", (u32)conn, conn->used);
183         return;
184     }
185     pcb = conn->pcb.tcp;
186     TLS_DBGPRT_INFO("tcp err = %d, pcb==%x, conn==%x, skt==%x\n", err, (u32)pcb, (u32)conn, (u32)conn->skd);
187 
188     if (pcb) {
189         tcp_arg(pcb, NULL);
190         tcp_sent(pcb, NULL);
191         tcp_recv(pcb, NULL);
192         tcp_err(pcb, NULL);
193         if (!conn->client) {
194             tcp_accept(pcb, NULL);
195         }
196         if (err == ERR_OK) {
197             tcp_close(pcb);
198         }
199         if (conn->state != NETCONN_STATE_NONE) {
200             conn->state = NETCONN_STATE_NONE;
201             event = NET_EVENT_TCP_DISCONNECT;
202         }
203 
204         net_send_event_to_hostif (conn, event);
205         if (conn->skd->errf != NULL) {
206             conn->skd->errf(conn->skt_num, err);
207         }
208         conn->pcb.tcp = NULL;
209         net_free_socket(socketno);
210     }
211 }
212 
213 #if (RAW_SOCKET_USE_CUSTOM_PBUF)
raw_sk_alloc_pbuf_custom(void)214 static struct raw_sk_pbuf_custom *raw_sk_alloc_pbuf_custom(void)
215 {
216     return (struct raw_sk_pbuf_custom*)mem_malloc(sizeof(struct raw_sk_pbuf_custom));
217 }
218 
raw_sk_free_pbuf_custom(struct raw_sk_pbuf_custom * p)219 static void raw_sk_free_pbuf_custom(struct raw_sk_pbuf_custom* p)
220 {
221     if (p != NULL) {
222         mem_free(p);
223         p = NULL;
224     }
225 }
226 
raw_sk_free_pbuf_custom_fn(struct pbuf * p)227 static void raw_sk_free_pbuf_custom_fn(struct pbuf *p)
228 {
229     struct raw_sk_pbuf_custom *pcr = (struct raw_sk_pbuf_custom *)p;
230 
231     if (p != NULL) {
232         if (TRUE == ((struct tls_netconn *)pcr->conn)->used &&
233             pcr->pcb == ((struct tls_netconn *)pcr->conn)->pcb.tcp) {
234             tcp_recved((struct tcp_pcb *)pcr->pcb, p->tot_len);
235         }
236 
237         if (pcr->original != NULL) {
238             pbuf_free(pcr->original);
239         }
240         raw_sk_free_pbuf_custom(pcr);
241     }
242 }
243 #endif
244 
245 /**
246  * Receive callback function for TCP connect.
247  */
net_tcp_recv_cb(void * arg,struct tcp_pcb * pcb,struct pbuf * p,err_t err)248 static err_t net_tcp_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
249 {
250     return;
251 }
252 
253 /**
254  * tcp connnect callback
255  */
net_tcp_connect_cb(void * arg,struct tcp_pcb * pcb,err_t err)256 static err_t net_tcp_connect_cb(void *arg, struct tcp_pcb *pcb, err_t err)
257 {
258     struct tls_netconn *conn;
259     err_t  err_ret = ERR_OK;
260     int socketno = -1;
261 
262     socketno = (int)arg;
263     conn = tls_net_get_socket(socketno);
264     if (conn == NULL || conn->used != TRUE) {
265         TLS_DBGPRT_ERR("\nconn=%x,used=%d\n", (u32)conn, conn->used);
266         tcp_abort(pcb);
267         return ERR_ABRT;
268     }
269 
270     if ((conn->proto == TLS_NETCONN_TCP) && (err == ERR_OK)) {
271         TLS_DBGPRT_INFO("net_tcp_connect_cb =====> state : %d\n", pcb->state);
272         conn->state = NETCONN_STATE_CONNECTED;
273         net_send_event_to_hostif(conn, NET_EVENT_TCP_CONNECTED);
274     } else {
275         TLS_DBGPRT_INFO("the err is =%d\n", err);
276     }
277 
278     if (conn->skd != NULL && conn->skd->connf != NULL) {
279         err_ret = conn->skd->connf(conn->skt_num, err);
280         if (err_ret == ERR_ABRT) {
281             tcp_abort(pcb);
282         }
283         return err_ret;
284     }
285 
286     return err;
287 }
288 
289 /**
290  * Accept callback function for TCP netconns.
291  */
net_tcp_accept_cb(void * arg,struct tcp_pcb * newpcb,err_t err)292 static err_t net_tcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err)
293 {
294     return;
295 }
296 
297 /**
298  * Start create TCP connection
299  */
net_tcp_start(struct tls_netconn * conn)300 static err_t net_tcp_start(struct tls_netconn *conn)
301 {
302     return;
303 }
304 
305 /**
306  * Start create UDP connection
307  */
net_udp_start(struct tls_netconn * conn)308 static err_t net_udp_start(struct tls_netconn *conn)
309 {
310     return ERR_OK;
311 }
312 
net_skt_tcp_send(struct tls_net_msg * net_msg)313 static err_t net_skt_tcp_send(struct tls_net_msg *net_msg)
314 {
315     struct tcp_pcb *pcb = NULL;
316     struct tls_netconn *conn;
317     err_t err;
318 
319     conn = tls_net_get_socket(net_msg->skt_no);
320     if (conn == NULL || TRUE != conn->used) {
321         TLS_DBGPRT_ERR("conn =%x,used=%d\n", (u32)conn, conn->used);
322         return ERR_ARG;
323     }
324     pcb = conn->pcb.tcp;
325     /*
326         When tcp error occured, lwip will delete the pcb and sometimes GSKT.
327         This function maybe registered by GSKT_TimerSend, so we must check if GSKT has been delted!!!
328     */
329     err = tcp_write(pcb, net_msg->dataptr, net_msg->len, TCP_WRITE_FLAG_COPY);
330     if (err == ERR_OK) {
331         tcp_output(pcb);
332     } else {
333     }
334     return err;
335 }
336 
337 /**
338  * Send data on a UDP pcb
339  */
net_do_send(void * ctx)340 static void net_do_send(void *ctx)
341 {
342     struct tls_net_msg *msg = (struct tls_net_msg *)ctx;
343     struct tls_netconn *conn = NULL;
344     struct pbuf *p;
345     int socketno = msg->skt_no;
346 
347     p = msg->p;
348     conn = tls_net_get_socket(socketno);
349     if (conn == NULL || TRUE != conn->used) {
350         TLS_DBGPRT_ERR("conn =%x,used=%d\n", (u32)conn, conn->used);
351         if (p) {
352             pbuf_free(p);
353         }
354 #if CONN_SEM_NOT_FREE
355         sys_sem_signal(&conn_op_completed[socketno - 1]);
356 #endif
357         return;
358     }
359 #if LWIP_CHECKSUM_ON_COPY
360     if (ip_addr_isany(&msg->addr)) {
361         msg->err = udp_send_chksum(conn->pcb.udp, p, 0, 0);
362     } else {
363         msg->err = udp_sendto_chksum(conn->pcb.udp, p, &msg->addr, msg->port, 0, 0);
364     }
365 #else /* LWIP_CHECKSUM_ON_COPY */
366     if (ip_addr_isany(&msg->addr)) {
367         msg->err = udp_send(conn->pcb.udp, p);
368     } else {
369         msg->err = udp_sendto(conn->pcb.udp, p, &msg->addr, msg->port);
370     }
371 #endif /* LWIP_CHECKSUM_ON_COPY */
372 
373     pbuf_free(p);
374 #if CONN_SEM_NOT_FREE
375     sys_sem_signal(&conn_op_completed[socketno - 1]);
376 #else
377     conn = tls_net_get_socket(socketno);
378     if (conn && TRUE == conn->used) {
379         sys_sem_signal(&conn->op_completed);
380     }
381 #endif
382 }
383 
do_create_connect(void * ctx)384 static void do_create_connect(void *ctx)
385 {
386     struct tls_net_msg *net_msg = (struct tls_net_msg *)ctx;
387     struct tls_netconn *conn;
388     err_t  err;
389     int socketno = -1;
390     socketno = net_msg->skt_no;
391     conn = tls_net_get_socket(socketno);
392     if (conn == NULL || TRUE != conn->used) {
393         tls_mem_free(net_msg);
394         TLS_DBGPRT_ERR("\nconn=%x,usde=%d\n", (u32)conn, conn->used);
395         return;
396     }
397     TLS_DBGPRT_INFO("conn ptr = 0x%x, conn->skt_num=%d, conn->client=%d\n", (u32)conn, conn->skt_num, conn->client);
398 
399     switch (conn->proto) {
400         case TLS_NETCONN_UDP:
401             err = net_udp_start(conn);
402             if (err != ERR_OK) {
403                 conn->state = NETCONN_STATE_NONE;
404                 net_send_event_to_hostif (conn, NET_EVENT_UDP_START_FAILED);
405                 net_free_socket(socketno);
406             } else {
407                 conn->state = NETCONN_STATE_CONNECTED;
408                 net_send_event_to_hostif (conn, NET_EVENT_UDP_START);
409             }
410             break;
411         case TLS_NETCONN_TCP:
412             err = net_tcp_start(conn);
413             if (err != ERR_OK) {
414                 conn->state = NETCONN_STATE_NONE;
415                 net_send_event_to_hostif (conn, NET_EVENT_TCP_CONNECT_FAILED);
416                 net_free_socket(socketno);
417             } else {
418                 if (!conn->client) {
419                     net_send_event_to_hostif (conn, NET_EVENT_TCP_CONNECTED);
420                 }
421             }
422             break;
423         default:
424             /* Unsupported netconn type, e.g. protocol disabled */
425             break;
426     }
427     tls_mem_free(net_msg);
428     return;
429 }
430 
do_close_connect(void * ctx)431 static void do_close_connect(void *ctx)
432 {
433     struct tls_net_msg *net_msg = (struct tls_net_msg *)ctx;
434     struct tls_netconn *conn = NULL;
435     struct tls_netconn *client_conn;
436     int socketno = net_msg->skt_no;
437     int i;
438     int sktNums[TLS_MAX_SOCKET_NUM] = {-1}; /* 2*TLS_MAX_NETCONN_NUM */
439 
440     conn = tls_net_get_socket(socketno);
441     if (conn == NULL || TRUE != conn->used) {
442         TLS_DBGPRT_ERR("conn==%x,used=%d\n", (u32)conn, conn->used);
443         tls_mem_free(net_msg);
444         return;
445     }
446 
447     switch (conn->proto) {
448         case TLS_NETCONN_UDP:
449             if (conn->pcb.udp != NULL) {
450                 udp_remove(conn->pcb.udp);
451                 conn->pcb.udp = NULL;
452             }
453             conn->state = NETCONN_STATE_CLOSED;
454             break;
455         case TLS_NETCONN_TCP:
456             if (!conn->client) {
457                 /* it's a server, close connected client */
458                 i = 0;
459                 dl_list_for_each(client_conn, &conn->list, struct tls_netconn, list) {
460                     if (client_conn->used) {
461                         client_conn->state = NETCONN_STATE_CLOSED;
462                         sktNums[i++] = client_conn->skt_num;
463                     }
464                 }
465 
466                 while (i-- > 0) {
467                     net_tcp_close_connect(sktNums[i]);
468                 }
469             }
470 
471             conn->state = NETCONN_STATE_CLOSED;
472             net_tcp_close_connect(socketno);
473             break;
474         default:
475             break;
476     }
477     net_free_socket(socketno);
478     tls_mem_free(net_msg);
479 }
480 
tls_socket_create(struct tls_socket_desc * skd)481 int tls_socket_create(struct tls_socket_desc *skd)
482 {
483     return;
484 }
485 
tls_socket_get_status(u8 socket,u8 * buf,u32 bufsize)486 int tls_socket_get_status(u8 socket, u8 *buf, u32 bufsize)
487 {
488     return;
489 }
490 
tls_net_get_socket(u8 socket)491 struct tls_netconn *tls_net_get_socket(u8 socket)
492 {
493     struct tls_netconn *conn = NULL;
494 
495     if (socket < 1 || socket > TLS_MAX_NETCONN_NUM) {
496         TLS_DBGPRT_ERR("skt num=%d\n", socket);
497         return NULL;
498     }
499 
500     conn = p_net_conn[socket - 1];
501     return conn;
502 }
503 
tls_socket_close(u8 socket)504 int tls_socket_close(u8 socket)
505 {
506     struct tls_net_msg *net_msg;
507     struct tls_netconn *conn;
508     err_t err;
509 
510     if (socket < 1 || socket > TLS_MAX_NETCONN_NUM) {
511         TLS_DBGPRT_ERR("skt num=%d\n", socket);
512         return -1;
513     }
514     conn = tls_net_get_socket(socket);
515     if (conn == NULL || TRUE != conn->used) {
516         TLS_DBGPRT_ERR("conn==%x,used=%d\n", (u32)conn, conn->used);
517         return -1;
518     }
519     net_msg = tls_mem_alloc(sizeof(*net_msg));
520     if (net_msg == NULL) {
521         TLS_DBGPRT_ERR("\nmem err\n");
522         return -1;
523     }
524     memset(net_msg, 0, sizeof(*net_msg));
525     net_msg->skt_no = socket;
526 
527     return err;
528 }
529 
tls_socket_udp_sendto(u16 localport,u8 * ip_addr,u16 port,void * pdata,u16 len)530 int tls_socket_udp_sendto(u16 localport, u8  *ip_addr, u16 port, void *pdata, u16 len)
531 {
532     return;
533 }
534 
tls_socket_send(u8 skt_num,void * pdata,u16 len)535 int tls_socket_send(u8 skt_num, void *pdata, u16 len)
536 {
537     return;
538 }
539 
tls_net_init()540 int tls_net_init()
541 {
542     memset(p_net_conn, 0, sizeof(struct tls_netconn *) *TLS_MAX_NETCONN_NUM);
543     return 0;
544 }
545 
546 #endif /* TLS_CONFIG_SOCKET_RAW */
547 
548