2 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
3 * James Leu (jleu@mindspring.net).
4 * Copyright (C) 2001 by various other people who didn't put their name here.
5 * Licensed under the GPL.
8 #include "linux/kernel.h"
9 #include "linux/netdevice.h"
10 #include "linux/rtnetlink.h"
11 #include "linux/skbuff.h"
12 #include "linux/socket.h"
13 #include "linux/spinlock.h"
14 #include "linux/module.h"
15 #include "linux/init.h"
16 #include "linux/etherdevice.h"
17 #include "linux/list.h"
18 #include "linux/inetdevice.h"
19 #include "linux/ctype.h"
20 #include "linux/bootmem.h"
21 #include "linux/ethtool.h"
22 #include "linux/platform_device.h"
23 #include "asm/uaccess.h"
24 #include "user_util.h"
25 #include "kern_util.h"
28 #include "mconsole_kern.h"
33 static inline void set_ether_mac(struct net_device
*dev
, unsigned char *addr
)
35 memcpy(dev
->dev_addr
, addr
, ETH_ALEN
);
38 #define DRIVER_NAME "uml-netdev"
40 static DEFINE_SPINLOCK(opened_lock
);
41 static LIST_HEAD(opened
);
43 static int uml_net_rx(struct net_device
*dev
)
45 struct uml_net_private
*lp
= dev
->priv
;
49 /* If we can't allocate memory, try again next round. */
50 skb
= dev_alloc_skb(dev
->mtu
);
52 lp
->stats
.rx_dropped
++;
57 skb_put(skb
, dev
->mtu
);
58 skb
->mac
.raw
= skb
->data
;
59 pkt_len
= (*lp
->read
)(lp
->fd
, &skb
, lp
);
62 skb_trim(skb
, pkt_len
);
63 skb
->protocol
= (*lp
->protocol
)(skb
);
66 lp
->stats
.rx_bytes
+= skb
->len
;
67 lp
->stats
.rx_packets
++;
75 static void uml_dev_close(struct work_struct
*work
)
77 struct uml_net_private
*lp
=
78 container_of(work
, struct uml_net_private
, work
);
82 irqreturn_t
uml_net_interrupt(int irq
, void *dev_id
)
84 struct net_device
*dev
= dev_id
;
85 struct uml_net_private
*lp
= dev
->priv
;
88 if(!netif_running(dev
))
92 while((err
= uml_net_rx(dev
)) > 0) ;
95 "Device '%s' read returned %d, shutting it down\n",
97 /* dev_close can't be called in interrupt context, and takes
99 * And dev_close() can be safely called multiple times on the
100 * same device, since it tests for (dev->flags & IFF_UP). So
101 * there's no harm in delaying the device shutdown.
102 * Furthermore, the workqueue will not re-enqueue an already
103 * enqueued work item. */
104 schedule_work(&lp
->work
);
107 reactivate_fd(lp
->fd
, UM_ETH_IRQ
);
110 spin_unlock(&lp
->lock
);
114 static int uml_net_open(struct net_device
*dev
)
116 struct uml_net_private
*lp
= dev
->priv
;
124 lp
->fd
= (*lp
->open
)(&lp
->user
);
130 err
= um_request_irq(dev
->irq
, lp
->fd
, IRQ_READ
, uml_net_interrupt
,
131 IRQF_DISABLED
| IRQF_SHARED
, dev
->name
, dev
);
133 printk(KERN_ERR
"uml_net_open: failed to get irq(%d)\n", err
);
138 lp
->tl
.data
= (unsigned long) &lp
->user
;
139 netif_start_queue(dev
);
141 /* clear buffer - it can happen that the host side of the interface
142 * is full when we get here. In this case, new data is never queued,
143 * SIGIOs never arrive, and the net never works.
145 while((err
= uml_net_rx(dev
)) > 0) ;
147 spin_lock(&opened_lock
);
148 list_add(&lp
->list
, &opened
);
149 spin_unlock(&opened_lock
);
153 if(lp
->close
!= NULL
) (*lp
->close
)(lp
->fd
, &lp
->user
);
159 static int uml_net_close(struct net_device
*dev
)
161 struct uml_net_private
*lp
= dev
->priv
;
163 netif_stop_queue(dev
);
165 free_irq(dev
->irq
, dev
);
166 if(lp
->close
!= NULL
)
167 (*lp
->close
)(lp
->fd
, &lp
->user
);
170 spin_lock(&opened_lock
);
172 spin_unlock(&opened_lock
);
177 static int uml_net_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
179 struct uml_net_private
*lp
= dev
->priv
;
183 netif_stop_queue(dev
);
185 spin_lock_irqsave(&lp
->lock
, flags
);
187 len
= (*lp
->write
)(lp
->fd
, &skb
, lp
);
189 if(len
== skb
->len
) {
190 lp
->stats
.tx_packets
++;
191 lp
->stats
.tx_bytes
+= skb
->len
;
192 dev
->trans_start
= jiffies
;
193 netif_start_queue(dev
);
195 /* this is normally done in the interrupt when tx finishes */
196 netif_wake_queue(dev
);
199 netif_start_queue(dev
);
200 lp
->stats
.tx_dropped
++;
203 netif_start_queue(dev
);
204 printk(KERN_ERR
"uml_net_start_xmit: failed(%d)\n", len
);
207 spin_unlock_irqrestore(&lp
->lock
, flags
);
214 static struct net_device_stats
*uml_net_get_stats(struct net_device
*dev
)
216 struct uml_net_private
*lp
= dev
->priv
;
220 static void uml_net_set_multicast_list(struct net_device
*dev
)
222 if (dev
->flags
& IFF_PROMISC
) return;
223 else if (dev
->mc_count
) dev
->flags
|= IFF_ALLMULTI
;
224 else dev
->flags
&= ~IFF_ALLMULTI
;
227 static void uml_net_tx_timeout(struct net_device
*dev
)
229 dev
->trans_start
= jiffies
;
230 netif_wake_queue(dev
);
233 static int uml_net_set_mac(struct net_device
*dev
, void *addr
)
235 struct uml_net_private
*lp
= dev
->priv
;
236 struct sockaddr
*hwaddr
= addr
;
238 spin_lock_irq(&lp
->lock
);
239 set_ether_mac(dev
, hwaddr
->sa_data
);
240 spin_unlock_irq(&lp
->lock
);
245 static int uml_net_change_mtu(struct net_device
*dev
, int new_mtu
)
247 struct uml_net_private
*lp
= dev
->priv
;
250 spin_lock_irq(&lp
->lock
);
252 new_mtu
= (*lp
->set_mtu
)(new_mtu
, &lp
->user
);
261 spin_unlock_irq(&lp
->lock
);
265 static void uml_net_get_drvinfo(struct net_device
*dev
,
266 struct ethtool_drvinfo
*info
)
268 strcpy(info
->driver
, DRIVER_NAME
);
269 strcpy(info
->version
, "42");
272 static struct ethtool_ops uml_net_ethtool_ops
= {
273 .get_drvinfo
= uml_net_get_drvinfo
,
274 .get_link
= ethtool_op_get_link
,
277 void uml_net_user_timer_expire(unsigned long _conn
)
280 struct connection
*conn
= (struct connection
*)_conn
;
282 dprintk(KERN_INFO
"uml_net_user_timer_expire [%p]\n", conn
);
287 static void setup_etheraddr(char *str
, unsigned char *addr
)
296 addr
[i
] = simple_strtoul(str
, &end
, 16);
298 ((*end
!= ':') && (*end
!= ',') && (*end
!= '\0'))){
300 "setup_etheraddr: failed to parse '%s' "
301 "as an ethernet address\n", str
);
308 "Attempt to assign a broadcast ethernet address to a "
309 "device disallowed\n");
315 random_ether_addr(addr
);
318 static DEFINE_SPINLOCK(devices_lock
);
319 static LIST_HEAD(devices
);
321 static struct platform_driver uml_net_driver
= {
326 static int driver_registered
;
328 static int eth_configure(int n
, void *init
, char *mac
,
329 struct transport
*transport
)
331 struct uml_net
*device
;
332 struct net_device
*dev
;
333 struct uml_net_private
*lp
;
336 size
= transport
->private_size
+ sizeof(struct uml_net_private
) +
337 sizeof(((struct uml_net_private
*) 0)->user
);
339 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
340 if (device
== NULL
) {
341 printk(KERN_ERR
"eth_configure failed to allocate uml_net\n");
345 INIT_LIST_HEAD(&device
->list
);
348 spin_lock(&devices_lock
);
349 list_add(&device
->list
, &devices
);
350 spin_unlock(&devices_lock
);
352 setup_etheraddr(mac
, device
->mac
);
354 printk(KERN_INFO
"Netdevice %d ", n
);
355 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
356 device
->mac
[0], device
->mac
[1],
357 device
->mac
[2], device
->mac
[3],
358 device
->mac
[4], device
->mac
[5]);
360 dev
= alloc_etherdev(size
);
362 printk(KERN_ERR
"eth_configure: failed to allocate device\n");
367 /* This points to the transport private data. It's still clear, but we
368 * must memset it to 0 *now*. Let's help the drivers. */
370 INIT_WORK(&lp
->work
, uml_dev_close
);
373 if (!driver_registered
) {
374 platform_driver_register(¨_net_driver
);
375 driver_registered
= 1;
378 device
->pdev
.name
= DRIVER_NAME
;
379 platform_device_register(&device
->pdev
);
380 SET_NETDEV_DEV(dev
,&device
->pdev
.dev
);
382 /* If this name ends up conflicting with an existing registered
383 * netdevice, that is OK, register_netdev{,ice}() will notice this
386 snprintf(dev
->name
, sizeof(dev
->name
), "eth%d", n
);
389 (*transport
->kern
->init
)(dev
, init
);
391 dev
->mtu
= transport
->user
->max_packet
;
392 dev
->open
= uml_net_open
;
393 dev
->hard_start_xmit
= uml_net_start_xmit
;
394 dev
->stop
= uml_net_close
;
395 dev
->get_stats
= uml_net_get_stats
;
396 dev
->set_multicast_list
= uml_net_set_multicast_list
;
397 dev
->tx_timeout
= uml_net_tx_timeout
;
398 dev
->set_mac_address
= uml_net_set_mac
;
399 dev
->change_mtu
= uml_net_change_mtu
;
400 dev
->ethtool_ops
= ¨_net_ethtool_ops
;
401 dev
->watchdog_timeo
= (HZ
>> 1);
402 dev
->irq
= UM_ETH_IRQ
;
405 err
= register_netdevice(dev
);
409 /* XXX: should we call ->remove() here? */
414 /* lp.user is the first four bytes of the transport data, which
415 * has already been initialized. This structure assignment will
416 * overwrite that, so we make sure that .user gets overwritten with
417 * what it already has.
420 *lp
= ((struct uml_net_private
)
421 { .list
= LIST_HEAD_INIT(lp
->list
),
424 .mac
= { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
425 .protocol
= transport
->kern
->protocol
,
426 .open
= transport
->user
->open
,
427 .close
= transport
->user
->close
,
428 .remove
= transport
->user
->remove
,
429 .read
= transport
->kern
->read
,
430 .write
= transport
->kern
->write
,
431 .add_address
= transport
->user
->add_address
,
432 .delete_address
= transport
->user
->delete_address
,
433 .set_mtu
= transport
->user
->set_mtu
,
437 spin_lock_init(&lp
->lock
);
438 lp
->tl
.function
= uml_net_user_timer_expire
;
439 memcpy(lp
->mac
, device
->mac
, sizeof(lp
->mac
));
441 if (transport
->user
->init
)
442 (*transport
->user
->init
)(&lp
->user
, dev
);
444 set_ether_mac(dev
, device
->mac
);
449 static struct uml_net
*find_device(int n
)
451 struct uml_net
*device
;
452 struct list_head
*ele
;
454 spin_lock(&devices_lock
);
455 list_for_each(ele
, &devices
){
456 device
= list_entry(ele
, struct uml_net
, list
);
457 if(device
->index
== n
)
462 spin_unlock(&devices_lock
);
466 static int eth_parse(char *str
, int *index_out
, char **str_out
)
471 n
= simple_strtoul(str
, &end
, 0);
473 printk(KERN_ERR
"eth_setup: Failed to parse '%s'\n", str
);
477 printk(KERN_ERR
"eth_setup: device %d is negative\n", n
);
483 "eth_setup: expected '=' after device number\n");
488 printk(KERN_ERR
"eth_setup: Device %d already configured\n",
492 if(index_out
) *index_out
= n
;
498 struct list_head list
;
503 /* Filled in at boot time. Will need locking if the transports become
506 struct list_head transports
= LIST_HEAD_INIT(transports
);
508 /* Filled in during early boot */
509 struct list_head eth_cmd_line
= LIST_HEAD_INIT(eth_cmd_line
);
511 static int check_transport(struct transport
*transport
, char *eth
, int n
,
512 void **init_out
, char **mac_out
)
516 len
= strlen(transport
->name
);
517 if(strncmp(eth
, transport
->name
, len
))
523 else if(*eth
!= '\0')
526 *init_out
= kmalloc(transport
->setup_size
, GFP_KERNEL
);
527 if(*init_out
== NULL
)
530 if(!transport
->setup(eth
, mac_out
, *init_out
)){
537 void register_transport(struct transport
*new)
539 struct list_head
*ele
, *next
;
540 struct eth_init
*eth
;
545 list_add(&new->list
, &transports
);
547 list_for_each_safe(ele
, next
, ð_cmd_line
){
548 eth
= list_entry(ele
, struct eth_init
, list
);
549 match
= check_transport(new, eth
->init
, eth
->index
, &init
,
553 else if(init
!= NULL
){
554 eth_configure(eth
->index
, init
, mac
, new);
557 list_del(ð
->list
);
561 static int eth_setup_common(char *str
, int index
)
563 struct list_head
*ele
;
564 struct transport
*transport
;
568 list_for_each(ele
, &transports
){
569 transport
= list_entry(ele
, struct transport
, list
);
570 if(!check_transport(transport
, str
, index
, &init
, &mac
))
573 eth_configure(index
, init
, mac
, transport
);
581 static int eth_setup(char *str
)
583 struct eth_init
*new;
586 err
= eth_parse(str
, &n
, &str
);
590 new = alloc_bootmem(sizeof(*new));
592 printk("eth_init : alloc_bootmem failed\n");
596 INIT_LIST_HEAD(&new->list
);
600 list_add_tail(&new->list
, ð_cmd_line
);
604 __setup("eth", eth_setup
);
605 __uml_help(eth_setup
,
606 "eth[0-9]+=<transport>,<options>\n"
607 " Configure a network device.\n\n"
611 static int eth_init(void)
613 struct list_head
*ele
, *next
;
614 struct eth_init
*eth
;
616 list_for_each_safe(ele
, next
, ð_cmd_line
){
617 eth
= list_entry(ele
, struct eth_init
, list
);
619 if(eth_setup_common(eth
->init
, eth
->index
))
620 list_del(ð
->list
);
625 __initcall(eth_init
);
628 static int net_config(char *str
)
632 err
= eth_parse(str
, &n
, &str
);
635 str
= kstrdup(str
, GFP_KERNEL
);
637 printk(KERN_ERR
"net_config failed to strdup string\n");
640 err
= !eth_setup_common(str
, n
);
646 static int net_id(char **str
, int *start_out
, int *end_out
)
651 n
= simple_strtoul(*str
, &end
, 0);
652 if((*end
!= '\0') || (end
== *str
))
661 static int net_remove(int n
)
663 struct uml_net
*device
;
664 struct net_device
*dev
;
665 struct uml_net_private
*lp
;
667 device
= find_device(n
);
675 if(lp
->remove
!= NULL
) (*lp
->remove
)(&lp
->user
);
676 unregister_netdev(dev
);
677 platform_device_unregister(&device
->pdev
);
679 list_del(&device
->list
);
685 static struct mc_device net_mc
= {
687 .config
= net_config
,
690 .remove
= net_remove
,
693 static int uml_inetaddr_event(struct notifier_block
*this, unsigned long event
,
696 struct in_ifaddr
*ifa
= ptr
;
697 struct net_device
*dev
= ifa
->ifa_dev
->dev
;
698 struct uml_net_private
*lp
;
699 void (*proc
)(unsigned char *, unsigned char *, void *);
700 unsigned char addr_buf
[4], netmask_buf
[4];
702 if(dev
->open
!= uml_net_open
) return(NOTIFY_DONE
);
709 proc
= lp
->add_address
;
712 proc
= lp
->delete_address
;
716 memcpy(addr_buf
, &ifa
->ifa_address
, sizeof(addr_buf
));
717 memcpy(netmask_buf
, &ifa
->ifa_mask
, sizeof(netmask_buf
));
718 (*proc
)(addr_buf
, netmask_buf
, &lp
->user
);
723 struct notifier_block uml_inetaddr_notifier
= {
724 .notifier_call
= uml_inetaddr_event
,
727 static int uml_net_init(void)
729 struct list_head
*ele
;
730 struct uml_net_private
*lp
;
731 struct in_device
*ip
;
732 struct in_ifaddr
*in
;
734 mconsole_register_dev(&net_mc
);
735 register_inetaddr_notifier(¨_inetaddr_notifier
);
737 /* Devices may have been opened already, so the uml_inetaddr_notifier
738 * didn't get a chance to run for them. This fakes it so that
739 * addresses which have already been set up get handled properly.
741 list_for_each(ele
, &opened
){
742 lp
= list_entry(ele
, struct uml_net_private
, list
);
743 ip
= lp
->dev
->ip_ptr
;
744 if(ip
== NULL
) continue;
747 uml_inetaddr_event(NULL
, NETDEV_UP
, in
);
755 __initcall(uml_net_init
);
757 static void close_devices(void)
759 struct list_head
*ele
;
760 struct uml_net_private
*lp
;
762 list_for_each(ele
, &opened
){
763 lp
= list_entry(ele
, struct uml_net_private
, list
);
764 free_irq(lp
->dev
->irq
, lp
->dev
);
765 if((lp
->close
!= NULL
) && (lp
->fd
>= 0))
766 (*lp
->close
)(lp
->fd
, &lp
->user
);
767 if(lp
->remove
!= NULL
) (*lp
->remove
)(&lp
->user
);
771 __uml_exitcall(close_devices
);
773 struct sk_buff
*ether_adjust_skb(struct sk_buff
*skb
, int extra
)
775 if((skb
!= NULL
) && (skb_tailroom(skb
) < extra
)){
776 struct sk_buff
*skb2
;
778 skb2
= skb_copy_expand(skb
, 0, extra
, GFP_ATOMIC
);
782 if(skb
!= NULL
) skb_put(skb
, extra
);
786 void iter_addresses(void *d
, void (*cb
)(unsigned char *, unsigned char *,
790 struct net_device
*dev
= d
;
791 struct in_device
*ip
= dev
->ip_ptr
;
792 struct in_ifaddr
*in
;
793 unsigned char address
[4], netmask
[4];
795 if(ip
== NULL
) return;
798 memcpy(address
, &in
->ifa_address
, sizeof(address
));
799 memcpy(netmask
, &in
->ifa_mask
, sizeof(netmask
));
800 (*cb
)(address
, netmask
, arg
);
805 int dev_netmask(void *d
, void *m
)
807 struct net_device
*dev
= d
;
808 struct in_device
*ip
= dev
->ip_ptr
;
809 struct in_ifaddr
*in
;
810 __be32
*mask_out
= m
;
819 *mask_out
= in
->ifa_mask
;
823 void *get_output_buffer(int *len_out
)
827 ret
= (void *) __get_free_pages(GFP_KERNEL
, 0);
828 if(ret
) *len_out
= PAGE_SIZE
;
833 void free_output_buffer(void *buffer
)
835 free_pages((unsigned long) buffer
, 0);
838 int tap_setup_common(char *str
, char *type
, char **dev_name
, char **mac_out
,
843 remain
= split_if_spec(str
, dev_name
, mac_out
, gate_addr
, NULL
);
845 printk("tap_setup_common - Extra garbage on specification : "
853 unsigned short eth_protocol(struct sk_buff
*skb
)
855 return(eth_type_trans(skb
, skb
->dev
));
859 * Overrides for Emacs so that we follow Linus's tabbing style.
860 * Emacs will notice this stuff at the end of the file and automatically
861 * adjust the settings for this buffer only. This must remain at the end
863 * ---------------------------------------------------------------------------
865 * c-file-style: "linux"