1f987e1bdSBrian Somers /*- 2e83aaae3SBrian Somers * Copyright (c) 2001 Charles Mott <cm@linktel.net> 3f987e1bdSBrian Somers * All rights reserved. 4f987e1bdSBrian Somers * 5f987e1bdSBrian Somers * Redistribution and use in source and binary forms, with or without 6f987e1bdSBrian Somers * modification, are permitted provided that the following conditions 7f987e1bdSBrian Somers * are met: 8f987e1bdSBrian Somers * 1. Redistributions of source code must retain the above copyright 9f987e1bdSBrian Somers * notice, this list of conditions and the following disclaimer. 10f987e1bdSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 11f987e1bdSBrian Somers * notice, this list of conditions and the following disclaimer in the 12f987e1bdSBrian Somers * documentation and/or other materials provided with the distribution. 13f987e1bdSBrian Somers * 14f987e1bdSBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15f987e1bdSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16f987e1bdSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17f987e1bdSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18f987e1bdSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19f987e1bdSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20f987e1bdSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21f987e1bdSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22f987e1bdSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23f987e1bdSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24f987e1bdSBrian Somers * SUCH DAMAGE. 25f987e1bdSBrian Somers */ 26f987e1bdSBrian Somers 27e2505aa6SMatthew Dillon #include <sys/cdefs.h> 28e2505aa6SMatthew Dillon __FBSDID("$FreeBSD$"); 29e2505aa6SMatthew Dillon 30e2505aa6SMatthew Dillon 313b160b8bSBrian Somers /* 3291cc2995SRuslan Ermilov Alias_util.c contains general utilities used by other functions 333b160b8bSBrian Somers in the packet aliasing module. At the moment, there are functions 343b160b8bSBrian Somers for computing IP header and TCP packet checksums. 353b160b8bSBrian Somers 363b160b8bSBrian Somers The checksum routines are based upon example code in a Unix networking 373b160b8bSBrian Somers text written by Stevens (sorry, I can't remember the title -- but 383b160b8bSBrian Somers at least this is a good author). 393b160b8bSBrian Somers 403b160b8bSBrian Somers Initial Version: August, 1996 (cjm) 413b160b8bSBrian Somers 423b160b8bSBrian Somers Version 1.7: January 9, 1997 433b160b8bSBrian Somers Added differential checksum update function. 443b160b8bSBrian Somers */ 453b160b8bSBrian Somers 463b160b8bSBrian Somers /* 473b160b8bSBrian Somers Note: the checksum routines assume that the actual checksum word has 4891cc2995SRuslan Ermilov been zeroed out. If the checksum word is filled with the proper value, 493b160b8bSBrian Somers then these routines will give a result of zero (useful for testing 503b160b8bSBrian Somers purposes); 513b160b8bSBrian Somers */ 523b160b8bSBrian Somers 535e289f9eSPoul-Henning Kamp #include <stdio.h> 543b160b8bSBrian Somers #include <sys/types.h> 553b160b8bSBrian Somers #include <netinet/in_systm.h> 563b160b8bSBrian Somers #include <netinet/in.h> 573b160b8bSBrian Somers #include <netinet/ip.h> 583b160b8bSBrian Somers #include <netinet/tcp.h> 593b160b8bSBrian Somers 603efa11bbSBrian Somers #include "alias.h" 613b160b8bSBrian Somers #include "alias_local.h" 623b160b8bSBrian Somers 633b160b8bSBrian Somers u_short 645e289f9eSPoul-Henning Kamp LibAliasInternetChecksum(struct libalias *la, u_short * ptr, int nbytes) 653b160b8bSBrian Somers { 663b160b8bSBrian Somers int sum, oddbyte; 673b160b8bSBrian Somers 683b160b8bSBrian Somers sum = 0; 69f0f93429SDag-Erling Smørgrav while (nbytes > 1) { 703b160b8bSBrian Somers sum += *ptr++; 713b160b8bSBrian Somers nbytes -= 2; 723b160b8bSBrian Somers } 73f0f93429SDag-Erling Smørgrav if (nbytes == 1) { 743b160b8bSBrian Somers oddbyte = 0; 7546d28b44SLuoqi Chen ((u_char *) & oddbyte)[0] = *(u_char *) ptr; 7646d28b44SLuoqi Chen ((u_char *) & oddbyte)[1] = 0; 773b160b8bSBrian Somers sum += oddbyte; 783b160b8bSBrian Somers } 793b160b8bSBrian Somers sum = (sum >> 16) + (sum & 0xffff); 803b160b8bSBrian Somers sum += (sum >> 16); 813b160b8bSBrian Somers return (~sum); 823b160b8bSBrian Somers } 833b160b8bSBrian Somers 843b160b8bSBrian Somers u_short 853b160b8bSBrian Somers IpChecksum(struct ip *pip) 863b160b8bSBrian Somers { 873efa11bbSBrian Somers return (PacketAliasInternetChecksum((u_short *) pip, 883efa11bbSBrian Somers (pip->ip_hl << 2))); 893b160b8bSBrian Somers 903b160b8bSBrian Somers } 913b160b8bSBrian Somers 923b160b8bSBrian Somers u_short 933b160b8bSBrian Somers TcpChecksum(struct ip *pip) 943b160b8bSBrian Somers { 953b160b8bSBrian Somers u_short *ptr; 963b160b8bSBrian Somers struct tcphdr *tc; 973b160b8bSBrian Somers int nhdr, ntcp, nbytes; 983b160b8bSBrian Somers int sum, oddbyte; 993b160b8bSBrian Somers 1003b160b8bSBrian Somers nhdr = pip->ip_hl << 2; 1013b160b8bSBrian Somers ntcp = ntohs(pip->ip_len) - nhdr; 1023b160b8bSBrian Somers 1033b160b8bSBrian Somers tc = (struct tcphdr *)((char *)pip + nhdr); 1043b160b8bSBrian Somers ptr = (u_short *) tc; 1053b160b8bSBrian Somers 1063b160b8bSBrian Somers /* Add up TCP header and data */ 1073b160b8bSBrian Somers nbytes = ntcp; 1083b160b8bSBrian Somers sum = 0; 109f0f93429SDag-Erling Smørgrav while (nbytes > 1) { 1103b160b8bSBrian Somers sum += *ptr++; 1113b160b8bSBrian Somers nbytes -= 2; 1123b160b8bSBrian Somers } 113f0f93429SDag-Erling Smørgrav if (nbytes == 1) { 1143b160b8bSBrian Somers oddbyte = 0; 11546d28b44SLuoqi Chen ((u_char *) & oddbyte)[0] = *(u_char *) ptr; 11646d28b44SLuoqi Chen ((u_char *) & oddbyte)[1] = 0; 1173b160b8bSBrian Somers sum += oddbyte; 1183b160b8bSBrian Somers } 1193b160b8bSBrian Somers /* "Pseudo-header" data */ 1203b160b8bSBrian Somers ptr = (u_short *) & (pip->ip_dst); 1213b160b8bSBrian Somers sum += *ptr++; 1223b160b8bSBrian Somers sum += *ptr; 1233b160b8bSBrian Somers ptr = (u_short *) & (pip->ip_src); 1243b160b8bSBrian Somers sum += *ptr++; 1253b160b8bSBrian Somers sum += *ptr; 1263b160b8bSBrian Somers sum += htons((u_short) ntcp); 1273b160b8bSBrian Somers sum += htons((u_short) pip->ip_p); 1283b160b8bSBrian Somers 1293b160b8bSBrian Somers /* Roll over carry bits */ 1303b160b8bSBrian Somers sum = (sum >> 16) + (sum & 0xffff); 1313b160b8bSBrian Somers sum += (sum >> 16); 1323b160b8bSBrian Somers 1333b160b8bSBrian Somers /* Return checksum */ 1343b160b8bSBrian Somers return ((u_short) ~ sum); 1353b160b8bSBrian Somers } 1363b160b8bSBrian Somers 1373b160b8bSBrian Somers 1383b160b8bSBrian Somers void 1393b160b8bSBrian Somers DifferentialChecksum(u_short * cksum, u_short * new, u_short * old, int n) 1403b160b8bSBrian Somers { 1413b160b8bSBrian Somers int i; 1423b160b8bSBrian Somers int accumulate; 1433b160b8bSBrian Somers 1443b160b8bSBrian Somers accumulate = *cksum; 145f0f93429SDag-Erling Smørgrav for (i = 0; i < n; i++) { 1463b160b8bSBrian Somers accumulate -= *new++; 1473b160b8bSBrian Somers accumulate += *old++; 1483b160b8bSBrian Somers } 1493b160b8bSBrian Somers 150f0f93429SDag-Erling Smørgrav if (accumulate < 0) { 1513b160b8bSBrian Somers accumulate = -accumulate; 1523b160b8bSBrian Somers accumulate = (accumulate >> 16) + (accumulate & 0xffff); 1533b160b8bSBrian Somers accumulate += accumulate >> 16; 1543b160b8bSBrian Somers *cksum = (u_short) ~ accumulate; 155f0f93429SDag-Erling Smørgrav } else { 1563b160b8bSBrian Somers accumulate = (accumulate >> 16) + (accumulate & 0xffff); 1573b160b8bSBrian Somers accumulate += accumulate >> 16; 1583b160b8bSBrian Somers *cksum = (u_short) accumulate; 1593b160b8bSBrian Somers } 1603b160b8bSBrian Somers } 161