linux.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // linux.c: low level access routines for Linux
  2. #define _GNU_SOURCE
  3. #include "config.h"
  4. #include <sys/socket.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <sys/time.h>
  10. #include <features.h> /* for the glibc version number */
  11. #if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
  12. #include <netpacket/packet.h>
  13. #include <net/ethernet.h> /* the L2 protocols */
  14. #else
  15. #include <asm/types.h>
  16. #include <linux/if_packet.h>
  17. #include <linux/if_ether.h> /* The L2 protocols */
  18. #endif
  19. #include <fcntl.h>
  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. int getindx(int, char *);
  29. int getea(int, char *, uchar *);
  30. int
  31. dial(char *eth, int bufcnt) // get us a raw connection to an interface
  32. {
  33. int i, n, s;
  34. struct sockaddr_ll sa;
  35. enum { aoe_type = 0x88a2 };
  36. memset(&sa, 0, sizeof sa);
  37. s = socket(PF_PACKET, SOCK_RAW, htons(aoe_type));
  38. if (s == -1) {
  39. perror("got bad socket");
  40. return -1;
  41. }
  42. i = getindx(s, eth);
  43. sa.sll_family = AF_PACKET;
  44. sa.sll_protocol = htons(0x88a2);
  45. sa.sll_ifindex = i;
  46. n = bind(s, (struct sockaddr *)&sa, sizeof sa);
  47. if (n == -1) {
  48. perror("bind funky");
  49. return -1;
  50. }
  51. struct bpf_program {
  52. ulong bf_len;
  53. void *bf_insns;
  54. } *bpf_program = create_bpf_program(shelf, slot);
  55. setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, bpf_program, sizeof(*bpf_program));
  56. free_bpf_program(bpf_program);
  57. n = bufcnt * getmtu(s, eth);
  58. if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) < 0)
  59. perror("setsockopt SOL_SOCKET, SO_SNDBUF");
  60. if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) < 0)
  61. perror("setsockopt SOL_SOCKET, SO_RCVBUF");
  62. return s;
  63. }
  64. int
  65. getindx(int s, char *name) // return the index of device 'name'
  66. {
  67. struct ifreq xx;
  68. int n;
  69. strcpy(xx.ifr_name, name);
  70. n = ioctl(s, SIOCGIFINDEX, &xx);
  71. if (n == -1)
  72. return -1;
  73. return xx.ifr_ifindex;
  74. }
  75. int
  76. getea(int s, char *name, uchar *ea)
  77. {
  78. struct ifreq xx;
  79. int n;
  80. strcpy(xx.ifr_name, name);
  81. n = ioctl(s, SIOCGIFHWADDR, &xx);
  82. if (n == -1) {
  83. perror("Can't get hw addr");
  84. return 0;
  85. }
  86. memmove(ea, xx.ifr_hwaddr.sa_data, 6);
  87. return 1;
  88. }
  89. int
  90. getmtu(int s, char *name)
  91. {
  92. struct ifreq xx;
  93. int n;
  94. strcpy(xx.ifr_name, name);
  95. n = ioctl(s, SIOCGIFMTU, &xx);
  96. if (n == -1) {
  97. perror("Can't get mtu");
  98. return 1500;
  99. }
  100. return xx.ifr_mtu;
  101. }
  102. int
  103. getsec(int fd, uchar *place, vlong lba, int nsec)
  104. {
  105. return pread(fd, place, nsec * 512, lba * 512);
  106. }
  107. int
  108. putsec(int fd, uchar *place, vlong lba, int nsec)
  109. {
  110. return pwrite(fd, place, nsec * 512, lba * 512);
  111. }
  112. int
  113. getpkt(int fd, uchar *buf, int sz)
  114. {
  115. return read(fd, buf, sz);
  116. }
  117. int
  118. putpkt(int fd, uchar *buf, int sz)
  119. {
  120. return write(fd, buf, sz);
  121. }
  122. vlong
  123. getsize(int fd)
  124. {
  125. vlong size;
  126. struct stat s;
  127. int n;
  128. n = ioctl(fd, BLKGETSIZE64, &size);
  129. if (n == -1) { // must not be a block special
  130. n = fstat(fd, &s);
  131. if (n == -1) {
  132. perror("getsize");
  133. exit(1);
  134. }
  135. size = s.st_size;
  136. }
  137. return size;
  138. }