File: ppp.c

package info (click to toggle)
diald 0.99.4-7.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,100 kB
  • ctags: 935
  • sloc: ansic: 7,109; tcl: 977; sh: 880; perl: 306; makefile: 109
file content (350 lines) | stat: -rw-r--r-- 9,279 bytes parent folder | download | duplicates (3)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * ppp.c - ppp and pppd control.
 *
 * Copyright (c) 1994, 1995, 1996 Eric Schenk.
 * Copyright (c) 1999 Mike Jagdis.
 * All rights reserved. Please see the file LICENSE which should be
 * distributed with this software for terms of use.
 */

#include "diald.h"

#if 0
#ifdef PPP_VERSION_2_2_0
#include <linux/ppp_defs.h>
#include <linux/if_ppp.h>
#else
#include <linux/ppp.h>
#endif
#else
#define PPPIOCGUNIT_2_1_2 0x5494
#define PPPIOCGUNIT_2_2_0 _IOR('t', 86, int)
#endif

/* internal flag to shortcut repeated calls to setaddr */
static int rx_count = -1;

void ppp_start()
{
    int pgrpid;
#ifdef DEBIAN_REDIRECT_PPPD_STDERR
    int pipes_fd[2];
    int fdflags;
#endif /* ndef DEBIAN_REDIRECT_PPPD_STDERR */

    block_signals();

    link_iface = -1;
    rx_count = -1;

#ifdef DEBIAN_REDIRECT_PPPD_STDERR
    if (pipe(pipes_fd)!=0) {
      unblock_signals();
      syslog(LOG_ERR, "pipe() failed: %m");
      die(1);
    }
    pppd_stderr_fd=pipes_fd[0];
    if ((fdflags=fcntl(pppd_stderr_fd, F_GETFL))==-1) {
      unblock_signals();
      syslog(LOG_ERR, "fcntl(pppd_stderr_fd, F_GETFL) failed: %m");
      die(1);
    }
    fdflags |= O_NONBLOCK;
    if (fcntl(pppd_stderr_fd, F_SETFL, fdflags)==-1) {
      unblock_signals();
      syslog(LOG_ERR, "fcntl(pppd_stderr_fd, F_SETFL) failed: %m");
      die(1);
    }

#endif /* ndef DEBIAN_REDIRECT_PPPD_STDERR */
    /* Run pppd directly here and set up to wait for the iface */
    link_pid = fork();

    if (link_pid < 0) {
        unblock_signals();
	mon_syslog(LOG_ERR, "failed to fork pppd: %m");
	die(1);
    }

#define ADD_ARG(arg) { argv[i] = arg; argv_len += strlen(argv[i++]) + 1; }
    
    if (link_pid == 0) {
	char **argv = (char **)malloc(sizeof(char *)*(pppd_argc+12));
	int argv_len = 0;
	char buf[24], *argv_buf;
	int i = 0, j;;

        default_sigacts();
        unblock_signals();

	ADD_ARG(path_pppd);
	ADD_ARG("-defaultroute");
	ADD_ARG("-detach");
	if (modem) ADD_ARG("modem");
	if (crtscts) ADD_ARG("crtscts");
	ADD_ARG("mtu");
	sprintf(buf,"%d",mtu);
	ADD_ARG(strdup(buf));
	ADD_ARG("mru");
	sprintf(buf,"%d",mru);
	ADD_ARG(strdup(buf));
	if (netmask) {
	  ADD_ARG("netmask");
	  ADD_ARG(netmask);
	}
	for (j = 0; j < pppd_argc; j++) {
	  ADD_ARG(pppd_argv[j]);
	}
	argv[i++] = 0;

	if ((argv_buf = (char *)malloc(argv_len + 1))) {
	  argv_len = i - 1;
	  *argv_buf = '\0';
	  for (i = 0; i < argv_len; i++) {
	    strcat(argv_buf, argv[i]);
	    strcat(argv_buf, " ");
	  }
	  mon_syslog(LOG_DEBUG, "Running pppd: %s", argv_buf);
	}

	/* make sure pppd is the session leader and has the controlling
         * terminal so it gets the SIGHUP's
         */
	pgrpid = setsid();
        ioctl(modem_fd, TIOCSCTTY, 1);
	tcsetpgrp(modem_fd, pgrpid);

	setreuid(getuid(), getuid());
	setregid(getgid(), getgid());

	if (modem_fd != 0)
	    dup2(modem_fd, 0);
	else
	    fcntl(modem_fd, F_SETFD, 0);
	dup2(modem_fd, 1);
#ifdef DEBIAN_REDIRECT_PPPD_STDERR
	dup2(pipes_fd[1], 2);
	close(pipes_fd[0]);
	close(pipes_fd[1]);
#endif /* ndef DEBIAN_REDIRECT_PPPD_STDERR */

	execv(path_pppd,argv);

	mon_syslog(LOG_ERR, "could not exec %s: %m",path_pppd);
	_exit(99);
	/* NOTREACHED */
    }
#ifdef DEBIAN_REDIRECT_PPPD_STDERR
    close(pipes_fd[1]);
#endif /* ndef DEBIAN_REDIRECT_PPPD_STDERR */
    unblock_signals();
    mon_syslog(LOG_INFO,"Running pppd (pid = %d).",link_pid);
}

