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