]> git.ipfire.org Git - thirdparty/dhcp.git/blob - common/dlpi.c
Make interfaces OMAPI objects.
[thirdparty/dhcp.git] / common / dlpi.c
1 /* dlpi.c
2
3 Data Link Provider Interface (DLPI) network interface code. */
4
5 /*
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:
12 *
13 * http://www.isc.org/isc-license-1.0.html.
14 *
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.
18 *
19 * Support and other services are available for ISC products - see
20 * http://www.isc.org for more information.
21 *
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''.
25 */
26
27 /*
28 * Based largely in part to the existing NIT code in nit.c.
29 *
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
32 * generic, though.
33 */
34
35 /*
36 * Implementation notes:
37 *
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).
43 *
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
47 * USE_DLPI_RAW.
48 *
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.
54 *
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
68 * to sleep.
69 */
70
71 #ifndef lint
72 static char copyright[] =
73 "$Id: dlpi.c,v 1.13 1999/09/08 01:43:39 mellon Exp $ Copyright (c) 1998, 1999 The Internet Software Consortium. All rights reserved.\n";
74 #endif /* not lint */
75
76 #include "dhcpd.h"
77
78 #if defined (USE_DLPI_SEND) || defined (USE_DLPI_RECEIVE)
79
80 # include <sys/ioctl.h>
81 # include <sys/time.h>
82 # include <sys/dlpi.h>
83 # include <stropts.h>
84 # ifdef USE_DLPI_PFMOD
85 # include <sys/pfmod.h>
86 # endif
87 # ifdef USE_POLL
88 # include <poll.h>
89 # endif
90
91 # include <netinet/in_systm.h>
92 # include "includes/netinet/ip.h"
93 # include "includes/netinet/udp.h"
94 # include "includes/netinet/if_ether.h"
95
96 # ifdef USE_DLPI_PFMOD
97 # ifdef USE_DLPI_RAW
98 # define DLPI_MODNAME "DLPI+RAW+PFMOD"
99 # else
100 # define DLPI_MODNAME "DLPI+PFMOD"
101 # endif
102 # else
103 # ifdef USE_DLPI_RAW
104 # define DLPI_MODNAME "DLPI+RAW"
105 # else
106 # define DLPI_MODNAME "DLPI"
107 # endif
108 # endif
109
110 # ifndef ABS
111 # define ABS(x) ((x) >= 0 ? (x) : 0-(x))
112 # endif
113
114 static int strioctl PROTO ((int fd, int cmd, int timeout, int len, char *dp));
115
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 */
120
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,
127 int saplen));
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,
145 int datalen));
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,
152 unsigned char *data,
153 int datalen));
154
155 # ifndef USE_POLL
156 static void sigalrm PROTO ((int sig));
157 # endif
158 static int expected PROTO ((unsigned long prim, union DL_primitives *dlp,
159 int msgflags));
160 static int strgetmsg PROTO ((int fd, struct strbuf *ctlp,
161 struct strbuf *datap, int *flagsp,
162 char *caller));
163
164 /* Reinitializes the specified interface after an address change. This
165 is not required for packet-filter APIs. */
166
167 #ifdef USE_DLPI_SEND
168 void if_reinitialize_send (info)
169 struct interface_info *info;
170 {
171 }
172 #endif
173
174 #ifdef USE_DLPI_RECEIVE
175 void if_reinitialize_receive (info)
176 struct interface_info *info;
177 {
178 }
179 #endif
180
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
183 mask. */
184
185 int if_register_dlpi (info)
186 struct interface_info *info;
187 {
188 int sock;
189 int unit;
190 long buf [DLPI_MAXDLBUF];
191 union DL_primitives *dlp;
192
193 dlp = (union DL_primitives *)buf;
194
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);
198 }
199
200 /*
201 * Get information about the provider.
202 */
203
204 /*
205 * Submit a DL_INFO_REQ request, to find
206 * the dl_mac_type and dl_provider_style
207 */
208 if (dlpiinforeq(sock) < 0 || dlpiinfoack(sock, (char *)buf) < 0) {
209 log_fatal ("Can't get DLPI MAC type for %s: %m", info -> name);
210 } else {
211 switch (dlp -> info_ack.dl_mac_type) {
212 case DL_CSMACD: /* IEEE 802.3 */
213 case DL_ETHER:
214 info -> hw_address.htype = HTYPE_ETHER;
215 break;
216 /* adding token ring 5/1999 - mayer@ping.at */
217 case DL_TPR:
218 info -> hw_address.htype = HTYPE_IEEE802;
219 break;
220 case DL_FDDI:
221 info -> hw_address.htype = HTYPE_FDDI;
222 break;
223 default:
224 log_fatal ("%s: unknown DLPI MAC type %d",
225 info -> name,
226 dlp -> info_ack.dl_mac_type);
227 break;
228 }
229 }
230
231 if (dlp -> info_ack.dl_provider_style == DL_STYLE2) {
232 /*
233 * Attach to the device. If this fails, the device
234 * does not exist.
235 */
236 unit = dlpiunit (info -> name);
237
238 if (dlpiattachreq (sock, unit) < 0
239 || dlpiokack (sock, (char *)buf) < 0) {
240 log_fatal ("Can't attach DLPI device for %s: %m", info -> name);
241 }
242 }
243
244 /*
245 * Bind to the IP service access point (SAP), connectionless (CLDLS).
246 */
247 if (dlpibindreq (sock, DLPI_DEFAULTSAP, 0, DL_CLDLS, 0, 0) < 0
248 || dlpibindack (sock, (char *)buf) < 0) {
249 log_fatal ("Can't bind DLPI device for %s: %m", info -> name);
250 }
251
252 /*
253 * Submit a DL_PHYS_ADDR_REQ request, to find
254 * the hardware address
255 */
256 if (dlpiphysaddrreq (sock, DL_CURR_PHYS_ADDR) < 0
257 || dlpiphysaddrack (sock, (char *)buf) < 0) {
258 log_fatal ("Can't get DLPI hardware address for %s: %m",
259 info -> name);
260 }
261
262 info -> hw_address.hlen = dlp -> physaddr_ack.dl_addr_length;
263 memcpy (info -> hw_address.haddr,
264 (char *)buf + dlp -> physaddr_ack.dl_addr_offset,
265 dlp -> physaddr_ack.dl_addr_length);
266
267 #ifdef USE_DLPI_RAW
268 if (strioctl (sock, DLIOCRAW, INFTIM, 0, 0) < 0) {
269 log_fatal ("Can't set DLPI RAW mode for %s: %m",
270 info -> name);
271 }
272 #endif
273
274 #ifdef USE_DLPI_PFMOD
275 if (ioctl (sock, I_PUSH, "pfmod") < 0) {
276 log_fatal ("Can't push packet filter onto DLPI for %s: %m",
277 info -> name);
278 }
279 #endif
280
281 return sock;
282 }
283
284 static int
285 strioctl (fd, cmd, timeout, len, dp)
286 int fd;
287 int cmd;
288 int timeout;
289 int len;
290 char *dp;
291 {
292 struct strioctl sio;
293 int rslt;
294
295 sio.ic_cmd = cmd;
296 sio.ic_timout = timeout;
297 sio.ic_len = len;
298 sio.ic_dp = dp;
299
300 if ((rslt = ioctl (fd, I_STR, &sio)) < 0) {
301 return rslt;
302 } else {
303 return sio.ic_len;
304 }
305 }
306
307 #ifdef USE_DLPI_SEND
308 void if_register_send (info)
309 struct interface_info *info;
310 {
311 /* If we're using the DLPI API for sending and receiving,
312 we don't need to register this interface twice. */
313 #ifndef USE_DLPI_RECEIVE
314 # ifdef USE_DLPI_PFMOD
315 struct packetfilt pf;
316 # endif
317
318 info -> wfdesc = if_register_dlpi (info);
319
320 # ifdef USE_DLPI_PFMOD
321 /* Set up an PFMOD filter that rejects everything... */
322 pf.Pf_Priority = 0;
323 pf.Pf_FilterLen = 1;
324 pf.Pf_Filter [0] = ENF_PUSHZERO;
325
326 /* Install the filter */
327 if (strioctl (info -> wfdesc, PFIOCSETF, INFTIM,
328 sizeof (pf), (char *)&pf) < 0) {
329 log_fatal ("Can't set PFMOD send filter on %s: %m", info -> name);
330 }
331
332 # endif /* USE_DLPI_PFMOD */
333 #else /* !defined (USE_DLPI_RECEIVE) */
334 /*
335 * If using DLPI for both send and receive, simply re-use
336 * the read file descriptor that was set up earlier.
337 */
338 info -> wfdesc = info -> rfdesc;
339 #endif
340
341 if (!quiet_interface_discovery)
342 log_info ("Sending on DLPI/%s/%s%s%s",
343 info -> name,
344 print_hw_addr (info -> hw_address.htype,
345 info -> hw_address.hlen,
346 info -> hw_address.haddr),
347 (info -> shared_network ? "/" : ""),
348 (info -> shared_network ?
349 info -> shared_network -> name : ""));
350
351 #ifdef DLPI_FIRST_SEND_WAIT
352 /* See the implementation notes at the beginning of this file */
353 # ifdef USE_DLPI_RECEIVE
354 sleep (DLPI_FIRST_SEND_WAIT - (DLPI_FIRST_SEND_WAIT / 2));
355 # else
356 sleep (DLPI_FIRST_SEND_WAIT);
357 # endif
358 #endif
359 }
360 #endif /* USE_DLPI_SEND */
361
362 #ifdef USE_DLPI_RECEIVE
363 /* Packet filter program...
364 XXX Changes to the filter program may require changes to the constant
365 offsets used in if_register_send to patch the NIT program! XXX */
366
367 void if_register_receive (info)
368 struct interface_info *info;
369 {
370 #ifdef USE_DLPI_PFMOD
371 struct packetfilt pf;
372 #endif
373
374 /* Open a DLPI device and hang it on this interface... */
375 info -> rfdesc = if_register_dlpi (info);
376
377 #ifdef USE_DLPI_PFMOD
378 /* Set up the PFMOD filter program. */
379 /* XXX Unlike the BPF filter program, this one won't work if the
380 XXX IP packet is fragmented or if there are options on the IP
381 XXX header. */
382 pf.Pf_Priority = 0;
383 pf.Pf_FilterLen = 0;
384
385 #ifdef USE_DLPI_RAW
386 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHWORD + 6;
387 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT + ENF_CAND;
388 pf.Pf_Filter [pf.Pf_FilterLen++] = htons (ETHERTYPE_IP);
389 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT;
390 pf.Pf_Filter [pf.Pf_FilterLen++] = htons (IPPROTO_UDP);
391 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHWORD + 11;
392 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSH00FF + ENF_AND;
393 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_CAND;
394 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHWORD + 18;
395 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT + ENF_CAND;
396 pf.Pf_Filter [pf.Pf_FilterLen++] = htons (local_port);
397 #else
398 /*
399 * The packets that will be received on this file descriptor
400 * will be IP packets (due to the SAP that was specified in
401 * the dlbind call). There will be no ethernet header.
402 * Therefore, setup the packet filter to check the protocol
403 * field for UDP, and the destination port number equal
404 * to the local port. All offsets are relative to the start
405 * of an IP packet.
406 */
407 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT;
408 pf.Pf_Filter [pf.Pf_FilterLen++] = htons (IPPROTO_UDP);
409 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHWORD + 4;
410 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSH00FF + ENF_AND;
411 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_CAND;
412 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHWORD + 11;
413 pf.Pf_Filter [pf.Pf_FilterLen++] = ENF_PUSHLIT + ENF_CAND;
414 pf.Pf_Filter [pf.Pf_FilterLen++] = htons (local_port);
415 #endif
416
417 /* Install the filter... */
418 if (strioctl (info -> rfdesc, PFIOCSETF, INFTIM,
419 sizeof (pf), (char *)&pf) < 0) {
420 log_fatal ("Can't set PFMOD receive filter on %s: %m", info -> name);
421 }
422 #endif
423
424 if (!quiet_interface_discovery)
425 log_info ("Listening on DLPI/%s/%s%s%s",
426 info -> name,
427 print_hw_addr (info -> hw_address.htype,
428 info -> hw_address.hlen,
429 info -> hw_address.haddr),
430 (info -> shared_network ? "/" : ""),
431 (info -> shared_network ?
432 info -> shared_network -> name : ""));
433
434 #ifdef DLPI_FIRST_SEND_WAIT
435 /* See the implementation notes at the beginning of this file */
436 # ifdef USE_DLPI_SEND
437 sleep (DLPI_FIRST_SEND_WAIT / 2);
438 # else
439 sleep (DLPI_FIRST_SEND_WAIT);
440 # endif
441 #endif
442 }
443 #endif /* USE_DLPI_RECEIVE */
444
445 #ifdef USE_DLPI_SEND
446 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
447 struct interface_info *interface;
448 struct packet *packet;
449 struct dhcp_packet *raw;
450 size_t len;
451 struct in_addr from;
452 struct sockaddr_in *to;
453 struct hardware *hto;
454 {
455 int dbuflen;
456 unsigned char dbuf [1536];
457 unsigned char sap [2];
458 unsigned char dstaddr [DLPI_MAXDLADDR];
459 unsigned addrlen;
460 int saplen;
461 int result;
462
463 if (!strcmp (interface -> name, "fallback"))
464 return send_fallback (interface, packet, raw,
465 len, from, to, hto);
466
467 dbuflen = 0;
468
469 /* Assemble the headers... */
470 #ifdef USE_DLPI_RAW
471 assemble_hw_header (interface, dbuf, &dbuflen, hto);
472 #endif
473 assemble_udp_ip_header (interface, dbuf, &dbuflen, from.s_addr,
474 to -> sin_addr.s_addr, to -> sin_port,
475 (unsigned char *)raw, len);
476
477 /* Copy the data into the buffer (yuk). */
478 memcpy (dbuf + dbuflen, raw, len);
479 dbuflen += len;
480
481 #ifdef USE_DLPI_RAW
482 result = write (interface -> wfdesc, dbuf, dbuflen);
483 #else
484 /* XXX: Assumes ethernet, with two byte SAP */
485 sap [0] = 0x08; /* ETHERTYPE_IP, high byte */
486 sap [1] = 0x0; /* ETHERTYPE_IP, low byte */
487 saplen = -2; /* -2 indicates a two byte SAP at the end
488 of the address */
489
490 /* Setup the destination address */
491 if (hto && hto -> hlen == interface -> hw_address.hlen) {
492 dlpi_makeaddr (hto -> haddr, hto -> hlen, sap, saplen, dstaddr);
493 } else {
494 /* XXX: Assumes broadcast addr is all ones */
495 /* Really should get the broadcast address as part of the
496 * dlpiinforeq, and store it somewhere in the interface structure.
497 */
498 unsigned char bcast_ether [DLPI_MAXDLADDR];
499
500 memset ((char *)bcast_ether, 0xFF, interface -> hw_address.hlen);
501 dlpi_makeaddr (bcast_ether, interface -> hw_address.hlen,
502 sap, saplen, dstaddr);
503 }
504 addrlen = interface -> hw_address.hlen + ABS (saplen);
505
506 /* Send the packet down the wire... */
507 result = dlpiunitdatareq (interface -> wfdesc, dstaddr, addrlen,
508 0, 0, dbuf, dbuflen);
509 #endif
510 if (result < 0)
511 log_error ("send_packet: %m");
512 return result;
513 }
514 #endif /* USE_DLPI_SEND */
515
516 #ifdef USE_DLPI_RECEIVE
517 ssize_t receive_packet (interface, buf, len, from, hfrom)
518 struct interface_info *interface;
519 unsigned char *buf;
520 size_t len;
521 struct sockaddr_in *from;
522 struct hardware *hfrom;
523 {
524 unsigned char dbuf [1536];
525 unsigned char sap [2];
526 unsigned char srcaddr [DLPI_MAXDLADDR];
527 unsigned long srcaddrlen;
528 int saplen;
529 int flags = 0;
530 int length = 0;
531 int offset = 0;
532 int rslt;
533 int bufix = 0;
534
535 #ifdef USE_DLPI_RAW
536 length = read (interface -> rfdesc, dbuf, sizeof (dbuf));
537 #else
538 length = dlpiunitdataind (interface -> rfdesc, (unsigned char *)NULL,
539 (unsigned long *)NULL, srcaddr, &srcaddrlen,
540 (unsigned long *)NULL, dbuf, sizeof (dbuf));
541 #endif
542
543 if (length <= 0) {
544 return length;
545 }
546
547 #ifndef USE_DLPI_RAW
548 /* Copy sender info */
549 /* XXX: Assumes ethernet, where SAP comes at end of haddr */
550 saplen = -2;
551 if (hfrom && srcaddrlen == ABS(saplen) + interface -> hw_address.hlen) {
552 hfrom -> htype = interface -> hw_address.htype;
553 hfrom -> hlen = interface -> hw_address.hlen;
554 dlpi_parseaddr (srcaddr, hfrom -> haddr,
555 interface -> hw_address.hlen, sap, saplen);
556 } else if (hfrom) {
557 memset ((char *)hfrom, '\0', sizeof (*hfrom));
558 }
559 #endif
560
561 /* Decode the IP and UDP headers... */
562 bufix = 0;
563 #ifdef USE_DLPI_RAW
564 /* Decode the physical header... */
565 offset = decode_hw_header (interface, dbuf, bufix, hfrom);
566
567 /* If a physical layer checksum failed (dunno of any
568 physical layer that supports this, but WTH), skip this
569 packet. */
570 if (offset < 0) {
571 return 0;
572 }
573 bufix += offset;
574 length -= offset;
575 #endif
576 offset = decode_udp_ip_header (interface, dbuf, bufix,
577 from, (unsigned char *)0, length);
578
579 /* If the IP or UDP checksum was bad, skip the packet... */
580 if (offset < 0) {
581 return 0;
582 }
583
584 bufix += offset;
585 length -= offset;
586
587 /* Copy out the data in the packet... */
588 memcpy (buf, &dbuf [bufix], length);
589 return length;
590 }
591 #endif
592
593 /* Common DLPI routines ...
594 *
595 * Written by Eric James Negaard, <lmdejn@lmd.ericsson.se>
596 *
597 * Based largely in part to the example code contained in the document
598 * "How to Use the STREAMS Data Link Provider Interface (DLPI)", written
599 * by Neal Nuckolls of SunSoft Internet Engineering.
600 *
601 * This code has been developed and tested on sparc-based machines running
602 * SunOS 5.5.1, with le and hme network interfaces. It should be pretty
603 * generic, though.
604 *
605 * The usual disclaimers apply. This code works for me. Don't blame me
606 * if it makes your machine or network go down in flames. That taken
607 * into consideration, use this code as you wish. If you make usefull
608 * modifications I'd appreciate hearing about it.
609 */
610
611 #define DLPI_MAXWAIT 15 /* Max timeout */
612
613 static void dlpi_makeaddr (physaddr, physaddrlen, sap, saplen, buf)
614 unsigned char *physaddr;
615 int physaddrlen;
616 unsigned char *sap;
617 int saplen;
618 unsigned char *buf;
619 {
620 /*
621 * If the saplen is negative, the SAP goes at the end of the address,
622 * otherwise it goes at the beginning.
623 */
624 if (saplen >= 0) {
625 memcpy ((char *)buf, (char *)sap, saplen);
626 memcpy ((char *)&buf [saplen], (char *)physaddr, physaddrlen);
627 } else {
628 memcpy ((char *)buf, (char *)physaddr, physaddrlen);
629 memcpy ((char *)&buf [physaddrlen], (char *)sap, 0 - saplen);
630 }
631 }
632
633 static void dlpi_parseaddr (buf, physaddr, physaddrlen, sap, saplen)
634 unsigned char *buf;
635 unsigned char *physaddr;
636 int physaddrlen;
637 unsigned char *sap;
638 int saplen;
639 {
640 /*
641 * If the saplen is negative, the SAP is at the end of the address,
642 * otherwise it is at the beginning.
643 */
644 if (saplen >= 0) {
645 memcpy ((char *)sap, (char *)buf, saplen);
646 memcpy ((char *)physaddr, (char *)&buf [saplen], physaddrlen);
647 } else {
648 memcpy ((char *)physaddr, (char *)buf, physaddrlen);
649 memcpy ((char *)sap, (char *)&buf [physaddrlen], 0 - saplen);
650 }
651 }
652
653 /*
654 * Parse an interface name and extract the unit number
655 */
656
657 static int dlpiunit (ifname)
658 char *ifname;
659 {
660 int fd;
661 char *cp, *dp, *ep;
662 int unit;
663
664 if (!ifname) {
665 return 0;
666 }
667
668 /* Advance to the end of the name */
669 cp = ifname;
670 while (*cp) cp++;
671 /* Back up to the start of the first digit */
672 while ((*(cp-1) >= '0' && *(cp-1) <= '9') || *(cp - 1) == ':') cp--;
673
674 /* Convert the unit number */
675 unit = 0;
676 while (*cp >= '0' && *cp <= '9') {
677 unit *= 10;
678 unit += (*cp++ - '0');
679 }
680
681 return unit;
682 }
683
684 /*
685 * dlpiopen - open the DLPI device for a given interface name
686 */
687 static int dlpiopen (ifname)
688 char *ifname;
689 {
690 char devname [50];
691 char *cp, *dp, *ep;
692
693 if (!ifname) {
694 return -1;
695 }
696
697 /* Open a DLPI device */
698 if (*ifname == '/') {
699 dp = devname;
700 } else {
701 /* Prepend the device directory */
702 memcpy (devname, DLPI_DEVDIR, strlen (DLPI_DEVDIR));
703 dp = &devname [strlen (DLPI_DEVDIR)];
704 }
705
706 /* Find the end of the interface name */
707 ep = cp = ifname;
708 while (*ep)
709 ep++;
710 /* And back up to the first digit (unit number) */
711 while ((*(ep - 1) >= '0' && *(ep - 1) <= '9') || *(ep - 1) == ':')
712 ep--;
713
714 /* Copy everything up to the unit number */
715 while (cp < ep) {
716 *dp++ = *cp++;
717 }
718 *dp = '\0';
719
720 return open (devname, O_RDWR, 0);
721 }
722
723 /*
724 * dlpiinforeq - request information about the data link provider.
725 */
726
727 static int dlpiinforeq (fd)
728 int fd;
729 {
730 dl_info_req_t info_req;
731 struct strbuf ctl;
732 int flags;
733
734 info_req.dl_primitive = DL_INFO_REQ;
735
736 ctl.maxlen = 0;
737 ctl.len = sizeof (info_req);
738 ctl.buf = (char *)&info_req;
739
740 flags = RS_HIPRI;
741
742 return putmsg (fd, &ctl, (struct strbuf *)NULL, flags);
743 }
744
745 /*
746 * dlpiphysaddrreq - request the current physical address.
747 */
748 static int dlpiphysaddrreq (fd, addrtype)
749 int fd;
750 unsigned long addrtype;
751 {
752 dl_phys_addr_req_t physaddr_req;
753 struct strbuf ctl;
754 int flags;
755
756 physaddr_req.dl_primitive = DL_PHYS_ADDR_REQ;
757 physaddr_req.dl_addr_type = addrtype;
758
759 ctl.maxlen = 0;
760 ctl.len = sizeof (physaddr_req);
761 ctl.buf = (char *)&physaddr_req;
762
763 flags = RS_HIPRI;
764
765 return putmsg (fd, &ctl, (struct strbuf *)NULL, flags);
766 }
767
768 /*
769 * dlpiattachreq - send a request to attach to a specific unit.
770 */
771 static int dlpiattachreq (fd, ppa)
772 unsigned long ppa;
773 int fd;
774 {
775 dl_attach_req_t attach_req;
776 struct strbuf ctl;
777 int flags;
778
779 attach_req.dl_primitive = DL_ATTACH_REQ;
780 attach_req.dl_ppa = ppa;
781
782 ctl.maxlen = 0;
783 ctl.len = sizeof (attach_req);
784 ctl.buf = (char *)&attach_req;
785
786 flags = 0;
787
788 return putmsg (fd, &ctl, (struct strbuf*)NULL, flags);
789 }
790
791 /*
792 * dlpibindreq - send a request to bind to a specific SAP address.
793 */
794 static int dlpibindreq (fd, sap, max_conind, service_mode, conn_mgmt, xidtest)
795 unsigned long sap;
796 unsigned long max_conind;
797 unsigned long service_mode;
798 unsigned long conn_mgmt;
799 unsigned long xidtest;
800 int fd;
801 {
802 dl_bind_req_t bind_req;
803 struct strbuf ctl;
804 int flags;
805
806 bind_req.dl_primitive = DL_BIND_REQ;
807 bind_req.dl_sap = sap;
808 bind_req.dl_max_conind = max_conind;
809 bind_req.dl_service_mode = service_mode;
810 bind_req.dl_conn_mgmt = conn_mgmt;
811 bind_req.dl_xidtest_flg = xidtest;
812
813 ctl.maxlen = 0;
814 ctl.len = sizeof (bind_req);
815 ctl.buf = (char *)&bind_req;
816
817 flags = 0;
818
819 return putmsg (fd, &ctl, (struct strbuf*)NULL, flags);
820 }
821
822 /*
823 * dlpiunbindreq - send a request to unbind.
824 */
825 static int dlpiunbindreq (fd)
826 int fd;
827 {
828 dl_unbind_req_t unbind_req;
829 struct strbuf ctl;
830 int flags;
831
832 unbind_req.dl_primitive = DL_UNBIND_REQ;
833
834 ctl.maxlen = 0;
835 ctl.len = sizeof (unbind_req);
836 ctl.buf = (char *)&unbind_req;
837
838 flags = 0;
839
840 return putmsg (fd, &ctl, (struct strbuf*)NULL, flags);
841 }
842
843
844 /*
845 * dlpidetachreq - send a request to detach.
846 */
847 static int dlpidetachreq (fd)
848 int fd;
849 {
850 dl_detach_req_t detach_req;
851 struct strbuf ctl;
852 int flags;
853
854 detach_req.dl_primitive = DL_DETACH_REQ;
855
856 ctl.maxlen = 0;
857 ctl.len = sizeof (detach_req);
858 ctl.buf = (char *)&detach_req;
859
860 flags = 0;
861
862 return putmsg (fd, &ctl, (struct strbuf*)NULL, flags);
863 }
864
865
866 /*
867 * dlpibindack - receive an ack to a dlbindreq.
868 */
869 static int dlpibindack (fd, bufp)
870 char *bufp;
871 int fd;
872 {
873 union DL_primitives *dlp;
874 struct strbuf ctl;
875 int flags;
876
877 ctl.maxlen = DLPI_MAXDLBUF;
878 ctl.len = 0;
879 ctl.buf = bufp;
880
881 if (strgetmsg (fd, &ctl,
882 (struct strbuf*)NULL, &flags, "dlpibindack") < 0) {
883 return -1;
884 }
885
886 dlp = (union DL_primitives *)ctl.buf;
887
888 if (!expected (DL_BIND_ACK, dlp, flags) < 0) {
889 return -1;
890 }
891
892 if (ctl.len < sizeof (dl_bind_ack_t)) {
893 /* Returned structure is too short */
894 return -1;
895 }
896
897 return 0;
898 }
899
900 /*
901 * dlpiokack - general acknowledgement reception.
902 */
903 static int dlpiokack (fd, bufp)
904 char *bufp;
905 int fd;
906 {
907 union DL_primitives *dlp;
908 struct strbuf ctl;
909 int flags;
910
911 ctl.maxlen = DLPI_MAXDLBUF;
912 ctl.len = 0;
913 ctl.buf = bufp;
914
915 if (strgetmsg (fd, &ctl,
916 (struct strbuf*)NULL, &flags, "dlpiokack") < 0) {
917 return -1;
918 }
919
920 dlp = (union DL_primitives *)ctl.buf;
921
922 if (!expected (DL_OK_ACK, dlp, flags) < 0) {
923 return -1;
924 }
925
926 if (ctl.len < sizeof (dl_ok_ack_t)) {
927 /* Returned structure is too short */
928 return -1;
929 }
930
931 return 0;
932 }
933
934 /*
935 * dlpiinfoack - receive an ack to a dlinforeq.
936 */
937 static int dlpiinfoack (fd, bufp)
938 char *bufp;
939 int fd;
940 {
941 union DL_primitives *dlp;
942 struct strbuf ctl;
943 int flags;
944
945 ctl.maxlen = DLPI_MAXDLBUF;
946 ctl.len = 0;
947 ctl.buf = bufp;
948
949 if (strgetmsg (fd, &ctl, (struct strbuf *)NULL, &flags,
950 "dlpiinfoack") < 0) {
951 return -1;
952 }
953
954 dlp = (union DL_primitives *) ctl.buf;
955
956 if (!expected (DL_INFO_ACK, dlp, flags) < 0) {
957 return -1;
958 }
959
960 if (ctl.len < sizeof (dl_info_ack_t)) {
961 /* Returned structure is too short */
962 return -1;
963 }
964
965 return 0;
966 }
967
968 /*
969 * dlpiphysaddrack - receive an ack to a dlpiphysaddrreq.
970 */
971 int dlpiphysaddrack (fd, bufp)
972 char *bufp;
973 int fd;
974 {
975 union DL_primitives *dlp;
976 struct strbuf ctl;
977 int flags;
978
979 ctl.maxlen = DLPI_MAXDLBUF;
980 ctl.len = 0;
981 ctl.buf = bufp;
982
983 if (strgetmsg (fd, &ctl, (struct strbuf *)NULL, &flags,
984 "dlpiphysaddrack") < 0) {
985 return -1;
986 }
987
988 dlp = (union DL_primitives *)ctl.buf;
989
990 if (!expected (DL_PHYS_ADDR_ACK, dlp, flags) < 0) {
991 return -1;
992 }
993
994 if (ctl.len < sizeof (dl_phys_addr_ack_t)) {
995 /* Returned structure is too short */
996 return -1;
997 }
998
999 return 0;
1000 }
1001
1002 int dlpiunitdatareq (fd, addr, addrlen, minpri, maxpri, dbuf, dbuflen)
1003 int fd;
1004 unsigned char *addr;
1005 int addrlen;
1006 unsigned long minpri;
1007 unsigned long maxpri;
1008 unsigned char *dbuf;
1009 int dbuflen;
1010 {
1011 long buf [DLPI_MAXDLBUF];
1012 union DL_primitives *dlp;
1013 struct strbuf ctl, data;
1014
1015 /* Set up the control information... */
1016 dlp = (union DL_primitives *)buf;
1017 dlp -> unitdata_req.dl_primitive = DL_UNITDATA_REQ;
1018 dlp -> unitdata_req.dl_dest_addr_length = addrlen;
1019 dlp -> unitdata_req.dl_dest_addr_offset = sizeof (dl_unitdata_req_t);
1020 dlp -> unitdata_req.dl_priority.dl_min = minpri;
1021 dlp -> unitdata_req.dl_priority.dl_max = maxpri;
1022
1023 /* Append the destination address */
1024 memcpy ((char *)buf + dlp -> unitdata_req.dl_dest_addr_offset,
1025 addr, addrlen);
1026
1027 ctl.maxlen = 0;
1028 ctl.len = dlp -> unitdata_req.dl_dest_addr_offset + addrlen;
1029 ctl.buf = (char *)buf;
1030
1031 data.maxlen = 0;
1032 data.buf = (char *)dbuf;
1033 data.len = dbuflen;
1034
1035 /* Send the packet down the wire... */
1036 return putmsg (fd, &ctl, &data, 0);
1037 }
1038
1039 static int dlpiunitdataind (fd, daddr, daddrlen,
1040 saddr, saddrlen, grpaddr, dbuf, dlen)
1041 int fd;
1042 unsigned char *daddr;
1043 unsigned long *daddrlen;
1044 unsigned char *saddr;
1045 unsigned long *saddrlen;
1046 unsigned long *grpaddr;
1047 unsigned char *dbuf;
1048 int dlen;
1049 {
1050 long buf [DLPI_MAXDLBUF];
1051 union DL_primitives *dlp;
1052 struct strbuf ctl, data;
1053 int flags = 0;
1054 int result;
1055
1056 /* Set up the msg_buf structure... */
1057 dlp = (union DL_primitives *)buf;
1058 dlp -> unitdata_ind.dl_primitive = DL_UNITDATA_IND;
1059
1060 ctl.maxlen = DLPI_MAXDLBUF;
1061 ctl.len = 0;
1062 ctl.buf = (char *)buf;
1063
1064 data.maxlen = dlen;
1065 data.len = 0;
1066 data.buf = (char *)dbuf;
1067
1068 result = getmsg (fd, &ctl, &data, &flags);
1069
1070 if (result != 0) {
1071 return -1;
1072 }
1073
1074 if (ctl.len < sizeof (dl_unitdata_ind_t) ||
1075 dlp -> unitdata_ind.dl_primitive != DL_UNITDATA_IND) {
1076 return -1;
1077 }
1078
1079 if (data.len <= 0) {
1080 return data.len;
1081 }
1082
1083 /* Copy sender info */
1084 if (saddr) {
1085 memcpy (saddr, &buf [dlp -> unitdata_ind.dl_src_addr_offset],
1086 dlp -> unitdata_ind.dl_src_addr_length);
1087 }
1088 if (saddrlen) {
1089 *saddrlen = dlp -> unitdata_ind.dl_src_addr_length;
1090 }
1091
1092 /* Copy destination info */
1093 if (daddr) {
1094 memcpy (daddr, &buf [dlp -> unitdata_ind.dl_dest_addr_offset],
1095 dlp -> unitdata_ind.dl_dest_addr_length);
1096 }
1097 if (daddrlen) {
1098 *daddrlen = dlp -> unitdata_ind.dl_dest_addr_length;
1099 }
1100
1101 if (grpaddr) {
1102 *grpaddr = dlp -> unitdata_ind.dl_group_address;
1103 }
1104
1105 return data.len;
1106 }
1107
1108 /*
1109 * expected - see if we got what we wanted.
1110 */
1111 static int expected (prim, dlp, msgflags)
1112 unsigned long prim;
1113 union DL_primitives *dlp;
1114 int msgflags;
1115 {
1116 if (msgflags != RS_HIPRI) {
1117 /* Message was not M_PCPROTO */
1118 return 0;
1119 }
1120
1121 if (dlp -> dl_primitive != prim) {
1122 /* Incorrect/unexpected return message */
1123 return 0;
1124 }
1125
1126 return 1;
1127 }
1128
1129 /*
1130 * strgetmsg - get a message from a stream, with timeout.
1131 */
1132 static int strgetmsg (fd, ctlp, datap, flagsp, caller)
1133 struct strbuf *ctlp, *datap;
1134 char *caller;
1135 int *flagsp;
1136 int fd;
1137 {
1138 int result;
1139 #ifdef USE_POLL
1140 struct pollfd pfd;
1141 int count;
1142 time_t now;
1143 time_t starttime;
1144 int to_msec;
1145 #endif
1146
1147 #ifdef USE_POLL
1148 pfd.fd = fd;
1149 pfd.events = POLLPRI; /* We're only interested in knowing
1150 * when we can receive the next high
1151 * priority message.
1152 */
1153 pfd.revents = 0;
1154
1155 now = time (&starttime);
1156 while (now <= starttime + DLPI_MAXWAIT) {
1157 to_msec = ((starttime + DLPI_MAXWAIT) - now) * 1000;
1158 count = poll (&pfd, 1, to_msec);
1159
1160 if (count == 0) {
1161 /* log_fatal ("strgetmsg: timeout"); */
1162 return -1;
1163 } else if (count < 0) {
1164 if (errno == EAGAIN || errno == EINTR) {
1165 time (&now);
1166 continue;
1167 } else {
1168 /* log_fatal ("poll: %m"); */
1169 return -1;
1170 }
1171 } else {
1172 break;
1173 }
1174 }
1175 #else /* defined (USE_POLL) */
1176 /*
1177 * Start timer. Can't use select, since it might return true if there
1178 * were non High-Priority data available on the stream.
1179 */
1180 (void) sigset (SIGALRM, sigalrm);
1181
1182 if (alarm (DLPI_MAXWAIT) < 0) {
1183 /* log_fatal ("alarm: %m"); */
1184 return -1;
1185 }
1186 #endif /* !defined (USE_POLL) */
1187
1188 /*
1189 * Set flags argument and issue getmsg ().
1190 */
1191 *flagsp = 0;
1192 if ((result = getmsg (fd, ctlp, datap, flagsp)) < 0) {
1193 return result;
1194 }
1195
1196 #ifndef USE_POLL
1197 /*
1198 * Stop timer.
1199 */
1200 if (alarm (0) < 0) {
1201 /* log_fatal ("alarm: %m"); */
1202 return -1;
1203 }
1204 #endif
1205
1206 /*
1207 * Check for MOREDATA and/or MORECTL.
1208 */
1209 if (result & (MORECTL|MOREDATA)) {
1210 return -1;
1211 }
1212
1213 /*
1214 * Check for at least sizeof (long) control data portion.
1215 */
1216 if (ctlp -> len < sizeof (long)) {
1217 return -1;
1218 }
1219
1220 return 0;
1221 }
1222
1223 #ifndef USE_POLL
1224 /*
1225 * sigalrm - handle alarms.
1226 */
1227 static void sigalrm (sig)
1228 int sig;
1229 {
1230 fprintf (stderr, "strgetmsg: timeout");
1231 exit (1);
1232 }
1233 #endif /* !defined (USE_POLL) */
1234
1235 int can_unicast_without_arp (ip)
1236 struct interface_info *ip;
1237 {
1238 return 1;
1239 }
1240
1241 int can_receive_unicast_unconfigured (ip)
1242 struct interface_info *ip;
1243 {
1244 return 1;
1245 }
1246
1247 void maybe_setup_fallback ()
1248 {
1249 isc_result_t status;
1250 struct interface_info *fbi;
1251 fbi = setup_fallback ();
1252 if (fbi) {
1253 if_register_fallback (fbi);
1254 fbi -> refcnt = 1;
1255 fbi -> type = dhcp_type_interface;
1256 status = omapi_register_io_object ((omapi_object_t)fbi,
1257 if_readsocket, 0,
1258 fallback_discard, 0, 0);
1259 if (status != ISC_R_SUCCESS)
1260 log_fatal ("Can't register I/O handle for %s: %s",
1261 fbi -> name, isc_result_totext (status));
1262 }
1263 }
1264 #endif /* USE_DLPI */