]>
Commit | Line | Data |
---|---|---|
8d01c344 A |
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 | * 4. 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: src/sbin/ifconfig/af_link.c,v 1.4.6.1 2008/11/25 02:59:29 kensmith Exp $"; | |
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 | static void | |
55 | link_status(int s __unused, const struct ifaddrs *ifa) | |
56 | { | |
57 | /* XXX no const 'cuz LLADDR is defined wrong */ | |
58 | struct sockaddr_dl *sdl = (struct sockaddr_dl *) ifa->ifa_addr; | |
59 | ||
60 | if (sdl != NULL && sdl->sdl_alen > 0) { | |
61 | #ifdef notyet | |
62 | if (sdl->sdl_type == IFT_ETHER && | |
63 | sdl->sdl_alen == ETHER_ADDR_LEN) | |
64 | printf("\tether %s\n", | |
65 | ether_ntoa((struct ether_addr *)LLADDR(sdl))); | |
66 | else { | |
67 | int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; | |
68 | ||
69 | printf("\tlladdr %s\n", link_ntoa(sdl) + n); | |
70 | } | |
71 | #else | |
72 | char *cp = (char *)LLADDR(sdl); | |
73 | int n = sdl->sdl_alen; | |
74 | ||
75 | if (sdl->sdl_type == IFT_ETHER) | |
76 | printf ("\tether "); | |
77 | else | |
78 | printf ("\tlladdr "); | |
79 | while (--n >= 0) | |
80 | printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' '); | |
81 | putchar('\n'); | |
82 | #endif | |
83 | } | |
84 | } | |
85 | ||
86 | static void | |
87 | link_getaddr(const char *addr, int which) | |
88 | { | |
89 | char *temp; | |
90 | struct sockaddr_dl sdl; | |
91 | struct sockaddr *sa = &link_ridreq.ifr_addr; | |
92 | ||
93 | if (which != ADDR) | |
94 | errx(1, "can't set link-level netmask or broadcast"); | |
95 | if ((temp = malloc(strlen(addr) + 2)) == NULL) | |
96 | errx(1, "malloc failed"); | |
97 | temp[0] = ':'; | |
98 | strcpy(temp + 1, addr); | |
99 | sdl.sdl_len = sizeof(sdl); | |
100 | link_addr(temp, &sdl); | |
101 | free(temp); | |
102 | if (sdl.sdl_alen > sizeof(sa->sa_data)) | |
103 | errx(1, "malformed link-level address"); | |
104 | sa->sa_family = AF_LINK; | |
105 | sa->sa_len = sdl.sdl_alen; | |
106 | bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen); | |
107 | } | |
108 | ||
109 | static struct afswtch af_link = { | |
110 | .af_name = "link", | |
111 | .af_af = AF_LINK, | |
112 | .af_status = link_status, | |
113 | .af_getaddr = link_getaddr, | |
114 | .af_aifaddr = SIOCSIFLLADDR, | |
115 | .af_addreq = &link_ridreq, | |
116 | }; | |
117 | static struct afswtch af_ether = { | |
118 | .af_name = "ether", | |
119 | .af_af = AF_LINK, | |
120 | .af_status = link_status, | |
121 | .af_getaddr = link_getaddr, | |
122 | .af_aifaddr = SIOCSIFLLADDR, | |
123 | .af_addreq = &link_ridreq, | |
124 | }; | |
125 | static struct afswtch af_lladdr = { | |
126 | .af_name = "lladdr", | |
127 | .af_af = AF_LINK, | |
128 | .af_status = link_status, | |
129 | .af_getaddr = link_getaddr, | |
130 | .af_aifaddr = SIOCSIFLLADDR, | |
131 | .af_addreq = &link_ridreq, | |
132 | }; | |
133 | ||
134 | static __constructor void | |
135 | link_ctor(void) | |
136 | { | |
137 | af_register(&af_link); | |
138 | af_register(&af_ether); | |
139 | af_register(&af_lladdr); | |
140 | } |