aboutsummaryrefslogtreecommitdiff
path: root/ChangeLog.old-ports-am33
diff options
context:
space:
mode:
authorSiddhesh Poyarekar <siddhesh@sourceware.org>2016-12-31 23:32:17 +0530
committerSiddhesh Poyarekar <siddhesh@sourceware.org>2016-12-31 23:49:24 +0530
commit67e58f39412ecd4467034761f3f074283c90f3c8 (patch)
tree1498c4ac17e0606ba3f38ac59296793fb0cc1dd5 /ChangeLog.old-ports-am33
parentbbe989ee87ee81f45a4f6450bff468f7a193e79e (diff)
downloadglibc-67e58f39412ecd4467034761f3f074283c90f3c8.zip
glibc-67e58f39412ecd4467034761f3f074283c90f3c8.tar.gz
glibc-67e58f39412ecd4467034761f3f074283c90f3c8.tar.bz2
Add framework for tunables
The tunables framework allows us to uniformly manage and expose global variables inside glibc as switches to users. tunables/README has instructions for glibc developers to add new tunables. Tunables support can be enabled by passing the --enable-tunables configure flag to the configure script. This patch only adds a framework and does not pose any limitations on how tunable values are read from the user. It also adds environment variables used in malloc behaviour tweaking to the tunables framework as a PoC of the compatibility interface. * manual/install.texi: Add --enable-tunables option. * INSTALL: Regenerate. * README.tunables: New file. * Makeconfig (CPPFLAGS): Define TOP_NAMESPACE. (before-compile): Generate dl-tunable-list.h early. * config.h.in: Add HAVE_TUNABLES. * config.make.in: Add have-tunables. * configure.ac: Add --enable-tunables option. * configure: Regenerate. * csu/init-first.c (__libc_init_first): Move __libc_init_secure earlier... * csu/init-first.c (LIBC_START_MAIN):... to here. Include dl-tunables.h, libc-internal.h. (LIBC_START_MAIN) [!SHARED]: Initialize tunables for static binaries. * elf/Makefile (dl-routines): Add dl-tunables. * elf/Versions (ld): Add __tunable_set_val to GLIBC_PRIVATE namespace. * elf/dl-support (_dl_nondynamic_init): Unset MALLOC_CHECK_ only when !HAVE_TUNABLES. * elf/rtld.c (process_envvars): Likewise. * elf/dl-sysdep.c [HAVE_TUNABLES]: Include dl-tunables.h (_dl_sysdep_start): Call __tunables_init. * elf/dl-tunable-types.h: New file. * elf/dl-tunables.c: New file. * elf/dl-tunables.h: New file. * elf/dl-tunables.list: New file. * malloc/tst-malloc-usable-static.c: New test case. * malloc/Makefile (tests-static): Add it. * malloc/arena.c [HAVE_TUNABLES]: Include dl-tunables.h. Define TUNABLE_NAMESPACE. (DL_TUNABLE_CALLBACK (set_mallopt_check)): New function. (DL_TUNABLE_CALLBACK_FNDECL): New macro. Use it to define callback functions. (ptmalloc_init): Set tunable values. * scripts/gen-tunables.awk: New file. * sysdeps/mach/hurd/dl-sysdep.c: Include dl-tunables.h. (_dl_sysdep_start): Call __tunables_init.
Diffstat (limited to 'ChangeLog.old-ports-am33')
0 files changed, 0 insertions, 0 deletions
href='#n608'>608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
/*
 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
 * Copyright (C) 2006 Nikhil Chandru Rao
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/list.h>
#include <ipxe/in.h>
#include <ipxe/arp.h>
#include <ipxe/if_ether.h>
#include <ipxe/iobuf.h>
#include <ipxe/netdevice.h>
#include <ipxe/ip.h>
#include <ipxe/tcpip.h>
#include <ipxe/dhcp.h>
#include <ipxe/settings.h>
#include <ipxe/fragment.h>
#include <ipxe/ipstat.h>
#include <ipxe/profile.h>

/** @file
 *
 * IPv4 protocol
 *
 */

