1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/stropts.h>
#include <poll.h>
#include <sys/dlpi.h>
#include <sys/ethernet.h>
#include <sys/stream.h>
#include <sys/kstat.h>
#include <sys/llc1.h>
#include <dirent.h>
#include "rpld.h"
#include "dluser.h"
#define BUFSIZE 10000
unsigned char multAddr[6] = {0x03, 0x00, 0x02, 0x00, 0x00, 0x00};
unsigned char multAddr2[6] = {0xc0, 0x00, 0x40, 0x00, 0x00, 0x00};
static char *llcdev;
extern int if_fd;
extern struct pollfd llc_fd;
extern char ifName[];
extern int debugLevel;
extern char debugmsg[];
extern int ifUnit;
extern int ppanum;
extern int need_llc;
struct ether_addr myNodeAddr;
/*
* Next lines makes enabmulti, and thus its fields, 32 bit aligned. This is
* necessary on sparc. Space allocated must be rounded up to the next highest
* number of words.
*/
uint32_t enabmulti_aligned[(
DL_ENABMULTI_REQ_SIZE /* Basic size */
+ 10 /* Address */
+ sizeof (uint32_t) - 1)/ /* Round up */
sizeof (uint32_t)]; /* Convert to 32 bit */
char *enabmulti = (char *)enabmulti_aligned;
union DL_primitives *enabp = (union DL_primitives *)enabmulti_aligned;
int
llcsetup(void)
{
struct strbuf ctl;
char resultbuf[MAXPRIMSZ]; /* Bigger than largest DLPI */
/* struct size */
union DL_primitives *dlp = (union DL_primitives *)resultbuf;
struct ll_snioc snioc;
struct strioctl strio;
int flags = 0;
int muxid;
char myname[64];
struct ether_addr eas;
DIR *dp;
struct dirent *de;
int use_llc1;
unsigned char *multAddrp;
dl_info_t if_info;
/*
* Open a stream to the Ethernet driver.
*/
if (debugLevel >= MSG_INFO_2) {
sprintf(debugmsg, "Opening %s\n", ifName);
senddebug(MSG_INFO_2);
}
if ((if_fd = dl_open(ifName, O_RDWR, NULL)) < 0) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg, "Cannot open device %s\n", ifName);
senddebug(MSG_FATAL);
}
return (-1);
}
if (dl_info(if_fd, &if_info) < 0) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg, "Cannot get info from device %s\n",
ifName);
senddebug(MSG_FATAL);
}
return (-1);
}
/*
* Attach the Ethernet stream to PPA specified by ifUnit.
*/
if (debugLevel >= MSG_INFO_2) {
sprintf(debugmsg, "Sending DL_ATTACH_REQ to %s for PPA %d\n",
ifName, ifUnit);
senddebug(MSG_INFO_2);
}
if (dl_attach(if_fd, ifUnit) < 0) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg, "Failed in dl_attach of %s to %d\n",
ifName, ifUnit);
senddebug(MSG_FATAL);
}
return (-1);
}
/*
* Get the node address
*/
if (dlpi_get_phys(if_fd, &myNodeAddr) == 0) {
if (debugLevel >= MSG_INFO_1) {
sprintf(debugmsg, "My node address: %s ",
ether_ntoa(&myNodeAddr));
senddebug(MSG_INFO_1);
sprintf(debugmsg, "\n");
senddebug(MSG_INFO_1);
}
} else {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg, "Cannot get my own node address\n");
senddebug(MSG_FATAL);
}
return (-1);
}
/*
* Bind the Ethernet stream to SAP 0xfc.
*/
if (debugLevel >= MSG_INFO_2) {
sprintf(debugmsg, "Sending DL_BIND_REQ to ethernet driver\n");
senddebug(MSG_INFO_2);
}
if (dl_bind(if_fd, 0xFC, 0, 0) < 0) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg, "Failed in dl_bind to %s\n", ifName);
senddebug(MSG_FATAL);
}
return (-1);
}
if (debugLevel >= MSG_INFO_1) {
if (need_llc)
sprintf(debugmsg, "Need to link LLC under %s\n",
ifName);
else
sprintf(debugmsg, "No need to link LLC under %s\n",
ifName);
senddebug(MSG_INFO_1);
}
/*
* Token ring (IEEE 802.5) has the multicast address bytes
* bit-reversed (but not byte-reserved)
*/
if (if_info.mac_type == DLTYPE_IEEE8025)
multAddrp = multAddr2;
else
multAddrp = multAddr;
if (need_llc) {
/*
* Open a stream to the LLC driver.
* At this point it is certain that an llc device is needed
* So we try only llc1.
*/
llcdev = "/dev/llc1";
if (debugLevel >= MSG_INFO_2) {
sprintf(debugmsg, "Opening %s for SAP FC\n",
llcdev);
senddebug(MSG_INFO_2);
}
if ((llc_fd.fd = dl_open(llcdev, O_RDWR, NULL)) < 0) {
/*
* llc1 doesnt work barf and quit.
*/
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg,
"Failed to open device %s\n",
llcdev);
senddebug(MSG_FATAL);
}
return (-1);
}
/*
* I_LINK ifName underneath /dev/llc
*/
if (debugLevel >= MSG_INFO_2) {
sprintf(debugmsg, "Linking %s underneath %s FC case\n",
ifName, llcdev);
senddebug(MSG_INFO_2);
}
if ((muxid = ioctl(llc_fd.fd, I_LINK, if_fd)) < 0) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg,
"Failed to I_LINK %s under %s\n",
ifName, llcdev);
senddebug(MSG_FATAL);
}
return (-1);
}
/*
* Set the PPA of the Ethernet driver to ppanum.
* (This is the PPA that LLC clients need to specify when they
* attach to LLC.)
*/
if (debugLevel >= MSG_INFO_2) {
sprintf(debugmsg, "setting PPA to %d\n", ppanum);
senddebug(MSG_INFO_2);
}
snioc.lli_type = LI_SPPA;
snioc.lli_ppa = ppanum;
snioc.lli_index = muxid;
strio.ic_cmd = L_SETPPA;
strio.ic_timout = -1; /* Infinite timeout */
strio.ic_len = sizeof (snioc);
strio.ic_dp = (char *)&snioc;
if (ioctl(llc_fd.fd, I_STR, (char *)&strio) < 0) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg,
"Failed to set PPA %d to device %s\n",
ppanum, llcdev);
senddebug(MSG_FATAL);
}
return (-1);
}
/*
* Attach the LLC stream to PPA ppanum.
*/
if (debugLevel >= MSG_INFO_2) {
sprintf(debugmsg,
"Sending DL_ATTACH_REQ to %s FC\n", llcdev);
senddebug(MSG_INFO_2);
}
if (dl_attach(llc_fd.fd, ppanum) < 0) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg,
"Failed to attach to device %s\n",
llcdev);
senddebug(MSG_FATAL);
}
return (-1);
}
/*
* Bind the LLC stream to SAP 0xfc.
*/
if (debugLevel >= MSG_INFO_2) {
sprintf(debugmsg,
"Sending DL_BIND_REQ to %s FC\n", llcdev);
senddebug(MSG_INFO_2);
}
if (dl_bind(llc_fd.fd, 0xFC, DL_AUTO_TEST | DL_AUTO_XID, 0)
< 0) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg,
"Failed to bind to device %s\n",
llcdev);
senddebug(MSG_FATAL);
}
return (-1);
}
} else {
/* All subsequent I/O done using llc_fd.fd */
llc_fd.fd = if_fd;
}
/*
* Set the RPL multicast address
*/
if (debugLevel >= MSG_INFO_2) {
sprintf(debugmsg, "setting RPL multicast address\n");
senddebug(MSG_INFO_2);
}
enabp->enabmulti_req.dl_primitive = DL_ENABMULTI_REQ;
enabp->enabmulti_req.dl_addr_length = 6;
enabp->enabmulti_req.dl_addr_offset = DL_ENABMULTI_REQ_SIZE;
memcpy(&enabmulti[DL_ENABMULTI_REQ_SIZE], multAddrp, 6);
ctl.len = DL_ENABMULTI_REQ_SIZE + 6;
ctl.buf = enabmulti;
if (putmsg(llc_fd.fd, &ctl, NULL, 0) < 0) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg,
"Failed to enable RPL required multicast address\n");
senddebug(MSG_FATAL);
}
return (-1);
}
/*
* Read the acknowledgement message (will be DL_OK_ACK if
* it was successful).
*/
ctl.maxlen = MAXPRIMSZ;
ctl.buf = resultbuf;
if (getmsg(llc_fd.fd, &ctl, NULL, &flags) < 0) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg,
"Failed in getmsg() to receive ack for multicast enable operation\n");
senddebug(MSG_FATAL);
}
return (-1);
}
if (dlp->dl_primitive != DL_OK_ACK) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg, "dl_errno %d unix_errno %d\n",
dlp->error_ack.dl_errno,
dlp->error_ack.dl_unix_errno);
senddebug(MSG_FATAL);
}
return (-1);
}
return (0);
}
int
dlpi_get_phys(int fd, uchar_t *eaddr)
{
union DL_primitives *dlp;
char *buf;
struct strbuf ctl;
int flags;
int tmp;
/* Allocate required buffers */
if ((buf = (char *)memalign(BUFSIZE, sizeof (uint32_t))) == NULL) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg, "malloc() failed\n");
senddebug(MSG_FATAL);
}
return (-1);
}
/* Issue DL_PHYS_ADDR_REQ */
dlp = (union DL_primitives *)buf;
dlp->physaddr_req.dl_primitive = DL_PHYS_ADDR_REQ;
dlp->physaddr_req.dl_addr_type = DL_CURR_PHYS_ADDR;
ctl.buf = (char *)dlp;
ctl.len = DL_PHYS_ADDR_REQ_SIZE;
if (putmsg(fd, &ctl, NULL, 0) < 0) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg,
"Failed in putmsg() for DL_PHYS_ADDR_REQ\n");
senddebug(MSG_FATAL);
}
(void) free(buf);
return (-1);
}
/* read reply */
ctl.buf = (char *)dlp;
ctl.len = 0;
ctl.maxlen = BUFSIZE;
flags = 0;
if ((tmp = getmsg(fd, &ctl, NULL, &flags)) < 0) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg,
"Failed in getmsg() for DL_PHYS_ADDR_REQ\n");
senddebug(MSG_FATAL);
}
(void) free(buf);
return (-1);
}
if (debugLevel >= MSG_ALWAYS) {
sprintf(debugmsg,
"phys_addr_ack: ret[%d] ctl.len[%d] flags[%d]\n",
tmp, ctl.len, flags);
senddebug(MSG_ALWAYS);
}
/* Validate DL_PHYS_ADDR_ACK reply */
if (ctl.len < sizeof (ulong_t)) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg,
"phys_addr failed: short reply to phys_addr request\n");
senddebug(MSG_FATAL);
}
(void) free(buf);
return (-1);
}
if (dlp->dl_primitive == DL_ERROR_ACK) {
/*
* Do not print errors for DL_UNSUPPORTED and DL_NOTSUPPORTED
*/
if (dlp->error_ack.dl_errno != DL_UNSUPPORTED &&
dlp->error_ack.dl_errno != DL_NOTSUPPORTED) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg,
"phys_addr failed: dl_errno %d unix_errno %d\n",
dlp->error_ack.dl_errno,
dlp->error_ack.dl_unix_errno);
senddebug(MSG_FATAL);
}
}
(void) free(buf);
return (-1);
}
if (dlp->dl_primitive != DL_PHYS_ADDR_ACK) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg,
"phys_addr failed: unrecognizable dl_primitive %d received\n",
dlp->dl_primitive);
senddebug(MSG_FATAL);
}
(void) free(buf);
return (-1);
}
if (ctl.len < DL_PHYS_ADDR_ACK_SIZE) {
if (debugLevel >= MSG_FATAL) {
sprintf(debugmsg,
"phys_addr failed: short phys_addr acknowledgement received\n");
senddebug(MSG_FATAL);
}
(void) free(buf);
return (-1);
}
/* Check length of address. */
if (dlp->physaddr_ack.dl_addr_length != ETHERADDRL)
return (-1);
/*
* copy ethernet address
*/
memcpy((char *)eaddr, (char *)(buf + dlp->physaddr_ack.dl_addr_offset),
ETHERADDRL);
(void) free(buf);
return (0);
}
|