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