📄 af_netrom.c
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) * Copyright Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk) * Copyright Darryl Miles G7LED (dlm@g7led.demon.co.uk) */#include <linux/config.h>#include <linux/module.h>#include <linux/moduleparam.h>#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/uaccess.h>#include <asm/system.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/netrom.h>#include <linux/proc_fs.h>#include <linux/seq_file.h>#include <net/ip.h>#include <net/tcp.h>#include <net/arp.h>#include <linux/init.h>int nr_ndevs = 4;int sysctl_netrom_default_path_quality = NR_DEFAULT_QUAL;int sysctl_netrom_obsolescence_count_initialiser = NR_DEFAULT_OBS;int sysctl_netrom_network_ttl_initialiser = NR_DEFAULT_TTL;int sysctl_netrom_transport_timeout = NR_DEFAULT_T1;int sysctl_netrom_transport_maximum_tries = NR_DEFAULT_N2;int sysctl_netrom_transport_acknowledge_delay = NR_DEFAULT_T2;int sysctl_netrom_transport_busy_delay = NR_DEFAULT_T4;int sysctl_netrom_transport_requested_window_size = NR_DEFAULT_WINDOW;int sysctl_netrom_transport_no_activity_timeout = NR_DEFAULT_IDLE;int sysctl_netrom_routing_control = NR_DEFAULT_ROUTING;int sysctl_netrom_link_fails_count = NR_DEFAULT_FAILS;static unsigned short circuit = 0x101;static HLIST_HEAD(nr_list);static spinlock_t nr_list_lock = SPIN_LOCK_UNLOCKED;static struct proto_ops nr_proto_ops;void nr_init_timers(struct sock *sk);static struct sock *nr_alloc_sock(void){ nr_cb *nr; struct sock *sk = sk_alloc(PF_NETROM, GFP_ATOMIC, 1, NULL); if (!sk) goto out; nr = sk->sk_protinfo = kmalloc(sizeof(*nr), GFP_ATOMIC); if (!nr) goto frees; memset(nr, 0x00, sizeof(*nr)); nr->sk = sk;out: return sk;frees: sk_free(sk); sk = NULL; goto out;}/* * Socket removal during an interrupt is now safe. */static void nr_remove_socket(struct sock *sk){ spin_lock_bh(&nr_list_lock); sk_del_node_init(sk); spin_unlock_bh(&nr_list_lock);}/* * Kill all bound sockets on a dropped device. */static void nr_kill_by_device(struct net_device *dev){ struct sock *s; struct hlist_node *node; spin_lock_bh(&nr_list_lock); sk_for_each(s, node, &nr_list) if (nr_sk(s)->device == dev) nr_disconnect(s, ENETUNREACH); spin_unlock_bh(&nr_list_lock);}/* * Handle device status changes. */static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr){ struct net_device *dev = (struct net_device *)ptr; if (event != NETDEV_DOWN) return NOTIFY_DONE; nr_kill_by_device(dev); nr_rt_device_down(dev); return NOTIFY_DONE;}/* * Add a socket to the bound sockets list. */static void nr_insert_socket(struct sock *sk){ spin_lock_bh(&nr_list_lock); sk_add_node(sk, &nr_list); spin_unlock_bh(&nr_list_lock);}/* * Find a socket that wants to accept the Connect Request we just * received. */static struct sock *nr_find_listener(ax25_address *addr){ struct sock *s; struct hlist_node *node; spin_lock_bh(&nr_list_lock); sk_for_each(s, node, &nr_list) if (!ax25cmp(&nr_sk(s)->source_addr, addr) && s->sk_state == TCP_LISTEN) { bh_lock_sock(s); goto found; } s = NULL;found: spin_unlock_bh(&nr_list_lock); return s;}/* * Find a connected NET/ROM socket given my circuit IDs. */static struct sock *nr_find_socket(unsigned char index, unsigned char id){ struct sock *s; struct hlist_node *node; spin_lock_bh(&nr_list_lock); sk_for_each(s, node, &nr_list) { nr_cb *nr = nr_sk(s); if (nr->my_index == index && nr->my_id == id) { bh_lock_sock(s); goto found; } } s = NULL;found: spin_unlock_bh(&nr_list_lock); return s;}/* * Find a connected NET/ROM socket given their circuit IDs. */static struct sock *nr_find_peer(unsigned char index, unsigned char id, ax25_address *dest){ struct sock *s; struct hlist_node *node; spin_lock_bh(&nr_list_lock); sk_for_each(s, node, &nr_list) { nr_cb *nr = nr_sk(s); if (nr->your_index == index && nr->your_id == id && !ax25cmp(&nr->dest_addr, dest)) { bh_lock_sock(s); goto found; } } s = NULL;found: spin_unlock_bh(&nr_list_lock); return s;}/* * Find next free circuit ID. */static unsigned short nr_find_next_circuit(void){ unsigned short id = circuit; unsigned char i, j; struct sock *sk; for (;;) { i = id / 256; j = id % 256; if (i != 0 && j != 0) { if ((sk=nr_find_socket(i, j)) == NULL) break; bh_unlock_sock(sk); } id++; } return id;}/* * Deferred destroy. */void nr_destroy_socket(struct sock *);/* * Handler for deferred kills. */static void nr_destroy_timer(unsigned long data){ struct sock *sk=(struct sock *)data; bh_lock_sock(sk); sock_hold(sk); nr_destroy_socket(sk); bh_unlock_sock(sk); sock_put(sk);}/* * 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 nr_destroy_socket(struct sock *sk){ struct sk_buff *skb; nr_remove_socket(sk); nr_stop_heartbeat(sk); nr_stop_t1timer(sk); nr_stop_t2timer(sk); nr_stop_t4timer(sk); nr_stop_idletimer(sk); nr_clear_queues(sk); /* Flush the queues */ while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { if (skb->sk != sk) { /* A pending connection */ /* Queue the unaccepted socket for death */ sock_set_flag(skb->sk, SOCK_DEAD); nr_start_heartbeat(skb->sk); nr_sk(skb->sk)->state = NR_STATE_0; } kfree_skb(skb); } if (atomic_read(&sk->sk_wmem_alloc) || atomic_read(&sk->sk_rmem_alloc)) { /* Defer: outstanding buffers */ sk->sk_timer.function = nr_destroy_timer; sk->sk_timer.expires = jiffies + 2 * HZ; add_timer(&sk->sk_timer); } else sock_put(sk);}/* * Handling for system calls applied via the various interfaces to a * NET/ROM socket object. */static int nr_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen){ struct sock *sk = sock->sk; nr_cb *nr = nr_sk(sk); int opt; if (level != SOL_NETROM) return -ENOPROTOOPT; if (optlen < sizeof(int)) return -EINVAL; if (get_user(opt, (int __user *)optval)) return -EFAULT; switch (optname) { case NETROM_T1: if (opt < 1) return -EINVAL; nr->t1 = opt * HZ; return 0; case NETROM_T2: if (opt < 1) return -EINVAL; nr->t2 = opt * HZ; return 0; case NETROM_N2: if (opt < 1 || opt > 31) return -EINVAL; nr->n2 = opt; return 0; case NETROM_T4: if (opt < 1) return -EINVAL; nr->t4 = opt * HZ; return 0; case NETROM_IDLE: if (opt < 0) return -EINVAL; nr->idle = opt * 60 * HZ; return 0; default: return -ENOPROTOOPT; }}static int nr_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen){ struct sock *sk = sock->sk; nr_cb *nr = nr_sk(sk); int val = 0; int len; if (level != SOL_NETROM) return -ENOPROTOOPT; if (get_user(len, optlen)) return -EFAULT; if (len < 0) return -EINVAL; switch (optname) { case NETROM_T1: val = nr->t1 / HZ; break; case NETROM_T2: val = nr->t2 / HZ; break; case NETROM_N2: val = nr->n2; break; case NETROM_T4: val = nr->t4 / HZ; break; case NETROM_IDLE: val = nr->idle / (60 * HZ); break; default: return -ENOPROTOOPT; } len = min_t(unsigned int, len, sizeof(int)); if (put_user(len, optlen)) return -EFAULT; return copy_to_user(optval, &val, len) ? -EFAULT : 0;}static int nr_listen(struct socket *sock, int backlog){ struct sock *sk = sock->sk; lock_sock(sk); if (sk->sk_state != TCP_LISTEN) { memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN); sk->sk_max_ack_backlog = backlog; sk->sk_state = TCP_LISTEN; release_sock(sk); return 0; } release_sock(sk); return -EOPNOTSUPP;}static int nr_create(struct socket *sock, int protocol){ struct sock *sk; nr_cb *nr; if (sock->type != SOCK_SEQPACKET || protocol != 0) return -ESOCKTNOSUPPORT; if ((sk = nr_alloc_sock()) == NULL) return -ENOMEM; nr = nr_sk(sk); sock_init_data(sock, sk); sk_set_owner(sk, THIS_MODULE); sock->ops = &nr_proto_ops; sk->sk_protocol = protocol; skb_queue_head_init(&nr->ack_queue); skb_queue_head_init(&nr->reseq_queue); skb_queue_head_init(&nr->frag_queue); nr_init_timers(sk); nr->t1 = sysctl_netrom_transport_timeout; nr->t2 = sysctl_netrom_transport_acknowledge_delay; nr->n2 = sysctl_netrom_transport_maximum_tries; nr->t4 = sysctl_netrom_transport_busy_delay; nr->idle = sysctl_netrom_transport_no_activity_timeout; nr->window = sysctl_netrom_transport_requested_window_size; nr->bpqext = 1; nr->state = NR_STATE_0; return 0;}static struct sock *nr_make_new(struct sock *osk){ struct sock *sk; nr_cb *nr, *onr; if (osk->sk_type != SOCK_SEQPACKET) return NULL; if ((sk = nr_alloc_sock()) == NULL) return NULL; nr = nr_sk(sk); sock_init_data(NULL, sk); sk_set_owner(sk, THIS_MODULE); sk->sk_type = osk->sk_type; sk->sk_socket = osk->sk_socket; sk->sk_priority = osk->sk_priority; sk->sk_protocol = osk->sk_protocol; sk->sk_rcvbuf = osk->sk_rcvbuf; sk->sk_sndbuf = osk->sk_sndbuf; sk->sk_debug = osk->sk_debug; sk->sk_state = TCP_ESTABLISHED; sk->sk_sleep = osk->sk_sleep; sk->sk_zapped = osk->sk_zapped; skb_queue_head_init(&nr->ack_queue); skb_queue_head_init(&nr->reseq_queue); skb_queue_head_init(&nr->frag_queue); nr_init_timers(sk); onr = nr_sk(osk); nr->t1 = onr->t1; nr->t2 = onr->t2; nr->n2 = onr->n2; nr->t4 = onr->t4; nr->idle = onr->idle; nr->window = onr->window;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -