dlpi.c 36.1 KB
Newer Older
1 2 3 4 5
/* dlpi.c
 
   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
8
 *
9 10 11
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
12
 *
13 14 15 16 17 18 19
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
 *
21 22 23 24
 *   Internet Systems Consortium, Inc.
 *   950 Charter Street
 *   Redwood City, CA 94063
 *   <info@isc.org>
25
 *   https://www.isc.org/
26
 *
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''.
30 31 32 33
 *
 * Joost Mulders has also done considerable work in debugging the DLPI API
 * support on Solaris and getting this code to work properly on a variety
 * of different Solaris platforms.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
 */

/*
 * Based largely in part to the existing NIT code in nit.c.
 *
 * This code has been developed and tested on sparc-based machines running
 * SunOS 5.5.1, with le and hme network interfaces.  It should be pretty
 * generic, though.
 */

/*
 * Implementation notes:
 *
 * I first tried to write this code to the "vanilla" DLPI 2.0 API.
 * It worked on a Sun Ultra-1 with a hme interface, but didn't work
 * on Sun SparcStation 5's with "le" interfaces (the packets sent out
 * via dlpiunitdatareq contained an Ethernet type of 0x0000 instead
 * of the expected 0x0800).
 *
 * Therefore I added the "DLPI_RAW" code which is a Sun extension to
 * the DLPI standard.  This code works on both of the above machines.
 * This is configurable in the OS-dependent include file by defining
 * USE_DLPI_RAW.
 *
 * It quickly became apparant that I should also use the "pfmod"
 * STREAMS module to cut down on the amount of user level packet
 * processing.  I don't know how widely available "pfmod" is, so it's
 * use is conditionally included. This is configurable in the
 * OS-dependent include file by defining USE_DLPI_PFMOD.
 *
 * A major quirk on the Sun's at least, is that no packets seem to get
 * sent out the interface until six seconds after the interface is
 * first "attached" to [per system reboot] (it's actually from when
 * the interface is attached, not when it is plumbed, so putting a
 * sleep into the dhclient-script at PREINIT time doesn't help).  I
 * HAVE tried, without success to poll the fd to see when it is ready
 * for writing.  This doesn't help at all. If the sleeps are not done,
 * the initial DHCPREQUEST or DHCPDISCOVER never gets sent out, so
 * I've put them here, when register_send and register_receive are
 * called (split up into two three-second sleeps between the notices,
 * so that it doesn't seem like so long when you're watching :-).  The
 * amount of time to sleep is configurable in the OS-dependent include
 * file by defining DLPI_FIRST_SEND_WAIT to be the number of seconds
 * to sleep.
 */

David Hankins's avatar
David Hankins committed
80 81 82 83 84 85 86 87
/*
 * The Open Group Technical Standard can be found here:
 * http://www.opengroup.org/onlinepubs/009618899/index.htm
 *
 * The HP DLPI Programmer's Guide can be found here:
 * http://docs.hp.com/en/B2355-90139/index.html
 */

88 89
#include "dhcpd.h"

90 91
#if defined (USE_DLPI_SEND) || defined (USE_DLPI_RECEIVE) || \
    defined(USE_DLPI_HWADDR)
92

93 94 95 96 97 98 99
# include <sys/ioctl.h>
# include <sys/time.h>
# include <sys/dlpi.h>
# include <stropts.h>
# ifdef USE_DLPI_PFMOD
#  include <sys/pfmod.h>
# endif
100 101
#include <poll.h>
#include <errno.h>
102

103 104 105 106
# include <netinet/in_systm.h>
# include "includes/netinet/ip.h"
# include "includes/netinet/udp.h"
# include "includes/netinet/if_ether.h"
107

108 109 110 111 112 113
# ifdef USE_DLPI_PFMOD
#  ifdef USE_DLPI_RAW
#   define DLPI_MODNAME "DLPI+RAW+PFMOD"
#  else
#   define DLPI_MODNAME "DLPI+PFMOD"
#  endif
114
# else
115 116 117 118 119
#  ifdef USE_DLPI_RAW
#   define DLPI_MODNAME "DLPI+RAW"
#  else
#   define DLPI_MODNAME "DLPI"
#  endif
120 121
# endif

122 123 124
# ifndef ABS
#  define ABS(x) ((x) >= 0 ? (x) : 0-(x))
# endif
125

126
#if defined(USE_DLPI_PFMOD) || defined(USE_DLPI_RAW)
127
static int strioctl (int fd, int cmd, int timeout, int len, char *dp);
128
#endif
129 130 131

#define DLPI_MAXDLBUF		8192	/* Buffer size */
#define DLPI_MAXDLADDR		1024	/* Max address size */
132 133 134 135 136 137 138

/* Device directory */
#if defined(USE_DEV_NET)
#define DLPI_DEVDIR		"/dev/net/"  /* Solaris 11 + */
#else
#define DLPI_DEVDIR		"/dev/"      /* Pre Solaris 11 */
#endif
139

David Hankins's avatar
David Hankins committed
140
static int dlpiopen(const char *ifname);
141 142 143 144 145 146 147
static int dlpiunit (char *ifname);
static int dlpiinforeq (int fd);
static int dlpiphysaddrreq (int fd, unsigned long addrtype);
static int dlpiattachreq (int fd, unsigned long ppa);
static int dlpibindreq (int fd, unsigned long sap, unsigned long max_conind,
			unsigned long service_mode, unsigned long conn_mgmt,
			unsigned long xidtest);
148 149 150 151
#if defined(UNUSED_DLPI_INTERFACE)
/* These functions are unused at present, but may be used at a later date.
 * defined out to avoid compiler warnings about unused static functions.
 */
