PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/sbin/dhclient/bpf.c

https://gitlab.com/tlevine/DragonFlyBSD
C | 421 lines | 212 code | 66 blank | 143 comment | 39 complexity | 35af6d6e21c80733bb6ea571c45a6c91 MD5 | raw file
  1. /* $OpenBSD: bpf.c,v 1.20 2007/01/08 02:51:13 krw Exp $ */
  2. /* $DragonFly: src/sbin/dhclient/bpf.c,v 1.2 2008/11/05 14:08:41 sephe Exp $ */
  3. /* BPF socket interface code, originally contributed by Archie Cobbs. */
  4. /*
  5. * Copyright (c) 1995, 1996, 1998, 1999
  6. * The Internet Software Consortium. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of The Internet Software Consortium nor the names
  18. * of its contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
  22. * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  23. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  24. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
  26. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  29. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  30. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  32. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * This software has been written for the Internet Software Consortium
  36. * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
  37. * Enterprises. To learn more about the Internet Software Consortium,
  38. * see ``http://www.vix.com/isc''. To learn more about Vixie
  39. * Enterprises, see ``http://www.vix.com''.
  40. */
  41. #include <sys/ioctl.h>
  42. #include <sys/uio.h>
  43. #include <net/bpf.h>
  44. #include <netinet/if_ether.h>
  45. #include <netinet/in_systm.h>
  46. #include <netinet/ip.h>
  47. #include <netinet/udp.h>
  48. #include "dhcpd.h"
  49. /*
  50. * Called by get_interface_list for each interface that's discovered.
  51. * Opens a packet filter for each interface and adds it to the select
  52. * mask.
  53. */
  54. int
  55. if_register_bpf(void)
  56. {
  57. char filename[50];
  58. int sock, b;
  59. /*
  60. * Open a BPF device. Try auto-clone first (newer kernels),
  61. * otherwise iterate (older kernels).
  62. */
  63. sock = open("/dev/bpf", O_RDWR, 0);
  64. if (sock < 0) {
  65. for (b = 0; 1; b++) {
  66. snprintf(filename, sizeof(filename), "/dev/bpf%d", b);
  67. sock = open(filename, O_RDWR, 0);
  68. if (sock < 0) {
  69. if (errno == EBUSY)
  70. continue;
  71. else
  72. error("Can't find free bpf: %m");
  73. } else {
  74. break;
  75. }
  76. }
  77. }
  78. /* Set the BPF device to point at this interface. */
  79. if (ioctl(sock, BIOCSETIF, ifi->ifp) < 0)
  80. error("Can't attach interface %s to bpf device %s: %m",
  81. ifi->name, filename);
  82. return (sock);
  83. }
  84. void
  85. if_register_send(void)
  86. {
  87. int sock, on = 1;
  88. /*
  89. * If we're using the bpf API for sending and receiving, we
  90. * don't need to register this interface twice.
  91. */
  92. ifi->wfdesc = ifi->rfdesc;
  93. /*
  94. * Use raw socket for unicast send.
  95. */
  96. if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) == -1)
  97. error("socket(SOCK_RAW): %m");
  98. if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on,
  99. sizeof(on)) == -1)
  100. error("setsockopt(IP_HDRINCL): %m");
  101. ifi->ufdesc = sock;
  102. }
  103. /*
  104. * Packet filter program...
  105. *
  106. * XXX: Changes to the filter program may require changes to the
  107. * constant offsets used in if_register_send to patch the BPF program!
  108. */
  109. struct bpf_insn dhcp_bpf_filter[] = {
  110. /* Make sure this is an IP packet... */
  111. BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
  112. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
  113. /* Make sure it's a UDP packet... */
  114. BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
  115. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
  116. /* Make sure this isn't a fragment... */
  117. BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
  118. BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
  119. /* Get the IP header length... */
  120. BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
  121. /* Make sure it's to the right port... */
  122. BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
  123. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), /* patch */
  124. /* If we passed all the tests, ask for the whole packet. */
  125. BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
  126. /* Otherwise, drop it. */
  127. BPF_STMT(BPF_RET+BPF_K, 0),
  128. };
  129. int dhcp_bpf_filter_len = sizeof(dhcp_bpf_filter) / sizeof(struct bpf_insn);
  130. /*
  131. * Packet write filter program:
  132. * 'ip and udp and src port bootps and dst port (bootps or bootpc)'
  133. */
  134. struct bpf_insn dhcp_bpf_wfilter[] = {
  135. BPF_STMT(BPF_LD + BPF_B + BPF_IND, 14),
  136. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (IPVERSION << 4) + 5, 0, 12),
  137. /* Make sure this is an IP packet... */
  138. BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
  139. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 10),
  140. /* Make sure it's a UDP packet... */
  141. BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
  142. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 8),
  143. /* Make sure this isn't a fragment... */
  144. BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
  145. BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 6, 0), /* patched */
  146. /* Get the IP header length... */
  147. BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
  148. /* Make sure it's from the right port... */
  149. BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14),
  150. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 68, 0, 3),
  151. /* Make sure it is to the right ports ... */
  152. BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
  153. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1),
  154. /* If we passed all the tests, ask for the whole packet. */
  155. BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
  156. /* Otherwise, drop it. */
  157. BPF_STMT(BPF_RET+BPF_K, 0),
  158. };
  159. int dhcp_bpf_wfilter_len = sizeof(dhcp_bpf_wfilter) / sizeof(struct bpf_insn);
  160. void
  161. if_register_receive(void)
  162. {
  163. struct bpf_version v;
  164. struct bpf_program p;
  165. int flag = 1, sz;
  166. /* Open a BPF device and hang it on this interface... */
  167. ifi->rfdesc = if_register_bpf();
  168. /* Make sure the BPF version is in range... */
  169. if (ioctl(ifi->rfdesc, BIOCVERSION, &v) < 0)
  170. error("Can't get BPF version: %m");
  171. if (v.bv_major != BPF_MAJOR_VERSION ||
  172. v.bv_minor < BPF_MINOR_VERSION)
  173. error("Kernel BPF version out of range - recompile dhcpd!");
  174. /*
  175. * Set immediate mode so that reads return as soon as a packet
  176. * comes in, rather than waiting for the input buffer to fill
  177. * with packets.
  178. */
  179. if (ioctl(ifi->rfdesc, BIOCIMMEDIATE, &flag) < 0)
  180. error("Can't set immediate mode on bpf device: %m");
  181. /*if (ioctl(ifi->rfdesc, BIOCSFILDROP, &flag) < 0)
  182. error("Can't set filter-drop mode on bpf device: %m");*/
  183. /* Get the required BPF buffer length from the kernel. */
  184. if (ioctl(ifi->rfdesc, BIOCGBLEN, &sz) < 0)
  185. error("Can't get bpf buffer length: %m");
  186. ifi->rbuf_max = sz;
  187. ifi->rbuf = malloc(ifi->rbuf_max);
  188. if (!ifi->rbuf)
  189. error("Can't allocate %lu bytes for bpf input buffer.",
  190. (unsigned long)ifi->rbuf_max);
  191. ifi->rbuf_offset = 0;
  192. ifi->rbuf_len = 0;
  193. /* Set up the bpf filter program structure. */
  194. p.bf_len = dhcp_bpf_filter_len;
  195. p.bf_insns = dhcp_bpf_filter;
  196. /* Patch the server port into the BPF program...
  197. *
  198. * XXX: changes to filter program may require changes to the
  199. * insn number(s) used below!
  200. */
  201. dhcp_bpf_filter[8].k = LOCAL_PORT;
  202. if (ioctl(ifi->rfdesc, BIOCSETF, &p) < 0)
  203. error("Can't install packet filter program: %m");
  204. /* Set up the bpf write filter program structure. */
  205. p.bf_len = dhcp_bpf_wfilter_len;
  206. p.bf_insns = dhcp_bpf_wfilter;
  207. if (dhcp_bpf_wfilter[7].k == 0x1fff)
  208. dhcp_bpf_wfilter[7].k = htons(IP_MF|IP_OFFMASK);
  209. if (ioctl(ifi->rfdesc, BIOCSETWF, &p) < 0)
  210. error("Can't install write filter program: %m");
  211. if (ioctl(ifi->rfdesc, BIOCLOCK, NULL) < 0)
  212. error("Cannot lock bpf");
  213. }
  214. ssize_t
  215. send_packet(struct in_addr from, struct sockaddr_in *to,
  216. struct hardware *hto)
  217. {
  218. #define IOVCNT 2
  219. unsigned char buf[256];
  220. struct iovec iov[IOVCNT];
  221. struct msghdr msg;
  222. int result, bufp = 0;
  223. if (to->sin_addr.s_addr == INADDR_BROADCAST) {
  224. assemble_hw_header(buf, &bufp, hto);
  225. }
  226. assemble_udp_ip_header(buf, &bufp, from.s_addr,
  227. to->sin_addr.s_addr, to->sin_port,
  228. (unsigned char *)&client->packet,
  229. client->packet_length);
  230. iov[0].iov_base = (char *)buf;
  231. iov[0].iov_len = bufp;
  232. iov[1].iov_base = (char *)&client->packet;
  233. iov[1].iov_len = client->packet_length;
  234. if (to->sin_addr.s_addr == INADDR_BROADCAST) {
  235. result = writev(ifi->wfdesc, iov, IOVCNT);
  236. } else {
  237. struct ip *ip = (struct ip *)buf;
  238. /*
  239. * DragonFly's raw socket expects ip_len/ip_off
  240. * in host byte order.
  241. */
  242. ip->ip_len = ntohs(ip->ip_len);
  243. ip->ip_off = ntohs(ip->ip_off);
  244. memset(&msg, 0, sizeof(msg));
  245. msg.msg_name = (struct sockaddr *)to;
  246. msg.msg_namelen = sizeof(*to);
  247. msg.msg_iov = iov;
  248. msg.msg_iovlen = IOVCNT;
  249. result = sendmsg(ifi->ufdesc, &msg, 0);
  250. }
  251. if (result == -1)
  252. warning("send_packet: %m");
  253. return (result);
  254. }
  255. ssize_t
  256. receive_packet(struct sockaddr_in *from, struct hardware *hfrom)
  257. {
  258. int length = 0, offset = 0;
  259. struct bpf_hdr hdr;
  260. /*
  261. * All this complexity is because BPF doesn't guarantee that
  262. * only one packet will be returned at a time. We're getting
  263. * what we deserve, though - this is a terrible abuse of the BPF
  264. * interface. Sigh.
  265. */
  266. /* Process packets until we get one we can return or until we've
  267. * done a read and gotten nothing we can return...
  268. */
  269. do {
  270. /* If the buffer is empty, fill it. */
  271. if (ifi->rbuf_offset >= ifi->rbuf_len) {
  272. length = read(ifi->rfdesc, ifi->rbuf, ifi->rbuf_max);
  273. if (length <= 0)
  274. return (length);
  275. ifi->rbuf_offset = 0;
  276. ifi->rbuf_len = BPF_WORDALIGN(length);
  277. }
  278. /*
  279. * If there isn't room for a whole bpf header, something
  280. * went wrong, but we'll ignore it and hope it goes
  281. * away... XXX
  282. */
  283. if (ifi->rbuf_len - ifi->rbuf_offset < sizeof(hdr)) {
  284. ifi->rbuf_offset = ifi->rbuf_len;
  285. continue;
  286. }
  287. /* Copy out a bpf header... */
  288. memcpy(&hdr, &ifi->rbuf[ifi->rbuf_offset], sizeof(hdr));
  289. /*
  290. * If the bpf header plus data doesn't fit in what's
  291. * left of the buffer, stick head in sand yet again...
  292. */
  293. if (ifi->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen >
  294. ifi->rbuf_len) {
  295. ifi->rbuf_offset = ifi->rbuf_len;
  296. continue;
  297. }
  298. /*
  299. * If the captured data wasn't the whole packet, or if
  300. * the packet won't fit in the input buffer, all we can
  301. * do is drop it.
  302. */
  303. if (hdr.bh_caplen != hdr.bh_datalen) {
  304. ifi->rbuf_offset = BPF_WORDALIGN(
  305. ifi->rbuf_offset + hdr.bh_hdrlen +
  306. hdr.bh_caplen);
  307. continue;
  308. }
  309. /* Skip over the BPF header... */
  310. ifi->rbuf_offset += hdr.bh_hdrlen;
  311. /* Decode the physical header... */
  312. offset = decode_hw_header(ifi->rbuf, ifi->rbuf_offset, hfrom);
  313. /*
  314. * If a physical layer checksum failed (dunno of any
  315. * physical layer that supports this, but WTH), skip
  316. * this packet.
  317. */
  318. if (offset < 0) {
  319. ifi->rbuf_offset = BPF_WORDALIGN(
  320. ifi->rbuf_offset + hdr.bh_caplen);
  321. continue;
  322. }
  323. ifi->rbuf_offset += offset;
  324. hdr.bh_caplen -= offset;
  325. /* Decode the IP and UDP headers... */
  326. offset = decode_udp_ip_header(ifi->rbuf,
  327. ifi->rbuf_offset, from, NULL, hdr.bh_caplen);
  328. /* If the IP or UDP checksum was bad, skip the packet... */
  329. if (offset < 0) {
  330. ifi->rbuf_offset = BPF_WORDALIGN(
  331. ifi->rbuf_offset + hdr.bh_caplen);
  332. continue;
  333. }
  334. ifi->rbuf_offset += offset;
  335. hdr.bh_caplen -= offset;
  336. /*
  337. * If there's not enough room to stash the packet data,
  338. * we have to skip it (this shouldn't happen in real
  339. * life, though).
  340. */
  341. if (hdr.bh_caplen > sizeof(client->packet)) {
  342. ifi->rbuf_offset = BPF_WORDALIGN(
  343. ifi->rbuf_offset + hdr.bh_caplen);
  344. continue;
  345. }
  346. /* Copy out the data in the packet... */
  347. memset(&client->packet, DHO_END, sizeof(client->packet));
  348. memcpy(&client->packet, ifi->rbuf + ifi->rbuf_offset,
  349. hdr.bh_caplen);
  350. ifi->rbuf_offset = BPF_WORDALIGN(ifi->rbuf_offset +
  351. hdr.bh_caplen);
  352. return (hdr.bh_caplen);
  353. } while (!length);
  354. return (0);
  355. }