FILE_LICENCE ( GPL2_OR_LATER );

/* Unique IP datagram identification number (high byte) */
static uint8_t next_ident_high = 0;

/** List of IPv4 miniroutes */
struct list_head ipv4_miniroutes = LIST_HEAD_INIT ( ipv4_miniroutes );

/** IPv4 statistics */
static struct ip_statistics ipv4_stats;

/** IPv4 statistics family */
struct ip_statistics_family
ipv4_stats_family __ip_statistics_family ( IP_STATISTICS_IPV4 ) = {
	.version = 4,
	.stats = &ipv4_stats,
};

/** Transmit profiler */
static struct profiler ipv4_tx_profiler __profiler = { .name = "ipv4.tx" };

/** Receive profiler */
static struct profiler ipv4_rx_profiler __profiler = { .name = "ipv4.rx" };

/**
 * Add IPv4 minirouting table entry
 *
 * @v netdev		Network device
 * @v address		IPv4 address
 * @v netmask		Subnet mask
 * @v gateway		Gateway address (if any)
 * @ret miniroute	Routing table entry, or NULL
 */
static struct ipv4_miniroute * __malloc
add_ipv4_miniroute ( struct net_device *netdev, struct in_addr address,
		     struct in_addr netmask, struct in_addr gateway ) {
	struct ipv4_miniroute *miniroute;

	DBGC ( netdev, "IPv4 add %s", inet_ntoa ( address ) );
	DBGC ( netdev, "/%s ", inet_ntoa ( netmask ) );
	if ( gateway.s_addr )
		DBGC ( netdev, "gw %s ", inet_ntoa ( gateway ) );
	DBGC ( netdev, "via %s\n", netdev->name );

	/* Allocate and populate miniroute structure */
	miniroute = malloc ( sizeof ( *miniroute ) );
	if ( ! miniroute ) {
		DBGC ( netdev, "IPv4 could not add miniroute\n" );
		return NULL;
	}

	/* Record routing information */
	miniroute->netdev = netdev_get ( netdev );
	miniroute->address = address;
	miniroute->netmask = netmask;
	miniroute->gateway = gateway;
		
	/* Add to end of list if we have a gateway, otherwise
	 * to start of list.
	 */
	if ( gateway.s_addr ) {
		list_add_tail ( &miniroute->list, &ipv4_miniroutes );
	} else {
		list_add ( &miniroute->list, &ipv4_miniroutes );
	}

	return miniroute;
}

/**
 * Delete IPv4 minirouting table entry
 *
 * @v miniroute		Routing table entry
 */
static void del_ipv4_miniroute ( struct ipv4_miniroute *miniroute ) {
	struct net_device *netdev = miniroute->netdev;

	DBGC ( netdev, "IPv4 del %s", inet_ntoa ( miniroute->address ) );
	DBGC ( netdev, "/%s ", inet_ntoa ( miniroute->netmask ) );
	if ( miniroute->gateway.s_addr )
		DBGC ( netdev, "gw %s ", inet_ntoa ( miniroute->gateway ) );
	DBGC ( netdev, "via %s\n", miniroute->netdev->name );

	netdev_put ( miniroute->netdev );
	list_del ( &miniroute->list );
	free ( miniroute );
}

/**
 * Perform IPv4 routing
 *
 * @v dest		Final destination address
 * @ret dest		Next hop destination address
 * @ret miniroute	Routing table entry to use, or NULL if no route
 *
 * If the route requires use of a gateway, the next hop destination
 * address will be overwritten with the gateway address.
 */
static struct ipv4_miniroute * ipv4_route ( struct in_addr *dest ) {
	struct ipv4_miniroute *miniroute;
	int local;
	int has_gw;

	/* Find first usable route in routing table */
	list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
		if ( ! netdev_is_open ( miniroute->netdev ) )
			continue;
		local = ( ( ( dest->s_addr ^ miniroute->address.s_addr )
			    & miniroute->netmask.s_addr ) == 0 );
		has_gw = ( miniroute->gateway.s_addr );
		if ( local || has_gw ) {
			if ( ! local )
				*dest = miniroute->gateway;
			return miniroute;
		}
	}

	return NULL;
}