152 153
static int dlpidetachreq (int fd);
static int dlpiunbindreq (int fd);
154
#endif
155 156 157 158
static int dlpiokack (int fd, char *bufp);
static int dlpiinfoack (int fd, char *bufp);
static int dlpiphysaddrack (int fd, char *bufp);
static int dlpibindack (int fd, char *bufp);
159 160 161 162
#if defined(USE_DLPI_SEND) || defined(USE_DLPI_RECEIVE)
/* These functions are not used if we're only sourcing the get_hw_addr()
 * function (for USE_SOCKETS).
 */
163 164 165 166 167 168 169 170 171 172 173
static int dlpiunitdatareq (int fd, unsigned char *addr, int addrlen, 
			    unsigned long minpri, unsigned long maxpri, 
			    unsigned char *data, int datalen);
static int dlpiunitdataind (int fd,
			    unsigned char *dstaddr,
			    unsigned long *dstaddrlen,
			    unsigned char *srcaddr,
			    unsigned long *srcaddrlen,
			    unsigned long *grpaddr,
			    unsigned char *data,
			    int datalen);
174
#endif /* !USE_DLPI_HWADDR: USE_DLPI_SEND || USE_DLPI_RECEIVE */
175 176 177 178
static int expected (unsigned long prim, union DL_primitives *dlp, 
		     int msgflags);
static int strgetmsg (int fd, struct strbuf *ctlp, struct strbuf *datap,
		      int *flagsp, char *caller);
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

/* Reinitializes the specified interface after an address change.   This
   is not required for packet-filter APIs. */

#ifdef USE_DLPI_SEND
void if_reinitialize_send (info)
	struct interface_info *info;
{
}
#endif

#ifdef USE_DLPI_RECEIVE
void if_reinitialize_receive (info)
	struct interface_info *info;
{
}
#endif

/* Called by get_interface_list for each interface that's discovered.
   Opens a packet filter for each interface and adds it to the select
   mask. */

int if_register_dlpi (info)
	struct interface_info *info;
{
	int sock;
	int unit;
	long buf [DLPI_MAXDLBUF];
	union DL_primitives *dlp;

	dlp = (union DL_primitives *)buf;

	/* Open a DLPI device */
	if ((sock = dlpiopen (info -> name)) < 0) {
213
	    log_fatal ("Can't open DLPI device for %s: %m", info -> name);
214 215 216
	}

	/*
David Hankins's avatar
David Hankins committed
217
	 * Submit a DL_INFO_REQ request, to find the dl_mac_type and 
218
         * dl_provider_style
219 220
	 */
	if (dlpiinforeq(sock) < 0 || dlpiinfoack(sock, (char *)buf) < 0) {
221
	    log_fatal ("Can't get DLPI MAC type for %s: %m", info -> name);
222 223 224 225
	} else {
	    switch (dlp -> info_ack.dl_mac_type) {
	      case DL_CSMACD: /* IEEE 802.3 */
	      case DL_ETHER:
Ted Lemon's avatar
Ted Lemon committed
226
		info -> hw_address.hbuf [0] = HTYPE_ETHER;
227
		break;
228 229
	      /* adding token ring 5/1999 - mayer@ping.at  */ 
	      case DL_TPR:
Ted Lemon's avatar
Ted Lemon committed
230
		info -> hw_address.hbuf [0] = HTYPE_IEEE802;
231
		break;
Ted Lemon's avatar
Ted Lemon committed
232
	      case DL_FDDI:
Ted Lemon's avatar
Ted Lemon committed
233
		info -> hw_address.hbuf [0] = HTYPE_FDDI;
Ted Lemon's avatar
Ted Lemon committed
234
		break;
235
	      default:
236 237
		log_fatal("%s: unsupported DLPI MAC type %lu", info->name,
			  (unsigned long)dlp->info_ack.dl_mac_type);
238 239
		break;
	    }
240 241 242 243 244 245 246 247 248 249 250
            /*
             * copy the sap length and broadcast address of this interface
             * to interface_info. This fixes nothing but seemed nicer than to
             * assume -2 and ffffff.
             */
            info -> dlpi_sap_length = dlp -> info_ack.dl_sap_length;
            info -> dlpi_broadcast_addr.hlen = 
             dlp -> info_ack.dl_brdcst_addr_length;
            memcpy (info -> dlpi_broadcast_addr.hbuf, 
             (char *)dlp + dlp -> info_ack.dl_brdcst_addr_offset, 
             dlp -> info_ack.dl_brdcst_addr_length);
251 252 253 254 255 256 257 258 259 260 261
	}

	if (dlp -> info_ack.dl_provider_style == DL_STYLE2) {
	    /*
	     * Attach to the device.  If this fails, the device
	     * does not exist.
	     */
	    unit = dlpiunit (info -> name);
	
	    if (dlpiattachreq (sock, unit) < 0
		|| dlpiokack (sock, (char *)buf) < 0) {
262
		log_fatal ("Can't attach DLPI device for %s: %m", info -> name);
263 264 265 266 267 268
	    }
	}

	/*
	 * Bind to the IP service access point (SAP), connectionless (CLDLS).
	 */
David Hankins's avatar
David Hankins committed
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);
272 273 274 275 276 277 278 279
	}

	/*
	 * Submit a DL_PHYS_ADDR_REQ request, to find
	 * the hardware address
	 */
	if (dlpiphysaddrreq (sock, DL_CURR_PHYS_ADDR) < 0
	    || dlpiphysaddrack (sock, (char *)buf) < 0) {
280
	    log_fatal ("Can't get DLPI hardware address for %s: %m",
281 282 283
		   info -> name);
	}

Ted Lemon's avatar
Ted Lemon committed
284 285
	info -> hw_address.hlen = dlp -> physaddr_ack.dl_addr_length + 1;
	memcpy (&info -> hw_address.hbuf [1],
286 287 288 289 290
		(char *)buf + dlp -> physaddr_ack.dl_addr_offset,
		dlp -> physaddr_ack.dl_addr_length);

