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