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