blob: 322cc9d558bf70dfc9935fe276b63eddcba7eb97 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
jjakoa7cd2492003-04-11 09:40:12 +00002 * OpenGGSN - Gateway GPRS Support Node
3 * Copyright (C) 2002, 2003 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 *
jjakoa7cd2492003-04-11 09:40:12 +000010 * The initial developer of the original code is
11 * Jens Jakobsen <jj@openggsn.org>
jjako52c24142002-12-16 13:33:51 +000012 *
jjakoa7cd2492003-04-11 09:40:12 +000013 * Contributor(s):
jjako52c24142002-12-16 13:33:51 +000014 *
15 */
16
17/* ggsn.c
18 *
19 */
20
21#ifdef __linux__
22#define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */
23#endif
24
25
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>
39#include <unistd.h>
40
41#include <sys/socket.h>
42#include <sys/ioctl.h>
43#include <net/if.h>
44#include <features.h>
45
46#include <errno.h>
47
48#include <asm/types.h>
49#include <sys/socket.h>
50#include <linux/netlink.h>
51
52#include <time.h>
53
54#include "tun.h"
jjakoa7cd2492003-04-11 09:40:12 +000055#include "ippool.h"
56#include "syserr.h"
jjako52c24142002-12-16 13:33:51 +000057#include "../gtp/pdp.h"
58#include "../gtp/gtp.h"
59#include "cmdline.h"
60
61
jjakoa7cd2492003-04-11 09:40:12 +000062int maxfd = 0; /* For select() */
jjako52c24142002-12-16 13:33:51 +000063struct tun_t *tun; /* TUN instance */
jjakoa7cd2492003-04-11 09:40:12 +000064
65struct in_addr listen_;
jjako52c24142002-12-16 13:33:51 +000066struct in_addr net, mask; /* Network interface */
jjakoa7cd2492003-04-11 09:40:12 +000067struct in_addr dns1, dns2; /* PCO DNS address */
68char *ipup, *ipdown; /* Filename of scripts */
69int debug; /* Print debug output */
70struct ul255_t pco;
71struct ul255_t qos;
72struct ul255_t apn;
73
74struct tun_t *tun; /* TUN instance */
75struct ippool_t *ippool; /* Pool of IP addresses */
76struct gsn_t *gsn; /* GSN instance */
77
jjako52c24142002-12-16 13:33:51 +000078
79
80/* Used to write process ID to file. Assume someone else will delete */
81void log_pid(char *pidfile) {
82 FILE *file;
83 mode_t oldmask;
84
85 oldmask = umask(022);
86 file = fopen(pidfile, "w");
87 umask(oldmask);
88 if(!file)
89 return;
90 fprintf(file, "%d\n", getpid());
91 fclose(file);
92}
93
94
95int encaps_printf(void *p, void *packet, unsigned len)
96{
97 int i;
98 if (debug) {
99 printf("The packet looks like this:\n");
100 for( i=0; i<len; i++) {
101 printf("%02x ", (unsigned char)*(char *)(packet+i));
102 if (!((i+1)%16)) printf("\n");
103 };
104 printf("\n");
105 }
106 return 0;
107}
108
jjako52c24142002-12-16 13:33:51 +0000109int delete_context(struct pdp_t *pdp) {
jjako49014712003-01-05 17:59:49 +0000110 if (debug) printf("Deleting PDP context\n");
jjakoa7cd2492003-04-11 09:40:12 +0000111 ippool_freeip((struct ippoolm_t *) pdp->peer);
jjako52c24142002-12-16 13:33:51 +0000112 return 0;
113}
114
115
jjako52c24142002-12-16 13:33:51 +0000116int create_context(struct pdp_t *pdp) {
jjakoa7cd2492003-04-11 09:40:12 +0000117 struct in_addr addr;
118 struct ippoolm_t *member;
jjako52c24142002-12-16 13:33:51 +0000119
120 if (debug) printf("Received create PDP context request\n");
121
122 pdp->eua.l=0; /* TODO: Indicates dynamic IP */
123
124 /* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */
125 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_neg));
jjakoa7cd2492003-04-11 09:40:12 +0000126 memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg));
jjako52c24142002-12-16 13:33:51 +0000127
jjakoa7cd2492003-04-11 09:40:12 +0000128 if (pdp_euaton(&pdp->eua, &addr)) {
129 addr.s_addr = 0; /* Request dynamic */
130 }
131
132 if (ippool_newip(ippool, &member, &addr)) {
133 return EOF; /* Allready in use, or no more available */
134 }
135
136 pdp_ntoeua(&member->addr, &pdp->eua);
137 pdp->peer = &member;
138 pdp->ipif = tun; /* TODO */
139 member->peer = pdp;
jjako52c24142002-12-16 13:33:51 +0000140
141 return 0; /* Success */
142}
143
144
jjakoa7cd2492003-04-11 09:40:12 +0000145/* Callback for receiving messages from tun */
146int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) {
147 struct ippoolm_t *ipm;
148 struct in_addr dst;
149 struct tun_packet_t *iph = (struct tun_packet_t*) pack;
150
151 dst.s_addr = iph->dst;
152
153 if (ippool_getip(ippool, &ipm, &dst)) {
jjako52c24142002-12-16 13:33:51 +0000154 if (debug) printf("Received packet with no destination!!!\n");
155 return 0;
156 }
jjakoa7cd2492003-04-11 09:40:12 +0000157
158 if (ipm->peer) /* Check if a peer protocol is defined */
159 gtp_gpdu(gsn, (struct pdp_t*) ipm->peer, pack, len);
160 return 0;
jjako52c24142002-12-16 13:33:51 +0000161}
162
jjako52c24142002-12-16 13:33:51 +0000163int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) {
164 /* printf("encaps_tun. Packet received: forwarding to tun\n");*/
165 return tun_encaps((struct tun_t*) pdp->ipif, pack, len);
166}
167
168
169int main(int argc, char **argv)
170{
171 /* gengeopt declarations */
172 struct gengetopt_args_info args_info;
173
174 struct hostent *host;
175
jjako52c24142002-12-16 13:33:51 +0000176
jjako52c24142002-12-16 13:33:51 +0000177 fd_set fds; /* For select() */
178 struct timeval idleTime; /* How long to select() */
jjako52c24142002-12-16 13:33:51 +0000179
jjako52c24142002-12-16 13:33:51 +0000180
181 int timelimit; /* Number of seconds to be connected */
182 int starttime; /* Time program was started */
183
184 /* open a connection to the syslog daemon */
185 /*openlog(PACKAGE, LOG_PID, LOG_DAEMON);*/
186 openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON);
187
188 if (cmdline_parser (argc, argv, &args_info) != 0)
189 exit(1);
190 if (args_info.debug_flag) {
191 printf("listen: %s\n", args_info.listen_arg);
192 printf("conf: %s\n", args_info.conf_arg);
193 printf("fg: %d\n", args_info.fg_flag);
194 printf("debug: %d\n", args_info.debug_flag);
195 printf("qos: %#08x\n", args_info.qos_arg);
196 printf("apn: %s\n", args_info.apn_arg);
197 printf("net: %s\n", args_info.net_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000198 printf("dynip: %s\n", args_info.dynip_arg);
199 printf("statip: %s\n", args_info.statip_arg);
jjako4b26b512003-01-28 16:13:57 +0000200 printf("ipup: %s\n", args_info.ipup_arg);
201 printf("ipdown: %s\n", args_info.ipdown_arg);
jjako52c24142002-12-16 13:33:51 +0000202 printf("pidfile: %s\n", args_info.pidfile_arg);
203 printf("statedir: %s\n", args_info.statedir_arg);
204 printf("timelimit: %d\n", args_info.timelimit_arg);
205 }
206
207 /* Try out our new parser */
208
209 if (cmdline_parser_configfile (args_info.conf_arg, &args_info, 0) != 0)
210 exit(1);
211 if (args_info.debug_flag) {
212 printf("cmdline_parser_configfile\n");
213 printf("listen: %s\n", args_info.listen_arg);
214 printf("conf: %s\n", args_info.conf_arg);
215 printf("fg: %d\n", args_info.fg_flag);
216 printf("debug: %d\n", args_info.debug_flag);
217 printf("qos: %#08x\n", args_info.qos_arg);
218 printf("apn: %s\n", args_info.apn_arg);
219 printf("net: %s\n", args_info.net_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000220 printf("dynip: %s\n", args_info.dynip_arg);
221 printf("statip: %s\n", args_info.statip_arg);
jjako4b26b512003-01-28 16:13:57 +0000222 printf("ipup: %s\n", args_info.ipup_arg);
223 printf("ipdown: %s\n", args_info.ipdown_arg);
jjako52c24142002-12-16 13:33:51 +0000224 printf("pidfile: %s\n", args_info.pidfile_arg);
225 printf("statedir: %s\n", args_info.statedir_arg);
226 printf("timelimit: %d\n", args_info.timelimit_arg);
227 }
228
229 /* Handle each option */
230
231 /* foreground */
232 /* If flag not given run as a daemon */
233 if (!args_info.fg_flag)
234 {
235 closelog();
236 /* Close the standard file descriptors. */
237 /* Is this really needed ? */
238 freopen("/dev/null", "w", stdout);
239 freopen("/dev/null", "w", stderr);
240 freopen("/dev/null", "r", stdin);
241 daemon(0, 0);
242 /* Open log again. This time with new pid */
243 openlog(PACKAGE, LOG_PID, LOG_DAEMON);
244 }
245
246 /* debug */
247 debug = args_info.debug_flag;
248
249 /* pidfile */
250 /* This has to be done after we have our final pid */
251 if (args_info.pidfile_arg) {
252 log_pid(args_info.pidfile_arg);
253 }
254
255 /* listen */
256 /* If no listen option is specified listen to any local port */
257 /* Do hostname lookup to translate hostname to IP address */
258 if (args_info.listen_arg) {
259 if (!(host = gethostbyname(args_info.listen_arg))) {
260 fprintf(stderr, "%s: Invalid listening address: %s!\n",
261 PACKAGE, args_info.listen_arg);
262 syslog(LOG_ERR, "Invalid listening address: %s!",
263 args_info.listen_arg);
264 return 1;
265 }
266 else {
jjakoa7cd2492003-04-11 09:40:12 +0000267 memcpy(&listen_.s_addr, host->h_addr, host->h_length);
jjako52c24142002-12-16 13:33:51 +0000268 }
269 }
270 else {
jjakoa7cd2492003-04-11 09:40:12 +0000271 listen_.s_addr = htonl(INADDR_ANY);
jjako52c24142002-12-16 13:33:51 +0000272 }
273
274 /* net */
jjakoa7cd2492003-04-11 09:40:12 +0000275 /* Store net as in_addr net and mask */
jjako52c24142002-12-16 13:33:51 +0000276 if (args_info.net_arg) {
jjakoa7cd2492003-04-11 09:40:12 +0000277 if(ippool_aton(&net, &mask, args_info.net_arg, 0)) {
278 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
279 "Invalid network address: %s!", args_info.net_arg);
280 return -1;
jjako52c24142002-12-16 13:33:51 +0000281 }
282 }
283
jjakoa7cd2492003-04-11 09:40:12 +0000284 /* dynip */
285 if (!args_info.dynip_arg) {
286 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
287 "No dynamic address pool given!");
288 return -1;
289 }
290 else {
291 if (ippool_new(&ippool, args_info.dynip_arg,
292 IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) {
293 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
294 "Failed to allocate IP pool!");
jjako52c24142002-12-16 13:33:51 +0000295 }
296 }
297
jjakoa7cd2492003-04-11 09:40:12 +0000298 /* DNS1 and DNS2 */
299 dns1.s_addr = 0;
300 if (args_info.pcodns1_arg)
301 inet_aton(args_info.pcodns1_arg, &dns1);
302
303 dns2.s_addr = 0;
304 if (args_info.pcodns2_arg)
305 inet_aton(args_info.pcodns2_arg, &dns2);
306
307 pco.l = 20;
308 pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */
309 pco.v[1] = 0x80; /* IPCP */
310 pco.v[2] = 0x21;
311 pco.v[3] = 0x10; /* Length of contents */
312 pco.v[4] = 0x02; /* ACK */
313 pco.v[5] = 0x00; /* ID: Need to match request */
314 pco.v[6] = 0x00; /* Length */
315 pco.v[7] = 0x10;
316 pco.v[8] = 0x81; /* DNS 1 */
317 pco.v[9] = 0x06;
318 memcpy(&pco.v[10], &dns1, sizeof(dns1));
319 pco.v[14] = 0x83;
320 pco.v[15] = 0x06; /* DNS 2 */
321 memcpy(&pco.v[16], &dns2, sizeof(dns2));
322
jjako4b26b512003-01-28 16:13:57 +0000323 /* ipup */
324 ipup = args_info.ipup_arg;
325
326 /* ipdown */
327 ipdown = args_info.ipdown_arg;
328
jjako52c24142002-12-16 13:33:51 +0000329 /* Timelimit */
330 timelimit = args_info.timelimit_arg;
331 starttime = time(NULL);
332
333 /* qos */
334 qos.l = 3;
jjako52c24142002-12-16 13:33:51 +0000335 qos.v[2] = (args_info.qos_arg) & 0xff;
336 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
337 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
jjakoa7cd2492003-04-11 09:40:12 +0000338
jjako52c24142002-12-16 13:33:51 +0000339 /* apn */
jjakoa7cd2492003-04-11 09:40:12 +0000340 if (strlen(args_info.apn_arg) > (sizeof(apn.v)-1)) {
341 printf("Invalid APN\n");
342 return -1;
jjako52c24142002-12-16 13:33:51 +0000343 }
344 apn.l = strlen(args_info.apn_arg) + 1;
jjako52c24142002-12-16 13:33:51 +0000345 apn.v[0] = (char) strlen(args_info.apn_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000346 strncpy(&apn.v[1], args_info.apn_arg, sizeof(apn.v)-1);
347
348
jjako52c24142002-12-16 13:33:51 +0000349
350 if (debug) printf("gtpclient: Initialising GTP tunnel\n");
351
jjakoa7cd2492003-04-11 09:40:12 +0000352 if (gtp_new(&gsn, args_info.statedir_arg, &listen_)) {
353 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
354 "Failed to create gtp");
355 exit(1);
356 }
357 if (gsn->fd > maxfd) maxfd = gsn->fd;
jjako52c24142002-12-16 13:33:51 +0000358
359
360 gtp_set_cb_gpdu(gsn, encaps_tun);
361 gtp_set_cb_delete_context(gsn, delete_context);
jjako52c24142002-12-16 13:33:51 +0000362 gtp_set_cb_create_context(gsn, create_context);
jjakoa7cd2492003-04-11 09:40:12 +0000363
364
365 /* Create a tunnel interface */
366 if (tun_new((struct tun_t**) &tun)) {
367 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
368 "Failed to create tun");
369 exit(1);
370 }
371
372 tun_setaddr(tun, &net, &net, &mask);
373 tun_set_cb_ind(tun, cb_tun_ind);
374 if (tun->fd > maxfd) maxfd = tun->fd;
375
376 if (ipup) {
377 char buf[1024];
378 char snet[100];
379 char smask[100];
380
381 strncpy(snet, inet_ntoa(net), sizeof(snet));
382 snet[sizeof(snet)-1] = 0;
383 strncpy(smask, inet_ntoa(mask), sizeof(smask));
384 smask[sizeof(smask)-1] = 0;
385
386 /* system("ipup /dev/tun0 192.168.0.10"); */
387 snprintf(buf, sizeof(buf), "%s %s %s %s",
388 ipup, tun->devname, snet, smask);
389 buf[sizeof(buf)-1] = 0;
390 if (debug) printf("%s\n", buf);
391 system(buf);
392 }
jjako52c24142002-12-16 13:33:51 +0000393
394 /******************************************************************/
395 /* Main select loop */
396 /******************************************************************/
397
398 while (((starttime + timelimit) > time(NULL)) || (0 == timelimit)) {
399
400 FD_ZERO(&fds);
jjakoa7cd2492003-04-11 09:40:12 +0000401 if (tun) FD_SET(tun->fd, &fds);
402 FD_SET(gsn->fd, &fds);
jjako52c24142002-12-16 13:33:51 +0000403
404 gtp_retranstimeout(gsn, &idleTime);
405 switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
406 case -1: /* Error with select() *
407 if (errno != EINTR)
408 syslog(LOG_ERR, "CTRL: Error with select(), quitting");
409 *goto leave_clear_call;*/
410 syslog(LOG_ERR, "GGSN: select = -1");
411 break;
412 case 0:
jjakoa7cd2492003-04-11 09:40:12 +0000413 /* printf("Select returned 0\n"); */
jjako52c24142002-12-16 13:33:51 +0000414 gtp_retrans(gsn); /* Only retransmit if nothing else */
415 break;
416 default:
417 break;
418 }
419
jjakoa7cd2492003-04-11 09:40:12 +0000420 if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
421 tun_decaps(tun) < 0) {
422 syslog(LOG_ERR, "TUN read failed (fd)=(%d)", tun->fd);
jjako52c24142002-12-16 13:33:51 +0000423 }
424
jjakoa7cd2492003-04-11 09:40:12 +0000425 if (FD_ISSET(gsn->fd, &fds))
426 gtp_decaps(gsn);
jjako52c24142002-12-16 13:33:51 +0000427
jjako4b26b512003-01-28 16:13:57 +0000428 }
jjako52c24142002-12-16 13:33:51 +0000429
430 gtp_free(gsn);
jjakoa7cd2492003-04-11 09:40:12 +0000431 tun_free(tun);
jjako52c24142002-12-16 13:33:51 +0000432
433 return 1;
434
435}
436