blob: 6d2a252fc8e8030b85fb76df7378f5302be25ec5 [file] [log] [blame]
Denis Vlasenko83335872007-06-21 07:27:43 +00001/* vi: set sw=4 ts=4: */
Denis Vlasenko14923db2007-06-20 15:23:03 +00002/*
3 * Stripped down version of net-tools for busybox.
4 *
5 * Author: Ignacio Garcia Perez (iggarpe at gmail dot com)
6 *
Denys Vlasenko3d4a8f82010-09-06 16:22:25 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenko14923db2007-06-20 15:23:03 +00008 *
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 Vlasenko47367e12016-11-23 09:05:14 +010015//config:config SLATTACH
Denys Vlasenkob097a842018-12-28 03:20:17 +010016//config: bool "slattach (6.2 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +010017//config: default y
Denys Vlasenko47367e12016-11-23 09:05:14 +010018//config: help
Denys Vlasenko22627462017-08-06 17:14:09 +020019//config: slattach configures serial line as SLIP network interface.
Denys Vlasenko47367e12016-11-23 09:05:14 +010020
21//applet:IF_SLATTACH(APPLET(slattach, BB_DIR_SBIN, BB_SUID_DROP))
Denys Vlasenko22627462017-08-06 17:14:09 +020022/* shouldn't be NOEXEC: may sleep indefinitely */
Denys Vlasenko47367e12016-11-23 09:05:14 +010023
24//kbuild:lib-$(CONFIG_SLATTACH) += slattach.o
Denis Vlasenko14923db2007-06-20 15:23:03 +000025
Pere Orga5bc8c002011-04-11 03:29:49 +020026//usage:#define slattach_trivial_usage
Denys Vlasenko22627462017-08-06 17:14:09 +020027//usage: "[-ehmLF] [-c SCRIPT] [-s BAUD] [-p PROTOCOL] SERIAL_DEVICE"
Pere Orga5bc8c002011-04-11 03:29:49 +020028//usage:#define slattach_full_usage "\n\n"
Denys Vlasenko22627462017-08-06 17:14:09 +020029//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 Orga5bc8c002011-04-11 03:29:49 +020036//usage: "\n -L Enable 3-wire operation"
37//usage: "\n -F Disable RTS/CTS flow control"
38
Denis Vlasenko14923db2007-06-20 15:23:03 +000039#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020040#include "common_bufsiz.h"
Denys Vlasenko0f296a32015-10-14 13:21:01 +020041#include "libiproute/utils.h" /* invarg_1_to_2() */
Denis Vlasenko14923db2007-06-20 15:23:03 +000042
Denis Vlasenko14923db2007-06-20 15:23:03 +000043struct globals {
Denis Vlasenko14923db2007-06-20 15:23:03 +000044 int saved_disc;
45 struct termios saved_state;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010046} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020047#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020048#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenko14923db2007-06-20 15:23:03 +000049
Denys Vlasenko22627462017-08-06 17:14:09 +020050#define serial_fd 3
Denis Vlasenko14923db2007-06-20 15:23:03 +000051
Denys Vlasenko22627462017-08-06 17:14:09 +020052static int tcsetattr_serial_or_warn(struct termios *state)
Denis Vlasenko83335872007-06-21 07:27:43 +000053{
54 int ret;
55
Denys Vlasenko22627462017-08-06 17:14:09 +020056 ret = tcsetattr(serial_fd, TCSANOW, state);
57 if (ret != 0) {
James Byrne69374872019-07-02 11:35:03 +020058 bb_simple_perror_msg("tcsetattr");
Denis Vlasenko83335872007-06-21 07:27:43 +000059 return 1; /* used as exitcode */
60 }
Denys Vlasenko22627462017-08-06 17:14:09 +020061 return ret; /* 0 */
Denis Vlasenko83335872007-06-21 07:27:43 +000062}
63
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000064static void restore_state_and_exit(int exitcode) NORETURN;
Denis Vlasenko14923db2007-06-20 15:23:03 +000065static void restore_state_and_exit(int exitcode)
66{
67 struct termios state;
68
69 /* Restore line discipline */
Denys Vlasenko22627462017-08-06 17:14:09 +020070 if (ioctl_or_warn(serial_fd, TIOCSETD, &G.saved_disc)) {
Denis Vlasenko14923db2007-06-20 15:23:03 +000071 exitcode = 1;
72 }
73
74 /* Hangup */
Denys Vlasenko22627462017-08-06 17:14:09 +020075 memcpy(&state, &G.saved_state, sizeof(state));
Denis Vlasenko14923db2007-06-20 15:23:03 +000076 cfsetispeed(&state, B0);
77 cfsetospeed(&state, B0);
Denys Vlasenko22627462017-08-06 17:14:09 +020078 exitcode |= tcsetattr_serial_or_warn(&state);
Denys Vlasenkoec16c032020-11-29 11:37:34 +010079 sleep1();
Denis Vlasenko14923db2007-06-20 15:23:03 +000080
81 /* Restore line status */
Denys Vlasenko22627462017-08-06 17:14:09 +020082 if (tcsetattr_serial_or_warn(&G.saved_state))
Denis Vlasenko83335872007-06-21 07:27:43 +000083 exit(EXIT_FAILURE);
Denys Vlasenko22627462017-08-06 17:14:09 +020084
Denis Vlasenko14923db2007-06-20 15:23:03 +000085 if (ENABLE_FEATURE_CLEAN_UP)
Denys Vlasenko22627462017-08-06 17:14:09 +020086 close(serial_fd);
Denis Vlasenko14923db2007-06-20 15:23:03 +000087
88 exit(exitcode);
89}
90
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000091static void sig_handler(int signo UNUSED_PARAM)
Denis Vlasenko14923db2007-06-20 15:23:03 +000092{
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +000093 restore_state_and_exit(EXIT_SUCCESS);
Denis Vlasenko14923db2007-06-20 15:23:03 +000094}
95
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000096int slattach_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000097int slattach_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko14923db2007-06-20 15:23:03 +000098{
Denis Vlasenko990d0f62007-07-24 15:54:42 +000099 /* Line discipline code table */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000100 static const char proto_names[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000101 "slip\0" /* 0 */
102 "cslip\0" /* 1 */
103 "slip6\0" /* 2 */
104 "cslip6\0" /* 3 */
105 "adaptive\0" /* 8 */
106 ;
Denys Vlasenko22627462017-08-06 17:14:09 +0200107 static const int int_N_SLIP = N_SLIP;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000108
Denys Vlasenko22627462017-08-06 17:14:09 +0200109 int encap, opt, fd;
Denis Vlasenko14923db2007-06-20 15:23:03 +0000110 struct termios state;
111 const char *proto = "cslip";
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200112 const char *extcmd; /* Command to execute after hangup */
Denis Vlasenko14923db2007-06-20 15:23:03 +0000113 const char *baud_str;
Denys Vlasenko22627462017-08-06 17:14:09 +0200114 int baud_code = baud_code; /* for compiler */
Denis Vlasenko14923db2007-06-20 15:23:03 +0000115
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 Vlasenko83335872007-06-21 07:27:43 +0000124 OPT_F_noflow = 1 << 7
Denis Vlasenko14923db2007-06-20 15:23:03 +0000125 };
126
127 INIT_G();
128
129 /* Parse command line options */
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200130 opt = getopt32(argv, "^" "p:s:c:ehmLF" "\0" "=1",
131 &proto, &baud_str, &extcmd
132 );
Denis Vlasenko14923db2007-06-20 15:23:03 +0000133 /*argc -= optind;*/
134 argv += optind;
135
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000136 encap = index_in_strings(proto_names, proto);
Denis Vlasenko14923db2007-06-20 15:23:03 +0000137 if (encap < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200138 invarg_1_to_2(proto, "protocol");
Denis Vlasenko14923db2007-06-20 15:23:03 +0000139 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 Vlasenko0f296a32015-10-14 13:21:01 +0200146 invarg_1_to_2(baud_str, "baud rate");
Denis Vlasenko14923db2007-06-20 15:23:03 +0000147 }
148
Denys Vlasenko22627462017-08-06 17:14:09 +0200149 /* 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 Byrne69374872019-07-02 11:35:03 +0200161 bb_simple_perror_msg_and_die("tcgetattr");
Denys Vlasenko22627462017-08-06 17:14:09 +0200162 /* Save line discipline */
163 xioctl(serial_fd, TIOCGETD, &G.saved_disc);
164
Denis Vlasenko14923db2007-06-20 15:23:03 +0000165 /* Trap signals in order to restore tty states upon exit */
166 if (!(opt & OPT_e_quit)) {
Denis Vlasenko25591c32008-02-16 22:58:56 +0000167 bb_signals(0
168 + (1 << SIGHUP)
169 + (1 << SIGINT)
170 + (1 << SIGQUIT)
171 + (1 << SIGTERM)
172 , sig_handler);
Denis Vlasenko14923db2007-06-20 15:23:03 +0000173 }
174
Denis Vlasenko83335872007-06-21 07:27:43 +0000175 /* Configure tty */
Denys Vlasenko22627462017-08-06 17:14:09 +0200176 memcpy(&state, &G.saved_state, sizeof(state));
Denis Vlasenko14923db2007-06-20 15:23:03 +0000177 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 Vlasenko22627462017-08-06 17:14:09 +0200181 /*state.c_oflag = 0;*/
182 /*state.c_lflag = 0;*/
Denis Vlasenko14923db2007-06-20 15:23:03 +0000183 state.c_cflag = CS8 | HUPCL | CREAD
184 | ((opt & OPT_L_local) ? CLOCAL : 0)
185 | ((opt & OPT_F_noflow) ? 0 : CRTSCTS);
Denys Vlasenko22627462017-08-06 17:14:09 +0200186 cfsetispeed(&state, cfgetispeed(&G.saved_state));
187 cfsetospeed(&state, cfgetospeed(&G.saved_state));
Denis Vlasenko14923db2007-06-20 15:23:03 +0000188 }
Denis Vlasenko14923db2007-06-20 15:23:03 +0000189 if (opt & OPT_s_baud) {
190 cfsetispeed(&state, baud_code);
191 cfsetospeed(&state, baud_code);
192 }
Denys Vlasenko22627462017-08-06 17:14:09 +0200193 /* 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 Vlasenko14923db2007-06-20 15:23:03 +0000202
203 /* Exit now if option -e was passed */
204 if (opt & OPT_e_quit)
Denys Vlasenko22627462017-08-06 17:14:09 +0200205 return EXIT_SUCCESS;
Denis Vlasenko14923db2007-06-20 15:23:03 +0000206
207 /* If we're not requested to watch, just keep descriptor open
Denis Vlasenko83335872007-06-21 07:27:43 +0000208 * until we are killed */
Denis Vlasenko14923db2007-06-20 15:23:03 +0000209 if (!(opt & OPT_h_watch))
210 while (1)
211 sleep(24*60*60);
212
213 /* Watch line for hangup */
214 while (1) {
Denys Vlasenko22627462017-08-06 17:14:09 +0200215 int modem_stat;
216 if (ioctl(serial_fd, TIOCMGET, &modem_stat))
217 break;
218 if (!(modem_stat & TIOCM_CAR))
219 break;
Denis Vlasenko14923db2007-06-20 15:23:03 +0000220 sleep(15);
221 }
222
Denis Vlasenko14923db2007-06-20 15:23:03 +0000223 /* Execute command on hangup */
224 if (opt & OPT_c_extcmd)
225 system(extcmd);
226
227 /* Restore states and exit */
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000228 restore_state_and_exit(EXIT_SUCCESS);
Denys Vlasenko22627462017-08-06 17:14:09 +0200229 bad:
230 restore_state_and_exit(EXIT_FAILURE);
Denis Vlasenko14923db2007-06-20 15:23:03 +0000231}