#ifdef USE_DLPI_RAW
	if (strioctl (sock, DLIOCRAW, INFTIM, 0, 0) < 0) {
291
	    log_fatal ("Can't set DLPI RAW mode for %s: %m",
292 293 294 295 296 297
		   info -> name);
	}
#endif

#ifdef USE_DLPI_PFMOD
	if (ioctl (sock, I_PUSH, "pfmod") < 0) {
298
	    log_fatal ("Can't push packet filter onto DLPI for %s: %m",
299 300 301 302 303 304 305
		   info -> name);
	}
#endif

	return sock;
}

306
#if defined(USE_DLPI_PFMOD) || defined(USE_DLPI_RAW)
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
static int
strioctl (fd, cmd, timeout, len, dp)
int fd;
int cmd;
int timeout;
int len;
char *dp;
{
    struct strioctl sio;
    int rslt;

    sio.ic_cmd = cmd;
    sio.ic_timout = timeout;
    sio.ic_len = len;
    sio.ic_dp = dp;

    if ((rslt = ioctl (fd, I_STR, &sio)) < 0) {
	return rslt;
    } else {
	return sio.ic_len;
    }
}
329
#endif /* USE_DPI_PFMOD || USE_DLPI_RAW */
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

#ifdef USE_DLPI_SEND
void if_register_send (info)
	struct interface_info *info;
{
	/* If we're using the DLPI API for sending and receiving,
	   we don't need to register this interface twice. */
#ifndef USE_DLPI_RECEIVE
# ifdef USE_DLPI_PFMOD
	struct packetfilt pf;
# endif

	info -> wfdesc = if_register_dlpi (info);

# ifdef USE_DLPI_PFMOD
	/* Set up an PFMOD filter that rejects everything... */
	pf.Pf_Priority = 0;
	pf.Pf_FilterLen = 1;
	pf.Pf_Filter [0] = ENF_PUSHZERO;

	/* Install the filter */
	if (strioctl (info -> wfdesc, PFIOCSETF, INFTIM,
		      sizeof (pf), (char *)&pf) < 0) {
353
	    log_fatal ("Can't set PFMOD send filter on %s: %m", info -> name);
354 355 356 357 358 359 360 361 362 363 364 365
	}

# endif /* USE_DLPI_PFMOD */
#else /* !defined (USE_DLPI_RECEIVE) */
	/*
	 * If using DLPI for both send and receive, simply re-use
	 * the read file descriptor that was set up earlier.
	 */
	info -> wfdesc = info -> rfdesc;
#endif

        if (!quiet_interface_discovery)
366
		log_info ("Sending on   DLPI/%s/%s%s%s",
367
		      info -> name,
Ted Lemon's avatar
Ted Lemon committed
368 369 370
		      print_hw_addr (info -> hw_address.hbuf [0],
				     info -> hw_address.hlen - 1,
				     &info -> hw_address.hbuf [1]),
371
		      (info -> shared_network ? "/" : ""),
372
		      (info -> shared_network ?
373
		       info -> shared_network -> name : ""));
374 375 376 377 378 379 380 381 382 383

#ifdef DLPI_FIRST_SEND_WAIT
/* See the implementation notes at the beginning of this file */
# ifdef USE_DLPI_RECEIVE
	sleep (DLPI_FIRST_SEND_WAIT - (DLPI_FIRST_SEND_WAIT / 2));
# else
	sleep (DLPI_FIRST_SEND_WAIT);
# endif
#endif
}
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

void if_deregister_send (info)
	struct interface_info *info;
{
	/* If we're using the DLPI API for sending and receiving,
	   we don't need to register this interface twice. */
#ifndef USE_DLPI_RECEIVE
	close (info -> wfdesc);
#endif
	info -> wfdesc = -1;

        if (!quiet_interface_discovery)
		log_info ("Disabling output on DLPI/%s/%s%s%s",
		      info -> name,
		      print_hw_addr (info -> hw_address.hbuf [0],
				     info -> hw_address.hlen - 1,
				     &info -> hw_address.hbuf [1]),
		      (info -> shared_network ? "/" : ""),
		      (info -> shared_network ?
		       info -> shared_network -> name : ""));
}
405 406 407 408 409 410 411
#endif /* USE_DLPI_SEND */

#ifdef USE_DLPI_RECEIVE
/* Packet filter program...
   XXX Changes to the filter program may require changes to the constant
   offsets used in if_register_send to patch the NIT program! XXX */

Francis Dupont's avatar
Francis Dupont committed
412 413 414 415
#if defined(RELAY_PORT)
#error "Relay port is not yet supported for DLPI"
#endif

416 417 418 419 420
void if_register_receive (info)
	struct interface_info *info;
{
#ifdef USE_DLPI_PFMOD
	struct packetfilt pf;
421 422
        struct ip iphdr;
        u_int16_t offset;
423 424 425 426 427 428 429 430 431 432 433 434 435
#endif

	/* Open a DLPI device and hang it on this interface... */
	info -> rfdesc = if_register_dlpi (info);

#ifdef USE_DLPI_PFMOD
	/* Set up the PFMOD filter program. */
	/* XXX Unlike the BPF filter program, this one won't work if the
	   XXX IP packet is fragmented or if there are options on the IP
	   XXX header. */
	pf.Pf_Priority = 0;
	pf.Pf_FilterLen = 0;

436 437
#if defined (USE_DLPI_RAW)
# define ETHER_H_PREFIX (14) /* sizeof (ethernet_header) */
438 439 440 441 442 443 444
    /*
     * ethertype == ETHERTYPE_IP
     */
    offset = 12;
    pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHWORD + (offset / 2);
    pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT | ENF_CAND;
    pf.Pf_Filter [pf.Pf_FilterLen++] = htons (ETHERTYPE_IP);
445 446
# else
# define ETHER_H_PREFIX (0)
447
# endif /* USE_DLPI_RAW */
448 449 450 451 452 453 454 455 456
	/*
	 * The packets that will be received on this file descriptor
	 * will be IP packets (due to the SAP that was specified in
	 * the dlbind call).  There will be no ethernet header.
	 * Therefore, setup the packet filter to check the protocol
	 * field for UDP, and the destination port number equal
	 * to the local port.  All offsets are relative to the start
	 * of an IP packet.
	 */
457 458 459 460

