blob: 57116cea13197299e99c7ee02f001390079d3627 [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
Emmanuel Bretelle2a103682010-09-07 17:01:20 +020050#include "../lib/tun.h"
51#include "../lib/ippool.h"
52#include "../lib/syserr.h"
jjako52c24142002-12-16 13:33:51 +000053#include "../gtp/pdp.h"
54#include "../gtp/gtp.h"
55#include "cmdline.h"
56
Harald Weltec3dcba02010-05-04 11:02:54 +020057int end = 0;
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
Harald Weltec3dcba02010-05-04 11:02:54 +020073/* To exit gracefully. Used with GCC compilation flag -pg and gprof */
74void signal_handler(int s) {
75 if (debug) printf("Received signal %d, exiting.\n", s);
76 end = 1;
77}
jjako52c24142002-12-16 13:33:51 +000078
79/* Used to write process ID to file. Assume someone else will delete */
80void log_pid(char *pidfile) {
81 FILE *file;
82 mode_t oldmask;
83
84 oldmask = umask(022);
85 file = fopen(pidfile, "w");
86 umask(oldmask);
jjakoe0149782003-07-06 17:07:04 +000087 if(!file) {
88 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
89 "Failed to create process ID file: %s!", pidfile);
jjako52c24142002-12-16 13:33:51 +000090 return;
jjakoe0149782003-07-06 17:07:04 +000091 }
jjako0141d202004-01-09 15:19:20 +000092 fprintf(file, "%d\n", (int) getpid());
jjako52c24142002-12-16 13:33:51 +000093 fclose(file);
94}
95
jjakobd937b72004-12-30 16:22:42 +000096#if defined(__sun__)
jjako0141d202004-01-09 15:19:20 +000097int daemon(int nochdir, int noclose) {
98 int fd;
99
100 switch (fork()) {
101 case -1:
102 return (-1);
103 case 0:
104 break;
105 default:
106 _exit(0);
107 }
108
109 if (setsid() == -1)
110 return (-1);
111
112 if (!nochdir) chdir("/");
113
114 if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
115 dup2(fd, STDIN_FILENO);
116 dup2(fd, STDOUT_FILENO);
117 dup2(fd, STDERR_FILENO);
118 if (fd > 2) close (fd);
119 }
120 return (0);
121}
122#endif
123
jjako52c24142002-12-16 13:33:51 +0000124
125int encaps_printf(void *p, void *packet, unsigned len)
126{
Harald Weltef54a1f42010-05-04 11:08:38 +0200127 unsigned int i;
jjako52c24142002-12-16 13:33:51 +0000128 if (debug) {
129 printf("The packet looks like this:\n");
130 for( i=0; i<len; i++) {
131 printf("%02x ", (unsigned char)*(char *)(packet+i));
132 if (!((i+1)%16)) printf("\n");
133 };
134 printf("\n");
135 }
136 return 0;
137}
138
jjako52c24142002-12-16 13:33:51 +0000139int delete_context(struct pdp_t *pdp) {
jjako49014712003-01-05 17:59:49 +0000140 if (debug) printf("Deleting PDP context\n");
jjako08d331d2003-10-13 20:33:30 +0000141 if (pdp->peer)
142 ippool_freeip(ippool, (struct ippoolm_t *) pdp->peer);
143 else
144 sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Peer not defined!");
jjako52c24142002-12-16 13:33:51 +0000145 return 0;
146}
147
148
jjako08d331d2003-10-13 20:33:30 +0000149int create_context_ind(struct pdp_t *pdp) {
jjakoa7cd2492003-04-11 09:40:12 +0000150 struct in_addr addr;
151 struct ippoolm_t *member;
jjako52c24142002-12-16 13:33:51 +0000152
153 if (debug) printf("Received create PDP context request\n");
154
155 pdp->eua.l=0; /* TODO: Indicates dynamic IP */
156
157 /* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */
jjako12f9e6e2004-09-17 10:28:25 +0000158 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_req0));
jjakoa7cd2492003-04-11 09:40:12 +0000159 memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg));
jjako52c24142002-12-16 13:33:51 +0000160
jjako08d331d2003-10-13 20:33:30 +0000161 memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */
162 pdp->qos_neg.l = pdp->qos_req.l;
163
jjakoa7cd2492003-04-11 09:40:12 +0000164 if (pdp_euaton(&pdp->eua, &addr)) {
165 addr.s_addr = 0; /* Request dynamic */
166 }
167
jjakoada9ffa2004-12-30 16:39:11 +0000168 if (ippool_newip(ippool, &member, &addr, 0)) {
jjako08d331d2003-10-13 20:33:30 +0000169 gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES);
170 return 0; /* Allready in use, or no more available */
jjakoa7cd2492003-04-11 09:40:12 +0000171 }
172
173 pdp_ntoeua(&member->addr, &pdp->eua);
jjakoa7c33812003-04-11 11:51:39 +0000174 pdp->peer = member;
jjakoa7cd2492003-04-11 09:40:12 +0000175 pdp->ipif = tun; /* TODO */
176 member->peer = pdp;
jjako52c24142002-12-16 13:33:51 +0000177
jjako08d331d2003-10-13 20:33:30 +0000178 gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ);
jjako52c24142002-12-16 13:33:51 +0000179 return 0; /* Success */
180}
181
182
jjakoa7cd2492003-04-11 09:40:12 +0000183/* Callback for receiving messages from tun */
184int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) {
185 struct ippoolm_t *ipm;
186 struct in_addr dst;
187 struct tun_packet_t *iph = (struct tun_packet_t*) pack;
188
189 dst.s_addr = iph->dst;
jjakoc6762cf2004-04-28 14:52:58 +0000190
191 if (debug) printf("Received packet from tun!\n");
192
jjakoa7cd2492003-04-11 09:40:12 +0000193 if (ippool_getip(ippool, &ipm, &dst)) {
jjako52c24142002-12-16 13:33:51 +0000194 if (debug) printf("Received packet with no destination!!!\n");
195 return 0;
196 }
jjakoa7cd2492003-04-11 09:40:12 +0000197
198 if (ipm->peer) /* Check if a peer protocol is defined */
jjako08d331d2003-10-13 20:33:30 +0000199 gtp_data_req(gsn, (struct pdp_t*) ipm->peer, pack, len);
jjakoa7cd2492003-04-11 09:40:12 +0000200 return 0;
jjako52c24142002-12-16 13:33:51 +0000201}
202
jjako52c24142002-12-16 13:33:51 +0000203int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) {
jjakoc6762cf2004-04-28 14:52:58 +0000204 if (debug) printf("encaps_tun. Packet received: forwarding to tun\n");
jjako52c24142002-12-16 13:33:51 +0000205 return tun_encaps((struct tun_t*) pdp->ipif, pack, len);
206}
207
208
209int main(int argc, char **argv)
210{
211 /* gengeopt declarations */
212 struct gengetopt_args_info args_info;
213
214 struct hostent *host;
215
Harald Weltec3dcba02010-05-04 11:02:54 +0200216 /* Handle keyboard interrupt SIGINT */
217 struct sigaction s;
218 s.sa_handler = (void *) signal_handler;
219 if ((0 != sigemptyset( &s.sa_mask )) && debug)
220 printf("sigemptyset failed.\n");
221 s.sa_flags = SA_RESETHAND;
222 if ((sigaction(SIGINT, &s, NULL) != 0) && debug)
223 printf("Could not register SIGINT signal handler.\n");
jjako52c24142002-12-16 13:33:51 +0000224
jjako52c24142002-12-16 13:33:51 +0000225 fd_set fds; /* For select() */
226 struct timeval idleTime; /* How long to select() */
jjako52c24142002-12-16 13:33:51 +0000227
jjako52c24142002-12-16 13:33:51 +0000228
229 int timelimit; /* Number of seconds to be connected */
230 int starttime; /* Time program was started */
231
232 /* open a connection to the syslog daemon */
233 /*openlog(PACKAGE, LOG_PID, LOG_DAEMON);*/
jjako0141d202004-01-09 15:19:20 +0000234
235 /* TODO: Only use LOG__PERROR for linux */
236#ifdef __linux__
jjako52c24142002-12-16 13:33:51 +0000237 openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON);
jjako0141d202004-01-09 15:19:20 +0000238#else
239 openlog(PACKAGE, (LOG_PID), LOG_DAEMON);
240#endif
241
jjako52c24142002-12-16 13:33:51 +0000242
243 if (cmdline_parser (argc, argv, &args_info) != 0)
244 exit(1);
245 if (args_info.debug_flag) {
246 printf("listen: %s\n", args_info.listen_arg);
jjakoc6762cf2004-04-28 14:52:58 +0000247 if (args_info.conf_arg) printf("conf: %s\n", args_info.conf_arg);
jjako52c24142002-12-16 13:33:51 +0000248 printf("fg: %d\n", args_info.fg_flag);
249 printf("debug: %d\n", args_info.debug_flag);
250 printf("qos: %#08x\n", args_info.qos_arg);
jjakoc6762cf2004-04-28 14:52:58 +0000251 if (args_info.apn_arg) printf("apn: %s\n", args_info.apn_arg);
252 if (args_info.net_arg) printf("net: %s\n", args_info.net_arg);
253 if (args_info.dynip_arg) printf("dynip: %s\n", args_info.dynip_arg);
254 if (args_info.statip_arg) printf("statip: %s\n", args_info.statip_arg);
255 if (args_info.ipup_arg) printf("ipup: %s\n", args_info.ipup_arg);
256 if (args_info.ipdown_arg) printf("ipdown: %s\n", args_info.ipdown_arg);
257 if (args_info.pidfile_arg) printf("pidfile: %s\n", args_info.pidfile_arg);
258 if (args_info.statedir_arg) printf("statedir: %s\n", args_info.statedir_arg);
jjako52c24142002-12-16 13:33:51 +0000259 printf("timelimit: %d\n", args_info.timelimit_arg);
260 }
261
262 /* Try out our new parser */
263
Harald Welte1b3e5772010-05-04 11:13:56 +0200264 if (cmdline_parser_configfile (args_info.conf_arg, &args_info, 0, 0, 0) != 0)
jjako52c24142002-12-16 13:33:51 +0000265 exit(1);
266 if (args_info.debug_flag) {
267 printf("cmdline_parser_configfile\n");
268 printf("listen: %s\n", args_info.listen_arg);
269 printf("conf: %s\n", args_info.conf_arg);
270 printf("fg: %d\n", args_info.fg_flag);
271 printf("debug: %d\n", args_info.debug_flag);
272 printf("qos: %#08x\n", args_info.qos_arg);
jjakoc6762cf2004-04-28 14:52:58 +0000273 if (args_info.apn_arg) printf("apn: %s\n", args_info.apn_arg);
274 if (args_info.net_arg) printf("net: %s\n", args_info.net_arg);
275 if (args_info.dynip_arg) printf("dynip: %s\n", args_info.dynip_arg);
276 if (args_info.statip_arg) printf("statip: %s\n", args_info.statip_arg);
277 if (args_info.ipup_arg) printf("ipup: %s\n", args_info.ipup_arg);
278 if (args_info.ipdown_arg) printf("ipdown: %s\n", args_info.ipdown_arg);
279 if (args_info.pidfile_arg) printf("pidfile: %s\n", args_info.pidfile_arg);
280 if (args_info.statedir_arg) printf("statedir: %s\n", args_info.statedir_arg);
jjako52c24142002-12-16 13:33:51 +0000281 printf("timelimit: %d\n", args_info.timelimit_arg);
282 }
283
284 /* Handle each option */
285
jjako52c24142002-12-16 13:33:51 +0000286 /* debug */
287 debug = args_info.debug_flag;
288
jjako52c24142002-12-16 13:33:51 +0000289 /* listen */
jjako52c24142002-12-16 13:33:51 +0000290 /* Do hostname lookup to translate hostname to IP address */
jjakoe0149782003-07-06 17:07:04 +0000291 /* Any port listening is not possible as a valid address is */
292 /* required for create_pdp_context_response messages */
jjako52c24142002-12-16 13:33:51 +0000293 if (args_info.listen_arg) {
294 if (!(host = gethostbyname(args_info.listen_arg))) {
jjakoe0149782003-07-06 17:07:04 +0000295 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
296 "Invalid listening address: %s!", args_info.listen_arg);
jjakoa7c33812003-04-11 11:51:39 +0000297 exit(1);
jjako52c24142002-12-16 13:33:51 +0000298 }
299 else {
jjakoa7cd2492003-04-11 09:40:12 +0000300 memcpy(&listen_.s_addr, host->h_addr, host->h_length);
jjako52c24142002-12-16 13:33:51 +0000301 }
302 }
303 else {
jjakoe0149782003-07-06 17:07:04 +0000304 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
305 "Listening address must be specified! "
306 "Please use command line option --listen or "
307 "edit %s configuration file\n", args_info.conf_arg);
308 exit(1);
jjako52c24142002-12-16 13:33:51 +0000309 }
310
jjako88c22162003-07-06 19:33:18 +0000311
jjako52c24142002-12-16 13:33:51 +0000312 /* net */
jjakoa7cd2492003-04-11 09:40:12 +0000313 /* Store net as in_addr net and mask */
jjako52c24142002-12-16 13:33:51 +0000314 if (args_info.net_arg) {
jjakoa7cd2492003-04-11 09:40:12 +0000315 if(ippool_aton(&net, &mask, args_info.net_arg, 0)) {
316 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
317 "Invalid network address: %s!", args_info.net_arg);
jjakoa7c33812003-04-11 11:51:39 +0000318 exit(1);
jjako52c24142002-12-16 13:33:51 +0000319 }
jjakoc6762cf2004-04-28 14:52:58 +0000320 netaddr.s_addr = htonl(ntohl(net.s_addr) + 1);
321 destaddr.s_addr = htonl(ntohl(net.s_addr) + 1);
jjako52c24142002-12-16 13:33:51 +0000322 }
jjakoa7c33812003-04-11 11:51:39 +0000323 else {
324 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
325 "Network address must be specified: %s!", args_info.net_arg);
326 exit(1);
327 }
jjako52c24142002-12-16 13:33:51 +0000328
jjakoa7cd2492003-04-11 09:40:12 +0000329 /* dynip */
330 if (!args_info.dynip_arg) {
jjako88c22162003-07-06 19:33:18 +0000331 if (ippool_new(&ippool, args_info.net_arg, NULL, 1, 0,
jjakoc6762cf2004-04-28 14:52:58 +0000332 IPPOOL_NONETWORK | IPPOOL_NOGATEWAY | IPPOOL_NOBROADCAST)) {
jjakoa7c33812003-04-11 11:51:39 +0000333 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
334 "Failed to allocate IP pool!");
335 exit(1);
336 }
jjakoa7cd2492003-04-11 09:40:12 +0000337 }
338 else {
jjako88c22162003-07-06 19:33:18 +0000339 if (ippool_new(&ippool, args_info.dynip_arg, NULL, 1 ,0,
jjakoc6762cf2004-04-28 14:52:58 +0000340 IPPOOL_NONETWORK | IPPOOL_NOGATEWAY | IPPOOL_NOBROADCAST)) {
jjakoa7cd2492003-04-11 09:40:12 +0000341 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
342 "Failed to allocate IP pool!");
jjakoa7c33812003-04-11 11:51:39 +0000343 exit(1);
jjako52c24142002-12-16 13:33:51 +0000344 }
345 }
346
jjakoa7cd2492003-04-11 09:40:12 +0000347 /* DNS1 and DNS2 */
jjakoff9985c2004-01-16 11:05:22 +0000348#ifdef HAVE_INET_ATON
jjakoa7cd2492003-04-11 09:40:12 +0000349 dns1.s_addr = 0;
jjako76032b92004-01-14 06:22:08 +0000350 if (args_info.pcodns1_arg) {
jjako1d3db972004-01-16 09:56:56 +0000351 if (0 == inet_aton(args_info.pcodns1_arg, &dns1)) {
jjako76032b92004-01-14 06:22:08 +0000352 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
353 "Failed to convert pcodns1!");
354 exit(1);
355 }
356 }
jjakoa7cd2492003-04-11 09:40:12 +0000357 dns2.s_addr = 0;
jjako76032b92004-01-14 06:22:08 +0000358 if (args_info.pcodns2_arg) {
jjako1d3db972004-01-16 09:56:56 +0000359 if (0 == inet_aton(args_info.pcodns2_arg, &dns2)) {
jjako76032b92004-01-14 06:22:08 +0000360 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
361 "Failed to convert pcodns2!");
362 exit(1);
363 }
364 }
jjako1d3db972004-01-16 09:56:56 +0000365#else
jjako1d3db972004-01-16 09:56:56 +0000366 dns1.s_addr = 0;
367 if (args_info.pcodns1_arg) {
jjakoff9985c2004-01-16 11:05:22 +0000368 dns1.s_addr = inet_addr(args_info.pcodns1_arg);
369 if (dns1.s_addr == -1) {
jjako1d3db972004-01-16 09:56:56 +0000370 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
371 "Failed to convert pcodns1!");
372 exit(1);
373 }
374 }
375 dns2.s_addr = 0;
376 if (args_info.pcodns2_arg) {
jjakoff9985c2004-01-16 11:05:22 +0000377 dns2.s_addr = inet_addr(args_info.pcodns2_arg);
378 if (dns2.s_addr == -1) {
jjako1d3db972004-01-16 09:56:56 +0000379 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
380 "Failed to convert pcodns2!");
381 exit(1);
382 }
383 }
jjako1d3db972004-01-16 09:56:56 +0000384#endif
385
jjakoa7cd2492003-04-11 09:40:12 +0000386
387 pco.l = 20;
388 pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */
389 pco.v[1] = 0x80; /* IPCP */
390 pco.v[2] = 0x21;
391 pco.v[3] = 0x10; /* Length of contents */
392 pco.v[4] = 0x02; /* ACK */
393 pco.v[5] = 0x00; /* ID: Need to match request */
394 pco.v[6] = 0x00; /* Length */
395 pco.v[7] = 0x10;
396 pco.v[8] = 0x81; /* DNS 1 */
397 pco.v[9] = 0x06;
398 memcpy(&pco.v[10], &dns1, sizeof(dns1));
399 pco.v[14] = 0x83;
400 pco.v[15] = 0x06; /* DNS 2 */
401 memcpy(&pco.v[16], &dns2, sizeof(dns2));
402
jjako4b26b512003-01-28 16:13:57 +0000403 /* ipup */
404 ipup = args_info.ipup_arg;
405
406 /* ipdown */
407 ipdown = args_info.ipdown_arg;
408
jjako52c24142002-12-16 13:33:51 +0000409 /* Timelimit */
410 timelimit = args_info.timelimit_arg;
411 starttime = time(NULL);
412
413 /* qos */
414 qos.l = 3;
jjako52c24142002-12-16 13:33:51 +0000415 qos.v[2] = (args_info.qos_arg) & 0xff;
416 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
417 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
jjakoa7cd2492003-04-11 09:40:12 +0000418
jjako52c24142002-12-16 13:33:51 +0000419 /* apn */
jjakoa7cd2492003-04-11 09:40:12 +0000420 if (strlen(args_info.apn_arg) > (sizeof(apn.v)-1)) {
421 printf("Invalid APN\n");
422 return -1;
jjako52c24142002-12-16 13:33:51 +0000423 }
424 apn.l = strlen(args_info.apn_arg) + 1;
jjako52c24142002-12-16 13:33:51 +0000425 apn.v[0] = (char) strlen(args_info.apn_arg);
Harald Weltef54a1f42010-05-04 11:08:38 +0200426 strncpy((char *) &apn.v[1], args_info.apn_arg, sizeof(apn.v)-1);
jjakoe0149782003-07-06 17:07:04 +0000427
428
429 /* foreground */
430 /* If flag not given run as a daemon */
431 if (!args_info.fg_flag)
432 {
Emmanuel Bretelle68521862010-09-07 15:46:40 +0200433 FILE *f;
434 int rc;
jjakoe0149782003-07-06 17:07:04 +0000435 closelog();
436 /* Close the standard file descriptors. */
437 /* Is this really needed ? */
Emmanuel Bretelle68521862010-09-07 15:46:40 +0200438 f = freopen("/dev/null", "w", stdout);
439 if (f == NULL) {
440 sys_err(LOG_WARNING, __FILE__, __LINE__, 0, "Could not redirect stdout to /dev/null");
441 }
442 f = freopen("/dev/null", "w", stderr);
443 if (f == NULL) {
444 sys_err(LOG_WARNING, __FILE__, __LINE__, 0, "Could not redirect stderr to /dev/null");
445 }
446 f = freopen("/dev/null", "r", stdin);
447 if (f == NULL) {
448 sys_err(LOG_WARNING, __FILE__, __LINE__, 0, "Could not redirect stdin to /dev/null");
449 }
450 rc = daemon(0, 0);
451 if (rc != 0) {
452 sys_err(LOG_ERR, __FILE__, __LINE__, rc, "Could not daemonize");
453 exit(1);
454 }
jjakoe0149782003-07-06 17:07:04 +0000455 /* Open log again. This time with new pid */
456 openlog(PACKAGE, LOG_PID, LOG_DAEMON);
457 }
458
459 /* pidfile */
460 /* This has to be done after we have our final pid */
461 if (args_info.pidfile_arg) {
462 log_pid(args_info.pidfile_arg);
463 }
jjakoa7cd2492003-04-11 09:40:12 +0000464
jjako52c24142002-12-16 13:33:51 +0000465
466 if (debug) printf("gtpclient: Initialising GTP tunnel\n");
467
jjako1db1c812003-07-06 20:53:57 +0000468 if (gtp_new(&gsn, args_info.statedir_arg, &listen_, GTP_MODE_GGSN)) {
jjakoa7cd2492003-04-11 09:40:12 +0000469 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
470 "Failed to create gtp");
471 exit(1);
472 }
jjako08d331d2003-10-13 20:33:30 +0000473 if (gsn->fd0 > maxfd) maxfd = gsn->fd0;
474 if (gsn->fd1c > maxfd) maxfd = gsn->fd1c;
475 if (gsn->fd1u > maxfd) maxfd = gsn->fd1u;
jjako52c24142002-12-16 13:33:51 +0000476
jjako08d331d2003-10-13 20:33:30 +0000477 gtp_set_cb_data_ind(gsn, encaps_tun);
jjako52c24142002-12-16 13:33:51 +0000478 gtp_set_cb_delete_context(gsn, delete_context);
jjako08d331d2003-10-13 20:33:30 +0000479 gtp_set_cb_create_context_ind(gsn, create_context_ind);
jjakoa7cd2492003-04-11 09:40:12 +0000480
481
482 /* Create a tunnel interface */
jjakoc6762cf2004-04-28 14:52:58 +0000483 if (debug) printf("Creating tun interface\n");
jjakoa7cd2492003-04-11 09:40:12 +0000484 if (tun_new((struct tun_t**) &tun)) {
485 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
486 "Failed to create tun");
jjakoc6762cf2004-04-28 14:52:58 +0000487 if (debug) printf("Failed to create tun\n");
jjakoa7cd2492003-04-11 09:40:12 +0000488 exit(1);
489 }
490
jjakoc6762cf2004-04-28 14:52:58 +0000491 if (debug) printf("Setting tun IP address\n");
492 if (tun_setaddr(tun, &netaddr, &destaddr, &mask)) {
493 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
494 "Failed to set tun IP address");
495 if (debug) printf("Failed to set tun IP address\n");
496 exit(1);
497 }
498
499
jjakoa7cd2492003-04-11 09:40:12 +0000500 tun_set_cb_ind(tun, cb_tun_ind);
501 if (tun->fd > maxfd) maxfd = tun->fd;
jjakoc6762cf2004-04-28 14:52:58 +0000502
jjako9c7ff082003-04-11 10:01:41 +0000503 if (ipup) tun_runscript(tun, ipup);
jjako52c24142002-12-16 13:33:51 +0000504
505 /******************************************************************/
506 /* Main select loop */
507 /******************************************************************/
508
Harald Weltec3dcba02010-05-04 11:02:54 +0200509 while ((((starttime + timelimit) > time(NULL)) || (0 == timelimit)) && (!end)) {
510
jjako52c24142002-12-16 13:33:51 +0000511 FD_ZERO(&fds);
jjakoa7cd2492003-04-11 09:40:12 +0000512 if (tun) FD_SET(tun->fd, &fds);
jjako08d331d2003-10-13 20:33:30 +0000513 FD_SET(gsn->fd0, &fds);
514 FD_SET(gsn->fd1c, &fds);
515 FD_SET(gsn->fd1u, &fds);
jjako52c24142002-12-16 13:33:51 +0000516
517 gtp_retranstimeout(gsn, &idleTime);
518 switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
jjakoe0149782003-07-06 17:07:04 +0000519 case -1: /* errno == EINTR : unblocked signal */
520 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
521 "select() returned -1");
Harald Welte5d064ec2010-05-04 11:04:54 +0200522 /* On error, select returns without modifying fds */
523 FD_ZERO(&fds);
jjako52c24142002-12-16 13:33:51 +0000524 break;
525 case 0:
jjakoa7cd2492003-04-11 09:40:12 +0000526 /* printf("Select returned 0\n"); */
jjako52c24142002-12-16 13:33:51 +0000527 gtp_retrans(gsn); /* Only retransmit if nothing else */
528 break;
529 default:
530 break;
531 }
532
jjakoa7cd2492003-04-11 09:40:12 +0000533 if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
534 tun_decaps(tun) < 0) {
jjakoe0149782003-07-06 17:07:04 +0000535 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
536 "TUN read failed (fd)=(%d)", tun->fd);
jjako52c24142002-12-16 13:33:51 +0000537 }
jjako08d331d2003-10-13 20:33:30 +0000538
539 if (FD_ISSET(gsn->fd0, &fds))
540 gtp_decaps0(gsn);
541
542 if (FD_ISSET(gsn->fd1c, &fds))
543 gtp_decaps1c(gsn);
544
545 if (FD_ISSET(gsn->fd1u, &fds))
546 gtp_decaps1u(gsn);
jjako52c24142002-12-16 13:33:51 +0000547
jjako4b26b512003-01-28 16:13:57 +0000548 }
jjako52c24142002-12-16 13:33:51 +0000549
Harald Welte1b3e5772010-05-04 11:13:56 +0200550 cmdline_parser_free(&args_info);
Harald Welte5701b0f2010-05-04 11:12:23 +0200551 ippool_free(ippool);
jjako52c24142002-12-16 13:33:51 +0000552 gtp_free(gsn);
jjakoa7cd2492003-04-11 09:40:12 +0000553 tun_free(tun);
jjako52c24142002-12-16 13:33:51 +0000554
555 return 1;
556
557}
558