xref: /minix/tests/net/if/t_compat.c (revision ebfedea0)
1 /*	$NetBSD: t_compat.c,v 1.1 2010/11/07 19:53:42 pooka 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 	/* query kernel for iface bcast */
69         RL(fd = rump_sys_socket(AF_INET, SOCK_DGRAM, 0));
70         RL(rump_sys_ioctl(fd, OOSIOCGIFBRDADDR, &ifreq));
71 
72 	/* make sure we got what we deserve */
73         sin = (struct sockaddr_in *)&ifreq.ifr_broadaddr;
74 	ATF_REQUIRE_EQ(sin->sin_addr.s_addr, htonl(0x0107ffff));
75         rump_sys_close(fd);
76 }
77 
78 ATF_TP_ADD_TCS(tp)
79 {
80 
81 	ATF_TP_ADD_TC(tp, OOSIOCGIFBRDADDR);
82 	return atf_no_error();
83 }
84