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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <errno.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 8 | #include <string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <sys/wait.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 10 | #include "kern_constants.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include "net_user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "os.h" |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 13 | #include "slirp.h" |
| 14 | #include "user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 16 | static int slirp_user_init(void *data, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | { |
| 18 | struct slirp_data *pri = data; |
| 19 | |
| 20 | pri->dev = dev; |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 21 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | struct slirp_pre_exec_data { |
| 25 | int stdin; |
| 26 | int stdout; |
| 27 | }; |
| 28 | |
| 29 | static void slirp_pre_exec(void *arg) |
| 30 | { |
| 31 | struct slirp_pre_exec_data *data = arg; |
| 32 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 33 | if (data->stdin != -1) |
| 34 | dup2(data->stdin, 0); |
| 35 | if (data->stdout != -1) |
| 36 | dup2(data->stdout, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static int slirp_tramp(char **argv, int fd) |
| 40 | { |
| 41 | struct slirp_pre_exec_data pe_data; |
| 42 | int pid; |
| 43 | |
| 44 | pe_data.stdin = fd; |
| 45 | pe_data.stdout = fd; |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 46 | pid = run_helper(slirp_pre_exec, &pe_data, argv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 48 | return pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static int slirp_open(void *data) |
| 52 | { |
| 53 | struct slirp_data *pri = data; |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 54 | int fds[2], pid, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 56 | err = os_pipe(fds, 1, 1); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 57 | if (err) |
| 58 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 60 | err = slirp_tramp(pri->argw.argv, fds[1]); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 61 | if (err < 0) { |
| 62 | printk(UM_KERN_ERR "slirp_tramp failed - errno = %d\n", -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 63 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 65 | pid = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 67 | pri->slave = fds[1]; |
| 68 | pri->slip.pos = 0; |
| 69 | pri->slip.esc = 0; |
| 70 | pri->pid = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 72 | return fds[0]; |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 73 | out: |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 74 | close(fds[0]); |
| 75 | close(fds[1]); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 76 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static void slirp_close(int fd, void *data) |
| 80 | { |
| 81 | struct slirp_data *pri = data; |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 82 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 84 | close(fd); |
| 85 | close(pri->slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | pri->slave = -1; |
| 88 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 89 | if (pri->pid<1) { |
| 90 | printk(UM_KERN_ERR "slirp_close: no child process to shut " |
| 91 | "down\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | return; |
| 93 | } |
| 94 | |
| 95 | #if 0 |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 96 | if (kill(pri->pid, SIGHUP)<0) { |
| 97 | printk(UM_KERN_ERR "slirp_close: sending hangup to %d failed " |
| 98 | "(%d)\n", pri->pid, errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | #endif |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 101 | err = helper_wait(pri->pid); |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 102 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | pri->pid = -1; |
| 106 | } |
| 107 | |
| 108 | int slirp_user_read(int fd, void *buf, int len, struct slirp_data *pri) |
| 109 | { |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 110 | return slip_proto_read(fd, buf, len, &pri->slip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | int slirp_user_write(int fd, void *buf, int len, struct slirp_data *pri) |
| 114 | { |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 115 | return slip_proto_write(fd, buf, len, &pri->slip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 118 | const struct net_user_info slirp_user_info = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .init = slirp_user_init, |
| 120 | .open = slirp_open, |
| 121 | .close = slirp_close, |
| 122 | .remove = NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | .add_address = NULL, |
| 124 | .delete_address = NULL, |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 125 | .mtu = BUF_SIZE, |
| 126 | .max_packet = BUF_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | }; |