xref: /freebsd/sbin/ifconfig/af_link.c (revision 076ad2f836d5f49dc1375f1677335a48fe0d4b82)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char rcsid[] =
32   "$FreeBSD$";
33 #endif /* not lint */
34 
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <net/if.h>
39 
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ifaddrs.h>
45 
46 #include <net/if_dl.h>
47 #include <net/if_types.h>
48 #include <net/ethernet.h>
49 
50 #include "ifconfig.h"
51 
52 static struct ifreq link_ridreq;
53 
54 extern char *f_ether;
55 
56 static void
57 link_status(int s __unused, const struct ifaddrs *ifa)
58 {
59 	/* XXX no const 'cuz LLADDR is defined wrong */
60 	struct sockaddr_dl *sdl = (struct sockaddr_dl *) ifa->ifa_addr;
61 	char *ether_format, *format_char;
62 
63 	if (sdl != NULL && sdl->sdl_alen > 0) {
64 		if ((sdl->sdl_type == IFT_ETHER ||
65 		    sdl->sdl_type == IFT_L2VLAN ||
66 		    sdl->sdl_type == IFT_BRIDGE) &&
67 		    sdl->sdl_alen == ETHER_ADDR_LEN) {
68 			ether_format = ether_ntoa((struct ether_addr *)LLADDR(sdl));
69 			if (f_ether != NULL && strcmp(f_ether, "dash") == 0) {
70 				for (format_char = strchr(ether_format, ':');
71 				    format_char != NULL;
72 				    format_char = strchr(ether_format, ':'))
73 					*format_char = '-';
74 			}
75 			printf("\tether %s\n", ether_format);
76 		} else {
77 			int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
78 
79 			printf("\tlladdr %s\n", link_ntoa(sdl) + n);
80 		}
81 	}
82 }
83 
84 static void
85 link_getaddr(const char *addr, int which)
86 {
87 	char *temp;
88 	struct sockaddr_dl sdl;
89 	struct sockaddr *sa = &link_ridreq.ifr_addr;
90 
91 	if (which != ADDR)
92 		errx(1, "can't set link-level netmask or broadcast");
93 	if (!strcmp(addr, "random")) {
94 		sdl.sdl_len = sizeof(sdl);
95 		sdl.sdl_alen = ETHER_ADDR_LEN;
96 		sdl.sdl_nlen = 0;
97 		sdl.sdl_family = AF_LINK;
98 		arc4random_buf(&sdl.sdl_data, ETHER_ADDR_LEN);
99 		/* Non-multicast and claim it is locally administered. */
100 		sdl.sdl_data[0] &= 0xfc;
101 		sdl.sdl_data[0] |= 0x02;
102 	} else {
103 		if ((temp = malloc(strlen(addr) + 2)) == NULL)
104 			errx(1, "malloc failed");
105 		temp[0] = ':';
106 		strcpy(temp + 1, addr);
107 		sdl.sdl_len = sizeof(sdl);
108 		link_addr(temp, &sdl);
109 		free(temp);
110 	}
111 	if (sdl.sdl_alen > sizeof(sa->sa_data))
112 		errx(1, "malformed link-level address");
113 	sa->sa_family = AF_LINK;
114 	sa->sa_len = sdl.sdl_alen;
115 	bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
116 }
117 
118 static struct afswtch af_link = {
119 	.af_name	= "link",
120 	.af_af		= AF_LINK,
121 	.af_status	= link_status,
122 	.af_getaddr	= link_getaddr,
123 	.af_aifaddr	= SIOCSIFLLADDR,
124 	.af_addreq	= &link_ridreq,
125 };
126 static struct afswtch af_ether = {
127 	.af_name	= "ether",
128 	.af_af		= AF_LINK,
129 	.af_status	= link_status,
130 	.af_getaddr	= link_getaddr,
131 	.af_aifaddr	= SIOCSIFLLADDR,
132 	.af_addreq	= &link_ridreq,
133 };
134 static struct afswtch af_lladdr = {
135 	.af_name	= "lladdr",
136 	.af_af		= AF_LINK,
137 	.af_status	= link_status,
138 	.af_getaddr	= link_getaddr,
139 	.af_aifaddr	= SIOCSIFLLADDR,
140 	.af_addreq	= &link_ridreq,
141 };
142 
143 static __constructor void
144 link_ctor(void)
145 {
146 	af_register(&af_link);
147 	af_register(&af_ether);
148 	af_register(&af_lladdr);
149 }
150