        /*
         * 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;
465

466 467 468 469
        /*
         * protocol should be udp. this is a byte compare, test for
         * endianess.
         */
470
        offset = ETHER_H_PREFIX + ((u_int8_t *)&(iphdr.ip_p) - (u_int8_t *)&iphdr);
471 472 473
        pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHWORD + (offset / 2);
        pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT | ENF_AND;
        pf.Pf_Filter [pf.Pf_FilterLen++] = htons (0x00FF);
474 475
        pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT | ENF_CAND;
      pf.Pf_Filter [pf.Pf_FilterLen++] = htons (IPPROTO_UDP);
476 477 478 479

	/* Install the filter... */
	if (strioctl (info -> rfdesc, PFIOCSETF, INFTIM,
		      sizeof (pf), (char *)&pf) < 0) {
480
	    log_fatal ("Can't set PFMOD receive filter on %s: %m", info -> name);
481
	}
482
#endif /* USE_DLPI_PFMOD */
483 484

        if (!quiet_interface_discovery)
485
		log_info ("Listening on DLPI/%s/%s%s%s",
486
		      info -> name,
Ted Lemon's avatar
Ted Lemon committed
487 488 489
		      print_hw_addr (info -> hw_address.hbuf [0],
				     info -> hw_address.hlen - 1,
				     &info -> hw_address.hbuf [1]),
490
		      (info -> shared_network ? "/" : ""),
491
		      (info -> shared_network ?
492
		       info -> shared_network -> name : ""));
493 494 495 496 497 498 499 500 501 502

#ifdef DLPI_FIRST_SEND_WAIT
/* See the implementation notes at the beginning of this file */
# ifdef USE_DLPI_SEND
	sleep (DLPI_FIRST_SEND_WAIT / 2);
# else
	sleep (DLPI_FIRST_SEND_WAIT);
# endif
#endif
}
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

void if_deregister_receive (info)
	struct interface_info *info;
{
	/* If we're using the DLPI API for sending and receiving,
	   we don't need to register this interface twice. */
#ifndef USE_DLPI_SEND
	close (info -> rfdesc);
#endif
	info -> rfdesc = -1;

        if (!quiet_interface_discovery)
		log_info ("Disabling input on DLPI/%s/%s%s%s",
		      info -> name,
		      print_hw_addr (info -> hw_address.hbuf [0],
				     info -> hw_address.hlen - 1,
				     &info -> hw_address.hbuf [1]),
		      (info -> shared_network ? "/" : ""),
		      (info -> shared_network ?
		       info -> shared_network -> name : ""));
}
524 525 526 527 528 529 530 531 532 533 534 535
#endif /* USE_DLPI_RECEIVE */

#ifdef USE_DLPI_SEND
ssize_t send_packet (interface, packet, raw, len, from, to, hto)
	struct interface_info *interface;
	struct packet *packet;
	struct dhcp_packet *raw;
	size_t len;
	struct in_addr from;
	struct sockaddr_in *to;
	struct hardware *hto;
{
536
#ifdef USE_DLPI_RAW
Ted Lemon's avatar
Ted Lemon committed
537
	double hh [32];
Shawn Routhier's avatar
Shawn Routhier committed
538
	int fudge;
539
#endif
540 541
	double ih [1536 / sizeof (double)];
	unsigned char *dbuf = (unsigned char *)ih;
542
	unsigned dbuflen;
543 544
	unsigned char dstaddr [DLPI_MAXDLADDR];
	unsigned addrlen;
545
	int result;
546

Ted Lemon's avatar
Ted Lemon committed
547 548 549 550
	if (!strcmp (interface -> name, "fallback"))
		return send_fallback (interface, packet, raw,
				      len, from, to, hto);

551 552 553
	if (hto == NULL && interface->anycast_mac_addr.hlen)
		hto = &interface->anycast_mac_addr;

554 555 556 557
	dbuflen = 0;

	/* Assemble the headers... */
#ifdef USE_DLPI_RAW
558
	assemble_hw_header (interface, (unsigned char *)hh, &dbuflen, hto);
559 560
      if (dbuflen > sizeof hh)
              log_fatal ("send_packet: hh buffer too small.\n");
561 562
	fudge = dbuflen % 4; /* IP header must be word-aligned. */
	memcpy (dbuf + fudge, (unsigned char *)hh, dbuflen);
Ted Lemon's avatar
Ted Lemon committed
563
	dbuflen += fudge;
564 565 566 567 568 569 570 571 572 573
#endif
	assemble_udp_ip_header (interface, dbuf, &dbuflen, from.s_addr,
				to -> sin_addr.s_addr, to -> sin_port,
				(unsigned char *)raw, len);

