diff options
-rw-r--r-- | lib/libalias/alias.c | 229 | ||||
-rw-r--r-- | lib/libalias/alias_local.h | 3 | ||||
-rw-r--r-- | lib/libalias/alias_pptp.c | 8 | ||||
-rw-r--r-- | lib/libalias/alias_util.c | 4 | ||||
-rw-r--r-- | sys/netinet/libalias/alias.c | 229 | ||||
-rw-r--r-- | sys/netinet/libalias/alias_local.h | 3 | ||||
-rw-r--r-- | sys/netinet/libalias/alias_pptp.c | 8 | ||||
-rw-r--r-- | sys/netinet/libalias/alias_util.c | 4 |
8 files changed, 126 insertions, 362 deletions
diff --git a/lib/libalias/alias.c b/lib/libalias/alias.c index 19406d4..e02957f 100644 --- a/lib/libalias/alias.c +++ b/lib/libalias/alias.c @@ -136,8 +136,13 @@ __FBSDID("$FreeBSD$"); #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 @@ IcmpAliasIn1(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasIn2(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasIn2(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasIn2(struct libalias *la, struct ip *pip) /* 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 @@ fragment contained in ICMP data section */ 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 @@ fragment contained in ICMP data section */ 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 @@ fragment contained in ICMP data section */ /* 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 @@ IcmpAliasOut1(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasOut2(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasOut2(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasOut2(struct libalias *la, struct ip *pip) */ 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 @@ fragment contained in ICMP data section */ 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 @@ fragment contained in ICMP data section */ 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 @@ fragment contained in ICMP data section */ */ 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 @@ ProtoAliasIn(struct libalias *la, struct ip *pip) /* 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 @@ ProtoAliasOut(struct libalias *la, struct ip *pip) /* 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 @@ UdpAliasIn(struct libalias *la, struct ip *pip) 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 @@ UdpAliasIn(struct libalias *la, struct ip *pip) 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 @@ UdpAliasOut(struct libalias *la, struct ip *pip) /* 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 @@ UdpAliasOut(struct libalias *la, struct ip *pip) /* 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 @@ TcpAliasIn(struct libalias *la, struct ip *pip) 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 @@ TcpAliasIn(struct libalias *la, struct ip *pip) /* 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 @@ TcpAliasIn(struct libalias *la, struct ip *pip) 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 @@ TcpAliasIn(struct libalias *la, struct ip *pip) 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 @@ TcpAliasOut(struct libalias *la, struct ip *pip, int maxpacketsize) 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 @@ TcpAliasOut(struct libalias *la, struct ip *pip, int maxpacketsize) 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 @@ TcpAliasOut(struct libalias *la, struct ip *pip, int maxpacketsize) 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 @@ TcpAliasOut(struct libalias *la, struct ip *pip, int maxpacketsize) 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 @@ FragmentIn(struct libalias *la, struct ip *pip) 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 @@ FragmentOut(struct libalias *la, struct ip *pip) 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 @@ LibAliasFragmentIn(struct libalias *la, char *ptr, /* Points to correctly 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 @@ LibAliasUnaliasOut(struct libalias *la, char *ptr, /* valid IP packet */ /* 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 @@ LibAliasUnaliasOut(struct libalias *la, char *ptr, /* valid IP packet */ 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 @@ LibAliasUnaliasOut(struct libalias *la, char *ptr, /* valid IP packet */ /* 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 @@ LibAliasUnaliasOut(struct libalias *la, char *ptr, /* valid IP packet */ } 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 @@ LibAliasUnaliasOut(struct libalias *la, char *ptr, /* valid IP packet */ 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; diff --git a/lib/libalias/alias_local.h b/lib/libalias/alias_local.h index 420a435..b70fee6 100644 --- a/lib/libalias/alias_local.h +++ b/lib/libalias/alias_local.h @@ -168,8 +168,7 @@ struct libalias { 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 * diff --git a/lib/libalias/alias_pptp.c b/lib/libalias/alias_pptp.c index 5df60cb..24bce3e 100644 --- a/lib/libalias/alias_pptp.c +++ b/lib/libalias/alias_pptp.c @@ -338,9 +338,7 @@ AliasHandlePptpGreOut(struct libalias *la, struct ip *pip) /* 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 @@ AliasHandlePptpGreIn(struct libalias *la, struct ip *pip) /* 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); diff --git a/lib/libalias/alias_util.c b/lib/libalias/alias_util.c index d7d0b3f..bec0ba7 100644 --- a/lib/libalias/alias_util.c +++ b/lib/libalias/alias_util.c @@ -136,10 +136,12 @@ TcpChecksum(struct ip *pip) 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++) { diff --git a/sys/netinet/libalias/alias.c b/sys/netinet/libalias/alias.c index 19406d4..e02957f 100644 --- a/sys/netinet/libalias/alias.c +++ b/sys/netinet/libalias/alias.c @@ -136,8 +136,13 @@ __FBSDID("$FreeBSD$"); #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 @@ IcmpAliasIn1(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasIn2(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasIn2(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasIn2(struct libalias *la, struct ip *pip) /* 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 @@ fragment contained in ICMP data section */ 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 @@ fragment contained in ICMP data section */ 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 @@ fragment contained in ICMP data section */ /* 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 @@ IcmpAliasOut1(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasOut2(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasOut2(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasOut2(struct libalias *la, struct ip *pip) */ 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 @@ fragment contained in ICMP data section */ 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 @@ fragment contained in ICMP data section */ 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 @@ fragment contained in ICMP data section */ */ 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 @@ ProtoAliasIn(struct libalias *la, struct ip *pip) /* 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 @@ ProtoAliasOut(struct libalias *la, struct ip *pip) /* 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 @@ UdpAliasIn(struct libalias *la, struct ip *pip) 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 @@ UdpAliasIn(struct libalias *la, struct ip *pip) 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 @@ UdpAliasOut(struct libalias *la, struct ip *pip) /* 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 @@ UdpAliasOut(struct libalias *la, struct ip *pip) /* 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 @@ TcpAliasIn(struct libalias *la, struct ip *pip) 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 @@ TcpAliasIn(struct libalias *la, struct ip *pip) /* 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 @@ TcpAliasIn(struct libalias *la, struct ip *pip) 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 @@ TcpAliasIn(struct libalias *la, struct ip *pip) 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 @@ TcpAliasOut(struct libalias *la, struct ip *pip, int maxpacketsize) 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 @@ TcpAliasOut(struct libalias *la, struct ip *pip, int maxpacketsize) 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 @@ TcpAliasOut(struct libalias *la, struct ip *pip, int maxpacketsize) 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 @@ TcpAliasOut(struct libalias *la, struct ip *pip, int maxpacketsize) 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 @@ FragmentIn(struct libalias *la, struct ip *pip) 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 @@ FragmentOut(struct libalias *la, struct ip *pip) 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 @@ LibAliasFragmentIn(struct libalias *la, char *ptr, /* Points to correctly 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 @@ LibAliasUnaliasOut(struct libalias *la, char *ptr, /* valid IP packet */ /* 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 @@ LibAliasUnaliasOut(struct libalias *la, char *ptr, /* valid IP packet */ 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 @@ LibAliasUnaliasOut(struct libalias *la, char *ptr, /* valid IP packet */ /* 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 @@ LibAliasUnaliasOut(struct libalias *la, char *ptr, /* valid IP packet */ } 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 @@ LibAliasUnaliasOut(struct libalias *la, char *ptr, /* valid IP packet */ 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; diff --git a/sys/netinet/libalias/alias_local.h b/sys/netinet/libalias/alias_local.h index 420a435..b70fee6 100644 --- a/sys/netinet/libalias/alias_local.h +++ b/sys/netinet/libalias/alias_local.h @@ -168,8 +168,7 @@ struct libalias { 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 * diff --git a/sys/netinet/libalias/alias_pptp.c b/sys/netinet/libalias/alias_pptp.c index 5df60cb..24bce3e 100644 --- a/sys/netinet/libalias/alias_pptp.c +++ b/sys/netinet/libalias/alias_pptp.c @@ -338,9 +338,7 @@ AliasHandlePptpGreOut(struct libalias *la, struct ip *pip) /* 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 @@ AliasHandlePptpGreIn(struct libalias *la, struct ip *pip) /* 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); diff --git a/sys/netinet/libalias/alias_util.c b/sys/netinet/libalias/alias_util.c index d7d0b3f..bec0ba7 100644 --- a/sys/netinet/libalias/alias_util.c +++ b/sys/netinet/libalias/alias_util.c @@ -136,10 +136,12 @@ TcpChecksum(struct ip *pip) 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++) { |