1 /* $NetBSD: device.c,v 1.10 2009/10/20 00:51:13 snj Exp $ */
4 * Copyright (c) 1993-95 Mats O Jansson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: device.c,v 1.10 2009/10/20 00:51:13 snj Exp $");
39 struct if_info
*iflist
; /* Interface List */
41 void deviceOpen(const char *, u_short
, int);
45 * Return ethernet address for interface
49 deviceEthAddr(const char *ifname
, u_char
*eaddr
)
51 struct sockaddr_dl
*sdl
;
52 struct ifaddrs
*ifap
, *ifa
;
54 if (getifaddrs(&ifap
) != 0)
55 mopLogErr("deviceEthAddr: getifaddrs");
57 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
58 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
59 if (sdl
->sdl_family
!= AF_LINK
|| sdl
->sdl_type
!= IFT_ETHER
||
62 if (!strcmp(ifa
->ifa_name
, ifname
)) {
63 memmove((caddr_t
)eaddr
, (caddr_t
)LLADDR(sdl
), 6);
70 mopLogErrX("deviceEthAddr: Never saw interface `%s'!", ifname
);
72 #endif /* DEV_NEW_CONF */
75 deviceOpen(const char *ifname
, u_short proto
, int trans
)
77 struct if_info
*p
, tmp
;
79 strlcpy(tmp
.if_name
, ifname
, sizeof(tmp
.if_name
));
85 tmp
.fd
= mopOpenRC(&tmp
, trans
);
89 tmp
.fd
= mopOpenDL(&tmp
, trans
);
96 p
= (struct if_info
*)malloc(sizeof(*p
));
98 mopLogErr("deviceOpen: malloc");
103 strlcpy(p
->if_name
, tmp
.if_name
, sizeof(p
->if_name
));
104 p
->iopen
= tmp
.iopen
;
107 memset((char *)p
->eaddr
, 0, sizeof(p
->eaddr
));
111 deviceEthAddr(p
->if_name
,&p
->eaddr
[0]);
113 p
->eaddr
[0]= tmp
.eaddr
[0];
114 p
->eaddr
[1]= tmp
.eaddr
[1];
115 p
->eaddr
[2]= tmp
.eaddr
[2];
116 p
->eaddr
[3]= tmp
.eaddr
[3];
117 p
->eaddr
[4]= tmp
.eaddr
[4];
118 p
->eaddr
[5]= tmp
.eaddr
[5];
119 #endif /* DEV_NEW_CONF */
125 deviceInitOne(const char *ifname
)
127 char interface
[IFNAME_SIZE
];
131 char dev
[IFNAME_SIZE
];
135 for (j
= 0; j
< strlen(ifname
); j
++) {
136 if (isalpha(ifname
[j
])) {
139 if (isdigit(ifname
[j
])) {
140 unit
= unit
*10 + ifname
[j
] - '0';
146 if ((strlen(dev
) == 2) &&
148 ((dev
[1] == 'n') || (dev
[1] == 't'))) {
149 snprintf(interface
, sizeof(interface
), "ent%d\0", unit
);
151 snprintf(interface
, sizeof(interface
), "%s%d\0", dev
, unit
);
154 snprintf(interface
, sizeof(interface
), "%s", ifname
);
157 /* Ok, init it just once */
160 for (p
= iflist
; p
; p
= p
->next
) {
161 if (strcmp(p
->if_name
,interface
) == 0) {
167 syslog(LOG_INFO
, "Initialized %s", interface
);
169 /* Ok, get transport information */
171 trans
= pfTrans(interface
);
174 /* Start with MOP Remote Console */
178 deviceOpen(interface
,MOP_K_PROTO_RC
,TRANS_ETHER
);
181 deviceOpen(interface
,MOP_K_PROTO_RC
,TRANS_8023
);
183 case TRANS_ETHER
+TRANS_8023
:
184 deviceOpen(interface
,MOP_K_PROTO_RC
,TRANS_ETHER
);
185 deviceOpen(interface
,MOP_K_PROTO_RC
,TRANS_8023
);
187 case TRANS_ETHER
+TRANS_8023
+TRANS_AND
:
188 deviceOpen(interface
,MOP_K_PROTO_RC
,TRANS_ETHER
+TRANS_8023
);
194 /* and next MOP Dump/Load */
198 deviceOpen(interface
,MOP_K_PROTO_DL
,TRANS_ETHER
);
201 deviceOpen(interface
,MOP_K_PROTO_DL
,TRANS_8023
);
203 case TRANS_ETHER
+TRANS_8023
:
204 deviceOpen(interface
,MOP_K_PROTO_DL
,TRANS_ETHER
);
205 deviceOpen(interface
,MOP_K_PROTO_DL
,TRANS_8023
);
207 case TRANS_ETHER
+TRANS_8023
+TRANS_AND
:
208 deviceOpen(interface
,MOP_K_PROTO_DL
,TRANS_ETHER
+TRANS_8023
);
216 * Initialize all "candidate" interfaces that are in the system
217 * configuration list. A "candidate" is up, not loopback and not
224 struct sockaddr_dl
*sdl
;
225 struct ifaddrs
*ifap
, *ifa
;
227 if (getifaddrs(&ifap
) != 0)
228 mopLogErr("deviceInitAll: socket");
230 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
231 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
232 if (sdl
->sdl_family
!= AF_LINK
|| sdl
->sdl_type
!= IFT_ETHER
||
235 if ((ifa
->ifa_flags
&
236 (IFF_UP
| IFF_LOOPBACK
| IFF_POINTOPOINT
)) != IFF_UP
)
238 deviceInitOne(ifa
->ifa_name
);
243 struct ifaddrs
*ifap
, *ifa
;
245 if (getifaddrs(&ifap
) != 0)
246 mopLogErr("deviceInitAll: old socket");
248 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
249 if (/*(ifa->ifa_flags & IFF_UP) == 0 ||*/
250 ifa
->ifa_flags
& IFF_LOOPBACK
||
251 ifa
->ifa_flags
& IFF_POINTOPOINT
)
253 deviceInitOne(ifa
->ifa_name
);
257 #endif /* DEV_NEW_CONF */