	/* Copy the data into the buffer (yuk). */
	memcpy (dbuf + dbuflen, raw, len);
	dbuflen += len;

#ifdef USE_DLPI_RAW
574
	result = write (interface -> wfdesc, dbuf + fudge, dbuflen - fudge);
575 576
#else

577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
	/*
         * Setup the destination address (DLSAP) in dstaddr 
         *
         * If sap_length < 0 we must deliver the DLSAP as phys+sap. 
         * If sap_length > 0 we must deliver the DLSAP as sap+phys.
         *
         * sap = Service Access Point == ETHERTYPE_IP
         * sap + datalink address is called DLSAP in dlpi speak.
         */
        { /* ENCODE DLSAP */
          unsigned char phys [DLPI_MAXDLADDR];
          unsigned char sap [4];
          int sap_len = interface -> dlpi_sap_length;
          int phys_len = interface -> hw_address.hlen - 1;

          /* sap = htons (ETHERTYPE_IP) kludge */
          memset (sap, 0, sizeof (sap));
# if (BYTE_ORDER == LITTLE_ENDIAN)
          sap [0] = 0x00;
          sap [1] = 0x08;
# else
          sap [0] = 0x08;
          sap [1] = 0x00;
# endif
601

602 603 604 605 606
        if (hto && hto -> hlen == interface -> hw_address.hlen)
             memcpy ( phys, (char *) &hto -> hbuf [1], phys_len);
          else 
             memcpy ( phys, interface -> dlpi_broadcast_addr.hbuf, 
              interface -> dlpi_broadcast_addr.hlen);
607

608 609 610 611 612 613 614 615 616 617
          if (sap_len < 0) { 
             memcpy ( dstaddr, phys, phys_len);
             memcpy ( (char *) &dstaddr [phys_len], sap, ABS (sap_len));
          }
          else {
             memcpy ( dstaddr, (void *) sap, sap_len);
             memcpy ( (char *) &dstaddr [sap_len], phys, phys_len);
          }
        addrlen = phys_len + ABS (sap_len);
      } /* ENCODE DLSAP */
618

619 620
	result = dlpiunitdatareq (interface -> wfdesc, dstaddr, addrlen,
				  0, 0, dbuf, dbuflen);
621
#endif /* USE_DLPI_RAW */
622
	if (result < 0)
Ted Lemon's avatar
Ted Lemon committed
623
		log_error ("send_packet: %m");
624
	return result;
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
}
#endif /* USE_DLPI_SEND */

#ifdef USE_DLPI_RECEIVE
ssize_t receive_packet (interface, buf, len, from, hfrom)
	struct interface_info *interface;
	unsigned char *buf;
	size_t len;
	struct sockaddr_in *from;
	struct hardware *hfrom;
{
	unsigned char dbuf [1536];
	unsigned char srcaddr [DLPI_MAXDLADDR];
	unsigned long srcaddrlen;
	int length = 0;
	int offset = 0;
641
	int bufix = 0;
Francis Dupont's avatar
Francis Dupont committed
642
	unsigned paylen;
643 644 645 646 647 648 649 650 651 652
	
#ifdef USE_DLPI_RAW
	length = read (interface -> rfdesc, dbuf, sizeof (dbuf));
#else	
	length = dlpiunitdataind (interface -> rfdesc, (unsigned char *)NULL,
				  (unsigned long *)NULL, srcaddr, &srcaddrlen,
				  (unsigned long *)NULL, dbuf, sizeof (dbuf));
#endif

	if (length <= 0) {
653
	    log_error("receive_packet: %m");
654 655 656
	    return length;
	}

657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
# if !defined (USE_DLPI_RAW)
        /*
         * Copy the sender's hw address into hfrom
         * If sap_len < 0 the DLSAP is as phys+sap.
         * If sap_len > 0 the DLSAP is as sap+phys.
         *
         * sap is discarded here.
         */
        { /* DECODE DLSAP */
          int sap_len = interface -> dlpi_sap_length;
          int phys_len = interface -> hw_address.hlen - 1;

          if (hfrom && (srcaddrlen == ABS (sap_len) + phys_len )) {
            hfrom -> hbuf [0] = interface -> hw_address.hbuf [0];
            hfrom -> hlen = interface -> hw_address.hlen;
            
            if (sap_len < 0) {
              memcpy ((char *) &hfrom -> hbuf [1], srcaddr, phys_len);
            }
            else {
677
              memcpy((char *)&hfrom->hbuf[1], srcaddr + sap_len, phys_len);
678 679 680 681 682 683 684 685
            }
          } 
          else if (hfrom) {
            memset (hfrom, '\0', sizeof *hfrom);
          }
        } /* DECODE_DLSAP */

# endif /* !defined (USE_DLPI_RAW) */
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702

	/* Decode the IP and UDP headers... */
	bufix = 0;
#ifdef USE_DLPI_RAW
	/* Decode the physical header... */
	offset = decode_hw_header (interface, dbuf, bufix, hfrom);

	/* If a physical layer checksum failed (dunno of any
	   physical layer that supports this, but WTH), skip this
	   packet. */
	if (offset < 0) {
		return 0;
	}
	bufix += offset;
	length -= offset;
#endif
	offset = decode_udp_ip_header (interface, dbuf, bufix,
703
				       from, length, &paylen, 1);
704

705 706 707 708 709 710 711 712
	/*
	 * If the IP or UDP checksum was bad, skip the packet...
	 *
	 * Note: this happens all the time when writing packets via the
	 * fallback socket.  The packet received by streams does not have
	 * the IP or UDP checksums filled in, as those are calculated by
	 * the hardware.
	 */
713
	if (offset < 0) {
714
		return 0;
715 716 717 718 719
	}

	bufix += offset;
	length -= offset;

720 721 722
	if (length < paylen)
		log_fatal("Internal inconsistency at %s:%d.", MDL);

723
	/* Copy out the data in the packet... */
724 725
	memcpy(buf, &dbuf [bufix], paylen);
	return paylen;
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
}
#endif

/* Common DLPI routines ...
 *
 * Written by Eric James Negaard, <lmdejn@lmd.ericsson.se>
 *
 * Based largely in part to the example code contained in the document
 * "How to Use the STREAMS Data Link Provider Interface (DLPI)", written
 * by Neal Nuckolls of SunSoft Internet Engineering.
 * 
 * This code has been developed and tested on sparc-based machines running
 * SunOS 5.5.1, with le and hme network interfaces.  It should be pretty
 * generic, though.
 * 
 * The usual disclaimers apply.  This code works for me.  Don't blame me
 * if it makes your machine or network go down in flames.  That taken
 * into consideration, use this code as you wish.  If you make usefull
 * modifications I'd appreciate hearing about it.
 */

#define DLPI_MAXWAIT		15	/* Max timeout */


/*
 * Parse an interface name and extract the unit number
 */