/*
 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
 * if it exists.
 */

#define SET_SA_FAMILY(addr, family)                     \
    memset ((char *) &(addr), '\0', sizeof(addr));      \
    addr.sa_family = (family);

/*
 * Find the interface number of the ppp device that pppd opened up and
 * do any routing we might need to do.
 * If pppd has not yet opened the device, then return 0, else return 1.
 */

int ppp_set_addrs()
{
    ulong laddr = 0, raddr = 0, baddr = 0;

    /* Try to get the interface number if we don't know it yet. */
    if (link_iface == -1) {
	 /* Try the pppd-2.2.0 ioctrl first,
	  * Try the pppd-2.1.2 ioctrl if that fails
	  */
   	 if (ioctl(modem_fd, PPPIOCGUNIT_2_2_0, &link_iface) == -1)
   	 	ioctl(modem_fd, PPPIOCGUNIT_2_1_2, &link_iface);
    }

    /* Ok then, see if pppd has upped the interface yet. */
    if (link_iface != -1) {
	struct ifreq   ifr; 

	SET_SA_FAMILY (ifr.ifr_addr,    AF_INET); 
	SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET); 
	SET_SA_FAMILY (ifr.ifr_netmask, AF_INET); 
	sprintf(ifr.ifr_name,"ppp%d",link_iface);
	if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) == -1) {
	   mon_syslog(LOG_ERR,"failed to read ppp interface status: %m");
	   return 0;
	}
	if (!(ifr.ifr_flags & IFF_UP))
	    return 0;	/* interface is not up yet */

	if (route_wait) {
	    /* set the initial rx counter once the link is up */
	    if (rx_count == -1) rx_count = ppp_rx_count();

	    /* check if we got the routing packet yet */
	    if (ppp_rx_count() == rx_count) return 0;
	}

	/* Ok, the interface is up, grab the addresses. */
	if (ioctl(sockfd, SIOCGIFADDR, (caddr_t) &ifr) == -1)
		mon_syslog(LOG_ERR,"failed to get ppp local address: %m");
	else
       	    laddr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;

	if (ioctl(sockfd, SIOCGIFDSTADDR, (caddr_t) &ifr) == -1) 
	   mon_syslog(LOG_ERR,"failed to get ppp remote address: %m");
	else
	   raddr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;

	if (ioctl(sockfd, SIOCGIFBRDADDR, (caddr_t) &ifr) == -1) 
	   mon_syslog(LOG_ERR,"failed to get ppp broadcast address: %m");
	else
	   baddr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;

 	/* Check the MTU, see if it matches what we asked for. If it
	 * doesn't warn the user and adjust the MTU setting.
	 * (NOTE: Adjusting the MTU setting may cause kernel nastyness...)
	 */
	if (ioctl(sockfd, SIOCGIFMTU, (caddr_t) &ifr) == -1) {
	    mon_syslog(LOG_ERR,"failed to get ppp mtu setting: %m");
	} else {
	    if (ifr.ifr_mtu != mtu) {
	        mon_syslog(LOG_WARNING,"PPP negotiated mtu of %d does not match requested setting %d.",ifr.ifr_mtu,mtu);
		mon_syslog(LOG_WARNING,"Attempting to auto adjust mtu.");
		mon_syslog(LOG_WARNING,"Restart diald with mtu set to %d to avoid errors.",ifr.ifr_mtu);
		mtu = ifr.ifr_mtu;
	    }
	}

	if (dynamic_addrs) {
	    /* only do the configuration in dynamic mode. */
	    struct in_addr addr;
	    addr.s_addr = baddr;
	    if (broadcast_ip) free(broadcast_ip);
	    broadcast_ip = strdup(inet_ntoa(addr));
	    addr.s_addr = raddr;
	    if (remote_ip) free(remote_ip);
	    remote_ip = strdup(inet_ntoa(addr));
	    addr.s_addr = laddr;
	    if (local_ip) free(local_ip);
	    local_ip = strdup(inet_ntoa(addr));
	    local_addr = laddr;
	    if (dynamic_addrs > 1) {
		if (orig_broadcast_ip) free(orig_broadcast_ip);
		orig_broadcast_ip = strdup(broadcast_ip);
		if (orig_remote_ip) free(orig_remote_ip);
		orig_remote_ip = strdup(remote_ip);
		if (orig_local_ip) free(orig_local_ip);
		orig_local_ip = strdup(local_ip);
	    }
	    mon_syslog(LOG_INFO, "New addresses: local %s%s%s%s%s",
		local_ip,
		remote_ip ? ", remote " : "",
		remote_ip ? remote_ip : "",
		broadcast_ip ? ", broadcast " : "",
		broadcast_ip ? broadcast_ip : "");
	}

	/* The pppd should have configured the interface but there
	 * may be user or default routes to add :-(.
	 */
	iface_start("link", "ppp", link_iface,
	    local_ip, remote_ip, broadcast_ip, metric);
	if (proxy.stop)
	    proxy.stop(&proxy);

	return 1;
    }
    return 0;
}

