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(void* dev
)
77 dev_close( (struct net_device
*) dev
);
80 irqreturn_t
uml_net_interrupt(int irq
, void *dev_id
)
82 struct net_device
*dev
= dev_id
;
83 struct uml_net_private
*lp
= dev
->priv
;
86 if(!netif_running(dev
))
90 while((err
= uml_net_rx(dev
)) > 0) ;
92 DECLARE_WORK(close_work
, uml_dev_close
, dev
);
94 "Device '%s' read returned %d, shutting it down\n",
96 /* dev_close can't be called in interrupt context, and takes
98 * And dev_close() can be safely called multiple times on the
99 * same device, since it tests for (dev->flags & IFF_UP). So
100 * there's no harm in delaying the device shutdown. */
101 schedule_work(&close_work
);
104 reactivate_fd(lp
->fd
, UM_ETH_IRQ
);
107 spin_unlock(&lp
->lock
);
111 static int uml_net_open(struct net_device
*dev
)
113 struct uml_net_private
*lp
= dev
->priv
;
121 lp
->fd
= (*lp
->open
)(&lp
->user
);
127 err
= um_request_irq(dev
->irq
, lp
->fd
, IRQ_READ
, uml_net_interrupt
,
128 IRQF_DISABLED
| IRQF_SHARED
, dev
->name
, dev
);
130 printk(KERN_ERR
"uml_net_open: failed to get irq(%d)\n", err
);
135 lp
->tl
.data
= (unsigned long) &lp
->user
;
136 netif_start_queue(dev
);
138 /* clear buffer - it can happen that the host side of the interface
139 * is full when we get here. In this case, new data is never queued,
140 * SIGIOs never arrive, and the net never works.
142 while((err
= uml_net_rx(dev
)) > 0) ;
144 spin_lock(&opened_lock
);
145 list_add(&lp
->list
, &opened
);
146 spin_unlock(&opened_lock
);
150 if(lp
->close
!= NULL
) (*lp
->close
)(lp
->fd
, &lp
->user
);
156 static int uml_net_close(struct net_device
*dev
)
158 struct uml_net_private
*lp
= dev
->priv
;
160 netif_stop_queue(dev
);
162 free_irq(dev
->irq
, dev
);
163 if(lp
->close
!= NULL
)
164 (*lp
->close
)(lp
->fd
, &lp
->user
);
167 spin_lock(&opened_lock
);
169 spin_unlock(&opened_lock
);
174 static int uml_net_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
176 struct uml_net_private
*lp
= dev
->priv
;
180 netif_stop_queue(dev
);
182 spin_lock_irqsave(&lp
->lock
, flags
);
184 len
= (*lp
->write
)(lp
->fd
, &skb
, lp
);
186 if(len
== skb
->len
) {
187 lp
->stats
.tx_packets
++;
188 lp
->stats
.tx_bytes
+= skb
->len
;
189 dev
->trans_start
= jiffies
;
190 netif_start_queue(dev
);
192 /* this is normally done in the interrupt when tx finishes */
193 netif_wake_queue(dev
);
196 netif_start_queue(dev
);
197 lp
->stats
.tx_dropped
++;
200 netif_start_queue(dev
);
201 printk(KERN_ERR
"uml_net_start_xmit: failed(%d)\n", len
);
204 spin_unlock_irqrestore(&lp
->lock
, flags
);
211 static struct net_device_stats
*uml_net_get_stats(struct net_device
*dev
)
213 struct uml_net_private
*lp
= dev
->priv
;
217 static void uml_net_set_multicast_list(struct net_device
*dev
)
219 if (dev
->flags
& IFF_PROMISC
) return;
220 else if (dev
->mc_count
) dev
->flags
|= IFF_ALLMULTI
;
221 else dev
->flags
&= ~IFF_ALLMULTI
;
224 static void uml_net_tx_timeout(struct net_device
*dev
)
226 dev
->trans_start
= jiffies
;
227 netif_wake_queue(dev
);
230 static int uml_net_set_mac(struct net_device
*dev
, void *addr
)
232 struct uml_net_private
*lp
= dev
->priv
;
233 struct sockaddr
*hwaddr
= addr
;
235 spin_lock_irq(&lp
->lock
);
236 set_ether_mac(dev
, hwaddr
->sa_data
);
237 spin_unlock_irq(&lp
->lock
);
242 static int uml_net_change_mtu(struct net_device
*dev
, int new_mtu
)
244 struct uml_net_private
*lp
= dev
->priv
;
247 spin_lock_irq(&lp
->lock
);
249 new_mtu
= (*lp
->set_mtu
)(new_mtu
, &lp
->user
);
258 spin_unlock_irq(&lp
->lock
);
262 static void uml_net_get_drvinfo(struct net_device
*dev
,
263 struct ethtool_drvinfo
*info
)
265 strcpy(info
->driver
, DRIVER_NAME
);
266 strcpy(info
->version
, "42");
269 static struct ethtool_ops uml_net_ethtool_ops
= {
270 .get_drvinfo
= uml_net_get_drvinfo
,
271 .get_link
= ethtool_op_get_link
,
274 void uml_net_user_timer_expire(unsigned long _conn
)
277 struct connection
*conn
= (struct connection
*)_conn
;
279 dprintk(KERN_INFO
"uml_net_user_timer_expire [%p]\n", conn
);
284 static void setup_etheraddr(char *str
, unsigned char *addr
)
293 addr
[i
] = simple_strtoul(str
, &end
, 16);
295 ((*end
!= ':') && (*end
!= ',') && (*end
!= '\0'))){
297 "setup_etheraddr: failed to parse '%s' "
298 "as an ethernet address\n", str
);
305 "Attempt to assign a broadcast ethernet address to a "
306 "device disallowed\n");
312 random_ether_addr(addr
);
315 static DEFINE_SPINLOCK(devices_lock
);
316 static LIST_HEAD(devices
);
318 static struct platform_driver uml_net_driver
= {
323 static int driver_registered
;
325 static int eth_configure(int n
, void *init
, char *mac
,
326 struct transport
*transport
)
328 struct uml_net
*device
;
329 struct net_device
*dev
;
330 struct uml_net_private
*lp
;
333 size
= transport
->private_size
+ sizeof(struct uml_net_private
) +
334 sizeof(((struct uml_net_private
*) 0)->user
);
336 device
= kmalloc(sizeof(*device
), GFP_KERNEL
);
337 if (device
== NULL
) {
338 printk(KERN_ERR
"eth_configure failed to allocate uml_net\n");
342 memset(device
, 0, sizeof(*device
));
343 INIT_LIST_HEAD(&device
->list
);
346 spin_lock(&devices_lock
);
347 list_add(&device
->list
, &devices
);
348 spin_unlock(&devices_lock
);
350 setup_etheraddr(mac
, device
->mac
);
352 printk(KERN_INFO
"Netdevice %d ", n
);
353 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
354 device
->mac
[0], device
->mac
[1],
355 device
->mac
[2], device
->mac
[3],
356 device
->mac
[4], device
->mac
[5]);
358 dev
= alloc_etherdev(size
);
360 printk(KERN_ERR
"eth_configure: failed to allocate device\n");
365 /* This points to the transport private data. It's still clear, but we
366 * must memset it to 0 *now*. Let's help the drivers. */
370 if (!driver_registered
) {
371 platform_driver_register(¨_net_driver
);
372 driver_registered
= 1;
375 device
->pdev
.name
= DRIVER_NAME
;
376 platform_device_register(&device
->pdev
);
377 SET_NETDEV_DEV(dev
,&device
->pdev
.dev
);
379 /* If this name ends up conflicting with an existing registered
380 * netdevice, that is OK, register_netdev{,ice}() will notice this
383 snprintf(dev
->name
, sizeof(dev
->name
), "eth%d", n
);
386 (*transport
->kern
->init
)(dev
, init
);
388 dev
->mtu
= transport
->user
->max_packet
;
389 dev
->open
= uml_net_open
;
390 dev
->hard_start_xmit
= uml_net_start_xmit
;
391 dev
->stop
= uml_net_close
;
392 dev
->get_stats
= uml_net_get_stats
;
393 dev
->set_multicast_list
= uml_net_set_multicast_list
;
394 dev
->tx_timeout
= uml_net_tx_timeout
;
395 dev
->set_mac_address
= uml_net_set_mac
;
396 dev
->change_mtu
= uml_net_change_mtu
;
397 dev
->ethtool_ops
= ¨_net_ethtool_ops
;
398 dev
->watchdog_timeo
= (HZ
>> 1);
399 dev
->irq
= UM_ETH_IRQ
;
402 err
= register_netdevice(dev
);
406 /* XXX: should we call ->remove() here? */
411 /* lp.user is the first four bytes of the transport data, which
412 * has already been initialized. This structure assignment will
413 * overwrite that, so we make sure that .user gets overwritten with
414 * what it already has.
417 *lp
= ((struct uml_net_private
)
418 { .list
= LIST_HEAD_INIT(lp
->list
),
421 .mac
= { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
422 .protocol
= transport
->kern
->protocol
,
423 .open
= transport
->user
->open
,
424 .close
= transport
->user
->close
,
425 .remove
= transport
->user
->remove
,
426 .read
= transport
->kern
->read
,
427 .write
= transport
->kern
->write
,
428 .add_address
= transport
->user
->add_address
,
429 .delete_address
= transport
->user
->delete_address
,
430 .set_mtu
= transport
->user
->set_mtu
,
434 spin_lock_init(&lp
->lock
);
435 lp
->tl
.function
= uml_net_user_timer_expire
;
436 memcpy(lp
->mac
, device
->mac
, sizeof(lp
->mac
));
438 if (transport
->user
->init
)
439 (*transport
->user
->init
)(&lp
->user
, dev
);
441 set_ether_mac(dev
, device
->mac
);
446 static struct uml_net
*find_device(int n
)
448 struct uml_net
*device
;
449 struct list_head
*ele
;
451 spin_lock(&devices_lock
);
452 list_for_each(ele
, &devices
){
453 device
= list_entry(ele
, struct uml_net
, list
);
454 if(device
->index
== n
)
459 spin_unlock(&devices_lock
);
463 static int eth_parse(char *str
, int *index_out
, char **str_out
)
468 n
= simple_strtoul(str
, &end
, 0);
470 printk(KERN_ERR
"eth_setup: Failed to parse '%s'\n", str
);
474 printk(KERN_ERR
"eth_setup: device %d is negative\n", n
);
480 "eth_setup: expected '=' after device number\n");
485 printk(KERN_ERR
"eth_setup: Device %d already configured\n",
489 if(index_out
) *index_out
= n
;
495 struct list_head list
;
500 /* Filled in at boot time. Will need locking if the transports become
503 struct list_head transports
= LIST_HEAD_INIT(transports
);
505 /* Filled in during early boot */
506 struct list_head eth_cmd_line
= LIST_HEAD_INIT(eth_cmd_line
);
508 static int check_transport(struct transport
*transport
, char *eth
, int n
,
509 void **init_out
, char **mac_out
)
513 len
= strlen(transport
->name
);
514 if(strncmp(eth
, transport
->name
, len
))
520 else if(*eth
!= '\0')
523 *init_out
= kmalloc(transport
->setup_size
, GFP_KERNEL
);
524 if(*init_out
== NULL
)
527 if(!transport
->setup(eth
, mac_out
, *init_out
)){
534 void register_transport(struct transport
*new)
536 struct list_head
*ele
, *next
;
537 struct eth_init
*eth
;
542 list_add(&new->list
, &transports
);
544 list_for_each_safe(ele
, next
, ð_cmd_line
){
545 eth
= list_entry(ele
, struct eth_init
, list
);
546 match
= check_transport(new, eth
->init
, eth
->index
, &init
,
550 else if(init
!= NULL
){
551 eth_configure(eth
->index
, init
, mac
, new);
554 list_del(ð
->list
);
558 static int eth_setup_common(char *str
, int index
)
560 struct list_head
*ele
;
561 struct transport
*transport
;
565 list_for_each(ele
, &transports
){
566 transport
= list_entry(ele
, struct transport
, list
);
567 if(!check_transport(transport
, str
, index
, &init
, &mac
))
570 eth_configure(index
, init
, mac
, transport
);
578 static int eth_setup(char *str
)
580 struct eth_init
*new;
583 err
= eth_parse(str
, &n
, &str
);
587 new = alloc_bootmem(sizeof(*new));
589 printk("eth_init : alloc_bootmem failed\n");
593 INIT_LIST_HEAD(&new->list
);
597 list_add_tail(&new->list
, ð_cmd_line
);
601 __setup("eth", eth_setup
);
602 __uml_help(eth_setup
,
603 "eth[0-9]+=<transport>,<options>\n"
604 " Configure a network device.\n\n"
608 static int eth_init(void)
610 struct list_head
*ele
, *next
;
611 struct eth_init
*eth
;
613 list_for_each_safe(ele
, next
, ð_cmd_line
){
614 eth
= list_entry(ele
, struct eth_init
, list
);
616 if(eth_setup_common(eth
->init
, eth
->index
))
617 list_del(ð
->list
);
622 __initcall(eth_init
);
625 static int net_config(char *str
)
629 err
= eth_parse(str
, &n
, &str
);
632 str
= kstrdup(str
, GFP_KERNEL
);
634 printk(KERN_ERR
"net_config failed to strdup string\n");
637 err
= !eth_setup_common(str
, n
);
643 static int net_id(char **str
, int *start_out
, int *end_out
)
648 n
= simple_strtoul(*str
, &end
, 0);
649 if((*end
!= '\0') || (end
== *str
))
658 static int net_remove(int n
)
660 struct uml_net
*device
;
661 struct net_device
*dev
;
662 struct uml_net_private
*lp
;
664 device
= find_device(n
);
672 if(lp
->remove
!= NULL
) (*lp
->remove
)(&lp
->user
);
673 unregister_netdev(dev
);
674 platform_device_unregister(&device
->pdev
);
676 list_del(&device
->list
);
682 static struct mc_device net_mc
= {
684 .config
= net_config
,
687 .remove
= net_remove
,
690 static int uml_inetaddr_event(struct notifier_block
*this, unsigned long event
,
693 struct in_ifaddr
*ifa
= ptr
;
694 struct net_device
*dev
= ifa
->ifa_dev
->dev
;
695 struct uml_net_private
*lp
;
696 void (*proc
)(unsigned char *, unsigned char *, void *);
697 unsigned char addr_buf
[4], netmask_buf
[4];
699 if(dev
->open
!= uml_net_open
) return(NOTIFY_DONE
);
706 proc
= lp
->add_address
;
709 proc
= lp
->delete_address
;
713 memcpy(addr_buf
, &ifa
->ifa_address
, sizeof(addr_buf
));
714 memcpy(netmask_buf
, &ifa
->ifa_mask
, sizeof(netmask_buf
));
715 (*proc
)(addr_buf
, netmask_buf
, &lp
->user
);
720 struct notifier_block uml_inetaddr_notifier
= {
721 .notifier_call
= uml_inetaddr_event
,
724 static int uml_net_init(void)
726 struct list_head
*ele
;
727 struct uml_net_private
*lp
;
728 struct in_device
*ip
;
729 struct in_ifaddr
*in
;
731 mconsole_register_dev(&net_mc
);
732 register_inetaddr_notifier(¨_inetaddr_notifier
);
734 /* Devices may have been opened already, so the uml_inetaddr_notifier
735 * didn't get a chance to run for them. This fakes it so that
736 * addresses which have already been set up get handled properly.
738 list_for_each(ele
, &opened
){
739 lp
= list_entry(ele
, struct uml_net_private
, list
);
740 ip
= lp
->dev
->ip_ptr
;
741 if(ip
== NULL
) continue;
744 uml_inetaddr_event(NULL
, NETDEV_UP
, in
);
752 __initcall(uml_net_init
);
754 static void close_devices(void)
756 struct list_head
*ele
;
757 struct uml_net_private
*lp
;
759 list_for_each(ele
, &opened
){
760 lp
= list_entry(ele
, struct uml_net_private
, list
);
761 free_irq(lp
->dev
->irq
, lp
->dev
);
762 if((lp
->close
!= NULL
) && (lp
->fd
>= 0))
763 (*lp
->close
)(lp
->fd
, &lp
->user
);
764 if(lp
->remove
!= NULL
) (*lp
->remove
)(&lp
->user
);
768 __uml_exitcall(close_devices
);
770 struct sk_buff
*ether_adjust_skb(struct sk_buff
*skb
, int extra
)
772 if((skb
!= NULL
) && (skb_tailroom(skb
) < extra
)){
773 struct sk_buff
*skb2
;
775 skb2
= skb_copy_expand(skb
, 0, extra
, GFP_ATOMIC
);
779 if(skb
!= NULL
) skb_put(skb
, extra
);
783 void iter_addresses(void *d
, void (*cb
)(unsigned char *, unsigned char *,
787 struct net_device
*dev
= d
;
788 struct in_device
*ip
= dev
->ip_ptr
;
789 struct in_ifaddr
*in
;
790 unsigned char address
[4], netmask
[4];
792 if(ip
== NULL
) return;
795 memcpy(address
, &in
->ifa_address
, sizeof(address
));
796 memcpy(netmask
, &in
->ifa_mask
, sizeof(netmask
));
797 (*cb
)(address
, netmask
, arg
);
802 int dev_netmask(void *d
, void *m
)
804 struct net_device
*dev
= d
;
805 struct in_device
*ip
= dev
->ip_ptr
;
806 struct in_ifaddr
*in
;
807 __be32
*mask_out
= m
;
816 *mask_out
= in
->ifa_mask
;
820 void *get_output_buffer(int *len_out
)
824 ret
= (void *) __get_free_pages(GFP_KERNEL
, 0);
825 if(ret
) *len_out
= PAGE_SIZE
;
830 void free_output_buffer(void *buffer
)
832 free_pages((unsigned long) buffer
, 0);
835 int tap_setup_common(char *str
, char *type
, char **dev_name
, char **mac_out
,
840 remain
= split_if_spec(str
, dev_name
, mac_out
, gate_addr
, NULL
);
842 printk("tap_setup_common - Extra garbage on specification : "
850 unsigned short eth_protocol(struct sk_buff
*skb
)
852 return(eth_type_trans(skb
, skb
->dev
));
856 * Overrides for Emacs so that we follow Linus's tabbing style.
857 * Emacs will notice this stuff at the end of the file and automatically
858 * adjust the settings for this buffer only. This must remain at the end
860 * ---------------------------------------------------------------------------
862 * c-file-style: "linux"