cregit-Linux how code gets into the kernel

Release 4.11 net/decnet/dn_dev.c

Directory: net/decnet
/*
 * DECnet       An implementation of the DECnet protocol suite for the LINUX
 *              operating system.  DECnet is implemented using the  BSD Socket
 *              interface as the means of communication with the user level.
 *
 *              DECnet Device Layer
 *
 * Authors:     Steve Whitehouse <SteveW@ACM.org>
 *              Eduardo Marcelo Serrat <emserrat@geocities.com>
 *
 * Changes:
 *          Steve Whitehouse : Devices now see incoming frames so they
 *                             can mark on who it came from.
 *          Steve Whitehouse : Fixed bug in creating neighbours. Each neighbour
 *                             can now have a device specific setup func.
 *          Steve Whitehouse : Added /proc/sys/net/decnet/conf/<dev>/
 *          Steve Whitehouse : Fixed bug which sometimes killed timer
 *          Steve Whitehouse : Multiple ifaddr support
 *          Steve Whitehouse : SIOCGIFCONF is now a compile time option
 *          Steve Whitehouse : /proc/sys/net/decnet/conf/<sys>/forwarding
 *          Steve Whitehouse : Removed timer1 - it's a user space issue now
 *         Patrick Caulfield : Fixed router hello message format
 *          Steve Whitehouse : Got rid of constant sizes for blksize for
 *                             devices. All mtu based now.
 */

#include <linux/capability.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/if_addr.h>
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/skbuff.h>
#include <linux/sysctl.h>
#include <linux/notifier.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/uaccess.h>
#include <net/net_namespace.h>
#include <net/neighbour.h>
#include <net/dst.h>
#include <net/flow.h>
#include <net/fib_rules.h>
#include <net/netlink.h>
#include <net/dn.h>
#include <net/dn_dev.h>
#include <net/dn_route.h>
#include <net/dn_neigh.h>
#include <net/dn_fib.h>


#define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))


static char dn_rt_all_end_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x04,0x00,0x00};

static char dn_rt_all_rt_mcast[ETH_ALEN]  = {0xAB,0x00,0x00,0x03,0x00,0x00};

static char dn_hiord[ETH_ALEN]            = {0xAA,0x00,0x04,0x00,0x00,0x00};

static unsigned char dn_eco_version[3]    = {0x02,0x00,0x00};

extern struct neigh_table dn_neigh_table;

/*
 * decnet_address is kept in network order.
 */

__le16 decnet_address = 0;

static DEFINE_SPINLOCK(dndev_lock);

static struct net_device *decnet_default_device;
static BLOCKING_NOTIFIER_HEAD(dnaddr_chain);

static struct dn_dev *dn_dev_create(struct net_device *dev, int *err);
static void dn_dev_delete(struct net_device *dev);
static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa);

static int dn_eth_up(struct net_device *);
static void dn_eth_down(struct net_device *);
static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa);
static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa);


static struct dn_dev_parms dn_dev_list[] =  {
{
	.type =		ARPHRD_ETHER, /* Ethernet */
	.mode =		DN_DEV_BCAST,
	.state =	DN_DEV_S_RU,
	.t2 =		1,
	.t3 =		10,
	.name =		"ethernet",
	.up =		dn_eth_up,
	.down = 	dn_eth_down,
	.timer3 =	dn_send_brd_hello,
},
{
	.type =		ARPHRD_IPGRE, /* DECnet tunneled over GRE in IP */
	.mode =		DN_DEV_BCAST,
	.state =	DN_DEV_S_RU,
	.t2 =		1,
	.t3 =		10,
	.name =		"ipgre",
	.timer3 =	dn_send_brd_hello,
},
#if 0
{
        .type =         ARPHRD_X25, /* Bog standard X.25 */
	.mode =         DN_DEV_UCAST,
        .state =        DN_DEV_S_DS,
        .t2 =           1,
        .t3 =           120,
        .name =         "x25",
        .timer3 =       dn_send_ptp_hello,
},
#endif
#if 0
{
        .type =         ARPHRD_PPP, /* DECnet over PPP */
	.mode =         DN_DEV_BCAST,
        .state =        DN_DEV_S_RU,
        .t2 =           1,
        .t3 =           10,
        .name =         "ppp",
        .timer3 =       dn_send_brd_hello,
},
#endif
{
	.type =		ARPHRD_DDCMP, /* DECnet over DDCMP */
	.mode =		DN_DEV_UCAST,
	.state =	DN_DEV_S_DS,
	.t2 =		1,
	.t3 =		120,
	.name =		"ddcmp",
	.timer3 =	dn_send_ptp_hello,
},
{
	.type =		ARPHRD_LOOPBACK, /* Loopback interface - always last */
	.mode =		DN_DEV_BCAST,
	.state =	DN_DEV_S_RU,
	.t2 =		1,
	.t3 =		10,
	.name =		"loopback",
	.timer3 =	dn_send_brd_hello,
}
};


