freebsd.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/disk.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, int bufcnt)
  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. struct bpf_program *bpf_program = create_bpf_program(shelf, slot);
  47. strncpy(device, BPF_DEV, sizeof BPF_DEV);
  48. /* find a bpf device we can use, check /dev/bpf[0-9] */
  49. for (m = '0'; m <= '9'; m++) {
  50. device[sizeof(BPF_DEV)-2] = m;
  51. if ((fd = open(device, O_RDWR)) > 0)
  52. break;
  53. }
  54. if (fd < 0) {
  55. perror("open");
  56. return -1;
  57. }
  58. if (ioctl(fd, BIOCVERSION, &bv) < 0) {
  59. perror("BIOCVERSION");
  60. goto bad;
  61. }
  62. if (bv.bv_major != BPF_MAJOR_VERSION ||
  63. bv.bv_minor < BPF_MINOR_VERSION) {
  64. fprintf(stderr,
  65. "kernel bpf filter out of date\n");
  66. goto bad;
  67. }
  68. /*
  69. * Try finding a good size for the buffer; 65536 may be too
  70. * big, so keep cutting it in half until we find a size
  71. * that works, or run out of sizes to try.
  72. *
  73. */
  74. for (v = 65536; v != 0; v >>= 1) {
  75. (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
  76. (void)strncpy(ifr.ifr_name, eth,
  77. sizeof(ifr.ifr_name));
  78. if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
  79. break; /* that size worked; we're done */
  80. if (errno != ENOBUFS) {
  81. fprintf(stderr, "BIOCSETIF: %s: %s\n",
  82. eth, strerror(errno));
  83. goto bad;
  84. }
  85. }
  86. if (v == 0) {
  87. fprintf(stderr,
  88. "BIOCSBLEN: %s: No buffer size worked\n", eth);
  89. goto bad;
  90. }
  91. /* Allocate memory for the packet buffer */
  92. pktbufsz = v;
  93. if ((pktbuf = malloc(pktbufsz)) == NULL) {
  94. perror("malloc");
  95. goto bad;
  96. }
  97. /* Don't wait for buffer to be full or timeout */
  98. v = 1;
  99. if (ioctl(fd, BIOCIMMEDIATE, &v) < 0) {
  100. perror("BIOCIMMEDIATE");
  101. goto bad;
  102. }
  103. /* Only read incoming packets */
  104. v = 0;
  105. if (ioctl(fd, BIOCSSEESENT, &v) < 0) {
  106. perror("BIOCSSEESENT");
  107. goto bad;
  108. }
  109. /* Don't complete ethernet hdr */
  110. v = 1;
  111. if (ioctl(fd, BIOCSHDRCMPLT, &v) < 0) {
  112. perror("BIOCSHDRCMPLT");
  113. goto bad;
  114. }
  115. /* Get the data link layer type. */
  116. if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
  117. perror("BIOCGDLT");
  118. goto bad;
  119. }
  120. linktype = v;
  121. /* Get the filter buf size */
  122. if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
  123. perror("BIOCGBLEN");
  124. goto bad;
  125. }
  126. bufsize = v;
  127. if (ioctl(fd, BIOCSETF, (caddr_t)bpf_program) < 0) {
  128. perror("BIOSETF");
  129. goto bad;
  130. }
  131. free_bpf_program(bpf_program);
  132. return(fd);
  133. bad:
  134. free_bpf_program(bpf_program);
  135. close(fd);
  136. return(-1);
  137. }
  138. int
  139. getea(int s, char *eth, uchar *ea)
  140. {
  141. int mib[6];
  142. size_t len;
  143. char *buf, *next, *end;
  144. struct if_msghdr *ifm;
  145. struct sockaddr_dl *sdl;
  146. mib[0] = CTL_NET; mib[1] = AF_ROUTE;
  147. mib[2] = 0; mib[3] = AF_LINK;
  148. mib[4] = NET_RT_IFLIST; mib[5] = 0;
  149. if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
  150. return (-1);
  151. }
  152. if (!(buf = (char *) malloc(len))) {
  153. return (-1);
  154. }
  155. if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
  156. free(buf);
  157. return (-1);
  158. }
  159. end = buf + len;
  160. for (next = buf; next < end; next += ifm->ifm_msglen) {
  161. ifm = (struct if_msghdr *)next;
  162. if (ifm->ifm_type == RTM_IFINFO) {
  163. sdl = (struct sockaddr_dl *)(ifm + 1);
  164. if (strncmp(&sdl->sdl_data[0], eth,
  165. sdl->sdl_nlen) == 0) {
  166. memcpy(ea, LLADDR(sdl), ETHER_ADDR_LEN);
  167. break;
  168. }
  169. }
  170. }
  171. free(buf);
  172. return(0);
  173. }
  174. int
  175. getsec(int fd, uchar *place, vlong lba, int nsec)
  176. {
  177. return pread(fd, place, nsec * 512, lba * 512);
  178. }
  179. int
  180. putsec(int fd, uchar *place, vlong lba, int nsec)
  181. {
  182. return pwrite(fd, place, nsec * 512, lba * 512);
  183. }
  184. static int pktn = 0;
  185. static uchar *pktbp = NULL;
  186. int
  187. getpkt(int fd, uchar *buf, int sz)
  188. {
  189. register struct bpf_hdr *bh;
  190. register int pktlen, retlen;
  191. if (pktn <= 0) {
  192. if ((pktn = read(fd, pktbuf, pktbufsz)) < 0) {
  193. perror("read");
  194. exit(1);
  195. }
  196. pktbp = pktbuf;
  197. }
  198. bh = (struct bpf_hdr *) pktbp;
  199. retlen = (int) bh->bh_caplen;
  200. /* This memcpy() is currently needed */
  201. memcpy(buf, (void *)(pktbp + bh->bh_hdrlen),
  202. retlen > sz ? sz : retlen);
  203. pktlen = bh->bh_hdrlen + bh->bh_caplen;
  204. pktbp = pktbp + BPF_WORDALIGN(pktlen);
  205. pktn -= (int) BPF_WORDALIGN(pktlen);
  206. return retlen;
  207. }
  208. int
  209. putpkt(int fd, uchar *buf, int sz)
  210. {
  211. return write(fd, buf, sz);
  212. }
  213. int
  214. getmtu(int fd, char *name)
  215. {
  216. struct ifreq xx;
  217. int s, n, p;
  218. s = socket(AF_INET, SOCK_RAW, 0);
  219. if (s == -1) {
  220. perror("Can't get mtu");
  221. return 1500;
  222. }
  223. xx.ifr_addr.sa_family = AF_INET;
  224. snprintf(xx.ifr_name, sizeof xx.ifr_name, "%s", name);
  225. n = ioctl(s, SIOCGIFMTU, &xx);
  226. if (n == -1) {
  227. perror("Can't get mtu");
  228. return 1500;
  229. }
  230. close(s);
  231. // FreeBSD bpf writes are capped at one PAGESIZE'd mbuf. As such we must
  232. // limit our sector count. See FreeBSD PR 205164, OpenAoE/vblade #7.
  233. p = getpagesize();
  234. if (xx.ifr_mtu > p) {
  235. return p;
  236. }
  237. return xx.ifr_mtu;
  238. }
  239. vlong
  240. getsize(int fd)
  241. {
  242. off_t media_size;
  243. vlong size;
  244. struct stat s;
  245. int n;
  246. // Try getting disklabel from block dev
  247. if ((n = ioctl(fd, DIOCGMEDIASIZE, &media_size)) != -1) {
  248. size = media_size;
  249. } else {
  250. // must not be a block special dev
  251. if (fstat(fd, &s) == -1) {
  252. perror("getsize");
  253. exit(1);
  254. }
  255. size = s.st_size;
  256. }
  257. printf("ioctl returned %d\n", n);
  258. printf("%lld bytes\n", size);
  259. return size;
  260. }