Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
| 3 | * Licensed under the GPL. |
| 4 | */ |
| 5 | |
| 6 | #include <linux/if_arp.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 7 | #include <linux/init.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 8 | #include <linux/netdevice.h> |
| 9 | #include <linux/string.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 10 | #include <net_kern.h> |
| 11 | #include <net_user.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "slirp.h" |
| 13 | |
| 14 | struct slirp_init { |
| 15 | struct arg_list_dummy_wrapper argw; /* XXX should be simpler... */ |
| 16 | }; |
| 17 | |
| 18 | void slirp_init(struct net_device *dev, void *data) |
| 19 | { |
| 20 | struct uml_net_private *private; |
| 21 | struct slirp_data *spri; |
| 22 | struct slirp_init *init = data; |
| 23 | int i; |
| 24 | |
Wang Chen | 17c324f | 2008-12-04 15:07:33 -0800 | [diff] [blame] | 25 | private = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | spri = (struct slirp_data *) private->user; |
Paolo 'Blaisorblade' Giarrusso | 854e981 | 2006-02-01 03:06:25 -0800 | [diff] [blame] | 27 | |
| 28 | spri->argw = init->argw; |
| 29 | spri->pid = -1; |
| 30 | spri->slave = -1; |
| 31 | spri->dev = dev; |
| 32 | |
| 33 | slip_proto_init(&spri->slip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | dev->hard_header_len = 0; |
Jeff Dike | be8ad5a | 2007-10-17 19:35:04 -0700 | [diff] [blame] | 36 | dev->header_ops = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | dev->addr_len = 0; |
| 38 | dev->type = ARPHRD_SLIP; |
| 39 | dev->tx_queue_len = 256; |
| 40 | dev->flags = IFF_NOARP; |
| 41 | printk("SLIRP backend - command line:"); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 42 | for (i = 0; spri->argw.argv[i] != NULL; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | printk(" '%s'",spri->argw.argv[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | printk("\n"); |
| 45 | } |
| 46 | |
| 47 | static unsigned short slirp_protocol(struct sk_buff *skbuff) |
| 48 | { |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 49 | return htons(ETH_P_IP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 52 | static int slirp_read(int fd, struct sk_buff *skb, struct uml_net_private *lp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 54 | return slirp_user_read(fd, skb_mac_header(skb), skb->dev->mtu, |
| 55 | (struct slirp_data *) &lp->user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 58 | static int slirp_write(int fd, struct sk_buff *skb, struct uml_net_private *lp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 60 | return slirp_user_write(fd, skb->data, skb->len, |
| 61 | (struct slirp_data *) &lp->user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 64 | const struct net_kern_info slirp_kern_info = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | .init = slirp_init, |
| 66 | .protocol = slirp_protocol, |
| 67 | .read = slirp_read, |
| 68 | .write = slirp_write, |
| 69 | }; |
| 70 | |
| 71 | static int slirp_setup(char *str, char **mac_out, void *data) |
| 72 | { |
| 73 | struct slirp_init *init = data; |
| 74 | int i=0; |
| 75 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 76 | *init = ((struct slirp_init) { .argw = { { "slirp", NULL } } }); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | str = split_if_spec(str, mac_out, NULL); |
| 79 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 80 | if (str == NULL) /* no command line given after MAC addr */ |
| 81 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | do { |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 84 | if (i >= SLIRP_MAX_ARGS - 1) { |
| 85 | printk(KERN_WARNING "slirp_setup: truncating slirp " |
| 86 | "arguments\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | break; |
| 88 | } |
| 89 | init->argw.argv[i++] = str; |
| 90 | while(*str && *str!=',') { |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 91 | if (*str == '_') |
| 92 | *str=' '; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | str++; |
| 94 | } |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 95 | if (*str != ',') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | break; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 97 | *str++ = '\0'; |
| 98 | } while (1); |
| 99 | |
| 100 | init->argw.argv[i] = NULL; |
| 101 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static struct transport slirp_transport = { |
| 105 | .list = LIST_HEAD_INIT(slirp_transport.list), |
| 106 | .name = "slirp", |
| 107 | .setup = slirp_setup, |
| 108 | .user = &slirp_user_info, |
| 109 | .kern = &slirp_kern_info, |
| 110 | .private_size = sizeof(struct slirp_data), |
| 111 | .setup_size = sizeof(struct slirp_init), |
| 112 | }; |
| 113 | |
| 114 | static int register_slirp(void) |
| 115 | { |
| 116 | register_transport(&slirp_transport); |
Jeff Dike | f4c57a7 | 2006-03-31 02:30:10 -0800 | [diff] [blame] | 117 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Jeff Dike | 8210fd2 | 2006-12-06 20:34:55 -0800 | [diff] [blame] | 120 | late_initcall(register_slirp); |