3 Data Link Provider Interface (DLPI) network interface code. */
6 * Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1996-2003 by Internet Software Consortium
9 * This Source Code Form is subject to the terms of the Mozilla Public
10 * License, v. 2.0. If a copy of the MPL was not distributed with this
11 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Internet Systems Consortium, Inc.
23 * Redwood City, CA 94063
25 * https://www.isc.org/
27 * This software was written for Internet Systems Consortium
28 * by Eric James Negaard, <lmdejn@lmd.ericsson.se>. To learn more about
29 * Internet Systems Consortium, see ``https://www.isc.org''.
31 * Joost Mulders has also done considerable work in debugging the DLPI API
32 * support on Solaris and getting this code to work properly on a variety
33 * of different Solaris platforms.
37 * Based largely in part to the existing NIT code in nit.c.
39 * This code has been developed and tested on sparc-based machines running
40 * SunOS 5.5.1, with le and hme network interfaces. It should be pretty
45 * Implementation notes:
47 * I first tried to write this code to the "vanilla" DLPI 2.0 API.
48 * It worked on a Sun Ultra-1 with a hme interface, but didn't work
49 * on Sun SparcStation 5's with "le" interfaces (the packets sent out
50 * via dlpiunitdatareq contained an Ethernet type of 0x0000 instead
51 * of the expected 0x0800).
53 * Therefore I added the "DLPI_RAW" code which is a Sun extension to
54 * the DLPI standard. This code works on both of the above machines.
55 * This is configurable in the OS-dependent include file by defining
58 * It quickly became apparant that I should also use the "pfmod"
59 * STREAMS module to cut down on the amount of user level packet
60 * processing. I don't know how widely available "pfmod" is, so it's
61 * use is conditionally included. This is configurable in the
62 * OS-dependent include file by defining USE_DLPI_PFMOD.
64 * A major quirk on the Sun's at least, is that no packets seem to get
65 * sent out the interface until six seconds after the interface is
66 * first "attached" to [per system reboot] (it's actually from when
67 * the interface is attached, not when it is plumbed, so putting a
68 * sleep into the dhclient-script at PREINIT time doesn't help). I
69 * HAVE tried, without success to poll the fd to see when it is ready
70 * for writing. This doesn't help at all. If the sleeps are not done,
71 * the initial DHCPREQUEST or DHCPDISCOVER never gets sent out, so
72 * I've put them here, when register_send and register_receive are
73 * called (split up into two three-second sleeps between the notices,
74 * so that it doesn't seem like so long when you're watching :-). The
75 * amount of time to sleep is configurable in the OS-dependent include
76 * file by defining DLPI_FIRST_SEND_WAIT to be the number of seconds
81 * The Open Group Technical Standard can be found here:
82 * http://www.opengroup.org/onlinepubs/009618899/index.htm
84 * The HP DLPI Programmer's Guide can be found here:
85 * http://docs.hp.com/en/B2355-90139/index.html
90 #if defined (USE_DLPI_SEND) || defined (USE_DLPI_RECEIVE) || \
91 defined(USE_DLPI_HWADDR)
93 # include <sys/ioctl.h>
94 # include <sys/time.h>
95 # include <sys/dlpi.h>
97 # ifdef USE_DLPI_PFMOD
98 # include <sys/pfmod.h>
103 # include <netinet/in_systm.h>
104 # include "includes/netinet/ip.h"
105 # include "includes/netinet/udp.h"
106 # include "includes/netinet/if_ether.h"
108 # ifdef USE_DLPI_PFMOD
110 # define DLPI_MODNAME "DLPI+RAW+PFMOD"
112 # define DLPI_MODNAME "DLPI+PFMOD"
116 # define DLPI_MODNAME "DLPI+RAW"
118 # define DLPI_MODNAME "DLPI"
123 # define ABS(x) ((x) >= 0 ? (x) : 0-(x))
126 #if defined(USE_DLPI_PFMOD) || defined(USE_DLPI_RAW)
127 static int strioctl (int fd
, int cmd
, int timeout
, int len
, char *dp
);
130 #define DLPI_MAXDLBUF 8192 /* Buffer size */
131 #define DLPI_MAXDLADDR 1024 /* Max address size */
133 /* Device directory */
134 #if defined(USE_DEV_NET)
135 #define DLPI_DEVDIR "/dev/net/" /* Solaris 11 + */
137 #define DLPI_DEVDIR "/dev/" /* Pre Solaris 11 */
140 static int dlpiopen(const char *ifname
);
141 static int dlpiunit (char *ifname
);
142 static int dlpiinforeq (int fd
);
143 static int dlpiphysaddrreq (int fd
, unsigned long addrtype
);
144 static int dlpiattachreq (int fd
, unsigned long ppa
);
145 static int dlpibindreq (int fd
, unsigned long sap
, unsigned long max_conind
,
146 unsigned long service_mode
, unsigned long conn_mgmt
,
147 unsigned long xidtest
);
148 #if defined(UNUSED_DLPI_INTERFACE)
149 /* These functions are unused at present, but may be used at a later date.
150 * defined out to avoid compiler warnings about unused static functions.
152 static int dlpidetachreq (int fd
);
153 static int dlpiunbindreq (int fd
);
155 static int dlpiokack (int fd
, char *bufp
);
156 static int dlpiinfoack (int fd
, char *bufp
);
157 static int dlpiphysaddrack (int fd
, char *bufp
);
158 static int dlpibindack (int fd
, char *bufp
);
159 #if defined(USE_DLPI_SEND) || defined(USE_DLPI_RECEIVE)
160 /* These functions are not used if we're only sourcing the get_hw_addr()
161 * function (for USE_SOCKETS).
163 static int dlpiunitdatareq (int fd
, unsigned char *addr
, int addrlen
,
164 unsigned long minpri
, unsigned long maxpri
,
165 unsigned char *data
, int datalen
);
166 static int dlpiunitdataind (int fd
,
167 unsigned char *dstaddr
,
168 unsigned long *dstaddrlen
,
169 unsigned char *srcaddr
,
170 unsigned long *srcaddrlen
,
171 unsigned long *grpaddr
,
174 #endif /* !USE_DLPI_HWADDR: USE_DLPI_SEND || USE_DLPI_RECEIVE */
175 static int expected (unsigned long prim
, union DL_primitives
*dlp
,
177 static int strgetmsg (int fd
, struct strbuf
*ctlp
, struct strbuf
*datap
,
178 int *flagsp
, char *caller
);
180 /* Reinitializes the specified interface after an address change. This
181 is not required for packet-filter APIs. */
184 void if_reinitialize_send (info
)
185 struct interface_info
*info
;
190 #ifdef USE_DLPI_RECEIVE
191 void if_reinitialize_receive (info
)
192 struct interface_info
*info
;
197 /* Called by get_interface_list for each interface that's discovered.
198 Opens a packet filter for each interface and adds it to the select
201 int if_register_dlpi (info
)
202 struct interface_info
*info
;
206 long buf
[DLPI_MAXDLBUF
];
207 union DL_primitives
*dlp
;
209 dlp
= (union DL_primitives
*)buf
;
211 /* Open a DLPI device */
212 if ((sock
= dlpiopen (info
-> name
)) < 0) {
213 log_fatal ("Can't open DLPI device for %s: %m", info
-> name
);
217 * Submit a DL_INFO_REQ request, to find the dl_mac_type and
220 if (dlpiinforeq(sock
) < 0 || dlpiinfoack(sock
, (char *)buf
) < 0) {
221 log_fatal ("Can't get DLPI MAC type for %s: %m", info
-> name
);
223 switch (dlp
-> info_ack
.dl_mac_type
) {
224 case DL_CSMACD
: /* IEEE 802.3 */
226 info
-> hw_address
.hbuf
[0] = HTYPE_ETHER
;
228 /* adding token ring 5/1999 - mayer@ping.at */
230 info
-> hw_address
.hbuf
[0] = HTYPE_IEEE802
;
233 info
-> hw_address
.hbuf
[0] = HTYPE_FDDI
;
236 log_fatal("%s: unsupported DLPI MAC type %lu", info
->name
,
237 (unsigned long)dlp
->info_ack
.dl_mac_type
);
241 * copy the sap length and broadcast address of this interface
242 * to interface_info. This fixes nothing but seemed nicer than to
243 * assume -2 and ffffff.
245 info
-> dlpi_sap_length
= dlp
-> info_ack
.dl_sap_length
;
246 info
-> dlpi_broadcast_addr
.hlen
=
247 dlp
-> info_ack
.dl_brdcst_addr_length
;
248 memcpy (info
-> dlpi_broadcast_addr
.hbuf
,
249 (char *)dlp
+ dlp
-> info_ack
.dl_brdcst_addr_offset
,
250 dlp
-> info_ack
.dl_brdcst_addr_length
);
253 if (dlp
-> info_ack
.dl_provider_style
== DL_STYLE2
) {
255 * Attach to the device. If this fails, the device
258 unit
= dlpiunit (info
-> name
);
260 if (dlpiattachreq (sock
, unit
) < 0
261 || dlpiokack (sock
, (char *)buf
) < 0) {
262 log_fatal ("Can't attach DLPI device for %s: %m", info
-> name
);
267 * Bind to the IP service access point (SAP), connectionless (CLDLS).
269 if (dlpibindreq (sock
, ETHERTYPE_IP
, 0, DL_CLDLS
, 0, 0) < 0
270 || dlpibindack (sock
, (char *)buf
) < 0) {
271 log_fatal ("Can't bind DLPI device for %s: %m", info
-> name
);
275 * Submit a DL_PHYS_ADDR_REQ request, to find
276 * the hardware address
278 if (dlpiphysaddrreq (sock
, DL_CURR_PHYS_ADDR
) < 0
279 || dlpiphysaddrack (sock
, (char *)buf
) < 0) {
280 log_fatal ("Can't get DLPI hardware address for %s: %m",
284 info
-> hw_address
.hlen
= dlp
-> physaddr_ack
.dl_addr_length
+ 1;
285 memcpy (&info
-> hw_address
.hbuf
[1],
286 (char *)buf
+ dlp
-> physaddr_ack
.dl_addr_offset
,
287 dlp
-> physaddr_ack
.dl_addr_length
);
290 if (strioctl (sock
, DLIOCRAW
, INFTIM
, 0, 0) < 0) {
291 log_fatal ("Can't set DLPI RAW mode for %s: %m",
296 #ifdef USE_DLPI_PFMOD
297 if (ioctl (sock
, I_PUSH
, "pfmod") < 0) {
298 log_fatal ("Can't push packet filter onto DLPI for %s: %m",
306 #if defined(USE_DLPI_PFMOD) || defined(USE_DLPI_RAW)
308 strioctl (fd
, cmd
, timeout
, len
, dp
)
319 sio
.ic_timout
= timeout
;
323 if ((rslt
= ioctl (fd
, I_STR
, &sio
)) < 0) {
329 #endif /* USE_DPI_PFMOD || USE_DLPI_RAW */
332 void if_register_send (info
)
333 struct interface_info
*info
;
335 /* If we're using the DLPI API for sending and receiving,
336 we don't need to register this interface twice. */
337 #ifndef USE_DLPI_RECEIVE
338 # ifdef USE_DLPI_PFMOD
339 struct packetfilt pf
;
342 info
-> wfdesc
= if_register_dlpi (info
);
344 # ifdef USE_DLPI_PFMOD
345 /* Set up an PFMOD filter that rejects everything... */
348 pf
.Pf_Filter
[0] = ENF_PUSHZERO
;
350 /* Install the filter */
351 if (strioctl (info
-> wfdesc
, PFIOCSETF
, INFTIM
,
352 sizeof (pf
), (char *)&pf
) < 0) {
353 log_fatal ("Can't set PFMOD send filter on %s: %m", info
-> name
);
356 # endif /* USE_DLPI_PFMOD */
357 #else /* !defined (USE_DLPI_RECEIVE) */
359 * If using DLPI for both send and receive, simply re-use
360 * the read file descriptor that was set up earlier.
362 info
-> wfdesc
= info
-> rfdesc
;
365 if (!quiet_interface_discovery
)
366 log_info ("Sending on DLPI/%s/%s%s%s",
368 print_hw_addr (info
-> hw_address
.hbuf
[0],
369 info
-> hw_address
.hlen
- 1,
370 &info
-> hw_address
.hbuf
[1]),
371 (info
-> shared_network
? "/" : ""),
372 (info
-> shared_network
?
373 info
-> shared_network
-> name
: ""));
375 #ifdef DLPI_FIRST_SEND_WAIT
376 /* See the implementation notes at the beginning of this file */
377 # ifdef USE_DLPI_RECEIVE
378 sleep (DLPI_FIRST_SEND_WAIT
- (DLPI_FIRST_SEND_WAIT
/ 2));
380 sleep (DLPI_FIRST_SEND_WAIT
);
385 void if_deregister_send (info
)
386 struct interface_info
*info
;
388 /* If we're using the DLPI API for sending and receiving,
389 we don't need to register this interface twice. */
390 #ifndef USE_DLPI_RECEIVE
391 close (info
-> wfdesc
);
395 if (!quiet_interface_discovery
)
396 log_info ("Disabling output on DLPI/%s/%s%s%s",
398 print_hw_addr (info
-> hw_address
.hbuf
[0],
399 info
-> hw_address
.hlen
- 1,
400 &info
-> hw_address
.hbuf
[1]),
401 (info
-> shared_network
? "/" : ""),
402 (info
-> shared_network
?
403 info
-> shared_network
-> name
: ""));
405 #endif /* USE_DLPI_SEND */
407 #ifdef USE_DLPI_RECEIVE
408 /* Packet filter program...
409 XXX Changes to the filter program may require changes to the constant
410 offsets used in if_register_send to patch the NIT program! XXX */
412 #if defined(RELAY_PORT)
413 #error "Relay port is not yet supported for DLPI"
416 void if_register_receive (info
)
417 struct interface_info
*info
;
419 #ifdef USE_DLPI_PFMOD
420 struct packetfilt pf
;
425 /* Open a DLPI device and hang it on this interface... */
426 info
-> rfdesc
= if_register_dlpi (info
);
428 #ifdef USE_DLPI_PFMOD
429 /* Set up the PFMOD filter program. */
430 /* XXX Unlike the BPF filter program, this one won't work if the
431 XXX IP packet is fragmented or if there are options on the IP
436 #if defined (USE_DLPI_RAW)
437 # define ETHER_H_PREFIX (14) /* sizeof (ethernet_header) */
439 * ethertype == ETHERTYPE_IP
442 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ (offset
/ 2);
443 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
| ENF_CAND
;
444 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (ETHERTYPE_IP
);
446 # define ETHER_H_PREFIX (0)
447 # endif /* USE_DLPI_RAW */
449 * The packets that will be received on this file descriptor
450 * will be IP packets (due to the SAP that was specified in
451 * the dlbind call). There will be no ethernet header.
452 * Therefore, setup the packet filter to check the protocol
453 * field for UDP, and the destination port number equal
454 * to the local port. All offsets are relative to the start
459 * BOOTPS destination port
461 offset
= ETHER_H_PREFIX
+ sizeof (iphdr
) + sizeof (u_int16_t
);
462 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ (offset
/ 2);
463 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
| ENF_CAND
;
464 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = local_port
;
467 * protocol should be udp. this is a byte compare, test for
470 offset
= ETHER_H_PREFIX
+ ((u_int8_t
*)&(iphdr
.ip_p
) - (u_int8_t
*)&iphdr
);
471 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHWORD
+ (offset
/ 2);
472 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
| ENF_AND
;
473 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (0x00FF);
474 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = ENF_PUSHLIT
| ENF_CAND
;
475 pf
.Pf_Filter
[pf
.Pf_FilterLen
++] = htons (IPPROTO_UDP
);
477 /* Install the filter... */
478 if (strioctl (info
-> rfdesc
, PFIOCSETF
, INFTIM
,
479 sizeof (pf
), (char *)&pf
) < 0) {
480 log_fatal ("Can't set PFMOD receive filter on %s: %m", info
-> name
);
482 #endif /* USE_DLPI_PFMOD */
484 if (!quiet_interface_discovery
)
485 log_info ("Listening on DLPI/%s/%s%s%s",
487 print_hw_addr (info
-> hw_address
.hbuf
[0],
488 info
-> hw_address
.hlen
- 1,
489 &info
-> hw_address
.hbuf
[1]),
490 (info
-> shared_network
? "/" : ""),
491 (info
-> shared_network
?
492 info
-> shared_network
-> name
: ""));
494 #ifdef DLPI_FIRST_SEND_WAIT
495 /* See the implementation notes at the beginning of this file */
496 # ifdef USE_DLPI_SEND
497 sleep (DLPI_FIRST_SEND_WAIT
/ 2);
499 sleep (DLPI_FIRST_SEND_WAIT
);
504 void if_deregister_receive (info
)
505 struct interface_info
*info
;
507 /* If we're using the DLPI API for sending and receiving,
508 we don't need to register this interface twice. */
509 #ifndef USE_DLPI_SEND
510 close (info
-> rfdesc
);
514 if (!quiet_interface_discovery
)
515 log_info ("Disabling input on DLPI/%s/%s%s%s",
517 print_hw_addr (info
-> hw_address
.hbuf
[0],
518 info
-> hw_address
.hlen
- 1,
519 &info
-> hw_address
.hbuf
[1]),
520 (info
-> shared_network
? "/" : ""),
521 (info
-> shared_network
?
522 info
-> shared_network
-> name
: ""));
524 #endif /* USE_DLPI_RECEIVE */
527 ssize_t
send_packet (interface
, packet
, raw
, len
, from
, to
, hto
)
528 struct interface_info
*interface
;
529 struct packet
*packet
;
530 struct dhcp_packet
*raw
;
533 struct sockaddr_in
*to
;
534 struct hardware
*hto
;
540 double ih
[1536 / sizeof (double)];
541 unsigned char *dbuf
= (unsigned char *)ih
;
543 unsigned char dstaddr
[DLPI_MAXDLADDR
];
547 if (!strcmp (interface
-> name
, "fallback"))
548 return send_fallback (interface
, packet
, raw
,
551 if (hto
== NULL
&& interface
->anycast_mac_addr
.hlen
)
552 hto
= &interface
->anycast_mac_addr
;
556 /* Assemble the headers... */
558 assemble_hw_header (interface
, (unsigned char *)hh
, &dbuflen
, hto
);
559 if (dbuflen
> sizeof hh
)
560 log_fatal ("send_packet: hh buffer too small.\n");
561 fudge
= dbuflen
% 4; /* IP header must be word-aligned. */
562 memcpy (dbuf
+ fudge
, (unsigned char *)hh
, dbuflen
);
565 assemble_udp_ip_header (interface
, dbuf
, &dbuflen
, from
.s_addr
,
566 to
-> sin_addr
.s_addr
, to
-> sin_port
,
567 (unsigned char *)raw
, len
);
569 /* Copy the data into the buffer (yuk). */
570 memcpy (dbuf
+ dbuflen
, raw
, len
);
574 result
= write (interface
-> wfdesc
, dbuf
+ fudge
, dbuflen
- fudge
);
578 * Setup the destination address (DLSAP) in dstaddr
580 * If sap_length < 0 we must deliver the DLSAP as phys+sap.
581 * If sap_length > 0 we must deliver the DLSAP as sap+phys.
583 * sap = Service Access Point == ETHERTYPE_IP
584 * sap + datalink address is called DLSAP in dlpi speak.
587 unsigned char phys
[DLPI_MAXDLADDR
];
588 unsigned char sap
[4];
589 int sap_len
= interface
-> dlpi_sap_length
;
590 int phys_len
= interface
-> hw_address
.hlen
- 1;
592 /* sap = htons (ETHERTYPE_IP) kludge */
593 memset (sap
, 0, sizeof (sap
));
594 # if (BYTE_ORDER == LITTLE_ENDIAN)
602 if (hto
&& hto
-> hlen
== interface
-> hw_address
.hlen
)
603 memcpy ( phys
, (char *) &hto
-> hbuf
[1], phys_len
);
605 memcpy ( phys
, interface
-> dlpi_broadcast_addr
.hbuf
,
606 interface
-> dlpi_broadcast_addr
.hlen
);
609 memcpy ( dstaddr
, phys
, phys_len
);
610 memcpy ( (char *) &dstaddr
[phys_len
], sap
, ABS (sap_len
));
613 memcpy ( dstaddr
, (void *) sap
, sap_len
);
614 memcpy ( (char *) &dstaddr
[sap_len
], phys
, phys_len
);
616 addrlen
= phys_len
+ ABS (sap_len
);
619 result
= dlpiunitdatareq (interface
-> wfdesc
, dstaddr
, addrlen
,
620 0, 0, dbuf
, dbuflen
);
621 #endif /* USE_DLPI_RAW */
623 log_error ("send_packet: %m");
626 #endif /* USE_DLPI_SEND */
628 #ifdef USE_DLPI_RECEIVE
629 ssize_t
receive_packet (interface
, buf
, len
, from
, hfrom
)
630 struct interface_info
*interface
;
633 struct sockaddr_in
*from
;
634 struct hardware
*hfrom
;
636 unsigned char dbuf
[1536];
637 unsigned char srcaddr
[DLPI_MAXDLADDR
];
638 unsigned long srcaddrlen
;
645 length
= read (interface
-> rfdesc
, dbuf
, sizeof (dbuf
));
647 length
= dlpiunitdataind (interface
-> rfdesc
, (unsigned char *)NULL
,
648 (unsigned long *)NULL
, srcaddr
, &srcaddrlen
,
649 (unsigned long *)NULL
, dbuf
, sizeof (dbuf
));
653 log_error("receive_packet: %m");
657 # if !defined (USE_DLPI_RAW)
659 * Copy the sender's hw address into hfrom
660 * If sap_len < 0 the DLSAP is as phys+sap.
661 * If sap_len > 0 the DLSAP is as sap+phys.
663 * sap is discarded here.
666 int sap_len
= interface
-> dlpi_sap_length
;
667 int phys_len
= interface
-> hw_address
.hlen
- 1;
669 if (hfrom
&& (srcaddrlen
== ABS (sap_len
) + phys_len
)) {
670 hfrom
-> hbuf
[0] = interface
-> hw_address
.hbuf
[0];
671 hfrom
-> hlen
= interface
-> hw_address
.hlen
;
674 memcpy ((char *) &hfrom
-> hbuf
[1], srcaddr
, phys_len
);
677 memcpy((char *)&hfrom
->hbuf
[1], srcaddr
+ sap_len
, phys_len
);
681 memset (hfrom
, '\0', sizeof *hfrom
);
685 # endif /* !defined (USE_DLPI_RAW) */
687 /* Decode the IP and UDP headers... */
690 /* Decode the physical header... */
691 offset
= decode_hw_header (interface
, dbuf
, bufix
, hfrom
);
693 /* If a physical layer checksum failed (dunno of any
694 physical layer that supports this, but WTH), skip this
702 offset
= decode_udp_ip_header (interface
, dbuf
, bufix
,
703 from
, length
, &paylen
, 1);
706 * If the IP or UDP checksum was bad, skip the packet...
708 * Note: this happens all the time when writing packets via the
709 * fallback socket. The packet received by streams does not have
710 * the IP or UDP checksums filled in, as those are calculated by
721 log_fatal("Internal inconsistency at %s:%d.", MDL
);
723 /* Copy out the data in the packet... */
724 memcpy(buf
, &dbuf
[bufix
], paylen
);
729 /* Common DLPI routines ...
731 * Written by Eric James Negaard, <lmdejn@lmd.ericsson.se>
733 * Based largely in part to the example code contained in the document
734 * "How to Use the STREAMS Data Link Provider Interface (DLPI)", written
735 * by Neal Nuckolls of SunSoft Internet Engineering.
737 * This code has been developed and tested on sparc-based machines running
738 * SunOS 5.5.1, with le and hme network interfaces. It should be pretty
741 * The usual disclaimers apply. This code works for me. Don't blame me
742 * if it makes your machine or network go down in flames. That taken
743 * into consideration, use this code as you wish. If you make usefull
744 * modifications I'd appreciate hearing about it.
747 #define DLPI_MAXWAIT 15 /* Max timeout */
751 * Parse an interface name and extract the unit number
754 static int dlpiunit (ifname
)
764 /* Advance to the end of the name */
767 /* Back up to the start of the first digit */
768 while ((*(cp
-1) >= '0' && *(cp
-1) <= '9') || *(cp
- 1) == ':') cp
--;
770 /* Convert the unit number */
772 while (*cp
>= '0' && *cp
<= '9') {
774 unit
+= (*cp
++ - '0');
781 * dlpiopen - open the DLPI device for a given interface name
784 dlpiopen(const char *ifname
) {
793 /* Open a DLPI device */
794 if (*ifname
== '/') {
797 /* Prepend the device directory */
798 memcpy (devname
, DLPI_DEVDIR
, strlen (DLPI_DEVDIR
));
799 dp
= &devname
[strlen (DLPI_DEVDIR
)];
802 /* Find the end of the interface name */
807 /* Before Solaris 11 we strip off the digit to open the base dev name */
808 #if !defined(USE_DEV_NET)
809 /* And back up to the first digit (unit number) */
810 while ((*(ep
- 1) >= '0' && *(ep
- 1) <= '9') || *(ep
- 1) == ':')
814 /* Copy everything up to the unit number */
820 return open (devname
, O_RDWR
, 0);
824 * dlpiinforeq - request information about the data link provider.
827 static int dlpiinforeq (fd
)
830 dl_info_req_t info_req
;
834 info_req
.dl_primitive
= DL_INFO_REQ
;
837 ctl
.len
= sizeof (info_req
);
838 ctl
.buf
= (char *)&info_req
;
842 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
846 * dlpiphysaddrreq - request the current physical address.
848 static int dlpiphysaddrreq (fd
, addrtype
)
850 unsigned long addrtype
;
852 dl_phys_addr_req_t physaddr_req
;
856 physaddr_req
.dl_primitive
= DL_PHYS_ADDR_REQ
;
857 physaddr_req
.dl_addr_type
= addrtype
;
860 ctl
.len
= sizeof (physaddr_req
);
861 ctl
.buf
= (char *)&physaddr_req
;
865 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
869 * dlpiattachreq - send a request to attach to a specific unit.
871 static int dlpiattachreq (fd
, ppa
)
875 dl_attach_req_t attach_req
;
879 attach_req
.dl_primitive
= DL_ATTACH_REQ
;
880 attach_req
.dl_ppa
= ppa
;
883 ctl
.len
= sizeof (attach_req
);
884 ctl
.buf
= (char *)&attach_req
;
888 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
892 * dlpibindreq - send a request to bind to a specific SAP address.
894 static int dlpibindreq (fd
, sap
, max_conind
, service_mode
, conn_mgmt
, xidtest
)
896 unsigned long max_conind
;
897 unsigned long service_mode
;
898 unsigned long conn_mgmt
;
899 unsigned long xidtest
;
902 dl_bind_req_t bind_req
;
906 bind_req
.dl_primitive
= DL_BIND_REQ
;
907 bind_req
.dl_sap
= sap
;
908 bind_req
.dl_max_conind
= max_conind
;
909 bind_req
.dl_service_mode
= service_mode
;
910 bind_req
.dl_conn_mgmt
= conn_mgmt
;
911 bind_req
.dl_xidtest_flg
= xidtest
;
914 ctl
.len
= sizeof (bind_req
);
915 ctl
.buf
= (char *)&bind_req
;
919 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
922 #if defined(UNUSED_DLPI_INTERFACE)
924 * dlpiunbindreq - send a request to unbind. This function is not actually
925 * used by ISC DHCP, but is included for completeness in case it is
926 * ever required for new work.
928 static int dlpiunbindreq (fd
)
931 dl_unbind_req_t unbind_req
;
935 unbind_req
.dl_primitive
= DL_UNBIND_REQ
;
938 ctl
.len
= sizeof (unbind_req
);
939 ctl
.buf
= (char *)&unbind_req
;
943 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
948 * dlpidetachreq - send a request to detach. This function is not actually
949 * used by ISC DHCP, but is included for completeness in case it is
950 * ever required for new work.
952 static int dlpidetachreq (fd
)
955 dl_detach_req_t detach_req
;
959 detach_req
.dl_primitive
= DL_DETACH_REQ
;
962 ctl
.len
= sizeof (detach_req
);
963 ctl
.buf
= (char *)&detach_req
;
967 return putmsg (fd
, &ctl
, (struct strbuf
*)NULL
, flags
);
969 #endif /* UNUSED_DLPI_INTERFACE */
973 * dlpibindack - receive an ack to a dlbindreq.
975 static int dlpibindack (fd
, bufp
)
979 union DL_primitives
*dlp
;
983 ctl
.maxlen
= DLPI_MAXDLBUF
;
987 if (strgetmsg (fd
, &ctl
,
988 (struct strbuf
*)NULL
, &flags
, "dlpibindack") < 0) {
992 dlp
= (union DL_primitives
*)ctl
.buf
;
994 if (expected (DL_BIND_ACK
, dlp
, flags
) == -1) {
998 if (ctl
.len
< sizeof (dl_bind_ack_t
)) {
999 /* Returned structure is too short */
1007 * dlpiokack - general acknowledgement reception.
1009 static int dlpiokack (fd
, bufp
)
1013 union DL_primitives
*dlp
;
1017 ctl
.maxlen
= DLPI_MAXDLBUF
;
1021 if (strgetmsg (fd
, &ctl
,
1022 (struct strbuf
*)NULL
, &flags
, "dlpiokack") < 0) {
1026 dlp
= (union DL_primitives
*)ctl
.buf
;
1028 if (expected (DL_OK_ACK
, dlp
, flags
) == -1) {
1032 if (ctl
.len
< sizeof (dl_ok_ack_t
)) {
1033 /* Returned structure is too short */
1041 * dlpiinfoack - receive an ack to a dlinforeq.
1043 static int dlpiinfoack (fd
, bufp
)
1047 union DL_primitives
*dlp
;
1051 ctl
.maxlen
= DLPI_MAXDLBUF
;
1055 if (strgetmsg (fd
, &ctl
, (struct strbuf
*)NULL
, &flags
,
1056 "dlpiinfoack") < 0) {
1060 dlp
= (union DL_primitives
*) ctl
.buf
;
1062 if (expected (DL_INFO_ACK
, dlp
, flags
) == -1) {
1066 if (ctl
.len
< sizeof (dl_info_ack_t
)) {
1067 /* Returned structure is too short */
1075 * dlpiphysaddrack - receive an ack to a dlpiphysaddrreq.
1077 int dlpiphysaddrack (fd
, bufp
)
1081 union DL_primitives
*dlp
;
1085 ctl
.maxlen
= DLPI_MAXDLBUF
;
1089 if (strgetmsg (fd
, &ctl
, (struct strbuf
*)NULL
, &flags
,
1090 "dlpiphysaddrack") < 0) {
1094 dlp
= (union DL_primitives
*)ctl
.buf
;
1096 if (expected (DL_PHYS_ADDR_ACK
, dlp
, flags
) == -1) {
1100 if (ctl
.len
< sizeof (dl_phys_addr_ack_t
)) {
1101 /* Returned structure is too short */
1108 #if defined(USE_DLPI_SEND) || defined(USE_DLPI_RECEIVE)
1109 int dlpiunitdatareq (fd
, addr
, addrlen
, minpri
, maxpri
, dbuf
, dbuflen
)
1111 unsigned char *addr
;
1113 unsigned long minpri
;
1114 unsigned long maxpri
;
1115 unsigned char *dbuf
;
1118 long buf
[DLPI_MAXDLBUF
];
1119 union DL_primitives
*dlp
;
1120 struct strbuf ctl
, data
;
1122 /* Set up the control information... */
1123 dlp
= (union DL_primitives
*)buf
;
1124 dlp
-> unitdata_req
.dl_primitive
= DL_UNITDATA_REQ
;
1125 dlp
-> unitdata_req
.dl_dest_addr_length
= addrlen
;
1126 dlp
-> unitdata_req
.dl_dest_addr_offset
= sizeof (dl_unitdata_req_t
);
1127 dlp
-> unitdata_req
.dl_priority
.dl_min
= minpri
;
1128 dlp
-> unitdata_req
.dl_priority
.dl_max
= maxpri
;
1130 /* Append the destination address */
1131 memcpy ((char *)buf
+ dlp
-> unitdata_req
.dl_dest_addr_offset
,
1135 ctl
.len
= dlp
-> unitdata_req
.dl_dest_addr_offset
+ addrlen
;
1136 ctl
.buf
= (char *)buf
;
1139 data
.buf
= (char *)dbuf
;
1142 /* Send the packet down the wire... */
1143 return putmsg (fd
, &ctl
, &data
, 0);
1146 static int dlpiunitdataind (fd
, daddr
, daddrlen
,
1147 saddr
, saddrlen
, grpaddr
, dbuf
, dlen
)
1149 unsigned char *daddr
;
1150 unsigned long *daddrlen
;
1151 unsigned char *saddr
;
1152 unsigned long *saddrlen
;
1153 unsigned long *grpaddr
;
1154 unsigned char *dbuf
;
1157 long buf
[DLPI_MAXDLBUF
];
1158 union DL_primitives
*dlp
;
1159 struct strbuf ctl
, data
;
1163 /* Set up the msg_buf structure... */
1164 dlp
= (union DL_primitives
*)buf
;
1165 dlp
-> unitdata_ind
.dl_primitive
= DL_UNITDATA_IND
;
1167 ctl
.maxlen
= DLPI_MAXDLBUF
;
1169 ctl
.buf
= (char *)buf
;
1173 data
.buf
= (char *)dbuf
;
1175 result
= getmsg (fd
, &ctl
, &data
, &flags
);
1178 log_debug("dlpiunitdataind: %m");
1182 if (ctl
.len
< sizeof (dl_unitdata_ind_t
) ||
1183 dlp
-> unitdata_ind
.dl_primitive
!= DL_UNITDATA_IND
) {
1187 if (data
.len
<= 0) {
1191 /* Copy sender info */
1194 (char *)buf
+ dlp
-> unitdata_ind
.dl_src_addr_offset
,
1195 dlp
-> unitdata_ind
.dl_src_addr_length
);
1198 *saddrlen
= dlp
-> unitdata_ind
.dl_src_addr_length
;
1201 /* Copy destination info */
1204 (char *)buf
+ dlp
-> unitdata_ind
.dl_dest_addr_offset
,
1205 dlp
-> unitdata_ind
.dl_dest_addr_length
);
1208 *daddrlen
= dlp
-> unitdata_ind
.dl_dest_addr_length
;
1212 *grpaddr
= dlp
-> unitdata_ind
.dl_group_address
;
1217 #endif /* !USE_DLPI_HWADDR: USE_DLPI_RECEIVE || USE_DLPI_SEND */
1220 * expected - see if we got what we wanted.
1222 static int expected (prim
, dlp
, msgflags
)
1224 union DL_primitives
*dlp
;
1227 if (msgflags
!= RS_HIPRI
) {
1228 /* Message was not M_PCPROTO */
1232 if (dlp
->dl_primitive
!= prim
) {
1233 /* Incorrect/unexpected return message */
1241 * strgetmsg - get a message from a stream, with timeout.
1243 static int strgetmsg (fd
, ctlp
, datap
, flagsp
, caller
)
1244 struct strbuf
*ctlp
, *datap
;
1257 pfd
.events
= POLLPRI
; /* We're only interested in knowing
1258 * when we can receive the next high
1263 now
= time (&starttime
);
1264 while (now
<= starttime
+ DLPI_MAXWAIT
) {
1265 to_msec
= ((starttime
+ DLPI_MAXWAIT
) - now
) * 1000;
1266 count
= poll (&pfd
, 1, to_msec
);
1269 /* log_fatal ("strgetmsg: timeout"); */
1271 } else if (count
< 0) {
1272 if (errno
== EAGAIN
|| errno
== EINTR
) {
1276 /* log_fatal ("poll: %m"); */
1285 * Set flags argument and issue getmsg ().
1288 if ((result
= getmsg (fd
, ctlp
, datap
, flagsp
)) < 0) {
1293 * Check for MOREDATA and/or MORECTL.
1295 if (result
& (MORECTL
|MOREDATA
)) {
1300 * Check for at least sizeof (long) control data portion.
1302 if (ctlp
-> len
< sizeof (long)) {
1309 #if defined(USE_DLPI_SEND)
1310 int can_unicast_without_arp (ip
)
1311 struct interface_info
*ip
;
1316 int can_receive_unicast_unconfigured (ip
)
1317 struct interface_info
*ip
;
1322 int supports_multiple_interfaces (ip
)
1323 struct interface_info
*ip
;
1328 void maybe_setup_fallback ()
1330 isc_result_t status
;
1331 struct interface_info
*fbi
= (struct interface_info
*)0;
1332 if (setup_fallback (&fbi
, MDL
)) {
1333 if_register_fallback (fbi
);
1334 status
= omapi_register_io_object ((omapi_object_t
*)fbi
,
1336 fallback_discard
, 0, 0);
1337 if (status
!= ISC_R_SUCCESS
)
1338 log_fatal ("Can't register I/O handle for %s: %s",
1339 fbi
-> name
, isc_result_totext (status
));
1340 interface_dereference (&fbi
, MDL
);
1343 #endif /* USE_DLPI_SEND */
1346 get_hw_addr(const char *name
, struct hardware
*hw
) {
1348 long buf
[DLPI_MAXDLBUF
];
1349 union DL_primitives
*dlp
;
1351 dlp
= (union DL_primitives
*)buf
;
1354 * Open a DLPI device.
1356 sock
= dlpiopen(name
);
1358 log_fatal("Can't open DLPI device for %s: %m", name
);
1362 * Submit a DL_INFO_REQ request, to find the dl_mac_type and
1365 if (dlpiinforeq(sock
) < 0) {
1366 log_fatal("Can't request DLPI MAC type for %s: %m", name
);
1368 if (dlpiinfoack(sock
, (char *)buf
) < 0) {
1369 log_fatal("Can't get DLPI MAC type for %s: %m", name
);
1371 switch (dlp
->info_ack
.dl_mac_type
) {
1372 case DL_CSMACD
: /* IEEE 802.3 */
1374 hw
->hbuf
[0] = HTYPE_ETHER
;
1377 hw
->hbuf
[0] = HTYPE_IEEE802
;
1380 hw
->hbuf
[0] = HTYPE_FDDI
;
1383 log_fatal("%s: unsupported DLPI MAC type %lu", name
,
1384 (unsigned long)dlp
->info_ack
.dl_mac_type
);
1387 if (dlp
->info_ack
.dl_provider_style
== DL_STYLE2
) {
1389 * Attach to the device. If this fails, the device
1392 unit
= dlpiunit((char *)name
);
1394 if (dlpiattachreq(sock
, unit
) < 0 ||
1395 dlpiokack(sock
, (char *)buf
) < 0) {
1396 log_fatal("Can't attach DLPI device for %s: %m",
1402 * Submit a DL_PHYS_ADDR_REQ request, to find
1403 * the hardware address.
1405 if (dlpiphysaddrreq(sock
, DL_CURR_PHYS_ADDR
) < 0) {
1406 log_fatal("Can't request DLPI hardware address for %s: %m",
1409 if (dlpiphysaddrack(sock
, (char *)buf
) < 0) {
1410 log_fatal("Can't get DLPI hardware address for %s: %m",
1413 if (dlp
->physaddr_ack
.dl_addr_length
< sizeof(hw
->hbuf
)) {
1415 (char *)buf
+ dlp
->physaddr_ack
.dl_addr_offset
,
1416 dlp
->physaddr_ack
.dl_addr_length
);
1417 hw
->hlen
= dlp
->physaddr_ack
.dl_addr_length
+ 1;
1420 (char *)buf
+ dlp
->physaddr_ack
.dl_addr_offset
,
1421 sizeof(hw
->hbuf
)-1);
1422 hw
->hlen
= sizeof(hw
->hbuf
);
1427 #endif /* USE_DLPI_SEND || USE_DLPI_RECEIVE || USE_DLPI_HWADDR */