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

📄 pppoe.c

📁 Linux Kernel 2.6.9 for OMAP1710
💻 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.7.0 * * 220102 :	Fix module use count on failure in pppoe_create, pppox_sk -acme * 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. * 050101 :	Fix PADT procesing. * 140501 :	Use pppoe_rcv_core to handle all backlog. (Alexey) * 170701 :	Do not lock_sock with rwlock held. (DaveM) *		Ignore discovery frames if user has socket *		locked. (DaveM) *		Ignore return value of dev_queue_xmit in __pppoe_xmit *		or else we may kfree an SKB twice. (DaveM) * 190701 :	When doing copies of skb's in __pppoe_xmit, always delete *		the original skb that was passed in on success, never on *		failure.  Delete the copy of the skb on failure to avoid *		a memory leak. * 081001 :	Misc. cleanup (licence string, non-blocking, prevent *		reference of device on close). * 121301 :	New ppp channels interface; cannot unregister a channel *		from interrupts.  Thus, we mark the socket as a ZOMBIE *		and do the unregistration later. * 081002 :	seq_file support for proc stuff -acme * 111602 :	Merge all 2.4 fixes into 2.5/2.6 tree.  Label 2.5/2.6 *		as version 0.7.  Spacing cleanup. * Author:	Michal Ostrowski <mostrows@speakeasy.net> * Contributors: * 		Arnaldo Carvalho de Melo <acme@conectiva.com.br> *		David S. Miller (davem@redhat.com) * * 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 <linux/kernel.h>#include <linux/slab.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 <linux/ppp_channel.h>#include <linux/ppp_defs.h>#include <linux/if_ppp.h>#include <linux/notifier.h>#include <linux/file.h>#include <linux/proc_fs.h>#include <linux/seq_file.h>#include <net/sock.h>#include <asm/uaccess.h>#define PPPOE_HASH_BITS 4#define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS)static struct ppp_channel_ops pppoe_chan_ops;static int pppoe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb);static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);static struct proto_ops pppoe_ops;static rwlock_t pppoe_hash_lock = RW_LOCK_UNLOCKED;static struct ppp_channel_ops pppoe_chan_ops;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 );}/* zeroed because its in .bss */static struct pppox_opt *item_hash_table[PPPOE_HASH_SIZE];/********************************************************************** * *  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 void pppoe_flush_dev(struct net_device *dev){	int hash;	BUG_ON(dev == NULL);	read_lock_bh(&pppoe_hash_lock);	for (hash = 0; hash < PPPOE_HASH_SIZE; hash++) {		struct pppox_opt *po = item_hash_table[hash];		while (po != NULL) {			if (po->pppoe_dev == dev) {				struct sock *sk = po->sk;				sock_hold(sk);				po->pppoe_dev = NULL;				/* We hold a reference to SK, now drop the				 * hash table lock so that we may attempt				 * to lock the socket (which can sleep).				 */				read_unlock_bh(&pppoe_hash_lock);				lock_sock(sk);				if (sk->sk_state &				    (PPPOX_CONNECTED | PPPOX_BOUND)) {					pppox_unbind_sock(sk);					dev_put(dev);					sk->sk_state = PPPOX_ZOMBIE;					sk->sk_state_change(sk);				}				release_sock(sk);				sock_put(sk);				read_lock_bh(&pppoe_hash_lock);				/* Now restart from the beginning of this				 * hash chain.  We always NULL out pppoe_dev				 * so we are guaranteed to make forward				 * progress.				 */				po = item_hash_table[hash];				continue;			}			po = po->next;		}	}	read_unlock_bh(&pppoe_hash_lock);}static int pppoe_device_event(struct notifier_block *this,			      unsigned long event, void *ptr){	struct net_device *dev = (struct net_device *) ptr;	/* 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. */		pppoe_flush_dev(dev);		break;	default:		break;	};	return NOTIFY_DONE;}static struct notifier_block pppoe_notifier = {	.notifier_call = pppoe_device_event,};/************************************************************************ * * Do the real work of receiving a PPPoE Session frame. * ***********************************************************************/static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb){	struct pppox_opt *po = pppox_sk(sk);	struct pppox_opt *relay_po = NULL;	if (sk->sk_state & PPPOX_BOUND) {		struct pppoe_hdr *ph = (struct pppoe_hdr *) skb->nh.raw;		int len = ntohs(ph->length);		skb_pull(skb, sizeof(struct pppoe_hdr));		skb_trim(skb, len);		ppp_input(&po->chan, skb);	} else if (sk->sk_state & PPPOX_RELAY) {		relay_po = get_item_by_addr(&po->pppoe_relay);		if (relay_po == NULL)			goto abort_kfree;		if ((relay_po->sk->sk_state & PPPOX_CONNECTED) == 0)			goto abort_put;		skb_pull(skb, sizeof(struct pppoe_hdr));		if (!__pppoe_xmit( relay_po->sk, skb))			goto abort_put;	} else {		if (sock_queue_rcv_skb(sk, skb))			goto abort_kfree;	}	return NET_RX_SUCCESS;abort_put:	sock_put(relay_po->sk);abort_kfree:	kfree_skb(skb);	return NET_RX_DROP;}/************************************************************************ * * 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 pppox_opt *po;	struct sock *sk;	int ret;	if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))		goto drop;	if (!(skb = skb_share_check(skb, GFP_ATOMIC))) 		goto out;	ph = (struct pppoe_hdr *) skb->nh.raw;	po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source);	if (!po) 		goto drop;	sk = po->sk;	bh_lock_sock(sk);	/* Socket state is unknown, must put skb into backlog. */	if (sock_owned_by_user(sk) != 0) {		sk_add_backlog(sk, skb);		ret = NET_RX_SUCCESS;	} else {		ret = pppoe_rcv_core(sk, skb);	}	bh_unlock_sock(sk);	sock_put(sk);	return ret;drop:	kfree_skb(skb);out:	return NET_RX_DROP;}/************************************************************************ * * 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 pppox_opt *po;	if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))		goto abort;	if (!(skb = skb_share_check(skb, GFP_ATOMIC))) 		goto out;	ph = (struct pppoe_hdr *) skb->nh.raw;	if (ph->code != PADT_CODE)		goto abort;	po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source);	if (po) {		struct sock *sk = po->sk;		bh_lock_sock(sk);		/* If the user has locked the socket, just ignore		 * the packet.  With the way two rcv protocols hook into		 * one socket family type, we cannot (easily) distinguish		 * what kind of SKB it is during backlog rcv.		 */		if (sock_owned_by_user(sk) == 0) {			/* We're no longer connect at the PPPOE layer,			 * and must wait for ppp channel to disconnect us.			 */			sk->sk_state = PPPOX_ZOMBIE;		}		bh_unlock_sock(sk);		sock_put(sk);	}abort:	kfree_skb(skb);out:	return NET_RX_SUCCESS; /* Lies... :-) */}static struct packet_type pppoes_ptype = {	.type	= __constant_htons(ETH_P_PPP_SES),	.func	= pppoe_rcv,};static struct packet_type pppoed_ptype = {	.type	= __constant_htons(ETH_P_PPP_DISC),	.func	= pppoe_disc_rcv,};/*********************************************************************** * * Really kill the socket. (Called from pppox_sk_free if refcnt == 0.) * **********************************************************************/static void pppoe_sk_free(struct sock *sk){	struct pppox_opt *po = pppox_sk(sk);	if (po)		kfree(po);}/*********************************************************************** * * Initialize a new struct sock. * **********************************************************************/static int pppoe_create(struct socket *sock){	int error = -ENOMEM;	struct sock *sk;	struct pppox_opt *po;	sk = sk_alloc(PF_PPPOX, GFP_KERNEL, 1, NULL);	if (!sk)		goto out;	sock_init_data(sock, sk);	sk_set_owner(sk, THIS_MODULE);	sock->state = SS_UNCONNECTED;	sock->ops   = &pppoe_ops;	sk->sk_backlog_rcv = pppoe_rcv_core;	sk->sk_state	   = PPPOX_NONE;	sk->sk_type	   = SOCK_STREAM;	sk->sk_family	   = PF_PPPOX;	sk->sk_protocol	   = PX_PROTO_OE;	sk->sk_destruct	   = pppoe_sk_free;	po = sk->sk_protinfo = kmalloc(sizeof(*po), GFP_KERNEL);	if (!po)		goto frees;	memset(po, 0, sizeof(*po));	po->sk = sk;	error = 0;out:	return error;frees:	sk_free(sk);	goto out;}static int pppoe_release(struct socket *sock){	struct sock *sk = sock->sk;	struct pppox_opt *po;	int error = 0;	if (!sk)		return 0;	if (sock_flag(sk, SOCK_DEAD))		return -EBADF;	pppox_unbind_sock(sk);	/* Signal the death of the socket. */	sk->sk_state = PPPOX_DEAD;	po = pppox_sk(sk);	if (po->pppoe_pa.sid) {		delete_item(po->pppoe_pa.sid, po->pppoe_pa.remote);	}	if (po->pppoe_dev)		dev_put(po->pppoe_dev);	po->pppoe_dev = NULL;	sock_orphan(sk);	sock->sk = NULL;	skb_queue_purge(&sk->sk_receive_queue);	sock_put(sk);	return error;}static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,		  int sockaddr_len, int flags){	struct sock *sk = sock->sk;	struct net_device *dev = NULL;	struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;	struct pppox_opt *po = pppox_sk(sk);	int error;	lock_sock(sk);	error = -EINVAL;

⌨️ 快捷键说明

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