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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <socket_impl.h>
#include <socket_inet.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_types.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <sys/promif.h>
#include <sys/prom_plat.h>
#include <sys/salib.h>
#include "mac.h"
#include "mac_impl.h"
#include "atm_inet.h"
#include "ethernet_inet.h"
#include "fddi_inet.h"
#include "token_inet.h"
#include "ibd_inet.h"
/*
* MAC layer interface
*/
boolean_t initialized; /* Boolean state */
struct mac_type mac_state;
int arp_index; /* current arp table index */
static struct arptable atable[ARP_TABLE_SIZE];
struct ofw_net_types {
char *n_name; /* OFW network media name */
int n_type; /* IFT */
} ofw_types[] = {
{ "atm", IFT_ATM },
{ "ethernet", IFT_ETHER },
{ "fddi", IFT_FDDI },
{ "token-ring", IFT_ISO88025 },
{ "ipib", IFT_IB }
};
/*
* given the mac type, initialize the mac interface state.
*/
void
mac_init(char *bootdevicename)
{
int type = 0;
static char *mtu_name = "max-frame-size";
static char *chosen_net = "chosen-network-type";
static char *supported_net = "supported-network-types";
static char *netiftype = "network-interface-type";
pnode_t node;
char *wp, *media_type;
int len = 0, i;
char tmpbuf[MAXNAMELEN];
char devname[MAXNAMELEN];
if (initialized)
return;
mac_state.mac_in_timeout = MAC_IN_TIMEOUT;
#ifdef DEBUG
printf("mac_init: device path: %s\n", bootdevicename);
#endif /* DEBUG */
if ((mac_state.mac_dev = prom_open(bootdevicename)) == 0) {
(void) snprintf(tmpbuf, sizeof (tmpbuf),
"Cannot prom_open network device %s.", bootdevicename);
prom_panic(tmpbuf);
}
(void) prom_devname_from_pathname(bootdevicename, devname);
#ifdef DEBUG
printf("mac_init: Network device name: %s\n", devname);
#endif /* DEBUG */
/*
* Ask the prom for our MTU and media type. "chosen-network-type"
* is of the form of "<network type>,<speed (Mbps)>,<connector type>,
* <duplex mode>: e.g.: "ethernet,100,rj45,full"
*/
node = prom_finddevice(devname);
if (node != OBP_NONODE && node != OBP_BADNODE) {
if (prom_getproplen(node, mtu_name) == sizeof (ihandle_t)) {
(void) prom_getprop(node, mtu_name,
(caddr_t)&mac_state.mac_mtu);
}
bzero(tmpbuf, sizeof (tmpbuf));
/*
* The following order of looking for properties is
* from FWARC 2002/345.
*/
if ((len = prom_getproplen(node, netiftype)) > 0 &&
len < sizeof (tmpbuf)) {
(void) prom_getprop(node, netiftype, tmpbuf);
} else if ((len = prom_getproplen(node, chosen_net)) > 0 &&
len < sizeof (tmpbuf)) {
(void) prom_getprop(node, chosen_net, tmpbuf);
} else if ((len = prom_getproplen(node, supported_net)) > 0 &&
len < sizeof (tmpbuf)) {
(void) prom_getprop(node, supported_net, tmpbuf);
}
media_type = NULL;
if (len > 0) {
if ((wp = strstr(tmpbuf, ",")) != NULL)
*wp = '\0';
media_type = tmpbuf;
}
if (media_type != NULL) {
#ifdef DEBUG
printf("mac_init: Media type: %s\n", media_type);
#endif /* DEBUG */
for (i = 0; i < sizeof (ofw_types) /
sizeof (struct ofw_net_types); i++) {
if (strcmp(ofw_types[i].n_name,
media_type) == 0) {
type = ofw_types[i].n_type;
break;
}
}
}
}
switch (type) {
case IFT_ATM:
/*
* ATM is currently treated mostly like ethernet,
* with the exception that the MTU is most likely
* different.
*/
mac_state.mac_type = IFT_ATM;
mac_state.mac_arp_timeout = ATM_ARP_TIMEOUT;
mac_state.mac_in_timeout = ATM_IN_TIMEOUT;
if (mac_state.mac_mtu == 0)
mac_state.mac_mtu = ATMSIZE;
mac_state.mac_addr_len = sizeof (ether_addr_t);
mac_state.mac_addr_buf = bkmem_alloc(mac_state.mac_addr_len);
if (mac_state.mac_addr_buf == NULL)
prom_panic("mac_init: Cannot allocate memory.");
if (prom_getmacaddr(mac_state.mac_dev,
(caddr_t)mac_state.mac_addr_buf) != 0)
prom_panic("mac_init: Cannot obtain MAC address.");
mac_state.mac_arp = ether_arp;
mac_state.mac_rarp = ether_revarp;
mac_state.mac_header_len = ether_header_len;
mac_state.mac_input = ether_input;
mac_state.mac_output = ether_output;
break;
case IFT_FDDI:
/*
* FDDI is currently treated mostly like ethernet,
* with the exception that the MTU is most likely
* different.
*/
mac_state.mac_type = IFT_FDDI;
mac_state.mac_arp_timeout = FDDI_ARP_TIMEOUT;
mac_state.mac_in_timeout = FDDI_IN_TIMEOUT;
if (mac_state.mac_mtu == 0)
mac_state.mac_mtu = FDDISIZE;
mac_state.mac_addr_len = sizeof (ether_addr_t);
mac_state.mac_addr_buf = bkmem_alloc(mac_state.mac_addr_len);
if (mac_state.mac_addr_buf == NULL)
prom_panic("mac_init: Cannot allocate memory.");
if (prom_getmacaddr(mac_state.mac_dev,
(caddr_t)mac_state.mac_addr_buf) != 0)
prom_panic("mac_init: Cannot obtain MAC address.");
mac_state.mac_arp = ether_arp;
mac_state.mac_rarp = ether_revarp;
mac_state.mac_header_len = ether_header_len;
mac_state.mac_input = ether_input;
mac_state.mac_output = ether_output;
break;
case IFT_ISO88025:
/*
* Token ring is currently treated mostly like ethernet,
* with the exception that the MTU is most likely different.
*/
mac_state.mac_type = IFT_ISO88025;
mac_state.mac_arp_timeout = TOKEN_ARP_TIMEOUT;
mac_state.mac_in_timeout = TOKEN_IN_TIMEOUT;
if (mac_state.mac_mtu == 0)
mac_state.mac_mtu = TOKENSIZE;
mac_state.mac_addr_len = sizeof (ether_addr_t);
mac_state.mac_addr_buf = bkmem_alloc(mac_state.mac_addr_len);
if (mac_state.mac_addr_buf == NULL)
prom_panic("mac_init: Cannot allocate memory.");
if (prom_getmacaddr(mac_state.mac_dev,
(caddr_t)mac_state.mac_addr_buf) != 0)
prom_panic("mac_init: Cannot obtain MAC address.");
mac_state.mac_arp = ether_arp;
mac_state.mac_rarp = ether_revarp;
mac_state.mac_header_len = ether_header_len;
mac_state.mac_input = ether_input;
mac_state.mac_output = ether_output;
break;
case IFT_IB:
mac_state.mac_type = IFT_IB;
ibd_init();
break;
case IFT_ETHER:
/* FALLTHRU - default to ethernet */
default:
mac_state.mac_type = IFT_ETHER;
mac_state.mac_mtu = ETHERSIZE;
mac_state.mac_arp_timeout = ETHER_ARP_TIMEOUT;
mac_state.mac_in_timeout = ETHER_IN_TIMEOUT;
if (mac_state.mac_mtu == 0)
mac_state.mac_mtu = ETHERSIZE;
mac_state.mac_addr_len = sizeof (ether_addr_t);
mac_state.mac_addr_buf = bkmem_alloc(mac_state.mac_addr_len);
if (mac_state.mac_addr_buf == NULL)
prom_panic("mac_init: Cannot allocate memory.");
if (prom_getmacaddr(mac_state.mac_dev,
(caddr_t)mac_state.mac_addr_buf) != 0)
prom_panic("mac_init: Cannot obtain MAC address.");
mac_state.mac_arp = ether_arp;
mac_state.mac_rarp = ether_revarp;
mac_state.mac_header_len = ether_header_len;
mac_state.mac_input = ether_input;
mac_state.mac_output = ether_output;
break;
}
mac_state.mac_buf = bkmem_alloc(mac_state.mac_mtu);
if (mac_state.mac_buf == NULL)
prom_panic("mac_init: Cannot allocate netbuf memory.");
else
initialized = B_TRUE;
}
void
mac_fini()
{
if (mac_state.mac_addr_buf != NULL) {
bkmem_free((caddr_t)mac_state.mac_addr_buf,
mac_state.mac_addr_len);
mac_state.mac_addr_buf = NULL;
}
if (mac_state.mac_buf != NULL) {
bkmem_free(mac_state.mac_buf, mac_state.mac_mtu);
mac_state.mac_buf = NULL;
}
(void) prom_close(mac_state.mac_dev);
initialized = B_FALSE;
}
/* MAC layer specific socket initialization */
void
mac_socket_init(struct inetboot_socket *isp)
{
isp->input[MEDIA_LVL] = mac_state.mac_input;
isp->output[MEDIA_LVL] = mac_state.mac_output;
isp->close[MEDIA_LVL] = NULL;
isp->headerlen[MEDIA_LVL] = mac_state.mac_header_len;
isp->in_timeout = mac_state.mac_in_timeout;
}
/*
* Add an entry to the ARP table. All values in table are network order.
* No checking is done to determine whether there's duplicates.
*/
void
mac_set_arp(struct in_addr *ip, void *hp, int hl)
{
atable[arp_index].ia.s_addr = ip->s_addr;
bcopy(hp, (char *)atable[arp_index].ha, hl);
atable[arp_index].hl = hl;
arp_index++;
if (arp_index >= ARP_TABLE_SIZE)
arp_index = 0;
}
/*
* Retrieve an entry from the ARP table using network-order IP address as
* search criteria. HW address buffer is filled in up to hl in len. (make
* sure the buffer is big enough given the mac type)
*
* Returns TRUE if successful, FALSE otherwise. Will wait timeout milliseconds
* for a response.
*/
int
mac_get_arp(struct in_addr *ip, void *hp, int hl, uint32_t timeout)
{
int i, result;
for (i = 0; i < ARP_TABLE_SIZE; i++) {
if (ip->s_addr == atable[i].ia.s_addr) {
bcopy((char *)atable[i].ha, hp, hl);
return (TRUE);
}
}
/* Not found. ARP for it. */
bzero(hp, hl);
result = mac_state.mac_arp(ip, hp, timeout);
if (result) {
/* Cool - add it to the arp table */
mac_set_arp(ip, hp, hl);
}
return (result);
}
int
mac_get_mtu(void)
{
if (!initialized)
return (-1);
else
return (mac_state.mac_mtu);
}
int
mac_get_dev(void)
{
if (!initialized)
return (-1);
else
return (mac_state.mac_dev);
}
uint8_t *
mac_get_addr_buf(void)
{
if (!initialized)
return (NULL);
else
return (mac_state.mac_addr_buf);
}
int
mac_get_addr_len(void)
{
if (!initialized)
return (-1);
else
return (mac_state.mac_addr_len);
}
int
mac_get_type(void)
{
if (!initialized)
return (-1);
else
return (mac_state.mac_type);
}
void
mac_set_arp_timeout(unsigned int timeout)
{
mac_state.mac_arp_timeout =
timeout;
}
int
mac_get_arp_timeout(void)
{
if (!initialized)
return (-1);
else
return (mac_state.mac_arp_timeout);
}
int
mac_get_hdr_len(void)
{
if (!initialized)
return (-1);
else
return (mac_state.mac_header_len(NULL));
}
int
mac_call_arp(struct in_addr *addr, void *buf, uint32_t timeout)
{
return (mac_state.mac_arp(addr, buf, timeout));
}
void
mac_call_rarp(void)
{
mac_state.mac_rarp();
}
/*
* Map a IFT_ type to an RFC 1700 arp hwtype.
*/
uint8_t
mac_arp_type(uint8_t ift_type)
{
uint8_t arptype;
switch (ift_type) {
case IFT_ISO88025:
arptype = 4; /* token ring */
break;
case IFT_ATM:
arptype = 16; /* ATM */
break;
case IFT_FDDI:
arptype = 18; /* Fiber Channel */
break;
case IFT_IB:
arptype = 32; /* Infiniband */
break;
case IFT_ETHER:
/* FALLTHRU */
default:
arptype = 1; /* default to ethernet */
break;
}
return (arptype);
}
|