2001-06-04 15:09:51 +00:00
|
|
|
/*-
|
2001-11-03 11:34:09 +00:00
|
|
|
* Copyright (c) 2001 Charles Mott <cm@linktel.net>
|
2001-06-04 15:09:51 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-09-30 21:03:33 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
|
1997-05-23 04:41:31 +00:00
|
|
|
/*
|
2000-04-05 07:45:39 +00:00
|
|
|
Alias_util.c contains general utilities used by other functions
|
1997-05-23 04:41:31 +00:00
|
|
|
in the packet aliasing module. At the moment, there are functions
|
|
|
|
for computing IP header and TCP packet checksums.
|
|
|
|
|
|
|
|
The checksum routines are based upon example code in a Unix networking
|
|
|
|
text written by Stevens (sorry, I can't remember the title -- but
|
|
|
|
at least this is a good author).
|
|
|
|
|
|
|
|
Initial Version: August, 1996 (cjm)
|
|
|
|
|
|
|
|
Version 1.7: January 9, 1997
|
2004-07-05 10:53:28 +00:00
|
|
|
Added differential checksum update function.
|
1997-05-23 04:41:31 +00:00
|
|
|
*/
|
|
|
|
|
2005-05-05 19:27:32 +00:00
|
|
|
#ifdef _KERNEL
|
|
|
|
#include <sys/param.h>
|
Summer of Code 2005: improve libalias - part 1 of 2
With the first part of my previous Summer of Code work, we get:
-made libalias modular:
-support for 'particular' protocols (like ftp/irc/etcetc) is no more
hardcoded inside libalias, but it's available through external
modules loadable at runtime
-modules are available both in kernel (/boot/kernel/alias_*.ko) and
user land (/lib/libalias_*)
-protocols/applications modularized are: cuseeme, ftp, irc, nbt, pptp,
skinny and smedia
-added logging support for kernel side
-cleanup
After a buildworld, do a 'mergemaster -i' to install the file libalias.conf
in /etc or manually copy it.
During startup (and after every HUP signal) user land applications running
the new libalias will try to read a file in /etc called libalias.conf:
that file contains the list of modules to load.
User land applications affected by this commit are ppp and natd:
if libalias.conf is present in /etc you won't notice any difference.
The only kernel land bit affected by this commit is ng_nat:
if you are using ng_nat, and it doesn't correctly handle
ftp/irc/etcetc sessions anymore, remember to kldload
the correspondent module (i.e. kldload alias_ftp).
General information and details about the inner working are available
in the libalias man page under the section 'MODULAR ARCHITECTURE
(AND ipfw(4) SUPPORT)'.
NOTA BENE: this commit affects _ONLY_ libalias, ipfw in-kernel nat
support will be part of the next libalias-related commit.
Approved by: glebius
Reviewed by: glebius, ru
2006-09-26 23:26:53 +00:00
|
|
|
#include <sys/proc.h>
|
2005-05-05 19:27:32 +00:00
|
|
|
#else
|
1997-05-23 04:41:31 +00:00
|
|
|
#include <sys/types.h>
|
2005-05-05 19:27:32 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
1997-05-23 04:41:31 +00:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
2005-05-05 19:27:32 +00:00
|
|
|
#ifdef _KERNEL
|
|
|
|
#include <netinet/libalias/alias.h>
|
|
|
|
#include <netinet/libalias/alias_local.h>
|
|
|
|
#else
|
1997-08-03 18:20:03 +00:00
|
|
|
#include "alias.h"
|
1997-05-23 04:41:31 +00:00
|
|
|
#include "alias_local.h"
|
2005-05-05 19:27:32 +00:00
|
|
|
#endif
|
1997-05-23 04:41:31 +00:00
|
|
|
|
2005-06-27 07:36:02 +00:00
|
|
|
/*
|
|
|
|
* Note: the checksum routines assume that the actual checksum word has
|
|
|
|
* been zeroed out. If the checksum word is filled with the proper value,
|
|
|
|
* then these routines will give a result of zero (useful for testing
|
|
|
|
* purposes);
|
|
|
|
*/
|
1997-05-23 04:41:31 +00:00
|
|
|
u_short
|
2005-06-20 08:31:48 +00:00
|
|
|
LibAliasInternetChecksum(struct libalias *la __unused, u_short * ptr,
|
|
|
|
int nbytes)
|
1997-05-23 04:41:31 +00:00
|
|
|
{
|
2004-03-16 21:30:41 +00:00
|
|
|
int sum, oddbyte;
|
|
|
|
|
2006-12-15 12:50:06 +00:00
|
|
|
LIBALIAS_LOCK(la);
|
2004-03-16 21:30:41 +00:00
|
|
|
sum = 0;
|
|
|
|
while (nbytes > 1) {
|
|
|
|
sum += *ptr++;
|
|
|
|
nbytes -= 2;
|
|
|
|
}
|
|
|
|
if (nbytes == 1) {
|
|
|
|
oddbyte = 0;
|
|
|
|
((u_char *) & oddbyte)[0] = *(u_char *) ptr;
|
|
|
|
((u_char *) & oddbyte)[1] = 0;
|
|
|
|
sum += oddbyte;
|
|
|
|
}
|
|
|
|
sum = (sum >> 16) + (sum & 0xffff);
|
|
|
|
sum += (sum >> 16);
|
2006-12-15 12:50:06 +00:00
|
|
|
LIBALIAS_UNLOCK(la);
|
2004-03-16 21:30:41 +00:00
|
|
|
return (~sum);
|
1997-05-23 04:41:31 +00:00
|
|
|
}
|
|
|
|
|
2005-06-27 07:36:02 +00:00
|
|
|
#ifndef _KERNEL
|
1997-05-23 04:41:31 +00:00
|
|
|
u_short
|
|
|
|
IpChecksum(struct ip *pip)
|
|
|
|
{
|
2005-06-20 08:31:48 +00:00
|
|
|
return (LibAliasInternetChecksum(NULL, (u_short *) pip,
|
2004-03-16 21:30:41 +00:00
|
|
|
(pip->ip_hl << 2)));
|
1997-05-23 04:41:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-07-01 11:19:40 +00:00
|
|
|
u_short
|
1997-05-23 04:41:31 +00:00
|
|
|
TcpChecksum(struct ip *pip)
|
|
|
|
{
|
2004-03-16 21:30:41 +00:00
|
|
|
u_short *ptr;
|
|
|
|
struct tcphdr *tc;
|
|
|
|
int nhdr, ntcp, nbytes;
|
|
|
|
int sum, oddbyte;
|
1997-05-23 04:41:31 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
nhdr = pip->ip_hl << 2;
|
|
|
|
ntcp = ntohs(pip->ip_len) - nhdr;
|
1997-05-23 04:41:31 +00:00
|
|
|
|
2004-07-06 12:13:28 +00:00
|
|
|
tc = (struct tcphdr *)ip_next(pip);
|
2004-03-16 21:30:41 +00:00
|
|
|
ptr = (u_short *) tc;
|
2002-07-01 11:19:40 +00:00
|
|
|
|
1997-05-23 04:41:31 +00:00
|
|
|
/* Add up TCP header and data */
|
2004-03-16 21:30:41 +00:00
|
|
|
nbytes = ntcp;
|
|
|
|
sum = 0;
|
|
|
|
while (nbytes > 1) {
|
|
|
|
sum += *ptr++;
|
|
|
|
nbytes -= 2;
|
|
|
|
}
|
|
|
|
if (nbytes == 1) {
|
|
|
|
oddbyte = 0;
|
|
|
|
((u_char *) & oddbyte)[0] = *(u_char *) ptr;
|
|
|
|
((u_char *) & oddbyte)[1] = 0;
|
|
|
|
sum += oddbyte;
|
|
|
|
}
|
1997-05-23 04:41:31 +00:00
|
|
|
/* "Pseudo-header" data */
|
2004-03-16 21:30:41 +00:00
|
|
|
ptr = (u_short *) & (pip->ip_dst);
|
|
|
|
sum += *ptr++;
|
|
|
|
sum += *ptr;
|
|
|
|
ptr = (u_short *) & (pip->ip_src);
|
|
|
|
sum += *ptr++;
|
|
|
|
sum += *ptr;
|
|
|
|
sum += htons((u_short) ntcp);
|
|
|
|
sum += htons((u_short) pip->ip_p);
|
1997-05-23 04:41:31 +00:00
|
|
|
|
|
|
|
/* Roll over carry bits */
|
2004-03-16 21:30:41 +00:00
|
|
|
sum = (sum >> 16) + (sum & 0xffff);
|
|
|
|
sum += (sum >> 16);
|
1997-05-23 04:41:31 +00:00
|
|
|
|
|
|
|
/* Return checksum */
|
2004-03-16 21:30:41 +00:00
|
|
|
return ((u_short) ~ sum);
|
1997-05-23 04:41:31 +00:00
|
|
|
}
|
2005-06-27 07:36:02 +00:00
|
|
|
#endif /* not _KERNEL */
|
1997-05-23 04:41:31 +00:00
|
|
|
|
|
|
|
void
|
2004-03-31 21:32:58 +00:00
|
|
|
DifferentialChecksum(u_short * cksum, void *newp, void *oldp, int n)
|
1997-05-23 04:41:31 +00:00
|
|
|
{
|
2004-03-16 21:30:41 +00:00
|
|
|
int i;
|
|
|
|
int accumulate;
|
2004-03-31 21:32:58 +00:00
|
|
|
u_short *new = newp;
|
|
|
|
u_short *old = oldp;
|
2004-03-16 21:30:41 +00:00
|
|
|
|
|
|
|
accumulate = *cksum;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
accumulate -= *new++;
|
|
|
|
accumulate += *old++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accumulate < 0) {
|
|
|
|
accumulate = -accumulate;
|
|
|
|
accumulate = (accumulate >> 16) + (accumulate & 0xffff);
|
|
|
|
accumulate += accumulate >> 16;
|
|
|
|
*cksum = (u_short) ~ accumulate;
|
|
|
|
} else {
|
|
|
|
accumulate = (accumulate >> 16) + (accumulate & 0xffff);
|
|
|
|
accumulate += accumulate >> 16;
|
|
|
|
*cksum = (u_short) accumulate;
|
|
|
|
}
|
1997-05-23 04:41:31 +00:00
|
|
|
}
|