added missing equals() method assignment for ID_ANY identities
[strongswan.git] / src / starter / interfaces.c
1 /* strongSwan IPsec interfaces management
2 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include <sys/socket.h>
16 #include <sys/ioctl.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <errno.h>
21
22 #include <freeswan.h>
23
24 #include <constants.h>
25 #include <defs.h>
26 #include <log.h>
27
28 #include "interfaces.h"
29 #include "exec.h"
30 #include "files.h"
31
32 /*
33 * discover the default route via /proc/net/route
34 */
35 void
36 get_defaultroute(defaultroute_t *defaultroute)
37 {
38 FILE *fd;
39 char line[BUF_LEN];
40 bool first = TRUE;
41
42 memset(defaultroute, 0, sizeof(defaultroute_t));
43
44 fd = fopen("/proc/net/route", "r");
45
46 if (!fd)
47 {
48 plog("could not open 'proc/net/route'");
49 return;
50 }
51
52 while (fgets(line, sizeof(line), fd) != 0)
53 {
54 char iface[11];
55 char destination[9];
56 char gateway[11];
57 char flags[5];
58 char mask[9];
59
60 int refcnt;
61 int use;
62 int metric;
63 int items;
64
65 /* proc/net/route returns IP addresses in host order */
66 strcpy(gateway, "0h");
67
68 /* skip the header line */
69 if (first)
70 {
71 first = FALSE;
72 continue;
73 }
74
75 /* parsing a single line of proc/net/route */
76 items = sscanf(line, "%10s\t%8s\t%8s\t%5s\t%d\t%d\t%d\t%8s\t"
77 , iface, destination, gateway+2, flags, &refcnt, &use, &metric, mask);
78 if (items < 8)
79 {
80 plog("parsing error while scanning /proc/net/route");
81 continue;
82 }
83
84 /* check for defaultroute (destination 0.0.0.0 and mask 0.0.0.0) */
85 if (streq(destination, "00000000") && streq(mask, "00000000"))
86 {
87 if (defaultroute->defined)
88 {
89 plog("multiple default routes - cannot cope with %%defaultroute!!!");
90 defaultroute->defined = FALSE;
91 fclose(fd);
92 return;
93 }
94 ttoaddr(gateway, strlen(gateway), AF_INET, &defaultroute->nexthop);
95 strncpy(defaultroute->iface, iface, IFNAMSIZ);
96 defaultroute->defined = TRUE;
97 }
98 }
99 fclose(fd);
100
101 if (!defaultroute->defined)
102 {
103 plog("no default route - cannot cope with %%defaultroute!!!");
104 }
105 else
106 {
107 char addr_buf[20], nexthop_buf[20];
108 struct ifreq physreq;
109
110 int sock = socket(AF_INET, SOCK_DGRAM, 0);
111
112 /* determine IP address of iface */
113 if (sock < 0)
114 {
115 plog("could not open SOCK_DGRAM socket");
116 defaultroute->defined = FALSE;
117 return;
118 }
119 memset ((void*)&physreq, 0, sizeof(physreq));
120 strncpy(physreq.ifr_name, defaultroute->iface, IFNAMSIZ);
121 ioctl(sock, SIOCGIFADDR, &physreq);
122 close(sock);
123 defaultroute->addr.u.v4 = *((struct sockaddr_in *)&physreq.ifr_addr);
124
125 addrtot(&defaultroute->addr, 0, addr_buf, sizeof(addr_buf));
126 addrtot(&defaultroute->nexthop, 0, nexthop_buf, sizeof(nexthop_buf));
127
128 DBG(DBG_CONTROL,
129 DBG_log("Default route found: iface=%s, addr=%s, nexthop=%s"
130 , defaultroute->iface, addr_buf, nexthop_buf)
131 )
132
133 /* for backwards-compatibility with the awk shell scripts
134 * store the defaultroute in /var/run/ipsec.info
135 */
136 fd = fopen(INFO_FILE, "w");
137
138 if (fd)
139 {
140 fprintf(fd, "defaultroutephys=%s\n", defaultroute->iface );
141 fprintf(fd, "defaultroutevirt=ipsec0\n");
142 fprintf(fd, "defaultrouteaddr=%s\n", addr_buf);
143 fprintf(fd, "defaultroutenexthop=%s\n", nexthop_buf);
144 fclose(fd);
145 }
146 }
147 return;
148 }