#define DN_DEV_LIST_SIZE ARRAY_SIZE(dn_dev_list)


#define DN_DEV_PARMS_OFFSET(x) offsetof(struct dn_dev_parms, x)

#ifdef CONFIG_SYSCTL


static int min_t2[] = { 1 };

static int max_t2[] = { 60 }; 
/* No max specified, but this seems sensible */

static int min_t3[] = { 1 };

static int max_t3[] = { 8191 }; 
/* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */


static int min_priority[1];

static int max_priority[] = { 127 }; 
/* From DECnet spec */

static int dn_forwarding_proc(struct ctl_table *, int,
			void __user *, size_t *, loff_t *);

static struct dn_dev_sysctl_table {
	
struct ctl_table_header *sysctl_header;
	
struct ctl_table dn_dev_vars[5];
} 
dn_dev_sysctl = {
	NULL,
	{
	{
		.procname = "forwarding",
		.data = (void *)DN_DEV_PARMS_OFFSET(forwarding),
		.maxlen = sizeof(int),
		.mode = 0644,
		.proc_handler = dn_forwarding_proc,
        },
	{
		.procname = "priority",
		.data = (void *)DN_DEV_PARMS_OFFSET(priority),
		.maxlen = sizeof(int),
		.mode = 0644,
		.proc_handler = proc_dointvec_minmax,
		.extra1 = &min_priority,
		.extra2 = &max_priority
	},
	{
		.procname = "t2",
		.data = (void *)DN_DEV_PARMS_OFFSET(t2),
		.maxlen = sizeof(int),
		.mode = 0644,
		.proc_handler = proc_dointvec_minmax,
		.extra1 = &min_t2,
		.extra2 = &max_t2
	},
	{
		.procname = "t3",
		.data = (void *)DN_DEV_PARMS_OFFSET(t3),
		.maxlen = sizeof(int),
		.mode = 0644,
		.proc_handler = proc_dointvec_minmax,
		.extra1 = &min_t3,
		.extra2 = &max_t3
	},
	{ }
	},
};


static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms) { struct dn_dev_sysctl_table *t; int i; char path[sizeof("net/decnet/conf/") + IFNAMSIZ]; t = kmemdup(&dn_dev_sysctl, sizeof(*t), GFP_KERNEL); if (t == NULL) return; for(i = 0; i < ARRAY_SIZE(t->dn_dev_vars) - 1; i++) { long offset = (long)t->dn_dev_vars[i].data; t->dn_dev_vars[i].data = ((char *)parms) + offset; } snprintf(path, sizeof(path), "net/decnet/conf/%s", dev? dev->name : parms->name); t->dn_dev_vars[0].extra1 = (void *)dev; t->sysctl_header = register_net_sysctl(&init_net, path, t->dn_dev_vars); if (t->sysctl_header == NULL) kfree(t); else parms->sysctl = t; }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)12968.98%337.50%
Eric W. Biedermann2915.51%225.00%
Pavel Emelyanov2412.83%112.50%
Arnaldo Carvalho de Melo42.14%112.50%
Andries E. Brouwer10.53%112.50%
Total187100.00%8100.00%


static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms) { if (parms->sysctl) { struct dn_dev_sysctl_table *t = parms->sysctl; parms->sysctl = NULL; unregister_net_sysctl_table(t->sysctl_header); kfree(t); } }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)4597.83%150.00%
Eric W. Biedermann12.17%150.00%
Total46100.00%2100.00%


