1 1.4 mrg /* $NetBSD: h_canutils.c,v 1.4 2019/10/13 07:46:16 mrg Exp $ */ 2 1.2 bouyer 3 1.2 bouyer /*- 4 1.2 bouyer * Copyright (c) 2017 The NetBSD Foundation, Inc. 5 1.2 bouyer * All rights reserved. 6 1.2 bouyer * 7 1.2 bouyer * This code is derived from software contributed to The NetBSD Foundation 8 1.2 bouyer * by Manuel Bouyer 9 1.2 bouyer * 10 1.2 bouyer * Redistribution and use in source and binary forms, with or without 11 1.2 bouyer * modification, are permitted provided that the following conditions 12 1.2 bouyer * are met: 13 1.2 bouyer * 1. Redistributions of source code must retain the above copyright 14 1.2 bouyer * notice, this list of conditions and the following disclaimer. 15 1.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright 16 1.2 bouyer * notice, this list of conditions and the following disclaimer in the 17 1.2 bouyer * documentation and/or other materials provided with the distribution. 18 1.2 bouyer * 19 1.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 20 1.2 bouyer * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 21 1.2 bouyer * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 22 1.2 bouyer * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 1.2 bouyer * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 24 1.2 bouyer * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 1.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 26 1.2 bouyer * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 1.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 28 1.2 bouyer * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 29 1.2 bouyer * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 30 1.2 bouyer * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 1.2 bouyer */ 32 1.2 bouyer 33 1.2 bouyer #include <sys/cdefs.h> 34 1.2 bouyer #ifndef lint 35 1.4 mrg __RCSID("$NetBSD: h_canutils.c,v 1.4 2019/10/13 07:46:16 mrg Exp $"); 36 1.2 bouyer #endif /* not lint */ 37 1.2 bouyer 38 1.2 bouyer #include <sys/types.h> 39 1.2 bouyer #include <sys/resource.h> 40 1.2 bouyer #include <sys/wait.h> 41 1.2 bouyer #include <sys/sockio.h> 42 1.2 bouyer #include <sys/param.h> 43 1.2 bouyer 44 1.2 bouyer #include <atf-c.h> 45 1.2 bouyer #include <assert.h> 46 1.2 bouyer #include <fcntl.h> 47 1.2 bouyer #include <stdio.h> 48 1.2 bouyer #include <stdlib.h> 49 1.2 bouyer #include <string.h> 50 1.2 bouyer #include <unistd.h> 51 1.2 bouyer 52 1.2 bouyer #include <net/if.h> 53 1.2 bouyer #include <netcan/can.h> 54 1.2 bouyer 55 1.2 bouyer #include <rump/rump.h> 56 1.2 bouyer #include <rump/rump_syscalls.h> 57 1.2 bouyer 58 1.2 bouyer #include "h_macros.h" 59 1.2 bouyer #include "h_canutils.h" 60 1.2 bouyer 61 1.2 bouyer void 62 1.2 bouyer cancfg_rump_createif(const char *ifname) 63 1.2 bouyer { 64 1.2 bouyer int s, rv; 65 1.2 bouyer struct ifreq ifr; 66 1.2 bouyer 67 1.2 bouyer s = -1; 68 1.2 bouyer if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) { 69 1.2 bouyer atf_tc_fail_errno("if config socket"); 70 1.2 bouyer } 71 1.2 bouyer 72 1.2 bouyer memset(&ifr, 0, sizeof(ifr)); 73 1.4 mrg strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1); 74 1.4 mrg ifr.ifr_name[IFNAMSIZ - 1] = '\0'; 75 1.2 bouyer 76 1.2 bouyer if ((rv = rump_sys_ioctl(s, SIOCIFCREATE, &ifr)) < 0) { 77 1.2 bouyer atf_tc_fail_errno("if config create"); 78 1.2 bouyer } 79 1.2 bouyer 80 1.2 bouyer memset(&ifr, 0, sizeof(ifr)); 81 1.4 mrg strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1); 82 1.4 mrg ifr.ifr_name[IFNAMSIZ - 1] = '\0'; 83 1.2 bouyer 84 1.2 bouyer if ((rv = rump_sys_ioctl(s, SIOCGIFFLAGS, &ifr)) < 0) { 85 1.2 bouyer atf_tc_fail_errno("if config get flags"); 86 1.2 bouyer } 87 1.2 bouyer 88 1.2 bouyer ifr.ifr_flags |= IFF_UP; 89 1.2 bouyer if ((rv = rump_sys_ioctl(s, SIOCSIFFLAGS, &ifr)) < 0) { 90 1.2 bouyer atf_tc_fail_errno("if config set flags"); 91 1.2 bouyer } 92 1.2 bouyer } 93 1.2 bouyer 94 1.2 bouyer int 95 1.2 bouyer can_recvfrom(int s, struct can_frame *cf, int *len, struct sockaddr_can *sa) 96 1.2 bouyer { 97 1.2 bouyer socklen_t salen; 98 1.2 bouyer fd_set rfds; 99 1.2 bouyer struct timeval tmout; 100 1.2 bouyer int rv; 101 1.2 bouyer 102 1.2 bouyer memset(cf, 0, sizeof(struct can_frame)); 103 1.2 bouyer FD_ZERO(&rfds); 104 1.2 bouyer FD_SET(s, &rfds); 105 1.2 bouyer /* wait 1 second for the message (in some tests we expect no message) */ 106 1.2 bouyer tmout.tv_sec = 1; 107 1.2 bouyer tmout.tv_usec = 0; 108 1.2 bouyer rv = rump_sys_select(s + 1, &rfds, NULL, NULL, &tmout); 109 1.2 bouyer switch(rv) { 110 1.2 bouyer case -1: 111 1.2 bouyer atf_tc_fail_errno("select"); 112 1.2 bouyer /* NOTREACHED */ 113 1.2 bouyer case 0: 114 1.2 bouyer /* timeout */ 115 1.2 bouyer errno = EWOULDBLOCK; 116 1.2 bouyer return -1; 117 1.2 bouyer default: break; 118 1.2 bouyer } 119 1.2 bouyer ATF_CHECK_MSG(FD_ISSET(s, &rfds), "select returns but s not in set"); 120 1.2 bouyer salen = sizeof(struct sockaddr_can); 121 1.2 bouyer if (( *len = rump_sys_recvfrom(s, cf, sizeof(struct can_frame), 122 1.2 bouyer 0, (struct sockaddr *)sa, &salen)) < 0) { 123 1.2 bouyer atf_tc_fail_errno("recvfrom"); 124 1.2 bouyer } 125 1.2 bouyer ATF_CHECK_MSG(rv > 0, "short read on socket"); 126 1.2 bouyer ATF_CHECK_MSG(sa->can_family == AF_CAN, 127 1.2 bouyer "recvfrom provided wrong %d family", sa->can_family); 128 1.2 bouyer ATF_CHECK_MSG(salen == sizeof(struct sockaddr_can), 129 1.3 kre "recvfrom provided wrong size %d (!= %zu)", salen, sizeof(sa)); 130 1.2 bouyer return 0; 131 1.2 bouyer } 132 1.2 bouyer 133 1.2 bouyer int 134 1.2 bouyer can_read(int s, struct can_frame *cf, int *len) 135 1.2 bouyer { 136 1.2 bouyer fd_set rfds; 137 1.2 bouyer struct timeval tmout; 138 1.2 bouyer int rv; 139 1.2 bouyer 140 1.2 bouyer memset(cf, 0, sizeof(struct can_frame)); 141 1.2 bouyer FD_ZERO(&rfds); 142 1.2 bouyer FD_SET(s, &rfds); 143 1.2 bouyer /* wait 1 second for the message (in some tests we expect no message) */ 144 1.2 bouyer tmout.tv_sec = 1; 145 1.2 bouyer tmout.tv_usec = 0; 146 1.2 bouyer rv = rump_sys_select(s + 1, &rfds, NULL, NULL, &tmout); 147 1.2 bouyer switch(rv) { 148 1.2 bouyer case -1: 149 1.2 bouyer atf_tc_fail_errno("select"); 150 1.2 bouyer /* NOTREACHED */ 151 1.2 bouyer case 0: 152 1.2 bouyer /* timeout */ 153 1.2 bouyer errno = EWOULDBLOCK; 154 1.2 bouyer return -1; 155 1.2 bouyer default: break; 156 1.2 bouyer } 157 1.2 bouyer ATF_CHECK_MSG(FD_ISSET(s, &rfds), "select returns but s not in set"); 158 1.2 bouyer if (( *len = rump_sys_read(s, cf, sizeof(struct can_frame))) < 0) { 159 1.2 bouyer atf_tc_fail_errno("read"); 160 1.2 bouyer } 161 1.2 bouyer ATF_CHECK_MSG(rv > 0, "short read on socket"); 162 1.2 bouyer return 0; 163 1.2 bouyer } 164 1.2 bouyer 165 1.2 bouyer int 166 1.2 bouyer can_bind(int s, const char *ifname) 167 1.2 bouyer { 168 1.2 bouyer struct ifreq ifr; 169 1.2 bouyer struct sockaddr_can sa; 170 1.2 bouyer 171 1.2 bouyer strcpy(ifr.ifr_name, ifname ); 172 1.2 bouyer if (rump_sys_ioctl(s, SIOCGIFINDEX, &ifr) < 0) { 173 1.2 bouyer atf_tc_fail_errno("SIOCGIFINDEX"); 174 1.2 bouyer } 175 1.2 bouyer ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)", 176 1.2 bouyer ifname, ifr.ifr_ifindex); 177 1.2 bouyer 178 1.2 bouyer sa.can_family = AF_CAN; 179 1.2 bouyer sa.can_ifindex = ifr.ifr_ifindex; 180 1.2 bouyer 181 1.2 bouyer if (rump_sys_bind(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) { 182 1.2 bouyer atf_tc_fail_errno("bind"); 183 1.2 bouyer } 184 1.2 bouyer return ifr.ifr_ifindex; 185 1.2 bouyer } 186 1.2 bouyer 187 1.2 bouyer int 188 1.2 bouyer can_socket_with_own(void) 189 1.2 bouyer { 190 1.2 bouyer int s; 191 1.2 bouyer int v; 192 1.2 bouyer 193 1.2 bouyer if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) { 194 1.2 bouyer atf_tc_fail_errno("CAN socket"); 195 1.2 bouyer } 196 1.2 bouyer v = 1; 197 1.2 bouyer if (rump_sys_setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, 198 1.2 bouyer &v, sizeof(v)) < 0) { 199 1.2 bouyer atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)"); 200 1.2 bouyer } 201 1.2 bouyer return s; 202 1.2 bouyer } 203