int ppp_dead()
{
    return (link_pid == 0);
}

int ppp_route_exists()
{
    char buf[128];
    int device = 0;
    int found = 0;
    FILE *fp;
    sprintf(buf,"%s -n",path_route);
    if ((fp = popen(buf,"r"))==NULL) {
        mon_syslog(LOG_ERR,"Could not run command '%s': %m",buf);
	return 0;	/* assume half dead in this case... */
    }

    while (fgets(buf,128,fp)) {
	if (sscanf(buf,"%*s %*s %*s %*s %*s %*s %*s ppp%d",&device) == 1) {
	    if (device == link_iface) found = 1;
	}
    }
    fclose(fp);
    return found;
}

int ppp_rx_count()
{
    char buf[128];
    int packets = 0;
    FILE *fp;
    sprintf(buf,"%s ppp%d",path_ifconfig,link_iface);
    if ((fp = popen(buf,"r"))==NULL) {
        mon_syslog(LOG_ERR,"Could not run command '%s': %m",buf);
	return 0;	/* assume half dead in this case... */
    }

    while (fgets(buf,128,fp)) {
	if (sscanf(buf," RX packets:%d",&packets) == 1) {
	    break;
	}
    }
    fclose(fp);
    return packets;
}

void ppp_stop()
{
    if (link_pid)
    	if (kill(link_pid,SIGINT) == -1 && errno == ESRCH)
	    link_pid = 0;
}

void ppp_reroute()
{
    /* Restore the original proxy. */
    if (proxy.start && (!blocked || blocked_route))
	proxy.start(&proxy);
    local_addr = (orig_local_ip ? inet_addr(orig_local_ip) : 0);

    if (link_iface != -1) {
    	iface_stop("link", "ppp", link_iface,
	    local_ip, remote_ip, broadcast_ip, metric);
    	iface_down("link", "ppp", link_iface,
	    local_ip, remote_ip, broadcast_ip, metric);
	link_iface = -1;
    }
}

void ppp_kill()
{
    if (link_pid)
    	if (kill(link_pid,SIGKILL) == -1 && errno == ESRCH)
	    link_pid = 0;
}

void ppp_zombie()
{
    /* Either ppp became a zombie or we missed a SIGCHLD signal */

    sig_chld(SIGKILL);	/* try to reap the child */
    link_pid = 0;	/* just in case the reaping failed, forget zombie */
}