2 * Copyright (c) 2007 Bruce M. Simpson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <sys/ioctl.h>
36 #include <net/if_dl.h>
37 #include <net/if_types.h>
38 #include <net/ethernet.h>
50 static int dorandom = 0;
51 static int verbose = 0;
52 static char *ifname = NULL;
55 * The test tool exercises IP-level socket options by interrogating the
56 * getsockopt()/setsockopt() APIs. It does not currently test that the
57 * intended semantics of each option are implemented (i.e., that setting IP
58 * options on the socket results in packets with the desired IP options in
63 * get_socket() is a wrapper function that returns a socket of the specified
64 * type, and created with or without restored root privilege (if running
65 * with a real uid of root and an effective uid of some other user). This
66 * us to test whether the same rights are granted using a socket with a
67 * privileged cached credential vs. a socket with a regular credential.
70 #define PRIV_GETROOT 1
72 get_socket_unpriv(int type)
75 return (socket(PF_INET, type, 0));
79 get_socket_priv(int type)
85 errx(-1, "get_sock_priv: running without real uid 0");
89 err(-1, "get_sock_priv: seteuid(0)");
91 sock = socket(PF_INET, type, 0);
93 if (seteuid(olduid) < 0)
94 err(-1, "get_sock_priv: seteuid(%d)", olduid);
100 get_socket(int type, int priv)
104 return (get_socket_priv(type));
106 return (get_socket_unpriv(type));
110 struct sockaddr_storage ss;
112 struct sockaddr_dl sdl;
114 typedef union sockunion sockunion_t;
117 test_ether_multi(int sock)
120 struct sockaddr_dl *dlp;
121 struct ether_addr ea;
122 struct ifmaddrs *ifma, *ifmap;
125 /* Choose an 802 multicast address. */
133 ea.octet[2] = ((mac4 >> 24 & 0xFF));
134 ea.octet[3] = ((mac4 >> 16 & 0xFF));
135 ea.octet[4] = ((mac4 >> 8 & 0xFF));
136 ea.octet[5] = (mac4 & 0xFF);
138 struct ether_addr *nep = ether_aton("01:80:DE:FA:CA:7E");
142 /* Fill out ifreq, and fill out 802 group address. */
143 memset(&ifr, 0, sizeof(struct ifreq));
144 strlcpy(&ifr.ifr_name[0], ifname, IFNAMSIZ);
145 dlp = (struct sockaddr_dl *)&ifr.ifr_addr;
146 memset(dlp, 0, sizeof(struct sockaddr_dl));
147 dlp->sdl_len = sizeof(struct sockaddr_dl);
148 dlp->sdl_family = AF_LINK;
149 dlp->sdl_alen = sizeof(struct ether_addr);
150 memcpy(LLADDR(dlp), &ea, sizeof(struct ether_addr));
152 /* Join an 802 group. */
153 if (ioctl(sock, SIOCADDMULTI, &ifr) < 0) {
154 warn("can't add ethernet multicast membership");
158 /* Check that we joined the group by calling getifmaddrs(). */
160 if (getifmaddrs(&ifmap) != 0) {
161 warn("getifmaddrs()");
163 for (ifma = ifmap; ifma; ifma = ifma->ifma_next) {
164 sockunion_t *psa = (sockunion_t *)ifma->ifma_addr;
165 if (ifma->ifma_name == NULL || psa == NULL)
168 if (psa->sa.sa_family != AF_LINK ||
169 psa->sdl.sdl_alen != ETHER_ADDR_LEN)
172 if (bcmp(LLADDR(&psa->sdl), LLADDR(dlp),
173 ETHER_ADDR_LEN) == 0) {
181 warnx("group membership for %s not returned by getifmaddrs()",
185 /* Fill out ifreq, and fill out 802 group address. */
186 memset(&ifr, 0, sizeof(struct ifreq));
187 strlcpy(&ifr.ifr_name[0], ifname, IFNAMSIZ);
188 dlp = (struct sockaddr_dl *)&ifr.ifr_addr;
189 memset(dlp, 0, sizeof(struct sockaddr_dl));
190 dlp->sdl_len = sizeof(struct sockaddr_dl);
191 dlp->sdl_family = AF_LINK;
192 dlp->sdl_alen = sizeof(struct ether_addr);
193 memcpy(LLADDR(dlp), &ea, sizeof(struct ether_addr));
195 /* Leave an 802 group. */
196 if (ioctl(sock, SIOCDELMULTI, &ifr) < 0)
197 warn("can't delete ethernet multicast membership");
206 sock = get_socket(SOCK_DGRAM, 0);
208 err(-1, "get_socket(SOCK_DGRAM) for test_ether_multi()", priv);
209 test_ether_multi(sock);
217 fprintf(stderr, "usage: ethermulti -i ifname [-r] [-v]\n");
222 main(int argc, char *argv[])
226 while ((ch = getopt(argc, argv, "i:rv")) != -1) {
232 dorandom = 1; /* introduce non-determinism */
245 if (geteuid() != 0) {
246 errx(1, "Not running as root, can't run tests as non-root");
250 "Running tests with ruid %d euid %d sock uid 0\n",
251 getuid(), geteuid());
252 testsuite(PRIV_ASIS);
254 printf("ok 1 - ethermulti\n");