/* IP header and checksum processing
 */

#include "global.h"
#include "mbuf.h"
#include "internet.h"
#include "timer.h"
#include "netuser.h"
#include "ip.h"
/* Convert IP header in host format to network mbuf */
struct mbuf *
htonip(ip,data)
struct ip *ip;
struct mbuf *data;
{
	int16 hdr_len;
	struct mbuf *bp;
	register char *cp;
	int16 checksum;

	hdr_len = IPLEN + ip->optlen;
	if((bp = pushdown(data,hdr_len)) == NULLBUF){
		free_p(data);
		return NULLBUF;
	}
	cp = bp->data;

	*cp++ = (IPVERSION << 4) | (hdr_len >> 2);
	*cp++ = ip->tos;
	cp = put16(cp,ip->length);
	cp = put16(cp,ip->id);
	cp = put16(cp,ip->fl_offs);
	*cp++ = ip->ttl;
	*cp++ = ip->protocol;
	cp = put16(cp,0);	/* Clear checksum */
	cp = put32(cp,ip->source);
	cp = put32(cp,ip->dest);
	if(ip->optlen != 0)
		memcpy(cp,ip->options,ip->optlen);

	/* Compute checksum and insert into header */
	checksum = cksum(NULLHEADER,bp,hdr_len);
	put16(&bp->data[10],checksum);

	return bp;
}
/* Extract an IP header from mbuf */
int
ntohip(ip,bpp)
struct ip *ip;
struct mbuf **bpp;
{
	int16 ihl;
	char iphead[20];

	if(pullup(bpp,iphead,20) != 20)
		return -1;

	if((ihl = (iphead[0] & 0xf) << 2) < IPLEN){
		/* Bogus packet; header is too short */
		return -1;
	}
	ip->version = (iphead[0] >> 4) & 0xf;
	ip->tos = uchar(iphead[1]);
	ip->length = get16(iphead + 2);
	ip->id = get16(iphead + 4);
	ip->fl_offs = get16(iphead + 6);
	ip->ttl = uchar(iphead[8]);
	ip->protocol = uchar(iphead[9]);
	ip->source = get32(iphead + 12);
	ip->dest = get32(iphead + 16);

	if((ip->optlen = ihl - IPLEN) != 0)
		if (pullup(bpp,ip->options,ip->optlen) != ip->optlen)
			return -1;

	return ip->optlen + IPLEN;
}
/* Perform end-around-carry adjustment */
int16
eac(sum)
register int32 sum;	/* Carries in high order 16 bits */
{
	register int16 csum;

	while((csum = hiword(sum)) != 0)
		sum = csum + (sum & 0xffffL);
	return (int16) (sum & 0xffffl); /* Chops to 16 bits */
}
/* Checksum a mbuf chain, with optional pseudo-header */
int16
cksum(ph,m,len)
struct pseudo_header *ph;
register struct mbuf *m;
int16 len;
{
	register unsigned int cnt, total;
	register int32 sum, csum;
	register char *up;
	int16 csum1;
	int swap = 0;
	int16 lcsum();

	sum = 0l;

	/* Sum pseudo-header, if present */
	if(ph != NULLHEADER){
		sum = hiword(ph->source);
		sum += loword(ph->source);
		sum += hiword(ph->dest);
		sum += loword(ph->dest);
		sum += uchar(ph->protocol);
		sum += ph->length;
	}
	/* Now do each mbuf on the chain */
	for(total = 0; m != NULLBUF && total < len; m = m->next) {
		cnt = min(m->cnt, len - total);
		up = (char *)m->data;
		csum = 0;

		if(((long)up) & 1){
			/* Handle odd leading byte */
			if(swap)
				csum = uchar(*up++);
			else
				csum = (int16)(uchar(*up++) << 8);
			cnt--;
			swap = !swap;
		}
		if(cnt > 1){
			/* Have the primitive checksumming routine do most of
			 * the work. At this point, up is guaranteed to be on
			 * a short boundary
			 */
			csum1 = lcsum((unsigned short *)up, cnt >> 1);
			if(swap)
				csum1 = (csum1 << 8) | (csum1 >> 8);
			csum += csum1;
		}
		/* Handle odd trailing byte */
		if(cnt & 1){
			if(swap)
				csum += uchar(up[--cnt]);
			else
				csum += (int16)(uchar(up[--cnt]) << 8);
			swap = !swap;
		}
		sum += csum;
		total += m->cnt;
	}
	/* Do final end-around carry, complement and return */
	return ~eac(sum) & 0xffff;
}

