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

📄 af_netlink.c

📁 《嵌入式系统设计与实例开发实验教材二源码》Linux内核移植与编译实验
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * NETLINK      Kernel-user communication protocol. * * 		Authors:	Alan Cox <alan@redhat.com> * 				Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> * *		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. *  * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith *                               added netlink_proto_exit * */#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/major.h>#include <linux/signal.h>#include <linux/sched.h>#include <linux/errno.h>#include <linux/string.h>#include <linux/stat.h>#include <linux/socket.h>#include <linux/un.h>#include <linux/fcntl.h>#include <linux/termios.h>#include <linux/sockios.h>#include <linux/net.h>#include <linux/fs.h>#include <linux/slab.h>#include <asm/uaccess.h>#include <linux/skbuff.h>#include <linux/netdevice.h>#include <linux/rtnetlink.h>#include <linux/proc_fs.h>#include <linux/smp_lock.h>#include <net/sock.h>#include <net/scm.h>#define Nprintk(a...)#if defined(CONFIG_NETLINK_DEV) || defined(CONFIG_NETLINK_DEV_MODULE)#define NL_EMULATE_DEV#endifstruct netlink_opt{	u32			pid;	unsigned		groups;	u32			dst_pid;	unsigned		dst_groups;	unsigned long		state;	int			(*handler)(int unit, struct sk_buff *skb);	wait_queue_head_t	wait;	struct netlink_callback	*cb;	spinlock_t		cb_lock;	void			(*data_ready)(struct sock *sk, int bytes);};static struct sock *nl_table[MAX_LINKS];static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);#ifdef NL_EMULATE_DEVstatic struct socket *netlink_kernel[MAX_LINKS];#endifstatic int netlink_dump(struct sock *sk);static void netlink_destroy_callback(struct netlink_callback *cb);atomic_t netlink_sock_nr;static rwlock_t nl_table_lock = RW_LOCK_UNLOCKED;static atomic_t nl_table_users = ATOMIC_INIT(0);static void netlink_sock_destruct(struct sock *sk){	skb_queue_purge(&sk->receive_queue);	if (!sk->dead) {		printk("Freeing alive netlink socket %p\n", sk);		return;	}	BUG_TRAP(atomic_read(&sk->rmem_alloc)==0);	BUG_TRAP(atomic_read(&sk->wmem_alloc)==0);	BUG_TRAP(sk->protinfo.af_netlink->cb==NULL);	kfree(sk->protinfo.af_netlink);	atomic_dec(&netlink_sock_nr);#ifdef NETLINK_REFCNT_DEBUG	printk(KERN_DEBUG "NETLINK %p released, %d are still alive\n", sk, atomic_read(&netlink_sock_nr));#endif}/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP. * Look, when several writers sleep and reader wakes them up, all but one * immediately hit write lock and grab all the cpus. Exclusive sleep solves * this, _but_ remember, it adds useless work on UP machines. */static void netlink_table_grab(void){	write_lock_bh(&nl_table_lock);	if (atomic_read(&nl_table_users)) {		DECLARE_WAITQUEUE(wait, current);		add_wait_queue_exclusive(&nl_table_wait, &wait);		for(;;) {			set_current_state(TASK_UNINTERRUPTIBLE);			if (atomic_read(&nl_table_users) == 0)				break;			write_unlock_bh(&nl_table_lock);			schedule();			write_lock_bh(&nl_table_lock);		}		__set_current_state(TASK_RUNNING);		remove_wait_queue(&nl_table_wait, &wait);	}}static __inline__ void netlink_table_ungrab(void){	write_unlock_bh(&nl_table_lock);	wake_up(&nl_table_wait);}static __inline__ voidnetlink_lock_table(void){	/* read_lock() synchronizes us to netlink_table_grab */	read_lock(&nl_table_lock);	atomic_inc(&nl_table_users);	read_unlock(&nl_table_lock);}static __inline__ voidnetlink_unlock_table(void){	if (atomic_dec_and_test(&nl_table_users))		wake_up(&nl_table_wait);}static __inline__ struct sock *netlink_lookup(int protocol, u32 pid){	struct sock *sk;	read_lock(&nl_table_lock);	for (sk=nl_table[protocol]; sk; sk=sk->next) {		if (sk->protinfo.af_netlink->pid == pid) {			sock_hold(sk);			read_unlock(&nl_table_lock);			return sk;		}	}	read_unlock(&nl_table_lock);	return NULL;}extern struct proto_ops netlink_ops;static int netlink_insert(struct sock *sk, u32 pid){	int err = -EADDRINUSE;	struct sock *osk;	netlink_table_grab();	for (osk=nl_table[sk->protocol]; osk; osk=osk->next) {		if (osk->protinfo.af_netlink->pid == pid)			break;	}	if (osk == NULL) {		err = -EBUSY;		if (sk->protinfo.af_netlink->pid == 0) {			sk->protinfo.af_netlink->pid = pid;			sk->next = nl_table[sk->protocol];			nl_table[sk->protocol] = sk;			sock_hold(sk);			err = 0;		}	}	netlink_table_ungrab();	return err;}static void netlink_remove(struct sock *sk){	struct sock **skp;	netlink_table_grab();	for (skp = &nl_table[sk->protocol]; *skp; skp = &((*skp)->next)) {		if (*skp == sk) {			*skp = sk->next;			__sock_put(sk);			break;		}	}	netlink_table_ungrab();}static int netlink_create(struct socket *sock, int protocol){	struct sock *sk;	sock->state = SS_UNCONNECTED;	if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)		return -ESOCKTNOSUPPORT;	if (protocol<0 || protocol >= MAX_LINKS)		return -EPROTONOSUPPORT;	sock->ops = &netlink_ops;	sk = sk_alloc(PF_NETLINK, GFP_KERNEL, 1);	if (!sk)		return -ENOMEM;	sock_init_data(sock,sk);	sk->protinfo.af_netlink = kmalloc(sizeof(struct netlink_opt), GFP_KERNEL);	if (sk->protinfo.af_netlink == NULL) {		sk_free(sk);		return -ENOMEM;	}	memset(sk->protinfo.af_netlink, 0, sizeof(struct netlink_opt));	spin_lock_init(&sk->protinfo.af_netlink->cb_lock);	init_waitqueue_head(&sk->protinfo.af_netlink->wait);	sk->destruct = netlink_sock_destruct;	atomic_inc(&netlink_sock_nr);	sk->protocol=protocol;	return 0;}static int netlink_release(struct socket *sock){	struct sock *sk = sock->sk;	if (!sk)		return 0;	netlink_remove(sk);	spin_lock(&sk->protinfo.af_netlink->cb_lock);	if (sk->protinfo.af_netlink->cb) {		sk->protinfo.af_netlink->cb->done(sk->protinfo.af_netlink->cb);		netlink_destroy_callback(sk->protinfo.af_netlink->cb);		sk->protinfo.af_netlink->cb = NULL;		__sock_put(sk);	}	spin_unlock(&sk->protinfo.af_netlink->cb_lock);	/* OK. Socket is unlinked, and, therefore,	   no new packets will arrive */	sock_orphan(sk);	sock->sk = NULL;	wake_up_interruptible_all(&sk->protinfo.af_netlink->wait);	skb_queue_purge(&sk->write_queue);	sock_put(sk);	return 0;}static int netlink_autobind(struct socket *sock){	struct sock *sk = sock->sk;	struct sock *osk;	s32 pid = current->pid;	int err;retry:	netlink_table_grab();	for (osk=nl_table[sk->protocol]; osk; osk=osk->next) {		if (osk->protinfo.af_netlink->pid == pid) {			/* Bind collision, search negative pid values. */			if (pid > 0)				pid = -4096;			pid--;			netlink_table_ungrab();			goto retry;		}	}	netlink_table_ungrab();	err = netlink_insert(sk, pid);	if (err == -EADDRINUSE)		goto retry;	sk->protinfo.af_netlink->groups = 0;	return 0;}static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len){	struct sock *sk = sock->sk;	int err;	struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;		if (nladdr->nl_family != AF_NETLINK)		return -EINVAL;	/* Only superuser is allowed to listen multicasts */	if (nladdr->nl_groups && !capable(CAP_NET_ADMIN))		return -EPERM;	if (sk->protinfo.af_netlink->pid) {		if (nladdr->nl_pid != sk->protinfo.af_netlink->pid)			return -EINVAL;		sk->protinfo.af_netlink->groups = nladdr->nl_groups;		return 0;	}	if (nladdr->nl_pid == 0) {		err = netlink_autobind(sock);		if (err == 0)			sk->protinfo.af_netlink->groups = nladdr->nl_groups;		return err;	}	err = netlink_insert(sk, nladdr->nl_pid);	if (err == 0)		sk->protinfo.af_netlink->groups = nladdr->nl_groups;	return err;}static int netlink_connect(struct socket *sock, struct sockaddr *addr,			   int alen, int flags){	int err = 0;	struct sock *sk = sock->sk;	struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;	if (addr->sa_family == AF_UNSPEC) {		sk->protinfo.af_netlink->dst_pid = 0;		sk->protinfo.af_netlink->dst_groups = 0;		return 0;	}	if (addr->sa_family != AF_NETLINK)		return -EINVAL;	/* Only superuser is allowed to send multicasts */	if (nladdr->nl_groups && !capable(CAP_NET_ADMIN))		return -EPERM;	if (!sk->protinfo.af_netlink->pid)		err = netlink_autobind(sock);	if (err == 0) {		sk->protinfo.af_netlink->dst_pid = nladdr->nl_pid;		sk->protinfo.af_netlink->dst_groups = nladdr->nl_groups;	}	return 0;}static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer){	struct sock *sk = sock->sk;	struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;		nladdr->nl_family = AF_NETLINK;	*addr_len = sizeof(*nladdr);	if (peer) {		nladdr->nl_pid = sk->protinfo.af_netlink->dst_pid;		nladdr->nl_groups = sk->protinfo.af_netlink->dst_groups;	} else {		nladdr->nl_pid = sk->protinfo.af_netlink->pid;		nladdr->nl_groups = sk->protinfo.af_netlink->groups;	}	return 0;}static void netlink_overrun(struct sock *sk){	if (!test_and_set_bit(0, &sk->protinfo.af_netlink->state)) {		sk->err = ENOBUFS;		sk->error_report(sk);	}}int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock){	struct sock *sk;	int len = skb->len;	int protocol = ssk->protocol;	long timeo;        DECLARE_WAITQUEUE(wait, current);	timeo = sock_sndtimeo(ssk, nonblock);retry:	sk = netlink_lookup(protocol, pid);	if (sk == NULL)		goto no_dst;#ifdef NL_EMULATE_DEV	if (sk->protinfo.af_netlink->handler) {		skb_orphan(skb);		len = sk->protinfo.af_netlink->handler(protocol, skb);		sock_put(sk);		return len;	}#endif	if (atomic_read(&sk->rmem_alloc) > sk->rcvbuf ||	    test_bit(0, &sk->protinfo.af_netlink->state)) {		if (!timeo) {			if (ssk->protinfo.af_netlink->pid == 0)				netlink_overrun(sk);			sock_put(sk);			kfree_skb(skb);			return -EAGAIN;		}		__set_current_state(TASK_INTERRUPTIBLE);		add_wait_queue(&sk->protinfo.af_netlink->wait, &wait);		if ((atomic_read(&sk->rmem_alloc) > sk->rcvbuf ||		    test_bit(0, &sk->protinfo.af_netlink->state)) &&		    !sk->dead)			timeo = schedule_timeout(timeo);		__set_current_state(TASK_RUNNING);		remove_wait_queue(&sk->protinfo.af_netlink->wait, &wait);		sock_put(sk);		if (signal_pending(current)) {			kfree_skb(skb);			return sock_intr_errno(timeo);		}		goto retry;	}	skb_orphan(skb);	skb_set_owner_r(skb, sk);	skb_queue_tail(&sk->receive_queue, skb);	sk->data_ready(sk, len);	sock_put(sk);	return len;no_dst:	kfree_skb(skb);	return -ECONNREFUSED;}static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb){#ifdef NL_EMULATE_DEV	if (sk->protinfo.af_netlink->handler) {		skb_orphan(skb);		sk->protinfo.af_netlink->handler(sk->protocol, skb);		return 0;	} else#endif	if (atomic_read(&sk->rmem_alloc) <= sk->rcvbuf &&	    !test_bit(0, &sk->protinfo.af_netlink->state)) {                skb_orphan(skb);		skb_set_owner_r(skb, sk);		skb_queue_tail(&sk->receive_queue, skb);		sk->data_ready(sk, skb->len);		return 0;	}	return -1;}void netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,		       u32 group, int allocation){	struct sock *sk;	struct sk_buff *skb2 = NULL;	int protocol = ssk->protocol;	int failure = 0;	/* While we sleep in clone, do not allow to change socket list */	netlink_lock_table();	for (sk = nl_table[protocol]; sk; sk = sk->next) {		if (ssk == sk)			continue;		if (sk->protinfo.af_netlink->pid == pid ||		    !(sk->protinfo.af_netlink->groups&group))			continue;		if (failure) {			netlink_overrun(sk);

⌨️ 快捷键说明

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