/**
 * Determine transmitting network device
 *
 * @v st_dest		Destination network-layer address
 * @ret netdev		Transmitting network device, or NULL
 */
static struct net_device * ipv4_netdev ( struct sockaddr_tcpip *st_dest ) {
	struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest );
	struct in_addr dest = sin_dest->sin_addr;
	struct ipv4_miniroute *miniroute;

	/* Find routing table entry */
	miniroute = ipv4_route ( &dest );
	if ( ! miniroute )
		return NULL;

	return miniroute->netdev;
}

/**
 * Check if IPv4 fragment matches fragment reassembly buffer
 *
 * @v fragment		Fragment reassembly buffer
 * @v iobuf		I/O buffer
 * @v hdrlen		Length of non-fragmentable potion of I/O buffer
 * @ret is_fragment	Fragment matches this reassembly buffer
 */
static int ipv4_is_fragment ( struct fragment *fragment,
			      struct io_buffer *iobuf,
			      size_t hdrlen __unused ) {
	struct iphdr *frag_iphdr = fragment->iobuf->data;
	struct iphdr *iphdr = iobuf->data;

	return ( ( iphdr->src.s_addr == frag_iphdr->src.s_addr ) &&
		 ( iphdr->ident == frag_iphdr->ident ) );
}

/**
 * Get IPv4 fragment offset
 *
 * @v iobuf		I/O buffer
 * @v hdrlen		Length of non-fragmentable potion of I/O buffer
 * @ret offset		Offset
 */
static size_t ipv4_fragment_offset ( struct io_buffer *iobuf,
				     size_t hdrlen __unused ) {
	struct iphdr *iphdr = iobuf->data;

	return ( ( ntohs ( iphdr->frags ) & IP_MASK_OFFSET ) << 3 );
}

/**
 * Check if more fragments exist
 *
 * @v iobuf		I/O buffer
 * @v hdrlen		Length of non-fragmentable potion of I/O buffer
 * @ret more_frags	More fragments exist
 */
static int ipv4_more_fragments ( struct io_buffer *iobuf,
				 size_t hdrlen __unused ) {
	struct iphdr *iphdr = iobuf->data;

	return ( iphdr->frags & htons ( IP_MASK_MOREFRAGS ) );
}

/** IPv4 fragment reassembler */
static struct fragment_reassembler ipv4_reassembler = {
	.list = LIST_HEAD_INIT ( ipv4_reassembler.list ),
	.is_fragment = ipv4_is_fragment,
	.fragment_offset = ipv4_fragment_offset,
	.more_fragments = ipv4_more_fragments,
	.stats = &ipv4_stats,
};

/**
 * Add IPv4 pseudo-header checksum to existing checksum
 *
 * @v iobuf		I/O buffer
 * @v csum		Existing checksum
 * @ret csum		Updated checksum
 */
static uint16_t ipv4_pshdr_chksum ( struct io_buffer *iobuf, uint16_t csum ) {
	struct ipv4_pseudo_header pshdr;
	struct iphdr *iphdr = iobuf->data;
	size_t hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );

	/* Build pseudo-header */
	pshdr.src = iphdr->src;
	pshdr.dest = iphdr->dest;
	pshdr.zero_padding = 0x00;
	pshdr.protocol = iphdr->protocol;
	pshdr.len = htons ( iob_len ( iobuf ) - hdrlen );

	/* Update the checksum value */
	return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
}

/**
 * Transmit IP packet
 *
 * @v iobuf		I/O buffer
 * @v tcpip		Transport-layer protocol
 * @v st_src		Source network-layer address
 * @v st_dest		Destination network-layer address
 * @v netdev		Network device to use if no route found, or NULL
 * @v trans_csum	Transport-layer checksum to complete, or NULL
 * @ret rc		Status
 *
 * This function expects a transport-layer segment and prepends the IP header
 */