static int dn_forwarding_proc(struct ctl_table *table, int write, void __user *buffer, size_t *lenp, loff_t *ppos) { #ifdef CONFIG_DECNET_ROUTER struct net_device *dev = table->extra1; struct dn_dev *dn_db; int err; int tmp, old; if (table->extra1 == NULL) return -EINVAL; dn_db = rcu_dereference_raw(dev->dn_ptr); old = dn_db->parms.forwarding; err = proc_dointvec(table, write, buffer, lenp, ppos); if ((err >= 0) && write) { if (dn_db->parms.forwarding < 0) dn_db->parms.forwarding = 0; if (dn_db->parms.forwarding > 2) dn_db->parms.forwarding = 2; /* * What an ugly hack this is... its works, just. It * would be nice if sysctl/proc were just that little * bit more flexible so I don't have to write a special * routine, or suffer hacks like this - SJW */ tmp = dn_db->parms.forwarding; dn_db->parms.forwarding = old; if (dn_db->parms.down) dn_db->parms.down(dev); dn_db->parms.forwarding = tmp; if (dn_db->parms.up) dn_db->parms.up(dev); } return err; #else return -EINVAL; #endif }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)20394.86%233.33%
Linus Torvalds62.80%116.67%
Eric Dumazet31.40%116.67%
Al Viro10.47%116.67%
Joe Perches10.47%116.67%
Total214100.00%6100.00%

#else /* CONFIG_SYSCTL */
static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms) { }

Contributors

PersonTokensPropCommitsCommitProp
Hideaki Yoshifuji / 吉藤英明10100.00%1100.00%
Total10100.00%1100.00%


static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms) { }

Contributors

PersonTokensPropCommitsCommitProp
Hideaki Yoshifuji / 吉藤英明15100.00%1100.00%
Total15100.00%1100.00%

#endif /* CONFIG_SYSCTL */
static inline __u16 mtu2blksize(struct net_device *dev) { u32 blksize = dev->mtu; if (blksize > 0xffff) blksize = 0xffff; if (dev->type == ARPHRD_ETHER || dev->type == ARPHRD_PPP || dev->type == ARPHRD_IPGRE || dev->type == ARPHRD_LOOPBACK) blksize -= 2; return (__u16)blksize; }

Contributors

PersonTokensPropCommitsCommitProp
Hideaki Yoshifuji / 吉藤英明6396.92%150.00%
Linus Torvalds (pre-git)23.08%150.00%
Total65100.00%2100.00%


static struct dn_ifaddr *dn_dev_alloc_ifa(void) { struct dn_ifaddr *ifa; ifa = kzalloc(sizeof(*ifa), GFP_KERNEL); return ifa; }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)3096.77%150.00%
Panagiotis Issaris13.23%150.00%
Total31100.00%2100.00%


static void dn_dev_free_ifa(struct dn_ifaddr *ifa) { kfree_rcu(ifa, rcu); }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)1583.33%133.33%
Lai Jiangshan211.11%133.33%
Eric Dumazet15.56%133.33%
Total18100.00%3100.00%


static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr __rcu **ifap, int destroy) { struct dn_ifaddr *ifa1 = rtnl_dereference(*ifap); unsigned char mac_addr[6]; struct net_device *dev = dn_db->dev; ASSERT_RTNL(); *ifap = ifa1->ifa_next; if (dn_db->dev->type == ARPHRD_ETHER) { if (ifa1->ifa_local != dn_eth2dn(dev->dev_addr)) { dn_dn2eth(mac_addr, ifa1->ifa_local); dev_mc_del(dev, mac_addr); } } dn_ifaddr_notify(RTM_DELADDR, ifa1); blocking_notifier_call_chain(&dnaddr_chain, NETDEV_DOWN, ifa1); if (destroy) { dn_dev_free_ifa(ifa1); if (dn_db->ifa_list == NULL) dn_dev_delete(dn_db->dev); } }

Contributors

PersonTokensPropCommitsCommitProp
Steven Whitehouse8357.64%228.57%
Linus Torvalds (pre-git)5437.50%114.29%
Eric Dumazet42.78%114.29%
Jiri Pirko10.69%114.29%
Alan Stern10.69%114.29%
Thomas Graf10.69%114.29%
Total144100.00%7100.00%


