2015-03-11 14:53:04 +01:00
|
|
|
/****************************************************************************
|
2021-06-16 10:26:22 +02:00
|
|
|
* apps/netutils/pppd/pppd.c
|
2015-03-11 14:53:04 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Max Nekludov. All rights reserved.
|
|
|
|
* Author: Max Nekludov <macscomp@gmail.com>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <nuttx/net/tun.h>
|
|
|
|
|
|
|
|
#include "ppp.h"
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/chat.h"
|
|
|
|
#include "netutils/pppd.h"
|
2015-04-14 15:02:12 +02:00
|
|
|
|
2016-01-31 16:35:42 +01:00
|
|
|
/****************************************************************************
|
2018-11-07 19:43:42 +01:00
|
|
|
* External Functions
|
2016-01-31 16:35:42 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef PPP_ARCH_HAVE_MODEM_RESET
|
2018-11-07 20:37:37 +01:00
|
|
|
void ppp_arch_modem_reset(const char *tty);
|
2015-03-11 14:53:04 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: make_nonblock
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int make_nonblock(int fd)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
|
2015-04-14 14:45:17 +02:00
|
|
|
if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2015-04-14 14:45:17 +02:00
|
|
|
if ((flags = fcntl(fd, F_SETFL, flags | O_NONBLOCK)) < 0)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_alloc
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tun_alloc(char *dev)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
2016-06-16 19:44:22 +02:00
|
|
|
int fd;
|
|
|
|
int errcode;
|
2015-03-11 14:53:04 +01:00
|
|
|
|
2015-04-14 14:45:17 +02:00
|
|
|
if ((fd = open("/dev/tun", O_RDWR)) < 0)
|
2015-09-02 01:28:17 +02:00
|
|
|
{
|
2015-03-11 14:53:04 +01:00
|
|
|
return fd;
|
2015-09-02 01:28:17 +02:00
|
|
|
}
|
2015-03-11 14:53:04 +01:00
|
|
|
|
2018-11-07 19:43:42 +01:00
|
|
|
debug_printf("tun fd:%i\n", fd);
|
2015-03-11 14:53:04 +01:00
|
|
|
|
2016-06-16 19:44:22 +02:00
|
|
|
if ((errcode = make_nonblock(fd)) < 0)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
close(fd);
|
2016-06-16 19:44:22 +02:00
|
|
|
return errcode;
|
2015-03-11 14:53:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
ifr.ifr_flags = IFF_TUN;
|
2015-04-14 14:45:17 +02:00
|
|
|
if (*dev)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
|
|
|
|
}
|
|
|
|
|
2016-06-16 19:44:22 +02:00
|
|
|
if ((errcode = ioctl(fd, TUNSETIFF, (unsigned long)&ifr)) < 0)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
close(fd);
|
2016-06-16 19:44:22 +02:00
|
|
|
return errcode;
|
2015-03-11 14:53:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(dev, ifr.ifr_name);
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: open_tty
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-07 19:43:42 +01:00
|
|
|
static int open_tty(const char *dev)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
int fd;
|
2016-06-16 19:44:22 +02:00
|
|
|
int errcode;
|
2015-03-11 14:53:04 +01:00
|
|
|
|
2015-04-14 14:45:17 +02:00
|
|
|
if ((fd = open(dev, O_RDWR)) < 0)
|
2015-09-02 01:28:17 +02:00
|
|
|
{
|
2015-03-11 14:53:04 +01:00
|
|
|
return fd;
|
2015-09-02 01:28:17 +02:00
|
|
|
}
|
2015-03-11 14:53:04 +01:00
|
|
|
|
2016-06-16 19:44:22 +02:00
|
|
|
if ((errcode = make_nonblock(fd)) < 0)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
close(fd);
|
2016-06-16 19:44:22 +02:00
|
|
|
return errcode;
|
2015-03-11 14:53:04 +01:00
|
|
|
}
|
|
|
|
|
2018-11-07 19:43:42 +01:00
|
|
|
debug_printf("tty fd:%i\n", fd);
|
2015-03-11 14:53:04 +01:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ppp_check_errors
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-07 19:43:42 +01:00
|
|
|
static uint8_t ppp_check_errors(FAR struct ppp_context_s *ctx)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
2018-11-07 19:43:42 +01:00
|
|
|
uint8_t ret = 0;
|
2015-03-11 14:53:04 +01:00
|
|
|
|
|
|
|
/* Check Errors */
|
|
|
|
|
2015-04-14 14:45:17 +02:00
|
|
|
if (ctx->lcp_state & (LCP_TX_TIMEOUT | LCP_RX_TIMEOUT | LCP_TERM_PEER))
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
2015-04-14 14:45:17 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_PPPD_PAP
|
|
|
|
if (ctx->pap_state & (PAP_TX_AUTH_FAIL | PAP_RX_AUTH_FAIL |
|
|
|
|
PAP_TX_TIMEOUT | PAP_RX_TIMEOUT))
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
ret = 2;
|
|
|
|
}
|
2018-11-07 19:43:42 +01:00
|
|
|
#endif
|
2015-03-11 14:53:04 +01:00
|
|
|
|
2015-04-14 14:45:17 +02:00
|
|
|
if (ctx->ipcp_state & (IPCP_TX_TIMEOUT))
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
ret = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ppp_reconnect
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-07 19:43:42 +01:00
|
|
|
void ppp_reconnect(FAR struct ppp_context_s *ctx)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int retry = PPP_MAX_CONNECT;
|
2018-11-07 19:43:42 +01:00
|
|
|
const struct pppd_settings_s *pppd_settings = ctx->settings;
|
|
|
|
netlib_ifdown((char *)ctx->ifname);
|
2015-03-11 14:53:04 +01:00
|
|
|
|
|
|
|
lcp_disconnect(ctx, ++ctx->ppp_id);
|
|
|
|
sleep(1);
|
|
|
|
lcp_disconnect(ctx, ++ctx->ppp_id);
|
|
|
|
sleep(1);
|
2016-01-30 00:00:59 +01:00
|
|
|
write(ctx->ctl.fd, "+++", 3);
|
2015-03-11 14:53:04 +01:00
|
|
|
sleep(2);
|
2016-01-30 00:00:59 +01:00
|
|
|
write(ctx->ctl.fd, "ATE1\r\n", 6);
|
2015-03-11 14:53:04 +01:00
|
|
|
|
2015-04-14 15:02:12 +02:00
|
|
|
if (pppd_settings->disconnect_script)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
2016-01-30 00:00:59 +01:00
|
|
|
ret = chat(&ctx->ctl, pppd_settings->disconnect_script);
|
2015-03-11 14:53:04 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-11-07 19:43:42 +01:00
|
|
|
debug_printf("ppp: disconnect script failed\n");
|
2015-03-11 14:53:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-14 15:02:12 +02:00
|
|
|
if (pppd_settings->connect_script)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2016-01-30 00:00:59 +01:00
|
|
|
ret = chat(&ctx->ctl, pppd_settings->connect_script);
|
2015-03-11 14:53:04 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-11-07 19:43:42 +01:00
|
|
|
debug_printf("ppp: connect script failed\n");
|
2015-03-11 14:53:04 +01:00
|
|
|
--retry;
|
|
|
|
if (retry == 0)
|
|
|
|
{
|
|
|
|
retry = PPP_MAX_CONNECT;
|
2016-01-31 16:35:42 +01:00
|
|
|
#ifdef PPP_ARCH_HAVE_MODEM_RESET
|
2015-04-14 15:02:12 +02:00
|
|
|
ppp_arch_modem_reset(pppd_settings->ttyname);
|
2015-03-11 14:53:04 +01:00
|
|
|
#endif
|
|
|
|
sleep(45);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sleep(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (ret != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ppp_init(ctx);
|
|
|
|
ppp_connect(ctx);
|
|
|
|
|
|
|
|
ctx->ip_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ppp_arch_clock_seconds
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
time_t ppp_arch_clock_seconds(void)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ts.tv_sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ppp_arch_getchar
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-07 19:43:42 +01:00
|
|
|
int ppp_arch_getchar(FAR struct ppp_context_s *ctx, FAR uint8_t * c)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2016-01-30 00:11:06 +01:00
|
|
|
|
2016-01-30 00:00:59 +01:00
|
|
|
ret = read(ctx->ctl.fd, c, 1);
|
2015-03-11 14:53:04 +01:00
|
|
|
return ret == 1 ? ret : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ppp_arch_putchar
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-07 19:43:42 +01:00
|
|
|
int ppp_arch_putchar(FAR struct ppp_context_s *ctx, uint8_t c)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct pollfd fds;
|
|
|
|
|
2016-01-30 00:00:59 +01:00
|
|
|
ret = write(ctx->ctl.fd, &c, 1);
|
2015-03-11 14:53:04 +01:00
|
|
|
if (ret < 0 && errno == EAGAIN)
|
|
|
|
{
|
2016-01-30 00:00:59 +01:00
|
|
|
fds.fd = ctx->ctl.fd;
|
2015-03-11 14:53:04 +01:00
|
|
|
fds.events = POLLOUT;
|
|
|
|
fds.revents = 0;
|
|
|
|
|
|
|
|
ret = poll(&fds, 1, 1000);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
2016-01-30 00:00:59 +01:00
|
|
|
ret = write(ctx->ctl.fd, &c, 1);
|
2015-03-11 14:53:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret == 1 ? ret : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-04-14 15:02:12 +02:00
|
|
|
* Name: pppd
|
2015-03-11 14:53:04 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-07 19:43:42 +01:00
|
|
|
int pppd(const struct pppd_settings_s *pppd_settings)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
|
|
|
struct pollfd fds[2];
|
|
|
|
int ret;
|
2018-11-07 19:43:42 +01:00
|
|
|
FAR struct ppp_context_s *ctx;
|
2015-03-11 14:53:04 +01:00
|
|
|
|
2018-11-07 19:43:42 +01:00
|
|
|
ctx = (struct ppp_context_s *)malloc(sizeof(struct ppp_context_s));
|
2015-03-11 14:53:04 +01:00
|
|
|
memset(ctx, 0, sizeof(struct ppp_context_s));
|
2018-11-07 19:43:42 +01:00
|
|
|
strcpy((char *)ctx->ifname, "ppp%d");
|
2015-03-11 14:53:04 +01:00
|
|
|
|
2015-04-14 15:02:12 +02:00
|
|
|
ctx->settings = pppd_settings;
|
2018-11-07 19:43:42 +01:00
|
|
|
ctx->if_fd = tun_alloc((char *)ctx->ifname);
|
2015-03-11 14:53:04 +01:00
|
|
|
if (ctx->if_fd < 0)
|
|
|
|
{
|
|
|
|
free(ctx);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2016-01-30 00:00:59 +01:00
|
|
|
ctx->ctl.fd = open_tty(pppd_settings->ttyname);
|
|
|
|
if (ctx->ctl.fd < 0)
|
2015-03-11 14:53:04 +01:00
|
|
|
{
|
2018-11-07 19:43:42 +01:00
|
|
|
close(ctx->if_fd);
|
2015-03-11 14:53:04 +01:00
|
|
|
free(ctx);
|
|
|
|
return 2;
|
|
|
|
}
|
2016-01-30 00:11:06 +01:00
|
|
|
|
2018-11-07 19:43:42 +01:00
|
|
|
ctx->ctl.echo = true;
|
2016-01-30 00:00:59 +01:00
|
|
|
ctx->ctl.verbose = true;
|
|
|
|
ctx->ctl.timeout = 30;
|
2015-03-11 14:53:04 +01:00
|
|
|
|
|
|
|
fds[0].fd = ctx->if_fd;
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
|
2016-01-30 00:00:59 +01:00
|
|
|
fds[1].fd = ctx->ctl.fd;
|
2015-03-11 14:53:04 +01:00
|
|
|
fds[1].events = POLLIN;
|
|
|
|
|
|
|
|
ppp_init(ctx);
|
|
|
|
ppp_reconnect(ctx);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
fds[0].revents = fds[1].revents = 0;
|
|
|
|
|
|
|
|
ret = poll(fds, 2, 1000);
|
|
|
|
|
|
|
|
if (ret > 0 && fds[0].revents & POLLIN)
|
|
|
|
{
|
|
|
|
ret = read(ctx->if_fd, ctx->ip_buf, PPP_RX_BUFFER_SIZE);
|
2018-11-07 19:43:42 +01:00
|
|
|
debug_printf("read from tun :%i\n", ret);
|
2015-03-11 14:53:04 +01:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
ctx->ip_len = ret;
|
|
|
|
ppp_send(ctx);
|
|
|
|
ctx->ip_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ppp_poll(ctx);
|
|
|
|
|
|
|
|
if (ppp_check_errors(ctx))
|
|
|
|
{
|
|
|
|
ppp_reconnect(ctx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ctx->ip_len > 0)
|
|
|
|
{
|
|
|
|
ret = write(ctx->if_fd, ctx->ip_buf, ctx->ip_len);
|
|
|
|
ctx->ip_len = 0;
|
|
|
|
|
|
|
|
ret = read(ctx->if_fd, ctx->ip_buf, PPP_RX_BUFFER_SIZE);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
ctx->ip_len = ret;
|
|
|
|
ppp_send(ctx);
|
|
|
|
ctx->ip_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|