static int ipv4_tx ( struct io_buffer *iobuf,
		     struct tcpip_protocol *tcpip_protocol,
		     struct sockaddr_tcpip *st_src,
		     struct sockaddr_tcpip *st_dest,
		     struct net_device *netdev,
		     uint16_t *trans_csum ) {
	struct iphdr *iphdr = iob_push ( iobuf, sizeof ( *iphdr ) );
	struct sockaddr_in *sin_src = ( ( struct sockaddr_in * ) st_src );
	struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest );
	struct ipv4_miniroute *miniroute;
	struct in_addr next_hop;
	struct in_addr netmask = { .s_addr = 0 };
	uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
	const void *ll_dest;
	int rc;

	/* Start profiling */
	profile_start ( &ipv4_tx_profiler );

	/* Update statistics */
	ipv4_stats.out_requests++;

	/* Fill up the IP header, except source address */
	memset ( iphdr, 0, sizeof ( *iphdr ) );
	iphdr->verhdrlen = ( IP_VER | ( sizeof ( *iphdr ) / 4 ) );
	iphdr->service = IP_TOS;
	iphdr->len = htons ( iob_len ( iobuf ) );	
	iphdr->ttl = IP_TTL;
	iphdr->protocol = tcpip_protocol->tcpip_proto;
	iphdr->dest = sin_dest->sin_addr;

	/* Use routing table to identify next hop and transmitting netdev */
	next_hop = iphdr->dest;
	if ( sin_src )
		iphdr->src = sin_src->sin_addr;
	if ( ( next_hop.s_addr != INADDR_BROADCAST ) &&
	     ( ! IN_MULTICAST ( ntohl ( next_hop.s_addr ) ) ) &&
	     ( ( miniroute = ipv4_route ( &next_hop ) ) != NULL ) ) {
		iphdr->src = miniroute->address;
		netmask = miniroute->netmask;
		netdev = miniroute->netdev;
	}
	if ( ! netdev ) {
		DBGC ( sin_dest->sin_addr, "IPv4 has no route to %s\n",
		       inet_ntoa ( iphdr->dest ) );
		ipv4_stats.out_no_routes++;
		rc = -ENETUNREACH;
		goto err;
	}

	/* (Ab)use the "ident" field to convey metadata about the
	 * network device statistics into packet traces.  Useful for
	 * extracting debug information from non-debug builds.
	 */
	iphdr->ident = htons ( ( (++next_ident_high) << 8 ) |
			       ( ( netdev->rx_stats.bad & 0xf ) << 4 ) |
			       ( ( netdev->rx_stats.good & 0xf ) << 0 ) );

	/* Fix up checksums */
	if ( trans_csum )
		*trans_csum = ipv4_pshdr_chksum ( iobuf, *trans_csum );
	iphdr->chksum = tcpip_chksum ( iphdr, sizeof ( *iphdr ) );

	/* Print IP4 header for debugging */
	DBGC2 ( sin_dest->sin_addr, "IPv4 TX %s->", inet_ntoa ( iphdr->src ) );
	DBGC2 ( sin_dest->sin_addr, "%s len %d proto %d id %04x csum %04x\n",
		inet_ntoa ( iphdr->dest ), ntohs ( iphdr->len ),
		iphdr->protocol, ntohs ( iphdr->ident ),
		ntohs ( iphdr->chksum ) );

	/* Calculate link-layer destination address, if possible */
	if ( ( ( next_hop.s_addr ^ INADDR_BROADCAST ) & ~netmask.s_addr ) == 0){
		/* Broadcast address */
		ipv4_stats.out_bcast_pkts++;
		ll_dest = netdev->ll_broadcast;
	} else if ( IN_MULTICAST ( ntohl ( next_hop.s_addr ) ) ) {
		/* Multicast address */
		ipv4_stats.out_mcast_pkts++;
		if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET, &next_hop,
							   ll_dest_buf ) ) !=0){
			DBGC ( sin_dest->sin_addr, "IPv4 could not hash "
			       "multicast %s: %s\n",
			       inet_ntoa ( next_hop ), strerror ( rc ) );
			goto err;
		}
		ll_dest = ll_dest_buf;
	} else {
		/* Unicast address */
		ll_dest = NULL;
	}

	/* Update statistics */
	ipv4_stats.out_transmits++;
	ipv4_stats.out_octets += iob_len ( iobuf );

	/* Hand off to link layer (via ARP if applicable) */
	if ( ll_dest ) {
		if ( ( rc = net_tx ( iobuf, netdev, &ipv4_protocol, ll_dest,
				     netdev->ll_addr ) ) != 0 ) {
			DBGC ( sin_dest->sin_addr, "IPv4 could not transmit "
			       "packet via %s: %s\n",
			       netdev->name, strerror ( rc ) );
			return rc;
		}
	} else {
		if ( ( rc = arp_tx ( iobuf, netdev, &ipv4_protocol, &next_hop,
				     &iphdr->src, netdev->ll_addr ) ) != 0 ) {
			DBGC ( sin_dest->sin_addr, "IPv4 could not transmit "
			       "packet via %s: %s\n",
			       netdev->name, strerror ( rc ) );
			return rc;
		}
	}

	profile_stop ( &ipv4_tx_profiler );
	return 0;

 err:
	free_iob ( iobuf );
	return rc;
}

