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

📄 pppoe.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/** -*- linux-c -*- *********************************************************** * Linux PPP over Ethernet (PPPoX/PPPoE) Sockets * * PPPoX --- Generic PPP encapsulation socket family * PPPoE --- PPP over Ethernet (RFC 2516) * * * Version:    0.6.4 * * 030700 :     Fixed connect logic to allow for disconnect. * 270700 :	Fixed potential SMP problems; we must protect against  *		simultaneous invocation of ppp_input  *		and ppp_unregister_channel. * 040800 :	Respect reference count mechanisms on net-devices. * 200800 :     fix kfree(skb) in pppoe_rcv (acme) *		Module reference count is decremented in the right spot now, *		guards against sock_put not actually freeing the sk  *		in pppoe_release. * 051000 :	Initialization cleanup. * 111100 :	Fix recvmsg. * * Author:	Michal Ostrowski <mostrows@styx.uwaterloo.ca> * Contributors: * 		Arnaldo Carvalho de Melo <acme@conectiva.com.br> * * License: *		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. * */#include <linux/string.h>#include <linux/module.h>#include <asm/uaccess.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/malloc.h>#include <linux/errno.h>#include <linux/netdevice.h>#include <linux/net.h>#include <linux/inetdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <linux/init.h>#include <linux/if_ether.h>#include <linux/if_pppox.h>#include <net/sock.h>#include <linux/ppp_channel.h>#include <linux/ppp_defs.h>#include <linux/if_ppp.h>#include <linux/if_pppvar.h>#include <linux/notifier.h>#include <linux/file.h>#include <linux/proc_fs.h>static int __attribute__((unused)) pppoe_debug = 7;#define PPPOE_HASH_BITS 4#define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS)int pppoe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb);int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);struct proto_ops pppoe_ops;#if 0#define CHECKPTR(x,y) { if (!(x) && pppoe_debug &7 ){ printk(KERN_CRIT "PPPoE Invalid pointer : %s , %p\n",#x,(x)); error=-EINVAL; goto y; }}#define DEBUG(s,args...) if( pppoe_debug & (s) ) printk(KERN_CRIT args );#else#define CHECKPTR(x,y) do {} while (0);#define DEBUG(s,args...) do { } while (0);#endifstatic rwlock_t pppoe_hash_lock = RW_LOCK_UNLOCKED;static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b){	return (a->sid == b->sid &&		(memcmp(a->remote, b->remote, ETH_ALEN) == 0));}static inline int cmp_addr(struct pppoe_addr *a, unsigned long sid, char *addr){	return (a->sid == sid &&		(memcmp(a->remote,addr,ETH_ALEN) == 0));}static int hash_item(unsigned long sid, unsigned char *addr){	char hash=0;	int i,j;	for (i = 0; i < ETH_ALEN ; ++i){		for (j = 0; j < 8/PPPOE_HASH_BITS ; ++j){			hash ^= addr[i] >> ( j * PPPOE_HASH_BITS );		}	}	for (i = 0; i < (sizeof(unsigned long)*8) / PPPOE_HASH_BITS ; ++i)		hash ^= sid >> (i*PPPOE_HASH_BITS);	return hash & ( PPPOE_HASH_SIZE - 1 );}	static struct pppox_opt *item_hash_table[PPPOE_HASH_SIZE] = { 0, };/********************************************************************** * *  Set/get/delete/rehash items  (internal versions) * **********************************************************************/static struct pppox_opt *__get_item(unsigned long sid, unsigned char *addr){	int hash = hash_item(sid, addr);	struct pppox_opt *ret;	ret = item_hash_table[hash];	while (ret && !cmp_addr(&ret->pppoe_pa, sid, addr))		ret = ret->next;	return ret;}static int __set_item(struct pppox_opt *po){	int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);	struct pppox_opt *ret;	ret = item_hash_table[hash];	while (ret) {		if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa))			return -EALREADY;		ret = ret->next;	}	if (!ret) {		po->next = item_hash_table[hash];		item_hash_table[hash] = po;	}	return 0;}static struct pppox_opt *__delete_item(unsigned long sid, char *addr){	int hash = hash_item(sid, addr);	struct pppox_opt *ret, **src;	ret = item_hash_table[hash];	src = &item_hash_table[hash];	while (ret) {		if (cmp_addr(&ret->pppoe_pa, sid, addr)) {			*src = ret->next;			break;		}		src = &ret->next;		ret = ret->next;	}	return ret;}/********************************************************************** * *  Set/get/delete/rehash items * **********************************************************************/static inline struct pppox_opt *get_item(unsigned long sid,					 unsigned char *addr){	struct pppox_opt *po;	read_lock_bh(&pppoe_hash_lock);	po = __get_item(sid, addr);	if(po)		sock_hold(po->sk);	read_unlock_bh(&pppoe_hash_lock);	return po;}static inline struct pppox_opt *get_item_by_addr(struct sockaddr_pppox *sp){	return get_item(sp->sa_addr.pppoe.sid, sp->sa_addr.pppoe.remote);}static inline int set_item(struct pppox_opt *po){	int i;	if (!po)		return -EINVAL;	write_lock_bh(&pppoe_hash_lock);	i = __set_item(po);	write_unlock_bh(&pppoe_hash_lock);	return i;}static inline struct pppox_opt *delete_item(unsigned long sid, char *addr){	struct pppox_opt *ret;	write_lock_bh(&pppoe_hash_lock);	ret = __delete_item(sid, addr);	write_unlock_bh(&pppoe_hash_lock);	return ret;}/*************************************************************************** * *  Handler for device events. *  Certain device events require that sockets be unconnected. * **************************************************************************/static int pppoe_device_event(struct notifier_block *this,			      unsigned long event, void *ptr){	int error = NOTIFY_DONE;	struct net_device *dev = (struct net_device *) ptr;	struct pppox_opt *po = NULL;	int hash = 0;		/* Only look at sockets that are using this specific device. */	switch (event) {	case NETDEV_CHANGEMTU:	    /* A change in mtu is a bad thing, requiring	     * LCP re-negotiation.	     */	case NETDEV_GOING_DOWN:	case NETDEV_DOWN:		/* Find every socket on this device and kill it. */		read_lock_bh(&pppoe_hash_lock);		while (!po && hash < PPPOE_HASH_SIZE){			po = item_hash_table[hash];			++hash;		}			while (po && hash < PPPOE_HASH_SIZE){			if(po->pppoe_dev == dev){				lock_sock(po->sk);				if (po->sk->state & (PPPOX_CONNECTED|PPPOX_BOUND)){					pppox_unbind_sock(po->sk);									dev_put(po->pppoe_dev);					po->pppoe_dev = NULL;					po->sk->state = PPPOX_DEAD;					po->sk->state_change(po->sk);				} 				release_sock(po->sk);			}			if (po->next) {				po = po->next;			} else {				po = NULL;				while (!po && hash < PPPOE_HASH_SIZE){					po = item_hash_table[hash];					++hash;				}			}		}		read_unlock_bh(&pppoe_hash_lock);		break;	default:		break;	};	return error;}static struct notifier_block pppoe_notifier = {	notifier_call: pppoe_device_event,};/************************************************************************ * * Do the real work of receiving a PPPoE Session frame. * ***********************************************************************/int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb){	struct pppox_opt  *po=sk->protinfo.pppox;	struct pppox_opt *relay_po = NULL;	if (sk->state & PPPOX_BOUND) {		skb_pull(skb, sizeof(struct pppoe_hdr));				ppp_input(&po->chan, skb);	} else if( sk->state & PPPOX_RELAY ){		relay_po = get_item_by_addr( &po->pppoe_relay );		if( relay_po == NULL  ||		    !( relay_po->sk->state & PPPOX_CONNECTED ) ){			goto abort;		}				skb_pull(skb, sizeof(struct pppoe_hdr));		if( !__pppoe_xmit( relay_po->sk , skb) ){			goto abort;		}	} else {		sock_queue_rcv_skb(sk, skb);	}	return 1;abort:	if(relay_po)		sock_put(relay_po->sk);	return 0;}/************************************************************************ * * Receive wrapper called in BH context. * ***********************************************************************/static int pppoe_rcv(struct sk_buff *skb,		      struct net_device *dev,		      struct packet_type *pt){	struct pppoe_hdr *ph = (struct pppoe_hdr *) skb->nh.raw;	struct pppox_opt *po;	struct sock *sk ;	int ret;	po = get_item((unsigned long) ph->sid, skb->mac.ethernet->h_source);	if(!po){		kfree_skb(skb);		return 0;	}	sk = po->sk;        bh_lock_sock(sk);	/* Socket state is unknown, must put skb into backlog. */	if( sk->lock.users != 0 ){		sk_add_backlog( sk, skb);		ret = 1;	}else{		ret = pppoe_rcv_core(sk, skb);	}		bh_unlock_sock(sk);	sock_put(sk);	return ret;}/************************************************************************ * * Receive wrapper called in process context. * ***********************************************************************/int pppoe_backlog_rcv(struct sock *sk, struct sk_buff *skb){	lock_sock(sk);	pppoe_rcv_core(sk, skb);	release_sock(sk);	return 0;}/************************************************************************ * * Receive a PPPoE Discovery frame. * This is solely for detection of PADT frames * ***********************************************************************/static int pppoe_disc_rcv(struct sk_buff *skb,			  struct net_device *dev,			  struct packet_type *pt){	struct pppoe_hdr *ph = (struct pppoe_hdr *) skb->nh.raw;	struct pppox_opt *po;	struct sock *sk = NULL;	if (ph->code != PADT_CODE)		goto abort;	po = get_item((unsigned long) ph->sid, skb->mac.ethernet->h_source);	if (!po)		goto abort_put;	sk = po->sk;	pppox_unbind_sock(sk); abort_put:	sock_put(sk); abort:	kfree_skb(skb);	return 0;}struct packet_type pppoes_ptype = {	__constant_htons(ETH_P_PPP_SES),	NULL,	pppoe_rcv,	NULL,	NULL};struct packet_type pppoed_ptype = {	__constant_htons(ETH_P_PPP_DISC),	NULL,	pppoe_disc_rcv,	NULL,	NULL};/*********************************************************************** * * Really kill the socket. (Called from sock_put if refcnt == 0.) * **********************************************************************/void pppoe_sock_destruct(struct sock *sk){	if (sk->protinfo.destruct_hook)		kfree(sk->protinfo.destruct_hook);	MOD_DEC_USE_COUNT;}/*********************************************************************** * * Initialize a new struct sock. * **********************************************************************/static int pppoe_create(struct socket *sock){	int error = 0;	struct sock *sk;		MOD_INC_USE_COUNT;		sk = sk_alloc(PF_PPPOX, GFP_KERNEL, 1);	if (!sk)		return -ENOMEM;	sock_init_data(sock, sk);	sock->state = SS_UNCONNECTED;	sock->ops   = &pppoe_ops;	sk->protocol = PX_PROTO_OE;	sk->family = PF_PPPOX;	sk->backlog_rcv = pppoe_backlog_rcv;	sk->next = NULL;	sk->pprev = NULL;	sk->state = PPPOX_NONE;	sk->type = SOCK_STREAM;	sk->destruct = pppoe_sock_destruct;	sk->protinfo.pppox = kmalloc(sizeof(struct pppox_opt), GFP_KERNEL);	if (!sk->protinfo.pppox) {		error = -ENOMEM;		goto free_sk;	}	memset((void *) sk->protinfo.pppox, 0, sizeof(struct pppox_opt));	sk->protinfo.pppox->sk = sk;	/* Delete the protinfo when it is time to do so. */	sk->protinfo.destruct_hook = sk->protinfo.pppox;	sock->sk = sk;	return 0;free_sk:	sk_free(sk);	return error;}int pppoe_release(struct socket *sock){	struct sock *sk = sock->sk;	struct pppox_opt *po;	int error = 0;	if (!sk)		return 0;	if (sk->dead != 0)		return -EBADF;	pppox_unbind_sock(sk);

⌨️ 快捷键说明

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