static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa) { struct net_device *dev = dn_db->dev; struct dn_ifaddr *ifa1; unsigned char mac_addr[6]; ASSERT_RTNL(); /* Check for duplicates */ for (ifa1 = rtnl_dereference(dn_db->ifa_list); ifa1 != NULL; ifa1 = rtnl_dereference(ifa1->ifa_next)) { if (ifa1->ifa_local == ifa->ifa_local) return -EEXIST; } if (dev->type == ARPHRD_ETHER) { if (ifa->ifa_local != dn_eth2dn(dev->dev_addr)) { dn_dn2eth(mac_addr, ifa->ifa_local); dev_mc_add(dev, mac_addr); } } ifa->ifa_next = dn_db->ifa_list; rcu_assign_pointer(dn_db->ifa_list, ifa); dn_ifaddr_notify(RTM_NEWADDR, ifa); blocking_notifier_call_chain(&dnaddr_chain, NETDEV_UP, ifa); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Steven Whitehouse10767.30%228.57%
Linus Torvalds (pre-git)3823.90%114.29%
Eric Dumazet127.55%228.57%
Thomas Graf10.63%114.29%
Alan Stern10.63%114.29%
Total159100.00%7100.00%


static int dn_dev_set_ifa(struct net_device *dev, struct dn_ifaddr *ifa) { struct dn_dev *dn_db = rtnl_dereference(dev->dn_ptr); int rv; if (dn_db == NULL) { int err; dn_db = dn_dev_create(dev, &err); if (dn_db == NULL) return err; } ifa->ifa_dev = dn_db; if (dev->flags & IFF_LOOPBACK) ifa->ifa_scope = RT_SCOPE_HOST; rv = dn_dev_insert_ifa(dn_db, ifa); if (rv) dn_dev_free_ifa(ifa); return rv; }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)8280.39%250.00%
Steven Whitehouse1716.67%125.00%
Eric Dumazet32.94%125.00%
Total102100.00%4100.00%


int dn_dev_ioctl(unsigned int cmd, void __user *arg) { char buffer[DN_IFREQ_SIZE]; struct ifreq *ifr = (struct ifreq *)buffer; struct sockaddr_dn *sdn = (struct sockaddr_dn *)&ifr->ifr_addr; struct dn_dev *dn_db; struct net_device *dev; struct dn_ifaddr *ifa = NULL; struct dn_ifaddr __rcu **ifap = NULL; int ret = 0; if (copy_from_user(ifr, arg, DN_IFREQ_SIZE)) return -EFAULT; ifr->ifr_name[IFNAMSIZ-1] = 0; dev_load(&init_net, ifr->ifr_name); switch (cmd) { case SIOCGIFADDR: break; case SIOCSIFADDR: if (!capable(CAP_NET_ADMIN)) return -EACCES; if (sdn->sdn_family != AF_DECnet) return -EINVAL; break; default: return -EINVAL; } rtnl_lock(); if ((dev = __dev_get_by_name(&init_net, ifr->ifr_name)) == NULL) { ret = -ENODEV; goto done; } if ((dn_db = rtnl_dereference(dev->dn_ptr)) != NULL) { for (ifap = &dn_db->ifa_list; (ifa = rtnl_dereference(*ifap)) != NULL; ifap = &ifa->ifa_next) if (strcmp(ifr->ifr_name, ifa->ifa_label) == 0) break; } if (ifa == NULL && cmd != SIOCSIFADDR) { ret = -EADDRNOTAVAIL; goto done; } switch (cmd) { case SIOCGIFADDR: *((__le16 *)sdn->sdn_nodeaddr) = ifa->ifa_local; goto rarok; case SIOCSIFADDR: if (!ifa) { if ((ifa = dn_dev_alloc_ifa()) == NULL) { ret = -ENOBUFS; break; } memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); } else { if (ifa->ifa_local == dn_saddr2dn(sdn)) break; dn_dev_del_ifa(dn_db, ifap, 0); } ifa->ifa_local = ifa->ifa_address = dn_saddr2dn(sdn); ret = dn_dev_set_ifa(dev, ifa); } done: rtnl_unlock(); return ret; rarok: if (copy_to_user(arg, ifr, DN_IFREQ_SIZE)) ret = -EFAULT; goto done; }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)38292.49%436.36%
Steven Whitehouse133.15%327.27%
Eric Dumazet102.42%19.09%
Eric W. Biedermann61.45%19.09%
Al Viro10.24%19.09%
David S. Miller10.24%19.09%
Total413100.00%11100.00%


