summaryrefslogtreecommitdiff
path: root/programs/starter/klips.c
blob: 5595eb6eb999360b3f2ff70153aaa0f9389e7094 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* strongSwan KLIPS starter
 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * RCSID $Id: klips.c,v 1.8 2006/02/15 18:33:57 as Exp $
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>

#include <freeswan.h>

#include "../pluto/constants.h"
#include "../pluto/defs.h"
#include "../pluto/log.h"

#include "confread.h"
#include "klips.h"
#include "files.h"
#include "exec.h"

static int _klips_module_loaded = 0;

bool
starter_klips_init(void)
{
    struct stat stb;

    if (stat(PROC_IPSECVERSION, &stb) != 0)
    {
	if (stat(PROC_MODULES, &stb) == 0)
	{
	    unsetenv("MODPATH");
	    unsetenv("MODULECONF");
	    system("depmod -a >/dev/null 2>&1");
	    system("modprobe -qv ipsec");
	}
	if (stat(PROC_IPSECVERSION, &stb) == 0)
	{
	    _klips_module_loaded = 1;
	}
	else
	{
	    DBG(DBG_CONTROL,
		DBG_log("kernel appears to lack KLIPS")
	    )
	    return FALSE;
	}
    }

    /* make sure that all available crypto algorithms are loaded */
    if (stat(PROC_MODULES, &stb) == 0)
    {
	system("modprobe -qv ipsec_aes");
	system("modprobe -qv ipsec_serpent");
	system("modprobe -qv ipsec_twofish");
	system("modprobe -qv ipsec_blowfish");
	system("modprobe -qv ipsec_sha2");
    }

    starter_klips_clear();

    DBG(DBG_CONTROL,
	DBG_log("Found KLIPS IPsec stack")
    )
    return TRUE;
}

static void
_sysflags (char *name, int value)
{
    int res = starter_exec("echo %d >%s/%s 2>/dev/null"
			, value? 1 : 0, PROC_SYSFLAGS, name);

    if (res)
	plog("can't set sysflag %s to %d", name, value? 1 : 0);
}

void
starter_klips_set_config(starter_config_t *cfg)
{
    char **l;

    _sysflags("icmp", cfg->setup.fragicmp);
    _sysflags("inbound_policy_check", 1);
    /* _sysflags("no_eroute_pass", 0); */
    /* _sysflags("opportunistic", 0);  */
    _sysflags("tos", cfg->setup.hidetos);

    starter_exec("%s/klipsdebug --none", IPSEC_EXECDIR);
    for (l = cfg->setup.klipsdebug; l && *l; l++)
    {
	if ((streq(*l, "none")) || (streq(*l, "all")))
	    starter_exec("%s/klipsdebug --%s", IPSEC_EXECDIR, *l);
	else
	    starter_exec("%s/klipsdebug --set %s", IPSEC_EXECDIR, *l);
    }

    starter_exec("%s/eroute --del --eraf inet --src 0/0 --dst 0/0 2>/dev/null"
		, IPSEC_EXECDIR);
    starter_exec("%s/eroute --label packetdefault --replace --eraf inet "
		 "--src 0/0 --dst 0/0 --said %%%s", IPSEC_EXECDIR
		, cfg->setup.packetdefault ? cfg->setup.packetdefault : "drop");
}

void
starter_klips_clear(void)
{
    system(IPSEC_EXECDIR"/eroute --clear");
    system(IPSEC_EXECDIR"/spi --clear");
    system(IPSEC_EXECDIR"/klipsdebug --none");
}

void
starter_klips_cleanup(void)
{
    starter_klips_clear();
    if (_klips_module_loaded)
    {
	system("rmmod ipsec");
	_klips_module_loaded = 0;
    }
}