⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 af_x25.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* *	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 */#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 <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/proc_fs.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;static struct sock *volatile x25_list /* = NULL initially */;static struct proto_ops x25_proto_ops;static x25_address null_x25_address = {"               "};int x25_addr_ntoa(unsigned char *p, x25_address *called_addr, 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  = '\0';	*calling = '\0';	return 1 + (called_len + calling_len + 1) / 2;}int x25_addr_aton(unsigned char *p, x25_address *called_addr, 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){	struct sock *s;	unsigned long flags;	save_flags(flags);	cli();	if ((s = x25_list) == sk) {		x25_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 x25_kill_by_device(struct net_device *dev){	struct sock *s;	for (s = x25_list; s != NULL; s = s->next)		if (s->protinfo.x25->neighbour &&		    s->protinfo.x25->neighbour->dev == dev)			x25_disconnect(s, ENETUNREACH, 0, 0);}/* *	Handle device status changes. */static int x25_device_event(struct notifier_block *this, unsigned long event, void *ptr){	struct net_device *dev = (struct net_device *)ptr;	struct x25_neigh *neigh;	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:				if ((neigh = x25_get_neigh(dev)))					x25_terminate_link(neigh);				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){	unsigned long flags;	save_flags(flags);	cli();	sk->next = x25_list;	x25_list = sk;	restore_flags(flags);}/* *	Find a socket that wants to accept the Call Request we just *	received. */static struct sock *x25_find_listener(x25_address *addr){	unsigned long flags;	struct sock *s;	save_flags(flags);	cli();	for (s = x25_list; s != NULL; s = s->next) {		if ((strcmp(addr->x25_addr, s->protinfo.x25->source_addr.x25_addr) == 0 ||		     strcmp(addr->x25_addr, null_x25_address.x25_addr) == 0) &&		     s->state == TCP_LISTEN) {			restore_flags(flags);			return s;		}	}	restore_flags(flags);	return NULL;}/* *	Find a connected X.25 socket given my LCI and neighbour. */struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *neigh){	struct sock *s;	unsigned long flags;	save_flags(flags);	cli();	for (s = x25_list; s != NULL; s = s->next) {		if (s->protinfo.x25->lci == lci && s->protinfo.x25->neighbour == neigh) {			restore_flags(flags);			return s;		}	}	restore_flags(flags);	return NULL;}/* *	Find a unique LCI for a given device. */unsigned int x25_new_lci(struct x25_neigh *neigh){	unsigned int lci = 1;	while (x25_find_socket(lci, neigh) != NULL) {		lci++;		if (lci == 4096) return 0;	}	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. */void x25_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();	x25_stop_heartbeat(sk);	x25_stop_timer(sk);	x25_remove_socket(sk);	x25_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 */			x25_start_heartbeat(skb->sk);			skb->sk->protinfo.x25->state = X25_STATE_0;		}		kfree_skb(skb);	}	if (atomic_read(&sk->wmem_alloc) != 0 || atomic_read(&sk->rmem_alloc) != 0) {		/* Defer: outstanding buffers */		init_timer(&sk->timer);		sk->timer.expires  = jiffies + 10 * HZ;		sk->timer.function = x25_destroy_timer;		sk->timer.data     = (unsigned long)sk;		add_timer(&sk->timer);	} else {		sk_free(sk);		MOD_DEC_USE_COUNT;	}	restore_flags(flags);}/* *	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 *optval, int optlen){	struct sock *sk = sock->sk;	int opt;	if (level != SOL_X25)		return -ENOPROTOOPT;	if (optlen < sizeof(int))		return-EINVAL;	if (get_user(opt, (int *)optval))		return -EFAULT;	switch (optname) {		case X25_QBITINCL:			sk->protinfo.x25->qbitincl = opt ? 1 : 0;			return 0;		default:			return -ENOPROTOOPT;	}}static int x25_getsockopt(struct socket *sock, int level, int optname,	char *optval, int *optlen){	struct sock *sk = sock->sk;	int val = 0;	int len; 		if (level != SOL_X25)		return -ENOPROTOOPT;	if (get_user(len, optlen))		return -EFAULT;	switch (optname) {		case X25_QBITINCL:			val = sk->protinfo.x25->qbitincl;			break;		default:			return -ENOPROTOOPT;	}	len = min(len, sizeof(int));	if (put_user(len, optlen))		return -EFAULT;	return copy_to_user(optval, &val, len) ? -EFAULT : 0;}static int x25_listen(struct socket *sock, int backlog){	struct sock *sk = sock->sk;	if (sk->state != TCP_LISTEN) {		memset(&sk->protinfo.x25->dest_addr, '\0', X25_ADDR_LEN);		sk->max_ack_backlog = backlog;		sk->state           = TCP_LISTEN;		return 0;	}	return -EOPNOTSUPP;}static struct sock *x25_alloc_socket(void){	struct sock *sk;	x25_cb *x25;	if ((sk = sk_alloc(AF_X25, GFP_ATOMIC, 1)) == NULL)		return NULL;	if ((x25 = kmalloc(sizeof(*x25), GFP_ATOMIC)) == NULL) {		sk_free(sk);		return NULL;	}	memset(x25, 0x00, sizeof(*x25));	x25->sk          = sk;	sk->protinfo.x25 = x25;	MOD_INC_USE_COUNT;	sock_init_data(NULL, sk);	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);	return sk;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -