File: interfaces.c

package info (click to toggle)
libreswan 5.2-2.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,644 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (94 lines) | stat: -rw-r--r-- 2,616 bytes parent folder | download | duplicates (2)
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
/* Libreswan interfaces management (interfaces.c)
 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
 * Copyright (C) 2012 Paul Wouters <paul@libreswan.org>
 * Copyright (C) 2013 D. Hugh Redelmeier <hugh@mimosa.com>
 *
 * 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 <https://www.gnu.org/licenses/gpl2.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.
 */

#include <sys/ioctl.h>
#include <net/if.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "lsw_socket.h"

#include "constants.h"
#include "ip_endpoint.h"
#include "ip_address.h"
#include "passert.h"
#include "ipsecconf/interfaces.h"
#include "lswlog.h"	/* for pexpect() */
#include "ip_info.h"
#include "ip_protocol.h"
#include "ip_sockaddr.h"

bool starter_iface_find(const char *iface, const struct ip_info *family,
			ip_address *dst, ip_address *nh)
{
	/* XXX: danger REQ is recycled by ioctl() calls */
	struct ifreq req;

	if (iface == NULL)
		return false;	/* ??? can this ever happen? */

	int sock = cloexec_socket(family->af, SOCK_DGRAM, 0);
	if (sock < 0)
		return false;

	fill_and_terminate(req.ifr_name, iface, IFNAMSIZ);

	/* UP? */
	if (ioctl(sock, SIOCGIFFLAGS, &req) != 0 ||
	    (req.ifr_flags & IFF_UP) == 0x0) {
		close(sock);
		return false;
	}

	/*
	 * convert the sockaddr to an endpoint (ADDRESS:PORT, but
	 * expect PORT==0)) and then extract just the address
	 */

	/* get NH */
	if (nh != NULL &&
	    (req.ifr_flags & IFF_POINTOPOINT) != 0x0 &&
	    (ioctl(sock, SIOCGIFDSTADDR, &req) == 0)) {
		if (req.ifr_addr.sa_family == family->af) {
			ip_address nh_address;
			ip_port nh_port;
			happy(sockaddr_to_address_port(&req.ifr_addr, family->sockaddr_size,
						       &nh_address, &nh_port));
			pexpect(hport(nh_port) == 0);
			*nh = nh_address;
		}
	}

	/* get DST */
	if (dst != NULL &&
	    ioctl(sock, SIOCGIFADDR, &req) == 0) {
		if (req.ifr_addr.sa_family == family->af) {
			ip_address dst_address;
			ip_port dst_port;
			happy(sockaddr_to_address_port(&req.ifr_addr, family->sockaddr_size,
						       &dst_address, &dst_port));
			pexpect(hport(dst_port) == 0);
			*dst = dst_address;
		}
	}

	close(sock);
	return true;
}