Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Stripped down version of net-tools for busybox. |
| 4 | * |
| 5 | * Author: Ignacio Garcia Perez (iggarpe at gmail dot com) |
| 6 | * |
Denys Vlasenko | 3d4a8f8 | 2010-09-06 16:22:25 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 8 | * |
| 9 | * There are some differences from the standard net-tools slattach: |
| 10 | * |
| 11 | * - The -l option is not supported. |
| 12 | * |
| 13 | * - The -F options allows disabling of RTS/CTS flow control. |
| 14 | */ |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 15 | //config:config SLATTACH |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 16 | //config: bool "slattach (6.1 kb)" |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 17 | //config: default y |
| 18 | //config: select PLATFORM_LINUX |
| 19 | //config: help |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 20 | //config: slattach configures serial line as SLIP network interface. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 21 | |
| 22 | //applet:IF_SLATTACH(APPLET(slattach, BB_DIR_SBIN, BB_SUID_DROP)) |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 23 | /* shouldn't be NOEXEC: may sleep indefinitely */ |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 24 | |
| 25 | //kbuild:lib-$(CONFIG_SLATTACH) += slattach.o |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 26 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 27 | //usage:#define slattach_trivial_usage |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 28 | //usage: "[-ehmLF] [-c SCRIPT] [-s BAUD] [-p PROTOCOL] SERIAL_DEVICE" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 29 | //usage:#define slattach_full_usage "\n\n" |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 30 | //usage: "Configure serial line as SLIP network interface\n" |
| 31 | //usage: "\n -p PROT Protocol: slip, cslip (default), slip6, clisp6, adaptive" |
| 32 | //usage: "\n -s BAUD Line speed" |
| 33 | //usage: "\n -e Exit after initialization" |
| 34 | //usage: "\n -h Exit if carrier is lost (else never exits)" |
| 35 | //usage: "\n -c PROG Run PROG on carrier loss" |
| 36 | //usage: "\n -m Do NOT set raw 8bit mode" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 37 | //usage: "\n -L Enable 3-wire operation" |
| 38 | //usage: "\n -F Disable RTS/CTS flow control" |
| 39 | |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 40 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 41 | #include "common_bufsiz.h" |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 42 | #include "libiproute/utils.h" /* invarg_1_to_2() */ |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 44 | struct globals { |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 45 | int saved_disc; |
| 46 | struct termios saved_state; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 47 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 48 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 49 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 50 | |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 51 | #define serial_fd 3 |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 52 | |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 53 | static int tcsetattr_serial_or_warn(struct termios *state) |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 54 | { |
| 55 | int ret; |
| 56 | |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 57 | ret = tcsetattr(serial_fd, TCSANOW, state); |
| 58 | if (ret != 0) { |
| 59 | bb_perror_msg("tcsetattr"); |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 60 | return 1; /* used as exitcode */ |
| 61 | } |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 62 | return ret; /* 0 */ |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 65 | static void restore_state_and_exit(int exitcode) NORETURN; |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 66 | static void restore_state_and_exit(int exitcode) |
| 67 | { |
| 68 | struct termios state; |
| 69 | |
| 70 | /* Restore line discipline */ |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 71 | if (ioctl_or_warn(serial_fd, TIOCSETD, &G.saved_disc)) { |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 72 | exitcode = 1; |
| 73 | } |
| 74 | |
| 75 | /* Hangup */ |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 76 | memcpy(&state, &G.saved_state, sizeof(state)); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 77 | cfsetispeed(&state, B0); |
| 78 | cfsetospeed(&state, B0); |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 79 | exitcode |= tcsetattr_serial_or_warn(&state); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 80 | sleep(1); |
| 81 | |
| 82 | /* Restore line status */ |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 83 | if (tcsetattr_serial_or_warn(&G.saved_state)) |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 84 | exit(EXIT_FAILURE); |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 85 | |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 86 | if (ENABLE_FEATURE_CLEAN_UP) |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 87 | close(serial_fd); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 88 | |
| 89 | exit(exitcode); |
| 90 | } |
| 91 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 92 | static void sig_handler(int signo UNUSED_PARAM) |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 93 | { |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 94 | restore_state_and_exit(EXIT_SUCCESS); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 97 | int slattach_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 98 | int slattach_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 99 | { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 100 | /* Line discipline code table */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 101 | static const char proto_names[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 102 | "slip\0" /* 0 */ |
| 103 | "cslip\0" /* 1 */ |
| 104 | "slip6\0" /* 2 */ |
| 105 | "cslip6\0" /* 3 */ |
| 106 | "adaptive\0" /* 8 */ |
| 107 | ; |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 108 | static const int int_N_SLIP = N_SLIP; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 109 | |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 110 | int encap, opt, fd; |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 111 | struct termios state; |
| 112 | const char *proto = "cslip"; |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 113 | const char *extcmd; /* Command to execute after hangup */ |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 114 | const char *baud_str; |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 115 | int baud_code = baud_code; /* for compiler */ |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 116 | |
| 117 | enum { |
| 118 | OPT_p_proto = 1 << 0, |
| 119 | OPT_s_baud = 1 << 1, |
| 120 | OPT_c_extcmd = 1 << 2, |
| 121 | OPT_e_quit = 1 << 3, |
| 122 | OPT_h_watch = 1 << 4, |
| 123 | OPT_m_nonraw = 1 << 5, |
| 124 | OPT_L_local = 1 << 6, |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 125 | OPT_F_noflow = 1 << 7 |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | INIT_G(); |
| 129 | |
| 130 | /* Parse command line options */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 131 | opt = getopt32(argv, "^" "p:s:c:ehmLF" "\0" "=1", |
| 132 | &proto, &baud_str, &extcmd |
| 133 | ); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 134 | /*argc -= optind;*/ |
| 135 | argv += optind; |
| 136 | |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 137 | encap = index_in_strings(proto_names, proto); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 138 | if (encap < 0) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 139 | invarg_1_to_2(proto, "protocol"); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 140 | if (encap > 3) |
| 141 | encap = 8; |
| 142 | |
| 143 | /* We want to know if the baud rate is valid before we start touching the ttys */ |
| 144 | if (opt & OPT_s_baud) { |
| 145 | baud_code = tty_value_to_baud(xatoi(baud_str)); |
| 146 | if (baud_code < 0) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 147 | invarg_1_to_2(baud_str, "baud rate"); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 150 | /* Open tty */ |
| 151 | fd = open(*argv, O_RDWR | O_NDELAY); |
| 152 | if (fd < 0) { |
| 153 | char *buf = concat_path_file("/dev", *argv); |
| 154 | fd = xopen(buf, O_RDWR | O_NDELAY); |
| 155 | /* maybe if (ENABLE_FEATURE_CLEAN_UP) ?? */ |
| 156 | free(buf); |
| 157 | } |
| 158 | xmove_fd(fd, serial_fd); |
| 159 | |
| 160 | /* Save current tty state */ |
| 161 | if (tcgetattr(serial_fd, &G.saved_state) != 0) |
| 162 | bb_perror_msg_and_die("tcgetattr"); |
| 163 | /* Save line discipline */ |
| 164 | xioctl(serial_fd, TIOCGETD, &G.saved_disc); |
| 165 | |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 166 | /* Trap signals in order to restore tty states upon exit */ |
| 167 | if (!(opt & OPT_e_quit)) { |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 168 | bb_signals(0 |
| 169 | + (1 << SIGHUP) |
| 170 | + (1 << SIGINT) |
| 171 | + (1 << SIGQUIT) |
| 172 | + (1 << SIGTERM) |
| 173 | , sig_handler); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 176 | /* Configure tty */ |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 177 | memcpy(&state, &G.saved_state, sizeof(state)); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 178 | if (!(opt & OPT_m_nonraw)) { /* raw not suppressed */ |
| 179 | memset(&state.c_cc, 0, sizeof(state.c_cc)); |
| 180 | state.c_cc[VMIN] = 1; |
| 181 | state.c_iflag = IGNBRK | IGNPAR; |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 182 | /*state.c_oflag = 0;*/ |
| 183 | /*state.c_lflag = 0;*/ |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 184 | state.c_cflag = CS8 | HUPCL | CREAD |
| 185 | | ((opt & OPT_L_local) ? CLOCAL : 0) |
| 186 | | ((opt & OPT_F_noflow) ? 0 : CRTSCTS); |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 187 | cfsetispeed(&state, cfgetispeed(&G.saved_state)); |
| 188 | cfsetospeed(&state, cfgetospeed(&G.saved_state)); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 189 | } |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 190 | if (opt & OPT_s_baud) { |
| 191 | cfsetispeed(&state, baud_code); |
| 192 | cfsetospeed(&state, baud_code); |
| 193 | } |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 194 | /* Set line status */ |
| 195 | if (tcsetattr_serial_or_warn(&state)) |
| 196 | goto bad; |
| 197 | /* Set line disclipline (N_SLIP always) */ |
| 198 | if (ioctl_or_warn(serial_fd, TIOCSETD, (void*)&int_N_SLIP)) |
| 199 | goto bad; |
| 200 | /* Set encapsulation (SLIP, CSLIP, etc) */ |
| 201 | if (ioctl_or_warn(serial_fd, SIOCSIFENCAP, &encap)) |
| 202 | goto bad; |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 203 | |
| 204 | /* Exit now if option -e was passed */ |
| 205 | if (opt & OPT_e_quit) |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 206 | return EXIT_SUCCESS; |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 207 | |
| 208 | /* If we're not requested to watch, just keep descriptor open |
Denis Vlasenko | 8333587 | 2007-06-21 07:27:43 +0000 | [diff] [blame] | 209 | * until we are killed */ |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 210 | if (!(opt & OPT_h_watch)) |
| 211 | while (1) |
| 212 | sleep(24*60*60); |
| 213 | |
| 214 | /* Watch line for hangup */ |
| 215 | while (1) { |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 216 | int modem_stat; |
| 217 | if (ioctl(serial_fd, TIOCMGET, &modem_stat)) |
| 218 | break; |
| 219 | if (!(modem_stat & TIOCM_CAR)) |
| 220 | break; |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 221 | sleep(15); |
| 222 | } |
| 223 | |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 224 | /* Execute command on hangup */ |
| 225 | if (opt & OPT_c_extcmd) |
| 226 | system(extcmd); |
| 227 | |
| 228 | /* Restore states and exit */ |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 229 | restore_state_and_exit(EXIT_SUCCESS); |
Denys Vlasenko | 2262746 | 2017-08-06 17:14:09 +0200 | [diff] [blame] | 230 | bad: |
| 231 | restore_state_and_exit(EXIT_FAILURE); |
Denis Vlasenko | 14923db | 2007-06-20 15:23:03 +0000 | [diff] [blame] | 232 | } |