static int dlpiunit (ifname)
	char *ifname;
{
757
	char *cp;
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
	int unit;
	
	if (!ifname) {
		return 0;
	}
	
	/* Advance to the end of the name */
	cp = ifname;
	while (*cp) cp++;
	/* Back up to the start of the first digit */
	while ((*(cp-1) >= '0' && *(cp-1) <= '9') || *(cp - 1) == ':') cp--;
	
	/* Convert the unit number */
	unit = 0;
	while (*cp >= '0' && *cp <= '9') {
		unit *= 10;
		unit += (*cp++ - '0');
	}
	
	return unit;
}

/*
 * dlpiopen - open the DLPI device for a given interface name
 */
David Hankins's avatar
David Hankins committed
783 784
static int
dlpiopen(const char *ifname) {
785
	char devname [50];
David Hankins's avatar
David Hankins committed
786 787
	char *dp;
	const char *cp, *ep;
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
	
	if (!ifname) {
		return -1;
	}
	
	/* Open a DLPI device */
	if (*ifname == '/') {
		dp = devname;
	} else {
		/* Prepend the device directory */
		memcpy (devname, DLPI_DEVDIR, strlen (DLPI_DEVDIR));
		dp = &devname [strlen (DLPI_DEVDIR)];
	}

	/* Find the end of the interface name */
	ep = cp = ifname;
	while (*ep)
		ep++;
806 807 808

/* Before Solaris 11 we strip off the digit to open the base dev name */
#if !defined(USE_DEV_NET)
809 810 811
	/* And back up to the first digit (unit number) */
	while ((*(ep - 1) >= '0' && *(ep - 1) <= '9') || *(ep - 1) == ':')
		ep--;
812
#endif
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
	
	/* Copy everything up to the unit number */
	while (cp < ep) {
		*dp++ = *cp++;
	}
	*dp = '\0';
	
	return open (devname, O_RDWR, 0);
}

/*
 * dlpiinforeq - request information about the data link provider.
 */

static int dlpiinforeq (fd)
	int fd;
{
	dl_info_req_t info_req;
	struct strbuf ctl;
	int flags;
	
	info_req.dl_primitive = DL_INFO_REQ;
	
	ctl.maxlen = 0;
	ctl.len = sizeof (info_req);
	ctl.buf = (char *)&info_req;
	
	flags = RS_HIPRI;
	
	return putmsg (fd, &ctl, (struct strbuf *)NULL, flags);
}

/*
 * dlpiphysaddrreq - request the current physical address.
 */
static int dlpiphysaddrreq (fd, addrtype)
	int fd;
	unsigned long addrtype;
{
	dl_phys_addr_req_t physaddr_req;
	struct strbuf ctl;
	int flags;
	
	physaddr_req.dl_primitive = DL_PHYS_ADDR_REQ;
	physaddr_req.dl_addr_type = addrtype;
	
	ctl.maxlen = 0;
	ctl.len = sizeof (physaddr_req);
	ctl.buf = (char *)&physaddr_req;
	
	flags = RS_HIPRI;
	
	return putmsg (fd, &ctl, (struct strbuf *)NULL, flags);
}

/*
 * dlpiattachreq - send a request to attach to a specific unit.
 */
static int dlpiattachreq (fd, ppa)
	unsigned long ppa;
	int fd;
{
	dl_attach_req_t	attach_req;
	struct strbuf ctl;
	int flags;
	
	attach_req.dl_primitive = DL_ATTACH_REQ;
	attach_req.dl_ppa = ppa;
	
	ctl.maxlen = 0;
	ctl.len = sizeof (attach_req);
	ctl.buf = (char *)&attach_req;
	
	flags = 0;
	
	return putmsg (fd, &ctl, (struct strbuf*)NULL, flags);
}

/*
 * dlpibindreq - send a request to bind to a specific SAP address.
 */
static int dlpibindreq (fd, sap, max_conind, service_mode, conn_mgmt, xidtest)
	unsigned long sap;
	unsigned long max_conind;
	unsigned long service_mode;
	unsigned long conn_mgmt;
	unsigned long xidtest;
	int fd;
{
	dl_bind_req_t bind_req;
	struct strbuf ctl;
	int flags;
	
	bind_req.dl_primitive = DL_BIND_REQ;
	bind_req.dl_sap = sap;
	bind_req.dl_max_conind = max_conind;
	bind_req.dl_service_mode = service_mode;
	bind_req.dl_conn_mgmt = conn_mgmt;
	bind_req.dl_xidtest_flg = xidtest;
	
	ctl.maxlen = 0;
	ctl.len = sizeof (bind_req);
	ctl.buf = (char *)&bind_req;
	
	flags = 0;
	
	return putmsg (fd, &ctl, (struct strbuf*)NULL, flags);
}

922
#if defined(UNUSED_DLPI_INTERFACE)
923
/*
924 925 926
 * dlpiunbindreq - send a request to unbind.  This function is not actually
 *	used by ISC DHCP, but is included for completeness in case it is
 *	ever required for new work.
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
 */
static int dlpiunbindreq (fd)
	int fd;
{
	dl_unbind_req_t	unbind_req;
	struct strbuf ctl;
	int flags;
	
	unbind_req.dl_primitive = DL_UNBIND_REQ;
	
	ctl.maxlen = 0;
	ctl.len = sizeof (unbind_req);
	ctl.buf = (char *)&unbind_req;
	
	flags = 0;
	
	return putmsg (fd, &ctl, (struct strbuf*)NULL, flags);
}


/*
948 949 950
 * dlpidetachreq - send a request to detach.  This function is not actually
 *	used by ISC DHCP, but is included for completeness in case it is
 *	ever required for new work.
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
 */
