2002-12-16 13:33:51 +00:00
|
|
|
/*
|
2003-04-11 09:40:12 +00:00
|
|
|
* OpenGGSN - Gateway GPRS Support Node
|
|
|
|
* Copyright (C) 2002, 2003 Mondru AB.
|
2002-12-16 13:33:51 +00:00
|
|
|
*
|
2003-04-11 09:40:12 +00:00
|
|
|
* The contents of this file may be used under the terms of the GNU
|
|
|
|
* General Public License Version 2, provided that the above copyright
|
|
|
|
* notice and this permission notice is included in all copies or
|
|
|
|
* substantial portions of the software.
|
2002-12-16 13:33:51 +00:00
|
|
|
*
|
2003-04-11 09:40:12 +00:00
|
|
|
* The initial developer of the original code is
|
|
|
|
* Jens Jakobsen <jj@openggsn.org>
|
2002-12-16 13:33:51 +00:00
|
|
|
*
|
2003-04-11 09:40:12 +00:00
|
|
|
* Contributor(s):
|
2002-12-16 13:33:51 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ggsn.c
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
#define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <features.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <asm/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <linux/netlink.h>
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "tun.h"
|
2003-04-11 09:40:12 +00:00
|
|
|
#include "ippool.h"
|
|
|
|
#include "syserr.h"
|
2002-12-16 13:33:51 +00:00
|
|
|
#include "../gtp/pdp.h"
|
|
|
|
#include "../gtp/gtp.h"
|
|
|
|
#include "cmdline.h"
|
|
|
|
|
|
|
|
|
2003-04-11 09:40:12 +00:00
|
|
|
int maxfd = 0; /* For select() */
|
|
|
|
|
|
|
|
struct in_addr listen_;
|
2002-12-16 13:33:51 +00:00
|
|
|
struct in_addr net, mask; /* Network interface */
|
2003-04-11 09:40:12 +00:00
|
|
|
struct in_addr dns1, dns2; /* PCO DNS address */
|
|
|
|
char *ipup, *ipdown; /* Filename of scripts */
|
|
|
|
int debug; /* Print debug output */
|
|
|
|
struct ul255_t pco;
|
|
|
|
struct ul255_t qos;
|
|
|
|
struct ul255_t apn;
|
|
|
|
|
2003-04-11 10:01:41 +00:00
|
|
|
struct gsn_t *gsn; /* GSN instance */
|
2003-04-11 09:40:12 +00:00
|
|
|
struct tun_t *tun; /* TUN instance */
|
|
|
|
struct ippool_t *ippool; /* Pool of IP addresses */
|
2002-12-16 13:33:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Used to write process ID to file. Assume someone else will delete */
|
|
|
|
void log_pid(char *pidfile) {
|
|
|
|
FILE *file;
|
|
|
|
mode_t oldmask;
|
|
|
|
|
|
|
|
oldmask = umask(022);
|
|
|
|
file = fopen(pidfile, "w");
|
|
|
|
umask(oldmask);
|
2003-07-06 17:07:04 +00:00
|
|
|
if(!file) {
|
|
|
|
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
|
|
|
|
"Failed to create process ID file: %s!", pidfile);
|
2002-12-16 13:33:51 +00:00
|
|
|
return;
|
2003-07-06 17:07:04 +00:00
|
|
|
}
|
2002-12-16 13:33:51 +00:00
|
|
|
fprintf(file, "%d\n", getpid());
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int encaps_printf(void *p, void *packet, unsigned len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (debug) {
|
|
|
|
printf("The packet looks like this:\n");
|
|
|
|
for( i=0; i<len; i++) {
|
|
|
|
printf("%02x ", (unsigned char)*(char *)(packet+i));
|
|
|
|
if (!((i+1)%16)) printf("\n");
|
|
|
|
};
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int delete_context(struct pdp_t *pdp) {
|
2003-01-05 17:59:49 +00:00
|
|
|
if (debug) printf("Deleting PDP context\n");
|
2003-10-13 20:33:30 +00:00
|
|
|
if (pdp->peer)
|
|
|
|
ippool_freeip(ippool, (struct ippoolm_t *) pdp->peer);
|
|
|
|
else
|
|
|
|
sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Peer not defined!");
|
2002-12-16 13:33:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-13 20:33:30 +00:00
|
|
|
int create_context_ind(struct pdp_t *pdp) {
|
2003-04-11 09:40:12 +00:00
|
|
|
struct in_addr addr;
|
|
|
|
struct ippoolm_t *member;
|
2002-12-16 13:33:51 +00:00
|
|
|
|
|
|
|
if (debug) printf("Received create PDP context request\n");
|
|
|
|
|
|
|
|
pdp->eua.l=0; /* TODO: Indicates dynamic IP */
|
|
|
|
|
|
|
|
/* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */
|
|
|
|
memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_neg));
|
2003-04-11 09:40:12 +00:00
|
|
|
memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg));
|
2002-12-16 13:33:51 +00:00
|
|
|
|
2003-10-13 20:33:30 +00:00
|
|
|
memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */
|
|
|
|
pdp->qos_neg.l = pdp->qos_req.l;
|
|
|
|
|
2003-04-11 09:40:12 +00:00
|
|
|
if (pdp_euaton(&pdp->eua, &addr)) {
|
|
|
|
addr.s_addr = 0; /* Request dynamic */
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
2003-04-11 09:40:12 +00:00
|
|
|
if (ippool_newip(ippool, &member, &addr)) {
|
2003-10-13 20:33:30 +00:00
|
|
|
gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES);
|
|
|
|
return 0; /* Allready in use, or no more available */
|
2003-01-28 16:13:57 +00:00
|
|
|
}
|
|
|
|
|
2003-04-11 09:40:12 +00:00
|
|
|
pdp_ntoeua(&member->addr, &pdp->eua);
|
2003-04-11 11:51:39 +00:00
|
|
|
pdp->peer = member;
|
2003-04-11 09:40:12 +00:00
|
|
|
pdp->ipif = tun; /* TODO */
|
|
|
|
member->peer = pdp;
|
|
|
|
|
2003-10-13 20:33:30 +00:00
|
|
|
gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ);
|
2003-04-11 09:40:12 +00:00
|
|
|
return 0; /* Success */
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-11 09:40:12 +00:00
|
|
|
/* Callback for receiving messages from tun */
|
|
|
|
int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) {
|
|
|
|
struct ippoolm_t *ipm;
|
|
|
|
struct in_addr dst;
|
|
|
|
struct tun_packet_t *iph = (struct tun_packet_t*) pack;
|
|
|
|
|
|
|
|
dst.s_addr = iph->dst;
|
|
|
|
|
|
|
|
if (ippool_getip(ippool, &ipm, &dst)) {
|
2002-12-16 13:33:51 +00:00
|
|
|
if (debug) printf("Received packet with no destination!!!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2003-04-11 09:40:12 +00:00
|
|
|
|
|
|
|
if (ipm->peer) /* Check if a peer protocol is defined */
|
2003-10-13 20:33:30 +00:00
|
|
|
gtp_data_req(gsn, (struct pdp_t*) ipm->peer, pack, len);
|
2003-04-11 09:40:12 +00:00
|
|
|
return 0;
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) {
|
|
|
|
/* printf("encaps_tun. Packet received: forwarding to tun\n");*/
|
|
|
|
return tun_encaps((struct tun_t*) pdp->ipif, pack, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
/* gengeopt declarations */
|
|
|
|
struct gengetopt_args_info args_info;
|
|
|
|
|
|
|
|
struct hostent *host;
|
|
|
|
|
|
|
|
|
|
|
|
fd_set fds; /* For select() */
|
|
|
|
struct timeval idleTime; /* How long to select() */
|
|
|
|
|
|
|
|
|
|
|
|
int timelimit; /* Number of seconds to be connected */
|
|
|
|
int starttime; /* Time program was started */
|
|
|
|
|
|
|
|
/* open a connection to the syslog daemon */
|
|
|
|
/*openlog(PACKAGE, LOG_PID, LOG_DAEMON);*/
|
|
|
|
openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON);
|
|
|
|
|
|
|
|
if (cmdline_parser (argc, argv, &args_info) != 0)
|
|
|
|
exit(1);
|
|
|
|
if (args_info.debug_flag) {
|
|
|
|
printf("listen: %s\n", args_info.listen_arg);
|
|
|
|
printf("conf: %s\n", args_info.conf_arg);
|
|
|
|
printf("fg: %d\n", args_info.fg_flag);
|
|
|
|
printf("debug: %d\n", args_info.debug_flag);
|
|
|
|
printf("qos: %#08x\n", args_info.qos_arg);
|
|
|
|
printf("apn: %s\n", args_info.apn_arg);
|
|
|
|
printf("net: %s\n", args_info.net_arg);
|
2003-04-11 09:40:12 +00:00
|
|
|
printf("dynip: %s\n", args_info.dynip_arg);
|
|
|
|
printf("statip: %s\n", args_info.statip_arg);
|
2003-01-28 16:13:57 +00:00
|
|
|
printf("ipup: %s\n", args_info.ipup_arg);
|
|
|
|
printf("ipdown: %s\n", args_info.ipdown_arg);
|
2002-12-16 13:33:51 +00:00
|
|
|
printf("pidfile: %s\n", args_info.pidfile_arg);
|
|
|
|
printf("statedir: %s\n", args_info.statedir_arg);
|
|
|
|
printf("timelimit: %d\n", args_info.timelimit_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try out our new parser */
|
|
|
|
|
|
|
|
if (cmdline_parser_configfile (args_info.conf_arg, &args_info, 0) != 0)
|
|
|
|
exit(1);
|
|
|
|
if (args_info.debug_flag) {
|
|
|
|
printf("cmdline_parser_configfile\n");
|
|
|
|
printf("listen: %s\n", args_info.listen_arg);
|
|
|
|
printf("conf: %s\n", args_info.conf_arg);
|
|
|
|
printf("fg: %d\n", args_info.fg_flag);
|
|
|
|
printf("debug: %d\n", args_info.debug_flag);
|
|
|
|
printf("qos: %#08x\n", args_info.qos_arg);
|
|
|
|
printf("apn: %s\n", args_info.apn_arg);
|
|
|
|
printf("net: %s\n", args_info.net_arg);
|
2003-04-11 09:40:12 +00:00
|
|
|
printf("dynip: %s\n", args_info.dynip_arg);
|
|
|
|
printf("statip: %s\n", args_info.statip_arg);
|
2003-01-28 16:13:57 +00:00
|
|
|
printf("ipup: %s\n", args_info.ipup_arg);
|
|
|
|
printf("ipdown: %s\n", args_info.ipdown_arg);
|
2002-12-16 13:33:51 +00:00
|
|
|
printf("pidfile: %s\n", args_info.pidfile_arg);
|
|
|
|
printf("statedir: %s\n", args_info.statedir_arg);
|
|
|
|
printf("timelimit: %d\n", args_info.timelimit_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle each option */
|
|
|
|
|
|
|
|
/* debug */
|
|
|
|
debug = args_info.debug_flag;
|
|
|
|
|
|
|
|
/* listen */
|
|
|
|
/* Do hostname lookup to translate hostname to IP address */
|
2003-07-06 17:07:04 +00:00
|
|
|
/* Any port listening is not possible as a valid address is */
|
|
|
|
/* required for create_pdp_context_response messages */
|
2002-12-16 13:33:51 +00:00
|
|
|
if (args_info.listen_arg) {
|
|
|
|
if (!(host = gethostbyname(args_info.listen_arg))) {
|
2003-07-06 17:07:04 +00:00
|
|
|
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
|
|
|
|
"Invalid listening address: %s!", args_info.listen_arg);
|
2003-04-11 11:51:39 +00:00
|
|
|
exit(1);
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
else {
|
2003-04-11 09:40:12 +00:00
|
|
|
memcpy(&listen_.s_addr, host->h_addr, host->h_length);
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2003-07-06 17:07:04 +00:00
|
|
|
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
|
|
|
|
"Listening address must be specified! "
|
|
|
|
"Please use command line option --listen or "
|
|
|
|
"edit %s configuration file\n", args_info.conf_arg);
|
|
|
|
exit(1);
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
2003-07-06 19:33:18 +00:00
|
|
|
|
2002-12-16 13:33:51 +00:00
|
|
|
/* net */
|
2003-04-11 09:40:12 +00:00
|
|
|
/* Store net as in_addr net and mask */
|
2002-12-16 13:33:51 +00:00
|
|
|
if (args_info.net_arg) {
|
2003-04-11 09:40:12 +00:00
|
|
|
if(ippool_aton(&net, &mask, args_info.net_arg, 0)) {
|
|
|
|
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
|
|
|
|
"Invalid network address: %s!", args_info.net_arg);
|
2003-04-11 11:51:39 +00:00
|
|
|
exit(1);
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
2003-04-11 11:51:39 +00:00
|
|
|
else {
|
|
|
|
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
|
|
|
|
"Network address must be specified: %s!", args_info.net_arg);
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-12-16 13:33:51 +00:00
|
|
|
|
2003-04-11 09:40:12 +00:00
|
|
|
/* dynip */
|
|
|
|
if (!args_info.dynip_arg) {
|
2003-07-06 19:33:18 +00:00
|
|
|
if (ippool_new(&ippool, args_info.net_arg, NULL, 1, 0,
|
2003-04-11 11:51:39 +00:00
|
|
|
IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) {
|
|
|
|
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
|
|
|
|
"Failed to allocate IP pool!");
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-04-11 09:40:12 +00:00
|
|
|
}
|
|
|
|
else {
|
2003-07-06 19:33:18 +00:00
|
|
|
if (ippool_new(&ippool, args_info.dynip_arg, NULL, 1 ,0,
|
2003-04-11 09:40:12 +00:00
|
|
|
IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) {
|
|
|
|
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
|
|
|
|
"Failed to allocate IP pool!");
|
2003-04-11 11:51:39 +00:00
|
|
|
exit(1);
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-11 09:40:12 +00:00
|
|
|
/* DNS1 and DNS2 */
|
|
|
|
dns1.s_addr = 0;
|
|
|
|
if (args_info.pcodns1_arg)
|
|
|
|
inet_aton(args_info.pcodns1_arg, &dns1);
|
|
|
|
|
|
|
|
dns2.s_addr = 0;
|
|
|
|
if (args_info.pcodns2_arg)
|
|
|
|
inet_aton(args_info.pcodns2_arg, &dns2);
|
|
|
|
|
|
|
|
pco.l = 20;
|
|
|
|
pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */
|
|
|
|
pco.v[1] = 0x80; /* IPCP */
|
|
|
|
pco.v[2] = 0x21;
|
|
|
|
pco.v[3] = 0x10; /* Length of contents */
|
|
|
|
pco.v[4] = 0x02; /* ACK */
|
|
|
|
pco.v[5] = 0x00; /* ID: Need to match request */
|
|
|
|
pco.v[6] = 0x00; /* Length */
|
|
|
|
pco.v[7] = 0x10;
|
|
|
|
pco.v[8] = 0x81; /* DNS 1 */
|
|
|
|
pco.v[9] = 0x06;
|
|
|
|
memcpy(&pco.v[10], &dns1, sizeof(dns1));
|
|
|
|
pco.v[14] = 0x83;
|
|
|
|
pco.v[15] = 0x06; /* DNS 2 */
|
|
|
|
memcpy(&pco.v[16], &dns2, sizeof(dns2));
|
|
|
|
|
2003-01-28 16:13:57 +00:00
|
|
|
/* ipup */
|
|
|
|
ipup = args_info.ipup_arg;
|
|
|
|
|
|
|
|
/* ipdown */
|
|
|
|
ipdown = args_info.ipdown_arg;
|
|
|
|
|
2002-12-16 13:33:51 +00:00
|
|
|
/* Timelimit */
|
|
|
|
timelimit = args_info.timelimit_arg;
|
|
|
|
starttime = time(NULL);
|
|
|
|
|
|
|
|
/* qos */
|
|
|
|
qos.l = 3;
|
|
|
|
qos.v[2] = (args_info.qos_arg) & 0xff;
|
|
|
|
qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
|
|
|
|
qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
|
2003-04-11 09:40:12 +00:00
|
|
|
|
2002-12-16 13:33:51 +00:00
|
|
|
/* apn */
|
2003-04-11 09:40:12 +00:00
|
|
|
if (strlen(args_info.apn_arg) > (sizeof(apn.v)-1)) {
|
|
|
|
printf("Invalid APN\n");
|
|
|
|
return -1;
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
|
|
|
apn.l = strlen(args_info.apn_arg) + 1;
|
|
|
|
apn.v[0] = (char) strlen(args_info.apn_arg);
|
2003-04-11 09:40:12 +00:00
|
|
|
strncpy(&apn.v[1], args_info.apn_arg, sizeof(apn.v)-1);
|
2003-07-06 17:07:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* foreground */
|
|
|
|
/* If flag not given run as a daemon */
|
|
|
|
if (!args_info.fg_flag)
|
|
|
|
{
|
|
|
|
closelog();
|
|
|
|
/* Close the standard file descriptors. */
|
|
|
|
/* Is this really needed ? */
|
|
|
|
freopen("/dev/null", "w", stdout);
|
|
|
|
freopen("/dev/null", "w", stderr);
|
|
|
|
freopen("/dev/null", "r", stdin);
|
|
|
|
daemon(0, 0);
|
|
|
|
/* Open log again. This time with new pid */
|
|
|
|
openlog(PACKAGE, LOG_PID, LOG_DAEMON);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pidfile */
|
|
|
|
/* This has to be done after we have our final pid */
|
|
|
|
if (args_info.pidfile_arg) {
|
|
|
|
log_pid(args_info.pidfile_arg);
|
|
|
|
}
|
2003-04-11 09:40:12 +00:00
|
|
|
|
2002-12-16 13:33:51 +00:00
|
|
|
|
|
|
|
if (debug) printf("gtpclient: Initialising GTP tunnel\n");
|
|
|
|
|
2003-07-06 20:53:57 +00:00
|
|
|
if (gtp_new(&gsn, args_info.statedir_arg, &listen_, GTP_MODE_GGSN)) {
|
2003-04-11 09:40:12 +00:00
|
|
|
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
|
|
|
|
"Failed to create gtp");
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-10-13 20:33:30 +00:00
|
|
|
if (gsn->fd0 > maxfd) maxfd = gsn->fd0;
|
|
|
|
if (gsn->fd1c > maxfd) maxfd = gsn->fd1c;
|
|
|
|
if (gsn->fd1u > maxfd) maxfd = gsn->fd1u;
|
2002-12-16 13:33:51 +00:00
|
|
|
|
2003-10-13 20:33:30 +00:00
|
|
|
gtp_set_cb_data_ind(gsn, encaps_tun);
|
2002-12-16 13:33:51 +00:00
|
|
|
gtp_set_cb_delete_context(gsn, delete_context);
|
2003-10-13 20:33:30 +00:00
|
|
|
gtp_set_cb_create_context_ind(gsn, create_context_ind);
|
2003-04-11 09:40:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Create a tunnel interface */
|
|
|
|
if (tun_new((struct tun_t**) &tun)) {
|
|
|
|
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
|
|
|
|
"Failed to create tun");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
tun_setaddr(tun, &net, &net, &mask);
|
|
|
|
tun_set_cb_ind(tun, cb_tun_ind);
|
|
|
|
if (tun->fd > maxfd) maxfd = tun->fd;
|
|
|
|
|
2003-04-11 10:01:41 +00:00
|
|
|
if (ipup) tun_runscript(tun, ipup);
|
2002-12-16 13:33:51 +00:00
|
|
|
|
|
|
|
/******************************************************************/
|
|
|
|
/* Main select loop */
|
|
|
|
/******************************************************************/
|
|
|
|
|
|
|
|
while (((starttime + timelimit) > time(NULL)) || (0 == timelimit)) {
|
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
2003-04-11 09:40:12 +00:00
|
|
|
if (tun) FD_SET(tun->fd, &fds);
|
2003-10-13 20:33:30 +00:00
|
|
|
FD_SET(gsn->fd0, &fds);
|
|
|
|
FD_SET(gsn->fd1c, &fds);
|
|
|
|
FD_SET(gsn->fd1u, &fds);
|
2002-12-16 13:33:51 +00:00
|
|
|
|
|
|
|
gtp_retranstimeout(gsn, &idleTime);
|
|
|
|
switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
|
2003-07-06 17:07:04 +00:00
|
|
|
case -1: /* errno == EINTR : unblocked signal */
|
|
|
|
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
|
|
|
|
"select() returned -1");
|
2002-12-16 13:33:51 +00:00
|
|
|
break;
|
|
|
|
case 0:
|
2003-04-11 09:40:12 +00:00
|
|
|
/* printf("Select returned 0\n"); */
|
2002-12-16 13:33:51 +00:00
|
|
|
gtp_retrans(gsn); /* Only retransmit if nothing else */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-04-11 09:40:12 +00:00
|
|
|
if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
|
|
|
|
tun_decaps(tun) < 0) {
|
2003-07-06 17:07:04 +00:00
|
|
|
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
|
|
|
|
"TUN read failed (fd)=(%d)", tun->fd);
|
2002-12-16 13:33:51 +00:00
|
|
|
}
|
2003-10-13 20:33:30 +00:00
|
|
|
|
|
|
|
if (FD_ISSET(gsn->fd0, &fds))
|
|
|
|
gtp_decaps0(gsn);
|
|
|
|
|
|
|
|
if (FD_ISSET(gsn->fd1c, &fds))
|
|
|
|
gtp_decaps1c(gsn);
|
|
|
|
|
|
|
|
if (FD_ISSET(gsn->fd1u, &fds))
|
|
|
|
gtp_decaps1u(gsn);
|
2002-12-16 13:33:51 +00:00
|
|
|
|
2003-01-28 16:13:57 +00:00
|
|
|
}
|
2002-12-16 13:33:51 +00:00
|
|
|
|
|
|
|
gtp_free(gsn);
|
2003-04-11 09:40:12 +00:00
|
|
|
tun_free(tun);
|
2002-12-16 13:33:51 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|