/**
 * Check if network device has any IPv4 address
 *
 * @v netdev		Network device
 * @ret has_any_addr	Network device has any IPv4 address
 */
int ipv4_has_any_addr ( struct net_device *netdev ) {
	struct ipv4_miniroute *miniroute;

	list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
		if ( miniroute->netdev == netdev )
			return 1;
	}
	return 0;
}

/**
 * Check if network device has a specific IPv4 address
 *
 * @v netdev		Network device
 * @v addr		IPv4 address
 * @ret has_addr	Network device has this IPv4 address
 */
static int ipv4_has_addr ( struct net_device *netdev, struct in_addr addr ) {
	struct ipv4_miniroute *miniroute;

	list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
		if ( ( miniroute->netdev == netdev ) &&
		     ( miniroute->address.s_addr == addr.s_addr ) ) {
			/* Found matching address */
			return 1;
		}
	}
	return 0;
}

/**
 * Process incoming packets
 *
 * @v iobuf		I/O buffer
 * @v netdev		Network device
 * @v ll_dest		Link-layer destination address
 * @v ll_source		Link-layer destination source
 * @v flags		Packet flags
 * @ret rc		Return status code
 *
 * This function expects an IP4 network datagram. It processes the headers 
 * and sends it to the transport layer.
 */
