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