xref: /freebsd/tests/sys/netgraph/ksocket.c (revision b5daf675efc746611c7cfcd1fa474b8905064c4b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2024 Gleb Smirnoff <glebius@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netgraph.h>
31 #include <netgraph/ng_socket.h>
32 #include <netgraph/ng_ksocket.h>
33 
34 #include <errno.h>
35 
36 #include <atf-c.h>
37 
38 static void
39 hellocheck(int wr, int rd)
40 {
41 	char sbuf[] = "Hello, peer!", rbuf[sizeof(sbuf)];
42 
43 	ATF_REQUIRE(send(wr, sbuf, sizeof(sbuf), 0) == sizeof(sbuf));
44 	ATF_REQUIRE(recv(rd, rbuf, sizeof(rbuf), 0) == sizeof(sbuf));
45 	ATF_REQUIRE(strcmp(sbuf, rbuf) == 0);
46 }
47 
48 #define	OURHOOK	"ks"
49 
50 ATF_TC_WITHOUT_HEAD(udp_connect);
51 ATF_TC_BODY(udp_connect, tc)
52 {
53 	struct ngm_mkpeer mkp = {
54 		.type = NG_KSOCKET_NODE_TYPE,
55 		.ourhook = OURHOOK,
56 		.peerhook = "inet/dgram/udp",
57 	};
58 	struct sockaddr_in sin = {
59 		.sin_family = AF_INET,
60 		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
61 		.sin_len = sizeof(sin),
62 	};
63 	socklen_t slen = sizeof(sin);
64 	int cs, ds, us;
65 
66 	ATF_REQUIRE((us = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
67 	ATF_REQUIRE(bind(us, (struct sockaddr *)&sin, sizeof(sin)) == 0);
68 	ATF_REQUIRE(getsockname(us, (struct sockaddr *)&sin, &slen) == 0);
69 
70 	ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0);
71 	ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp,
72 	    sizeof(mkp)) >= 0);
73 	ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
74 	    NGM_KSOCKET_CONNECT, &sin, sizeof(sin)) >= 0);
75 
76 	hellocheck(ds, us);
77 }
78 
79 ATF_TC_WITHOUT_HEAD(udp_bind);
80 ATF_TC_BODY(udp_bind, tc)
81 {
82 	struct ngm_mkpeer mkp = {
83 		.type = NG_KSOCKET_NODE_TYPE,
84 		.ourhook = OURHOOK,
85 		.peerhook = "inet/dgram/udp",
86 	};
87 	struct sockaddr_in sin = {
88 		.sin_family = AF_INET,
89 		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
90 		.sin_len = sizeof(sin),
91 	};
92 	struct ng_mesg *rep;
93 	int cs, ds, us;
94 
95 	ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0);
96 	ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp,
97 	    sizeof(mkp)) >= 0);
98 	ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
99 	    NGM_KSOCKET_BIND, &sin, sizeof(sin)) >= 0);
100 	ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
101 	    NGM_KSOCKET_GETNAME, NULL, 0) >= 0);
102 	ATF_REQUIRE(NgAllocRecvMsg(cs, &rep, NULL) == sizeof(struct ng_mesg) +
103 	    sizeof(struct sockaddr_in));
104 
105 	ATF_REQUIRE((us = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
106 	ATF_REQUIRE(connect(us, (struct sockaddr *)rep->data,
107 	    sizeof(struct sockaddr_in)) == 0);
108 
109 	hellocheck(us, ds);
110 }
111 
112 ATF_TC_WITHOUT_HEAD(udp6_connect);
113 ATF_TC_BODY(udp6_connect, tc)
114 {
115 	struct ngm_mkpeer mkp = {
116 		.type = NG_KSOCKET_NODE_TYPE,
117 		.ourhook = OURHOOK,
118 		.peerhook = "inet6/dgram/udp6",
119 	};
120 	struct sockaddr_in6 sin6 = {
121 		.sin6_family = AF_INET6,
122 	};
123 	socklen_t slen = sizeof(sin6);
124 	int cs, ds, us;
125 
126 	ATF_REQUIRE((us = socket(PF_INET6, SOCK_DGRAM, 0)) > 0);
127 	ATF_REQUIRE(bind(us, (struct sockaddr *)&sin6, sizeof(sin6)) == 0);
128 	ATF_REQUIRE(getsockname(us, (struct sockaddr *)&sin6, &slen) == 0);
129 
130 	ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0);
131 	ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp,
132 	    sizeof(mkp)) >= 0);
133 	ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
134 	    NGM_KSOCKET_CONNECT, &sin6, sizeof(sin6)) >= 0);
135 
136 	hellocheck(ds, us);
137 }
138 
139 
140 ATF_TC_WITHOUT_HEAD(udp6_bind);
141 ATF_TC_BODY(udp6_bind, tc)
142 {
143 	struct ngm_mkpeer mkp = {
144 		.type = NG_KSOCKET_NODE_TYPE,
145 		.ourhook = OURHOOK,
146 		.peerhook = "inet6/dgram/udp6",
147 	};
148 	struct sockaddr_in6 sin6 = {
149 		.sin6_family = AF_INET6,
150 		.sin6_len = sizeof(sin6),
151 	};
152 	struct ng_mesg *rep;
153 	int cs, ds, us;
154 
155 	ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0);
156 	ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp,
157 	    sizeof(mkp)) >= 0);
158 	ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
159 	    NGM_KSOCKET_BIND, &sin6, sizeof(sin6)) >= 0);
160 	ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
161 	    NGM_KSOCKET_GETNAME, NULL, 0) >= 0);
162 	ATF_REQUIRE(NgAllocRecvMsg(cs, &rep, NULL) == sizeof(struct ng_mesg) +
163 	    sizeof(struct sockaddr_in6));
164 
165 	ATF_REQUIRE((us = socket(PF_INET6, SOCK_DGRAM, 0)) > 0);
166 	ATF_REQUIRE(connect(us, (struct sockaddr *)rep->data,
167 			sizeof(struct sockaddr_in6)) == 0);
168 
169 	hellocheck(us, ds);
170 }
171 
172 ATF_TP_ADD_TCS(tp)
173 {
174 	ATF_TP_ADD_TC(tp, udp_connect);
175 	ATF_TP_ADD_TC(tp, udp_bind);
176 	ATF_TP_ADD_TC(tp, udp6_connect);
177 	ATF_TP_ADD_TC(tp, udp6_bind);
178 
179 	return (atf_no_error());
180 }
181