1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include "libbb.h"
30#include "common_bufsiz.h"
31#include "libiproute/utils.h"
32
33struct globals {
34 int handle;
35 int saved_disc;
36 struct termios saved_state;
37} FIX_ALIASING;
38#define G (*(struct globals*)bb_common_bufsiz1)
39#define handle (G.handle )
40#define saved_disc (G.saved_disc )
41#define saved_state (G.saved_state )
42#define INIT_G() do { setup_common_bufsiz(); } while (0)
43
44
45
46
47
48
49
50static void save_state(void)
51{
52
53 if (tcgetattr(handle, &saved_state) < 0)
54 bb_perror_msg_and_die("get state");
55
56
57 xioctl(handle, TIOCGETD, &saved_disc);
58}
59
60static int set_termios_state_or_warn(struct termios *state)
61{
62 int ret;
63
64 ret = tcsetattr(handle, TCSANOW, state);
65 if (ret < 0) {
66 bb_perror_msg("set state");
67 return 1;
68 }
69 return 0;
70}
71
72
73
74
75
76
77
78
79
80
81static void restore_state_and_exit(int exitcode) NORETURN;
82static void restore_state_and_exit(int exitcode)
83{
84 struct termios state;
85
86
87 if (ioctl_or_warn(handle, TIOCSETD, &saved_disc) < 0) {
88 exitcode = 1;
89 }
90
91
92 memcpy(&state, &saved_state, sizeof(state));
93 cfsetispeed(&state, B0);
94 cfsetospeed(&state, B0);
95 if (set_termios_state_or_warn(&state))
96 exitcode = 1;
97 sleep(1);
98
99
100 if (set_termios_state_or_warn(&saved_state))
101 exit(EXIT_FAILURE);
102 if (ENABLE_FEATURE_CLEAN_UP)
103 close(handle);
104
105 exit(exitcode);
106}
107
108
109
110
111static void set_state(struct termios *state, int encap)
112{
113 int disc;
114
115
116 if (set_termios_state_or_warn(state))
117 goto bad;
118
119 disc = N_SLIP;
120 if (ioctl_or_warn(handle, TIOCSETD, &disc) < 0) {
121 goto bad;
122 }
123
124
125 if (ioctl_or_warn(handle, SIOCSIFENCAP, &encap) < 0) {
126 bad:
127 restore_state_and_exit(EXIT_FAILURE);
128 }
129}
130
131static void sig_handler(int signo UNUSED_PARAM)
132{
133 restore_state_and_exit(EXIT_SUCCESS);
134}
135
136int slattach_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
137int slattach_main(int argc UNUSED_PARAM, char **argv)
138{
139
140 static const char proto_names[] ALIGN1 =
141 "slip\0"
142 "cslip\0"
143 "slip6\0"
144 "cslip6\0"
145 "adaptive\0"
146 ;
147
148 int i, encap, opt;
149 struct termios state;
150 const char *proto = "cslip";
151 const char *extcmd;
152 const char *baud_str;
153 int baud_code = -1;
154
155 enum {
156 OPT_p_proto = 1 << 0,
157 OPT_s_baud = 1 << 1,
158 OPT_c_extcmd = 1 << 2,
159 OPT_e_quit = 1 << 3,
160 OPT_h_watch = 1 << 4,
161 OPT_m_nonraw = 1 << 5,
162 OPT_L_local = 1 << 6,
163 OPT_F_noflow = 1 << 7
164 };
165
166 INIT_G();
167
168
169 opt = getopt32(argv, "p:s:c:ehmLF", &proto, &baud_str, &extcmd);
170
171 argv += optind;
172
173 if (!*argv)
174 bb_show_usage();
175
176 encap = index_in_strings(proto_names, proto);
177
178 if (encap < 0)
179 invarg_1_to_2(proto, "protocol");
180 if (encap > 3)
181 encap = 8;
182
183
184 if (opt & OPT_s_baud) {
185 baud_code = tty_value_to_baud(xatoi(baud_str));
186 if (baud_code < 0)
187 invarg_1_to_2(baud_str, "baud rate");
188 }
189
190
191 if (!(opt & OPT_e_quit)) {
192 bb_signals(0
193 + (1 << SIGHUP)
194 + (1 << SIGINT)
195 + (1 << SIGQUIT)
196 + (1 << SIGTERM)
197 , sig_handler);
198 }
199
200
201 handle = open(*argv, O_RDWR | O_NDELAY);
202 if (handle < 0) {
203 char *buf = concat_path_file("/dev", *argv);
204 handle = xopen(buf, O_RDWR | O_NDELAY);
205
206 free(buf);
207 }
208
209
210 save_state();
211
212
213 memcpy(&state, &saved_state, sizeof(state));
214 if (!(opt & OPT_m_nonraw)) {
215 memset(&state.c_cc, 0, sizeof(state.c_cc));
216 state.c_cc[VMIN] = 1;
217 state.c_iflag = IGNBRK | IGNPAR;
218 state.c_oflag = 0;
219 state.c_lflag = 0;
220 state.c_cflag = CS8 | HUPCL | CREAD
221 | ((opt & OPT_L_local) ? CLOCAL : 0)
222 | ((opt & OPT_F_noflow) ? 0 : CRTSCTS);
223 cfsetispeed(&state, cfgetispeed(&saved_state));
224 cfsetospeed(&state, cfgetospeed(&saved_state));
225 }
226
227 if (opt & OPT_s_baud) {
228 cfsetispeed(&state, baud_code);
229 cfsetospeed(&state, baud_code);
230 }
231
232 set_state(&state, encap);
233
234
235 if (opt & OPT_e_quit)
236 return 0;
237
238
239
240 if (!(opt & OPT_h_watch))
241 while (1)
242 sleep(24*60*60);
243
244
245 while (1) {
246 if (ioctl(handle, TIOCMGET, &i) < 0 || !(i & TIOCM_CAR))
247 goto no_carrier;
248 sleep(15);
249 }
250
251 no_carrier:
252
253
254 if (opt & OPT_c_extcmd)
255 system(extcmd);
256
257
258 restore_state_and_exit(EXIT_SUCCESS);
259}
260