更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
sockets.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <lwip/sockets.h>
33#include <lwip/priv/tcpip_priv.h>
34#include <lwip/fixme.h>
35//https://blog.csdn.net/zhaozhiyuan111/article/details/79197692
36
37#if LWIP_ENABLE_NET_CAPABILITY
38#include "capability_type.h"
39#include "capability_api.h"
40#define BIND_SERVICE_CAP_MIN_PORT 1024
41#endif
42
43#define IOCTL_CMD_CASE_HANDLER() \
44 { \
45 err_t err; \
46 struct lwip_ioctl_apimsg msg; \
47 msg.sock = sock; \
48 msg.cmd = cmd; \
49 msg.argp = argp; \
50 \
51 err = tcpip_api_call(lwip_do_ioctl_impl, &msg.call); \
52 if (err != ENOSYS) { \
53 sock_set_errno(sock, err); \
54 done_socket(sock); \
55 return -(err != ERR_OK); \
56 } \
57 }
58
60 struct tcpip_api_call_data call;
61 struct lwip_sock *sock;
62 long cmd;
63 void *argp;
64};
65
66static err_t lwip_do_ioctl_impl(struct tcpip_api_call_data *call);
67
68static void poll_check_waiters(int s, int check_waiters);
69
70static int lwip_socket_wrap(int domain, int type, int protocol);
71int lwip_socket(int domain, int type, int protocol)
72{
73 return lwip_socket_wrap(domain, type, protocol);
74}
75
76static int lwip_setsockopt_wrap(int s, int level, int optname, const void *optval, socklen_t optlen);
77int lwip_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
78{
79 return lwip_setsockopt_wrap(s, level, optname, optval, optlen);
80}
81
82static int lwip_bind_wrap(int s, const struct sockaddr *name, socklen_t namelen);
83int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen)
84{
85 return lwip_bind_wrap(s, name, namelen);
86}
87
88static ssize_t lwip_sendto_wrap(int s, const void *dataptr, size_t size, int flags,
89 const struct sockaddr *to, socklen_t tolen);
90ssize_t lwip_sendto(int s, const void *dataptr, size_t size, int flags, const struct sockaddr *to, socklen_t tolen)
91{
92 return lwip_sendto_wrap(s, dataptr, size, flags, to, tolen);
93}
94
95#ifdef lwip_socket
96#undef lwip_socket
97#endif
98#define lwip_socket static lwip_socket2
99static int lwip_socket2(int domain, int type, int protocol);
100
101#ifdef lwip_setsockopt
102#undef lwip_setsockopt
103#endif
104#define lwip_setsockopt static lwip_setsockopt2
105static int lwip_setsockopt2(int s, int level, int optname, const void *optval, socklen_t optlen);
106
107#ifdef lwip_bind
108#undef lwip_bind
109#endif
110#define lwip_bind static lwip_bind2
111static int lwip_bind2(int s, const struct sockaddr *name, socklen_t namelen);
112
113#ifdef lwip_sendto
114#undef lwip_sendto
115#endif
116#define lwip_sendto lwip_sendto2
117ssize_t lwip_sendto2(int s, const void *dataptr, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
118
119#include "../api/sockets.c"
120
121#undef lwip_socket
122#undef lwip_setsockopt
123#undef lwip_bind
124#undef lwip_sendto
125
126static int lwip_socket_wrap(int domain, int type, int protocol)
127{
128 if (domain != AF_INET && domain != AF_INET6) {
129 set_errno(EAFNOSUPPORT);
130 return -1;
131 }
132#if LWIP_ENABLE_NET_CAPABILITY
133 if (type == SOCK_RAW && !IsCapPermit(CAP_NET_RAW)) {
134 set_errno(EPERM);
135 return -1;
136 }
137#endif
138 return lwip_socket2(domain, type, protocol);
139}
140
141static int lwip_setsockopt_wrap(int s, int level, int optname, const void *optval, socklen_t optlen)
142{
143#if LWIP_ENABLE_NET_CAPABILITY
144 if (level == SOL_SOCKET) {
145 switch (optname) {
146#if LWIP_ENABLE_CAP_NET_BROADCAST
147 case SO_BROADCAST:
148 if (!IsCapPermit(CAP_NET_BROADCAST)) {
149 set_errno(EPERM);
150 return -1;
151 }
152 break;
153#endif
154 case SO_DEBUG:
155 case SO_MARK:
156 case SO_PRIORITY:
157 case SO_RCVBUFFORCE:
158 case SO_SNDBUFFORCE:
159 if (!IsCapPermit(CAP_NET_ADMIN)) {
160 set_errno(EPERM);
161 return -1;
162 }
163 break;
164 default:
165 break;
166 }
167 }
168#endif
169 return lwip_setsockopt2(s, level, optname, optval, optlen);
170}
171
172#if LWIP_ENABLE_NET_CAPABILITY && LWIP_ENABLE_CAP_NET_BROADCAST
173static int ip_addr_isbroadcast_bysock(const ip_addr_t *ipaddr, int s)
174{
175 struct sockaddr sa;
176 socklen_t salen = sizeof(sa);
177
178 if (ipaddr == NULL) {
179 return 0;
180 }
181
182 if (lwip_getsockname(s, &sa, &salen) == -1) {
183 return 0;
184 }
185
186 ip_addr_t addr;
187 u16_t port;
188
189 SOCKADDR_TO_IPADDR_PORT(&sa, &addr, port);
190
191 struct netif *netif = NULL;
192 NETIF_FOREACH(netif) {
193 if (ip_addr_cmp(&netif->ip_addr, &addr)) {
194 return ip_addr_isbroadcast(ipaddr, netif);
195 }
196 for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
197 if (ip_addr_cmp(&netif->ip6_addr[i], &addr)) {
198 return ip_addr_isbroadcast(ipaddr, netif);
199 }
200 }
201 }
202
203 return 0;
204}
205#endif
206
207static int lwip_bind_wrap(int s, const struct sockaddr *name, socklen_t namelen)
208{
209#if LWIP_ENABLE_NET_CAPABILITY
210 if ((name->sa_family == AF_INET && namelen >= sizeof(struct sockaddr_in)) ||
211 (name->sa_family == AF_INET6 && namelen >= sizeof(struct sockaddr_in6))) {
212 ip_addr_t ipaddr;
213 u16_t port;
214
215 SOCKADDR_TO_IPADDR_PORT(name, &ipaddr, port);
216
217 if (port != 0 && port < BIND_SERVICE_CAP_MIN_PORT) {
218 LWIP_ERROR("permission deny: NET_BIND_SERVICE\n", IsCapPermit(CAP_NET_BIND_SERVICE),
219 set_errno(EPERM); return -1);
220 }
221#if LWIP_ENABLE_CAP_NET_BROADCAST
222 if (ip_addr_ismulticast(&ipaddr) || ip_addr_isbroadcast_bysock(&ipaddr, s)) {
223 LWIP_ERROR("permission deny: NET_BROADCAST\n", IsCapPermit(CAP_NET_BROADCAST),
224 set_errno(EPERM); return -1);
225 }
226#endif
227 }
228#endif
229
230 return lwip_bind2(s, name, namelen);
231}
232
233static ssize_t lwip_sendto_wrap(int s, const void *dataptr, size_t size, int flags,
234 const struct sockaddr *to, socklen_t tolen)
235{
236#if LWIP_ENABLE_NET_CAPABILITY
237 if (to &&
238 ((to->sa_family == AF_INET && tolen >= sizeof(struct sockaddr_in)) ||
239 (to->sa_family == AF_INET6 && tolen >= sizeof(struct sockaddr_in6)))) {
240 ip_addr_t ipaddr;
241 u16_t port;
242
243 SOCKADDR_TO_IPADDR_PORT(to, &ipaddr, port);
244#if LWIP_ENABLE_CAP_NET_BROADCAST
245 if (ip_addr_ismulticast(&ipaddr) || ip_addr_isbroadcast_bysock(&ipaddr, s)) {
246 LWIP_ERROR("permission deny: NET_BROADCAST\n", IsCapPermit(CAP_NET_BROADCAST),
247 set_errno(EPERM); return -1);
248 }
249#endif
250 }
251#endif
252
253 return lwip_sendto2(s, dataptr, size, flags, to, tolen);
254}
255
256#if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL
257
258struct file;
259extern void poll_wait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p);
260extern void __wake_up_interruptible_poll(wait_queue_head_t *wait, pollevent_t key);
261
262static void poll_check_waiters(int s, int check_waiters)
263{
264 unsigned long int_save, wq_empty;
265 pollevent_t mask = 0;
266 struct lwip_sock *sock;
267 SYS_ARCH_DECL_PROTECT(lev);
268
269 if (!check_waiters) {
270 return;
271 }
272
273 sock = get_socket(s);
274 if (!sock) {
275 return;
276 }
277
278 SYS_ARCH_PROTECT(lev);
279
280 mask |= (sock->rcvevent > 0) ? (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND) : 0;
281 mask |= (sock->sendevent != 0) ? (POLLOUT | POLLWRNORM | POLLWRBAND) : 0;
282 mask |= (sock->errevent != 0) ? (POLLERR) : 0;
283
284 SYS_ARCH_UNPROTECT(lev);
285
286 spin_lock_irqsave(&sock->wq.lock, int_save);
287 wq_empty = LOS_ListEmpty(&(sock->wq.poll_queue));
288 spin_unlock_irqrestore(&sock->wq.lock, int_save);
289
290 if (mask && !wq_empty) {
291 __wake_up_interruptible_poll(&sock->wq, mask);
292 }
293
294 done_socket(sock);
295}
296
297int socks_poll(int s, poll_table *wait)
298{
299 int ret;
300 pollevent_t mask = 0;
301 struct lwip_sock *sock;
302 SYS_ARCH_DECL_PROTECT(lev);
303
304 LWIP_ERROR("sock_poll: invalid poll_table", (wait != NULL), return -EINVAL;);
305
306 sock = get_socket(s);
307 if (!sock) {
308 LWIP_DEBUGF(SOCKETS_DEBUG, ("sock_poll: Invalid socket"));
309 set_errno(EBADF);
310 return -EBADF; /* compatible with file poll */
311 }
312
313 SYS_ARCH_PROTECT(lev);
314
315 mask |= (sock->rcvevent > 0 || sock->lastdata.pbuf) ? (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND) : 0;
316 mask |= (sock->sendevent != 0) ? (POLLOUT | POLLWRNORM | POLLWRBAND) : 0;
317 mask |= (sock->errevent != 0) ? (POLLERR) : 0;
318
319 SYS_ARCH_UNPROTECT(lev);
320
321 ret = wait->key & mask;
322 if (!ret) {
323 poll_wait(NULL, &sock->wq, wait);
324 }
325
326 done_socket(sock);
327 return ret;
328}
329
330#endif /* LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL */
331
332#if !LWIP_COMPAT_SOCKETS
333
334#define API_ALIAS(old, new) \
335 extern __typeof(old) new __attribute__((__weak__, __alias__(#old)))
336
337API_ALIAS(lwip_ioctl, ioctlsocket);
338
339#endif /* !LWIP_COMPAT_SOCKETS */
340
341#if LWIP_ENABLE_LOS_SHELL_CMD
342/* get numbers of unused sockets */
344{
345 int unused = 0;
346 SYS_ARCH_DECL_PROTECT(lev);
347
348 SYS_ARCH_PROTECT(lev);
349
350 for (int i = 0; i < NUM_SOCKETS; i++) {
351 if (sockets[i].conn == NULL) {
352#if LWIP_NETCONN_FULLDUPLEX
353 if (sockets[i].fd_used) {
354 continue;
355 }
356#endif
357 unused++;
358 }
359 }
360
361 SYS_ARCH_UNPROTECT(lev);
362
363 return unused;
364}
365#endif
366
367/**
368 * socket ioctl
369 */
370
371// Options for lwip ioctl
372#define LWIP_IOCTL_ROUTE 1
373#define LWIP_IOCTL_IF 1
374#define LWIP_NETIF_ETHTOOL 0
375#define LWIP_IOCTL_IPV6DPCTD 0
376#undef LWIP_IPV6_DUP_DETECT_ATTEMPTS
377#define LWIP_IPV6_DUP_DETECT_ATTEMPTS 0
378
379#ifndef SIOCSIPV6DAD
380#define SIOCSIPV6DAD _IOW('z', 0, unsigned long) /* set DAD enable/disable on netif */
381#endif
382
383#ifndef SIOCGIPV6DAD
384#define SIOCGIPV6DAD _IOR('z', 1, unsigned long) /* get DAD status on netif */
385#endif
386
387#ifndef SIOCSIPV6DPCTD
388#define SIOCSIPV6DPCTD _IOW('z', 2, unsigned long)
389#endif
390
391#ifndef SIOCGIPV6DPCTD
392#define SIOCGIPV6DPCTD _IOR('z', 3, unsigned long)
393#endif
394
395#ifndef SIOCETHTOOL
396#define SIOCETHTOOL 0x8946
397#endif
398
399#if LWIP_NETIF_PROMISC
400#define NETIF_FLAG_PROMISC 0x80U
401#endif /* LWIP_NETIF_PROMISC */
402
403
404#if LWIP_IOCTL_ROUTE
405
406#include <net/route.h>
407
408static u8_t lwip_ioctl_internal_SIOCADDRT(struct rtentry *rmten)
409{
410 struct netif *netif = NULL;
411 ip_addr_t rtgw_addr;
412 u16_t rtgw_port;
413
414#if LWIP_ENABLE_NET_CAPABILITY
415 if (!IsCapPermit(CAP_NET_ADMIN)) {
416 return EPERM;
417 }
418#endif
419
420 SOCKADDR_TO_IPADDR_PORT(&rmten->rt_gateway, &rtgw_addr, rtgw_port);
421
422 if (!IP_IS_V4_VAL(rtgw_addr)) {
423 return EINVAL;
424 }
425
426 /* check if multicast/0/loopback */
427 if (ip_addr_ismulticast(&rtgw_addr) || ip_addr_isany(&rtgw_addr) ||
428 ip_addr_isloopback(&rtgw_addr)) {
429 return EINVAL;
430 }
431
432 /* check if reachable */
433 for (netif = netif_list; netif != NULL; netif = netif->next) {
434 if (ip_addr_netcmp(&rtgw_addr, &netif->ip_addr, ip_2_ip4(&netif->netmask))) {
435 break;
436 }
437 }
438
439 if (netif == NULL) {
440 return EHOSTUNREACH;
441 }
442
443 /* check if broadcast */
444 if (ip_addr_isbroadcast(&rtgw_addr, netif) != 0) {
445 return EINVAL;
446 }
447
448 /* Check flags */
449 if ((rmten->rt_flags & RTF_GATEWAY) == 0) {
450 return EINVAL;
451 }
452
453 /* Add validation */
454 if ((netif_default != NULL) && (netif_default != netif)) {
455 ip_addr_set_zero(&netif_default->gw);
456 (void)netif_set_default(netif);
457 }
458 netif_set_gw(netif, ip_2_ip4(&rtgw_addr));
459
460 return 0;
461}
462
463#endif
464
465#if LWIP_IOCTL_IF
466
467static u8_t lwip_ioctl_internal_SIOCGIFCONF(struct ifreq *ifr)
468{
469 struct ifconf *ifc = NULL;
470 struct netif *netif = NULL;
471 struct ifreq ifreq;
472 struct sockaddr_in *sock_in = NULL;
473 int pos;
474 int len;
475 int ret;
476
477 /* Format the caller's buffer. */
478 ifc = (struct ifconf *)ifr;
479 len = ifc->ifc_len;
480
481 /* Loop over the interfaces, and write an info block for each. */
482 pos = 0;
483 for (netif = netif_list; netif != NULL; netif = netif->next) {
484 if (ifc->ifc_buf == NULL) {
485 pos = (pos + (int)sizeof(struct ifreq));
486 continue;
487 }
488
489 if (len < (int)sizeof(ifreq)) {
490 break;
491 }
492 (void)memset_s(&ifreq, sizeof(struct ifreq), 0, sizeof(struct ifreq));
493 if (netif->link_layer_type == LOOPBACK_IF) {
494 ret = snprintf_s(ifreq.ifr_name, IFNAMSIZ, (IFNAMSIZ - 1), "%.2s", netif->name);
495 if ((ret <= 0) || (ret >= IFNAMSIZ)) {
496 LWIP_DEBUGF(NETIF_DEBUG, ("lwip_ioctl: snprintf_s ifr_name failed."));
497 return ENOBUFS;
498 }
499 } else {
500 ret = snprintf_s(ifreq.ifr_name, IFNAMSIZ, (IFNAMSIZ - 1), "%s", netif_get_name(netif));
501 if ((ret <= 0) || (ret >= IFNAMSIZ)) {
502 LWIP_DEBUGF(NETIF_DEBUG, ("lwip_ioctl: snprintf_s ifr_name failed."));
503 return ENOBUFS;
504 }
505 }
506
507 sock_in = (struct sockaddr_in *)&ifreq.ifr_addr;
508 sock_in->sin_family = AF_INET;
509 sock_in->sin_addr.s_addr = ip_2_ip4(&netif->ip_addr)->addr;
510 if (memcpy_s(ifc->ifc_buf + pos, sizeof(struct ifreq), &ifreq, sizeof(struct ifreq)) != EOK) {
511 return ENOBUFS;
512 }
513 pos = pos + (int)sizeof(struct ifreq);
514 len = len - (int)sizeof(struct ifreq);
515 }
516
517 ifc->ifc_len = pos;
518
519 return 0;
520}
521
522static u8_t lwip_ioctl_internal_SIOCGIFADDR(struct ifreq *ifr)
523{
524 struct netif *netif = NULL;
525 struct sockaddr_in *sock_in = NULL;
526
527 /* get netif ipaddr */
528 netif = netif_find(ifr->ifr_name);
529 if (netif == NULL) {
530 return ENODEV;
531 } else {
532 sock_in = (struct sockaddr_in *)&ifr->ifr_addr;
533 sock_in->sin_family = AF_INET;
534 sock_in->sin_addr.s_addr = ip_2_ip4(&netif->ip_addr)->addr;
535 return 0;
536 }
537}
538
539#ifndef LWIP_IPV6_PREFIX_LEN
540#define LWIP_IPV6_PREFIX_LEN 64
541#endif
542
543#ifndef LWIP_NETIF_IFINDEX_MAX_EX
544#define LWIP_NETIF_IFINDEX_MAX_EX 255
545#endif
546
547#include "lwip/dhcp.h"
548#include "lwip/dhcp6.h"
549#include "lwip/prot/dhcp.h"
550#include "lwip/prot/dhcp6.h"
551
552static u8_t lwip_ioctl_internal_SIOCSIFADDR_6(struct ifreq *ifr)
553{
554 (void)ifr;
555 return ENOSYS;
556}
557
558static u8_t lwip_ioctl_internal_SIOCSIFADDR(struct ifreq *ifr)
559{
560 struct netif *netif = NULL;
561
562 struct netif *loc_netif = NULL;
563 ip_addr_t taget_addr;
564 u16_t taget_port;
565 SOCKADDR_TO_IPADDR_PORT(&ifr->ifr_addr, &taget_addr, taget_port);
566
567#if LWIP_ENABLE_NET_CAPABILITY
568 if (!IsCapPermit(CAP_NET_ADMIN)) {
569 return EPERM;
570 }
571#endif
572
573 /* set netif ipaddr */
574 netif = netif_find(ifr->ifr_name);
575 if (netif == NULL) {
576 return ENODEV;
577 }
578#if LWIP_HAVE_LOOPIF
579 else if (netif->link_layer_type == LOOPBACK_IF) {
580 return EPERM;
581 }
582#endif
583 else {
584 /* check the address is not multicast/broadcast/0/loopback */
585 if (!IP_IS_V4(&taget_addr) || ip_addr_ismulticast(&taget_addr) ||
586 ip_addr_isbroadcast(&taget_addr, netif) ||
587 ip_addr_isany(&taget_addr) ||
588 ip_addr_isloopback(&taget_addr)) {
589 return EINVAL;
590 }
591
592 /* reset gateway if new and previous ipaddr not in same net */
593 if (ip_addr_netcmp(&taget_addr, &netif->ip_addr, ip_2_ip4(&netif->netmask)) == 0) {
594 ip_addr_set_zero(&netif->gw);
595 if (netif == netif_default) {
596 (void)netif_set_default(NULL);
597 }
598 }
599
600 /* lwip disallow two netif sit in same net at the same time */
601 loc_netif = netif_list;
602 while (loc_netif != NULL) {
603 if (loc_netif == netif) {
604 loc_netif = loc_netif->next;
605 continue;
606 }
607 if (ip_addr_cmp(&netif->netmask, &loc_netif->netmask) &&
608 ip_addr_netcmp(&loc_netif->ip_addr, &taget_addr,
609 ip_2_ip4(&netif->netmask))) {
610 return EINVAL;
611 }
612 loc_netif = loc_netif->next;
613 }
614
615#if LWIP_DHCP
616 if ((netif_dhcp_data(netif) != NULL) &&
617 (netif_dhcp_data(netif)->state != DHCP_STATE_OFF)) {
618 (void)netif_dhcp_off(netif);
619 }
620#endif
621
622#if LWIP_ARP
623 /* clear ARP cache when IP address changed */
624 if ((netif->flags & NETIF_FLAG_ETHARP) != 0) {
625 etharp_cleanup_netif(netif);
626 }
627#endif /* LWIP_ARP */
628
629 netif_set_ipaddr(netif, ip_2_ip4(&taget_addr));
630
631 return 0;
632 }
633}
634
635static u8_t lwip_ioctl_internal_SIOCDIFADDR_6(struct ifreq *ifr)
636{
637 (void)ifr;
638 return ENOSYS;
639}
640
641static u8_t lwip_ioctl_internal_SIOCDIFADDR(struct ifreq *ifr)
642{
643 struct netif *netif = NULL;
644
645 ip_addr_t target_addr;
646 u16_t target_port;
647
648 SOCKADDR_TO_IPADDR_PORT(&ifr->ifr_addr, &target_addr, target_port);
649
650#if LWIP_ENABLE_NET_CAPABILITY
651 if (!IsCapPermit(CAP_NET_ADMIN)) {
652 return EPERM;
653 }
654#endif
655
656 /* set netif ipaddr */
657 netif = netif_find(ifr->ifr_name);
658 if (netif == NULL) {
659 return ENODEV;
660 }
661#if LWIP_HAVE_LOOPIF
662 else if (netif->link_layer_type == LOOPBACK_IF) {
663 return EPERM;
664 }
665#endif
666
667 /* check the address is not loopback */
668 if (!IP_IS_V4(&target_addr) || ip_addr_isloopback(&target_addr)) {
669 return EINVAL;
670 }
671
672#if LWIP_DHCP
673 if ((netif_dhcp_data(netif) != NULL) &&
674 (netif_dhcp_data(netif)->state != DHCP_STATE_OFF)) {
675 (void)netif_dhcp_off(netif);
676 }
677#endif
678
679 ip_addr_set_zero(&netif->gw);
680 ip_addr_set_zero(&netif->ip_addr);
681 ip_addr_set_zero(&netif->netmask);
682 if (netif == netif_default) {
683 (void)netif_set_default(NULL);
684 }
685
686#if LWIP_IPV4 && LWIP_ARP
687 if ((netif->flags & NETIF_FLAG_ETHARP) != 0) {
688 etharp_cleanup_netif(netif);
689 }
690#endif /* LWIP_IPV4 && LWIP_ARP */
691
692 return ERR_OK;
693}
694
695static u8_t lwip_ioctl_internal_SIOCGIFNETMASK(struct ifreq *ifr)
696{
697 struct netif *netif = NULL;
698 struct sockaddr_in *sock_in = NULL;
699
700 /* get netif netmask */
701 netif = netif_find(ifr->ifr_name);
702 if (netif == NULL) {
703 return ENODEV;
704 } else {
705 sock_in = (struct sockaddr_in *)&ifr->ifr_netmask;
706 sock_in->sin_family = AF_INET;
707 sock_in->sin_addr.s_addr = ip_2_ip4(&netif->netmask)->addr;
708 return 0;
709 }
710}
711
712static u8_t lwip_ioctl_internal_SIOCSIFNETMASK(struct ifreq *ifr)
713{
714 struct netif *netif = NULL;
715
716 struct netif *loc_netif = NULL;
717 ip_addr_t taget_addr;
718 u16_t taget_port;
719 SOCKADDR_TO_IPADDR_PORT(&ifr->ifr_addr, &taget_addr, taget_port);
720
721#if LWIP_ENABLE_NET_CAPABILITY
722 if (!IsCapPermit(CAP_NET_ADMIN)) {
723 return EPERM;
724 }
725#endif
726
727 if (!IP_IS_V4(&taget_addr)) {
728 return EINVAL;
729 }
730
731 /* set netif netmask */
732 netif = netif_find(ifr->ifr_name);
733 if (netif == NULL) {
734 return ENODEV;
735 }
736#if LWIP_HAVE_LOOPIF
737 else if (netif->link_layer_type == LOOPBACK_IF) {
738 return EPERM;
739 }
740#endif
741 else {
742 if (ip_addr_cmp(&netif->netmask, &taget_addr)) {
743 return 0;
744 }
745 /* check data valid */
746 if (ip_addr_netmask_valid(ip_2_ip4(&taget_addr)) != 0) {
747 return EINVAL;
748 }
749
750 /* lwip disallow two netif sit in same net at the same time */
751 loc_netif = netif_list;
752 while (loc_netif != NULL) {
753 if (loc_netif == netif) {
754 loc_netif = loc_netif->next;
755 continue;
756 }
757 if (ip_addr_cmp(&loc_netif->netmask, &taget_addr) &&
758 ip_addr_netcmp(&loc_netif->ip_addr,
759 &netif->ip_addr, ip_2_ip4(&loc_netif->netmask))) {
760 return EINVAL;
761 }
762 loc_netif = loc_netif->next;
763 }
764
765#if LWIP_DHCP
766 if ((netif_dhcp_data(netif) != NULL) &&
767 (netif_dhcp_data(netif)->state != DHCP_STATE_OFF)) {
768 (void)netif_dhcp_off(netif);
769 }
770#endif
771
772 netif_set_netmask(netif, ip_2_ip4(&taget_addr));
773
774 /* check if gateway still reachable */
775 if (!ip_addr_netcmp(&netif->gw, &netif->ip_addr, ip_2_ip4(&taget_addr))) {
776 ip_addr_set_zero(&(netif->gw));
777 if (netif == netif_default) {
778 (void)netif_set_default(NULL);
779 }
780 }
781 return 0;
782 }
783}
784
785static u8_t lwip_ioctl_internal_SIOCSIFHWADDR(struct ifreq *ifr)
786{
787 struct netif *netif = NULL;
788 err_t ret;
789
790#if LWIP_ENABLE_NET_CAPABILITY
791 if (!IsCapPermit(CAP_NET_ADMIN)) {
792 return EPERM;
793 }
794#endif
795
796 /* set netif hw addr */
797 netif = netif_find(ifr->ifr_name);
798 if (netif == NULL) {
799 return ENODEV;
800 }
801#if LWIP_HAVE_LOOPIF
802 else if (netif->link_layer_type == LOOPBACK_IF) {
803 return EPERM;
804 }
805#endif
806 else {
807
808 /* bring netif down to clear all Neighbor Cache Entry */
809 (void)netif_set_down(netif);
810
811 ret = netif_set_hwaddr(netif, (const unsigned char *)ifr->ifr_hwaddr.sa_data, netif->hwaddr_len);
812
813 if (ret != ERR_OK) {
814 (void)netif_set_up(netif);
815 return err_to_errno(ret);
816 }
817
818 /*
819 * bring netif up to try to send GARP/IGMP/NA/MLD/RS. GARP and NA would
820 * make the neighboring nodes update their Neighbor Cache immediately.
821 */
822 (void)netif_set_up(netif);
823 return 0;
824 }
825}
826
827static u8_t lwip_ioctl_internal_SIOCGIFHWADDR(struct ifreq *ifr)
828{
829 struct netif *netif = NULL;
830
831 /* get netif hw addr */
832 netif = netif_find(ifr->ifr_name);
833 if (netif == NULL) {
834 return ENODEV;
835 }
836#if LWIP_HAVE_LOOPIF
837 else if (netif->link_layer_type == LOOPBACK_IF) {
838 return EPERM;
839 }
840#endif /* LWIP_HAVE_LOOPIF */
841 else {
842 if (memcpy_s((void *)ifr->ifr_hwaddr.sa_data, sizeof(ifr->ifr_hwaddr.sa_data),
843 (void *)netif->hwaddr, netif->hwaddr_len) != EOK) {
844 return EINVAL;
845 }
846 return 0;
847 }
848}
849
850static u8_t lwip_ioctl_internal_SIOCSIFFLAGS(struct ifreq *ifr)
851{
852 struct netif *netif = NULL;
853
854#if LWIP_ENABLE_NET_CAPABILITY
855 if (!IsCapPermit(CAP_NET_ADMIN)) {
856 return EPERM;
857 }
858#endif
859
860 /* set netif hw addr */
861 netif = netif_find(ifr->ifr_name);
862 if (netif == NULL) {
863 return ENODEV;
864 }
865#if LWIP_HAVE_LOOPIF
866 else if (netif->link_layer_type == LOOPBACK_IF) {
867 return EPERM;
868 }
869#endif /* LWIP_HAVE_LOOPIF */
870 else {
871 if (((unsigned short)ifr->ifr_flags & IFF_UP) && !(netif->flags & NETIF_FLAG_UP)) {
872 (void)netif_set_up(netif);
873 } else if (!((unsigned short)ifr->ifr_flags & IFF_UP) && (netif->flags & NETIF_FLAG_UP)) {
874 (void)netif_set_down(netif);
875 }
876 if (((unsigned short)ifr->ifr_flags & IFF_RUNNING) && !(netif->flags & NETIF_FLAG_LINK_UP)) {
877 (void)netif_set_link_up(netif);
878 } else if (!((unsigned short)ifr->ifr_flags & IFF_RUNNING) && (netif->flags & NETIF_FLAG_LINK_UP)) {
879 (void)netif_set_link_down(netif);
880 }
881
882 if ((unsigned short)ifr->ifr_flags & IFF_BROADCAST) {
883 netif->flags |= NETIF_FLAG_BROADCAST;
884 } else {
885 netif->flags = netif->flags & (~NETIF_FLAG_BROADCAST);
886 }
887 if ((unsigned short)ifr->ifr_flags & IFF_NOARP) {
888 netif->flags = (netif->flags & (~NETIF_FLAG_ETHARP));
889 } else {
890 netif->flags |= NETIF_FLAG_ETHARP;
891 }
892
893 if ((unsigned short)ifr->ifr_flags & IFF_MULTICAST) {
894#if LWIP_IGMP
895 netif->flags |= NETIF_FLAG_IGMP;
896#endif /* LWIP_IGMP */
897#if LWIP_IPV6 && LWIP_IPV6_MLD
898 netif->flags |= NETIF_FLAG_MLD6;
899#endif /* LWIP_IPV6_MLD */
900 } else {
901#if LWIP_IGMP
902 netif->flags = (netif->flags & ~NETIF_FLAG_IGMP);
903#endif /* LWIP_IGMP */
904#if LWIP_IPV6 && LWIP_IPV6_MLD
905 netif->flags = (netif->flags & ~NETIF_FLAG_MLD6);
906#endif /* LWIP_IPV6_MLD */
907 }
908
909#if LWIP_DHCP
910 if ((unsigned short)ifr->ifr_flags & IFF_DYNAMIC) {
911 (void)dhcp_start(netif);
912 } else {
913 dhcp_stop(netif);
914#if !LWIP_DHCP_SUBSTITUTE
915 dhcp_cleanup(netif);
916#endif
917 }
918#endif
919
920#if LWIP_NETIF_PROMISC
921 if (((unsigned short)ifr->ifr_flags & IFF_PROMISC)) {
922 netif->flags |= NETIF_FLAG_PROMISC;
923 } else {
924 netif->flags &= ~NETIF_FLAG_PROMISC;
925 }
926 if (netif->drv_config) {
927 netif->drv_config(netif, IFF_PROMISC, !!((unsigned short)ifr->ifr_flags & IFF_PROMISC));
928 }
929#endif /* LWIP_NETIF_PROMISC */
930 return 0;
931 }
932}
933
934static u8_t lwip_ioctl_internal_SIOCGIFFLAGS(struct ifreq *ifr)
935{
936 struct netif *netif = NULL;
937
938 /* set netif hw addr */
939 netif = netif_find(ifr->ifr_name);
940 if (netif == NULL) {
941 return ENODEV;
942 } else {
943 if (netif->flags & NETIF_FLAG_UP) {
944 ifr->ifr_flags = ((unsigned short)(ifr->ifr_flags)) | IFF_UP;
945 } else {
946 ifr->ifr_flags = ((unsigned short)(ifr->ifr_flags)) & ~IFF_UP;
947 }
948 if (netif->flags & NETIF_FLAG_LINK_UP) {
949 ifr->ifr_flags = ((unsigned short)(ifr->ifr_flags)) | IFF_RUNNING;
950 } else {
951 ifr->ifr_flags = ((unsigned short)(ifr->ifr_flags)) & ~IFF_RUNNING;
952 }
953 if (netif->flags & NETIF_FLAG_BROADCAST) {
954 ifr->ifr_flags = ((unsigned short)(ifr->ifr_flags)) | IFF_BROADCAST;
955 } else {
956 ifr->ifr_flags = ((unsigned short)(ifr->ifr_flags)) & ~IFF_BROADCAST;
957 }
958 if (netif->flags & NETIF_FLAG_ETHARP) {
959 ifr->ifr_flags = ((unsigned short)(ifr->ifr_flags)) & ~IFF_NOARP;
960 } else {
961 ifr->ifr_flags = ((unsigned short)(ifr->ifr_flags)) | IFF_NOARP;
962 }
963
964#if LWIP_IGMP || LWIP_IPV6_MLD
965 if (
966#if LWIP_IGMP
967 (netif->flags & NETIF_FLAG_IGMP)
968#endif /* LWIP_IGMP */
969#if LWIP_IGMP && LWIP_IPV6_MLD
970 ||
971#endif /* LWIP_IGMP && LWIP_IPV6_MLD */
972#if LWIP_IPV6_MLD
973 (netif->flags & NETIF_FLAG_MLD6)
974#endif /* LWIP_IPV6_MLD */
975 ) {
976 ifr->ifr_flags = (short)((unsigned short)ifr->ifr_flags | IFF_MULTICAST);
977 } else {
978 ifr->ifr_flags = (short)((unsigned short)ifr->ifr_flags & (~IFF_MULTICAST));
979 }
980#endif /* LWIP_IGMP || LWIP_IPV6_MLD */
981
982#if LWIP_DHCP
983 //if ((netif->flags & NETIF_FLAG_DHCP) != 0) {
984 if (dhcp_supplied_address(netif)) {
985 ifr->ifr_flags = (short)((unsigned short)ifr->ifr_flags | IFF_DYNAMIC);
986 } else {
987 ifr->ifr_flags = (short)((unsigned short)ifr->ifr_flags & (~IFF_DYNAMIC));
988 }
989#endif
990
991#if LWIP_HAVE_LOOPIF
992 if (netif->link_layer_type == LOOPBACK_IF) {
993 ifr->ifr_flags = ((unsigned short)(ifr->ifr_flags)) | IFF_LOOPBACK;
994 }
995#endif
996
997#if LWIP_NETIF_PROMISC
998 if (netif->flags & NETIF_FLAG_PROMISC) {
999 ifr->ifr_flags = ((unsigned short)(ifr->ifr_flags)) | IFF_PROMISC;
1000 } else {
1001 ifr->ifr_flags = ((unsigned short)(ifr->ifr_flags)) & ~IFF_PROMISC;
1002 }
1003#endif /* LWIP_NETIF_PROMISC */
1004
1005 return 0;
1006 }
1007}
1008
1009static u8_t lwip_ioctl_internal_SIOCGIFNAME(struct ifreq *ifr)
1010{
1011 struct netif *netif = NULL;
1012 int ret;
1013
1014 for (netif = netif_list; netif != NULL; netif = netif->next) {
1015 if (ifr->ifr_ifindex == netif_get_index(netif)) {
1016 break;
1017 }
1018 }
1019
1020 if (netif == NULL) {
1021 return ENODEV;
1022 } else {
1023 if (netif->link_layer_type == LOOPBACK_IF) {
1024 ret = snprintf_s(ifr->ifr_name, IFNAMSIZ, (IFNAMSIZ - 1), "%.2s", netif->name);
1025 if ((ret <= 0) || (ret >= IFNAMSIZ)) {
1026 return ENOBUFS;
1027 }
1028 } else {
1029 ret = snprintf_s(ifr->ifr_name, IFNAMSIZ, (IFNAMSIZ - 1), "%s", netif_get_name(netif));
1030 if ((ret <= 0) || (ret >= IFNAMSIZ)) {
1031 return ENOBUFS;
1032 }
1033 }
1034 return 0;
1035 }
1036}
1037
1038static bool lwip_validate_ifname(const char *name, u8_t *let_pos)
1039{
1040 unsigned short num_pos = 0;
1041 unsigned short letter_pos = 0;
1042 unsigned short pos = 0;
1043 bool have_num = 0;
1044
1045 /* if the first position of variable name is not letter, such as '6eth2' */
1046 if (((*name >= 'a') && (*name <= 'z')) || ((*name >= 'A') && (*name <= 'Z'))) {
1047 return 0;
1048 }
1049
1050 /* check if the position of letter is bigger than the the position of digital */
1051 while (*name != '\0') {
1052 if ((*name >= '0') && (*name <= '9')) {
1053 num_pos = pos;
1054 have_num = 1;
1055 } else if (((*name >= 'a') && (*name <= 'z')) || ((*name >= 'A') && (*name <= 'Z'))) {
1056 letter_pos = pos;
1057 if (have_num != 0) {
1058 return 0;
1059 }
1060 } else {
1061 return 0;
1062 }
1063 pos++;
1064 name++;
1065 }
1066
1067 /* for the speacil case as all position of variable name is letter, such as 'ethabc' */
1068 if (num_pos == 0) {
1069 return 0;
1070 }
1071
1072 /* cheak if the digital in the variable name is bigger than 255, such as 'eth266' */
1073 if (atoi(name - (pos - letter_pos - 1)) > 255) {
1074 return 0;
1075 }
1076
1077 *let_pos = (u8_t)letter_pos;
1078
1079 return 1;
1080}
1081
1082static u8_t lwip_ioctl_internal_SIOCSIFNAME(struct ifreq *ifr)
1083{
1084 struct netif *netif = NULL;
1085 u8_t letter_pos = 0;
1086
1087#if LWIP_ENABLE_NET_CAPABILITY
1088 if (!IsCapPermit(CAP_NET_ADMIN)) {
1089 return EPERM;
1090 }
1091#endif
1092
1093 netif = netif_find(ifr->ifr_name);
1094
1095 if (netif == NULL) {
1096 return ENODEV;
1097 } else if (netif->link_layer_type == LOOPBACK_IF) {
1098 return EPERM;
1099 } else if ((netif->flags & IFF_UP) != 0) {
1100 return EBUSY;
1101 } else {
1102 if (strncmp(ifr->ifr_name, ifr->ifr_newname, IFNAMSIZ) == 0) {
1103 /* not change */
1104 return 0;
1105 }
1106
1107 ifr->ifr_newname[IFNAMSIZ - 1] = '\0';
1108 if ((lwip_validate_ifname(ifr->ifr_newname, &letter_pos) == 0) || (strlen(ifr->ifr_newname) > (IFNAMSIZ - 1))) {
1109 return EINVAL;
1110 }
1111
1112 if (strncpy_s(netif->full_name, sizeof(netif->full_name), ifr->ifr_newname, strlen(ifr->ifr_newname)) != EOK) {
1113 return EINVAL;
1114 }
1115 }
1116
1117 return 0;
1118}
1119
1120static u8_t lwip_ioctl_internal_SIOCGIFINDEX(struct ifreq *ifr)
1121{
1122 struct netif *netif = NULL;
1123
1124 netif = netif_find(ifr->ifr_name);
1125 if (netif == NULL) {
1126 return ENODEV;
1127 } else {
1128 ifr->ifr_ifindex = netif_get_index(netif);
1129 return 0;
1130 }
1131}
1132
1133static u8_t lwip_ioctl_internal_SIOCSIFMTU(struct ifreq *ifr)
1134{
1135 struct netif *netif = NULL;
1136
1137#if LWIP_ENABLE_NET_CAPABILITY
1138 if (!IsCapPermit(CAP_NET_ADMIN)) {
1139 return EPERM;
1140 }
1141#endif
1142
1143 /* set netif hw addr */
1144 netif = netif_find(ifr->ifr_name);
1145 if (netif == NULL) {
1146 return ENODEV;
1147 }
1148#if LWIP_HAVE_LOOPIF
1149 /* the mtu of loopif is not used. */
1150 else if (netif->link_layer_type == LOOPBACK_IF) {
1151 return EPERM;
1152 }
1153#endif
1154 else {
1155 if (ERR_OK != netif_set_mtu(netif, (u16_t)ifr->ifr_mtu)) {
1156 return EINVAL;
1157 }
1158
1159 return 0;
1160 }
1161}
1162
1163static u8_t lwip_ioctl_internal_SIOCGIFMTU(struct ifreq *ifr)
1164{
1165 struct netif *netif = NULL;
1166
1167 /* get netif hw addr */
1168 netif = netif_find(ifr->ifr_name);
1169
1170 if (netif == NULL) {
1171 return ENODEV;
1172 } else {
1173 ifr->ifr_mtu = netif->mtu;
1174 return 0;
1175 }
1176}
1177
1178static u8_t lwip_ioctl_internal_SIOCGIFBRDADDR(struct ifreq *ifr)
1179{
1180 struct netif *netif = NULL;
1181 struct sockaddr_in *sock_in = NULL;
1182
1183 /* get netif subnet broadcast addr */
1184 netif = netif_find(ifr->ifr_name);
1185 if (netif == NULL) {
1186 return ENODEV;
1187 }
1188 if (ip4_addr_isany_val(*(ip_2_ip4(&netif->netmask)))) {
1189 return ENXIO;
1190 }
1191 sock_in = (struct sockaddr_in *)&ifr->ifr_addr;
1192 sock_in->sin_family = AF_INET;
1193 sock_in->sin_addr.s_addr = (ip_2_ip4(&((netif)->ip_addr))->addr | ~(ip_2_ip4(&netif->netmask)->addr));
1194 return 0;
1195}
1196
1197#endif /* LWIP_IOCTL_IF */
1198
1199#if LWIP_NETIF_ETHTOOL
1200
1201static s32_t lwip_ioctl_internal_SIOCETHTOOL(struct ifreq *ifr)
1202{
1203 struct netif *netif;
1204
1205#if LWIP_ENABLE_NET_CAPABILITY
1206 if (!IsCapPermit(CAP_NET_ADMIN)) {
1207 return EPERM;
1208 }
1209#endif
1210
1211 netif = netif_find(ifr->ifr_name);
1212 if (netif == NULL) {
1213 return ENODEV;
1214 } else {
1215 return dev_ethtool(netif, ifr);
1216 }
1217}
1218
1219#endif
1220
1221#if LWIP_IPV6
1222#if LWIP_IPV6_DUP_DETECT_ATTEMPTS
1223
1224static u8_t lwip_ioctl_internal_SIOCSIPV6DAD(struct ifreq *ifr)
1225{
1226#if LWIP_ENABLE_NET_CAPABILITY
1227 if (!IsCapPermit(CAP_NET_ADMIN)) {
1228 return EPERM;
1229 }
1230#endif
1231
1232 struct netif *tmpnetif = netif_find(ifr->ifr_name);
1233 if (tmpnetif == NULL) {
1234 LWIP_DEBUGF(SOCKETS_DEBUG, ("Interface not found.\n"));
1235 return ENODEV;
1236 }
1237
1238 if ((ifr->ifr_ifru.ifru_ivalue != 0) && (ifr->ifr_ifru.ifru_ivalue != 1)) {
1239 LWIP_DEBUGF(SOCKETS_DEBUG, ("Invalid ioctl argument(ifru_ivalue 0/1).\n"));
1240 return EBADRQC;
1241 }
1242
1243 if (ifr->ifr_ifru.ifru_ivalue == 1) {
1244 tmpnetif->ipv6_flags = (tmpnetif->ipv6_flags | LWIP_IPV6_ND6_FLAG_DAD);
1245
1246 LWIP_DEBUGF(SOCKETS_DEBUG, ("DAD turned on through ioctl for %s iface index %u \n",
1247 tmpnetif->name, tmpnetif->num));
1248 } else {
1249 tmpnetif->ipv6_flags = (tmpnetif->ipv6_flags & ((~LWIP_IPV6_ND6_FLAG_DAD) & 0xffU));
1250
1251 LWIP_DEBUGF(SOCKETS_DEBUG, ("DAD turned off through ioctl for %s iface index %u \n",
1252 tmpnetif->name, tmpnetif->num));
1253 }
1254 return 0;
1255}
1256
1257static u8_t lwip_ioctl_internal_SIOCGIPV6DAD(struct ifreq *ifr)
1258{
1259 struct netif *tmpnetif = netif_find(ifr->ifr_name);
1260 if (tmpnetif == NULL) {
1261 LWIP_DEBUGF(SOCKETS_DEBUG, ("Interface not found.\n"));
1262 return ENODEV;
1263 }
1264 ifr->ifr_ifru.ifru_ivalue = (tmpnetif->ipv6_flags & LWIP_IPV6_ND6_FLAG_DAD) ? 1 : 0;
1265 return 0;
1266}
1267
1268#endif
1269
1270#if LWIP_IOCTL_IPV6DPCTD
1271
1272static u8_t lwip_ioctl_internal_SIOCSIPV6DPCTD(struct ifreq *ifr)
1273{
1274#if LWIP_ENABLE_NET_CAPABILITY
1275 if (!IsCapPermit(CAP_NET_ADMIN)) {
1276 return EPERM;
1277 }
1278#endif
1279
1280 struct netif *tmpnetif = netif_find(ifr->ifr_name);
1281 if (tmpnetif == NULL) {
1282 LWIP_DEBUGF(SOCKETS_DEBUG, ("Interface not found.\n"));
1283 return ENODEV;
1284 }
1285 if ((ifr->ifr_ifru.ifru_ivalue != 0) && (ifr->ifr_ifru.ifru_ivalue != 1)) {
1286 LWIP_DEBUGF(SOCKETS_DEBUG, ("Invalid ioctl argument(ifru_ivalue 0/1).\n"));
1287 return EBADRQC;
1288 }
1289 if (ifr->ifr_ifru.ifru_ivalue == 1) {
1290 tmpnetif->ipv6_flags = (tmpnetif->ipv6_flags | LWIP_IPV6_ND6_FLAG_DEPRECATED);
1291 LWIP_DEBUGF(SOCKETS_DEBUG, ("Deprecation turned on through ioctl for %s iface index %u \n",
1292 tmpnetif->name, tmpnetif->num));
1293 } else {
1294 tmpnetif->ipv6_flags = (tmpnetif->ipv6_flags & ((~LWIP_IPV6_ND6_FLAG_DEPRECATED) & 0xffU));
1295 LWIP_DEBUGF(SOCKETS_DEBUG, ("Deprecation turned off through ioctl for %s iface index %u \n",
1296 tmpnetif->name, tmpnetif->num));
1297 }
1298 return 0;
1299}
1300
1301static u8_t lwip_ioctl_internal_SIOCGIPV6DPCTD(struct ifreq *ifr)
1302{
1303 struct netif *tmpnetif = netif_find(ifr->ifr_name);
1304 if (tmpnetif == NULL) {
1305 LWIP_DEBUGF(SOCKETS_DEBUG, ("Interface not found.\n"));
1306 return ENODEV;
1307 }
1308
1309 ifr->ifr_ifru.ifru_ivalue = (tmpnetif->ipv6_flags & LWIP_IPV6_ND6_FLAG_DEPRECATED) ? 1 : 0;
1310 return 0;
1311}
1312
1313#endif /* LWIP_IOCTL_IPV6DPCTD */
1314#endif
1315
1316static u8_t lwip_ioctl_impl(const struct lwip_sock *sock, long cmd, void *argp)
1317{
1318 u8_t err = 0;
1319#if LWIP_NETIF_ETHTOOL
1320 s32_t ret;
1321#endif
1322#if LWIP_IPV6_DUP_DETECT_ATTEMPTS || LWIP_IOCTL_IPV6DPCTD || LWIP_IOCTL_IF || LWIP_NETIF_ETHTOOL
1323 struct ifreq *ifr = (struct ifreq *)argp;
1324#endif
1325#if LWIP_IOCTL_ROUTE
1326 struct rtentry *rmten = (struct rtentry *)argp;
1327#endif
1328#if LWIP_IPV6_DUP_DETECT_ATTEMPTS || LWIP_IOCTL_IPV6DPCTD || LWIP_IOCTL_ROUTE || LWIP_IOCTL_IF
1329 bool is_ipv6 = 0;
1330
1331 /* allow it only on IPv6 sockets... */
1332 is_ipv6 = NETCONNTYPE_ISIPV6((unsigned int)(sock->conn->type));
1333#endif
1334
1335 switch ((u32_t)cmd) {
1336#if LWIP_IPV6
1337#if LWIP_IPV6_DUP_DETECT_ATTEMPTS
1338 case SIOCSIPV6DAD:
1339 /* allow it only on IPv6 sockets... */
1340 if (is_ipv6 == 0) {
1341 err = EINVAL;
1342 } else {
1344 }
1345 break;
1346 case SIOCGIPV6DAD:
1347 /* allow it only on IPv6 sockets... */
1348 if (is_ipv6 == 0) {
1349 err = EINVAL;
1350 } else {
1352 }
1353 break;
1354#endif /* LWIP_IPV6_DUP_DETECT_ATTEMPTS */
1355#if LWIP_IOCTL_IPV6DPCTD
1356 case SIOCSIPV6DPCTD:
1357 /* allow it only on IPv6 sockets... */
1358 if (is_ipv6 == 0) {
1359 err = EINVAL;
1360 } else {
1362 }
1363 break;
1364 case SIOCGIPV6DPCTD:
1365 /* allow it only on IPv6 sockets... */
1366 if (is_ipv6 == 0) {
1367 err = EINVAL;
1368 } else {
1370 }
1371 break;
1372#endif
1373#endif /* LWIP_IPV6 */
1374#if LWIP_IOCTL_ROUTE
1375 case SIOCADDRT:
1376 /* Do not allow if socket is AF_INET6 */
1377 if (is_ipv6 != 0) {
1378 err = EINVAL;
1379 } else {
1380 err = lwip_ioctl_internal_SIOCADDRT(rmten);
1381 }
1382 break;
1383#endif
1384#if LWIP_IOCTL_IF
1385 case SIOCGIFCONF:
1386 /* Do not allow if socket is AF_INET6 */
1387 if (is_ipv6 != 0) {
1388 err = EINVAL;
1389 } else {
1391 }
1392 break;
1393 case SIOCGIFADDR:
1394 if (is_ipv6 != 0) {
1395 err = EINVAL;
1396 } else {
1398 }
1399 break;
1400 case SIOCSIFADDR:
1401 if (is_ipv6 != 0) {
1403 } else {
1405 }
1406 break;
1407 case SIOCDIFADDR:
1408 /* Delete interface address */
1409 if (is_ipv6 != 0) {
1411 } else {
1413 }
1414 break;
1415 case SIOCGIFNETMASK:
1416 if (is_ipv6 != 0) {
1417 err = EINVAL;
1418 } else {
1420 }
1421 break;
1422 case SIOCSIFNETMASK:
1423 if (is_ipv6 != 0) {
1424 err = EINVAL;
1425 } else {
1427 }
1428 break;
1429 case SIOCSIFHWADDR:
1431 break;
1432 case SIOCGIFHWADDR:
1434 break;
1435 case SIOCSIFFLAGS:
1437 break;
1438 case SIOCGIFFLAGS:
1440 break;
1441 case SIOCGIFNAME:
1443 break;
1444 case SIOCSIFNAME:
1446 break;
1447 /* Need to support the get index through ioctl
1448 * As of now the options is restricted to PF_PACKET scenario , so removed the compiler flag Begin
1449 */
1450 case SIOCGIFINDEX:
1452 break;
1453 case SIOCGIFMTU:
1455 break;
1456 case SIOCSIFMTU:
1458 break;
1459 case SIOCGIFBRDADDR:
1460 if (is_ipv6 != 0) {
1461 err = EINVAL;
1462 } else {
1464 }
1465 break;
1466#endif /* LWIP_IOCTL_IF */
1467#if LWIP_NETIF_ETHTOOL
1468 case SIOCETHTOOL:
1470 if (ret != 0) {
1471 /* an IO error happened */
1472 err = EIO;
1473 }
1474 break;
1475#endif
1476 /* START For cmd = -1 stack has to treat it as Invalid Input and return EINVAL */
1477 case 0xFFFFFFFF:
1478 err = EINVAL;
1479 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl_impl(INVALID: 0x%lx)\n", cmd));
1480 break;
1481 default:
1482 err = ENOSYS;
1483 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(UNIMPL: 0x%lx)\n", cmd));
1484 break;
1485 }
1486
1487 return err;
1488}
1489
1490static err_t lwip_do_ioctl_impl(struct tcpip_api_call_data *call)
1491{
1492 struct lwip_ioctl_apimsg *msg = (struct lwip_ioctl_apimsg *)(void *)call;
1493 return lwip_ioctl_impl(msg->sock, msg->cmd, msg->argp);
1494}
1495
1496#include "los_vm_map.h"
1497#include "user_copy.h"
1498static int do_ioctl_SIOCGIFCONF(int sockfd, long cmd, void *argp)
1499{
1500 int nbytes;
1501 struct ifconf ifc;
1502 char *buf_bak = NULL;
1503 int ret;
1504
1505 if (LOS_ArchCopyFromUser(&ifc, argp, sizeof(struct ifconf)) != 0) {
1506 set_errno(EFAULT);
1507 return -1;
1508 }
1509 nbytes = ifc.ifc_len;
1510 if (nbytes <= 0) {
1511 set_errno(EINVAL);
1512 return -1;
1513 }
1514 buf_bak = ifc.ifc_buf;
1515 if (!LOS_IsUserAddress((VADDR_T)(uintptr_t)buf_bak)) {
1516 set_errno(EFAULT);
1517 return -1;
1518 }
1519 ifc.ifc_buf = malloc(nbytes);
1520 if (ifc.ifc_buf == NULL) {
1521 set_errno(ENOMEM);
1522 return -1;
1523 }
1524 (void)memset_s(ifc.ifc_buf, nbytes, 0, nbytes);
1525
1526 ret = lwip_ioctl(sockfd, cmd, &ifc);
1527 if (ret == 0) {
1528 if (LOS_ArchCopyToUser(buf_bak, ifc.ifc_buf, nbytes) != 0) {
1529 set_errno(EFAULT);
1530 ret = -1;
1531 }
1532 }
1533
1534 free(ifc.ifc_buf);
1535 ifc.ifc_buf = buf_bak;
1536 if (LOS_ArchCopyToUser(argp, &ifc, sizeof(struct ifconf)) != 0) {
1537 set_errno(EFAULT);
1538 ret = -1;
1539 }
1540 return ret;
1541}
1542
1543int socks_ioctl(int sockfd, long cmd, void *argp)
1544{
1545 void *argpbak = argp;
1546 int ret;
1547 size_t nbytes = 0;
1548
1549 if (LOS_IsUserAddress((VADDR_T)(uintptr_t)argp)) {
1550 switch (cmd) {
1551 case FIONREAD:
1552 case FIONBIO:
1553 nbytes = sizeof(int);
1554 break;
1555 case SIOCADDRT:
1556 nbytes = sizeof(struct rtentry);
1557 break;
1558 case SIOCGIFCONF:
1559 return do_ioctl_SIOCGIFCONF(sockfd, cmd, argp);
1560 case SIOCSIPV6DAD:
1561 case SIOCGIPV6DAD:
1562 case SIOCSIPV6DPCTD:
1563 case SIOCGIPV6DPCTD:
1564 case SIOCGIFADDR:
1565 case SIOCSIFADDR:
1566 case SIOCDIFADDR:
1567 case SIOCGIFNETMASK:
1568 case SIOCSIFNETMASK:
1569 case SIOCSIFHWADDR:
1570 case SIOCGIFHWADDR:
1571 case SIOCSIFFLAGS:
1572 case SIOCGIFFLAGS:
1573 case SIOCGIFNAME:
1574 case SIOCSIFNAME:
1575 case SIOCGIFINDEX:
1576 case SIOCGIFMTU:
1577 case SIOCSIFMTU:
1578 case SIOCETHTOOL:
1579 case SIOCGIFBRDADDR:
1580 nbytes = sizeof(struct ifreq);
1581 break;
1582 default:
1583 nbytes = 0;
1584 }
1585 if (argp != NULL && nbytes > 0) {
1586 argp = malloc(nbytes);
1587 if (argp == NULL) {
1588 set_errno(ENOMEM);
1589 return -1;
1590 }
1591 if (LOS_ArchCopyFromUser(argp, argpbak, nbytes) != 0) {
1592 free(argp);
1593 set_errno(EFAULT);
1594 return -1;
1595 }
1596 }
1597 }
1598 ret = lwip_ioctl(sockfd, cmd, argp);
1599 if (ret == 0 && argp != argpbak) {
1600 if (LOS_ArchCopyToUser(argpbak, argp, nbytes) != 0) {
1601 /* how to rollback ioctl ? */
1602 set_errno(EFAULT);
1603 ret = -1;
1604 }
1605 }
1606 if (argp != argpbak) {
1607 free(argp);
1608 }
1609 return ret;
1610}
1611
1612void socks_refer(int sockfd)
1613{
1614 struct lwip_sock *sock = NULL;
1615 SYS_ARCH_DECL_PROTECT(lev);
1616
1617 sock = get_socket(sockfd);
1618 if (!sock) {
1619 return;
1620 }
1621
1622 SYS_ARCH_PROTECT(lev);
1623
1624 sock->s_refcount++;
1625
1626 SYS_ARCH_UNPROTECT(lev);
1627
1628 done_socket(sock);
1629}
1630
1631int socks_close(int sockfd)
1632{
1633 struct lwip_sock *sock = NULL;
1634 SYS_ARCH_DECL_PROTECT(lev);
1635
1636 sock = get_socket(sockfd);
1637 if (!sock) {
1638 return -1;
1639 }
1640
1641 SYS_ARCH_PROTECT(lev);
1642
1643 if (sock->s_refcount == 0) {
1644 SYS_ARCH_UNPROTECT(lev);
1645 done_socket(sock);
1646 return lwip_close(sockfd);
1647 }
1648
1649 sock->s_refcount--;
1650
1651 SYS_ARCH_UNPROTECT(lev);
1652 done_socket(sock);
1653 return 0;
1654}
BOOL IsCapPermit(UINT32 capIndex)
Definition: capability.c:43
err_t netif_set_hwaddr(struct netif *netif, const unsigned char *hw_addr, int hw_len)
Definition: fixme.c:297
err_t netif_dhcp_off(struct netif *netif)
Definition: fixme.c:54
err_t netif_set_mtu(struct netif *netif, u16_t netif_mtu)
Definition: fixme.c:270
LITE_OS_SEC_ALW_INLINE STATIC INLINE BOOL LOS_ListEmpty(LOS_DL_LIST *list)
Identify whether a specified doubly linked list is empty. | 判断链表是否为空
Definition: los_list.h:321
unsigned long VADDR_T
Definition: los_typedef.h:208
INT64 ssize_t
Definition: los_typedef.h:79
STATIC INLINE BOOL LOS_IsUserAddress(VADDR_T vaddr)
虚拟地址是否在用户空间
Definition: los_vm_map.h:275
void * malloc(size_t size)
动态分配内存块大小
Definition: malloc.c:81
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
static u8_t lwip_ioctl_internal_SIOCGIPV6DAD(struct ifreq *ifr)
Definition: sockets.c:1257
static u8_t lwip_ioctl_internal_SIOCSIFNETMASK(struct ifreq *ifr)
Definition: sockets.c:712
static int lwip_socket_wrap(int domain, int type, int protocol)
Definition: sockets.c:126
static u8_t lwip_ioctl_internal_SIOCGIFFLAGS(struct ifreq *ifr)
Definition: sockets.c:934
static int lwip_bind2(int s, const struct sockaddr *name, socklen_t namelen)
ssize_t lwip_sendto(int s, const void *dataptr, size_t size, int flags, const struct sockaddr *to, socklen_t tolen)
Definition: sockets.c:90
static u8_t lwip_ioctl_internal_SIOCGIFCONF(struct ifreq *ifr)
Definition: sockets.c:467
int get_unused_socket_num(void)
Definition: sockets.c:343
int socks_ioctl(int sockfd, long cmd, void *argp)
Definition: sockets.c:1543
static u8_t lwip_ioctl_internal_SIOCADDRT(struct rtentry *rmten)
Definition: sockets.c:408
static int lwip_setsockopt2(int s, int level, int optname, const void *optval, socklen_t optlen)
ssize_t lwip_sendto2(int s, const void *dataptr, size_t size, int flags, const struct sockaddr *to, socklen_t tolen)
int lwip_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
Definition: sockets.c:77
static int ip_addr_isbroadcast_bysock(const ip_addr_t *ipaddr, int s)
Definition: sockets.c:173
static u8_t lwip_ioctl_internal_SIOCDIFADDR_6(struct ifreq *ifr)
Definition: sockets.c:635
static u8_t lwip_ioctl_internal_SIOCGIFMTU(struct ifreq *ifr)
Definition: sockets.c:1163
static ssize_t lwip_sendto_wrap(int s, const void *dataptr, size_t size, int flags, const struct sockaddr *to, socklen_t tolen)
Definition: sockets.c:233
static u8_t lwip_ioctl_internal_SIOCGIFNETMASK(struct ifreq *ifr)
Definition: sockets.c:695
static err_t lwip_do_ioctl_impl(struct tcpip_api_call_data *call)
Definition: sockets.c:1490
static void poll_check_waiters(int s, int check_waiters)
Definition: sockets.c:262
static u8_t lwip_ioctl_impl(const struct lwip_sock *sock, long cmd, void *argp)
Definition: sockets.c:1316
static u8_t lwip_ioctl_internal_SIOCSIFADDR_6(struct ifreq *ifr)
Definition: sockets.c:552
static u8_t lwip_ioctl_internal_SIOCGIFNAME(struct ifreq *ifr)
Definition: sockets.c:1009
static s32_t lwip_ioctl_internal_SIOCETHTOOL(struct ifreq *ifr)
Definition: sockets.c:1201
int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen)
Definition: sockets.c:83
int socks_close(int sockfd)
Definition: sockets.c:1631
static u8_t lwip_ioctl_internal_SIOCSIFMTU(struct ifreq *ifr)
Definition: sockets.c:1133
API_ALIAS(lwip_ioctl, ioctlsocket)
static u8_t lwip_ioctl_internal_SIOCSIFHWADDR(struct ifreq *ifr)
Definition: sockets.c:785
void socks_refer(int sockfd)
Definition: sockets.c:1612
static u8_t lwip_ioctl_internal_SIOCSIFADDR(struct ifreq *ifr)
Definition: sockets.c:558
static u8_t lwip_ioctl_internal_SIOCGIFINDEX(struct ifreq *ifr)
Definition: sockets.c:1120
void __wake_up_interruptible_poll(wait_queue_head_t *wait, pollevent_t key)
static u8_t lwip_ioctl_internal_SIOCDIFADDR(struct ifreq *ifr)
Definition: sockets.c:641
static u8_t lwip_ioctl_internal_SIOCGIPV6DPCTD(struct ifreq *ifr)
Definition: sockets.c:1301
static u8_t lwip_ioctl_internal_SIOCGIFADDR(struct ifreq *ifr)
Definition: sockets.c:522
static int lwip_setsockopt_wrap(int s, int level, int optname, const void *optval, socklen_t optlen)
Definition: sockets.c:141
static u8_t lwip_ioctl_internal_SIOCSIFNAME(struct ifreq *ifr)
Definition: sockets.c:1082
int lwip_socket(int domain, int type, int protocol)
Definition: sockets.c:71
static u8_t lwip_ioctl_internal_SIOCGIFBRDADDR(struct ifreq *ifr)
Definition: sockets.c:1178
static int lwip_socket2(int domain, int type, int protocol)
static int lwip_bind_wrap(int s, const struct sockaddr *name, socklen_t namelen)
Definition: sockets.c:207
static u8_t lwip_ioctl_internal_SIOCSIPV6DPCTD(struct ifreq *ifr)
Definition: sockets.c:1272
static u8_t lwip_ioctl_internal_SIOCSIPV6DAD(struct ifreq *ifr)
Definition: sockets.c:1224
int socks_poll(int s, poll_table *wait)
Definition: sockets.c:297
static u8_t lwip_ioctl_internal_SIOCSIFFLAGS(struct ifreq *ifr)
Definition: sockets.c:850
static bool lwip_validate_ifname(const char *name, u8_t *let_pos)
Definition: sockets.c:1038
static int do_ioctl_SIOCGIFCONF(int sockfd, long cmd, void *argp)
Definition: sockets.c:1498
static u8_t lwip_ioctl_internal_SIOCGIFHWADDR(struct ifreq *ifr)
Definition: sockets.c:827
void poll_wait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
struct tcpip_api_call_data call
Definition: sockets.c:60
struct lwip_sock * sock
Definition: sockets.c:61
ARG_NUM_3 int
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void
size_t LOS_ArchCopyToUser(void *dst, const void *src, size_t len)
从内核空间拷贝到用户空间
Definition: user_copy.c:79
size_t LOS_ArchCopyFromUser(void *dst, const void *src, size_t len)
Definition: user_copy.c:58