Esempio n. 1
0
static int
IcmpAliasIn1(struct libalias *la, struct ip *pip)
{

/*
    De-alias incoming echo and timestamp replies.
    Alias incoming echo and timestamp requests.
*/
    struct alias_link *lnk;
    struct icmp *ic;

    LIBALIAS_LOCK_ASSERT(la);

    ic = (struct icmp *)ip_next(pip);

/* Get source address from ICMP data field and restore original data */
    lnk = FindIcmpIn(la, pip->ip_src, pip->ip_dst, ic->icmp_id, 1);
    if (lnk != NULL) {
        u_short original_id;
        int accumulate;

        original_id = GetOriginalPort(lnk);

/* Adjust ICMP checksum */
        accumulate = ic->icmp_id;
        accumulate -= original_id;
        ADJUST_CHECKSUM(accumulate, ic->icmp_cksum);

/* Put original sequence number back in */
        ic->icmp_id = original_id;

/* Put original address back into IP header */
        {
            struct in_addr original_address;

            original_address = GetOriginalAddress(lnk);
            DifferentialChecksum(&pip->ip_sum,
                &original_address, &pip->ip_dst, 2);
            pip->ip_dst = original_address;
        }

        return (PKT_ALIAS_OK);
    }
    return (PKT_ALIAS_IGNORED);
}
Esempio n. 2
0
static int
IcmpAliasIn2(struct libalias *la, struct ip *pip)
{

	LIBALIAS_LOCK_ASSERT(la);
/*
    Alias incoming ICMP error messages containing
    IP header and first 64 bits of datagram.
*/
	struct ip *ip;
	struct icmp *ic, *ic2;
	struct udphdr *ud;
	struct tcphdr *tc;
	struct alias_link *lnk;

	ic = (struct icmp *)ip_next(pip);
	ip = &ic->icmp_ip;

	ud = (struct udphdr *)ip_next(ip);
	tc = (struct tcphdr *)ip_next(ip);
	ic2 = (struct icmp *)ip_next(ip);

	if (ip->ip_p == IPPROTO_UDP)
		lnk = FindUdpTcpIn(la, ip->ip_dst, ip->ip_src,
		    ud->uh_dport, ud->uh_sport,
		    IPPROTO_UDP, 0);
	else if (ip->ip_p == IPPROTO_TCP)
		lnk = FindUdpTcpIn(la, ip->ip_dst, ip->ip_src,
		    tc->th_dport, tc->th_sport,
		    IPPROTO_TCP, 0);
	else if (ip->ip_p == IPPROTO_ICMP) {
		if (ic2->icmp_type == ICMP_ECHO || ic2->icmp_type == ICMP_TSTAMP)
			lnk = FindIcmpIn(la, ip->ip_dst, ip->ip_src, ic2->icmp_id, 0);
		else
			lnk = NULL;
	} else
		lnk = NULL;

	if (lnk != NULL) {
		if (ip->ip_p == IPPROTO_UDP || ip->ip_p == IPPROTO_TCP) {
			int accumulate, accumulate2;
			struct in_addr original_address;
			u_short original_port;

			original_address = GetOriginalAddress(lnk);
			original_port = GetOriginalPort(lnk);

/* Adjust ICMP checksum */
			accumulate = twowords(&ip->ip_src);
			accumulate -= twowords(&original_address);
			accumulate += ud->uh_sport;
			accumulate -= original_port;
			accumulate2 = accumulate;
			accumulate2 += ip->ip_sum;
			ADJUST_CHECKSUM(accumulate, ip->ip_sum);
			accumulate2 -= ip->ip_sum;
			ADJUST_CHECKSUM(accumulate2, ic->icmp_cksum);

/* Un-alias address in IP header */
			DifferentialChecksum(&pip->ip_sum,
			    &original_address, &pip->ip_dst, 2);
			pip->ip_dst = original_address;

/* Un-alias address and port number of original IP packet
fragment contained in ICMP data section */
			ip->ip_src = original_address;
			ud->uh_sport = original_port;
		} else if (ip->ip_p == IPPROTO_ICMP) {
			int accumulate, accumulate2;
			struct in_addr original_address;
			u_short original_id;

			original_address = GetOriginalAddress(lnk);
			original_id = GetOriginalPort(lnk);

/* Adjust ICMP checksum */
			accumulate = twowords(&ip->ip_src);
			accumulate -= twowords(&original_address);
			accumulate += ic2->icmp_id;
			accumulate -= original_id;
			accumulate2 = accumulate;
			accumulate2 += ip->ip_sum;
			ADJUST_CHECKSUM(accumulate, ip->ip_sum);
			accumulate2 -= ip->ip_sum;
			ADJUST_CHECKSUM(accumulate2, ic->icmp_cksum);

/* Un-alias address in IP header */
			DifferentialChecksum(&pip->ip_sum,
			    &original_address, &pip->ip_dst, 2);
			pip->ip_dst = original_address;

/* Un-alias address of original IP packet and sequence number of
   embedded ICMP datagram */
			ip->ip_src = original_address;
			ic2->icmp_id = original_id;
		}
		return (PKT_ALIAS_OK);
	}
	return (PKT_ALIAS_IGNORED);
}
Esempio n. 3
0
int
LibAliasUnaliasOut(struct libalias *la, char *ptr,	/* valid IP packet */
    int maxpacketsize		/* for error checking */
)
{
	struct ip *pip;
	struct icmp *ic;
	struct udphdr *ud;
	struct tcphdr *tc;
	struct alias_link *lnk;
	int iresult = PKT_ALIAS_IGNORED;

	LIBALIAS_LOCK(la);
	pip = (struct ip *)ptr;

	/* Defense against mangled packets */
	if (ntohs(pip->ip_len) > maxpacketsize
	    || (pip->ip_hl << 2) > maxpacketsize)
		goto getout;

	ud = (struct udphdr *)ip_next(pip);
	tc = (struct tcphdr *)ip_next(pip);
	ic = (struct icmp *)ip_next(pip);

	/* Find a link */
	if (pip->ip_p == IPPROTO_UDP)
		lnk = FindUdpTcpIn(la, pip->ip_dst, pip->ip_src,
		    ud->uh_dport, ud->uh_sport,
		    IPPROTO_UDP, 0);
	else if (pip->ip_p == IPPROTO_TCP)
		lnk = FindUdpTcpIn(la, pip->ip_dst, pip->ip_src,
		    tc->th_dport, tc->th_sport,
		    IPPROTO_TCP, 0);
	else if (pip->ip_p == IPPROTO_ICMP)
		lnk = FindIcmpIn(la, pip->ip_dst, pip->ip_src, ic->icmp_id, 0);
	else
		lnk = NULL;

	/* Change it from an aliased packet to an unaliased packet */
	if (lnk != NULL) {
		if (pip->ip_p == IPPROTO_UDP || pip->ip_p == IPPROTO_TCP) {
			int accumulate;
			struct in_addr original_address;
			u_short original_port;

			original_address = GetOriginalAddress(lnk);
			original_port = GetOriginalPort(lnk);

			/* Adjust TCP/UDP checksum */
			accumulate = twowords(&pip->ip_src);
			accumulate -= twowords(&original_address);

			if (pip->ip_p == IPPROTO_UDP) {
				accumulate += ud->uh_sport;
				accumulate -= original_port;
				ADJUST_CHECKSUM(accumulate, ud->uh_sum);
			} else {
				accumulate += tc->th_sport;
				accumulate -= original_port;
				ADJUST_CHECKSUM(accumulate, tc->th_sum);
			}

			/* Adjust IP checksum */
			DifferentialChecksum(&pip->ip_sum,
			    &original_address, &pip->ip_src, 2);

			/* Un-alias source address and port number */
			pip->ip_src = original_address;
			if (pip->ip_p == IPPROTO_UDP)
				ud->uh_sport = original_port;
			else
				tc->th_sport = original_port;

			iresult = PKT_ALIAS_OK;

		} else if (pip->ip_p == IPPROTO_ICMP) {

			int accumulate;
			struct in_addr original_address;
			u_short original_id;

			original_address = GetOriginalAddress(lnk);
			original_id = GetOriginalPort(lnk);

			/* Adjust ICMP checksum */
			accumulate = twowords(&pip->ip_src);
			accumulate -= twowords(&original_address);
			accumulate += ic->icmp_id;
			accumulate -= original_id;
			ADJUST_CHECKSUM(accumulate, ic->icmp_cksum);

			/* Adjust IP checksum */
			DifferentialChecksum(&pip->ip_sum,
			    &original_address, &pip->ip_src, 2);

			/* Un-alias source address and port number */
			pip->ip_src = original_address;
			ic->icmp_id = original_id;

			iresult = PKT_ALIAS_OK;
		}
	}
getout:
	LIBALIAS_UNLOCK(la);
	return (iresult);

}
Esempio n. 4
0
int
PacketUnaliasOut(char *ptr,           /* valid IP packet */
                 int  maxpacketsize   /* for error checking */
                )
{
    struct ip		*pip;
    struct icmp 	*ic;
    struct udphdr	*ud;
    struct tcphdr 	*tc;
    struct alias_link 	*link;
    int 		iresult = PKT_ALIAS_IGNORED;

    pip = (struct ip *) ptr;

    /* Defense against mangled packets */
    if (ntohs(pip->ip_len) > maxpacketsize
     || (pip->ip_hl<<2) > maxpacketsize)
        return(iresult);

    ud = (struct udphdr *) ((char *) pip + (pip->ip_hl << 2));
    tc = (struct tcphdr *) ud;
    ic = (struct icmp *) ud;

    /* Find a link */
    if (pip->ip_p == IPPROTO_UDP)
        link = FindUdpTcpIn(pip->ip_dst, pip->ip_src,
                            ud->uh_dport, ud->uh_sport,
                            IPPROTO_UDP, 0);
    else if (pip->ip_p == IPPROTO_TCP)
        link = FindUdpTcpIn(pip->ip_dst, pip->ip_src,
                            tc->th_dport, tc->th_sport,
                            IPPROTO_TCP, 0);
    else if (pip->ip_p == IPPROTO_ICMP) 
        link = FindIcmpIn(pip->ip_dst, pip->ip_src, ic->icmp_id, 0);
    else
        link = NULL;

    /* Change it from an aliased packet to an unaliased packet */
    if (link != NULL)
    {
        if (pip->ip_p == IPPROTO_UDP || pip->ip_p == IPPROTO_TCP)
        {
            u_short        *sptr;
            int 	   accumulate;
            struct in_addr original_address;
            u_short        original_port;

            original_address = GetOriginalAddress(link);
            original_port = GetOriginalPort(link);
    
            /* Adjust TCP/UDP checksum */
            sptr = (u_short *) &(pip->ip_src);
            accumulate  = *sptr++;
            accumulate += *sptr;
            sptr = (u_short *) &original_address;
            accumulate -= *sptr++;
            accumulate -= *sptr;

            if (pip->ip_p == IPPROTO_UDP) {
                accumulate += ud->uh_sport;
                accumulate -= original_port;
                ADJUST_CHECKSUM(accumulate, ud->uh_sum);
	    } else { 
                accumulate += tc->th_sport;
                accumulate -= original_port;
                ADJUST_CHECKSUM(accumulate, tc->th_sum);
	    }

            /* Adjust IP checksum */
            DifferentialChecksum(&pip->ip_sum,
                                 (u_short *) &original_address,
                                 (u_short *) &pip->ip_src,
                                 2);

            /* Un-alias source address and port number */ 
            pip->ip_src = original_address;
            if (pip->ip_p == IPPROTO_UDP) 
                ud->uh_sport = original_port; 
	    else   
                tc->th_sport = original_port; 
            
	    iresult = PKT_ALIAS_OK;

        } else if (pip->ip_p == IPPROTO_ICMP) {

            u_short        *sptr;
            int            accumulate;
            struct in_addr original_address;
            u_short        original_id;

            original_address = GetOriginalAddress(link);
            original_id = GetOriginalPort(link);

            /* Adjust ICMP checksum */
            sptr = (u_short *) &(pip->ip_src);
            accumulate  = *sptr++;
            accumulate += *sptr;
            sptr = (u_short *) &original_address;
            accumulate -= *sptr++;
            accumulate -= *sptr;
            accumulate += ic->icmp_id;
            accumulate -= original_id;
            ADJUST_CHECKSUM(accumulate, ic->icmp_cksum);

            /* Adjust IP checksum */
            DifferentialChecksum(&pip->ip_sum,
                                 (u_short *) &original_address,
                                 (u_short *) &pip->ip_src,
                                 2);

            /* Un-alias source address and port number */
            pip->ip_src = original_address;
            ic->icmp_id = original_id;

	    iresult = PKT_ALIAS_OK;
        }
    }
    return(iresult);

}