blob: 7a20feb135d7e44fce54d41f1f68f7f837380907 [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");
jjakoa7cd2492003-04-11 09:40:12 +0000112 ippool_freeip((struct ippoolm_t *) pdp->peer);
jjako52c24142002-12-16 13:33:51 +0000113 return 0;
114}
115
116
jjako52c24142002-12-16 13:33:51 +0000117int create_context(struct pdp_t *pdp) {
jjakoa7cd2492003-04-11 09:40:12 +0000118 struct in_addr addr;
119 struct ippoolm_t *member;
jjako52c24142002-12-16 13:33:51 +0000120
121 if (debug) printf("Received create PDP context request\n");
122
123 pdp->eua.l=0; /* TODO: Indicates dynamic IP */
124
125 /* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */
126 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_neg));
jjakoa7cd2492003-04-11 09:40:12 +0000127 memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg));
jjako52c24142002-12-16 13:33:51 +0000128
jjakoa7cd2492003-04-11 09:40:12 +0000129 if (pdp_euaton(&pdp->eua, &addr)) {
130 addr.s_addr = 0; /* Request dynamic */
131 }
132
133 if (ippool_newip(ippool, &member, &addr)) {
134 return EOF; /* Allready in use, or no more available */
135 }
136
137 pdp_ntoeua(&member->addr, &pdp->eua);
jjakoa7c33812003-04-11 11:51:39 +0000138 pdp->peer = member;
jjakoa7cd2492003-04-11 09:40:12 +0000139 pdp->ipif = tun; /* TODO */
140 member->peer = pdp;
jjako52c24142002-12-16 13:33:51 +0000141
142 return 0; /* Success */
143}
144
145
jjakoa7cd2492003-04-11 09:40:12 +0000146/* Callback for receiving messages from tun */
147int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) {
148 struct ippoolm_t *ipm;
149 struct in_addr dst;
150 struct tun_packet_t *iph = (struct tun_packet_t*) pack;
151
152 dst.s_addr = iph->dst;
153
154 if (ippool_getip(ippool, &ipm, &dst)) {
jjako52c24142002-12-16 13:33:51 +0000155 if (debug) printf("Received packet with no destination!!!\n");
156 return 0;
157 }
jjakoa7cd2492003-04-11 09:40:12 +0000158
159 if (ipm->peer) /* Check if a peer protocol is defined */
160 gtp_gpdu(gsn, (struct pdp_t*) ipm->peer, pack, len);
161 return 0;
jjako52c24142002-12-16 13:33:51 +0000162}
163
jjako52c24142002-12-16 13:33:51 +0000164int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) {
165 /* printf("encaps_tun. Packet received: forwarding to tun\n");*/
166 return tun_encaps((struct tun_t*) pdp->ipif, pack, len);
167}
168
169
170int main(int argc, char **argv)
171{
172 /* gengeopt declarations */
173 struct gengetopt_args_info args_info;
174
175 struct hostent *host;
176
jjako52c24142002-12-16 13:33:51 +0000177
jjako52c24142002-12-16 13:33:51 +0000178 fd_set fds; /* For select() */
179 struct timeval idleTime; /* How long to select() */
jjako52c24142002-12-16 13:33:51 +0000180
jjako52c24142002-12-16 13:33:51 +0000181
182 int timelimit; /* Number of seconds to be connected */
183 int starttime; /* Time program was started */
184
185 /* open a connection to the syslog daemon */
186 /*openlog(PACKAGE, LOG_PID, LOG_DAEMON);*/
187 openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON);
188
189 if (cmdline_parser (argc, argv, &args_info) != 0)
190 exit(1);
191 if (args_info.debug_flag) {
192 printf("listen: %s\n", args_info.listen_arg);
193 printf("conf: %s\n", args_info.conf_arg);
194 printf("fg: %d\n", args_info.fg_flag);
195 printf("debug: %d\n", args_info.debug_flag);
196 printf("qos: %#08x\n", args_info.qos_arg);
197 printf("apn: %s\n", args_info.apn_arg);
198 printf("net: %s\n", args_info.net_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000199 printf("dynip: %s\n", args_info.dynip_arg);
200 printf("statip: %s\n", args_info.statip_arg);
jjako4b26b512003-01-28 16:13:57 +0000201 printf("ipup: %s\n", args_info.ipup_arg);
202 printf("ipdown: %s\n", args_info.ipdown_arg);
jjako52c24142002-12-16 13:33:51 +0000203 printf("pidfile: %s\n", args_info.pidfile_arg);
204 printf("statedir: %s\n", args_info.statedir_arg);
205 printf("timelimit: %d\n", args_info.timelimit_arg);
206 }
207
208 /* Try out our new parser */
209
210 if (cmdline_parser_configfile (args_info.conf_arg, &args_info, 0) != 0)
211 exit(1);
212 if (args_info.debug_flag) {
213 printf("cmdline_parser_configfile\n");
214 printf("listen: %s\n", args_info.listen_arg);
215 printf("conf: %s\n", args_info.conf_arg);
216 printf("fg: %d\n", args_info.fg_flag);
217 printf("debug: %d\n", args_info.debug_flag);
218 printf("qos: %#08x\n", args_info.qos_arg);
219 printf("apn: %s\n", args_info.apn_arg);
220 printf("net: %s\n", args_info.net_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000221 printf("dynip: %s\n", args_info.dynip_arg);
222 printf("statip: %s\n", args_info.statip_arg);
jjako4b26b512003-01-28 16:13:57 +0000223 printf("ipup: %s\n", args_info.ipup_arg);
224 printf("ipdown: %s\n", args_info.ipdown_arg);
jjako52c24142002-12-16 13:33:51 +0000225 printf("pidfile: %s\n", args_info.pidfile_arg);
226 printf("statedir: %s\n", args_info.statedir_arg);
227 printf("timelimit: %d\n", args_info.timelimit_arg);
228 }
229
230 /* Handle each option */
231
jjako52c24142002-12-16 13:33:51 +0000232 /* debug */
233 debug = args_info.debug_flag;
234
jjako52c24142002-12-16 13:33:51 +0000235 /* listen */
jjako52c24142002-12-16 13:33:51 +0000236 /* Do hostname lookup to translate hostname to IP address */
jjakoe0149782003-07-06 17:07:04 +0000237 /* Any port listening is not possible as a valid address is */
238 /* required for create_pdp_context_response messages */
jjako52c24142002-12-16 13:33:51 +0000239 if (args_info.listen_arg) {
240 if (!(host = gethostbyname(args_info.listen_arg))) {
jjakoe0149782003-07-06 17:07:04 +0000241 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
242 "Invalid listening address: %s!", args_info.listen_arg);
jjakoa7c33812003-04-11 11:51:39 +0000243 exit(1);
jjako52c24142002-12-16 13:33:51 +0000244 }
245 else {
jjakoa7cd2492003-04-11 09:40:12 +0000246 memcpy(&listen_.s_addr, host->h_addr, host->h_length);
jjako52c24142002-12-16 13:33:51 +0000247 }
248 }
249 else {
jjakoe0149782003-07-06 17:07:04 +0000250 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
251 "Listening address must be specified! "
252 "Please use command line option --listen or "
253 "edit %s configuration file\n", args_info.conf_arg);
254 exit(1);
jjako52c24142002-12-16 13:33:51 +0000255 }
256
257 /* net */
jjakoa7cd2492003-04-11 09:40:12 +0000258 /* Store net as in_addr net and mask */
jjako52c24142002-12-16 13:33:51 +0000259 if (args_info.net_arg) {
jjakoa7cd2492003-04-11 09:40:12 +0000260 if(ippool_aton(&net, &mask, args_info.net_arg, 0)) {
261 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
262 "Invalid network address: %s!", args_info.net_arg);
jjakoa7c33812003-04-11 11:51:39 +0000263 exit(1);
jjako52c24142002-12-16 13:33:51 +0000264 }
265 }
jjakoa7c33812003-04-11 11:51:39 +0000266 else {
267 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
268 "Network address must be specified: %s!", args_info.net_arg);
269 exit(1);
270 }
jjako52c24142002-12-16 13:33:51 +0000271
jjakoa7cd2492003-04-11 09:40:12 +0000272 /* dynip */
273 if (!args_info.dynip_arg) {
jjakoa7c33812003-04-11 11:51:39 +0000274 if (ippool_new(&ippool, args_info.net_arg,
275 IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) {
276 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
277 "Failed to allocate IP pool!");
278 exit(1);
279 }
jjakoa7cd2492003-04-11 09:40:12 +0000280 }
281 else {
282 if (ippool_new(&ippool, args_info.dynip_arg,
283 IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) {
284 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
285 "Failed to allocate IP pool!");
jjakoa7c33812003-04-11 11:51:39 +0000286 exit(1);
jjako52c24142002-12-16 13:33:51 +0000287 }
288 }
289
jjakoa7cd2492003-04-11 09:40:12 +0000290 /* DNS1 and DNS2 */
291 dns1.s_addr = 0;
292 if (args_info.pcodns1_arg)
293 inet_aton(args_info.pcodns1_arg, &dns1);
294
295 dns2.s_addr = 0;
296 if (args_info.pcodns2_arg)
297 inet_aton(args_info.pcodns2_arg, &dns2);
298
299 pco.l = 20;
300 pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */
301 pco.v[1] = 0x80; /* IPCP */
302 pco.v[2] = 0x21;
303 pco.v[3] = 0x10; /* Length of contents */
304 pco.v[4] = 0x02; /* ACK */
305 pco.v[5] = 0x00; /* ID: Need to match request */
306 pco.v[6] = 0x00; /* Length */
307 pco.v[7] = 0x10;
308 pco.v[8] = 0x81; /* DNS 1 */
309 pco.v[9] = 0x06;
310 memcpy(&pco.v[10], &dns1, sizeof(dns1));
311 pco.v[14] = 0x83;
312 pco.v[15] = 0x06; /* DNS 2 */
313 memcpy(&pco.v[16], &dns2, sizeof(dns2));
314
jjako4b26b512003-01-28 16:13:57 +0000315 /* ipup */
316 ipup = args_info.ipup_arg;
317
318 /* ipdown */
319 ipdown = args_info.ipdown_arg;
320
jjako52c24142002-12-16 13:33:51 +0000321 /* Timelimit */
322 timelimit = args_info.timelimit_arg;
323 starttime = time(NULL);
324
325 /* qos */
326 qos.l = 3;
jjako52c24142002-12-16 13:33:51 +0000327 qos.v[2] = (args_info.qos_arg) & 0xff;
328 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
329 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
jjakoa7cd2492003-04-11 09:40:12 +0000330
jjako52c24142002-12-16 13:33:51 +0000331 /* apn */
jjakoa7cd2492003-04-11 09:40:12 +0000332 if (strlen(args_info.apn_arg) > (sizeof(apn.v)-1)) {
333 printf("Invalid APN\n");
334 return -1;
jjako52c24142002-12-16 13:33:51 +0000335 }
336 apn.l = strlen(args_info.apn_arg) + 1;
jjako52c24142002-12-16 13:33:51 +0000337 apn.v[0] = (char) strlen(args_info.apn_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000338 strncpy(&apn.v[1], args_info.apn_arg, sizeof(apn.v)-1);
jjakoe0149782003-07-06 17:07:04 +0000339
340
341 /* foreground */
342 /* If flag not given run as a daemon */
343 if (!args_info.fg_flag)
344 {
345 closelog();
346 /* Close the standard file descriptors. */
347 /* Is this really needed ? */
348 freopen("/dev/null", "w", stdout);
349 freopen("/dev/null", "w", stderr);
350 freopen("/dev/null", "r", stdin);
351 daemon(0, 0);
352 /* Open log again. This time with new pid */
353 openlog(PACKAGE, LOG_PID, LOG_DAEMON);
354 }
355
356 /* pidfile */
357 /* This has to be done after we have our final pid */
358 if (args_info.pidfile_arg) {
359 log_pid(args_info.pidfile_arg);
360 }
jjakoa7cd2492003-04-11 09:40:12 +0000361
jjako52c24142002-12-16 13:33:51 +0000362
363 if (debug) printf("gtpclient: Initialising GTP tunnel\n");
364
jjakoa7cd2492003-04-11 09:40:12 +0000365 if (gtp_new(&gsn, args_info.statedir_arg, &listen_)) {
366 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
367 "Failed to create gtp");
368 exit(1);
369 }
370 if (gsn->fd > maxfd) maxfd = gsn->fd;
jjako52c24142002-12-16 13:33:51 +0000371
372 gtp_set_cb_gpdu(gsn, encaps_tun);
373 gtp_set_cb_delete_context(gsn, delete_context);
jjako52c24142002-12-16 13:33:51 +0000374 gtp_set_cb_create_context(gsn, create_context);
jjakoa7cd2492003-04-11 09:40:12 +0000375
376
377 /* Create a tunnel interface */
378 if (tun_new((struct tun_t**) &tun)) {
379 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
380 "Failed to create tun");
381 exit(1);
382 }
383
384 tun_setaddr(tun, &net, &net, &mask);
385 tun_set_cb_ind(tun, cb_tun_ind);
386 if (tun->fd > maxfd) maxfd = tun->fd;
387
jjako9c7ff082003-04-11 10:01:41 +0000388 if (ipup) tun_runscript(tun, ipup);
jjako52c24142002-12-16 13:33:51 +0000389
390 /******************************************************************/
391 /* Main select loop */
392 /******************************************************************/
393
394 while (((starttime + timelimit) > time(NULL)) || (0 == timelimit)) {
395
396 FD_ZERO(&fds);
jjakoa7cd2492003-04-11 09:40:12 +0000397 if (tun) FD_SET(tun->fd, &fds);
398 FD_SET(gsn->fd, &fds);
jjako52c24142002-12-16 13:33:51 +0000399
400 gtp_retranstimeout(gsn, &idleTime);
401 switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
jjakoe0149782003-07-06 17:07:04 +0000402 case -1: /* errno == EINTR : unblocked signal */
403 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
404 "select() returned -1");
jjako52c24142002-12-16 13:33:51 +0000405 break;
406 case 0:
jjakoa7cd2492003-04-11 09:40:12 +0000407 /* printf("Select returned 0\n"); */
jjako52c24142002-12-16 13:33:51 +0000408 gtp_retrans(gsn); /* Only retransmit if nothing else */
409 break;
410 default:
411 break;
412 }
413
jjakoa7cd2492003-04-11 09:40:12 +0000414 if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
415 tun_decaps(tun) < 0) {
jjakoe0149782003-07-06 17:07:04 +0000416 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
417 "TUN read failed (fd)=(%d)", tun->fd);
jjako52c24142002-12-16 13:33:51 +0000418 }
419
jjakoa7cd2492003-04-11 09:40:12 +0000420 if (FD_ISSET(gsn->fd, &fds))
421 gtp_decaps(gsn);
jjako52c24142002-12-16 13:33:51 +0000422
jjako4b26b512003-01-28 16:13:57 +0000423 }
jjako52c24142002-12-16 13:33:51 +0000424
425 gtp_free(gsn);
jjakoa7cd2492003-04-11 09:40:12 +0000426 tun_free(tun);
jjako52c24142002-12-16 13:33:51 +0000427
428 return 1;
429
430}
431