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
73 #if defined (USE_DLPI_SEND) || defined (USE_DLPI_RECEIVE)
75 # include <sys/ioctl.h>
76 # include <sys/time.h>
77 # include <sys/dlpi.h>
79 # ifdef USE_DLPI_PFMOD
80 # include <sys/pfmod.h>
86 # include <netinet/in_systm.h>
87 # include "includes/netinet/ip.h"
88 # include "includes/netinet/udp.h"
89 # include "includes/netinet/if_ether.h"
91 # ifdef USE_DLPI_PFMOD
93 # define DLPI_MODNAME "DLPI+RAW+PFMOD"
95 # define DLPI_MODNAME "DLPI+PFMOD"
99 # define DLPI_MODNAME "DLPI+RAW"
101 # define DLPI_MODNAME "DLPI"
106 # define ABS(x) ((x) >= 0 ? (x) : 0-(x))
110 static char copyright
[] =
111 "$Id: dlpi.c,v 1.8 1999/03/16 05:50:33 mellon Exp $ Copyright (c) 1998, 1999 The Internet Software Consortium. All rights reserved.\n";
112 #endif /* not lint */
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
.htype
= HTYPE_ETHER
;
217 info
-> hw_address
.htype
= HTYPE_FDDI
;
220 log_fatal ("%s: unknown DLPI MAC type %d",
222 dlp
-> info_ack
.dl_mac_type
);
227 if (dlp
-> info_ack
.dl_provider_style
== DL_STYLE2
) {
229 * Attach to the device. If this fails, the device
232 unit
= dlpiunit (info
-> name
);
234 if (dlpiattachreq (sock
, unit
) < 0
235 || dlpiokack (sock
, (char *)buf
) < 0) {
236 log_fatal ("Can't attach DLPI device for %s: %m", info
-> name
);
241 * Bind to the IP service access point (SAP), connectionless (CLDLS).
243 if (dlpibindreq (sock
, DLPI_DEFAULTSAP
, 0, DL_CLDLS
, 0, 0) < 0
244 || dlpibindack (sock
, (char *)buf
) < 0) {
245 log_fatal ("Can't bind DLPI device for %s: %m", info
-> name
);
249 * Submit a DL_PHYS_ADDR_REQ request, to find
250 * the hardware address
252 if (dlpiphysaddrreq (sock
, DL_CURR_PHYS_ADDR
) < 0
253 || dlpiphysaddrack (sock
, (char *)buf
) < 0) {
254 log_fatal ("Can't get DLPI hardware address for %s: %m",
258 info
-> hw_address
.hlen
= dlp
-> physaddr_ack
.dl_addr_length
;
259 memcpy (info
-> hw_address
.haddr
,
260 (char *)buf
+ dlp
-> physaddr_ack
.dl_addr_offset
,
261 dlp
-> physaddr_ack
.dl_addr_length
);
264 if (strioctl (sock
, DLIOCRAW
, INFTIM
, 0, 0) < 0) {
265 log_fatal ("Can't set DLPI RAW mode for %s: %m",
270 #ifdef USE_DLPI_PFMOD
271 if (ioctl (sock
, I_PUSH
, "pfmod") < 0) {
272 log_fatal ("Can't push packet filter onto DLPI for %s: %m",
281 strioctl (fd
, cmd
, timeout
, len
, dp
)
292 sio
.ic_timout
= timeout
;
296 if ((rslt
= ioctl (fd
, I_STR
, &sio
)) < 0) {
304 void if_register_send (info
)
305 struct interface_info
*info
;
307 /* If we're using the DLPI API for sending and receiving,
308 we don't need to register this interface twice. */
309 #ifndef USE_DLPI_RECEIVE
310 # ifdef USE_DLPI_PFMOD
311 struct packetfilt pf
;
314 info
-> wfdesc
= if_register_dlpi (info
);
316 # ifdef USE_DLPI_PFMOD
317 /* Set up an PFMOD filter that rejects everything... */
320 pf
.Pf_Filter
[0] = ENF_PUSHZERO
;
322 /* Install the filter */
323 if (strioctl (info
-> wfdesc
, PFIOCSETF
, INFTIM
,
324 sizeof (pf
), (char *)&pf
) < 0) {
325 log_fatal ("Can't set PFMOD send filter on %s: %m", info
-> name
);
328 # endif /* USE_DLPI_PFMOD */
329 #else /* !defined (USE_DLPI_RECEIVE) */
331 * If using DLPI for both send and receive, simply re-use
332 * the read file descriptor that was set up earlier.
334 info
-> wfdesc
= info
-> rfdesc
;
337 if (!quiet_interface_discovery
)
338 log_info ("Sending on DLPI/%s/%s%s%s",
340 print_hw_addr (info
-> hw_address
.htype
,
341 info
-> hw_address
.hlen
,
342 info
-> hw_address
.haddr
),
343 (info
-> shared_network
? "/" : ""),
344 (info
-> shared_network
?
345 info
-> shared_network
-> name
: ""));
347 #ifdef DLPI_FIRST_SEND_WAIT
348 /* See the implementation notes at the beginning of this file */
349 # ifdef USE_DLPI_RECEIVE
350 sleep (DLPI_FIRST_SEND_WAIT
- (DLPI_FIRST_SEND_WAIT
/ 2));
352 sleep (DLPI_FIRST_SEND_WAIT
);
356 #endif /* USE_DLPI_SEND */
358 #ifdef USE_DLPI_RECEIVE
359 /* Packet filter program...
360 XXX Changes to the filter program may require changes to the constant
361 offsets used in if_register_send to patch the NIT program! XXX */
363 void if_register_receive (info
)
364 struct interface_info
*info
;
366 #ifdef USE_DLPI_PFMOD
367 struct packetfilt pf
;
370 /* Open a DLPI device and hang it on this interface... */
371 info
-> rfdesc
= if_register_dlpi (info
);
373 #ifdef USE_DLPI_PFMOD
374 /* Set up the PFMOD filter program. */
375 /* XXX Unlike the BPF filter program, this one won't work if the
376 XXX IP packet is fragmented or if there are options on the IP
382 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ 6;
383 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
+ ENF_CAND
;
384 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (ETHERTYPE_IP
);
385 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
;
386 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (IPPROTO_UDP
);
387 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ 11;
388 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSH00FF
+ ENF_AND
;
389 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_CAND
;
390 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ 18;
391 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
+ ENF_CAND
;
392 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (local_port
);
395 * The packets that will be received on this file descriptor
396 * will be IP packets (due to the SAP that was specified in
397 * the dlbind call). There will be no ethernet header.
398 * Therefore, setup the packet filter to check the protocol
399 * field for UDP, and the destination port number equal
400 * to the local port. All offsets are relative to the start
403 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
;
404 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (IPPROTO_UDP
);
405 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ 4;
406 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSH00FF
+ ENF_AND
;
407 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_CAND
;
408 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ 11;
409 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
+ ENF_CAND
;
410 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (local_port
);
413 /* Install the filter... */
414 if (strioctl (info
-> rfdesc
, PFIOCSETF
, INFTIM
,
415 sizeof (pf
), (char *)&pf
) < 0) {
416 log_fatal ("Can't set PFMOD receive filter on %s: %m", info
-> name
);
420 if (!quiet_interface_discovery
)
421 log_info ("Listening on DLPI/%s/%s%s%s",
423 print_hw_addr (info
-> hw_address
.htype
,
424 info
-> hw_address
.hlen
,
425 info
-> hw_address
.haddr
),
426 (info
-> shared_network
? "/" : ""),
427 (info
-> shared_network
?
428 info
-> shared_network
-> name
: ""));
430 #ifdef DLPI_FIRST_SEND_WAIT
431 /* See the implementation notes at the beginning of this file */
432 # ifdef USE_DLPI_SEND
433 sleep (DLPI_FIRST_SEND_WAIT
/ 2);
435 sleep (DLPI_FIRST_SEND_WAIT
);
439 #endif /* USE_DLPI_RECEIVE */
442 ssize_t
send_packet (interface
, packet
, raw
, len
, from
, to
, hto
)
443 struct interface_info
*interface
;
444 struct packet
*packet
;
445 struct dhcp_packet
*raw
;
448 struct sockaddr_in
*to
;
449 struct hardware
*hto
;
452 unsigned char dbuf
[1536];
453 unsigned char sap
[2];
454 unsigned char dstaddr
[DLPI_MAXDLADDR
];
459 if (!strcmp (interface
-> name
, "fallback"))
460 return send_fallback (interface
, packet
, raw
,
465 /* Assemble the headers... */
467 assemble_hw_header (interface
, dbuf
, &dbuflen
, hto
);
469 assemble_udp_ip_header (interface
, dbuf
, &dbuflen
, from
.s_addr
,
470 to
-> sin_addr
.s_addr
, to
-> sin_port
,
471 (unsigned char *)raw
, len
);
473 /* Copy the data into the buffer (yuk). */
474 memcpy (dbuf
+ dbuflen
, raw
, len
);
478 result
= write (interface
-> wfdesc
, dbuf
, dbuflen
);
480 /* XXX: Assumes ethernet, with two byte SAP */
481 sap
[0] = 0x08; /* ETHERTYPE_IP, high byte */
482 sap
[1] = 0x0; /* ETHERTYPE_IP, low byte */
483 saplen
= -2; /* -2 indicates a two byte SAP at the end
486 /* Setup the destination address */
487 if (hto
&& hto
-> hlen
== interface
-> hw_address
.hlen
) {
488 dlpi_makeaddr (hto
-> haddr
, hto
-> hlen
, sap
, saplen
, dstaddr
);
490 /* XXX: Assumes broadcast addr is all ones */
491 /* Really should get the broadcast address as part of the
492 * dlpiinforeq, and store it somewhere in the interface structure.
494 unsigned char bcast_ether
[DLPI_MAXDLADDR
];
496 memset ((char *)bcast_ether
, 0xFF, interface
-> hw_address
.hlen
);
497 dlpi_makeaddr (bcast_ether
, interface
-> hw_address
.hlen
,
498 sap
, saplen
, dstaddr
);
500 addrlen
= interface
-> hw_address
.hlen
+ ABS (saplen
);
502 /* Send the packet down the wire... */
503 result
= dlpiunitdatareq (interface
-> wfdesc
, dstaddr
, addrlen
,
504 0, 0, dbuf
, dbuflen
);
507 warn ("send_packet: %m");
510 #endif /* USE_DLPI_SEND */
512 #ifdef USE_DLPI_RECEIVE
513 ssize_t
receive_packet (interface
, buf
, len
, from
, hfrom
)
514 struct interface_info
*interface
;
517 struct sockaddr_in
*from
;
518 struct hardware
*hfrom
;
520 unsigned char dbuf
[1536];
521 unsigned char sap
[2];
522 unsigned char srcaddr
[DLPI_MAXDLADDR
];
523 unsigned long srcaddrlen
;
532 length
= read (interface
-> rfdesc
, dbuf
, sizeof (dbuf
));
534 length
= dlpiunitdataind (interface
-> rfdesc
, (unsigned char *)NULL
,
535 (unsigned long *)NULL
, srcaddr
, &srcaddrlen
,
536 (unsigned long *)NULL
, dbuf
, sizeof (dbuf
));
544 /* Copy sender info */
545 /* XXX: Assumes ethernet, where SAP comes at end of haddr */
547 if (hfrom
&& srcaddrlen
== ABS(saplen
) + interface
-> hw_address
.hlen
) {
548 hfrom
-> htype
= interface
-> hw_address
.htype
;
549 hfrom
-> hlen
= interface
-> hw_address
.hlen
;
550 dlpi_parseaddr (srcaddr
, hfrom
-> haddr
,
551 interface
-> hw_address
.hlen
, sap
, saplen
);
553 memset ((char *)hfrom
, '\0', sizeof (*hfrom
));
557 /* Decode the IP and UDP headers... */
560 /* Decode the physical header... */
561 offset
= decode_hw_header (interface
, dbuf
, bufix
, hfrom
);
563 /* If a physical layer checksum failed (dunno of any
564 physical layer that supports this, but WTH), skip this
572 offset
= decode_udp_ip_header (interface
, dbuf
, bufix
,
573 from
, (unsigned char *)0, length
);
575 /* If the IP or UDP checksum was bad, skip the packet... */
583 /* Copy out the data in the packet... */
584 memcpy (buf
, &dbuf
[bufix
], length
);
589 /* Common DLPI routines ...
591 * Written by Eric James Negaard, <lmdejn@lmd.ericsson.se>
593 * Based largely in part to the example code contained in the document
594 * "How to Use the STREAMS Data Link Provider Interface (DLPI)", written
595 * by Neal Nuckolls of SunSoft Internet Engineering.
597 * This code has been developed and tested on sparc-based machines running
598 * SunOS 5.5.1, with le and hme network interfaces. It should be pretty
601 * The usual disclaimers apply. This code works for me. Don't blame me
602 * if it makes your machine or network go down in flames. That taken
603 * into consideration, use this code as you wish. If you make usefull
604 * modifications I'd appreciate hearing about it.
607 #define DLPI_MAXWAIT 15 /* Max timeout */
609 static void dlpi_makeaddr (physaddr
, physaddrlen
, sap
, saplen
, buf
)
610 unsigned char *physaddr
;
617 * If the saplen is negative, the SAP goes at the end of the address,
618 * otherwise it goes at the beginning.
621 memcpy ((char *)buf
, (char *)sap
, saplen
);
622 memcpy ((char *)&buf
[saplen
], (char *)physaddr
, physaddrlen
);
624 memcpy ((char *)buf
, (char *)physaddr
, physaddrlen
);
625 memcpy ((char *)&buf
[physaddrlen
], (char *)sap
, 0 - saplen
);
629 static void dlpi_parseaddr (buf
, physaddr
, physaddrlen
, sap
, saplen
)
631 unsigned char *physaddr
;
637 * If the saplen is negative, the SAP is at the end of the address,
638 * otherwise it is at the beginning.
641 memcpy ((char *)sap
, (char *)buf
, saplen
);
642 memcpy ((char *)physaddr
, (char *)&buf
[saplen
], physaddrlen
);
644 memcpy ((char *)physaddr
, (char *)buf
, physaddrlen
);
645 memcpy ((char *)sap
, (char *)&buf
[physaddrlen
], 0 - saplen
);
650 * Parse an interface name and extract the unit number
653 static int dlpiunit (ifname
)
664 /* Advance to the end of the name */
667 /* Back up to the start of the first digit */
668 while ((*(cp
-1) >= '0' && *(cp
-1) <= '9') || *(cp
- 1) == ':') cp
--;
670 /* Convert the unit number */
672 while (*cp
>= '0' && *cp
<= '9') {
674 unit
+= (*cp
++ - '0');
681 * dlpiopen - open the DLPI device for a given interface name
683 static int dlpiopen (ifname
)
693 /* Open a DLPI device */
694 if (*ifname
== '/') {
697 /* Prepend the device directory */
698 memcpy (devname
, DLPI_DEVDIR
, strlen (DLPI_DEVDIR
));
699 dp
= &devname
[strlen (DLPI_DEVDIR
)];
702 /* Find the end of the interface name */
706 /* And back up to the first digit (unit number) */
707 while ((*(ep
- 1) >= '0' && *(ep
- 1) <= '9') || *(ep
- 1) == ':')
710 /* Copy everything up to the unit number */
716 return open (devname
, O_RDWR
, 0);
720 * dlpiinforeq - request information about the data link provider.
723 static int dlpiinforeq (fd
)
726 dl_info_req_t info_req
;
730 info_req
.dl_primitive
= DL_INFO_REQ
;
733 ctl
.len
= sizeof (info_req
);
734 ctl
.buf
= (char *)&info_req
;
738 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
742 * dlpiphysaddrreq - request the current physical address.
744 static int dlpiphysaddrreq (fd
, addrtype
)
746 unsigned long addrtype
;
748 dl_phys_addr_req_t physaddr_req
;
752 physaddr_req
.dl_primitive
= DL_PHYS_ADDR_REQ
;
753 physaddr_req
.dl_addr_type
= addrtype
;
756 ctl
.len
= sizeof (physaddr_req
);
757 ctl
.buf
= (char *)&physaddr_req
;
761 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
765 * dlpiattachreq - send a request to attach to a specific unit.
767 static int dlpiattachreq (fd
, ppa
)
771 dl_attach_req_t attach_req
;
775 attach_req
.dl_primitive
= DL_ATTACH_REQ
;
776 attach_req
.dl_ppa
= ppa
;
779 ctl
.len
= sizeof (attach_req
);
780 ctl
.buf
= (char *)&attach_req
;
784 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
788 * dlpibindreq - send a request to bind to a specific SAP address.
790 static int dlpibindreq (fd
, sap
, max_conind
, service_mode
, conn_mgmt
, xidtest
)
792 unsigned long max_conind
;
793 unsigned long service_mode
;
794 unsigned long conn_mgmt
;
795 unsigned long xidtest
;
798 dl_bind_req_t bind_req
;
802 bind_req
.dl_primitive
= DL_BIND_REQ
;
803 bind_req
.dl_sap
= sap
;
804 bind_req
.dl_max_conind
= max_conind
;
805 bind_req
.dl_service_mode
= service_mode
;
806 bind_req
.dl_conn_mgmt
= conn_mgmt
;
807 bind_req
.dl_xidtest_flg
= xidtest
;
810 ctl
.len
= sizeof (bind_req
);
811 ctl
.buf
= (char *)&bind_req
;
815 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
819 * dlpiunbindreq - send a request to unbind.
821 static int dlpiunbindreq (fd
)
824 dl_unbind_req_t unbind_req
;
828 unbind_req
.dl_primitive
= DL_UNBIND_REQ
;
831 ctl
.len
= sizeof (unbind_req
);
832 ctl
.buf
= (char *)&unbind_req
;
836 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
841 * dlpidetachreq - send a request to detach.
843 static int dlpidetachreq (fd
)
846 dl_detach_req_t detach_req
;
850 detach_req
.dl_primitive
= DL_DETACH_REQ
;
853 ctl
.len
= sizeof (detach_req
);
854 ctl
.buf
= (char *)&detach_req
;
858 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
863 * dlpibindack - receive an ack to a dlbindreq.
865 static int dlpibindack (fd
, bufp
)
869 union DL_primitives
*dlp
;
873 ctl
.maxlen
= DLPI_MAXDLBUF
;
877 if (strgetmsg (fd
, &ctl
,
878 (struct strbuf
*)NULL
, &flags
, "dlpibindack") < 0) {
882 dlp
= (union DL_primitives
*)ctl
.buf
;
884 if (!expected (DL_BIND_ACK
, dlp
, flags
) < 0) {
888 if (ctl
.len
< sizeof (dl_bind_ack_t
)) {
889 /* Returned structure is too short */
897 * dlpiokack - general acknowledgement reception.
899 static int dlpiokack (fd
, bufp
)
903 union DL_primitives
*dlp
;
907 ctl
.maxlen
= DLPI_MAXDLBUF
;
911 if (strgetmsg (fd
, &ctl
,
912 (struct strbuf
*)NULL
, &flags
, "dlpiokack") < 0) {
916 dlp
= (union DL_primitives
*)ctl
.buf
;
918 if (!expected (DL_OK_ACK
, dlp
, flags
) < 0) {
922 if (ctl
.len
< sizeof (dl_ok_ack_t
)) {
923 /* Returned structure is too short */
931 * dlpiinfoack - receive an ack to a dlinforeq.
933 static int dlpiinfoack (fd
, bufp
)
937 union DL_primitives
*dlp
;
941 ctl
.maxlen
= DLPI_MAXDLBUF
;
945 if (strgetmsg (fd
, &ctl
, (struct strbuf
*)NULL
, &flags
,
946 "dlpiinfoack") < 0) {
950 dlp
= (union DL_primitives
*) ctl
.buf
;
952 if (!expected (DL_INFO_ACK
, dlp
, flags
) < 0) {
956 if (ctl
.len
< sizeof (dl_info_ack_t
)) {
957 /* Returned structure is too short */
965 * dlpiphysaddrack - receive an ack to a dlpiphysaddrreq.
967 int dlpiphysaddrack (fd
, bufp
)
971 union DL_primitives
*dlp
;
975 ctl
.maxlen
= DLPI_MAXDLBUF
;
979 if (strgetmsg (fd
, &ctl
, (struct strbuf
*)NULL
, &flags
,
980 "dlpiphysaddrack") < 0) {
984 dlp
= (union DL_primitives
*)ctl
.buf
;
986 if (!expected (DL_PHYS_ADDR_ACK
, dlp
, flags
) < 0) {
990 if (ctl
.len
< sizeof (dl_phys_addr_ack_t
)) {
991 /* Returned structure is too short */
998 int dlpiunitdatareq (fd
, addr
, addrlen
, minpri
, maxpri
, dbuf
, dbuflen
)
1000 unsigned char *addr
;
1002 unsigned long minpri
;
1003 unsigned long maxpri
;
1004 unsigned char *dbuf
;
1007 long buf
[DLPI_MAXDLBUF
];
1008 union DL_primitives
*dlp
;
1009 struct strbuf ctl
, data
;
1011 /* Set up the control information... */
1012 dlp
= (union DL_primitives
*)buf
;
1013 dlp
-> unitdata_req
.dl_primitive
= DL_UNITDATA_REQ
;
1014 dlp
-> unitdata_req
.dl_dest_addr_length
= addrlen
;
1015 dlp
-> unitdata_req
.dl_dest_addr_offset
= sizeof (dl_unitdata_req_t
);
1016 dlp
-> unitdata_req
.dl_priority
.dl_min
= minpri
;
1017 dlp
-> unitdata_req
.dl_priority
.dl_max
= maxpri
;
1019 /* Append the destination address */
1020 memcpy ((char *)buf
+ dlp
-> unitdata_req
.dl_dest_addr_offset
,
1024 ctl
.len
= dlp
-> unitdata_req
.dl_dest_addr_offset
+ addrlen
;
1025 ctl
.buf
= (char *)buf
;
1028 data
.buf
= (char *)dbuf
;
1031 /* Send the packet down the wire... */
1032 return putmsg (fd
, &ctl
, &data
, 0);
1035 static int dlpiunitdataind (fd
, daddr
, daddrlen
,
1036 saddr
, saddrlen
, grpaddr
, dbuf
, dlen
)
1038 unsigned char *daddr
;
1039 unsigned long *daddrlen
;
1040 unsigned char *saddr
;
1041 unsigned long *saddrlen
;
1042 unsigned long *grpaddr
;
1043 unsigned char *dbuf
;
1046 long buf
[DLPI_MAXDLBUF
];
1047 union DL_primitives
*dlp
;
1048 struct strbuf ctl
, data
;
1052 /* Set up the msg_buf structure... */
1053 dlp
= (union DL_primitives
*)buf
;
1054 dlp
-> unitdata_ind
.dl_primitive
= DL_UNITDATA_IND
;
1056 ctl
.maxlen
= DLPI_MAXDLBUF
;
1058 ctl
.buf
= (char *)buf
;
1062 data
.buf
= (char *)dbuf
;
1064 result
= getmsg (fd
, &ctl
, &data
, &flags
);
1070 if (ctl
.len
< sizeof (dl_unitdata_ind_t
) ||
1071 dlp
-> unitdata_ind
.dl_primitive
!= DL_UNITDATA_IND
) {
1075 if (data
.len
<= 0) {
1079 /* Copy sender info */
1081 memcpy (saddr
, &buf
[dlp
-> unitdata_ind
.dl_src_addr_offset
],
1082 dlp
-> unitdata_ind
.dl_src_addr_length
);
1085 *saddrlen
= dlp
-> unitdata_ind
.dl_src_addr_length
;
1088 /* Copy destination info */
1090 memcpy (daddr
, &buf
[dlp
-> unitdata_ind
.dl_dest_addr_offset
],
1091 dlp
-> unitdata_ind
.dl_dest_addr_length
);
1094 *daddrlen
= dlp
-> unitdata_ind
.dl_dest_addr_length
;
1098 *grpaddr
= dlp
-> unitdata_ind
.dl_group_address
;
1105 * expected - see if we got what we wanted.
1107 static int expected (prim
, dlp
, msgflags
)
1109 union DL_primitives
*dlp
;
1112 if (msgflags
!= RS_HIPRI
) {
1113 /* Message was not M_PCPROTO */
1117 if (dlp
-> dl_primitive
!= prim
) {
1118 /* Incorrect/unexpected return message */
1126 * strgetmsg - get a message from a stream, with timeout.
1128 static int strgetmsg (fd
, ctlp
, datap
, flagsp
, caller
)
1129 struct strbuf
*ctlp
, *datap
;
1145 pfd
.events
= POLLPRI
; /* We're only interested in knowing
1146 * when we can receive the next high
1151 now
= time (&starttime
);
1152 while (now
<= starttime
+ DLPI_MAXWAIT
) {
1153 to_msec
= ((starttime
+ DLPI_MAXWAIT
) - now
) * 1000;
1154 count
= poll (&pfd
, 1, to_msec
);
1157 /* log_fatal ("strgetmsg: timeout"); */
1159 } else if (count
< 0) {
1160 if (errno
== EAGAIN
|| errno
== EINTR
) {
1164 /* log_fatal ("poll: %m"); */
1171 #else /* defined (USE_POLL) */
1173 * Start timer. Can't use select, since it might return true if there
1174 * were non High-Priority data available on the stream.
1176 (void) sigset (SIGALRM
, sigalrm
);
1178 if (alarm (DLPI_MAXWAIT
) < 0) {
1179 /* log_fatal ("alarm: %m"); */
1182 #endif /* !defined (USE_POLL) */
1185 * Set flags argument and issue getmsg ().
1188 if ((result
= getmsg (fd
, ctlp
, datap
, flagsp
)) < 0) {
1196 if (alarm (0) < 0) {
1197 /* log_fatal ("alarm: %m"); */
1203 * Check for MOREDATA and/or MORECTL.
1205 if (result
& (MORECTL
|MOREDATA
)) {
1210 * Check for at least sizeof (long) control data portion.
1212 if (ctlp
-> len
< sizeof (long)) {
1221 * sigalrm - handle alarms.
1223 static void sigalrm (sig
)
1226 fprintf (stderr
, "strgetmsg: timeout");
1229 #endif /* !defined (USE_POLL) */
1231 int can_unicast_without_arp (ip
)
1232 struct interface_info
*ip
;
1237 int can_receive_unicast_unconfigured (ip
)
1238 struct interface_info
*ip
;
1243 void maybe_setup_fallback ()
1245 struct interface_info
*fbi
;
1246 fbi
= setup_fallback ();
1248 if_register_fallback (fbi
);
1249 add_protocol ("fallback", fallback_interface
-> wfdesc
,
1250 fallback_discard
, fallback_interface
);
1253 #endif /* USE_DLPI */