static int dlpidetachreq (fd)
	int fd;
{
	dl_detach_req_t	detach_req;
	struct strbuf ctl;
	int flags;
	
	detach_req.dl_primitive = DL_DETACH_REQ;
	
	ctl.maxlen = 0;
	ctl.len = sizeof (detach_req);
	ctl.buf = (char *)&detach_req;
	
	flags = 0;
	
	return putmsg (fd, &ctl, (struct strbuf*)NULL, flags);
}
969
#endif /* UNUSED_DLPI_INTERFACE */
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993


/*
 * dlpibindack - receive an ack to a dlbindreq.
 */
static int dlpibindack (fd, bufp)
	char *bufp;
	int fd;
{
	union DL_primitives *dlp;
	struct strbuf ctl;
	int flags;
	
	ctl.maxlen = DLPI_MAXDLBUF;
	ctl.len = 0;
	ctl.buf = bufp;

	if (strgetmsg (fd, &ctl,
		       (struct strbuf*)NULL, &flags, "dlpibindack") < 0) {
		return -1;
	}
	
	dlp = (union DL_primitives *)ctl.buf;
	
Shawn Routhier's avatar
Shawn Routhier committed
994
	if (expected (DL_BIND_ACK, dlp, flags) == -1) {
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
		return -1;
	}
	
	if (ctl.len < sizeof (dl_bind_ack_t)) {
		/* Returned structure is too short */
		return -1;
	}

	return 0;
}

/*
 * dlpiokack - general acknowledgement reception.
 */
static int dlpiokack (fd, bufp)
	char *bufp;
	int fd;
{
	union DL_primitives *dlp;
	struct strbuf ctl;
	int flags;
	
	ctl.maxlen = DLPI_MAXDLBUF;
	ctl.len = 0;
	ctl.buf = bufp;
	
	if (strgetmsg (fd, &ctl,
		       (struct strbuf*)NULL, &flags, "dlpiokack") < 0) {
		return -1;
	}
	
	dlp = (union DL_primitives *)ctl.buf;
	
Shawn Routhier's avatar
Shawn Routhier committed
1028
	if (expected (DL_OK_ACK, dlp, flags) == -1) {
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
		return -1;
	}
	
	if (ctl.len < sizeof (dl_ok_ack_t)) {
		/* Returned structure is too short */
		return -1;
	}
	
	return 0;
}

/*
 * dlpiinfoack - receive an ack to a dlinforeq.
 */
static int dlpiinfoack (fd, bufp)
	char *bufp;
	int fd;
{
	union DL_primitives *dlp;
	struct strbuf ctl;
	int flags;
	
	ctl.maxlen = DLPI_MAXDLBUF;
	ctl.len = 0;
	ctl.buf = bufp;
	
	if (strgetmsg (fd, &ctl, (struct strbuf *)NULL, &flags,
		       "dlpiinfoack") < 0) {
		return -1;
	}
	
	dlp = (union DL_primitives *) ctl.buf;
	
Shawn Routhier's avatar
Shawn Routhier committed
1062
	if (expected (DL_INFO_ACK, dlp, flags) == -1) {
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
		return -1;
	}
	
	if (ctl.len < sizeof (dl_info_ack_t)) {
		/* Returned structure is too short */
		return -1;
	}
	
	return 0;
}

/*
 * dlpiphysaddrack - receive an ack to a dlpiphysaddrreq.
 */
int dlpiphysaddrack (fd, bufp)
	char *bufp;
	int fd;
{
	union DL_primitives *dlp;
	struct strbuf ctl;
	int flags;
	
	ctl.maxlen = DLPI_MAXDLBUF;
	ctl.len = 0;
	ctl.buf = bufp;
	
	if (strgetmsg (fd, &ctl, (struct strbuf *)NULL, &flags,
		       "dlpiphysaddrack") < 0) {
		return -1;
	}

	dlp = (union DL_primitives *)ctl.buf;
	
Shawn Routhier's avatar
Shawn Routhier committed
1096
	if (expected (DL_PHYS_ADDR_ACK, dlp, flags) == -1) {
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
		return -1;
	}

	if (ctl.len < sizeof (dl_phys_addr_ack_t)) {
		/* Returned structure is too short */
		return -1;
	}
	
	return 0;
}

1108
#if defined(USE_DLPI_SEND) || defined(USE_DLPI_RECEIVE)
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
int dlpiunitdatareq (fd, addr, addrlen, minpri, maxpri, dbuf, dbuflen)
	int fd;
	unsigned char *addr;
	int addrlen;
	unsigned long minpri;
	unsigned long maxpri;
	unsigned char *dbuf;
	int dbuflen;
{
	long buf [DLPI_MAXDLBUF];
	union DL_primitives *dlp;
	struct strbuf ctl, data;
	
	/* Set up the control information... */
	dlp = (union DL_primitives *)buf;
	dlp -> unitdata_req.dl_primitive = DL_UNITDATA_REQ;
	dlp -> unitdata_req.dl_dest_addr_length = addrlen;
	dlp -> unitdata_req.dl_dest_addr_offset = sizeof (dl_unitdata_req_t);
	dlp -> unitdata_req.dl_priority.dl_min = minpri;
	dlp -> unitdata_req.dl_priority.dl_max = maxpri;

	/* Append the destination address */
	memcpy ((char *)buf + dlp -> unitdata_req.dl_dest_addr_offset,
		addr, addrlen);
	
	ctl.maxlen = 0;
	ctl.len = dlp -> unitdata_req.dl_dest_addr_offset + addrlen;
	ctl.buf = (char *)buf;

	data.maxlen = 0;
	data.buf = (char *)dbuf;
	data.len = dbuflen;

	/* Send the packet down the wire... */
	return putmsg (fd, &ctl, &data, 0);
}

