3 Data Link Provider Interface (DLPI) network interface code. */
6 * Copyright (c) 2009-2011 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC")
8 * Copyright (c) 1996-2003 by Internet Software Consortium
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Internet Systems Consortium, Inc.
24 * Redwood City, CA 94063
26 * https://www.isc.org/
28 * This software was written for Internet Systems Consortium
29 * by Eric James Negaard, <lmdejn@lmd.ericsson.se>. To learn more about
30 * Internet Systems Consortium, see ``https://www.isc.org''.
32 * Joost Mulders has also done considerable work in debugging the DLPI API
33 * support on Solaris and getting this code to work properly on a variety
34 * of different Solaris platforms.
38 * Based largely in part to the existing NIT code in nit.c.
40 * This code has been developed and tested on sparc-based machines running
41 * SunOS 5.5.1, with le and hme network interfaces. It should be pretty
46 * Implementation notes:
48 * I first tried to write this code to the "vanilla" DLPI 2.0 API.
49 * It worked on a Sun Ultra-1 with a hme interface, but didn't work
50 * on Sun SparcStation 5's with "le" interfaces (the packets sent out
51 * via dlpiunitdatareq contained an Ethernet type of 0x0000 instead
52 * of the expected 0x0800).
54 * Therefore I added the "DLPI_RAW" code which is a Sun extension to
55 * the DLPI standard. This code works on both of the above machines.
56 * This is configurable in the OS-dependent include file by defining
59 * It quickly became apparant that I should also use the "pfmod"
60 * STREAMS module to cut down on the amount of user level packet
61 * processing. I don't know how widely available "pfmod" is, so it's
62 * use is conditionally included. This is configurable in the
63 * OS-dependent include file by defining USE_DLPI_PFMOD.
65 * A major quirk on the Sun's at least, is that no packets seem to get
66 * sent out the interface until six seconds after the interface is
67 * first "attached" to [per system reboot] (it's actually from when
68 * the interface is attached, not when it is plumbed, so putting a
69 * sleep into the dhclient-script at PREINIT time doesn't help). I
70 * HAVE tried, without success to poll the fd to see when it is ready
71 * for writing. This doesn't help at all. If the sleeps are not done,
72 * the initial DHCPREQUEST or DHCPDISCOVER never gets sent out, so
73 * I've put them here, when register_send and register_receive are
74 * called (split up into two three-second sleeps between the notices,
75 * so that it doesn't seem like so long when you're watching :-). The
76 * amount of time to sleep is configurable in the OS-dependent include
77 * file by defining DLPI_FIRST_SEND_WAIT to be the number of seconds
82 * The Open Group Technical Standard can be found here:
83 * http://www.opengroup.org/onlinepubs/009618899/index.htm
85 * The HP DLPI Programmer's Guide can be found here:
86 * http://docs.hp.com/en/B2355-90139/index.html
91 #if defined (USE_DLPI_SEND) || defined (USE_DLPI_RECEIVE) || \
92 defined(USE_DLPI_HWADDR)
94 # include <sys/ioctl.h>
95 # include <sys/time.h>
96 # include <sys/dlpi.h>
98 # ifdef USE_DLPI_PFMOD
99 # include <sys/pfmod.h>
104 # include <netinet/in_systm.h>
105 # include "includes/netinet/ip.h"
106 # include "includes/netinet/udp.h"
107 # include "includes/netinet/if_ether.h"
109 # ifdef USE_DLPI_PFMOD
111 # define DLPI_MODNAME "DLPI+RAW+PFMOD"
113 # define DLPI_MODNAME "DLPI+PFMOD"
117 # define DLPI_MODNAME "DLPI+RAW"
119 # define DLPI_MODNAME "DLPI"
124 # define ABS(x) ((x) >= 0 ? (x) : 0-(x))
127 #if defined(USE_DLPI_PFMOD) || defined(USE_DLPI_RAW)
128 static int strioctl (int fd
, int cmd
, int timeout
, int len
, char *dp
);
131 #define DLPI_MAXDLBUF 8192 /* Buffer size */
132 #define DLPI_MAXDLADDR 1024 /* Max address size */
133 #define DLPI_DEVDIR "/dev/" /* Device directory */
135 static int dlpiopen(const char *ifname
);
136 static int dlpiunit (char *ifname
);
137 static int dlpiinforeq (int fd
);
138 static int dlpiphysaddrreq (int fd
, unsigned long addrtype
);
139 static int dlpiattachreq (int fd
, unsigned long ppa
);
140 static int dlpibindreq (int fd
, unsigned long sap
, unsigned long max_conind
,
141 unsigned long service_mode
, unsigned long conn_mgmt
,
142 unsigned long xidtest
);
143 #if defined(UNUSED_DLPI_INTERFACE)
144 /* These functions are unused at present, but may be used at a later date.
145 * defined out to avoid compiler warnings about unused static functions.
147 static int dlpidetachreq (int fd
);
148 static int dlpiunbindreq (int fd
);
150 static int dlpiokack (int fd
, char *bufp
);
151 static int dlpiinfoack (int fd
, char *bufp
);
152 static int dlpiphysaddrack (int fd
, char *bufp
);
153 static int dlpibindack (int fd
, char *bufp
);
154 #if defined(USE_DLPI_SEND) || defined(USE_DLPI_RECEIVE)
155 /* These functions are not used if we're only sourcing the get_hw_addr()
156 * function (for USE_SOCKETS).
158 static int dlpiunitdatareq (int fd
, unsigned char *addr
, int addrlen
,
159 unsigned long minpri
, unsigned long maxpri
,
160 unsigned char *data
, int datalen
);
161 static int dlpiunitdataind (int fd
,
162 unsigned char *dstaddr
,
163 unsigned long *dstaddrlen
,
164 unsigned char *srcaddr
,
165 unsigned long *srcaddrlen
,
166 unsigned long *grpaddr
,
169 #endif /* !USE_DLPI_HWADDR: USE_DLPI_SEND || USE_DLPI_RECEIVE */
170 static int expected (unsigned long prim
, union DL_primitives
*dlp
,
172 static int strgetmsg (int fd
, struct strbuf
*ctlp
, struct strbuf
*datap
,
173 int *flagsp
, char *caller
);
175 /* Reinitializes the specified interface after an address change. This
176 is not required for packet-filter APIs. */
179 void if_reinitialize_send (info
)
180 struct interface_info
*info
;
185 #ifdef USE_DLPI_RECEIVE
186 void if_reinitialize_receive (info
)
187 struct interface_info
*info
;
192 /* Called by get_interface_list for each interface that's discovered.
193 Opens a packet filter for each interface and adds it to the select
196 int if_register_dlpi (info
)
197 struct interface_info
*info
;
201 long buf
[DLPI_MAXDLBUF
];
202 union DL_primitives
*dlp
;
204 dlp
= (union DL_primitives
*)buf
;
206 /* Open a DLPI device */
207 if ((sock
= dlpiopen (info
-> name
)) < 0) {
208 log_fatal ("Can't open DLPI device for %s: %m", info
-> name
);
212 * Submit a DL_INFO_REQ request, to find the dl_mac_type and
215 if (dlpiinforeq(sock
) < 0 || dlpiinfoack(sock
, (char *)buf
) < 0) {
216 log_fatal ("Can't get DLPI MAC type for %s: %m", info
-> name
);
218 switch (dlp
-> info_ack
.dl_mac_type
) {
219 case DL_CSMACD
: /* IEEE 802.3 */
221 info
-> hw_address
.hbuf
[0] = HTYPE_ETHER
;
223 /* adding token ring 5/1999 - mayer@ping.at */
225 info
-> hw_address
.hbuf
[0] = HTYPE_IEEE802
;
228 info
-> hw_address
.hbuf
[0] = HTYPE_FDDI
;
231 log_fatal("%s: unsupported DLPI MAC type %lu", info
->name
,
232 (unsigned long)dlp
->info_ack
.dl_mac_type
);
236 * copy the sap length and broadcast address of this interface
237 * to interface_info. This fixes nothing but seemed nicer than to
238 * assume -2 and ffffff.
240 info
-> dlpi_sap_length
= dlp
-> info_ack
.dl_sap_length
;
241 info
-> dlpi_broadcast_addr
.hlen
=
242 dlp
-> info_ack
.dl_brdcst_addr_length
;
243 memcpy (info
-> dlpi_broadcast_addr
.hbuf
,
244 (char *)dlp
+ dlp
-> info_ack
.dl_brdcst_addr_offset
,
245 dlp
-> info_ack
.dl_brdcst_addr_length
);
248 if (dlp
-> info_ack
.dl_provider_style
== DL_STYLE2
) {
250 * Attach to the device. If this fails, the device
253 unit
= dlpiunit (info
-> name
);
255 if (dlpiattachreq (sock
, unit
) < 0
256 || dlpiokack (sock
, (char *)buf
) < 0) {
257 log_fatal ("Can't attach DLPI device for %s: %m", info
-> name
);
262 * Bind to the IP service access point (SAP), connectionless (CLDLS).
264 if (dlpibindreq (sock
, ETHERTYPE_IP
, 0, DL_CLDLS
, 0, 0) < 0
265 || dlpibindack (sock
, (char *)buf
) < 0) {
266 log_fatal ("Can't bind DLPI device for %s: %m", info
-> name
);
270 * Submit a DL_PHYS_ADDR_REQ request, to find
271 * the hardware address
273 if (dlpiphysaddrreq (sock
, DL_CURR_PHYS_ADDR
) < 0
274 || dlpiphysaddrack (sock
, (char *)buf
) < 0) {
275 log_fatal ("Can't get DLPI hardware address for %s: %m",
279 info
-> hw_address
.hlen
= dlp
-> physaddr_ack
.dl_addr_length
+ 1;
280 memcpy (&info
-> hw_address
.hbuf
[1],
281 (char *)buf
+ dlp
-> physaddr_ack
.dl_addr_offset
,
282 dlp
-> physaddr_ack
.dl_addr_length
);
285 if (strioctl (sock
, DLIOCRAW
, INFTIM
, 0, 0) < 0) {
286 log_fatal ("Can't set DLPI RAW mode for %s: %m",
291 #ifdef USE_DLPI_PFMOD
292 if (ioctl (sock
, I_PUSH
, "pfmod") < 0) {
293 log_fatal ("Can't push packet filter onto DLPI for %s: %m",
301 #if defined(USE_DLPI_PFMOD) || defined(USE_DLPI_RAW)
303 strioctl (fd
, cmd
, timeout
, len
, dp
)
314 sio
.ic_timout
= timeout
;
318 if ((rslt
= ioctl (fd
, I_STR
, &sio
)) < 0) {
324 #endif /* USE_DPI_PFMOD || USE_DLPI_RAW */
327 void if_register_send (info
)
328 struct interface_info
*info
;
330 /* If we're using the DLPI API for sending and receiving,
331 we don't need to register this interface twice. */
332 #ifndef USE_DLPI_RECEIVE
333 # ifdef USE_DLPI_PFMOD
334 struct packetfilt pf
;
337 info
-> wfdesc
= if_register_dlpi (info
);
339 # ifdef USE_DLPI_PFMOD
340 /* Set up an PFMOD filter that rejects everything... */
343 pf
.Pf_Filter
[0] = ENF_PUSHZERO
;
345 /* Install the filter */
346 if (strioctl (info
-> wfdesc
, PFIOCSETF
, INFTIM
,
347 sizeof (pf
), (char *)&pf
) < 0) {
348 log_fatal ("Can't set PFMOD send filter on %s: %m", info
-> name
);
351 # endif /* USE_DLPI_PFMOD */
352 #else /* !defined (USE_DLPI_RECEIVE) */
354 * If using DLPI for both send and receive, simply re-use
355 * the read file descriptor that was set up earlier.
357 info
-> wfdesc
= info
-> rfdesc
;
360 if (!quiet_interface_discovery
)
361 log_info ("Sending on DLPI/%s/%s%s%s",
363 print_hw_addr (info
-> hw_address
.hbuf
[0],
364 info
-> hw_address
.hlen
- 1,
365 &info
-> hw_address
.hbuf
[1]),
366 (info
-> shared_network
? "/" : ""),
367 (info
-> shared_network
?
368 info
-> shared_network
-> name
: ""));
370 #ifdef DLPI_FIRST_SEND_WAIT
371 /* See the implementation notes at the beginning of this file */
372 # ifdef USE_DLPI_RECEIVE
373 sleep (DLPI_FIRST_SEND_WAIT
- (DLPI_FIRST_SEND_WAIT
/ 2));
375 sleep (DLPI_FIRST_SEND_WAIT
);
380 void if_deregister_send (info
)
381 struct interface_info
*info
;
383 /* If we're using the DLPI API for sending and receiving,
384 we don't need to register this interface twice. */
385 #ifndef USE_DLPI_RECEIVE
386 close (info
-> wfdesc
);
390 if (!quiet_interface_discovery
)
391 log_info ("Disabling output on DLPI/%s/%s%s%s",
393 print_hw_addr (info
-> hw_address
.hbuf
[0],
394 info
-> hw_address
.hlen
- 1,
395 &info
-> hw_address
.hbuf
[1]),
396 (info
-> shared_network
? "/" : ""),
397 (info
-> shared_network
?
398 info
-> shared_network
-> name
: ""));
400 #endif /* USE_DLPI_SEND */
402 #ifdef USE_DLPI_RECEIVE
403 /* Packet filter program...
404 XXX Changes to the filter program may require changes to the constant
405 offsets used in if_register_send to patch the NIT program! XXX */
407 void if_register_receive (info
)
408 struct interface_info
*info
;
410 #ifdef USE_DLPI_PFMOD
411 struct packetfilt pf
;
416 /* Open a DLPI device and hang it on this interface... */
417 info
-> rfdesc
= if_register_dlpi (info
);
419 #ifdef USE_DLPI_PFMOD
420 /* Set up the PFMOD filter program. */
421 /* XXX Unlike the BPF filter program, this one won't work if the
422 XXX IP packet is fragmented or if there are options on the IP
427 #if defined (USE_DLPI_RAW)
428 # define ETHER_H_PREFIX (14) /* sizeof (ethernet_header) */
430 * ethertype == ETHERTYPE_IP
433 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ (offset
/ 2);
434 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
| ENF_CAND
;
435 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (ETHERTYPE_IP
);
437 # define ETHER_H_PREFIX (0)
438 # endif /* USE_DLPI_RAW */
440 * The packets that will be received on this file descriptor
441 * will be IP packets (due to the SAP that was specified in
442 * the dlbind call). There will be no ethernet header.
443 * Therefore, setup the packet filter to check the protocol
444 * field for UDP, and the destination port number equal
445 * to the local port. All offsets are relative to the start
450 * BOOTPS destination port
452 offset
= ETHER_H_PREFIX
+ sizeof (iphdr
) + sizeof (u_int16_t
);
453 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ (offset
/ 2);
454 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
| ENF_CAND
;
455 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = local_port
;
458 * protocol should be udp. this is a byte compare, test for
461 offset
= ETHER_H_PREFIX
+ ((u_int8_t
*)&(iphdr
.ip_p
) - (u_int8_t
*)&iphdr
);
462 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ (offset
/ 2);
463 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
| ENF_AND
;
464 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (0x00FF);
465 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
| ENF_CAND
;
466 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (IPPROTO_UDP
);
468 /* Install the filter... */
469 if (strioctl (info
-> rfdesc
, PFIOCSETF
, INFTIM
,
470 sizeof (pf
), (char *)&pf
) < 0) {
471 log_fatal ("Can't set PFMOD receive filter on %s: %m", info
-> name
);
473 #endif /* USE_DLPI_PFMOD */
475 if (!quiet_interface_discovery
)
476 log_info ("Listening on DLPI/%s/%s%s%s",
478 print_hw_addr (info
-> hw_address
.hbuf
[0],
479 info
-> hw_address
.hlen
- 1,
480 &info
-> hw_address
.hbuf
[1]),
481 (info
-> shared_network
? "/" : ""),
482 (info
-> shared_network
?
483 info
-> shared_network
-> name
: ""));
485 #ifdef DLPI_FIRST_SEND_WAIT
486 /* See the implementation notes at the beginning of this file */
487 # ifdef USE_DLPI_SEND
488 sleep (DLPI_FIRST_SEND_WAIT
/ 2);
490 sleep (DLPI_FIRST_SEND_WAIT
);
495 void if_deregister_receive (info
)
496 struct interface_info
*info
;
498 /* If we're using the DLPI API for sending and receiving,
499 we don't need to register this interface twice. */
500 #ifndef USE_DLPI_SEND
501 close (info
-> rfdesc
);
505 if (!quiet_interface_discovery
)
506 log_info ("Disabling input on DLPI/%s/%s%s%s",
508 print_hw_addr (info
-> hw_address
.hbuf
[0],
509 info
-> hw_address
.hlen
- 1,
510 &info
-> hw_address
.hbuf
[1]),
511 (info
-> shared_network
? "/" : ""),
512 (info
-> shared_network
?
513 info
-> shared_network
-> name
: ""));
515 #endif /* USE_DLPI_RECEIVE */
518 ssize_t
send_packet (interface
, packet
, raw
, len
, from
, to
, hto
)
519 struct interface_info
*interface
;
520 struct packet
*packet
;
521 struct dhcp_packet
*raw
;
524 struct sockaddr_in
*to
;
525 struct hardware
*hto
;
530 double ih
[1536 / sizeof (double)];
531 unsigned char *dbuf
= (unsigned char *)ih
;
533 unsigned char dstaddr
[DLPI_MAXDLADDR
];
538 if (!strcmp (interface
-> name
, "fallback"))
539 return send_fallback (interface
, packet
, raw
,
542 if (hto
== NULL
&& interface
->anycast_mac_addr
.hlen
)
543 hto
= &interface
->anycast_mac_addr
;
547 /* Assemble the headers... */
549 assemble_hw_header (interface
, (unsigned char *)hh
, &dbuflen
, hto
);
550 if (dbuflen
> sizeof hh
)
551 log_fatal ("send_packet: hh buffer too small.\n");
552 fudge
= dbuflen
% 4; /* IP header must be word-aligned. */
553 memcpy (dbuf
+ fudge
, (unsigned char *)hh
, dbuflen
);
558 assemble_udp_ip_header (interface
, dbuf
, &dbuflen
, from
.s_addr
,
559 to
-> sin_addr
.s_addr
, to
-> sin_port
,
560 (unsigned char *)raw
, len
);
562 /* Copy the data into the buffer (yuk). */
563 memcpy (dbuf
+ dbuflen
, raw
, len
);
567 result
= write (interface
-> wfdesc
, dbuf
+ fudge
, dbuflen
- fudge
);
571 * Setup the destination address (DLSAP) in dstaddr
573 * If sap_length < 0 we must deliver the DLSAP as phys+sap.
574 * If sap_length > 0 we must deliver the DLSAP as sap+phys.
576 * sap = Service Access Point == ETHERTYPE_IP
577 * sap + datalink address is called DLSAP in dlpi speak.
580 unsigned char phys
[DLPI_MAXDLADDR
];
581 unsigned char sap
[4];
582 int sap_len
= interface
-> dlpi_sap_length
;
583 int phys_len
= interface
-> hw_address
.hlen
- 1;
585 /* sap = htons (ETHERTYPE_IP) kludge */
586 memset (sap
, 0, sizeof (sap
));
587 # if (BYTE_ORDER == LITTLE_ENDIAN)
595 if (hto
&& hto
-> hlen
== interface
-> hw_address
.hlen
)
596 memcpy ( phys
, (char *) &hto
-> hbuf
[1], phys_len
);
598 memcpy ( phys
, interface
-> dlpi_broadcast_addr
.hbuf
,
599 interface
-> dlpi_broadcast_addr
.hlen
);
602 memcpy ( dstaddr
, phys
, phys_len
);
603 memcpy ( (char *) &dstaddr
[phys_len
], sap
, ABS (sap_len
));
606 memcpy ( dstaddr
, (void *) sap
, sap_len
);
607 memcpy ( (char *) &dstaddr
[sap_len
], phys
, phys_len
);
609 addrlen
= phys_len
+ ABS (sap_len
);
612 result
= dlpiunitdatareq (interface
-> wfdesc
, dstaddr
, addrlen
,
613 0, 0, dbuf
, dbuflen
);
614 #endif /* USE_DLPI_RAW */
616 log_error ("send_packet: %m");
619 #endif /* USE_DLPI_SEND */
621 #ifdef USE_DLPI_RECEIVE
622 ssize_t
receive_packet (interface
, buf
, len
, from
, hfrom
)
623 struct interface_info
*interface
;
626 struct sockaddr_in
*from
;
627 struct hardware
*hfrom
;
629 unsigned char dbuf
[1536];
630 unsigned char srcaddr
[DLPI_MAXDLADDR
];
631 unsigned long srcaddrlen
;
638 length
= read (interface
-> rfdesc
, dbuf
, sizeof (dbuf
));
640 length
= dlpiunitdataind (interface
-> rfdesc
, (unsigned char *)NULL
,
641 (unsigned long *)NULL
, srcaddr
, &srcaddrlen
,
642 (unsigned long *)NULL
, dbuf
, sizeof (dbuf
));
646 log_error("receive_packet: %m");
650 # if !defined (USE_DLPI_RAW)
652 * Copy the sender's hw address into hfrom
653 * If sap_len < 0 the DLSAP is as phys+sap.
654 * If sap_len > 0 the DLSAP is as sap+phys.
656 * sap is discarded here.
659 int sap_len
= interface
-> dlpi_sap_length
;
660 int phys_len
= interface
-> hw_address
.hlen
- 1;
662 if (hfrom
&& (srcaddrlen
== ABS (sap_len
) + phys_len
)) {
663 hfrom
-> hbuf
[0] = interface
-> hw_address
.hbuf
[0];
664 hfrom
-> hlen
= interface
-> hw_address
.hlen
;
667 memcpy ((char *) &hfrom
-> hbuf
[1], srcaddr
, phys_len
);
670 memcpy((char *)&hfrom
->hbuf
[1], srcaddr
+ sap_len
, phys_len
);
674 memset (hfrom
, '\0', sizeof *hfrom
);
678 # endif /* !defined (USE_DLPI_RAW) */
680 /* Decode the IP and UDP headers... */
683 /* Decode the physical header... */
684 offset
= decode_hw_header (interface
, dbuf
, bufix
, hfrom
);
686 /* If a physical layer checksum failed (dunno of any
687 physical layer that supports this, but WTH), skip this
695 offset
= decode_udp_ip_header (interface
, dbuf
, bufix
,
696 from
, length
, &paylen
);
699 * If the IP or UDP checksum was bad, skip the packet...
701 * Note: this happens all the time when writing packets via the
702 * fallback socket. The packet received by streams does not have
703 * the IP or UDP checksums filled in, as those are calculated by
714 log_fatal("Internal inconsistency at %s:%d.", MDL
);
716 /* Copy out the data in the packet... */
717 memcpy(buf
, &dbuf
[bufix
], paylen
);
722 /* Common DLPI routines ...
724 * Written by Eric James Negaard, <lmdejn@lmd.ericsson.se>
726 * Based largely in part to the example code contained in the document
727 * "How to Use the STREAMS Data Link Provider Interface (DLPI)", written
728 * by Neal Nuckolls of SunSoft Internet Engineering.
730 * This code has been developed and tested on sparc-based machines running
731 * SunOS 5.5.1, with le and hme network interfaces. It should be pretty
734 * The usual disclaimers apply. This code works for me. Don't blame me
735 * if it makes your machine or network go down in flames. That taken
736 * into consideration, use this code as you wish. If you make usefull
737 * modifications I'd appreciate hearing about it.
740 #define DLPI_MAXWAIT 15 /* Max timeout */
744 * Parse an interface name and extract the unit number
747 static int dlpiunit (ifname
)
757 /* Advance to the end of the name */
760 /* Back up to the start of the first digit */
761 while ((*(cp
-1) >= '0' && *(cp
-1) <= '9') || *(cp
- 1) == ':') cp
--;
763 /* Convert the unit number */
765 while (*cp
>= '0' && *cp
<= '9') {
767 unit
+= (*cp
++ - '0');
774 * dlpiopen - open the DLPI device for a given interface name
777 dlpiopen(const char *ifname
) {
786 /* Open a DLPI device */
787 if (*ifname
== '/') {
790 /* Prepend the device directory */
791 memcpy (devname
, DLPI_DEVDIR
, strlen (DLPI_DEVDIR
));
792 dp
= &devname
[strlen (DLPI_DEVDIR
)];
795 /* Find the end of the interface name */
799 /* And back up to the first digit (unit number) */
800 while ((*(ep
- 1) >= '0' && *(ep
- 1) <= '9') || *(ep
- 1) == ':')
803 /* Copy everything up to the unit number */
809 return open (devname
, O_RDWR
, 0);
813 * dlpiinforeq - request information about the data link provider.
816 static int dlpiinforeq (fd
)
819 dl_info_req_t info_req
;
823 info_req
.dl_primitive
= DL_INFO_REQ
;
826 ctl
.len
= sizeof (info_req
);
827 ctl
.buf
= (char *)&info_req
;
831 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
835 * dlpiphysaddrreq - request the current physical address.
837 static int dlpiphysaddrreq (fd
, addrtype
)
839 unsigned long addrtype
;
841 dl_phys_addr_req_t physaddr_req
;
845 physaddr_req
.dl_primitive
= DL_PHYS_ADDR_REQ
;
846 physaddr_req
.dl_addr_type
= addrtype
;
849 ctl
.len
= sizeof (physaddr_req
);
850 ctl
.buf
= (char *)&physaddr_req
;
854 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
858 * dlpiattachreq - send a request to attach to a specific unit.
860 static int dlpiattachreq (fd
, ppa
)
864 dl_attach_req_t attach_req
;
868 attach_req
.dl_primitive
= DL_ATTACH_REQ
;
869 attach_req
.dl_ppa
= ppa
;
872 ctl
.len
= sizeof (attach_req
);
873 ctl
.buf
= (char *)&attach_req
;
877 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
881 * dlpibindreq - send a request to bind to a specific SAP address.
883 static int dlpibindreq (fd
, sap
, max_conind
, service_mode
, conn_mgmt
, xidtest
)
885 unsigned long max_conind
;
886 unsigned long service_mode
;
887 unsigned long conn_mgmt
;
888 unsigned long xidtest
;
891 dl_bind_req_t bind_req
;
895 bind_req
.dl_primitive
= DL_BIND_REQ
;
896 bind_req
.dl_sap
= sap
;
897 bind_req
.dl_max_conind
= max_conind
;
898 bind_req
.dl_service_mode
= service_mode
;
899 bind_req
.dl_conn_mgmt
= conn_mgmt
;
900 bind_req
.dl_xidtest_flg
= xidtest
;
903 ctl
.len
= sizeof (bind_req
);
904 ctl
.buf
= (char *)&bind_req
;
908 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
911 #if defined(UNUSED_DLPI_INTERFACE)
913 * dlpiunbindreq - send a request to unbind. This function is not actually
914 * used by ISC DHCP, but is included for completeness in case it is
915 * ever required for new work.
917 static int dlpiunbindreq (fd
)
920 dl_unbind_req_t unbind_req
;
924 unbind_req
.dl_primitive
= DL_UNBIND_REQ
;
927 ctl
.len
= sizeof (unbind_req
);
928 ctl
.buf
= (char *)&unbind_req
;
932 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
937 * dlpidetachreq - send a request to detach. This function is not actually
938 * used by ISC DHCP, but is included for completeness in case it is
939 * ever required for new work.
941 static int dlpidetachreq (fd
)
944 dl_detach_req_t detach_req
;
948 detach_req
.dl_primitive
= DL_DETACH_REQ
;
951 ctl
.len
= sizeof (detach_req
);
952 ctl
.buf
= (char *)&detach_req
;
956 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
958 #endif /* UNUSED_DLPI_INTERFACE */
962 * dlpibindack - receive an ack to a dlbindreq.
964 static int dlpibindack (fd
, bufp
)
968 union DL_primitives
*dlp
;
972 ctl
.maxlen
= DLPI_MAXDLBUF
;
976 if (strgetmsg (fd
, &ctl
,
977 (struct strbuf
*)NULL
, &flags
, "dlpibindack") < 0) {
981 dlp
= (union DL_primitives
*)ctl
.buf
;
983 if (expected (DL_BIND_ACK
, dlp
, flags
) == -1) {
987 if (ctl
.len
< sizeof (dl_bind_ack_t
)) {
988 /* Returned structure is too short */
996 * dlpiokack - general acknowledgement reception.
998 static int dlpiokack (fd
, bufp
)
1002 union DL_primitives
*dlp
;
1006 ctl
.maxlen
= DLPI_MAXDLBUF
;
1010 if (strgetmsg (fd
, &ctl
,
1011 (struct strbuf
*)NULL
, &flags
, "dlpiokack") < 0) {
1015 dlp
= (union DL_primitives
*)ctl
.buf
;
1017 if (expected (DL_OK_ACK
, dlp
, flags
) == -1) {
1021 if (ctl
.len
< sizeof (dl_ok_ack_t
)) {
1022 /* Returned structure is too short */
1030 * dlpiinfoack - receive an ack to a dlinforeq.
1032 static int dlpiinfoack (fd
, bufp
)
1036 union DL_primitives
*dlp
;
1040 ctl
.maxlen
= DLPI_MAXDLBUF
;
1044 if (strgetmsg (fd
, &ctl
, (struct strbuf
*)NULL
, &flags
,
1045 "dlpiinfoack") < 0) {
1049 dlp
= (union DL_primitives
*) ctl
.buf
;
1051 if (expected (DL_INFO_ACK
, dlp
, flags
) == -1) {
1055 if (ctl
.len
< sizeof (dl_info_ack_t
)) {
1056 /* Returned structure is too short */
1064 * dlpiphysaddrack - receive an ack to a dlpiphysaddrreq.
1066 int dlpiphysaddrack (fd
, bufp
)
1070 union DL_primitives
*dlp
;
1074 ctl
.maxlen
= DLPI_MAXDLBUF
;
1078 if (strgetmsg (fd
, &ctl
, (struct strbuf
*)NULL
, &flags
,
1079 "dlpiphysaddrack") < 0) {
1083 dlp
= (union DL_primitives
*)ctl
.buf
;
1085 if (expected (DL_PHYS_ADDR_ACK
, dlp
, flags
) == -1) {
1089 if (ctl
.len
< sizeof (dl_phys_addr_ack_t
)) {
1090 /* Returned structure is too short */
1097 #if defined(USE_DLPI_SEND) || defined(USE_DLPI_RECEIVE)
1098 int dlpiunitdatareq (fd
, addr
, addrlen
, minpri
, maxpri
, dbuf
, dbuflen
)
1100 unsigned char *addr
;
1102 unsigned long minpri
;
1103 unsigned long maxpri
;
1104 unsigned char *dbuf
;
1107 long buf
[DLPI_MAXDLBUF
];
1108 union DL_primitives
*dlp
;
1109 struct strbuf ctl
, data
;
1111 /* Set up the control information... */
1112 dlp
= (union DL_primitives
*)buf
;
1113 dlp
-> unitdata_req
.dl_primitive
= DL_UNITDATA_REQ
;
1114 dlp
-> unitdata_req
.dl_dest_addr_length
= addrlen
;
1115 dlp
-> unitdata_req
.dl_dest_addr_offset
= sizeof (dl_unitdata_req_t
);
1116 dlp
-> unitdata_req
.dl_priority
.dl_min
= minpri
;
1117 dlp
-> unitdata_req
.dl_priority
.dl_max
= maxpri
;
1119 /* Append the destination address */
1120 memcpy ((char *)buf
+ dlp
-> unitdata_req
.dl_dest_addr_offset
,
1124 ctl
.len
= dlp
-> unitdata_req
.dl_dest_addr_offset
+ addrlen
;
1125 ctl
.buf
= (char *)buf
;
1128 data
.buf
= (char *)dbuf
;
1131 /* Send the packet down the wire... */
1132 return putmsg (fd
, &ctl
, &data
, 0);
1135 static int dlpiunitdataind (fd
, daddr
, daddrlen
,
1136 saddr
, saddrlen
, grpaddr
, dbuf
, dlen
)
1138 unsigned char *daddr
;
1139 unsigned long *daddrlen
;
1140 unsigned char *saddr
;
1141 unsigned long *saddrlen
;
1142 unsigned long *grpaddr
;
1143 unsigned char *dbuf
;
1146 long buf
[DLPI_MAXDLBUF
];
1147 union DL_primitives
*dlp
;
1148 struct strbuf ctl
, data
;
1152 /* Set up the msg_buf structure... */
1153 dlp
= (union DL_primitives
*)buf
;
1154 dlp
-> unitdata_ind
.dl_primitive
= DL_UNITDATA_IND
;
1156 ctl
.maxlen
= DLPI_MAXDLBUF
;
1158 ctl
.buf
= (char *)buf
;
1162 data
.buf
= (char *)dbuf
;
1164 result
= getmsg (fd
, &ctl
, &data
, &flags
);
1167 log_debug("dlpiunitdataind: %m");
1171 if (ctl
.len
< sizeof (dl_unitdata_ind_t
) ||
1172 dlp
-> unitdata_ind
.dl_primitive
!= DL_UNITDATA_IND
) {
1176 if (data
.len
<= 0) {
1180 /* Copy sender info */
1183 (char *)buf
+ dlp
-> unitdata_ind
.dl_src_addr_offset
,
1184 dlp
-> unitdata_ind
.dl_src_addr_length
);
1187 *saddrlen
= dlp
-> unitdata_ind
.dl_src_addr_length
;
1190 /* Copy destination info */
1193 (char *)buf
+ dlp
-> unitdata_ind
.dl_dest_addr_offset
,
1194 dlp
-> unitdata_ind
.dl_dest_addr_length
);
1197 *daddrlen
= dlp
-> unitdata_ind
.dl_dest_addr_length
;
1201 *grpaddr
= dlp
-> unitdata_ind
.dl_group_address
;
1206 #endif /* !USE_DLPI_HWADDR: USE_DLPI_RECEIVE || USE_DLPI_SEND */
1209 * expected - see if we got what we wanted.
1211 static int expected (prim
, dlp
, msgflags
)
1213 union DL_primitives
*dlp
;
1216 if (msgflags
!= RS_HIPRI
) {
1217 /* Message was not M_PCPROTO */
1221 if (dlp
->dl_primitive
!= prim
) {
1222 /* Incorrect/unexpected return message */
1230 * strgetmsg - get a message from a stream, with timeout.
1232 static int strgetmsg (fd
, ctlp
, datap
, flagsp
, caller
)
1233 struct strbuf
*ctlp
, *datap
;
1246 pfd
.events
= POLLPRI
; /* We're only interested in knowing
1247 * when we can receive the next high
1252 now
= time (&starttime
);
1253 while (now
<= starttime
+ DLPI_MAXWAIT
) {
1254 to_msec
= ((starttime
+ DLPI_MAXWAIT
) - now
) * 1000;
1255 count
= poll (&pfd
, 1, to_msec
);
1258 /* log_fatal ("strgetmsg: timeout"); */
1260 } else if (count
< 0) {
1261 if (errno
== EAGAIN
|| errno
== EINTR
) {
1265 /* log_fatal ("poll: %m"); */
1274 * Set flags argument and issue getmsg ().
1277 if ((result
= getmsg (fd
, ctlp
, datap
, flagsp
)) < 0) {
1282 * Check for MOREDATA and/or MORECTL.
1284 if (result
& (MORECTL
|MOREDATA
)) {
1289 * Check for at least sizeof (long) control data portion.
1291 if (ctlp
-> len
< sizeof (long)) {
1298 #if defined(USE_DLPI_SEND)
1299 int can_unicast_without_arp (ip
)
1300 struct interface_info
*ip
;
1305 int can_receive_unicast_unconfigured (ip
)
1306 struct interface_info
*ip
;
1311 int supports_multiple_interfaces (ip
)
1312 struct interface_info
*ip
;
1317 void maybe_setup_fallback ()
1319 isc_result_t status
;
1320 struct interface_info
*fbi
= (struct interface_info
*)0;
1321 if (setup_fallback (&fbi
, MDL
)) {
1322 if_register_fallback (fbi
);
1323 status
= omapi_register_io_object ((omapi_object_t
*)fbi
,
1325 fallback_discard
, 0, 0);
1326 if (status
!= ISC_R_SUCCESS
)
1327 log_fatal ("Can't register I/O handle for %s: %s",
1328 fbi
-> name
, isc_result_totext (status
));
1329 interface_dereference (&fbi
, MDL
);
1332 #endif /* USE_DLPI_SEND */
1335 get_hw_addr(const char *name
, struct hardware
*hw
) {
1337 long buf
[DLPI_MAXDLBUF
];
1338 union DL_primitives
*dlp
;
1340 dlp
= (union DL_primitives
*)buf
;
1343 * Open a DLPI device.
1345 sock
= dlpiopen(name
);
1347 log_fatal("Can't open DLPI device for %s: %m", name
);
1351 * Submit a DL_INFO_REQ request, to find the dl_mac_type and
1354 if (dlpiinforeq(sock
) < 0) {
1355 log_fatal("Can't request DLPI MAC type for %s: %m", name
);
1357 if (dlpiinfoack(sock
, (char *)buf
) < 0) {
1358 log_fatal("Can't get DLPI MAC type for %s: %m", name
);
1360 switch (dlp
->info_ack
.dl_mac_type
) {
1361 case DL_CSMACD
: /* IEEE 802.3 */
1363 hw
->hbuf
[0] = HTYPE_ETHER
;
1366 hw
->hbuf
[0] = HTYPE_IEEE802
;
1369 hw
->hbuf
[0] = HTYPE_FDDI
;
1372 log_fatal("%s: unsupported DLPI MAC type %lu", name
,
1373 (unsigned long)dlp
->info_ack
.dl_mac_type
);
1376 if (dlp
->info_ack
.dl_provider_style
== DL_STYLE2
) {
1378 * Attach to the device. If this fails, the device
1381 unit
= dlpiunit((char *)name
);
1383 if (dlpiattachreq(sock
, unit
) < 0 ||
1384 dlpiokack(sock
, (char *)buf
) < 0) {
1385 log_fatal("Can't attach DLPI device for %s: %m",
1391 * Submit a DL_PHYS_ADDR_REQ request, to find
1392 * the hardware address.
1394 if (dlpiphysaddrreq(sock
, DL_CURR_PHYS_ADDR
) < 0) {
1395 log_fatal("Can't request DLPI hardware address for %s: %m",
1398 if (dlpiphysaddrack(sock
, (char *)buf
) < 0) {
1399 log_fatal("Can't get DLPI hardware address for %s: %m",
1402 if (dlp
->physaddr_ack
.dl_addr_length
< sizeof(hw
->hbuf
)) {
1404 (char *)buf
+ dlp
->physaddr_ack
.dl_addr_offset
,
1405 dlp
->physaddr_ack
.dl_addr_length
);
1406 hw
->hlen
= dlp
->physaddr_ack
.dl_addr_length
+ 1;
1409 (char *)buf
+ dlp
->physaddr_ack
.dl_addr_offset
,
1410 sizeof(hw
->hbuf
)-1);
1411 hw
->hlen
= sizeof(hw
->hbuf
);
1416 #endif /* USE_DLPI_SEND || USE_DLPI_RECEIVE || USE_DLPI_HWADDR */