4343#define DBG_LVL DBG_INFO
4444#include <rtdbg.h>
4545
46- #define SOCKET_TABLE_STEP_LEN 4
46+ #define VALID_PROTOCOL (protocol ) ((protocol) >= 0 && (protocol) <= IPPROTO_RAW)
47+ #define VALID_COMBO (domain , type , protocol ) \
48+ ( \
49+ (((domain) == AF_INET || (domain) == AF_INET6) && \
50+ (((type) == SOCK_STREAM && ((protocol) == 0 || (protocol) == IPPROTO_TCP)) || \
51+ ((type) == SOCK_DGRAM && ((protocol) == 0 || (protocol) == IPPROTO_UDP)) || \
52+ ((type) == SOCK_RAW && ((protocol) == IPPROTO_RAW)) \
53+ )) || \
54+ ((domain) == AF_UNIX && (type) == SOCK_STREAM && (protocol) == 0) || \
55+ ((domain) == AF_NETLINK && (type) == SOCK_RAW && (protocol) == 0) \
56+ )
4757
4858/* the socket table used to dynamic allocate sockets */
4959struct sal_socket_table
@@ -434,31 +444,51 @@ int sal_netdev_cleanup(struct netdev *netdev)
434444 * -1 : input the wrong family
435445 * -2 : input the wrong socket type
436446 * -3 : get network interface failed
447+ * -4 : invalid protocol or combo
437448 */
438449static int socket_init (int family , int type , int protocol , struct sal_socket * * res )
439450{
440-
441451 struct sal_socket * sock ;
442452 struct sal_proto_family * pf ;
443453 struct netdev * netdv_def = netdev_default ;
444454 struct netdev * netdev = RT_NULL ;
445455 rt_bool_t flag = RT_FALSE ;
446456
457+ /* Existing range checks for family and type */
447458 if (family < 0 || family > AF_MAX )
448459 {
460+ LOG_E ("Invalid family: %d (must be 0 ~ %d)" , family , AF_MAX );
449461 return -1 ;
450462 }
451463
452464 if (type < 0 || type > SOCK_MAX )
453465 {
466+ LOG_E ("Invalid type: %d (must be 0 ~ %d)" , type , SOCK_MAX );
454467 return -2 ;
455468 }
456469
470+ /* Range check for protocol */
471+ if (!VALID_PROTOCOL (protocol ))
472+ {
473+ LOG_E ("Invalid protocol: %d (must be 0 ~ %d)" , protocol , IPPROTO_RAW );
474+ rt_set_errno (EINVAL );
475+ return -4 ;
476+ }
477+
457478 sock = * res ;
458479 sock -> domain = family ;
459480 sock -> type = type ;
460481 sock -> protocol = protocol ;
461482
483+ /* Combo compatibility check */
484+ if (!VALID_COMBO (family , type , protocol ))
485+ {
486+ LOG_E ("Invalid combo: domain=%d, type=%d, protocol=%d" , family , type , protocol );
487+ rt_set_errno (EINVAL );
488+ return -4 ;
489+ }
490+
491+ /* Existing netdev selection logic */
462492 if (netdv_def && netdev_is_up (netdv_def ))
463493 {
464494 /* check default network interface device protocol family */
@@ -483,6 +513,8 @@ static int socket_init(int family, int type, int protocol, struct sal_socket **r
483513 sock -> netdev = netdev ;
484514 }
485515
516+ LOG_D ("Socket init success: domain=%d, type=%d, protocol=%d, netdev=%s" ,
517+ family , type , protocol , sock -> netdev ? sock -> netdev -> name : "default" );
486518 return 0 ;
487519}
488520
@@ -1051,7 +1083,7 @@ int sal_socket(int domain, int type, int protocol)
10511083 {
10521084 LOG_E ("SAL socket protocol family input failed, return error %d." , retval );
10531085 socket_delete (socket );
1054- return -1 ;
1086+ return retval ;
10551087 }
10561088
10571089 /* valid the network interface socket opreation */
0 commit comments