xref: /openbsd-src/usr.sbin/mopd/common/device.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: device.c,v 1.13 2006/04/17 16:23:01 deraadt 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef lint
28 static const char rcsid[] =
29     "$OpenBSD: device.c,v 1.13 2006/04/17 16:23:01 deraadt Exp $";
30 #endif
31 
32 #include "os.h"
33 #include "common/common.h"
34 #include "common/mopdef.h"
35 
36 struct if_info	*iflist;		/* Interface List		*/
37 
38 void mopReadDL(void);
39 void mopReadRC(void);
40 int  mopOpenDL(struct if_info *, int);
41 int  mopOpenRC(struct if_info *, int);
42 int  pfTrans();
43 int  pfInit();
44 int  pfWrite();
45 
46 #ifdef	DEV_NEW_CONF
47 /*
48  * Return ethernet address for interface
49  */
50 
51 void
52 deviceEthAddr(char *ifname, u_char *eaddr)
53 {
54 	struct sockaddr_dl	*sdl;
55 	struct ifaddrs		*ifap, *ifa;
56 
57 	if (getifaddrs(&ifap) != 0) {
58 		syslog(LOG_ERR, "deviceEthAddr: getifaddrs: %m");
59 		exit(1);
60 	}
61 
62 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
63 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
64 		if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
65 		    sdl->sdl_alen != 6)
66 			continue;
67 		if (!strcmp(ifa->ifa_name, ifname)) {
68 			bcopy(LLADDR(sdl), eaddr, 6);
69 			freeifaddrs(ifap);
70 			return;
71 		}
72 	}
73 
74 	syslog(LOG_ERR, "deviceEthAddr: Never saw interface `%s'!", ifname);
75 	exit(1);
76 }
77 #endif	/* DEV_NEW_CONF */
78 
79 void
80 deviceOpen(char *ifname, u_short proto, int trans)
81 {
82 	struct if_info	*p, tmp;
83 
84 	strncpy(tmp.if_name, ifname, sizeof(tmp.if_name) - 1);
85 	tmp.if_name[sizeof(tmp.if_name) - 1] = 0;
86 	tmp.iopen = pfInit;
87 
88 	switch (proto) {
89 	case MOP_K_PROTO_RC:
90 		tmp.read = mopReadRC;
91 		tmp.fd   = mopOpenRC(&tmp, trans);
92 		break;
93 	case MOP_K_PROTO_DL:
94 		tmp.read = mopReadDL;
95 		tmp.fd   = mopOpenDL(&tmp, trans);
96 		break;
97 	default:
98 		break;
99 	}
100 
101 	if (tmp.fd != -1) {
102 		p = malloc(sizeof(*p));
103 		if (p == 0) {
104 			syslog(LOG_ERR, "deviceOpen: malloc: %m");
105 			exit(1);
106 		}
107 
108 		p->next = iflist;
109 		iflist = p;
110 
111 		strlcpy(p->if_name, tmp.if_name, IFNAME_SIZE);
112 		p->iopen   = tmp.iopen;
113 		p->write   = pfWrite;
114 		p->read    = tmp.read;
115 		bzero(p->eaddr, sizeof(p->eaddr));
116 		p->fd      = tmp.fd;
117 
118 #ifdef	DEV_NEW_CONF
119 		deviceEthAddr(p->if_name, &p->eaddr[0]);
120 #else
121 		p->eaddr[0] = tmp.eaddr[0];
122 		p->eaddr[1] = tmp.eaddr[1];
123 		p->eaddr[2] = tmp.eaddr[2];
124 		p->eaddr[3] = tmp.eaddr[3];
125 		p->eaddr[4] = tmp.eaddr[4];
126 		p->eaddr[5] = tmp.eaddr[5];
127 #endif	/* DEV_NEW_CONF */
128 
129 #ifdef LINUX2_PF
130 		{
131 			int s;
132 
133 			s = socket(AF_INET, SOCK_DGRAM, 0);
134 			pfEthAddr(s, p->if_name, &p->eaddr[0]);
135 			close(s);
136 		}
137 #endif
138 	}
139 }
140 
141 void
142 deviceInitOne(char *ifname)
143 {
144 	char	interface[IFNAME_SIZE];
145 	struct if_info *p;
146 	int	trans;
147 #ifdef _AIX
148 	char	dev[IFNAME_SIZE];
149 	int	unit,j;
150 
151 	unit = 0;
152 	for (j = 0; j < strlen(ifname); j++) {
153 		if (isalpha(ifname[j]))
154 			dev[j] = ifname[j];
155 		else
156 			if (isdigit(ifname[j])) {
157 				unit = unit * 10 + ifname[j] - '0';
158 				dev[j] = '\0';
159 			}
160 	}
161 
162 	if ((strlen(dev) == 2) && (dev[0] == 'e') &&
163 	    ((dev[1] == 'n') || (dev[1] == 't')))
164 		snprintf(interface, sizeof(interface), "ent%d\0", unit);
165 	else
166 		snprintf(interface, sizeof(interface), "%s%d\0", dev, unit);
167 #else
168 	snprintf(interface, sizeof(interface), "%s", ifname);
169 #endif /* _AIX */
170 
171 	/* Ok, init it just once */
172 	p = iflist;
173 	for (p = iflist; p; p = p->next)
174 		if (strcmp(p->if_name, interface) == 0)
175 			return;
176 
177 	syslog(LOG_INFO, "Initialized %s", interface);
178 
179 	/* Ok, get transport information */
180 	trans = pfTrans(interface);
181 
182 #ifndef NORC
183 	/* Start with MOP Remote Console */
184 	switch (trans) {
185 	case TRANS_ETHER:
186 		deviceOpen(interface, MOP_K_PROTO_RC, TRANS_ETHER);
187 		break;
188 	case TRANS_8023:
189 		deviceOpen(interface, MOP_K_PROTO_RC, TRANS_8023);
190 		break;
191 	case TRANS_ETHER + TRANS_8023:
192 		deviceOpen(interface, MOP_K_PROTO_RC, TRANS_ETHER);
193 		deviceOpen(interface, MOP_K_PROTO_RC, TRANS_8023);
194 		break;
195 	case TRANS_ETHER + TRANS_8023 + TRANS_AND:
196 		deviceOpen(interface, MOP_K_PROTO_RC, TRANS_ETHER + TRANS_8023);
197 		break;
198 	}
199 #endif
200 
201 #ifndef NODL
202 	/* and next MOP Dump/Load */
203 	switch (trans) {
204 	case TRANS_ETHER:
205 		deviceOpen(interface, MOP_K_PROTO_DL, TRANS_ETHER);
206 		break;
207 	case TRANS_8023:
208 		deviceOpen(interface, MOP_K_PROTO_DL, TRANS_8023);
209 		break;
210 	case TRANS_ETHER + TRANS_8023:
211 		deviceOpen(interface, MOP_K_PROTO_DL, TRANS_ETHER);
212 		deviceOpen(interface, MOP_K_PROTO_DL, TRANS_8023);
213 		break;
214 	case TRANS_ETHER + TRANS_8023 + TRANS_AND:
215 		deviceOpen(interface, MOP_K_PROTO_DL, TRANS_ETHER + TRANS_8023);
216 		break;
217 	}
218 #endif
219 
220 }
221 
222 /*
223  * Initialize all "candidate" interfaces that are in the system
224  * configuration list.  A "candidate" is up, not loopback and not
225  * point to point.
226  */
227 void
228 deviceInitAll()
229 {
230 #ifdef DEV_NEW_CONF
231 	struct sockaddr_dl	*sdl;
232 	struct ifaddrs		*ifap, *ifa;
233 
234 	if (getifaddrs(&ifap) != 0) {
235 		syslog(LOG_ERR, "deviceInitAll: getifaddrs: %m");
236 		exit(1);
237 	}
238 
239 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
240 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
241 		if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
242 		    sdl->sdl_alen != 6)
243 			continue;
244 		if ((ifa->ifa_flags &
245 		    (IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP)
246 			continue;
247 		deviceInitOne(ifa->ifa_name);
248 	}
249 	freeifaddrs(ifap);
250 #else
251 	struct ifaddrs	*ifap, *ifa;
252 
253 	if (getifaddrs(&ifap) != 0) {
254 		syslog(LOG_ERR, "deviceInitAll: getifaddrs: %m");
255 		exit(1);
256 	}
257 
258 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
259 		if (/*(ifa->ifa_flags & IFF_UP) == 0 ||*/
260 		    ifa->ifa_flags & IFF_LOOPBACK ||
261 		    ifa->ifa_flags & IFF_POINTOPOINT)
262 			continue;
263 		deviceInitOne(ifa->ifa_name);
264 	}
265 	freeifaddrs(ifap);
266 #endif
267 }
268