blob: 56113597edc922ab35207c55279069501860a9f7 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
jjakoa7cd2492003-04-11 09:40:12 +00002 * OpenGGSN - Gateway GPRS Support Node
3 * Copyright (C) 2002, 2003 Mondru AB.
jjako52c24142002-12-16 13:33:51 +00004 *
jjakoa7cd2492003-04-11 09:40:12 +00005 * The contents of this file may be used under the terms of the GNU
6 * General Public License Version 2, provided that the above copyright
7 * notice and this permission notice is included in all copies or
8 * substantial portions of the software.
jjako52c24142002-12-16 13:33:51 +00009 *
jjakoa7cd2492003-04-11 09:40:12 +000010 * The initial developer of the original code is
11 * Jens Jakobsen <jj@openggsn.org>
jjako52c24142002-12-16 13:33:51 +000012 *
jjakoa7cd2492003-04-11 09:40:12 +000013 * Contributor(s):
jjako52c24142002-12-16 13:33:51 +000014 *
15 */
16
17/* ggsn.c
18 *
19 */
20
21#ifdef __linux__
22#define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */
23#endif
24
25
26#include <syslog.h>
27#include <ctype.h>
28#include <netdb.h>
29#include <signal.h>
30#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
37#include <sys/wait.h>
38#include <sys/stat.h>
jjako0141d202004-01-09 15:19:20 +000039#include <fcntl.h>
jjako52c24142002-12-16 13:33:51 +000040#include <unistd.h>
41
42#include <sys/socket.h>
43#include <sys/ioctl.h>
44#include <net/if.h>
jjako52c24142002-12-16 13:33:51 +000045
46#include <errno.h>
47
jjako52c24142002-12-16 13:33:51 +000048#include <time.h>
49
jjakoff9985c2004-01-16 11:05:22 +000050#include "config.h"
51
jjako52c24142002-12-16 13:33:51 +000052#include "tun.h"
jjakoa7cd2492003-04-11 09:40:12 +000053#include "ippool.h"
54#include "syserr.h"
jjako52c24142002-12-16 13:33:51 +000055#include "../gtp/pdp.h"
56#include "../gtp/gtp.h"
57#include "cmdline.h"
58
59
jjakoa7cd2492003-04-11 09:40:12 +000060int maxfd = 0; /* For select() */
jjakoa7cd2492003-04-11 09:40:12 +000061
62struct in_addr listen_;
jjakoc6762cf2004-04-28 14:52:58 +000063struct in_addr netaddr, destaddr, net, mask; /* Network interface */
jjakoa7cd2492003-04-11 09:40:12 +000064struct in_addr dns1, dns2; /* PCO DNS address */
65char *ipup, *ipdown; /* Filename of scripts */
66int debug; /* Print debug output */
67struct ul255_t pco;
68struct ul255_t qos;
69struct ul255_t apn;
70
jjako9c7ff082003-04-11 10:01:41 +000071struct gsn_t *gsn; /* GSN instance */
jjakoa7cd2492003-04-11 09:40:12 +000072struct tun_t *tun; /* TUN instance */
73struct ippool_t *ippool; /* Pool of IP addresses */
jjako52c24142002-12-16 13:33:51 +000074
75
76/* Used to write process ID to file. Assume someone else will delete */
77void log_pid(char *pidfile) {
78 FILE *file;
79 mode_t oldmask;
80
81 oldmask = umask(022);
82 file = fopen(pidfile, "w");
83 umask(oldmask);
jjakoe0149782003-07-06 17:07:04 +000084 if(!file) {
85 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
86 "Failed to create process ID file: %s!", pidfile);
jjako52c24142002-12-16 13:33:51 +000087 return;
jjakoe0149782003-07-06 17:07:04 +000088 }
jjako0141d202004-01-09 15:19:20 +000089 fprintf(file, "%d\n", (int) getpid());
jjako52c24142002-12-16 13:33:51 +000090 fclose(file);
91}
92
jjako1f158642004-02-05 20:39:57 +000093#if defined(_sun__)
jjako0141d202004-01-09 15:19:20 +000094int daemon(int nochdir, int noclose) {
95 int fd;
96
97 switch (fork()) {
98 case -1:
99 return (-1);
100 case 0:
101 break;
102 default:
103 _exit(0);
104 }
105
106 if (setsid() == -1)
107 return (-1);
108
109 if (!nochdir) chdir("/");
110
111 if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
112 dup2(fd, STDIN_FILENO);
113 dup2(fd, STDOUT_FILENO);
114 dup2(fd, STDERR_FILENO);
115 if (fd > 2) close (fd);
116 }
117 return (0);
118}
119#endif
120
jjako52c24142002-12-16 13:33:51 +0000121
122int encaps_printf(void *p, void *packet, unsigned len)
123{
124 int i;
125 if (debug) {
126 printf("The packet looks like this:\n");
127 for( i=0; i<len; i++) {
128 printf("%02x ", (unsigned char)*(char *)(packet+i));
129 if (!((i+1)%16)) printf("\n");
130 };
131 printf("\n");
132 }
133 return 0;
134}
135
jjako52c24142002-12-16 13:33:51 +0000136int delete_context(struct pdp_t *pdp) {
jjako49014712003-01-05 17:59:49 +0000137 if (debug) printf("Deleting PDP context\n");
jjako08d331d2003-10-13 20:33:30 +0000138 if (pdp->peer)
139 ippool_freeip(ippool, (struct ippoolm_t *) pdp->peer);
140 else
141 sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Peer not defined!");
jjako52c24142002-12-16 13:33:51 +0000142 return 0;
143}
144
145
jjako08d331d2003-10-13 20:33:30 +0000146int create_context_ind(struct pdp_t *pdp) {
jjakoa7cd2492003-04-11 09:40:12 +0000147 struct in_addr addr;
148 struct ippoolm_t *member;
jjako52c24142002-12-16 13:33:51 +0000149
150 if (debug) printf("Received create PDP context request\n");
151
152 pdp->eua.l=0; /* TODO: Indicates dynamic IP */
153
154 /* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */
155 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_neg));
jjakoa7cd2492003-04-11 09:40:12 +0000156 memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg));
jjako52c24142002-12-16 13:33:51 +0000157
jjako08d331d2003-10-13 20:33:30 +0000158 memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */
159 pdp->qos_neg.l = pdp->qos_req.l;
160
jjakoa7cd2492003-04-11 09:40:12 +0000161 if (pdp_euaton(&pdp->eua, &addr)) {
162 addr.s_addr = 0; /* Request dynamic */
163 }
164
165 if (ippool_newip(ippool, &member, &addr)) {
jjako08d331d2003-10-13 20:33:30 +0000166 gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES);
167 return 0; /* Allready in use, or no more available */
jjakoa7cd2492003-04-11 09:40:12 +0000168 }
169
170 pdp_ntoeua(&member->addr, &pdp->eua);
jjakoa7c33812003-04-11 11:51:39 +0000171 pdp->peer = member;
jjakoa7cd2492003-04-11 09:40:12 +0000172 pdp->ipif = tun; /* TODO */
173 member->peer = pdp;
jjako52c24142002-12-16 13:33:51 +0000174
jjako08d331d2003-10-13 20:33:30 +0000175 gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ);
jjako52c24142002-12-16 13:33:51 +0000176 return 0; /* Success */
177}
178
179
jjakoa7cd2492003-04-11 09:40:12 +0000180/* Callback for receiving messages from tun */
181int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) {
182 struct ippoolm_t *ipm;
183 struct in_addr dst;
184 struct tun_packet_t *iph = (struct tun_packet_t*) pack;
185
186 dst.s_addr = iph->dst;
jjakoc6762cf2004-04-28 14:52:58 +0000187
188 if (debug) printf("Received packet from tun!\n");
189
jjakoa7cd2492003-04-11 09:40:12 +0000190 if (ippool_getip(ippool, &ipm, &dst)) {
jjako52c24142002-12-16 13:33:51 +0000191 if (debug) printf("Received packet with no destination!!!\n");
192 return 0;
193 }
jjakoa7cd2492003-04-11 09:40:12 +0000194
195 if (ipm->peer) /* Check if a peer protocol is defined */
jjako08d331d2003-10-13 20:33:30 +0000196 gtp_data_req(gsn, (struct pdp_t*) ipm->peer, pack, len);
jjakoa7cd2492003-04-11 09:40:12 +0000197 return 0;
jjako52c24142002-12-16 13:33:51 +0000198}
199
jjako52c24142002-12-16 13:33:51 +0000200int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) {
jjakoc6762cf2004-04-28 14:52:58 +0000201 if (debug) printf("encaps_tun. Packet received: forwarding to tun\n");
jjako52c24142002-12-16 13:33:51 +0000202 return tun_encaps((struct tun_t*) pdp->ipif, pack, len);
203}
204
205
206int main(int argc, char **argv)
207{
208 /* gengeopt declarations */
209 struct gengetopt_args_info args_info;
210
211 struct hostent *host;
212
jjako52c24142002-12-16 13:33:51 +0000213
jjako52c24142002-12-16 13:33:51 +0000214 fd_set fds; /* For select() */
215 struct timeval idleTime; /* How long to select() */
jjako52c24142002-12-16 13:33:51 +0000216
jjako52c24142002-12-16 13:33:51 +0000217
218 int timelimit; /* Number of seconds to be connected */
219 int starttime; /* Time program was started */
220
221 /* open a connection to the syslog daemon */
222 /*openlog(PACKAGE, LOG_PID, LOG_DAEMON);*/
jjako0141d202004-01-09 15:19:20 +0000223
224 /* TODO: Only use LOG__PERROR for linux */
225#ifdef __linux__
jjako52c24142002-12-16 13:33:51 +0000226 openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON);
jjako0141d202004-01-09 15:19:20 +0000227#else
228 openlog(PACKAGE, (LOG_PID), LOG_DAEMON);
229#endif
230
jjako52c24142002-12-16 13:33:51 +0000231
232 if (cmdline_parser (argc, argv, &args_info) != 0)
233 exit(1);
234 if (args_info.debug_flag) {
235 printf("listen: %s\n", args_info.listen_arg);
jjakoc6762cf2004-04-28 14:52:58 +0000236 if (args_info.conf_arg) printf("conf: %s\n", args_info.conf_arg);
jjako52c24142002-12-16 13:33:51 +0000237 printf("fg: %d\n", args_info.fg_flag);
238 printf("debug: %d\n", args_info.debug_flag);
239 printf("qos: %#08x\n", args_info.qos_arg);
jjakoc6762cf2004-04-28 14:52:58 +0000240 if (args_info.apn_arg) printf("apn: %s\n", args_info.apn_arg);
241 if (args_info.net_arg) printf("net: %s\n", args_info.net_arg);
242 if (args_info.dynip_arg) printf("dynip: %s\n", args_info.dynip_arg);
243 if (args_info.statip_arg) printf("statip: %s\n", args_info.statip_arg);
244 if (args_info.ipup_arg) printf("ipup: %s\n", args_info.ipup_arg);
245 if (args_info.ipdown_arg) printf("ipdown: %s\n", args_info.ipdown_arg);
246 if (args_info.pidfile_arg) printf("pidfile: %s\n", args_info.pidfile_arg);
247 if (args_info.statedir_arg) printf("statedir: %s\n", args_info.statedir_arg);
jjako52c24142002-12-16 13:33:51 +0000248 printf("timelimit: %d\n", args_info.timelimit_arg);
249 }
250
251 /* Try out our new parser */
252
253 if (cmdline_parser_configfile (args_info.conf_arg, &args_info, 0) != 0)
254 exit(1);
255 if (args_info.debug_flag) {
256 printf("cmdline_parser_configfile\n");
257 printf("listen: %s\n", args_info.listen_arg);
258 printf("conf: %s\n", args_info.conf_arg);
259 printf("fg: %d\n", args_info.fg_flag);
260 printf("debug: %d\n", args_info.debug_flag);
261 printf("qos: %#08x\n", args_info.qos_arg);
jjakoc6762cf2004-04-28 14:52:58 +0000262 if (args_info.apn_arg) printf("apn: %s\n", args_info.apn_arg);
263 if (args_info.net_arg) printf("net: %s\n", args_info.net_arg);
264 if (args_info.dynip_arg) printf("dynip: %s\n", args_info.dynip_arg);
265 if (args_info.statip_arg) printf("statip: %s\n", args_info.statip_arg);
266 if (args_info.ipup_arg) printf("ipup: %s\n", args_info.ipup_arg);
267 if (args_info.ipdown_arg) printf("ipdown: %s\n", args_info.ipdown_arg);
268 if (args_info.pidfile_arg) printf("pidfile: %s\n", args_info.pidfile_arg);
269 if (args_info.statedir_arg) printf("statedir: %s\n", args_info.statedir_arg);
jjako52c24142002-12-16 13:33:51 +0000270 printf("timelimit: %d\n", args_info.timelimit_arg);
271 }
272
273 /* Handle each option */
274
jjako52c24142002-12-16 13:33:51 +0000275 /* debug */
276 debug = args_info.debug_flag;
277
jjako52c24142002-12-16 13:33:51 +0000278 /* listen */
jjako52c24142002-12-16 13:33:51 +0000279 /* Do hostname lookup to translate hostname to IP address */
jjakoe0149782003-07-06 17:07:04 +0000280 /* Any port listening is not possible as a valid address is */
281 /* required for create_pdp_context_response messages */
jjako52c24142002-12-16 13:33:51 +0000282 if (args_info.listen_arg) {
283 if (!(host = gethostbyname(args_info.listen_arg))) {
jjakoe0149782003-07-06 17:07:04 +0000284 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
285 "Invalid listening address: %s!", args_info.listen_arg);
jjakoa7c33812003-04-11 11:51:39 +0000286 exit(1);
jjako52c24142002-12-16 13:33:51 +0000287 }
288 else {
jjakoa7cd2492003-04-11 09:40:12 +0000289 memcpy(&listen_.s_addr, host->h_addr, host->h_length);
jjako52c24142002-12-16 13:33:51 +0000290 }
291 }
292 else {
jjakoe0149782003-07-06 17:07:04 +0000293 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
294 "Listening address must be specified! "
295 "Please use command line option --listen or "
296 "edit %s configuration file\n", args_info.conf_arg);
297 exit(1);
jjako52c24142002-12-16 13:33:51 +0000298 }
299
jjako88c22162003-07-06 19:33:18 +0000300
jjako52c24142002-12-16 13:33:51 +0000301 /* net */
jjakoa7cd2492003-04-11 09:40:12 +0000302 /* Store net as in_addr net and mask */
jjako52c24142002-12-16 13:33:51 +0000303 if (args_info.net_arg) {
jjakoa7cd2492003-04-11 09:40:12 +0000304 if(ippool_aton(&net, &mask, args_info.net_arg, 0)) {
305 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
306 "Invalid network address: %s!", args_info.net_arg);
jjakoa7c33812003-04-11 11:51:39 +0000307 exit(1);
jjako52c24142002-12-16 13:33:51 +0000308 }
jjakoc6762cf2004-04-28 14:52:58 +0000309 netaddr.s_addr = htonl(ntohl(net.s_addr) + 1);
310 destaddr.s_addr = htonl(ntohl(net.s_addr) + 1);
jjako52c24142002-12-16 13:33:51 +0000311 }
jjakoa7c33812003-04-11 11:51:39 +0000312 else {
313 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
314 "Network address must be specified: %s!", args_info.net_arg);
315 exit(1);
316 }
jjako52c24142002-12-16 13:33:51 +0000317
jjakoa7cd2492003-04-11 09:40:12 +0000318 /* dynip */
319 if (!args_info.dynip_arg) {
jjako88c22162003-07-06 19:33:18 +0000320 if (ippool_new(&ippool, args_info.net_arg, NULL, 1, 0,
jjakoc6762cf2004-04-28 14:52:58 +0000321 IPPOOL_NONETWORK | IPPOOL_NOGATEWAY | IPPOOL_NOBROADCAST)) {
jjakoa7c33812003-04-11 11:51:39 +0000322 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
323 "Failed to allocate IP pool!");
324 exit(1);
325 }
jjakoa7cd2492003-04-11 09:40:12 +0000326 }
327 else {
jjako88c22162003-07-06 19:33:18 +0000328 if (ippool_new(&ippool, args_info.dynip_arg, NULL, 1 ,0,
jjakoc6762cf2004-04-28 14:52:58 +0000329 IPPOOL_NONETWORK | IPPOOL_NOGATEWAY | IPPOOL_NOBROADCAST)) {
jjakoa7cd2492003-04-11 09:40:12 +0000330 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
331 "Failed to allocate IP pool!");
jjakoa7c33812003-04-11 11:51:39 +0000332 exit(1);
jjako52c24142002-12-16 13:33:51 +0000333 }
334 }
335
jjakoa7cd2492003-04-11 09:40:12 +0000336 /* DNS1 and DNS2 */
jjakoff9985c2004-01-16 11:05:22 +0000337#ifdef HAVE_INET_ATON
jjakoa7cd2492003-04-11 09:40:12 +0000338 dns1.s_addr = 0;
jjako76032b92004-01-14 06:22:08 +0000339 if (args_info.pcodns1_arg) {
jjako1d3db972004-01-16 09:56:56 +0000340 if (0 == inet_aton(args_info.pcodns1_arg, &dns1)) {
jjako76032b92004-01-14 06:22:08 +0000341 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
342 "Failed to convert pcodns1!");
343 exit(1);
344 }
345 }
jjakoa7cd2492003-04-11 09:40:12 +0000346 dns2.s_addr = 0;
jjako76032b92004-01-14 06:22:08 +0000347 if (args_info.pcodns2_arg) {
jjako1d3db972004-01-16 09:56:56 +0000348 if (0 == inet_aton(args_info.pcodns2_arg, &dns2)) {
jjako76032b92004-01-14 06:22:08 +0000349 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
350 "Failed to convert pcodns2!");
351 exit(1);
352 }
353 }
jjako1d3db972004-01-16 09:56:56 +0000354#else
jjako1d3db972004-01-16 09:56:56 +0000355 dns1.s_addr = 0;
356 if (args_info.pcodns1_arg) {
jjakoff9985c2004-01-16 11:05:22 +0000357 dns1.s_addr = inet_addr(args_info.pcodns1_arg);
358 if (dns1.s_addr == -1) {
jjako1d3db972004-01-16 09:56:56 +0000359 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
360 "Failed to convert pcodns1!");
361 exit(1);
362 }
363 }
364 dns2.s_addr = 0;
365 if (args_info.pcodns2_arg) {
jjakoff9985c2004-01-16 11:05:22 +0000366 dns2.s_addr = inet_addr(args_info.pcodns2_arg);
367 if (dns2.s_addr == -1) {
jjako1d3db972004-01-16 09:56:56 +0000368 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
369 "Failed to convert pcodns2!");
370 exit(1);
371 }
372 }
jjako1d3db972004-01-16 09:56:56 +0000373#endif
374
jjakoa7cd2492003-04-11 09:40:12 +0000375
376 pco.l = 20;
377 pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */
378 pco.v[1] = 0x80; /* IPCP */
379 pco.v[2] = 0x21;
380 pco.v[3] = 0x10; /* Length of contents */
381 pco.v[4] = 0x02; /* ACK */
382 pco.v[5] = 0x00; /* ID: Need to match request */
383 pco.v[6] = 0x00; /* Length */
384 pco.v[7] = 0x10;
385 pco.v[8] = 0x81; /* DNS 1 */
386 pco.v[9] = 0x06;
387 memcpy(&pco.v[10], &dns1, sizeof(dns1));
388 pco.v[14] = 0x83;
389 pco.v[15] = 0x06; /* DNS 2 */
390 memcpy(&pco.v[16], &dns2, sizeof(dns2));
391
jjako4b26b512003-01-28 16:13:57 +0000392 /* ipup */
393 ipup = args_info.ipup_arg;
394
395 /* ipdown */
396 ipdown = args_info.ipdown_arg;
397
jjako52c24142002-12-16 13:33:51 +0000398 /* Timelimit */
399 timelimit = args_info.timelimit_arg;
400 starttime = time(NULL);
401
402 /* qos */
403 qos.l = 3;
jjako52c24142002-12-16 13:33:51 +0000404 qos.v[2] = (args_info.qos_arg) & 0xff;
405 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
406 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
jjakoa7cd2492003-04-11 09:40:12 +0000407
jjako52c24142002-12-16 13:33:51 +0000408 /* apn */
jjakoa7cd2492003-04-11 09:40:12 +0000409 if (strlen(args_info.apn_arg) > (sizeof(apn.v)-1)) {
410 printf("Invalid APN\n");
411 return -1;
jjako52c24142002-12-16 13:33:51 +0000412 }
413 apn.l = strlen(args_info.apn_arg) + 1;
jjako52c24142002-12-16 13:33:51 +0000414 apn.v[0] = (char) strlen(args_info.apn_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000415 strncpy(&apn.v[1], args_info.apn_arg, sizeof(apn.v)-1);
jjakoe0149782003-07-06 17:07:04 +0000416
417
418 /* foreground */
419 /* If flag not given run as a daemon */
420 if (!args_info.fg_flag)
421 {
422 closelog();
423 /* Close the standard file descriptors. */
424 /* Is this really needed ? */
425 freopen("/dev/null", "w", stdout);
426 freopen("/dev/null", "w", stderr);
427 freopen("/dev/null", "r", stdin);
428 daemon(0, 0);
429 /* Open log again. This time with new pid */
430 openlog(PACKAGE, LOG_PID, LOG_DAEMON);
431 }
432
433 /* pidfile */
434 /* This has to be done after we have our final pid */
435 if (args_info.pidfile_arg) {
436 log_pid(args_info.pidfile_arg);
437 }
jjakoa7cd2492003-04-11 09:40:12 +0000438
jjako52c24142002-12-16 13:33:51 +0000439
440 if (debug) printf("gtpclient: Initialising GTP tunnel\n");
441
jjako1db1c812003-07-06 20:53:57 +0000442 if (gtp_new(&gsn, args_info.statedir_arg, &listen_, GTP_MODE_GGSN)) {
jjakoa7cd2492003-04-11 09:40:12 +0000443 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
444 "Failed to create gtp");
445 exit(1);
446 }
jjako08d331d2003-10-13 20:33:30 +0000447 if (gsn->fd0 > maxfd) maxfd = gsn->fd0;
448 if (gsn->fd1c > maxfd) maxfd = gsn->fd1c;
449 if (gsn->fd1u > maxfd) maxfd = gsn->fd1u;
jjako52c24142002-12-16 13:33:51 +0000450
jjako08d331d2003-10-13 20:33:30 +0000451 gtp_set_cb_data_ind(gsn, encaps_tun);
jjako52c24142002-12-16 13:33:51 +0000452 gtp_set_cb_delete_context(gsn, delete_context);
jjako08d331d2003-10-13 20:33:30 +0000453 gtp_set_cb_create_context_ind(gsn, create_context_ind);
jjakoa7cd2492003-04-11 09:40:12 +0000454
455
456 /* Create a tunnel interface */
jjakoc6762cf2004-04-28 14:52:58 +0000457 if (debug) printf("Creating tun interface\n");
jjakoa7cd2492003-04-11 09:40:12 +0000458 if (tun_new((struct tun_t**) &tun)) {
459 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
460 "Failed to create tun");
jjakoc6762cf2004-04-28 14:52:58 +0000461 if (debug) printf("Failed to create tun\n");
jjakoa7cd2492003-04-11 09:40:12 +0000462 exit(1);
463 }
464
jjakoc6762cf2004-04-28 14:52:58 +0000465 if (debug) printf("Setting tun IP address\n");
466 if (tun_setaddr(tun, &netaddr, &destaddr, &mask)) {
467 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
468 "Failed to set tun IP address");
469 if (debug) printf("Failed to set tun IP address\n");
470 exit(1);
471 }
472
473
jjakoa7cd2492003-04-11 09:40:12 +0000474 tun_set_cb_ind(tun, cb_tun_ind);
475 if (tun->fd > maxfd) maxfd = tun->fd;
jjakoc6762cf2004-04-28 14:52:58 +0000476
jjako9c7ff082003-04-11 10:01:41 +0000477 if (ipup) tun_runscript(tun, ipup);
jjako52c24142002-12-16 13:33:51 +0000478
479 /******************************************************************/
480 /* Main select loop */
481 /******************************************************************/
482
483 while (((starttime + timelimit) > time(NULL)) || (0 == timelimit)) {
484
485 FD_ZERO(&fds);
jjakoa7cd2492003-04-11 09:40:12 +0000486 if (tun) FD_SET(tun->fd, &fds);
jjako08d331d2003-10-13 20:33:30 +0000487 FD_SET(gsn->fd0, &fds);
488 FD_SET(gsn->fd1c, &fds);
489 FD_SET(gsn->fd1u, &fds);
jjako52c24142002-12-16 13:33:51 +0000490
491 gtp_retranstimeout(gsn, &idleTime);
492 switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
jjakoe0149782003-07-06 17:07:04 +0000493 case -1: /* errno == EINTR : unblocked signal */
494 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
495 "select() returned -1");
jjako52c24142002-12-16 13:33:51 +0000496 break;
497 case 0:
jjakoa7cd2492003-04-11 09:40:12 +0000498 /* printf("Select returned 0\n"); */
jjako52c24142002-12-16 13:33:51 +0000499 gtp_retrans(gsn); /* Only retransmit if nothing else */
500 break;
501 default:
502 break;
503 }
504
jjakoa7cd2492003-04-11 09:40:12 +0000505 if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
506 tun_decaps(tun) < 0) {
jjakoe0149782003-07-06 17:07:04 +0000507 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
508 "TUN read failed (fd)=(%d)", tun->fd);
jjako52c24142002-12-16 13:33:51 +0000509 }
jjako08d331d2003-10-13 20:33:30 +0000510
511 if (FD_ISSET(gsn->fd0, &fds))
512 gtp_decaps0(gsn);
513
514 if (FD_ISSET(gsn->fd1c, &fds))
515 gtp_decaps1c(gsn);
516
517 if (FD_ISSET(gsn->fd1u, &fds))
518 gtp_decaps1u(gsn);
jjako52c24142002-12-16 13:33:51 +0000519
jjako4b26b512003-01-28 16:13:57 +0000520 }
jjako52c24142002-12-16 13:33:51 +0000521
522 gtp_free(gsn);
jjakoa7cd2492003-04-11 09:40:12 +0000523 tun_free(tun);
jjako52c24142002-12-16 13:33:51 +0000524
525 return 1;
526
527}
528