1 //#include <sys/param.h>
5 #include <netinet/in.h>
8 #include <sys/linker.h>
11 #include <sys/socket.h>
12 #include <sys/sockio.h>
16 #define OVPN_NEW_PEER _IO ('D', 1)
21 uint32_t addr = htonl(INADDR_LOOPBACK);
24 nvl = nvlist_create(0);
26 nvlist_add_number(nvl, "af", AF_INET);
27 nvlist_add_binary(nvl, "address", &addr, 4);
28 nvlist_add_number(nvl, "port", 1024);
33 static char ovpn_ifname[IFNAMSIZ];
37 create_interface(int fd)
42 bzero(&ifr, sizeof(ifr));
44 /* Create ovpnx first, then rename it. */
45 snprintf(ifr.ifr_name, IFNAMSIZ, "ovpn");
46 ret = ioctl(fd, SIOCIFCREATE2, &ifr);
50 snprintf(ovpn_ifname, IFNAMSIZ, "%s", ifr.ifr_name);
51 printf("Created %s\n", ovpn_ifname);
57 destroy_interface(int fd)
62 if (ovpn_ifname[0] == 0)
65 printf("Destroy %s\n", ovpn_ifname);
67 bzero(&ifr, sizeof(ifr));
68 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ovpn_ifname);
70 ret = ioctl(fd, SIOCIFDESTROY, &ifr);
72 atf_tc_fail("Failed to destroy interface");
77 ATF_TC_WITH_CLEANUP(tcp);
80 atf_tc_set_md_var(tc, "require.user", "root");
86 struct sockaddr_in sock_in;
90 /* Ensure the module is loaded. */
91 if (kldfind("if_ovpn") == -1 && errno == ENOENT)
92 atf_tc_skip("if_ovpn not loaded");
94 ovpn_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
96 /* Kick off a connect so there's a local address set, which we need for
97 * ovpn_new_peer() to get to the critical point. */
98 bzero(&sock_in, sizeof(sock_in));
99 sock_in.sin_family = AF_INET;
100 sock_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
101 sock_in.sin_port = htons(1024);
102 connect(ovpn_fd, (struct sockaddr *)&sock_in, sizeof(sock_in));
104 ret = create_interface(ovpn_fd);
106 atf_tc_fail("Failed to create interface");
108 nvl = nvlist_create(0);
110 nvlist_add_number(nvl, "peerid", 0);
111 nvlist_add_number(nvl, "fd", ovpn_fd);
112 nvlist_add_nvlist(nvl, "remote", fake_sockaddr());
114 bzero(&drv, sizeof(drv));
115 snprintf(drv.ifd_name, IFNAMSIZ, "%s", ovpn_ifname);
116 drv.ifd_cmd = OVPN_NEW_PEER;
117 drv.ifd_data = nvlist_pack(nvl, &drv.ifd_len);
119 ret = ioctl(ovpn_fd, SIOCSDRVSPEC, &drv);
120 ATF_CHECK_EQ(ret, -1);
121 ATF_CHECK_EQ(errno, EPROTOTYPE);
124 ATF_TC_CLEANUP(tcp, tc)
126 destroy_interface(ovpn_fd);
132 ATF_TP_ADD_TC(tp, tcp);
134 return (atf_no_error());