3 Data Link Provider Interface (DLPI) network interface code. */
6 * Copyright (c) 1996-1999 Internet Software Consortium.
7 * Use is subject to license terms which appear in the file named
8 * ISC-LICENSE that should have accompanied this file when you
9 * received it. If a file named ISC-LICENSE did not accompany this
10 * file, or you are not sure the one you have is correct, you may
11 * obtain an applicable copy of the license at:
13 * http://www.isc.org/isc-license-1.0.html.
15 * This file is part of the ISC DHCP distribution. The documentation
16 * associated with this file is listed in the file DOCUMENTATION,
17 * included in the top-level directory of this release.
19 * Support and other services are available for ISC products - see
20 * http://www.isc.org for more information.
22 * This software was written for the Internet Software Consortium
23 * by Eric James Negaard, <lmdejn@lmd.ericsson.se>. To learn more about
24 * the Internet Software Consortium, see ``http://www.vix.com/isc''.
28 * Based largely in part to the existing NIT code in nit.c.
30 * This code has been developed and tested on sparc-based machines running
31 * SunOS 5.5.1, with le and hme network interfaces. It should be pretty
36 * Implementation notes:
38 * I first tried to write this code to the "vanilla" DLPI 2.0 API.
39 * It worked on a Sun Ultra-1 with a hme interface, but didn't work
40 * on Sun SparcStation 5's with "le" interfaces (the packets sent out
41 * via dlpiunitdatareq contained an Ethernet type of 0x0000 instead
42 * of the expected 0x0800).
44 * Therefore I added the "DLPI_RAW" code which is a Sun extension to
45 * the DLPI standard. This code works on both of the above machines.
46 * This is configurable in the OS-dependent include file by defining
49 * It quickly became apparant that I should also use the "pfmod"
50 * STREAMS module to cut down on the amount of user level packet
51 * processing. I don't know how widely available "pfmod" is, so it's
52 * use is conditionally included. This is configurable in the
53 * OS-dependent include file by defining USE_DLPI_PFMOD.
55 * A major quirk on the Sun's at least, is that no packets seem to get
56 * sent out the interface until six seconds after the interface is
57 * first "attached" to [per system reboot] (it's actually from when
58 * the interface is attached, not when it is plumbed, so putting a
59 * sleep into the dhclient-script at PREINIT time doesn't help). I
60 * HAVE tried, without success to poll the fd to see when it is ready
61 * for writing. This doesn't help at all. If the sleeps are not done,
62 * the initial DHCPREQUEST or DHCPDISCOVER never gets sent out, so
63 * I've put them here, when register_send and register_receive are
64 * called (split up into two three-second sleeps between the notices,
65 * so that it doesn't seem like so long when you're watching :-). The
66 * amount of time to sleep is configurable in the OS-dependent include
67 * file by defining DLPI_FIRST_SEND_WAIT to be the number of seconds
72 static char copyright
[] =
73 "$Id: dlpi.c,v 1.18 2000/03/06 19:39:53 mellon Exp $ Copyright (c) 1998, 1999 The Internet Software Consortium. All rights reserved.\n";
78 #if defined (USE_DLPI_SEND) || defined (USE_DLPI_RECEIVE)
80 # include <sys/ioctl.h>
81 # include <sys/time.h>
82 # include <sys/dlpi.h>
84 # ifdef USE_DLPI_PFMOD
85 # include <sys/pfmod.h>
91 # include <netinet/in_systm.h>
92 # include "includes/netinet/ip.h"
93 # include "includes/netinet/udp.h"
94 # include "includes/netinet/if_ether.h"
96 # ifdef USE_DLPI_PFMOD
98 # define DLPI_MODNAME "DLPI+RAW+PFMOD"
100 # define DLPI_MODNAME "DLPI+PFMOD"
104 # define DLPI_MODNAME "DLPI+RAW"
106 # define DLPI_MODNAME "DLPI"
111 # define ABS(x) ((x) >= 0 ? (x) : 0-(x))
114 static int strioctl
PROTO ((int fd
, int cmd
, int timeout
, int len
, char *dp
));
116 #define DLPI_MAXDLBUF 8192 /* Buffer size */
117 #define DLPI_MAXDLADDR 1024 /* Max address size */
118 #define DLPI_DEVDIR "/dev/" /* Device directory */
119 #define DLPI_DEFAULTSAP 0x0800 /* IP protocol */
121 static void dlpi_makeaddr
PROTO ((unsigned char *physaddr
, int physaddrlen
,
122 unsigned char *sap
, int saplen
,
123 unsigned char *buf
));
124 static void dlpi_parseaddr
PROTO ((unsigned char *buf
,
125 unsigned char *physaddr
,
126 int physaddrlen
, unsigned char *sap
,
128 static int dlpiopen
PROTO ((char *ifname
));
129 static int dlpiunit
PROTO ((char *ifname
));
130 static int dlpiinforeq
PROTO ((int fd
));
131 static int dlpiphysaddrreq
PROTO ((int fd
, unsigned long addrtype
));
132 static int dlpiattachreq
PROTO ((int fd
, unsigned long ppa
));
133 static int dlpibindreq
PROTO ((int fd
, unsigned long sap
, unsigned long max_conind
,
134 unsigned long service_mode
, unsigned long conn_mgmt
,
135 unsigned long xidtest
));
136 static int dlpidetachreq
PROTO ((int fd
));
137 static int dlpiunbindreq
PROTO ((int fd
));
138 static int dlpiokack
PROTO ((int fd
, char *bufp
));
139 static int dlpiinfoack
PROTO ((int fd
, char *bufp
));
140 static int dlpiphysaddrack
PROTO ((int fd
, char *bufp
));
141 static int dlpibindack
PROTO ((int fd
, char *bufp
));
142 static int dlpiunitdatareq
PROTO ((int fd
, unsigned char *addr
,
143 int addrlen
, unsigned long minpri
,
144 unsigned long maxpri
, unsigned char *data
,
146 static int dlpiunitdataind
PROTO ((int fd
,
147 unsigned char *dstaddr
,
148 unsigned long *dstaddrlen
,
149 unsigned char *srcaddr
,
150 unsigned long *srcaddrlen
,
151 unsigned long *grpaddr
,
156 static void sigalrm
PROTO ((int sig
));
158 static int expected
PROTO ((unsigned long prim
, union DL_primitives
*dlp
,
160 static int strgetmsg
PROTO ((int fd
, struct strbuf
*ctlp
,
161 struct strbuf
*datap
, int *flagsp
,
164 /* Reinitializes the specified interface after an address change. This
165 is not required for packet-filter APIs. */
168 void if_reinitialize_send (info
)
169 struct interface_info
*info
;
174 #ifdef USE_DLPI_RECEIVE
175 void if_reinitialize_receive (info
)
176 struct interface_info
*info
;
181 /* Called by get_interface_list for each interface that's discovered.
182 Opens a packet filter for each interface and adds it to the select
185 int if_register_dlpi (info
)
186 struct interface_info
*info
;
190 long buf
[DLPI_MAXDLBUF
];
191 union DL_primitives
*dlp
;
193 dlp
= (union DL_primitives
*)buf
;
195 /* Open a DLPI device */
196 if ((sock
= dlpiopen (info
-> name
)) < 0) {
197 log_fatal ("Can't open DLPI device for %s: %m", info
-> name
);
201 * Get information about the provider.
205 * Submit a DL_INFO_REQ request, to find
206 * the dl_mac_type and dl_provider_style
208 if (dlpiinforeq(sock
) < 0 || dlpiinfoack(sock
, (char *)buf
) < 0) {
209 log_fatal ("Can't get DLPI MAC type for %s: %m", info
-> name
);
211 switch (dlp
-> info_ack
.dl_mac_type
) {
212 case DL_CSMACD
: /* IEEE 802.3 */
214 info
-> hw_address
.hbuf
[0] = HTYPE_ETHER
;
216 /* adding token ring 5/1999 - mayer@ping.at */
218 info
-> hw_address
.hbuf
[0] = HTYPE_IEEE802
;
221 info
-> hw_address
.hbuf
[0] = HTYPE_FDDI
;
224 log_fatal ("%s: unknown DLPI MAC type %ld",
226 dlp
-> info_ack
.dl_mac_type
);
231 if (dlp
-> info_ack
.dl_provider_style
== DL_STYLE2
) {
233 * Attach to the device. If this fails, the device
236 unit
= dlpiunit (info
-> name
);
238 if (dlpiattachreq (sock
, unit
) < 0
239 || dlpiokack (sock
, (char *)buf
) < 0) {
240 log_fatal ("Can't attach DLPI device for %s: %m", info
-> name
);
245 * Bind to the IP service access point (SAP), connectionless (CLDLS).
247 if (dlpibindreq (sock
, DLPI_DEFAULTSAP
, 0, DL_CLDLS
, 0, 0) < 0
248 || dlpibindack (sock
, (char *)buf
) < 0) {
249 log_fatal ("Can't bind DLPI device for %s: %m", info
-> name
);
253 * Submit a DL_PHYS_ADDR_REQ request, to find
254 * the hardware address
256 if (dlpiphysaddrreq (sock
, DL_CURR_PHYS_ADDR
) < 0
257 || dlpiphysaddrack (sock
, (char *)buf
) < 0) {
258 log_fatal ("Can't get DLPI hardware address for %s: %m",
262 info
-> hw_address
.hlen
= dlp
-> physaddr_ack
.dl_addr_length
+ 1;
263 memcpy (&info
-> hw_address
.hbuf
[1],
264 (char *)buf
+ dlp
-> physaddr_ack
.dl_addr_offset
,
265 dlp
-> physaddr_ack
.dl_addr_length
);
268 if (strioctl (sock
, DLIOCRAW
, INFTIM
, 0, 0) < 0) {
269 log_fatal ("Can't set DLPI RAW mode for %s: %m",
274 #ifdef USE_DLPI_PFMOD
275 if (ioctl (sock
, I_PUSH
, "pfmod") < 0) {
276 log_fatal ("Can't push packet filter onto DLPI for %s: %m",
285 strioctl (fd
, cmd
, timeout
, len
, dp
)
296 sio
.ic_timout
= timeout
;
300 if ((rslt
= ioctl (fd
, I_STR
, &sio
)) < 0) {
308 void if_register_send (info
)
309 struct interface_info
*info
;
311 /* If we're using the DLPI API for sending and receiving,
312 we don't need to register this interface twice. */
313 #ifndef USE_DLPI_RECEIVE
314 # ifdef USE_DLPI_PFMOD
315 struct packetfilt pf
;
318 info
-> wfdesc
= if_register_dlpi (info
);
320 # ifdef USE_DLPI_PFMOD
321 /* Set up an PFMOD filter that rejects everything... */
324 pf
.Pf_Filter
[0] = ENF_PUSHZERO
;
326 /* Install the filter */
327 if (strioctl (info
-> wfdesc
, PFIOCSETF
, INFTIM
,
328 sizeof (pf
), (char *)&pf
) < 0) {
329 log_fatal ("Can't set PFMOD send filter on %s: %m", info
-> name
);
332 # endif /* USE_DLPI_PFMOD */
333 #else /* !defined (USE_DLPI_RECEIVE) */
335 * If using DLPI for both send and receive, simply re-use
336 * the read file descriptor that was set up earlier.
338 info
-> wfdesc
= info
-> rfdesc
;
341 if (!quiet_interface_discovery
)
342 log_info ("Sending on DLPI/%s/%s%s%s",
344 print_hw_addr (info
-> hw_address
.hbuf
[0],
345 info
-> hw_address
.hlen
- 1,
346 &info
-> hw_address
.hbuf
[1]),
347 (info
-> shared_network
? "/" : ""),
348 (info
-> shared_network
?
349 info
-> shared_network
-> name
: ""));
351 #ifdef DLPI_FIRST_SEND_WAIT
352 /* See the implementation notes at the beginning of this file */
353 # ifdef USE_DLPI_RECEIVE
354 sleep (DLPI_FIRST_SEND_WAIT
- (DLPI_FIRST_SEND_WAIT
/ 2));
356 sleep (DLPI_FIRST_SEND_WAIT
);
361 void if_deregister_send (info
)
362 struct interface_info
*info
;
364 /* If we're using the DLPI API for sending and receiving,
365 we don't need to register this interface twice. */
366 #ifndef USE_DLPI_RECEIVE
367 close (info
-> wfdesc
);
371 if (!quiet_interface_discovery
)
372 log_info ("Disabling output on DLPI/%s/%s%s%s",
374 print_hw_addr (info
-> hw_address
.hbuf
[0],
375 info
-> hw_address
.hlen
- 1,
376 &info
-> hw_address
.hbuf
[1]),
377 (info
-> shared_network
? "/" : ""),
378 (info
-> shared_network
?
379 info
-> shared_network
-> name
: ""));
381 #endif /* USE_DLPI_SEND */
383 #ifdef USE_DLPI_RECEIVE
384 /* Packet filter program...
385 XXX Changes to the filter program may require changes to the constant
386 offsets used in if_register_send to patch the NIT program! XXX */
388 void if_register_receive (info
)
389 struct interface_info
*info
;
391 #ifdef USE_DLPI_PFMOD
392 struct packetfilt pf
;
395 /* Open a DLPI device and hang it on this interface... */
396 info
-> rfdesc
= if_register_dlpi (info
);
398 #ifdef USE_DLPI_PFMOD
399 /* Set up the PFMOD filter program. */
400 /* XXX Unlike the BPF filter program, this one won't work if the
401 XXX IP packet is fragmented or if there are options on the IP
407 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ 6;
408 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
+ ENF_CAND
;
409 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (ETHERTYPE_IP
);
410 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
;
411 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (IPPROTO_UDP
);
412 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ 11;
413 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSH00FF
+ ENF_AND
;
414 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_CAND
;
415 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ 18;
416 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
+ ENF_CAND
;
417 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (local_port
);
420 * The packets that will be received on this file descriptor
421 * will be IP packets (due to the SAP that was specified in
422 * the dlbind call). There will be no ethernet header.
423 * Therefore, setup the packet filter to check the protocol
424 * field for UDP, and the destination port number equal
425 * to the local port. All offsets are relative to the start
428 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
;
429 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (IPPROTO_UDP
);
430 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ 4;
431 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSH00FF
+ ENF_AND
;
432 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_CAND
;
433 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ 11;
434 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
+ ENF_CAND
;
435 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (local_port
);
438 /* Install the filter... */
439 if (strioctl (info
-> rfdesc
, PFIOCSETF
, INFTIM
,
440 sizeof (pf
), (char *)&pf
) < 0) {
441 log_fatal ("Can't set PFMOD receive filter on %s: %m", info
-> name
);
445 if (!quiet_interface_discovery
)
446 log_info ("Listening on DLPI/%s/%s%s%s",
448 print_hw_addr (info
-> hw_address
.hbuf
[0],
449 info
-> hw_address
.hlen
- 1,
450 &info
-> hw_address
.hbuf
[1]),
451 (info
-> shared_network
? "/" : ""),
452 (info
-> shared_network
?
453 info
-> shared_network
-> name
: ""));
455 #ifdef DLPI_FIRST_SEND_WAIT
456 /* See the implementation notes at the beginning of this file */
457 # ifdef USE_DLPI_SEND
458 sleep (DLPI_FIRST_SEND_WAIT
/ 2);
460 sleep (DLPI_FIRST_SEND_WAIT
);
465 void if_deregister_receive (info
)
466 struct interface_info
*info
;
468 /* If we're using the DLPI API for sending and receiving,
469 we don't need to register this interface twice. */
470 #ifndef USE_DLPI_SEND
471 close (info
-> rfdesc
);
475 if (!quiet_interface_discovery
)
476 log_info ("Disabling input 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 #endif /* USE_DLPI_RECEIVE */
488 ssize_t
send_packet (interface
, packet
, raw
, len
, from
, to
, hto
)
489 struct interface_info
*interface
;
490 struct packet
*packet
;
491 struct dhcp_packet
*raw
;
494 struct sockaddr_in
*to
;
495 struct hardware
*hto
;
498 unsigned char dbuf
[1536];
499 unsigned char sap
[2];
500 unsigned char dstaddr
[DLPI_MAXDLADDR
];
505 if (!strcmp (interface
-> name
, "fallback"))
506 return send_fallback (interface
, packet
, raw
,
511 /* Assemble the headers... */
513 assemble_hw_header (interface
, dbuf
, &dbuflen
, hto
);
515 assemble_udp_ip_header (interface
, dbuf
, &dbuflen
, from
.s_addr
,
516 to
-> sin_addr
.s_addr
, to
-> sin_port
,
517 (unsigned char *)raw
, len
);
519 /* Copy the data into the buffer (yuk). */
520 memcpy (dbuf
+ dbuflen
, raw
, len
);
524 result
= write (interface
-> wfdesc
, dbuf
, dbuflen
);
526 /* XXX: Assumes ethernet, with two byte SAP */
527 sap
[0] = 0x08; /* ETHERTYPE_IP, high byte */
528 sap
[1] = 0x0; /* ETHERTYPE_IP, low byte */
529 saplen
= -2; /* -2 indicates a two byte SAP at the end
532 /* Setup the destination address */
533 if (hto
&& hto
-> hlen
== interface
-> hw_address
.hlen
) {
534 dlpi_makeaddr (&hto
-> hbuf
[1],
535 hto
-> hlen
- 1, sap
, saplen
, dstaddr
);
537 /* XXX: Assumes broadcast addr is all ones */
538 /* Really should get the broadcast address as part of the
539 * dlpiinforeq, and store it somewhere in the interface structure.
541 unsigned char bcast_ether
[DLPI_MAXDLADDR
];
543 memset ((char *)bcast_ether
, 0xFF,
544 interface
-> hw_address
.hlen
- 1);
545 dlpi_makeaddr (bcast_ether
, interface
-> hw_address
.hlen
- 1,
546 sap
, saplen
, dstaddr
);
548 addrlen
= interface
-> hw_address
.hlen
- 1 + ABS (saplen
);
550 /* Send the packet down the wire... */
551 result
= dlpiunitdatareq (interface
-> wfdesc
, dstaddr
, addrlen
,
552 0, 0, dbuf
, dbuflen
);
555 log_error ("send_packet: %m");
558 #endif /* USE_DLPI_SEND */
560 #ifdef USE_DLPI_RECEIVE
561 ssize_t
receive_packet (interface
, buf
, len
, from
, hfrom
)
562 struct interface_info
*interface
;
565 struct sockaddr_in
*from
;
566 struct hardware
*hfrom
;
568 unsigned char dbuf
[1536];
569 unsigned char sap
[2];
570 unsigned char srcaddr
[DLPI_MAXDLADDR
];
571 unsigned long srcaddrlen
;
580 length
= read (interface
-> rfdesc
, dbuf
, sizeof (dbuf
));
582 length
= dlpiunitdataind (interface
-> rfdesc
, (unsigned char *)NULL
,
583 (unsigned long *)NULL
, srcaddr
, &srcaddrlen
,
584 (unsigned long *)NULL
, dbuf
, sizeof (dbuf
));
592 /* Copy sender info */
593 /* XXX: Assumes ethernet, where SAP comes at end of haddr */
595 if (hfrom
&& (srcaddrlen
==
596 ABS (saplen
) + interface
-> hw_address
.hlen
- 1)) {
597 hfrom
-> hbuf
[0] = interface
-> hw_address
.hbuf
[0];
598 hfrom
-> hlen
= interface
-> hw_address
.hlen
;
599 dlpi_parseaddr (srcaddr
, &hfrom
-> hbuf
[1],
600 interface
-> hw_address
.hlen
- 1, sap
, saplen
);
602 memset (hfrom
, '\0', sizeof *hfrom
);
606 /* Decode the IP and UDP headers... */
609 /* Decode the physical header... */
610 offset
= decode_hw_header (interface
, dbuf
, bufix
, hfrom
);
612 /* If a physical layer checksum failed (dunno of any
613 physical layer that supports this, but WTH), skip this
621 offset
= decode_udp_ip_header (interface
, dbuf
, bufix
,
622 from
, (unsigned char *)0, length
);
624 /* If the IP or UDP checksum was bad, skip the packet... */
632 /* Copy out the data in the packet... */
633 memcpy (buf
, &dbuf
[bufix
], length
);
638 /* Common DLPI routines ...
640 * Written by Eric James Negaard, <lmdejn@lmd.ericsson.se>
642 * Based largely in part to the example code contained in the document
643 * "How to Use the STREAMS Data Link Provider Interface (DLPI)", written
644 * by Neal Nuckolls of SunSoft Internet Engineering.
646 * This code has been developed and tested on sparc-based machines running
647 * SunOS 5.5.1, with le and hme network interfaces. It should be pretty
650 * The usual disclaimers apply. This code works for me. Don't blame me
651 * if it makes your machine or network go down in flames. That taken
652 * into consideration, use this code as you wish. If you make usefull
653 * modifications I'd appreciate hearing about it.
656 #define DLPI_MAXWAIT 15 /* Max timeout */
658 static void dlpi_makeaddr (physaddr
, physaddrlen
, sap
, saplen
, buf
)
659 unsigned char *physaddr
;
666 * If the saplen is negative, the SAP goes at the end of the address,
667 * otherwise it goes at the beginning.
670 memcpy ((char *)buf
, (char *)sap
, saplen
);
671 memcpy ((char *)&buf
[saplen
], (char *)physaddr
, physaddrlen
);
673 memcpy ((char *)buf
, (char *)physaddr
, physaddrlen
);
674 memcpy ((char *)&buf
[physaddrlen
], (char *)sap
, 0 - saplen
);
678 static void dlpi_parseaddr (buf
, physaddr
, physaddrlen
, sap
, saplen
)
680 unsigned char *physaddr
;
686 * If the saplen is negative, the SAP is at the end of the address,
687 * otherwise it is at the beginning.
690 memcpy ((char *)sap
, (char *)buf
, saplen
);
691 memcpy ((char *)physaddr
, (char *)&buf
[saplen
], physaddrlen
);
693 memcpy ((char *)physaddr
, (char *)buf
, physaddrlen
);
694 memcpy ((char *)sap
, (char *)&buf
[physaddrlen
], 0 - saplen
);
699 * Parse an interface name and extract the unit number
702 static int dlpiunit (ifname
)
713 /* Advance to the end of the name */
716 /* Back up to the start of the first digit */
717 while ((*(cp
-1) >= '0' && *(cp
-1) <= '9') || *(cp
- 1) == ':') cp
--;
719 /* Convert the unit number */
721 while (*cp
>= '0' && *cp
<= '9') {
723 unit
+= (*cp
++ - '0');
730 * dlpiopen - open the DLPI device for a given interface name
732 static int dlpiopen (ifname
)
742 /* Open a DLPI device */
743 if (*ifname
== '/') {
746 /* Prepend the device directory */
747 memcpy (devname
, DLPI_DEVDIR
, strlen (DLPI_DEVDIR
));
748 dp
= &devname
[strlen (DLPI_DEVDIR
)];
751 /* Find the end of the interface name */
755 /* And back up to the first digit (unit number) */
756 while ((*(ep
- 1) >= '0' && *(ep
- 1) <= '9') || *(ep
- 1) == ':')
759 /* Copy everything up to the unit number */
765 return open (devname
, O_RDWR
, 0);
769 * dlpiinforeq - request information about the data link provider.
772 static int dlpiinforeq (fd
)
775 dl_info_req_t info_req
;
779 info_req
.dl_primitive
= DL_INFO_REQ
;
782 ctl
.len
= sizeof (info_req
);
783 ctl
.buf
= (char *)&info_req
;
787 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
791 * dlpiphysaddrreq - request the current physical address.
793 static int dlpiphysaddrreq (fd
, addrtype
)
795 unsigned long addrtype
;
797 dl_phys_addr_req_t physaddr_req
;
801 physaddr_req
.dl_primitive
= DL_PHYS_ADDR_REQ
;
802 physaddr_req
.dl_addr_type
= addrtype
;
805 ctl
.len
= sizeof (physaddr_req
);
806 ctl
.buf
= (char *)&physaddr_req
;
810 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
814 * dlpiattachreq - send a request to attach to a specific unit.
816 static int dlpiattachreq (fd
, ppa
)
820 dl_attach_req_t attach_req
;
824 attach_req
.dl_primitive
= DL_ATTACH_REQ
;
825 attach_req
.dl_ppa
= ppa
;
828 ctl
.len
= sizeof (attach_req
);
829 ctl
.buf
= (char *)&attach_req
;
833 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
837 * dlpibindreq - send a request to bind to a specific SAP address.
839 static int dlpibindreq (fd
, sap
, max_conind
, service_mode
, conn_mgmt
, xidtest
)
841 unsigned long max_conind
;
842 unsigned long service_mode
;
843 unsigned long conn_mgmt
;
844 unsigned long xidtest
;
847 dl_bind_req_t bind_req
;
851 bind_req
.dl_primitive
= DL_BIND_REQ
;
852 bind_req
.dl_sap
= sap
;
853 bind_req
.dl_max_conind
= max_conind
;
854 bind_req
.dl_service_mode
= service_mode
;
855 bind_req
.dl_conn_mgmt
= conn_mgmt
;
856 bind_req
.dl_xidtest_flg
= xidtest
;
859 ctl
.len
= sizeof (bind_req
);
860 ctl
.buf
= (char *)&bind_req
;
864 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
868 * dlpiunbindreq - send a request to unbind.
870 static int dlpiunbindreq (fd
)
873 dl_unbind_req_t unbind_req
;
877 unbind_req
.dl_primitive
= DL_UNBIND_REQ
;
880 ctl
.len
= sizeof (unbind_req
);
881 ctl
.buf
= (char *)&unbind_req
;
885 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
890 * dlpidetachreq - send a request to detach.
892 static int dlpidetachreq (fd
)
895 dl_detach_req_t detach_req
;
899 detach_req
.dl_primitive
= DL_DETACH_REQ
;
902 ctl
.len
= sizeof (detach_req
);
903 ctl
.buf
= (char *)&detach_req
;
907 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
912 * dlpibindack - receive an ack to a dlbindreq.
914 static int dlpibindack (fd
, bufp
)
918 union DL_primitives
*dlp
;
922 ctl
.maxlen
= DLPI_MAXDLBUF
;
926 if (strgetmsg (fd
, &ctl
,
927 (struct strbuf
*)NULL
, &flags
, "dlpibindack") < 0) {
931 dlp
= (union DL_primitives
*)ctl
.buf
;
933 if (!expected (DL_BIND_ACK
, dlp
, flags
) < 0) {
937 if (ctl
.len
< sizeof (dl_bind_ack_t
)) {
938 /* Returned structure is too short */
946 * dlpiokack - general acknowledgement reception.
948 static int dlpiokack (fd
, bufp
)
952 union DL_primitives
*dlp
;
956 ctl
.maxlen
= DLPI_MAXDLBUF
;
960 if (strgetmsg (fd
, &ctl
,
961 (struct strbuf
*)NULL
, &flags
, "dlpiokack") < 0) {
965 dlp
= (union DL_primitives
*)ctl
.buf
;
967 if (!expected (DL_OK_ACK
, dlp
, flags
) < 0) {
971 if (ctl
.len
< sizeof (dl_ok_ack_t
)) {
972 /* Returned structure is too short */
980 * dlpiinfoack - receive an ack to a dlinforeq.
982 static int dlpiinfoack (fd
, bufp
)
986 union DL_primitives
*dlp
;
990 ctl
.maxlen
= DLPI_MAXDLBUF
;
994 if (strgetmsg (fd
, &ctl
, (struct strbuf
*)NULL
, &flags
,
995 "dlpiinfoack") < 0) {
999 dlp
= (union DL_primitives
*) ctl
.buf
;
1001 if (!expected (DL_INFO_ACK
, dlp
, flags
) < 0) {
1005 if (ctl
.len
< sizeof (dl_info_ack_t
)) {
1006 /* Returned structure is too short */
1014 * dlpiphysaddrack - receive an ack to a dlpiphysaddrreq.
1016 int dlpiphysaddrack (fd
, bufp
)
1020 union DL_primitives
*dlp
;
1024 ctl
.maxlen
= DLPI_MAXDLBUF
;
1028 if (strgetmsg (fd
, &ctl
, (struct strbuf
*)NULL
, &flags
,
1029 "dlpiphysaddrack") < 0) {
1033 dlp
= (union DL_primitives
*)ctl
.buf
;
1035 if (!expected (DL_PHYS_ADDR_ACK
, dlp
, flags
) < 0) {
1039 if (ctl
.len
< sizeof (dl_phys_addr_ack_t
)) {
1040 /* Returned structure is too short */
1047 int dlpiunitdatareq (fd
, addr
, addrlen
, minpri
, maxpri
, dbuf
, dbuflen
)
1049 unsigned char *addr
;
1051 unsigned long minpri
;
1052 unsigned long maxpri
;
1053 unsigned char *dbuf
;
1056 long buf
[DLPI_MAXDLBUF
];
1057 union DL_primitives
*dlp
;
1058 struct strbuf ctl
, data
;
1060 /* Set up the control information... */
1061 dlp
= (union DL_primitives
*)buf
;
1062 dlp
-> unitdata_req
.dl_primitive
= DL_UNITDATA_REQ
;
1063 dlp
-> unitdata_req
.dl_dest_addr_length
= addrlen
;
1064 dlp
-> unitdata_req
.dl_dest_addr_offset
= sizeof (dl_unitdata_req_t
);
1065 dlp
-> unitdata_req
.dl_priority
.dl_min
= minpri
;
1066 dlp
-> unitdata_req
.dl_priority
.dl_max
= maxpri
;
1068 /* Append the destination address */
1069 memcpy ((char *)buf
+ dlp
-> unitdata_req
.dl_dest_addr_offset
,
1073 ctl
.len
= dlp
-> unitdata_req
.dl_dest_addr_offset
+ addrlen
;
1074 ctl
.buf
= (char *)buf
;
1077 data
.buf
= (char *)dbuf
;
1080 /* Send the packet down the wire... */
1081 return putmsg (fd
, &ctl
, &data
, 0);
1084 static int dlpiunitdataind (fd
, daddr
, daddrlen
,
1085 saddr
, saddrlen
, grpaddr
, dbuf
, dlen
)
1087 unsigned char *daddr
;
1088 unsigned long *daddrlen
;
1089 unsigned char *saddr
;
1090 unsigned long *saddrlen
;
1091 unsigned long *grpaddr
;
1092 unsigned char *dbuf
;
1095 long buf
[DLPI_MAXDLBUF
];
1096 union DL_primitives
*dlp
;
1097 struct strbuf ctl
, data
;
1101 /* Set up the msg_buf structure... */
1102 dlp
= (union DL_primitives
*)buf
;
1103 dlp
-> unitdata_ind
.dl_primitive
= DL_UNITDATA_IND
;
1105 ctl
.maxlen
= DLPI_MAXDLBUF
;
1107 ctl
.buf
= (char *)buf
;
1111 data
.buf
= (char *)dbuf
;
1113 result
= getmsg (fd
, &ctl
, &data
, &flags
);
1119 if (ctl
.len
< sizeof (dl_unitdata_ind_t
) ||
1120 dlp
-> unitdata_ind
.dl_primitive
!= DL_UNITDATA_IND
) {
1124 if (data
.len
<= 0) {
1128 /* Copy sender info */
1130 memcpy (saddr
, &buf
[dlp
-> unitdata_ind
.dl_src_addr_offset
],
1131 dlp
-> unitdata_ind
.dl_src_addr_length
);
1134 *saddrlen
= dlp
-> unitdata_ind
.dl_src_addr_length
;
1137 /* Copy destination info */
1139 memcpy (daddr
, &buf
[dlp
-> unitdata_ind
.dl_dest_addr_offset
],
1140 dlp
-> unitdata_ind
.dl_dest_addr_length
);
1143 *daddrlen
= dlp
-> unitdata_ind
.dl_dest_addr_length
;
1147 *grpaddr
= dlp
-> unitdata_ind
.dl_group_address
;
1154 * expected - see if we got what we wanted.
1156 static int expected (prim
, dlp
, msgflags
)
1158 union DL_primitives
*dlp
;
1161 if (msgflags
!= RS_HIPRI
) {
1162 /* Message was not M_PCPROTO */
1166 if (dlp
-> dl_primitive
!= prim
) {
1167 /* Incorrect/unexpected return message */
1175 * strgetmsg - get a message from a stream, with timeout.
1177 static int strgetmsg (fd
, ctlp
, datap
, flagsp
, caller
)
1178 struct strbuf
*ctlp
, *datap
;
1194 pfd
.events
= POLLPRI
; /* We're only interested in knowing
1195 * when we can receive the next high
1200 now
= time (&starttime
);
1201 while (now
<= starttime
+ DLPI_MAXWAIT
) {
1202 to_msec
= ((starttime
+ DLPI_MAXWAIT
) - now
) * 1000;
1203 count
= poll (&pfd
, 1, to_msec
);
1206 /* log_fatal ("strgetmsg: timeout"); */
1208 } else if (count
< 0) {
1209 if (errno
== EAGAIN
|| errno
== EINTR
) {
1213 /* log_fatal ("poll: %m"); */
1220 #else /* defined (USE_POLL) */
1222 * Start timer. Can't use select, since it might return true if there
1223 * were non High-Priority data available on the stream.
1225 (void) sigset (SIGALRM
, sigalrm
);
1227 if (alarm (DLPI_MAXWAIT
) < 0) {
1228 /* log_fatal ("alarm: %m"); */
1231 #endif /* !defined (USE_POLL) */
1234 * Set flags argument and issue getmsg ().
1237 if ((result
= getmsg (fd
, ctlp
, datap
, flagsp
)) < 0) {
1245 if (alarm (0) < 0) {
1246 /* log_fatal ("alarm: %m"); */
1252 * Check for MOREDATA and/or MORECTL.
1254 if (result
& (MORECTL
|MOREDATA
)) {
1259 * Check for at least sizeof (long) control data portion.
1261 if (ctlp
-> len
< sizeof (long)) {
1270 * sigalrm - handle alarms.
1272 static void sigalrm (sig
)
1275 fprintf (stderr
, "strgetmsg: timeout");
1278 #endif /* !defined (USE_POLL) */
1280 int can_unicast_without_arp (ip
)
1281 struct interface_info
*ip
;
1286 int can_receive_unicast_unconfigured (ip
)
1287 struct interface_info
*ip
;
1292 void maybe_setup_fallback ()
1294 isc_result_t status
;
1295 struct interface_info
*fbi
;
1296 fbi
= setup_fallback ();
1298 if_register_fallback (fbi
);
1300 fbi
-> type
= dhcp_type_interface
;
1301 status
= omapi_register_io_object ((omapi_object_t
*)fbi
,
1303 fallback_discard
, 0, 0);
1304 if (status
!= ISC_R_SUCCESS
)
1305 log_fatal ("Can't register I/O handle for %s: %s",
1306 fbi
-> name
, isc_result_totext (status
));
1309 #endif /* USE_DLPI */