📄 af_x25.c
字号:
/* * X.25 Packet Layer release 002 * * 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.15 or higher * * 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 * X.25 001 Jonathan Naylor Started coding. * X.25 002 Jonathan Naylor Centralised disconnect handling. * New timer architecture. * 2000-03-11 Henner Eisen MSG_EOR handling more POSIX compliant. * 2000-03-22 Daniela Squassoni Allowed disabling/enabling of * facilities negotiation and increased * the throughput upper limit. * 2000-08-27 Arnaldo C. Melo s/suser/capable/ + micro cleanups * 2000-09-04 Henner Eisen Set sock->state in x25_accept(). * Fixed x25_output() related skb leakage. * 2000-10-02 Henner Eisen Made x25_kick() single threaded per socket. * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation. * 2000-11-14 Henner Eisen Closing datalink from NETDEV_GOING_DOWN * 2002-10-06 Arnaldo C. Melo Get rid of cli/sti, move proc stuff to * x25_proc.c, using seq_file */#include <linux/config.h>#include <linux/module.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 <linux/inet.h>#include <linux/netdevice.h>#include <linux/if_arp.h>#include <linux/skbuff.h>#include <net/sock.h>#include <net/tcp.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/init.h>#include <net/x25.h>int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;HLIST_HEAD(x25_list);rwlock_t x25_list_lock = RW_LOCK_UNLOCKED;static struct proto_ops x25_proto_ops;static struct x25_address null_x25_address = {" "};int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr, struct x25_address *calling_addr){ int called_len, calling_len; char *called, *calling; int i; called_len = (*p >> 0) & 0x0F; calling_len = (*p >> 4) & 0x0F; called = called_addr->x25_addr; calling = calling_addr->x25_addr; p++; for (i = 0; i < (called_len + calling_len); i++) { if (i < called_len) { if (i % 2 != 0) { *called++ = ((*p >> 0) & 0x0F) + '0'; p++; } else { *called++ = ((*p >> 4) & 0x0F) + '0'; } } else { if (i % 2 != 0) { *calling++ = ((*p >> 0) & 0x0F) + '0'; p++; } else { *calling++ = ((*p >> 4) & 0x0F) + '0'; } } } *called = *calling = '\0'; return 1 + (called_len + calling_len + 1) / 2;}int x25_addr_aton(unsigned char *p, struct x25_address *called_addr, struct x25_address *calling_addr){ unsigned int called_len, calling_len; char *called, *calling; int i; called = called_addr->x25_addr; calling = calling_addr->x25_addr; called_len = strlen(called); calling_len = strlen(calling); *p++ = (calling_len << 4) | (called_len << 0); for (i = 0; i < (called_len + calling_len); i++) { if (i < called_len) { if (i % 2 != 0) { *p |= (*called++ - '0') << 0; p++; } else { *p = 0x00; *p |= (*called++ - '0') << 4; } } else { if (i % 2 != 0) { *p |= (*calling++ - '0') << 0; p++; } else { *p = 0x00; *p |= (*calling++ - '0') << 4; } } } return 1 + (called_len + calling_len + 1) / 2;}/* * Socket removal during an interrupt is now safe. */static void x25_remove_socket(struct sock *sk){ write_lock_bh(&x25_list_lock); sk_del_node_init(sk); write_unlock_bh(&x25_list_lock);}/* * Kill all bound sockets on a dropped device. */static void x25_kill_by_device(struct net_device *dev){ struct sock *s; struct hlist_node *node; write_lock_bh(&x25_list_lock); sk_for_each(s, node, &x25_list) if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev) x25_disconnect(s, ENETUNREACH, 0, 0); write_unlock_bh(&x25_list_lock);}/* * Handle device status changes. */static int x25_device_event(struct notifier_block *this, unsigned long event, void *ptr){ struct net_device *dev = ptr; struct x25_neigh *nb; if (dev->type == ARPHRD_X25#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE) || dev->type == ARPHRD_ETHER#endif ) { switch (event) { case NETDEV_UP: x25_link_device_up(dev); break; case NETDEV_GOING_DOWN: nb = x25_get_neigh(dev); if (nb) { x25_terminate_link(nb); x25_neigh_put(nb); } break; case NETDEV_DOWN: x25_kill_by_device(dev); x25_route_device_down(dev); x25_link_device_down(dev); break; } } return NOTIFY_DONE;}/* * Add a socket to the bound sockets list. */static void x25_insert_socket(struct sock *sk){ write_lock_bh(&x25_list_lock); sk_add_node(sk, &x25_list); write_unlock_bh(&x25_list_lock);}/* * Find a socket that wants to accept the Call Request we just * received. */static struct sock *x25_find_listener(struct x25_address *addr){ struct sock *s; struct hlist_node *node; read_lock_bh(&x25_list_lock); sk_for_each(s, node, &x25_list) if ((!strcmp(addr->x25_addr, x25_sk(s)->source_addr.x25_addr) || !strcmp(addr->x25_addr, null_x25_address.x25_addr)) && s->sk_state == TCP_LISTEN) { sock_hold(s); goto found; } s = NULL;found: read_unlock_bh(&x25_list_lock); return s;}/* * Find a connected X.25 socket given my LCI and neighbour. */struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb){ struct sock *s; struct hlist_node *node; sk_for_each(s, node, &x25_list) if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) { sock_hold(s); goto found; } s = NULL;found: return s;}struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb){ struct sock *s; read_lock_bh(&x25_list_lock); s = __x25_find_socket(lci, nb); read_unlock_bh(&x25_list_lock); return s;}/* * Find a unique LCI for a given device. */unsigned int x25_new_lci(struct x25_neigh *nb){ unsigned int lci = 1; struct sock *sk; read_lock_bh(&x25_list_lock); while ((sk = __x25_find_socket(lci, nb)) != NULL) { sock_put(sk); if (++lci == 4096) { lci = 0; break; } } read_unlock_bh(&x25_list_lock); return lci;}/* * Deferred destroy. */void x25_destroy_socket(struct sock *);/* * handler for deferred kills. */static void x25_destroy_timer(unsigned long data){ x25_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. * Not static as it's used by the timer */void x25_destroy_socket(struct sock *sk){ struct sk_buff *skb; sock_hold(sk); lock_sock(sk); x25_stop_heartbeat(sk); x25_stop_timer(sk); x25_remove_socket(sk); x25_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); x25_start_heartbeat(skb->sk); x25_sk(skb->sk)->state = X25_STATE_0; } kfree_skb(skb); } if (atomic_read(&sk->sk_wmem_alloc) || atomic_read(&sk->sk_rmem_alloc)) { /* Defer: outstanding buffers */ sk->sk_timer.expires = jiffies + 10 * HZ; sk->sk_timer.function = x25_destroy_timer; add_timer(&sk->sk_timer); } else { /* drop last reference so sock_put will free */ __sock_put(sk); } release_sock(sk); sock_put(sk);}/* * Handling for system calls applied via the various interfaces to a * X.25 socket object. */static int x25_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen){ int opt; struct sock *sk = sock->sk; int rc = -ENOPROTOOPT; if (level != SOL_X25 || optname != X25_QBITINCL) goto out; rc = -EINVAL; if (optlen < sizeof(int)) goto out; rc = -EFAULT; if (get_user(opt, (int __user *)optval)) goto out; x25_sk(sk)->qbitincl = !!opt; rc = 0;out: return rc;}static int x25_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen){ struct sock *sk = sock->sk; int val, len, rc = -ENOPROTOOPT; if (level != SOL_X25 || optname != X25_QBITINCL) goto out; rc = -EFAULT; if (get_user(len, optlen)) goto out; len = min_t(unsigned int, len, sizeof(int)); rc = -EINVAL; if (len < 0) goto out; rc = -EFAULT; if (put_user(len, optlen)) goto out; val = x25_sk(sk)->qbitincl; rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;out: return rc;}static int x25_listen(struct socket *sock, int backlog){ struct sock *sk = sock->sk; int rc = -EOPNOTSUPP; if (sk->sk_state != TCP_LISTEN) { memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN); sk->sk_max_ack_backlog = backlog; sk->sk_state = TCP_LISTEN; rc = 0; } return rc;}static struct sock *x25_alloc_socket(void){ struct x25_opt *x25; struct sock *sk = sk_alloc(AF_X25, GFP_ATOMIC, 1, NULL); if (!sk) goto out; x25 = sk->sk_protinfo = kmalloc(sizeof(*x25), GFP_ATOMIC); if (!x25) goto frees; memset(x25, 0, sizeof(*x25)); x25->sk = sk; sock_init_data(NULL, sk); sk_set_owner(sk, THIS_MODULE); skb_queue_head_init(&x25->ack_queue); skb_queue_head_init(&x25->fragment_queue); skb_queue_head_init(&x25->interrupt_in_queue); skb_queue_head_init(&x25->interrupt_out_queue);out: return sk;frees: sk_free(sk); sk = NULL; goto out;}void x25_init_timers(struct sock *sk);static int x25_create(struct socket *sock, int protocol){ struct sock *sk; struct x25_opt *x25; int rc = -ESOCKTNOSUPPORT;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -