blob: a0ada8fec72ab5cde45462e3a2ed0311a0561e50 [file] [log] [blame]
Jeff Dikecd1ae0e2007-10-16 01:27:29 -07001/*
2 * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL.
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <errno.h>
Jeff Dikecd1ae0e2007-10-16 01:27:29 -07008#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <sys/wait.h>
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070010#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "net_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "os.h"
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070013#include "slirp.h"
14#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Jeff Dikef34d9d22007-05-06 14:51:04 -070016static int slirp_user_init(void *data, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
18 struct slirp_data *pri = data;
19
20 pri->dev = dev;
Jeff Dikef34d9d22007-05-06 14:51:04 -070021 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022}
23
24struct slirp_pre_exec_data {
25 int stdin;
26 int stdout;
27};
28
29static void slirp_pre_exec(void *arg)
30{
31 struct slirp_pre_exec_data *data = arg;
32
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070033 if (data->stdin != -1)
34 dup2(data->stdin, 0);
35 if (data->stdout != -1)
36 dup2(data->stdout, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
39static 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 Dikec4399012007-07-15 23:38:56 -070046 pid = run_helper(slirp_pre_exec, &pe_data, argv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070048 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int slirp_open(void *data)
52{
53 struct slirp_data *pri = data;
Jeff Dikea3c77c62005-06-13 15:52:18 -070054 int fds[2], pid, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jeff Dikea3c77c62005-06-13 15:52:18 -070056 err = os_pipe(fds, 1, 1);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070057 if (err)
58 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jeff Dikea3c77c62005-06-13 15:52:18 -070060 err = slirp_tramp(pri->argw.argv, fds[1]);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070061 if (err < 0) {
62 printk(UM_KERN_ERR "slirp_tramp failed - errno = %d\n", -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -070063 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
Jeff Dikea3c77c62005-06-13 15:52:18 -070065 pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jeff Dikea3c77c62005-06-13 15:52:18 -070067 pri->slave = fds[1];
68 pri->slip.pos = 0;
69 pri->slip.esc = 0;
70 pri->pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070072 return fds[0];
Jeff Dikea3c77c62005-06-13 15:52:18 -070073out:
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070074 close(fds[0]);
75 close(fds[1]);
Jeff Dikea3c77c62005-06-13 15:52:18 -070076 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79static void slirp_close(int fd, void *data)
80{
81 struct slirp_data *pri = data;
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -080082 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070084 close(fd);
85 close(pri->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 pri->slave = -1;
88
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070089 if (pri->pid<1) {
90 printk(UM_KERN_ERR "slirp_close: no child process to shut "
91 "down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return;
93 }
94
95#if 0
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070096 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 Torvalds1da177e2005-04-16 15:20:36 -070099 }
100#endif
Jeff Dike1aa351a2008-02-04 22:31:10 -0800101 err = helper_wait(pri->pid);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800102 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 pri->pid = -1;
106}
107
108int slirp_user_read(int fd, void *buf, int len, struct slirp_data *pri)
109{
Jeff Dikea3c77c62005-06-13 15:52:18 -0700110 return slip_proto_read(fd, buf, len, &pri->slip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113int slirp_user_write(int fd, void *buf, int len, struct slirp_data *pri)
114{
Jeff Dikea3c77c62005-06-13 15:52:18 -0700115 return slip_proto_write(fd, buf, len, &pri->slip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Jeff Dike5e7672e2006-09-27 01:50:33 -0700118const struct net_user_info slirp_user_info = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .init = slirp_user_init,
120 .open = slirp_open,
121 .close = slirp_close,
122 .remove = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .add_address = NULL,
124 .delete_address = NULL,
Jeff Dikeb53f35a2007-10-16 01:27:31 -0700125 .mtu = BUF_SIZE,
126 .max_packet = BUF_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};