-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers.c
142 lines (111 loc) · 2.67 KB
/
helpers.c
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
135
136
137
138
139
140
141
142
/*
* Authors:
* Alexander Aring <alex.aring@gmail.com>
*
* This software is Copyright 2019 by the above mentioned author(s),
* All Rights Reserved.
*
* The license which is distributed with this software in the file COPYRIGHT
* applies to this software. If your distribution is missing this file, you
* may request it from <alex.aring@gmail.com>.
*/
#define _GNU_SOURCE
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include "helpers.h"
#include "config.h"
#include "log.h"
int gen_stateless_addr(const struct in6_prefix *prefix,
const struct iface_llinfo *llinfo,
struct in6_addr *dst)
{
uint8_t len = bits_to_bytes(prefix->len);
int i;
memset(dst, 0, sizeof(*dst));
/* TODO only supported right now, gets tricky with bluetooth */
if (prefix->len != 64 || llinfo->addr_len != 8)
return -1;
memcpy(dst, &prefix->prefix, len);
for (i = 0; i < llinfo->addr_len; i++)
dst->s6_addr[len + i] = llinfo->addr[i];
/* U/L */
dst->s6_addr[8] ^= 0x02;
return 0;
}
__attribute__((format(printf, 1, 2))) static char *strdupf(char const *format, ...)
{
va_list va;
va_start(va, format);
char *strp = 0;
int rc = vasprintf(&strp, format, va);
if (rc == -1 || !strp) {
flog(LOG_ERR, "vasprintf failed: %s", strerror(errno));
exit(-1);
}
va_end(va);
return strp;
}
/* note: also called from the root context */
int set_var(const char *var, uint32_t val)
{
int retval = -1;
FILE *fp = 0;
if (access(var, F_OK) != 0)
goto cleanup;
fp = fopen(var, "w");
if (!fp) {
flog(LOG_ERR, "failed to set %s: %s", var, strerror(errno));
goto cleanup;
}
if (0 > fprintf(fp, "%u", val)) {
goto cleanup;
}
retval = 0;
cleanup:
if (fp)
fclose(fp);
return retval;
}
int set_interface_var(const char *iface, const char *var, const char *name, uint32_t val)
{
int retval = -1;
FILE *fp = 0;
char *spath = strdupf(var, iface);
/* No path traversal */
if (!iface[0] || !strcmp(iface, ".") || !strcmp(iface, "..") || strchr(iface, '/'))
goto cleanup;
if (access(spath, F_OK) != 0)
goto cleanup;
fp = fopen(spath, "w");
if (!fp) {
if (name)
flog(LOG_ERR, "failed to set %s (%u) for %s: %s", name, val, iface, strerror(errno));
goto cleanup;
}
if (0 > fprintf(fp, "%u", val)) {
goto cleanup;
}
retval = 0;
cleanup:
if (fp)
fclose(fp);
free(spath);
return retval;
}
void init_random_gen()
{
srandom(time(NULL));
}
int gen_random_private_ula_pfx(struct in6_prefix *prefix)
{
int i;
prefix->len = 64;
memset(&prefix->prefix, 0, sizeof(prefix->prefix));
prefix->prefix.s6_addr[0] = 0xfd;
for (i = 1; i < 6; i++)
prefix->prefix.s6_addr[i] = random() & 0xff;
return 0;
}