PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/libexec/bootpd/getether.c

https://github.com/blacklion/GEOM-Events
C | 389 lines | 278 code | 36 blank | 75 comment | 41 complexity | 6e2edf3d2ba11d8f43ba9b7abca76c8f MD5 | raw file
  1. /*
  2. * getether.c : get the ethernet address of an interface
  3. *
  4. * All of this code is quite system-specific. As you may well
  5. * guess, it took a good bit of detective work to figure out!
  6. *
  7. * If you figure out how to do this on another system,
  8. * please let me know. <gwr@mc.com>
  9. *
  10. * $FreeBSD$
  11. */
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #ifndef NO_UNISTD
  15. #include <unistd.h>
  16. #endif
  17. #include <ctype.h>
  18. #include <paths.h>
  19. #include <string.h>
  20. #include <syslog.h>
  21. #include "getether.h"
  22. #include "report.h"
  23. #define EALEN 6
  24. #if defined(ultrix) || (defined(__osf__) && defined(__alpha))
  25. /*
  26. * This is really easy on Ultrix! Thanks to
  27. * Harald Lundberg <hl@tekla.fi> for this code.
  28. *
  29. * The code here is not specific to the Alpha, but that was the
  30. * only symbol we could find to identify DEC's version of OSF.
  31. * (Perhaps we should just define DEC in the Makefile... -gwr)
  32. */
  33. #include <sys/ioctl.h>
  34. #include <net/if.h> /* struct ifdevea */
  35. getether(ifname, eap)
  36. char *ifname, *eap;
  37. {
  38. int rc = -1;
  39. int fd;
  40. struct ifdevea phys;
  41. bzero(&phys, sizeof(phys));
  42. strcpy(phys.ifr_name, ifname);
  43. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  44. report(LOG_ERR, "getether: socket(INET,DGRAM) failed");
  45. return -1;
  46. }
  47. if (ioctl(fd, SIOCRPHYSADDR, &phys) < 0) {
  48. report(LOG_ERR, "getether: ioctl SIOCRPHYSADDR failed");
  49. } else {
  50. bcopy(&phys.current_pa[0], eap, EALEN);
  51. rc = 0;
  52. }
  53. close(fd);
  54. return rc;
  55. }
  56. #define GETETHER
  57. #endif /* ultrix|osf1 */
  58. #ifdef SUNOS
  59. #include <sys/sockio.h>
  60. #include <sys/time.h> /* needed by net_if.h */
  61. #include <net/nit_if.h> /* for NIOCBIND */
  62. #include <net/if.h> /* for struct ifreq */
  63. getether(ifname, eap)
  64. char *ifname; /* interface name from ifconfig structure */
  65. char *eap; /* Ether address (output) */
  66. {
  67. int rc = -1;
  68. struct ifreq ifrnit;
  69. int nit;
  70. bzero((char *) &ifrnit, sizeof(ifrnit));
  71. strlcpy(&ifrnit.ifr_name[0], ifname, IFNAMSIZ);
  72. nit = open("/dev/nit", 0);
  73. if (nit < 0) {
  74. report(LOG_ERR, "getether: open /dev/nit: %s",
  75. get_errmsg());
  76. return rc;
  77. }
  78. do {
  79. if (ioctl(nit, NIOCBIND, &ifrnit) < 0) {
  80. report(LOG_ERR, "getether: NIOCBIND on nit");
  81. break;
  82. }
  83. if (ioctl(nit, SIOCGIFADDR, &ifrnit) < 0) {
  84. report(LOG_ERR, "getether: SIOCGIFADDR on nit");
  85. break;
  86. }
  87. bcopy(&ifrnit.ifr_addr.sa_data[0], eap, EALEN);
  88. rc = 0;
  89. } while (0);
  90. close(nit);
  91. return rc;
  92. }
  93. #define GETETHER
  94. #endif /* SUNOS */
  95. #if defined(__FreeBSD__) || defined(__NetBSD__)
  96. /* Thanks to John Brezak <brezak@ch.hp.com> for this code. */
  97. #include <sys/ioctl.h>
  98. #include <sys/time.h>
  99. #include <net/if.h>
  100. #include <net/if_dl.h>
  101. #include <net/if_types.h>
  102. int
  103. getether(ifname, eap)
  104. char *ifname; /* interface name from ifconfig structure */
  105. char *eap; /* Ether address (output) */
  106. {
  107. int fd, rc = -1;
  108. register int n;
  109. struct ifreq ibuf[16];
  110. struct ifconf ifc;
  111. register struct ifreq *ifrp, *ifend;
  112. /* Fetch the interface configuration */
  113. fd = socket(AF_INET, SOCK_DGRAM, 0);
  114. if (fd < 0) {
  115. report(LOG_ERR, "getether: socket %s: %s", ifname, get_errmsg());
  116. return (fd);
  117. }
  118. ifc.ifc_len = sizeof(ibuf);
  119. ifc.ifc_buf = (caddr_t) ibuf;
  120. if (ioctl(fd, SIOCGIFCONF, (char *) &ifc) < 0 ||
  121. ifc.ifc_len < sizeof(struct ifreq)) {
  122. report(LOG_ERR, "getether: SIOCGIFCONF: %s", get_errmsg());
  123. goto out;
  124. }
  125. /* Search interface configuration list for link layer address. */
  126. ifrp = ibuf;
  127. ifend = (struct ifreq *) ((char *) ibuf + ifc.ifc_len);
  128. while (ifrp < ifend) {
  129. /* Look for interface */
  130. if (strcmp(ifname, ifrp->ifr_name) == 0 &&
  131. ifrp->ifr_addr.sa_family == AF_LINK &&
  132. ((struct sockaddr_dl *) &ifrp->ifr_addr)->sdl_type == IFT_ETHER) {
  133. bcopy(LLADDR((struct sockaddr_dl *) &ifrp->ifr_addr), eap, EALEN);
  134. rc = 0;
  135. break;
  136. }
  137. /* Bump interface config pointer */
  138. n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
  139. if (n < sizeof(*ifrp))
  140. n = sizeof(*ifrp);
  141. ifrp = (struct ifreq *) ((char *) ifrp + n);
  142. }
  143. out:
  144. close(fd);
  145. return (rc);
  146. }
  147. #define GETETHER
  148. #endif /* __NetBSD__ */
  149. #ifdef SVR4
  150. /*
  151. * This is for "Streams TCP/IP" by Lachman Associates.
  152. * They sure made this cumbersome! -gwr
  153. */
  154. #include <sys/sockio.h>
  155. #include <sys/dlpi.h>
  156. #include <stropts.h>
  157. #include <string.h>
  158. #ifndef NULL
  159. #define NULL 0
  160. #endif
  161. int
  162. getether(ifname, eap)
  163. char *ifname; /* interface name from ifconfig structure */
  164. char *eap; /* Ether address (output) */
  165. {
  166. int rc = -1;
  167. char devname[32];
  168. char tmpbuf[sizeof(union DL_primitives) + 16];
  169. struct strbuf cbuf;
  170. int fd, flags;
  171. union DL_primitives *dlp;
  172. char *enaddr;
  173. int unit = -1; /* which unit to attach */
  174. snprintf(devname, sizeof(devname), "%s%s", _PATH_DEV, ifname);
  175. fd = open(devname, 2);
  176. if (fd < 0) {
  177. /* Try without the trailing digit. */
  178. char *p = devname + 5;
  179. while (isalpha(*p))
  180. p++;
  181. if (isdigit(*p)) {
  182. unit = *p - '0';
  183. *p = '\0';
  184. }
  185. fd = open(devname, 2);
  186. if (fd < 0) {
  187. report(LOG_ERR, "getether: open %s: %s",
  188. devname, get_errmsg());
  189. return rc;
  190. }
  191. }
  192. #ifdef DL_ATTACH_REQ
  193. /*
  194. * If this is a "Style 2" DLPI, then we must "attach" first
  195. * to tell the driver which unit (board, port) we want.
  196. * For now, decide this based on the device name.
  197. * (Should do "info_req" and check dl_provider_style ...)
  198. */
  199. if (unit >= 0) {
  200. memset(tmpbuf, 0, sizeof(tmpbuf));
  201. dlp = (union DL_primitives *) tmpbuf;
  202. dlp->dl_primitive = DL_ATTACH_REQ;
  203. dlp->attach_req.dl_ppa = unit;
  204. cbuf.buf = tmpbuf;
  205. cbuf.len = DL_ATTACH_REQ_SIZE;
  206. if (putmsg(fd, &cbuf, NULL, 0) < 0) {
  207. report(LOG_ERR, "getether: attach: putmsg: %s", get_errmsg());
  208. goto out;
  209. }
  210. /* Recv the ack. */
  211. cbuf.buf = tmpbuf;
  212. cbuf.maxlen = sizeof(tmpbuf);
  213. flags = 0;
  214. if (getmsg(fd, &cbuf, NULL, &flags) < 0) {
  215. report(LOG_ERR, "getether: attach: getmsg: %s", get_errmsg());
  216. goto out;
  217. }
  218. /*
  219. * Check the type, etc.
  220. */
  221. if (dlp->dl_primitive == DL_ERROR_ACK) {
  222. report(LOG_ERR, "getether: attach: dlpi_errno=%d, unix_errno=%d",
  223. dlp->error_ack.dl_errno,
  224. dlp->error_ack.dl_unix_errno);
  225. goto out;
  226. }
  227. if (dlp->dl_primitive != DL_OK_ACK) {
  228. report(LOG_ERR, "getether: attach: not OK or ERROR");
  229. goto out;
  230. }
  231. } /* unit >= 0 */
  232. #endif /* DL_ATTACH_REQ */
  233. /*
  234. * Get the Ethernet address the same way the ARP module
  235. * does when it is pushed onto a new stream (bind).
  236. * One should instead be able just do a dl_info_req
  237. * but many drivers do not supply the hardware address
  238. * in the response to dl_info_req (they MUST supply it
  239. * for dl_bind_ack because the ARP module requires it).
  240. */
  241. memset(tmpbuf, 0, sizeof(tmpbuf));
  242. dlp = (union DL_primitives *) tmpbuf;
  243. dlp->dl_primitive = DL_BIND_REQ;
  244. dlp->bind_req.dl_sap = 0x8FF; /* XXX - Unused SAP */
  245. cbuf.buf = tmpbuf;
  246. cbuf.len = DL_BIND_REQ_SIZE;
  247. if (putmsg(fd, &cbuf, NULL, 0) < 0) {
  248. report(LOG_ERR, "getether: bind: putmsg: %s", get_errmsg());
  249. goto out;
  250. }
  251. /* Recv the ack. */
  252. cbuf.buf = tmpbuf;
  253. cbuf.maxlen = sizeof(tmpbuf);
  254. flags = 0;
  255. if (getmsg(fd, &cbuf, NULL, &flags) < 0) {
  256. report(LOG_ERR, "getether: bind: getmsg: %s", get_errmsg());
  257. goto out;
  258. }
  259. /*
  260. * Check the type, etc.
  261. */
  262. if (dlp->dl_primitive == DL_ERROR_ACK) {
  263. report(LOG_ERR, "getether: bind: dlpi_errno=%d, unix_errno=%d",
  264. dlp->error_ack.dl_errno,
  265. dlp->error_ack.dl_unix_errno);
  266. goto out;
  267. }
  268. if (dlp->dl_primitive != DL_BIND_ACK) {
  269. report(LOG_ERR, "getether: bind: not OK or ERROR");
  270. goto out;
  271. }
  272. if (dlp->bind_ack.dl_addr_offset == 0) {
  273. report(LOG_ERR, "getether: bind: ack has no address");
  274. goto out;
  275. }
  276. if (dlp->bind_ack.dl_addr_length < EALEN) {
  277. report(LOG_ERR, "getether: bind: ack address truncated");
  278. goto out;
  279. }
  280. /*
  281. * Copy the Ethernet address out of the message.
  282. */
  283. enaddr = tmpbuf + dlp->bind_ack.dl_addr_offset;
  284. memcpy(eap, enaddr, EALEN);
  285. rc = 0;
  286. out:
  287. close(fd);
  288. return rc;
  289. }
  290. #define GETETHER
  291. #endif /* SVR4 */
  292. #ifdef __linux__
  293. /*
  294. * This is really easy on Linux! This version (for linux)
  295. * written by Nigel Metheringham <nigelm@ohm.york.ac.uk> and
  296. * updated by Pauline Middelink <middelin@polyware.iaf.nl>
  297. *
  298. * The code is almost identical to the Ultrix code - however
  299. * the names are different to confuse the innocent :-)
  300. * Most of this code was stolen from the Ultrix bit above.
  301. */
  302. #include <memory.h>
  303. #include <sys/ioctl.h>
  304. #include <net/if.h> /* struct ifreq */
  305. #include <sys/socketio.h> /* Needed for IOCTL defs */
  306. int
  307. getether(ifname, eap)
  308. char *ifname, *eap;
  309. {
  310. int rc = -1;
  311. int fd;
  312. struct ifreq phys;
  313. memset(&phys, 0, sizeof(phys));
  314. strcpy(phys.ifr_name, ifname);
  315. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  316. report(LOG_ERR, "getether: socket(INET,DGRAM) failed");
  317. return -1;
  318. }
  319. if (ioctl(fd, SIOCGIFHWADDR, &phys) < 0) {
  320. report(LOG_ERR, "getether: ioctl SIOCGIFHWADDR failed");
  321. } else {
  322. memcpy(eap, &phys.ifr_hwaddr.sa_data, EALEN);
  323. rc = 0;
  324. }
  325. close(fd);
  326. return rc;
  327. }
  328. #define GETETHER
  329. #endif /* __linux__ */
  330. /* If we don't know how on this system, just return an error. */
  331. #ifndef GETETHER
  332. int
  333. getether(ifname, eap)
  334. char *ifname, *eap;
  335. {
  336. return -1;
  337. }
  338. #endif /* !GETETHER */
  339. /*
  340. * Local Variables:
  341. * tab-width: 4
  342. * c-indent-level: 4
  343. * c-argdecl-indent: 4
  344. * c-continued-statement-offset: 4
  345. * c-continued-brace-offset: -4
  346. * c-label-offset: -4
  347. * c-brace-offset: 0
  348. * End:
  349. */