[Dhcp-agent-commits] CVS: dhcp-agent dhcp-packet-build.c,NONE,1.1 COST,1.3,1.4 Makefile.am,1.8,1.9 M
Status: Alpha
Brought to you by:
actmodern
From: Thamer Al-H. <act...@us...> - 2002-02-15 02:00:28
|
Update of /cvsroot/dhcp-agent/dhcp-agent In directory usw-pr-cvs1:/tmp/cvs-serv31286 Modified Files: COST Makefile.am Makefile.in THANKS dhcp-agent.h dhcp-client-states.c dhcp-net.c Added Files: dhcp-packet-build.c Log Message: moved packet building routines to dhcp-packet-build.c --- NEW FILE: dhcp-packet-build.c --- /* $Header: /cvsroot/dhcp-agent/dhcp-agent/dhcp-packet-build.c,v 1.1 2002/02/15 02:00:24 actmodern Exp $ * * Copyright 2001 Thamer Alharbash <tm...@wh...> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * Packet building routines: * Utility routines wrapped around the packet objects. * */ #include <dhcp-agent.h> /* constants we need. */ static const eth_addr_t eth_null = { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }; static const eth_addr_t eth_broadcast = { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }; static const uint32_t ip_addr_broadcast = 0xffffffff; /* * * * * * * * * * * * * * * * Packet writing routines. * * * * * * * * * * * * * * * */ static void write_packet_dhcp(rawnet_t *net) { char *packet_ptr; /* Dump packet images for DHCP. */ packet_ptr = net->packet_data; memset(packet_ptr, 0, DEFAULT_MTU); /* fixme: fix the default mtu issue! */ eth_write_packet_image(net->ether_p, packet_ptr); packet_ptr += ETH_HDR_LEN; ip_write_packet_image(net->ip_p, packet_ptr); packet_ptr += (ip_get_hl(net->ip_p)); udp_write_packet_image(net->udp_p, packet_ptr); packet_ptr += UDP_HDR_LEN; dhcp_write_packet_image(net->dhcp_p, packet_ptr); /* Run the IP checksum routine. */ ip_checksum((net->packet_data + ETH_HDR_LEN), (net->packet_len - ETH_HDR_LEN)); return; } static void write_packet_arp(rawnet_t *net) { char *packet_ptr; /* Dump packet images for ARP/UNARP. */ packet_ptr = net->packet_data; memset(packet_ptr, 0, DEFAULT_MTU); /* fixme: fix the default mtu issue! */ eth_write_packet_image(net->ether_p, packet_ptr); packet_ptr += ETH_HDR_LEN; arp_write_packet_image(net->arp_p, packet_ptr); return; } static void write_packet_icmp(rawnet_t *net) { unsigned char *packet_ptr; /* Dump packet images for ICMP. */ packet_ptr = net->packet_data; memset(packet_ptr, 0, DEFAULT_MTU); /* fixme: fix the default mtu issue! */ eth_write_packet_image(net->ether_p, packet_ptr); packet_ptr += ETH_HDR_LEN; ip_write_packet_image(net->ip_p, packet_ptr); packet_ptr += (ip_get_hl(net->ip_p)); icmp_write_packet_image(net->icmp_p, packet_ptr); ip_checksum((net->packet_data + ETH_HDR_LEN), (net->packet_len - ETH_HDR_LEN)); return; } static void write_packet(rawnet_t *net) { switch(net->type) { case RAWNET_DHCP: write_packet_dhcp(net); break; case RAWNET_ARP: write_packet_arp(net); break; case RAWNET_ICMP: write_packet_icmp(net); break; default: warn_message("warning: invalid packet type passed to write_packet() -- this is a bug report it please."); break; } return; } /* * * * * * * * * * * * * Ethernet Routines. * * * * * * * * * * * * */ /* build ethernet header with broadcast destination address. */ static void build_eth_broadcast(rawnet_t *net, eth_addr_t client_hw_addr, uint16_t type) { eth_set_dst_address(net->ether_p, eth_broadcast); eth_set_src_address(net->ether_p, client_hw_addr); eth_set_type(net->ether_p, type); return; } /* * * * * * * * * * * * * ARP/UNARP Routines. * * * * * * * * * * * * */ /* Build ARP packet: write out all the members. */ static void build_arp(rawnet_t *net, uint16_t opcode, eth_addr_t sender_hw_addr, uint32_t sender_ip_addr, eth_addr_t target_hw_addr, uint32_t target_ip_addr, uint8_t hardware_len) /* for unarp it's 0 */ { arp_set_hardware_type(net->arp_p, ARP_HRD_ETH); arp_set_protocol_type(net->arp_p, ARP_PRO_IP); arp_set_hardware_len(net->arp_p, hardware_len); arp_set_protocol_len(net->arp_p, IP_ADDR_LEN); arp_set_op(net->arp_p, opcode); arp_set_sender_hardware_address(net->arp_p, sender_hw_addr); arp_set_sender_protocol_address(net->arp_p, sender_ip_addr); arp_set_target_hardware_address(net->arp_p, target_hw_addr); arp_set_target_protocol_address(net->arp_p, target_ip_addr); return; } /* Build ARP Proc: Do all the work for an arp packet. */ static void build_arp_proc(rawnet_t *net, uint16_t opcode, eth_addr_t sender_hw_addr, uint32_t sender_ip_addr, eth_addr_t target_hw_addr, uint32_t target_ip_addr) { build_eth_broadcast(net, sender_hw_addr, ETH_TYPE_ARP); build_arp(net, opcode, sender_hw_addr, sender_ip_addr, target_hw_addr, target_ip_addr, ETH_ADDR_LEN); net->type = RAWNET_ARP; net->packet_len = ETH_HDR_LEN + ARP_ETHIP_LEN + ARP_HDR_LEN; write_packet(net); return; } /* Interface routines to ARP/UNARP building. */ /* Build ARP reply. */ void build_arp_reply(rawnet_t *net, uint32_t source_addr, uint32_t dest_addr, eth_addr_t source_hw_addr, eth_addr_t dest_hw_addr) { build_arp_proc(net, ARP_OP_REPLY, source_hw_addr, source_addr, dest_hw_addr, dest_addr); return; } /* Build ARP reply with broadcast destination hardware address. */ void build_arp_reply_broadcast(rawnet_t *net, uint32_t source_addr, eth_addr_t source_hw_addr) { build_arp_reply(net, source_addr, ip_addr_broadcast, source_hw_addr, eth_broadcast); return; } /* only one way to build unarp, so don't procify it. */ void build_unarp(rawnet_t *net, uint32_t source_addr, eth_addr_t source_hw_addr) { build_eth_broadcast(net, source_hw_addr, ETH_TYPE_ARP); build_arp(net, ARP_OP_REPLY, eth_null, source_addr, eth_null, ip_addr_broadcast, 0); net->type = RAWNET_ARP; net->packet_len = ETH_HDR_LEN + ARP_ETHIP_LEN + ARP_HDR_LEN; write_packet(net); return; } /* * * * * * * * * * * * * * * * * Internet Protocol routines. * * * * * * * * * * * * * * * * */ static void build_ip_broadcast(rawnet_t *net, uint16_t ip_len, uint8_t protocol) { ip_set_hl(net->ip_p, IP_HDR_LEN); ip_set_tos(net->ip_p, IP_TOS_RELIABILITY); ip_set_len(net->ip_p, ip_len); ip_set_id(net->ip_p, IP_DF); ip_set_off(net->ip_p, 0); ip_set_ttl(net->ip_p, IP_TTL_MAX); ip_set_proto(net->ip_p, protocol); /* Do checksum later. */ ip_set_source_addr(net->ip_p, 0); ip_set_dest_addr(net->ip_p, ip_addr_broadcast); return; } /* * * * * * * * * * * UDP routines. * * * * * * * * * * */ static void build_udp(rawnet_t *net, uint16_t udp_len) { udp_set_src_port(net->udp_p, net->src_port); udp_set_dst_port(net->udp_p, net->dst_port); udp_set_len(net->udp_p, udp_len); /* libdnet sets this properly for us. */ udp_set_cksum(net->udp_p, 0); return; } /* * * * * * * * * * * DHCP routines. * * * * * * * * * * */ static void build_dhcp(rawnet_t *net, uint32_t xid, uint16_t secs, eth_addr_t client_hw_addr, uint32_t ciaddr, uint32_t yiaddr, uint32_t siaddr, uint32_t giaddr, list_t *options, unsigned char bootp_type) { dhcp_set_op(net->dhcp_p, bootp_type); dhcp_set_htype(net->dhcp_p, DLT_EN10MB); dhcp_set_hlen(net->dhcp_p, ETH_ADDR_LEN); dhcp_set_hops(net->dhcp_p, 0); dhcp_set_xid(net->dhcp_p, xid); dhcp_set_secs(net->dhcp_p, secs); dhcp_set_flag_broadcast(net->dhcp_p); dhcp_set_ciaddr(net->dhcp_p, ciaddr); dhcp_set_yiaddr(net->dhcp_p, yiaddr); dhcp_set_siaddr(net->dhcp_p, siaddr); dhcp_set_giaddr(net->dhcp_p, giaddr); dhcp_clear_chaddr(net->dhcp_p); dhcp_set_chaddr(net->dhcp_p, (unsigned char *)&client_hw_addr, ETH_ADDR_LEN); dhcp_clear_sname(net->dhcp_p); dhcp_clear_filename(net->dhcp_p); dhcp_set_magic_cookie(net->dhcp_p); dhcp_set_options(net->dhcp_p, options); return; } static void build_dhcp_proc(rawnet_t *net, uint32_t xid, time_t secs, uint32_t ciaddr, uint32_t yiaddr, uint32_t siaddr, uint32_t giaddr, list_t *options, unsigned char bootp_type) { uint16_t ip_len, udp_len; int total_len; dhcp_purge(net->dhcp_p); /* clear up old options. */ /* Calculate the total length of the packet * including IP header but not datalink header. * This goes into ip_len */ ip_len = IP_HDR_LEN + /* IP Header length. */ UDP_HDR_LEN + /* UDP Header length. */ DHCP_FIXEDHDR_LEN + /* DHCP fixed header. */ 4 + /* magic cookie length. */ dhcp_get_options_len(options); /* options length. */ udp_len = ip_len - IP_HDR_LEN; total_len = ip_len + ETH_HDR_LEN; /* It is possible that we exceeded MTU. * * On smaller MTUs (once we configure it and not * hardcode it) it may be possible. So keep * this check in. XXX -- fixme: check for * UDP size, and overload DHCP when it * goes over that size. * */ if(total_len > DEFAULT_MTU) fatal_error("Outgoing DHCP packet too large. I'm currently not implementing this properly so I'll have to exit!"); build_eth_broadcast(net, net->hw_addr, ETH_TYPE_IP); build_ip_broadcast(net, ip_len, IP_PROTO_UDP); build_udp(net, udp_len); build_dhcp(net, xid, secs, net->hw_addr, ciaddr, yiaddr, siaddr, giaddr, options, bootp_type); /* Write packet. */ net->type = RAWNET_DHCP; net->packet_len = total_len; write_packet(net); return; } /* Create a dhcp discover message. */ void build_dhcp_discover(rawnet_t *net, uint32_t xid, time_t secs, list_t *options) { build_dhcp_proc(net, xid, secs, 0, 0, 0, 0, options, DHCP_BOOTP_REQUEST); } /* Create dhcp request message. */ void build_dhcp_request(rawnet_t *net, uint32_t xid, time_t secs, list_t *options) { build_dhcp_proc(net, xid, secs, 0, 0, 0, 0, options, DHCP_BOOTP_REQUEST); } /* Create icmp packet: procify this so we can use it for all other icmp building. */ static void build_icmp_header_proc(rawnet_t *net, uint8_t icmp_type, uint8_t icmp_code) { icmp_set_type(net->icmp_p, icmp_type); icmp_set_code(net->icmp_p, icmp_code); icmp_clear_cksum(net->icmp_p); return; } /* Create icmp mask reply/request packet: procify this to act as * a subroutine for the icmp mask interface routines. */ static void build_icmp_mask_proc(rawnet_t *net, uint8_t icmp_type, uint32_t icmp_id, uint32_t icmp_seq, uint32_t mask) { build_icmp_header_proc(net, icmp_type, 0); icmp_mask_set_seq(net->icmp_p, icmp_id); icmp_mask_set_seq(net->icmp_p, icmp_seq); icmp_mask_set_mask(net->icmp_p, mask); return; } /* build icmp mask request packet. */ void build_icmp_mask_request(rawnet_t *net, uint32_t id, uint32_t seq) { int ip_len = ICMP_HDR_LEN + 12 + IP_HDR_LEN; /* 12 bytes for the mask request. */ build_eth_broadcast(net, net->hw_addr, ETH_TYPE_IP); build_ip_broadcast(net, ip_len, IP_PROTO_ICMP); build_icmp_mask_proc(net, ICMP_MASK, id, seq, 0); net->type = RAWNET_ICMP; net->packet_len = ETH_HDR_LEN + ip_len; write_packet(net); } /* build icmp mask reply packet. */ void build_icmp_mask_reply(rawnet_t *net, uint32_t id, uint32_t seq, uint32_t subnet_mask) { int ip_len = IP_HDR_LEN + ICMP_HDR_LEN + 12; /* 12 bytes for the mask reply. */ build_eth_broadcast(net, net->hw_addr, ETH_TYPE_IP); build_ip_broadcast(net, ip_len, IP_PROTO_ICMP); build_icmp_mask_proc(net, ICMP_MASK, id, seq, subnet_mask); net->type = RAWNET_ICMP; net->packet_len = ETH_HDR_LEN + ip_len; write_packet(net); } Index: COST =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/COST,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** COST 11 Feb 2002 18:02:24 -0000 1.3 --- COST 15 Feb 2002 02:00:24 -0000 1.4 *************** *** 3,9 **** Feb - March 2001 (Current values as of the Feb 9) ! 18 pots of coffee; 86 camel filter cigarettes; 8 diet cokes ! (cutting down); 845 mp3s (thanks stephanie); 12 historical trips ! to the wash room; 10 showers; one pretty geekette; Jan - Feb 2001 --- 3,10 ---- Feb - March 2001 (Current values as of the Feb 9) ! 24 pots of coffee; 132 camel filter cigarettes; 12 diet cokes ! (cutting down); 845 mp3s (thanks stephanie); 18 historical trips ! to the wash room; 16 showers; one pretty geekette; one job offer ! :-) Jan - Feb 2001 Index: Makefile.am =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/Makefile.am,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** Makefile.am 9 Feb 2002 15:50:30 -0000 1.8 --- Makefile.am 15 Feb 2002 02:00:24 -0000 1.9 *************** *** 20,24 **** dhcp-interface.c dhcp-client-conf.c @DHCP_SNPRINTF@ dhcp-files.c \ dhcp-client-states.c dhcp-options-strings.c dhcp-convert.c \ ! dhcp-sysconf.c dhcp-rtt.c dhcpclient_LDADD = @PCAP_LIB@ @DNET_LIB@ --- 20,24 ---- dhcp-interface.c dhcp-client-conf.c @DHCP_SNPRINTF@ dhcp-files.c \ dhcp-client-states.c dhcp-options-strings.c dhcp-convert.c \ ! dhcp-sysconf.c dhcp-rtt.c dhcp-packet-build.c dhcpclient_LDADD = @PCAP_LIB@ @DNET_LIB@ Index: Makefile.in =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/Makefile.in,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** Makefile.in 9 Feb 2002 15:50:30 -0000 1.8 --- Makefile.in 15 Feb 2002 02:00:24 -0000 1.9 *************** *** 83,87 **** dhcpsniff_LDADD = @PCAP_LIB@ @DNET_LIB@ ! dhcpclient_SOURCES = dhcp-client.c dhcp-util.c dhcp-align.c dhcp-net.c dhcp-list.c dhcp-com.c dhcp-eth.c dhcp-ip.c dhcp-udp.c dhcp-arp.c dhcp-icmp.c dhcp-log.c dhcp-daemon.c dhcp-client-cache.c dhcp-cache-entry.c dhcp-client-control.c dhcp-interface.c dhcp-client-conf.c @DHCP_SNPRINTF@ dhcp-files.c dhcp-client-states.c dhcp-options-strings.c dhcp-convert.c dhcp-sysconf.c dhcp-rtt.c --- 83,87 ---- dhcpsniff_LDADD = @PCAP_LIB@ @DNET_LIB@ ! dhcpclient_SOURCES = dhcp-client.c dhcp-util.c dhcp-align.c dhcp-net.c dhcp-list.c dhcp-com.c dhcp-eth.c dhcp-ip.c dhcp-udp.c dhcp-arp.c dhcp-icmp.c dhcp-log.c dhcp-daemon.c dhcp-client-cache.c dhcp-cache-entry.c dhcp-client-control.c dhcp-interface.c dhcp-client-conf.c @DHCP_SNPRINTF@ dhcp-files.c dhcp-client-states.c dhcp-options-strings.c dhcp-convert.c dhcp-sysconf.c dhcp-rtt.c dhcp-packet-build.c *************** *** 114,118 **** dhcp-cache-entry.o dhcp-client-control.o dhcp-interface.o \ dhcp-client-conf.o dhcp-files.o dhcp-client-states.o \ ! dhcp-options-strings.o dhcp-convert.o dhcp-sysconf.o dhcp-rtt.o dhcpclient_DEPENDENCIES = dhcpclient_LDFLAGS = --- 114,119 ---- dhcp-cache-entry.o dhcp-client-control.o dhcp-interface.o \ dhcp-client-conf.o dhcp-files.o dhcp-client-states.o \ ! dhcp-options-strings.o dhcp-convert.o dhcp-sysconf.o dhcp-rtt.o \ ! dhcp-packet-build.o dhcpclient_DEPENDENCIES = dhcpclient_LDFLAGS = *************** *** 142,148 **** .deps/dhcp-files.P .deps/dhcp-icmp.P .deps/dhcp-interface.P \ .deps/dhcp-ip.P .deps/dhcp-list.P .deps/dhcp-log.P .deps/dhcp-net.P \ ! .deps/dhcp-options-strings.P .deps/dhcp-print.P .deps/dhcp-rtt.P \ ! .deps/dhcp-sniff.P .deps/dhcp-sniffer-ohandlers.P .deps/dhcp-sysconf.P \ ! .deps/dhcp-udp.P .deps/dhcp-util.P SOURCES = $(dhcpsniff_SOURCES) $(dhcpclient_SOURCES) OBJECTS = $(dhcpsniff_OBJECTS) $(dhcpclient_OBJECTS) --- 143,150 ---- .deps/dhcp-files.P .deps/dhcp-icmp.P .deps/dhcp-interface.P \ .deps/dhcp-ip.P .deps/dhcp-list.P .deps/dhcp-log.P .deps/dhcp-net.P \ ! .deps/dhcp-options-strings.P .deps/dhcp-packet-build.P \ ! .deps/dhcp-print.P .deps/dhcp-rtt.P .deps/dhcp-sniff.P \ ! .deps/dhcp-sniffer-ohandlers.P .deps/dhcp-sysconf.P .deps/dhcp-udp.P \ ! .deps/dhcp-util.P SOURCES = $(dhcpsniff_SOURCES) $(dhcpclient_SOURCES) OBJECTS = $(dhcpsniff_OBJECTS) $(dhcpclient_OBJECTS) Index: THANKS =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/THANKS,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** THANKS 1 Feb 2002 00:42:03 -0000 1.2 --- THANKS 15 Feb 2002 02:00:24 -0000 1.3 *************** *** 12,13 **** --- 12,15 ---- -- The Tcpdump Group for their work on libpcap + + -- Bill Traynor for Solaris x86. Index: dhcp-agent.h =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-agent.h,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** dhcp-agent.h 14 Feb 2002 10:43:34 -0000 1.26 --- dhcp-agent.h 15 Feb 2002 02:00:24 -0000 1.27 *************** *** 489,502 **** extern void rawnet_use_fake_hw_addr(rawnet_t *raw, char *mac_string); ! extern void rawnet_build_dhcp_discover(rawnet_t *net, uint32_t xid, time_t secs, list_t *options); ! extern void rawnet_build_dhcp_request(rawnet_t *net, uint32_t xid, time_t secs, list_t *options); ! extern void rawnet_build_arp_reply_broadcast(rawnet_t *net, uint32_t source_addr, eth_addr_t source_hw_addr); ! extern void rawnet_build_arp_reply(rawnet_t *net, uint32_t source_addr, uint32_t dest_addr, eth_addr_t source_hw_addr, eth_addr_t dest_hw_addr); ! extern void rawnet_build_unarp(rawnet_t *net, uint32_t source_addr, eth_addr_t source_hw_addr); --- 489,502 ---- extern void rawnet_use_fake_hw_addr(rawnet_t *raw, char *mac_string); ! extern void build_dhcp_discover(rawnet_t *net, uint32_t xid, time_t secs, list_t *options); ! extern void build_dhcp_request(rawnet_t *net, uint32_t xid, time_t secs, list_t *options); ! extern void build_arp_reply_broadcast(rawnet_t *net, uint32_t source_addr, eth_addr_t source_hw_addr); ! extern void build_arp_reply(rawnet_t *net, uint32_t source_addr, uint32_t dest_addr, eth_addr_t source_hw_addr, eth_addr_t dest_hw_addr); ! extern void build_unarp(rawnet_t *net, uint32_t source_addr, eth_addr_t source_hw_addr); *************** *** 506,511 **** extern int rawnet_is_valid(rawnet_t *net); ! extern void rawnet_build_icmp_mask_request(rawnet_t *net, uint32_t id, uint32_t seq); ! extern void rawnet_build_icmp_mask_reply(rawnet_t *net, uint32_t id, uint32_t seq, uint32_t subnet_mask); extern int rawnet_up(rawnet_t *net); --- 506,511 ---- extern int rawnet_is_valid(rawnet_t *net); ! extern void build_icmp_mask_request(rawnet_t *net, uint32_t id, uint32_t seq); ! extern void build_icmp_mask_reply(rawnet_t *net, uint32_t id, uint32_t seq, uint32_t subnet_mask); extern int rawnet_up(rawnet_t *net); Index: dhcp-client-states.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-client-states.c,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** dhcp-client-states.c 12 Feb 2002 15:23:59 -0000 1.15 --- dhcp-client-states.c 15 Feb 2002 02:00:24 -0000 1.16 *************** *** 126,130 **** { ! rawnet_build_arp_reply_broadcast(dc->rawnet, client_ip_addr, client_hw_addr); rawnet_send_packet(dc->rawnet); --- 126,130 ---- { ! build_arp_reply_broadcast(dc->rawnet, client_ip_addr, client_hw_addr); rawnet_send_packet(dc->rawnet); *************** *** 135,139 **** static void client_broadcast_unarp(dhcp_client_control_t *dc) { ! rawnet_build_unarp(dc->rawnet, client_ip_addr, client_hw_addr); rawnet_send_packet(dc->rawnet); --- 135,139 ---- static void client_broadcast_unarp(dhcp_client_control_t *dc) { ! build_unarp(dc->rawnet, client_ip_addr, client_hw_addr); rawnet_send_packet(dc->rawnet); *************** *** 243,250 **** dhcp_client_update_secs(dc); ! /* Our options cleared by rawnet_build_* since they are passed * down and later purged. */ ! rawnet_build_dhcp_discover(dc->rawnet, dc->xid, dc->secs, options); if(client_transact(dc, 1, client_discover_check)) { --- 243,250 ---- dhcp_client_update_secs(dc); ! /* Our options cleared by build_* since they are passed * down and later purged. */ ! build_dhcp_discover(dc->rawnet, dc->xid, dc->secs, options); if(client_transact(dc, 1, client_discover_check)) { *************** *** 294,301 **** options = join_lists(options, cache_options); ! /* Our options cleared by rawnet_build_* since they are passed * down and later purged. */ ! rawnet_build_dhcp_request(dc->rawnet, dc->xid, dc->secs, options); if(client_transact(dc, 1, client_request_check)) { --- 294,301 ---- options = join_lists(options, cache_options); ! /* Our options cleared by build_* since they are passed * down and later purged. */ ! build_dhcp_request(dc->rawnet, dc->xid, dc->secs, options); if(client_transact(dc, 1, client_request_check)) { *************** *** 389,396 **** options = join_lists(options, cache_options); ! /* Our options cleared by rawnet_build_* since they are passed * down and later purged. */ ! rawnet_build_dhcp_request(dc->rawnet, dc->xid, dc->secs, options); if(rawnet_send_packet(dc->rawnet) < 0) { --- 389,396 ---- options = join_lists(options, cache_options); ! /* Our options cleared by build_* since they are passed * down and later purged. */ ! build_dhcp_request(dc->rawnet, dc->xid, dc->secs, options); if(rawnet_send_packet(dc->rawnet) < 0) { Index: dhcp-net.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-net.c,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** dhcp-net.c 14 Feb 2002 10:06:53 -0000 1.13 --- dhcp-net.c 15 Feb 2002 02:00:24 -0000 1.14 *************** *** 63,72 **** #include <dhcp-agent.h> - /* constants we need. */ - - static const eth_addr_t eth_null = { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }; - static const eth_addr_t eth_broadcast = { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }; - static const uint32_t ip_addr_broadcast = 0xffffffff; - /* Get port number for named service. */ --- 63,66 ---- *************** *** 413,783 **** } - /* Build routines. - * Convenient utility routines - * to build the kind of packets - * we need. */ - - static void build_arp(rawnet_t *net, - uint16_t opcode, - eth_addr_t sender_hw_addr, - uint32_t sender_ip_addr, - eth_addr_t target_hw_addr, - uint32_t target_ip_addr, - uint8_t hardware_len) /* for unarp it's 0 */ - { - - arp_set_hardware_type(net->arp_p, ARP_HRD_ETH); - arp_set_protocol_type(net->arp_p, ARP_PRO_IP); - arp_set_hardware_len(net->arp_p, hardware_len); - arp_set_protocol_len(net->arp_p, IP_ADDR_LEN); - arp_set_op(net->arp_p, opcode); - arp_set_sender_hardware_address(net->arp_p, sender_hw_addr); - arp_set_sender_protocol_address(net->arp_p, sender_ip_addr); - arp_set_target_hardware_address(net->arp_p, target_hw_addr); - arp_set_target_protocol_address(net->arp_p, target_ip_addr); - - return; - } - - static void build_eth_broadcast(rawnet_t *net, - eth_addr_t client_hw_addr, - uint16_t type) - - { - eth_set_dst_address(net->ether_p, eth_broadcast); - eth_set_src_address(net->ether_p, client_hw_addr); - eth_set_type(net->ether_p, type); - - return; - } - - static void build_ip_broadcast(rawnet_t *net, uint16_t ip_len, uint8_t protocol) - { - ip_set_hl(net->ip_p, IP_HDR_LEN); - ip_set_tos(net->ip_p, IP_TOS_RELIABILITY); - ip_set_len(net->ip_p, ip_len); - ip_set_id(net->ip_p, IP_DF); - ip_set_off(net->ip_p, 0); - ip_set_ttl(net->ip_p, IP_TTL_MAX); - ip_set_proto(net->ip_p, protocol); - - /* Do checksum later. */ - - ip_set_source_addr(net->ip_p, 0); - ip_set_dest_addr(net->ip_p, ip_addr_broadcast); - - return; - } - - static void build_udp(rawnet_t *net, uint16_t udp_len) - { - - udp_set_src_port(net->udp_p, net->src_port); - udp_set_dst_port(net->udp_p, net->dst_port); - udp_set_len(net->udp_p, udp_len); - - /* libdnet sets this properly for us. */ - - udp_set_cksum(net->udp_p, 0); - - return; - } - - static void build_dhcp(rawnet_t *net, - uint32_t xid, - uint16_t secs, - eth_addr_t client_hw_addr, - uint32_t ciaddr, - uint32_t yiaddr, - uint32_t siaddr, - uint32_t giaddr, - list_t *options, - unsigned char bootp_type) - { - - dhcp_set_op(net->dhcp_p, bootp_type); - dhcp_set_htype(net->dhcp_p, DLT_EN10MB); - dhcp_set_hlen(net->dhcp_p, ETH_ADDR_LEN); - dhcp_set_hops(net->dhcp_p, 0); - dhcp_set_xid(net->dhcp_p, xid); - dhcp_set_secs(net->dhcp_p, secs); - - dhcp_set_flag_broadcast(net->dhcp_p); - dhcp_set_ciaddr(net->dhcp_p, ciaddr); - dhcp_set_yiaddr(net->dhcp_p, yiaddr); - dhcp_set_siaddr(net->dhcp_p, siaddr); - dhcp_set_giaddr(net->dhcp_p, giaddr); - - dhcp_clear_chaddr(net->dhcp_p); - dhcp_set_chaddr(net->dhcp_p, (unsigned char *)&client_hw_addr, ETH_ADDR_LEN); - - dhcp_clear_sname(net->dhcp_p); - dhcp_clear_filename(net->dhcp_p); - - dhcp_set_magic_cookie(net->dhcp_p); - dhcp_set_options(net->dhcp_p, options); - - return; - } - - static void rawnet_write_packet_dhcp(rawnet_t *net) - { - char *packet_ptr; - - /* Dump packet images for DHCP. */ - - packet_ptr = net->packet_data; - memset(packet_ptr, 0, DEFAULT_MTU); /* fixme: fix the default mtu issue! */ - - eth_write_packet_image(net->ether_p, packet_ptr); - packet_ptr += ETH_HDR_LEN; - - ip_write_packet_image(net->ip_p, packet_ptr); - packet_ptr += (ip_get_hl(net->ip_p)); - - udp_write_packet_image(net->udp_p, packet_ptr); - packet_ptr += UDP_HDR_LEN; - - dhcp_write_packet_image(net->dhcp_p, packet_ptr); - - /* Run the IP checksum routine. */ - - ip_checksum((net->packet_data + ETH_HDR_LEN), (net->packet_len - ETH_HDR_LEN)); - - return; - } - - static void rawnet_write_packet_arp(rawnet_t *net) - { - char *packet_ptr; - - /* Dump packet images for ARP/UNARP. */ - - packet_ptr = net->packet_data; - memset(packet_ptr, 0, DEFAULT_MTU); /* fixme: fix the default mtu issue! */ - - eth_write_packet_image(net->ether_p, packet_ptr); - packet_ptr += ETH_HDR_LEN; - - arp_write_packet_image(net->arp_p, packet_ptr); - - return; - } - - static void rawnet_write_packet_icmp(rawnet_t *net) - { - unsigned char *packet_ptr; - - /* Dump packet images for ICMP. */ - - packet_ptr = net->packet_data; - memset(packet_ptr, 0, DEFAULT_MTU); /* fixme: fix the default mtu issue! */ - - eth_write_packet_image(net->ether_p, packet_ptr); - packet_ptr += ETH_HDR_LEN; - - ip_write_packet_image(net->ip_p, packet_ptr); - packet_ptr += (ip_get_hl(net->ip_p)); - - icmp_write_packet_image(net->icmp_p, packet_ptr); - - ip_checksum((net->packet_data + ETH_HDR_LEN), (net->packet_len - ETH_HDR_LEN)); - - return; - } - - static void rawnet_write_packet(rawnet_t *net) - { - - switch(net->type) { - - case RAWNET_DHCP: - rawnet_write_packet_dhcp(net); - break; - - case RAWNET_ARP: - rawnet_write_packet_arp(net); - break; - - case RAWNET_ICMP: - rawnet_write_packet_icmp(net); - break; - - default: - warn_message("warning: invalid send type -- this is a bug report it please."); - break; - } - - return; - } - - - static void rawnet_build_dhcp_proc(rawnet_t *net, uint32_t xid, time_t secs, - uint32_t ciaddr, uint32_t yiaddr, uint32_t siaddr, - uint32_t giaddr, list_t *options, unsigned char bootp_type) - { - uint16_t ip_len, udp_len; - int total_len; - - dhcp_purge(net->dhcp_p); /* clear up old options. */ - - /* Calculate the total length of the packet - * including IP header but not datalink header. - * This goes into ip_len */ - - ip_len = IP_HDR_LEN + /* IP Header length. */ - UDP_HDR_LEN + /* UDP Header length. */ - DHCP_FIXEDHDR_LEN + /* DHCP fixed header. */ - 4 + /* magic cookie length. */ - dhcp_get_options_len(options); /* options length. */ - - udp_len = ip_len - IP_HDR_LEN; - total_len = ip_len + ETH_HDR_LEN; - - /* It is possible that we exceeded MTU. - * - * On smaller MTUs (once we configure it and not - * hardcode it) it may be possible. So keep - * this check in. XXX -- fixme: check for - * UDP size, and overload DHCP when it - * goes over that size. - * - */ - - if(total_len > DEFAULT_MTU) - fatal_error("Outgoing DHCP packet too large. I'm currently not implementing this properly so I'll have to exit!"); - - build_eth_broadcast(net, net->hw_addr, ETH_TYPE_IP); - build_ip_broadcast(net, ip_len, IP_PROTO_UDP); - build_udp(net, udp_len); - build_dhcp(net, xid, secs, net->hw_addr, ciaddr, yiaddr, siaddr, giaddr, options, bootp_type); - - /* Write packet. */ - - net->type = RAWNET_DHCP; - net->packet_len = total_len; - - rawnet_write_packet(net); - - return; - } - - static void build_arp_proc(rawnet_t *net, - uint16_t opcode, - eth_addr_t sender_hw_addr, - uint32_t sender_ip_addr, - eth_addr_t target_hw_addr, - uint32_t target_ip_addr) - { - build_eth_broadcast(net, sender_hw_addr, ETH_TYPE_ARP); - build_arp(net, opcode, sender_hw_addr, sender_ip_addr, target_hw_addr, - target_ip_addr, ETH_ADDR_LEN); - - net->type = RAWNET_ARP; - net->packet_len = ETH_HDR_LEN + ARP_ETHIP_LEN + ARP_HDR_LEN; - - rawnet_write_packet(net); - return; - } - - void rawnet_build_arp_reply(rawnet_t *net, - uint32_t source_addr, - uint32_t dest_addr, - eth_addr_t source_hw_addr, - eth_addr_t dest_hw_addr) - { - build_arp_proc(net, ARP_OP_REPLY, source_hw_addr, source_addr, dest_hw_addr, dest_addr); - return; - } - - void rawnet_build_arp_reply_broadcast(rawnet_t *net, uint32_t source_addr, eth_addr_t source_hw_addr) - { - - rawnet_build_arp_reply(net, source_addr, ip_addr_broadcast, - source_hw_addr, eth_broadcast); - return; - } - - /* only one way to build unarp, so don't procify it. */ - - void rawnet_build_unarp(rawnet_t *net, uint32_t source_addr, eth_addr_t source_hw_addr) - { - build_eth_broadcast(net, source_hw_addr, ETH_TYPE_ARP); - build_arp(net, ARP_OP_REPLY, eth_null, source_addr, eth_null, ip_addr_broadcast, 0); - - net->type = RAWNET_ARP; - net->packet_len = ETH_HDR_LEN + ARP_ETHIP_LEN + ARP_HDR_LEN; - - rawnet_write_packet(net); - - return; - } - - /* Create a dhcp discover message. */ - - void rawnet_build_dhcp_discover(rawnet_t *net, uint32_t xid, time_t secs, list_t *options) - { - rawnet_build_dhcp_proc(net, xid, secs, 0, 0, 0, 0, options, DHCP_BOOTP_REQUEST); - } - - /* Create dhcp request message. */ - - void rawnet_build_dhcp_request(rawnet_t *net, uint32_t xid, time_t secs, list_t *options) - { - rawnet_build_dhcp_proc(net, xid, secs, 0, 0, 0, 0, options, DHCP_BOOTP_REQUEST); - } - - /* Create icmp packet: procify this so we can use it for all other icmp building. */ - static void rawnet_build_icmp_header_proc(rawnet_t *net, uint8_t icmp_type, uint8_t icmp_code) - { - icmp_set_type(net->icmp_p, icmp_type); - icmp_set_code(net->icmp_p, icmp_code); - icmp_clear_cksum(net->icmp_p); - - return; - } - - /* Create icmp mask reply/request packet: procify this to act as - a subroutine for the icmp mask interface routines. */ - static void rawnet_build_icmp_mask_proc(rawnet_t *net, uint8_t icmp_type, uint32_t icmp_id, - uint32_t icmp_seq, uint32_t mask) - { - rawnet_build_icmp_header_proc(net, icmp_type, 0); - icmp_mask_set_seq(net->icmp_p, icmp_id); - icmp_mask_set_seq(net->icmp_p, icmp_seq); - icmp_mask_set_mask(net->icmp_p, mask); - - return; - } - - /* build icmp mask request packet. */ - void rawnet_build_icmp_mask_request(rawnet_t *net, uint32_t id, uint32_t seq) - { - int ip_len = ICMP_HDR_LEN + 12 + IP_HDR_LEN; /* 12 bytes for the mask request. */ - - build_eth_broadcast(net, net->hw_addr, ETH_TYPE_IP); - build_ip_broadcast(net, ip_len, IP_PROTO_ICMP); - rawnet_build_icmp_mask_proc(net, ICMP_MASK, id, seq, 0); - - net->type = RAWNET_ICMP; - net->packet_len = ETH_HDR_LEN + ip_len; - - rawnet_write_packet(net); - } - - /* build icmp mask reply packet. */ - void rawnet_build_icmp_mask_reply(rawnet_t *net, uint32_t id, uint32_t seq, uint32_t subnet_mask) - { - int ip_len = IP_HDR_LEN + ICMP_HDR_LEN + 12; /* 12 bytes for the mask reply. */ - - build_eth_broadcast(net, net->hw_addr, ETH_TYPE_IP); - build_ip_broadcast(net, ip_len, IP_PROTO_ICMP); - rawnet_build_icmp_mask_proc(net, ICMP_MASK, id, seq, subnet_mask); - - net->type = RAWNET_ICMP; - net->packet_len = ETH_HDR_LEN + ip_len; - - rawnet_write_packet(net); - } /* Just update the seconds field. --- 407,410 ---- |