device.c revision 1.6 1 1.6 christos /* $NetBSD: device.c,v 1.6 2003/04/20 00:17:22 christos Exp $ */
2 1.2 thorpej
3 1.1 cjs /*
4 1.1 cjs * Copyright (c) 1993-95 Mats O Jansson. All rights reserved.
5 1.1 cjs *
6 1.1 cjs * Redistribution and use in source and binary forms, with or without
7 1.1 cjs * modification, are permitted provided that the following conditions
8 1.1 cjs * are met:
9 1.1 cjs * 1. Redistributions of source code must retain the above copyright
10 1.1 cjs * notice, this list of conditions and the following disclaimer.
11 1.1 cjs * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 cjs * notice, this list of conditions and the following disclaimer in the
13 1.1 cjs * documentation and/or other materials provided with the distribution.
14 1.1 cjs * 3. All advertising materials mentioning features or use of this software
15 1.1 cjs * must display the following acknowledgement:
16 1.1 cjs * This product includes software developed by Mats O Jansson.
17 1.1 cjs * 4. The name of the author may not be used to endorse or promote products
18 1.1 cjs * derived from this software without specific prior written permission.
19 1.1 cjs *
20 1.1 cjs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 cjs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 cjs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 cjs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 cjs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 cjs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 cjs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 cjs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 cjs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 cjs * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 cjs */
31 1.1 cjs
32 1.3 lukem #include <sys/cdefs.h>
33 1.3 lukem #ifndef lint
34 1.6 christos __RCSID("$NetBSD: device.c,v 1.6 2003/04/20 00:17:22 christos Exp $");
35 1.1 cjs #endif
36 1.1 cjs
37 1.1 cjs #include "os.h"
38 1.3 lukem #include "common.h"
39 1.3 lukem #include "device.h"
40 1.3 lukem #include "mopdef.h"
41 1.3 lukem #include "pf.h"
42 1.6 christos #include "log.h"
43 1.1 cjs
44 1.1 cjs struct if_info *iflist; /* Interface List */
45 1.1 cjs
46 1.3 lukem void deviceOpen __P((char *, u_short, int));
47 1.1 cjs
48 1.1 cjs #ifdef DEV_NEW_CONF
49 1.1 cjs /*
50 1.1 cjs * Return ethernet adress for interface
51 1.1 cjs */
52 1.1 cjs
53 1.1 cjs void
54 1.1 cjs deviceEthAddr(ifname, eaddr)
55 1.1 cjs char *ifname;
56 1.1 cjs u_char *eaddr;
57 1.1 cjs {
58 1.1 cjs char inbuf[8192];
59 1.1 cjs struct ifconf ifc;
60 1.1 cjs struct ifreq *ifr;
61 1.1 cjs struct sockaddr_dl *sdl;
62 1.1 cjs int fd;
63 1.1 cjs int i, len;
64 1.1 cjs
65 1.1 cjs /* We cannot use SIOCGIFADDR on the BPF descriptor.
66 1.1 cjs We must instead get all the interfaces with SIOCGIFCONF
67 1.1 cjs and find the right one. */
68 1.1 cjs
69 1.1 cjs /* Use datagram socket to get Ethernet address. */
70 1.6 christos if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
71 1.6 christos mopLogErr("deviceEthAddr: socket");
72 1.1 cjs
73 1.1 cjs ifc.ifc_len = sizeof(inbuf);
74 1.1 cjs ifc.ifc_buf = inbuf;
75 1.1 cjs if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
76 1.6 christos ifc.ifc_len < sizeof(struct ifreq))
77 1.6 christos mopLogErr("deviceEthAddr: SIOGIFCONF");
78 1.1 cjs ifr = ifc.ifc_req;
79 1.1 cjs for (i = 0; i < ifc.ifc_len;
80 1.1 cjs i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
81 1.1 cjs len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
82 1.1 cjs sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
83 1.1 cjs if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
84 1.1 cjs sdl->sdl_alen != 6)
85 1.1 cjs continue;
86 1.1 cjs if (!strncmp(ifr->ifr_name, ifname, sizeof(ifr->ifr_name))) {
87 1.3 lukem memmove((caddr_t)eaddr, (caddr_t)LLADDR(sdl), 6);
88 1.1 cjs return;
89 1.1 cjs }
90 1.1 cjs }
91 1.1 cjs
92 1.6 christos mopLogErrX("deviceEthAddr: Never saw interface `%s'!", ifname);
93 1.1 cjs }
94 1.1 cjs #endif /* DEV_NEW_CONF */
95 1.1 cjs
96 1.1 cjs void
97 1.1 cjs deviceOpen(ifname, proto, trans)
98 1.1 cjs char *ifname;
99 1.1 cjs u_short proto;
100 1.1 cjs int trans;
101 1.1 cjs {
102 1.1 cjs struct if_info *p, tmp;
103 1.1 cjs
104 1.5 itojun strlcpy(tmp.if_name, ifname, sizeof(tmp.if_name));
105 1.1 cjs tmp.iopen = pfInit;
106 1.1 cjs
107 1.1 cjs switch (proto) {
108 1.1 cjs case MOP_K_PROTO_RC:
109 1.1 cjs tmp.read = mopReadRC;
110 1.1 cjs tmp.fd = mopOpenRC(&tmp, trans);
111 1.1 cjs break;
112 1.1 cjs case MOP_K_PROTO_DL:
113 1.1 cjs tmp.read = mopReadDL;
114 1.1 cjs tmp.fd = mopOpenDL(&tmp, trans);
115 1.1 cjs break;
116 1.1 cjs default:
117 1.1 cjs break;
118 1.1 cjs }
119 1.1 cjs
120 1.1 cjs if (tmp.fd != -1) {
121 1.1 cjs
122 1.1 cjs p = (struct if_info *)malloc(sizeof(*p));
123 1.6 christos if (p == 0)
124 1.6 christos mopLogErr("deviceOpen: malloc");
125 1.1 cjs
126 1.1 cjs p->next = iflist;
127 1.1 cjs iflist = p;
128 1.1 cjs
129 1.1 cjs strcpy(p->if_name,tmp.if_name);
130 1.1 cjs p->iopen = tmp.iopen;
131 1.1 cjs p->write = pfWrite;
132 1.1 cjs p->read = tmp.read;
133 1.3 lukem memset((char *)p->eaddr, 0, sizeof(p->eaddr));
134 1.1 cjs p->fd = tmp.fd;
135 1.1 cjs
136 1.1 cjs #ifdef DEV_NEW_CONF
137 1.1 cjs deviceEthAddr(p->if_name,&p->eaddr[0]);
138 1.1 cjs #else
139 1.1 cjs p->eaddr[0]= tmp.eaddr[0];
140 1.1 cjs p->eaddr[1]= tmp.eaddr[1];
141 1.1 cjs p->eaddr[2]= tmp.eaddr[2];
142 1.1 cjs p->eaddr[3]= tmp.eaddr[3];
143 1.1 cjs p->eaddr[4]= tmp.eaddr[4];
144 1.1 cjs p->eaddr[5]= tmp.eaddr[5];
145 1.1 cjs #endif /* DEV_NEW_CONF */
146 1.1 cjs
147 1.1 cjs }
148 1.1 cjs }
149 1.1 cjs
150 1.1 cjs void
151 1.1 cjs deviceInitOne(ifname)
152 1.1 cjs char *ifname;
153 1.1 cjs {
154 1.1 cjs char interface[IFNAME_SIZE];
155 1.1 cjs struct if_info *p;
156 1.1 cjs int trans;
157 1.1 cjs #ifdef _AIX
158 1.1 cjs char dev[IFNAME_SIZE];
159 1.1 cjs int unit,j;
160 1.1 cjs
161 1.1 cjs unit = 0;
162 1.1 cjs for (j = 0; j < strlen(ifname); j++) {
163 1.1 cjs if (isalpha(ifname[j])) {
164 1.1 cjs dev[j] = ifname[j];
165 1.1 cjs } else {
166 1.1 cjs if (isdigit(ifname[j])) {
167 1.1 cjs unit = unit*10 + ifname[j] - '0';
168 1.1 cjs dev[j] = '\0';
169 1.1 cjs }
170 1.1 cjs }
171 1.1 cjs }
172 1.1 cjs
173 1.1 cjs if ((strlen(dev) == 2) &&
174 1.1 cjs (dev[0] == 'e') &&
175 1.1 cjs ((dev[1] == 'n') || (dev[1] == 't'))) {
176 1.4 itojun snprintf(interface, sizeof(interface), "ent%d\0", unit);
177 1.1 cjs } else {
178 1.4 itojun snprintf(interface, sizeof(interface), "%s%d\0", dev, unit);
179 1.1 cjs }
180 1.1 cjs #else
181 1.4 itojun snprintf(interface, sizeof(interface), "%s", ifname);
182 1.1 cjs #endif /* _AIX */
183 1.1 cjs
184 1.1 cjs /* Ok, init it just once */
185 1.1 cjs
186 1.1 cjs p = iflist;
187 1.1 cjs for (p = iflist; p; p = p->next) {
188 1.1 cjs if (strcmp(p->if_name,interface) == 0) {
189 1.1 cjs return;
190 1.1 cjs }
191 1.1 cjs }
192 1.1 cjs
193 1.6 christos if (!mopInteractive)
194 1.6 christos syslog(LOG_INFO, "Initialized %s", interface);
195 1.1 cjs
196 1.1 cjs /* Ok, get transport information */
197 1.1 cjs
198 1.1 cjs trans = pfTrans(interface);
199 1.1 cjs
200 1.1 cjs #ifndef NORC
201 1.1 cjs /* Start with MOP Remote Console */
202 1.1 cjs
203 1.1 cjs switch (trans) {
204 1.1 cjs case TRANS_ETHER:
205 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_ETHER);
206 1.1 cjs break;
207 1.1 cjs case TRANS_8023:
208 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_8023);
209 1.1 cjs break;
210 1.1 cjs case TRANS_ETHER+TRANS_8023:
211 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_ETHER);
212 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_8023);
213 1.1 cjs break;
214 1.1 cjs case TRANS_ETHER+TRANS_8023+TRANS_AND:
215 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_ETHER+TRANS_8023);
216 1.1 cjs break;
217 1.1 cjs }
218 1.1 cjs #endif
219 1.1 cjs
220 1.1 cjs #ifndef NODL
221 1.1 cjs /* and next MOP Dump/Load */
222 1.1 cjs
223 1.1 cjs switch (trans) {
224 1.1 cjs case TRANS_ETHER:
225 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_ETHER);
226 1.1 cjs break;
227 1.1 cjs case TRANS_8023:
228 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_8023);
229 1.1 cjs break;
230 1.1 cjs case TRANS_ETHER+TRANS_8023:
231 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_ETHER);
232 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_8023);
233 1.1 cjs break;
234 1.1 cjs case TRANS_ETHER+TRANS_8023+TRANS_AND:
235 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_ETHER+TRANS_8023);
236 1.1 cjs break;
237 1.1 cjs }
238 1.1 cjs #endif
239 1.1 cjs
240 1.1 cjs }
241 1.1 cjs
242 1.1 cjs /*
243 1.1 cjs * Initialize all "candidate" interfaces that are in the system
244 1.1 cjs * configuration list. A "candidate" is up, not loopback and not
245 1.1 cjs * point to point.
246 1.1 cjs */
247 1.1 cjs void
248 1.1 cjs deviceInitAll()
249 1.1 cjs {
250 1.1 cjs #ifdef DEV_NEW_CONF
251 1.1 cjs char inbuf[8192];
252 1.1 cjs struct ifconf ifc;
253 1.1 cjs struct ifreq *ifr;
254 1.1 cjs struct sockaddr_dl *sdl;
255 1.1 cjs int fd;
256 1.1 cjs int i, len;
257 1.1 cjs
258 1.6 christos if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
259 1.6 christos mopLogErr("deviceInitAll: socket");
260 1.1 cjs
261 1.1 cjs ifc.ifc_len = sizeof(inbuf);
262 1.1 cjs ifc.ifc_buf = inbuf;
263 1.1 cjs if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
264 1.6 christos ifc.ifc_len < sizeof(struct ifreq))
265 1.6 christos mopLogErr("deviceInitAll: SIOCGIFCONF");
266 1.1 cjs ifr = ifc.ifc_req;
267 1.1 cjs for (i = 0; i < ifc.ifc_len;
268 1.1 cjs i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
269 1.1 cjs len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
270 1.1 cjs sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
271 1.1 cjs if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
272 1.1 cjs sdl->sdl_alen != 6)
273 1.1 cjs continue;
274 1.1 cjs if (ioctl(fd, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
275 1.6 christos mopLogWarn("deviceInitAll: SIOCGIFFLAGS");
276 1.1 cjs continue;
277 1.1 cjs }
278 1.1 cjs if ((ifr->ifr_flags &
279 1.1 cjs (IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP)
280 1.1 cjs continue;
281 1.1 cjs deviceInitOne(ifr->ifr_name);
282 1.1 cjs }
283 1.1 cjs (void) close(fd);
284 1.1 cjs #else
285 1.1 cjs int fd;
286 1.1 cjs int n;
287 1.1 cjs struct ifreq ibuf[8], *ifrp;
288 1.1 cjs struct ifconf ifc;
289 1.1 cjs
290 1.6 christos if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
291 1.6 christos mopLogErr("deviceInitAll: old socket");
292 1.1 cjs ifc.ifc_len = sizeof ibuf;
293 1.1 cjs ifc.ifc_buf = (caddr_t)ibuf;
294 1.1 cjs if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
295 1.6 christos ifc.ifc_len < sizeof(struct ifreq))
296 1.6 christos mopLogErr("deviceInitAll: old SIOCGIFCONF");
297 1.1 cjs ifrp = ibuf;
298 1.1 cjs n = ifc.ifc_len / sizeof(*ifrp);
299 1.1 cjs for (; --n >= 0; ++ifrp) {
300 1.1 cjs if (ioctl(fd, SIOCGIFFLAGS, (char *)ifrp) < 0) {
301 1.1 cjs continue;
302 1.1 cjs }
303 1.1 cjs if (/*(ifrp->ifr_flags & IFF_UP) == 0 ||*/
304 1.1 cjs ifrp->ifr_flags & IFF_LOOPBACK ||
305 1.1 cjs ifrp->ifr_flags & IFF_POINTOPOINT)
306 1.1 cjs continue;
307 1.1 cjs deviceInitOne(ifrp->ifr_name);
308 1.1 cjs }
309 1.1 cjs
310 1.1 cjs (void) close(fd);
311 1.1 cjs #endif /* DEV_NEW_CONF */
312 1.1 cjs }
313