static int ipv4_rx ( struct io_buffer *iobuf,
		     struct net_device *netdev,
		     const void *ll_dest __unused,
		     const void *ll_source __unused,
		     unsigned int flags ) {
	struct iphdr *iphdr = iobuf->data;
	size_t hdrlen;
	size_t len;
	union {
		struct sockaddr_in sin;
		struct sockaddr_tcpip st;
	} src, dest;
	uint16_t csum;
	uint16_t pshdr_csum;
	int rc;

	/* Start profiling */
	profile_start ( &ipv4_rx_profiler );

	/* Update statistics */
	ipv4_stats.in_receives++;
	ipv4_stats.in_octets += iob_len ( iobuf );
	if ( flags & LL_BROADCAST ) {
		ipv4_stats.in_bcast_pkts++;
	} else if ( flags & LL_MULTICAST ) {
		ipv4_stats.in_mcast_pkts++;
	}

	/* Sanity check the IPv4 header */
	if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
		DBGC ( iphdr->src, "IPv4 packet too short at %zd bytes (min "
		       "%zd bytes)\n", iob_len ( iobuf ), sizeof ( *iphdr ) );
		goto err_header;
	}
	if ( ( iphdr->verhdrlen & IP_MASK_VER ) != IP_VER ) {
		DBGC ( iphdr->src, "IPv4 version %#02x not supported\n",
		       iphdr->verhdrlen );
		goto err_header;
	}
	hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
	if ( hdrlen < sizeof ( *iphdr ) ) {
		DBGC ( iphdr->src, "IPv4 header too short at %zd bytes (min "
		       "%zd bytes)\n", hdrlen, sizeof ( *iphdr ) );
		goto err_header;
	}
	if ( hdrlen > iob_len ( iobuf ) ) {
		DBGC ( iphdr->src, "IPv4 header too long at %zd bytes "
		       "(packet is %zd bytes)\n", hdrlen, iob_len ( iobuf ) );
		goto err_header;
	}
	if ( ( csum = tcpip_chksum ( iphdr, hdrlen ) ) != 0 ) {
		DBGC ( iphdr->src, "IPv4 checksum incorrect (is %04x "
		       "including checksum field, should be 0000)\n", csum );
		goto err_header;
	}
	len = ntohs ( iphdr->len );
	if ( len < hdrlen ) {
		DBGC ( iphdr->src, "IPv4 length too short at %zd bytes "
		       "(header is %zd bytes)\n", len, hdrlen );
		goto err_header;
	}
	if ( len > iob_len ( iobuf ) ) {
		DBGC ( iphdr->src, "IPv4 length too long at %zd bytes "
		       "(packet is %zd bytes)\n", len, iob_len ( iobuf ) );
		ipv4_stats.in_truncated_pkts++;
		goto err_other;
	}

	/* Truncate packet to correct length */
	iob_unput ( iobuf, ( iob_len ( iobuf ) - len ) );

	/* Print IPv4 header for debugging */
	DBGC2 ( iphdr->src, "IPv4 RX %s<-", inet_ntoa ( iphdr->dest ) );
	DBGC2 ( iphdr->src, "%s len %d proto %d id %04x csum %04x\n",
		inet_ntoa ( iphdr->src ), ntohs ( iphdr->len ), iphdr->protocol,
		ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );

	/* Discard unicast packets not destined for us */
	if ( ( ! ( flags & LL_MULTICAST ) ) &&
	     ipv4_has_any_addr ( netdev ) &&
	     ( ! ipv4_has_addr ( netdev, iphdr->dest ) ) ) {
		DBGC ( iphdr->src, "IPv4 discarding non-local unicast packet "
		       "for %s\n", inet_ntoa ( iphdr->dest ) );
		ipv4_stats.in_addr_errors++;
		goto err_other;
	}

	/* Perform fragment reassembly if applicable */
	if ( iphdr->frags & htons ( IP_MASK_OFFSET | IP_MASK_MOREFRAGS ) ) {
		/* Pass the fragment to fragment_reassemble() which returns
		 * either a fully reassembled I/O buffer or NULL.
		 */
		iobuf = fragment_reassemble ( &ipv4_reassembler, iobuf,
					      &hdrlen );
		if ( ! iobuf )
			return 0;
		iphdr = iobuf->data;
	}

	/* Construct socket addresses, calculate pseudo-header
	 * checksum, and hand off to transport layer
	 */
	memset ( &src, 0, sizeof ( src ) );
	src.sin.sin_family = AF_INET;
	src.sin.sin_addr = iphdr->src;
	memset ( &dest, 0, sizeof ( dest ) );
	dest.sin.sin_family = AF_INET;
	dest.sin.sin_addr = iphdr->dest;
	pshdr_csum = ipv4_pshdr_chksum ( iobuf, TCPIP_EMPTY_CSUM );
	iob_pull ( iobuf, hdrlen );
	if ( ( rc = tcpip_rx ( iobuf, netdev, iphdr->protocol, &src.st,
			       &dest.st, pshdr_csum, &ipv4_stats ) ) != 0 ) {
		DBGC ( src.sin.sin_addr, "IPv4 received packet rejected by "
		       "stack: %s\n", strerror ( rc ) );
		return rc;
	}

	profile_stop ( &ipv4_rx_profiler );
	return 0;

 err_header:
	ipv4_stats.in_hdr_errors++;
 err_other:
	free_iob ( iobuf );
	return -EINVAL;
}

/** 
 * Check existence of IPv4 address for ARP
 *
 * @v netdev		Network device
 * @v net_addr		Network-layer address
 * @ret rc		Return status code
 */
static int ipv4_arp_check ( struct net_device *netdev, const void *net_addr ) {
	const struct in_addr *address = net_addr;

	if ( ipv4_has_addr ( netdev, *address ) )
		return 0;

	return -ENOENT;
}

