xref: /xnu-10063.121.3/tests/net_ndrv.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1 /*
2  * Copyright (c) 2022 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /*
30  * net_ndrv.c
31  * - test ndrv socket
32  */
33 
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <sys/wait.h>
40 #include <sys/errno.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
43 #include <net/if.h>
44 #include <net/if_media.h>
45 #include <net/if_types.h>
46 #include <net/if_dl.h>
47 #include <net/dlil.h>
48 #include <net/ndrv.h>
49 #include <net/ethernet.h>
50 #include <sys/sockio.h>
51 #include <fcntl.h>
52 #include <stdbool.h>
53 #include <TargetConditionals.h>
54 #include <darwintest_utils.h>
55 
56 static const struct ether_addr multicast_one = {
57 	{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x01 }
58 };
59 
60 static const struct ether_addr multicast_two = {
61 	{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 }
62 };
63 
64 static void
ndrv_socket_do_multicast(int s,const struct ether_addr * multiaddr,bool add)65 ndrv_socket_do_multicast(int s, const struct ether_addr * multiaddr,
66     bool add)
67 {
68 	struct sockaddr_dl      dl;
69 	int                     status;
70 
71 
72 	bzero(&dl, sizeof(dl));
73 	dl.sdl_len = sizeof(dl);
74 	dl.sdl_family = AF_LINK;
75 	dl.sdl_type = IFT_ETHER;
76 	dl.sdl_nlen = 0;
77 	dl.sdl_alen = sizeof(*multiaddr);
78 	bcopy(multiaddr, dl.sdl_data, sizeof(*multiaddr));
79 	status = setsockopt(s, SOL_NDRVPROTO,
80 	    add ? NDRV_ADDMULTICAST : NDRV_DELMULTICAST,
81 	    &dl, dl.sdl_len);
82 	T_ASSERT_POSIX_SUCCESS(status,
83 	    "setsockopt(NDRV_%sMULTICAST)",
84 	    add ? "ADD" : "DEL");
85 }
86 
87 static void
ndrv_socket_add_multicast(int s,const struct ether_addr * multiaddr)88 ndrv_socket_add_multicast(int s, const struct ether_addr * multiaddr)
89 {
90 	ndrv_socket_do_multicast(s, multiaddr, true);
91 }
92 
93 static void
ndrv_socket_remove_multicast(int s,const struct ether_addr * multiaddr)94 ndrv_socket_remove_multicast(int s, const struct ether_addr * multiaddr)
95 {
96 	ndrv_socket_do_multicast(s, multiaddr, false);
97 }
98 
99 static int
ndrv_socket_open(const char * ifname)100 ndrv_socket_open(const char * ifname)
101 {
102 	struct sockaddr_ndrv    ndrv;
103 	int                     s;
104 	int                     status;
105 
106 	s = socket(AF_NDRV, SOCK_RAW, 0);
107 	T_ASSERT_POSIX_SUCCESS(s, "socket(AF_NDRV, SOCK_RAW, 0)");
108 	bzero(&ndrv, sizeof(ndrv));
109 	strlcpy((char *)ndrv.snd_name, ifname, sizeof(ndrv.snd_name));
110 	ndrv.snd_len = sizeof(ndrv);
111 	ndrv.snd_family = AF_NDRV;
112 	status = bind(s, (struct sockaddr *)&ndrv, sizeof(ndrv));
113 	T_ASSERT_POSIX_SUCCESS(status, "bind ndrv socket");
114 	return s;
115 }
116 
117 static void
ndrv_socket_multicast_add_remove(const char * ifname)118 ndrv_socket_multicast_add_remove(const char * ifname)
119 {
120 	int                     s;
121 
122 	/* test for rdar://99667160 */
123 	s = ndrv_socket_open(ifname);
124 	ndrv_socket_add_multicast(s, &multicast_one);
125 	ndrv_socket_add_multicast(s, &multicast_two);
126 	ndrv_socket_remove_multicast(s, &multicast_one);
127 	close(s);
128 }
129 
130 T_DECL(ndrv_socket_multicast_add_remove,
131     "ndrv socket multicast add remove",
132     T_META_ASROOT(true))
133 {
134 	ndrv_socket_multicast_add_remove("lo0");
135 }
136