linux.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* linux.c: low level access routines for Linux
  2. * Copyright 2007, Coraid, Inc., and licensed under GPL v.2.
  3. */
  4. #include "config.h"
  5. #include <sys/socket.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <sys/time.h>
  11. #include <features.h> /* for the glibc version number */
  12. #if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
  13. #include <netpacket/packet.h>
  14. #include <net/ethernet.h> /* the L2 protocols */
  15. #else
  16. #include <asm/types.h>
  17. #include <linux/if_packet.h>
  18. #include <linux/if_ether.h> /* The L2 protocols */
  19. #endif
  20. #include <sys/ioctl.h>
  21. #include <sys/types.h>
  22. #include <net/if.h>
  23. #include <netinet/in.h>
  24. #include <linux/fs.h>
  25. #include <sys/stat.h>
  26. #include "dat.h"
  27. #include "fns.h"
  28. static int
  29. getindx(int s, char *name) // return the index of device 'name'
  30. {
  31. struct ifreq xx;
  32. int n;
  33. strcpy(xx.ifr_name, name);
  34. n = ioctl(s, SIOCGIFINDEX, &xx);
  35. if (n == -1)
  36. return -1;
  37. return xx.ifr_ifindex;
  38. }
  39. int
  40. dial(char *eth) // get us a raw connection to an interface
  41. {
  42. int i;
  43. int n, s;
  44. struct sockaddr_ll sa;
  45. enum { aoe_type = 0x88a2 };
  46. memset(&sa, 0, sizeof sa);
  47. s = socket(PF_PACKET, SOCK_RAW, htons(aoe_type));
  48. if (s == -1) {
  49. perror("got bad socket");
  50. return -1;
  51. }
  52. i = getindx(s, eth);
  53. sa.sll_family = AF_PACKET;
  54. sa.sll_protocol = htons(0x88a2);
  55. sa.sll_ifindex = i;
  56. n = bind(s, (struct sockaddr *)&sa, sizeof sa);
  57. if (n == -1) {
  58. perror("bind funky");
  59. return -1;
  60. }
  61. return s;
  62. }
  63. int
  64. getea(int s, char *name, uchar *ea)
  65. {
  66. struct ifreq xx;
  67. int n;
  68. strcpy(xx.ifr_name, name);
  69. n = ioctl(s, SIOCGIFHWADDR, &xx);
  70. if (n == -1) {
  71. perror("Can't get hw addr");
  72. return 0;
  73. }
  74. memmove(ea, xx.ifr_hwaddr.sa_data, 6);
  75. return 1;
  76. }