Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * James Leu (jleu@mindspring.net).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2001 by various other people who didn't put their name here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/ethtool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/inetdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <irq_kern.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <irq_user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "mconsole_kern.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <net_kern.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <net_user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define DRIVER_NAME "uml-netdev"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static DEFINE_SPINLOCK(opened_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static LIST_HEAD(opened);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * The drop_skb is used when we can't allocate an skb.  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * packet is read into drop_skb in order to get the data off the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * connection to the host.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * It is reallocated whenever a maximum packet size is seen which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * larger than any seen before.  update_drop_skb is called from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * eth_configure when a new interface is added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static DEFINE_SPINLOCK(drop_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static struct sk_buff *drop_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int drop_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static int update_drop_skb(int max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct sk_buff *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	spin_lock_irqsave(&drop_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (max <= drop_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	new = dev_alloc_skb(max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (new == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	skb_put(new, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	kfree_skb(drop_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	drop_skb = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	drop_max = max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	spin_unlock_irqrestore(&drop_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static int uml_net_rx(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct uml_net_private *lp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	int pkt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	/* If we can't allocate memory, try again next round. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	skb = dev_alloc_skb(lp->max_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (skb == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		drop_skb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		/* Read a packet into drop_skb and don't do anything with it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		(*lp->read)(lp->fd, drop_skb, lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	skb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	skb_put(skb, lp->max_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	pkt_len = (*lp->read)(lp->fd, skb, lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (pkt_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		skb_trim(skb, pkt_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		skb->protocol = (*lp->protocol)(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		dev->stats.rx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		dev->stats.rx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		return pkt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	return pkt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static void uml_dev_close(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct uml_net_private *lp =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		container_of(work, struct uml_net_private, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	dev_close(lp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static irqreturn_t uml_net_interrupt(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct net_device *dev = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	struct uml_net_private *lp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (!netif_running(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	spin_lock(&lp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	while ((err = uml_net_rx(dev)) > 0) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		       "Device '%s' read returned %d, shutting it down\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		       dev->name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		/* dev_close can't be called in interrupt context, and takes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		 * again lp->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		 * And dev_close() can be safely called multiple times on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		 * same device, since it tests for (dev->flags & IFF_UP). So
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		 * there's no harm in delaying the device shutdown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		 * Furthermore, the workqueue will not re-enqueue an already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		 * enqueued work item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		schedule_work(&lp->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	spin_unlock(&lp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int uml_net_open(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	struct uml_net_private *lp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (lp->fd >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		err = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	lp->fd = (*lp->open)(&lp->user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (lp->fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		err = lp->fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			     IRQF_SHARED, dev->name, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (err != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		err = -ENETUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	netif_start_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	/* clear buffer - it can happen that the host side of the interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	 * is full when we get here.  In this case, new data is never queued,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	 * SIGIOs never arrive, and the net never works.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	while ((err = uml_net_rx(dev)) > 0) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	spin_lock(&opened_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	list_add(&lp->list, &opened);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	spin_unlock(&opened_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) out_close:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	lp->fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int uml_net_close(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	struct uml_net_private *lp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	netif_stop_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	um_free_irq(dev->irq, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (lp->close != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		(*lp->close)(lp->fd, &lp->user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	lp->fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	spin_lock(&opened_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	list_del(&lp->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	spin_unlock(&opened_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	struct uml_net_private *lp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	netif_stop_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	spin_lock_irqsave(&lp->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	len = (*lp->write)(lp->fd, skb, lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	skb_tx_timestamp(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (len == skb->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		dev->stats.tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		dev->stats.tx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		netif_trans_update(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		netif_start_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		/* this is normally done in the interrupt when tx finishes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		netif_wake_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	else if (len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		netif_start_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		netif_start_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	spin_unlock_irqrestore(&lp->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	dev_consume_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static void uml_net_set_multicast_list(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static void uml_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	netif_trans_update(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	netif_wake_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) #ifdef CONFIG_NET_POLL_CONTROLLER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static void uml_net_poll_controller(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	disable_irq(dev->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	uml_net_interrupt(dev->irq, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	enable_irq(dev->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static void uml_net_get_drvinfo(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 				struct ethtool_drvinfo *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static const struct ethtool_ops uml_net_ethtool_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	.get_drvinfo	= uml_net_get_drvinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	.get_link	= ethtool_op_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	.get_ts_info	= ethtool_op_get_ts_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) void uml_net_setup_etheraddr(struct net_device *dev, char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	unsigned char *addr = dev->dev_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	char *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (str == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		goto random;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	for (i = 0; i < 6; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		addr[i] = simple_strtoul(str, &end, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		if ((end == str) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		   ((*end != ':') && (*end != ',') && (*end != '\0'))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			       "setup_etheraddr: failed to parse '%s' "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			       "as an ethernet address\n", str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			goto random;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		str = end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (is_multicast_ether_addr(addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		       "Attempt to assign a multicast ethernet address to a "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		       "device disallowed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		goto random;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	if (!is_valid_ether_addr(addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		       "Attempt to assign an invalid ethernet address to a "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		       "device disallowed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		goto random;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if (!is_local_ether_addr(addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		       "Warning: Assigning a globally valid ethernet "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		       "address to a device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		printk(KERN_WARNING "You should set the 2nd rightmost bit in "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		       "the first byte of the MAC,\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		printk(KERN_WARNING "i.e. %02x:%02x:%02x:%02x:%02x:%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		       addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		       addr[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) random:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	printk(KERN_INFO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	       "Choosing a random ethernet address for device %s\n", dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	eth_hw_addr_random(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static DEFINE_SPINLOCK(devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static LIST_HEAD(devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static struct platform_driver uml_net_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		.name  = DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static void net_device_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct uml_net *device = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	struct net_device *netdev = device->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	struct uml_net_private *lp = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (lp->remove != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		(*lp->remove)(&lp->user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	list_del(&device->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	kfree(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	free_netdev(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static const struct net_device_ops uml_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	.ndo_open 		= uml_net_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	.ndo_stop 		= uml_net_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	.ndo_start_xmit 	= uml_net_start_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	.ndo_set_rx_mode	= uml_net_set_multicast_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	.ndo_tx_timeout 	= uml_net_tx_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	.ndo_set_mac_address	= eth_mac_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	.ndo_validate_addr	= eth_validate_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) #ifdef CONFIG_NET_POLL_CONTROLLER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	.ndo_poll_controller = uml_net_poll_controller,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)  * Ensures that platform_driver_register is called only once by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)  * eth_configure.  Will be set in an initcall.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int driver_registered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static void eth_configure(int n, void *init, char *mac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			  struct transport *transport, gfp_t gfp_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	struct uml_net *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	struct uml_net_private *lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	int err, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	size = transport->private_size + sizeof(struct uml_net_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	device = kzalloc(sizeof(*device), gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	if (device == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		printk(KERN_ERR "eth_configure failed to allocate struct "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		       "uml_net\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	dev = alloc_etherdev(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (dev == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		printk(KERN_ERR "eth_configure: failed to allocate struct "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		       "net_device for eth%d\n", n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		goto out_free_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	INIT_LIST_HEAD(&device->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	device->index = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	/* If this name ends up conflicting with an existing registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	 * netdevice, that is OK, register_netdev{,ice}() will notice this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	 * and fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	snprintf(dev->name, sizeof(dev->name), "eth%d", n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	uml_net_setup_etheraddr(dev, mac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	printk(KERN_INFO "Netdevice %d (%pM) : ", n, dev->dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	lp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	/* This points to the transport private data. It's still clear, but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	 * must memset it to 0 *now*. Let's help the drivers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	memset(lp, 0, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	INIT_WORK(&lp->work, uml_dev_close);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	/* sysfs register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (!driver_registered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		platform_driver_register(&uml_net_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		driver_registered = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	device->pdev.id = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	device->pdev.name = DRIVER_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	device->pdev.dev.release = net_device_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	dev_set_drvdata(&device->pdev.dev, device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	if (platform_device_register(&device->pdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		goto out_free_netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	SET_NETDEV_DEV(dev,&device->pdev.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	device->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	 * These just fill in a data structure, so there's no failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	 * to be worried about.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	(*transport->kern->init)(dev, init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	*lp = ((struct uml_net_private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		{ .list  		= LIST_HEAD_INIT(lp->list),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		  .dev 			= dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		  .fd 			= -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		  .mac 			= { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		  .max_packet		= transport->user->max_packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		  .protocol 		= transport->kern->protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		  .open 		= transport->user->open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		  .close 		= transport->user->close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		  .remove 		= transport->user->remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		  .read 		= transport->kern->read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		  .write 		= transport->kern->write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		  .add_address 		= transport->user->add_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		  .delete_address  	= transport->user->delete_address });
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	spin_lock_init(&lp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	memcpy(lp->mac, dev->dev_addr, sizeof(lp->mac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	if ((transport->user->init != NULL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	    ((*transport->user->init)(&lp->user, dev) != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		goto out_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	dev->mtu = transport->user->mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	dev->netdev_ops = &uml_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	dev->ethtool_ops = &uml_net_ethtool_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	dev->watchdog_timeo = (HZ >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	dev->irq = UM_ETH_IRQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	err = update_drop_skb(lp->max_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		goto out_undo_user_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	err = register_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		goto out_undo_user_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	spin_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	list_add(&device->list, &devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	spin_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) out_undo_user_init:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	if (transport->user->remove != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		(*transport->user->remove)(&lp->user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) out_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	platform_device_unregister(&device->pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	return; /* platform_device_unregister frees dev and device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) out_free_netdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) out_free_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	kfree(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) static struct uml_net *find_device(int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	struct uml_net *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	struct list_head *ele;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	spin_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	list_for_each(ele, &devices) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		device = list_entry(ele, struct uml_net, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		if (device->index == n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	device = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	spin_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	return device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) static int eth_parse(char *str, int *index_out, char **str_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		     char **error_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	char *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	int n, err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	n = simple_strtoul(str, &end, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	if (end == str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		*error_out = "Bad device number";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	str = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	if (*str != '=') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		*error_out = "Expected '=' after device number";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	str++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (find_device(n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		*error_out = "Device already configured";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	*index_out = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	*str_out = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) struct eth_init {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	char *init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) static DEFINE_SPINLOCK(transports_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) static LIST_HEAD(transports);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) /* Filled in during early boot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) static LIST_HEAD(eth_cmd_line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) static int check_transport(struct transport *transport, char *eth, int n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 			   void **init_out, char **mac_out, gfp_t gfp_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	len = strlen(transport->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	if (strncmp(eth, transport->name, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	eth += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	if (*eth == ',')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		eth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	else if (*eth != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	*init_out = kmalloc(transport->setup_size, gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	if (*init_out == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	if (!transport->setup(eth, mac_out, *init_out)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		kfree(*init_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		*init_out = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) void register_transport(struct transport *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	struct list_head *ele, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	struct eth_init *eth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	void *init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	char *mac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	int match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	spin_lock(&transports_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	BUG_ON(!list_empty(&new->list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	list_add(&new->list, &transports);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	spin_unlock(&transports_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	list_for_each_safe(ele, next, &eth_cmd_line) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		eth = list_entry(ele, struct eth_init, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		match = check_transport(new, eth->init, eth->index, &init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 					&mac, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		if (!match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		else if (init != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 			eth_configure(eth->index, init, mac, new, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 			kfree(init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		list_del(&eth->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) static int eth_setup_common(char *str, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	struct list_head *ele;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	struct transport *transport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	void *init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	char *mac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	spin_lock(&transports_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	list_for_each(ele, &transports) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		transport = list_entry(ele, struct transport, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	        if (!check_transport(transport, str, index, &init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 					&mac, GFP_ATOMIC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		if (init != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 			eth_configure(index, init, mac, transport, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 			kfree(init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	spin_unlock(&transports_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) static int __init eth_setup(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	struct eth_init *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	char *error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	int n, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	err = eth_parse(str, &n, &str, &error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		printk(KERN_ERR "eth_setup - Couldn't parse '%s' : %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		       str, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	new = memblock_alloc(sizeof(*new), SMP_CACHE_BYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		panic("%s: Failed to allocate %zu bytes\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 		      sizeof(*new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	INIT_LIST_HEAD(&new->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	new->index = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	new->init = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	list_add_tail(&new->list, &eth_cmd_line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) __setup("eth", eth_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) __uml_help(eth_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) "eth[0-9]+=<transport>,<options>\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) "    Configure a network device.\n\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) static int net_config(char *str, char **error_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	int n, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	err = eth_parse(str, &n, &str, error_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	/* This string is broken up and the pieces used by the underlying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	 * driver.  So, it is freed only if eth_setup_common fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	str = kstrdup(str, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	if (str == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	        *error_out = "net_config failed to strdup string";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	err = !eth_setup_common(str, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		kfree(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) static int net_id(char **str, int *start_out, int *end_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	char *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	n = simple_strtoul(*str, &end, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	if ((*end != '\0') || (end == *str))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	*start_out = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	*end_out = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	*str = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) static int net_remove(int n, char **error_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	struct uml_net *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	struct uml_net_private *lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	device = find_device(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	if (device == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	dev = device->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	lp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	if (lp->fd > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	unregister_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	platform_device_unregister(&device->pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) static struct mc_device net_mc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	.list		= LIST_HEAD_INIT(net_mc.list),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	.name		= "eth",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	.config		= net_config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	.get_config	= NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	.id		= net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	.remove		= net_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) #ifdef CONFIG_INET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 			      void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	struct in_ifaddr *ifa = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	struct net_device *dev = ifa->ifa_dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	struct uml_net_private *lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	void (*proc)(unsigned char *, unsigned char *, void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	unsigned char addr_buf[4], netmask_buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	if (dev->netdev_ops->ndo_open != uml_net_open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	lp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	proc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	case NETDEV_UP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		proc = lp->add_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	case NETDEV_DOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 		proc = lp->delete_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	if (proc != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 		(*proc)(addr_buf, netmask_buf, &lp->user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) /* uml_net_init shouldn't be called twice on two CPUs at the same time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) static struct notifier_block uml_inetaddr_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	.notifier_call		= uml_inetaddr_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) static void inet_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	struct list_head *ele;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	struct uml_net_private *lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	struct in_device *ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	struct in_ifaddr *in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	register_inetaddr_notifier(&uml_inetaddr_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	/* Devices may have been opened already, so the uml_inetaddr_notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	 * didn't get a chance to run for them.  This fakes it so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	 * addresses which have already been set up get handled properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 	spin_lock(&opened_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	list_for_each(ele, &opened) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 		lp = list_entry(ele, struct uml_net_private, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 		ip = lp->dev->ip_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 		if (ip == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 		in = ip->ifa_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 		while (in != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 			uml_inetaddr_event(NULL, NETDEV_UP, in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 			in = in->ifa_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	spin_unlock(&opened_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) static inline void inet_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) static int uml_net_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	mconsole_register_dev(&net_mc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	inet_register();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) __initcall(uml_net_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) static void close_devices(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	struct list_head *ele;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	struct uml_net_private *lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	spin_lock(&opened_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 	list_for_each(ele, &opened) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 		lp = list_entry(ele, struct uml_net_private, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 		um_free_irq(lp->dev->irq, lp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 		if ((lp->close != NULL) && (lp->fd >= 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 			(*lp->close)(lp->fd, &lp->user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 		if (lp->remove != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 			(*lp->remove)(&lp->user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 	spin_unlock(&opened_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) __uml_exitcall(close_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 					void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 		    void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 	struct net_device *dev = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	struct in_device *ip = dev->ip_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	struct in_ifaddr *in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	unsigned char address[4], netmask[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	if (ip == NULL) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 	in = ip->ifa_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 	while (in != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 		memcpy(address, &in->ifa_address, sizeof(address));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 		memcpy(netmask, &in->ifa_mask, sizeof(netmask));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 		(*cb)(address, netmask, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 		in = in->ifa_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) int dev_netmask(void *d, void *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 	struct net_device *dev = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 	struct in_device *ip = dev->ip_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	struct in_ifaddr *in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 	__be32 *mask_out = m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	if (ip == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 	in = ip->ifa_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 	if (in == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 	*mask_out = in->ifa_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) void *get_output_buffer(int *len_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 	void *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 	ret = (void *) __get_free_pages(GFP_KERNEL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 	if (ret) *len_out = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 	else *len_out = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) void free_output_buffer(void *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 	free_pages((unsigned long) buffer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 		     char **gate_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 	char *remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 	remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 	if (remain != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 		printk(KERN_ERR "tap_setup_common - Extra garbage on "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 		       "specification : '%s'\n", remain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) unsigned short eth_protocol(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 	return eth_type_trans(skb, skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) }