diff options
author | Ralf Baechle <ralf@linux-mips.org> | 1997-01-07 02:33:00 +0000 |
---|---|---|
committer | <ralf@linux-mips.org> | 1997-01-07 02:33:00 +0000 |
commit | beb116954b9b7f3bb56412b2494b562f02b864b1 (patch) | |
tree | 120e997879884e1b9d93b265221b939d2ef1ade1 /net/rose | |
parent | 908d4681a1dc3792ecafbe64265783a86c4cccb6 (diff) |
Import of Linux/MIPS 2.1.14
Diffstat (limited to 'net/rose')
-rw-r--r-- | net/rose/Makefile | 17 | ||||
-rw-r--r-- | net/rose/af_rose.c | 1459 | ||||
-rw-r--r-- | net/rose/rose_dev.c | 298 | ||||
-rw-r--r-- | net/rose/rose_in.c | 333 | ||||
-rw-r--r-- | net/rose/rose_link.c | 301 | ||||
-rw-r--r-- | net/rose/rose_out | 254 | ||||
-rw-r--r-- | net/rose/rose_out.c | 193 | ||||
-rw-r--r-- | net/rose/rose_route.c | 810 | ||||
-rw-r--r-- | net/rose/rose_subr.c | 494 | ||||
-rw-r--r-- | net/rose/rose_timer.c | 153 | ||||
-rw-r--r-- | net/rose/sysctl_net_rose.c | 62 |
11 files changed, 4374 insertions, 0 deletions
diff --git a/net/rose/Makefile b/net/rose/Makefile new file mode 100644 index 000000000..0d71de9cf --- /dev/null +++ b/net/rose/Makefile @@ -0,0 +1,17 @@ +# +# Makefile for the Linux Rose (X.25 PLP) layer. +# +# Note! Dependencies are done automagically by 'make dep', which also +# removes any old dependencies. DON'T put your own dependencies here +# unless it's something special (ie not a .c file). +# +# Note 2! The CFLAGS definition is now in the main makefile... + +O_TARGET := rose.o +O_OBJS := af_rose.o sysctl_net_rose.o rose_dev.o rose_in.o rose_link.o rose_out.o rose_route.o rose_subr.o rose_timer.o +M_OBJS := $(O_TARGET) + +include $(TOPDIR)/Rules.make + +tar: + tar -cvf /dev/f1 . diff --git a/net/rose/af_rose.c b/net/rose/af_rose.c new file mode 100644 index 000000000..4b8acd3f8 --- /dev/null +++ b/net/rose/af_rose.c @@ -0,0 +1,1459 @@ +/* + * Rose release 001 + * + * This is ALPHA test software. This code may break your machine, randomly fail to work with new + * releases, misbehave and/or generally screw up. It might even work. + * + * This code REQUIRES 2.1.0 or higher/ NET3.029 + * + * This module: + * This module 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. + * + * History + * Rose 001 Jonathan(G4KLX) Cloned from af_netrom.c. + */ + +#include <linux/config.h> +#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE) +#include <linux/errno.h> +#include <linux/types.h> +#include <linux/socket.h> +#include <linux/in.h> +#include <linux/kernel.h> +#include <linux/sched.h> +#include <linux/timer.h> +#include <linux/string.h> +#include <linux/sockios.h> +#include <linux/net.h> +#include <linux/stat.h> +#include <net/ax25.h> +#include <linux/inet.h> +#include <linux/netdevice.h> +#include <linux/if_arp.h> +#include <linux/skbuff.h> +#include <net/sock.h> +#include <asm/segment.h> +#include <asm/system.h> +#include <asm/uaccess.h> +#include <linux/fcntl.h> +#include <linux/termios.h> /* For TIOCINQ/OUTQ */ +#include <linux/mm.h> +#include <linux/interrupt.h> +#include <linux/notifier.h> +#include <net/rose.h> +#include <linux/proc_fs.h> +#include <net/ip.h> +#include <net/arp.h> +#include <linux/if_arp.h> + +int sysctl_rose_restart_request_timeout = ROSE_DEFAULT_T0; +int sysctl_rose_call_request_timeout = ROSE_DEFAULT_T1; +int sysctl_rose_reset_request_timeout = ROSE_DEFAULT_T2; +int sysctl_rose_clear_request_timeout = ROSE_DEFAULT_T3; +int sysctl_rose_no_activity_timeout = ROSE_DEFAULT_IDLE; +int sysctl_rose_routing_control = 1; + +static unsigned int lci = 1; + +static struct sock *volatile rose_list = NULL; + +/* + * Convert a Rose address into text. + */ +char *rose2asc(rose_address *addr) +{ + static char buffer[11]; + + if (addr->rose_addr[0] == 0x00 && addr->rose_addr[1] == 0x00 && + addr->rose_addr[2] == 0x00 && addr->rose_addr[3] == 0x00 && + addr->rose_addr[4] == 0x00) { + strcpy(buffer, "*"); + } else { + sprintf(buffer, "%02X%02X%02X%02X%02X", addr->rose_addr[0] & 0xFF, + addr->rose_addr[1] & 0xFF, + addr->rose_addr[2] & 0xFF, + addr->rose_addr[3] & 0xFF, + addr->rose_addr[4] & 0xFF); + } + + return buffer; +} + +/* + * Compare two Rose addresses, 0 == equal. + */ +int rosecmp(rose_address *addr1, rose_address *addr2) +{ + int i; + + for (i = 0; i < 5; i++) + if (addr1->rose_addr[i] != addr2->rose_addr[i]) + return 1; + + return 0; +} + +/* + * Socket removal during an interrupt is now safe. + */ +static void rose_remove_socket(struct sock *sk) +{ + struct sock *s; + unsigned long flags; + + save_flags(flags); + cli(); + + if ((s = rose_list) == sk) { + rose_list = s->next; + restore_flags(flags); + return; + } + + while (s != NULL && s->next != NULL) { + if (s->next == sk) { + s->next = sk->next; + restore_flags(flags); + return; + } + + s = s->next; + } + + restore_flags(flags); +} + +/* + * Kill all bound sockets on a dropped device. + */ +static void rose_kill_by_device(struct device *dev) +{ + struct sock *s; + + for (s = rose_list; s != NULL; s = s->next) { + if (s->protinfo.rose->device == dev) { + s->protinfo.rose->state = ROSE_STATE_0; + s->protinfo.rose->device = NULL; + s->state = TCP_CLOSE; + s->err = ENETUNREACH; + s->shutdown |= SEND_SHUTDOWN; + s->state_change(s); + s->dead = 1; + } + } +} + +/* + * Handle device status changes. + */ +static int rose_device_event(struct notifier_block *this, unsigned long event, void *ptr) +{ + struct device *dev = (struct device *)ptr; + + if (event != NETDEV_DOWN) + return NOTIFY_DONE; + + rose_kill_by_device(dev); + rose_rt_device_down(dev); + rose_link_device_down(dev); + + return NOTIFY_DONE; +} + +/* + * Add a socket to the bound sockets list. + */ +static void rose_insert_socket(struct sock *sk) +{ + unsigned long flags; + + save_flags(flags); + cli(); + + sk->next = rose_list; + rose_list = sk; + + restore_flags(flags); +} + +/* + * Find a socket that wants to accept the Call Request we just + * received. + */ +static struct sock *rose_find_listener(ax25_address *call) +{ + unsigned long flags; + struct sock *s; + + save_flags(flags); + cli(); + + for (s = rose_list; s != NULL; s = s->next) { + if (ax25cmp(&s->protinfo.rose->source_call, call) == 0 && s->protinfo.rose->source_ndigis == 0 && s->state == TCP_LISTEN) { + restore_flags(flags); + return s; + } + } + + for (s = rose_list; s != NULL; s = s->next) { + if (ax25cmp(&s->protinfo.rose->source_call, &null_ax25_address) == 0 && s->state == TCP_LISTEN) { + restore_flags(flags); + return s; + } + } + + restore_flags(flags); + return NULL; +} + +/* + * Find a connected Rose socket given my LCI and device. + */ +struct sock *rose_find_socket(unsigned int lci, struct device *dev) +{ + struct sock *s; + unsigned long flags; + + save_flags(flags); + cli(); + + for (s = rose_list; s != NULL; s = s->next) { + if (s->protinfo.rose->lci == lci && s->protinfo.rose->neighbour->dev == dev) { + restore_flags(flags); + return s; + } + } + + restore_flags(flags); + + return NULL; +} + +/* + * Find a unique LCI for a given device. + */ +unsigned int rose_new_lci(struct device *dev) +{ + lci++; + if (lci > 4095) lci = 1; + + while (rose_find_socket(lci, dev) != NULL) { + lci++; + if (lci > 4095) lci = 1; + } + + return lci; +} + +/* + * Deferred destroy. + */ +void rose_destroy_socket(struct sock *); + +/* + * Handler for deferred kills. + */ +static void rose_destroy_timer(unsigned long data) +{ + rose_destroy_socket((struct sock *)data); +} + +/* + * This is called from user mode and the timers. Thus it protects itself against + * interrupt users but doesn't worry about being called during work. + * Once it is removed from the queue no interrupt or bottom half will + * touch it and we are (fairly 8-) ) safe. + */ +void rose_destroy_socket(struct sock *sk) /* Not static as it's used by the timer */ +{ + struct sk_buff *skb; + unsigned long flags; + + save_flags(flags); + cli(); + + del_timer(&sk->timer); + + rose_remove_socket(sk); + rose_clear_queues(sk); /* Flush the queues */ + + while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) { + if (skb->sk != sk) { /* A pending connection */ + skb->sk->dead = 1; /* Queue the unaccepted socket for death */ + rose_set_timer(skb->sk); + skb->sk->protinfo.rose->state = ROSE_STATE_0; + } + + kfree_skb(skb, FREE_READ); + } + + if (sk->wmem_alloc || sk->rmem_alloc) { /* Defer: outstanding buffers */ + init_timer(&sk->timer); + sk->timer.expires = jiffies + 10 * HZ; + sk->timer.function = rose_destroy_timer; + sk->timer.data = (unsigned long)sk; + add_timer(&sk->timer); + } else { + kfree_s(sk->protinfo.rose, sizeof(*sk->protinfo.rose)); + sk_free(sk); + } + + restore_flags(flags); +} + +/* + * Handling for system calls applied via the various interfaces to a + * Rose socket object. + */ + +static int rose_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg) +{ + return -EINVAL; +} + +/* + * dl1bke 960311: set parameters for existing Rose connections, + * includes a KILL command to abort any connection. + * VERY useful for debugging ;-) + */ +static int rose_ctl_ioctl(const unsigned int cmd, void *arg) +{ + struct rose_ctl_struct rose_ctl; + struct sock *sk; + unsigned long flags; + struct device *dev; + int err; + + if ((err = verify_area(VERIFY_READ, arg, sizeof(rose_ctl))) != 0) + return err; + + copy_from_user(&rose_ctl, arg, sizeof(rose_ctl)); + + if ((dev = rose_ax25_dev_get(rose_ctl.dev)) == NULL) + return -EINVAL; + + if ((sk = rose_find_socket(rose_ctl.lci, dev)) == NULL) + return -ENOTCONN; + + switch (rose_ctl.cmd) { + case ROSE_KILL: + rose_clear_queues(sk); + rose_write_internal(sk, ROSE_CLEAR_REQUEST); + sk->protinfo.rose->state = ROSE_STATE_0; + sk->state = TCP_CLOSE; + sk->err = ENETRESET; + sk->shutdown |= SEND_SHUTDOWN; + if (!sk->dead) + sk->state_change(sk); + sk->dead = 1; + rose_set_timer(sk); + break; + + case ROSE_T0: + if (rose_ctl.arg < 1) + return -EINVAL; + if (sk->protinfo.rose->neighbour != NULL) { + save_flags(flags); cli(); + sk->protinfo.rose->neighbour->t0 = rose_ctl.arg * PR_SLOWHZ; + restore_flags(flags); + } + break; + + case ROSE_T1: + if (rose_ctl.arg < 1) + return -EINVAL; + save_flags(flags); cli(); + sk->protinfo.rose->t1 = rose_ctl.arg * PR_SLOWHZ; + restore_flags(flags); + break; + + case ROSE_T2: + if (rose_ctl.arg < 1) + return -EINVAL; + save_flags(flags); cli(); + sk->protinfo.rose->t2 = rose_ctl.arg * PR_SLOWHZ; + restore_flags(flags); + break; + + case ROSE_T3: + if (rose_ctl.arg < 1) + return -EINVAL; + save_flags(flags); cli(); + sk->protinfo.rose->t3 = rose_ctl.arg * PR_SLOWHZ; + restore_flags(flags); + break; + + case ROSE_IDLE: + if (rose_ctl.arg < 1) + return -EINVAL; + save_flags(flags); cli(); + sk->protinfo.rose->idle = rose_ctl.arg * 60 * PR_SLOWHZ; + restore_flags(flags); + break; + + default: + return -EINVAL; + } + + return 0; +} + +static int rose_setsockopt(struct socket *sock, int level, int optname, + char *optval, int optlen) +{ + struct sock *sk; + int err, opt; + + sk = (struct sock *)sock->data; + + if (level == SOL_SOCKET) + return sock_setsockopt(sk, level, optname, optval, optlen); + + if (level != SOL_ROSE) + return -EOPNOTSUPP; + + if (optval == NULL) + return -EINVAL; + + if ((err = verify_area(VERIFY_READ, optval, sizeof(int))) != 0) + return err; + + get_user(opt, (int *)optval); + + switch (optname) { + case ROSE_T0: + if (opt < 1) + return -EINVAL; + if (sk->protinfo.rose->neighbour != NULL) + sk->protinfo.rose->neighbour->t0 = opt * PR_SLOWHZ; + return 0; + + case ROSE_T1: + if (opt < 1) + return -EINVAL; + sk->protinfo.rose->t1 = opt * PR_SLOWHZ; + return 0; + + case ROSE_T2: + if (opt < 1) + return -EINVAL; + sk->protinfo.rose->t2 = opt * PR_SLOWHZ; + return 0; + + case ROSE_T3: + if (opt < 1) + return -EINVAL; + sk->protinfo.rose->t3 = opt * PR_SLOWHZ; + return 0; + + case ROSE_IDLE: + if (opt < 1) + return -EINVAL; + sk->protinfo.rose->idle = opt * 60 * PR_SLOWHZ; + return 0; + + case ROSE_HDRINCL: + sk->protinfo.rose->hdrincl = opt ? 1 : 0; + return 0; + + default: + return -ENOPROTOOPT; + } +} + +static int rose_getsockopt(struct socket *sock, int level, int optname, + char *optval, int *optlen) +{ + struct sock *sk; + int val = 0; + int err; + + sk = (struct sock *)sock->data; + + if (level == SOL_SOCKET) + return sock_getsockopt(sk, level, optname, optval, optlen); + + if (level != SOL_ROSE) + return -EOPNOTSUPP; + + switch (optname) { + case ROSE_T0: + if (sk->protinfo.rose->neighbour != NULL) + val = sk->protinfo.rose->neighbour->t0 / PR_SLOWHZ; + else + val = sysctl_rose_restart_request_timeout / PR_SLOWHZ; + break; + + case ROSE_T1: + val = sk->protinfo.rose->t1 / PR_SLOWHZ; + break; + + case ROSE_T2: + val = sk->protinfo.rose->t2 / PR_SLOWHZ; + break; + + case ROSE_T3: + val = sk->protinfo.rose->t3 / PR_SLOWHZ; + break; + + case ROSE_IDLE: + val = sk->protinfo.rose->idle / (PR_SLOWHZ * 60); + break; + + case ROSE_HDRINCL: + val = sk->protinfo.rose->hdrincl; + break; + + default: + return -ENOPROTOOPT; + } + + if ((err = verify_area(VERIFY_WRITE, optlen, sizeof(int))) != 0) + return err; + + put_user(sizeof(int), (unsigned long *)optlen); + + if ((err = verify_area(VERIFY_WRITE, optval, sizeof(int))) != 0) + return err; + + put_user(val, (unsigned long *)optval); + + return 0; +} + +static int rose_listen(struct socket *sock, int backlog) +{ + struct sock *sk = (struct sock *)sock->data; + + if (sk->state != TCP_LISTEN) { + sk->protinfo.rose->dest_ndigis = 0; + memset(&sk->protinfo.rose->dest_addr, '\0', ROSE_ADDR_LEN); + memset(&sk->protinfo.rose->dest_call, '\0', AX25_ADDR_LEN); + memset(&sk->protinfo.rose->dest_digi, '\0', AX25_ADDR_LEN); + sk->max_ack_backlog = backlog; + sk->state = TCP_LISTEN; + return 0; + } + + return -EOPNOTSUPP; +} + +static void def_callback1(struct sock *sk) +{ + if (!sk->dead) + wake_up_interruptible(sk->sleep); +} + +static void def_callback2(struct sock *sk, int len) +{ + if (!sk->dead) + wake_up_interruptible(sk->sleep); +} + +static int rose_create(struct socket *sock, int protocol) +{ + struct sock *sk; + rose_cb *rose; + + if (sock->type != SOCK_SEQPACKET || protocol != 0) + return -ESOCKTNOSUPPORT; + + if ((sk = sk_alloc(GFP_ATOMIC)) == NULL) + return -ENOMEM; + + if ((rose = (rose_cb *)kmalloc(sizeof(*rose), GFP_ATOMIC)) == NULL) { + sk_free(sk); + return -ENOMEM; + } + + skb_queue_head_init(&sk->receive_queue); + skb_queue_head_init(&sk->write_queue); + skb_queue_head_init(&sk->back_log); + + init_timer(&sk->timer); + + sk->socket = sock; + sk->type = sock->type; + sk->protocol = protocol; + sk->allocation = GFP_KERNEL; + sk->rcvbuf = SK_RMEM_MAX; + sk->sndbuf = SK_WMEM_MAX; + sk->state = TCP_CLOSE; + sk->priority = SOPRI_NORMAL; + sk->mtu = ROSE_MTU; /* 128 */ + sk->zapped = 1; + sk->window = ROSE_DEFAULT_WINDOW; + + sk->state_change = def_callback1; + sk->data_ready = def_callback2; + sk->write_space = def_callback1; + sk->error_report = def_callback1; + + if (sock != NULL) { + sock->data = (void *)sk; + sk->sleep = sock->wait; + } + + skb_queue_head_init(&rose->ack_queue); + skb_queue_head_init(&rose->frag_queue); + + rose->lci = 0; + + rose->t1 = sysctl_rose_call_request_timeout; + rose->t2 = sysctl_rose_reset_request_timeout; + rose->t3 = sysctl_rose_clear_request_timeout; + rose->idle = sysctl_rose_no_activity_timeout; + + rose->timer = 0; + + rose->va = 0; + rose->vr = 0; + rose->vs = 0; + rose->vl = 0; + + rose->fraglen = 0; + rose->hdrincl = 0; + rose->state = ROSE_STATE_0; + rose->neighbour = NULL; + rose->device = NULL; + + rose->source_ndigis = 0; + rose->dest_ndigis = 0; + + memset(&rose->source_addr, '\0', ROSE_ADDR_LEN); + memset(&rose->dest_addr, '\0', ROSE_ADDR_LEN); + memset(&rose->source_call, '\0', AX25_ADDR_LEN); + memset(&rose->dest_call, '\0', AX25_ADDR_LEN); + memset(&rose->source_digi, '\0', AX25_ADDR_LEN); + memset(&rose->dest_digi, '\0', AX25_ADDR_LEN); + + rose->sk = sk; + sk->protinfo.rose = rose; + + return 0; +} + +static struct sock *rose_make_new(struct sock *osk) +{ + struct sock *sk; + rose_cb *rose; + + if (osk->type != SOCK_SEQPACKET) + return NULL; + + if ((sk = (struct sock *)sk_alloc(GFP_ATOMIC)) == NULL) + return NULL; + + if ((rose = (rose_cb *)kmalloc(sizeof(*rose), GFP_ATOMIC)) == NULL) { + sk_free(sk); + return NULL; + } + + skb_queue_head_init(&sk->receive_queue); + skb_queue_head_init(&sk->write_queue); + skb_queue_head_init(&sk->back_log); + + init_timer(&sk->timer); + + sk->type = osk->type; + sk->socket = osk->socket; + sk->priority = osk->priority; + sk->protocol = osk->protocol; + sk->rcvbuf = osk->rcvbuf; + sk->sndbuf = osk->sndbuf; + sk->debug = osk->debug; + sk->state = TCP_ESTABLISHED; + sk->window = osk->window; + sk->mtu = osk->mtu; + sk->sleep = osk->sleep; + sk->zapped = osk->zapped; + + sk->state_change = def_callback1; + sk->data_ready = def_callback2; + sk->write_space = def_callback1; + sk->error_report = def_callback1; + + skb_queue_head_init(&rose->ack_queue); + skb_queue_head_init(&rose->frag_queue); + + rose->t1 = osk->protinfo.rose->t1; + rose->t2 = osk->protinfo.rose->t2; + rose->t3 = osk->protinfo.rose->t3; + rose->idle = osk->protinfo.rose->idle; + + rose->device = osk->protinfo.rose->device; + rose->hdrincl = osk->protinfo.rose->hdrincl; + rose->fraglen = 0; + + rose->timer = 0; + + rose->va = 0; + rose->vr = 0; + rose->vs = 0; + rose->vl = 0; + + sk->protinfo.rose = rose; + rose->sk = sk; + + return sk; +} + +static int rose_dup(struct socket *newsock, struct socket *oldsock) +{ + struct sock *sk = (struct sock *)oldsock->data; + + return rose_create(newsock, sk->protocol); +} + +static int rose_release(struct socket *sock, struct socket *peer) +{ + struct sock *sk = (struct sock *)sock->data; + + if (sk == NULL) return 0; + + switch (sk->protinfo.rose->state) { + + case ROSE_STATE_0: + sk->state = TCP_CLOSE; + sk->shutdown |= SEND_SHUTDOWN; + sk->state_change(sk); + sk->dead = 1; + rose_destroy_socket(sk); + break; + + case ROSE_STATE_1: + sk->protinfo.rose->state = ROSE_STATE_0; + sk->state = TCP_CLOSE; + sk->shutdown |= SEND_SHUTDOWN; + sk->state_change(sk); + sk->dead = 1; + rose_destroy_socket(sk); + break; + + case ROSE_STATE_2: + sk->protinfo.rose->state = ROSE_STATE_0; + sk->state = TCP_CLOSE; + sk->shutdown |= SEND_SHUTDOWN; + sk->state_change(sk); + sk->dead = 1; + rose_destroy_socket(sk); + break; + + case ROSE_STATE_3: + case ROSE_STATE_4: + rose_clear_queues(sk); + rose_write_internal(sk, ROSE_CLEAR_REQUEST); + sk->protinfo.rose->timer = sk->protinfo.rose->t3; + sk->protinfo.rose->state = ROSE_STATE_2; + sk->state = TCP_CLOSE; + sk->shutdown |= SEND_SHUTDOWN; + sk->state_change(sk); + sk->dead = 1; + sk->destroy = 1; + break; + + default: + break; + } + + sock->data = NULL; + sk->socket = NULL; /* Not used, but we should do this. **/ + + return 0; +} + +static int rose_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) +{ + struct sock *sk; + struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr; + struct device *dev; + ax25_address *user, *source; + + sk = (struct sock *)sock->data; + + if (sk->zapped == 0) + return -EINVAL; + + if (addr_len != sizeof(struct sockaddr_rose)) + return -EINVAL; + + if ((dev = rose_dev_get(&addr->srose_addr)) == NULL) { + if (sk->debug) + printk("Rose: bind failed: invalid address\n"); + return -EADDRNOTAVAIL; + } + + source = &addr->srose_call; + + if ((user = ax25_findbyuid(current->euid)) == NULL) { + if (ax25_uid_policy && !suser()) + return -EACCES; + user = source; + } + + sk->protinfo.rose->source_addr = addr->srose_addr; + sk->protinfo.rose->source_call = *user; + sk->protinfo.rose->device = dev; + + if (addr->srose_ndigis == 1) { + sk->protinfo.rose->source_ndigis = 1; + sk->protinfo.rose->source_digi = addr->srose_digi; + } + + rose_insert_socket(sk); + + sk->zapped = 0; + + if (sk->debug) + printk("Rose: socket is bound\n"); + + return 0; +} + +static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags) +{ + struct sock *sk = (struct sock *)sock->data; + struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr; + ax25_address *user; + struct device *dev; + + if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) { + sock->state = SS_CONNECTED; + return 0; /* Connect completed during a ERESTARTSYS event */ + } + + if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) { + sock->state = SS_UNCONNECTED; + return -ECONNREFUSED; + } + + if (sk->state == TCP_ESTABLISHED) + return -EISCONN; /* No reconnect on a seqpacket socket */ + + sk->state = TCP_CLOSE; + sock->state = SS_UNCONNECTED; + + if (addr_len != sizeof(struct sockaddr_rose)) + return -EINVAL; + + if ((sk->protinfo.rose->neighbour = rose_get_neigh(&addr->srose_addr)) == NULL) + return -ENETUNREACH; + + if (sk->zapped) { /* Must bind first - autobinding in this may or may not work */ + sk->zapped = 0; + + if ((dev = rose_dev_first()) == NULL) + return -ENETUNREACH; + + if ((user = ax25_findbyuid(current->euid)) == NULL) + return -EINVAL; + + memcpy(&sk->protinfo.rose->source_addr, dev->dev_addr, ROSE_ADDR_LEN); + sk->protinfo.rose->source_call = *user; + sk->protinfo.rose->device = dev; + + rose_insert_socket(sk); /* Finish the bind */ + } + + sk->protinfo.rose->dest_addr = addr->srose_addr; + sk->protinfo.rose->dest_call = addr->srose_call; + if (addr->srose_ndigis == 1) { + sk->protinfo.rose->dest_ndigis = 1; + sk->protinfo.rose->dest_digi = addr->srose_digi; + } + sk->protinfo.rose->lci = rose_new_lci(sk->protinfo.rose->neighbour->dev); + + /* Move to connecting socket, start sending Connect Requests */ + sock->state = SS_CONNECTING; + sk->state = TCP_SYN_SENT; + + sk->protinfo.rose->state = ROSE_STATE_1; + sk->protinfo.rose->timer = sk->protinfo.rose->t1; + rose_write_internal(sk, ROSE_CALL_REQUEST); + + rose_set_timer(sk); + + /* Now the loop */ + if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) + return -EINPROGRESS; + + cli(); /* To avoid races on the sleep */ + + /* + * A Connect Ack with Choke or timeout or failed routing will go to closed. + */ + while (sk->state == TCP_SYN_SENT) { + interruptible_sleep_on(sk->sleep); + if (current->signal & ~current->blocked) { + sti(); + return -ERESTARTSYS; + } + } + + if (sk->state != TCP_ESTABLISHED) { + sti(); + sock->state = SS_UNCONNECTED; + return sock_error(sk); /* Always set at this point */ + } + + sock->state = SS_CONNECTED; + + sti(); + + return 0; +} + +static int rose_socketpair(struct socket *sock1, struct socket *sock2) +{ + return -EOPNOTSUPP; +} + +static int rose_accept(struct socket *sock, struct socket *newsock, int flags) +{ + struct sock *sk; + struct sock *newsk; + struct sk_buff *skb; + + if (newsock->data) + sk_free(newsock->data); + + newsock->data = NULL; + + sk = (struct sock *)sock->data; + + if (sk->type != SOCK_SEQPACKET) + return -EOPNOTSUPP; + + if (sk->state != TCP_LISTEN) + return -EINVAL; + + /* + * The write queue this time is holding sockets ready to use + * hooked into the SABM we saved + */ + do { + cli(); + if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) { + if (flags & O_NONBLOCK) { + sti(); + return 0; + } + interruptible_sleep_on(sk->sleep); + if (current->signal & ~current->blocked) { + sti(); + return -ERESTARTSYS; + } + } + } while (skb == NULL); + + newsk = skb->sk; + newsk->pair = NULL; + sti(); + + /* Now attach up the new socket */ + skb->sk = NULL; + kfree_skb(skb, FREE_READ); + sk->ack_backlog--; + newsock->data = newsk; + + return 0; +} + +static int rose_getname(struct socket *sock, struct sockaddr *uaddr, + int *uaddr_len, int peer) +{ + struct sockaddr_rose *srose = (struct sockaddr_rose *)uaddr; + struct sock *sk; + + sk = (struct sock *)sock->data; + + if (peer != 0) { + if (sk->state != TCP_ESTABLISHED) + return -ENOTCONN; + srose->srose_family = AF_ROSE; + srose->srose_ndigis = 0; + srose->srose_addr = sk->protinfo.rose->dest_addr; + srose->srose_call = sk->protinfo.rose->dest_call; + if (sk->protinfo.rose->dest_ndigis == 1) { + srose->srose_ndigis = 1; + srose->srose_digi = sk->protinfo.rose->dest_digi; + } + *uaddr_len = sizeof(struct sockaddr_rose); + } else { + srose->srose_family = AF_ROSE; + srose->srose_ndigis = 0; + srose->srose_addr = sk->protinfo.rose->source_addr; + srose->srose_call = sk->protinfo.rose->source_call; + if (sk->protinfo.rose->source_ndigis == 1) { + srose->srose_ndigis = 1; + srose->srose_digi = sk->protinfo.rose->source_digi; + } + *uaddr_len = sizeof(struct sockaddr_rose); + } + + return 0; +} + +int rose_rx_call_request(struct sk_buff *skb, struct device *dev, struct rose_neigh *neigh, unsigned int lci) +{ + struct sock *sk; + struct sock *make; + rose_cb rose; + + skb->sk = NULL; /* Initially we don't know who it's for */ + + /* + * skb->data points to the rose frame start + */ + + /* + * XXX This is an error. + */ + if (!rose_parse_facilities(skb, &rose)) { + return 0; + } + + sk = rose_find_listener(&rose.source_call); + + /* + * We can't accept the Call Request. + */ + if (sk == NULL || sk->ack_backlog == sk->max_ack_backlog || (make = rose_make_new(sk)) == NULL) { + rose_transmit_clear_request(neigh, lci, 0x01); + return 0; + } + + skb->sk = make; + make->state = TCP_ESTABLISHED; + + make->protinfo.rose->lci = lci; + make->protinfo.rose->dest_addr = rose.dest_addr; + make->protinfo.rose->dest_call = rose.dest_call; + make->protinfo.rose->dest_ndigis = rose.dest_ndigis; + make->protinfo.rose->dest_digi = rose.dest_digi; + make->protinfo.rose->source_addr = rose.source_addr; + make->protinfo.rose->source_call = rose.source_call; + make->protinfo.rose->source_ndigis = rose.source_ndigis; + make->protinfo.rose->source_digi = rose.source_digi; + make->protinfo.rose->neighbour = neigh; + make->protinfo.rose->device = dev; + + rose_write_internal(make, ROSE_CALL_ACCEPTED); + + make->protinfo.rose->condition = 0x00; + make->protinfo.rose->vs = 0; + make->protinfo.rose->va = 0; + make->protinfo.rose->vr = 0; + make->protinfo.rose->vl = 0; + make->protinfo.rose->state = ROSE_STATE_3; + sk->ack_backlog++; + make->pair = sk; + + rose_insert_socket(make); + + skb_queue_head(&sk->receive_queue, skb); + + rose_set_timer(make); + + if (!sk->dead) + sk->data_ready(sk, skb->len); + + return 1; +} + +static int rose_sendmsg(struct socket *sock, struct msghdr *msg, int len, int noblock, int flags) +{ + struct sock *sk = (struct sock *)sock->data; + struct sockaddr_rose *usrose = (struct sockaddr_rose *)msg->msg_name; + int err; + struct sockaddr_rose srose; + struct sk_buff *skb; + unsigned char *asmptr; + int size; + + if (sk->err) + return sock_error(sk); + + if (flags) + return -EINVAL; + + if (sk->zapped) + return -EADDRNOTAVAIL; + + if (sk->shutdown & SEND_SHUTDOWN) { + send_sig(SIGPIPE, current, 0); + return -EPIPE; + } + + if (sk->protinfo.rose->device == NULL) + return -ENETUNREACH; + + if (usrose) { + if (msg->msg_namelen < sizeof(srose)) + return -EINVAL; + srose = *usrose; + if (rosecmp(&sk->protinfo.rose->dest_addr, &srose.srose_addr) != 0 || + ax25cmp(&sk->protinfo.rose->dest_call, &srose.srose_call) != 0) + return -EISCONN; + if (srose.srose_ndigis == 1 && sk->protinfo.rose->dest_ndigis == 1) { + if (ax25cmp(&sk->protinfo.rose->dest_digi, &srose.srose_digi) != 0) + return -EISCONN; + } + if (srose.srose_family != AF_ROSE) + return -EINVAL; + } else { + if (sk->state != TCP_ESTABLISHED) + return -ENOTCONN; + + srose.srose_family = AF_ROSE; + srose.srose_addr = sk->protinfo.rose->dest_addr; + srose.srose_call = sk->protinfo.rose->dest_call; + srose.srose_ndigis = 0; + + if (sk->protinfo.rose->dest_ndigis == 1) { + srose.srose_ndigis = 1; + srose.srose_digi = sk->protinfo.rose->dest_digi; + } + } + + if (sk->debug) + printk("Rose: sendto: Addresses built.\n"); + + /* Build a packet */ + if (sk->debug) + printk("Rose: sendto: building packet.\n"); + + size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN; + + if ((skb = sock_alloc_send_skb(sk, size, 0, 0, &err)) == NULL) + return err; + + skb->sk = sk; + skb->free = 1; + skb->arp = 1; + + skb_reserve(skb, size - len); + + /* + * Push down the Rose header + */ + + asmptr = skb_push(skb, ROSE_MIN_LEN); + + if (sk->debug) + printk("Building Rose Header.\n"); + + /* Build a Rose Transport header */ + + *asmptr++ = ((sk->protinfo.rose->lci >> 8) & 0x0F) | GFI; + *asmptr++ = (sk->protinfo.rose->lci >> 0) & 0xFF; + *asmptr++ = ROSE_DATA; + + if (sk->debug) + printk("Built header.\n"); + + /* + * Put the data on the end + */ + + skb->h.raw = skb_put(skb, len); + + asmptr = skb->h.raw; + + if (sk->debug) + printk("Rose: Appending user data\n"); + + /* User data follows immediately after the Rose transport header */ + memcpy_fromiovec(asmptr, msg->msg_iov, len); + + if (sk->debug) + printk("Rose: Transmitting buffer\n"); + + if (sk->state != TCP_ESTABLISHED) { + kfree_skb(skb, FREE_WRITE); + return -ENOTCONN; + } + + rose_output(sk, skb); /* Shove it onto the queue */ + + return len; +} + + +static int rose_recvmsg(struct socket *sock, struct msghdr *msg, int size, int noblock, + int flags, int *addr_len) +{ + struct sock *sk = (struct sock *)sock->data; + struct sockaddr_rose *srose = (struct sockaddr_rose *)msg->msg_name; + int copied; + struct sk_buff *skb; + int er; + + if (addr_len != NULL) + *addr_len = sizeof(*srose); + + /* + * This works for seqpacket too. The receiver has ordered the queue for + * us! We do one quick check first though + */ + if (sk->state != TCP_ESTABLISHED) + return -ENOTCONN; + + /* Now we can treat all alike */ + if ((skb = skb_recv_datagram(sk, flags, noblock, &er)) == NULL) + return er; + + if (!sk->protinfo.rose->hdrincl) { + skb_pull(skb, ROSE_MIN_LEN); + skb->h.raw = skb->data; + } + + copied = skb->len; + + if (copied > size) { + copied = size; + msg->msg_flags |= MSG_TRUNC; + } + + skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); + + if (srose != NULL) { + struct sockaddr_rose addr; + + addr.srose_family = AF_ROSE; + addr.srose_addr = sk->protinfo.rose->dest_addr; + addr.srose_call = sk->protinfo.rose->dest_call; + addr.srose_ndigis = 0; + + if (sk->protinfo.rose->dest_ndigis == 1) { + addr.srose_ndigis = 1; + addr.srose_digi = sk->protinfo.rose->dest_digi; + } + + *srose = addr; + + *addr_len = sizeof(*srose); + } + + skb_free_datagram(sk, skb); + + return copied; +} + +static int rose_shutdown(struct socket *sk, int how) +{ + return -EOPNOTSUPP; +} + +static int rose_select(struct socket *sock , int sel_type, select_table *wait) +{ + struct sock *sk = (struct sock *)sock->data; + + return datagram_select(sk, sel_type, wait); +} + +static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) +{ + struct sock *sk = (struct sock *)sock->data; + int err; + long amount = 0; + + switch (cmd) { + case TIOCOUTQ: + if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(unsigned int))) != 0) + return err; + amount = sk->sndbuf - sk->wmem_alloc; + if (amount < 0) + amount = 0; + put_user(amount, (unsigned int *)arg); + return 0; + + case TIOCINQ: { + struct sk_buff *skb; + /* These two are safe on a single CPU system as only user tasks fiddle here */ + if ((skb = skb_peek(&sk->receive_queue)) != NULL) + amount = skb->len - 20; + if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(unsigned int))) != 0) + return err; + put_user(amount, (unsigned int *)arg); + return 0; + } + + case SIOCGSTAMP: + if (sk != NULL) { + if (sk->stamp.tv_sec==0) + return -ENOENT; + if ((err = verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval))) != 0) + return err; + copy_to_user((void *)arg, &sk->stamp, sizeof(struct timeval)); + return 0; + } + return -EINVAL; + + case SIOCGIFADDR: + case SIOCSIFADDR: + case SIOCGIFDSTADDR: + case SIOCSIFDSTADDR: + case SIOCGIFBRDADDR: + case SIOCSIFBRDADDR: + case SIOCGIFNETMASK: + case SIOCSIFNETMASK: + case SIOCGIFMETRIC: + case SIOCSIFMETRIC: + return -EINVAL; + + case SIOCADDRT: + case SIOCDELRT: + if (!suser()) return -EPERM; + return rose_rt_ioctl(cmd, (void *)arg); + + case SIOCRSCTLCON: + if (!suser()) return -EPERM; + return rose_ctl_ioctl(cmd, (void *)arg); + + default: + return dev_ioctl(cmd, (void *)arg); + } + + /*NOTREACHED*/ + return 0; +} + +static int rose_get_info(char *buffer, char **start, off_t offset, int length, int dummy) +{ + struct sock *s; + struct device *dev; + const char *devname, *callsign; + int len = 0; + off_t pos = 0; + off_t begin = 0; + + cli(); + + len += sprintf(buffer, "dest_addr dest_call dest_digi src_addr src_call src_digi dev lci st vs vr va t t1 t2 t3 Snd-Q Rcv-Q\n"); + + for (s = rose_list; s != NULL; s = s->next) { + if ((dev = s->protinfo.rose->device) == NULL) + devname = "???"; + else + devname = dev->name; + + len += sprintf(buffer + len, "%-10s %-9s ", + rose2asc(&s->protinfo.rose->dest_addr), + ax2asc(&s->protinfo.rose->dest_call)); + len += sprintf(buffer + len, "%-9s ", + ax2asc(&s->protinfo.rose->dest_digi)); + + if (ax25cmp(&s->protinfo.rose->source_call, &null_ax25_address) == 0) + callsign = "??????-?"; + else + callsign = ax2asc(&s->protinfo.rose->source_call); + + len += sprintf(buffer + len, "%-10s %-9s ", + rose2asc(&s->protinfo.rose->source_addr), + callsign); + len += sprintf(buffer + len, "%-9s %-5s %3.3X %d %d %d %d %3d %3d %3d %3d %5d %5d\n", + ax2asc(&s->protinfo.rose->source_digi), + devname, s->protinfo.rose->lci & 0x0FFF, + s->protinfo.rose->state, + s->protinfo.rose->vs, s->protinfo.rose->vr, s->protinfo.rose->va, + s->protinfo.rose->timer / PR_SLOWHZ, + s->protinfo.rose->t1 / PR_SLOWHZ, + s->protinfo.rose->t2 / PR_SLOWHZ, + s->protinfo.rose->t3 / PR_SLOWHZ, + s->wmem_alloc, s->rmem_alloc); + + pos = begin + len; + + if (pos < offset) { + len = 0; + begin = pos; + } + + if (pos > offset + length) + break; + } + + sti(); + + *start = buffer + (offset - begin); + len -= (offset - begin); + + if (len > length) len = length; + + return(len); +} + +struct proto_ops rose_proto_ops = { + AF_ROSE, + + rose_create, + rose_dup, + rose_release, + rose_bind, + rose_connect, + rose_socketpair, + rose_accept, + rose_getname, + rose_select, + rose_ioctl, + rose_listen, + rose_shutdown, + rose_setsockopt, + rose_getsockopt, + rose_fcntl, + rose_sendmsg, + rose_recvmsg +}; + +struct notifier_block rose_dev_notifier = { + rose_device_event, + 0 +}; + +void rose_proto_init(struct net_proto *pro) +{ + sock_register(rose_proto_ops.family, &rose_proto_ops); + register_netdevice_notifier(&rose_dev_notifier); + printk(KERN_INFO "G4KLX Rose for Linux. Version 0.1 for AX25.034 Linux 2.1\n"); + + if (!ax25_protocol_register(AX25_P_ROSE, rose_route_frame)) + printk(KERN_ERR "Rose unable to register protocol with AX.25\n"); + if (!ax25_linkfail_register(rose_link_failed)) + printk(KERN_ERR "Rose unable to register linkfail handler with AX.25\n"); + + rose_register_sysctl(); + +#ifdef CONFIG_PROC_FS + proc_net_register(&(struct proc_dir_entry) { + PROC_NET_RS, 4, "rose", + S_IFREG | S_IRUGO, 1, 0, 0, + 0, &proc_net_inode_operations, + rose_get_info + }); + proc_net_register(&(struct proc_dir_entry) { + PROC_NET_RS_NEIGH, 10, "rose_neigh", + S_IFREG | S_IRUGO, 1, 0, 0, + 0, &proc_net_inode_operations, + rose_neigh_get_info + }); + proc_net_register(&(struct proc_dir_entry) { + PROC_NET_RS_NODES, 10, "rose_nodes", + S_IFREG | S_IRUGO, 1, 0, 0, + 0, &proc_net_inode_operations, + rose_nodes_get_info + }); + + proc_net_register(&(struct proc_dir_entry) { + PROC_NET_RS_ROUTES, 11, "rose_routes", + S_IFREG | S_IRUGO, 1, 0, 0, + 0, &proc_net_inode_operations, + rose_routes_get_info + }); +#endif +} + +#endif diff --git a/net/rose/rose_dev.c b/net/rose/rose_dev.c new file mode 100644 index 000000000..7fe6a00e6 --- /dev/null +++ b/net/rose/rose_dev.c @@ -0,0 +1,298 @@ +/* + * Rose release 001 + * + * This is ALPHA test software. This code may break your machine, randomly fail to work with new + * releases, misbehave and/or generally screw up. It might even work. + * + * This code REQUIRES 2.1.0 or higher/ NET3.029 + * + * This module: + * This module 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. + * + * History + * Rose 001 Jonathan(G4KLX) Cloned from nr_dev.c. + */ + +#include <linux/config.h> +#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE) +#include <linux/module.h> +#include <linux/proc_fs.h> +#include <linux/kernel.h> +#include <linux/sched.h> +#include <linux/interrupt.h> +#include <linux/fs.h> +#include <linux/types.h> +#include <linux/sysctl.h> +#include <linux/string.h> +#include <linux/socket.h> +#include <linux/errno.h> +#include <linux/fcntl.h> +#include <linux/in.h> +#include <linux/if_ether.h> /* For the statistics structure. */ + +#include <asm/system.h> +#include <asm/segment.h> +#include <asm/io.h> + +#include <linux/inet.h> +#include <linux/netdevice.h> +#include <linux/etherdevice.h> +#include <linux/if_arp.h> +#include <linux/skbuff.h> + +#include <net/ip.h> +#include <net/arp.h> + +#include <net/ax25.h> +#include <net/rose.h> + +/* + * Only allow IP over Rose frames through if the netrom device is up. + */ + +int rose_rx_ip(struct sk_buff *skb, struct device *dev) +{ + struct enet_statistics *stats = (struct enet_statistics *)dev->priv; + + if (!dev->start) { + stats->rx_errors++; + return 0; + } + + stats->rx_packets++; + skb->protocol = htons(ETH_P_IP); + + /* Spoof incoming device */ + skb->dev = dev; + + skb->h.raw = skb->data; + ip_rcv(skb, skb->dev, NULL); + + return 1; +} + +static int rose_header(struct sk_buff *skb, struct device *dev, unsigned short type, + void *daddr, void *saddr, unsigned len) +{ + unsigned char *buff = skb_push(skb, ROSE_MIN_LEN + 2); + + *buff++ = GFI | Q_BIT; + *buff++ = 0x00; + *buff++ = ROSE_DATA; + *buff++ = 0x7F; + *buff++ = AX25_P_IP; + + if (daddr != NULL) + return 37; + + return -37; +} + +static int rose_rebuild_header(void *buff, struct device *dev, + unsigned long raddr, struct sk_buff *skb) +{ + struct enet_statistics *stats = (struct enet_statistics *)dev->priv; + unsigned char *bp = (unsigned char *)buff; + struct sk_buff *skbn; + + if (!arp_query(bp + 7, raddr, dev)) { + dev_kfree_skb(skb, FREE_WRITE); + return 1; + } + + if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) { + dev_kfree_skb(skb, FREE_WRITE); + return 1; + } + + skbn->sk = skb->sk; + + if (skbn->sk != NULL) + atomic_add(skbn->truesize, &skbn->sk->wmem_alloc); + + dev_kfree_skb(skb, FREE_WRITE); + + if (!rose_route_frame(skbn, NULL)) { + dev_kfree_skb(skbn, FREE_WRITE); + stats->tx_errors++; + } + + stats->tx_packets++; + + return 1; +} + +static int rose_set_mac_address(struct device *dev, void *addr) +{ + struct sockaddr *sa = addr; + + ax25_listen_release((ax25_address *)dev->dev_addr, NULL); + + memcpy(dev->dev_addr, sa->sa_data, dev->addr_len); + + ax25_listen_register((ax25_address *)dev->dev_addr, NULL); + + return 0; +} + +static int rose_open(struct device *dev) +{ + dev->tbusy = 0; + dev->start = 1; + + MOD_INC_USE_COUNT; + + ax25_listen_register((ax25_address *)dev->dev_addr, NULL); + + return 0; +} + +static int rose_close(struct device *dev) +{ + dev->tbusy = 1; + dev->start = 0; + + ax25_listen_release((ax25_address *)dev->dev_addr, NULL); + + MOD_DEC_USE_COUNT; + + return 0; +} + +static int rose_xmit(struct sk_buff *skb, struct device *dev) +{ + struct enet_statistics *stats = (struct enet_statistics *)dev->priv; + + if (skb == NULL || dev == NULL) + return 0; + + if (!dev->start) { + printk(KERN_ERR "rose: xmit call when iface is down\n"); + return 1; + } + + cli(); + + if (dev->tbusy != 0) { + sti(); + stats->tx_errors++; + return 1; + } + + dev->tbusy = 1; + + sti(); + + dev_kfree_skb(skb, FREE_WRITE); + + stats->tx_errors++; + + dev->tbusy = 0; + + mark_bh(NET_BH); + + return 0; +} + +static struct enet_statistics *rose_get_stats(struct device *dev) +{ + return (struct enet_statistics *)dev->priv; +} + +int rose_init(struct device *dev) +{ + int i; + + dev->mtu = ROSE_PACLEN - 2; + dev->tbusy = 0; + dev->hard_start_xmit = rose_xmit; + dev->open = rose_open; + dev->stop = rose_close; + + dev->hard_header = rose_header; + dev->hard_header_len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN; + dev->addr_len = ROSE_ADDR_LEN; + dev->type = ARPHRD_ROSE; + dev->rebuild_header = rose_rebuild_header; + dev->set_mac_address = rose_set_mac_address; + + /* New-style flags. */ + dev->flags = 0; + dev->family = AF_INET; + + dev->pa_addr = 0; + dev->pa_brdaddr = 0; + dev->pa_mask = 0; + dev->pa_alen = sizeof(unsigned long); + + if ((dev->priv = kmalloc(sizeof(struct enet_statistics), GFP_KERNEL)) == NULL) + return -ENOMEM; + + memset(dev->priv, 0, sizeof(struct enet_statistics)); + + dev->get_stats = rose_get_stats; + + /* Fill in the generic fields of the device structure. */ + for (i = 0; i < DEV_NUMBUFFS; i++) + skb_queue_head_init(&dev->buffs[i]); + + return 0; +}; + +#ifdef MODULE +extern struct proto_ops rose_proto_ops; +extern struct notifier_block rose_dev_notifier; + +static struct device dev_rose[] = { + {"rose0", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, rose_init}, + {"rose1", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, rose_init} +}; + +int init_module(void) +{ + register_netdev(&dev_rose[0]); + register_netdev(&dev_rose[1]); + + register_symtab(NULL); + + rose_proto_init(NULL); + + return 0; +} + +void cleanup_module(void) +{ + int i; + +#ifdef CONFIG_PROC_FS + proc_net_unregister(PROC_NET_RS); + proc_net_unregister(PROC_NET_RS_NEIGH); + proc_net_unregister(PROC_NET_RS_NODES); + proc_net_unregister(PROC_NET_RS_ROUTES); +#endif + rose_rt_free(); + + ax25_protocol_release(AX25_P_ROSE); + ax25_linkfail_release(rose_link_failed); + + rose_unregister_sysctl(); + + unregister_netdevice_notifier(&rose_dev_notifier); + + sock_unregister(rose_proto_ops.family); + + for (i = 0; i < 2; i++) { + if (dev_rose[i].priv != NULL) { + kfree(dev_rose[i].priv); + dev_rose[i].priv = NULL; + unregister_netdev(&dev_rose[i]); + } + } +} + +#endif + +#endif diff --git a/net/rose/rose_in.c b/net/rose/rose_in.c new file mode 100644 index 000000000..20374dbb1 --- /dev/null +++ b/net/rose/rose_in.c @@ -0,0 +1,333 @@ +/* + * Rose release 001 + * + * This is ALPHA test software. This code may break your machine, randomly fail to work with new + * releases, misbehave and/or generally screw up. It might even work. + * + * This code REQUIRES 2.1.0 or higher/ NET3.029 + * + * This module: + * This module 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. + * + * Most of this code is based on the SDL diagrams published in the 7th + * ARRL Computer Networking Conference papers. The diagrams have mistakes + * in them, but are mostly correct. Before you modify the code could you + * read the SDL diagrams as the code is not obvious and probably very + * easy to break; + * + * History + * Rose 001 Jonathan(G4KLX) Cloned from nr_in.c + */ + +#include <linux/config.h> +#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE) +#include <linux/errno.h> +#include <linux/types.h> +#include <linux/socket.h> +#include <linux/in.h> +#include <linux/kernel.h> +#include <linux/sched.h> +#include <linux/timer.h> +#include <linux/string.h> +#include <linux/sockios.h> +#include <linux/net.h> +#include <net/ax25.h> +#include <linux/inet.h> +#include <linux/netdevice.h> +#include <linux/skbuff.h> +#include <net/sock.h> +#include <net/ip.h> /* For ip_rcv */ +#include <asm/segment.h> +#include <asm/system.h> +#include <linux/fcntl.h> +#include <linux/mm.h> +#include <linux/interrupt.h> +#include <net/rose.h> + +static int rose_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more) +{ + struct sk_buff *skbo, *skbn = skb; + + if (more) { + sk->protinfo.rose->fraglen += skb->len; + skb_queue_tail(&sk->protinfo.rose->frag_queue, skb); + return 0; + } + + if (!more && sk->protinfo.rose->fraglen > 0) { /* End of fragment */ + sk->protinfo.rose->fraglen += skb->len; + skb_queue_tail(&sk->protinfo.rose->frag_queue, skb); + + if ((skbn = alloc_skb(sk->protinfo.rose->fraglen, GFP_ATOMIC)) == NULL) + return 1; + + skbn->free = 1; + skbn->arp = 1; + skbn->sk = sk; + sk->rmem_alloc += skbn->truesize; + skbn->h.raw = skbn->data; + + skbo = skb_dequeue(&sk->protinfo.rose->frag_queue); + memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len); + kfree_skb(skbo, FREE_READ); + + while ((skbo = skb_dequeue(&sk->protinfo.rose->frag_queue)) != NULL) { + skb_pull(skbo, ROSE_MIN_LEN); + memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len); + kfree_skb(skbo, FREE_READ); + } + + sk->protinfo.rose->fraglen = 0; + } + + return sock_queue_rcv_skb(sk, skbn); +} + +/* + * State machine for state 1, Awaiting Call Accepted State. + * The handling of the timer(s) is in file rose_timer.c. + * Handling of state 0 and connection release is in af_rose.c. + */ +static int rose_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype) +{ + switch (frametype) { + + case ROSE_CALL_ACCEPTED: + sk->protinfo.rose->timer = 0; + sk->protinfo.rose->vs = 0; + sk->protinfo.rose->va = 0; + sk->protinfo.rose->vr = 0; + sk->protinfo.rose->vl = 0; + sk->protinfo.rose->state = ROSE_STATE_3; + sk->state = TCP_ESTABLISHED; + if (!sk->dead) + sk->state_change(sk); + break; + + case ROSE_CLEAR_REQUEST: + rose_clear_queues(sk); + sk->protinfo.rose->state = ROSE_STATE_0; + sk->state = TCP_CLOSE; + sk->err = ECONNREFUSED; + sk->shutdown |= SEND_SHUTDOWN; + if (!sk->dead) + sk->state_change(sk); + sk->dead = 1; + break; + + default: /* XXX */ + printk(KERN_WARNING "rose: unknown %02X in state 1\n", frametype); + break; + } + + return 0; +} + +/* + * State machine for state 2, Awaiting Clear Confirmation State. + * The handling of the timer(s) is in file rose_timer.c + * Handling of state 0 and connection release is in af_rose.c. + */ +static int rose_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype) +{ + switch (frametype) { + + case ROSE_CLEAR_REQUEST: + case ROSE_CLEAR_CONFIRMATION: + sk->protinfo.rose->state = ROSE_STATE_0; + sk->state = TCP_CLOSE; + sk->err = 0; + sk->shutdown |= SEND_SHUTDOWN; + if (!sk->dead) + sk->state_change(sk); + sk->dead = 1; + break; + + default: /* XXX */ + printk(KERN_WARNING "rose: unknown %02X in state 2\n", frametype); + break; + } + + return 0; +} + +/* + * State machine for state 3, Connected State. + * The handling of the timer(s) is in file rose_timer.c + * Handling of state 0 and connection release is in af_rose.c. + */ +static int rose_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m) +{ + int queued = 0; + + switch (frametype) { + + case ROSE_RESET_REQUEST: + rose_clear_queues(sk); + rose_write_internal(sk, ROSE_RESET_CONFIRMATION); + sk->protinfo.rose->condition = 0x00; + sk->protinfo.rose->vs = 0; + sk->protinfo.rose->vr = 0; + sk->protinfo.rose->va = 0; + sk->protinfo.rose->vl = 0; + break; + + case ROSE_CLEAR_REQUEST: + rose_clear_queues(sk); + rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION); + sk->protinfo.rose->state = ROSE_STATE_0; + sk->state = TCP_CLOSE; + sk->err = 0; + sk->shutdown |= SEND_SHUTDOWN; + if (!sk->dead) + sk->state_change(sk); + sk->dead = 1; + break; + + case ROSE_RR: + case ROSE_RNR: + if (frametype == ROSE_RNR) { + sk->protinfo.rose->condition |= PEER_RX_BUSY_CONDITION; + } else { + sk->protinfo.rose->condition &= ~PEER_RX_BUSY_CONDITION; + } + if (!rose_validate_nr(sk, nr)) { + rose_clear_queues(sk); + rose_write_internal(sk, ROSE_RESET_REQUEST); + sk->protinfo.rose->condition = 0x00; + sk->protinfo.rose->vs = 0; + sk->protinfo.rose->vr = 0; + sk->protinfo.rose->va = 0; + sk->protinfo.rose->vl = 0; + sk->protinfo.rose->state = ROSE_STATE_4; + sk->protinfo.rose->timer = sk->protinfo.rose->t2; + } else { + if (sk->protinfo.rose->condition & PEER_RX_BUSY_CONDITION) { + rose_frames_acked(sk, nr); + } else { + rose_check_iframes_acked(sk, nr); + } + } + break; + + case ROSE_DATA: /* XXX */ + sk->protinfo.rose->condition &= ~PEER_RX_BUSY_CONDITION; + if (!rose_validate_nr(sk, nr)) { + rose_clear_queues(sk); + rose_write_internal(sk, ROSE_RESET_REQUEST); + sk->protinfo.rose->condition = 0x00; + sk->protinfo.rose->vs = 0; + sk->protinfo.rose->vr = 0; + sk->protinfo.rose->va = 0; + sk->protinfo.rose->vl = 0; + sk->protinfo.rose->state = ROSE_STATE_4; + sk->protinfo.rose->timer = sk->protinfo.rose->t2; + break; + } + if (sk->protinfo.rose->condition & PEER_RX_BUSY_CONDITION) { + rose_frames_acked(sk, nr); + } else { + rose_check_iframes_acked(sk, nr); + } + if (sk->protinfo.rose->condition & OWN_RX_BUSY_CONDITION) + break; + if (ns == sk->protinfo.rose->vr) { + if (rose_queue_rx_frame(sk, skb, m) == 0) { + sk->protinfo.rose->vr = (sk->protinfo.rose->vr + 1) % ROSE_MODULUS; + queued = 1; + } else { + sk->protinfo.rose->condition |= OWN_RX_BUSY_CONDITION; + } + } + /* + * If the window is full, ack the frame. + */ + if (((sk->protinfo.rose->vl + sk->window) % ROSE_MODULUS) == sk->protinfo.rose->vr) + rose_enquiry_response(sk); + break; + + default: + printk(KERN_WARNING "rose: unknown %02X in state 3\n", frametype); + break; + } + + return queued; +} + +/* + * State machine for state 4, Awaiting Reset Confirmation State. + * The handling of the timer(s) is in file rose_timer.c + * Handling of state 0 and connection release is in af_rose.c. + */ +static int rose_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype) +{ + switch (frametype) { + + case ROSE_RESET_CONFIRMATION: + case ROSE_RESET_REQUEST: + sk->protinfo.rose->timer = 0; + sk->protinfo.rose->condition = 0x00; + sk->protinfo.rose->va = 0; + sk->protinfo.rose->vr = 0; + sk->protinfo.rose->vs = 0; + sk->protinfo.rose->vl = 0; + sk->protinfo.rose->state = ROSE_STATE_3; + break; + + case ROSE_CLEAR_REQUEST: + rose_clear_queues(sk); + rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION); + sk->protinfo.rose->timer = 0; + sk->protinfo.rose->state = ROSE_STATE_0; + sk->state = TCP_CLOSE; + sk->err = 0; + sk->shutdown |= SEND_SHUTDOWN; + if (!sk->dead) + sk->state_change(sk); + sk->dead = 1; + break; + + default: /* XXX */ + printk(KERN_WARNING "rose: unknown %02X in state 4\n", frametype); + break; + } + + return 0; +} + +/* Higher level upcall for a LAPB frame */ +int rose_process_rx_frame(struct sock *sk, struct sk_buff *skb) +{ + int queued = 0, frametype, ns, nr, q, d, m; + + if (sk->protinfo.rose->state == ROSE_STATE_0) + return 0; + + del_timer(&sk->timer); + + frametype = rose_decode(skb, &ns, &nr, &q, &d, &m); + + switch (sk->protinfo.rose->state) { + case ROSE_STATE_1: + queued = rose_state1_machine(sk, skb, frametype); + break; + case ROSE_STATE_2: + queued = rose_state2_machine(sk, skb, frametype); + break; + case ROSE_STATE_3: + queued = rose_state3_machine(sk, skb, frametype, ns, nr, q, d, m); + break; + case ROSE_STATE_4: + queued = rose_state4_machine(sk, skb, frametype); + break; + } + + rose_set_timer(sk); + + return queued; +} + +#endif diff --git a/net/rose/rose_link.c b/net/rose/rose_link.c new file mode 100644 index 000000000..d0bf308f0 --- /dev/null +++ b/net/rose/rose_link.c @@ -0,0 +1,301 @@ +/* + * Rose release 001 + * + * This is ALPHA test software. This code may break your machine, randomly fail to work with new + * releases, misbehave and/or generally screw up. It might even work. + * + * This code REQUIRES 2.1.0 or higher/ NET3.029 + * + * This module: + * This module 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. + * + * History + * Rose 001 Jonathan(G4KLX) Cloned from rose_timer.c + */ + +#include <linux/config.h> +#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE) +#include <linux/errno.h> +#include <linux/types.h> +#include <linux/socket.h> +#include <linux/in.h> +#include <linux/kernel.h> +#include <linux/sched.h> +#include <linux/timer.h> +#include <linux/string.h> +#include <linux/sockios.h> +#include <linux/net.h> +#include <net/ax25.h> +#include <linux/inet.h> +#include <linux/netdevice.h> +#include <linux/skbuff.h> +#include <net/sock.h> +#include <asm/segment.h> +#include <asm/system.h> +#include <linux/fcntl.h> +#include <linux/mm.h> +#include <linux/interrupt.h> +#include <linux/firewall.h> +#include <net/rose.h> + +static void rose_link_timer(unsigned long); + +/* + * Linux set/reset timer routines + */ +static void rose_link_set_timer(struct rose_neigh *neigh) +{ + unsigned long flags; + + save_flags(flags); + cli(); + del_timer(&neigh->timer); + restore_flags(flags); + + neigh->timer.next = neigh->timer.prev = NULL; + neigh->timer.data = (unsigned long)neigh; + neigh->timer.function = &rose_link_timer; + + neigh->timer.expires = jiffies + 10; + add_timer(&neigh->timer); +} + +static void rose_link_reset_timer(struct rose_neigh *neigh) +{ + unsigned long flags; + + save_flags(flags); + cli(); + del_timer(&neigh->timer); + restore_flags(flags); + + neigh->timer.data = (unsigned long)neigh; + neigh->timer.function = &rose_link_timer; + neigh->timer.expires = jiffies + 10; + add_timer(&neigh->timer); +} + +/* + * Rose Link Timer + * + * This routine is called every 100ms. Decrement timer by this + * amount - if expired then process the event. + */ +static void rose_link_timer(unsigned long param) +{ + struct rose_neigh *neigh = (struct rose_neigh *)param; + + if (neigh->t0timer == 0 || --neigh->t0timer > 0) { + rose_link_reset_timer(neigh); + return; + } + + /* + * T0 for a link has expired. + */ + rose_transmit_restart_request(neigh); + + neigh->t0timer = neigh->t0; + + rose_link_set_timer(neigh); +} + +/* + * This handles all restart and diagnostic frames. + */ +void rose_link_rx_restart(struct sk_buff *skb, struct rose_neigh *neigh, unsigned short frametype) +{ + struct sk_buff *skbn; + + switch (frametype) { + case ROSE_RESTART_REQUEST: + neigh->t0timer = 0; + neigh->restarted = 1; + del_timer(&neigh->timer); + rose_transmit_restart_confirmation(neigh); + break; + + case ROSE_RESTART_CONFIRMATION: + neigh->t0timer = 0; + neigh->restarted = 1; + del_timer(&neigh->timer); + break; + + case ROSE_DIAGNOSTIC: + printk(KERN_WARNING "rose: diagnostic #%d\n", skb->data[3]); + break; + + default: + printk(KERN_WARNING "rose: received unknown %02X with LCI 000\n", frametype); + break; + } + + if (neigh->restarted) { + while ((skbn = skb_dequeue(&neigh->queue)) != NULL) + if (!ax25_send_frame(skbn, (ax25_address *)neigh->dev->dev_addr, &neigh->callsign, neigh->digipeat, neigh->dev)) + kfree_skb(skbn, FREE_WRITE); + } +} + +/* + * This routine is called when a Restart Request is needed + */ +void rose_transmit_restart_request(struct rose_neigh *neigh) +{ + struct sk_buff *skb; + unsigned char *dptr; + int len; + + len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3; + + if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL) + return; + + skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN); + + dptr = skb_put(skb, ROSE_MIN_LEN + 3); + + *dptr++ = AX25_P_ROSE; + *dptr++ = GFI; + *dptr++ = 0x00; + *dptr++ = ROSE_RESTART_REQUEST; + *dptr++ = 0x00; + *dptr++ = 0; + + skb->free = 1; + skb->sk = NULL; + + if (!ax25_send_frame(skb, (ax25_address *)neigh->dev->dev_addr, &neigh->callsign, neigh->digipeat, neigh->dev)) + kfree_skb(skb, FREE_WRITE); +} + +/* + * This routine is called when a Restart Confirmation is needed + */ +void rose_transmit_restart_confirmation(struct rose_neigh *neigh) +{ + struct sk_buff *skb; + unsigned char *dptr; + int len; + + len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1; + + if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL) + return; + + skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN); + + dptr = skb_put(skb, ROSE_MIN_LEN + 1); + + *dptr++ = AX25_P_ROSE; + *dptr++ = GFI; + *dptr++ = 0x00; + *dptr++ = ROSE_RESTART_CONFIRMATION; + + skb->free = 1; + skb->sk = NULL; + + if (!ax25_send_frame(skb, (ax25_address *)neigh->dev->dev_addr, &neigh->callsign, neigh->digipeat, neigh->dev)) + kfree_skb(skb, FREE_WRITE); +} + +/* + * This routine is called when a Diagnostic is required. + */ +void rose_transmit_diagnostic(struct rose_neigh *neigh, unsigned char diag) +{ + struct sk_buff *skb; + unsigned char *dptr; + int len; + + len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 2; + + if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL) + return; + + skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN); + + dptr = skb_put(skb, ROSE_MIN_LEN + 2); + + *dptr++ = AX25_P_ROSE; + *dptr++ = GFI; + *dptr++ = 0x00; + *dptr++ = ROSE_DIAGNOSTIC; + *dptr++ = diag; + + skb->free = 1; + skb->sk = NULL; + + if (!ax25_send_frame(skb, (ax25_address *)neigh->dev->dev_addr, &neigh->callsign, neigh->digipeat, neigh->dev)) + kfree_skb(skb, FREE_WRITE); +} + +/* + * This routine is called when a Clear Request is needed outside of the context + * of a connected socket. + */ +void rose_transmit_clear_request(struct rose_neigh *neigh, unsigned int lci, unsigned char cause) +{ + struct sk_buff *skb; + unsigned char *dptr; + int len; + + len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3; + + if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL) + return; + + skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN); + + dptr = skb_put(skb, ROSE_MIN_LEN + 3); + + *dptr++ = AX25_P_ROSE; + *dptr++ = ((lci >> 8) & 0x0F) | GFI; + *dptr++ = ((lci >> 0) & 0xFF); + *dptr++ = ROSE_CLEAR_REQUEST; + *dptr++ = cause; + *dptr++ = 0x00; + + skb->free = 1; + skb->sk = NULL; + + if (!ax25_send_frame(skb, (ax25_address *)neigh->dev->dev_addr, &neigh->callsign, neigh->digipeat, neigh->dev)) + kfree_skb(skb, FREE_WRITE); +} + +void rose_transmit_link(struct sk_buff *skb, struct rose_neigh *neigh) +{ + unsigned char *dptr; + +#ifdef CONFIG_FIREWALL + if (call_fw_firewall(PF_ROSE, skb->dev, skb->data, NULL) != FW_ACCEPT) + return; +#endif + + if (!ax25_link_up((ax25_address *)neigh->dev->dev_addr, &neigh->callsign, neigh->dev)) + neigh->restarted = 0; + + dptr = skb_push(skb, 1); + *dptr++ = AX25_P_ROSE; + + skb->arp = 1; + skb->free = 1; + + if (neigh->restarted) { + if (!ax25_send_frame(skb, (ax25_address *)neigh->dev->dev_addr, &neigh->callsign, neigh->digipeat, neigh->dev)) + kfree_skb(skb, FREE_WRITE); + } else { + skb_queue_tail(&neigh->queue, skb); + + if (neigh->t0timer == 0) { + rose_transmit_restart_request(neigh); + neigh->t0timer = neigh->t0; + rose_link_set_timer(neigh); + } + } +} + +#endif diff --git a/net/rose/rose_out b/net/rose/rose_out new file mode 100644 index 000000000..745cb5a2b --- /dev/null +++ b/net/rose/rose_out @@ -0,0 +1,254 @@ +/* + * Rose release 001 + * + * This is ALPHA test software. This code may break your machine, randomly fail to work with new + * releases, misbehave and/or generally screw up. It might even work. + * + * This code REQUIRES 2.1.0 or higher/ NET3.029 + * + * This module: + * This module 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. + * + * History + * Rose 001 Jonathan(G4KLX) Cloned from nr_out.c + */ + +#include <linux/config.h> +#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE) +#include <linux/errno.h> +#include <linux/types.h> +#include <linux/socket.h> +#include <linux/in.h> +#include <linux/kernel.h> +#include <linux/sched.h> +#include <linux/timer.h> +#include <linux/string.h> +#include <linux/sockios.h> +#include <linux/net.h> +#include <net/ax25.h> +#include <linux/inet.h> +#include <linux/netdevice.h> +#include <linux/skbuff.h> +#include <net/sock.h> +#include <asm/segment.h> +#include <asm/system.h> +#include <linux/fcntl.h> +#include <linux/mm.h> +#include <linux/interrupt.h> +#include <net/rose.h> + +/* + * This is where all Rose frames pass; + */ +void rose_output(struct sock *sk, struct sk_buff *skb) +{ + struct sk_buff *skbn; + unsigned char header[ROSE_MIN_LEN]; + int err, frontlen, len; + + if (skb->len - ROSE_MIN_LEN > ROSE_PACLEN) { + /* Save a copy of the Header */ + memcpy(header, skb->data, ROSE_MIN_LEN); + skb_pull(skb, ROSE_MIN_LEN); + + frontlen = skb_headroom(skb); + + while (skb->len > 0) { + if ((skbn = sock_alloc_send_skb(sk, frontlen + ROSE_PACLEN, 0, 0, &err)) == NULL) + return; + + skbn->sk = sk; + skbn->free = 1; + skbn->arp = 1; + + skb_reserve(skbn, frontlen); + + len = (ROSE_PACLEN > skb->len) ? skb->len : ROSE_PACLEN; + + /* Copy the user data */ + memcpy(skb_put(skbn, len), skb->data, len); + skb_pull(skb, len); + + /* Duplicate the Header */ + skb_push(skbn, ROSE_MIN_LEN); + memcpy(skbn->data, header, ROSE_MIN_LEN); + + if (skb->len > 0) + skbn->data[2] |= M_BIT; + + skb_queue_tail(&sk->write_queue, skbn); /* Throw it on the queue */ + } + + skb->free = 1; + kfree_skb(skb, FREE_WRITE); + } else { + skb_queue_tail(&sk->write_queue, skb); /* Throw it on the queue */ + } + + if (sk->protinfo.rose->state == ROSE_STATE_3) + rose_kick(sk); +} + +/* + * This procedure is passed a buffer descriptor for an iframe. It builds + * the rest of the control part of the frame and then writes it out. + */ +static void rose_send_iframe(struct sock *sk, struct sk_buff *skb, int last) +{ + if (skb == NULL) + return; + + if (last) + skb->data[0] |= D_BIT; + + skb->data[2] |= (sk->protinfo.rose->vr << 5) & 0xE0; + skb->data[2] |= (sk->protinfo.rose->vs << 1) & 0x0E; + + rose_transmit_buffer(sk, skb); +} + +void rose_send_nak_frame(struct sock *sk) +{ + struct sk_buff *skb, *skbn; + + if ((skb = skb_peek(&sk->protinfo.rose->ack_queue)) == NULL) + return; + + if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) + return; + + skbn->data[2] = sk->protinfo.rose->va; + skbn->data[3] = sk->protinfo.rose->vr; + + if (sk->protinfo.rose->condition & OWN_RX_BUSY_CONDITION) + skbn->data[4] |= NR_CHOKE_FLAG; + + rose_transmit_buffer(sk, skbn); + + sk->protinfo.rose->condition &= ~ACK_PENDING_CONDITION; + sk->protinfo.rose->vl = sk->protinfo.rose->vr; + sk->protinfo.rose->t1timer = 0; +} + +void rose_kick(struct sock *sk) +{ + struct sk_buff *skb, *skbn; + int last = 1; + unsigned short start, end, next; + + del_timer(&sk->timer); + + start = (skb_peek(&sk->protinfo.rose->ack_queue) == NULL) ? sk->protinfo.rose->va : sk->protinfo.rose->vs; + end = (sk->protinfo.rose->va + sk->window) % ROSE_MODULUS; + + if (!(sk->protinfo.rose->condition & PEER_RX_BUSY_CONDITION) && + start != end && + skb_peek(&sk->write_queue) != NULL) { + + sk->protinfo.rose->vs = start; + + /* + * Transmit data until either we're out of data to send or + * the window is full. + */ + + /* + * Dequeue the frame and copy it. + */ + skb = skb_dequeue(&sk->write_queue); + + do { + if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) { + skb_queue_head(&sk->write_queue, skb); + break; + } + + next = (sk->protinfo.rose->vs + 1) % ROSE_MODULUS; + last = (next == end); + + /* + * Transmit the frame copy. + */ + rose_send_iframe(sk, skbn, last); + + sk->protinfo.rose->vs = next; + + /* + * Requeue the original data frame. + */ + skb_queue_tail(&sk->protinfo.rose->ack_queue, skb); + + } while (!last && (skb = skb_dequeue(&sk->write_queue)) != NULL); + + sk->protinfo.rose->vl = sk->protinfo.rose->vr; + sk->protinfo.rose->condition &= ~ACK_PENDING_CONDITION; + } + + rose_set_timer(sk); +} + +void rose_transmit_buffer(struct sock *sk, struct sk_buff *skb) +{ + unsigned char *dptr; + + dptr = skb_push(skb, 1); + *dptr = AX25_P_ROSE; + + skb->arp = 1; + + if (!ax25_send_frame(skb, (ax25_address *)sk->protinfo.rose->neighbour->dev->dev_addr, &sk->protinfo.rose->neighbour->callsign, sk->protinfo.rose->neighbour->digipeat, sk->protinfo.rose->neighbour->dev)) { + kfree_skb(skb, FREE_WRITE); + + sk->state = TCP_CLOSE; + sk->err = ENETUNREACH; + if (!sk->dead) + sk->state_change(sk); + sk->dead = 1; + } +} + +/* + * The following routines are taken from page 170 of the 7th ARRL Computer + * Networking Conference paper, as is the whole state machine. + */ + +void rose_establish_data_link(struct sock *sk) +{ + sk->protinfo.rose->condition = 0x00; + + rose_write_internal(sk, ROSE_CALL_REQUEST); + + sk->protinfo.rose->t1timer = sk->protinfo.rose->t1; +} + +/* + * Never send a NAK when we are CHOKEd. + */ +void rose_enquiry_response(struct sock *sk) +{ + int frametype = NR_INFOACK; + + if (sk->protinfo.rose->condition & OWN_RX_BUSY_CONDITION) + frametype |= NR_CHOKE_FLAG; + + rose_write_internal(sk, frametype); + + sk->protinfo.rose->vl = sk->protinfo.rose->vr; + sk->protinfo.rose->condition &= ~ACK_PENDING_CONDITION; +} + +void rose_check_iframes_acked(struct sock *sk, unsigned short nr) +{ + if (sk->protinfo.rose->vs == nr) { + rose_frames_acked(sk, nr); + } else { + if (sk->protinfo.rose->va != nr) { + rose_frames_acked(sk, nr); + } + } +} + +#endif diff --git a/net/rose/rose_out.c b/net/rose/rose_out.c new file mode 100644 index 000000000..50b2587f8 --- /dev/null +++ b/net/rose/rose_out.c @@ -0,0 +1,193 @@ +/* + * Rose release 001 + * + * This is ALPHA test software. This code may break your machine, randomly fail to work with new + * releases, misbehave and/or generally screw up. It might even work. + * + * This code REQUIRES 2.1.0 or higher/ NET3.029 + * + * This module: + * This module 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. + * + * History + * Rose 001 Jonathan(G4KLX) Cloned from nr_out.c + */ + +#include <linux/config.h> +#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE) +#include <linux/errno.h> +#include <linux/types.h> +#include <linux/socket.h> +#include <linux/in.h> +#include <linux/kernel.h> +#include <linux/sched.h> +#include <linux/timer.h> +#include <linux/string.h> +#include <linux/sockios.h> +#include <linux/net.h> +#include <net/ax25.h> +#include <linux/inet.h> +#include <linux/netdevice.h> +#include <linux/skbuff.h> +#include <net/sock.h> +#include <asm/segment.h> +#include <asm/system.h> +#include <linux/fcntl.h> +#include <linux/mm.h> +#include <linux/interrupt.h> +#include <net/rose.h> + +/* + * This is where all Rose frames pass; + */ +void rose_output(struct sock *sk, struct sk_buff *skb) +{ + struct sk_buff *skbn; + unsigned char header[ROSE_MIN_LEN]; + int err, frontlen, len; + + if (skb->len - ROSE_MIN_LEN > ROSE_PACLEN) { + /* Save a copy of the Header */ + memcpy(header, skb->data, ROSE_MIN_LEN); + skb_pull(skb, ROSE_MIN_LEN); + + frontlen = skb_headroom(skb); + + while (skb->len > 0) { + if ((skbn = sock_alloc_send_skb(sk, frontlen + ROSE_PACLEN, 0, 0, &err)) == NULL) + return; + + skbn->sk = sk; + skbn->free = 1; + skbn->arp = 1; + + skb_reserve(skbn, frontlen); + + len = (ROSE_PACLEN > skb->len) ? skb->len : ROSE_PACLEN; + + /* Copy the user data */ + memcpy(skb_put(skbn, len), skb->data, len); + skb_pull(skb, len); + + /* Duplicate the Header */ + skb_push(skbn, ROSE_MIN_LEN); + memcpy(skbn->data, header, ROSE_MIN_LEN); + + if (skb->len > 0) + skbn->data[2] |= M_BIT; + + skb_queue_tail(&sk->write_queue, skbn); /* Throw it on the queue */ + } + + skb->free = 1; + kfree_skb(skb, FREE_WRITE); + } else { + skb_queue_tail(&sk->write_queue, skb); /* Throw it on the queue */ + } + + if (sk->protinfo.rose->state == ROSE_STATE_3) + rose_kick(sk); +} + +/* + * This procedure is passed a buffer descriptor for an iframe. It builds + * the rest of the control part of the frame and then writes it out. + */ +static void rose_send_iframe(struct sock *sk, struct sk_buff *skb) +{ + if (skb == NULL) + return; + + skb->data[2] |= (sk->protinfo.rose->vr << 5) & 0xE0; + skb->data[2] |= (sk->protinfo.rose->vs << 1) & 0x0E; + + rose_transmit_link(skb, sk->protinfo.rose->neighbour); +} + +void rose_kick(struct sock *sk) +{ + struct sk_buff *skb, *skbn; + int last = 1; + unsigned short start, end, next; + + del_timer(&sk->timer); + + start = (skb_peek(&sk->protinfo.rose->ack_queue) == NULL) ? sk->protinfo.rose->va : sk->protinfo.rose->vs; + end = (sk->protinfo.rose->va + sk->window) % ROSE_MODULUS; + + if (!(sk->protinfo.rose->condition & PEER_RX_BUSY_CONDITION) && + start != end && + skb_peek(&sk->write_queue) != NULL) { + + sk->protinfo.rose->vs = start; + + /* + * Transmit data until either we're out of data to send or + * the window is full. + */ + + /* + * Dequeue the frame and copy it. + */ + skb = skb_dequeue(&sk->write_queue); + + do { + if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) { + skb_queue_head(&sk->write_queue, skb); + break; + } + + next = (sk->protinfo.rose->vs + 1) % ROSE_MODULUS; + last = (next == end); + + /* + * Transmit the frame copy. + */ + rose_send_iframe(sk, skbn); + + sk->protinfo.rose->vs = next; + + /* + * Requeue the original data frame. + */ + skb_queue_tail(&sk->protinfo.rose->ack_queue, skb); + + } while (!last && (skb = skb_dequeue(&sk->write_queue)) != NULL); + + sk->protinfo.rose->vl = sk->protinfo.rose->vr; + } + + rose_set_timer(sk); +} + +/* + * The following routines are taken from page 170 of the 7th ARRL Computer + * Networking Conference paper, as is the whole state machine. + */ + +void rose_enquiry_response(struct sock *sk) +{ + if (sk->protinfo.rose->condition & OWN_RX_BUSY_CONDITION) { + rose_write_internal(sk, ROSE_RNR); + } else { + rose_write_internal(sk, ROSE_RR); + } + + sk->protinfo.rose->vl = sk->protinfo.rose->vr; +} + +void rose_check_iframes_acked(struct sock *sk, unsigned short nr) +{ + if (sk->protinfo.rose->vs == nr) { + rose_frames_acked(sk, nr); + } else { + if (sk->protinfo.rose->va != nr) { + rose_frames_acked(sk, nr); + } + } +} + +#endif diff --git a/net/rose/rose_route.c b/net/rose/rose_route.c new file mode 100644 index 000000000..9396831b3 --- /dev/null +++ b/net/rose/rose_route.c @@ -0,0 +1,810 @@ +/* + * Rose release 001 + * + * This is ALPHA test software. This code may break your machine, randomly fail to work with new + * releases, misbehave and/or generally screw up. It might even work. + * + * This code REQUIRES 2.1.0 or higher/ NET3.029 + * + * This module: + * This module 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. + * + * History + * Rose 001 Jonathan(G4KLX) Cloned from nr_route.c. + */ + +#include <linux/config.h> +#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE) +#include <linux/errno.h> +#include <linux/types.h> +#include <linux/socket.h> +#include <linux/in.h> +#include <linux/kernel.h> +#include <linux/sched.h> +#include <linux/timer.h> +#include <linux/string.h> +#include <linux/sockios.h> +#include <linux/net.h> +#include <net/ax25.h> +#include <linux/inet.h> +#include <linux/netdevice.h> +#include <net/arp.h> +#include <linux/if_arp.h> +#include <linux/skbuff.h> +#include <net/sock.h> +#include <asm/segment.h> +#include <asm/system.h> +#include <asm/uaccess.h> +#include <linux/fcntl.h> +#include <linux/termios.h> /* For TIOCINQ/OUTQ */ +#include <linux/mm.h> +#include <linux/interrupt.h> +#include <linux/notifier.h> +#include <linux/firewall.h> +#include <net/rose.h> + +static unsigned int rose_neigh_no = 1; + +static struct rose_node *rose_node_list = NULL; +static struct rose_neigh *rose_neigh_list = NULL; +static struct rose_route *rose_route_list = NULL; + +static void rose_remove_neigh(struct rose_neigh *); + +/* + * Add a new route to a node, and in the process add the node and the + * neighbour if it is new. + */ +static int rose_add_node(struct rose_route_struct *rose_route, struct device *dev) +{ + struct rose_node *rose_node; + struct rose_neigh *rose_neigh; + unsigned long flags; + int i; + + for (rose_node = rose_node_list; rose_node != NULL; rose_node = rose_node->next) + if (rosecmp(&rose_route->address, &rose_node->address) == 0) + break; + + for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) + if (ax25cmp(&rose_route->neighbour, &rose_neigh->callsign) == 0 && rose_neigh->dev == dev) + break; + + if (rose_neigh == NULL) { + if ((rose_neigh = (struct rose_neigh *)kmalloc(sizeof(*rose_neigh), GFP_ATOMIC)) == NULL) + return -ENOMEM; + + rose_neigh->callsign = rose_route->neighbour; + rose_neigh->digipeat = NULL; + rose_neigh->dev = dev; + rose_neigh->count = 0; + rose_neigh->number = rose_neigh_no++; + rose_neigh->restarted = 0; + skb_queue_head_init(&rose_neigh->queue); + rose_neigh->t0 = sysctl_rose_restart_request_timeout; + rose_neigh->t0timer = 0; + init_timer(&rose_neigh->timer); + + if (rose_route->ndigis != 0) { + if ((rose_neigh->digipeat = kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL) { + kfree_s(rose_neigh, sizeof(*rose_neigh)); + return -ENOMEM; + } + rose_neigh->digipeat->ndigi = rose_route->ndigis; + for (i = 0; i < rose_route->ndigis; i++) + rose_neigh->digipeat->calls[i] = rose_route->digipeaters[i]; + } + + save_flags(flags); cli(); + rose_neigh->next = rose_neigh_list; + rose_neigh_list = rose_neigh; + restore_flags(flags); + } + + if (rose_node == NULL) { + if ((rose_node = (struct rose_node *)kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) + return -ENOMEM; + + rose_node->address = rose_route->address; + rose_node->which = 0; + rose_node->count = 1; + + rose_node->neighbour[0] = rose_neigh; + + save_flags(flags); cli(); + rose_node->next = rose_node_list; + rose_node_list = rose_node; + restore_flags(flags); + + rose_neigh->count++; + + return 0; + } + + /* We have space at the bottom, slot it in */ + if (rose_node->count < 3) { + rose_node->neighbour[2] = rose_node->neighbour[1]; + rose_node->neighbour[1] = rose_node->neighbour[0]; + + rose_node->neighbour[0] = rose_neigh; + + rose_node->count++; + rose_neigh->count++; + } + + return 0; +} + +static void rose_remove_node(struct rose_node *rose_node) +{ + struct rose_node *s; + unsigned long flags; + + save_flags(flags); + cli(); + + if ((s = rose_node_list) == rose_node) { + rose_node_list = rose_node->next; + restore_flags(flags); + kfree_s(rose_node, sizeof(struct rose_node)); + return; + } + + while (s != NULL && s->next != NULL) { + if (s->next == rose_node) { + s->next = rose_node->next; + restore_flags(flags); + kfree_s(rose_node, sizeof(struct rose_node)); + return; + } + + s = s->next; + } + + restore_flags(flags); +} + +static void rose_remove_neigh(struct rose_neigh *rose_neigh) +{ + struct rose_neigh *s; + unsigned long flags; + struct sk_buff *skb; + + del_timer(&rose_neigh->timer); + + while ((skb = skb_dequeue(&rose_neigh->queue)) != NULL) + kfree_skb(skb, FREE_WRITE); + + save_flags(flags); + cli(); + + if ((s = rose_neigh_list) == rose_neigh) { + rose_neigh_list = rose_neigh->next; + restore_flags(flags); + if (rose_neigh->digipeat != NULL) + kfree_s(rose_neigh->digipeat, sizeof(ax25_digi)); + kfree_s(rose_neigh, sizeof(struct rose_neigh)); + return; + } + + while (s != NULL && s->next != NULL) { + if (s->next == rose_neigh) { + s->next = rose_neigh->next; + restore_flags(flags); + if (rose_neigh->digipeat != NULL) + kfree_s(rose_neigh->digipeat, sizeof(ax25_digi)); + kfree_s(rose_neigh, sizeof(struct rose_neigh)); + return; + } + + s = s->next; + } + + restore_flags(flags); +} + +static void rose_remove_route(struct rose_route *rose_route) +{ + struct rose_route *s; + unsigned long flags; + + save_flags(flags); + cli(); + + if ((s = rose_route_list) == rose_route) { + rose_route_list = rose_route->next; + restore_flags(flags); + kfree_s(rose_route, sizeof(struct rose_route)); + return; + } + + while (s != NULL && s->next != NULL) { + if (s->next == rose_route) { + s->next = rose_route->next; + restore_flags(flags); + kfree_s(rose_route, sizeof(struct rose_route)); + return; + } + + s = s->next; + } + + restore_flags(flags); +} + +/* + * "Delete" a node. Strictly speaking remove a route to a node. The node + * is only deleted if no routes are left to it. + */ +static int rose_del_node(struct rose_route_struct *rose_route, struct device *dev) +{ + struct rose_node *rose_node; + struct rose_neigh *rose_neigh; + int i; + + for (rose_node = rose_node_list; rose_node != NULL; rose_node = rose_node->next) + if (rosecmp(&rose_route->address, &rose_node->address) == 0) + break; + + if (rose_node == NULL) return -EINVAL; + + for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) + if (ax25cmp(&rose_route->neighbour, &rose_neigh->callsign) == 0 && rose_neigh->dev == dev) + break; + + if (rose_neigh == NULL) return -EINVAL; + + for (i = 0; i < rose_node->count; i++) { + if (rose_node->neighbour[i] == rose_neigh) { + rose_neigh->count--; + + if (rose_neigh->count == 0) + rose_remove_neigh(rose_neigh); + + rose_node->count--; + + if (rose_node->count == 0) { + rose_remove_node(rose_node); + } else { + switch (i) { + case 0: + rose_node->neighbour[0] = rose_node->neighbour[1]; + case 1: + rose_node->neighbour[1] = rose_node->neighbour[2]; + case 2: + break; + } + } + + return 0; + } + } + + return -EINVAL; +} + +/* + * A device has been removed. Remove its routes and neighbours. + */ +void rose_rt_device_down(struct device *dev) +{ + struct rose_neigh *s, *rose_neigh = rose_neigh_list; + struct rose_node *t, *rose_node; + int i; + + while (rose_neigh != NULL) { + s = rose_neigh; + rose_neigh = rose_neigh->next; + + if (s->dev == dev) { + rose_node = rose_node_list; + + while (rose_node != NULL) { + t = rose_node; + rose_node = rose_node->next; + + for (i = 0; i < t->count; i++) { + if (t->neighbour[i] == s) { + t->count--; + + switch (i) { + case 0: + t->neighbour[0] = t->neighbour[1]; + case 1: + t->neighbour[1] = t->neighbour[2]; + case 2: + break; + } + } + } + + if (t->count <= 0) + rose_remove_node(t); + } + + rose_remove_neigh(s); + } + } +} + +/* + * A device has been removed. Remove its links. + */ +void rose_route_device_down(struct device *dev) +{ + struct rose_route *s, *rose_route = rose_route_list; + + while (rose_route != NULL) { + s = rose_route; + rose_route = rose_route->next; + + if (s->neigh1->dev == dev || s->neigh2->dev == dev) + rose_remove_route(s); + } +} + +/* + * Check that the device given is a valid AX.25 interface that is "up". + */ +struct device *rose_ax25_dev_get(char *devname) +{ + struct device *dev; + + if ((dev = dev_get(devname)) == NULL) + return NULL; + + if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25) + return dev; + + return NULL; +} + +/* + * Find the first active Rose device, usually "rose0". + */ +struct device *rose_dev_first(void) +{ + struct device *dev, *first = NULL; + + for (dev = dev_base; dev != NULL; dev = dev->next) + if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE) + if (first == NULL || strncmp(dev->name, first->name, 3) < 0) + first = dev; + + return first; +} + +/* + * Find the Rose device for the given address. + */ +struct device *rose_dev_get(rose_address *addr) +{ + struct device *dev; + + for (dev = dev_base; dev != NULL; dev = dev->next) + if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) + return dev; + + return NULL; +} + +/* + * Find a neighbour given a Rose address. + */ +struct rose_neigh *rose_get_neigh(rose_address *addr) +{ + struct rose_node *node; + + for (node = rose_node_list; node != NULL; node = node->next) + if (rosecmp(&node->address, addr) == 0) + break; + + if (node == NULL) return NULL; + + if (node->which >= node->count) return NULL; + + return node->neighbour[node->which]; +} + +/* + * Handle the ioctls that control the routing functions. + */ +int rose_rt_ioctl(unsigned int cmd, void *arg) +{ + struct rose_route_struct rose_route; + struct device *dev; + int err; + + switch (cmd) { + + case SIOCADDRT: + if ((err = verify_area(VERIFY_READ, arg, sizeof(struct rose_route_struct))) != 0) + return err; + copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)); + if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL) + return -EINVAL; + if (rose_dev_get(&rose_route.address) != NULL) /* Can't add routes to ourself */ + return -EINVAL; + return rose_add_node(&rose_route, dev); + + case SIOCDELRT: + if ((err = verify_area(VERIFY_READ, arg, sizeof(struct rose_route_struct))) != 0) + return err; + copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)); + if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL) + return -EINVAL; + return rose_del_node(&rose_route, dev); + + default: + return -EINVAL; + } + + return 0; +} + +/* + * A level 2 link has timed out, therefore it appears to be a poor link, + * then don't use that neighbour until it is reset. XXX others. + */ +void rose_link_failed(ax25_address *callsign, struct device *dev) +{ + struct rose_neigh *rose_neigh; + struct rose_node *rose_node; + struct sk_buff *skb; + + for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) + if (ax25cmp(&rose_neigh->callsign, callsign) == 0 && rose_neigh->dev == dev) + break; + + if (rose_neigh == NULL) return; + + rose_neigh->restarted = 0; + rose_neigh->t0timer = 0; + del_timer(&rose_neigh->timer); + + while ((skb = skb_dequeue(&rose_neigh->queue)) != NULL) + kfree_skb(skb, FREE_WRITE); + + for (rose_node = rose_node_list; rose_node != NULL; rose_node = rose_node->next) + if (rose_node->which < rose_node->count && rose_node->neighbour[rose_node->which] == rose_neigh) + rose_node->which++; +} + +/* + * A device has been "downed" remove its link status. XXX others. + */ +void rose_link_device_down(struct device *dev) +{ + struct rose_neigh *rose_neigh; + struct rose_node *rose_node; + struct sk_buff *skb; + + for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) { + if (rose_neigh->dev == dev) { + rose_neigh->restarted = 0; + rose_neigh->t0timer = 0; + del_timer(&rose_neigh->timer); + + while ((skb = skb_dequeue(&rose_neigh->queue)) != NULL) + kfree_skb(skb, FREE_WRITE); + + for (rose_node = rose_node_list; rose_node != NULL; rose_node = rose_node->next) + if (rose_node->which < rose_node->count && rose_node->neighbour[rose_node->which] == rose_neigh) + rose_node->which++; + } + } +} + +/* + * Route a frame to an appropriate AX.25 connection. A NULL ax25_cb + * indicates an internally generated frame. + */ +int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25) +{ + struct rose_neigh *rose_neigh, *new_neigh; + struct rose_node *rose_node; + struct rose_route *rose_route; + rose_address *dest_addr; + struct sock *sk; + unsigned short frametype; + unsigned int lci; + struct device *dev; + unsigned long flags; + +#ifdef CONFIG_FIREWALL + if (call_in_firewall(PF_ROSE, skb->dev, skb->data, NULL) != FW_ACCEPT) + return 0; +#endif + + frametype = skb->data[2]; + lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF); + + for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) + if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 && ax25->device == rose_neigh->dev) + break; + + if (rose_neigh == NULL) + return 0; + + /* + * LCI of zero is always for us, and its always a restart + * frame. + */ + if (lci == 0) { + rose_link_rx_restart(skb, rose_neigh, frametype); + return 0; + } + + /* + * Find an existing socket. + */ + if ((sk = rose_find_socket(lci, rose_neigh->dev)) != NULL) { + skb->h.raw = skb->data; + return rose_process_rx_frame(sk, skb); + } + + /* + * Is is a Call Request and is it for us ? + */ + if (frametype == ROSE_CALL_REQUEST) { + dest_addr = (rose_address *)(skb->data + 4); + + if ((dev = rose_dev_get(dest_addr)) != NULL) + return rose_rx_call_request(skb, dev, rose_neigh, lci); + } + + if (!sysctl_rose_routing_control) { + rose_transmit_clear_request(rose_neigh, lci, 0x0D); + return 0; + } + + /* + * Route it to the next in line if we have an entry for it. + */ + + /* + * We should check for the random number in the facilities + * here. XXX. + */ + for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next) { + if (rose_route->lci1 == lci && rose_route->neigh1 == rose_neigh) { + skb->data[0] &= 0xF0; + skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F; + skb->data[1] = (rose_route->lci2 >> 0) & 0xFF; + rose_transmit_link(skb, rose_route->neigh2); + if (frametype == ROSE_CLEAR_CONFIRMATION) + rose_remove_route(rose_route); + return 1; + } + if (rose_route->lci2 == lci && rose_route->neigh2 == rose_neigh) { + skb->data[0] &= 0xF0; + skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F; + skb->data[1] = (rose_route->lci1 >> 0) & 0xFF; + rose_transmit_link(skb, rose_route->neigh1); + if (frametype == ROSE_CLEAR_CONFIRMATION) + rose_remove_route(rose_route); + return 1; + } + } + + /* + * We know that: + * 1. The frame isn't for us, + * 2. It isn't "owned" by any existing route. + */ + if (frametype != ROSE_CALL_REQUEST) /* XXX */ + return 0; + + dest_addr = (rose_address *)(skb->data + 4); + + /* + * Create a new route entry, if we can. + */ + for (rose_node = rose_node_list; rose_node != NULL; rose_node = rose_node->next) + if (rosecmp(&rose_node->address, dest_addr) == 0) + break; + /* + * Its an unknown node, or is unreachable. + */ + if (rose_node == NULL || rose_node->which >= rose_node->count) { + rose_transmit_clear_request(rose_neigh, lci, 0x0D); + return 0; + } + + if ((rose_route = (struct rose_route *)kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) { + rose_transmit_clear_request(rose_neigh, lci, 0x0D); + return 0; + } + + new_neigh = rose_node->neighbour[rose_node->which]; + + rose_route->lci1 = lci; + rose_route->neigh1 = rose_neigh; + rose_route->lci2 = rose_new_lci(new_neigh->dev); + rose_route->neigh2 = new_neigh; + + save_flags(flags); cli(); + rose_route->next = rose_route_list; + rose_route_list = rose_route; + restore_flags(flags); + + skb->data[0] &= 0xF0; + skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F; + skb->data[1] = (rose_route->lci2 >> 0) & 0xFF; + + rose_transmit_link(skb, rose_route->neigh2); + + return 1; +} + +int rose_nodes_get_info(char *buffer, char **start, off_t offset, + int length, int dummy) +{ + struct rose_node *rose_node; + int len = 0; + off_t pos = 0; + off_t begin = 0; + int i; + + cli(); + + len += sprintf(buffer, "address w n neigh neigh neigh\n"); + + for (rose_node = rose_node_list; rose_node != NULL; rose_node = rose_node->next) { + len += sprintf(buffer + len, "%-10s %d %d", + rose2asc(&rose_node->address), + rose_node->which + 1, + rose_node->count); + + for (i = 0; i < rose_node->count; i++) + len += sprintf(buffer + len, " %05d", + rose_node->neighbour[i]->number); + + len += sprintf(buffer + len, "\n"); + + pos = begin + len; + + if (pos < offset) { + len = 0; + begin = pos; + } + + if (pos > offset + length) + break; + } + + sti(); + + *start = buffer + (offset - begin); + len -= (offset - begin); + + if (len > length) len = length; + + return len; +} + +int rose_neigh_get_info(char *buffer, char **start, off_t offset, + int length, int dummy) +{ + struct rose_neigh *rose_neigh; + int len = 0; + off_t pos = 0; + off_t begin = 0; + + cli(); + + len += sprintf(buffer, "addr callsign dev count restart t0\n"); + + for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) { + len += sprintf(buffer + len, "%05d %-9s %-4s %3d %3s %3d/%03d\n", + rose_neigh->number, + ax2asc(&rose_neigh->callsign), + rose_neigh->dev ? rose_neigh->dev->name : "???", + rose_neigh->count, + (rose_neigh->restarted) ? "yes" : "no", + rose_neigh->t0timer / PR_SLOWHZ, + rose_neigh->t0 / PR_SLOWHZ); + + pos = begin + len; + + if (pos < offset) { + len = 0; + begin = pos; + } + + if (pos > offset + length) + break; + } + + sti(); + + *start = buffer + (offset - begin); + len -= (offset - begin); + + if (len > length) len = length; + + return len; +} + +int rose_routes_get_info(char *buffer, char **start, off_t offset, + int length, int dummy) +{ + struct rose_route *rose_route; + int len = 0; + off_t pos = 0; + off_t begin = 0; + + cli(); + + len += sprintf(buffer, "lci callsign dev <-> lci callsign dev\n"); + + for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next) { + len += sprintf(buffer + len, "%3.3X %-9s %-4s ", + rose_route->lci1, + ax2asc(&rose_route->neigh1->callsign), + rose_route->neigh1->dev ? rose_route->neigh1->dev->name : "???"); + len += sprintf(buffer + len, "%3.3X %-9s %-4s\n", + rose_route->lci2, + ax2asc(&rose_route->neigh2->callsign), + rose_route->neigh2->dev ? rose_route->neigh2->dev->name : "???"); + + pos = begin + len; + + if (pos < offset) { + len = 0; + begin = pos; + } + + if (pos > offset + length) + break; + } + + sti(); + + *start = buffer + (offset - begin); + len -= (offset - begin); + + if (len > length) len = length; + + return len; +} + +#ifdef MODULE + +/* + * Release all memory associated with Rose routing structures. + */ +void rose_rt_free(void) +{ + struct rose_neigh *s, *rose_neigh = rose_neigh_list; + struct rose_node *t, *rose_node = rose_node_list; + struct rose_route *u, *rose_route = rose_route_list; + + while (rose_neigh != NULL) { + s = rose_neigh; + rose_neigh = rose_neigh->next; + + rose_remove_neigh(s); + } + + while (rose_node != NULL) { + t = rose_node; + rose_node = rose_node->next; + + rose_remove_node(t); + } + + while (rose_route != NULL) { + u = rose_route; + rose_route = rose_route->next; + + rose_remove_route(u); + } +} + +#endif + +#endif diff --git a/net/rose/rose_subr.c b/net/rose/rose_subr.c new file mode 100644 index 000000000..0c1c83fa8 --- /dev/null +++ b/net/rose/rose_subr.c @@ -0,0 +1,494 @@ +/* + * Rose release 001 + * + * This is ALPHA test software. This code may break your machine, randomly fail to work with new + * releases, misbehave and/or generally screw up. It might even work. + * + * This code REQUIRES 2.1.0 or higher/ NET3.029 + * + * This module: + * This module 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. + * + * History + * Rose 001 Jonathan(G4KLX) Cloned from nr_subr.c + */ + +#include <linux/config.h> +#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE) +#include <linux/errno.h> +#include <linux/types.h> +#include <linux/socket.h> +#include <linux/in.h> +#include <linux/kernel.h> +#include <linux/sched.h> +#include <linux/timer.h> +#include <linux/string.h> +#include <linux/sockios.h> +#include <linux/net.h> +#include <net/ax25.h> +#include <linux/inet.h> +#include <linux/netdevice.h> +#include <linux/skbuff.h> +#include <net/sock.h> +#include <asm/segment.h> +#include <asm/system.h> +#include <linux/fcntl.h> +#include <linux/mm.h> +#include <linux/interrupt.h> +#include <net/rose.h> + +/* + * This routine purges all of the queues of frames. + */ +void rose_clear_queues(struct sock *sk) +{ + struct sk_buff *skb; + + while ((skb = skb_dequeue(&sk->write_queue)) != NULL) { + skb->sk = sk; + skb->free = 1; + kfree_skb(skb, FREE_WRITE); + } + + while ((skb = skb_dequeue(&sk->protinfo.rose->ack_queue)) != NULL) { + skb->sk = sk; + skb->free = 1; + kfree_skb(skb, FREE_WRITE); + } + + while ((skb = skb_dequeue(&sk->protinfo.rose->frag_queue)) != NULL) { + kfree_skb(skb, FREE_READ); + } +} + +/* + * This routine purges the input queue of those frames that have been + * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the + * SDL diagram. + */ +void rose_frames_acked(struct sock *sk, unsigned short nr) +{ + struct sk_buff *skb; + + /* + * Remove all the ack-ed frames from the ack queue. + */ + if (sk->protinfo.rose->va != nr) { + while (skb_peek(&sk->protinfo.rose->ack_queue) != NULL && sk->protinfo.rose->va != nr) { + skb = skb_dequeue(&sk->protinfo.rose->ack_queue); + skb->sk = sk; + skb->free = 1; + kfree_skb(skb, FREE_WRITE); + sk->protinfo.rose->va = (sk->protinfo.rose->va + 1) % ROSE_MODULUS; + } + } +} + +/* + * Requeue all the un-ack-ed frames on the output queue to be picked + * up by rose_kick called from the timer. This arrangement handles the + * possibility of an empty output queue. + */ +void rose_requeue_frames(struct sock *sk) +{ + struct sk_buff *skb, *skb_prev = NULL; + + while ((skb = skb_dequeue(&sk->protinfo.rose->ack_queue)) != NULL) { + if (skb_prev == NULL) + skb_queue_head(&sk->write_queue, skb); + else + skb_append(skb_prev, skb); + skb_prev = skb; + } +} + +/* + * Validate that the value of nr is between va and vs. Return true or + * false for testing. + */ +int rose_validate_nr(struct sock *sk, unsigned short nr) +{ + unsigned short vc = sk->protinfo.rose->va; + + while (vc != sk->protinfo.rose->vs) { + if (nr == vc) return 1; + vc = (vc + 1) % ROSE_MODULUS; + } + + if (nr == sk->protinfo.rose->vs) return 1; + + return 0; +} + +/* + * This routine is called when the packet layer internally generates a + * control frame. + */ +void rose_write_internal(struct sock *sk, int frametype) +{ + struct sk_buff *skb; + unsigned char *dptr; + unsigned char lci1, lci2; + char buffer[100]; + int len, faclen = 0; + + len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1; + + switch (frametype) { + case ROSE_CALL_REQUEST: + len += 1 + ROSE_ADDR_LEN + ROSE_ADDR_LEN; + faclen = rose_create_facilities(buffer, sk->protinfo.rose); + len += faclen; + break; + case ROSE_CALL_ACCEPTED: + case ROSE_CLEAR_REQUEST: + case ROSE_RESET_REQUEST: + case ROSE_DIAGNOSTIC: + len += 2; + break; + case ROSE_INTERRUPT: + len += 1; + break; + } + + if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL) + return; + + /* + * Space for AX.25 header and PID. + */ + skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 1); + + dptr = skb_put(skb, skb_tailroom(skb)); + + lci1 = (sk->protinfo.rose->lci >> 8) & 0x0F; + lci2 = (sk->protinfo.rose->lci >> 0) & 0xFF; + + switch (frametype) { + + case ROSE_CALL_REQUEST: + *dptr++ = GFI | lci1; + *dptr++ = lci2; + *dptr++ = frametype; + *dptr++ = 0xAA; + memcpy(dptr, &sk->protinfo.rose->dest_addr, ROSE_ADDR_LEN); + dptr += ROSE_ADDR_LEN; + memcpy(dptr, &sk->protinfo.rose->source_addr, ROSE_ADDR_LEN); + dptr += ROSE_ADDR_LEN; + memcpy(dptr, buffer, faclen); + dptr += faclen; + break; + + case ROSE_CALL_ACCEPTED: + *dptr++ = GFI | lci1; + *dptr++ = lci2; + *dptr++ = frametype; + *dptr++ = 0x00; /* Address length */ + *dptr++ = 0; /* Facilities length */ + break; + + case ROSE_CLEAR_REQUEST: + case ROSE_RESET_REQUEST: + *dptr++ = GFI | lci1; + *dptr++ = lci2; + *dptr++ = frametype; + *dptr++ = 0x00; /* XXX */ + *dptr++ = 0x00; /* XXX */ + break; + + case ROSE_INTERRUPT: + *dptr++ = GFI | lci1; + *dptr++ = lci2; + *dptr++ = frametype; + *dptr++ = 0x00; /* XXX */ + break; + + case ROSE_RR: + case ROSE_RNR: + case ROSE_REJ: + *dptr++ = GFI | lci1; + *dptr++ = lci2; + *dptr = frametype; + *dptr++ |= (sk->protinfo.rose->vr << 5) & 0xE0; + break; + + case ROSE_CLEAR_CONFIRMATION: + case ROSE_INTERRUPT_CONFIRMATION: + case ROSE_RESET_CONFIRMATION: + *dptr++ = GFI | lci1; + *dptr++ = lci2; + *dptr++ = frametype; + break; + + default: + printk(KERN_ERR "rose_write_internal: invalid frametype %02X\n", frametype); + kfree_skb(skb, FREE_WRITE); + return; + } + + rose_transmit_link(skb, sk->protinfo.rose->neighbour); +} + +int rose_decode(struct sk_buff *skb, int *ns, int *nr, int *q, int *d, int *m) +{ + unsigned char *frame; + + frame = skb->data; + + *ns = *nr = *q = *d = *m = 0; + + switch (frame[2]) { + case ROSE_CALL_REQUEST: + case ROSE_CALL_ACCEPTED: + case ROSE_CLEAR_REQUEST: + case ROSE_CLEAR_CONFIRMATION: + case ROSE_INTERRUPT: + case ROSE_INTERRUPT_CONFIRMATION: + case ROSE_RESET_REQUEST: + case ROSE_RESET_CONFIRMATION: + case ROSE_RESTART_REQUEST: + case ROSE_RESTART_CONFIRMATION: + case ROSE_REGISTRATION_REQUEST: + case ROSE_REGISTRATION_CONFIRMATION: + case ROSE_DIAGNOSTIC: + return frame[2]; + default: + break; + } + + if ((frame[2] & 0x1F) == ROSE_RR || + (frame[2] & 0x1F) == ROSE_RNR || + (frame[2] & 0x1F) == ROSE_REJ) { + *nr = (frame[2] >> 5) & 0x07; + return frame[2] & 0x1F; + } + + if ((frame[2] & 0x01) == ROSE_DATA) { + *q = (frame[0] & Q_BIT) == Q_BIT; + *d = (frame[0] & D_BIT) == D_BIT; + *m = (frame[2] & M_BIT) == M_BIT; + *nr = (frame[2] >> 5) & 0x07; + *ns = (frame[2] >> 1) & 0x07; + return ROSE_DATA; + } + + return ROSE_ILLEGAL; +} + +static int rose_parse_national(unsigned char *p, rose_cb *rose, int len) +{ + unsigned char l, n = 0; + + do { + switch (*p & 0xC0) { + case 0x00: + p += 2; + n += 2; + len -= 2; + break; + + case 0x40: + if (*p == FAC_NATIONAL_RAND) + rose->rand = ((p[1] << 8) & 0xFF00) + ((p[2] << 0) & 0x00FF); + p += 3; + n += 3; + len -= 3; + break; + + case 0x80: + p += 4; + n += 4; + len -= 4; + break; + + case 0xC0: + l = p[1]; + if (*p == FAC_NATIONAL_DEST_DIGI) { + memcpy(&rose->source_digi, p + 2, AX25_ADDR_LEN); + rose->source_ndigis = 1; + } + if (*p == FAC_NATIONAL_SRC_DIGI) { + memcpy(&rose->dest_digi, p + 2, AX25_ADDR_LEN); + rose->dest_ndigis = 1; + } + p += l + 2; + n += l + 2; + len -= l + 2; + break; + } + } while (*p != 0x00 && len > 0); + + return n; +} + +static int rose_parse_ccitt(unsigned char *p, rose_cb *rose, int len) +{ + unsigned char l, n = 0; + char callsign[11]; + + do { + switch (*p & 0xC0) { + case 0x00: + p += 2; + n += 2; + len -= 2; + break; + + case 0x40: + p += 3; + n += 3; + len -= 3; + break; + + case 0x80: + p += 4; + n += 4; + len -= 4; + break; + + case 0xC0: + l = p[1]; + if (*p == FAC_CCITT_DEST_NSAP) { + memcpy(&rose->source_addr, p + 7, ROSE_ADDR_LEN); + memcpy(callsign, p + 12, l - 10); + callsign[l - 10] = '\0'; + rose->source_call = *asc2ax(callsign); + } + if (*p == FAC_CCITT_SRC_NSAP) { + memcpy(&rose->dest_addr, p + 7, ROSE_ADDR_LEN); + memcpy(callsign, p + 12, l - 10); + callsign[l - 10] = '\0'; + rose->dest_call = *asc2ax(callsign); + } + p += l + 2; + n += l + 2; + len -= l + 2; + break; + } + } while (*p != 0x00 && len > 0); + + return n; +} + +int rose_parse_facilities(struct sk_buff *skb, rose_cb *rose) +{ + int facilities_len, len; + unsigned char *p; + + memset(rose, 0x00, sizeof(rose_cb)); + + len = (((skb->data[3] >> 4) & 0x0F) + 1) / 2; + len += (((skb->data[3] >> 0) & 0x0F) + 1) / 2; + + p = skb->data + len + 4; + + facilities_len = *p++; + + if (facilities_len == 0) + return 0; + + while (facilities_len > 0) { + if (*p == 0x00) { + facilities_len--; + p++; + + switch (*p) { + case FAC_NATIONAL: /* National */ + len = rose_parse_national(p + 1, rose, facilities_len - 1); + facilities_len -= len + 1; + p += len + 1; + break; + + case FAC_CCITT: /* CCITT */ + len = rose_parse_ccitt(p + 1, rose, facilities_len - 1); + facilities_len -= len + 1; + p += len + 1; + break; + + default: + printk(KERN_DEBUG "rose_parse_facilities: unknown facilities family %02X\n", *p); + facilities_len--; + p++; + break; + } + } + } + + return 1; +} + +int rose_create_facilities(unsigned char *buffer, rose_cb *rose) +{ + unsigned char *p = buffer + 1; + char *callsign; + int len; + + /* National Facilities */ + if (rose->rand != 0 || rose->source_ndigis == 1 || rose->dest_ndigis == 1) { + *p++ = 0x00; + *p++ = FAC_NATIONAL; + + if (rose->rand != 0) { + *p++ = FAC_NATIONAL_RAND; + *p++ = (rose->rand >> 8) & 0xFF; + *p++ = (rose->rand >> 0) & 0xFF; + } + + if (rose->source_ndigis == 1) { + *p++ = FAC_NATIONAL_SRC_DIGI; + *p++ = AX25_ADDR_LEN; + memcpy(p, &rose->source_digi, AX25_ADDR_LEN); + p += AX25_ADDR_LEN; + } + + if (rose->dest_ndigis == 1) { + *p++ = FAC_NATIONAL_DEST_DIGI; + *p++ = AX25_ADDR_LEN; + memcpy(p, &rose->dest_digi, AX25_ADDR_LEN); + p += AX25_ADDR_LEN; + } + } + + *p++ = 0x00; + *p++ = FAC_CCITT; + + *p++ = FAC_CCITT_DEST_NSAP; + + callsign = ax2asc(&rose->dest_call); + + *p++ = strlen(callsign) + 10; + *p++ = (strlen(callsign) + 9) * 2; /* ??? */ + + *p++ = 0x47; *p++ = 0x00; *p++ = 0x11; + *p++ = ROSE_ADDR_LEN * 2; + memcpy(p, &rose->dest_addr, ROSE_ADDR_LEN); + p += ROSE_ADDR_LEN; + + memcpy(p, callsign, strlen(callsign)); + p += strlen(callsign); + + *p++ = FAC_CCITT_SRC_NSAP; + + callsign = ax2asc(&rose->source_call); + + *p++ = strlen(callsign) + 10; + *p++ = (strlen(callsign) + 9) * 2; /* ??? */ + + *p++ = 0x47; *p++ = 0x00; *p++ = 0x11; + *p++ = ROSE_ADDR_LEN * 2; + memcpy(p, &rose->source_addr, ROSE_ADDR_LEN); + p += ROSE_ADDR_LEN; + + memcpy(p, callsign, strlen(callsign)); + p += strlen(callsign); + + len = p - buffer; + buffer[0] = len - 1; + + return len; +} + +#endif diff --git a/net/rose/rose_timer.c b/net/rose/rose_timer.c new file mode 100644 index 000000000..313756847 --- /dev/null +++ b/net/rose/rose_timer.c @@ -0,0 +1,153 @@ +/* + * Rose release 001 + * + * This is ALPHA test software. This code may break your machine, randomly fail to work with new + * releases, misbehave and/or generally screw up. It might even work. + * + * This code REQUIRES 2.1.0 or higher/ NET3.029 + * + * This module: + * This module 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. + * + * History + * Rose 001 Jonathan(G4KLX) Cloned from nr_timer.c + */ + +#include <linux/config.h> +#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE) +#include <linux/errno.h> +#include <linux/types.h> +#include <linux/socket.h> +#include <linux/in.h> +#include <linux/kernel.h> +#include <linux/sched.h> +#include <linux/timer.h> +#include <linux/string.h> +#include <linux/sockios.h> +#include <linux/net.h> +#include <net/ax25.h> +#include <linux/inet.h> +#include <linux/netdevice.h> +#include <linux/skbuff.h> +#include <net/sock.h> +#include <asm/segment.h> +#include <asm/system.h> +#include <linux/fcntl.h> +#include <linux/mm.h> +#include <linux/interrupt.h> +#include <net/rose.h> + +static void rose_timer(unsigned long); + +/* + * Linux set/reset timer routines + */ +void rose_set_timer(struct sock *sk) +{ + unsigned long flags; + + save_flags(flags); + cli(); + del_timer(&sk->timer); + restore_flags(flags); + + sk->timer.next = sk->timer.prev = NULL; + sk->timer.data = (unsigned long)sk; + sk->timer.function = &rose_timer; + + sk->timer.expires = jiffies + 10; + add_timer(&sk->timer); +} + +static void rose_reset_timer(struct sock *sk) +{ + unsigned long flags; + + save_flags(flags); + cli(); + del_timer(&sk->timer); + restore_flags(flags); + + sk->timer.data = (unsigned long)sk; + sk->timer.function = &rose_timer; + sk->timer.expires = jiffies + 10; + add_timer(&sk->timer); +} + +/* + * Rose Timer + * + * This routine is called every 100ms. Decrement timer by this + * amount - if expired then process the event. + */ +static void rose_timer(unsigned long param) +{ + struct sock *sk = (struct sock *)param; + + switch (sk->protinfo.rose->state) { + case ROSE_STATE_0: + /* Magic here: If we listen() and a new link dies before it + is accepted() it isn't 'dead' so doesn't get removed. */ + if (sk->destroy || (sk->state == TCP_LISTEN && sk->dead)) { + del_timer(&sk->timer); + rose_destroy_socket(sk); + return; + } + break; + + case ROSE_STATE_3: + /* + * Check for the state of the receive buffer. + */ + if (sk->rmem_alloc < (sk->rcvbuf / 2) && (sk->protinfo.rose->condition & OWN_RX_BUSY_CONDITION)) { + sk->protinfo.rose->condition &= ~OWN_RX_BUSY_CONDITION; + sk->protinfo.rose->vl = sk->protinfo.rose->vr; + rose_write_internal(sk, ROSE_RR); + break; + } + /* + * Check for frames to transmit. + */ + rose_kick(sk); + break; + + default: + break; + } + + if (sk->protinfo.rose->timer == 0 || --sk->protinfo.rose->timer > 0) { + rose_reset_timer(sk); + return; + } + + /* + * Timer has expired, it may have been T1, T2, or T3. We can tell + * by the socket state. + */ + switch (sk->protinfo.rose->state) { + case ROSE_STATE_1: /* T1 */ + case ROSE_STATE_4: /* T2 */ + rose_write_internal(sk, ROSE_CLEAR_REQUEST); + sk->protinfo.rose->state = ROSE_STATE_2; + sk->protinfo.rose->timer = sk->protinfo.rose->t3; + break; + + case ROSE_STATE_2: /* T3 */ + rose_clear_queues(sk); + sk->protinfo.rose->state = ROSE_STATE_0; + sk->state = TCP_CLOSE; + sk->err = ETIMEDOUT; + sk->shutdown |= SEND_SHUTDOWN; + if (!sk->dead) + sk->state_change(sk); + sk->dead = 1; + break; + } + + rose_set_timer(sk); +} + +#endif diff --git a/net/rose/sysctl_net_rose.c b/net/rose/sysctl_net_rose.c new file mode 100644 index 000000000..558702dbd --- /dev/null +++ b/net/rose/sysctl_net_rose.c @@ -0,0 +1,62 @@ +/* -*- linux-c -*- + * sysctl_net_rose.c: sysctl interface to net Rose subsystem. + * + * Begun April 1, 1996, Mike Shaver. + * Added /proc/sys/net/rose directory entry (empty =) ). [MS] + */ + +#include <linux/mm.h> +#include <linux/sysctl.h> +#include <net/ax25.h> +#include <net/rose.h> + +static int min_timer[] = {1 * PR_SLOWHZ}; +static int max_timer[] = {300 * PR_SLOWHZ}; +static int min_idle[] = {0 * PR_SLOWHZ}; +static int max_idle[] = {65535 * PR_SLOWHZ}; +static int min_route[] = {0}; +static int max_route[] = {0}; + +static struct ctl_table_header *rose_table_header; + +static ctl_table rose_table[] = { + {NET_ROSE_RESTART_REQUEST_TIMEOUT, "restart_request_timeout", + &sysctl_rose_restart_request_timeout, sizeof(int), 0644, NULL, + &proc_dointvec_minmax, &sysctl_intvec, NULL, &min_timer, &max_timer}, + {NET_ROSE_CALL_REQUEST_TIMEOUT, "call_request_timeout", + &sysctl_rose_call_request_timeout, sizeof(int), 0644, NULL, + &proc_dointvec_minmax, &sysctl_intvec, NULL, &min_timer, &max_timer}, + {NET_ROSE_RESET_REQUEST_TIMEOUT, "reset_request_timeout", + &sysctl_rose_reset_request_timeout, sizeof(int), 0644, NULL, + &proc_dointvec_minmax, &sysctl_intvec, NULL, &min_timer, &max_timer}, + {NET_ROSE_CLEAR_REQUEST_TIMEOUT, "clear_request_timeout", + &sysctl_rose_clear_request_timeout, sizeof(int), 0644, NULL, + &proc_dointvec_minmax, &sysctl_intvec, NULL, &min_timer, &max_timer}, + {NET_ROSE_NO_ACTIVITY_TIMEOUT, "no_activity_timeout", + &sysctl_rose_no_activity_timeout, sizeof(int), 0644, NULL, + &proc_dointvec_minmax, &sysctl_intvec, NULL, &min_idle, &max_idle}, + {NET_ROSE_ROUTING_CONTROL, "routing_control", + &sysctl_rose_routing_control, sizeof(int), 0644, NULL, + &proc_dointvec_minmax, &sysctl_intvec, NULL, &min_route, &max_route}, + {0} +}; + +static ctl_table rose_dir_table[] = { + {NET_ROSE, "rose", NULL, 0, 0555, rose_table}, + {0} +}; + +static ctl_table rose_root_table[] = { + {CTL_NET, "net", NULL, 0, 0555, rose_dir_table}, + {0} +}; + +void rose_register_sysctl(void) +{ + rose_table_header = register_sysctl_table(rose_root_table, 1); +} + +void rose_unregister_sysctl(void) +{ + unregister_sysctl_table(rose_table_header); +} |