Deleted Added
full compact
bpf.c (178232) bpf.c (198352)
1/* $OpenBSD: bpf.c,v 1.13 2004/05/05 14:28:58 deraadt Exp $ */
2
3/* BPF socket interface code, originally contributed by Archie Cobbs. */
4
5/*
6 * Copyright (c) 1995, 1996, 1998, 1999
7 * The Internet Software Consortium. All rights reserved.
8 *

--- 27 unchanged lines hidden (view full) ---

36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38 * Enterprises. To learn more about the Internet Software Consortium,
39 * see ``http://www.vix.com/isc''. To learn more about Vixie
40 * Enterprises, see ``http://www.vix.com''.
41 */
42
43#include <sys/cdefs.h>
1/* $OpenBSD: bpf.c,v 1.13 2004/05/05 14:28:58 deraadt Exp $ */
2
3/* BPF socket interface code, originally contributed by Archie Cobbs. */
4
5/*
6 * Copyright (c) 1995, 1996, 1998, 1999
7 * The Internet Software Consortium. All rights reserved.
8 *

--- 27 unchanged lines hidden (view full) ---

36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38 * Enterprises. To learn more about the Internet Software Consortium,
39 * see ``http://www.vix.com/isc''. To learn more about Vixie
40 * Enterprises, see ``http://www.vix.com''.
41 */
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: head/sbin/dhclient/bpf.c 178232 2008-04-15 22:48:56Z brooks $");
44__FBSDID("$FreeBSD: head/sbin/dhclient/bpf.c 198352 2009-10-21 23:50:35Z philip $");
45
46#include "dhcpd.h"
47#include <sys/ioctl.h>
48#include <sys/uio.h>
49
50#include <net/bpf.h>
51#include <netinet/in_systm.h>
52#include <netinet/ip.h>

--- 32 unchanged lines hidden (view full) ---

85 info->name, filename);
86
87 return (sock);
88}
89
90void
91if_register_send(struct interface_info *info)
92{
45
46#include "dhcpd.h"
47#include <sys/ioctl.h>
48#include <sys/uio.h>
49
50#include <net/bpf.h>
51#include <netinet/in_systm.h>
52#include <netinet/ip.h>

--- 32 unchanged lines hidden (view full) ---

85 info->name, filename);
86
87 return (sock);
88}
89
90void
91if_register_send(struct interface_info *info)
92{
93 int sock, on = 1;
94
93 /*
94 * If we're using the bpf API for sending and receiving, we
95 * don't need to register this interface twice.
96 */
97 info->wfdesc = info->rfdesc;
95 /*
96 * If we're using the bpf API for sending and receiving, we
97 * don't need to register this interface twice.
98 */
99 info->wfdesc = info->rfdesc;
100
101 /*
102 * Use raw socket for unicast send.
103 */
104 if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) == -1)
105 error("socket(SOCK_RAW): %m");
106 if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on,
107 sizeof(on)) == -1)
108 error("setsockopt(IP_HDRINCL): %m");
109 info->ufdesc = sock;
98}
99
100/*
101 * Packet filter program...
102 *
103 * XXX: Changes to the filter program may require changes to the
104 * constant offsets used in if_register_send to patch the BPF program!
105 */

--- 133 unchanged lines hidden (view full) ---

239
240ssize_t
241send_packet(struct interface_info *interface, struct dhcp_packet *raw,
242 size_t len, struct in_addr from, struct sockaddr_in *to,
243 struct hardware *hto)
244{
245 unsigned char buf[256];
246 struct iovec iov[2];
110}
111
112/*
113 * Packet filter program...
114 *
115 * XXX: Changes to the filter program may require changes to the
116 * constant offsets used in if_register_send to patch the BPF program!
117 */

--- 133 unchanged lines hidden (view full) ---

251
252ssize_t
253send_packet(struct interface_info *interface, struct dhcp_packet *raw,
254 size_t len, struct in_addr from, struct sockaddr_in *to,
255 struct hardware *hto)
256{
257 unsigned char buf[256];
258 struct iovec iov[2];
259 struct msghdr msg;
247 int result, bufp = 0;
260 int result, bufp = 0;
248 int sock;
249
261
250 if (to->sin_addr.s_addr != INADDR_BROADCAST) {
251 note("SENDING DIRECT");
252 /* We know who the server is, send the packet via
253 normal socket interface */
254
255 if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) >= 0) {
256 result = sendto (sock, (char *)raw, len, 0,
257 (struct sockaddr *)to, sizeof *to);
258 close(sock);
259 if (result > 0)
260 return result;
261 }
262 }
263
264 /* Assemble the headers... */
262 /* Assemble the headers... */
265 assemble_hw_header(interface, buf, &bufp, hto);
263 if (to->sin_addr.s_addr == INADDR_BROADCAST)
264 assemble_hw_header(interface, buf, &bufp, hto);
266 assemble_udp_ip_header(buf, &bufp, from.s_addr,
267 to->sin_addr.s_addr, to->sin_port, (unsigned char *)raw, len);
268
265 assemble_udp_ip_header(buf, &bufp, from.s_addr,
266 to->sin_addr.s_addr, to->sin_port, (unsigned char *)raw, len);
267
269 /* Fire it off */
270 iov[0].iov_base = (char *)buf;
271 iov[0].iov_len = bufp;
272 iov[1].iov_base = (char *)raw;
273 iov[1].iov_len = len;
274
268 iov[0].iov_base = (char *)buf;
269 iov[0].iov_len = bufp;
270 iov[1].iov_base = (char *)raw;
271 iov[1].iov_len = len;
272
275 result = writev(interface->wfdesc, iov, 2);
273 /* Fire it off */
274 if (to->sin_addr.s_addr == INADDR_BROADCAST)
275 result = writev(interface->wfdesc, iov, 2);
276 else {
277 memset(&msg, 0, sizeof(msg));
278 msg.msg_name = (struct sockaddr *)to;
279 msg.msg_namelen = sizeof(*to);
280 msg.msg_iov = iov;
281 msg.msg_iovlen = 2;
282 result = sendmsg(interface->ufdesc, &msg, 0);
283 }
284
276 if (result < 0)
277 warning("send_packet: %m");
278 return (result);
279}
280
281ssize_t
282receive_packet(struct interface_info *interface, unsigned char *buf,
283 size_t len, struct sockaddr_in *from, struct hardware *hfrom)

--- 119 unchanged lines hidden ---
285 if (result < 0)
286 warning("send_packet: %m");
287 return (result);
288}
289
290ssize_t
291receive_packet(struct interface_info *interface, unsigned char *buf,
292 size_t len, struct sockaddr_in *from, struct hardware *hfrom)

--- 119 unchanged lines hidden ---