nat_t *nat_new(np, ip, hlen, flags, direction) ipnat_t *np; ip_t *ip; int hlen; u_short flags; int direction;
nat_t	*nat_new __P((ipnat_t *, ip_t *, int, u_short, int));     int nat_ioctl(data, cmd, mode) caddr_t data; int cmd, mode; { register ipnat_t *nat, *n, **np; ipnat_t natd; int error = 0, ret;   MUTEX_ENTER(&ipf_nat); if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT)) { IRCOPY(data, &natd, sizeof(natd)); nat = &natd; for (np = &nat_list; (n = *np); np = &n->in_next) if (!bcmp((char *)&nat->in_port, (char *)&n->in_port, IPN_CMPSIZ)) break; }  switch (cmd) { case SIOCADNAT : if (!(mode & FWRITE)) { error = EPERM; break; } if (n) { error = EEXIST; break; } if (!(n = (ipnat_t *)KMALLOC(sizeof(*n)))) { error = ENOMEM; break; } IRCOPY((char *)data, (char *)n, sizeof(*n)); n->in_ifp = (void *)GETUNIT(n->in_ifname); n->in_next = *np; n->in_space = ~(0xffffffff & ntohl(n->in_outmsk)); n->in_space -= 2;  if (n->in_inmsk != 0xffffffff) n->in_nip = ntohl(n->in_outip) + 1; else n->in_nip = ntohl(n->in_outip); if (n->in_redir == NAT_MAP) n->in_pnext = ntohs(n->in_pmin);  *np = n; break; case SIOCRMNAT : if (!(mode & FWRITE)) { error = EPERM; break; } if (!n) { error = ESRCH; break; } *np = n->in_next; KFREE(n); break; case SIOCGNATS : nat_stats.ns_table = (nat_t ***)nat_table; nat_stats.ns_list = nat_list; nat_stats.ns_inuse = nat_inuse; IWCOPY((char *)&nat_stats, (char *)data, sizeof(nat_stats)); break; case SIOCGNATL : { natlookup_t nl; nat_t	*na;  IRCOPY((char *)data, (char *)&nl, sizeof(nl)); if ((na = nat_lookupredir(&nl))) { nl.nl_inip = na->nat_outip; nl.nl_inport = na->nat_outport; IWCOPY((char *)&nl, (char *)data, sizeof(nl)); } else error = ESRCH; break; } case SIOCFLNAT : if (!(mode & FWRITE)) { error = EPERM; break; } ret = flush_nattable(); IWCOPY((caddr_t)&ret, data, sizeof(ret)); break; case SIOCCNATL : if (!(mode & FWRITE)) { error = EPERM; break; } ret = clear_natlist(); IWCOPY((caddr_t)&ret, data, sizeof(ret)); break; } MUTEX_EXIT(&ipf_nat); return error; }    int flush_nattable() { nat_t *nat, **natp; int i, j = 0;  for (natp = &nat_table[0][0], i = NAT_SIZE - 1; i >= 0; i--, natp++) while ((nat = *natp)) { *natp = nat->nat_next; KFREE((caddr_t)nat); j++; }  for (natp = &nat_table[1][0], i = NAT_SIZE - 1; i >= 0; i--, natp++) while ((nat = *natp)) { *natp = nat->nat_next; KFREE((caddr_t)nat); j++; } return j; }    int clear_natlist() { register ipnat_t *n, **np; int i = 0;  for (np = &nat_list; (n = *np); i++) { *np = n->in_next; KFREE(n); } return i; }    nat_t * nat_new(np, ip, hlen, flags, direction) ipnat_t *np; ip_t *ip; int hlen; u_short flags; int direction;
nat_t *nat_new(np, ip, hlen, flags, direction)ipnat_t *np;ip_t *ip;int hlen;u_short flags;int direction;{	register u_long sum1, sum2, sumd;	u_short port = 0, sport = 0, dport = 0, nport = 0;	struct in_addr in;	tcphdr_t *tcp;	nat_t *nat, **natp;	if (flags) {		tcp = (tcphdr_t *)((char *)ip + hlen);		sport = tcp->th_sport;		dport = tcp->th_dport;	}	/* Give me a new nat */	if (!(nat = (nat_t *)KMALLOC(sizeof(*nat))))		return NULL;	/*	 * Search the current table for a match.	 */	if (direction == NAT_OUTBOUND) {		/*		 * If it's an outbound packet which doesn't match any existing		 * record, then create a new port		 */		do {			in.s_addr = np->in_nip;			if (np->in_flags & IPN_TCPUDP) {				port = htons(np->in_pnext++);				if (np->in_pnext >= ntohs(np->in_pmax)) {					np->in_pnext = ntohs(np->in_pmin);					np->in_space--;					if (np->in_outmsk != 0xffffffff)						np->in_nip++;				}			} else {				np->in_space--;				if (np->in_outmsk != 0xffffffff)					np->in_nip++;			}			if ((np->in_nip & ntohl(np->in_outmsk)) >					ntohl(np->in_outip))				np->in_nip = ntohl(np->in_outip) + 1;		} while (nat_lookupinip(in, sport));		/* Setup the NAT table */		nat->nat_use = 0;		nat->nat_inip = ip->ip_src;		nat->nat_outip.s_addr = htonl(in.s_addr);		sum1 = (ntohl(ip->ip_src.s_addr) & 0xffff) +			(ntohl(ip->ip_src.s_addr) >> 16) + ntohs(sport);		/* Do it twice */		sum1 = (sum1 & 0xffff) + (sum1 >> 16);		sum1 = (sum1 & 0xffff) + (sum1 >> 16);		sum2 = (in.s_addr & 0xffff) + (in.s_addr >> 16) + ntohs(port);		/* Do it twice */		sum2 = (sum2 & 0xffff) + (sum2 >> 16);		sum2 = (sum2 & 0xffff) + (sum2 >> 16);		if (sum1 > sum2)			sum2--; /* Because ~1 == -2, We really need ~1 == -1 */		sumd = sum2 - sum1;		sumd = (sumd & 0xffff) + (sumd >> 16);		nat->nat_sumd = (sumd & 0xffff) + (sumd >> 16);		if (sport) {			nat->nat_inport = sport;			nat->nat_outport = port;		} else {			nat->nat_inport = 0;			nat->nat_outport = 0;		}	} else {		/*		 * Otherwise, it's an inbound packet. Most likely, we don't		 * want to rewrite source ports and source addresses. Instead,		 * we want to rewrite to a fixed internal address and fixed		 * internal port.		 */		in.s_addr = ntohl(np->in_inip);		nport = np->in_pnext;		nat->nat_use = 0;		nat->nat_inip.s_addr = htonl(in.s_addr);		nat->nat_outip = ip->ip_dst;		nat->nat_oip = ip->ip_src;		sum1 = (ntohl(ip->ip_dst.s_addr) & 0xffff) +			(ntohl(ip->ip_dst.s_addr) >> 16) + ntohs(dport);		/* Do it twice */		sum1 = (sum1 & 0xffff) + (sum1 >> 16);		sum1 = (sum1 & 0xffff) + (sum1 >> 16);		sum2 = (in.s_addr & 0xffff) + (in.s_addr >> 16) + ntohs(nport);		/* Do it twice */		sum2 = (sum2 & 0xffff) + (sum2 >> 16);		sum2 = (sum2 & 0xffff) + (sum2 >> 16);		if (sum2 > sum1)			sum1--; /* Because ~1 == -2, We really need ~1 == -1 */		sumd = (sum1 - sum2);		sumd = (sumd & 0xffff) + (sumd >> 16);		nat->nat_sumd = (sumd & 0xffff) + (sumd >> 16);		if (dport) {			nat->nat_inport = nport;			nat->nat_outport = dport;			nat->nat_oport = sport;		} else {			nat->nat_inport = 0;			nat->nat_outport = 0;		}	}	in.s_addr = htonl(in.s_addr);	natp = &nat_table[0][nat->nat_inip.s_addr % NAT_SIZE];	nat->nat_next = *natp;	*natp = nat;	nat->nat_use++;	natp = &nat_table[1][nat->nat_outip.s_addr % NAT_SIZE];	nat->nat_next = *natp;	*natp = nat;	nat->nat_use++;	if (direction == NAT_REDIRECT) {		ip->ip_src = in;		if (flags)			tcp->th_sport = htons(port);	} else {		ip->ip_dst = in;		if (flags)			tcp->th_dport = htons(nport);	}	nat_stats.ns_added++;	nat_inuse++;	return nat;}