2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
8 * Copyright (C) 2002-2009 OpenVPN Technologies, Inc. <sales@openvpn.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 ifconfig_pool_entry_free (struct ifconfig_pool_entry
*ipe
, bool hard
)
41 if (hard
&& ipe
->common_name
)
43 free (ipe
->common_name
);
44 ipe
->common_name
= NULL
;
47 ipe
->last_release
= 0;
49 ipe
->last_release
= now
;
53 ifconfig_pool_find (struct ifconfig_pool
*pool
, const char *common_name
)
56 time_t earliest_release
= 0;
57 int previous_usage
= -1;
60 for (i
= 0; i
< pool
->size
; ++i
)
62 struct ifconfig_pool_entry
*ipe
= &pool
->list
[i
];
66 * If duplicate_cn mode, take first available IP address
68 if (pool
->duplicate_cn
)
75 * Keep track of the unused IP address entry which
76 * was released earliest.
78 if ((new_usage
== -1 || ipe
->last_release
< earliest_release
) && !ipe
->fixed
)
80 earliest_release
= ipe
->last_release
;
85 * Keep track of a possible allocation to us
86 * from an earlier session.
88 if (previous_usage
< 0
91 && !strcmp (common_name
, ipe
->common_name
))
97 if (previous_usage
>= 0)
98 return previous_usage
;
107 * Verify start/end range
110 ifconfig_pool_verify_range (const int msglevel
, const in_addr_t start
, const in_addr_t end
)
112 struct gc_arena gc
= gc_new ();
117 msg (msglevel
, "--ifconfig-pool start IP [%s] is greater than end IP [%s]",
118 print_in_addr_t (start
, 0, &gc
),
119 print_in_addr_t (end
, 0, &gc
));
122 if (end
- start
>= IFCONFIG_POOL_MAX
)
124 msg (msglevel
, "--ifconfig-pool address range is too large [%s -> %s]. Current maximum is %d addresses, as defined by IFCONFIG_POOL_MAX variable.",
125 print_in_addr_t (start
, 0, &gc
),
126 print_in_addr_t (end
, 0, &gc
),
134 struct ifconfig_pool
*
135 ifconfig_pool_init (int type
, in_addr_t start
, in_addr_t end
, const bool duplicate_cn
)
137 struct gc_arena gc
= gc_new ();
138 struct ifconfig_pool
*pool
= NULL
;
140 ASSERT (start
<= end
&& end
- start
< IFCONFIG_POOL_MAX
);
141 ALLOC_OBJ_CLEAR (pool
, struct ifconfig_pool
);
144 pool
->duplicate_cn
= duplicate_cn
;
148 case IFCONFIG_POOL_30NET
:
149 pool
->base
= start
& ~3;
150 pool
->size
= (((end
| 3) + 1) - pool
->base
) >> 2;
152 case IFCONFIG_POOL_INDIV
:
154 pool
->size
= end
- start
+ 1;
160 ALLOC_ARRAY_CLEAR (pool
->list
, struct ifconfig_pool_entry
, pool
->size
);
162 msg (D_IFCONFIG_POOL
, "IFCONFIG POOL: base=%s size=%d",
163 print_in_addr_t (pool
->base
, 0, &gc
),
171 ifconfig_pool_free (struct ifconfig_pool
*pool
)
176 for (i
= 0; i
< pool
->size
; ++i
)
177 ifconfig_pool_entry_free (&pool
->list
[i
], true);
184 ifconfig_pool_acquire (struct ifconfig_pool
*pool
, in_addr_t
*local
, in_addr_t
*remote
, const char *common_name
)
188 i
= ifconfig_pool_find (pool
, common_name
);
191 struct ifconfig_pool_entry
*ipe
= &pool
->list
[i
];
192 ASSERT (!ipe
->in_use
);
193 ifconfig_pool_entry_free (ipe
, true);
196 ipe
->common_name
= string_alloc (common_name
, NULL
);
200 case IFCONFIG_POOL_30NET
:
202 in_addr_t b
= pool
->base
+ (i
<< 2);
207 case IFCONFIG_POOL_INDIV
:
209 in_addr_t b
= pool
->base
+ i
;
222 ifconfig_pool_release (struct ifconfig_pool
* pool
, ifconfig_pool_handle hand
, const bool hard
)
225 if (pool
&& hand
>= 0 && hand
< pool
->size
)
227 ifconfig_pool_entry_free (&pool
->list
[hand
], hard
);
234 * private access functions
237 static ifconfig_pool_handle
238 ifconfig_pool_ip_base_to_handle (const struct ifconfig_pool
* pool
, const in_addr_t addr
)
240 ifconfig_pool_handle ret
= -1;
244 case IFCONFIG_POOL_30NET
:
246 ret
= (addr
- pool
->base
) >> 2;
249 case IFCONFIG_POOL_INDIV
:
251 ret
= (addr
- pool
->base
);
258 if (ret
< 0 || ret
>= pool
->size
)
265 ifconfig_pool_handle_to_ip_base (const struct ifconfig_pool
* pool
, ifconfig_pool_handle hand
)
269 if (hand
>= 0 && hand
< pool
->size
)
273 case IFCONFIG_POOL_30NET
:
275 ret
= pool
->base
+ (hand
<< 2);;
278 case IFCONFIG_POOL_INDIV
:
280 ret
= pool
->base
+ hand
;
292 ifconfig_pool_set (struct ifconfig_pool
* pool
, const char *cn
, const in_addr_t addr
, const bool fixed
)
294 ifconfig_pool_handle h
= ifconfig_pool_ip_base_to_handle (pool
, addr
);
297 struct ifconfig_pool_entry
*e
= &pool
->list
[h
];
298 ifconfig_pool_entry_free (e
, true);
300 e
->common_name
= string_alloc (cn
, NULL
);
301 e
->last_release
= now
;
307 ifconfig_pool_list (const struct ifconfig_pool
* pool
, struct status_output
*out
)
311 struct gc_arena gc
= gc_new ();
314 for (i
= 0; i
< pool
->size
; ++i
)
316 const struct ifconfig_pool_entry
*e
= &pool
->list
[i
];
319 const in_addr_t ip
= ifconfig_pool_handle_to_ip_base (pool
, i
);
320 status_printf (out
, "%s,%s",
322 print_in_addr_t (ip
, 0, &gc
));
330 ifconfig_pool_msg (const struct ifconfig_pool
* pool
, int msglevel
)
332 struct status_output
*so
= status_open (NULL
, 0, msglevel
, NULL
, 0);
334 status_printf (so
, "IFCONFIG POOL LIST");
335 ifconfig_pool_list (pool
, so
);
340 * Deal with reading/writing the ifconfig pool database to a file
343 struct ifconfig_pool_persist
*
344 ifconfig_pool_persist_init (const char *filename
, int refresh_freq
)
346 struct ifconfig_pool_persist
*ret
;
350 ALLOC_OBJ_CLEAR (ret
, struct ifconfig_pool_persist
);
351 if (refresh_freq
> 0)
354 ret
->file
= status_open (filename
, refresh_freq
, -1, NULL
, STATUS_OUTPUT_READ
|STATUS_OUTPUT_WRITE
);
359 ret
->file
= status_open (filename
, 0, -1, NULL
, STATUS_OUTPUT_READ
);
365 ifconfig_pool_persist_close (struct ifconfig_pool_persist
*persist
)
370 status_close (persist
->file
);
376 ifconfig_pool_write_trigger (struct ifconfig_pool_persist
*persist
)
379 return status_trigger (persist
->file
);
385 ifconfig_pool_read (struct ifconfig_pool_persist
*persist
, struct ifconfig_pool
*pool
)
387 const int buf_size
= 128;
390 if (persist
&& persist
->file
&& pool
)
392 struct gc_arena gc
= gc_new ();
393 struct buffer in
= alloc_buf_gc (256, &gc
);
398 ALLOC_ARRAY_CLEAR_GC (cn_buf
, char, buf_size
, &gc
);
399 ALLOC_ARRAY_CLEAR_GC (ip_buf
, char, buf_size
, &gc
);
403 ASSERT (buf_init (&in
, 0));
404 if (!status_read (persist
->file
, &in
))
410 if (c
== '#' || c
== ';')
412 if (buf_parse (&in
, ',', cn_buf
, buf_size
)
413 && buf_parse (&in
, ',', ip_buf
, buf_size
))
416 const in_addr_t addr
= getaddr (GETADDR_HOST_ORDER
, ip_buf
, 0, &succeeded
, NULL
);
419 ifconfig_pool_set (pool
, cn_buf
, addr
, persist
->fixed
);
425 ifconfig_pool_msg (pool
, D_IFCONFIG_POOL
);
432 ifconfig_pool_write (struct ifconfig_pool_persist
*persist
, const struct ifconfig_pool
*pool
)
434 if (persist
&& persist
->file
&& (status_rw_flags (persist
->file
) & STATUS_OUTPUT_WRITE
) && pool
)
436 status_reset (persist
->file
);
437 ifconfig_pool_list (pool
, persist
->file
);
438 status_flush (persist
->file
);
446 #ifdef IFCONFIG_POOL_TEST
451 ifconfig_pool_test (in_addr_t start
, in_addr_t end
)
453 struct gc_arena gc
= gc_new ();
454 struct ifconfig_pool
*p
= ifconfig_pool_init (IFCONFIG_POOL_30NET
, start
, end
);
455 /*struct ifconfig_pool *p = ifconfig_pool_init (IFCONFIG_POOL_INDIV, start, end);*/
456 ifconfig_pool_handle array
[256];
461 msg (M_INFO
| M_NOPREFIX
, "************ 1");
462 for (i
= 0; i
< (int) SIZE (array
); ++i
)
465 ifconfig_pool_handle h
;
466 in_addr_t local
, remote
;
468 openvpn_snprintf (buf
, sizeof(buf
), "common-name-%d", i
);
474 h
= ifconfig_pool_acquire (p
, &local
, &remote
, cn
);
477 msg (M_INFO
| M_NOPREFIX
, "IFCONFIG_POOL TEST pass 1: l=%s r=%s cn=%s",
478 print_in_addr_t (local
, 0, &gc
),
479 print_in_addr_t (remote
, 0, &gc
),
485 msg (M_INFO
| M_NOPREFIX
, "************* 2");
486 for (i
= (int) SIZE (array
) / 16; i
< (int) SIZE (array
) / 8; ++i
)
488 msg (M_INFO
, "Attempt to release %d cn=%s", array
[i
], p
->list
[i
].common_name
);
489 if (!ifconfig_pool_release (p
, array
[i
]))
491 msg (M_INFO
, "Succeeded");
496 msg (M_INFO
| M_NOPREFIX
, "**************** 3");
497 for (i
= 0; i
< (int) SIZE (array
); ++i
)
500 ifconfig_pool_handle h
;
501 in_addr_t local
, remote
;
503 snprintf (buf
, sizeof(buf
), "common-name-%d", i
+24);
509 h
= ifconfig_pool_acquire (p
, &local
, &remote
, cn
);
512 msg (M_INFO
| M_NOPREFIX
, "IFCONFIG_POOL TEST pass 3: l=%s r=%s cn=%s",
513 print_in_addr_t (local
, 0, &gc
),
514 print_in_addr_t (remote
, 0, &gc
),
520 ifconfig_pool_free (p
);