blob: 5011ed17c40f2cce8674ad527698a29e8e3fd781 [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);
86 if(!file)
87 return;
88 fprintf(file, "%d\n", getpid());
89 fclose(file);
90}
91
92
93int encaps_printf(void *p, void *packet, unsigned len)
94{
95 int i;
96 if (debug) {
97 printf("The packet looks like this:\n");
98 for( i=0; i<len; i++) {
99 printf("%02x ", (unsigned char)*(char *)(packet+i));
100 if (!((i+1)%16)) printf("\n");
101 };
102 printf("\n");
103 }
104 return 0;
105}
106
jjako52c24142002-12-16 13:33:51 +0000107int delete_context(struct pdp_t *pdp) {
jjako49014712003-01-05 17:59:49 +0000108 if (debug) printf("Deleting PDP context\n");
jjakoa7cd2492003-04-11 09:40:12 +0000109 ippool_freeip((struct ippoolm_t *) pdp->peer);
jjako52c24142002-12-16 13:33:51 +0000110 return 0;
111}
112
113
jjako52c24142002-12-16 13:33:51 +0000114int create_context(struct pdp_t *pdp) {
jjakoa7cd2492003-04-11 09:40:12 +0000115 struct in_addr addr;
116 struct ippoolm_t *member;
jjako52c24142002-12-16 13:33:51 +0000117
118 if (debug) printf("Received create PDP context request\n");
119
120 pdp->eua.l=0; /* TODO: Indicates dynamic IP */
121
122 /* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */
123 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_neg));
jjakoa7cd2492003-04-11 09:40:12 +0000124 memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg));
jjako52c24142002-12-16 13:33:51 +0000125
jjakoa7cd2492003-04-11 09:40:12 +0000126 if (pdp_euaton(&pdp->eua, &addr)) {
127 addr.s_addr = 0; /* Request dynamic */
128 }
129
130 if (ippool_newip(ippool, &member, &addr)) {
131 return EOF; /* Allready in use, or no more available */
132 }
133
134 pdp_ntoeua(&member->addr, &pdp->eua);
jjakoa7c33812003-04-11 11:51:39 +0000135 pdp->peer = member;
jjakoa7cd2492003-04-11 09:40:12 +0000136 pdp->ipif = tun; /* TODO */
137 member->peer = pdp;
jjako52c24142002-12-16 13:33:51 +0000138
139 return 0; /* Success */
140}
141
142
jjakoa7cd2492003-04-11 09:40:12 +0000143/* Callback for receiving messages from tun */
144int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) {
145 struct ippoolm_t *ipm;
146 struct in_addr dst;
147 struct tun_packet_t *iph = (struct tun_packet_t*) pack;
148
149 dst.s_addr = iph->dst;
150
151 if (ippool_getip(ippool, &ipm, &dst)) {
jjako52c24142002-12-16 13:33:51 +0000152 if (debug) printf("Received packet with no destination!!!\n");
153 return 0;
154 }
jjakoa7cd2492003-04-11 09:40:12 +0000155
156 if (ipm->peer) /* Check if a peer protocol is defined */
157 gtp_gpdu(gsn, (struct pdp_t*) ipm->peer, pack, len);
158 return 0;
jjako52c24142002-12-16 13:33:51 +0000159}
160
jjako52c24142002-12-16 13:33:51 +0000161int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) {
162 /* printf("encaps_tun. Packet received: forwarding to tun\n");*/
163 return tun_encaps((struct tun_t*) pdp->ipif, pack, len);
164}
165
166
167int main(int argc, char **argv)
168{
169 /* gengeopt declarations */
170 struct gengetopt_args_info args_info;
171
172 struct hostent *host;
173
jjako52c24142002-12-16 13:33:51 +0000174
jjako52c24142002-12-16 13:33:51 +0000175 fd_set fds; /* For select() */
176 struct timeval idleTime; /* How long to select() */
jjako52c24142002-12-16 13:33:51 +0000177
jjako52c24142002-12-16 13:33:51 +0000178
179 int timelimit; /* Number of seconds to be connected */
180 int starttime; /* Time program was started */
181
182 /* open a connection to the syslog daemon */
183 /*openlog(PACKAGE, LOG_PID, LOG_DAEMON);*/
184 openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON);
185
186 if (cmdline_parser (argc, argv, &args_info) != 0)
187 exit(1);
188 if (args_info.debug_flag) {
189 printf("listen: %s\n", args_info.listen_arg);
190 printf("conf: %s\n", args_info.conf_arg);
191 printf("fg: %d\n", args_info.fg_flag);
192 printf("debug: %d\n", args_info.debug_flag);
193 printf("qos: %#08x\n", args_info.qos_arg);
194 printf("apn: %s\n", args_info.apn_arg);
195 printf("net: %s\n", args_info.net_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000196 printf("dynip: %s\n", args_info.dynip_arg);
197 printf("statip: %s\n", args_info.statip_arg);
jjako4b26b512003-01-28 16:13:57 +0000198 printf("ipup: %s\n", args_info.ipup_arg);
199 printf("ipdown: %s\n", args_info.ipdown_arg);
jjako52c24142002-12-16 13:33:51 +0000200 printf("pidfile: %s\n", args_info.pidfile_arg);
201 printf("statedir: %s\n", args_info.statedir_arg);
202 printf("timelimit: %d\n", args_info.timelimit_arg);
203 }
204
205 /* Try out our new parser */
206
207 if (cmdline_parser_configfile (args_info.conf_arg, &args_info, 0) != 0)
208 exit(1);
209 if (args_info.debug_flag) {
210 printf("cmdline_parser_configfile\n");
211 printf("listen: %s\n", args_info.listen_arg);
212 printf("conf: %s\n", args_info.conf_arg);
213 printf("fg: %d\n", args_info.fg_flag);
214 printf("debug: %d\n", args_info.debug_flag);
215 printf("qos: %#08x\n", args_info.qos_arg);
216 printf("apn: %s\n", args_info.apn_arg);
217 printf("net: %s\n", args_info.net_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000218 printf("dynip: %s\n", args_info.dynip_arg);
219 printf("statip: %s\n", args_info.statip_arg);
jjako4b26b512003-01-28 16:13:57 +0000220 printf("ipup: %s\n", args_info.ipup_arg);
221 printf("ipdown: %s\n", args_info.ipdown_arg);
jjako52c24142002-12-16 13:33:51 +0000222 printf("pidfile: %s\n", args_info.pidfile_arg);
223 printf("statedir: %s\n", args_info.statedir_arg);
224 printf("timelimit: %d\n", args_info.timelimit_arg);
225 }
226
227 /* Handle each option */
228
229 /* foreground */
230 /* If flag not given run as a daemon */
231 if (!args_info.fg_flag)
232 {
233 closelog();
234 /* Close the standard file descriptors. */
235 /* Is this really needed ? */
236 freopen("/dev/null", "w", stdout);
237 freopen("/dev/null", "w", stderr);
238 freopen("/dev/null", "r", stdin);
239 daemon(0, 0);
240 /* Open log again. This time with new pid */
241 openlog(PACKAGE, LOG_PID, LOG_DAEMON);
242 }
243
244 /* debug */
245 debug = args_info.debug_flag;
246
247 /* pidfile */
248 /* This has to be done after we have our final pid */
249 if (args_info.pidfile_arg) {
250 log_pid(args_info.pidfile_arg);
251 }
252
253 /* listen */
254 /* If no listen option is specified listen to any local port */
255 /* Do hostname lookup to translate hostname to IP address */
256 if (args_info.listen_arg) {
257 if (!(host = gethostbyname(args_info.listen_arg))) {
258 fprintf(stderr, "%s: Invalid listening address: %s!\n",
259 PACKAGE, args_info.listen_arg);
260 syslog(LOG_ERR, "Invalid listening address: %s!",
261 args_info.listen_arg);
jjakoa7c33812003-04-11 11:51:39 +0000262 exit(1);
jjako52c24142002-12-16 13:33:51 +0000263 }
264 else {
jjakoa7cd2492003-04-11 09:40:12 +0000265 memcpy(&listen_.s_addr, host->h_addr, host->h_length);
jjako52c24142002-12-16 13:33:51 +0000266 }
267 }
268 else {
jjakoa7cd2492003-04-11 09:40:12 +0000269 listen_.s_addr = htonl(INADDR_ANY);
jjako52c24142002-12-16 13:33:51 +0000270 }
271
272 /* net */
jjakoa7cd2492003-04-11 09:40:12 +0000273 /* Store net as in_addr net and mask */
jjako52c24142002-12-16 13:33:51 +0000274 if (args_info.net_arg) {
jjakoa7cd2492003-04-11 09:40:12 +0000275 if(ippool_aton(&net, &mask, args_info.net_arg, 0)) {
276 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
277 "Invalid network address: %s!", args_info.net_arg);
jjakoa7c33812003-04-11 11:51:39 +0000278 exit(1);
jjako52c24142002-12-16 13:33:51 +0000279 }
280 }
jjakoa7c33812003-04-11 11:51:39 +0000281 else {
282 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
283 "Network address must be specified: %s!", args_info.net_arg);
284 exit(1);
285 }
jjako52c24142002-12-16 13:33:51 +0000286
jjakoa7cd2492003-04-11 09:40:12 +0000287 /* dynip */
288 if (!args_info.dynip_arg) {
jjakoa7c33812003-04-11 11:51:39 +0000289 if (ippool_new(&ippool, args_info.net_arg,
290 IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) {
291 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
292 "Failed to allocate IP pool!");
293 exit(1);
294 }
jjakoa7cd2492003-04-11 09:40:12 +0000295 }
296 else {
297 if (ippool_new(&ippool, args_info.dynip_arg,
298 IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) {
299 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
300 "Failed to allocate IP pool!");
jjakoa7c33812003-04-11 11:51:39 +0000301 exit(1);
jjako52c24142002-12-16 13:33:51 +0000302 }
303 }
304
jjakoa7cd2492003-04-11 09:40:12 +0000305 /* DNS1 and DNS2 */
306 dns1.s_addr = 0;
307 if (args_info.pcodns1_arg)
308 inet_aton(args_info.pcodns1_arg, &dns1);
309
310 dns2.s_addr = 0;
311 if (args_info.pcodns2_arg)
312 inet_aton(args_info.pcodns2_arg, &dns2);
313
314 pco.l = 20;
315 pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */
316 pco.v[1] = 0x80; /* IPCP */
317 pco.v[2] = 0x21;
318 pco.v[3] = 0x10; /* Length of contents */
319 pco.v[4] = 0x02; /* ACK */
320 pco.v[5] = 0x00; /* ID: Need to match request */
321 pco.v[6] = 0x00; /* Length */
322 pco.v[7] = 0x10;
323 pco.v[8] = 0x81; /* DNS 1 */
324 pco.v[9] = 0x06;
325 memcpy(&pco.v[10], &dns1, sizeof(dns1));
326 pco.v[14] = 0x83;
327 pco.v[15] = 0x06; /* DNS 2 */
328 memcpy(&pco.v[16], &dns2, sizeof(dns2));
329
jjako4b26b512003-01-28 16:13:57 +0000330 /* ipup */
331 ipup = args_info.ipup_arg;
332
333 /* ipdown */
334 ipdown = args_info.ipdown_arg;
335
jjako52c24142002-12-16 13:33:51 +0000336 /* Timelimit */
337 timelimit = args_info.timelimit_arg;
338 starttime = time(NULL);
339
340 /* qos */
341 qos.l = 3;
jjako52c24142002-12-16 13:33:51 +0000342 qos.v[2] = (args_info.qos_arg) & 0xff;
343 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
344 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
jjakoa7cd2492003-04-11 09:40:12 +0000345
jjako52c24142002-12-16 13:33:51 +0000346 /* apn */
jjakoa7cd2492003-04-11 09:40:12 +0000347 if (strlen(args_info.apn_arg) > (sizeof(apn.v)-1)) {
348 printf("Invalid APN\n");
349 return -1;
jjako52c24142002-12-16 13:33:51 +0000350 }
351 apn.l = strlen(args_info.apn_arg) + 1;
jjako52c24142002-12-16 13:33:51 +0000352 apn.v[0] = (char) strlen(args_info.apn_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000353 strncpy(&apn.v[1], args_info.apn_arg, sizeof(apn.v)-1);
354
jjako52c24142002-12-16 13:33:51 +0000355
356 if (debug) printf("gtpclient: Initialising GTP tunnel\n");
357
jjakoa7cd2492003-04-11 09:40:12 +0000358 if (gtp_new(&gsn, args_info.statedir_arg, &listen_)) {
359 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
360 "Failed to create gtp");
361 exit(1);
362 }
363 if (gsn->fd > maxfd) maxfd = gsn->fd;
jjako52c24142002-12-16 13:33:51 +0000364
365 gtp_set_cb_gpdu(gsn, encaps_tun);
366 gtp_set_cb_delete_context(gsn, delete_context);
jjako52c24142002-12-16 13:33:51 +0000367 gtp_set_cb_create_context(gsn, create_context);
jjakoa7cd2492003-04-11 09:40:12 +0000368
369
370 /* Create a tunnel interface */
371 if (tun_new((struct tun_t**) &tun)) {
372 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
373 "Failed to create tun");
374 exit(1);
375 }
376
377 tun_setaddr(tun, &net, &net, &mask);
378 tun_set_cb_ind(tun, cb_tun_ind);
379 if (tun->fd > maxfd) maxfd = tun->fd;
380
jjako9c7ff082003-04-11 10:01:41 +0000381 if (ipup) tun_runscript(tun, ipup);
jjako52c24142002-12-16 13:33:51 +0000382
383 /******************************************************************/
384 /* Main select loop */
385 /******************************************************************/
386
387 while (((starttime + timelimit) > time(NULL)) || (0 == timelimit)) {
388
389 FD_ZERO(&fds);
jjakoa7cd2492003-04-11 09:40:12 +0000390 if (tun) FD_SET(tun->fd, &fds);
391 FD_SET(gsn->fd, &fds);
jjako52c24142002-12-16 13:33:51 +0000392
393 gtp_retranstimeout(gsn, &idleTime);
394 switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
395 case -1: /* Error with select() *
396 if (errno != EINTR)
397 syslog(LOG_ERR, "CTRL: Error with select(), quitting");
398 *goto leave_clear_call;*/
399 syslog(LOG_ERR, "GGSN: select = -1");
400 break;
401 case 0:
jjakoa7cd2492003-04-11 09:40:12 +0000402 /* printf("Select returned 0\n"); */
jjako52c24142002-12-16 13:33:51 +0000403 gtp_retrans(gsn); /* Only retransmit if nothing else */
404 break;
405 default:
406 break;
407 }
408
jjakoa7cd2492003-04-11 09:40:12 +0000409 if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
410 tun_decaps(tun) < 0) {
411 syslog(LOG_ERR, "TUN read failed (fd)=(%d)", tun->fd);
jjako52c24142002-12-16 13:33:51 +0000412 }
413
jjakoa7cd2492003-04-11 09:40:12 +0000414 if (FD_ISSET(gsn->fd, &fds))
415 gtp_decaps(gsn);
jjako52c24142002-12-16 13:33:51 +0000416
jjako4b26b512003-01-28 16:13:57 +0000417 }
jjako52c24142002-12-16 13:33:51 +0000418
419 gtp_free(gsn);
jjakoa7cd2492003-04-11 09:40:12 +0000420 tun_free(tun);
jjako52c24142002-12-16 13:33:51 +0000421
422 return 1;
423
424}
425