/**
 * Parse IPv4 address
 *
 * @v string		IPv4 address string
 * @ret in		IPv4 address to fill in
 * @ret ok		IPv4 address is valid
 *
 * Note that this function returns nonzero iff the address is valid,
 * to match the standard BSD API function of the same name.  Unlike
 * most other iPXE functions, a zero therefore indicates failure.
 */
int inet_aton ( const char *string, struct in_addr *in ) {
	const char *separator = "...";
	uint8_t *byte = ( ( uint8_t * ) in );
	char *endp;
	unsigned long value;

	while ( 1 ) {
		value = strtoul ( string, &endp, 0 );
		if ( string == endp )
			return 0;
		if ( value > 0xff )
			return 0;
		*(byte++) = value;
		if ( *endp != *separator )
			return 0;
		if ( ! *(separator++) )
			return 1;
		string = ( endp + 1 );
	}
}

/**
 * Convert IPv4 address to dotted-quad notation
 *
 * @v in		IPv4 address
 * @ret string		IPv4 address in dotted-quad notation
 */
char * inet_ntoa ( struct in_addr in ) {
	static char buf[16]; /* "xxx.xxx.xxx.xxx" */
	uint8_t *bytes = ( uint8_t * ) &in;
	
	sprintf ( buf, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
	return buf;
}

/**
 * Transcribe IPv4 address
 *
 * @v net_addr		IPv4 address
 * @ret string		IPv4 address in dotted-quad notation
 *
 */
static const char * ipv4_ntoa ( const void *net_addr ) {
	return inet_ntoa ( * ( ( struct in_addr * ) net_addr ) );
}

/**
 * Transcribe IPv4 socket address
 *
 * @v sa		Socket address
 * @ret string		Socket address in standard notation
 */
static const char * ipv4_sock_ntoa ( struct sockaddr *sa ) {
	struct sockaddr_in *sin = ( ( struct sockaddr_in * ) sa );

	return inet_ntoa ( sin->sin_addr );
}

/**
 * Parse IPv4 socket address
 *
 * @v string		Socket address string
 * @v sa		Socket address to fill in
 * @ret rc		Return status code
 */
static int ipv4_sock_aton ( const char *string, struct sockaddr *sa ) {
	struct sockaddr_in *sin = ( ( struct sockaddr_in * ) sa );
	struct in_addr in;

	if ( inet_aton ( string, &in ) ) {
		sin->sin_addr = in;
		return 0;
	}
	return -EINVAL;
}

/** IPv4 protocol */
struct net_protocol ipv4_protocol __net_protocol = {
	.name = "IP",
	.net_proto = htons ( ETH_P_IP ),
	.net_addr_len = sizeof ( struct in_addr ),
	.rx = ipv4_rx,
	.ntoa = ipv4_ntoa,
};

/** IPv4 TCPIP net protocol */
struct tcpip_net_protocol ipv4_tcpip_protocol __tcpip_net_protocol = {
	.name = "IPv4",
	.sa_family = AF_INET,
	.header_len = sizeof ( struct iphdr ),
	.tx = ipv4_tx,
	.netdev = ipv4_netdev,
};

/** IPv4 ARP protocol */
struct arp_net_protocol ipv4_arp_protocol __arp_net_protocol = {
	.net_protocol = &ipv4_protocol,
	.check = ipv4_arp_check,
};

/** IPv4 socket address converter */
struct sockaddr_converter ipv4_sockaddr_converter __sockaddr_converter = {
	.family = AF_INET,
	.ntoa = ipv4_sock_ntoa,
	.aton = ipv4_sock_aton,
};

/******************************************************************************
 *
 * Settings
 *
 ******************************************************************************
 */

/**
 * Parse IPv4 address setting value
 *
 * @v type		Setting type
 * @v value		Formatted setting value
 * @v buf		Buffer to contain raw value
 * @v len		Length of buffer
 * @ret len		Length of raw value, or negative error
 */
int parse_ipv4_setting ( const struct setting_type *type __unused,
			 const char *value, void *buf, size_t len ) {
	struct in_addr ipv4;

	/* Parse IPv4 address */
	if ( inet_aton ( value, &ipv4 ) == 0 )
		return -EINVAL;

	/* Copy to buffer */
	if ( len > sizeof ( ipv4 ) )
		len = sizeof ( ipv4 );
	memcpy ( buf, &ipv4, len );

	return ( sizeof ( ipv4 ) );
}

/**
 * Format IPv4 address setting value
 *
 * @v type		Setting type
 * @v raw		Raw setting value
 * @v raw_len		Length of raw setting value
 * @v buf		Buffer to contain formatted value
 * @v len		Length of buffer
 * @ret len		Length of formatted value, or negative error
 */
int format_ipv4_setting ( const struct setting_type *type __unused,
			  const void *raw, size_t raw_len, char *buf,
			  size_t len ) {
	const struct in_addr *ipv4 = raw;

	if ( raw_len < sizeof ( *ipv4 ) )
		return -EINVAL;
	return snprintf ( buf, len, "%s", inet_ntoa ( *ipv4 ) );
}

/** IPv4 address setting */
const struct setting ip_setting __setting ( SETTING_IP, ip ) = {
	.name = "ip",
	.description = "IP address",
	.tag = DHCP_EB_YIADDR,
	.type = &setting_type_ipv4,
};

/** IPv4 subnet mask setting */
const struct setting netmask_setting __setting ( SETTING_IP, netmask ) = {
	.name = "netmask",
	.description = "Subnet mask",
	.tag = DHCP_SUBNET_MASK,
	.type = &setting_type_ipv4,
};

/** Default gateway setting */
const struct setting gateway_setting __setting ( SETTING_IP, gateway ) = {
	.name = "gateway",
	.description = "Default gateway",
	.tag = DHCP_ROUTERS,
	.type = &setting_type_ipv4,
};

/**
 * Create IPv4 routing table based on configured settings
 *
 * @ret rc		Return status code
 */
static int ipv4_create_routes ( void ) {
	struct ipv4_miniroute *miniroute;
	struct ipv4_miniroute *tmp;
	struct net_device *netdev;
	struct settings *settings;
	struct in_addr address = { 0 };
	struct in_addr netmask = { 0 };
	struct in_addr gateway = { 0 };

	/* Delete all existing routes */
	list_for_each_entry_safe ( miniroute, tmp, &ipv4_miniroutes, list )
		del_ipv4_miniroute ( miniroute );

	/* Create a route for each configured network device */
	for_each_netdev ( netdev ) {
		settings = netdev_settings ( netdev );
		/* Get IPv4 address */
		address.s_addr = 0;
		fetch_ipv4_setting ( settings, &ip_setting, &address );
		if ( ! address.s_addr )
			continue;
		/* Get subnet mask */
		fetch_ipv4_setting ( settings, &netmask_setting, &netmask );
		/* Calculate default netmask, if necessary */
		if ( ! netmask.s_addr ) {
			if ( IN_CLASSA ( ntohl ( address.s_addr ) ) ) {
				netmask.s_addr = htonl ( IN_CLASSA_NET );
			} else if ( IN_CLASSB ( ntohl ( address.s_addr ) ) ) {
				netmask.s_addr = htonl ( IN_CLASSB_NET );
			} else if ( IN_CLASSC ( ntohl ( address.s_addr ) ) ) {
				netmask.s_addr = htonl ( IN_CLASSC_NET );
			}
		}
		/* Get default gateway, if present */
		fetch_ipv4_setting ( settings, &gateway_setting, &gateway );
		/* Configure route */
		miniroute = add_ipv4_miniroute ( netdev, address,
						 netmask, gateway );
		if ( ! miniroute )
			return -ENOMEM;
	}

	return 0;
}

/** IPv4 settings applicator */
struct settings_applicator ipv4_settings_applicator __settings_applicator = {
	.apply = ipv4_create_routes,
};

/* Drag in ICMPv4 */
REQUIRE_OBJECT ( icmpv4 );