Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
| 3 | * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * James Leu (jleu@mindspring.net). |
| 5 | * Copyright (C) 2001 by various other people who didn't put their name here. |
| 6 | * Licensed under the GPL. |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <stdint.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 10 | #include <unistd.h> |
| 11 | #include <errno.h> |
| 12 | #include <sys/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <sys/socket.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <sys/time.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 15 | #include <sys/un.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "daemon.h" |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 17 | #include <net_user.h> |
| 18 | #include <os.h> |
| 19 | #include <um_malloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | enum request_type { REQ_NEW_CONTROL }; |
| 22 | |
| 23 | #define SWITCH_MAGIC 0xfeedface |
| 24 | |
| 25 | struct request_v3 { |
| 26 | uint32_t magic; |
| 27 | uint32_t version; |
| 28 | enum request_type type; |
| 29 | struct sockaddr_un sock; |
| 30 | }; |
| 31 | |
| 32 | static struct sockaddr_un *new_addr(void *name, int len) |
| 33 | { |
| 34 | struct sockaddr_un *sun; |
| 35 | |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 36 | sun = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 37 | if (sun == NULL) { |
| 38 | printk(UM_KERN_ERR "new_addr: allocation of sockaddr_un " |
| 39 | "failed\n"); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 40 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } |
| 42 | sun->sun_family = AF_UNIX; |
| 43 | memcpy(sun->sun_path, name, len); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 44 | return sun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int connect_to_switch(struct daemon_data *pri) |
| 48 | { |
| 49 | struct sockaddr_un *ctl_addr = pri->ctl_addr; |
| 50 | struct sockaddr_un *local_addr = pri->local_addr; |
| 51 | struct sockaddr_un *sun; |
| 52 | struct request_v3 req; |
| 53 | int fd, n, err; |
| 54 | |
| 55 | pri->control = socket(AF_UNIX, SOCK_STREAM, 0); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 56 | if (pri->control < 0) { |
Paolo 'Blaisorblade' Giarrusso | de7b37c | 2007-03-07 20:41:14 -0800 | [diff] [blame] | 57 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 58 | printk(UM_KERN_ERR "daemon_open : control socket failed, " |
| 59 | "errno = %d\n", -err); |
Paolo 'Blaisorblade' Giarrusso | de7b37c | 2007-03-07 20:41:14 -0800 | [diff] [blame] | 60 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 63 | if (connect(pri->control, (struct sockaddr *) ctl_addr, |
| 64 | sizeof(*ctl_addr)) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 66 | printk(UM_KERN_ERR "daemon_open : control connect failed, " |
| 67 | "errno = %d\n", -err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | goto out; |
| 69 | } |
| 70 | |
| 71 | fd = socket(AF_UNIX, SOCK_DGRAM, 0); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 72 | if (fd < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 74 | printk(UM_KERN_ERR "daemon_open : data socket failed, " |
| 75 | "errno = %d\n", -err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | goto out; |
| 77 | } |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 78 | if (bind(fd, (struct sockaddr *) local_addr, sizeof(*local_addr)) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 80 | printk(UM_KERN_ERR "daemon_open : data bind failed, " |
| 81 | "errno = %d\n", -err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | goto out_close; |
| 83 | } |
| 84 | |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 85 | sun = uml_kmalloc(sizeof(struct sockaddr_un), UM_GFP_KERNEL); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 86 | if (sun == NULL) { |
| 87 | printk(UM_KERN_ERR "new_addr: allocation of sockaddr_un " |
| 88 | "failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | err = -ENOMEM; |
| 90 | goto out_close; |
| 91 | } |
| 92 | |
| 93 | req.magic = SWITCH_MAGIC; |
| 94 | req.version = SWITCH_VERSION; |
| 95 | req.type = REQ_NEW_CONTROL; |
| 96 | req.sock = *local_addr; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 97 | n = write(pri->control, &req, sizeof(req)); |
| 98 | if (n != sizeof(req)) { |
| 99 | printk(UM_KERN_ERR "daemon_open : control setup request " |
| 100 | "failed, err = %d\n", -errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | err = -ENOTCONN; |
Paolo 'Blaisorblade' Giarrusso | ba260e2 | 2005-11-13 16:07:14 -0800 | [diff] [blame] | 102 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 105 | n = read(pri->control, sun, sizeof(*sun)); |
| 106 | if (n != sizeof(*sun)) { |
| 107 | printk(UM_KERN_ERR "daemon_open : read of data socket failed, " |
| 108 | "err = %d\n", -errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | err = -ENOTCONN; |
Paolo 'Blaisorblade' Giarrusso | ba260e2 | 2005-11-13 16:07:14 -0800 | [diff] [blame] | 110 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | pri->data_addr = sun; |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 114 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Paolo 'Blaisorblade' Giarrusso | ba260e2 | 2005-11-13 16:07:14 -0800 | [diff] [blame] | 116 | out_free: |
| 117 | kfree(sun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | out_close: |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 119 | close(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | out: |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 121 | close(pri->control); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 122 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 125 | static int daemon_user_init(void *data, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
| 127 | struct daemon_data *pri = data; |
| 128 | struct timeval tv; |
| 129 | struct { |
| 130 | char zero; |
| 131 | int pid; |
| 132 | int usecs; |
| 133 | } name; |
| 134 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 135 | if (!strcmp(pri->sock_type, "unix")) |
| 136 | pri->ctl_addr = new_addr(pri->ctl_sock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | strlen(pri->ctl_sock) + 1); |
| 138 | name.zero = 0; |
| 139 | name.pid = os_getpid(); |
| 140 | gettimeofday(&tv, NULL); |
| 141 | name.usecs = tv.tv_usec; |
| 142 | pri->local_addr = new_addr(&name, sizeof(name)); |
| 143 | pri->dev = dev; |
| 144 | pri->fd = connect_to_switch(pri); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 145 | if (pri->fd < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | kfree(pri->local_addr); |
| 147 | pri->local_addr = NULL; |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 148 | return pri->fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 150 | |
| 151 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static int daemon_open(void *data) |
| 155 | { |
| 156 | struct daemon_data *pri = data; |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 157 | return pri->fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void daemon_remove(void *data) |
| 161 | { |
| 162 | struct daemon_data *pri = data; |
| 163 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 164 | close(pri->fd); |
Paolo 'Blaisorblade' Giarrusso | c42791b | 2006-01-18 17:42:53 -0800 | [diff] [blame] | 165 | pri->fd = -1; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 166 | close(pri->control); |
Paolo 'Blaisorblade' Giarrusso | c42791b | 2006-01-18 17:42:53 -0800 | [diff] [blame] | 167 | pri->control = -1; |
| 168 | |
Jesper Juhl | 41f2148 | 2005-06-25 14:55:20 -0700 | [diff] [blame] | 169 | kfree(pri->data_addr); |
Paolo 'Blaisorblade' Giarrusso | c42791b | 2006-01-18 17:42:53 -0800 | [diff] [blame] | 170 | pri->data_addr = NULL; |
Jesper Juhl | 41f2148 | 2005-06-25 14:55:20 -0700 | [diff] [blame] | 171 | kfree(pri->ctl_addr); |
Paolo 'Blaisorblade' Giarrusso | c42791b | 2006-01-18 17:42:53 -0800 | [diff] [blame] | 172 | pri->ctl_addr = NULL; |
Jesper Juhl | 41f2148 | 2005-06-25 14:55:20 -0700 | [diff] [blame] | 173 | kfree(pri->local_addr); |
Paolo 'Blaisorblade' Giarrusso | c42791b | 2006-01-18 17:42:53 -0800 | [diff] [blame] | 174 | pri->local_addr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | int daemon_user_write(int fd, void *buf, int len, struct daemon_data *pri) |
| 178 | { |
| 179 | struct sockaddr_un *data_addr = pri->data_addr; |
| 180 | |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 181 | return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 184 | const struct net_user_info daemon_user_info = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | .init = daemon_user_init, |
| 186 | .open = daemon_open, |
| 187 | .close = NULL, |
| 188 | .remove = daemon_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | .add_address = NULL, |
| 190 | .delete_address = NULL, |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 191 | .mtu = ETH_MAX_PACKET, |
| 192 | .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | }; |