device.c revision 1.5 1 1.5 itojun /* $NetBSD: device.c,v 1.5 2002/08/22 07:18:42 itojun 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.5 itojun __RCSID("$NetBSD: device.c,v 1.5 2002/08/22 07:18:42 itojun 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.1 cjs
43 1.1 cjs struct if_info *iflist; /* Interface List */
44 1.1 cjs
45 1.3 lukem void deviceOpen __P((char *, u_short, int));
46 1.1 cjs
47 1.1 cjs #ifdef DEV_NEW_CONF
48 1.1 cjs /*
49 1.1 cjs * Return ethernet adress for interface
50 1.1 cjs */
51 1.1 cjs
52 1.1 cjs void
53 1.1 cjs deviceEthAddr(ifname, eaddr)
54 1.1 cjs char *ifname;
55 1.1 cjs u_char *eaddr;
56 1.1 cjs {
57 1.1 cjs char inbuf[8192];
58 1.1 cjs struct ifconf ifc;
59 1.1 cjs struct ifreq *ifr;
60 1.1 cjs struct sockaddr_dl *sdl;
61 1.1 cjs int fd;
62 1.1 cjs int i, len;
63 1.1 cjs
64 1.1 cjs /* We cannot use SIOCGIFADDR on the BPF descriptor.
65 1.1 cjs We must instead get all the interfaces with SIOCGIFCONF
66 1.1 cjs and find the right one. */
67 1.1 cjs
68 1.1 cjs /* Use datagram socket to get Ethernet address. */
69 1.1 cjs if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
70 1.1 cjs syslog(LOG_ERR, "deviceEthAddr: socket: %m");
71 1.1 cjs exit(1);
72 1.1 cjs }
73 1.1 cjs
74 1.1 cjs ifc.ifc_len = sizeof(inbuf);
75 1.1 cjs ifc.ifc_buf = inbuf;
76 1.1 cjs if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
77 1.1 cjs ifc.ifc_len < sizeof(struct ifreq)) {
78 1.1 cjs syslog(LOG_ERR, "deviceEthAddr: SIOGIFCONF: %m");
79 1.1 cjs exit(1);
80 1.1 cjs }
81 1.1 cjs ifr = ifc.ifc_req;
82 1.1 cjs for (i = 0; i < ifc.ifc_len;
83 1.1 cjs i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
84 1.1 cjs len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
85 1.1 cjs sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
86 1.1 cjs if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
87 1.1 cjs sdl->sdl_alen != 6)
88 1.1 cjs continue;
89 1.1 cjs if (!strncmp(ifr->ifr_name, ifname, sizeof(ifr->ifr_name))) {
90 1.3 lukem memmove((caddr_t)eaddr, (caddr_t)LLADDR(sdl), 6);
91 1.1 cjs return;
92 1.1 cjs }
93 1.1 cjs }
94 1.1 cjs
95 1.1 cjs syslog(LOG_ERR, "deviceEthAddr: Never saw interface `%s'!", ifname);
96 1.1 cjs exit(1);
97 1.1 cjs }
98 1.1 cjs #endif /* DEV_NEW_CONF */
99 1.1 cjs
100 1.1 cjs void
101 1.1 cjs deviceOpen(ifname, proto, trans)
102 1.1 cjs char *ifname;
103 1.1 cjs u_short proto;
104 1.1 cjs int trans;
105 1.1 cjs {
106 1.1 cjs struct if_info *p, tmp;
107 1.1 cjs
108 1.5 itojun strlcpy(tmp.if_name, ifname, sizeof(tmp.if_name));
109 1.1 cjs tmp.iopen = pfInit;
110 1.1 cjs
111 1.1 cjs switch (proto) {
112 1.1 cjs case MOP_K_PROTO_RC:
113 1.1 cjs tmp.read = mopReadRC;
114 1.1 cjs tmp.fd = mopOpenRC(&tmp, trans);
115 1.1 cjs break;
116 1.1 cjs case MOP_K_PROTO_DL:
117 1.1 cjs tmp.read = mopReadDL;
118 1.1 cjs tmp.fd = mopOpenDL(&tmp, trans);
119 1.1 cjs break;
120 1.1 cjs default:
121 1.1 cjs break;
122 1.1 cjs }
123 1.1 cjs
124 1.1 cjs if (tmp.fd != -1) {
125 1.1 cjs
126 1.1 cjs p = (struct if_info *)malloc(sizeof(*p));
127 1.1 cjs if (p == 0) {
128 1.1 cjs syslog(LOG_ERR, "deviceOpen: malloc: %m");
129 1.1 cjs exit(1);
130 1.1 cjs }
131 1.1 cjs
132 1.1 cjs p->next = iflist;
133 1.1 cjs iflist = p;
134 1.1 cjs
135 1.1 cjs strcpy(p->if_name,tmp.if_name);
136 1.1 cjs p->iopen = tmp.iopen;
137 1.1 cjs p->write = pfWrite;
138 1.1 cjs p->read = tmp.read;
139 1.3 lukem memset((char *)p->eaddr, 0, sizeof(p->eaddr));
140 1.1 cjs p->fd = tmp.fd;
141 1.1 cjs
142 1.1 cjs #ifdef DEV_NEW_CONF
143 1.1 cjs deviceEthAddr(p->if_name,&p->eaddr[0]);
144 1.1 cjs #else
145 1.1 cjs p->eaddr[0]= tmp.eaddr[0];
146 1.1 cjs p->eaddr[1]= tmp.eaddr[1];
147 1.1 cjs p->eaddr[2]= tmp.eaddr[2];
148 1.1 cjs p->eaddr[3]= tmp.eaddr[3];
149 1.1 cjs p->eaddr[4]= tmp.eaddr[4];
150 1.1 cjs p->eaddr[5]= tmp.eaddr[5];
151 1.1 cjs #endif /* DEV_NEW_CONF */
152 1.1 cjs
153 1.1 cjs }
154 1.1 cjs }
155 1.1 cjs
156 1.1 cjs void
157 1.1 cjs deviceInitOne(ifname)
158 1.1 cjs char *ifname;
159 1.1 cjs {
160 1.1 cjs char interface[IFNAME_SIZE];
161 1.1 cjs struct if_info *p;
162 1.1 cjs int trans;
163 1.1 cjs #ifdef _AIX
164 1.1 cjs char dev[IFNAME_SIZE];
165 1.1 cjs int unit,j;
166 1.1 cjs
167 1.1 cjs unit = 0;
168 1.1 cjs for (j = 0; j < strlen(ifname); j++) {
169 1.1 cjs if (isalpha(ifname[j])) {
170 1.1 cjs dev[j] = ifname[j];
171 1.1 cjs } else {
172 1.1 cjs if (isdigit(ifname[j])) {
173 1.1 cjs unit = unit*10 + ifname[j] - '0';
174 1.1 cjs dev[j] = '\0';
175 1.1 cjs }
176 1.1 cjs }
177 1.1 cjs }
178 1.1 cjs
179 1.1 cjs if ((strlen(dev) == 2) &&
180 1.1 cjs (dev[0] == 'e') &&
181 1.1 cjs ((dev[1] == 'n') || (dev[1] == 't'))) {
182 1.4 itojun snprintf(interface, sizeof(interface), "ent%d\0", unit);
183 1.1 cjs } else {
184 1.4 itojun snprintf(interface, sizeof(interface), "%s%d\0", dev, unit);
185 1.1 cjs }
186 1.1 cjs #else
187 1.4 itojun snprintf(interface, sizeof(interface), "%s", ifname);
188 1.1 cjs #endif /* _AIX */
189 1.1 cjs
190 1.1 cjs /* Ok, init it just once */
191 1.1 cjs
192 1.1 cjs p = iflist;
193 1.1 cjs for (p = iflist; p; p = p->next) {
194 1.1 cjs if (strcmp(p->if_name,interface) == 0) {
195 1.1 cjs return;
196 1.1 cjs }
197 1.1 cjs }
198 1.1 cjs
199 1.1 cjs syslog(LOG_INFO, "Initialized %s", interface);
200 1.1 cjs
201 1.1 cjs /* Ok, get transport information */
202 1.1 cjs
203 1.1 cjs trans = pfTrans(interface);
204 1.1 cjs
205 1.1 cjs #ifndef NORC
206 1.1 cjs /* Start with MOP Remote Console */
207 1.1 cjs
208 1.1 cjs switch (trans) {
209 1.1 cjs case TRANS_ETHER:
210 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_ETHER);
211 1.1 cjs break;
212 1.1 cjs case TRANS_8023:
213 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_8023);
214 1.1 cjs break;
215 1.1 cjs case TRANS_ETHER+TRANS_8023:
216 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_ETHER);
217 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_8023);
218 1.1 cjs break;
219 1.1 cjs case TRANS_ETHER+TRANS_8023+TRANS_AND:
220 1.1 cjs deviceOpen(interface,MOP_K_PROTO_RC,TRANS_ETHER+TRANS_8023);
221 1.1 cjs break;
222 1.1 cjs }
223 1.1 cjs #endif
224 1.1 cjs
225 1.1 cjs #ifndef NODL
226 1.1 cjs /* and next MOP Dump/Load */
227 1.1 cjs
228 1.1 cjs switch (trans) {
229 1.1 cjs case TRANS_ETHER:
230 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_ETHER);
231 1.1 cjs break;
232 1.1 cjs case TRANS_8023:
233 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_8023);
234 1.1 cjs break;
235 1.1 cjs case TRANS_ETHER+TRANS_8023:
236 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_ETHER);
237 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_8023);
238 1.1 cjs break;
239 1.1 cjs case TRANS_ETHER+TRANS_8023+TRANS_AND:
240 1.1 cjs deviceOpen(interface,MOP_K_PROTO_DL,TRANS_ETHER+TRANS_8023);
241 1.1 cjs break;
242 1.1 cjs }
243 1.1 cjs #endif
244 1.1 cjs
245 1.1 cjs }
246 1.1 cjs
247 1.1 cjs /*
248 1.1 cjs * Initialize all "candidate" interfaces that are in the system
249 1.1 cjs * configuration list. A "candidate" is up, not loopback and not
250 1.1 cjs * point to point.
251 1.1 cjs */
252 1.1 cjs void
253 1.1 cjs deviceInitAll()
254 1.1 cjs {
255 1.1 cjs #ifdef DEV_NEW_CONF
256 1.1 cjs char inbuf[8192];
257 1.1 cjs struct ifconf ifc;
258 1.1 cjs struct ifreq *ifr;
259 1.1 cjs struct sockaddr_dl *sdl;
260 1.1 cjs int fd;
261 1.1 cjs int i, len;
262 1.1 cjs
263 1.1 cjs if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
264 1.1 cjs syslog(LOG_ERR, "deviceInitAll: socket: %m");
265 1.1 cjs exit(1);
266 1.1 cjs }
267 1.1 cjs
268 1.1 cjs ifc.ifc_len = sizeof(inbuf);
269 1.1 cjs ifc.ifc_buf = inbuf;
270 1.1 cjs if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
271 1.1 cjs ifc.ifc_len < sizeof(struct ifreq)) {
272 1.1 cjs syslog(LOG_ERR, "deviceInitAll: SIOCGIFCONF: %m");
273 1.1 cjs exit(1);
274 1.1 cjs }
275 1.1 cjs ifr = ifc.ifc_req;
276 1.1 cjs for (i = 0; i < ifc.ifc_len;
277 1.1 cjs i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
278 1.1 cjs len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
279 1.1 cjs sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
280 1.1 cjs if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
281 1.1 cjs sdl->sdl_alen != 6)
282 1.1 cjs continue;
283 1.1 cjs if (ioctl(fd, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
284 1.1 cjs syslog(LOG_ERR, "deviceInitAll: SIOCGIFFLAGS: %m");
285 1.1 cjs continue;
286 1.1 cjs }
287 1.1 cjs if ((ifr->ifr_flags &
288 1.1 cjs (IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP)
289 1.1 cjs continue;
290 1.1 cjs deviceInitOne(ifr->ifr_name);
291 1.1 cjs }
292 1.1 cjs (void) close(fd);
293 1.1 cjs #else
294 1.1 cjs int fd;
295 1.1 cjs int n;
296 1.1 cjs struct ifreq ibuf[8], *ifrp;
297 1.1 cjs struct ifconf ifc;
298 1.1 cjs
299 1.1 cjs if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
300 1.1 cjs syslog(LOG_ERR, "deviceInitAll: old socket: %m");
301 1.1 cjs exit(1);
302 1.1 cjs }
303 1.1 cjs ifc.ifc_len = sizeof ibuf;
304 1.1 cjs ifc.ifc_buf = (caddr_t)ibuf;
305 1.1 cjs if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
306 1.1 cjs ifc.ifc_len < sizeof(struct ifreq)) {
307 1.1 cjs syslog(LOG_ERR, "deviceInitAll: old SIOCGIFCONF: %m");
308 1.1 cjs exit(1);
309 1.1 cjs }
310 1.1 cjs ifrp = ibuf;
311 1.1 cjs n = ifc.ifc_len / sizeof(*ifrp);
312 1.1 cjs for (; --n >= 0; ++ifrp) {
313 1.1 cjs if (ioctl(fd, SIOCGIFFLAGS, (char *)ifrp) < 0) {
314 1.1 cjs continue;
315 1.1 cjs }
316 1.1 cjs if (/*(ifrp->ifr_flags & IFF_UP) == 0 ||*/
317 1.1 cjs ifrp->ifr_flags & IFF_LOOPBACK ||
318 1.1 cjs ifrp->ifr_flags & IFF_POINTOPOINT)
319 1.1 cjs continue;
320 1.1 cjs deviceInitOne(ifrp->ifr_name);
321 1.1 cjs }
322 1.1 cjs
323 1.1 cjs (void) close(fd);
324 1.1 cjs #endif /* DEV_NEW_CONF */
325 1.1 cjs }
326