blob: 5d60b90fbb3d0044dca657716a900fe2bf263298 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
jjakoa7cd2492003-04-11 09:40:12 +00002 * OpenGGSN - Gateway GPRS Support Node
jjako0fe0df02004-09-17 11:30:40 +00003 * Copyright (C) 2002, 2003, 2004 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 *
jjako52c24142002-12-16 13:33:51 +000010 */
11
12/* ggsn.c
13 *
14 */
15
16#ifdef __linux__
17#define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */
18#endif
19
jjako0fe0df02004-09-17 11:30:40 +000020#include "../config.h"
21
22#ifdef HAVE_STDINT_H
23#include <stdint.h>
24#endif
jjako52c24142002-12-16 13:33:51 +000025
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
50#include "tun.h"
jjakoa7cd2492003-04-11 09:40:12 +000051#include "ippool.h"
52#include "syserr.h"
jjako52c24142002-12-16 13:33:51 +000053#include "../gtp/pdp.h"
54#include "../gtp/gtp.h"
55#include "cmdline.h"
56
57
jjakoa7cd2492003-04-11 09:40:12 +000058int maxfd = 0; /* For select() */
jjakoa7cd2492003-04-11 09:40:12 +000059
60struct in_addr listen_;
jjakoc6762cf2004-04-28 14:52:58 +000061struct in_addr netaddr, destaddr, net, mask; /* Network interface */
jjakoa7cd2492003-04-11 09:40:12 +000062struct in_addr dns1, dns2; /* PCO DNS address */
63char *ipup, *ipdown; /* Filename of scripts */
64int debug; /* Print debug output */
65struct ul255_t pco;
66struct ul255_t qos;
67struct ul255_t apn;
68
jjako9c7ff082003-04-11 10:01:41 +000069struct gsn_t *gsn; /* GSN instance */
jjakoa7cd2492003-04-11 09:40:12 +000070struct tun_t *tun; /* TUN instance */
71struct ippool_t *ippool; /* Pool of IP addresses */
jjako52c24142002-12-16 13:33:51 +000072
73
74/* Used to write process ID to file. Assume someone else will delete */
75void log_pid(char *pidfile) {
76 FILE *file;
77 mode_t oldmask;
78
79 oldmask = umask(022);
80 file = fopen(pidfile, "w");
81 umask(oldmask);
jjakoe0149782003-07-06 17:07:04 +000082 if(!file) {
83 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
84 "Failed to create process ID file: %s!", pidfile);
jjako52c24142002-12-16 13:33:51 +000085 return;
jjakoe0149782003-07-06 17:07:04 +000086 }
jjako0141d202004-01-09 15:19:20 +000087 fprintf(file, "%d\n", (int) getpid());
jjako52c24142002-12-16 13:33:51 +000088 fclose(file);
89}
90
jjako1f158642004-02-05 20:39:57 +000091#if defined(_sun__)
jjako0141d202004-01-09 15:19:20 +000092int daemon(int nochdir, int noclose) {
93 int fd;
94
95 switch (fork()) {
96 case -1:
97 return (-1);
98 case 0:
99 break;
100 default:
101 _exit(0);
102 }
103
104 if (setsid() == -1)
105 return (-1);
106
107 if (!nochdir) chdir("/");
108
109 if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
110 dup2(fd, STDIN_FILENO);
111 dup2(fd, STDOUT_FILENO);
112 dup2(fd, STDERR_FILENO);
113 if (fd > 2) close (fd);
114 }
115 return (0);
116}
117#endif
118
jjako52c24142002-12-16 13:33:51 +0000119
120int encaps_printf(void *p, void *packet, unsigned len)
121{
122 int i;
123 if (debug) {
124 printf("The packet looks like this:\n");
125 for( i=0; i<len; i++) {
126 printf("%02x ", (unsigned char)*(char *)(packet+i));
127 if (!((i+1)%16)) printf("\n");
128 };
129 printf("\n");
130 }
131 return 0;
132}
133
jjako52c24142002-12-16 13:33:51 +0000134int delete_context(struct pdp_t *pdp) {
jjako49014712003-01-05 17:59:49 +0000135 if (debug) printf("Deleting PDP context\n");
jjako08d331d2003-10-13 20:33:30 +0000136 if (pdp->peer)
137 ippool_freeip(ippool, (struct ippoolm_t *) pdp->peer);
138 else
139 sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Peer not defined!");
jjako52c24142002-12-16 13:33:51 +0000140 return 0;
141}
142
143
jjako08d331d2003-10-13 20:33:30 +0000144int create_context_ind(struct pdp_t *pdp) {
jjakoa7cd2492003-04-11 09:40:12 +0000145 struct in_addr addr;
146 struct ippoolm_t *member;
jjako52c24142002-12-16 13:33:51 +0000147
148 if (debug) printf("Received create PDP context request\n");
149
150 pdp->eua.l=0; /* TODO: Indicates dynamic IP */
151
152 /* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */
jjako12f9e6e2004-09-17 10:28:25 +0000153 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_req0));
jjakoa7cd2492003-04-11 09:40:12 +0000154 memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg));
jjako52c24142002-12-16 13:33:51 +0000155
jjako08d331d2003-10-13 20:33:30 +0000156 memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */
157 pdp->qos_neg.l = pdp->qos_req.l;
158
jjakoa7cd2492003-04-11 09:40:12 +0000159 if (pdp_euaton(&pdp->eua, &addr)) {
160 addr.s_addr = 0; /* Request dynamic */
161 }
162
163 if (ippool_newip(ippool, &member, &addr)) {
jjako08d331d2003-10-13 20:33:30 +0000164 gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES);
165 return 0; /* Allready in use, or no more available */
jjakoa7cd2492003-04-11 09:40:12 +0000166 }
167
168 pdp_ntoeua(&member->addr, &pdp->eua);
jjakoa7c33812003-04-11 11:51:39 +0000169 pdp->peer = member;
jjakoa7cd2492003-04-11 09:40:12 +0000170 pdp->ipif = tun; /* TODO */
171 member->peer = pdp;
jjako52c24142002-12-16 13:33:51 +0000172
jjako08d331d2003-10-13 20:33:30 +0000173 gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ);
jjako52c24142002-12-16 13:33:51 +0000174 return 0; /* Success */
175}
176
177
jjakoa7cd2492003-04-11 09:40:12 +0000178/* Callback for receiving messages from tun */
179int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) {
180 struct ippoolm_t *ipm;
181 struct in_addr dst;
182 struct tun_packet_t *iph = (struct tun_packet_t*) pack;
183
184 dst.s_addr = iph->dst;
jjakoc6762cf2004-04-28 14:52:58 +0000185
186 if (debug) printf("Received packet from tun!\n");
187
jjakoa7cd2492003-04-11 09:40:12 +0000188 if (ippool_getip(ippool, &ipm, &dst)) {
jjako52c24142002-12-16 13:33:51 +0000189 if (debug) printf("Received packet with no destination!!!\n");
190 return 0;
191 }
jjakoa7cd2492003-04-11 09:40:12 +0000192
193 if (ipm->peer) /* Check if a peer protocol is defined */
jjako08d331d2003-10-13 20:33:30 +0000194 gtp_data_req(gsn, (struct pdp_t*) ipm->peer, pack, len);
jjakoa7cd2492003-04-11 09:40:12 +0000195 return 0;
jjako52c24142002-12-16 13:33:51 +0000196}
197
jjako52c24142002-12-16 13:33:51 +0000198int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) {
jjakoc6762cf2004-04-28 14:52:58 +0000199 if (debug) printf("encaps_tun. Packet received: forwarding to tun\n");
jjako52c24142002-12-16 13:33:51 +0000200 return tun_encaps((struct tun_t*) pdp->ipif, pack, len);
201}
202
203
204int main(int argc, char **argv)
205{
206 /* gengeopt declarations */
207 struct gengetopt_args_info args_info;
208
209 struct hostent *host;
210
jjako52c24142002-12-16 13:33:51 +0000211
jjako52c24142002-12-16 13:33:51 +0000212 fd_set fds; /* For select() */
213 struct timeval idleTime; /* How long to select() */
jjako52c24142002-12-16 13:33:51 +0000214
jjako52c24142002-12-16 13:33:51 +0000215
216 int timelimit; /* Number of seconds to be connected */
217 int starttime; /* Time program was started */
218
219 /* open a connection to the syslog daemon */
220 /*openlog(PACKAGE, LOG_PID, LOG_DAEMON);*/
jjako0141d202004-01-09 15:19:20 +0000221
222 /* TODO: Only use LOG__PERROR for linux */
223#ifdef __linux__
jjako52c24142002-12-16 13:33:51 +0000224 openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON);
jjako0141d202004-01-09 15:19:20 +0000225#else
226 openlog(PACKAGE, (LOG_PID), LOG_DAEMON);
227#endif
228
jjako52c24142002-12-16 13:33:51 +0000229
230 if (cmdline_parser (argc, argv, &args_info) != 0)
231 exit(1);
232 if (args_info.debug_flag) {
233 printf("listen: %s\n", args_info.listen_arg);
jjakoc6762cf2004-04-28 14:52:58 +0000234 if (args_info.conf_arg) printf("conf: %s\n", args_info.conf_arg);
jjako52c24142002-12-16 13:33:51 +0000235 printf("fg: %d\n", args_info.fg_flag);
236 printf("debug: %d\n", args_info.debug_flag);
237 printf("qos: %#08x\n", args_info.qos_arg);
jjakoc6762cf2004-04-28 14:52:58 +0000238 if (args_info.apn_arg) printf("apn: %s\n", args_info.apn_arg);
239 if (args_info.net_arg) printf("net: %s\n", args_info.net_arg);
240 if (args_info.dynip_arg) printf("dynip: %s\n", args_info.dynip_arg);
241 if (args_info.statip_arg) printf("statip: %s\n", args_info.statip_arg);
242 if (args_info.ipup_arg) printf("ipup: %s\n", args_info.ipup_arg);
243 if (args_info.ipdown_arg) printf("ipdown: %s\n", args_info.ipdown_arg);
244 if (args_info.pidfile_arg) printf("pidfile: %s\n", args_info.pidfile_arg);
245 if (args_info.statedir_arg) printf("statedir: %s\n", args_info.statedir_arg);
jjako52c24142002-12-16 13:33:51 +0000246 printf("timelimit: %d\n", args_info.timelimit_arg);
247 }
248
249 /* Try out our new parser */
250
251 if (cmdline_parser_configfile (args_info.conf_arg, &args_info, 0) != 0)
252 exit(1);
253 if (args_info.debug_flag) {
254 printf("cmdline_parser_configfile\n");
255 printf("listen: %s\n", args_info.listen_arg);
256 printf("conf: %s\n", args_info.conf_arg);
257 printf("fg: %d\n", args_info.fg_flag);
258 printf("debug: %d\n", args_info.debug_flag);
259 printf("qos: %#08x\n", args_info.qos_arg);
jjakoc6762cf2004-04-28 14:52:58 +0000260 if (args_info.apn_arg) printf("apn: %s\n", args_info.apn_arg);
261 if (args_info.net_arg) printf("net: %s\n", args_info.net_arg);
262 if (args_info.dynip_arg) printf("dynip: %s\n", args_info.dynip_arg);
263 if (args_info.statip_arg) printf("statip: %s\n", args_info.statip_arg);
264 if (args_info.ipup_arg) printf("ipup: %s\n", args_info.ipup_arg);
265 if (args_info.ipdown_arg) printf("ipdown: %s\n", args_info.ipdown_arg);
266 if (args_info.pidfile_arg) printf("pidfile: %s\n", args_info.pidfile_arg);
267 if (args_info.statedir_arg) printf("statedir: %s\n", args_info.statedir_arg);
jjako52c24142002-12-16 13:33:51 +0000268 printf("timelimit: %d\n", args_info.timelimit_arg);
269 }
270
271 /* Handle each option */
272
jjako52c24142002-12-16 13:33:51 +0000273 /* debug */
274 debug = args_info.debug_flag;
275
jjako52c24142002-12-16 13:33:51 +0000276 /* listen */
jjako52c24142002-12-16 13:33:51 +0000277 /* Do hostname lookup to translate hostname to IP address */
jjakoe0149782003-07-06 17:07:04 +0000278 /* Any port listening is not possible as a valid address is */
279 /* required for create_pdp_context_response messages */
jjako52c24142002-12-16 13:33:51 +0000280 if (args_info.listen_arg) {
281 if (!(host = gethostbyname(args_info.listen_arg))) {
jjakoe0149782003-07-06 17:07:04 +0000282 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
283 "Invalid listening address: %s!", args_info.listen_arg);
jjakoa7c33812003-04-11 11:51:39 +0000284 exit(1);
jjako52c24142002-12-16 13:33:51 +0000285 }
286 else {
jjakoa7cd2492003-04-11 09:40:12 +0000287 memcpy(&listen_.s_addr, host->h_addr, host->h_length);
jjako52c24142002-12-16 13:33:51 +0000288 }
289 }
290 else {
jjakoe0149782003-07-06 17:07:04 +0000291 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
292 "Listening address must be specified! "
293 "Please use command line option --listen or "
294 "edit %s configuration file\n", args_info.conf_arg);
295 exit(1);
jjako52c24142002-12-16 13:33:51 +0000296 }
297
jjako88c22162003-07-06 19:33:18 +0000298
jjako52c24142002-12-16 13:33:51 +0000299 /* net */
jjakoa7cd2492003-04-11 09:40:12 +0000300 /* Store net as in_addr net and mask */
jjako52c24142002-12-16 13:33:51 +0000301 if (args_info.net_arg) {
jjakoa7cd2492003-04-11 09:40:12 +0000302 if(ippool_aton(&net, &mask, args_info.net_arg, 0)) {
303 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
304 "Invalid network address: %s!", args_info.net_arg);
jjakoa7c33812003-04-11 11:51:39 +0000305 exit(1);
jjako52c24142002-12-16 13:33:51 +0000306 }
jjakoc6762cf2004-04-28 14:52:58 +0000307 netaddr.s_addr = htonl(ntohl(net.s_addr) + 1);
308 destaddr.s_addr = htonl(ntohl(net.s_addr) + 1);
jjako52c24142002-12-16 13:33:51 +0000309 }
jjakoa7c33812003-04-11 11:51:39 +0000310 else {
311 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
312 "Network address must be specified: %s!", args_info.net_arg);
313 exit(1);
314 }
jjako52c24142002-12-16 13:33:51 +0000315
jjakoa7cd2492003-04-11 09:40:12 +0000316 /* dynip */
317 if (!args_info.dynip_arg) {
jjako88c22162003-07-06 19:33:18 +0000318 if (ippool_new(&ippool, args_info.net_arg, NULL, 1, 0,
jjakoc6762cf2004-04-28 14:52:58 +0000319 IPPOOL_NONETWORK | IPPOOL_NOGATEWAY | IPPOOL_NOBROADCAST)) {
jjakoa7c33812003-04-11 11:51:39 +0000320 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
321 "Failed to allocate IP pool!");
322 exit(1);
323 }
jjakoa7cd2492003-04-11 09:40:12 +0000324 }
325 else {
jjako88c22162003-07-06 19:33:18 +0000326 if (ippool_new(&ippool, args_info.dynip_arg, NULL, 1 ,0,
jjakoc6762cf2004-04-28 14:52:58 +0000327 IPPOOL_NONETWORK | IPPOOL_NOGATEWAY | IPPOOL_NOBROADCAST)) {
jjakoa7cd2492003-04-11 09:40:12 +0000328 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
329 "Failed to allocate IP pool!");
jjakoa7c33812003-04-11 11:51:39 +0000330 exit(1);
jjako52c24142002-12-16 13:33:51 +0000331 }
332 }
333
jjakoa7cd2492003-04-11 09:40:12 +0000334 /* DNS1 and DNS2 */
jjakoff9985c2004-01-16 11:05:22 +0000335#ifdef HAVE_INET_ATON
jjakoa7cd2492003-04-11 09:40:12 +0000336 dns1.s_addr = 0;
jjako76032b92004-01-14 06:22:08 +0000337 if (args_info.pcodns1_arg) {
jjako1d3db972004-01-16 09:56:56 +0000338 if (0 == inet_aton(args_info.pcodns1_arg, &dns1)) {
jjako76032b92004-01-14 06:22:08 +0000339 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
340 "Failed to convert pcodns1!");
341 exit(1);
342 }
343 }
jjakoa7cd2492003-04-11 09:40:12 +0000344 dns2.s_addr = 0;
jjako76032b92004-01-14 06:22:08 +0000345 if (args_info.pcodns2_arg) {
jjako1d3db972004-01-16 09:56:56 +0000346 if (0 == inet_aton(args_info.pcodns2_arg, &dns2)) {
jjako76032b92004-01-14 06:22:08 +0000347 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
348 "Failed to convert pcodns2!");
349 exit(1);
350 }
351 }
jjako1d3db972004-01-16 09:56:56 +0000352#else
jjako1d3db972004-01-16 09:56:56 +0000353 dns1.s_addr = 0;
354 if (args_info.pcodns1_arg) {
jjakoff9985c2004-01-16 11:05:22 +0000355 dns1.s_addr = inet_addr(args_info.pcodns1_arg);
356 if (dns1.s_addr == -1) {
jjako1d3db972004-01-16 09:56:56 +0000357 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
358 "Failed to convert pcodns1!");
359 exit(1);
360 }
361 }
362 dns2.s_addr = 0;
363 if (args_info.pcodns2_arg) {
jjakoff9985c2004-01-16 11:05:22 +0000364 dns2.s_addr = inet_addr(args_info.pcodns2_arg);
365 if (dns2.s_addr == -1) {
jjako1d3db972004-01-16 09:56:56 +0000366 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
367 "Failed to convert pcodns2!");
368 exit(1);
369 }
370 }
jjako1d3db972004-01-16 09:56:56 +0000371#endif
372
jjakoa7cd2492003-04-11 09:40:12 +0000373
374 pco.l = 20;
375 pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */
376 pco.v[1] = 0x80; /* IPCP */
377 pco.v[2] = 0x21;
378 pco.v[3] = 0x10; /* Length of contents */
379 pco.v[4] = 0x02; /* ACK */
380 pco.v[5] = 0x00; /* ID: Need to match request */
381 pco.v[6] = 0x00; /* Length */
382 pco.v[7] = 0x10;
383 pco.v[8] = 0x81; /* DNS 1 */
384 pco.v[9] = 0x06;
385 memcpy(&pco.v[10], &dns1, sizeof(dns1));
386 pco.v[14] = 0x83;
387 pco.v[15] = 0x06; /* DNS 2 */
388 memcpy(&pco.v[16], &dns2, sizeof(dns2));
389
jjako4b26b512003-01-28 16:13:57 +0000390 /* ipup */
391 ipup = args_info.ipup_arg;
392
393 /* ipdown */
394 ipdown = args_info.ipdown_arg;
395
jjako52c24142002-12-16 13:33:51 +0000396 /* Timelimit */
397 timelimit = args_info.timelimit_arg;
398 starttime = time(NULL);
399
400 /* qos */
401 qos.l = 3;
jjako52c24142002-12-16 13:33:51 +0000402 qos.v[2] = (args_info.qos_arg) & 0xff;
403 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
404 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
jjakoa7cd2492003-04-11 09:40:12 +0000405
jjako52c24142002-12-16 13:33:51 +0000406 /* apn */
jjakoa7cd2492003-04-11 09:40:12 +0000407 if (strlen(args_info.apn_arg) > (sizeof(apn.v)-1)) {
408 printf("Invalid APN\n");
409 return -1;
jjako52c24142002-12-16 13:33:51 +0000410 }
411 apn.l = strlen(args_info.apn_arg) + 1;
jjako52c24142002-12-16 13:33:51 +0000412 apn.v[0] = (char) strlen(args_info.apn_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000413 strncpy(&apn.v[1], args_info.apn_arg, sizeof(apn.v)-1);
jjakoe0149782003-07-06 17:07:04 +0000414
415
416 /* foreground */
417 /* If flag not given run as a daemon */
418 if (!args_info.fg_flag)
419 {
420 closelog();
421 /* Close the standard file descriptors. */
422 /* Is this really needed ? */
423 freopen("/dev/null", "w", stdout);
424 freopen("/dev/null", "w", stderr);
425 freopen("/dev/null", "r", stdin);
426 daemon(0, 0);
427 /* Open log again. This time with new pid */
428 openlog(PACKAGE, LOG_PID, LOG_DAEMON);
429 }
430
431 /* pidfile */
432 /* This has to be done after we have our final pid */
433 if (args_info.pidfile_arg) {
434 log_pid(args_info.pidfile_arg);
435 }
jjakoa7cd2492003-04-11 09:40:12 +0000436
jjako52c24142002-12-16 13:33:51 +0000437
438 if (debug) printf("gtpclient: Initialising GTP tunnel\n");
439
jjako1db1c812003-07-06 20:53:57 +0000440 if (gtp_new(&gsn, args_info.statedir_arg, &listen_, GTP_MODE_GGSN)) {
jjakoa7cd2492003-04-11 09:40:12 +0000441 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
442 "Failed to create gtp");
443 exit(1);
444 }
jjako08d331d2003-10-13 20:33:30 +0000445 if (gsn->fd0 > maxfd) maxfd = gsn->fd0;
446 if (gsn->fd1c > maxfd) maxfd = gsn->fd1c;
447 if (gsn->fd1u > maxfd) maxfd = gsn->fd1u;
jjako52c24142002-12-16 13:33:51 +0000448
jjako08d331d2003-10-13 20:33:30 +0000449 gtp_set_cb_data_ind(gsn, encaps_tun);
jjako52c24142002-12-16 13:33:51 +0000450 gtp_set_cb_delete_context(gsn, delete_context);
jjako08d331d2003-10-13 20:33:30 +0000451 gtp_set_cb_create_context_ind(gsn, create_context_ind);
jjakoa7cd2492003-04-11 09:40:12 +0000452
453
454 /* Create a tunnel interface */
jjakoc6762cf2004-04-28 14:52:58 +0000455 if (debug) printf("Creating tun interface\n");
jjakoa7cd2492003-04-11 09:40:12 +0000456 if (tun_new((struct tun_t**) &tun)) {
457 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
458 "Failed to create tun");
jjakoc6762cf2004-04-28 14:52:58 +0000459 if (debug) printf("Failed to create tun\n");
jjakoa7cd2492003-04-11 09:40:12 +0000460 exit(1);
461 }
462
jjakoc6762cf2004-04-28 14:52:58 +0000463 if (debug) printf("Setting tun IP address\n");
464 if (tun_setaddr(tun, &netaddr, &destaddr, &mask)) {
465 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
466 "Failed to set tun IP address");
467 if (debug) printf("Failed to set tun IP address\n");
468 exit(1);
469 }
470
471
jjakoa7cd2492003-04-11 09:40:12 +0000472 tun_set_cb_ind(tun, cb_tun_ind);
473 if (tun->fd > maxfd) maxfd = tun->fd;
jjakoc6762cf2004-04-28 14:52:58 +0000474
jjako9c7ff082003-04-11 10:01:41 +0000475 if (ipup) tun_runscript(tun, ipup);
jjako52c24142002-12-16 13:33:51 +0000476
477 /******************************************************************/
478 /* Main select loop */
479 /******************************************************************/
480
481 while (((starttime + timelimit) > time(NULL)) || (0 == timelimit)) {
482
483 FD_ZERO(&fds);
jjakoa7cd2492003-04-11 09:40:12 +0000484 if (tun) FD_SET(tun->fd, &fds);
jjako08d331d2003-10-13 20:33:30 +0000485 FD_SET(gsn->fd0, &fds);
486 FD_SET(gsn->fd1c, &fds);
487 FD_SET(gsn->fd1u, &fds);
jjako52c24142002-12-16 13:33:51 +0000488
489 gtp_retranstimeout(gsn, &idleTime);
490 switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
jjakoe0149782003-07-06 17:07:04 +0000491 case -1: /* errno == EINTR : unblocked signal */
492 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
493 "select() returned -1");
jjako52c24142002-12-16 13:33:51 +0000494 break;
495 case 0:
jjakoa7cd2492003-04-11 09:40:12 +0000496 /* printf("Select returned 0\n"); */
jjako52c24142002-12-16 13:33:51 +0000497 gtp_retrans(gsn); /* Only retransmit if nothing else */
498 break;
499 default:
500 break;
501 }
502
jjakoa7cd2492003-04-11 09:40:12 +0000503 if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
504 tun_decaps(tun) < 0) {
jjakoe0149782003-07-06 17:07:04 +0000505 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
506 "TUN read failed (fd)=(%d)", tun->fd);
jjako52c24142002-12-16 13:33:51 +0000507 }
jjako08d331d2003-10-13 20:33:30 +0000508
509 if (FD_ISSET(gsn->fd0, &fds))
510 gtp_decaps0(gsn);
511
512 if (FD_ISSET(gsn->fd1c, &fds))
513 gtp_decaps1c(gsn);
514
515 if (FD_ISSET(gsn->fd1u, &fds))
516 gtp_decaps1u(gsn);
jjako52c24142002-12-16 13:33:51 +0000517
jjako4b26b512003-01-28 16:13:57 +0000518 }
jjako52c24142002-12-16 13:33:51 +0000519
520 gtp_free(gsn);
jjakoa7cd2492003-04-11 09:40:12 +0000521 tun_free(tun);
jjako52c24142002-12-16 13:33:51 +0000522
523 return 1;
524
525}
526