xref: /freebsd/contrib/netbsd-tests/net/if/t_compat.c (revision aa24f48b361effe51163877d84f1b70d32b77e04)
1 /*	$NetBSD: t_compat.c,v 1.4 2016/11/12 15:12:59 kre Exp $	*/
2 
3 #include <sys/socket.h>
4 #include <sys/ioctl.h>
5 #include <net/if.h>
6 #include <netinet/in.h>
7 
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 #include <rump/rump.h>
13 #include <rump/rump_syscalls.h>
14 
15 #include "../config/netconfig.c"
16 
17 /*
18  * Test for stack smashing in compat ioctl handling.  Adapted as an
19  * atf test from code provided by Onno van der Linden in PR kern/44054
20  */
21 
22 struct oifreq {
23         char    ifr_name[IFNAMSIZ];             /* if name, e.g. "en0" */
24         union {
25                 struct  sockaddr ifru_addr;
26                 struct  sockaddr ifru_dstaddr;
27                 struct  sockaddr ifru_broadaddr;
28                 short   ifru_flags;
29                 int     ifru_metric;
30                 int     ifru_mtu;
31                 int     ifru_dlt;
32                 u_int   ifru_value;
33                 void *  ifru_data;
34                 struct {
35                         uint32_t        b_buflen;
36                         void            *b_buf;
37                 } ifru_b;
38         } ifr_ifru;
39 };
40 #define OOSIOCGIFBRDADDR _IOWR('i', 18, struct oifreq)
41 
42 ATF_TC(OOSIOCGIFBRDADDR);
43 ATF_TC_HEAD(OOSIOCGIFBRDADDR, tc)
44 {
45 
46 	atf_tc_set_md_var(tc, "descr", "Checks that OOSIOCGIFBRDADDR works "
47 	    "(PR kern/44054)");
48 }
49 
50 ATF_TC_BODY(OOSIOCGIFBRDADDR, tc)
51 {
52         int fd, ifnum;
53         struct oifreq ifreq;
54         struct sockaddr_in *sin;
55 	int rv;
56 
57         memset(&ifreq,'\0',sizeof ifreq);
58 
59 	rump_init();
60 
61 	/* create an interface and give it netmask 0xffff0000 */
62 	rv = rump_pub_shmif_create("bus", &ifnum);
63 	if (rv)
64 		atf_tc_fail("failed to create shmif: %s", strerror(rv));
65 	sprintf(ifreq.ifr_name, "shmif%d", ifnum);
66 	netcfg_rump_if(ifreq.ifr_name, "1.7.64.10", "255.255.0.0");
67 
68 	atf_tc_expect_fail("PR kern/51610: rump does not include COMPAT_43");
69 
70 	/* query kernel for iface bcast */
71         RL(fd = rump_sys_socket(AF_INET, SOCK_DGRAM, 0));
72         RL(rump_sys_ioctl(fd, OOSIOCGIFBRDADDR, &ifreq));
73 
74 	/* make sure we got what we deserve */
75         sin = (struct sockaddr_in *)&ifreq.ifr_broadaddr;
76 	ATF_REQUIRE_EQ(sin->sin_addr.s_addr, htonl(0x0107ffff));
77         rump_sys_close(fd);
78 }
79 
80 ATF_TP_ADD_TCS(tp)
81 {
82 
83 	ATF_TP_ADD_TC(tp, OOSIOCGIFBRDADDR);
84 	return atf_no_error();
85 }
86