blob: 1152519aaf2a7addc2efc533be36126aea3b756b [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);
286 if (args_info.debug_flag) {
287 printf("cmdline_parser_configfile\n");
288 printf("listen: %s\n", args_info.listen_arg);
289 printf("conf: %s\n", args_info.conf_arg);
290 printf("fg: %d\n", args_info.fg_flag);
291 printf("debug: %d\n", args_info.debug_flag);
292 printf("qos: %#08x\n", args_info.qos_arg);
293 if (args_info.apn_arg)
294 printf("apn: %s\n", args_info.apn_arg);
295 if (args_info.net_arg)
296 printf("net: %s\n", args_info.net_arg);
297 if (args_info.dynip_arg)
298 printf("dynip: %s\n", args_info.dynip_arg);
299 if (args_info.statip_arg)
300 printf("statip: %s\n", args_info.statip_arg);
301 if (args_info.ipup_arg)
302 printf("ipup: %s\n", args_info.ipup_arg);
303 if (args_info.ipdown_arg)
304 printf("ipdown: %s\n", args_info.ipdown_arg);
305 if (args_info.pidfile_arg)
306 printf("pidfile: %s\n", args_info.pidfile_arg);
307 if (args_info.statedir_arg)
308 printf("statedir: %s\n", args_info.statedir_arg);
309 printf("timelimit: %d\n", args_info.timelimit_arg);
310 }
jjako52c24142002-12-16 13:33:51 +0000311
Harald Weltebed35df2011-11-02 13:06:18 +0100312 /* Handle each option */
jjako52c24142002-12-16 13:33:51 +0000313
Harald Weltebed35df2011-11-02 13:06:18 +0100314 /* debug */
315 debug = args_info.debug_flag;
jjako52c24142002-12-16 13:33:51 +0000316
Harald Weltebed35df2011-11-02 13:06:18 +0100317 /* listen */
318 /* Do hostname lookup to translate hostname to IP address */
319 /* Any port listening is not possible as a valid address is */
320 /* required for create_pdp_context_response messages */
321 if (args_info.listen_arg) {
322 if (!(host = gethostbyname(args_info.listen_arg))) {
323 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
324 "Invalid listening address: %s!",
325 args_info.listen_arg);
326 exit(1);
327 } else {
328 memcpy(&listen_.s_addr, host->h_addr, host->h_length);
329 }
330 } else {
331 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
332 "Listening address must be specified! "
333 "Please use command line option --listen or "
334 "edit %s configuration file\n", args_info.conf_arg);
335 exit(1);
336 }
jjako88c22162003-07-06 19:33:18 +0000337
Harald Weltebed35df2011-11-02 13:06:18 +0100338 /* net */
339 /* Store net as in_addr net and mask */
340 if (args_info.net_arg) {
341 if (ippool_aton(&net, &mask, args_info.net_arg, 0)) {
342 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
343 "Invalid network address: %s!",
344 args_info.net_arg);
345 exit(1);
346 }
347 netaddr.s_addr = htonl(ntohl(net.s_addr) + 1);
348 destaddr.s_addr = htonl(ntohl(net.s_addr) + 1);
349 } else {
350 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
351 "Network address must be specified: %s!",
352 args_info.net_arg);
353 exit(1);
354 }
jjako52c24142002-12-16 13:33:51 +0000355
Harald Weltebed35df2011-11-02 13:06:18 +0100356 /* dynip */
357 if (!args_info.dynip_arg) {
358 if (ippool_new(&ippool, args_info.net_arg, NULL, 1, 0,
359 IPPOOL_NONETWORK | IPPOOL_NOGATEWAY |
360 IPPOOL_NOBROADCAST)) {
361 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
362 "Failed to allocate IP pool!");
363 exit(1);
364 }
365 } else {
366 if (ippool_new(&ippool, args_info.dynip_arg, NULL, 1, 0,
367 IPPOOL_NONETWORK | IPPOOL_NOGATEWAY |
368 IPPOOL_NOBROADCAST)) {
369 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
370 "Failed to allocate IP pool!");
371 exit(1);
372 }
373 }
jjako52c24142002-12-16 13:33:51 +0000374
Harald Weltebed35df2011-11-02 13:06:18 +0100375 /* DNS1 and DNS2 */
jjakoff9985c2004-01-16 11:05:22 +0000376#ifdef HAVE_INET_ATON
Harald Weltebed35df2011-11-02 13:06:18 +0100377 dns1.s_addr = 0;
378 if (args_info.pcodns1_arg) {
379 if (0 == inet_aton(args_info.pcodns1_arg, &dns1)) {
380 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
381 "Failed to convert pcodns1!");
382 exit(1);
383 }
384 }
385 dns2.s_addr = 0;
386 if (args_info.pcodns2_arg) {
387 if (0 == inet_aton(args_info.pcodns2_arg, &dns2)) {
388 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
389 "Failed to convert pcodns2!");
390 exit(1);
391 }
392 }
jjako1d3db972004-01-16 09:56:56 +0000393#else
Harald Weltebed35df2011-11-02 13:06:18 +0100394 dns1.s_addr = 0;
395 if (args_info.pcodns1_arg) {
396 dns1.s_addr = inet_addr(args_info.pcodns1_arg);
397 if (dns1.s_addr == -1) {
398 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
399 "Failed to convert pcodns1!");
400 exit(1);
401 }
402 }
403 dns2.s_addr = 0;
404 if (args_info.pcodns2_arg) {
405 dns2.s_addr = inet_addr(args_info.pcodns2_arg);
406 if (dns2.s_addr == -1) {
407 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
408 "Failed to convert pcodns2!");
409 exit(1);
410 }
411 }
jjako1d3db972004-01-16 09:56:56 +0000412#endif
413
Harald Weltebed35df2011-11-02 13:06:18 +0100414 pco.l = 20;
415 pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */
416 pco.v[1] = 0x80; /* IPCP */
417 pco.v[2] = 0x21;
418 pco.v[3] = 0x10; /* Length of contents */
419 pco.v[4] = 0x02; /* ACK */
420 pco.v[5] = 0x00; /* ID: Need to match request */
421 pco.v[6] = 0x00; /* Length */
422 pco.v[7] = 0x10;
423 pco.v[8] = 0x81; /* DNS 1 */
424 pco.v[9] = 0x06;
425 memcpy(&pco.v[10], &dns1, sizeof(dns1));
426 pco.v[14] = 0x83;
427 pco.v[15] = 0x06; /* DNS 2 */
428 memcpy(&pco.v[16], &dns2, sizeof(dns2));
jjakoa7cd2492003-04-11 09:40:12 +0000429
Harald Weltebed35df2011-11-02 13:06:18 +0100430 /* ipup */
431 ipup = args_info.ipup_arg;
jjakoa7cd2492003-04-11 09:40:12 +0000432
Harald Weltebed35df2011-11-02 13:06:18 +0100433 /* ipdown */
434 ipdown = args_info.ipdown_arg;
jjako4b26b512003-01-28 16:13:57 +0000435
Harald Weltebed35df2011-11-02 13:06:18 +0100436 /* Timelimit */
437 timelimit = args_info.timelimit_arg;
438 starttime = time(NULL);
jjako4b26b512003-01-28 16:13:57 +0000439
Harald Weltebed35df2011-11-02 13:06:18 +0100440 /* qos */
441 qos.l = 3;
442 qos.v[2] = (args_info.qos_arg) & 0xff;
443 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
444 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
jjakoa7cd2492003-04-11 09:40:12 +0000445
Harald Weltebed35df2011-11-02 13:06:18 +0100446 /* apn */
447 if (strlen(args_info.apn_arg) > (sizeof(apn.v) - 1)) {
448 printf("Invalid APN\n");
449 return -1;
450 }
451 apn.l = strlen(args_info.apn_arg) + 1;
452 apn.v[0] = (char)strlen(args_info.apn_arg);
453 strncpy((char *)&apn.v[1], args_info.apn_arg, sizeof(apn.v) - 1);
jjakoe0149782003-07-06 17:07:04 +0000454
Harald Weltebed35df2011-11-02 13:06:18 +0100455 /* foreground */
456 /* If flag not given run as a daemon */
457 if (!args_info.fg_flag) {
458 FILE *f;
459 int rc;
460 closelog();
461 /* Close the standard file descriptors. */
462 /* Is this really needed ? */
463 f = freopen("/dev/null", "w", stdout);
464 if (f == NULL) {
465 sys_err(LOG_WARNING, __FILE__, __LINE__, 0,
466 "Could not redirect stdout to /dev/null");
467 }
468 f = freopen("/dev/null", "w", stderr);
469 if (f == NULL) {
470 sys_err(LOG_WARNING, __FILE__, __LINE__, 0,
471 "Could not redirect stderr to /dev/null");
472 }
473 f = freopen("/dev/null", "r", stdin);
474 if (f == NULL) {
475 sys_err(LOG_WARNING, __FILE__, __LINE__, 0,
476 "Could not redirect stdin to /dev/null");
477 }
478 rc = daemon(0, 0);
479 if (rc != 0) {
480 sys_err(LOG_ERR, __FILE__, __LINE__, rc,
481 "Could not daemonize");
482 exit(1);
483 }
484 /* Open log again. This time with new pid */
485 openlog(PACKAGE, LOG_PID, LOG_DAEMON);
486 }
jjakoe0149782003-07-06 17:07:04 +0000487
Harald Weltebed35df2011-11-02 13:06:18 +0100488 /* pidfile */
489 /* This has to be done after we have our final pid */
490 if (args_info.pidfile_arg) {
491 log_pid(args_info.pidfile_arg);
492 }
jjakoe0149782003-07-06 17:07:04 +0000493
Harald Weltebed35df2011-11-02 13:06:18 +0100494 if (debug)
495 printf("gtpclient: Initialising GTP tunnel\n");
jjako52c24142002-12-16 13:33:51 +0000496
Harald Weltebed35df2011-11-02 13:06:18 +0100497 if (gtp_new(&gsn, args_info.statedir_arg, &listen_, GTP_MODE_GGSN)) {
498 sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Failed to create gtp");
499 exit(1);
500 }
501 if (gsn->fd0 > maxfd)
502 maxfd = gsn->fd0;
503 if (gsn->fd1c > maxfd)
504 maxfd = gsn->fd1c;
505 if (gsn->fd1u > maxfd)
506 maxfd = gsn->fd1u;
jjako52c24142002-12-16 13:33:51 +0000507
Harald Weltebed35df2011-11-02 13:06:18 +0100508 gtp_set_cb_data_ind(gsn, encaps_tun);
509 gtp_set_cb_delete_context(gsn, delete_context);
510 gtp_set_cb_create_context_ind(gsn, create_context_ind);
jjakoa7cd2492003-04-11 09:40:12 +0000511
Harald Weltebed35df2011-11-02 13:06:18 +0100512 /* Create a tunnel interface */
513 if (debug)
514 printf("Creating tun interface\n");
515 if (tun_new((struct tun_t **)&tun)) {
516 sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Failed to create tun");
517 if (debug)
518 printf("Failed to create tun\n");
519 exit(1);
520 }
jjakoa7cd2492003-04-11 09:40:12 +0000521
Harald Weltebed35df2011-11-02 13:06:18 +0100522 if (debug)
523 printf("Setting tun IP address\n");
524 if (tun_setaddr(tun, &netaddr, &destaddr, &mask)) {
525 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
526 "Failed to set tun IP address");
527 if (debug)
528 printf("Failed to set tun IP address\n");
529 exit(1);
530 }
jjakoa7cd2492003-04-11 09:40:12 +0000531
Harald Weltebed35df2011-11-02 13:06:18 +0100532 tun_set_cb_ind(tun, cb_tun_ind);
533 if (tun->fd > maxfd)
534 maxfd = tun->fd;
jjakoc6762cf2004-04-28 14:52:58 +0000535
Harald Weltebed35df2011-11-02 13:06:18 +0100536 if (ipup)
537 tun_runscript(tun, ipup);
jjako52c24142002-12-16 13:33:51 +0000538
539 /******************************************************************/
Harald Weltebed35df2011-11-02 13:06:18 +0100540 /* Main select loop */
jjako52c24142002-12-16 13:33:51 +0000541 /******************************************************************/
542
Harald Weltebed35df2011-11-02 13:06:18 +0100543 while ((((starttime + timelimit) > time(NULL)) || (0 == timelimit))
544 && (!end)) {
Harald Weltec3dcba02010-05-04 11:02:54 +0200545
Harald Weltebed35df2011-11-02 13:06:18 +0100546 FD_ZERO(&fds);
547 if (tun)
548 FD_SET(tun->fd, &fds);
549 FD_SET(gsn->fd0, &fds);
550 FD_SET(gsn->fd1c, &fds);
551 FD_SET(gsn->fd1u, &fds);
jjako52c24142002-12-16 13:33:51 +0000552
Harald Weltebed35df2011-11-02 13:06:18 +0100553 gtp_retranstimeout(gsn, &idleTime);
554 switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
555 case -1: /* errno == EINTR : unblocked signal */
556 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
557 "select() returned -1");
558 /* On error, select returns without modifying fds */
559 FD_ZERO(&fds);
560 break;
561 case 0:
562 /* printf("Select returned 0\n"); */
563 gtp_retrans(gsn); /* Only retransmit if nothing else */
564 break;
565 default:
566 break;
567 }
jjako52c24142002-12-16 13:33:51 +0000568
Harald Weltebed35df2011-11-02 13:06:18 +0100569 if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
570 tun_decaps(tun) < 0) {
571 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
572 "TUN read failed (fd)=(%d)", tun->fd);
573 }
574
575 if (FD_ISSET(gsn->fd0, &fds))
576 gtp_decaps0(gsn);
577
578 if (FD_ISSET(gsn->fd1c, &fds))
579 gtp_decaps1c(gsn);
580
581 if (FD_ISSET(gsn->fd1u, &fds))
582 gtp_decaps1u(gsn);
583
584 }
585
586 cmdline_parser_free(&args_info);
587 ippool_free(ippool);
588 gtp_free(gsn);
589 tun_free(tun);
590
591 return 1;
592
jjako52c24142002-12-16 13:33:51 +0000593}