blob: 25da64ccfaf70735419a438df926c7b1c0415e24 [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() */
jjakoa7cd2492003-04-11 09:40:12 +000063
64struct in_addr listen_;
jjako52c24142002-12-16 13:33:51 +000065struct in_addr net, mask; /* Network interface */
jjakoa7cd2492003-04-11 09:40:12 +000066struct in_addr dns1, dns2; /* PCO DNS address */
67char *ipup, *ipdown; /* Filename of scripts */
68int debug; /* Print debug output */
69struct ul255_t pco;
70struct ul255_t qos;
71struct ul255_t apn;
72
jjako9c7ff082003-04-11 10:01:41 +000073struct gsn_t *gsn; /* GSN instance */
jjakoa7cd2492003-04-11 09:40:12 +000074struct tun_t *tun; /* TUN instance */
75struct ippool_t *ippool; /* Pool of IP addresses */
jjako52c24142002-12-16 13:33:51 +000076
77
78/* Used to write process ID to file. Assume someone else will delete */
79void log_pid(char *pidfile) {
80 FILE *file;
81 mode_t oldmask;
82
83 oldmask = umask(022);
84 file = fopen(pidfile, "w");
85 umask(oldmask);
jjakoe0149782003-07-06 17:07:04 +000086 if(!file) {
87 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
88 "Failed to create process ID file: %s!", pidfile);
jjako52c24142002-12-16 13:33:51 +000089 return;
jjakoe0149782003-07-06 17:07:04 +000090 }
jjako52c24142002-12-16 13:33:51 +000091 fprintf(file, "%d\n", getpid());
92 fclose(file);
93}
94
95
96int encaps_printf(void *p, void *packet, unsigned len)
97{
98 int i;
99 if (debug) {
100 printf("The packet looks like this:\n");
101 for( i=0; i<len; i++) {
102 printf("%02x ", (unsigned char)*(char *)(packet+i));
103 if (!((i+1)%16)) printf("\n");
104 };
105 printf("\n");
106 }
107 return 0;
108}
109
jjako52c24142002-12-16 13:33:51 +0000110int delete_context(struct pdp_t *pdp) {
jjako49014712003-01-05 17:59:49 +0000111 if (debug) printf("Deleting PDP context\n");
jjako08d331d2003-10-13 20:33:30 +0000112 if (pdp->peer)
113 ippool_freeip(ippool, (struct ippoolm_t *) pdp->peer);
114 else
115 sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Peer not defined!");
jjako52c24142002-12-16 13:33:51 +0000116 return 0;
117}
118
119
jjako08d331d2003-10-13 20:33:30 +0000120int create_context_ind(struct pdp_t *pdp) {
jjakoa7cd2492003-04-11 09:40:12 +0000121 struct in_addr addr;
122 struct ippoolm_t *member;
jjako52c24142002-12-16 13:33:51 +0000123
124 if (debug) printf("Received create PDP context request\n");
125
126 pdp->eua.l=0; /* TODO: Indicates dynamic IP */
127
128 /* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */
129 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_neg));
jjakoa7cd2492003-04-11 09:40:12 +0000130 memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg));
jjako52c24142002-12-16 13:33:51 +0000131
jjako08d331d2003-10-13 20:33:30 +0000132 memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */
133 pdp->qos_neg.l = pdp->qos_req.l;
134
jjakoa7cd2492003-04-11 09:40:12 +0000135 if (pdp_euaton(&pdp->eua, &addr)) {
136 addr.s_addr = 0; /* Request dynamic */
137 }
138
139 if (ippool_newip(ippool, &member, &addr)) {
jjako08d331d2003-10-13 20:33:30 +0000140 gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES);
141 return 0; /* Allready in use, or no more available */
jjakoa7cd2492003-04-11 09:40:12 +0000142 }
143
144 pdp_ntoeua(&member->addr, &pdp->eua);
jjakoa7c33812003-04-11 11:51:39 +0000145 pdp->peer = member;
jjakoa7cd2492003-04-11 09:40:12 +0000146 pdp->ipif = tun; /* TODO */
147 member->peer = pdp;
jjako52c24142002-12-16 13:33:51 +0000148
jjako08d331d2003-10-13 20:33:30 +0000149 gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ);
jjako52c24142002-12-16 13:33:51 +0000150 return 0; /* Success */
151}
152
153
jjakoa7cd2492003-04-11 09:40:12 +0000154/* Callback for receiving messages from tun */
155int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) {
156 struct ippoolm_t *ipm;
157 struct in_addr dst;
158 struct tun_packet_t *iph = (struct tun_packet_t*) pack;
159
160 dst.s_addr = iph->dst;
161
162 if (ippool_getip(ippool, &ipm, &dst)) {
jjako52c24142002-12-16 13:33:51 +0000163 if (debug) printf("Received packet with no destination!!!\n");
164 return 0;
165 }
jjakoa7cd2492003-04-11 09:40:12 +0000166
167 if (ipm->peer) /* Check if a peer protocol is defined */
jjako08d331d2003-10-13 20:33:30 +0000168 gtp_data_req(gsn, (struct pdp_t*) ipm->peer, pack, len);
jjakoa7cd2492003-04-11 09:40:12 +0000169 return 0;
jjako52c24142002-12-16 13:33:51 +0000170}
171
jjako52c24142002-12-16 13:33:51 +0000172int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) {
173 /* printf("encaps_tun. Packet received: forwarding to tun\n");*/
174 return tun_encaps((struct tun_t*) pdp->ipif, pack, len);
175}
176
177
178int main(int argc, char **argv)
179{
180 /* gengeopt declarations */
181 struct gengetopt_args_info args_info;
182
183 struct hostent *host;
184
jjako52c24142002-12-16 13:33:51 +0000185
jjako52c24142002-12-16 13:33:51 +0000186 fd_set fds; /* For select() */
187 struct timeval idleTime; /* How long to select() */
jjako52c24142002-12-16 13:33:51 +0000188
jjako52c24142002-12-16 13:33:51 +0000189
190 int timelimit; /* Number of seconds to be connected */
191 int starttime; /* Time program was started */
192
193 /* open a connection to the syslog daemon */
194 /*openlog(PACKAGE, LOG_PID, LOG_DAEMON);*/
195 openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON);
196
197 if (cmdline_parser (argc, argv, &args_info) != 0)
198 exit(1);
199 if (args_info.debug_flag) {
200 printf("listen: %s\n", args_info.listen_arg);
201 printf("conf: %s\n", args_info.conf_arg);
202 printf("fg: %d\n", args_info.fg_flag);
203 printf("debug: %d\n", args_info.debug_flag);
204 printf("qos: %#08x\n", args_info.qos_arg);
205 printf("apn: %s\n", args_info.apn_arg);
206 printf("net: %s\n", args_info.net_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000207 printf("dynip: %s\n", args_info.dynip_arg);
208 printf("statip: %s\n", args_info.statip_arg);
jjako4b26b512003-01-28 16:13:57 +0000209 printf("ipup: %s\n", args_info.ipup_arg);
210 printf("ipdown: %s\n", args_info.ipdown_arg);
jjako52c24142002-12-16 13:33:51 +0000211 printf("pidfile: %s\n", args_info.pidfile_arg);
212 printf("statedir: %s\n", args_info.statedir_arg);
213 printf("timelimit: %d\n", args_info.timelimit_arg);
214 }
215
216 /* Try out our new parser */
217
218 if (cmdline_parser_configfile (args_info.conf_arg, &args_info, 0) != 0)
219 exit(1);
220 if (args_info.debug_flag) {
221 printf("cmdline_parser_configfile\n");
222 printf("listen: %s\n", args_info.listen_arg);
223 printf("conf: %s\n", args_info.conf_arg);
224 printf("fg: %d\n", args_info.fg_flag);
225 printf("debug: %d\n", args_info.debug_flag);
226 printf("qos: %#08x\n", args_info.qos_arg);
227 printf("apn: %s\n", args_info.apn_arg);
228 printf("net: %s\n", args_info.net_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000229 printf("dynip: %s\n", args_info.dynip_arg);
230 printf("statip: %s\n", args_info.statip_arg);
jjako4b26b512003-01-28 16:13:57 +0000231 printf("ipup: %s\n", args_info.ipup_arg);
232 printf("ipdown: %s\n", args_info.ipdown_arg);
jjako52c24142002-12-16 13:33:51 +0000233 printf("pidfile: %s\n", args_info.pidfile_arg);
234 printf("statedir: %s\n", args_info.statedir_arg);
235 printf("timelimit: %d\n", args_info.timelimit_arg);
236 }
237
238 /* Handle each option */
239
jjako52c24142002-12-16 13:33:51 +0000240 /* debug */
241 debug = args_info.debug_flag;
242
jjako52c24142002-12-16 13:33:51 +0000243 /* listen */
jjako52c24142002-12-16 13:33:51 +0000244 /* Do hostname lookup to translate hostname to IP address */
jjakoe0149782003-07-06 17:07:04 +0000245 /* Any port listening is not possible as a valid address is */
246 /* required for create_pdp_context_response messages */
jjako52c24142002-12-16 13:33:51 +0000247 if (args_info.listen_arg) {
248 if (!(host = gethostbyname(args_info.listen_arg))) {
jjakoe0149782003-07-06 17:07:04 +0000249 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
250 "Invalid listening address: %s!", args_info.listen_arg);
jjakoa7c33812003-04-11 11:51:39 +0000251 exit(1);
jjako52c24142002-12-16 13:33:51 +0000252 }
253 else {
jjakoa7cd2492003-04-11 09:40:12 +0000254 memcpy(&listen_.s_addr, host->h_addr, host->h_length);
jjako52c24142002-12-16 13:33:51 +0000255 }
256 }
257 else {
jjakoe0149782003-07-06 17:07:04 +0000258 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
259 "Listening address must be specified! "
260 "Please use command line option --listen or "
261 "edit %s configuration file\n", args_info.conf_arg);
262 exit(1);
jjako52c24142002-12-16 13:33:51 +0000263 }
264
jjako88c22162003-07-06 19:33:18 +0000265
jjako52c24142002-12-16 13:33:51 +0000266 /* net */
jjakoa7cd2492003-04-11 09:40:12 +0000267 /* Store net as in_addr net and mask */
jjako52c24142002-12-16 13:33:51 +0000268 if (args_info.net_arg) {
jjakoa7cd2492003-04-11 09:40:12 +0000269 if(ippool_aton(&net, &mask, args_info.net_arg, 0)) {
270 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
271 "Invalid network address: %s!", args_info.net_arg);
jjakoa7c33812003-04-11 11:51:39 +0000272 exit(1);
jjako52c24142002-12-16 13:33:51 +0000273 }
274 }
jjakoa7c33812003-04-11 11:51:39 +0000275 else {
276 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
277 "Network address must be specified: %s!", args_info.net_arg);
278 exit(1);
279 }
jjako52c24142002-12-16 13:33:51 +0000280
jjakoa7cd2492003-04-11 09:40:12 +0000281 /* dynip */
282 if (!args_info.dynip_arg) {
jjako88c22162003-07-06 19:33:18 +0000283 if (ippool_new(&ippool, args_info.net_arg, NULL, 1, 0,
jjakoa7c33812003-04-11 11:51:39 +0000284 IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) {
285 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
286 "Failed to allocate IP pool!");
287 exit(1);
288 }
jjakoa7cd2492003-04-11 09:40:12 +0000289 }
290 else {
jjako88c22162003-07-06 19:33:18 +0000291 if (ippool_new(&ippool, args_info.dynip_arg, NULL, 1 ,0,
jjakoa7cd2492003-04-11 09:40:12 +0000292 IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) {
293 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
294 "Failed to allocate IP pool!");
jjakoa7c33812003-04-11 11:51:39 +0000295 exit(1);
jjako52c24142002-12-16 13:33:51 +0000296 }
297 }
298
jjakoa7cd2492003-04-11 09:40:12 +0000299 /* DNS1 and DNS2 */
300 dns1.s_addr = 0;
301 if (args_info.pcodns1_arg)
302 inet_aton(args_info.pcodns1_arg, &dns1);
303
304 dns2.s_addr = 0;
305 if (args_info.pcodns2_arg)
306 inet_aton(args_info.pcodns2_arg, &dns2);
307
308 pco.l = 20;
309 pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */
310 pco.v[1] = 0x80; /* IPCP */
311 pco.v[2] = 0x21;
312 pco.v[3] = 0x10; /* Length of contents */
313 pco.v[4] = 0x02; /* ACK */
314 pco.v[5] = 0x00; /* ID: Need to match request */
315 pco.v[6] = 0x00; /* Length */
316 pco.v[7] = 0x10;
317 pco.v[8] = 0x81; /* DNS 1 */
318 pco.v[9] = 0x06;
319 memcpy(&pco.v[10], &dns1, sizeof(dns1));
320 pco.v[14] = 0x83;
321 pco.v[15] = 0x06; /* DNS 2 */
322 memcpy(&pco.v[16], &dns2, sizeof(dns2));
323
jjako4b26b512003-01-28 16:13:57 +0000324 /* ipup */
325 ipup = args_info.ipup_arg;
326
327 /* ipdown */
328 ipdown = args_info.ipdown_arg;
329
jjako52c24142002-12-16 13:33:51 +0000330 /* Timelimit */
331 timelimit = args_info.timelimit_arg;
332 starttime = time(NULL);
333
334 /* qos */
335 qos.l = 3;
jjako52c24142002-12-16 13:33:51 +0000336 qos.v[2] = (args_info.qos_arg) & 0xff;
337 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
338 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
jjakoa7cd2492003-04-11 09:40:12 +0000339
jjako52c24142002-12-16 13:33:51 +0000340 /* apn */
jjakoa7cd2492003-04-11 09:40:12 +0000341 if (strlen(args_info.apn_arg) > (sizeof(apn.v)-1)) {
342 printf("Invalid APN\n");
343 return -1;
jjako52c24142002-12-16 13:33:51 +0000344 }
345 apn.l = strlen(args_info.apn_arg) + 1;
jjako52c24142002-12-16 13:33:51 +0000346 apn.v[0] = (char) strlen(args_info.apn_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000347 strncpy(&apn.v[1], args_info.apn_arg, sizeof(apn.v)-1);
jjakoe0149782003-07-06 17:07:04 +0000348
349
350 /* foreground */
351 /* If flag not given run as a daemon */
352 if (!args_info.fg_flag)
353 {
354 closelog();
355 /* Close the standard file descriptors. */
356 /* Is this really needed ? */
357 freopen("/dev/null", "w", stdout);
358 freopen("/dev/null", "w", stderr);
359 freopen("/dev/null", "r", stdin);
360 daemon(0, 0);
361 /* Open log again. This time with new pid */
362 openlog(PACKAGE, LOG_PID, LOG_DAEMON);
363 }
364
365 /* pidfile */
366 /* This has to be done after we have our final pid */
367 if (args_info.pidfile_arg) {
368 log_pid(args_info.pidfile_arg);
369 }
jjakoa7cd2492003-04-11 09:40:12 +0000370
jjako52c24142002-12-16 13:33:51 +0000371
372 if (debug) printf("gtpclient: Initialising GTP tunnel\n");
373
jjako1db1c812003-07-06 20:53:57 +0000374 if (gtp_new(&gsn, args_info.statedir_arg, &listen_, GTP_MODE_GGSN)) {
jjakoa7cd2492003-04-11 09:40:12 +0000375 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
376 "Failed to create gtp");
377 exit(1);
378 }
jjako08d331d2003-10-13 20:33:30 +0000379 if (gsn->fd0 > maxfd) maxfd = gsn->fd0;
380 if (gsn->fd1c > maxfd) maxfd = gsn->fd1c;
381 if (gsn->fd1u > maxfd) maxfd = gsn->fd1u;
jjako52c24142002-12-16 13:33:51 +0000382
jjako08d331d2003-10-13 20:33:30 +0000383 gtp_set_cb_data_ind(gsn, encaps_tun);
jjako52c24142002-12-16 13:33:51 +0000384 gtp_set_cb_delete_context(gsn, delete_context);
jjako08d331d2003-10-13 20:33:30 +0000385 gtp_set_cb_create_context_ind(gsn, create_context_ind);
jjakoa7cd2492003-04-11 09:40:12 +0000386
387
388 /* Create a tunnel interface */
389 if (tun_new((struct tun_t**) &tun)) {
390 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
391 "Failed to create tun");
392 exit(1);
393 }
394
395 tun_setaddr(tun, &net, &net, &mask);
396 tun_set_cb_ind(tun, cb_tun_ind);
397 if (tun->fd > maxfd) maxfd = tun->fd;
398
jjako9c7ff082003-04-11 10:01:41 +0000399 if (ipup) tun_runscript(tun, ipup);
jjako52c24142002-12-16 13:33:51 +0000400
401 /******************************************************************/
402 /* Main select loop */
403 /******************************************************************/
404
405 while (((starttime + timelimit) > time(NULL)) || (0 == timelimit)) {
406
407 FD_ZERO(&fds);
jjakoa7cd2492003-04-11 09:40:12 +0000408 if (tun) FD_SET(tun->fd, &fds);
jjako08d331d2003-10-13 20:33:30 +0000409 FD_SET(gsn->fd0, &fds);
410 FD_SET(gsn->fd1c, &fds);
411 FD_SET(gsn->fd1u, &fds);
jjako52c24142002-12-16 13:33:51 +0000412
413 gtp_retranstimeout(gsn, &idleTime);
414 switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
jjakoe0149782003-07-06 17:07:04 +0000415 case -1: /* errno == EINTR : unblocked signal */
416 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
417 "select() returned -1");
jjako52c24142002-12-16 13:33:51 +0000418 break;
419 case 0:
jjakoa7cd2492003-04-11 09:40:12 +0000420 /* printf("Select returned 0\n"); */
jjako52c24142002-12-16 13:33:51 +0000421 gtp_retrans(gsn); /* Only retransmit if nothing else */
422 break;
423 default:
424 break;
425 }
426
jjakoa7cd2492003-04-11 09:40:12 +0000427 if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
428 tun_decaps(tun) < 0) {
jjakoe0149782003-07-06 17:07:04 +0000429 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
430 "TUN read failed (fd)=(%d)", tun->fd);
jjako52c24142002-12-16 13:33:51 +0000431 }
jjako08d331d2003-10-13 20:33:30 +0000432
433 if (FD_ISSET(gsn->fd0, &fds))
434 gtp_decaps0(gsn);
435
436 if (FD_ISSET(gsn->fd1c, &fds))
437 gtp_decaps1c(gsn);
438
439 if (FD_ISSET(gsn->fd1u, &fds))
440 gtp_decaps1u(gsn);
jjako52c24142002-12-16 13:33:51 +0000441
jjako4b26b512003-01-28 16:13:57 +0000442 }
jjako52c24142002-12-16 13:33:51 +0000443
444 gtp_free(gsn);
jjakoa7cd2492003-04-11 09:40:12 +0000445 tun_free(tun);
jjako52c24142002-12-16 13:33:51 +0000446
447 return 1;
448
449}
450