]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/ifconfig/af_link.c
Import tzdata 2017c
[FreeBSD/FreeBSD.git] / sbin / ifconfig / af_link.c
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 #include <unistd.h>
46
47 #include <net/if_dl.h>
48 #include <net/if_types.h>
49 #include <net/ethernet.h>
50
51 #include "ifconfig.h"
52
53 static struct ifreq link_ridreq;
54
55 extern char *f_ether;
56
57 static void
58 link_status(int s __unused, const struct ifaddrs *ifa)
59 {
60         /* XXX no const 'cuz LLADDR is defined wrong */
61         struct sockaddr_dl *sdl = (struct sockaddr_dl *) ifa->ifa_addr;
62         char *ether_format, *format_char;
63
64         if (sdl != NULL && sdl->sdl_alen > 0) {
65                 if ((sdl->sdl_type == IFT_ETHER ||
66                     sdl->sdl_type == IFT_L2VLAN ||
67                     sdl->sdl_type == IFT_BRIDGE) &&
68                     sdl->sdl_alen == ETHER_ADDR_LEN) {
69                         ether_format = ether_ntoa((struct ether_addr *)LLADDR(sdl));
70                         if (f_ether != NULL && strcmp(f_ether, "dash") == 0) {
71                                 for (format_char = strchr(ether_format, ':');
72                                     format_char != NULL; 
73                                     format_char = strchr(ether_format, ':'))
74                                         *format_char = '-';
75                         }
76                         printf("\tether %s\n", ether_format);
77                 } else {
78                         int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
79
80                         printf("\tlladdr %s\n", link_ntoa(sdl) + n);
81                 }
82                 /* Best-effort (i.e. failures are silent) to get original
83                  * hardware address, as read by NIC driver at attach time. Only
84                  * applies to Ethernet NICs (IFT_ETHER). However, laggX
85                  * interfaces claim to be IFT_ETHER, and re-type their component
86                  * Ethernet NICs as IFT_IEEE8023ADLAG. So, check for both. If
87                  * the MAC is zeroed, then it's actually a lagg.
88                  */
89                 if ((sdl->sdl_type == IFT_ETHER ||
90                     sdl->sdl_type == IFT_IEEE8023ADLAG) &&
91                     sdl->sdl_alen == ETHER_ADDR_LEN) {
92                         struct ifreq ifr;
93                         int sock_hw;
94                         int rc;
95                         static const u_char laggaddr[6] = {0};
96
97                         strncpy(ifr.ifr_name, ifa->ifa_name,
98                             sizeof(ifr.ifr_name));
99                         memcpy(&ifr.ifr_addr, ifa->ifa_addr,
100                             sizeof(ifa->ifa_addr->sa_len));
101                         ifr.ifr_addr.sa_family = AF_LOCAL;
102                         if ((sock_hw = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0) {
103                                 warn("socket(AF_LOCAL,SOCK_DGRAM)");
104                                 return;
105                         }
106                         rc = ioctl(sock_hw, SIOCGHWADDR, &ifr);
107                         close(sock_hw);
108                         if (rc != 0) {
109                                 return;
110                         }
111
112                         /*
113                          * If this is definitely a lagg device or the hwaddr
114                          * matches the link addr, don't bother.
115                          */
116                         if (memcmp(ifr.ifr_addr.sa_data, laggaddr,
117                             sdl->sdl_alen) == 0 ||
118                             memcmp(ifr.ifr_addr.sa_data, LLADDR(sdl),
119                             sdl->sdl_alen) == 0) {
120                                 return;
121                         }
122                         ether_format = ether_ntoa((const struct ether_addr *)
123                             &ifr.ifr_addr.sa_data);
124                         if (f_ether != NULL && strcmp(f_ether, "dash") == 0) {
125                                 for (format_char = strchr(ether_format, ':');
126                                     format_char != NULL; 
127                                     format_char = strchr(ether_format, ':'))
128                                         *format_char = '-';
129                         }
130                         printf("\thwaddr %s\n", ether_format);
131                 }
132         }
133 }
134
135 static void
136 link_getaddr(const char *addr, int which)
137 {
138         char *temp;
139         struct sockaddr_dl sdl;
140         struct sockaddr *sa = &link_ridreq.ifr_addr;
141
142         if (which != ADDR)
143                 errx(1, "can't set link-level netmask or broadcast");
144         if (!strcmp(addr, "random")) {
145                 sdl.sdl_len = sizeof(sdl);
146                 sdl.sdl_alen = ETHER_ADDR_LEN;
147                 sdl.sdl_nlen = 0;
148                 sdl.sdl_family = AF_LINK;
149                 arc4random_buf(&sdl.sdl_data, ETHER_ADDR_LEN);
150                 /* Non-multicast and claim it is locally administered. */
151                 sdl.sdl_data[0] &= 0xfc;
152                 sdl.sdl_data[0] |= 0x02;
153         } else {
154                 if ((temp = malloc(strlen(addr) + 2)) == NULL)
155                         errx(1, "malloc failed");
156                 temp[0] = ':';
157                 strcpy(temp + 1, addr);
158                 sdl.sdl_len = sizeof(sdl);
159                 link_addr(temp, &sdl);
160                 free(temp);
161         }
162         if (sdl.sdl_alen > sizeof(sa->sa_data))
163                 errx(1, "malformed link-level address");
164         sa->sa_family = AF_LINK;
165         sa->sa_len = sdl.sdl_alen;
166         bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
167 }
168
169 static struct afswtch af_link = {
170         .af_name        = "link",
171         .af_af          = AF_LINK,
172         .af_status      = link_status,
173         .af_getaddr     = link_getaddr,
174         .af_aifaddr     = SIOCSIFLLADDR,
175         .af_addreq      = &link_ridreq,
176 };
177 static struct afswtch af_ether = {
178         .af_name        = "ether",
179         .af_af          = AF_LINK,
180         .af_status      = link_status,
181         .af_getaddr     = link_getaddr,
182         .af_aifaddr     = SIOCSIFLLADDR,
183         .af_addreq      = &link_ridreq,
184 };
185 static struct afswtch af_lladdr = {
186         .af_name        = "lladdr",
187         .af_af          = AF_LINK,
188         .af_status      = link_status,
189         .af_getaddr     = link_getaddr,
190         .af_aifaddr     = SIOCSIFLLADDR,
191         .af_addreq      = &link_ridreq,
192 };
193
194 static __constructor void
195 link_ctor(void)
196 {
197         af_register(&af_link);
198         af_register(&af_ether);
199         af_register(&af_lladdr);
200 }