4322 lines · 104.1 KB · C
RawDownload
/* * INET		An implementation of the TCP/IP protocol suite for the LINUX *		operating system.  INET is implemented using the  BSD Socket *		interface as the means of communication with the user level. * *		Routing netlink socket interface: protocol independent part. * * Authors:	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. * *	Fixes: *	Vitaly E. Lavrov		RTA_OK arithmetics was wrong. */ #include <linux/bitops.h>#include <linux/errno.h>#include <linux/module.h>#include <linux/types.h>#include <linux/socket.h>#include <linux/kernel.h>#include <linux/timer.h>#include <linux/string.h>#include <linux/sockios.h>#include <linux/net.h>#include <linux/fcntl.h>#include <linux/mm.h>#include <linux/slab.h>#include <linux/interrupt.h>#include <linux/capability.h>#include <linux/skbuff.h>#include <linux/init.h>#include <linux/security.h>#include <linux/mutex.h>#include <linux/if_addr.h>#include <linux/if_bridge.h>#include <linux/if_vlan.h>#include <linux/pci.h>#include <linux/etherdevice.h>#include <linux/bpf.h> #include <linux/uaccess.h> #include <linux/inet.h>#include <linux/netdevice.h>#include <net/switchdev.h>#include <net/ip.h>#include <net/protocol.h>#include <net/arp.h>#include <net/route.h>#include <net/udp.h>#include <net/tcp.h>#include <net/sock.h>#include <net/pkt_sched.h>#include <net/fib_rules.h>#include <net/rtnetlink.h>#include <net/net_namespace.h> struct rtnl_link {	rtnl_doit_func		doit;	rtnl_dumpit_func	dumpit;	rtnl_calcit_func 	calcit;}; static DEFINE_MUTEX(rtnl_mutex); void rtnl_lock(void){	mutex_lock(&rtnl_mutex);}EXPORT_SYMBOL(rtnl_lock); static struct sk_buff *defer_kfree_skb_list;void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail){	if (head && tail) {		tail->next = defer_kfree_skb_list;		defer_kfree_skb_list = head;	}}EXPORT_SYMBOL(rtnl_kfree_skbs); void __rtnl_unlock(void){	struct sk_buff *head = defer_kfree_skb_list; 	defer_kfree_skb_list = NULL; 	mutex_unlock(&rtnl_mutex); 	while (head) {		struct sk_buff *next = head->next; 		kfree_skb(head);		cond_resched();		head = next;	}} void rtnl_unlock(void){	/* This fellow will unlock it for us. */	netdev_run_todo();}EXPORT_SYMBOL(rtnl_unlock); int rtnl_trylock(void){	return mutex_trylock(&rtnl_mutex);}EXPORT_SYMBOL(rtnl_trylock); int rtnl_is_locked(void){	return mutex_is_locked(&rtnl_mutex);}EXPORT_SYMBOL(rtnl_is_locked); #ifdef CONFIG_PROVE_LOCKINGbool lockdep_rtnl_is_held(void){	return lockdep_is_held(&rtnl_mutex);}EXPORT_SYMBOL(lockdep_rtnl_is_held);#endif /* #ifdef CONFIG_PROVE_LOCKING */ static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; static inline int rtm_msgindex(int msgtype){	int msgindex = msgtype - RTM_BASE; 	/*	 * msgindex < 0 implies someone tried to register a netlink	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that	 * the message type has not been added to linux/rtnetlink.h	 */	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 	return msgindex;} static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex){	struct rtnl_link *tab; 	if (protocol <= RTNL_FAMILY_MAX)		tab = rtnl_msg_handlers[protocol];	else		tab = NULL; 	if (tab == NULL || tab[msgindex].doit == NULL)		tab = rtnl_msg_handlers[PF_UNSPEC]; 	return tab[msgindex].doit;} static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex){	struct rtnl_link *tab; 	if (protocol <= RTNL_FAMILY_MAX)		tab = rtnl_msg_handlers[protocol];	else		tab = NULL; 	if (tab == NULL || tab[msgindex].dumpit == NULL)		tab = rtnl_msg_handlers[PF_UNSPEC]; 	return tab[msgindex].dumpit;} static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex){	struct rtnl_link *tab; 	if (protocol <= RTNL_FAMILY_MAX)		tab = rtnl_msg_handlers[protocol];	else		tab = NULL; 	if (tab == NULL || tab[msgindex].calcit == NULL)		tab = rtnl_msg_handlers[PF_UNSPEC]; 	return tab[msgindex].calcit;} /** * __rtnl_register - Register a rtnetlink message type * @protocol: Protocol family or PF_UNSPEC * @msgtype: rtnetlink message type * @doit: Function pointer called for each request message * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message * @calcit: Function pointer to calc size of dump message * * Registers the specified function pointers (at least one of them has * to be non-NULL) to be called whenever a request message for the * specified protocol family and message type is received. * * The special protocol family PF_UNSPEC may be used to define fallback * function pointers for the case when no entry for the specific protocol * family exists. * * Returns 0 on success or a negative error code. */int __rtnl_register(int protocol, int msgtype,		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,		    rtnl_calcit_func calcit){	struct rtnl_link *tab;	int msgindex; 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);	msgindex = rtm_msgindex(msgtype); 	tab = rtnl_msg_handlers[protocol];	if (tab == NULL) {		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);		if (tab == NULL)			return -ENOBUFS; 		rtnl_msg_handlers[protocol] = tab;	} 	if (doit)		tab[msgindex].doit = doit; 	if (dumpit)		tab[msgindex].dumpit = dumpit; 	if (calcit)		tab[msgindex].calcit = calcit; 	return 0;}EXPORT_SYMBOL_GPL(__rtnl_register); /** * rtnl_register - Register a rtnetlink message type * * Identical to __rtnl_register() but panics on failure. This is useful * as failure of this function is very unlikely, it can only happen due * to lack of memory when allocating the chain to store all message * handlers for a protocol. Meant for use in init functions where lack * of memory implies no sense in continuing. */void rtnl_register(int protocol, int msgtype,		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,		   rtnl_calcit_func calcit){	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)		panic("Unable to register rtnetlink message handler, "		      "protocol = %d, message type = %d\n",		      protocol, msgtype);}EXPORT_SYMBOL_GPL(rtnl_register); /** * rtnl_unregister - Unregister a rtnetlink message type * @protocol: Protocol family or PF_UNSPEC * @msgtype: rtnetlink message type * * Returns 0 on success or a negative error code. */int rtnl_unregister(int protocol, int msgtype){	int msgindex; 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);	msgindex = rtm_msgindex(msgtype); 	if (rtnl_msg_handlers[protocol] == NULL)		return -ENOENT; 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;	rtnl_msg_handlers[protocol][msgindex].calcit = NULL; 	return 0;}EXPORT_SYMBOL_GPL(rtnl_unregister); /** * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol * @protocol : Protocol family or PF_UNSPEC * * Identical to calling rtnl_unregster() for all registered message types * of a certain protocol family. */void rtnl_unregister_all(int protocol){	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 	kfree(rtnl_msg_handlers[protocol]);	rtnl_msg_handlers[protocol] = NULL;}EXPORT_SYMBOL_GPL(rtnl_unregister_all); static LIST_HEAD(link_ops); static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind){	const struct rtnl_link_ops *ops; 	list_for_each_entry(ops, &link_ops, list) {		if (!strcmp(ops->kind, kind))			return ops;	}	return NULL;} /** * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. * @ops: struct rtnl_link_ops * to register * * The caller must hold the rtnl_mutex. This function should be used * by drivers that create devices during module initialization. It * must be called before registering the devices. * * Returns 0 on success or a negative error code. */int __rtnl_link_register(struct rtnl_link_ops *ops){	if (rtnl_link_ops_get(ops->kind))		return -EEXIST; 	/* The check for setup is here because if ops	 * does not have that filled up, it is not possible	 * to use the ops for creating device. So do not	 * fill up dellink as well. That disables rtnl_dellink.	 */	if (ops->setup && !ops->dellink)		ops->dellink = unregister_netdevice_queue; 	list_add_tail(&ops->list, &link_ops);	return 0;}EXPORT_SYMBOL_GPL(__rtnl_link_register); /** * rtnl_link_register - Register rtnl_link_ops with rtnetlink. * @ops: struct rtnl_link_ops * to register * * Returns 0 on success or a negative error code. */int rtnl_link_register(struct rtnl_link_ops *ops){	int err; 	rtnl_lock();	err = __rtnl_link_register(ops);	rtnl_unlock();	return err;}EXPORT_SYMBOL_GPL(rtnl_link_register); static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops){	struct net_device *dev;	LIST_HEAD(list_kill); 	for_each_netdev(net, dev) {		if (dev->rtnl_link_ops == ops)			ops->dellink(dev, &list_kill);	}	unregister_netdevice_many(&list_kill);} /** * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. * @ops: struct rtnl_link_ops * to unregister * * The caller must hold the rtnl_mutex. */void __rtnl_link_unregister(struct rtnl_link_ops *ops){	struct net *net; 	for_each_net(net) {		__rtnl_kill_links(net, ops);	}	list_del(&ops->list);}EXPORT_SYMBOL_GPL(__rtnl_link_unregister); /* Return with the rtnl_lock held when there are no network * devices unregistering in any network namespace. */static void rtnl_lock_unregistering_all(void){	struct net *net;	bool unregistering;	DEFINE_WAIT_FUNC(wait, woken_wake_function); 	add_wait_queue(&netdev_unregistering_wq, &wait);	for (;;) {		unregistering = false;		rtnl_lock();		for_each_net(net) {			if (net->dev_unreg_count > 0) {				unregistering = true;				break;			}		}		if (!unregistering)			break;		__rtnl_unlock(); 		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);	}	remove_wait_queue(&netdev_unregistering_wq, &wait);} /** * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. * @ops: struct rtnl_link_ops * to unregister */void rtnl_link_unregister(struct rtnl_link_ops *ops){	/* Close the race with cleanup_net() */	mutex_lock(&net_mutex);	rtnl_lock_unregistering_all();	__rtnl_link_unregister(ops);	rtnl_unlock();	mutex_unlock(&net_mutex);}EXPORT_SYMBOL_GPL(rtnl_link_unregister); static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev){	struct net_device *master_dev;	const struct rtnl_link_ops *ops; 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);	if (!master_dev)		return 0;	ops = master_dev->rtnl_link_ops;	if (!ops || !ops->get_slave_size)		return 0;	/* IFLA_INFO_SLAVE_DATA + nested data */	return nla_total_size(sizeof(struct nlattr)) +	       ops->get_slave_size(master_dev, dev);} static size_t rtnl_link_get_size(const struct net_device *dev){	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;	size_t size; 	if (!ops)		return 0; 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */ 	if (ops->get_size)		/* IFLA_INFO_DATA + nested data */		size += nla_total_size(sizeof(struct nlattr)) +			ops->get_size(dev); 	if (ops->get_xstats_size)		/* IFLA_INFO_XSTATS */		size += nla_total_size(ops->get_xstats_size(dev)); 	size += rtnl_link_get_slave_info_data_size(dev); 	return size;} static LIST_HEAD(rtnl_af_ops); static const struct rtnl_af_ops *rtnl_af_lookup(const int family){	const struct rtnl_af_ops *ops; 	list_for_each_entry(ops, &rtnl_af_ops, list) {		if (ops->family == family)			return ops;	} 	return NULL;} /** * rtnl_af_register - Register rtnl_af_ops with rtnetlink. * @ops: struct rtnl_af_ops * to register * * Returns 0 on success or a negative error code. */void rtnl_af_register(struct rtnl_af_ops *ops){	rtnl_lock();	list_add_tail(&ops->list, &rtnl_af_ops);	rtnl_unlock();}EXPORT_SYMBOL_GPL(rtnl_af_register); 

Lines 1–500 of 4322.