linux.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if (i < 0) {
  44. perror(eth);
  45. return -1;
  46. }
  47. sa.sll_family = AF_PACKET;
  48. sa.sll_protocol = htons(0x88a2);
  49. sa.sll_ifindex = i;
  50. n = bind(s, (struct sockaddr *)&sa, sizeof sa);
  51. if (n == -1) {
  52. perror("bind funky");
  53. return -1;
  54. }
  55. struct bpf_program {
  56. ulong bf_len;
  57. void *bf_insns;
  58. } *bpf_program = create_bpf_program(shelf, slot);
  59. setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, bpf_program, sizeof(*bpf_program));
  60. free_bpf_program(bpf_program);
  61. n = bufcnt * getmtu(s, eth);
  62. if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) < 0)
  63. perror("setsockopt SOL_SOCKET, SO_SNDBUF");
  64. if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) < 0)
  65. perror("setsockopt SOL_SOCKET, SO_RCVBUF");
  66. return s;
  67. }
  68. int
  69. getindx(int s, char *name) // return the index of device 'name'
  70. {
  71. struct ifreq xx;
  72. int n;
  73. snprintf(xx.ifr_name, sizeof xx.ifr_name, "%s", name);
  74. n = ioctl(s, SIOCGIFINDEX, &xx);
  75. if (n == -1)
  76. return -1;
  77. return xx.ifr_ifindex;
  78. }
  79. int
  80. getea(int s, char *name, uchar *ea)
  81. {
  82. struct ifreq xx;
  83. int n;
  84. snprintf(xx.ifr_name, sizeof xx.ifr_name, "%s", name);
  85. n = ioctl(s, SIOCGIFHWADDR, &xx);
  86. if (n == -1) {
  87. perror("Can't get hw addr");
  88. return 0;
  89. }
  90. memmove(ea, xx.ifr_hwaddr.sa_data, 6);
  91. return 1;
  92. }
  93. int
  94. getmtu(int s, char *name)
  95. {
  96. struct ifreq xx;
  97. int n;
  98. snprintf(xx.ifr_name, sizeof xx.ifr_name, "%s", name);
  99. n = ioctl(s, SIOCGIFMTU, &xx);
  100. if (n == -1) {
  101. perror("Can't get mtu");
  102. return 1500;
  103. }
  104. return xx.ifr_mtu;
  105. }
  106. int
  107. getsec(int fd, uchar *place, vlong lba, int nsec)
  108. {
  109. return pread(fd, place, nsec * 512, lba * 512);
  110. }
  111. int
  112. putsec(int fd, uchar *place, vlong lba, int nsec)
  113. {
  114. return pwrite(fd, place, nsec * 512, lba * 512);
  115. }
  116. int
  117. getpkt(int fd, uchar *buf, int sz)
  118. {
  119. return read(fd, buf, sz);
  120. }
  121. int
  122. putpkt(int fd, uchar *buf, int sz)
  123. {
  124. return write(fd, buf, sz);
  125. }
  126. vlong
  127. getsize(int fd)
  128. {
  129. vlong size;
  130. struct stat s;
  131. int n;
  132. n = ioctl(fd, BLKGETSIZE64, &size);
  133. if (n == -1) { // must not be a block special
  134. n = fstat(fd, &s);
  135. if (n == -1) {
  136. perror("getsize");
  137. exit(1);
  138. }
  139. size = s.st_size;
  140. }
  141. return size;
  142. }