OpenVPN
lladdr.c
Go to the documentation of this file.
1/*
2 * Support routine for configuring link layer address
3 */
4
5#ifdef HAVE_CONFIG_H
6#include "config.h"
7#endif
8
9#include "syshead.h"
10#include "error.h"
11#include "misc.h"
12#include "run_command.h"
13#include "lladdr.h"
14#include "proto.h"
15
16int
17set_lladdr(openvpn_net_ctx_t *ctx, const char *ifname, const char *lladdr,
18 const struct env_set *es)
19{
20 int r;
21
22 if (!ifname || !lladdr)
23 {
24 return -1;
25 }
26
27#if defined(TARGET_LINUX)
28 uint8_t addr[OPENVPN_ETH_ALEN];
29
30 sscanf(lladdr, MAC_FMT, MAC_SCAN_ARG(addr));
31 r = (net_addr_ll_set(ctx, ifname, addr) == 0);
32#else /* if defined(TARGET_LINUX) */
33 struct argv argv = argv_new();
34#if defined(TARGET_SOLARIS)
36 "%s %s ether %s",
37 IFCONFIG_PATH,
38 ifname, lladdr);
39#elif defined(TARGET_OPENBSD)
41 "%s %s lladdr %s",
42 IFCONFIG_PATH,
43 ifname, lladdr);
44#elif defined(TARGET_DARWIN)
46 "%s %s lladdr %s",
47 IFCONFIG_PATH,
48 ifname, lladdr);
49#elif defined(TARGET_FREEBSD)
51 "%s %s ether %s",
52 IFCONFIG_PATH,
53 ifname, lladdr);
54#else /* if defined(TARGET_SOLARIS) */
55 msg(M_WARN, "Sorry, but I don't know how to configure link layer addresses on this operating system.");
56 return -1;
57#endif /* if defined(TARGET_SOLARIS) */
59 r = openvpn_execve_check(&argv, es, M_WARN, "ERROR: Unable to set link layer address.");
61#endif /* if defined(TARGET_LINUX) */
62
63 if (r)
64 {
65 msg(M_INFO, "TUN/TAP link layer address set to %s", lladdr);
66 }
67
68 return r;
69}
void argv_msg(const int msglev, const struct argv *a)
Write the arguments stored in a struct argv via the msg() command.
Definition argv.c:243
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
Definition argv.c:102
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
Definition argv.c:440
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition argv.c:88
#define M_INFO
Definition errlevel.h:55
int set_lladdr(openvpn_net_ctx_t *ctx, const char *ifname, const char *lladdr, const struct env_set *es)
Definition lladdr.c:17
#define MAC_SCAN_ARG(_mac)
Definition misc.h:229
#define MAC_FMT
Definition misc.h:225
void * openvpn_net_ctx_t
Definition networking.h:39
#define msg(flags,...)
Definition error.h:144
#define M_WARN
Definition error.h:91
#define OPENVPN_ETH_ALEN
Definition proto.h:53
int openvpn_execve_check(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *error_message)
Definition argv.h:35
struct env_set * es