diff options
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/ipoe/CMakeLists.txt | 19 | ||||
-rw-r--r-- | drivers/ipoe/Makefile | 4 | ||||
-rw-r--r-- | drivers/ipoe/ipoe.c | 1535 | ||||
-rw-r--r-- | drivers/ipoe/ipoe.h | 43 | ||||
-rw-r--r-- | drivers/pptp/CMakeLists.txt | 19 | ||||
-rw-r--r-- | drivers/pptp/Makefile | 4 | ||||
-rw-r--r-- | drivers/pptp/gre.c | 220 | ||||
-rw-r--r-- | drivers/pptp/gre.h | 18 | ||||
-rw-r--r-- | drivers/pptp/if_pppox.h | 222 | ||||
-rw-r--r-- | drivers/pptp/pptp.c | 1272 |
10 files changed, 3356 insertions, 0 deletions
diff --git a/drivers/ipoe/CMakeLists.txt b/drivers/ipoe/CMakeLists.txt new file mode 100644 index 00000000..14ac12eb --- /dev/null +++ b/drivers/ipoe/CMakeLists.txt @@ -0,0 +1,19 @@ +if (NOT DEFINED KDIR) + set(KDIR "/usr/src/linux") +endif (NOT DEFINED KDIR) + +ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/driver/ipoe.ko + COMMAND rm -rf ${CMAKE_CURRENT_BINARY_DIR}/driver + COMMAND mkdir ${CMAKE_CURRENT_BINARY_DIR}/driver + COMMAND ln -sf ${CMAKE_CURRENT_SOURCE_DIR}/* ${CMAKE_CURRENT_BINARY_DIR}/driver + COMMAND make -C ${KDIR} M=${CMAKE_CURRENT_BINARY_DIR}/driver modules + DEPENDS ipoe.c ipoe.h +) + +ADD_CUSTOM_TARGET(ipoe_drv ALL + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/driver/ipoe.ko +) + + +INSTALL(CODE "EXECUTE_PROCESS(COMMAND make -C ${KDIR} M=${CMAKE_CURRENT_BINARY_DIR}/drivers/ipoe modules_install)") + diff --git a/drivers/ipoe/Makefile b/drivers/ipoe/Makefile new file mode 100644 index 00000000..22ea273d --- /dev/null +++ b/drivers/ipoe/Makefile @@ -0,0 +1,4 @@ +obj-m += ipoe.o + +default: + make -C $(KDIR) M=$(PWD) modules diff --git a/drivers/ipoe/ipoe.c b/drivers/ipoe/ipoe.c new file mode 100644 index 00000000..2d500ef9 --- /dev/null +++ b/drivers/ipoe/ipoe.c @@ -0,0 +1,1535 @@ +#include <linux/capability.h> +#include <linux/module.h> +#include <linux/types.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/skbuff.h> +#include <linux/netdevice.h> +#include <linux/etherdevice.h> +#include <linux/in.h> +#include <linux/tcp.h> +#include <linux/udp.h> +#include <linux/if_arp.h> +#include <linux/mroute.h> +#include <linux/init.h> +#include <linux/if_ether.h> +#include <linux/semaphore.h> +#include <linux/version.h> + +#include <net/genetlink.h> +#include <net/route.h> +#include <net/sock.h> +#include <net/ip.h> +#include <net/icmp.h> +#include <net/flow.h> +#include <net/net_namespace.h> +#include <net/netns/generic.h> + +#include "ipoe.h" + +#define BEGIN_UPDATE 1 +#define UPDATE 2 +#define END_UPDATE 3 + +#define HASH_BITS 0xff + +#define IPOE_MAGIC 0x55aa + +#define IPOE_QUEUE_LEN 100 +#define IPOE_RATE_U 3000 //3s +#define IPOE_TIMEOUT_U 30 //5s + +#define IPOE_NLMSG_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN - 128) + +#ifndef DEFINE_SEMAPHORE +#define DEFINE_SEMAPHORE(name) struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1) +#endif + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35) +struct ipoe_stats +{ + struct u64_stats_sync sync; + u64 packets; + u64 bytes; +}; +#endif + +struct ipoe_session +{ + struct list_head entry; + struct list_head entry2; + + __be32 addr; + __be32 peer_addr; + __u8 hwaddr[ETH_ALEN]; + + struct net_device *dev; + struct net_device *link_dev; + + atomic_t refs; + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35) + struct ipoe_stats __percpu *rx_stats; + struct ipoe_stats __percpu *tx_stats; +#endif +}; + +struct ipoe_network +{ + struct rcu_head rcu_head; + struct list_head entry; + + __be32 addr; + __be32 mask; +}; + +struct ipoe_entry_u +{ + struct rcu_head rcu_head; + struct list_head entry1; + struct list_head entry2; + + __be32 addr; + unsigned long tstamp; +}; + +static struct list_head ipoe_list[HASH_BITS + 1]; +static struct list_head ipoe_list1_u[HASH_BITS + 1]; +static LIST_HEAD(ipoe_list2); +static LIST_HEAD(ipoe_list2_u); +static DEFINE_SEMAPHORE(ipoe_wlock); +static LIST_HEAD(ipoe_networks); +static struct work_struct ipoe_queue_work; +static struct sk_buff_head ipoe_queue; + +static void ipoe_start_queue_work(unsigned long); +static DEFINE_TIMER(ipoe_timer_u, ipoe_start_queue_work, 0, 0); + +static struct ipoe_session *ipoe_lookup(__be32 addr); +static int ipoe_do_nat(struct sk_buff *skb, __be32 new_addr, int to_peer); +static void ipoe_queue_u(struct sk_buff *skb, __be32 addr); +static int ipoe_lookup1_u(__be32 addr, unsigned long *ts); + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34) +static const struct net_device_ops ipoe_netdev_ops; +#endif + +static struct genl_family ipoe_nl_family; +static struct genl_multicast_group ipoe_nl_mcg; + +static inline int hash_addr(__be32 addr) +{ +#ifdef __LITTLE_ENDIAN + return ((addr >> 24) ^ (addr >> 16)) & HASH_BITS; +#else + return (addr ^ (addr >> 8)) & HASH_BITS; +#endif +} + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35) +static void ipoe_update_stats(struct sk_buff *skb, struct ipoe_stats *st, int corr) +{ + u64_stats_update_begin(&st->sync); + st->packets++; + st->bytes += skb->len - corr; + u64_stats_update_end(&st->sync); +} +#endif + +#if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0) +static void __kfree_rcu(struct rcu_head *head) +{ + kfree(head); +} +#endif + +static int ipoe_check_network(__be32 addr) +{ + struct ipoe_network *n; + int r = 0; + + rcu_read_lock(); + + list_for_each_entry_rcu(n, &ipoe_networks, entry) { + if ((addr & n->mask) == n->addr) { + r = 1; + break; + } + } + + rcu_read_unlock(); + + return r; +} + +static int ipoe_do_nat(struct sk_buff *skb, __be32 new_addr, int to_peer) +{ + struct iphdr *iph; + int noff; + int ihl; + __be32 addr; + + noff = skb_network_offset(skb); + + iph = ip_hdr(skb); + + if (to_peer) + addr = iph->daddr; + else + addr = iph->saddr; + + if (skb_cloned(skb) && + !skb_clone_writable(skb, sizeof(*iph) + noff) && + pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) + return -1; + + iph = ip_hdr(skb); + + if (to_peer) + iph->daddr = new_addr; + else + iph->saddr = new_addr; + + csum_replace4(&iph->check, addr, new_addr); + + ihl = iph->ihl * 4; + + switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) { + case IPPROTO_TCP: + { + struct tcphdr *tcph; + + if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) || + (skb_cloned(skb) && + !skb_clone_writable(skb, ihl + sizeof(*tcph) + noff) && + pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) + return -1; + + tcph = (void *)(skb_network_header(skb) + ihl); + inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, 1); + break; + } + case IPPROTO_UDP: + { + struct udphdr *udph; + + if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) || + (skb_cloned(skb) && + !skb_clone_writable(skb, ihl + sizeof(*udph) + noff) && + pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) + return -1; + + udph = (void *)(skb_network_header(skb) + ihl); + if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) { + inet_proto_csum_replace4(&udph->check, skb, addr, new_addr, 1); + if (!udph->check) + udph->check = CSUM_MANGLED_0; + } + break; + } + case IPPROTO_ICMP: + { + struct icmphdr *icmph; + + if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff)) + return -1; + + icmph = (void *)(skb_network_header(skb) + ihl); + + if ((icmph->type != ICMP_DEST_UNREACH) && + (icmph->type != ICMP_TIME_EXCEEDED) && + (icmph->type != ICMP_PARAMETERPROB)) + break; + + if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) + + noff)) + return -1; + + icmph = (void *)(skb_network_header(skb) + ihl); + iph = (void *)(icmph + 1); + + if (skb_cloned(skb) && + !skb_clone_writable(skb, ihl + sizeof(*icmph) + + sizeof(*iph) + noff) && + pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) + return -1; + + icmph = (void *)(skb_network_header(skb) + ihl); + iph = (void *)(icmph + 1); + if (to_peer) + iph->saddr = new_addr; + else + iph->daddr = new_addr; + + inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr, 0); + break; + } + default: + break; + } + + return 0; +} + +static struct net *pick_net(struct sk_buff *skb) +{ +#ifdef CONFIG_NET_NS + const struct dst_entry *dst; + + if (skb->dev != NULL) + return dev_net(skb->dev); + dst = skb_dst(skb); + if (dst != NULL && dst->dev != NULL) + return dev_net(dst->dev); +#endif + return &init_net; +} + +static int ipoe_route4(struct sk_buff *skb) +{ + const struct iphdr *iph = ip_hdr(skb); + struct net *net = pick_net(skb); + struct rtable *rt; + +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,37) + struct flowi fl4; +#else + struct flowi4 fl4; +#endif + + memset(&fl4, 0, sizeof(fl4)); +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,37) + fl4.fl4_dst = iph->daddr; + fl4.fl4_tos = RT_TOS(iph->tos); + fl4.fl4_scope = RT_SCOPE_UNIVERSE; + if (ip_route_output_key(net, &rt, &fl4)) + return -1; +#else + fl4.daddr = iph->daddr; + fl4.flowi4_tos = RT_TOS(iph->tos); + fl4.flowi4_scope = RT_SCOPE_UNIVERSE; + rt = ip_route_output_key(net, &fl4); + if (IS_ERR(rt)) + return -1; +#endif + + skb_dst_drop(skb); +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,37) + skb_dst_set(skb, &rt->u.dst); + skb->dev = rt->u.dst.dev; +#else + skb_dst_set(skb, &rt->dst); + skb->dev = rt->dst.dev; +#endif + + return 0; +} + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) +static int ipoe_xmit(struct sk_buff *skb, struct net_device *dev) +#else +static netdev_tx_t ipoe_xmit(struct sk_buff *skb, struct net_device *dev) +#endif +{ + struct ipoe_session *ses = netdev_priv(dev); + struct net_device_stats *stats = &dev->stats; + struct iphdr *iph; + struct ethhdr *eth; + struct sk_buff *skb1; + /*struct arphdr *arp; + unsigned char *arp_ptr; + __be32 tip;*/ + int noff; + + if (!ses->peer_addr) + goto drop; + + noff = skb_network_offset(skb); + + if (skb->protocol == htons(ETH_P_IP)) { + if (!pskb_may_pull(skb, sizeof(*iph) + noff)) + goto drop; + + iph = ip_hdr(skb); + + //pr_info("ipoe: xmit %08x %08x\n", iph->saddr, iph->daddr); + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35) + ipoe_update_stats(skb, this_cpu_ptr(ses->tx_stats), ETH_HLEN); +#else + stats->tx_packets++; + stats->tx_bytes += skb->len - ETH_HLEN; +#endif + + if (iph->daddr == ses->addr) { + if (skb_shared(skb)) { + skb1 = skb_clone(skb, GFP_ATOMIC); + if (!skb1) + goto drop; + skb = skb1; + } + + if (ipoe_do_nat(skb, ses->peer_addr, 1)) + goto drop; + + if (!ses->link_dev) { + iph = ip_hdr(skb); + + ip_send_check(iph); + + if (ipoe_route4(skb)) + goto drop; + + pskb_pull(skb, ETH_HLEN); + skb_reset_network_header(skb); + + ip_local_out(skb); + + return NETDEV_TX_OK; + } else { + eth = (struct ethhdr *)skb->data; + + memcpy(eth->h_dest, ses->hwaddr, ETH_ALEN); + memcpy(eth->h_source, ses->link_dev->dev_addr, ETH_ALEN); + } + } + } /*else if (skb->protocol == htons(ETH_P_ARP)) { + if (!pskb_may_pull(skb, arp_hdr_len(dev) + noff)) + goto drop; + + arp = arp_hdr(skb); + arp_ptr = (unsigned char *)(arp + 1); + + if (arp->ar_op == htons(ARPOP_REQUEST)) { + memcpy(&tip, arp_ptr + ETH_ALEN + 4 + ETH_ALEN, 4); + if (tip == ses->addr) { + if (skb_cloned(skb) && + !skb_clone_writable(skb, arp_hdr_len(dev) + noff) && + pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) + goto drop; + + arp = arp_hdr(skb); + arp_ptr = (unsigned char *)(arp + 1); + memcpy(arp_ptr + ETH_ALEN + 4 + ETH_ALEN, &ses->peer_addr, 4); + } + } + }*/ + + if (ses->link_dev) { + skb->dev = ses->link_dev; + //skb->skb_iif = dev->ifindex; + dev_queue_xmit(skb); + + return NETDEV_TX_OK; + } +drop: + stats->tx_dropped++; + dev_kfree_skb(skb); + return NETDEV_TX_OK; +} + +static int ipoe_rcv_arp(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) +{ + struct ipoe_session *ses = NULL; + struct arphdr *arp; + unsigned char *arp_ptr; + int noff; + __be32 sip; + struct sk_buff *skb1; + unsigned char *cb_ptr; + struct net_device_stats *stats; + + //pr_info("ipoe: recv arp\n"); + + if (skb->pkt_type == PACKET_OTHERHOST) + goto drop; + + cb_ptr = skb->cb + sizeof(skb->cb) - 2; + + if (*(__u16 *)cb_ptr == IPOE_MAGIC) + goto drop; + + noff = skb_network_offset(skb); + + if (!pskb_may_pull(skb, arp_hdr_len(dev) + noff)) + goto drop; + + arp = arp_hdr(skb); + arp_ptr = (unsigned char *)(arp + 1); + + if (arp->ar_pro != htons(ETH_P_IP)) + goto drop; + + memcpy(&sip, arp_ptr + ETH_ALEN, 4); + + if (!sip) + goto drop; + //pr_info("ipoe: recv arp %08x\n", sip); + + ses = ipoe_lookup(sip); + + if (!ses) + goto drop; + + stats = &ses->dev->stats; + + if (ses->addr || skb->dev == ses->dev) { + atomic_dec(&ses->refs); + goto drop; + } + + skb1 = skb_clone(skb, GFP_ATOMIC); + if (!skb1) { + stats->rx_dropped++; + goto drop_unlock; + } + + skb1->dev = ses->dev; + skb1->skb_iif = ses->dev->ifindex; + + cb_ptr = skb1->cb + sizeof(skb1->cb) - 2; + *(__u16 *)cb_ptr = IPOE_MAGIC; + + netif_rx(skb1); + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35) + ipoe_update_stats(skb, this_cpu_ptr(ses->rx_stats), 0); +#else + stats->rx_packets++; + stats->rx_bytes += skb->len; +#endif + +drop_unlock: + atomic_dec(&ses->refs); + skb->pkt_type = PACKET_OTHERHOST; + +drop: + kfree_skb(skb); + return NET_RX_DROP; +} + +static int ipoe_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) +{ + struct ipoe_session *ses = NULL; + struct iphdr *iph; + struct ethhdr *eth; + int noff; + struct sk_buff *skb1; + unsigned char *cb_ptr; + struct net_device_stats *stats; + + if (skb->pkt_type == PACKET_OTHERHOST) + goto drop; + + cb_ptr = skb->cb + sizeof(skb->cb) - 2; + + if (*(__u16 *)cb_ptr == IPOE_MAGIC) + goto drop; + + noff = skb_network_offset(skb); + + if (!pskb_may_pull(skb, sizeof(*iph) + noff)) + goto drop; + + iph = ip_hdr(skb); + + if (!iph->saddr) + goto drop; + + //pr_info("ipoe: recv %08x %08x\n", iph->saddr, iph->daddr); + if (!ipoe_check_network(iph->saddr)) + goto drop; + + ses = ipoe_lookup(iph->saddr); + + if (!ses) { + ipoe_queue_u(skb, iph->saddr); + goto drop; + } + + //pr_info("ipoe: recv cb=%x\n", *(__u16 *)cb_ptr); + + if (ses->link_dev) { + eth = eth_hdr(skb); + if (memcmp(eth->h_source, ses->hwaddr, ETH_ALEN)) + goto drop_unlock; + } + + stats = &ses->dev->stats; + + if (skb->dev == ses->dev) { + //pr_info("ipoe: dup\n"); + atomic_dec(&ses->refs); + goto drop; + } + + if (ses->addr && ipoe_check_network(iph->daddr)) { + atomic_dec(&ses->refs); + goto drop; + } + + skb1 = skb_clone(skb, GFP_ATOMIC); + if (!skb1) { + stats->rx_dropped++; + goto drop_unlock; + } + + if (ses->addr && ipoe_do_nat(skb1, ses->addr, 0)) { + kfree_skb(skb1); + goto drop_unlock; + } + + skb1->dev = ses->dev; + //skb1->skb_iif = ses->dev->ifindex; + + cb_ptr = skb1->cb + sizeof(skb1->cb) - 2; + *(__u16 *)cb_ptr = IPOE_MAGIC; + + netif_rx(skb1); + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35) + ipoe_update_stats(skb, this_cpu_ptr(ses->rx_stats), 0); +#else + stats->rx_packets++; + stats->rx_bytes += skb->len; +#endif + +drop_unlock: + atomic_dec(&ses->refs); + skb->pkt_type = PACKET_OTHERHOST; + +drop: + kfree_skb(skb); + return NET_RX_DROP; +} + +static int ipoe_lookup1_u(__be32 addr, unsigned long *ts) +{ + struct ipoe_entry_u *e; + struct list_head *head = &ipoe_list1_u[hash_addr(addr)]; + int r = 0; + + rcu_read_lock(); + + list_for_each_entry_rcu(e, head, entry1) { + if (e->addr == addr) { + *ts = e->tstamp; + r = 1; + break; + } + } + + rcu_read_unlock(); + + return r; +} + +static struct ipoe_entry_u *ipoe_lookup2_u(__be32 addr) +{ + struct ipoe_entry_u *e; + struct list_head *head = &ipoe_list1_u[hash_addr(addr)]; + + list_for_each_entry_rcu(e, head, entry1) { + if (e->addr == addr) + return e; + } + + return NULL; +} + + +static void ipoe_queue_u(struct sk_buff *skb, __u32 addr) +{ + unsigned long ts; + + if (ipoe_lookup1_u(addr, &ts) && jiffies_to_msecs(jiffies - ts) < IPOE_RATE_U) { + //pr_info("not queue %08x\n", addr); + return; + } + + if (skb_queue_len(&ipoe_queue) > IPOE_QUEUE_LEN) + return; + + skb = skb_clone(skb, GFP_ATOMIC); + if (!skb) + return; + + //pr_info("queue %08x\n", addr); + + skb_queue_tail(&ipoe_queue, skb); + schedule_work(&ipoe_queue_work); +} + +static void ipoe_start_queue_work(unsigned long dummy) +{ + schedule_work(&ipoe_queue_work); +} + +static void ipoe_process_queue(struct work_struct *w) +{ + struct sk_buff *skb; + struct ipoe_entry_u *e; + struct ethhdr *eth; + struct iphdr *iph; + struct sk_buff *report_skb = NULL; + void *header = NULL; + struct nlattr *ns; + int id = 1; + + do { + while ((skb = skb_dequeue(&ipoe_queue))) { + eth = eth_hdr(skb); + iph = ip_hdr(skb); + + e = ipoe_lookup2_u(iph->saddr); + + if (!e) { + e = kmalloc(sizeof(*e), GFP_KERNEL); + e->addr = iph->saddr; + e->tstamp = jiffies; + + list_add_tail_rcu(&e->entry1, &ipoe_list1_u[hash_addr(iph->saddr)]); + list_add_tail(&e->entry2, &ipoe_list2_u); + + //pr_info("create %08x\n", e->addr); + } else if (jiffies_to_msecs(jiffies - e->tstamp) < IPOE_RATE_U) { + //pr_info("skip %08x\n", e->addr); + kfree_skb(skb); + continue; + } else { + e->tstamp = jiffies; + list_move_tail(&e->entry2, &ipoe_list2_u); + //pr_info("update %08x\n", e->addr); + } + + if (!report_skb) { + report_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); + if (report_skb) + header = genlmsg_put(report_skb, 0, ipoe_nl_mcg.id, &ipoe_nl_family, 0, IPOE_REP_PKT); + } + + if (report_skb) { + ns = nla_nest_start(report_skb, id++); + if (!ns) + goto nl_err; + + if (nla_put_u32(report_skb, IPOE_ATTR_IFINDEX, skb->dev ? skb->dev->ifindex : skb->skb_iif)) + goto nl_err; + + if (nla_put(report_skb, IPOE_ATTR_ETH_HDR, sizeof(*eth), eth)) + goto nl_err; + + if (nla_put(report_skb, IPOE_ATTR_IP_HDR, sizeof(*iph), iph)) + goto nl_err; + + if (nla_nest_end(report_skb, ns) >= IPOE_NLMSG_SIZE) { + genlmsg_end(report_skb, header); + genlmsg_multicast(report_skb, 0, ipoe_nl_mcg.id, GFP_KERNEL); + report_skb = NULL; + } + + kfree_skb(skb); + continue; + +nl_err: + nlmsg_free(report_skb); + report_skb = NULL; + } + + kfree_skb(skb); + } + + while (!list_empty(&ipoe_list2_u)) { + e = list_entry(ipoe_list2_u.next, typeof(*e), entry2); + if (jiffies_to_msecs(jiffies - e->tstamp) < IPOE_TIMEOUT_U * 1000) + break; + + //pr_info("free %08x\n", e->addr); + list_del(&e->entry2); + list_del_rcu(&e->entry1); +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0) + kfree_rcu(e, rcu_head); +#else + call_rcu(&e->rcu_head, __kfree_rcu); +#endif + } + + synchronize_rcu(); + } while (skb_queue_len(&ipoe_queue)); + + if (report_skb) { + genlmsg_end(report_skb, header); + genlmsg_multicast(report_skb, 0, ipoe_nl_mcg.id, GFP_KERNEL); + } + + if (!list_empty(&ipoe_list2_u)) + mod_timer(&ipoe_timer_u, jiffies + IPOE_TIMEOUT_U * HZ); + else + del_timer(&ipoe_timer_u); +} + +static struct ipoe_session *ipoe_lookup(__be32 addr) +{ + struct ipoe_session *ses; + struct list_head *head; + + head = &ipoe_list[hash_addr(addr)]; + + rcu_read_lock(); + + list_for_each_entry_rcu(ses, head, entry) { + if (ses->peer_addr == addr) { + atomic_inc(&ses->refs); + rcu_read_unlock(); + return ses; + } + } + + rcu_read_unlock(); + + return NULL; +} + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35) +static struct rtnl_link_stats64 *ipoe_stats64(struct net_device *dev, + struct rtnl_link_stats64 *stats) +{ + struct ipoe_session *ses = netdev_priv(dev); + struct ipoe_stats *st; + unsigned int start; + int i; + u64 packets, bytes; + u64 rx_packets = 0, rx_bytes = 0, tx_packets = 0, tx_bytes = 0; + + for_each_possible_cpu(i) { + st = per_cpu_ptr(ses->rx_stats, i); + + do { + start = u64_stats_fetch_begin_bh(&st->sync); + packets = st->packets; + bytes = st->bytes; + } while (u64_stats_fetch_retry_bh(&st->sync, start)); + + rx_packets += packets; + rx_bytes += bytes; + + st = per_cpu_ptr(ses->tx_stats, i); + + do { + start = u64_stats_fetch_begin_bh(&st->sync); + packets = st->packets; + bytes = st->bytes; + } while (u64_stats_fetch_retry_bh(&st->sync, start)); + + tx_packets += packets; + tx_bytes += bytes; + } + + stats->rx_packets = rx_packets; + stats->rx_bytes = rx_bytes; + stats->tx_packets = tx_packets; + stats->tx_bytes = tx_bytes; + + stats->rx_dropped = dev->stats.rx_dropped; + stats->tx_dropped = dev->stats.tx_dropped; + + return stats; +} +#endif + +static void ipoe_free_netdev(struct net_device *dev) +{ +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35) + struct ipoe_session *ses = netdev_priv(dev); + + if (ses->rx_stats) + free_percpu(ses->rx_stats); + if (ses->tx_stats) + free_percpu(ses->tx_stats); +#endif + + free_netdev(dev); +} + +static int ipoe_hard_header(struct sk_buff *skb, struct net_device *dev, + unsigned short type, const void *daddr, + const void *saddr, unsigned len) +{ + const struct ipoe_session *ses = netdev_priv(dev); + + if (ses->link_dev) + return dev_hard_header(skb, ses->link_dev, type, daddr, + saddr, len); + else + return eth_header(skb, dev, type, daddr, saddr, len); +} + +static const struct header_ops ipoe_hard_header_ops = { + .create = ipoe_hard_header, + .rebuild = eth_rebuild_header, + .parse = eth_header_parse, + .cache = eth_header_cache, + .cache_update = eth_header_cache_update, +}; + +static void ipoe_netdev_setup(struct net_device *dev) +{ +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34) + dev->hard_start_xmit = ipoe_xmit; +#else + dev->netdev_ops = &ipoe_netdev_ops; +#endif + dev->destructor = ipoe_free_netdev; + + dev->type = ARPHRD_ETHER; + dev->hard_header_len = 0; + dev->mtu = ETH_DATA_LEN; + dev->flags = IFF_MULTICAST | IFF_POINTOPOINT; + dev->iflink = 0; + dev->addr_len = ETH_ALEN; + dev->features |= NETIF_F_NETNS_LOCAL; + dev->header_ops = &ipoe_hard_header_ops; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35) + dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; +#endif +} + +static int ipoe_create(__be32 peer_addr, __be32 addr, const char *link_ifname, const __u8 *hwaddr) +{ + struct ipoe_session *ses; + struct net_device *dev, *link_dev = NULL; + char name[IFNAMSIZ]; + int r = -EINVAL; + int h = hash_addr(peer_addr); + + if (link_ifname) { + link_dev = dev_get_by_name(&init_net, link_ifname); + if (!link_dev) + return -EINVAL; + sprintf(name, "%s.ipoe%%d", link_ifname); + } else + sprintf(name, "ipoe%%d"); + + dev = alloc_netdev(sizeof(*ses), name, ipoe_netdev_setup); + if (dev == NULL) { + r = -ENOMEM; + goto failed; + } + + dev_net_set(dev, &init_net); + + r = dev_alloc_name(dev, name); + if (r < 0) { + r = -ENOMEM; + goto failed_free; + } + + ses = netdev_priv(dev); + atomic_set(&ses->refs, 0); + ses->dev = dev; + ses->addr = addr; + ses->peer_addr = peer_addr; + ses->link_dev = link_dev; + memcpy(ses->hwaddr, hwaddr, ETH_ALEN); +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35) + ses->rx_stats = alloc_percpu(struct ipoe_stats); + ses->tx_stats = alloc_percpu(struct ipoe_stats); + if (!ses->rx_stats || !ses->tx_stats) { + r = -ENOMEM; + goto failed_free; + } +#endif + + if (link_dev) { + dev->features = link_dev->features; + memcpy(dev->dev_addr, link_dev->dev_addr, ETH_ALEN); + memcpy(dev->broadcast, link_dev->broadcast, ETH_ALEN); + } + + if (addr) + dev->flags |= IFF_NOARP; + else + dev->flags &= ~IFF_NOARP; + + rtnl_lock(); + r = register_netdevice(dev); + rtnl_unlock(); + if (r < 0) + goto failed_free; + + down(&ipoe_wlock); + if (peer_addr) + list_add_tail_rcu(&ses->entry, &ipoe_list[h]); + list_add_tail(&ses->entry2, &ipoe_list2); + r = dev->ifindex; + up(&ipoe_wlock); + + return r; + +failed_free: + free_netdev(dev); +failed: + if (link_dev) + dev_put(link_dev); + return r; +} + +static int ipoe_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info) +{ + struct sk_buff *msg; + void *hdr; + int ret = -ENOBUFS; + + msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); + if (!msg) { + ret = -ENOMEM; + goto out; + } + + hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, + &ipoe_nl_family, 0, IPOE_CMD_NOOP); + if (IS_ERR(hdr)) { + ret = PTR_ERR(hdr); + goto err_out; + } + + genlmsg_end(msg, hdr); + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34) + return genlmsg_unicast(msg, info->snd_pid); +#else + return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid); +#endif + +err_out: + nlmsg_free(msg); + +out: + return ret; +} + +static int ipoe_nl_cmd_create(struct sk_buff *skb, struct genl_info *info) +{ + struct sk_buff *msg; + void *hdr; + __be32 peer_addr = 0, addr = 0; + int ret = 0; + char ifname[IFNAMSIZ]; + __u8 hwaddr[ETH_ALEN]; + struct ipoe_session *ses; + //struct net *net = genl_info_net(info); + + if (info->attrs[IPOE_ATTR_PEER_ADDR]) { + peer_addr = nla_get_be32(info->attrs[IPOE_ATTR_PEER_ADDR]); + if (peer_addr) { + ses = ipoe_lookup(peer_addr); + if (ses) { + atomic_dec(&ses->refs); + return -EEXIST; + } + } + } + + if (info->attrs[IPOE_ATTR_ADDR]) + addr = nla_get_be32(info->attrs[IPOE_ATTR_ADDR]); + + if (info->attrs[IPOE_ATTR_IFNAME]) + nla_strlcpy(ifname, info->attrs[IPOE_ATTR_IFNAME], IFNAMSIZ - 1); + + if (info->attrs[IPOE_ATTR_HWADDR]) + nla_memcpy(hwaddr, info->attrs[IPOE_ATTR_HWADDR], ETH_ALEN); + else + memset(hwaddr, 0, sizeof(hwaddr)); + + msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); + if (!msg) { + ret = -ENOMEM; + goto out; + } + + hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, + &ipoe_nl_family, 0, IPOE_CMD_CREATE); + if (IS_ERR(hdr)) { + ret = PTR_ERR(hdr); + goto err_out; + } + + //pr_info("ipoe: create %08x %08x %s\n", peer_addr, addr, info->attrs[IPOE_ATTR_IFNAME] ? ifname : "-"); + + ret = ipoe_create(peer_addr, addr, info->attrs[IPOE_ATTR_IFNAME] ? ifname : NULL, hwaddr); + + if (ret < 0) { + nlmsg_free(msg); + return ret; + } + + nla_put_u32(msg, IPOE_ATTR_IFINDEX, ret); + + genlmsg_end(msg, hdr); +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34) + return genlmsg_unicast(msg, info->snd_pid); +#else + return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid); +#endif + +err_out: + nlmsg_free(msg); + +out: + return ret; +} + +static int ipoe_nl_cmd_delete(struct sk_buff *skb, struct genl_info *info) +{ + struct net_device *dev; + struct ipoe_session *ses; + int ifindex; + int r = 0; + int ret = -EINVAL; + + if (!info->attrs[IPOE_ATTR_IFINDEX]) + return -EINVAL; + + ifindex = nla_get_u32(info->attrs[IPOE_ATTR_IFINDEX]); + + down(&ipoe_wlock); + + rcu_read_lock(); +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34) + dev = dev_get_by_index_rcu(ifindex); +#else + dev = dev_get_by_index_rcu(&init_net, ifindex); +#endif + if (!dev || dev->header_ops != &ipoe_hard_header_ops) + r = 1; + rcu_read_unlock(); + + if (r) + goto out_unlock; + + ses = netdev_priv(dev); + + //pr_info("ipoe: delete %08x\n", ses->peer_addr); + + if (ses->peer_addr) + list_del_rcu(&ses->entry); + list_del(&ses->entry2); + + up(&ipoe_wlock); + + synchronize_rcu(); + + while (atomic_read(&ses->refs)) + schedule_timeout_uninterruptible(1); + + if (ses->link_dev) + dev_put(ses->link_dev); + + unregister_netdev(ses->dev); + + ret = 0; + +out_unlock: + up(&ipoe_wlock); + return ret; +} + +static int ipoe_nl_cmd_modify(struct sk_buff *skb, struct genl_info *info) +{ + int ret = -EINVAL, r = 0; + char ifname[IFNAMSIZ]; + struct net_device *dev, *link_dev, *old_dev; + struct ipoe_session *ses, *ses1; + int ifindex; + __be32 peer_addr; + + if (!info->attrs[IPOE_ATTR_IFINDEX]) + return -EINVAL; + + down(&ipoe_wlock); + + ifindex = nla_get_u32(info->attrs[IPOE_ATTR_IFINDEX]); + + rcu_read_lock(); +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34) + dev = dev_get_by_index_rcu(ifindex); +#else + dev = dev_get_by_index_rcu(&init_net, ifindex); +#endif + if (!dev || dev->header_ops != &ipoe_hard_header_ops) + r = 1; + rcu_read_unlock(); + + if (r) + goto out_unlock; + + ses = netdev_priv(dev); + + if (info->attrs[IPOE_ATTR_PEER_ADDR]) { + peer_addr = nla_get_be32(info->attrs[IPOE_ATTR_PEER_ADDR]); + if (peer_addr) { + ses1 = ipoe_lookup(peer_addr); + if (ses1) { + atomic_dec(&ses1->refs); + if (ses1 != ses) { + ret = -EEXIST; + goto out_unlock; + } + } + } + + if (ses->peer_addr) { + list_del_rcu(&ses->entry); + synchronize_rcu(); + } + + ses->peer_addr = peer_addr; + + if (peer_addr) + list_add_tail_rcu(&ses->entry, &ipoe_list[hash_addr(peer_addr)]); + } + + if (info->attrs[IPOE_ATTR_IFNAME]) { + nla_strlcpy(ifname, info->attrs[IPOE_ATTR_IFNAME], IFNAMSIZ - 1); + + if (*ifname) { + link_dev = dev_get_by_name(&init_net, ifname); + + if (!link_dev) + goto out_unlock; + } else + link_dev = NULL; + + old_dev = ses->link_dev; + ses->link_dev = link_dev; + + if (link_dev) { + ses->dev->features = link_dev->features; + memcpy(dev->dev_addr, link_dev->dev_addr, ETH_ALEN); + memcpy(dev->broadcast, link_dev->broadcast, ETH_ALEN); + } + + if (old_dev) + dev_put(old_dev); + } + + if (info->attrs[IPOE_ATTR_ADDR]) { + ses->addr = nla_get_be32(info->attrs[IPOE_ATTR_ADDR]); + if (ses->addr) + dev->flags |= IFF_NOARP; + else + dev->flags &= ~IFF_NOARP; + } + + if (info->attrs[IPOE_ATTR_HWADDR]) + nla_memcpy(ses->hwaddr, info->attrs[IPOE_ATTR_HWADDR], ETH_ALEN); + + //pr_info("ipoe: modify %08x %08x\n", ses->peer_addr, ses->addr); + + ret = 0; + +out_unlock: + up(&ipoe_wlock); + return ret; +} + +static int fill_info(struct sk_buff *skb, struct ipoe_session *ses, u32 pid, u32 seq) +{ + void *hdr; + + hdr = genlmsg_put(skb, pid, seq, &ipoe_nl_family, NLM_F_MULTI, IPOE_CMD_GET); + if (!hdr) + return -EMSGSIZE; + + NLA_PUT_U32(skb, IPOE_ATTR_IFINDEX, ses->dev->ifindex); + NLA_PUT_U32(skb, IPOE_ATTR_PEER_ADDR, ses->peer_addr); + NLA_PUT_U32(skb, IPOE_ATTR_ADDR, ses->addr); + + return genlmsg_end(skb, hdr); + +nla_put_failure: + genlmsg_cancel(skb, hdr); + return -EMSGSIZE; +} + +static int ipoe_nl_cmd_dump_sessions(struct sk_buff *skb, struct netlink_callback *cb) +{ + struct ipoe_session *ses; + int idx = 0, start_idx = cb->args[0]; + + down(&ipoe_wlock); + + list_for_each_entry(ses, &ipoe_list2, entry2) { + if (idx > start_idx) + start_idx = 0; + + if (idx++ < start_idx) + continue; + + if (fill_info(skb, ses, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq) < 0) + break; + } + + up(&ipoe_wlock); + + cb->args[0] = idx; + + return skb->len; +} + +static int ipoe_nl_cmd_add_net(struct sk_buff *skb, struct genl_info *info) +{ + struct ipoe_network *n; + + if (!info->attrs[IPOE_ATTR_ADDR] || !info->attrs[IPOE_ATTR_MASK]) + return -EINVAL; + + n = kmalloc(sizeof(*n), GFP_KERNEL); + if (!n) + return -ENOMEM; + + n->addr = nla_get_u32(info->attrs[IPOE_ATTR_ADDR]); + n->mask = nla_get_u32(info->attrs[IPOE_ATTR_MASK]); + //pr_info("add net %08x/%08x\n", n->addr, n->mask); + + down(&ipoe_wlock); + list_add_tail_rcu(&n->entry, &ipoe_networks); + up(&ipoe_wlock); + + return 0; +} + +static int ipoe_nl_cmd_del_net(struct sk_buff *skb, struct genl_info *info) +{ + struct ipoe_network *n; + __be32 addr; + + if (!info->attrs[IPOE_ATTR_ADDR]) + return -EINVAL; + + addr = nla_get_u32(info->attrs[IPOE_ATTR_ADDR]); + + rcu_read_lock(); + list_for_each_entry_rcu(n, &ipoe_networks, entry) { + if (!addr || addr == n->addr) { + //pr_info("del net %08x/%08x\n", n->addr, n->mask); + list_del_rcu(&n->entry); +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0) + kfree_rcu(n, rcu_head); +#else + call_rcu(&n->rcu_head, __kfree_rcu); +#endif + } + } + rcu_read_unlock(); + + synchronize_rcu(); + + return 0; +} + + +static struct nla_policy ipoe_nl_policy[IPOE_ATTR_MAX + 1] = { + [IPOE_ATTR_NONE] = { .type = NLA_UNSPEC, }, + [IPOE_ATTR_ADDR] = { .type = NLA_U32, }, + [IPOE_ATTR_PEER_ADDR] = { .type = NLA_U32, }, + [IPOE_ATTR_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, + [IPOE_ATTR_HWADDR] = { .type = NLA_U64 }, + [IPOE_ATTR_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, + [IPOE_ATTR_MASK] = { .type = NLA_U32, }, +}; + +static struct genl_ops ipoe_nl_ops[] = { + { + .cmd = IPOE_CMD_NOOP, + .doit = ipoe_nl_cmd_noop, + .policy = ipoe_nl_policy, + /* can be retrieved by unprivileged users */ + }, + { + .cmd = IPOE_CMD_CREATE, + .doit = ipoe_nl_cmd_create, + .policy = ipoe_nl_policy, + .flags = GENL_ADMIN_PERM, + }, + { + .cmd = IPOE_CMD_DELETE, + .doit = ipoe_nl_cmd_delete, + .policy = ipoe_nl_policy, + .flags = GENL_ADMIN_PERM, + }, + { + .cmd = IPOE_CMD_MODIFY, + .doit = ipoe_nl_cmd_modify, + .policy = ipoe_nl_policy, + .flags = GENL_ADMIN_PERM, + }, + { + .cmd = IPOE_CMD_GET, + .dumpit = ipoe_nl_cmd_dump_sessions, + .policy = ipoe_nl_policy, + }, + { + .cmd = IPOE_CMD_ADD_NET, + .doit = ipoe_nl_cmd_add_net, + .policy = ipoe_nl_policy, + .flags = GENL_ADMIN_PERM, + }, + { + .cmd = IPOE_CMD_DEL_NET, + .doit = ipoe_nl_cmd_del_net, + .policy = ipoe_nl_policy, + .flags = GENL_ADMIN_PERM, + }, +}; + +static struct genl_family ipoe_nl_family = { + .id = GENL_ID_GENERATE, + .name = IPOE_GENL_NAME, + .version = IPOE_GENL_VERSION, + .hdrsize = 0, + .maxattr = IPOE_ATTR_MAX, +}; + +static struct genl_multicast_group ipoe_nl_mcg = { + .name = IPOE_GENL_MCG_PKT, +}; + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34) +static const struct net_device_ops ipoe_netdev_ops = { + .ndo_start_xmit = ipoe_xmit, +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35) + .ndo_get_stats64 = ipoe_stats64, +#endif +}; +#endif + +static struct packet_type ip_packet_type = { + .type = __constant_htons(ETH_P_IP), + .func = ipoe_rcv, +}; + +static struct packet_type arp_packet_type = { + .type = __constant_htons(ETH_P_ARP), + .func = ipoe_rcv_arp, +}; + +/*static struct pernet_operations ipoe_net_ops = { + .init = ipoe_init_net, + .exit = ipoe_exit_net, + .id = &ipoe_net_id, + .size = sizeof(struct ipoe_net), +};*/ + +static int __init ipoe_init(void) +{ + int err, i; + + printk("IPoE session driver v0.1\n"); + + /*err = register_pernet_device(&ipoe_net_ops); + if (err < 0) + return err;*/ + for (i = 0; i < HASH_BITS + 1; i++) { + INIT_LIST_HEAD(&ipoe_list[i]); + INIT_LIST_HEAD(&ipoe_list1_u[i]); + } + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) + err = genl_register_family(&ipoe_nl_family); + if (err < 0) { + printk(KERN_INFO "ipoe: can't register netlink interface\n"); + goto out; + } + + for (i = 0; i < ARRAY_SIZE(ipoe_nl_ops); i++) { + err = genl_register_ops(&ipoe_nl_family, &ipoe_nl_ops[i]); + if (err) + break; + } + + if (err < 0) { + printk(KERN_INFO "ipoe: can't register netlink interface\n"); + goto out_unreg; + } +#else + err = genl_register_family_with_ops(&ipoe_nl_family, ipoe_nl_ops, + ARRAY_SIZE(ipoe_nl_ops)); + if (err < 0) { + printk(KERN_INFO "ipoe: can't register netlink interface\n"); + goto out; + } +#endif + + err = genl_register_mc_group(&ipoe_nl_family, &ipoe_nl_mcg); + if (err < 0) { + printk(KERN_INFO "ipoe: can't register netlink multicast group\n"); + goto out_unreg; + } + + skb_queue_head_init(&ipoe_queue); + INIT_WORK(&ipoe_queue_work, ipoe_process_queue); + + dev_add_pack(&ip_packet_type); + dev_add_pack(&arp_packet_type); + + return 0; + +out_unreg: + genl_unregister_family(&ipoe_nl_family); +out: + return err; +} + +static void __exit ipoe_fini(void) +{ + struct ipoe_network *n; + struct ipoe_entry_u *e; + struct ipoe_session *ses; + int i; + + genl_unregister_mc_group(&ipoe_nl_family, &ipoe_nl_mcg); + genl_unregister_family(&ipoe_nl_family); + + dev_remove_pack(&ip_packet_type); + dev_remove_pack(&arp_packet_type); + + flush_work(&ipoe_queue_work); + skb_queue_purge(&ipoe_queue); + + del_timer(&ipoe_timer_u); + + down(&ipoe_wlock); + up(&ipoe_wlock); + + for (i = 0; i < HASH_BITS; i++) + rcu_assign_pointer(ipoe_list[i].next, &ipoe_list[i]); + + rcu_barrier(); + + while (!list_empty(&ipoe_list2)) { + ses = list_entry(ipoe_list2.next, typeof(*ses), entry2); + list_del(&ses->entry2); + + if (ses->link_dev) + dev_put(ses->link_dev); + + unregister_netdev(ses->dev); + } + + while (!list_empty(&ipoe_networks)) { + n = list_entry(ipoe_networks.next, typeof(*n), entry); + list_del(&n->entry); + kfree(n); + } + + while (!list_empty(&ipoe_list2_u)) { + e = list_entry(ipoe_list2_u.next, typeof(*e), entry2); + list_del(&e->entry2); + kfree(e); + } +} + +module_init(ipoe_init); +module_exit(ipoe_fini); +MODULE_LICENSE("GPL"); diff --git a/drivers/ipoe/ipoe.h b/drivers/ipoe/ipoe.h new file mode 100644 index 00000000..d40ac21a --- /dev/null +++ b/drivers/ipoe/ipoe.h @@ -0,0 +1,43 @@ +#ifndef __LINUX_IPOE_H +#define __LINUX_IPOE_H + +#include <linux/types.h> + +enum { + IPOE_CMD_NOOP, + IPOE_CMD_CREATE, + IPOE_CMD_DELETE, + IPOE_CMD_MODIFY, + IPOE_CMD_GET, + IPOE_CMD_ADD_NET, + IPOE_CMD_DEL_NET, + IPOE_REP_PKT, + __IPOE_CMD_MAX, +}; + +#define IPOE_CMD_MAX (__IPOE_CMD_MAX - 1) + +enum { + IPOE_ATTR_NONE, /* no data */ + IPOE_ATTR_ADDR, /* u32 */ + IPOE_ATTR_PEER_ADDR, /* u32 */ + IPOE_ATTR_IFNAME, /* u32 */ + IPOE_ATTR_HWADDR, /* u32 */ + IPOE_ATTR_MASK, /* u32 */ + IPOE_ATTR_IFINDEX, /* u32 */ + IPOE_ATTR_ETH_HDR, /* u32 */ + IPOE_ATTR_IP_HDR, /* u32 */ + __IPOE_ATTR_MAX, +}; + +#define IPOE_ATTR_MAX (__IPOE_ATTR_MAX - 1) + +/* + * NETLINK_GENERIC related info + */ +#define IPOE_GENL_NAME "IPoE" +#define IPOE_GENL_MCG_PKT "Packet" +#define IPOE_GENL_VERSION 0x1 + +#endif + diff --git a/drivers/pptp/CMakeLists.txt b/drivers/pptp/CMakeLists.txt new file mode 100644 index 00000000..fd732e6a --- /dev/null +++ b/drivers/pptp/CMakeLists.txt @@ -0,0 +1,19 @@ +if (NOT DEFINED KDIR) + set(KDIR "/usr/src/linux") +endif (NOT DEFINED KDIR) + +ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/driver/pptp.ko + COMMAND rm -rf ${CMAKE_CURRENT_BINARY_DIR}/driver + COMMAND mkdir ${CMAKE_CURRENT_BINARY_DIR}/driver + COMMAND ln -sf ${CMAKE_CURRENT_SOURCE_DIR}/* ${CMAKE_CURRENT_BINARY_DIR}/driver + COMMAND make -C ${KDIR} M=${CMAKE_CURRENT_BINARY_DIR}/driver modules + DEPENDS pptp.c +) + +ADD_CUSTOM_TARGET(pptp_drv ALL + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/driver/pptp.ko +) + +IF (NOT DEFINED CPACK_TYPE) + INSTALL(CODE "EXECUTE_PROCESS(COMMAND make -C ${KDIR} M=${CMAKE_CURRENT_BINARY_DIR}/driver modules_install)") +ENDIF (NOT DEFINED CPACK_TYPE) diff --git a/drivers/pptp/Makefile b/drivers/pptp/Makefile new file mode 100644 index 00000000..8ccbbedf --- /dev/null +++ b/drivers/pptp/Makefile @@ -0,0 +1,4 @@ +obj-m += pptp.o + +default: + make -C $(KDIR) M=$(PWD) modules diff --git a/drivers/pptp/gre.c b/drivers/pptp/gre.c new file mode 100644 index 00000000..77886d5d --- /dev/null +++ b/drivers/pptp/gre.c @@ -0,0 +1,220 @@ +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/init.h> +#include <linux/skbuff.h> +#include <linux/in.h> +#include <linux/netdevice.h> +#include <linux/version.h> +#include <linux/spinlock.h> +#include <net/protocol.h> + +#include "gre.h" + +struct gre_protocol *gre_proto[GREPROTO_MAX] ____cacheline_aligned_in_smp; +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) +static rwlock_t gre_proto_lock=RW_LOCK_UNLOCKED; +#else +static DEFINE_SPINLOCK(gre_proto_lock); +#endif + +int gre_add_protocol(struct gre_protocol *proto, u8 version) +{ + int ret; + + if (version >= GREPROTO_MAX) + return -EINVAL; + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + write_lock_bh(&gre_proto_lock); +#else + spin_lock(&gre_proto_lock); +#endif + if (gre_proto[version]) { + ret = -EAGAIN; + } else { +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + gre_proto[version] = proto; +#else + rcu_assign_pointer(gre_proto[version], proto); +#endif + ret = 0; + } +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + write_unlock_bh(&gre_proto_lock); +#else + spin_unlock(&gre_proto_lock); +#endif + + return ret; +} + +int gre_del_protocol(struct gre_protocol *proto, u8 version) +{ + if (version >= GREPROTO_MAX) + goto out_err; + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + write_lock_bh(&gre_proto_lock); +#else + spin_lock(&gre_proto_lock); +#endif + if (gre_proto[version] == proto) +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + gre_proto[version] = NULL; +#else + rcu_assign_pointer(gre_proto[version], NULL); +#endif + else + goto out_err_unlock; +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + write_unlock_bh(&gre_proto_lock); +#else + spin_unlock(&gre_proto_lock); + synchronize_rcu(); +#endif + return 0; + +out_err_unlock: +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + write_unlock_bh(&gre_proto_lock); +#else + spin_unlock(&gre_proto_lock); +#endif +out_err: + return -EINVAL; +} + +static int gre_rcv(struct sk_buff *skb) +{ + u8 ver; + int ret; + struct gre_protocol *proto; + + if (!pskb_may_pull(skb, 12)) + goto drop_nolock; + + ver = skb->data[1]&0x7f; + if (ver >= GREPROTO_MAX) + goto drop_nolock; + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + read_lock(&gre_proto_lock); + proto = gre_proto[ver]; +#else + rcu_read_lock(); + proto = rcu_dereference(gre_proto[ver]); +#endif + if (!proto || !proto->handler) + goto drop; + + ret = proto->handler(skb); + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + read_unlock(&gre_proto_lock); +#else + rcu_read_unlock(); +#endif + + return ret; + +drop: +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + read_unlock(&gre_proto_lock); +#else + rcu_read_unlock(); +#endif +drop_nolock: + kfree_skb(skb); + return NET_RX_DROP; +} + +static void gre_err(struct sk_buff *skb, u32 info) +{ + u8 ver; + struct gre_protocol *proto; + + if (!pskb_may_pull(skb, 12)) + goto drop_nolock; + + ver=skb->data[1]&0x7f; + if (ver>=GREPROTO_MAX) + goto drop_nolock; + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + read_lock(&gre_proto_lock); + proto = gre_proto[ver]; +#else + rcu_read_lock(); + proto = rcu_dereference(gre_proto[ver]); +#endif + if (!proto || !proto->err_handler) + goto drop; + + proto->err_handler(skb, info); +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + read_unlock(&gre_proto_lock); +#else + rcu_read_unlock(); +#endif + + return; + +drop: +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + read_unlock(&gre_proto_lock); +#else + rcu_read_unlock(); +#endif +drop_nolock: + kfree_skb(skb); +} + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) +static struct inet_protocol net_gre_protocol = { + .handler = gre_rcv, + .err_handler = gre_err, + .protocol = IPPROTO_GRE, + .name = "GRE", +}; +#else +static struct net_protocol net_gre_protocol = { + .handler = gre_rcv, + .err_handler = gre_err, +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,24) + .netns_ok=1, +#endif +}; +#endif + +static int __init gre_init(void) +{ + printk(KERN_INFO "GRE over IPv4 demultiplexor driver"); + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + inet_add_protocol(&net_gre_protocol); +#else + if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) { + printk(KERN_INFO "gre: can't add protocol\n"); + return -EAGAIN; + } +#endif + return 0; +} + +static void __exit gre_exit(void) +{ +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + inet_del_protocol(&net_gre_protocol); +#else + inet_del_protocol(&net_gre_protocol, IPPROTO_GRE); +#endif +} + +module_init(gre_init); +module_exit(gre_exit); + +MODULE_DESCRIPTION("GRE over IPv4 demultiplexor driver"); +MODULE_AUTHOR("Kozlov D. (xeb@mail.ru)"); +MODULE_LICENSE("GPL"); +EXPORT_SYMBOL_GPL(gre_add_protocol); +EXPORT_SYMBOL_GPL(gre_del_protocol); diff --git a/drivers/pptp/gre.h b/drivers/pptp/gre.h new file mode 100644 index 00000000..2ca7f749 --- /dev/null +++ b/drivers/pptp/gre.h @@ -0,0 +1,18 @@ +#ifndef __LINUX_GRE_H +#define __LINUX_GRE_H + +#include <linux/skbuff.h> + +#define GREPROTO_CISCO 0 +#define GREPROTO_PPTP 1 +#define GREPROTO_MAX 2 + +struct gre_protocol { + int (*handler)(struct sk_buff *skb); + void (*err_handler)(struct sk_buff *skb, u32 info); +}; + +int gre_add_protocol(struct gre_protocol *proto, u8 version); +int gre_del_protocol(struct gre_protocol *proto, u8 version); + +#endif diff --git a/drivers/pptp/if_pppox.h b/drivers/pptp/if_pppox.h new file mode 100644 index 00000000..bc05b533 --- /dev/null +++ b/drivers/pptp/if_pppox.h @@ -0,0 +1,222 @@ +/*************************************************************************** + * Linux PPP over X - Generic PPP transport layer sockets + * Linux PPP over Ethernet (PPPoE) Socket Implementation (RFC 2516) + * + * This file supplies definitions required by the PPP over Ethernet driver + * (pppox.c). All version information wrt this file is located in pppox.c + * + * License: + * 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. + * + */ + +#ifndef __LINUX_IF_PPPOX_H +#define __LINUX_IF_PPPOX_H + + +#include <asm/types.h> +#include <asm/byteorder.h> +#include <linux/version.h> + +#ifdef __KERNEL__ +#include <linux/in.h> +#include <linux/if_ether.h> +#include <linux/if.h> +#include <linux/netdevice.h> +#include <linux/ppp_channel.h> +#endif /* __KERNEL__ */ + +/* For user-space programs to pick up these definitions + * which they wouldn't get otherwise without defining __KERNEL__ + */ +#ifndef AF_PPPOX +#define AF_PPPOX 24 +#define PF_PPPOX AF_PPPOX +#endif /* !(AF_PPPOX) */ + +/************************************************************************ + * PPPoE addressing definition + */ +typedef __u16 sid_t; +struct pppoe_addr{ + sid_t sid; /* Session identifier */ + unsigned char remote[ETH_ALEN]; /* Remote address */ + char dev[IFNAMSIZ]; /* Local device to use */ +}; + +struct pptp_addr{ + __u16 call_id; + struct in_addr sin_addr; +}; +/************************************************************************ + * Protocols supported by AF_PPPOX + */ +#define PX_PROTO_OE 0 /* Currently just PPPoE */ +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,22) +#define PX_PROTO_PPTP 1 +#define PX_MAX_PROTO 2 +#else +#define PX_PROTO_PPTP 2 +#define PX_MAX_PROTO 3 +#endif + +struct sockaddr_pppox { + sa_family_t sa_family; /* address family, AF_PPPOX */ + unsigned int sa_protocol; /* protocol identifier */ + union{ + struct pppoe_addr pppoe; + struct pptp_addr pptp; + }sa_addr; +}__attribute__ ((packed)); + + +/********************************************************************* + * + * ioctl interface for defining forwarding of connections + * + ********************************************************************/ + +#define PPPOEIOCSFWD _IOW(0xB1 ,0, size_t) +#define PPPOEIOCDFWD _IO(0xB1 ,1) +/*#define PPPOEIOCGFWD _IOWR(0xB1,2, size_t)*/ + +/* Codes to identify message types */ +#define PADI_CODE 0x09 +#define PADO_CODE 0x07 +#define PADR_CODE 0x19 +#define PADS_CODE 0x65 +#define PADT_CODE 0xa7 +struct pppoe_tag { + __u16 tag_type; + __u16 tag_len; + char tag_data[0]; +} __attribute ((packed)); + +/* Tag identifiers */ +#define PTT_EOL __constant_htons(0x0000) +#define PTT_SRV_NAME __constant_htons(0x0101) +#define PTT_AC_NAME __constant_htons(0x0102) +#define PTT_HOST_UNIQ __constant_htons(0x0103) +#define PTT_AC_COOKIE __constant_htons(0x0104) +#define PTT_VENDOR __constant_htons(0x0105) +#define PTT_RELAY_SID __constant_htons(0x0110) +#define PTT_SRV_ERR __constant_htons(0x0201) +#define PTT_SYS_ERR __constant_htons(0x0202) +#define PTT_GEN_ERR __constant_htons(0x0203) + +struct pppoe_hdr { +#if defined(__LITTLE_ENDIAN_BITFIELD) + __u8 ver : 4; + __u8 type : 4; +#elif defined(__BIG_ENDIAN_BITFIELD) + __u8 type : 4; + __u8 ver : 4; +#else +#error "Please fix <asm/byteorder.h>" +#endif + __u8 code; + __u16 sid; + __u16 length; + struct pppoe_tag tag[0]; +} __attribute__ ((packed)); + + +/* Socket options */ +#define PPTP_SO_TIMEOUT 1 + + +#ifdef __KERNEL__ +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) +struct pppoe_opt { + struct net_device *dev; /* device associated with socket*/ + struct pppoe_addr pa; /* what this socket is bound to*/ + struct sockaddr_pppox relay; /* what socket data will be + relayed to (PPPoE relaying) */ +}; +#endif +struct pptp_opt { + struct pptp_addr src_addr; + struct pptp_addr dst_addr; + __u32 ack_sent, ack_recv; + __u32 seq_sent, seq_recv; + int ppp_flags; +}; +#define PPTP_FLAG_PAUSE 0 +#define PPTP_FLAG_PROC 1 + +#include <net/sock.h> + +struct pppox_sock { + /* struct sock must be the first member of pppox_sock */ + #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + struct ppp_channel chan; + struct sock *sk; + #else + struct sock sk; + struct ppp_channel chan; + #endif + struct pppox_sock *next; /* for hash table */ + union { + struct pppoe_opt pppoe; + struct pptp_opt pptp; + } proto; + unsigned short num; +}; +#define pppoe_dev proto.pppoe.dev +#define pppoe_pa proto.pppoe.pa +#define pppoe_relay proto.pppoe.relay + +static inline struct pppox_sock *pppox_sk(struct sock *sk) +{ + #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + return (struct pppox_sock *)sk->protinfo.pppox; + #else + return (struct pppox_sock *)sk; + #endif +} + +static inline struct sock *sk_pppox(struct pppox_sock *po) +{ + #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + return po->sk; + #else + return (struct sock *)po; + #endif +} + +struct module; + +struct pppox_proto { + #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24) + int (*create)(struct socket *sock); + #else + int (*create)(struct net *net, struct socket *sock); + #endif + int (*ioctl)(struct socket *sock, unsigned int cmd, + unsigned long arg); + #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15) + struct module *owner; + #endif +}; + +extern int register_pppox_proto(int proto_num, struct pppox_proto *pp); +extern void unregister_pppox_proto(int proto_num); +extern void pppox_unbind_sock(struct sock *sk);/* delete ppp-channel binding */ +extern int pppox_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); + +/* PPPoX socket states */ +enum { + PPPOX_NONE = 0, /* initial state */ + PPPOX_CONNECTED = 1, /* connection established ==TCP_ESTABLISHED */ + PPPOX_BOUND = 2, /* bound to ppp device */ + PPPOX_RELAY = 4, /* forwarding is enabled */ + PPPOX_ZOMBIE = 8, /* dead, but still bound to ppp device */ + PPPOX_DEAD = 16 /* dead, useless, please clean me up!*/ +}; + +#endif /* __KERNEL__ */ + +#endif /* !(__LINUX_IF_PPPOX_H) */ diff --git a/drivers/pptp/pptp.c b/drivers/pptp/pptp.c new file mode 100644 index 00000000..78853fcb --- /dev/null +++ b/drivers/pptp/pptp.c @@ -0,0 +1,1272 @@ +/* + * Point-to-Point Tunneling Protocol for Linux + * + * Authors: Kozlov D. (xeb@mail.ru) + * + * 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. + * + */ + +#include <linux/string.h> +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/errno.h> +#include <linux/netdevice.h> +#include <linux/net.h> +#include <linux/skbuff.h> +#include <linux/init.h> +#include <linux/ppp_channel.h> +#include <linux/ppp_defs.h> +#include "if_pppox.h" +#include <linux/if_ppp.h> +#include <linux/notifier.h> +#include <linux/file.h> +#include <linux/in.h> +#include <linux/ip.h> +#include <linux/netfilter.h> +#include <linux/netfilter_ipv4.h> +#include <linux/version.h> + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) +#include <asm/bitops.h> +#endif + +#include <net/sock.h> +#include <net/protocol.h> +#include <net/ip.h> +#include <net/icmp.h> +#include <net/route.h> + +#include <asm/uaccess.h> + +#define DEBUG +//#define CONFIG_GRE + +#if defined(CONFIG_GRE) || defined(CONFIG_GRE_MODULE) +#include "gre.h" +#endif + +#define PPTP_DRIVER_VERSION "0.8.5" + +static int log_level=0; +static int log_packets=10; + +#define MAX_CALLID 65535 +#define PPP_LCP_ECHOREQ 0x09 +#define PPP_LCP_ECHOREP 0x0A + +static DECLARE_BITMAP(callid_bitmap, MAX_CALLID + 1); +static struct pppox_sock **callid_sock; + +#define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP) + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) +#define INIT_TIMER(_timer,_routine,_data) \ +do { \ + (_timer)->function=_routine; \ + (_timer)->data=_data; \ + init_timer(_timer); \ +} while (0); + +static inline void *kzalloc(size_t size,int gfp) +{ + void *p=kmalloc(size,gfp); + memset(p,0,size); + return p; +} + +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,4,20) +static inline void nf_reset(struct sk_buff *skb) +{ +#ifdef CONFIG_NETFILTER + nf_conntrack_put(skb->nfct); + skb->nfct=NULL; +#ifdef CONFIG_NETFILTER_DEBUG + skb->nf_debug=0; +#endif +#endif +} +#define __user +#endif + +/** + * __ffs - find first bit in word. + * @word: The word to search + * + * Undefined if no bit exists, so code should check against 0 first. + */ +static inline unsigned long __ffs(unsigned long word) +{ + int num = 0; + +#if BITS_PER_LONG == 64 + if ((word & 0xffffffff) == 0) { + num += 32; + word >>= 32; + } +#endif + if ((word & 0xffff) == 0) { + num += 16; + word >>= 16; + } + if ((word & 0xff) == 0) { + num += 8; + word >>= 8; + } + if ((word & 0xf) == 0) { + num += 4; + word >>= 4; + } + if ((word & 0x3) == 0) { + num += 2; + word >>= 2; + } + if ((word & 0x1) == 0) + num += 1; + return num; +} + +#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG) +/* + * Find the next set bit in a memory region. + */ +static unsigned long find_next_bit(const unsigned long *addr, unsigned long size, + unsigned long offset) +{ + const unsigned long *p = addr + BITOP_WORD(offset); + unsigned long result = offset & ~(BITS_PER_LONG-1); + unsigned long tmp; + + if (offset >= size) + return size; + size -= result; + offset %= BITS_PER_LONG; + if (offset) { + tmp = *(p++); + tmp &= (~0UL << offset); + if (size < BITS_PER_LONG) + goto found_first; + if (tmp) + goto found_middle; + size -= BITS_PER_LONG; + result += BITS_PER_LONG; + } + while (size & ~(BITS_PER_LONG-1)) { + if ((tmp = *(p++))) + goto found_middle; + result += BITS_PER_LONG; + size -= BITS_PER_LONG; + } + if (!size) + return result; + tmp = *p; + +found_first: + tmp &= (~0UL >> (BITS_PER_LONG - size)); + if (tmp == 0UL) /* Are any bits set? */ + return result + size; /* Nope. */ +found_middle: + return result + __ffs(tmp); +} +#endif + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) +static rwlock_t chan_lock=RW_LOCK_UNLOCKED; +#define SK_STATE(sk) (sk)->state +#else +static DEFINE_SPINLOCK(chan_lock); +#define SK_STATE(sk) (sk)->sk_state +#endif + +static int pptp_xmit(struct ppp_channel *chan, struct sk_buff *skb); +static int pptp_ppp_ioctl(struct ppp_channel *chan, unsigned int cmd, + unsigned long arg); +static int pptp_rcv_core(struct sock *sk,struct sk_buff *skb); + +static struct ppp_channel_ops pptp_chan_ops= { + .start_xmit = pptp_xmit, + .ioctl=pptp_ppp_ioctl, +}; + + +#define MISSING_WINDOW 20 +#define WRAPPED( curseq, lastseq) \ + ((((curseq) & 0xffffff00) == 0) && \ + (((lastseq) & 0xffffff00 ) == 0xffffff00)) + +/* gre header structure: -------------------------------------------- */ + +#define PPTP_GRE_PROTO 0x880B +#define PPTP_GRE_VER 0x1 + +#define PPTP_GRE_FLAG_C 0x80 +#define PPTP_GRE_FLAG_R 0x40 +#define PPTP_GRE_FLAG_K 0x20 +#define PPTP_GRE_FLAG_S 0x10 +#define PPTP_GRE_FLAG_A 0x80 + +#define PPTP_GRE_IS_C(f) ((f)&PPTP_GRE_FLAG_C) +#define PPTP_GRE_IS_R(f) ((f)&PPTP_GRE_FLAG_R) +#define PPTP_GRE_IS_K(f) ((f)&PPTP_GRE_FLAG_K) +#define PPTP_GRE_IS_S(f) ((f)&PPTP_GRE_FLAG_S) +#define PPTP_GRE_IS_A(f) ((f)&PPTP_GRE_FLAG_A) + +struct pptp_gre_header { + u8 flags; /* bitfield */ + u8 ver; /* should be PPTP_GRE_VER (enhanced GRE) */ + u16 protocol; /* should be PPTP_GRE_PROTO (ppp-encaps) */ + u16 payload_len; /* size of ppp payload, not inc. gre header */ + u16 call_id; /* peer's call_id for this session */ + u32 seq; /* sequence number. Present if S==1 */ + u32 ack; /* seq number of highest packet recieved by */ + /* sender in this session */ +} __packed; +#define PPTP_HEADER_OVERHEAD (2+sizeof(struct pptp_gre_header)) + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) +static struct pppox_sock * lookup_chan(u16 call_id, u32 s_addr) +#else +static struct pppox_sock * lookup_chan(u16 call_id, __be32 s_addr) +#endif +{ + struct pppox_sock *sock; + struct pptp_opt *opt; + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + rcu_read_lock(); + sock = rcu_dereference(callid_sock[call_id]); +#else + read_lock(&chan_lock); + sock = callid_sock[call_id]; +#endif + if (sock) { + opt=&sock->proto.pptp; + if (opt->dst_addr.sin_addr.s_addr!=s_addr) sock=NULL; + else sock_hold(sk_pppox(sock)); + } +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + rcu_read_unlock(); +#else + read_unlock(&chan_lock); +#endif + + return sock; +} + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) +static int lookup_chan_dst(u16 call_id, u32 d_addr) +#else +static int lookup_chan_dst(u16 call_id, __be32 d_addr) +#endif +{ + struct pppox_sock *sock; + struct pptp_opt *opt; + int i; + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + rcu_read_lock(); +#else + down(&chan_lock); +#endif + for(i = find_next_bit(callid_bitmap,MAX_CALLID,1); i < MAX_CALLID; + i = find_next_bit(callid_bitmap, MAX_CALLID, i + 1)){ +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + sock = rcu_dereference(callid_sock[i]); +#else + sock = callid_sock[i]; +#endif + if (!sock) + continue; + opt = &sock->proto.pptp; + if (opt->dst_addr.call_id == call_id && opt->dst_addr.sin_addr.s_addr == d_addr) break; + } +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + rcu_read_unlock(); +#else + up(&chan_lock); +#endif + + return i<MAX_CALLID; +} + +static int add_chan(struct pppox_sock *sock) +{ + static int call_id=0; + int res=-1; + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + spin_lock(&chan_lock); +#else + write_lock_bh(&chan_lock); +#endif + + if (!sock->proto.pptp.src_addr.call_id) + { + call_id=find_next_zero_bit(callid_bitmap,MAX_CALLID,call_id+1); + if (call_id==MAX_CALLID) + call_id=find_next_zero_bit(callid_bitmap,MAX_CALLID,1); + sock->proto.pptp.src_addr.call_id=call_id; + } + else if (test_bit(sock->proto.pptp.src_addr.call_id,callid_bitmap)) + goto exit; + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + rcu_assign_pointer(callid_sock[sock->proto.pptp.src_addr.call_id],sock); +#else + callid_sock[sock->proto.pptp.src_addr.call_id] = sock; +#endif + set_bit(sock->proto.pptp.src_addr.call_id,callid_bitmap); + res=0; + +exit: + #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + spin_unlock(&chan_lock); + #else + write_unlock_bh(&chan_lock); + #endif + + return res; +} + +static void del_chan(struct pppox_sock *sock) +{ +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + spin_lock(&chan_lock); +#else + write_lock_bh(&chan_lock); +#endif + clear_bit(sock->proto.pptp.src_addr.call_id,callid_bitmap); +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + rcu_assign_pointer(callid_sock[sock->proto.pptp.src_addr.call_id],NULL); + spin_unlock(&chan_lock); + synchronize_rcu(); +#else + callid_sock[sock->proto.pptp.src_addr.call_id] = NULL; + write_unlock_bh(&chan_lock); +#endif +} + +static int pptp_xmit(struct ppp_channel *chan, struct sk_buff *skb) +{ + struct sock *sk = (struct sock *) chan->private; + struct pppox_sock *po = pppox_sk(sk); + struct pptp_opt *opt=&po->proto.pptp; + struct pptp_gre_header *hdr; + unsigned int header_len=sizeof(*hdr); + int err=0; + int islcp; + int len; + unsigned char *data; + u32 seq_recv; + + + struct rtable *rt; /* Route to the other host */ + struct net_device *tdev; /* Device to other host */ + struct iphdr *iph; /* Our new IP header */ + int max_headroom; /* The extra header space needed */ + + if (SK_STATE(sk_pppox(po)) & PPPOX_DEAD) + goto tx_error; + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + { + struct rt_key key = { + .dst=opt->dst_addr.sin_addr.s_addr, + .src=opt->src_addr.sin_addr.s_addr, + .tos=RT_TOS(0), + }; + if ((err=ip_route_output_key(&rt, &key))) { + goto tx_error; + } + } +#else + { + struct flowi fl = { .oif = 0, + .nl_u = { .ip4_u = + { .daddr = opt->dst_addr.sin_addr.s_addr, + .saddr = opt->src_addr.sin_addr.s_addr, + .tos = RT_TOS(0) } }, + .proto = IPPROTO_GRE }; +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) + if ((err=ip_route_output_key(&rt, &fl))) { +#else + if ((err=ip_route_output_key(&init_net,&rt, &fl))) { +#endif + goto tx_error; + } + } +#endif +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36) + tdev = rt->u.dst.dev; +#else + tdev = rt->dst.dev; +#endif + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + max_headroom = ((tdev->hard_header_len+15)&~15) + sizeof(*iph)+sizeof(*hdr)+2; +#else + max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(*iph)+sizeof(*hdr)+2; +#endif + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22) + if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) { +#else + if (skb_headroom(skb) < max_headroom || skb_shared(skb) || + (skb_cloned(skb) && !skb_clone_writable(skb,0))) { +#endif + struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom); + if (!new_skb) { + ip_rt_put(rt); + goto tx_error; + } + if (skb->sk) + skb_set_owner_w(new_skb, skb->sk); + kfree_skb(skb); + skb = new_skb; + } + + data=skb->data; + islcp=((data[0] << 8) + data[1])== PPP_LCP && 1 <= data[2] && data[2] <= 7; + + /* compress protocol field */ + if ((opt->ppp_flags & SC_COMP_PROT) && data[0]==0 && !islcp) + skb_pull(skb,1); + + /* + * Put in the address/control bytes if necessary + */ + if ((opt->ppp_flags & SC_COMP_AC) == 0 || islcp) { + data=skb_push(skb,2); + data[0]=PPP_ALLSTATIONS; + data[1]=PPP_UI; + } + + len=skb->len; + + seq_recv = opt->seq_recv; + + if (opt->ack_sent == seq_recv) header_len-=sizeof(hdr->ack); + + // Push down and install GRE header + skb_push(skb,header_len); + hdr=(struct pptp_gre_header *)(skb->data); + + hdr->flags = PPTP_GRE_FLAG_K; + hdr->ver = PPTP_GRE_VER; + hdr->protocol = htons(PPTP_GRE_PROTO); + hdr->call_id = htons(opt->dst_addr.call_id); + + hdr->flags |= PPTP_GRE_FLAG_S; + hdr->seq = htonl(++opt->seq_sent); +#ifdef DEBUG + if (log_level>=3 && opt->seq_sent<=log_packets) + printk(KERN_INFO"PPTP[%i]: send packet: seq=%i",opt->src_addr.call_id,opt->seq_sent); +#endif + if (opt->ack_sent != seq_recv) { + /* send ack with this message */ + hdr->ver |= PPTP_GRE_FLAG_A; + hdr->ack = htonl(seq_recv); + opt->ack_sent = seq_recv; +#ifdef DEBUG + if (log_level>=3 && opt->seq_sent<=log_packets) + printk(" ack=%i",seq_recv); +#endif + } + hdr->payload_len = htons(len); +#ifdef DEBUG + if (log_level>=3 && opt->seq_sent<=log_packets) + printk("\n"); +#endif + + /* + * Push down and install the IP header. + */ + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31) + skb_reset_transport_header(skb); + skb_push(skb, sizeof(*iph)); + skb_reset_network_header(skb); +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22) + skb->transport_header = skb->network_header; + skb_push(skb, sizeof(*iph)); + skb_reset_network_header(skb); +#else + skb->h.raw = skb->nh.raw; + skb->nh.raw = skb_push(skb, sizeof(*iph)); +#endif + memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) + IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | + IPSKB_REROUTED); +#endif + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22) + iph = ip_hdr(skb); +#else + iph = skb->nh.iph; +#endif + iph->version = 4; + iph->ihl = sizeof(struct iphdr) >> 2; +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36) + if (ip_dont_fragment(sk, &rt->u.dst)) +#else + if (ip_dont_fragment(sk, &rt->dst)) +#endif + iph->frag_off = htons(IP_DF); + else + iph->frag_off = 0; + iph->protocol = IPPROTO_GRE; + iph->tos = 0; + iph->daddr = rt->rt_dst; + iph->saddr = rt->rt_src; +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + iph->ttl = sk->protinfo.af_inet.ttl; +#else +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36) + iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT); +#else + iph->ttl = dst_metric(&rt->dst, RTAX_HOPLIMIT); +#endif +#endif + iph->tot_len = htons(skb->len); + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31) + skb_dst_drop(skb); +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36) + skb_dst_set(skb,&rt->u.dst); +#else + skb_dst_set(skb,&rt->dst); +#endif +#else + dst_release(skb->dst); + skb->dst = &rt->u.dst; +#endif + + nf_reset(skb); + + skb->ip_summed = CHECKSUM_NONE; +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36) + ip_select_ident(iph, &rt->u.dst, NULL); +#else + ip_select_ident(iph, &rt->dst, NULL); +#endif + ip_send_check(iph); + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev, ip_send); +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) + err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev, dst_output); +#else + err = ip_local_out(skb); +#endif + +tx_error: + return 1; +} + +static int pptp_rcv_core(struct sock *sk,struct sk_buff *skb) +{ + struct pppox_sock *po = pppox_sk(sk); + struct pptp_opt *opt=&po->proto.pptp; + int headersize,payload_len,seq; + u8 *payload; + struct pptp_gre_header *header; + + if (!(SK_STATE(sk) & PPPOX_CONNECTED)) { + if (sock_queue_rcv_skb(sk, skb)) + goto drop; + return NET_RX_SUCCESS; + } + + header = (struct pptp_gre_header *)(skb->data); + + /* test if acknowledgement present */ + if (PPTP_GRE_IS_A(header->ver)){ + u32 ack = (PPTP_GRE_IS_S(header->flags))? + header->ack:header->seq; /* ack in different place if S = 0 */ + + ack = ntohl( ack); + + if (ack > opt->ack_recv) opt->ack_recv = ack; + /* also handle sequence number wrap-around */ + if (WRAPPED(ack,opt->ack_recv)) opt->ack_recv = ack; + } + + /* test if payload present */ + if (!PPTP_GRE_IS_S(header->flags)){ + goto drop; + } + + headersize = sizeof(*header); + payload_len = ntohs(header->payload_len); + seq = ntohl(header->seq); + + /* no ack present? */ + if (!PPTP_GRE_IS_A(header->ver)) headersize -= sizeof(header->ack); + /* check for incomplete packet (length smaller than expected) */ + if (skb->len - headersize < payload_len){ +#ifdef DEBUG + if (log_level>=1) + printk(KERN_INFO"PPTP: discarding truncated packet (expected %d, got %d bytes)\n", + payload_len, skb->len - headersize); +#endif + goto drop; + } + + payload=skb->data+headersize; + /* check for expected sequence number */ + if ( seq < opt->seq_recv + 1 || WRAPPED(opt->seq_recv, seq) ){ + if ( (payload[0] == PPP_ALLSTATIONS) && (payload[1] == PPP_UI) && + (PPP_PROTOCOL(payload) == PPP_LCP) && + ((payload[4] == PPP_LCP_ECHOREQ) || (payload[4] == PPP_LCP_ECHOREP)) ){ +#ifdef DEBUG + if ( log_level >= 1) + printk(KERN_INFO"PPTP[%i]: allowing old LCP Echo packet %d (expecting %d)\n", opt->src_addr.call_id, + seq, opt->seq_recv + 1); +#endif + goto allow_packet; + } +#ifdef DEBUG + if ( log_level >= 1) + printk(KERN_INFO"PPTP[%i]: discarding duplicate or old packet %d (expecting %d)\n",opt->src_addr.call_id, + seq, opt->seq_recv + 1); +#endif + }else{ + opt->seq_recv = seq; +allow_packet: +#ifdef DEBUG + if ( log_level >= 3 && opt->seq_sent<=log_packets) + printk(KERN_INFO"PPTP[%i]: accepting packet %d size=%i (%02x %02x %02x %02x %02x %02x)\n",opt->src_addr.call_id, seq,payload_len, + *(payload +0), + *(payload +1), + *(payload +2), + *(payload +3), + *(payload +4), + *(payload +5)); +#endif + + skb_pull(skb,headersize); + + if (payload[0] == PPP_ALLSTATIONS && payload[1] == PPP_UI){ + /* chop off address/control */ + if (skb->len < 3) + goto drop; + skb_pull(skb,2); + } + + if ((*skb->data) & 1){ + /* protocol is compressed */ + skb_push(skb, 1)[0] = 0; + } + + skb->ip_summed=CHECKSUM_NONE; +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,21) + skb_set_network_header(skb,skb->head-skb->data); +#endif + ppp_input(&po->chan,skb); + + return NET_RX_SUCCESS; + } +drop: + kfree_skb(skb); + return NET_RX_DROP; +} + +static int pptp_rcv(struct sk_buff *skb) +{ + struct pppox_sock *po; + struct pptp_gre_header *header; + struct iphdr *iph; +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) + int ret; + struct sock *sk; +#endif + + if (skb->pkt_type != PACKET_HOST) + goto drop; + + /*if (!pskb_may_pull(skb, 12)) + goto drop;*/ + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22) + iph = ip_hdr(skb); +#else + iph = skb->nh.iph; +#endif + + header = (struct pptp_gre_header *)skb->data; + + if ( /* version should be 1 */ + ((header->ver & 0x7F) != PPTP_GRE_VER) || + /* PPTP-GRE protocol for PPTP */ + (ntohs(header->protocol) != PPTP_GRE_PROTO)|| + /* flag C should be clear */ + PPTP_GRE_IS_C(header->flags) || + /* flag R should be clear */ + PPTP_GRE_IS_R(header->flags) || + /* flag K should be set */ + (!PPTP_GRE_IS_K(header->flags)) || + /* routing and recursion ctrl = 0 */ + ((header->flags&0xF) != 0)){ + /* if invalid, discard this packet */ + if (log_level>=1) + printk(KERN_INFO"PPTP: Discarding GRE: %X %X %X %X %X %X\n", + header->ver&0x7F, ntohs(header->protocol), + PPTP_GRE_IS_C(header->flags), + PPTP_GRE_IS_R(header->flags), + PPTP_GRE_IS_K(header->flags), + header->flags & 0xF); + goto drop; + } + + + if ((po=lookup_chan(htons(header->call_id),iph->saddr))) { +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31) + skb_dst_drop(skb); +#else + dst_release(skb->dst); + skb->dst = NULL; +#endif + nf_reset(skb); +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) + sk=sk_pppox(po); + bh_lock_sock(sk); + /* Socket state is unknown, must put skb into backlog. */ + if (sk->lock.users != 0) { + sk_add_backlog(sk, skb); + ret = NET_RX_SUCCESS; + } else { + ret = pptp_rcv_core(sk, skb); + } + bh_unlock_sock(sk); + sock_put(sk); + return ret; + +#else /* LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) */ + +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,19) + return sk_receive_skb(sk_pppox(po), skb); +#else + return sk_receive_skb(sk_pppox(po), skb, 0); +#endif + +#endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) */ + }else { +#ifdef DEBUG + if (log_level>=1) + printk(KERN_INFO"PPTP: Discarding packet from unknown call_id %i\n",htons(header->call_id)); +#endif + } + +drop: + kfree_skb(skb); + return NET_RX_DROP; +} + +static int pptp_bind(struct socket *sock,struct sockaddr *uservaddr,int sockaddr_len) +{ + struct sock *sk = sock->sk; + struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr; + struct pppox_sock *po = pppox_sk(sk); + struct pptp_opt *opt=&po->proto.pptp; + int error=0; + +#ifdef DEBUG + if (log_level>=1) + printk(KERN_INFO"PPTP: bind: addr=%X call_id=%i\n",sp->sa_addr.pptp.sin_addr.s_addr, + sp->sa_addr.pptp.call_id); +#endif + lock_sock(sk); + + opt->src_addr=sp->sa_addr.pptp; + if (add_chan(po)) + { + release_sock(sk); + error=-EBUSY; + } +#ifdef DEBUG + if (log_level>=1) + printk(KERN_INFO"PPTP: using call_id %i\n",opt->src_addr.call_id); +#endif + + release_sock(sk); + return error; +} + +static int pptp_connect(struct socket *sock, struct sockaddr *uservaddr, + int sockaddr_len, int flags) +{ + struct sock *sk = sock->sk; + struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr; + struct pppox_sock *po = pppox_sk(sk); + struct pptp_opt *opt = &po->proto.pptp; + struct rtable *rt; /* Route to the other host */ + int error=0; + + if (sp->sa_protocol != PX_PROTO_PPTP) + return -EINVAL; + +#ifdef DEBUG + if (log_level>=1) + printk(KERN_INFO"PPTP[%i]: connect: addr=%X call_id=%i\n",opt->src_addr.call_id, + sp->sa_addr.pptp.sin_addr.s_addr,sp->sa_addr.pptp.call_id); +#endif + + if (lookup_chan_dst(sp->sa_addr.pptp.call_id,sp->sa_addr.pptp.sin_addr.s_addr)) + return -EALREADY; + + lock_sock(sk); + /* Check for already bound sockets */ + if (SK_STATE(sk) & PPPOX_CONNECTED){ + error = -EBUSY; + goto end; + } + + /* Check for already disconnected sockets, on attempts to disconnect */ + if (SK_STATE(sk) & PPPOX_DEAD){ + error = -EALREADY; + goto end; + } + + if (!opt->src_addr.sin_addr.s_addr || !sp->sa_addr.pptp.sin_addr.s_addr){ + error = -EINVAL; + goto end; + } + + po->chan.private=sk; + po->chan.ops=&pptp_chan_ops; + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + { + struct rt_key key = { + .dst=opt->dst_addr.sin_addr.s_addr, + .src=opt->src_addr.sin_addr.s_addr, + .tos=RT_TOS(0), + }; +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) + if (ip_route_output_key(&rt, &key)) { +#else + if (ip_route_output_key(&init_net, &rt, &key)) { +#endif + error = -EHOSTUNREACH; + goto end; + } + } +#else + { + struct flowi fl = { + .nl_u = { .ip4_u = + { .daddr = opt->dst_addr.sin_addr.s_addr, + .saddr = opt->src_addr.sin_addr.s_addr, + .tos = RT_CONN_FLAGS(sk) } }, + .proto = IPPROTO_GRE }; +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18) + security_sk_classify_flow(sk, &fl); +#endif +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) + if (ip_route_output_key(&rt, &fl)){ +#else + if (ip_route_output_key(&init_net, &rt, &fl)){ +#endif + error = -EHOSTUNREACH; + goto end; + } +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36) + sk_setup_caps(sk, &rt->u.dst); +#else + sk_setup_caps(sk, &rt->dst); +#endif + } +#endif +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + po->chan.mtu=PPP_MTU; +#else +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36) + po->chan.mtu=dst_mtu(&rt->u.dst); +#else + po->chan.mtu=dst_mtu(&rt->dst); +#endif + if (!po->chan.mtu) po->chan.mtu=PPP_MTU; +#endif + ip_rt_put(rt); + po->chan.mtu-=PPTP_HEADER_OVERHEAD; + + po->chan.hdrlen=2+sizeof(struct pptp_gre_header); + error = ppp_register_channel(&po->chan); + if (error){ + printk(KERN_ERR "PPTP: failed to register PPP channel (%d)\n",error); + goto end; + } + + opt->dst_addr=sp->sa_addr.pptp; + SK_STATE(sk) = PPPOX_CONNECTED; + + end: + release_sock(sk); + return error; +} + +static int pptp_getname(struct socket *sock, struct sockaddr *uaddr, + int *usockaddr_len, int peer) +{ + int len = sizeof(struct sockaddr_pppox); + struct sockaddr_pppox sp; + + sp.sa_family = AF_PPPOX; + sp.sa_protocol = PX_PROTO_PPTP; + sp.sa_addr.pptp=pppox_sk(sock->sk)->proto.pptp.src_addr; + + memcpy(uaddr, &sp, len); + + *usockaddr_len = len; + + return 0; +} + +static int pptp_release(struct socket *sock) +{ + struct sock *sk = sock->sk; + struct pppox_sock *po; + struct pptp_opt *opt; + int error = 0; + + if (!sk) + return 0; + + lock_sock(sk); + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + if (sk->dead) +#else + if (sock_flag(sk, SOCK_DEAD)) +#endif + { + release_sock(sk); + return -EBADF; + } + + po = pppox_sk(sk); + opt=&po->proto.pptp; + del_chan(po); + + pppox_unbind_sock(sk); + SK_STATE(sk) = PPPOX_DEAD; + +#ifdef DEBUG + if (log_level>=1) + printk(KERN_INFO"PPTP[%i]: release\n",opt->src_addr.call_id); +#endif + + sock_orphan(sk); + sock->sk = NULL; + + release_sock(sk); + sock_put(sk); + + return error; +} + + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) +static struct proto pptp_sk_proto = { + .name = "PPTP", + .owner = THIS_MODULE, + .obj_size = sizeof(struct pppox_sock), +}; +#endif + +static struct proto_ops pptp_ops = { + .family = AF_PPPOX, +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + .owner = THIS_MODULE, +#endif + .release = pptp_release, + .bind = pptp_bind, + .connect = pptp_connect, + .socketpair = sock_no_socketpair, + .accept = sock_no_accept, + .getname = pptp_getname, + .poll = sock_no_poll, + .listen = sock_no_listen, + .shutdown = sock_no_shutdown, + .setsockopt = sock_no_setsockopt, + .getsockopt = sock_no_getsockopt, + .sendmsg = sock_no_sendmsg, + .recvmsg = sock_no_recvmsg, + .mmap = sock_no_mmap, +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) + .ioctl = pppox_ioctl, +#endif +}; + + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) +static void pptp_sock_destruct(struct sock *sk) +{ + skb_queue_purge(&sk->receive_queue); + if (!(SK_STATE(sk) & PPPOX_DEAD)) { + del_chan(pppox_sk(sk)); + pppox_unbind_sock(sk); + } + if (sk->protinfo.destruct_hook) + kfree(sk->protinfo.destruct_hook); + + MOD_DEC_USE_COUNT; +} + +static int pptp_create(struct socket *sock) +{ + int error = -ENOMEM; + struct sock *sk; + struct pppox_sock *po; + struct pptp_opt *opt; + + MOD_INC_USE_COUNT; + + sk = sk_alloc(PF_PPPOX, GFP_KERNEL, 1); + if (!sk) + goto out; + + sock_init_data(sock, sk); + + sock->state = SS_UNCONNECTED; + sock->ops = &pptp_ops; + + //sk->sk_backlog_rcv = pppoe_rcv_core; + sk->state = PPPOX_NONE; + sk->type = SOCK_STREAM; + sk->family = PF_PPPOX; + sk->protocol = PX_PROTO_PPTP; + + sk->protinfo.pppox=kzalloc(sizeof(struct pppox_sock),GFP_KERNEL); + sk->destruct=pptp_sock_destruct; + sk->protinfo.destruct_hook=sk->protinfo.pppox; + + po = pppox_sk(sk); + po->sk=sk; + opt=&po->proto.pptp; + + opt->seq_sent=0; opt->seq_recv=0; + opt->ack_recv=0; opt->ack_sent=0; + + error = 0; +out: + return error; +} +#else +static void pptp_sock_destruct(struct sock *sk) +{ + if (!(SK_STATE(sk) & PPPOX_DEAD)){ + del_chan(pppox_sk(sk)); + pppox_unbind_sock(sk); + } + skb_queue_purge(&sk->sk_receive_queue); +} +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24) +static int pptp_create(struct socket *sock) +#else +static int pptp_create(struct net *net, struct socket *sock) +#endif +{ + int error = -ENOMEM; + struct sock *sk; + struct pppox_sock *po; + struct pptp_opt *opt; + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24) + sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pptp_sk_proto, 1); +#else + sk = sk_alloc(net,PF_PPPOX, GFP_KERNEL, &pptp_sk_proto); +#endif + if (!sk) + goto out; + + sock_init_data(sock, sk); + + sock->state = SS_UNCONNECTED; + sock->ops = &pptp_ops; + + sk->sk_backlog_rcv = pptp_rcv_core; + sk->sk_state = PPPOX_NONE; + sk->sk_type = SOCK_STREAM; + sk->sk_family = PF_PPPOX; + sk->sk_protocol = PX_PROTO_PPTP; + sk->sk_destruct = pptp_sock_destruct; + + po = pppox_sk(sk); +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + po->sk=sk; +#endif + opt=&po->proto.pptp; + + opt->seq_sent=0; opt->seq_recv=0; + opt->ack_recv=0; opt->ack_sent=0; + + error = 0; +out: + return error; +} +#endif + + +static int pptp_ppp_ioctl(struct ppp_channel *chan, unsigned int cmd, + unsigned long arg) +{ + struct sock *sk = (struct sock *) chan->private; + struct pppox_sock *po = pppox_sk(sk); + struct pptp_opt *opt=&po->proto.pptp; + void __user *argp = (void __user *)arg; + int __user *p = argp; + int err, val; + + err = -EFAULT; + switch (cmd) { + case PPPIOCGFLAGS: + val = opt->ppp_flags; + if (put_user(val, p)) + break; + err = 0; + break; + case PPPIOCSFLAGS: + if (get_user(val, p)) + break; + opt->ppp_flags = val & ~SC_RCV_BITS; + err = 0; + break; + default: + err = -ENOTTY; + } + + return err; +} + + +static struct pppox_proto pppox_pptp_proto = { + .create = pptp_create, +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15) + .owner = THIS_MODULE, +#endif +}; + +#if defined(CONFIG_GRE) || defined(CONFIG_GRE_MODULE) +static struct gre_protocol gre_pptp_protocol = { + .handler = pptp_rcv, +}; +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) +static struct inet_protocol net_pptp_protocol = { + .handler = pptp_rcv, + .protocol = IPPROTO_GRE, + .name = "PPTP", +}; +#else +static struct net_protocol net_pptp_protocol = { + .handler = pptp_rcv, +}; +#endif + +static int __init pptp_init_module(void) +{ + int err=0; + printk(KERN_INFO "PPTP driver version " PPTP_DRIVER_VERSION "\n"); + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + callid_sock = __vmalloc((MAX_CALLID + 1) * sizeof(void *), + GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL); +#else + callid_sock = __vmalloc((MAX_CALLID + 1) * sizeof(void *), + GFP_KERNEL, PAGE_KERNEL); + memset(callid_sock, 0, (MAX_CALLID + 1) * sizeof(void *)); +#endif + if (!callid_sock) { + printk(KERN_ERR "PPTP: cann't allocate memory\n"); + return -ENOMEM; + } + +#if defined(CONFIG_GRE) || defined(CONFIG_GRE_MODULE) + if (gre_add_protocol(&gre_pptp_protocol, GREPROTO_PPTP) < 0) { + printk(KERN_INFO "PPTP: can't add protocol\n"); + goto out_free_mem; + } +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + inet_add_protocol(&net_pptp_protocol); +#else + if (inet_add_protocol(&net_pptp_protocol, IPPROTO_GRE) < 0) { + printk(KERN_INFO "PPTP: can't add protocol\n"); + goto out_free_mem; + } +#endif + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + err = proto_register(&pptp_sk_proto, 0); + if (err){ + printk(KERN_INFO "PPTP: can't register sk_proto\n"); + goto out_inet_del_protocol; + } +#endif + + err = register_pppox_proto(PX_PROTO_PPTP, &pppox_pptp_proto); + if (err){ + printk(KERN_INFO "PPTP: can't register pppox_proto\n"); + goto out_unregister_sk_proto; + } + + return 0; +out_unregister_sk_proto: +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + proto_unregister(&pptp_sk_proto); +#endif + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) +out_inet_del_protocol: +#endif + +#if defined(CONFIG_GRE) || defined(CONFIG_GRE_MODULE) + gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP); +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + inet_del_protocol(&net_pptp_protocol); +#else + inet_del_protocol(&net_pptp_protocol, IPPROTO_GRE); +#endif +out_free_mem: + vfree(callid_sock); + + return err; +} + +static void __exit pptp_exit_module(void) +{ + unregister_pppox_proto(PX_PROTO_PPTP); +#if defined(CONFIG_GRE) || defined(CONFIG_GRE_MODULE) +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) + proto_unregister(&pptp_sk_proto); +#endif + gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP); +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + inet_del_protocol(&net_pptp_protocol); +#else + proto_unregister(&pptp_sk_proto); + inet_del_protocol(&net_pptp_protocol, IPPROTO_GRE); +#endif + vfree(callid_sock); +} + +module_init(pptp_init_module); +module_exit(pptp_exit_module); + +MODULE_DESCRIPTION("Point-to-Point Tunneling Protocol for Linux"); +MODULE_AUTHOR("Kozlov D. (xeb@mail.ru)"); +MODULE_LICENSE("GPL"); + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) +MODULE_PARM(log_level,"i"); +MODULE_PARM(log_packets,"i"); +#else +module_param(log_level,int,0); +module_param(log_packets,int,0); +#endif +MODULE_PARM_DESC(log_level,"Logging level (default=0)"); + |