sys/lib/libsa/ether.c

http://www.minix3.org/ · C · 124 lines · 61 code · 17 blank · 46 comment · 12 complexity · 78de258056655503654d9bea018ae928 MD5 · raw file

  1. /* $NetBSD: ether.c,v 1.22 2009/01/12 11:32:45 tsutsui Exp $ */
  2. /*
  3. * Copyright (c) 1992 Regents of the University of California.
  4. * All rights reserved.
  5. *
  6. * This software was developed by the Computer Systems Engineering group
  7. * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  8. * contributed to Berkeley.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. All advertising materials mentioning features or use of this software
  19. * must display the following acknowledgement:
  20. * This product includes software developed by the University of
  21. * California, Lawrence Berkeley Laboratory and its contributors.
  22. * 4. Neither the name of the University nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  27. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * SUCH DAMAGE.
  37. *
  38. * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL)
  39. */
  40. #include <sys/param.h>
  41. #include <sys/socket.h>
  42. #ifdef _STANDALONE
  43. #include <lib/libkern/libkern.h>
  44. #else
  45. #include <string.h>
  46. #endif
  47. #include <net/if.h>
  48. #include <net/if_ether.h>
  49. #include <netinet/in.h>
  50. #include <netinet/in_systm.h>
  51. #include "stand.h"
  52. #include "net.h"
  53. /* Caller must leave room for ethernet header in front!! */
  54. ssize_t
  55. sendether(struct iodesc *d, void *pkt, size_t len, u_char *dea, int etype)
  56. {
  57. ssize_t n;
  58. struct ether_header *eh;
  59. #ifdef ETHER_DEBUG
  60. if (debug)
  61. printf("sendether: called\n");
  62. #endif
  63. eh = (struct ether_header *)pkt - 1;
  64. len += sizeof(*eh);
  65. MACPY(d->myea, eh->ether_shost); /* by byte */
  66. MACPY(dea, eh->ether_dhost); /* by byte */
  67. eh->ether_type = htons(etype);
  68. n = netif_put(d, eh, len);
  69. if (n == -1 || (size_t)n < sizeof(*eh))
  70. return -1;
  71. n -= sizeof(*eh);
  72. return n;
  73. }
  74. /*
  75. * Get a packet of any Ethernet type, with our address or
  76. * the broadcast address. Save the Ether type in arg 5.
  77. * NOTE: Caller must leave room for the Ether header.
  78. */
  79. ssize_t
  80. readether(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft,
  81. u_int16_t *etype)
  82. {
  83. ssize_t n;
  84. struct ether_header *eh;
  85. #ifdef ETHER_DEBUG
  86. if (debug)
  87. printf("readether: called\n");
  88. #endif
  89. eh = (struct ether_header *)pkt - 1;
  90. len += sizeof(*eh);
  91. n = netif_get(d, eh, len, tleft);
  92. if (n == -1 || (size_t)n < sizeof(*eh))
  93. return -1;
  94. /* Validate Ethernet address. */
  95. if (memcmp(d->myea, eh->ether_dhost, 6) != 0 &&
  96. memcmp(bcea, eh->ether_dhost, 6) != 0) {
  97. #ifdef ETHER_DEBUG
  98. if (debug)
  99. printf("readether: not ours (ea=%s)\n",
  100. ether_sprintf(eh->ether_dhost));
  101. #endif
  102. return -1;
  103. }
  104. *etype = ntohs(eh->ether_type);
  105. n -= sizeof(*eh);
  106. return n;
  107. }