blob: 0022b1b74b8cc6da85f2c6275e8007873db72f88 [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
Harald Weltebed35df2011-11-02 13:06:18 +010042#include <sys/socket.h>
jjako52c24142002-12-16 13:33:51 +000043#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;
Harald Weltebed35df2011-11-02 13:06:18 +010058int maxfd = 0; /* For select() */
jjakoa7cd2492003-04-11 09:40:12 +000059
60struct in_addr listen_;
Harald Weltebed35df2011-11-02 13:06:18 +010061struct in_addr netaddr, destaddr, net, mask; /* Network interface */
62struct in_addr dns1, dns2; /* PCO DNS address */
63char *ipup, *ipdown; /* Filename of scripts */
64int debug; /* Print debug output */
jjakoa7cd2492003-04-11 09:40:12 +000065struct ul255_t pco;
66struct ul255_t qos;
67struct ul255_t apn;
68
Harald Weltebed35df2011-11-02 13:06:18 +010069struct gsn_t *gsn; /* GSN instance */
70struct 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 */
Harald Weltebed35df2011-11-02 13:06:18 +010074void signal_handler(int s)
75{
76 if (debug)
77 printf("Received signal %d, exiting.\n", s);
78 end = 1;
Harald Weltec3dcba02010-05-04 11:02:54 +020079}
jjako52c24142002-12-16 13:33:51 +000080
81/* Used to write process ID to file. Assume someone else will delete */
Harald Weltebed35df2011-11-02 13:06:18 +010082void log_pid(char *pidfile)
83{
84 FILE *file;
85 mode_t oldmask;
86
87 oldmask = umask(022);
88 file = fopen(pidfile, "w");
89 umask(oldmask);
90 if (!file) {
91 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
92 "Failed to create process ID file: %s!", pidfile);
93 return;
94 }
95 fprintf(file, "%d\n", (int)getpid());
96 fclose(file);
jjako52c24142002-12-16 13:33:51 +000097}
98
jjakobd937b72004-12-30 16:22:42 +000099#if defined(__sun__)
Harald Weltebed35df2011-11-02 13:06:18 +0100100int daemon(int nochdir, int noclose)
101{
102 int fd;
jjako0141d202004-01-09 15:19:20 +0000103
Harald Weltebed35df2011-11-02 13:06:18 +0100104 switch (fork()) {
105 case -1:
106 return (-1);
107 case 0:
108 break;
109 default:
110 _exit(0);
111 }
jjako0141d202004-01-09 15:19:20 +0000112
Harald Weltebed35df2011-11-02 13:06:18 +0100113 if (setsid() == -1)
114 return (-1);
jjako0141d202004-01-09 15:19:20 +0000115
Harald Weltebed35df2011-11-02 13:06:18 +0100116 if (!nochdir)
117 chdir("/");
118
119 if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
120 dup2(fd, STDIN_FILENO);
121 dup2(fd, STDOUT_FILENO);
122 dup2(fd, STDERR_FILENO);
123 if (fd > 2)
124 close(fd);
125 }
126 return (0);
jjako0141d202004-01-09 15:19:20 +0000127}
128#endif
129
jjako52c24142002-12-16 13:33:51 +0000130int encaps_printf(void *p, void *packet, unsigned len)
131{
Harald Weltebed35df2011-11-02 13:06:18 +0100132 unsigned int i;
133 if (debug) {
134 printf("The packet looks like this:\n");
135 for (i = 0; i < len; i++) {
136 printf("%02x ", (unsigned char)*(char *)(packet + i));
137 if (!((i + 1) % 16))
138 printf("\n");
139 };
140 printf("\n");
141 }
142 return 0;
jjako52c24142002-12-16 13:33:51 +0000143}
144
Harald Weltebed35df2011-11-02 13:06:18 +0100145int delete_context(struct pdp_t *pdp)
146{
147 if (debug)
148 printf("Deleting PDP context\n");
149 if (pdp->peer)
150 ippool_freeip(ippool, (struct ippoolm_t *)pdp->peer);
151 else
152 sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Peer not defined!");
153 return 0;
jjako52c24142002-12-16 13:33:51 +0000154}
155
Harald Weltebed35df2011-11-02 13:06:18 +0100156int create_context_ind(struct pdp_t *pdp)
157{
158 struct in_addr addr;
159 struct ippoolm_t *member;
jjako52c24142002-12-16 13:33:51 +0000160
Harald Weltebed35df2011-11-02 13:06:18 +0100161 if (debug)
162 printf("Received create PDP context request\n");
jjako52c24142002-12-16 13:33:51 +0000163
Harald Weltebed35df2011-11-02 13:06:18 +0100164 pdp->eua.l = 0; /* TODO: Indicates dynamic IP */
jjako52c24142002-12-16 13:33:51 +0000165
Harald Weltebed35df2011-11-02 13:06:18 +0100166 /* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */
167 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_req0));
168 memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg));
jjako52c24142002-12-16 13:33:51 +0000169
Harald Weltebed35df2011-11-02 13:06:18 +0100170 memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */
171 pdp->qos_neg.l = pdp->qos_req.l;
jjako52c24142002-12-16 13:33:51 +0000172
Harald Weltebed35df2011-11-02 13:06:18 +0100173 if (pdp_euaton(&pdp->eua, &addr)) {
174 addr.s_addr = 0; /* Request dynamic */
175 }
jjakoa7cd2492003-04-11 09:40:12 +0000176
Harald Weltebed35df2011-11-02 13:06:18 +0100177 if (ippool_newip(ippool, &member, &addr, 0)) {
178 gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES);
179 return 0; /* Allready in use, or no more available */
180 }
jjakoa7cd2492003-04-11 09:40:12 +0000181
Harald Weltebed35df2011-11-02 13:06:18 +0100182 pdp_ntoeua(&member->addr, &pdp->eua);
183 pdp->peer = member;
184 pdp->ipif = tun; /* TODO */
185 member->peer = pdp;
jjako52c24142002-12-16 13:33:51 +0000186
Harald Weltebed35df2011-11-02 13:06:18 +0100187 gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ);
188 return 0; /* Success */
jjako52c24142002-12-16 13:33:51 +0000189}
190
jjakoa7cd2492003-04-11 09:40:12 +0000191/* Callback for receiving messages from tun */
Harald Weltebed35df2011-11-02 13:06:18 +0100192int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len)
193{
194 struct ippoolm_t *ipm;
195 struct in_addr dst;
196 struct tun_packet_t *iph = (struct tun_packet_t *)pack;
jjakoc6762cf2004-04-28 14:52:58 +0000197
Harald Weltebed35df2011-11-02 13:06:18 +0100198 dst.s_addr = iph->dst;
jjakoc6762cf2004-04-28 14:52:58 +0000199
Harald Weltebed35df2011-11-02 13:06:18 +0100200 if (debug)
201 printf("Received packet from tun!\n");
202
203 if (ippool_getip(ippool, &ipm, &dst)) {
204 if (debug)
205 printf("Received packet with no destination!!!\n");
206 return 0;
207 }
208
209 if (ipm->peer) /* Check if a peer protocol is defined */
210 gtp_data_req(gsn, (struct pdp_t *)ipm->peer, pack, len);
211 return 0;
jjako52c24142002-12-16 13:33:51 +0000212}
213
Harald Weltebed35df2011-11-02 13:06:18 +0100214int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len)
215{
216 if (debug)
217 printf("encaps_tun. Packet received: forwarding to tun\n");
218 return tun_encaps((struct tun_t *)pdp->ipif, pack, len);
jjako52c24142002-12-16 13:33:51 +0000219}
220
jjako52c24142002-12-16 13:33:51 +0000221int main(int argc, char **argv)
222{
Harald Weltebed35df2011-11-02 13:06:18 +0100223 /* gengeopt declarations */
224 struct gengetopt_args_info args_info;
jjako52c24142002-12-16 13:33:51 +0000225
Harald Weltebed35df2011-11-02 13:06:18 +0100226 struct hostent *host;
jjako52c24142002-12-16 13:33:51 +0000227
Harald Weltebed35df2011-11-02 13:06:18 +0100228 /* Handle keyboard interrupt SIGINT */
229 struct sigaction s;
230 s.sa_handler = (void *)signal_handler;
231 if ((0 != sigemptyset(&s.sa_mask)) && debug)
232 printf("sigemptyset failed.\n");
233 s.sa_flags = SA_RESETHAND;
234 if ((sigaction(SIGINT, &s, NULL) != 0) && debug)
235 printf("Could not register SIGINT signal handler.\n");
jjako52c24142002-12-16 13:33:51 +0000236
Harald Weltebed35df2011-11-02 13:06:18 +0100237 fd_set fds; /* For select() */
238 struct timeval idleTime; /* How long to select() */
jjako52c24142002-12-16 13:33:51 +0000239
Harald Weltebed35df2011-11-02 13:06:18 +0100240 int timelimit; /* Number of seconds to be connected */
241 int starttime; /* Time program was started */
jjako52c24142002-12-16 13:33:51 +0000242
Harald Weltebed35df2011-11-02 13:06:18 +0100243 /* open a connection to the syslog daemon */
244 /*openlog(PACKAGE, LOG_PID, LOG_DAEMON); */
jjako0141d202004-01-09 15:19:20 +0000245
Harald Weltebed35df2011-11-02 13:06:18 +0100246 /* TODO: Only use LOG__PERROR for linux */
jjako0141d202004-01-09 15:19:20 +0000247#ifdef __linux__
Harald Weltebed35df2011-11-02 13:06:18 +0100248 openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON);
jjako0141d202004-01-09 15:19:20 +0000249#else
Harald Weltebed35df2011-11-02 13:06:18 +0100250 openlog(PACKAGE, (LOG_PID), LOG_DAEMON);
jjako0141d202004-01-09 15:19:20 +0000251#endif
252
Harald Weltebed35df2011-11-02 13:06:18 +0100253 if (cmdline_parser(argc, argv, &args_info) != 0)
254 exit(1);
255 if (args_info.debug_flag) {
256 printf("listen: %s\n", args_info.listen_arg);
257 if (args_info.conf_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);
262 if (args_info.apn_arg)
263 printf("apn: %s\n", args_info.apn_arg);
264 if (args_info.net_arg)
265 printf("net: %s\n", args_info.net_arg);
266 if (args_info.dynip_arg)
267 printf("dynip: %s\n", args_info.dynip_arg);
268 if (args_info.statip_arg)
269 printf("statip: %s\n", args_info.statip_arg);
270 if (args_info.ipup_arg)
271 printf("ipup: %s\n", args_info.ipup_arg);
272 if (args_info.ipdown_arg)
273 printf("ipdown: %s\n", args_info.ipdown_arg);
274 if (args_info.pidfile_arg)
275 printf("pidfile: %s\n", args_info.pidfile_arg);
276 if (args_info.statedir_arg)
277 printf("statedir: %s\n", args_info.statedir_arg);
278 printf("timelimit: %d\n", args_info.timelimit_arg);
279 }
jjako52c24142002-12-16 13:33:51 +0000280
Harald Weltebed35df2011-11-02 13:06:18 +0100281 /* Try out our new parser */
jjako52c24142002-12-16 13:33:51 +0000282
Harald Weltebed35df2011-11-02 13:06:18 +0100283 if (cmdline_parser_configfile(args_info.conf_arg, &args_info, 0, 0, 0)
284 != 0)
285 exit(1);
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100286
287 /* Open a log file */
288 if (args_info.logfile_arg) {
289 FILE* log_file = fopen(args_info.logfile_arg, "a");
290 if (!log_file) {
291 printf("Failed to open logfile: '%s'\n",
292 args_info.logfile_arg);
293 exit(1);
294 }
295 sys_err_setlogfile(log_file);
296 }
297
Harald Weltebed35df2011-11-02 13:06:18 +0100298 if (args_info.debug_flag) {
299 printf("cmdline_parser_configfile\n");
300 printf("listen: %s\n", args_info.listen_arg);
301 printf("conf: %s\n", args_info.conf_arg);
302 printf("fg: %d\n", args_info.fg_flag);
303 printf("debug: %d\n", args_info.debug_flag);
304 printf("qos: %#08x\n", args_info.qos_arg);
305 if (args_info.apn_arg)
306 printf("apn: %s\n", args_info.apn_arg);
307 if (args_info.net_arg)
308 printf("net: %s\n", args_info.net_arg);
309 if (args_info.dynip_arg)
310 printf("dynip: %s\n", args_info.dynip_arg);
311 if (args_info.statip_arg)
312 printf("statip: %s\n", args_info.statip_arg);
313 if (args_info.ipup_arg)
314 printf("ipup: %s\n", args_info.ipup_arg);
315 if (args_info.ipdown_arg)
316 printf("ipdown: %s\n", args_info.ipdown_arg);
317 if (args_info.pidfile_arg)
318 printf("pidfile: %s\n", args_info.pidfile_arg);
319 if (args_info.statedir_arg)
320 printf("statedir: %s\n", args_info.statedir_arg);
321 printf("timelimit: %d\n", args_info.timelimit_arg);
322 }
jjako52c24142002-12-16 13:33:51 +0000323
Harald Weltebed35df2011-11-02 13:06:18 +0100324 /* Handle each option */
jjako52c24142002-12-16 13:33:51 +0000325
Harald Weltebed35df2011-11-02 13:06:18 +0100326 /* debug */
327 debug = args_info.debug_flag;
jjako52c24142002-12-16 13:33:51 +0000328
Harald Weltebed35df2011-11-02 13:06:18 +0100329 /* listen */
330 /* Do hostname lookup to translate hostname to IP address */
331 /* Any port listening is not possible as a valid address is */
332 /* required for create_pdp_context_response messages */
333 if (args_info.listen_arg) {
334 if (!(host = gethostbyname(args_info.listen_arg))) {
335 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
336 "Invalid listening address: %s!",
337 args_info.listen_arg);
338 exit(1);
339 } else {
340 memcpy(&listen_.s_addr, host->h_addr, host->h_length);
341 }
342 } else {
343 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
344 "Listening address must be specified! "
345 "Please use command line option --listen or "
346 "edit %s configuration file\n", args_info.conf_arg);
347 exit(1);
348 }
jjako88c22162003-07-06 19:33:18 +0000349
Harald Weltebed35df2011-11-02 13:06:18 +0100350 /* net */
351 /* Store net as in_addr net and mask */
352 if (args_info.net_arg) {
353 if (ippool_aton(&net, &mask, args_info.net_arg, 0)) {
354 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
355 "Invalid network address: %s!",
356 args_info.net_arg);
357 exit(1);
358 }
359 netaddr.s_addr = htonl(ntohl(net.s_addr) + 1);
360 destaddr.s_addr = htonl(ntohl(net.s_addr) + 1);
361 } else {
362 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
363 "Network address must be specified: %s!",
364 args_info.net_arg);
365 exit(1);
366 }
jjako52c24142002-12-16 13:33:51 +0000367
Harald Weltebed35df2011-11-02 13:06:18 +0100368 /* dynip */
369 if (!args_info.dynip_arg) {
370 if (ippool_new(&ippool, args_info.net_arg, NULL, 1, 0,
371 IPPOOL_NONETWORK | IPPOOL_NOGATEWAY |
372 IPPOOL_NOBROADCAST)) {
373 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
374 "Failed to allocate IP pool!");
375 exit(1);
376 }
377 } else {
378 if (ippool_new(&ippool, args_info.dynip_arg, NULL, 1, 0,
379 IPPOOL_NONETWORK | IPPOOL_NOGATEWAY |
380 IPPOOL_NOBROADCAST)) {
381 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
382 "Failed to allocate IP pool!");
383 exit(1);
384 }
385 }
jjako52c24142002-12-16 13:33:51 +0000386
Harald Weltebed35df2011-11-02 13:06:18 +0100387 /* DNS1 and DNS2 */
jjakoff9985c2004-01-16 11:05:22 +0000388#ifdef HAVE_INET_ATON
Harald Weltebed35df2011-11-02 13:06:18 +0100389 dns1.s_addr = 0;
390 if (args_info.pcodns1_arg) {
391 if (0 == inet_aton(args_info.pcodns1_arg, &dns1)) {
392 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
393 "Failed to convert pcodns1!");
394 exit(1);
395 }
396 }
397 dns2.s_addr = 0;
398 if (args_info.pcodns2_arg) {
399 if (0 == inet_aton(args_info.pcodns2_arg, &dns2)) {
400 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
401 "Failed to convert pcodns2!");
402 exit(1);
403 }
404 }
jjako1d3db972004-01-16 09:56:56 +0000405#else
Harald Weltebed35df2011-11-02 13:06:18 +0100406 dns1.s_addr = 0;
407 if (args_info.pcodns1_arg) {
408 dns1.s_addr = inet_addr(args_info.pcodns1_arg);
409 if (dns1.s_addr == -1) {
410 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
411 "Failed to convert pcodns1!");
412 exit(1);
413 }
414 }
415 dns2.s_addr = 0;
416 if (args_info.pcodns2_arg) {
417 dns2.s_addr = inet_addr(args_info.pcodns2_arg);
418 if (dns2.s_addr == -1) {
419 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
420 "Failed to convert pcodns2!");
421 exit(1);
422 }
423 }
jjako1d3db972004-01-16 09:56:56 +0000424#endif
425
Harald Weltebed35df2011-11-02 13:06:18 +0100426 pco.l = 20;
427 pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */
428 pco.v[1] = 0x80; /* IPCP */
429 pco.v[2] = 0x21;
430 pco.v[3] = 0x10; /* Length of contents */
431 pco.v[4] = 0x02; /* ACK */
432 pco.v[5] = 0x00; /* ID: Need to match request */
433 pco.v[6] = 0x00; /* Length */
434 pco.v[7] = 0x10;
435 pco.v[8] = 0x81; /* DNS 1 */
436 pco.v[9] = 0x06;
437 memcpy(&pco.v[10], &dns1, sizeof(dns1));
438 pco.v[14] = 0x83;
439 pco.v[15] = 0x06; /* DNS 2 */
440 memcpy(&pco.v[16], &dns2, sizeof(dns2));
jjakoa7cd2492003-04-11 09:40:12 +0000441
Harald Weltebed35df2011-11-02 13:06:18 +0100442 /* ipup */
443 ipup = args_info.ipup_arg;
jjakoa7cd2492003-04-11 09:40:12 +0000444
Harald Weltebed35df2011-11-02 13:06:18 +0100445 /* ipdown */
446 ipdown = args_info.ipdown_arg;
jjako4b26b512003-01-28 16:13:57 +0000447
Harald Weltebed35df2011-11-02 13:06:18 +0100448 /* Timelimit */
449 timelimit = args_info.timelimit_arg;
450 starttime = time(NULL);
jjako4b26b512003-01-28 16:13:57 +0000451
Harald Weltebed35df2011-11-02 13:06:18 +0100452 /* qos */
453 qos.l = 3;
454 qos.v[2] = (args_info.qos_arg) & 0xff;
455 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
456 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
jjakoa7cd2492003-04-11 09:40:12 +0000457
Harald Weltebed35df2011-11-02 13:06:18 +0100458 /* apn */
459 if (strlen(args_info.apn_arg) > (sizeof(apn.v) - 1)) {
460 printf("Invalid APN\n");
461 return -1;
462 }
463 apn.l = strlen(args_info.apn_arg) + 1;
464 apn.v[0] = (char)strlen(args_info.apn_arg);
465 strncpy((char *)&apn.v[1], args_info.apn_arg, sizeof(apn.v) - 1);
jjakoe0149782003-07-06 17:07:04 +0000466
Harald Weltebed35df2011-11-02 13:06:18 +0100467 /* foreground */
468 /* If flag not given run as a daemon */
469 if (!args_info.fg_flag) {
470 FILE *f;
471 int rc;
472 closelog();
473 /* Close the standard file descriptors. */
474 /* Is this really needed ? */
475 f = freopen("/dev/null", "w", stdout);
476 if (f == NULL) {
477 sys_err(LOG_WARNING, __FILE__, __LINE__, 0,
478 "Could not redirect stdout to /dev/null");
479 }
480 f = freopen("/dev/null", "w", stderr);
481 if (f == NULL) {
482 sys_err(LOG_WARNING, __FILE__, __LINE__, 0,
483 "Could not redirect stderr to /dev/null");
484 }
485 f = freopen("/dev/null", "r", stdin);
486 if (f == NULL) {
487 sys_err(LOG_WARNING, __FILE__, __LINE__, 0,
488 "Could not redirect stdin to /dev/null");
489 }
490 rc = daemon(0, 0);
491 if (rc != 0) {
492 sys_err(LOG_ERR, __FILE__, __LINE__, rc,
493 "Could not daemonize");
494 exit(1);
495 }
496 /* Open log again. This time with new pid */
497 openlog(PACKAGE, LOG_PID, LOG_DAEMON);
498 }
jjakoe0149782003-07-06 17:07:04 +0000499
Harald Weltebed35df2011-11-02 13:06:18 +0100500 /* pidfile */
501 /* This has to be done after we have our final pid */
502 if (args_info.pidfile_arg) {
503 log_pid(args_info.pidfile_arg);
504 }
jjakoe0149782003-07-06 17:07:04 +0000505
Harald Weltebed35df2011-11-02 13:06:18 +0100506 if (debug)
507 printf("gtpclient: Initialising GTP tunnel\n");
jjako52c24142002-12-16 13:33:51 +0000508
Harald Weltebed35df2011-11-02 13:06:18 +0100509 if (gtp_new(&gsn, args_info.statedir_arg, &listen_, GTP_MODE_GGSN)) {
510 sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Failed to create gtp");
511 exit(1);
512 }
513 if (gsn->fd0 > maxfd)
514 maxfd = gsn->fd0;
515 if (gsn->fd1c > maxfd)
516 maxfd = gsn->fd1c;
517 if (gsn->fd1u > maxfd)
518 maxfd = gsn->fd1u;
jjako52c24142002-12-16 13:33:51 +0000519
Harald Weltebed35df2011-11-02 13:06:18 +0100520 gtp_set_cb_data_ind(gsn, encaps_tun);
521 gtp_set_cb_delete_context(gsn, delete_context);
522 gtp_set_cb_create_context_ind(gsn, create_context_ind);
jjakoa7cd2492003-04-11 09:40:12 +0000523
Harald Weltebed35df2011-11-02 13:06:18 +0100524 /* Create a tunnel interface */
525 if (debug)
526 printf("Creating tun interface\n");
527 if (tun_new((struct tun_t **)&tun)) {
528 sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Failed to create tun");
529 if (debug)
530 printf("Failed to create tun\n");
531 exit(1);
532 }
jjakoa7cd2492003-04-11 09:40:12 +0000533
Harald Weltebed35df2011-11-02 13:06:18 +0100534 if (debug)
535 printf("Setting tun IP address\n");
536 if (tun_setaddr(tun, &netaddr, &destaddr, &mask)) {
537 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
538 "Failed to set tun IP address");
539 if (debug)
540 printf("Failed to set tun IP address\n");
541 exit(1);
542 }
jjakoa7cd2492003-04-11 09:40:12 +0000543
Harald Weltebed35df2011-11-02 13:06:18 +0100544 tun_set_cb_ind(tun, cb_tun_ind);
545 if (tun->fd > maxfd)
546 maxfd = tun->fd;
jjakoc6762cf2004-04-28 14:52:58 +0000547
Harald Weltebed35df2011-11-02 13:06:18 +0100548 if (ipup)
549 tun_runscript(tun, ipup);
jjako52c24142002-12-16 13:33:51 +0000550
551 /******************************************************************/
Harald Weltebed35df2011-11-02 13:06:18 +0100552 /* Main select loop */
jjako52c24142002-12-16 13:33:51 +0000553 /******************************************************************/
554
Harald Weltebed35df2011-11-02 13:06:18 +0100555 while ((((starttime + timelimit) > time(NULL)) || (0 == timelimit))
556 && (!end)) {
Harald Weltec3dcba02010-05-04 11:02:54 +0200557
Harald Weltebed35df2011-11-02 13:06:18 +0100558 FD_ZERO(&fds);
559 if (tun)
560 FD_SET(tun->fd, &fds);
561 FD_SET(gsn->fd0, &fds);
562 FD_SET(gsn->fd1c, &fds);
563 FD_SET(gsn->fd1u, &fds);
jjako52c24142002-12-16 13:33:51 +0000564
Harald Weltebed35df2011-11-02 13:06:18 +0100565 gtp_retranstimeout(gsn, &idleTime);
566 switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
567 case -1: /* errno == EINTR : unblocked signal */
568 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
569 "select() returned -1");
570 /* On error, select returns without modifying fds */
571 FD_ZERO(&fds);
572 break;
573 case 0:
574 /* printf("Select returned 0\n"); */
575 gtp_retrans(gsn); /* Only retransmit if nothing else */
576 break;
577 default:
578 break;
579 }
jjako52c24142002-12-16 13:33:51 +0000580
Harald Weltebed35df2011-11-02 13:06:18 +0100581 if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
582 tun_decaps(tun) < 0) {
583 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
584 "TUN read failed (fd)=(%d)", tun->fd);
585 }
586
587 if (FD_ISSET(gsn->fd0, &fds))
588 gtp_decaps0(gsn);
589
590 if (FD_ISSET(gsn->fd1c, &fds))
591 gtp_decaps1c(gsn);
592
593 if (FD_ISSET(gsn->fd1u, &fds))
594 gtp_decaps1u(gsn);
595
596 }
597
598 cmdline_parser_free(&args_info);
599 ippool_free(ippool);
600 gtp_free(gsn);
601 tun_free(tun);
602
603 return 1;
604
jjako52c24142002-12-16 13:33:51 +0000605}