static int dlpiunitdataind (fd, daddr, daddrlen,
			    saddr, saddrlen, grpaddr, dbuf, dlen)
	int fd;
	unsigned char *daddr;
	unsigned long *daddrlen;
	unsigned char *saddr;
	unsigned long *saddrlen;
	unsigned long *grpaddr;
	unsigned char *dbuf;
	int dlen;
{
	long buf [DLPI_MAXDLBUF];
	union DL_primitives *dlp;
	struct strbuf ctl, data;
	int flags = 0;
1161
	int result;
1162 1163 1164 1165 1166 1167 1168 1169

	/* Set up the msg_buf structure... */
	dlp = (union DL_primitives *)buf;
	dlp -> unitdata_ind.dl_primitive = DL_UNITDATA_IND;

	ctl.maxlen = DLPI_MAXDLBUF;
	ctl.len = 0;
	ctl.buf = (char *)buf;
1170

1171 1172 1173
	data.maxlen = dlen;
	data.len = 0;
	data.buf = (char *)dbuf;
1174

1175
	result = getmsg (fd, &ctl, &data, &flags);
1176

1177
	if (result < 0) {
1178
		log_debug("dlpiunitdataind: %m");
1179 1180
		return -1;
	}
1181

1182 1183 1184 1185
	if (ctl.len < sizeof (dl_unitdata_ind_t) ||
	    dlp -> unitdata_ind.dl_primitive != DL_UNITDATA_IND) {
		return -1;
	}
1186

1187 1188 1189 1190 1191 1192
	if (data.len <= 0) {
		return data.len;
	}

	/* Copy sender info */
	if (saddr) {
1193 1194
		memcpy (saddr,
			(char *)buf + dlp -> unitdata_ind.dl_src_addr_offset,
1195 1196 1197 1198 1199 1200 1201 1202
			dlp -> unitdata_ind.dl_src_addr_length);
	}
	if (saddrlen) {
		*saddrlen = dlp -> unitdata_ind.dl_src_addr_length;
	}

	/* Copy destination info */
	if (daddr) {
1203 1204
		memcpy (daddr,
			(char *)buf + dlp -> unitdata_ind.dl_dest_addr_offset,
1205 1206 1207 1208 1209
			dlp -> unitdata_ind.dl_dest_addr_length);
	}
	if (daddrlen) {
		*daddrlen = dlp -> unitdata_ind.dl_dest_addr_length;
	}
1210

1211 1212 1213
	if (grpaddr) {
		*grpaddr = dlp -> unitdata_ind.dl_group_address;
	}
1214

1215 1216
	return data.len;
}
1217
#endif /* !USE_DLPI_HWADDR: USE_DLPI_RECEIVE || USE_DLPI_SEND */
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

/*
 * expected - see if we got what we wanted.
 */
static int expected (prim, dlp, msgflags)
	unsigned long prim;
	union DL_primitives *dlp;
	int msgflags;
{
	if (msgflags != RS_HIPRI) {
		/* Message was not M_PCPROTO */
Shawn Routhier's avatar
Shawn Routhier committed
1229
		return -1;
1230 1231
	}

Shawn Routhier's avatar
Shawn Routhier committed
1232
	if (dlp->dl_primitive != prim) {
1233
		/* Incorrect/unexpected return message */
Shawn Routhier's avatar
Shawn Routhier committed
1234
		return -1;
1235 1236
	}
	
Shawn Routhier's avatar
Shawn Routhier committed
1237
	return 0;
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
}

/*
 * strgetmsg - get a message from a stream, with timeout.
 */
static int strgetmsg (fd, ctlp, datap, flagsp, caller)
	struct strbuf *ctlp, *datap;
	char *caller;
	int *flagsp;
	int fd;
{
1249
	int result;
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
	struct pollfd pfd;
	int count;
	time_t now;
	time_t starttime;
	int to_msec;
	
	pfd.fd = fd;
	pfd.events = POLLPRI;	/* We're only interested in knowing
				 * when we can receive the next high
				 * priority message.
				 */
	pfd.revents = 0;

	now = time (&starttime);
	while (now <= starttime + DLPI_MAXWAIT) {
		to_msec = ((starttime + DLPI_MAXWAIT) - now) * 1000;
		count = poll (&pfd, 1, to_msec);
		
		if (count == 0) {
1269
			/* log_fatal ("strgetmsg: timeout"); */
1270 1271 1272 1273 1274 1275
			return -1;
		} else if (count < 0) {
			if (errno == EAGAIN || errno == EINTR) {
				time (&now);
				continue;
			} else {
1276
				/* log_fatal ("poll: %m"); */
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
				return -1;
			}
		} else {
			break;
		}
	}

	/*
	 * Set flags argument and issue getmsg ().
	 */
	*flagsp = 0;
1288 1289
	if ((result = getmsg (fd, ctlp, datap, flagsp)) < 0) {
		return result;
1290 1291 1292 1293 1294
	}

	/*
	 * Check for MOREDATA and/or MORECTL.
	 */
1295
	if (result & (MORECTL|MOREDATA)) {
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
		return -1;
	}

	/*
	 * Check for at least sizeof (long) control data portion.
	 */
	if (ctlp -> len < sizeof (long)) {
		return -1;
	}

	return 0;
}

1309
#if defined(USE_DLPI_SEND)
Ted Lemon's avatar
Ted Lemon committed
1310 1311
int can_unicast_without_arp (ip)
	struct interface_info *ip;
Ted Lemon's avatar
Ted Lemon committed
1312 1313 1314 1315
{
	return 1;
}

Ted Lemon's avatar
Ted Lemon committed
1316 1317
int can_receive_unicast_unconfigured (ip)
	struct interface_info *ip;
1318 1319 1320 1321
{
	return 1;
}

1322 1323 1324 1325 1326 1327
int supports_multiple_interfaces (ip)
	struct interface_info *ip;
{
	return 1;
}

Ted Lemon's avatar
Ted Lemon committed
1328 1329
void maybe_setup_fallback ()
{
Ted Lemon's avatar
Ted Lemon committed
1330
	isc_result_t status;
1331 1332
	struct interface_info *fbi = (struct interface_info *)0;
	if (setup_fallback (&fbi, MDL)) {