1 // SPDX-License-Identifier: GPL-2.0
2 /* nettest - used for functional tests of networking APIs
4 * Copyright (c) 2013-2019 David Ahern <dsahern@gmail.com>. All rights reserved.
10 #include <sys/ioctl.h>
11 #include <sys/socket.h>
12 #include <linux/tcp.h>
13 #include <arpa/inet.h>
15 #include <netinet/in.h>
28 #ifndef IPV6_UNICAST_IF
29 #define IPV6_UNICAST_IF 76
31 #ifndef IPV6_MULTICAST_IF
32 #define IPV6_MULTICAST_IF 17
35 #define DEFAULT_PORT 12345
38 #define MAX(a, b) ((a) > (b) ? (a) : (b))
41 #define MIN(a, b) ((a) < (b) ? (a) : (b))
56 int scope_id
; /* remote scope; v6 send only */
58 struct in_addr grp
; /* multicast group */
60 unsigned int has_local_ip
:1,
69 int type
; /* DGRAM, STREAM, RAW */
71 int version
; /* AF_INET/AF_INET6 */
79 /* prefix for MD5 password */
81 struct sockaddr_in v4
;
82 struct sockaddr_in6 v6
;
84 unsigned int prefix_len
;
86 /* expected addresses and device index for connection */
102 static int server_mode
;
103 static unsigned int prog_timeout
= 5;
104 static unsigned int interactive
;
106 static char *msg
= "Hello world!";
109 static int try_broadcast
= 1;
111 static char *timestamp(char *timebuf
, int buflen
)
116 if (strftime(timebuf
, buflen
, "%T", localtime(&now
)) == 0) {
117 memset(timebuf
, 0, buflen
);
118 strncpy(timebuf
, "00:00:00", buflen
-1);
124 static void log_msg(const char *format
, ...)
132 fprintf(stdout
, "%s %s:",
133 timestamp(timebuf
, sizeof(timebuf
)),
134 server_mode
? "server" : "client");
135 va_start(args
, format
);
136 vfprintf(stdout
, format
, args
);
142 static void log_error(const char *format
, ...)
150 fprintf(stderr
, "%s %s:",
151 timestamp(timebuf
, sizeof(timebuf
)),
152 server_mode
? "server" : "client");
153 va_start(args
, format
);
154 vfprintf(stderr
, format
, args
);
160 static void log_err_errno(const char *fmt
, ...)
168 fprintf(stderr
, "%s %s: ",
169 timestamp(timebuf
, sizeof(timebuf
)),
170 server_mode
? "server" : "client");
172 vfprintf(stderr
, fmt
, args
);
175 fprintf(stderr
, ": %d: %s\n", errno
, strerror(errno
));
179 static void log_address(const char *desc
, struct sockaddr
*sa
)
186 if (sa
->sa_family
== AF_INET
) {
187 struct sockaddr_in
*s
= (struct sockaddr_in
*) sa
;
191 inet_ntop(AF_INET
, &s
->sin_addr
, addrstr
,
195 } else if (sa
->sa_family
== AF_INET6
) {
196 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*) sa
;
198 log_msg("%s [%s]:%d",
200 inet_ntop(AF_INET6
, &s6
->sin6_addr
, addrstr
,
202 ntohs(s6
->sin6_port
));
210 static int tcp_md5sig(int sd
, void *addr
, socklen_t alen
, struct sock_args
*args
)
212 int keylen
= strlen(args
->password
);
213 struct tcp_md5sig md5sig
= {};
214 int opt
= TCP_MD5SIG
;
217 md5sig
.tcpm_keylen
= keylen
;
218 memcpy(md5sig
.tcpm_key
, args
->password
, keylen
);
220 if (args
->prefix_len
) {
221 opt
= TCP_MD5SIG_EXT
;
222 md5sig
.tcpm_flags
|= TCP_MD5SIG_FLAG_PREFIX
;
224 md5sig
.tcpm_prefixlen
= args
->prefix_len
;
225 addr
= &args
->md5_prefix
;
227 memcpy(&md5sig
.tcpm_addr
, addr
, alen
);
230 opt
= TCP_MD5SIG_EXT
;
231 md5sig
.tcpm_flags
|= TCP_MD5SIG_FLAG_IFINDEX
;
233 md5sig
.tcpm_ifindex
= args
->ifindex
;
236 rc
= setsockopt(sd
, IPPROTO_TCP
, opt
, &md5sig
, sizeof(md5sig
));
238 /* ENOENT is harmless. Returned when a password is cleared */
242 log_err_errno("setsockopt(TCP_MD5SIG)");
248 static int tcp_md5_remote(int sd
, struct sock_args
*args
)
250 struct sockaddr_in sin
= {
251 .sin_family
= AF_INET
,
253 struct sockaddr_in6 sin6
= {
254 .sin6_family
= AF_INET6
,
259 switch (args
->version
) {
261 sin
.sin_port
= htons(args
->port
);
262 sin
.sin_addr
= args
->remote_addr
.in
;
267 sin6
.sin6_port
= htons(args
->port
);
268 sin6
.sin6_addr
= args
->remote_addr
.in6
;
273 log_error("unknown address family\n");
277 if (tcp_md5sig(sd
, addr
, alen
, args
))
283 static int get_ifidx(const char *ifname
)
288 if (!ifname
|| *ifname
== '\0')
291 memset(&ifdata
, 0, sizeof(ifdata
));
293 strcpy(ifdata
.ifr_name
, ifname
);
295 sd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
297 log_err_errno("socket failed");
301 rc
= ioctl(sd
, SIOCGIFINDEX
, (char *)&ifdata
);
304 log_err_errno("ioctl(SIOCGIFINDEX) failed");
308 return ifdata
.ifr_ifindex
;
311 static int bind_to_device(int sd
, const char *name
)
315 rc
= setsockopt(sd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen(name
)+1);
317 log_err_errno("setsockopt(SO_BINDTODEVICE)");
322 static int get_bind_to_device(int sd
, char *name
, size_t len
)
325 socklen_t optlen
= len
;
328 rc
= getsockopt(sd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, &optlen
);
330 log_err_errno("setsockopt(SO_BINDTODEVICE)");
335 static int check_device(int sd
, struct sock_args
*args
)
340 if (get_bind_to_device(sd
, name
, sizeof(name
)))
343 ifindex
= get_ifidx(name
);
345 log_msg(" bound to device %s/%d\n",
346 *name
? name
: "<none>", ifindex
);
348 if (!args
->expected_ifindex
)
351 if (args
->expected_ifindex
!= ifindex
) {
352 log_error("Device index mismatch: expected %d have %d\n",
353 args
->expected_ifindex
, ifindex
);
357 log_msg("Device index matches: expected %d have %d\n",
358 args
->expected_ifindex
, ifindex
);
363 static int set_pktinfo_v4(int sd
)
368 rc
= setsockopt(sd
, SOL_IP
, IP_PKTINFO
, &one
, sizeof(one
));
369 if (rc
< 0 && rc
!= -ENOTSUP
)
370 log_err_errno("setsockopt(IP_PKTINFO)");
375 static int set_recvpktinfo_v6(int sd
)
380 rc
= setsockopt(sd
, SOL_IPV6
, IPV6_RECVPKTINFO
, &one
, sizeof(one
));
381 if (rc
< 0 && rc
!= -ENOTSUP
)
382 log_err_errno("setsockopt(IPV6_RECVPKTINFO)");
387 static int set_recverr_v4(int sd
)
392 rc
= setsockopt(sd
, SOL_IP
, IP_RECVERR
, &one
, sizeof(one
));
393 if (rc
< 0 && rc
!= -ENOTSUP
)
394 log_err_errno("setsockopt(IP_RECVERR)");
399 static int set_recverr_v6(int sd
)
404 rc
= setsockopt(sd
, SOL_IPV6
, IPV6_RECVERR
, &one
, sizeof(one
));
405 if (rc
< 0 && rc
!= -ENOTSUP
)
406 log_err_errno("setsockopt(IPV6_RECVERR)");
411 static int set_unicast_if(int sd
, int ifindex
, int version
)
413 int opt
= IP_UNICAST_IF
;
417 ifindex
= htonl(ifindex
);
419 if (version
== AF_INET6
) {
420 opt
= IPV6_UNICAST_IF
;
423 rc
= setsockopt(sd
, level
, opt
, &ifindex
, sizeof(ifindex
));
425 log_err_errno("setsockopt(IP_UNICAST_IF)");
430 static int set_multicast_if(int sd
, int ifindex
)
432 struct ip_mreqn mreq
= { .imr_ifindex
= ifindex
};
435 rc
= setsockopt(sd
, SOL_IP
, IP_MULTICAST_IF
, &mreq
, sizeof(mreq
));
437 log_err_errno("setsockopt(IP_MULTICAST_IF)");
442 static int set_membership(int sd
, uint32_t grp
, uint32_t addr
, int ifindex
)
444 uint32_t if_addr
= addr
;
445 struct ip_mreqn mreq
;
448 if (addr
== htonl(INADDR_ANY
) && !ifindex
) {
449 log_error("Either local address or device needs to be given for multicast membership\n");
453 mreq
.imr_multiaddr
.s_addr
= grp
;
454 mreq
.imr_address
.s_addr
= if_addr
;
455 mreq
.imr_ifindex
= ifindex
;
457 rc
= setsockopt(sd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
, &mreq
, sizeof(mreq
));
459 log_err_errno("setsockopt(IP_ADD_MEMBERSHIP)");
466 static int set_broadcast(int sd
)
468 unsigned int one
= 1;
471 if (setsockopt(sd
, SOL_SOCKET
, SO_BROADCAST
, &one
, sizeof(one
)) != 0) {
472 log_err_errno("setsockopt(SO_BROADCAST)");
479 static int set_reuseport(int sd
)
481 unsigned int one
= 1;
484 if (setsockopt(sd
, SOL_SOCKET
, SO_REUSEPORT
, &one
, sizeof(one
)) != 0) {
485 log_err_errno("setsockopt(SO_REUSEPORT)");
492 static int set_reuseaddr(int sd
)
494 unsigned int one
= 1;
497 if (setsockopt(sd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
)) != 0) {
498 log_err_errno("setsockopt(SO_REUSEADDR)");
505 static int str_to_uint(const char *str
, int min
, int max
, unsigned int *value
)
511 number
= (unsigned int) strtoul(str
, &end
, 0);
513 /* entire string should be consumed by conversion
514 * and value should be between min and max
516 if (((*end
== '\0') || (*end
== '\n')) && (end
!= str
) &&
517 (errno
!= ERANGE
) && (min
<= number
) && (number
<= max
)) {
525 static int expected_addr_match(struct sockaddr
*sa
, void *expected
,
531 if (sa
->sa_family
== AF_INET
) {
532 struct sockaddr_in
*s
= (struct sockaddr_in
*) sa
;
533 struct in_addr
*exp_in
= (struct in_addr
*) expected
;
535 if (s
->sin_addr
.s_addr
!= exp_in
->s_addr
) {
536 log_error("%s address does not match expected %s",
538 inet_ntop(AF_INET
, exp_in
,
539 addrstr
, sizeof(addrstr
)));
542 } else if (sa
->sa_family
== AF_INET6
) {
543 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*) sa
;
544 struct in6_addr
*exp_in
= (struct in6_addr
*) expected
;
546 if (memcmp(&s6
->sin6_addr
, exp_in
, sizeof(*exp_in
))) {
547 log_error("%s address does not match expected %s",
549 inet_ntop(AF_INET6
, exp_in
,
550 addrstr
, sizeof(addrstr
)));
554 log_error("%s address does not match expected - unknown family",
560 log_msg("%s address matches expected\n", desc
);
565 static int show_sockstat(int sd
, struct sock_args
*args
)
567 struct sockaddr_in6 local_addr
, remote_addr
;
568 socklen_t alen
= sizeof(local_addr
);
573 desc
= server_mode
? "server local:" : "client local:";
574 sa
= (struct sockaddr
*) &local_addr
;
575 if (getsockname(sd
, sa
, &alen
) == 0) {
576 log_address(desc
, sa
);
578 if (args
->has_expected_laddr
) {
579 rc
= expected_addr_match(sa
, &args
->expected_laddr
,
583 log_err_errno("getsockname failed");
586 sa
= (struct sockaddr
*) &remote_addr
;
587 desc
= server_mode
? "server peer:" : "client peer:";
588 if (getpeername(sd
, sa
, &alen
) == 0) {
589 log_address(desc
, sa
);
591 if (args
->has_expected_raddr
) {
592 rc
|= expected_addr_match(sa
, &args
->expected_raddr
,
596 log_err_errno("getpeername failed");
602 static int get_index_from_cmsg(struct msghdr
*m
)
608 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(m
);
609 m
->msg_controllen
!= 0 && cm
;
610 cm
= (struct cmsghdr
*)CMSG_NXTHDR(m
, cm
)) {
612 if (cm
->cmsg_level
== SOL_IP
&&
613 cm
->cmsg_type
== IP_PKTINFO
) {
614 struct in_pktinfo
*pi
;
616 pi
= (struct in_pktinfo
*)(CMSG_DATA(cm
));
617 inet_ntop(AF_INET
, &pi
->ipi_addr
, buf
, sizeof(buf
));
618 ifindex
= pi
->ipi_ifindex
;
619 } else if (cm
->cmsg_level
== SOL_IPV6
&&
620 cm
->cmsg_type
== IPV6_PKTINFO
) {
621 struct in6_pktinfo
*pi6
;
623 pi6
= (struct in6_pktinfo
*)(CMSG_DATA(cm
));
624 inet_ntop(AF_INET6
, &pi6
->ipi6_addr
, buf
, sizeof(buf
));
625 ifindex
= pi6
->ipi6_ifindex
;
630 log_msg(" pktinfo: ifindex %d dest addr %s\n",
636 static int send_msg_no_cmsg(int sd
, void *addr
, socklen_t alen
)
641 err
= sendto(sd
, msg
, msglen
, 0, addr
, alen
);
643 if (errno
== EACCES
&& try_broadcast
) {
645 if (!set_broadcast(sd
))
650 log_err_errno("sendto failed");
657 static int send_msg_cmsg(int sd
, void *addr
, socklen_t alen
,
658 int ifindex
, int version
)
660 unsigned char cmsgbuf
[64];
666 iov
[0].iov_base
= msg
;
667 iov
[0].iov_len
= msglen
;
670 m
.msg_name
= (caddr_t
)addr
;
671 m
.msg_namelen
= alen
;
673 memset(cmsgbuf
, 0, sizeof(cmsgbuf
));
674 cm
= (struct cmsghdr
*)cmsgbuf
;
675 m
.msg_control
= (caddr_t
)cm
;
677 if (version
== AF_INET
) {
678 struct in_pktinfo
*pi
;
680 cm
->cmsg_level
= SOL_IP
;
681 cm
->cmsg_type
= IP_PKTINFO
;
682 cm
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
683 pi
= (struct in_pktinfo
*)(CMSG_DATA(cm
));
684 pi
->ipi_ifindex
= ifindex
;
686 m
.msg_controllen
= cm
->cmsg_len
;
688 } else if (version
== AF_INET6
) {
689 struct in6_pktinfo
*pi6
;
691 cm
->cmsg_level
= SOL_IPV6
;
692 cm
->cmsg_type
= IPV6_PKTINFO
;
693 cm
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
695 pi6
= (struct in6_pktinfo
*)(CMSG_DATA(cm
));
696 pi6
->ipi6_ifindex
= ifindex
;
698 m
.msg_controllen
= cm
->cmsg_len
;
702 err
= sendmsg(sd
, &m
, 0);
704 if (errno
== EACCES
&& try_broadcast
) {
706 if (!set_broadcast(sd
))
711 log_err_errno("sendmsg failed");
719 static int send_msg(int sd
, void *addr
, socklen_t alen
, struct sock_args
*args
)
721 if (args
->type
== SOCK_STREAM
) {
722 if (write(sd
, msg
, msglen
) < 0) {
723 log_err_errno("write failed sending msg to peer");
726 } else if (args
->ifindex
&& args
->use_cmsg
) {
727 if (send_msg_cmsg(sd
, addr
, alen
, args
->ifindex
, args
->version
))
730 if (send_msg_no_cmsg(sd
, addr
, alen
))
734 log_msg("Sent message:\n");
735 log_msg(" %.24s%s\n", msg
, msglen
> 24 ? " ..." : "");
740 static int socket_read_dgram(int sd
, struct sock_args
*args
)
742 unsigned char addr
[sizeof(struct sockaddr_in6
)];
743 struct sockaddr
*sa
= (struct sockaddr
*) addr
;
744 socklen_t alen
= sizeof(addr
);
747 .msg_name
= (caddr_t
)addr
,
752 unsigned char cmsgbuf
[256];
753 struct cmsghdr
*cm
= (struct cmsghdr
*)cmsgbuf
;
758 iov
[0].iov_base
= (caddr_t
)buf
;
759 iov
[0].iov_len
= sizeof(buf
);
761 memset(cmsgbuf
, 0, sizeof(cmsgbuf
));
762 m
.msg_control
= (caddr_t
)cm
;
763 m
.msg_controllen
= sizeof(cmsgbuf
);
765 len
= recvmsg(sd
, &m
, 0);
767 log_msg("peer closed connection.\n");
769 } else if (len
< 0) {
770 log_msg("failed to read message: %d: %s\n",
771 errno
, strerror(errno
));
777 log_address("Message from:", sa
);
778 log_msg(" %.24s%s\n", buf
, len
> 24 ? " ..." : "");
780 ifindex
= get_index_from_cmsg(&m
);
781 if (args
->expected_ifindex
) {
782 if (args
->expected_ifindex
!= ifindex
) {
783 log_error("Device index mismatch: expected %d have %d\n",
784 args
->expected_ifindex
, ifindex
);
787 log_msg("Device index matches: expected %d have %d\n",
788 args
->expected_ifindex
, ifindex
);
791 if (!interactive
&& server_mode
) {
792 if (sa
->sa_family
== AF_INET6
) {
793 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*) sa
;
794 struct in6_addr
*in6
= &s6
->sin6_addr
;
796 if (IN6_IS_ADDR_V4MAPPED(in6
)) {
797 const uint32_t *pa
= (uint32_t *) &in6
->s6_addr
;
799 struct sockaddr_in
*sin
;
801 sin
= (struct sockaddr_in
*) addr
;
805 sin
->sin_family
= AF_INET
;
806 if (send_msg_cmsg(sd
, addr
, alen
,
807 ifindex
, AF_INET
) < 0)
812 iov
[0].iov_len
= len
;
814 if (args
->version
== AF_INET6
) {
815 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*) sa
;
818 /* avoid PKTINFO conflicts with bindtodev */
819 if (sendto(sd
, buf
, len
, 0,
820 (void *) addr
, alen
) < 0)
823 /* kernel is allowing scope_id to be set to VRF
824 * index for LLA. for sends to global address
827 s6
->sin6_scope_id
= ifindex
;
828 if (sendmsg(sd
, &m
, 0) < 0)
834 err
= sendmsg(sd
, &m
, 0);
836 if (errno
== EACCES
&& try_broadcast
) {
838 if (!set_broadcast(sd
))
845 log_msg("Sent message:\n");
846 log_msg(" %.24s%s\n", buf
, len
> 24 ? " ..." : "");
851 log_err_errno("failed to send msg to peer");
855 static int socket_read_stream(int sd
)
860 len
= read(sd
, buf
, sizeof(buf
)-1);
862 log_msg("client closed connection.\n");
864 } else if (len
< 0) {
865 log_msg("failed to read message\n");
870 log_msg("Incoming message:\n");
871 log_msg(" %.24s%s\n", buf
, len
> 24 ? " ..." : "");
873 if (!interactive
&& server_mode
) {
874 if (write(sd
, buf
, len
) < 0) {
875 log_err_errno("failed to send buf");
878 log_msg("Sent message:\n");
879 log_msg(" %.24s%s\n", buf
, len
> 24 ? " ..." : "");
885 static int socket_read(int sd
, struct sock_args
*args
)
887 if (args
->type
== SOCK_STREAM
)
888 return socket_read_stream(sd
);
890 return socket_read_dgram(sd
, args
);
893 static int stdin_to_socket(int sd
, int type
, void *addr
, socklen_t alen
)
898 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
902 if (type
== SOCK_STREAM
) {
903 if (write(sd
, buf
, len
) < 0) {
904 log_err_errno("failed to send buf");
911 err
= sendto(sd
, buf
, len
, 0, addr
, alen
);
913 if (errno
== EACCES
&& try_broadcast
) {
915 if (!set_broadcast(sd
))
919 log_err_errno("failed to send msg to peer");
923 log_msg("Sent message:\n");
924 log_msg(" %.24s%s\n", buf
, len
> 24 ? " ..." : "");
929 static void set_recv_attr(int sd
, int version
)
931 if (version
== AF_INET6
) {
932 set_recvpktinfo_v6(sd
);
940 static int msg_loop(int client
, int sd
, void *addr
, socklen_t alen
,
941 struct sock_args
*args
)
943 struct timeval timeout
= { .tv_sec
= prog_timeout
}, *ptval
= NULL
;
948 if (args
->type
!= SOCK_STREAM
)
949 set_recv_attr(sd
, args
->version
);
952 msglen
= strlen(msg
);
954 /* client sends first message */
956 if (send_msg(sd
, addr
, alen
, args
))
966 nfds
= interactive
? MAX(fileno(stdin
), sd
) + 1 : sd
+ 1;
971 FD_SET(fileno(stdin
), &rfds
);
973 rc
= select(nfds
, &rfds
, NULL
, NULL
, ptval
);
979 log_err_errno("select failed");
981 } else if (rc
== 0) {
982 log_error("Timed out waiting for response\n");
987 if (FD_ISSET(sd
, &rfds
)) {
988 rc
= socket_read(sd
, args
);
999 if (FD_ISSET(fileno(stdin
), &rfds
)) {
1000 if (stdin_to_socket(sd
, args
->type
, addr
, alen
) <= 0)
1013 log_msg("Going into quiet mode\n");
1017 if (send_msg(sd
, addr
, alen
, args
)) {
1027 static int msock_init(struct sock_args
*args
, int server
)
1029 uint32_t if_addr
= htonl(INADDR_ANY
);
1030 struct sockaddr_in laddr
= {
1031 .sin_family
= AF_INET
,
1032 .sin_port
= htons(args
->port
),
1037 if (!server
&& args
->has_local_ip
)
1038 if_addr
= args
->local_addr
.in
.s_addr
;
1040 sd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1042 log_err_errno("socket");
1046 if (setsockopt(sd
, SOL_SOCKET
, SO_REUSEADDR
,
1047 (char *)&one
, sizeof(one
)) < 0) {
1048 log_err_errno("Setting SO_REUSEADDR error");
1052 if (setsockopt(sd
, SOL_SOCKET
, SO_BROADCAST
,
1053 (char *)&one
, sizeof(one
)) < 0)
1054 log_err_errno("Setting SO_BROADCAST error");
1056 if (args
->dev
&& bind_to_device(sd
, args
->dev
) != 0)
1058 else if (args
->use_setsockopt
&&
1059 set_multicast_if(sd
, args
->ifindex
))
1062 laddr
.sin_addr
.s_addr
= if_addr
;
1064 if (bind(sd
, (struct sockaddr
*) &laddr
, sizeof(laddr
)) < 0) {
1065 log_err_errno("bind failed");
1070 set_membership(sd
, args
->grp
.s_addr
,
1071 args
->local_addr
.in
.s_addr
, args
->ifindex
))
1080 static int msock_server(struct sock_args
*args
)
1082 return msock_init(args
, 1);
1085 static int msock_client(struct sock_args
*args
)
1087 return msock_init(args
, 0);
1090 static int bind_socket(int sd
, struct sock_args
*args
)
1092 struct sockaddr_in serv_addr
= {
1093 .sin_family
= AF_INET
,
1095 struct sockaddr_in6 serv6_addr
= {
1096 .sin6_family
= AF_INET6
,
1101 if (!args
->has_local_ip
&& args
->type
== SOCK_RAW
)
1104 switch (args
->version
) {
1106 serv_addr
.sin_port
= htons(args
->port
);
1107 serv_addr
.sin_addr
= args
->local_addr
.in
;
1109 alen
= sizeof(serv_addr
);
1113 serv6_addr
.sin6_port
= htons(args
->port
);
1114 serv6_addr
.sin6_addr
= args
->local_addr
.in6
;
1116 alen
= sizeof(serv6_addr
);
1120 log_error("Invalid address family\n");
1124 if (bind(sd
, addr
, alen
) < 0) {
1125 log_err_errno("error binding socket");
1132 static int lsock_init(struct sock_args
*args
)
1137 sd
= socket(args
->version
, args
->type
, args
->protocol
);
1139 log_err_errno("Error opening socket");
1143 if (set_reuseaddr(sd
) != 0)
1146 if (set_reuseport(sd
) != 0)
1149 if (args
->dev
&& bind_to_device(sd
, args
->dev
) != 0)
1151 else if (args
->use_setsockopt
&&
1152 set_unicast_if(sd
, args
->ifindex
, args
->version
))
1155 if (bind_socket(sd
, args
))
1158 if (args
->bind_test_only
)
1161 if (args
->type
== SOCK_STREAM
&& listen(sd
, 1) < 0) {
1162 log_err_errno("listen failed");
1166 flags
= fcntl(sd
, F_GETFL
);
1167 if ((flags
< 0) || (fcntl(sd
, F_SETFL
, flags
|O_NONBLOCK
) < 0)) {
1168 log_err_errno("Failed to set non-blocking option");
1172 if (fcntl(sd
, F_SETFD
, FD_CLOEXEC
) < 0)
1173 log_err_errno("Failed to set close-on-exec flag");
1183 static int do_server(struct sock_args
*args
)
1185 struct timeval timeout
= { .tv_sec
= prog_timeout
}, *ptval
= NULL
;
1186 unsigned char addr
[sizeof(struct sockaddr_in6
)] = {};
1187 socklen_t alen
= sizeof(addr
);
1197 lsd
= msock_server(args
);
1199 lsd
= lsock_init(args
);
1204 if (args
->bind_test_only
) {
1209 if (args
->type
!= SOCK_STREAM
) {
1210 rc
= msg_loop(0, lsd
, (void *) addr
, alen
, args
);
1215 if (args
->password
&& tcp_md5_remote(lsd
, args
)) {
1222 log_msg("waiting for client connection.\n");
1226 rc
= select(lsd
+1, &rfds
, NULL
, NULL
, ptval
);
1236 log_err_errno("select failed");
1240 if (FD_ISSET(lsd
, &rfds
)) {
1242 csd
= accept(lsd
, (void *) addr
, &alen
);
1244 log_err_errno("accept failed");
1248 rc
= show_sockstat(csd
, args
);
1252 rc
= check_device(csd
, args
);
1257 rc
= msg_loop(0, csd
, (void *) addr
, alen
, args
);
1269 static int wait_for_connect(int sd
)
1271 struct timeval _tv
= { .tv_sec
= prog_timeout
}, *tv
= NULL
;
1273 int val
= 0, sz
= sizeof(val
);
1282 rc
= select(FD_SETSIZE
, NULL
, &wfd
, NULL
, tv
);
1284 log_error("connect timed out\n");
1286 } else if (rc
< 0) {
1287 log_err_errno("select failed");
1291 if (getsockopt(sd
, SOL_SOCKET
, SO_ERROR
, &val
, (socklen_t
*)&sz
) < 0) {
1292 log_err_errno("getsockopt(SO_ERROR) failed");
1297 log_error("connect failed: %d: %s\n", val
, strerror(val
));
1304 static int connectsock(void *addr
, socklen_t alen
, struct sock_args
*args
)
1309 sd
= socket(args
->version
, args
->type
, args
->protocol
);
1311 log_err_errno("Failed to create socket");
1315 flags
= fcntl(sd
, F_GETFL
);
1316 if ((flags
< 0) || (fcntl(sd
, F_SETFL
, flags
|O_NONBLOCK
) < 0)) {
1317 log_err_errno("Failed to set non-blocking option");
1321 if (set_reuseport(sd
) != 0)
1324 if (args
->dev
&& bind_to_device(sd
, args
->dev
) != 0)
1326 else if (args
->use_setsockopt
&&
1327 set_unicast_if(sd
, args
->ifindex
, args
->version
))
1330 if (args
->has_local_ip
&& bind_socket(sd
, args
))
1333 if (args
->type
!= SOCK_STREAM
)
1336 if (args
->password
&& tcp_md5sig(sd
, addr
, alen
, args
))
1339 if (args
->bind_test_only
)
1342 if (connect(sd
, addr
, alen
) < 0) {
1343 if (errno
!= EINPROGRESS
) {
1344 log_err_errno("Failed to connect to remote host");
1348 rc
= wait_for_connect(sd
);
1360 static int do_client(struct sock_args
*args
)
1362 struct sockaddr_in sin
= {
1363 .sin_family
= AF_INET
,
1365 struct sockaddr_in6 sin6
= {
1366 .sin6_family
= AF_INET6
,
1373 if (!args
->has_remote_ip
&& !args
->has_grp
) {
1374 fprintf(stderr
, "remote IP or multicast group not given\n");
1378 switch (args
->version
) {
1380 sin
.sin_port
= htons(args
->port
);
1382 sin
.sin_addr
= args
->grp
;
1384 sin
.sin_addr
= args
->remote_addr
.in
;
1389 sin6
.sin6_port
= htons(args
->port
);
1390 sin6
.sin6_addr
= args
->remote_addr
.in6
;
1391 sin6
.sin6_scope_id
= args
->scope_id
;
1393 alen
= sizeof(sin6
);
1398 sd
= msock_client(args
);
1400 sd
= connectsock(addr
, alen
, args
);
1405 if (args
->bind_test_only
)
1408 if (args
->type
== SOCK_STREAM
) {
1409 rc
= show_sockstat(sd
, args
);
1414 rc
= msg_loop(1, sd
, addr
, alen
, args
);
1426 ADDR_TYPE_EXPECTED_LOCAL
,
1427 ADDR_TYPE_EXPECTED_REMOTE
,
1428 ADDR_TYPE_MD5_PREFIX
,
1431 static int convert_addr(struct sock_args
*args
, const char *_str
,
1432 enum addr_type atype
)
1434 int pfx_len_max
= args
->version
== AF_INET6
? 128 : 32;
1435 int family
= args
->version
;
1436 char *str
, *dev
, *sep
;
1437 struct in6_addr
*in6
;
1448 case ADDR_TYPE_LOCAL
:
1450 addr
= &args
->local_addr
;
1452 case ADDR_TYPE_REMOTE
:
1454 addr
= &args
->remote_addr
;
1456 case ADDR_TYPE_MCAST
:
1460 case ADDR_TYPE_EXPECTED_LOCAL
:
1461 desc
= "expected local";
1462 addr
= &args
->expected_laddr
;
1464 case ADDR_TYPE_EXPECTED_REMOTE
:
1465 desc
= "expected remote";
1466 addr
= &args
->expected_raddr
;
1468 case ADDR_TYPE_MD5_PREFIX
:
1469 desc
= "md5 prefix";
1470 if (family
== AF_INET
) {
1471 args
->md5_prefix
.v4
.sin_family
= AF_INET
;
1472 addr
= &args
->md5_prefix
.v4
.sin_addr
;
1473 } else if (family
== AF_INET6
) {
1474 args
->md5_prefix
.v6
.sin6_family
= AF_INET6
;
1475 addr
= &args
->md5_prefix
.v6
.sin6_addr
;
1479 sep
= strchr(str
, '/');
1483 if (str_to_uint(sep
, 1, pfx_len_max
,
1484 &args
->prefix_len
) != 0) {
1485 fprintf(stderr
, "Invalid port\n");
1489 args
->prefix_len
= pfx_len_max
;
1493 log_error("unknown address type");
1499 in
= (struct in_addr
*) addr
;
1501 if (inet_pton(AF_INET
, str
, in
) == 0) {
1502 log_error("Invalid %s IP address\n", desc
);
1507 in
->s_addr
= htonl(INADDR_ANY
);
1512 dev
= strchr(str
, '%');
1518 in6
= (struct in6_addr
*) addr
;
1520 if (inet_pton(AF_INET6
, str
, in6
) == 0) {
1521 log_error("Invalid %s IPv6 address\n", desc
);
1529 args
->scope_id
= get_ifidx(dev
);
1530 if (args
->scope_id
< 0) {
1531 log_error("Invalid scope on %s IPv6 address\n",
1540 log_error("Invalid address family\n");
1548 static char *random_msg(int len
)
1550 int i
, n
= 0, olen
= len
+ 1;
1561 i
= snprintf(m
+ n
, olen
- n
, "%.26s",
1562 "abcdefghijklmnopqrstuvwxyz");
1566 i
= snprintf(m
+ n
, olen
- n
, "%.*s", len
,
1567 "abcdefghijklmnopqrstuvwxyz");
1571 #define GETOPT_STR "sr:l:p:t:g:P:DRn:M:m:d:SCi6L:0:1:2:Fbq"
1573 static void print_usage(char *prog
)
1578 " -r addr remote address to connect to (client mode only)\n"
1579 " -p port port to connect to (client mode)/listen on (server mode)\n"
1581 " -s server mode (default: client mode)\n"
1582 " -t timeout seconds (default: none)\n"
1585 " -F Restart server loop\n"
1586 " -6 IPv6 (default is IPv4)\n"
1587 " -P proto protocol for socket: icmp, ospf (default: none)\n"
1588 " -D|R datagram (D) / raw (R) socket (default stream)\n"
1589 " -l addr local address to bind to\n"
1591 " -d dev bind socket to given device name\n"
1592 " -S use setsockopt (IP_UNICAST_IF or IP_MULTICAST_IF)\n"
1593 " to set device binding\n"
1594 " -C use cmsg and IP_PKTINFO to specify device binding\n"
1596 " -L len send random message of given length\n"
1597 " -n num number of times to send message\n"
1599 " -M password use MD5 sum protection\n"
1600 " -m prefix/len prefix and length to use for MD5 key\n"
1601 " -g grp multicast group (e.g., 239.1.1.1)\n"
1602 " -i interactive mode (default is echo and terminate)\n"
1604 " -0 addr Expected local address\n"
1605 " -1 addr Expected remote address\n"
1606 " -2 dev Expected device name (or index) to receive packet\n"
1608 " -b Bind test only.\n"
1609 " -q Be quiet. Run test without printing anything.\n"
1610 , prog
, DEFAULT_PORT
);
1613 int main(int argc
, char *argv
[])
1615 struct sock_args args
= {
1617 .type
= SOCK_STREAM
,
1618 .port
= DEFAULT_PORT
,
1620 struct protoent
*pe
;
1624 /* process inputs */
1625 extern char *optarg
;
1629 * process input args
1632 while ((rc
= getopt(argc
, argv
, GETOPT_STR
)) != -1) {
1641 args
.has_local_ip
= 1;
1642 if (convert_addr(&args
, optarg
, ADDR_TYPE_LOCAL
) < 0)
1646 args
.has_remote_ip
= 1;
1647 if (convert_addr(&args
, optarg
, ADDR_TYPE_REMOTE
) < 0)
1651 if (str_to_uint(optarg
, 1, 65535, &tmp
) != 0) {
1652 fprintf(stderr
, "Invalid port\n");
1655 args
.port
= (unsigned short) tmp
;
1658 if (str_to_uint(optarg
, 0, INT_MAX
,
1659 &prog_timeout
) != 0) {
1660 fprintf(stderr
, "Invalid timeout\n");
1665 args
.type
= SOCK_DGRAM
;
1668 args
.type
= SOCK_RAW
;
1671 args
.protocol
= IPPROTO_RAW
;
1674 pe
= getprotobyname(optarg
);
1676 args
.protocol
= pe
->p_proto
;
1678 if (str_to_uint(optarg
, 0, 0xffff, &tmp
) != 0) {
1679 fprintf(stderr
, "Invalid protocol\n");
1682 args
.protocol
= tmp
;
1686 iter
= atoi(optarg
);
1689 msg
= random_msg(atoi(optarg
));
1692 args
.password
= optarg
;
1695 if (convert_addr(&args
, optarg
, ADDR_TYPE_MD5_PREFIX
) < 0)
1699 args
.use_setsockopt
= 1;
1706 args
.ifindex
= get_ifidx(optarg
);
1707 if (args
.ifindex
< 0) {
1708 fprintf(stderr
, "Invalid device name\n");
1717 if (convert_addr(&args
, optarg
, ADDR_TYPE_MCAST
) < 0)
1719 args
.type
= SOCK_DGRAM
;
1722 args
.version
= AF_INET6
;
1725 args
.bind_test_only
= 1;
1728 args
.has_expected_laddr
= 1;
1729 if (convert_addr(&args
, optarg
,
1730 ADDR_TYPE_EXPECTED_LOCAL
))
1734 args
.has_expected_raddr
= 1;
1735 if (convert_addr(&args
, optarg
,
1736 ADDR_TYPE_EXPECTED_REMOTE
))
1741 if (str_to_uint(optarg
, 0, INT_MAX
, &tmp
) == 0) {
1742 args
.expected_ifindex
= (int)tmp
;
1744 args
.expected_ifindex
= get_ifidx(optarg
);
1745 if (args
.expected_ifindex
< 0) {
1747 "Invalid expected device\n");
1756 print_usage(argv
[0]);
1761 if (args
.password
&&
1762 ((!args
.has_remote_ip
&& !args
.prefix_len
) || args
.type
!= SOCK_STREAM
)) {
1763 log_error("MD5 passwords apply to TCP only and require a remote ip for the password\n");
1767 if (args
.prefix_len
&& !args
.password
) {
1768 log_error("Prefix range for MD5 protection specified without a password\n");
1772 if ((args
.use_setsockopt
|| args
.use_cmsg
) && !args
.ifindex
) {
1773 fprintf(stderr
, "Device binding not specified\n");
1776 if (args
.use_setsockopt
|| args
.use_cmsg
)
1780 fprintf(stderr
, "Invalid number of messages to send\n");
1784 if (args
.type
== SOCK_STREAM
&& !args
.protocol
)
1785 args
.protocol
= IPPROTO_TCP
;
1786 if (args
.type
== SOCK_DGRAM
&& !args
.protocol
)
1787 args
.protocol
= IPPROTO_UDP
;
1789 if ((args
.type
== SOCK_STREAM
|| args
.type
== SOCK_DGRAM
) &&
1791 fprintf(stderr
, "Invalid port number\n");
1795 if (!server_mode
&& !args
.has_grp
&&
1796 !args
.has_remote_ip
&& !args
.has_local_ip
) {
1798 "Local (server mode) or remote IP (client IP) required\n");
1809 rc
= do_server(&args
);
1814 return do_client(&args
);