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

📄 af_x25.c

📁 之前积累的一个X25在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 *	2002-10-06	Arnaldo C. Melo Get rid of cli/sti, move proc stuff to *					x25_proc.c, using seq_file *	2005-04-02	Shaun Pereira	Selective sub address matching *					with call user data *	2005-04-15	Shaun Pereira	Fast select with no restriction on *					response */#include <linux/module.h>#include <linux/capability.h>#include <linux/errno.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/timer.h>#include <linux/string.h>#include <linux/net.h>#include <linux/netdevice.h>#include <linux/if_arp.h>#include <linux/skbuff.h>#include <net/sock.h>#include <net/tcp_states.h>#include <asm/uaccess.h>#include <linux/fcntl.h>#include <linux/termios.h>	/* For TIOCINQ/OUTQ */#include <linux/notifier.h>#include <linux/init.h>#include <linux/compat.h>#include <net/x25.h>#include <net/compat.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);DEFINE_RWLOCK(x25_list_lock);static const struct proto_ops x25_proto_ops;static struct x25_address null_x25_address = {"               "};#ifdef CONFIG_COMPATstruct compat_x25_subscrip_struct {	char device[200-sizeof(compat_ulong_t)];	compat_ulong_t global_facil_mask;	compat_uint_t extended;};#endifint 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. Check the full list for an address/cud match. *	If no cuds match return the next_best thing, an address match. *	Note: if a listening socket has cud set it must only get calls *	with matching cud. */static struct sock *x25_find_listener(struct x25_address *addr,					struct sk_buff *skb){	struct sock *s;	struct sock *next_best;	struct hlist_node *node;	read_lock_bh(&x25_list_lock);	next_best = NULL;	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) {			/*			 * Found a listening socket, now check the incoming			 * call user data vs this sockets call user data			 */			if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {			 	if((memcmp(x25_sk(s)->calluserdata.cuddata,			 		skb->data,					x25_sk(s)->cudmatchlength)) == 0) {					sock_hold(s);					goto found;				 }			} else				next_best = s;		}	if (next_best) {		s = next_best;		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. */static 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. */static 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;		sk->sk_timer.data = (unsigned long)sk;		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 proto x25_proto = {	.name	  = "X25",	.owner	  = THIS_MODULE,	.obj_size = sizeof(struct x25_sock),};static struct sock *x25_alloc_socket(void){	struct x25_sock *x25;	struct sock *sk = sk_alloc(AF_X25, GFP_ATOMIC, &x25_proto, 1);	if (!sk)		goto out;	sock_init_data(NULL, sk);	x25 = x25_sk(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);out:	return sk;}void x25_init_timers(struct sock *sk);static int x25_create(struct socket *sock, int protocol){	struct sock *sk;	struct x25_sock *x25;	int rc = -ESOCKTNOSUPPORT;	if (sock->type != SOCK_SEQPACKET || protocol)		goto out;	rc = -ENOMEM;	if ((sk = x25_alloc_socket()) == NULL)		goto out;	x25 = x25_sk(sk);	sock_init_data(sock, sk);	x25_init_timers(sk);	sock->ops    = &x25_proto_ops;	sk->sk_protocol = protocol;	sk->sk_backlog_rcv = x25_backlog_rcv;	x25->t21   = sysctl_x25_call_request_timeout;	x25->t22   = sysctl_x25_reset_request_timeout;	x25->t23   = sysctl_x25_clear_request_timeout;	x25->t2    = sysctl_x25_ack_holdback_timeout;	x25->state = X25_STATE_0;	x25->cudmatchlength = 0;	x25->accptapprv = X25_DENY_ACCPT_APPRV;		/* normally no cud  */							/* on call accept   */	x25->facilities.winsize_in  = X25_DEFAULT_WINDOW_SIZE;	x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;	x25->facilities.pacsize_in  = X25_DEFAULT_PACKET_SIZE;	x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;	x25->facilities.throughput  = X25_DEFAULT_THROUGHPUT;	x25->facilities.reverse     = X25_DEFAULT_REVERSE; 	x25->dte_facilities.calling_len = 0; 	x25->dte_facilities.called_len = 0; 	memset(x25->dte_facilities.called_ae, '\0',		 	sizeof(x25->dte_facilities.called_ae)); 	memset(x25->dte_facilities.calling_ae, '\0',		 	sizeof(x25->dte_facilities.calling_ae));	rc = 0;out:	return rc;}static struct sock *x25_make_new(struct sock *osk){	struct sock *sk = NULL;	struct x25_sock *x25, *ox25;	if (osk->sk_type != SOCK_SEQPACKET)		goto out;	if ((sk = x25_alloc_socket()) == NULL)		goto out;	x25 = x25_sk(sk);

⌨️ 快捷键说明

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