freebsd.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2005, Stacey Son <sson (at) verio (dot) net>
  3. * All rights reserved.
  4. */
  5. // freebsd.c: low level access routines for FreeBSD
  6. #include "config.h"
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <sys/time.h>
  14. #include <netinet/in.h>
  15. #include <net/ethernet.h>
  16. #include <net/bpf.h>
  17. #include <net/if.h>
  18. #include <net/if_arp.h>
  19. #include <net/if_dl.h>
  20. #include <net/route.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/types.h>
  23. #include <net/if.h>
  24. #include <sys/stat.h>
  25. #include <sys/disklabel.h>
  26. #include <sys/select.h>
  27. #include <sys/sysctl.h>
  28. #include <fcntl.h>
  29. #include <errno.h>
  30. #include "dat.h"
  31. #include "fns.h"
  32. #define BPF_DEV "/dev/bpf0"
  33. /* Packet buffer for getpkt() */
  34. static uchar *pktbuf = NULL;
  35. static int pktbufsz = 0;
  36. int
  37. dial(char *eth)
  38. {
  39. char m;
  40. int fd = -1;
  41. struct bpf_version bv;
  42. u_int v;
  43. unsigned bufsize, linktype;
  44. char device[sizeof BPF_DEV];
  45. struct ifreq ifr;
  46. /* packet filter for bpf */
  47. struct bpf_insn bpf_insns[] = {
  48. /* Load the type into register */
  49. BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
  50. /* Does it match AoE Type (0x88a2)? No, goto INVALID */
  51. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x88a2, 0, 10),
  52. /* Load the flags into register */
  53. BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 14),
  54. /* Check to see if the Resp flag is set */
  55. BPF_STMT(BPF_ALU+BPF_AND+BPF_K, Resp),
  56. /* Yes, goto INVALID */
  57. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0, 0, 7),
  58. /* Load the command into register */
  59. BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 19),
  60. /* Is this a ATAcmd? No, goto VALID */
  61. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ATAcmd, 0, 4),
  62. /* Load the shelf number into register */
  63. BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 16),
  64. /* Does it match shelf number? No, goto INVALID */
  65. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (u_int) shelf, 0, 3),
  66. /* Load the slot number into register */
  67. BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 18),
  68. /* Does it match shelf number? No, goto INVALID */
  69. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (u_int) slot, 0, 1),
  70. /* VALID: return -1 (allow the packet to be read) */
  71. BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
  72. /* INVALID: return 0 (ignore the packet) */
  73. BPF_STMT(BPF_RET+BPF_K, 0),
  74. };
  75. struct bpf_program bpf_program = {
  76. sizeof(bpf_insns)/sizeof(struct bpf_insn),
  77. bpf_insns
  78. };
  79. strncpy(device, BPF_DEV, sizeof BPF_DEV);
  80. /* find a bpf device we can use, check /dev/bpf[0-9] */
  81. for (m = '0'; m <= '9'; m++) {
  82. device[sizeof(BPF_DEV)-2] = m;
  83. if ((fd = open(device, O_RDWR)) > 0)
  84. break;
  85. }
  86. if (fd < 0) {
  87. perror("open");
  88. return -1;
  89. }
  90. if (ioctl(fd, BIOCVERSION, &bv) < 0) {
  91. perror("BIOCVERSION");
  92. goto bad;
  93. }
  94. if (bv.bv_major != BPF_MAJOR_VERSION ||
  95. bv.bv_minor < BPF_MINOR_VERSION) {
  96. fprintf(stderr,
  97. "kernel bpf filter out of date\n");
  98. goto bad;
  99. }
  100. /*
  101. * Try finding a good size for the buffer; 65536 may be too
  102. * big, so keep cutting it in half until we find a size
  103. * that works, or run out of sizes to try.
  104. *
  105. */
  106. for (v = 65536; v != 0; v >>= 1) {
  107. (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
  108. (void)strncpy(ifr.ifr_name, eth,
  109. sizeof(ifr.ifr_name));
  110. if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
  111. break; /* that size worked; we're done */
  112. if (errno != ENOBUFS) {
  113. fprintf(stderr, "BIOCSETIF: %s: %s\n",
  114. eth, strerror(errno));
  115. goto bad;
  116. }
  117. }
  118. if (v == 0) {
  119. fprintf(stderr,
  120. "BIOCSBLEN: %s: No buffer size worked\n", eth);
  121. goto bad;
  122. }
  123. /* Allocate memory for the packet buffer */
  124. pktbufsz = v;
  125. if ((pktbuf = malloc(pktbufsz)) == NULL) {
  126. perror("malloc");
  127. goto bad;
  128. }
  129. /* Don't wait for buffer to be full or timeout */
  130. v = 1;
  131. if (ioctl(fd, BIOCIMMEDIATE, &v) < 0) {
  132. perror("BIOCIMMEDIATE");
  133. goto bad;
  134. }
  135. /* Only read incoming packets */
  136. v = 0;
  137. if (ioctl(fd, BIOCSSEESENT, &v) < 0) {
  138. perror("BIOCSSEESENT");
  139. goto bad;
  140. }
  141. /* Don't complete ethernet hdr */
  142. v = 1;
  143. if (ioctl(fd, BIOCSHDRCMPLT, &v) < 0) {
  144. perror("BIOCSHDRCMPLT");
  145. goto bad;
  146. }
  147. /* Get the data link layer type. */
  148. if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
  149. perror("BIOCGDLT");
  150. goto bad;
  151. }
  152. linktype = v;
  153. /* Get the filter buf size */
  154. if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
  155. perror("BIOCGBLEN");
  156. goto bad;
  157. }
  158. bufsize = v;
  159. if (ioctl(fd, BIOCSETF, (caddr_t)&bpf_program) < 0) {
  160. perror("BIOSETF");
  161. goto bad;
  162. }
  163. return(fd);
  164. bad:
  165. close(fd);
  166. return(-1);
  167. }
  168. int
  169. getea(int s, char *eth, uchar *ea)
  170. {
  171. int mib[6];
  172. size_t len;
  173. char *buf, *next, *end;
  174. struct if_msghdr *ifm;
  175. struct sockaddr_dl *sdl;
  176. mib[0] = CTL_NET; mib[1] = AF_ROUTE;
  177. mib[2] = 0; mib[3] = AF_LINK;
  178. mib[4] = NET_RT_IFLIST; mib[5] = 0;
  179. if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
  180. return (-1);
  181. }
  182. if (!(buf = (char *) malloc(len))) {
  183. return (-1);
  184. }
  185. if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
  186. free(buf);
  187. return (-1);
  188. }
  189. end = buf + len;
  190. for (next = buf; next < end; next += ifm->ifm_msglen) {
  191. ifm = (struct if_msghdr *)next;
  192. if (ifm->ifm_type == RTM_IFINFO) {
  193. sdl = (struct sockaddr_dl *)(ifm + 1);
  194. if (strncmp(&sdl->sdl_data[0], eth,
  195. sdl->sdl_nlen) == 0) {
  196. memcpy(ea, LLADDR(sdl), ETHER_ADDR_LEN);
  197. break;
  198. }
  199. }
  200. }
  201. free(buf);
  202. return(0);
  203. }
  204. int
  205. getsec(int fd, uchar *place, vlong lba, int nsec)
  206. {
  207. return pread(fd, place, nsec * 512, lba * 512);
  208. }
  209. int
  210. putsec(int fd, uchar *place, vlong lba, int nsec)
  211. {
  212. return pwrite(fd, place, nsec * 512, lba * 512);
  213. }
  214. static int pktn = 0;
  215. static uchar *pktbp = NULL;
  216. int
  217. getpkt(int fd, uchar *buf, int sz)
  218. {
  219. register struct bpf_hdr *bh;
  220. register int pktlen, retlen;
  221. if (pktn <= 0) {
  222. if ((pktn = read(fd, pktbuf, pktbufsz)) < 0) {
  223. perror("read");
  224. exit(1);
  225. }
  226. pktbp = pktbuf;
  227. }
  228. bh = (struct bpf_hdr *) pktbp;
  229. retlen = (int) bh->bh_caplen;
  230. /* This memcpy() is currently needed */
  231. memcpy(buf, (void *)(pktbp + bh->bh_hdrlen),
  232. retlen > sz ? sz : retlen);
  233. pktlen = bh->bh_hdrlen + bh->bh_caplen;
  234. pktbp = pktbp + BPF_WORDALIGN(pktlen);
  235. pktn -= (int) BPF_WORDALIGN(pktlen);
  236. return retlen;
  237. }
  238. int
  239. putpkt(int fd, uchar *buf, int sz)
  240. {
  241. return write(fd, buf, sz);
  242. }
  243. vlong
  244. getsize(int fd)
  245. {
  246. vlong size;
  247. struct stat s;
  248. int n;
  249. struct disklabel lab;
  250. // Try getting disklabel from block dev
  251. if ((n = ioctl(fd, DIOCGDINFO, lab)) != -1) {
  252. size = lab.d_secsize * lab.d_secperunit;
  253. } else {
  254. // must not be a block special dev
  255. if (fstat(fd, &s) == -1) {
  256. perror("getsize");
  257. exit(1);
  258. }
  259. size = s.st_size;
  260. }
  261. printf("ioctl returned %d\n", n);
  262. printf("%lld bytes\n", size);
  263. return size;
  264. }