2 * $Id: libnet_link_dlpi.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * libnet_dlpi.c - dlpi routines
7 * Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
10 * Copyright (c) 1993, 1994, 1995, 1996, 1997
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that: (1) source code distributions
15 * retain the above copyright notice and this paragraph in its entirety, (2)
16 * distributions including binary code include the above copyright notice and
17 * this paragraph in its entirety in the documentation or other materials
18 * provided with the distribution, and (3) all advertising materials mentioning
19 * features or use of this software display the following acknowledgement:
20 * ``This product includes software developed by the University of California,
21 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
22 * the University nor the names of its contributors may be used to endorse
23 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 * This code contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk),
30 * University College London.
35 #include "../include/config.h"
37 #include <sys/types.h>
39 #ifdef HAVE_SYS_BUFMOD_H
40 #include <sys/bufmod.h>
44 #include <sys/socket.h>
46 #ifdef DL_HP_PPA_ACK_OBS
49 #include <sys/stream.h>
50 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
51 #include <sys/systeminfo.h>
71 #include "../include/libnet.h"
72 #include "../include/bpf.h"
74 #include "../include/gnuc.h"
75 #ifdef HAVE_OS_PROTO_H
76 #include "../include/os-proto.h"
79 #ifndef DLPI_DEV_PREFIX
80 #define DLPI_DEV_PREFIX "/dev"
86 static int dlattachreq(int, bpf_u_int32, char *);
87 static int dlbindack(int, char *, char *);
88 static int dlbindreq(int, bpf_u_int32, char *);
89 static int dlinfoack(int, char *, char *);
90 static int dlinforeq(int, char *);
91 static int dlokack(int, const char *, char *, char *);
92 static int recv_ack(int, int, const char *, char *, char *);
93 static int send_request(int, char *, int, char *, char *, int);
94 #ifdef HAVE_SYS_BUFMOD_H
95 static int strioctl(int, int, int, char *);
98 static int dlpi_kread(int, off_t, void *, u_int, char *);
101 static int get_dlpi_ppa(int, const char *, int, char *);
104 /* XXX Needed by HP-UX (at least) */
105 static bpf_u_int32 ctlbuf[MAXDLBUF];
108 struct libnet_link_int *
109 libnet_open_link_interface(char *device, char *ebuf)
113 register struct libnet_link_int *l;
115 register dl_info_ack_t *infop;
116 bpf_u_int32 buf[MAXDLBUF];
118 #ifndef HAVE_DEV_DLPI
122 l = (struct libnet_link_int *)malloc(sizeof(*l));
125 strcpy(ebuf, ll_strerror(errno));
128 memset(l, 0, sizeof(*l));
131 * Determine device and ppa
133 cp = strpbrk(device, "0123456789");
136 sprintf(ebuf, "%s missing unit number", device);
139 ppa = strtol(cp, &eos, 10);
142 sprintf(ebuf, "%s bad unit number", device);
148 memset(&dname, 0, sizeof(dname));
149 strncpy(dname, device, sizeof(dname) - 1);
153 sprintf(dname, "%s/%s", DLPI_DEV_PREFIX, device);
157 * Map network device to /dev/dlpi unit
161 l->fd = open(cp, O_RDWR);
164 sprintf(ebuf, "%s: %s", cp, ll_strerror(errno));
169 * Map network interface to /dev/dlpi unit
171 ppa = get_dlpi_ppa(l->fd, dname, ppa, ebuf);
178 * Try device without unit number
180 strcpy(dname2, dname);
181 cp = strchr(dname, *cp);
184 l->fd = open(dname, O_RDWR);
189 sprintf(ebuf, "%s: %s", dname, ll_strerror(errno));
194 * Try again with unit number
196 l->fd = open(dname2, O_RDWR);
199 sprintf(ebuf, "%s: %s", dname2, ll_strerror(errno));
204 while (*cp && !isdigit((int)*cp)) cp++;
205 if (*cp) ppa = atoi(cp);
208 * XXX Assume unit zero
214 * Attach if "style 2" provider
216 if (dlinforeq(l->fd, ebuf) < 0 || dlinfoack(l->fd, (char *)buf, ebuf) < 0)
220 infop = &((union DL_primitives *)buf)->info_ack;
221 if (infop->dl_provider_style == DL_STYLE2 && (dlattachreq(l->fd, ppa, ebuf)
222 < 0 || dlokack(l->fd, "attach", (char *)buf, ebuf) < 0))
228 * Bind HP-UX 9 and HP-UX 10.20
230 #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20) || defined(HAVE_SOLARIS)
231 if (dlbindreq(l->fd, 0, ebuf) < 0 || dlbindack(l->fd, (char *)buf, ebuf) < 0)
238 * Determine link type
240 if (dlinforeq(l->fd, ebuf) < 0 || dlinfoack(l->fd, (char *)buf, ebuf) < 0)
245 infop = &((union DL_primitives *)buf)->info_ack;
246 switch (infop->dl_mac_type)
250 l->linktype = DLT_EN10MB;
253 l->linktype = DLT_FDDI;
256 sprintf(ebuf, "unknown mac type 0x%lu", infop->dl_mac_type);
262 * This is a non standard SunOS hack to get the ethernet header.
264 if (strioctl(l->fd, DLIOCRAW, 0, NULL) < 0)
266 sprintf(ebuf, "DLIOCRAW: %s", ll_strerror(errno));
279 send_request(int fd, char *ptr, int len, char *what, char *ebuf, int flags)
287 if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0)
289 sprintf(ebuf, "send_request: putmsg \"%s\": %s", what, ll_strerror(errno));
296 recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf)
298 union DL_primitives *dlp;
302 ctl.maxlen = MAXDLBUF;
307 if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0)
309 sprintf(ebuf, "recv_ack: %s getmsg: %s", what, ll_strerror(errno));
313 dlp = (union DL_primitives *)ctl.buf;
314 switch (dlp->dl_primitive)
317 case DL_PHYS_ADDR_ACK:
329 switch (dlp->error_ack.dl_errno)
332 sprintf(ebuf, "recv_ack: %s bad ppa (device unit)", what);
335 sprintf(ebuf, "recv_ack: %s: %s",
336 what, ll_strerror(dlp->error_ack.dl_unix_errno));
340 "recv_ack: %s: Service not supplied by provider", what);
343 sprintf(ebuf, "recv_ack: %s error 0x%x", what,
344 (bpf_u_int32)dlp->error_ack.dl_errno);
350 sprintf(ebuf, "recv_ack: %s unexpected primitive ack 0x%x ",
351 what, (bpf_u_int32)dlp->dl_primitive);
357 sprintf(ebuf, "recv_ack: %s ack too small (%d < %d)",
358 what, ctl.len, size);
365 dlpromiscoffreq(int fd, bpf_u_int32 level, char *ebuf)
367 dl_promiscon_req_t req;
369 req.dl_primitive = DL_PROMISCOFF_REQ;
370 req.dl_level = level;
372 return (send_request(fd, (char *)&req, sizeof(req), "promiscoff", ebuf, 0));
376 dlpromisconreq(int fd, bpf_u_int32 level, char *ebuf)
378 dl_promiscon_req_t req;
380 req.dl_primitive = DL_PROMISCON_REQ;
381 req.dl_level = level;
383 return (send_request(fd, (char *)&req, sizeof(req), "promiscon", ebuf, 0));
388 dlattachreq(int fd, bpf_u_int32 ppa, char *ebuf)
392 req.dl_primitive = DL_ATTACH_REQ;
395 return (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf, 0));
399 dlbindreq(int fd, bpf_u_int32 sap, char *ebuf)
404 memset((char *)&req, 0, sizeof(req));
405 req.dl_primitive = DL_BIND_REQ;
407 req.dl_max_conind = 1; /* XXX magic number */
409 * 22 is INSAP as per the HP-UX DLPI Programmer's Guide
412 req.dl_service_mode = DL_HP_RAWDLS;
416 req.dl_service_mode = DL_CLDLS;
419 return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf, 0));
424 dlbindack(int fd, char *bufp, char *ebuf)
426 return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf));
431 dlokack(int fd, const char *what, char *bufp, char *ebuf)
433 return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf));
438 dlinforeq(int fd, char *ebuf)
442 req.dl_primitive = DL_INFO_REQ;
444 return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf, RS_HIPRI));
448 dlinfoack(int fd, char *bufp, char *ebuf)
450 return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf));
454 #ifdef HAVE_SYS_BUFMOD_H
456 strioctl(int fd, int cmd, int len, char *dp)
466 rc = ioctl(fd, I_STR, &str);
479 #ifdef DL_HP_PPA_ACK_OBS
481 * Under HP-UX 10, we can ask for the ppa
484 get_dlpi_ppa(register int fd, register const char *device, register int unit,
487 register dl_hp_ppa_ack_t *ap;
488 register dl_hp_ppa_info_t *ip;
490 register u_long majdev;
493 bpf_u_int32 buf[MAXDLBUF];
495 if (stat(device, &statbuf) < 0)
497 sprintf(ebuf, "stat: %s: %s", device, ll_strerror(errno));
500 majdev = major(statbuf.st_rdev);
502 memset((char *)&req, 0, sizeof(req));
503 req.dl_primitive = DL_HP_PPA_REQ;
505 memset((char *)buf, 0, sizeof(buf));
506 if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf, 0) < 0 ||
507 recv_ack(fd, DL_HP_PPA_ACK_SIZE, "hpppa", (char *)buf, ebuf) < 0)
512 ap = (dl_hp_ppa_ack_t *)buf;
513 ip = (dl_hp_ppa_info_t *)((u_char *)ap + ap->dl_offset);
515 for (i = 0; i < ap->dl_count; i++)
517 if (ip->dl_mjr_num == majdev && ip->dl_instance_num == unit)
520 ip = (dl_hp_ppa_info_t *)((u_char *)ip + ip->dl_next_offset);
523 if (i == ap->dl_count)
525 sprintf(ebuf, "can't find PPA for %s", device);
529 if (ip->dl_hdw_state == HDW_DEAD)
531 sprintf(ebuf, "%s: hardware state: DOWN\n", device);
534 return ((int)ip->dl_ppa);
540 * Under HP-UX 9, there is no good way to determine the ppa.
541 * So punt and read it from /dev/kmem.
543 static struct nlist nl[] =
550 static char path_vmunix[] = "/hp-ux";
553 * Determine ppa number that specifies ifname
556 get_dlpi_ppa(register int fd, register const char *ifname, register int unit,
559 register const char *cp;
563 char if_name[sizeof(ifnet.if_name)], tifname[32];
565 cp = strrchr(ifname, '/');
570 if (nlist(path_vmunix, &nl) < 0)
572 sprintf(ebuf, "nlist %s failed", path_vmunix);
576 if (nl[NL_IFNET].n_value == 0)
578 sprintf(ebuf, "could't find %s kernel symbol", nl[NL_IFNET].n_name);
582 kd = open("/dev/kmem", O_RDONLY);
585 sprintf(ebuf, "kmem open: %s", ll_strerror(errno));
589 if (dlpi_kread(kd, nl[NL_IFNET].n_value, &addr, sizeof(addr), ebuf) < 0)
594 for (; addr != NULL; addr = ifnet.if_next)
596 if (dlpi_kread(kd, (off_t)addr, &ifnet, sizeof(ifnet), ebuf) < 0 ||
597 dlpi_kread(kd, (off_t)ifnet.if_name,
598 if_name, sizeof(if_name), ebuf) < 0)
603 sprintf(tifname, "%.*s%d",
604 (int)sizeof(if_name), if_name, ifnet.if_unit);
605 if (strcmp(tifname, ifname) == 0)
607 return (ifnet.if_index);
611 sprintf(ebuf, "Can't find %s", ifname);
616 dlpi_kread(register int fd, register off_t addr,
617 register void *buf, register u_int len, register char *ebuf)
621 if (lseek(fd, addr, SEEK_SET) < 0)
623 sprintf(ebuf, "lseek: %s", ll_strerror(errno));
626 cc = read(fd, buf, len);
629 sprintf(ebuf, "read: %s", ll_strerror(errno));
634 sprintf(ebuf, "short read (%d != %d)", cc, len);
641 /*#include <netinet/if_ether.h>*/
643 struct EnetHeaderInfo
645 struct ether_addr DestEtherAddr;
646 u_short EtherFrameType;
651 libnet_close_link_interface(struct libnet_link_int *l)
653 if (close(l->fd) == 0)
664 struct EnetHeaderInfo ArpHeader =
666 {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, ETHERTYPE_ARP
671 libnet_write_link_layer(struct libnet_link_int *l, const char *device,
672 u_char *buf, int len)
674 struct strbuf data, ctl;
675 union DL_primitives *dlp;
677 struct EnetHeaderInfo *EnetHeaderInfoP;
679 dlp = (union DL_primitives*) ctlbuf;
680 dlp->unitdata_req.dl_primitive = DL_UNITDATA_REQ;
681 dlp->unitdata_req.dl_priority.dl_min = 0;
682 dlp->unitdata_req.dl_priority.dl_max = 0;
683 dlp->unitdata_req.dl_dest_addr_length = (sizeof(struct ether_addr) +
685 dlp->unitdata_req.dl_dest_addr_offset = DL_UNITDATA_REQ_SIZE;
687 EnetHeaderInfoP = (struct EnetHeaderInfo *)(ctlbuf + DL_UNITDATA_REQ_SIZE);
688 memcpy(EnetHeaderInfoP, (char *)&(ArpHeader), (sizeof(struct ether_addr) +
692 ctl.len = DL_UNITDATA_REQ_SIZE + sizeof (struct EnetHeaderInfo);
693 ctl.buf = (char *)dlp;
699 c = putmsg(l->fd, NULL, &data, 0);
703 fprintf(stderr, "write_link_layer: (%s)\n", strerror(errno));
712 libnet_get_hwaddr(struct libnet_link_int *l, const char *device, char *ebuf)
715 union DL_primitives *dlp;
716 struct ether_addr *eap;
718 dlp = (union DL_primitives*) buf;
720 dlp->physaddr_req.dl_primitive = DL_PHYS_ADDR_REQ;
721 dlp->physaddr_req.dl_addr_type = DL_CURR_PHYS_ADDR;
723 if (send_request(l->fd, (char *)dlp, DL_PHYS_ADDR_REQ_SIZE, "physaddr",
726 sprintf(ebuf, "get_hwaddr %s", strerror(errno));
729 if (recv_ack(l->fd, DL_PHYS_ADDR_ACK_SIZE, "physaddr", (char *)dlp, ebuf)
732 sprintf(ebuf, "get_hwaddr %s", strerror(errno));
736 eap = (struct ether_addr *)
737 ((char *) dlp + dlp->physaddr_ack.dl_addr_offset);