11 #include <linux/ethtool.h>
14 #include <linux/list.h>
15 #include <linux/netdevice.h>
17 #include <linux/rtnetlink.h>
19 #include <linux/slab.h>
28 #define DRIVER_NAME "uml-netdev"
42 static struct sk_buff *drop_skb;
45 static int update_drop_skb(
int max)
57 new = dev_alloc_skb(max);
68 spin_unlock_irqrestore(&drop_lock, flags);
84 (*lp->
read)(lp->
fd, drop_skb, lp);
85 dev->
stats.rx_dropped++;
91 skb_reset_mac_header(skb);
92 pkt_len = (*lp->
read)(lp->
fd, skb, lp);
99 dev->
stats.rx_packets++;
121 if (!netif_running(dev))
124 spin_lock(&lp->
lock);
125 while ((err = uml_net_rx(dev)) > 0) ;
128 "Device '%s' read returned %d, shutting it down\n",
143 spin_unlock(&lp->
lock);
147 static int uml_net_open(
struct net_device *dev)
172 netif_start_queue(dev);
178 while ((err = uml_net_rx(dev)) > 0) ;
180 spin_lock(&opened_lock);
181 list_add(&lp->
list, &opened);
182 spin_unlock(&opened_lock);
192 static int uml_net_close(
struct net_device *dev)
196 netif_stop_queue(dev);
203 spin_lock(&opened_lock);
205 spin_unlock(&opened_lock);
216 netif_stop_queue(dev);
220 len = (*lp->
write)(lp->
fd, skb, lp);
222 if (len == skb->
len) {
223 dev->
stats.tx_packets++;
226 netif_start_queue(dev);
229 netif_wake_queue(dev);
232 netif_start_queue(dev);
233 dev->
stats.tx_dropped++;
236 netif_start_queue(dev);
240 spin_unlock_irqrestore(&lp->
lock, flags);
247 static void uml_net_set_multicast_list(
struct net_device *dev)
252 static void uml_net_tx_timeout(
struct net_device *dev)
255 netif_wake_queue(dev);
258 static int uml_net_change_mtu(
struct net_device *dev,
int new_mtu)
265 #ifdef CONFIG_NET_POLL_CONTROLLER
266 static void uml_net_poll_controller(
struct net_device *dev)
269 uml_net_interrupt(dev->
irq, dev);
274 static void uml_net_get_drvinfo(
struct net_device *dev,
281 static const struct ethtool_ops uml_net_ethtool_ops = {
282 .get_drvinfo = uml_net_get_drvinfo,
286 static void uml_net_user_timer_expire(
unsigned long _conn)
296 static int setup_etheraddr(
char *
str,
unsigned char *
addr,
char *
name)
304 for (i = 0; i < 6; i++) {
307 ((*end !=
':') && (*end !=
',') && (*end !=
'\0'))) {
309 "setup_etheraddr: failed to parse '%s' "
310 "as an ethernet address\n", str);
315 if (is_multicast_ether_addr(addr)) {
317 "Attempt to assign a multicast ethernet address to a "
318 "device disallowed\n");
321 if (!is_valid_ether_addr(addr)) {
323 "Attempt to assign an invalid ethernet address to a "
324 "device disallowed\n");
327 if (!is_local_ether_addr(addr)) {
329 "Warning: Assigning a globally valid ethernet "
330 "address to a device\n");
332 "the first byte of the MAC,\n");
334 addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4],
341 "Choosing a random ethernet address for device %s\n", name);
342 eth_random_addr(addr);
355 static void net_device_release(
struct device *dev)
369 .ndo_open = uml_net_open,
370 .ndo_stop = uml_net_close,
371 .ndo_start_xmit = uml_net_start_xmit,
372 .ndo_set_rx_mode = uml_net_set_multicast_list,
373 .ndo_tx_timeout = uml_net_tx_timeout,
375 .ndo_change_mtu = uml_net_change_mtu,
377 #ifdef CONFIG_NET_POLL_CONTROLLER
378 .ndo_poll_controller = uml_net_poll_controller,
386 static int driver_registered;
388 static void eth_configure(
int n,
void *
init,
char *
mac,
399 device = kzalloc(
sizeof(*device),
GFP_KERNEL);
400 if (device ==
NULL) {
406 dev = alloc_etherdev(size);
409 "net_device for eth%d\n", n);
410 goto out_free_device;
413 INIT_LIST_HEAD(&device->
list);
422 random_mac = setup_etheraddr(mac, device->
mac, dev->
name);
426 lp = netdev_priv(dev);
433 if (!driver_registered) {
435 driver_registered = 1;
439 device->
pdev.dev.release = net_device_release;
442 goto out_free_netdev;
451 (*transport->
kern->init)(dev, init);
457 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
458 .max_packet = transport->
user->max_packet,
459 .protocol = transport->
kern->protocol,
460 .open = transport->
user->open,
461 .close = transport->
user->close,
462 .remove = transport->
user->remove,
463 .read = transport->
kern->read,
464 .write = transport->
kern->write,
465 .add_address = transport->
user->add_address,
466 .delete_address = transport->
user->delete_address });
470 lp->
tl.function = uml_net_user_timer_expire;
473 if ((transport->
user->init !=
NULL) &&
474 ((*transport->
user->init)(&lp->
user, dev) != 0))
482 dev->
mtu = transport->
user->mtu;
490 goto out_undo_user_init;
496 goto out_undo_user_init;
498 spin_lock(&devices_lock);
499 list_add(&device->
list, &devices);
500 spin_unlock(&devices_lock);
505 if (transport->
user->remove !=
NULL)
506 (*transport->
user->remove)(&lp->
user);
516 static struct uml_net *find_device(
int n)
521 spin_lock(&devices_lock);
524 if (device->
index == n)
529 spin_unlock(&devices_lock);
533 static int eth_parse(
char *str,
int *index_out,
char **str_out,
541 *error_out =
"Bad device number";
547 *error_out =
"Expected '=' after device number";
552 if (find_device(n)) {
553 *error_out =
"Device already configured";
574 static int check_transport(
struct transport *transport,
char *eth,
int n,
575 void **init_out,
char **mac_out)
586 else if (*eth !=
'\0')
590 if (*init_out ==
NULL)
593 if (!transport->
setup(eth, mac_out, *init_out)) {
608 spin_lock(&transports_lock);
609 BUG_ON(!list_empty(&new->list));
610 list_add(&new->list, &transports);
611 spin_unlock(&transports_lock);
615 match = check_transport(
new, eth->
init, eth->
index, &init,
619 else if (init !=
NULL) {
620 eth_configure(eth->
index, init, mac,
new);
627 static int eth_setup_common(
char *str,
int index)
635 spin_lock(&transports_lock);
638 if (!check_transport(transport, str, index, &init, &mac))
641 eth_configure(index, init, mac, transport);
648 spin_unlock(&transports_lock);
652 static int __init eth_setup(
char *str)
658 err = eth_parse(str, &n, &str, &error);
671 INIT_LIST_HEAD(&new->list);
681 "eth[0-9]+=<transport>,<options>\n"
682 " Configure a network device.\n\n"
685 static int net_config(
char *str,
char **error_out)
689 err = eth_parse(str, &n, &str, error_out);
698 *error_out =
"net_config failed to strdup string";
701 err = !eth_setup_common(str, n);
707 static int net_id(
char **str,
int *start_out,
int *end_out)
713 if ((*end !=
'\0') || (end == *str))
722 static int net_remove(
int n,
char **error_out)
728 device = find_device(n);
733 lp = netdev_priv(dev);
745 .config = net_config,
748 .remove = net_remove,
755 struct in_ifaddr *ifa =
ptr;
758 void (*proc)(
unsigned char *,
unsigned char *,
void *);
759 unsigned char addr_buf[4], netmask_buf[4];
761 if (dev->
netdev_ops->ndo_open != uml_net_open)
764 lp = netdev_priv(dev);
776 memcpy(addr_buf, &ifa->ifa_address,
sizeof(addr_buf));
777 memcpy(netmask_buf, &ifa->ifa_mask,
sizeof(netmask_buf));
778 (*proc)(addr_buf, netmask_buf, &lp->
user);
788 static void inet_register(
void)
792 struct in_device *
ip;
793 struct in_ifaddr *
in;
801 spin_lock(&opened_lock);
804 ip = lp->
dev->ip_ptr;
813 spin_unlock(&opened_lock);
816 static inline void inet_register(
void)
821 static int uml_net_init(
void)
830 static void close_devices(
void)
835 spin_lock(&opened_lock);
844 spin_unlock(&opened_lock);
854 struct in_device *ip = dev->
ip_ptr;
855 struct in_ifaddr *
in;
856 unsigned char address[4], netmask[4];
858 if (ip ==
NULL)
return;
861 memcpy(address, &in->ifa_address,
sizeof(address));
862 memcpy(netmask, &in->ifa_mask,
sizeof(netmask));
871 struct in_device *ip = dev->
ip_ptr;
872 struct in_ifaddr *
in;
882 *mask_out = in->ifa_mask;
907 if (remain !=
NULL) {
909 "specification : '%s'\n", remain);