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