Date: Tue, 16 Mar 2004 23:14:23 +0100 From: des@des.no (Dag-Erling =?iso-8859-1?q?Sm=F8rgrav?=) To: current@freebsd.org Cc: net@freebsd.org Subject: Re: libalias patch for review / testing Message-ID: <xzp3c88jv7k.fsf@dwp.des.no> In-Reply-To: <xzp7jxkjv98.fsf@dwp.des.no> (Dag-Erling =?iso-8859-1?q?Sm=F8rgrav's?= message of "Tue, 16 Mar 2004 23:13:23 %2B0100") References: <xzp7jxkjv98.fsf@dwp.des.no>
next in thread | previous in thread | raw e-mail | index | archive | help
--=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable des@des.no (Dag-Erling Sm=C3=B8rgrav) writes: > Anybody running natd on -CURRENT, please test the attached patch Umm, here's a patch that actually compiles. DES --=20 Dag-Erling Sm=C3=B8rgrav - des@des.no --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=libalias.diff Index: Makefile =================================================================== RCS file: /home/ncvs/src/lib/libalias/Makefile,v retrieving revision 1.24 diff -u -r1.24 Makefile --- Makefile 17 Jan 2004 10:52:20 -0000 1.24 +++ Makefile 16 Mar 2004 22:14:03 -0000 @@ -8,5 +8,6 @@ alias_nbt.c alias_pptp.c alias_proxy.c alias_skinny.c alias_smedia.c \ alias_util.c alias_old.c INCS= alias.h +WARNS?= 2 .include <bsd.lib.mk> Index: alias.c =================================================================== RCS file: /home/ncvs/src/lib/libalias/alias.c,v retrieving revision 1.40 diff -u -r1.40 alias.c --- alias.c 16 Mar 2004 21:30:41 -0000 1.40 +++ alias.c 16 Mar 2004 22:14:03 -0000 @@ -136,8 +136,13 @@ #define TFTP_PORT_NUMBER 69 #define PPTP_CONTROL_PORT_NUMBER 1723 +static __inline int +twowords(void *p) +{ + u_short *s = p; - + return (s[0] + s[1]); +} /* TCP Handling Routines @@ -295,9 +300,7 @@ original_address = GetOriginalAddress(link); DifferentialChecksum(&pip->ip_sum, - (u_short *) & original_address, - (u_short *) & pip->ip_dst, - 2); + &original_address, &pip->ip_dst, 2); pip->ip_dst = original_address; } @@ -344,7 +347,6 @@ if (link != NULL) { if (ip->ip_p == IPPROTO_UDP || ip->ip_p == IPPROTO_TCP) { - u_short *sptr; int accumulate, accumulate2; struct in_addr original_address; u_short original_port; @@ -353,12 +355,8 @@ original_port = GetOriginalPort(link); /* Adjust ICMP checksum */ - sptr = (u_short *) & (ip->ip_src); - accumulate = *sptr++; - accumulate += *sptr; - sptr = (u_short *) & original_address; - accumulate -= *sptr++; - accumulate -= *sptr; + accumulate = twowords(&ip->ip_src); + accumulate -= twowords(&original_address); accumulate += ud->uh_sport; accumulate -= original_port; accumulate2 = accumulate; @@ -369,9 +367,7 @@ /* Un-alias address in IP header */ DifferentialChecksum(&pip->ip_sum, - (u_short *) & original_address, - (u_short *) & pip->ip_dst, - 2); + &original_address, &pip->ip_dst, 2); pip->ip_dst = original_address; /* Un-alias address and port number of original IP packet @@ -379,7 +375,6 @@ ip->ip_src = original_address; ud->uh_sport = original_port; } else if (ip->ip_p == IPPROTO_ICMP) { - u_short *sptr; int accumulate, accumulate2; struct in_addr original_address; u_short original_id; @@ -388,12 +383,8 @@ original_id = GetOriginalPort(link); /* Adjust ICMP checksum */ - sptr = (u_short *) & (ip->ip_src); - accumulate = *sptr++; - accumulate += *sptr; - sptr = (u_short *) & original_address; - accumulate -= *sptr++; - accumulate -= *sptr; + accumulate = twowords(&ip->ip_src); + accumulate -= twowords(&original_address); accumulate += ic2->icmp_id; accumulate -= original_id; accumulate2 = accumulate; @@ -404,9 +395,7 @@ /* Un-alias address in IP header */ DifferentialChecksum(&pip->ip_sum, - (u_short *) & original_address, - (u_short *) & pip->ip_dst, - 2); + &original_address, &pip->ip_dst, 2); pip->ip_dst = original_address; /* Un-alias address of original IP packet and sequence number of @@ -489,9 +478,7 @@ alias_address = GetAliasAddress(link); DifferentialChecksum(&pip->ip_sum, - (u_short *) & alias_address, - (u_short *) & pip->ip_src, - 2); + &alias_address, &pip->ip_src, 2); pip->ip_src = alias_address; } @@ -539,7 +526,6 @@ if (link != NULL) { if (ip->ip_p == IPPROTO_UDP || ip->ip_p == IPPROTO_TCP) { - u_short *sptr; int accumulate; struct in_addr alias_address; u_short alias_port; @@ -548,12 +534,8 @@ alias_port = GetAliasPort(link); /* Adjust ICMP checksum */ - sptr = (u_short *) & (ip->ip_dst); - accumulate = *sptr++; - accumulate += *sptr; - sptr = (u_short *) & alias_address; - accumulate -= *sptr++; - accumulate -= *sptr; + accumulate = twowords(&ip->ip_dst); + accumulate -= twowords(&alias_address); accumulate += ud->uh_dport; accumulate -= alias_port; ADJUST_CHECKSUM(accumulate, ic->icmp_cksum); @@ -564,9 +546,7 @@ */ if (pip->ip_src.s_addr == ip->ip_dst.s_addr) { DifferentialChecksum(&pip->ip_sum, - (u_short *) & alias_address, - (u_short *) & pip->ip_src, - 2); + &alias_address, &pip->ip_src, 2); pip->ip_src = alias_address; } /* Alias address and port number of original IP packet @@ -574,7 +554,6 @@ ip->ip_dst = alias_address; ud->uh_dport = alias_port; } else if (ip->ip_p == IPPROTO_ICMP) { - u_short *sptr; int accumulate; struct in_addr alias_address; u_short alias_id; @@ -583,12 +562,8 @@ alias_id = GetAliasPort(link); /* Adjust ICMP checksum */ - sptr = (u_short *) & (ip->ip_dst); - accumulate = *sptr++; - accumulate += *sptr; - sptr = (u_short *) & alias_address; - accumulate -= *sptr++; - accumulate -= *sptr; + accumulate = twowords(&ip->ip_dst); + accumulate -= twowords(&alias_address); accumulate += ic2->icmp_id; accumulate -= alias_id; ADJUST_CHECKSUM(accumulate, ic->icmp_cksum); @@ -599,9 +574,7 @@ */ if (pip->ip_src.s_addr == ip->ip_dst.s_addr) { DifferentialChecksum(&pip->ip_sum, - (u_short *) & alias_address, - (u_short *) & pip->ip_src, - 2); + &alias_address, &pip->ip_src, 2); pip->ip_src = alias_address; } /* Alias address of original IP packet and sequence number of @@ -673,9 +646,7 @@ /* Restore original IP address */ DifferentialChecksum(&pip->ip_sum, - (u_short *) & original_address, - (u_short *) & pip->ip_dst, - 2); + &original_address, &pip->ip_dst, 2); pip->ip_dst = original_address; return (PKT_ALIAS_OK); @@ -706,9 +677,7 @@ /* Change source address */ DifferentialChecksum(&pip->ip_sum, - (u_short *) & alias_address, - (u_short *) & pip->ip_src, - 2); + &alias_address, &pip->ip_src, 2); pip->ip_src = alias_address; return (PKT_ALIAS_OK); @@ -737,7 +706,6 @@ struct in_addr original_address; u_short alias_port; int accumulate; - u_short *sptr; int r = 0; alias_address = GetAliasAddress(link); @@ -762,19 +730,13 @@ if (ud->uh_sum != 0) { accumulate = alias_port; accumulate -= ud->uh_dport; - sptr = (u_short *) & alias_address; - accumulate += *sptr++; - accumulate += *sptr; - sptr = (u_short *) & original_address; - accumulate -= *sptr++; - accumulate -= *sptr; + accumulate += twowords(&alias_address); + accumulate -= twowords(&original_address); ADJUST_CHECKSUM(accumulate, ud->uh_sum); } /* Restore original IP address */ DifferentialChecksum(&pip->ip_sum, - (u_short *) & original_address, - (u_short *) & pip->ip_dst, - 2); + &original_address, &pip->ip_dst, 2); pip->ip_dst = original_address; /* @@ -834,16 +796,11 @@ /* being aliased and source address is being altered */ if (ud->uh_sum != 0) { int accumulate; - u_short *sptr; accumulate = ud->uh_sport; accumulate -= alias_port; - sptr = (u_short *) & (pip->ip_src); - accumulate += *sptr++; - accumulate += *sptr; - sptr = (u_short *) & alias_address; - accumulate -= *sptr++; - accumulate -= *sptr; + accumulate += twowords(&pip->ip_src); + accumulate -= twowords(&alias_address); ADJUST_CHECKSUM(accumulate, ud->uh_sum); } /* Put alias port in UDP header */ @@ -851,9 +808,7 @@ /* Change source address */ DifferentialChecksum(&pip->ip_sum, - (u_short *) & alias_address, - (u_short *) & pip->ip_src, - 2); + &alias_address, &pip->ip_src, 2); pip->ip_src = alias_address; return (PKT_ALIAS_OK); @@ -882,7 +837,6 @@ u_short alias_port; u_short proxy_port; int accumulate; - u_short *sptr; /* Special processing for IP encoding protocols */ if (ntohs(tc->th_dport) == PPTP_CONTROL_PORT_NUMBER @@ -903,12 +857,8 @@ /* and destination port is being altered. */ accumulate = alias_port; accumulate -= tc->th_dport; - sptr = (u_short *) & alias_address; - accumulate += *sptr++; - accumulate += *sptr; - sptr = (u_short *) & original_address; - accumulate -= *sptr++; - accumulate -= *sptr; + accumulate += twowords(&alias_address); + accumulate -= twowords(&original_address); /* If this is a proxy, then modify the TCP source port and checksum accumulation */ @@ -916,13 +866,8 @@ accumulate += tc->th_sport; tc->th_sport = proxy_port; accumulate -= tc->th_sport; - - sptr = (u_short *) & pip->ip_src; - accumulate += *sptr++; - accumulate += *sptr; - sptr = (u_short *) & proxy_address; - accumulate -= *sptr++; - accumulate -= *sptr; + accumulate += twowords(&pip->ip_src); + accumulate -= twowords(&proxy_address); } /* See if ACK number needs to be modified */ if (GetAckModified(link) == 1) { @@ -930,36 +875,23 @@ delta = GetDeltaAckIn(pip, link); if (delta != 0) { - sptr = (u_short *) & tc->th_ack; - accumulate += *sptr++; - accumulate += *sptr; + accumulate += twowords(&tc->th_ack); tc->th_ack = htonl(ntohl(tc->th_ack) - delta); - sptr = (u_short *) & tc->th_ack; - accumulate -= *sptr++; - accumulate -= *sptr; + accumulate -= twowords(&tc->th_ack); } } ADJUST_CHECKSUM(accumulate, tc->th_sum); /* Restore original IP address */ - sptr = (u_short *) & pip->ip_dst; - accumulate = *sptr++; - accumulate += *sptr; - pip->ip_dst = original_address; - sptr = (u_short *) & pip->ip_dst; - accumulate -= *sptr++; - accumulate -= *sptr; + accumulate = twowords(&pip->ip_dst); + accumulate -= twowords(&pip->ip_dst); /* If this is a transparent proxy packet, then modify the source address */ if (proxy_address.s_addr != 0) { - sptr = (u_short *) & pip->ip_src; - accumulate += *sptr++; - accumulate += *sptr; + accumulate += twowords(&pip->ip_src); pip->ip_src = proxy_address; - sptr = (u_short *) & pip->ip_src; - accumulate -= *sptr++; - accumulate -= *sptr; + accumulate -= twowords(&pip->ip_src); } ADJUST_CHECKSUM(accumulate, pip->ip_sum); @@ -995,29 +927,17 @@ dest_address = pip->ip_dst; if (proxy_type != 0) { int accumulate; - u_short *sptr; accumulate = tc->th_dport; tc->th_dport = proxy_server_port; accumulate -= tc->th_dport; - - sptr = (u_short *) & (pip->ip_dst); - accumulate += *sptr++; - accumulate += *sptr; - sptr = (u_short *) & proxy_server_address; - accumulate -= *sptr++; - accumulate -= *sptr; - + accumulate += twowords(&pip->ip_dst); + accumulate -= twowords(&proxy_server_address); ADJUST_CHECKSUM(accumulate, tc->th_sum); - sptr = (u_short *) & (pip->ip_dst); - accumulate = *sptr++; - accumulate += *sptr; + accumulate = twowords(&pip->ip_dst); pip->ip_dst = proxy_server_address; - sptr = (u_short *) & (pip->ip_dst); - accumulate -= *sptr++; - accumulate -= *sptr; - + accumulate -= twowords(&pip->ip_dst); ADJUST_CHECKSUM(accumulate, pip->ip_sum); } link = FindUdpTcpOut(la, pip->ip_src, pip->ip_dst, @@ -1027,7 +947,6 @@ u_short alias_port; struct in_addr alias_address; int accumulate; - u_short *sptr; /* Save original destination address, if this is a proxy packet. Also modify packet to include destination encoding. This may @@ -1069,13 +988,8 @@ accumulate = tc->th_sport; tc->th_sport = alias_port; accumulate -= tc->th_sport; - - sptr = (u_short *) & (pip->ip_src); - accumulate += *sptr++; - accumulate += *sptr; - sptr = (u_short *) & alias_address; - accumulate -= *sptr++; - accumulate -= *sptr; + accumulate += twowords(&pip->ip_src); + accumulate -= twowords(&alias_address); /* Modify sequence number if necessary */ if (GetAckModified(link) == 1) { @@ -1083,26 +997,17 @@ delta = GetDeltaSeqOut(pip, link); if (delta != 0) { - sptr = (u_short *) & tc->th_seq; - accumulate += *sptr++; - accumulate += *sptr; + accumulate += twowords(&tc->th_seq); tc->th_seq = htonl(ntohl(tc->th_seq) + delta); - sptr = (u_short *) & tc->th_seq; - accumulate -= *sptr++; - accumulate -= *sptr; + accumulate -= twowords(&tc->th_seq); } } ADJUST_CHECKSUM(accumulate, tc->th_sum); /* Change source address */ - sptr = (u_short *) & (pip->ip_src); - accumulate = *sptr++; - accumulate += *sptr; + accumulate = twowords(&pip->ip_src); pip->ip_src = alias_address; - sptr = (u_short *) & (pip->ip_src); - accumulate -= *sptr++; - accumulate -= *sptr; - + accumulate -= twowords(&pip->ip_src); ADJUST_CHECKSUM(accumulate, pip->ip_sum); return (PKT_ALIAS_OK); @@ -1142,9 +1047,7 @@ GetFragmentAddr(link, &original_address); DifferentialChecksum(&pip->ip_sum, - (u_short *) & original_address, - (u_short *) & pip->ip_dst, - 2); + &original_address, &pip->ip_dst, 2); pip->ip_dst = original_address; return (PKT_ALIAS_OK); @@ -1160,9 +1063,7 @@ alias_address = FindAliasAddress(la, pip->ip_src); DifferentialChecksum(&pip->ip_sum, - (u_short *) & alias_address, - (u_short *) & pip->ip_src, - 2); + &alias_address, &pip->ip_src, 2); pip->ip_src = alias_address; return (PKT_ALIAS_OK); @@ -1240,9 +1141,7 @@ fpip = (struct ip *)ptr_fragment; DifferentialChecksum(&fpip->ip_sum, - (u_short *) & pip->ip_dst, - (u_short *) & fpip->ip_dst, - 2); + &pip->ip_dst, &fpip->ip_dst, 2); fpip->ip_dst = pip->ip_dst; } @@ -1443,7 +1342,6 @@ /* 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; @@ -1452,12 +1350,8 @@ 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; + accumulate = twowords(&pip->ip_src); + accumulate -= twowords(&original_address); if (pip->ip_p == IPPROTO_UDP) { accumulate += ud->uh_sport; @@ -1471,9 +1365,7 @@ /* Adjust IP checksum */ DifferentialChecksum(&pip->ip_sum, - (u_short *) & original_address, - (u_short *) & pip->ip_src, - 2); + &original_address, &pip->ip_src, 2); /* Un-alias source address and port number */ pip->ip_src = original_address; @@ -1486,7 +1378,6 @@ } else if (pip->ip_p == IPPROTO_ICMP) { - u_short *sptr; int accumulate; struct in_addr original_address; u_short original_id; @@ -1495,21 +1386,15 @@ 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 = 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, - (u_short *) & original_address, - (u_short *) & pip->ip_src, - 2); + &original_address, &pip->ip_src, 2); /* Un-alias source address and port number */ pip->ip_src = original_address; Index: alias_local.h =================================================================== RCS file: /home/ncvs/src/lib/libalias/alias_local.h,v retrieving revision 1.26 diff -u -r1.26 alias_local.h --- alias_local.h 16 Mar 2004 21:30:41 -0000 1.26 +++ alias_local.h 16 Mar 2004 22:14:03 -0000 @@ -168,8 +168,7 @@ u_short IpChecksum(struct ip *_pip); u_short TcpChecksum(struct ip *_pip); void -DifferentialChecksum(u_short * _cksum, u_short * _new, u_short * _old, - int _n); +DifferentialChecksum(u_short * _cksum, void * _new, void * _old, int _n); /* Internal data access */ struct alias_link * Index: alias_pptp.c =================================================================== RCS file: /home/ncvs/src/lib/libalias/alias_pptp.c,v retrieving revision 1.8 diff -u -r1.8 alias_pptp.c --- alias_pptp.c 16 Mar 2004 21:30:41 -0000 1.8 +++ alias_pptp.c 16 Mar 2004 22:14:03 -0000 @@ -338,9 +338,7 @@ /* Change source IP address. */ DifferentialChecksum(&pip->ip_sum, - (u_short *) & alias_addr, - (u_short *) & pip->ip_src, - 2); + &alias_addr, &pip->ip_src, 2); pip->ip_src = alias_addr; } return (0); @@ -368,9 +366,7 @@ /* Restore original IP address. */ DifferentialChecksum(&pip->ip_sum, - (u_short *) & src_addr, - (u_short *) & pip->ip_dst, - 2); + &src_addr, &pip->ip_dst, 2); pip->ip_dst = src_addr; } return (0); Index: alias_util.c =================================================================== RCS file: /home/ncvs/src/lib/libalias/alias_util.c,v retrieving revision 1.11 diff -u -r1.11 alias_util.c --- alias_util.c 16 Mar 2004 21:30:41 -0000 1.11 +++ alias_util.c 16 Mar 2004 22:14:03 -0000 @@ -136,10 +136,12 @@ void -DifferentialChecksum(u_short * cksum, u_short * new, u_short * old, int n) +DifferentialChecksum(u_short * cksum, void *newp, void *oldp, int n) { int i; int accumulate; + u_short *new = newp; + u_short *old = oldp; accumulate = *cksum; for (i = 0; i < n; i++) { --=-=-=--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?xzp3c88jv7k.fsf>