28 #include <freeradius-devel/util/pcap.h>
43 uint8_t dhcp_packet[1518] = { 0 };
63 ip_hdr->
ip_len = htons(IP_HDR_SIZE + UDP_HDR_SIZE + packet->
data_len);
70 ip_hdr->ip_src.s_addr = packet->
socket.inet.src_ipaddr.addr.v4.s_addr;
71 ip_hdr->
ip_dst.s_addr = packet->
socket.inet.dst_ipaddr.addr.v4.s_addr;
80 udp_hdr->
src = htons(packet->
socket.inet.src_port);
81 udp_hdr->
dst = htons(packet->
socket.inet.dst_port);
82 l4_len = (UDP_HDR_SIZE + packet->
data_len);
83 udp_hdr->
len = htons(l4_len);
94 packet->
socket.inet.src_ipaddr.addr.v4,
95 packet->
socket.inet.dst_ipaddr.addr.v4);
97 ret = pcap_inject(pcap->handle, dhcp_packet, (end - dhcp_packet + packet->
data_len));
99 fr_strerror_printf(
"Error sending packet with pcap: %d, %s", ret, pcap_geterr(pcap->handle));
121 struct pcap_pkthdr *header;
134 ret = pcap_next_ex(pcap->handle, &header, &
data);
140 fr_strerror_printf(
"Error requesting next packet, got (%i): %s", ret, pcap_geterr(pcap->handle));
144 link_len = fr_pcap_link_layer_offset(
data, header->caplen, pcap->link_layer);
155 version = (p[0] & 0xf0) >> 4;
159 len = (0x0f & ip->
ip_vhl) * 4;
173 if (ip->
ip_p != IPPROTO_UDP) {
175 IPPROTO_UDP, ip->
ip_p);
182 len = (p -
data) + UDP_HDR_SIZE;
183 if ((
size_t) len > header->caplen) {
193 if (ret < 0)
return NULL;
198 data_len = ntohs(udp->
len);
200 dst_port = ntohs(udp->
dst);
201 src_port = ntohs(udp->
src);
203 src_ipaddr.
af = AF_INET;
204 src_ipaddr.
addr.v4 = ip->ip_src;
207 dst_ipaddr.
af = AF_INET;
212 if (!
fr_dhcpv4_ok(p, data_len, NULL, NULL))
return NULL;
215 if (!packet)
return NULL;
217 packet->
socket.inet.dst_port = dst_port;
218 packet->
socket.inet.src_port = src_port;
220 packet->
socket.inet.src_ipaddr = src_ipaddr;
221 packet->
socket.inet.dst_ipaddr = dst_ipaddr;
222 packet->
socket.inet.ifindex = pcap->ifindex;
224 packet->
data = talloc_memdup(packet, p, packet->
data_len);
Implementation of the DHCPv4 protocol.
fr_packet_t * fr_dhcpv4_packet_alloc(uint8_t const *data, ssize_t data_len)
uint8_t dst_addr[ETHER_ADDR_LEN]
uint8_t src_addr[ETHER_ADDR_LEN]
uint8_t prefix
Prefix length - Between 0-32 for IPv4 and 0-128 for IPv6.
uint32_t scope_id
A host may have multiple link-local interfaces the scope ID allows the application to specify which o...
union fr_ipaddr_t::@130 addr
int fr_udp_header_check(uint8_t const *data, uint16_t remaining, ip_header_t const *ip)
Check UDP header is valid.
uint16_t fr_udp_checksum(uint8_t const *data, uint16_t len, uint16_t checksum, struct in_addr const src_addr, struct in_addr const dst_addr)
Calculate UDP checksum.
uint16_t fr_ip_header_checksum(uint8_t const *data, uint8_t ihl)
Calculate IP header checksum.
uint8_t ip_tos
Type of service.
uint16_t ip_len
Total length.
uint16_t ip_id
identification.
uint8_t ip_ttl
Time To Live.
uint16_t ip_off
Fragment offset field.
uint16_t dst
Destination port.
uint16_t checksum
UDP checksum.
struct in_addr ip_src ip_dst
Src and Dst address.
uint8_t ip_vhl
Header length, version.
bool fr_dhcpv4_ok(uint8_t const *data, ssize_t data_len, uint8_t *message_type, uint32_t *xid)
Check received DHCP request is valid and build fr_packet_t structure if it is.
static fr_time_t fr_time_from_timeval(struct timeval const *when_tv)
Convert a timeval (wallclock time) to a fr_time_t (internal time)
fr_socket_t socket
This packet was received on.
uint8_t * data
Packet data (body).
size_t data_len
Length of packet data.
fr_time_t timestamp
When we received the packet.
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
#define fr_strerror_const_push(_msg)
#define fr_strerror_const(_msg)