2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
4 * James Leu (jleu@mindspring.net).
5 * Copyright (C) 2001 by various other people who didn't put their name here.
6 * Licensed under the GPL.
9 #include <linux/bootmem.h>
10 #include <linux/etherdevice.h>
11 #include <linux/ethtool.h>
12 #include <linux/inetdevice.h>
13 #include <linux/init.h>
14 #include <linux/list.h>
15 #include <linux/netdevice.h>
16 #include <linux/platform_device.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/skbuff.h>
19 #include <linux/spinlock.h>
23 #include "mconsole_kern.h"
27 static inline void set_ether_mac(struct net_device
*dev
, unsigned char *addr
)
29 memcpy(dev
->dev_addr
, addr
, ETH_ALEN
);
32 #define DRIVER_NAME "uml-netdev"
34 static DEFINE_SPINLOCK(opened_lock
);
35 static LIST_HEAD(opened
);
37 static int uml_net_rx(struct net_device
*dev
)
39 struct uml_net_private
*lp
= dev
->priv
;
43 /* If we can't allocate memory, try again next round. */
44 skb
= dev_alloc_skb(dev
->mtu
);
46 lp
->stats
.rx_dropped
++;
51 skb_put(skb
, dev
->mtu
);
52 skb_reset_mac_header(skb
);
53 pkt_len
= (*lp
->read
)(lp
->fd
, &skb
, lp
);
56 skb_trim(skb
, pkt_len
);
57 skb
->protocol
= (*lp
->protocol
)(skb
);
60 lp
->stats
.rx_bytes
+= skb
->len
;
61 lp
->stats
.rx_packets
++;
69 static void uml_dev_close(struct work_struct
*work
)
71 struct uml_net_private
*lp
=
72 container_of(work
, struct uml_net_private
, work
);
76 irqreturn_t
uml_net_interrupt(int irq
, void *dev_id
)
78 struct net_device
*dev
= dev_id
;
79 struct uml_net_private
*lp
= dev
->priv
;
82 if (!netif_running(dev
))
86 while ((err
= uml_net_rx(dev
)) > 0) ;
89 "Device '%s' read returned %d, shutting it down\n",
91 /* dev_close can't be called in interrupt context, and takes
93 * And dev_close() can be safely called multiple times on the
94 * same device, since it tests for (dev->flags & IFF_UP). So
95 * there's no harm in delaying the device shutdown.
96 * Furthermore, the workqueue will not re-enqueue an already
97 * enqueued work item. */
98 schedule_work(&lp
->work
);
101 reactivate_fd(lp
->fd
, UM_ETH_IRQ
);
104 spin_unlock(&lp
->lock
);
108 static int uml_net_open(struct net_device
*dev
)
110 struct uml_net_private
*lp
= dev
->priv
;
118 lp
->fd
= (*lp
->open
)(&lp
->user
);
124 err
= um_request_irq(dev
->irq
, lp
->fd
, IRQ_READ
, uml_net_interrupt
,
125 IRQF_DISABLED
| IRQF_SHARED
, dev
->name
, dev
);
127 printk(KERN_ERR
"uml_net_open: failed to get irq(%d)\n", err
);
132 lp
->tl
.data
= (unsigned long) &lp
->user
;
133 netif_start_queue(dev
);
135 /* clear buffer - it can happen that the host side of the interface
136 * is full when we get here. In this case, new data is never queued,
137 * SIGIOs never arrive, and the net never works.
139 while ((err
= uml_net_rx(dev
)) > 0) ;
141 spin_lock(&opened_lock
);
142 list_add(&lp
->list
, &opened
);
143 spin_unlock(&opened_lock
);
147 if (lp
->close
!= NULL
) (*lp
->close
)(lp
->fd
, &lp
->user
);
153 static int uml_net_close(struct net_device
*dev
)
155 struct uml_net_private
*lp
= dev
->priv
;
157 netif_stop_queue(dev
);
159 free_irq(dev
->irq
, dev
);
160 if (lp
->close
!= NULL
)
161 (*lp
->close
)(lp
->fd
, &lp
->user
);
164 spin_lock(&opened_lock
);
166 spin_unlock(&opened_lock
);
171 static int uml_net_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
173 struct uml_net_private
*lp
= dev
->priv
;
177 netif_stop_queue(dev
);
179 spin_lock_irqsave(&lp
->lock
, flags
);
181 len
= (*lp
->write
)(lp
->fd
, &skb
, lp
);
183 if (len
== skb
->len
) {
184 lp
->stats
.tx_packets
++;
185 lp
->stats
.tx_bytes
+= skb
->len
;
186 dev
->trans_start
= jiffies
;
187 netif_start_queue(dev
);
189 /* this is normally done in the interrupt when tx finishes */
190 netif_wake_queue(dev
);
193 netif_start_queue(dev
);
194 lp
->stats
.tx_dropped
++;
197 netif_start_queue(dev
);
198 printk(KERN_ERR
"uml_net_start_xmit: failed(%d)\n", len
);
201 spin_unlock_irqrestore(&lp
->lock
, flags
);
208 static struct net_device_stats
*uml_net_get_stats(struct net_device
*dev
)
210 struct uml_net_private
*lp
= dev
->priv
;
214 static void uml_net_set_multicast_list(struct net_device
*dev
)
216 if (dev
->flags
& IFF_PROMISC
)
218 else if (dev
->mc_count
)
219 dev
->flags
|= IFF_ALLMULTI
;
220 else dev
->flags
&= ~IFF_ALLMULTI
;
223 static void uml_net_tx_timeout(struct net_device
*dev
)
225 dev
->trans_start
= jiffies
;
226 netif_wake_queue(dev
);
229 static int uml_net_set_mac(struct net_device
*dev
, void *addr
)
231 struct uml_net_private
*lp
= dev
->priv
;
232 struct sockaddr
*hwaddr
= addr
;
234 spin_lock_irq(&lp
->lock
);
235 set_ether_mac(dev
, hwaddr
->sa_data
);
236 spin_unlock_irq(&lp
->lock
);
241 static int uml_net_change_mtu(struct net_device
*dev
, int new_mtu
)
243 struct uml_net_private
*lp
= dev
->priv
;
246 spin_lock_irq(&lp
->lock
);
248 new_mtu
= (*lp
->set_mtu
)(new_mtu
, &lp
->user
);
257 spin_unlock_irq(&lp
->lock
);
261 static void uml_net_get_drvinfo(struct net_device
*dev
,
262 struct ethtool_drvinfo
*info
)
264 strcpy(info
->driver
, DRIVER_NAME
);
265 strcpy(info
->version
, "42");
268 static struct ethtool_ops uml_net_ethtool_ops
= {
269 .get_drvinfo
= uml_net_get_drvinfo
,
270 .get_link
= ethtool_op_get_link
,
273 void uml_net_user_timer_expire(unsigned long _conn
)
276 struct connection
*conn
= (struct connection
*)_conn
;
278 dprintk(KERN_INFO
"uml_net_user_timer_expire [%p]\n", conn
);
283 static void setup_etheraddr(char *str
, unsigned char *addr
, char *name
)
291 for (i
= 0;i
< 6; i
++) {
292 addr
[i
] = simple_strtoul(str
, &end
, 16);
294 ((*end
!= ':') && (*end
!= ',') && (*end
!= '\0'))) {
296 "setup_etheraddr: failed to parse '%s' "
297 "as an ethernet address\n", str
);
302 if (is_multicast_ether_addr(addr
)) {
304 "Attempt to assign a multicast ethernet address to a "
305 "device disallowed\n");
308 if (!is_valid_ether_addr(addr
)) {
310 "Attempt to assign an invalid ethernet address to a "
311 "device disallowed\n");
314 if (!is_local_ether_addr(addr
)) {
316 "Warning: attempt to assign a globally valid ethernet "
317 "address to a device\n");
318 printk(KERN_WARNING
"You should better enable the 2nd "
319 "rightmost bit in the first byte of the MAC,\n");
320 printk(KERN_WARNING
"i.e. %02x:%02x:%02x:%02x:%02x:%02x\n",
321 addr
[0] | 0x02, addr
[1], addr
[2], addr
[3], addr
[4],
329 "Choosing a random ethernet address for device %s\n", name
);
330 random_ether_addr(addr
);
333 static DEFINE_SPINLOCK(devices_lock
);
334 static LIST_HEAD(devices
);
336 static struct platform_driver uml_net_driver
= {
341 static int driver_registered
;
343 static void net_device_release(struct device
*dev
)
345 struct uml_net
*device
= dev
->driver_data
;
346 struct net_device
*netdev
= device
->dev
;
347 struct uml_net_private
*lp
= netdev
->priv
;
349 if (lp
->remove
!= NULL
)
350 (*lp
->remove
)(&lp
->user
);
351 list_del(&device
->list
);
356 static void eth_configure(int n
, void *init
, char *mac
,
357 struct transport
*transport
)
359 struct uml_net
*device
;
360 struct net_device
*dev
;
361 struct uml_net_private
*lp
;
364 size
= transport
->private_size
+ sizeof(struct uml_net_private
);
366 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
367 if (device
== NULL
) {
368 printk(KERN_ERR
"eth_configure failed to allocate struct "
373 dev
= alloc_etherdev(size
);
375 printk(KERN_ERR
"eth_configure: failed to allocate struct "
376 "net_device for eth%d\n", n
);
377 goto out_free_device
;
380 INIT_LIST_HEAD(&device
->list
);
383 /* If this name ends up conflicting with an existing registered
384 * netdevice, that is OK, register_netdev{,ice}() will notice this
387 snprintf(dev
->name
, sizeof(dev
->name
), "eth%d", n
);
389 setup_etheraddr(mac
, device
->mac
, dev
->name
);
391 printk(KERN_INFO
"Netdevice %d ", n
);
392 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
393 device
->mac
[0], device
->mac
[1],
394 device
->mac
[2], device
->mac
[3],
395 device
->mac
[4], device
->mac
[5]);
399 /* This points to the transport private data. It's still clear, but we
400 * must memset it to 0 *now*. Let's help the drivers. */
402 INIT_WORK(&lp
->work
, uml_dev_close
);
405 if (!driver_registered
) {
406 platform_driver_register(¨_net_driver
);
407 driver_registered
= 1;
410 device
->pdev
.name
= DRIVER_NAME
;
411 device
->pdev
.dev
.release
= net_device_release
;
412 device
->pdev
.dev
.driver_data
= device
;
413 if (platform_device_register(&device
->pdev
))
414 goto out_free_netdev
;
415 SET_NETDEV_DEV(dev
,&device
->pdev
.dev
);
420 * These just fill in a data structure, so there's no failure
421 * to be worried about.
423 (*transport
->kern
->init
)(dev
, init
);
425 *lp
= ((struct uml_net_private
)
426 { .list
= LIST_HEAD_INIT(lp
->list
),
429 .mac
= { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
430 .protocol
= transport
->kern
->protocol
,
431 .open
= transport
->user
->open
,
432 .close
= transport
->user
->close
,
433 .remove
= transport
->user
->remove
,
434 .read
= transport
->kern
->read
,
435 .write
= transport
->kern
->write
,
436 .add_address
= transport
->user
->add_address
,
437 .delete_address
= transport
->user
->delete_address
,
438 .set_mtu
= transport
->user
->set_mtu
});
441 spin_lock_init(&lp
->lock
);
442 lp
->tl
.function
= uml_net_user_timer_expire
;
443 memcpy(lp
->mac
, device
->mac
, sizeof(lp
->mac
));
445 if ((transport
->user
->init
!= NULL
) &&
446 ((*transport
->user
->init
)(&lp
->user
, dev
) != 0))
449 set_ether_mac(dev
, device
->mac
);
450 dev
->mtu
= transport
->user
->max_packet
;
451 dev
->open
= uml_net_open
;
452 dev
->hard_start_xmit
= uml_net_start_xmit
;
453 dev
->stop
= uml_net_close
;
454 dev
->get_stats
= uml_net_get_stats
;
455 dev
->set_multicast_list
= uml_net_set_multicast_list
;
456 dev
->tx_timeout
= uml_net_tx_timeout
;
457 dev
->set_mac_address
= uml_net_set_mac
;
458 dev
->change_mtu
= uml_net_change_mtu
;
459 dev
->ethtool_ops
= ¨_net_ethtool_ops
;
460 dev
->watchdog_timeo
= (HZ
>> 1);
461 dev
->irq
= UM_ETH_IRQ
;
464 err
= register_netdevice(dev
);
467 goto out_undo_user_init
;
469 spin_lock(&devices_lock
);
470 list_add(&device
->list
, &devices
);
471 spin_unlock(&devices_lock
);
476 if (transport
->user
->remove
!= NULL
)
477 (*transport
->user
->remove
)(&lp
->user
);
479 platform_device_unregister(&device
->pdev
);
480 return; /* platform_device_unregister frees dev and device */
487 static struct uml_net
*find_device(int n
)
489 struct uml_net
*device
;
490 struct list_head
*ele
;
492 spin_lock(&devices_lock
);
493 list_for_each(ele
, &devices
) {
494 device
= list_entry(ele
, struct uml_net
, list
);
495 if (device
->index
== n
)
500 spin_unlock(&devices_lock
);
504 static int eth_parse(char *str
, int *index_out
, char **str_out
,
508 int n
, err
= -EINVAL
;;
510 n
= simple_strtoul(str
, &end
, 0);
512 *error_out
= "Bad device number";
518 *error_out
= "Expected '=' after device number";
523 if (find_device(n
)) {
524 *error_out
= "Device already configured";
534 struct list_head list
;
539 static DEFINE_SPINLOCK(transports_lock
);
540 static LIST_HEAD(transports
);
542 /* Filled in during early boot */
543 static LIST_HEAD(eth_cmd_line
);
545 static int check_transport(struct transport
*transport
, char *eth
, int n
,
546 void **init_out
, char **mac_out
)
550 len
= strlen(transport
->name
);
551 if (strncmp(eth
, transport
->name
, len
))
557 else if (*eth
!= '\0')
560 *init_out
= kmalloc(transport
->setup_size
, GFP_KERNEL
);
561 if (*init_out
== NULL
)
564 if (!transport
->setup(eth
, mac_out
, *init_out
)) {
571 void register_transport(struct transport
*new)
573 struct list_head
*ele
, *next
;
574 struct eth_init
*eth
;
579 spin_lock(&transports_lock
);
580 BUG_ON(!list_empty(&new->list
));
581 list_add(&new->list
, &transports
);
582 spin_unlock(&transports_lock
);
584 list_for_each_safe(ele
, next
, ð_cmd_line
) {
585 eth
= list_entry(ele
, struct eth_init
, list
);
586 match
= check_transport(new, eth
->init
, eth
->index
, &init
,
590 else if (init
!= NULL
) {
591 eth_configure(eth
->index
, init
, mac
, new);
594 list_del(ð
->list
);
598 static int eth_setup_common(char *str
, int index
)
600 struct list_head
*ele
;
601 struct transport
*transport
;
606 spin_lock(&transports_lock
);
607 list_for_each(ele
, &transports
) {
608 transport
= list_entry(ele
, struct transport
, list
);
609 if (!check_transport(transport
, str
, index
, &init
, &mac
))
612 eth_configure(index
, init
, mac
, transport
);
619 spin_unlock(&transports_lock
);
623 static int __init
eth_setup(char *str
)
625 struct eth_init
*new;
629 err
= eth_parse(str
, &n
, &str
, &error
);
631 printk(KERN_ERR
"eth_setup - Couldn't parse '%s' : %s\n",
636 new = alloc_bootmem(sizeof(*new));
638 printk(KERN_ERR
"eth_init : alloc_bootmem failed\n");
642 INIT_LIST_HEAD(&new->list
);
646 list_add_tail(&new->list
, ð_cmd_line
);
650 __setup("eth", eth_setup
);
651 __uml_help(eth_setup
,
652 "eth[0-9]+=<transport>,<options>\n"
653 " Configure a network device.\n\n"
656 static int net_config(char *str
, char **error_out
)
660 err
= eth_parse(str
, &n
, &str
, error_out
);
664 /* This string is broken up and the pieces used by the underlying
665 * driver. So, it is freed only if eth_setup_common fails.
667 str
= kstrdup(str
, GFP_KERNEL
);
669 *error_out
= "net_config failed to strdup string";
672 err
= !eth_setup_common(str
, n
);
678 static int net_id(char **str
, int *start_out
, int *end_out
)
683 n
= simple_strtoul(*str
, &end
, 0);
684 if ((*end
!= '\0') || (end
== *str
))
693 static int net_remove(int n
, char **error_out
)
695 struct uml_net
*device
;
696 struct net_device
*dev
;
697 struct uml_net_private
*lp
;
699 device
= find_device(n
);
707 unregister_netdev(dev
);
708 platform_device_unregister(&device
->pdev
);
713 static struct mc_device net_mc
= {
714 .list
= LIST_HEAD_INIT(net_mc
.list
),
716 .config
= net_config
,
719 .remove
= net_remove
,
722 static int uml_inetaddr_event(struct notifier_block
*this, unsigned long event
,
725 struct in_ifaddr
*ifa
= ptr
;
726 struct net_device
*dev
= ifa
->ifa_dev
->dev
;
727 struct uml_net_private
*lp
;
728 void (*proc
)(unsigned char *, unsigned char *, void *);
729 unsigned char addr_buf
[4], netmask_buf
[4];
731 if (dev
->open
!= uml_net_open
)
739 proc
= lp
->add_address
;
742 proc
= lp
->delete_address
;
746 memcpy(addr_buf
, &ifa
->ifa_address
, sizeof(addr_buf
));
747 memcpy(netmask_buf
, &ifa
->ifa_mask
, sizeof(netmask_buf
));
748 (*proc
)(addr_buf
, netmask_buf
, &lp
->user
);
753 /* uml_net_init shouldn't be called twice on two CPUs at the same time */
754 struct notifier_block uml_inetaddr_notifier
= {
755 .notifier_call
= uml_inetaddr_event
,
758 static int uml_net_init(void)
760 struct list_head
*ele
;
761 struct uml_net_private
*lp
;
762 struct in_device
*ip
;
763 struct in_ifaddr
*in
;
765 mconsole_register_dev(&net_mc
);
766 register_inetaddr_notifier(¨_inetaddr_notifier
);
768 /* Devices may have been opened already, so the uml_inetaddr_notifier
769 * didn't get a chance to run for them. This fakes it so that
770 * addresses which have already been set up get handled properly.
772 spin_lock(&opened_lock
);
773 list_for_each(ele
, &opened
) {
774 lp
= list_entry(ele
, struct uml_net_private
, list
);
775 ip
= lp
->dev
->ip_ptr
;
780 uml_inetaddr_event(NULL
, NETDEV_UP
, in
);
784 spin_unlock(&opened_lock
);
789 __initcall(uml_net_init
);
791 static void close_devices(void)
793 struct list_head
*ele
;
794 struct uml_net_private
*lp
;
796 spin_lock(&opened_lock
);
797 list_for_each(ele
, &opened
) {
798 lp
= list_entry(ele
, struct uml_net_private
, list
);
799 free_irq(lp
->dev
->irq
, lp
->dev
);
800 if ((lp
->close
!= NULL
) && (lp
->fd
>= 0))
801 (*lp
->close
)(lp
->fd
, &lp
->user
);
802 if (lp
->remove
!= NULL
)
803 (*lp
->remove
)(&lp
->user
);
805 spin_unlock(&opened_lock
);
808 __uml_exitcall(close_devices
);
810 struct sk_buff
*ether_adjust_skb(struct sk_buff
*skb
, int extra
)
812 if ((skb
!= NULL
) && (skb_tailroom(skb
) < extra
)) {
813 struct sk_buff
*skb2
;
815 skb2
= skb_copy_expand(skb
, 0, extra
, GFP_ATOMIC
);
819 if (skb
!= NULL
) skb_put(skb
, extra
);
823 void iter_addresses(void *d
, void (*cb
)(unsigned char *, unsigned char *,
827 struct net_device
*dev
= d
;
828 struct in_device
*ip
= dev
->ip_ptr
;
829 struct in_ifaddr
*in
;
830 unsigned char address
[4], netmask
[4];
832 if (ip
== NULL
) return;
835 memcpy(address
, &in
->ifa_address
, sizeof(address
));
836 memcpy(netmask
, &in
->ifa_mask
, sizeof(netmask
));
837 (*cb
)(address
, netmask
, arg
);
842 int dev_netmask(void *d
, void *m
)
844 struct net_device
*dev
= d
;
845 struct in_device
*ip
= dev
->ip_ptr
;
846 struct in_ifaddr
*in
;
847 __be32
*mask_out
= m
;
856 *mask_out
= in
->ifa_mask
;
860 void *get_output_buffer(int *len_out
)
864 ret
= (void *) __get_free_pages(GFP_KERNEL
, 0);
865 if (ret
) *len_out
= PAGE_SIZE
;
870 void free_output_buffer(void *buffer
)
872 free_pages((unsigned long) buffer
, 0);
875 int tap_setup_common(char *str
, char *type
, char **dev_name
, char **mac_out
,
880 remain
= split_if_spec(str
, dev_name
, mac_out
, gate_addr
, NULL
);
881 if (remain
!= NULL
) {
882 printk(KERN_ERR
"tap_setup_common - Extra garbage on "
883 "specification : '%s'\n", remain
);
890 unsigned short eth_protocol(struct sk_buff
*skb
)
892 return eth_type_trans(skb
, skb
->dev
);