blob: dd24fcea10f72a7151863fa2392a995b27870a32 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
2 * OpenGGSN - Gateway GPRS Support Node
3 * Copyright (C) 2002 Mondru AB.
4 *
5 * 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.
9 *
10 * The initial developer of the original code is
11 * Jens Jakobsen <jj@openggsn.org>
12 *
13 * Contributor(s):
14 *
15 */
16
17/*
18 * sgsnemu.c
19 *
20 */
21
22
23#ifdef __linux__
24#define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */
25#endif
26
27
28#include <syslog.h>
29#include <ctype.h>
30#include <netdb.h>
31#include <signal.h>
32#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35#include <sys/types.h>
36#include <sys/socket.h>
37#include <netinet/in.h>
38#include <arpa/inet.h>
39#include <sys/wait.h>
40#include <sys/stat.h>
41#include <unistd.h>
42#include <sys/socket.h>
43#include <sys/ioctl.h>
44#include <net/if.h>
45#include <features.h>
46#include <errno.h>
47#include <asm/types.h>
48#include <sys/socket.h>
49#include <linux/netlink.h>
50#include <resolv.h>
51#include <time.h>
52
53#include "tun.h"
54#include "../gtp/pdp.h"
55#include "../gtp/gtp.h"
56#include "cmdline.h"
57
58/* State variable */
59/* 0: Idle */
60/* 1: Wait_connect */
61/* 2: Connected */
62/* 3: Wait_disconnect */
63int state = 0;
64
65int maxfd = 0; /* For select() */
66int tun_fd = -1; /* Network file descriptor */
67struct tun_t *tun; /* TUN instance */
68struct tun_t *tun1, *tun2; /* TUN instance for client */
69int tun_fd1 = -1; /* Network file descriptor */
70int tun_fd2 = -1; /* Network file descriptor */
71struct in_addr net, mask; /* Network interface */
72int stattun; /* Allocate static tun */
73
74int debug; /* Print debug messages */
75
76int encaps_printf(void *p, void *packet, unsigned len)
77{
78 int i;
79 printf("The packet looks like this:\n");
80 for( i=0; i<len; i++) {
81 printf("%02x ", (unsigned char)*(char *)(packet+i));
82 if (!((i+1)%16)) printf("\n");
83 };
84 printf("\n");
85}
86
87/* Used to write process ID to file. Assume someone else will delete */
88void log_pid(char *pidfile) {
89 FILE *file;
90 mode_t oldmask;
91
92 oldmask = umask(022);
93 file = fopen(pidfile, "w");
94 umask(oldmask);
95 if(!file)
96 return;
97 fprintf(file, "%d\n", getpid());
98 fclose(file);
99}
100
101
102int create_tun() {
103 char buf[1024];
104 char snet[100], smask[100];
105
106 if ((tun_fd = tun_newtun((struct tun_t**) &tun)) > maxfd)
107 maxfd = tun_fd;
108
109 if (tun_fd == -1) {
110 printf("Failed to open tun\n");
111 exit(1);
112 }
113
114 strncpy(snet, inet_ntoa(net), 100);
115 strncpy(smask, inet_ntoa(mask), 100);
116
jjako49014712003-01-05 17:59:49 +0000117 sprintf(buf, "/sbin/ifconfig %s %s mtu 1450 netmask %s",
jjako52c24142002-12-16 13:33:51 +0000118 tun->devname, snet, smask);
119 if (debug) printf("%s\n", buf);
120 system(buf);
121
122 system("echo 1 > /proc/sys/net/ipv4/ip_forward");
123
124 return 0;
125}
126
127int getip(struct pdp_t *pdp, void* ipif, struct ul66_t *eua,
128 struct in_addr *net, struct in_addr *mask) {
129 struct in_addr addr;
130 uint32_t ip_start, ip_end, ip_cur;
131 struct pdp_t *pdp_;
132 struct ul66_t eua_;
133
134 printf("Begin getip %d %d %2x%2x%2x%2x\n", (unsigned)ipif, eua->l,
135 eua->v[2],eua->v[3],eua->v[4],eua->v[5]);
136
137 ip_start = ntoh32(net->s_addr & mask->s_addr);
138 ip_end = ntoh32(hton32(ip_start) | ~mask->s_addr);
139
140 /* By convention the first address is the network address, and the last */
141 /* address is the broadcast address. This way two IP addresses are "lost" */
142 ip_start++;
143
144 if (eua->l == 0) { /* No address supplied. Find one that is available! */
145 /* This routine does linear search. In order to support millions of
146 * addresses we should instead keep a linked list of available adresses */
147 for (ip_cur = ip_start; ip_cur < ip_end; ip_cur++) {
148 addr.s_addr = hton32(ip_cur);
149 pdp_ntoeua(&addr, &eua_);
150 if (pdp_ipget(&pdp_, ipif, &eua_) == -1) {
151 pdp_ntoeua(&addr, &pdp->eua);
152 pdp->ipif = ipif;
153 return 0;
154 };
155 }
156 return EOF; /* No addresses available */
157 }
158 else { /* Address supplied */
159 if (pdp_ipget(&pdp_, ipif, eua) == -1) {
160 pdp->ipif = ipif;
161 pdp->eua.l = eua->l;
162 memcpy(pdp->eua.v, eua->v, eua->l);
163 return 0;
164 }
165 else return EOF; /* Specified address not available */
166 }
167}
168
169int delete_context(struct pdp_t *pdp) {
170
171 if (!stattun) {
172 tun_freetun((struct tun_t*) pdp->ipif);
173
174 /* Clean up locally */
175 if (pdp->ipif == tun1) {
176 printf("Deleting tun interface\n");
177 tun_fd1=-1;
178 }
179 if (pdp->ipif == tun2) {
180 printf("Deleting tun interface\n");
181 tun_fd2=-1;
182 }
183 }
184
185 pdp_ipdel(pdp);
186 return 0;
187}
188
189int create_pdp_conf(struct pdp_t *pdp, int cause) {
190 char buf[1024];
191
192 printf("Received create PDP context response. Cause value: %d\n", cause);
193 if ((cause == 128) && (pdp->eua.l == 6)) {
194
195
196 if (stattun) {
197 pdp->ipif = tun1;
198 }
199 else {
200 printf("Setting up interface and routing\n");
201 if ((tun_fd = tun_newtun((struct tun_t**) &pdp->ipif)) > maxfd)
202 maxfd = tun_fd;
203
204 /* HACK: Only support select of up to two tun interfaces */
205 if (NULL == tun1) {
206 tun1 = pdp->ipif;
207 tun_fd1 = tun1->fd;
208 }
209 else {
210 tun2 = pdp->ipif;
211 tun_fd2 = tun2->fd;
212 }
213
jjako49014712003-01-05 17:59:49 +0000214 /*system("/sbin/ifconfig tun0 192.168.0.10");*/
215 sprintf(buf, "/sbin/ifconfig %s %hu.%hu.%hu.%hu",
jjako52c24142002-12-16 13:33:51 +0000216 ((struct tun_t*) pdp->ipif)->devname,
217 pdp->eua.v[2], pdp->eua.v[3], pdp->eua.v[4], pdp->eua.v[5]);
218 printf(buf); printf("\n");
219 system(buf);
220
221
222 /*system("route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.0.10");*/
jjako49014712003-01-05 17:59:49 +0000223 sprintf(buf, "/sbin/route add -net %hu.%hu.%hu.0 netmask 255.255.255.0 gw %hu.%hu.%hu.%hu",
jjako52c24142002-12-16 13:33:51 +0000224 pdp->eua.v[2], pdp->eua.v[3], pdp->eua.v[4],
225 pdp->eua.v[2], pdp->eua.v[3], pdp->eua.v[4], pdp->eua.v[5]);
226 printf(buf); printf("\n");
227 system(buf);
228
229 system("echo 1 > /proc/sys/net/ipv4/ip_forward");
230 }
231
232 pdp_ipset(pdp, pdp->ipif, &pdp->eua);
233
234 state = 2; /* Connected */
235 }
236 else {
237 state = 0;
238 }
239
240 printf("\n");
241
242 return 0;
243}
244
245
246int create_pdp_ind(struct pdp_t *pdp) {
247
248 printf("Received create PDP context request\n");
249
250 pdp->eua.l=0; /* TODO: Indicates dynamic IP */
251
252 /* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */
253 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_neg));
254
255 getip(pdp, &tun, &pdp->eua, &net, &mask);
256 pdp_ipset(pdp, pdp->ipif, &pdp->eua);
257
258 return 0; /* Success */
259}
260
261
262int delete_pdp_conf(struct pdp_t *pdp, int cause) {
263 printf("Received delete PDP context response. Cause value: %d\n", cause);
264 return 0;
265}
266
267int echo_conf(struct pdp_t *pdp, int cause) {
268 printf("Received echo response. Cause value: %d\n", cause);
269 return 0;
270}
271
272int conf(int type, int cause, struct pdp_t* pdp, void *aid) {
273 /* if (cause < 0) return 0; Some error occurred. We don't care */
274 switch (type) {
275 case GTP_ECHO_REQ:
276 return echo_conf(pdp, cause);
277 case GTP_CREATE_PDP_REQ:
278 if (cause !=128) return 0; /* Request not accepted. We don't care */
279 return create_pdp_conf(pdp, cause);
280 case GTP_DELETE_PDP_REQ:
281 if (cause !=128) return 0; /* Request not accepted. We don't care */
282 return delete_pdp_conf(pdp, cause);
283 default:
284 return 0;
285 }
286}
287
288int encaps_gtp_client(void *gsn, struct tun_t *tun, void *pack, unsigned len) {
289 /* Special client version which checks for source address instead */
290 struct pdp_t *pdp;
291 struct in_addr addr;
292 struct ul66_t eua;
293 /*printf("encaps_gtp. Packet received: forwarding to gtp.\n");*/
294 /* First we need to extract the IP destination address */
295 memcpy(&addr.s_addr, pack+12, 4); /* This ought to be dest addr */
296 pdp_ntoeua(&addr, &eua);
297 if (pdp_ipget(&pdp, tun, &eua) == 0) {
298 return gtp_gpdu((struct gsn_t*) gsn, pdp, pack, len);
299 }
300 else {
301 printf("Received packet with no destination!!!\n");
302 return 0;
303 }
304}
305
306int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) {
307 /* printf("encaps_tun. Packet received: forwarding to tun\n");*/
308 return tun_encaps((struct tun_t*) pdp->ipif, pack, len);
309}
310
311int main(int argc, char **argv)
312{
313 /* gengeopt declarations */
314 struct gengetopt_args_info args_info;
315
316 /* function-local options */
317
318 struct hostent *host;
319
320 struct in_addr listen, remote;
321 struct in_addr dns;
322
323 int gtpfd = -1; /* Network file descriptor */
324 struct gsn_t *gsn; /* GSN instance */
325
326 fd_set fds; /* For select() */
327 struct timeval idleTime; /* How long to select() */
328
329 struct pdp_t *pdp[2];
330
331 int n; /* For counter */
332
333 int contexts; /* Number of contexts to create */
334 int timelimit; /* Number of seconds to be connected */
335 int starttime; /* Time program was started */
336
337 struct ul_t imsi, qos, apn, msisdn;
338 unsigned char qosh[3], imsih[8], apnh[256], msisdnh[256];
339 struct ul255_t pco;
340 uint64_t imsi3;
341
342 /* open a connection to the syslog daemon */
343 /*openlog(PACKAGE, LOG_PID, LOG_DAEMON);*/
344 openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON);
345
346 if (cmdline_parser (argc, argv, &args_info) != 0)
347 exit(1);
348 if (args_info.debug_flag) {
349 printf("remote: %s\n", args_info.remote_arg);
350 printf("listen: %s\n", args_info.listen_arg);
351 printf("conf: %s\n", args_info.conf_arg);
352 printf("fg: %d\n", args_info.fg_flag);
353 printf("debug: %d\n", args_info.debug_flag);
354 printf("imsi: %s\n", args_info.imsi_arg);
355 printf("qos: %#08x\n", args_info.qos_arg);
356 printf("apn: %s\n", args_info.apn_arg);
357 printf("msisdn: %s\n", args_info.msisdn_arg);
358 printf("uid: %s\n", args_info.uid_arg);
359 printf("pwd: %s\n", args_info.pwd_arg);
360 printf("static: %d\n", args_info.static_flag);
361 printf("net: %s\n", args_info.net_arg);
362 printf("mask: %s\n", args_info.mask_arg);
363 printf("pidfile: %s\n", args_info.pidfile_arg);
364 printf("statedir: %s\n", args_info.statedir_arg);
365 printf("dns: %s\n", args_info.dns_arg);
366 printf("contexts: %d\n", args_info.contexts_arg);
367 printf("timelimit: %d\n", args_info.timelimit_arg);
368 }
369
370 /* Try out our new parser */
371
372 if (args_info.conf_arg) {
373 if (cmdline_parser_configfile (args_info.conf_arg, &args_info, 0) != 0)
374 exit(1);
375 if (args_info.debug_flag) {
376 printf("cmdline_parser_configfile\n");
377 printf("remote: %s\n", args_info.remote_arg);
378 printf("listen: %s\n", args_info.listen_arg);
379 printf("conf: %s\n", args_info.conf_arg);
380 printf("fg: %d\n", args_info.fg_flag);
381 printf("debug: %d\n", args_info.debug_flag);
382 printf("imsi: %s\n", args_info.imsi_arg);
383 printf("qos: %#08x\n", args_info.qos_arg);
384 printf("apn: %s\n", args_info.apn_arg);
385 printf("msisdn: %s\n", args_info.msisdn_arg);
386 printf("uid: %s\n", args_info.uid_arg);
387 printf("pwd: %s\n", args_info.pwd_arg);
388 printf("static: %d\n", args_info.static_flag);
389 printf("net: %s\n", args_info.net_arg);
390 printf("mask: %s\n", args_info.mask_arg);
391 printf("pidfile: %s\n", args_info.pidfile_arg);
392 printf("statedir: %s\n", args_info.statedir_arg);
393 printf("dns: %s\n", args_info.dns_arg);
394 printf("contexts: %d\n", args_info.contexts_arg);
395 printf("timelimit: %d\n", args_info.timelimit_arg);
396 }
397 }
398
399 /* Handle each option */
400
401 /* foreground */
402 /* If flag not given run as a daemon */
403 if (!args_info.fg_flag)
404 {
405 closelog();
406 /* Close the standard file descriptors. Why? */
407 freopen("/dev/null", "w", stdout);
408 freopen("/dev/null", "w", stderr);
409 freopen("/dev/null", "r", stdin);
410 daemon(0, 0);
411 /* Open log again. This time with new pid */
412 openlog(PACKAGE, LOG_PID, LOG_DAEMON);
413 }
414
415 /* debug */
416 debug = args_info.debug_flag;
417
418 /* pidfile */
419 /* This has to be done after we have our final pid */
420 if (args_info.pidfile_arg) {
421 log_pid(args_info.pidfile_arg);
422 }
423
424 /* dns */
425 /* If no dns option is given use system default */
426 /* Do hostname lookup to translate hostname to IP address */
427 printf("\n");
428 if (args_info.dns_arg) {
429 if (!(host = gethostbyname(args_info.dns_arg))) {
430 fprintf(stderr, "%s: Invalid dns address: %s!\n",
431 PACKAGE, args_info.dns_arg);
432 syslog(LOG_ERR, "Invalid dns address: %s!",
433 args_info.dns_arg);
434 exit(1);
435 }
436 else {
437 memcpy(&dns.s_addr, host->h_addr, host->h_length);
438 _res.nscount = 1;
439 _res.nsaddr_list[0].sin_addr = dns;
440 printf("Using DNS server: %s (%s)\n", args_info.dns_arg, inet_ntoa(dns));
441 }
442 }
443 else {
444 dns.s_addr= 0;
445 printf("Using default DNS server\n");
446 }
447
448 /* listen */
449 /* If no listen option is specified listen to any local port */
450 /* Do hostname lookup to translate hostname to IP address */
451 if (args_info.listen_arg) {
452 if (!(host = gethostbyname(args_info.listen_arg))) {
453 fprintf(stderr, "%s: Invalid listening address: %s!\n",
454 PACKAGE, args_info.listen_arg);
455 syslog(LOG_ERR, "Invalid listening address: %s!",
456 args_info.listen_arg);
457 exit(1);
458 }
459 else {
460 memcpy(&listen.s_addr, host->h_addr, host->h_length);
461 printf("Local IP address is: %s (%s)\n", args_info.listen_arg, inet_ntoa(listen));
462 }
463 }
464 else {
465 fprintf(stderr, "%s: Listening address must be specified: %s!\n",
466 PACKAGE, args_info.listen_arg);
467 syslog(LOG_ERR, "Listening address must be specified: %s!",
468 args_info.listen_arg);
469 exit(1);
470 }
471
472
473 /* remote */
474 /* If no remote option is specified terminate */
475 /* Do hostname lookup to translate hostname to IP address */
476 if (args_info.remote_arg) {
477 if (!(host = gethostbyname(args_info.remote_arg))) {
478 fprintf(stderr, "%s: Invalid remote address: %s!\n",
479 PACKAGE, args_info.remote_arg);
480 syslog(LOG_ERR, "Invalid remote address: %s!",
481 args_info.remote_arg);
482 exit(1);
483 }
484 else {
485 memcpy(&remote.s_addr, host->h_addr, host->h_length);
486 printf("Remote IP address is: %s (%s)\n", args_info.remote_arg, inet_ntoa(remote));
487 }
488 }
489 else {
490 fprintf(stderr, "%s: No remote address given!\n",
491 PACKAGE);
492 syslog(LOG_ERR, "No remote address given!");
493 exit(1);
494 }
495
496
497 /* net */
498 /* Store net as in_addr */
499 if (args_info.net_arg) {
500 if (!inet_aton(args_info.net_arg, &net)) {
501 fprintf(stderr, "%s: Invalid network address: %s!\n",
502 PACKAGE, args_info.net_arg);
503 syslog(LOG_ERR, "Invalid network address: %s!",
504 args_info.net_arg);
505 exit(1);
506 }
507 }
508
509 /* mask */
510 /* Store mask as in_addr */
511 if (args_info.mask_arg) {
512 if (!inet_aton(args_info.mask_arg, &mask)) {
513 fprintf(stderr, "%s: Invalid network mask: %s!\n",
514 PACKAGE, args_info.mask_arg);
515 syslog(LOG_ERR, "Invalid network mask: %s!",
516 args_info.mask_arg);
517 exit(1);
518 }
519 }
520
521 /* imsi */
522 if (strlen(args_info.imsi_arg)!=15) {
523 printf("Invalid IMSI\n");
524 exit(1);
525 }
526 imsi.l = 8;
527 imsi.v = imsih;
528 imsi.v[0] = args_info.imsi_arg[0]-48 + (args_info.imsi_arg[1]-48)*16;
529 imsi.v[1] = args_info.imsi_arg[2]-48 + (args_info.imsi_arg[3]-48)*16;
530 imsi.v[2] = args_info.imsi_arg[4]-48 + (args_info.imsi_arg[5]-48)*16;
531 imsi.v[3] = args_info.imsi_arg[6]-48 + (args_info.imsi_arg[7]-48)*16;
532 imsi.v[4] = args_info.imsi_arg[8]-48 + (args_info.imsi_arg[9]-48)*16;
533 imsi.v[5] = args_info.imsi_arg[10]-48 + (args_info.imsi_arg[11]-48)*16;
534 imsi.v[6] = args_info.imsi_arg[12]-48 + (args_info.imsi_arg[13]-48)*16;
535 imsi.v[7] = args_info.imsi_arg[14]-48 + 0*16;
536
537 if (imsi.l > sizeof(imsi3)) {
538 printf("Invalid IMSI\n");
539 exit(1);
540 }
541 else {
542 memcpy(&imsi3, imsi.v, imsi.l);
543 printf("IMSI is: %s (%#08llx)\n", args_info.imsi_arg, imsi3);
544 }
545
546 /* qos */
547 qos.l = 3;
548 qos.v = qosh;
549 qos.v[2] = (args_info.qos_arg) & 0xff;
550 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
551 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
552
553 /* contexts */
554 contexts = args_info.contexts_arg;
555
556 /* Timelimit */
557 timelimit = args_info.timelimit_arg;
558 starttime = time(NULL);
559
560 /* apn */
561 if (strlen(args_info.apn_arg)>255) {
562 printf("Invalid APN\n");
563 exit(1);
564 }
565 apn.l = strlen(args_info.apn_arg) + 1;
566 apn.v = apnh;
567 apn.v[0] = (char) strlen(args_info.apn_arg);
568 strncpy(&apn.v[1], args_info.apn_arg, 255);
569 printf("Using APN: %s\n", args_info.apn_arg);
570
571 /* msisdn */
572 if (strlen(args_info.msisdn_arg)>255) {
573 printf("Invalid MSISDN\n");
574 exit(1);
575 }
576 msisdn.l = 1;
577 msisdn.v = msisdnh;
578 msisdn.v[0] = 0x91; /* International format */
579 for(n=0; n<strlen(args_info.msisdn_arg); n++) {
580 if ((n%2) == 0) {
581 msisdn.v[((int)n/2)+1] = args_info.msisdn_arg[n] - 48 + 0xf0;
582 msisdn.l += 1;
583 }
584 else {
585 msisdn.v[((int)n/2)+1] = (msisdn.v[((int)n/2)+1] & 0x0f) + (args_info.msisdn_arg[n] - 48) * 16;
586 }
587 }
588 printf("Using MSISDN: %s\n", args_info.msisdn_arg);
589
590 /* UID and PWD */
591 /* Might need to also insert stuff like DNS etc. */
592 if ((strlen(args_info.uid_arg) + strlen(args_info.pwd_arg) + 10)>255) {
593 printf("invalid UID and PWD\n");
594 exit(1);
595 }
596 pco.l = strlen(args_info.uid_arg) + strlen(args_info.pwd_arg) + 10;
597 pco.v[0] = 0x80; /* PPP */
598 pco.v[1] = 0xc0;
599 pco.v[2] = 0x23; /* PAP */
600 pco.v[3] = 0x12;
601 pco.v[4] = 0x01; /* Authenticate request */
602 pco.v[5] = 0x01;
603 pco.v[6] = 0x00; /* MSB of length */
604 pco.v[7] = strlen(args_info.uid_arg) + strlen(args_info.pwd_arg) + 6;
605 pco.v[8] = strlen(args_info.uid_arg);
606 memcpy(&pco.v[9], args_info.uid_arg, strlen(args_info.uid_arg));
607 pco.v[9+strlen(args_info.uid_arg)] = strlen(args_info.pwd_arg);
608 memcpy(&pco.v[10+strlen(args_info.uid_arg)], args_info.pwd_arg, strlen(args_info.pwd_arg));
609
610 /* static */
611 stattun = args_info.static_flag;
612
613 printf("\nInitialising GTP library\n");
614 if ((gtpfd = gtp_new(&gsn, args_info.statedir_arg, &listen)) > maxfd)
615 maxfd = gtpfd;
616
617 if ((gtpfd = gtp_fd(gsn)) > maxfd)
618 maxfd = gtpfd;
619
620 gtp_set_cb_gpdu(gsn, encaps_tun);
621 gtp_set_cb_delete_context(gsn, delete_context);
622
623 gtp_set_cb_conf(gsn, conf);
624 printf("Done initialising GTP library\n\n");
625
626 if (stattun) {
627 create_tun();
628 tun1 = tun;
629 tun_fd1 = tun1->fd;
630 }
631
632 /* See if anybody is there */
633 printf("Sending off echo request\n");
634 if (gtpfd != -1) gtp_echo_req(gsn, &remote); /* See if remote is alive ? */
635
636 for(n=0; n<contexts; n++) {
637 printf("Setting up PDP context #%d\n", n);
638
639 pdp_newpdp(&pdp[n], imsi3, n, NULL); /* Allocated here. Cleaned up in gtp.c: TODO Should be statically allocated! */
640
641 /*
642 if (qos.l > sizeof(pdp[n]->qos_req.v)) {
643 exit(1);
644 }
645 else {
646 pdp[n]->qos_req.l = qos.l;
647 memcpy(pdp[n]->qos_req.v, qos.v, qos.l);
648 }
649 */
650 memcpy(pdp[n]->qos_req0, qos.v, qos.l); /* TODO range check */
651
652 pdp[n]->selmode = 0x01; /* MS provided APN, subscription not verified */
653
654 if (apn.l > sizeof(pdp[n]->apn_use.v)) {
655 exit(1);
656 }
657 else {
658 pdp[n]->apn_use.l = apn.l;
659 memcpy(pdp[n]->apn_use.v, apn.v, apn.l);
660 }
661
662 pdp[n]->gsnlc.l = 4;
663 memcpy(pdp[n]->gsnlc.v, &listen, 4);
664 pdp[n]->gsnlu.l = 4;
665 memcpy(pdp[n]->gsnlu.v, &listen, 4);
666
667 if (msisdn.l > sizeof(pdp[n]->msisdn.v)) {
668 exit(1);
669 }
670 else {
671 pdp[n]->msisdn.l = msisdn.l;
672 memcpy(pdp[n]->msisdn.v, msisdn.v, msisdn.l);
673 }
674
675 ipv42eua(&pdp[n]->eua, NULL); /* Request dynamic IP address */
676
677 if (pco.l > sizeof(pdp[n]->pco_req.v)) {
678 exit(1);
679 }
680 else {
681 pdp[n]->pco_req.l = pco.l;
682 memcpy(pdp[n]->pco_req.v, pco.v, pco.l);
683 }
684
685 /* Create context */
686 /* We send this of once. Retransmissions are handled by gtplib */
687 if (gtpfd != -1) gtp_create_context(gsn, pdp[n], NULL, &remote);
688 }
689
690 state = 1; /* Enter wait_connection state */
691
692 printf("Waiting for response from ggsn........\n\n");
693
694
695 /******************************************************************/
696 /* Main select loop */
697 /******************************************************************/
698
699 while (((starttime + timelimit + 10) > time(NULL)) || (0 == timelimit)) {
700
701 /* Take down client connections at some stage */
702 if (((starttime + timelimit) <= time(NULL)) && (0 != timelimit) && (2 == state)) {
703 state = 3;
704 for(n=0; n<contexts; n++) {
705 /* Delete context */
706 printf("Disconnecting PDP context #%d\n", n);
707 if (gtpfd != -1) gtp_delete_context(gsn, pdp[n], NULL);
708 }
709 }
710
711 FD_ZERO(&fds);
712 if (tun_fd1 != -1) FD_SET(tun_fd1, &fds);
713 if (tun_fd2 != -1) FD_SET(tun_fd2, &fds);
714 if (gtpfd != -1) FD_SET(gtpfd, &fds);
715
716 gtp_retranstimeout(gsn, &idleTime);
717
718 switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
719 case -1:
720 syslog(LOG_ERR, "sgsnemu: select = -1");
721 break;
722 case 0:
723 gtp_retrans(gsn); /* Only retransmit if nothing else */
724 break;
725 default:
726 break;
727 }
728
729 if (tun_fd1 != -1 &&
730 FD_ISSET(tun_fd1, &fds) &&
731 tun_decaps(tun1, encaps_gtp_client, gsn) < 0) {
732 syslog(LOG_ERR, "TUN read failed (fd)=(%d)", tun_fd1);
733 }
734
735 if (tun_fd2 != -1 &&
736 FD_ISSET(tun_fd2, &fds) &&
737 tun_decaps(tun2, encaps_gtp_client, gsn) < 0) {
738 syslog(LOG_ERR, "TUN read failed (fd)=(%d)", tun_fd2);
739 }
740
741 if (gtpfd != -1 && FD_ISSET(gtpfd, &fds) &&
742 gtp_decaps(gsn) < 0) {
743 syslog(LOG_ERR, "GTP read failed (gre)=(%d)", gtpfd);
744 }
745
746
747 }
748
749 gtp_free(gsn); /* Clean up the gsn instance */
750
751 return 1;
752
753}
754