Home | History | Annotate | Line # | Download | only in can
t_can.c revision 1.1.2.2
      1  1.1.2.2  bouyer /*	$NetBSD: t_can.c,v 1.1.2.2 2017/01/16 18:04:27 bouyer Exp $	*/
      2  1.1.2.1  bouyer 
      3  1.1.2.1  bouyer /*-
      4  1.1.2.1  bouyer  * Copyright (c) 2017 The NetBSD Foundation, Inc.
      5  1.1.2.1  bouyer  * All rights reserved.
      6  1.1.2.1  bouyer  *
      7  1.1.2.1  bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.2.1  bouyer  * by Manuel Bouyer
      9  1.1.2.1  bouyer  *
     10  1.1.2.1  bouyer  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.1  bouyer  * modification, are permitted provided that the following conditions
     12  1.1.2.1  bouyer  * are met:
     13  1.1.2.1  bouyer  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.1  bouyer  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.1  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.1  bouyer  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.1  bouyer  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.1  bouyer  *
     19  1.1.2.1  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     20  1.1.2.1  bouyer  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     21  1.1.2.1  bouyer  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     22  1.1.2.1  bouyer  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.1.2.1  bouyer  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     24  1.1.2.1  bouyer  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.1.2.1  bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     26  1.1.2.1  bouyer  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1.2.1  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     28  1.1.2.1  bouyer  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     29  1.1.2.1  bouyer  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     30  1.1.2.1  bouyer  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1.2.1  bouyer  */
     32  1.1.2.1  bouyer 
     33  1.1.2.1  bouyer #include <sys/cdefs.h>
     34  1.1.2.1  bouyer #ifndef lint
     35  1.1.2.2  bouyer __RCSID("$NetBSD: t_can.c,v 1.1.2.2 2017/01/16 18:04:27 bouyer Exp $");
     36  1.1.2.1  bouyer #endif /* not lint */
     37  1.1.2.1  bouyer 
     38  1.1.2.1  bouyer #include <sys/types.h>
     39  1.1.2.1  bouyer #include <sys/resource.h>
     40  1.1.2.1  bouyer #include <sys/sysctl.h>
     41  1.1.2.1  bouyer #include <sys/wait.h>
     42  1.1.2.1  bouyer #include <sys/sockio.h>
     43  1.1.2.2  bouyer #include <sys/param.h>
     44  1.1.2.1  bouyer 
     45  1.1.2.1  bouyer #include <atf-c.h>
     46  1.1.2.1  bouyer #include <assert.h>
     47  1.1.2.1  bouyer #include <fcntl.h>
     48  1.1.2.1  bouyer #include <stdio.h>
     49  1.1.2.1  bouyer #include <stdlib.h>
     50  1.1.2.1  bouyer #include <string.h>
     51  1.1.2.1  bouyer #include <unistd.h>
     52  1.1.2.1  bouyer #include <signal.h>
     53  1.1.2.1  bouyer 
     54  1.1.2.1  bouyer #include <net/if.h>
     55  1.1.2.1  bouyer #include <netcan/can.h>
     56  1.1.2.1  bouyer 
     57  1.1.2.1  bouyer #include <rump/rump.h>
     58  1.1.2.1  bouyer #include <rump/rump_syscalls.h>
     59  1.1.2.1  bouyer 
     60  1.1.2.1  bouyer #include "h_macros.h"
     61  1.1.2.1  bouyer 
     62  1.1.2.1  bouyer static void
     63  1.1.2.1  bouyer cancfg_rump_createif(const char *ifname)
     64  1.1.2.1  bouyer {
     65  1.1.2.1  bouyer 	int s, rv;
     66  1.1.2.1  bouyer 	struct ifreq ifr;
     67  1.1.2.1  bouyer 
     68  1.1.2.1  bouyer 	s = -1;
     69  1.1.2.1  bouyer 	if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
     70  1.1.2.1  bouyer 		atf_tc_fail_errno("if config socket");
     71  1.1.2.1  bouyer 	}
     72  1.1.2.1  bouyer 
     73  1.1.2.1  bouyer 	memset(&ifr, 0, sizeof(ifr));
     74  1.1.2.1  bouyer 	strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
     75  1.1.2.1  bouyer 
     76  1.1.2.1  bouyer 	if ((rv = rump_sys_ioctl(s, SIOCIFCREATE, &ifr)) < 0) {
     77  1.1.2.1  bouyer 		atf_tc_fail_errno("if config create");
     78  1.1.2.1  bouyer 	}
     79  1.1.2.1  bouyer 
     80  1.1.2.1  bouyer 	memset(&ifr, 0, sizeof(ifr));
     81  1.1.2.1  bouyer 	strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
     82  1.1.2.1  bouyer 
     83  1.1.2.1  bouyer 	if ((rv = rump_sys_ioctl(s, SIOCGIFFLAGS, &ifr)) < 0) {
     84  1.1.2.1  bouyer 		atf_tc_fail_errno("if config get flags");
     85  1.1.2.1  bouyer 	}
     86  1.1.2.1  bouyer 
     87  1.1.2.1  bouyer 	ifr.ifr_flags |= IFF_UP;
     88  1.1.2.1  bouyer 	if ((rv = rump_sys_ioctl(s, SIOCSIFFLAGS, &ifr)) < 0) {
     89  1.1.2.1  bouyer 		atf_tc_fail_errno("if config set flags");
     90  1.1.2.1  bouyer 	}
     91  1.1.2.1  bouyer }
     92  1.1.2.1  bouyer 
     93  1.1.2.1  bouyer ATF_TC(canlocreate);
     94  1.1.2.1  bouyer ATF_TC_HEAD(canlocreate, tc)
     95  1.1.2.1  bouyer {
     96  1.1.2.1  bouyer 
     97  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "descr", "check CAN loopback create/destroy");
     98  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "timeout", "5");
     99  1.1.2.1  bouyer }
    100  1.1.2.1  bouyer 
    101  1.1.2.1  bouyer ATF_TC_BODY(canlocreate, tc)
    102  1.1.2.1  bouyer {
    103  1.1.2.1  bouyer 	const char ifname[] = "canlo0";
    104  1.1.2.1  bouyer 	int s, rv;
    105  1.1.2.1  bouyer 	struct ifreq ifr;
    106  1.1.2.1  bouyer 
    107  1.1.2.1  bouyer 	rump_init();
    108  1.1.2.1  bouyer 	cancfg_rump_createif(ifname);
    109  1.1.2.1  bouyer 
    110  1.1.2.1  bouyer 	s = -1;
    111  1.1.2.1  bouyer 	if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    112  1.1.2.1  bouyer 		atf_tc_fail_errno("if config socket(2)");
    113  1.1.2.1  bouyer 	}
    114  1.1.2.1  bouyer 
    115  1.1.2.1  bouyer 	memset(&ifr, 0, sizeof(ifr));
    116  1.1.2.1  bouyer 	strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
    117  1.1.2.1  bouyer 
    118  1.1.2.1  bouyer 	if ((rv = rump_sys_ioctl(s, SIOCIFDESTROY, &ifr)) < 0) {
    119  1.1.2.1  bouyer 		atf_tc_fail_errno("if config destroy");
    120  1.1.2.1  bouyer 	}
    121  1.1.2.1  bouyer }
    122  1.1.2.1  bouyer 
    123  1.1.2.2  bouyer ATF_TC(cannoown);
    124  1.1.2.2  bouyer ATF_TC_HEAD(cannoown, tc)
    125  1.1.2.2  bouyer {
    126  1.1.2.2  bouyer 
    127  1.1.2.2  bouyer 	atf_tc_set_md_var(tc, "descr", "check that CAN sockets don't gets its own message");
    128  1.1.2.2  bouyer 	atf_tc_set_md_var(tc, "timeout", "5");
    129  1.1.2.2  bouyer }
    130  1.1.2.2  bouyer 
    131  1.1.2.2  bouyer ATF_TC_BODY(cannoown, tc)
    132  1.1.2.2  bouyer {
    133  1.1.2.2  bouyer 	const char ifname[] = "canlo0";
    134  1.1.2.2  bouyer 	int s, rv, v, vlen;
    135  1.1.2.2  bouyer 	struct sockaddr_can sa;
    136  1.1.2.2  bouyer 	int salen;
    137  1.1.2.2  bouyer 	struct ifreq ifr;
    138  1.1.2.2  bouyer 	struct can_frame cf_send, cf_receive;
    139  1.1.2.2  bouyer 	fd_set rfds;
    140  1.1.2.2  bouyer 	struct timeval tmout;
    141  1.1.2.2  bouyer 
    142  1.1.2.2  bouyer 	rump_init();
    143  1.1.2.2  bouyer 	cancfg_rump_createif(ifname);
    144  1.1.2.2  bouyer 
    145  1.1.2.2  bouyer 	s = -1;
    146  1.1.2.2  bouyer 	if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    147  1.1.2.2  bouyer 		atf_tc_fail_errno("CAN socket");
    148  1.1.2.2  bouyer 	}
    149  1.1.2.2  bouyer 
    150  1.1.2.2  bouyer 	strcpy(ifr.ifr_name, ifname );
    151  1.1.2.2  bouyer 	if (rump_sys_ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
    152  1.1.2.2  bouyer 		atf_tc_fail_errno("SIOCGIFINDEX");
    153  1.1.2.2  bouyer 	}
    154  1.1.2.2  bouyer 	ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
    155  1.1.2.2  bouyer 	    ifname, ifr.ifr_ifindex);
    156  1.1.2.2  bouyer 
    157  1.1.2.2  bouyer 	sa.can_family = AF_CAN;
    158  1.1.2.2  bouyer 	sa.can_ifindex = ifr.ifr_ifindex;
    159  1.1.2.2  bouyer 
    160  1.1.2.2  bouyer 	if (rump_sys_bind(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
    161  1.1.2.2  bouyer 		atf_tc_fail_errno("bind");
    162  1.1.2.2  bouyer 	}
    163  1.1.2.2  bouyer 
    164  1.1.2.2  bouyer 	/* check sockopt CAN_RAW_LOOPBACK */
    165  1.1.2.2  bouyer 	vlen = sizeof(v);
    166  1.1.2.2  bouyer 	if (rump_sys_getsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
    167  1.1.2.2  bouyer 	    &v, &vlen) < 0) {
    168  1.1.2.2  bouyer 		atf_tc_fail_errno("getsockopt(CAN_RAW_LOOPBACK)");
    169  1.1.2.2  bouyer 	}
    170  1.1.2.2  bouyer 	ATF_CHECK_MSG(vlen == sizeof(v), "getsockopt(CAN_RAW_LOOPBACK) returns wrong len %d", vlen);
    171  1.1.2.2  bouyer 	ATF_CHECK_MSG(v == 1, "CAN_RAW_LOOPBACK is not on by default");
    172  1.1.2.2  bouyer 
    173  1.1.2.2  bouyer 	/* check sockopt CAN_RAW_RECV_OWN_MSGS, and set it */
    174  1.1.2.2  bouyer 	vlen = sizeof(v);
    175  1.1.2.2  bouyer 	if (rump_sys_getsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
    176  1.1.2.2  bouyer 	    &v, &vlen) < 0) {
    177  1.1.2.2  bouyer 		atf_tc_fail_errno("getsockopt(CAN_RAW_RECV_OWN_MSGS)");
    178  1.1.2.2  bouyer 	}
    179  1.1.2.2  bouyer 	ATF_CHECK_MSG(vlen == sizeof(v), "getsockopt(CAN_RAW_RECV_OWN_MSGS) returns wrong len %d", vlen);
    180  1.1.2.2  bouyer 	ATF_CHECK_MSG(v == 0, "CAN_RAW_RECV_OWN_MSGS is not off by default");
    181  1.1.2.2  bouyer 
    182  1.1.2.2  bouyer 	/*
    183  1.1.2.2  bouyer 	 * send a single byte message, but make sure remaining payload is
    184  1.1.2.2  bouyer 	 * not 0.
    185  1.1.2.2  bouyer 	 */
    186  1.1.2.2  bouyer 
    187  1.1.2.2  bouyer 	memset(&cf_send, 0, sizeof(cf_send));
    188  1.1.2.2  bouyer 	cf_send.can_id = 1;
    189  1.1.2.2  bouyer 	cf_send.can_dlc = 1;
    190  1.1.2.2  bouyer 	cf_send.data[0] = 0xde;
    191  1.1.2.2  bouyer 	cf_send.data[1] = 0xad;
    192  1.1.2.2  bouyer 	cf_send.data[2] = 0xbe;
    193  1.1.2.2  bouyer 	cf_send.data[3] = 0xef;
    194  1.1.2.2  bouyer 
    195  1.1.2.2  bouyer 	if (rump_sys_write(s, &cf_send, sizeof(cf_send) - 7) < 0) {
    196  1.1.2.2  bouyer 		atf_tc_fail_errno("write");
    197  1.1.2.2  bouyer 	}
    198  1.1.2.2  bouyer 
    199  1.1.2.2  bouyer 	/* now try to read */
    200  1.1.2.2  bouyer 
    201  1.1.2.2  bouyer 	memset(&cf_receive, 0, sizeof(cf_receive));
    202  1.1.2.2  bouyer 	FD_ZERO(&rfds);
    203  1.1.2.2  bouyer 	FD_SET(s, &rfds);
    204  1.1.2.2  bouyer 	/* we should receive no message; wait for 2 seconds */
    205  1.1.2.2  bouyer 	tmout.tv_sec = 2;
    206  1.1.2.2  bouyer 	tmout.tv_usec = 0;
    207  1.1.2.2  bouyer 	rv = rump_sys_select(s + 1, &rfds, NULL, NULL, &tmout);
    208  1.1.2.2  bouyer 	switch(rv) {
    209  1.1.2.2  bouyer 	case -1:
    210  1.1.2.2  bouyer 		atf_tc_fail_errno("select");
    211  1.1.2.2  bouyer 		break;
    212  1.1.2.2  bouyer 	case 0:
    213  1.1.2.2  bouyer 		/* timeout: expected case */
    214  1.1.2.2  bouyer 		return;
    215  1.1.2.2  bouyer 	default: break;
    216  1.1.2.2  bouyer 	}
    217  1.1.2.2  bouyer 	salen = sizeof(sa);
    218  1.1.2.2  bouyer 	ATF_CHECK_MSG(FD_ISSET(s, &rfds), "select returns but s not in set");
    219  1.1.2.2  bouyer 	if (( rv = rump_sys_recvfrom(s, &cf_receive, sizeof(cf_receive),
    220  1.1.2.2  bouyer 	    0, (struct sockaddr *)&sa, &salen)) < 0) {
    221  1.1.2.2  bouyer 		atf_tc_fail_errno("recvfrom");
    222  1.1.2.2  bouyer 	}
    223  1.1.2.2  bouyer 
    224  1.1.2.2  bouyer 	ATF_CHECK_MSG(rv > 0, "short read on socket");
    225  1.1.2.2  bouyer 
    226  1.1.2.2  bouyer 	ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive, sizeof(cf_send)) == 0,
    227  1.1.2.2  bouyer 	    "recvfrom packet is not what we sent");
    228  1.1.2.2  bouyer 	ATF_CHECK_MSG(sa.can_family == AF_CAN,
    229  1.1.2.2  bouyer 	    "recvfrom provided wrong %d family", sa.can_family);
    230  1.1.2.2  bouyer 	ATF_CHECK_MSG(salen == sizeof(sa),
    231  1.1.2.2  bouyer 	    "recvfrom provided wrong size %d (!= %d)", salen, sizeof(sa));
    232  1.1.2.2  bouyer 	ATF_CHECK_MSG(sa.can_ifindex == ifr.ifr_ifindex,
    233  1.1.2.2  bouyer 	   "recvfrom provided wrong ifindex %d (!= %d)",
    234  1.1.2.2  bouyer 	    sa.can_ifindex, ifr.ifr_ifindex);
    235  1.1.2.2  bouyer 	atf_tc_fail("we got our own message");
    236  1.1.2.2  bouyer }
    237  1.1.2.2  bouyer 
    238  1.1.2.1  bouyer ATF_TC(canwritelo);
    239  1.1.2.1  bouyer ATF_TC_HEAD(canwritelo, tc)
    240  1.1.2.1  bouyer {
    241  1.1.2.1  bouyer 
    242  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "descr", "check that CAN sockets gets its own message via write");
    243  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "timeout", "5");
    244  1.1.2.1  bouyer }
    245  1.1.2.1  bouyer 
    246  1.1.2.1  bouyer ATF_TC_BODY(canwritelo, tc)
    247  1.1.2.1  bouyer {
    248  1.1.2.1  bouyer 	const char ifname[] = "canlo0";
    249  1.1.2.2  bouyer 	int s, rv, v, vlen;
    250  1.1.2.1  bouyer 	struct sockaddr_can sa;
    251  1.1.2.1  bouyer 	struct ifreq ifr;
    252  1.1.2.1  bouyer 	struct can_frame cf_send, cf_receive;
    253  1.1.2.1  bouyer 
    254  1.1.2.1  bouyer 	rump_init();
    255  1.1.2.1  bouyer 	cancfg_rump_createif(ifname);
    256  1.1.2.1  bouyer 
    257  1.1.2.1  bouyer 	s = -1;
    258  1.1.2.1  bouyer 	if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    259  1.1.2.1  bouyer 		atf_tc_fail_errno("CAN socket");
    260  1.1.2.1  bouyer 	}
    261  1.1.2.1  bouyer 
    262  1.1.2.1  bouyer 	strcpy(ifr.ifr_name, ifname );
    263  1.1.2.2  bouyer 	if (rump_sys_ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
    264  1.1.2.1  bouyer 		atf_tc_fail_errno("SIOCGIFINDEX");
    265  1.1.2.1  bouyer 	}
    266  1.1.2.1  bouyer 	ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
    267  1.1.2.1  bouyer 	    ifname, ifr.ifr_ifindex);
    268  1.1.2.1  bouyer 
    269  1.1.2.1  bouyer 	sa.can_family = AF_CAN;
    270  1.1.2.1  bouyer 	sa.can_ifindex = ifr.ifr_ifindex;
    271  1.1.2.1  bouyer 
    272  1.1.2.2  bouyer 	if (rump_sys_bind(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
    273  1.1.2.1  bouyer 		atf_tc_fail_errno("bind");
    274  1.1.2.1  bouyer 	}
    275  1.1.2.1  bouyer 
    276  1.1.2.2  bouyer 	/* check sockopt CAN_RAW_LOOPBACK */
    277  1.1.2.2  bouyer 	vlen = sizeof(v);
    278  1.1.2.2  bouyer 	if (rump_sys_getsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
    279  1.1.2.2  bouyer 	    &v, &vlen) < 0) {
    280  1.1.2.2  bouyer 		atf_tc_fail_errno("getsockopt(CAN_RAW_LOOPBACK)");
    281  1.1.2.2  bouyer 	}
    282  1.1.2.2  bouyer 	ATF_CHECK_MSG(vlen == sizeof(v), "getsockopt(CAN_RAW_LOOPBACK) returns wrong len %d", vlen);
    283  1.1.2.2  bouyer 	ATF_CHECK_MSG(v == 1, "CAN_RAW_LOOPBACK is not on by default");
    284  1.1.2.2  bouyer 
    285  1.1.2.2  bouyer 	/* check sockopt CAN_RAW_RECV_OWN_MSGS, and set it */
    286  1.1.2.2  bouyer 	vlen = sizeof(v);
    287  1.1.2.2  bouyer 	if (rump_sys_getsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
    288  1.1.2.2  bouyer 	    &v, &vlen) < 0) {
    289  1.1.2.2  bouyer 		atf_tc_fail_errno("getsockopt(CAN_RAW_RECV_OWN_MSGS)");
    290  1.1.2.2  bouyer 	}
    291  1.1.2.2  bouyer 	ATF_CHECK_MSG(vlen == sizeof(v), "getsockopt(CAN_RAW_RECV_OWN_MSGS) returns wrong len %d", vlen);
    292  1.1.2.2  bouyer 	ATF_CHECK_MSG(v == 0, "CAN_RAW_RECV_OWN_MSGS is not off by default");
    293  1.1.2.2  bouyer 	v = 1;
    294  1.1.2.2  bouyer 	if (rump_sys_setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
    295  1.1.2.2  bouyer 	    &v, sizeof(v)) < 0) {
    296  1.1.2.2  bouyer 		atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
    297  1.1.2.2  bouyer 	}
    298  1.1.2.2  bouyer 	/* check sockopt CAN_RAW_RECV_OWN_MSGS again */
    299  1.1.2.2  bouyer 	vlen = sizeof(v);
    300  1.1.2.2  bouyer 	if (rump_sys_getsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
    301  1.1.2.2  bouyer 	    &v, &vlen) < 0) {
    302  1.1.2.2  bouyer 		atf_tc_fail_errno("getsockopt(CAN_RAW_RECV_OWN_MSGS)");
    303  1.1.2.2  bouyer 	}
    304  1.1.2.2  bouyer 	ATF_CHECK_MSG(v == 1, "CAN_RAW_RECV_OWN_MSGS is not on");
    305  1.1.2.2  bouyer 
    306  1.1.2.1  bouyer 	/*
    307  1.1.2.1  bouyer 	 * send a single byte message, but make sure remaining payload is
    308  1.1.2.1  bouyer 	 * not 0.
    309  1.1.2.1  bouyer 	 */
    310  1.1.2.1  bouyer 
    311  1.1.2.1  bouyer 	memset(&cf_send, 0, sizeof(cf_send));
    312  1.1.2.1  bouyer 	cf_send.can_id = 1;
    313  1.1.2.1  bouyer 	cf_send.can_dlc = 1;
    314  1.1.2.1  bouyer 	cf_send.data[0] = 0xde;
    315  1.1.2.1  bouyer 	cf_send.data[1] = 0xad;
    316  1.1.2.1  bouyer 	cf_send.data[2] = 0xbe;
    317  1.1.2.1  bouyer 	cf_send.data[3] = 0xef;
    318  1.1.2.1  bouyer 
    319  1.1.2.1  bouyer 	if (rump_sys_write(s, &cf_send, sizeof(cf_send) - 7) < 0) {
    320  1.1.2.1  bouyer 		atf_tc_fail_errno("write");
    321  1.1.2.1  bouyer 	}
    322  1.1.2.1  bouyer 
    323  1.1.2.1  bouyer 	memset(&cf_receive, 0, sizeof(cf_receive));
    324  1.1.2.1  bouyer 	if (( rv = rump_sys_read(s, &cf_receive, sizeof(cf_receive))) < 0) {
    325  1.1.2.1  bouyer 		atf_tc_fail_errno("read");
    326  1.1.2.1  bouyer 	}
    327  1.1.2.1  bouyer 
    328  1.1.2.1  bouyer 	ATF_CHECK_MSG(rv > 0, "short read on socket");
    329  1.1.2.1  bouyer 
    330  1.1.2.1  bouyer 	memset(&cf_send, 0, sizeof(cf_send));
    331  1.1.2.1  bouyer 	cf_send.can_id = 1;
    332  1.1.2.1  bouyer 	cf_send.can_dlc = 1;
    333  1.1.2.1  bouyer 	cf_send.data[0] = 0xde;
    334  1.1.2.1  bouyer 	/* other data[] are expected to be 0 */
    335  1.1.2.1  bouyer 
    336  1.1.2.1  bouyer 	ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive, sizeof(cf_send)) == 0,
    337  1.1.2.1  bouyer 	    "received packet is not what we sent");
    338  1.1.2.1  bouyer }
    339  1.1.2.1  bouyer 
    340  1.1.2.1  bouyer ATF_TC(canwriteunbound);
    341  1.1.2.1  bouyer ATF_TC_HEAD(canwriteunbound, tc)
    342  1.1.2.1  bouyer {
    343  1.1.2.1  bouyer 
    344  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "descr", "check that write to unbound CAN sockets fails");
    345  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "timeout", "5");
    346  1.1.2.1  bouyer }
    347  1.1.2.1  bouyer 
    348  1.1.2.1  bouyer ATF_TC_BODY(canwriteunbound, tc)
    349  1.1.2.1  bouyer {
    350  1.1.2.1  bouyer 	const char ifname[] = "canlo0";
    351  1.1.2.1  bouyer 	int s, rv;
    352  1.1.2.1  bouyer 	struct sockaddr_can sa;
    353  1.1.2.1  bouyer 	struct can_frame cf_send;
    354  1.1.2.1  bouyer 
    355  1.1.2.1  bouyer 	rump_init();
    356  1.1.2.1  bouyer 	cancfg_rump_createif(ifname);
    357  1.1.2.1  bouyer 
    358  1.1.2.1  bouyer 	s = -1;
    359  1.1.2.1  bouyer 	if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    360  1.1.2.1  bouyer 		atf_tc_fail_errno("CAN socket");
    361  1.1.2.1  bouyer 	}
    362  1.1.2.1  bouyer 
    363  1.1.2.1  bouyer 	/*
    364  1.1.2.1  bouyer 	 * send a single byte message.
    365  1.1.2.1  bouyer 	 * not 0.
    366  1.1.2.1  bouyer 	 */
    367  1.1.2.1  bouyer 
    368  1.1.2.1  bouyer 	memset(&cf_send, 0, sizeof(cf_send));
    369  1.1.2.1  bouyer 	cf_send.can_id = 1;
    370  1.1.2.1  bouyer 	cf_send.can_dlc = 1;
    371  1.1.2.1  bouyer 	cf_send.data[0] = 0xde;
    372  1.1.2.1  bouyer 
    373  1.1.2.1  bouyer 	rv = rump_sys_write(s, &cf_send, sizeof(cf_send) - 7);
    374  1.1.2.1  bouyer 	ATF_CHECK_MSG(rv < 0 && errno == EDESTADDRREQ,
    375  1.1.2.1  bouyer 	    "write to unbound socket didn't fail");
    376  1.1.2.1  bouyer }
    377  1.1.2.1  bouyer 
    378  1.1.2.1  bouyer ATF_TC(cansendtolo);
    379  1.1.2.1  bouyer ATF_TC_HEAD(cansendtolo, tc)
    380  1.1.2.1  bouyer {
    381  1.1.2.1  bouyer 
    382  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "descr", "check that CAN sockets gets its own message via sendto");
    383  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "timeout", "5");
    384  1.1.2.1  bouyer }
    385  1.1.2.1  bouyer 
    386  1.1.2.1  bouyer ATF_TC_BODY(cansendtolo, tc)
    387  1.1.2.1  bouyer {
    388  1.1.2.1  bouyer 	const char ifname[] = "canlo0";
    389  1.1.2.2  bouyer 	int s, v, rv;
    390  1.1.2.1  bouyer 	struct sockaddr_can sa;
    391  1.1.2.1  bouyer 	struct ifreq ifr;
    392  1.1.2.1  bouyer 	struct can_frame cf_send, cf_receive;
    393  1.1.2.1  bouyer 
    394  1.1.2.1  bouyer 	rump_init();
    395  1.1.2.1  bouyer 	cancfg_rump_createif(ifname);
    396  1.1.2.1  bouyer 
    397  1.1.2.1  bouyer 	s = -1;
    398  1.1.2.1  bouyer 	if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    399  1.1.2.1  bouyer 		atf_tc_fail_errno("CAN socket");
    400  1.1.2.1  bouyer 	}
    401  1.1.2.1  bouyer 
    402  1.1.2.2  bouyer 	v = 1;
    403  1.1.2.2  bouyer 	if (rump_sys_setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
    404  1.1.2.2  bouyer 	    &v, sizeof(v)) < 0) {
    405  1.1.2.2  bouyer 		atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
    406  1.1.2.2  bouyer 	}
    407  1.1.2.2  bouyer 
    408  1.1.2.1  bouyer 	strcpy(ifr.ifr_name, ifname );
    409  1.1.2.1  bouyer 	if ((rv = rump_sys_ioctl(s, SIOCGIFINDEX, &ifr)) < 0) {
    410  1.1.2.1  bouyer 		atf_tc_fail_errno("SIOCGIFINDEX");
    411  1.1.2.1  bouyer 	}
    412  1.1.2.1  bouyer 	ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
    413  1.1.2.1  bouyer 	    ifname, ifr.ifr_ifindex);
    414  1.1.2.1  bouyer 
    415  1.1.2.1  bouyer 	sa.can_family = AF_CAN;
    416  1.1.2.1  bouyer 	sa.can_ifindex = ifr.ifr_ifindex;
    417  1.1.2.1  bouyer 
    418  1.1.2.1  bouyer 	/*
    419  1.1.2.1  bouyer 	 * send a single byte message, but make sure remaining payload is
    420  1.1.2.1  bouyer 	 * not 0.
    421  1.1.2.1  bouyer 	 */
    422  1.1.2.1  bouyer 
    423  1.1.2.1  bouyer 	memset(&cf_send, 0, sizeof(cf_send));
    424  1.1.2.1  bouyer 	cf_send.can_id = 1;
    425  1.1.2.1  bouyer 	cf_send.can_dlc = 1;
    426  1.1.2.1  bouyer 	cf_send.data[0] = 0xde;
    427  1.1.2.1  bouyer 	cf_send.data[1] = 0xad;
    428  1.1.2.1  bouyer 	cf_send.data[2] = 0xbe;
    429  1.1.2.1  bouyer 	cf_send.data[3] = 0xef;
    430  1.1.2.1  bouyer 
    431  1.1.2.1  bouyer 	if (rump_sys_sendto(s, &cf_send, sizeof(cf_send) - 7,
    432  1.1.2.1  bouyer 	    0, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
    433  1.1.2.1  bouyer 		atf_tc_fail_errno("sendto");
    434  1.1.2.1  bouyer 	}
    435  1.1.2.1  bouyer 
    436  1.1.2.1  bouyer 	memset(&cf_receive, 0, sizeof(cf_receive));
    437  1.1.2.1  bouyer 	if (( rv = rump_sys_read(s, &cf_receive, sizeof(cf_receive))) < 0) {
    438  1.1.2.1  bouyer 		atf_tc_fail_errno("read");
    439  1.1.2.1  bouyer 	}
    440  1.1.2.1  bouyer 
    441  1.1.2.1  bouyer 	ATF_CHECK_MSG(rv > 0, "short read on socket");
    442  1.1.2.1  bouyer 
    443  1.1.2.1  bouyer 	memset(&cf_send, 0, sizeof(cf_send));
    444  1.1.2.1  bouyer 	cf_send.can_id = 1;
    445  1.1.2.1  bouyer 	cf_send.can_dlc = 1;
    446  1.1.2.1  bouyer 	cf_send.data[0] = 0xde;
    447  1.1.2.1  bouyer 	/* other data[] are expected to be 0 */
    448  1.1.2.1  bouyer 
    449  1.1.2.1  bouyer 	ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive, sizeof(cf_send)) == 0,
    450  1.1.2.1  bouyer 	    "received packet is not what we sent");
    451  1.1.2.1  bouyer }
    452  1.1.2.1  bouyer 
    453  1.1.2.1  bouyer ATF_TC(cansendtowrite);
    454  1.1.2.1  bouyer ATF_TC_HEAD(cansendtowrite, tc)
    455  1.1.2.1  bouyer {
    456  1.1.2.1  bouyer 
    457  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "descr", "check that write after sendto on unbound socket fails");
    458  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "timeout", "5");
    459  1.1.2.1  bouyer }
    460  1.1.2.1  bouyer 
    461  1.1.2.1  bouyer ATF_TC_BODY(cansendtowrite, tc)
    462  1.1.2.1  bouyer {
    463  1.1.2.1  bouyer 	const char ifname[] = "canlo0";
    464  1.1.2.2  bouyer 	int s, rv, v;
    465  1.1.2.1  bouyer 	struct sockaddr_can sa;
    466  1.1.2.1  bouyer 	struct ifreq ifr;
    467  1.1.2.1  bouyer 	struct can_frame cf_send, cf_receive;
    468  1.1.2.1  bouyer 
    469  1.1.2.1  bouyer 	rump_init();
    470  1.1.2.1  bouyer 	cancfg_rump_createif(ifname);
    471  1.1.2.1  bouyer 
    472  1.1.2.1  bouyer 	s = -1;
    473  1.1.2.1  bouyer 	if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    474  1.1.2.1  bouyer 		atf_tc_fail_errno("CAN socket");
    475  1.1.2.1  bouyer 	}
    476  1.1.2.2  bouyer 	v = 1;
    477  1.1.2.2  bouyer 	if (rump_sys_setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
    478  1.1.2.2  bouyer 	    &v, sizeof(v)) < 0) {
    479  1.1.2.2  bouyer 		atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
    480  1.1.2.2  bouyer 	}
    481  1.1.2.1  bouyer 
    482  1.1.2.1  bouyer 	strcpy(ifr.ifr_name, ifname );
    483  1.1.2.1  bouyer 	if ((rv = rump_sys_ioctl(s, SIOCGIFINDEX, &ifr)) < 0) {
    484  1.1.2.1  bouyer 		atf_tc_fail_errno("SIOCGIFINDEX");
    485  1.1.2.1  bouyer 	}
    486  1.1.2.1  bouyer 	ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
    487  1.1.2.1  bouyer 	    ifname, ifr.ifr_ifindex);
    488  1.1.2.1  bouyer 
    489  1.1.2.1  bouyer 	sa.can_family = AF_CAN;
    490  1.1.2.1  bouyer 	sa.can_ifindex = ifr.ifr_ifindex;
    491  1.1.2.1  bouyer 
    492  1.1.2.1  bouyer 	/*
    493  1.1.2.1  bouyer 	 * send a single byte message.
    494  1.1.2.1  bouyer 	 * not 0.
    495  1.1.2.1  bouyer 	 */
    496  1.1.2.1  bouyer 
    497  1.1.2.1  bouyer 	memset(&cf_send, 0, sizeof(cf_send));
    498  1.1.2.1  bouyer 	cf_send.can_id = 1;
    499  1.1.2.1  bouyer 	cf_send.can_dlc = 1;
    500  1.1.2.1  bouyer 	cf_send.data[0] = 0xde;
    501  1.1.2.1  bouyer 
    502  1.1.2.1  bouyer 	if (rump_sys_sendto(s, &cf_send, sizeof(cf_send) - 7,
    503  1.1.2.1  bouyer 	    0, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
    504  1.1.2.1  bouyer 		atf_tc_fail_errno("sendto");
    505  1.1.2.1  bouyer 	}
    506  1.1.2.1  bouyer 
    507  1.1.2.1  bouyer 	memset(&cf_receive, 0, sizeof(cf_receive));
    508  1.1.2.1  bouyer 	if (( rv = rump_sys_read(s, &cf_receive, sizeof(cf_receive))) < 0) {
    509  1.1.2.1  bouyer 		atf_tc_fail_errno("read");
    510  1.1.2.1  bouyer 	}
    511  1.1.2.1  bouyer 
    512  1.1.2.1  bouyer 	ATF_CHECK_MSG(rv > 0, "short read on socket");
    513  1.1.2.1  bouyer 
    514  1.1.2.1  bouyer 	memset(&cf_send, 0, sizeof(cf_send));
    515  1.1.2.1  bouyer 	cf_send.can_id = 1;
    516  1.1.2.1  bouyer 	cf_send.can_dlc = 1;
    517  1.1.2.1  bouyer 	cf_send.data[0] = 0xde;
    518  1.1.2.1  bouyer 	/* other data[] are expected to be 0 */
    519  1.1.2.1  bouyer 
    520  1.1.2.1  bouyer 	ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive, sizeof(cf_send)) == 0,
    521  1.1.2.1  bouyer 	    "received packet is not what we sent");
    522  1.1.2.1  bouyer 
    523  1.1.2.1  bouyer 	rv = rump_sys_write(s, &cf_send, sizeof(cf_send) - 7);
    524  1.1.2.1  bouyer 	ATF_CHECK_MSG(rv < 0 && errno == EDESTADDRREQ,
    525  1.1.2.1  bouyer 	    "write to unbound socket didn't fail");
    526  1.1.2.1  bouyer }
    527  1.1.2.1  bouyer 
    528  1.1.2.1  bouyer ATF_TC(canreadlocal);
    529  1.1.2.1  bouyer ATF_TC_HEAD(canreadlocal, tc)
    530  1.1.2.1  bouyer {
    531  1.1.2.1  bouyer 
    532  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "descr", "check all CAN sockets get messages");
    533  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "timeout", "5");
    534  1.1.2.1  bouyer }
    535  1.1.2.1  bouyer 
    536  1.1.2.1  bouyer ATF_TC_BODY(canreadlocal, tc)
    537  1.1.2.1  bouyer {
    538  1.1.2.1  bouyer 	const char ifname[] = "canlo0";
    539  1.1.2.1  bouyer 	int s1, rv1;
    540  1.1.2.1  bouyer 	int s2, rv2;
    541  1.1.2.2  bouyer 	int v;
    542  1.1.2.1  bouyer 	struct sockaddr_can sa;
    543  1.1.2.1  bouyer 	struct ifreq ifr;
    544  1.1.2.1  bouyer 	struct can_frame cf_send, cf_receive1, cf_receive2;
    545  1.1.2.1  bouyer 
    546  1.1.2.1  bouyer 	rump_init();
    547  1.1.2.1  bouyer 	cancfg_rump_createif(ifname);
    548  1.1.2.1  bouyer 
    549  1.1.2.1  bouyer 	memset(&cf_send, 0, sizeof(cf_send));
    550  1.1.2.1  bouyer 	cf_send.can_id = 1;
    551  1.1.2.1  bouyer 	cf_send.can_dlc = 8;
    552  1.1.2.1  bouyer 	cf_send.data[0] = 0xde;
    553  1.1.2.1  bouyer 	cf_send.data[1] = 0xad;
    554  1.1.2.1  bouyer 	cf_send.data[2] = 0xbe;
    555  1.1.2.1  bouyer 	cf_send.data[3] = 0xef;
    556  1.1.2.1  bouyer 
    557  1.1.2.1  bouyer 
    558  1.1.2.1  bouyer 	s1 = -1;
    559  1.1.2.1  bouyer 	if ((s1 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    560  1.1.2.1  bouyer 		atf_tc_fail_errno("CAN socket");
    561  1.1.2.1  bouyer 	}
    562  1.1.2.1  bouyer 
    563  1.1.2.1  bouyer 	/* create a second socket */
    564  1.1.2.1  bouyer 
    565  1.1.2.1  bouyer 	s2 = -1;
    566  1.1.2.1  bouyer 	if ((s2 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    567  1.1.2.1  bouyer 		atf_tc_fail_errno("CAN socket");
    568  1.1.2.1  bouyer 	}
    569  1.1.2.2  bouyer 	v = 1;
    570  1.1.2.2  bouyer 	if (rump_sys_setsockopt(s2, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
    571  1.1.2.2  bouyer 	    &v, sizeof(v)) < 0) {
    572  1.1.2.2  bouyer 		atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
    573  1.1.2.2  bouyer 	}
    574  1.1.2.1  bouyer 
    575  1.1.2.1  bouyer 	strcpy(ifr.ifr_name, ifname );
    576  1.1.2.1  bouyer 	if ((rv2 = rump_sys_ioctl(s2, SIOCGIFINDEX, &ifr)) < 0) {
    577  1.1.2.1  bouyer 		atf_tc_fail_errno("SIOCGIFINDEX");
    578  1.1.2.1  bouyer 	}
    579  1.1.2.1  bouyer 	ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
    580  1.1.2.1  bouyer 	    ifname, ifr.ifr_ifindex);
    581  1.1.2.1  bouyer 
    582  1.1.2.1  bouyer 	sa.can_family = AF_CAN;
    583  1.1.2.1  bouyer 	sa.can_ifindex = ifr.ifr_ifindex;
    584  1.1.2.1  bouyer 
    585  1.1.2.1  bouyer 	if ((rv2 = rump_sys_bind(s2, (struct sockaddr *)&sa, sizeof(sa))) < 0) {
    586  1.1.2.1  bouyer 		atf_tc_fail_errno("bind");
    587  1.1.2.1  bouyer 	}
    588  1.1.2.1  bouyer 
    589  1.1.2.1  bouyer 	/*
    590  1.1.2.1  bouyer 	 * send a single byte message, but make sure remaining payload is
    591  1.1.2.1  bouyer 	 * not 0.
    592  1.1.2.1  bouyer 	 */
    593  1.1.2.1  bouyer 
    594  1.1.2.1  bouyer 	if (rump_sys_write(s2, &cf_send, sizeof(cf_send)) < 0) {
    595  1.1.2.1  bouyer 		atf_tc_fail_errno("write");
    596  1.1.2.1  bouyer 	}
    597  1.1.2.1  bouyer 
    598  1.1.2.1  bouyer 	memset(&cf_receive2, 0, sizeof(cf_receive2));
    599  1.1.2.1  bouyer 	if (( rv2 = rump_sys_read(s2, &cf_receive2, sizeof(cf_receive2))) < 0) {
    600  1.1.2.1  bouyer 		atf_tc_fail_errno("read");
    601  1.1.2.1  bouyer 	}
    602  1.1.2.1  bouyer 
    603  1.1.2.1  bouyer 	ATF_CHECK_MSG(rv2 > 0, "short read on socket");
    604  1.1.2.1  bouyer 
    605  1.1.2.1  bouyer 	ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive2, sizeof(cf_send)) == 0,
    606  1.1.2.1  bouyer 	    "received (2) packet is not what we sent");
    607  1.1.2.1  bouyer 
    608  1.1.2.1  bouyer 	/* now check first socket */
    609  1.1.2.1  bouyer 	memset(&cf_receive1, 0, sizeof(cf_receive1));
    610  1.1.2.1  bouyer 	if (( rv1 = rump_sys_read(s1, &cf_receive1, sizeof(cf_receive1))) < 0) {
    611  1.1.2.1  bouyer 		atf_tc_fail_errno("read");
    612  1.1.2.1  bouyer 	}
    613  1.1.2.1  bouyer 
    614  1.1.2.1  bouyer 	ATF_CHECK_MSG(rv1 > 0, "short read on socket");
    615  1.1.2.1  bouyer 
    616  1.1.2.1  bouyer 	ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive1, sizeof(cf_send)) == 0,
    617  1.1.2.1  bouyer 	    "received (1) packet is not what we sent");
    618  1.1.2.1  bouyer }
    619  1.1.2.1  bouyer 
    620  1.1.2.1  bouyer ATF_TC(canrecvfrom);
    621  1.1.2.1  bouyer ATF_TC_HEAD(canrecvfrom, tc)
    622  1.1.2.1  bouyer {
    623  1.1.2.1  bouyer 
    624  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "descr", "check that recvfrom gets the CAN interface");
    625  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "timeout", "5");
    626  1.1.2.1  bouyer }
    627  1.1.2.1  bouyer 
    628  1.1.2.1  bouyer ATF_TC_BODY(canrecvfrom, tc)
    629  1.1.2.1  bouyer {
    630  1.1.2.1  bouyer 	const char ifname[] = "canlo0";
    631  1.1.2.1  bouyer 	int s1, rv1;
    632  1.1.2.1  bouyer 	int s2, rv2;
    633  1.1.2.2  bouyer 	int v;
    634  1.1.2.1  bouyer 	struct sockaddr_can sa;
    635  1.1.2.1  bouyer 	struct ifreq ifr;
    636  1.1.2.1  bouyer 	struct can_frame cf_send, cf_receive1, cf_receive2;
    637  1.1.2.1  bouyer 	socklen_t salen;
    638  1.1.2.1  bouyer 
    639  1.1.2.1  bouyer 	rump_init();
    640  1.1.2.1  bouyer 	cancfg_rump_createif(ifname);
    641  1.1.2.1  bouyer 
    642  1.1.2.1  bouyer 	memset(&cf_send, 0, sizeof(cf_send));
    643  1.1.2.1  bouyer 	cf_send.can_id = 1;
    644  1.1.2.1  bouyer 	cf_send.can_dlc = 8;
    645  1.1.2.1  bouyer 	cf_send.data[0] = 0xde;
    646  1.1.2.1  bouyer 	cf_send.data[1] = 0xad;
    647  1.1.2.1  bouyer 	cf_send.data[2] = 0xbe;
    648  1.1.2.1  bouyer 	cf_send.data[3] = 0xef;
    649  1.1.2.1  bouyer 
    650  1.1.2.1  bouyer 
    651  1.1.2.1  bouyer 	s1 = -1;
    652  1.1.2.1  bouyer 	if ((s1 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    653  1.1.2.1  bouyer 		atf_tc_fail_errno("CAN socket");
    654  1.1.2.1  bouyer 	}
    655  1.1.2.1  bouyer 
    656  1.1.2.1  bouyer 	/* create a second socket */
    657  1.1.2.1  bouyer 
    658  1.1.2.1  bouyer 	s2 = -1;
    659  1.1.2.1  bouyer 	if ((s2 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    660  1.1.2.1  bouyer 		atf_tc_fail_errno("CAN socket");
    661  1.1.2.1  bouyer 	}
    662  1.1.2.2  bouyer 	v = 1;
    663  1.1.2.2  bouyer 	if (rump_sys_setsockopt(s2, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
    664  1.1.2.2  bouyer 	    &v, sizeof(v)) < 0) {
    665  1.1.2.2  bouyer 		atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
    666  1.1.2.2  bouyer 	}
    667  1.1.2.1  bouyer 
    668  1.1.2.1  bouyer 	strcpy(ifr.ifr_name, ifname );
    669  1.1.2.1  bouyer 	if ((rv2 = rump_sys_ioctl(s2, SIOCGIFINDEX, &ifr)) < 0) {
    670  1.1.2.1  bouyer 		atf_tc_fail_errno("SIOCGIFINDEX");
    671  1.1.2.1  bouyer 	}
    672  1.1.2.1  bouyer 	ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
    673  1.1.2.1  bouyer 	    ifname, ifr.ifr_ifindex);
    674  1.1.2.1  bouyer 
    675  1.1.2.1  bouyer 	sa.can_family = AF_CAN;
    676  1.1.2.1  bouyer 	sa.can_ifindex = ifr.ifr_ifindex;
    677  1.1.2.1  bouyer 
    678  1.1.2.1  bouyer 	if ((rv2 = rump_sys_bind(s2, (struct sockaddr *)&sa, sizeof(sa))) < 0) {
    679  1.1.2.1  bouyer 		atf_tc_fail_errno("bind");
    680  1.1.2.1  bouyer 	}
    681  1.1.2.1  bouyer 
    682  1.1.2.1  bouyer 	if (rump_sys_write(s2, &cf_send, sizeof(cf_send)) < 0) {
    683  1.1.2.1  bouyer 		atf_tc_fail_errno("write");
    684  1.1.2.1  bouyer 	}
    685  1.1.2.1  bouyer 
    686  1.1.2.1  bouyer 	memset(&cf_receive2, 0, sizeof(cf_receive2));
    687  1.1.2.1  bouyer 	if (( rv2 = rump_sys_read(s2, &cf_receive2, sizeof(cf_receive2))) < 0) {
    688  1.1.2.1  bouyer 		atf_tc_fail_errno("read");
    689  1.1.2.1  bouyer 	}
    690  1.1.2.1  bouyer 
    691  1.1.2.1  bouyer 	ATF_CHECK_MSG(rv2 > 0, "short read on socket");
    692  1.1.2.1  bouyer 
    693  1.1.2.1  bouyer 	ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive2, sizeof(cf_send)) == 0,
    694  1.1.2.1  bouyer 	    "received (2) packet is not what we sent");
    695  1.1.2.1  bouyer 
    696  1.1.2.1  bouyer 	/* now check first socket */
    697  1.1.2.1  bouyer 	memset(&cf_receive1, 0, sizeof(cf_receive1));
    698  1.1.2.1  bouyer 	memset(&sa, 0, sizeof(sa));
    699  1.1.2.1  bouyer 	salen = sizeof(sa);
    700  1.1.2.1  bouyer 	if (( rv1 = rump_sys_recvfrom(s1, &cf_receive1, sizeof(cf_receive1),
    701  1.1.2.1  bouyer 	    0, (struct sockaddr *)&sa, &salen)) < 0) {
    702  1.1.2.1  bouyer 		atf_tc_fail_errno("recvfrom");
    703  1.1.2.1  bouyer 	}
    704  1.1.2.1  bouyer 
    705  1.1.2.1  bouyer 	ATF_CHECK_MSG(rv1 > 0, "short read on socket");
    706  1.1.2.1  bouyer 
    707  1.1.2.1  bouyer 	ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive1, sizeof(cf_send)) == 0,
    708  1.1.2.1  bouyer 	    "recvfrom (1) packet is not what we sent");
    709  1.1.2.1  bouyer 	ATF_CHECK_MSG(sa.can_family == AF_CAN,
    710  1.1.2.1  bouyer 	    "recvfrom provided wrong %d family", sa.can_family);
    711  1.1.2.1  bouyer 	ATF_CHECK_MSG(salen == sizeof(sa),
    712  1.1.2.1  bouyer 	    "recvfrom provided wrong size %d (!= %d)", salen, sizeof(sa));
    713  1.1.2.1  bouyer 	ATF_CHECK_MSG(sa.can_ifindex == ifr.ifr_ifindex,
    714  1.1.2.1  bouyer 	   "recvfrom provided wrong ifindex %d (!= %d)",
    715  1.1.2.1  bouyer 	    sa.can_ifindex, ifr.ifr_ifindex);
    716  1.1.2.1  bouyer }
    717  1.1.2.1  bouyer 
    718  1.1.2.1  bouyer ATF_TC(canbindfilter);
    719  1.1.2.1  bouyer ATF_TC_HEAD(canbindfilter, tc)
    720  1.1.2.1  bouyer {
    721  1.1.2.1  bouyer 
    722  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "descr", "check that  socket bound to an interface doens't get other interface's messages");
    723  1.1.2.1  bouyer 	atf_tc_set_md_var(tc, "timeout", "5");
    724  1.1.2.1  bouyer }
    725  1.1.2.1  bouyer 
    726  1.1.2.1  bouyer ATF_TC_BODY(canbindfilter, tc)
    727  1.1.2.1  bouyer {
    728  1.1.2.1  bouyer 	const char ifname[] = "canlo0";
    729  1.1.2.1  bouyer 	const char ifname2[] = "canlo1";
    730  1.1.2.1  bouyer 	int s1, rv1;
    731  1.1.2.1  bouyer 	int s2, rv2;
    732  1.1.2.2  bouyer 	int v;
    733  1.1.2.1  bouyer 	struct sockaddr_can sa;
    734  1.1.2.1  bouyer 	struct ifreq ifr;
    735  1.1.2.1  bouyer 	struct can_frame cf_send, cf_receive1, cf_receive2;
    736  1.1.2.1  bouyer 	socklen_t salen;
    737  1.1.2.1  bouyer 	fd_set rfds;
    738  1.1.2.1  bouyer 	struct timeval tmout;
    739  1.1.2.1  bouyer 
    740  1.1.2.1  bouyer 	rump_init();
    741  1.1.2.1  bouyer 	cancfg_rump_createif(ifname);
    742  1.1.2.1  bouyer 	cancfg_rump_createif(ifname2);
    743  1.1.2.1  bouyer 
    744  1.1.2.1  bouyer 	memset(&cf_send, 0, sizeof(cf_send));
    745  1.1.2.1  bouyer 	cf_send.can_id = 1;
    746  1.1.2.1  bouyer 	cf_send.can_dlc = 8;
    747  1.1.2.1  bouyer 	cf_send.data[0] = 0xde;
    748  1.1.2.1  bouyer 	cf_send.data[1] = 0xad;
    749  1.1.2.1  bouyer 	cf_send.data[2] = 0xbe;
    750  1.1.2.1  bouyer 	cf_send.data[3] = 0xef;
    751  1.1.2.1  bouyer 
    752  1.1.2.1  bouyer 
    753  1.1.2.1  bouyer 	s1 = -1;
    754  1.1.2.1  bouyer 	if ((s1 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    755  1.1.2.1  bouyer 		atf_tc_fail_errno("CAN socket");
    756  1.1.2.1  bouyer 	}
    757  1.1.2.2  bouyer 	v = 1;
    758  1.1.2.2  bouyer 	if (rump_sys_setsockopt(s1, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
    759  1.1.2.2  bouyer 	    &v, sizeof(v)) < 0) {
    760  1.1.2.2  bouyer 		atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
    761  1.1.2.2  bouyer 	}
    762  1.1.2.1  bouyer 
    763  1.1.2.1  bouyer 	strcpy(ifr.ifr_name, ifname );
    764  1.1.2.1  bouyer 	if ((rv1 = rump_sys_ioctl(s1, SIOCGIFINDEX, &ifr)) < 0) {
    765  1.1.2.1  bouyer 		atf_tc_fail_errno("SIOCGIFINDEX (1)");
    766  1.1.2.1  bouyer 	}
    767  1.1.2.1  bouyer 	ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
    768  1.1.2.1  bouyer 	    ifname, ifr.ifr_ifindex);
    769  1.1.2.1  bouyer 
    770  1.1.2.1  bouyer 	sa.can_family = AF_CAN;
    771  1.1.2.1  bouyer 	sa.can_ifindex = ifr.ifr_ifindex;
    772  1.1.2.1  bouyer 
    773  1.1.2.1  bouyer 	if ((rv1 = rump_sys_bind(s1, (struct sockaddr *)&sa, sizeof(sa))) < 0) {
    774  1.1.2.1  bouyer 		atf_tc_fail_errno("bind (1)");
    775  1.1.2.1  bouyer 	}
    776  1.1.2.1  bouyer 
    777  1.1.2.1  bouyer 	/* create a second socket */
    778  1.1.2.1  bouyer 
    779  1.1.2.1  bouyer 	s2 = -1;
    780  1.1.2.1  bouyer 	if ((s2 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    781  1.1.2.1  bouyer 		atf_tc_fail_errno("CAN socket");
    782  1.1.2.1  bouyer 	}
    783  1.1.2.2  bouyer 	v = 1;
    784  1.1.2.2  bouyer 	if (rump_sys_setsockopt(s2, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
    785  1.1.2.2  bouyer 	    &v, sizeof(v)) < 0) {
    786  1.1.2.2  bouyer 		atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
    787  1.1.2.2  bouyer 	}
    788  1.1.2.1  bouyer 
    789  1.1.2.1  bouyer 	strcpy(ifr.ifr_name, ifname2);
    790  1.1.2.1  bouyer 	if ((rv2 = rump_sys_ioctl(s2, SIOCGIFINDEX, &ifr)) < 0) {
    791  1.1.2.1  bouyer 		atf_tc_fail_errno("SIOCGIFINDEX");
    792  1.1.2.1  bouyer 	}
    793  1.1.2.1  bouyer 	ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
    794  1.1.2.1  bouyer 	    ifname, ifr.ifr_ifindex);
    795  1.1.2.1  bouyer 
    796  1.1.2.1  bouyer 	sa.can_family = AF_CAN;
    797  1.1.2.1  bouyer 	sa.can_ifindex = ifr.ifr_ifindex;
    798  1.1.2.1  bouyer 
    799  1.1.2.1  bouyer 	if ((rv2 = rump_sys_bind(s2, (struct sockaddr *)&sa, sizeof(sa))) < 0) {
    800  1.1.2.1  bouyer 		atf_tc_fail_errno("bind");
    801  1.1.2.1  bouyer 	}
    802  1.1.2.1  bouyer 
    803  1.1.2.1  bouyer 	if (rump_sys_write(s2, &cf_send, sizeof(cf_send)) < 0) {
    804  1.1.2.1  bouyer 		atf_tc_fail_errno("write");
    805  1.1.2.1  bouyer 	}
    806  1.1.2.1  bouyer 
    807  1.1.2.1  bouyer 	memset(&cf_receive2, 0, sizeof(cf_receive2));
    808  1.1.2.1  bouyer 	if (( rv2 = rump_sys_read(s2, &cf_receive2, sizeof(cf_receive2))) < 0) {
    809  1.1.2.1  bouyer 		atf_tc_fail_errno("read");
    810  1.1.2.1  bouyer 	}
    811  1.1.2.1  bouyer 
    812  1.1.2.1  bouyer 	ATF_CHECK_MSG(rv2 > 0, "short read on socket");
    813  1.1.2.1  bouyer 
    814  1.1.2.1  bouyer 	ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive2, sizeof(cf_send)) == 0,
    815  1.1.2.1  bouyer 	    "received (2) packet is not what we sent");
    816  1.1.2.1  bouyer 
    817  1.1.2.1  bouyer 	/* now check first socket */
    818  1.1.2.1  bouyer 	memset(&cf_receive1, 0, sizeof(cf_receive1));
    819  1.1.2.1  bouyer 	FD_ZERO(&rfds);
    820  1.1.2.1  bouyer 	FD_SET(s1, &rfds);
    821  1.1.2.1  bouyer 	/* we should receive no message; wait for 2 seconds */
    822  1.1.2.1  bouyer 	tmout.tv_sec = 2;
    823  1.1.2.1  bouyer 	tmout.tv_usec = 0;
    824  1.1.2.1  bouyer 	rv1 = rump_sys_select(s1 + 1, &rfds, NULL, NULL, &tmout);
    825  1.1.2.1  bouyer 	switch(rv1) {
    826  1.1.2.1  bouyer 	case -1:
    827  1.1.2.1  bouyer 		atf_tc_fail_errno("select");
    828  1.1.2.1  bouyer 		break;
    829  1.1.2.1  bouyer 	case 0:
    830  1.1.2.1  bouyer 		/* timeout: expected case */
    831  1.1.2.1  bouyer 		return;
    832  1.1.2.1  bouyer 	default: break;
    833  1.1.2.1  bouyer 	}
    834  1.1.2.1  bouyer 	salen = sizeof(sa);
    835  1.1.2.1  bouyer 	ATF_CHECK_MSG(FD_ISSET(s1, &rfds), "select returns but s1 not in set");
    836  1.1.2.1  bouyer 	if (( rv1 = rump_sys_recvfrom(s1, &cf_receive1, sizeof(cf_receive1),
    837  1.1.2.1  bouyer 	    0, (struct sockaddr *)&sa, &salen)) < 0) {
    838  1.1.2.1  bouyer 		atf_tc_fail_errno("recvfrom");
    839  1.1.2.1  bouyer 	}
    840  1.1.2.1  bouyer 
    841  1.1.2.1  bouyer 	ATF_CHECK_MSG(rv1 > 0, "short read on socket");
    842  1.1.2.1  bouyer 
    843  1.1.2.1  bouyer 	ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive1, sizeof(cf_send)) == 0,
    844  1.1.2.1  bouyer 	    "recvfrom (1) packet is not what we sent");
    845  1.1.2.1  bouyer 	ATF_CHECK_MSG(sa.can_family == AF_CAN,
    846  1.1.2.1  bouyer 	    "recvfrom provided wrong %d family", sa.can_family);
    847  1.1.2.1  bouyer 	ATF_CHECK_MSG(salen == sizeof(sa),
    848  1.1.2.1  bouyer 	    "recvfrom provided wrong size %d (!= %d)", salen, sizeof(sa));
    849  1.1.2.1  bouyer 	ATF_CHECK_MSG(sa.can_ifindex == ifr.ifr_ifindex,
    850  1.1.2.1  bouyer 	   "recvfrom provided wrong ifindex %d (!= %d)",
    851  1.1.2.1  bouyer 	    sa.can_ifindex, ifr.ifr_ifindex);
    852  1.1.2.1  bouyer 	atf_tc_fail("we got message from other interface");
    853  1.1.2.1  bouyer }
    854  1.1.2.1  bouyer 
    855  1.1.2.2  bouyer ATF_TC(cannoloop);
    856  1.1.2.2  bouyer ATF_TC_HEAD(cannoloop, tc)
    857  1.1.2.2  bouyer {
    858  1.1.2.2  bouyer 
    859  1.1.2.2  bouyer 	atf_tc_set_md_var(tc, "descr", "check that disabling loopback works");
    860  1.1.2.2  bouyer 	atf_tc_set_md_var(tc, "timeout", "5");
    861  1.1.2.2  bouyer }
    862  1.1.2.2  bouyer 
    863  1.1.2.2  bouyer ATF_TC_BODY(cannoloop, tc)
    864  1.1.2.2  bouyer {
    865  1.1.2.2  bouyer 	const char ifname[] = "canlo0";
    866  1.1.2.2  bouyer 	int s1, rv1;
    867  1.1.2.2  bouyer 	int s2, rv2;
    868  1.1.2.2  bouyer 	int v, vlen;
    869  1.1.2.2  bouyer 	struct sockaddr_can sa;
    870  1.1.2.2  bouyer 	struct ifreq ifr;
    871  1.1.2.2  bouyer 	struct can_frame cf_send, cf_receive1, cf_receive2;
    872  1.1.2.2  bouyer 	socklen_t salen;
    873  1.1.2.2  bouyer 	fd_set rfds;
    874  1.1.2.2  bouyer 	struct timeval tmout;
    875  1.1.2.2  bouyer 
    876  1.1.2.2  bouyer 	rump_init();
    877  1.1.2.2  bouyer 	cancfg_rump_createif(ifname);
    878  1.1.2.2  bouyer 
    879  1.1.2.2  bouyer 	memset(&cf_send, 0, sizeof(cf_send));
    880  1.1.2.2  bouyer 	cf_send.can_id = 1;
    881  1.1.2.2  bouyer 	cf_send.can_dlc = 8;
    882  1.1.2.2  bouyer 	cf_send.data[0] = 0xde;
    883  1.1.2.2  bouyer 	cf_send.data[1] = 0xad;
    884  1.1.2.2  bouyer 	cf_send.data[2] = 0xbe;
    885  1.1.2.2  bouyer 	cf_send.data[3] = 0xef;
    886  1.1.2.2  bouyer 
    887  1.1.2.2  bouyer 
    888  1.1.2.2  bouyer 	s1 = -1;
    889  1.1.2.2  bouyer 	if ((s1 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    890  1.1.2.2  bouyer 		atf_tc_fail_errno("CAN socket");
    891  1.1.2.2  bouyer 	}
    892  1.1.2.2  bouyer 	v = 1;
    893  1.1.2.2  bouyer 	if (rump_sys_setsockopt(s1, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
    894  1.1.2.2  bouyer 	    &v, sizeof(v)) < 0) {
    895  1.1.2.2  bouyer 		atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
    896  1.1.2.2  bouyer 	}
    897  1.1.2.2  bouyer 	v = 0;
    898  1.1.2.2  bouyer 	if (rump_sys_setsockopt(s1, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
    899  1.1.2.2  bouyer 	    &v, sizeof(v)) < 0) {
    900  1.1.2.2  bouyer 		atf_tc_fail_errno("setsockopt(LOOPBACK)");
    901  1.1.2.2  bouyer 	}
    902  1.1.2.2  bouyer 	v = -1;
    903  1.1.2.2  bouyer 	vlen = sizeof(v);
    904  1.1.2.2  bouyer 	if (rump_sys_getsockopt(s1, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
    905  1.1.2.2  bouyer 	    &v, &vlen) < 0) {
    906  1.1.2.2  bouyer 		atf_tc_fail_errno("getsockopt(CAN_RAW_LOOPBACK)");
    907  1.1.2.2  bouyer 	}
    908  1.1.2.2  bouyer 	ATF_CHECK_MSG(vlen == sizeof(v), "getsockopt(CAN_RAW_LOOPBACK) returns wrong len %d", vlen);
    909  1.1.2.2  bouyer 	ATF_CHECK_MSG(v == 0, "CAN_RAW_LOOPBACK is not off");
    910  1.1.2.2  bouyer 
    911  1.1.2.2  bouyer 	strcpy(ifr.ifr_name, ifname );
    912  1.1.2.2  bouyer 	if ((rv1 = rump_sys_ioctl(s1, SIOCGIFINDEX, &ifr)) < 0) {
    913  1.1.2.2  bouyer 		atf_tc_fail_errno("SIOCGIFINDEX (1)");
    914  1.1.2.2  bouyer 	}
    915  1.1.2.2  bouyer 	ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
    916  1.1.2.2  bouyer 	    ifname, ifr.ifr_ifindex);
    917  1.1.2.2  bouyer 
    918  1.1.2.2  bouyer 	sa.can_family = AF_CAN;
    919  1.1.2.2  bouyer 	sa.can_ifindex = ifr.ifr_ifindex;
    920  1.1.2.2  bouyer 
    921  1.1.2.2  bouyer 	if ((rv1 = rump_sys_bind(s1, (struct sockaddr *)&sa, sizeof(sa))) < 0) {
    922  1.1.2.2  bouyer 		atf_tc_fail_errno("bind (1)");
    923  1.1.2.2  bouyer 	}
    924  1.1.2.2  bouyer 
    925  1.1.2.2  bouyer 	/* create a second socket */
    926  1.1.2.2  bouyer 
    927  1.1.2.2  bouyer 	s2 = -1;
    928  1.1.2.2  bouyer 	if ((s2 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    929  1.1.2.2  bouyer 		atf_tc_fail_errno("CAN socket");
    930  1.1.2.2  bouyer 	}
    931  1.1.2.2  bouyer 	v = 1;
    932  1.1.2.2  bouyer 	if (rump_sys_setsockopt(s2, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
    933  1.1.2.2  bouyer 	    &v, sizeof(v)) < 0) {
    934  1.1.2.2  bouyer 		atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
    935  1.1.2.2  bouyer 	}
    936  1.1.2.2  bouyer 
    937  1.1.2.2  bouyer 	if (rump_sys_write(s1, &cf_send, sizeof(cf_send)) < 0) {
    938  1.1.2.2  bouyer 		atf_tc_fail_errno("write");
    939  1.1.2.2  bouyer 	}
    940  1.1.2.2  bouyer 
    941  1.1.2.2  bouyer 
    942  1.1.2.2  bouyer 	/* now check the sockets */
    943  1.1.2.2  bouyer 	memset(&cf_receive1, 0, sizeof(cf_receive1));
    944  1.1.2.2  bouyer 	memset(&cf_receive2, 0, sizeof(cf_receive2));
    945  1.1.2.2  bouyer 	FD_ZERO(&rfds);
    946  1.1.2.2  bouyer 	FD_SET(s1, &rfds);
    947  1.1.2.2  bouyer 	FD_SET(s2, &rfds);
    948  1.1.2.2  bouyer 	/* we should receive no message; wait for 2 seconds */
    949  1.1.2.2  bouyer 	tmout.tv_sec = 2;
    950  1.1.2.2  bouyer 	tmout.tv_usec = 0;
    951  1.1.2.2  bouyer 	rv1 = rump_sys_select(MAX(s1,s2) + 1, &rfds, NULL, NULL, &tmout);
    952  1.1.2.2  bouyer 	switch(rv1) {
    953  1.1.2.2  bouyer 	case -1:
    954  1.1.2.2  bouyer 		atf_tc_fail_errno("select");
    955  1.1.2.2  bouyer 		break;
    956  1.1.2.2  bouyer 	case 0:
    957  1.1.2.2  bouyer 		/* timeout: expected case */
    958  1.1.2.2  bouyer 		return;
    959  1.1.2.2  bouyer 	default: break;
    960  1.1.2.2  bouyer 	}
    961  1.1.2.2  bouyer 	salen = sizeof(sa);
    962  1.1.2.2  bouyer 	ATF_CHECK_MSG(FD_ISSET(s1, &rfds) || FD_ISSET(s2, &rfds),
    963  1.1.2.2  bouyer 	   "select returns but s1 nor s2 is in set");
    964  1.1.2.2  bouyer 	if (FD_ISSET(s1, &rfds)) {
    965  1.1.2.2  bouyer 		if (( rv1 = rump_sys_recvfrom(s1, &cf_receive1,
    966  1.1.2.2  bouyer 		    sizeof(cf_receive1), 0,
    967  1.1.2.2  bouyer 		    (struct sockaddr *)&sa, &salen)) < 0) {
    968  1.1.2.2  bouyer 			atf_tc_fail_errno("recvfrom");
    969  1.1.2.2  bouyer 		}
    970  1.1.2.2  bouyer 
    971  1.1.2.2  bouyer 		ATF_CHECK_MSG(rv1 > 0, "short read on socket");
    972  1.1.2.2  bouyer 
    973  1.1.2.2  bouyer 		ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive1,
    974  1.1.2.2  bouyer 		    sizeof(cf_send)) == 0,
    975  1.1.2.2  bouyer 		    "recvfrom (1) packet is not what we sent");
    976  1.1.2.2  bouyer 		ATF_CHECK_MSG(sa.can_family == AF_CAN,
    977  1.1.2.2  bouyer 		    "recvfrom provided wrong %d family", sa.can_family);
    978  1.1.2.2  bouyer 		ATF_CHECK_MSG(salen == sizeof(sa),
    979  1.1.2.2  bouyer 		    "recvfrom provided wrong size %d (!= %d)",
    980  1.1.2.2  bouyer 		    salen, sizeof(sa));
    981  1.1.2.2  bouyer 		ATF_CHECK_MSG(sa.can_ifindex == ifr.ifr_ifindex,
    982  1.1.2.2  bouyer 		   "recvfrom provided wrong ifindex %d (!= %d)",
    983  1.1.2.2  bouyer 		    sa.can_ifindex, ifr.ifr_ifindex);
    984  1.1.2.2  bouyer 		atf_tc_fail_nonfatal("we got message on s1");
    985  1.1.2.2  bouyer 	}
    986  1.1.2.2  bouyer 	if (FD_ISSET(s2, &rfds)) {
    987  1.1.2.2  bouyer 		if (( rv2 = rump_sys_recvfrom(s2, &cf_receive2,
    988  1.1.2.2  bouyer 		    sizeof(cf_receive2), 0,
    989  1.1.2.2  bouyer 		    (struct sockaddr *)&sa, &salen)) < 0) {
    990  1.1.2.2  bouyer 			atf_tc_fail_errno("recvfrom");
    991  1.1.2.2  bouyer 		}
    992  1.1.2.2  bouyer 
    993  1.1.2.2  bouyer 		ATF_CHECK_MSG(rv2 > 0, "short read on socket");
    994  1.1.2.2  bouyer 
    995  1.1.2.2  bouyer 		ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive2,
    996  1.1.2.2  bouyer 		    sizeof(cf_send)) == 0,
    997  1.1.2.2  bouyer 		    "recvfrom (2) packet is not what we sent");
    998  1.1.2.2  bouyer 		ATF_CHECK_MSG(sa.can_family == AF_CAN,
    999  1.1.2.2  bouyer 		    "recvfrom provided wrong %d family", sa.can_family);
   1000  1.1.2.2  bouyer 		ATF_CHECK_MSG(salen == sizeof(sa),
   1001  1.1.2.2  bouyer 		    "recvfrom provided wrong size %d (!= %d)",
   1002  1.1.2.2  bouyer 		    salen, sizeof(sa));
   1003  1.1.2.2  bouyer 		ATF_CHECK_MSG(sa.can_ifindex == ifr.ifr_ifindex,
   1004  1.1.2.2  bouyer 		   "recvfrom provided wrong ifindex %d (!= %d)",
   1005  1.1.2.2  bouyer 		    sa.can_ifindex, ifr.ifr_ifindex);
   1006  1.1.2.2  bouyer 		atf_tc_fail_nonfatal("we got message on s2");
   1007  1.1.2.2  bouyer 	}
   1008  1.1.2.2  bouyer }
   1009  1.1.2.2  bouyer 
   1010  1.1.2.1  bouyer ATF_TP_ADD_TCS(tp)
   1011  1.1.2.1  bouyer {
   1012  1.1.2.1  bouyer 
   1013  1.1.2.1  bouyer         ATF_TP_ADD_TC(tp, canlocreate);
   1014  1.1.2.2  bouyer         ATF_TP_ADD_TC(tp, cannoown);
   1015  1.1.2.1  bouyer         ATF_TP_ADD_TC(tp, canwritelo);
   1016  1.1.2.1  bouyer         ATF_TP_ADD_TC(tp, canwriteunbound);
   1017  1.1.2.1  bouyer         ATF_TP_ADD_TC(tp, cansendtolo);
   1018  1.1.2.1  bouyer         ATF_TP_ADD_TC(tp, cansendtowrite);
   1019  1.1.2.1  bouyer         ATF_TP_ADD_TC(tp, canreadlocal);
   1020  1.1.2.1  bouyer         ATF_TP_ADD_TC(tp, canrecvfrom);
   1021  1.1.2.1  bouyer         ATF_TP_ADD_TC(tp, canbindfilter);
   1022  1.1.2.2  bouyer         ATF_TP_ADD_TC(tp, cannoloop);
   1023  1.1.2.1  bouyer 	return atf_no_error();
   1024  1.1.2.1  bouyer }
   1025  1.1.2.1  bouyer 
   1026