struct net_device *dn_dev_get_default(void) { struct net_device *dev; spin_lock(&dndev_lock); dev = decnet_default_device; if (dev) { if (dev->dn_ptr) dev_hold(dev); else dev = NULL; } spin_unlock(&dndev_lock); return dev; }

Contributors

PersonTokensPropCommitsCommitProp
Hideaki Yoshifuji / 吉藤英明5396.36%150.00%
Stephen Hemminger23.64%150.00%
Total55100.00%2100.00%


int dn_dev_set_default(struct net_device *dev, int force) { struct net_device *old = NULL; int rv = -EBUSY; if (!dev->dn_ptr) return -ENODEV; spin_lock(&dndev_lock); if (force || decnet_default_device == NULL) { old = decnet_default_device; decnet_default_device = dev; rv = 0; } spin_unlock(&dndev_lock); if (old) dev_put(old); return rv; }

Contributors

PersonTokensPropCommitsCommitProp
Hideaki Yoshifuji / 吉藤英明8096.39%133.33%
Stephen Hemminger22.41%133.33%
Patrick Caulfield11.20%133.33%
Total83100.00%3100.00%


static void dn_dev_check_default(struct net_device *dev) { spin_lock(&dndev_lock); if (dev == decnet_default_device) { decnet_default_device = NULL; } else { dev = NULL; } spin_unlock(&dndev_lock); if (dev) dev_put(dev); }

Contributors

PersonTokensPropCommitsCommitProp
Hideaki Yoshifuji / 吉藤英明4996.08%150.00%
Stephen Hemminger23.92%150.00%
Total51100.00%2100.00%

/* * Called with RTNL */
static struct dn_dev *dn_dev_by_index(int ifindex) { struct net_device *dev; struct dn_dev *dn_dev = NULL; dev = __dev_get_by_index(&init_net, ifindex); if (dev) dn_dev = rtnl_dereference(dev->dn_ptr); return dn_dev; }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)4285.71%240.00%
Eric Dumazet48.16%240.00%
Eric W. Biedermann36.12%120.00%
Total49100.00%5100.00%

static const struct nla_policy dn_ifa_policy[IFA_MAX+1] = { [IFA_ADDRESS] = { .type = NLA_U16 }, [IFA_LOCAL] = { .type = NLA_U16 }, [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, [IFA_FLAGS] = { .type = NLA_U32 }, };
static int dn_nl_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh) { struct net *net = sock_net(skb->sk); struct nlattr *tb[IFA_MAX+1]; struct dn_dev *dn_db; struct ifaddrmsg *ifm; struct dn_ifaddr *ifa; struct dn_ifaddr __rcu **ifap; int err = -EINVAL; if (!netlink_capable(skb, CAP_NET_ADMIN)) return -EPERM; if (!net_eq(net, &init_net)) goto errout; err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy); if (err < 0) goto errout; err = -ENODEV; ifm = nlmsg_data(nlh); if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL) goto errout; err = -EADDRNOTAVAIL; for (ifap = &dn_db->ifa_list; (ifa = rtnl_dereference(*ifap)) != NULL; ifap = &ifa->ifa_next) { if (tb[IFA_LOCAL] && nla_memcmp(tb[IFA_LOCAL], &ifa->ifa_local, 2)) continue; if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label)) continue; dn_dev_del_ifa(dn_db, ifap, 1); return 0; } errout: return err; }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)11847.77%111.11%
Thomas Graf6727.13%111.11%
Denis V. Lunev218.50%111.11%
Eric W. Biedermann145.67%222.22%
Pavel Emelyanov104.05%111.11%
Eric Dumazet93.64%111.11%
Octavian Purdila52.02%111.11%
Hideaki Yoshifuji / 吉藤英明31.21%111.11%
Total247100.00%9100.00%


static int dn_nl_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh) { struct net *net = sock_net(skb->sk); struct nlattr *tb[IFA_MAX