📄 ipsec_tunnel.c
字号:
/* * IPSEC Tunneling code. Heavily based on drivers/net/new_tunnel.c * Copyright (C) 1996, 1997 John Ioannidis. * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. */char ipsec_tunnel_c_version[] = "RCSID $Id: ipsec_tunnel.c,v 1.164 2001/06/14 19:35:10 rgb Exp $";#define __NO_VERSION__#include <linux/module.h>#include <linux/config.h> /* for CONFIG_IP_FORWARD */#include <linux/version.h>#include <linux/kernel.h> /* printk() */#include <linux/malloc.h> /* kmalloc() */#include <linux/errno.h> /* error codes */#include <linux/types.h> /* size_t */#include <linux/interrupt.h> /* mark_bh */#include <linux/netdevice.h> /* struct device, and other headers */#include <linux/etherdevice.h> /* eth_type_trans */#include <linux/ip.h> /* struct iphdr */#include <linux/tcp.h> /* struct tcphdr */#include <linux/udp.h> /* struct udphdr */#include <linux/skbuff.h>#include <freeswan.h>#ifdef SPINLOCK #ifdef SPINLOCK_23 #include <linux/spinlock.h> /* *lock* */ #else /* SPINLOCK_23 */ #include <asm/spinlock.h> /* *lock* */ #endif /* SPINLOCK_23 */#endif /* SPINLOCK */#ifdef NET_21 #define MSS_HACK_ /* experimental */ #include <asm/uaccess.h> #include <linux/in6.h> #define ip_chk_addr inet_addr_type #define IS_MYADDR RTN_LOCAL #include <net/dst.h> #undef dev_kfree_skb #define dev_kfree_skb(a,b) kfree_skb(a) #define proto_priv cb #define PHYSDEV_TYPE #define DEV_QUEUE_XMIT(skb, device, pri) {\ skb->dev = device; \ neigh_compat_output(skb); \ /* skb->dst->output(skb); */ \ } #define ICMP_SEND(skb_in, type, code, info, dev) \ icmp_send(skb_in, type, code, htonl(info)) #define IP_SEND(skb, dev) \ ip_send(skb);#else /* NET_21 */ #define DEV_QUEUE_XMIT(skb, device, pri) {\ dev_queue_xmit(skb, device, pri); \ } #define ICMP_SEND(skb_in, type, code, info, dev) \ icmp_send(skb_in, type, code, info, dev) #define IP_SEND(skb, dev) \ if(ntohs(iph->tot_len) > physmtu) { \ ip_fragment(NULL, skb, dev, 0); \ dev_kfree_skb(skb, FREE_WRITE); \ } else { \ dev_queue_xmit(skb, dev, SOPRI_NORMAL); \ }#endif /* NET_21 */#include <asm/checksum.h>#include <net/icmp.h> /* icmp_send() */#include <net/ip.h>#ifdef NETDEV_23#include <linux/netfilter_ipv4.h>#endif /* NETDEV_23 */#include "radij.h"#include "ipsec_encap.h"#include "ipsec_radij.h"#include "ipsec_netlink.h"#include "ipsec_xform.h"#include "ipsec_tunnel.h"#include "ipsec_ipe4.h"#include "ipsec_ah.h"#include "ipsec_esp.h"#ifdef CONFIG_IPSEC_IPCOMP#include "ipcomp.h"#endif /* CONFIG_IPSEC_IPCOMP */#include <pfkeyv2.h>#include <pfkey.h>#include <linux/if_arp.h>#ifdef MSS_HACK#include <net/tcp.h> /* TCP options */#endif /* MSS_HACK */extern void des_ede3_cbc_encrypt(caddr_t, caddr_t, int, caddr_t, caddr_t, caddr_t, caddr_t, int);static __u32 zeroes[64];#ifdef CONFIG_IPSEC_DEBUGint debug_tunnel = 0;int sysctl_ipsec_debug_verbose = 0;#endif /* CONFIG_IPSEC_DEBUG */int sysctl_ipsec_icmp = 0;int sysctl_ipsec_tos = 0;#ifdef CONFIG_IPSEC_DEBUG_DEBUG_NO_STATIC voiddmp(char *s, caddr_t bb, int len){ int i; unsigned char *b = bb; if (debug_tunnel) { printk(KERN_INFO "klips_debug:ipsec_tunnel_:dmp: " "at %s, len=%d:", s, len); for (i=0; i < len; i++) { if(!(i%16)){ printk("\nklips_debug: "); } printk(" %02x", *b++); } printk("\n"); }}#else /* CONFIG_IPSEC_DEBUG */#define dmp(_x, _y, _z) #endif /* CONFIG_IPSEC_DEBUG */#ifndef SKB_COPY_EXPAND/* * This is mostly skbuff.c:skb_copy(). */struct sk_buff *skb_copy_expand(struct sk_buff *skb, int headroom, int tailroom, int priority){ struct sk_buff *n; unsigned long offset; /* * Do sanity checking */ if((headroom < 0) || (tailroom < 0) || ((headroom+tailroom) < 0)) { printk(KERN_WARNING "klips_error:skb_copy_expand: " "Illegal negative head,tailroom %d,%d\n", headroom, tailroom); return NULL; } /* * Allocate the copy buffer */ #ifndef NET_21 IS_SKB(skb);#endif /* !NET_21 */ n=alloc_skb(skb->end - skb->head + headroom + tailroom, priority); KLIPS_PRINT(debug_tunnel & DB_TN_CROUT, "klips_debug:skb_copy_expand: " "head=%p data=%p tail=%p end=%p end-head=%d tail-data=%d\n", skb->head, skb->data, skb->tail, skb->end, skb->end - skb->head, skb->tail - skb->data); if(n==NULL) return NULL; /* * Shift between the two data areas in bytes */ /* offset=n->head-skb->head; */ /* moved down a few lines */ /* Set the data pointer */ skb_reserve(n,skb->data-skb->head+headroom); /* Set the tail pointer and length */ if(skb_tailroom(n) < skb->len) { printk(KERN_WARNING "klips_error:skb_copy_expand: " "tried to skb_put %ld, %d available. This should never happen, please report.\n", (unsigned long int)skb->len, skb_tailroom(n)); dev_kfree_skb(n, FREE_WRITE); return NULL; } skb_put(n,skb->len); offset=n->head + headroom - skb->head; /* Copy the bytes */ memcpy(n->head + headroom, skb->head,skb->end-skb->head);#ifdef NET_21 n->csum=skb->csum; n->priority=skb->priority; n->dst=dst_clone(skb->dst); if(skb->nh.raw) n->nh.raw=skb->nh.raw+offset;#ifndef NETDEV_23 n->is_clone=0;#endif /* NETDEV_23 */ atomic_set(&n->users, 1); n->destructor = NULL; n->security=skb->security;#else /* NET_21 */ n->link3=NULL; n->when=skb->when; if(skb->ip_hdr) n->ip_hdr=(struct iphdr *)(((char *)skb->ip_hdr)+offset); n->saddr=skb->saddr; n->daddr=skb->daddr; n->raddr=skb->raddr; n->seq=skb->seq; n->end_seq=skb->end_seq; n->ack_seq=skb->ack_seq; n->acked=skb->acked; n->free=1; n->arp=skb->arp; n->tries=0; n->lock=0; n->users=0;#endif /* NET_21 */ n->protocol=skb->protocol; n->list=NULL; n->sk=NULL; n->dev=skb->dev; if(skb->h.raw) n->h.raw=skb->h.raw+offset; if(skb->mac.raw) n->mac.raw=skb->mac.raw+offset; memcpy(n->proto_priv, skb->proto_priv, sizeof(skb->proto_priv));#ifndef NETDEV_23 n->used=skb->used;#endif /* !NETDEV_23 */ n->pkt_type=skb->pkt_type; n->stamp=skb->stamp; #ifndef NET_21 IS_SKB(n);#endif /* !NET_21 */ return n;}#endif /* !SKB_COPY_EXPAND */#ifdef CONFIG_IPSEC_DEBUGvoidipsec_print_ip(struct iphdr *ip){ char buf[ADDRTOA_BUF]; printk(KERN_INFO "klips_debug: IP:"); printk(" ihl:%d", ip->ihl*4); printk(" ver:%d", ip->version); printk(" tos:%d", ip->tos); printk(" tlen:%d", ntohs(ip->tot_len)); printk(" id:%d", ntohs(ip->id)); printk(" %s%s%sfrag_off:%d", ip->frag_off & __constant_htons(IP_CE) ? "CE " : "", ip->frag_off & __constant_htons(IP_DF) ? "DF " : "", ip->frag_off & __constant_htons(IP_MF) ? "MF " : "", (ntohs(ip->frag_off) & IP_OFFSET) << 3); printk(" ttl:%d", ip->ttl); printk(" proto:%d", ip->protocol); printk(" chk:%d", ntohs(ip->check)); addrtoa(*((struct in_addr*)(&ip->saddr)), 0, buf, sizeof(buf)); printk(" saddr:%s", buf); addrtoa(*((struct in_addr*)(&ip->daddr)), 0, buf, sizeof(buf)); printk(" daddr:%s", buf); printk("\n"); if(sysctl_ipsec_debug_verbose) { __u8 *c; int i; c = ((__u8*)ip) + ip->ihl*4; for(i = 0; i < ntohs(ip->tot_len) - ip->ihl*4; i++ /*, c++*/) { if(!(i % 16)) { printk(KERN_INFO "klips_debug: @%03x:", i); } printk(" %02x", /***/c[i]); if(!((i + 1) % 16)) { printk("\n"); } } if(i % 16) { printk("\n"); } }}#endif /* CONFIG_IPSEC_DEBUG */#ifdef REAL_LOCKING_P/* * Locking */ #if 0DEBUG_NO_STATIC intipsec_tunnel_lock(struct ipsecpriv *prv){ unsigned long flags; save_flags(flags); cli(); /* * Lock in an interrupt may fail */ if(prv->locked && in_interrupt()) { restore_flags(flags); return 0; } while(prv->locked) sleep_on(&prv->wait_queue); prv->locked=1; restore_flags(flags); return 1;}#endif#if 0DEBUG_NO_STATIC voidipsec_tunnel_unlock(struct ipsecpriv *prv){ prv->locked=0; wake_up(&prv->wait_queue);}#endif#endif /* REAL_LOCKING_P */DEBUG_NO_STATIC intipsec_tunnel_open(struct device *dev){ struct ipsecpriv *prv = dev->priv; /* * Can't open until attached. */ KLIPS_PRINT(debug_tunnel & DB_TN_INIT, "klips_debug:ipsec_tunnel_open: " "dev = %s, prv->dev = %s\n", dev->name, prv->dev?prv->dev->name:"NONE"); if (prv->dev == NULL) return -ENODEV; MOD_INC_USE_COUNT; return 0;}DEBUG_NO_STATIC intipsec_tunnel_close(struct device *dev){ MOD_DEC_USE_COUNT; return 0;}#ifdef MSS_HACK/* * Issues: * 1) Fragments arriving in the tunnel should probably be rejected. * 2) How does this affect syncookies, mss_cache, dst cache ? * 3) Path MTU discovery handling needs to be reviewed. For example, * if we receive an ICMP 'packet too big' message from an intermediate * router specifying it's next hop MTU, our stack may process this and * adjust the MSS without taking our AH/ESP overheads into account. */ /* * Recaclulate checksum using differences between changed datum, * borrowed from netfilter. */DEBUG_NO_STATIC u_int16_t ipsec_fast_csum(u_int32_t oldvalinv, u_int32_t newval, u_int16_t oldcheck){ u_int32_t diffs[] = { oldvalinv, newval }; return csum_fold(csum_partial((char *)diffs, sizeof(diffs), oldcheck^0xFFFF));}/* * Determine effective MSS. * * Note that we assume that there is always an MSS option for our own * SYN segments, which is mentioned in tcp_syn_build_options(), kernel 2.2.x. * This could change, and we should probably parse TCP options instead. * */DEBUG_NO_STATIC u_int8_tipsec_adjust_mss(struct sk_buff *skb, struct tcphdr *tcph, u_int16_t mtu){ u_int16_t oldmss, newmss; u_int32_t *mssp; struct sock *sk = skb->sk; newmss = tcp_sync_mss(sk, mtu); printk(KERN_INFO "klips: setting mss to %u\n", newmss); mssp = (u_int32_t *)tcph + sizeof(struct tcphdr) / sizeof(u_int32_t); oldmss = ntohl(*mssp) & 0x0000FFFF; *mssp = htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) | newmss); tcph->check = ipsec_fast_csum(htons(~oldmss), htons(newmss), tcph->check); return 1;}#endif /* MSS_HACK */ #ifdef NETDEV_23static inline int ipsec_tunnel_xmit2(struct sk_buff *skb){ return ip_send(skb);}#endif /* NETDEV_23 *//*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -