h_canutils.c revision 1.3 1 1.3 kre /* $NetBSD: h_canutils.c,v 1.3 2017/05/28 13:55:07 kre 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.3 kre __RCSID("$NetBSD: h_canutils.c,v 1.3 2017/05/28 13:55:07 kre 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.2 bouyer strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
74 1.2 bouyer
75 1.2 bouyer if ((rv = rump_sys_ioctl(s, SIOCIFCREATE, &ifr)) < 0) {
76 1.2 bouyer atf_tc_fail_errno("if config create");
77 1.2 bouyer }
78 1.2 bouyer
79 1.2 bouyer memset(&ifr, 0, sizeof(ifr));
80 1.2 bouyer strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
81 1.2 bouyer
82 1.2 bouyer if ((rv = rump_sys_ioctl(s, SIOCGIFFLAGS, &ifr)) < 0) {
83 1.2 bouyer atf_tc_fail_errno("if config get flags");
84 1.2 bouyer }
85 1.2 bouyer
86 1.2 bouyer ifr.ifr_flags |= IFF_UP;
87 1.2 bouyer if ((rv = rump_sys_ioctl(s, SIOCSIFFLAGS, &ifr)) < 0) {
88 1.2 bouyer atf_tc_fail_errno("if config set flags");
89 1.2 bouyer }
90 1.2 bouyer }
91 1.2 bouyer
92 1.2 bouyer int
93 1.2 bouyer can_recvfrom(int s, struct can_frame *cf, int *len, struct sockaddr_can *sa)
94 1.2 bouyer {
95 1.2 bouyer socklen_t salen;
96 1.2 bouyer fd_set rfds;
97 1.2 bouyer struct timeval tmout;
98 1.2 bouyer int rv;
99 1.2 bouyer
100 1.2 bouyer memset(cf, 0, sizeof(struct can_frame));
101 1.2 bouyer FD_ZERO(&rfds);
102 1.2 bouyer FD_SET(s, &rfds);
103 1.2 bouyer /* wait 1 second for the message (in some tests we expect no message) */
104 1.2 bouyer tmout.tv_sec = 1;
105 1.2 bouyer tmout.tv_usec = 0;
106 1.2 bouyer rv = rump_sys_select(s + 1, &rfds, NULL, NULL, &tmout);
107 1.2 bouyer switch(rv) {
108 1.2 bouyer case -1:
109 1.2 bouyer atf_tc_fail_errno("select");
110 1.2 bouyer /* NOTREACHED */
111 1.2 bouyer case 0:
112 1.2 bouyer /* timeout */
113 1.2 bouyer errno = EWOULDBLOCK;
114 1.2 bouyer return -1;
115 1.2 bouyer default: break;
116 1.2 bouyer }
117 1.2 bouyer ATF_CHECK_MSG(FD_ISSET(s, &rfds), "select returns but s not in set");
118 1.2 bouyer salen = sizeof(struct sockaddr_can);
119 1.2 bouyer if (( *len = rump_sys_recvfrom(s, cf, sizeof(struct can_frame),
120 1.2 bouyer 0, (struct sockaddr *)sa, &salen)) < 0) {
121 1.2 bouyer atf_tc_fail_errno("recvfrom");
122 1.2 bouyer }
123 1.2 bouyer ATF_CHECK_MSG(rv > 0, "short read on socket");
124 1.2 bouyer ATF_CHECK_MSG(sa->can_family == AF_CAN,
125 1.2 bouyer "recvfrom provided wrong %d family", sa->can_family);
126 1.2 bouyer ATF_CHECK_MSG(salen == sizeof(struct sockaddr_can),
127 1.3 kre "recvfrom provided wrong size %d (!= %zu)", salen, sizeof(sa));
128 1.2 bouyer return 0;
129 1.2 bouyer }
130 1.2 bouyer
131 1.2 bouyer int
132 1.2 bouyer can_read(int s, struct can_frame *cf, int *len)
133 1.2 bouyer {
134 1.2 bouyer fd_set rfds;
135 1.2 bouyer struct timeval tmout;
136 1.2 bouyer int rv;
137 1.2 bouyer
138 1.2 bouyer memset(cf, 0, sizeof(struct can_frame));
139 1.2 bouyer FD_ZERO(&rfds);
140 1.2 bouyer FD_SET(s, &rfds);
141 1.2 bouyer /* wait 1 second for the message (in some tests we expect no message) */
142 1.2 bouyer tmout.tv_sec = 1;
143 1.2 bouyer tmout.tv_usec = 0;
144 1.2 bouyer rv = rump_sys_select(s + 1, &rfds, NULL, NULL, &tmout);
145 1.2 bouyer switch(rv) {
146 1.2 bouyer case -1:
147 1.2 bouyer atf_tc_fail_errno("select");
148 1.2 bouyer /* NOTREACHED */
149 1.2 bouyer case 0:
150 1.2 bouyer /* timeout */
151 1.2 bouyer errno = EWOULDBLOCK;
152 1.2 bouyer return -1;
153 1.2 bouyer default: break;
154 1.2 bouyer }
155 1.2 bouyer ATF_CHECK_MSG(FD_ISSET(s, &rfds), "select returns but s not in set");
156 1.2 bouyer if (( *len = rump_sys_read(s, cf, sizeof(struct can_frame))) < 0) {
157 1.2 bouyer atf_tc_fail_errno("read");
158 1.2 bouyer }
159 1.2 bouyer ATF_CHECK_MSG(rv > 0, "short read on socket");
160 1.2 bouyer return 0;
161 1.2 bouyer }
162 1.2 bouyer
163 1.2 bouyer int
164 1.2 bouyer can_bind(int s, const char *ifname)
165 1.2 bouyer {
166 1.2 bouyer struct ifreq ifr;
167 1.2 bouyer struct sockaddr_can sa;
168 1.2 bouyer
169 1.2 bouyer strcpy(ifr.ifr_name, ifname );
170 1.2 bouyer if (rump_sys_ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
171 1.2 bouyer atf_tc_fail_errno("SIOCGIFINDEX");
172 1.2 bouyer }
173 1.2 bouyer ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
174 1.2 bouyer ifname, ifr.ifr_ifindex);
175 1.2 bouyer
176 1.2 bouyer sa.can_family = AF_CAN;
177 1.2 bouyer sa.can_ifindex = ifr.ifr_ifindex;
178 1.2 bouyer
179 1.2 bouyer if (rump_sys_bind(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
180 1.2 bouyer atf_tc_fail_errno("bind");
181 1.2 bouyer }
182 1.2 bouyer return ifr.ifr_ifindex;
183 1.2 bouyer }
184 1.2 bouyer
185 1.2 bouyer int
186 1.2 bouyer can_socket_with_own(void)
187 1.2 bouyer {
188 1.2 bouyer int s;
189 1.2 bouyer int v;
190 1.2 bouyer
191 1.2 bouyer if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
192 1.2 bouyer atf_tc_fail_errno("CAN socket");
193 1.2 bouyer }
194 1.2 bouyer v = 1;
195 1.2 bouyer if (rump_sys_setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
196 1.2 bouyer &v, sizeof(v)) < 0) {
197 1.2 bouyer atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
198 1.2 bouyer }
199 1.2 bouyer return s;
200 1.2 bouyer }
201