Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*	$NetBSD: af_inet6.c,v 1.38 2016/10/01 15:10:58 roy Exp $	*/

/*
 * Copyright (c) 1983, 1993
 *      The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: af_inet6.c,v 1.38 2016/10/01 15:10:58 roy Exp $");
#endif /* not lint */

#include <sys/param.h> 
#include <sys/ioctl.h> 
#include <sys/socket.h>

#include <net/if.h> 
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet6/nd6.h>

#include <err.h>
#include <errno.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <util.h>

#include "env.h"
#include "extern.h"
#include "parse.h"
#include "extern.h"
#include "af_inetany.h"
#include "prog_ops.h"

static void in6_constructor(void) __attribute__((constructor));
static void in6_alias(struct ifaddrs *, prop_dictionary_t, prop_dictionary_t);
static void in6_commit_address(prop_dictionary_t, prop_dictionary_t);

static int setia6eui64_impl(prop_dictionary_t, struct in6_aliasreq *);
static int setia6flags_impl(prop_dictionary_t, struct in6_aliasreq *);
static int setia6pltime_impl(prop_dictionary_t, struct in6_aliasreq *);
static int setia6vltime_impl(prop_dictionary_t, struct in6_aliasreq *);

static int setia6lifetime(prop_dictionary_t, int64_t, time_t *, uint32_t *);

static void in6_status(prop_dictionary_t, prop_dictionary_t, bool);
static bool in6_addr_tentative(struct ifaddrs *ifa);
static bool in6_addr_tentative_or_detached(struct ifaddrs *ifa);

static struct usage_func usage;
static cmdloop_branch_t branch[2];

static const struct kwinst ia6flagskw[] = {
	  IFKW("anycast",	IN6_IFF_ANYCAST)
	, IFKW("deprecated",	IN6_IFF_DEPRECATED)
};

static struct pinteger parse_pltime = PINTEGER_INITIALIZER(&parse_pltime,
    "pltime", 0, NULL, "pltime", &command_root.pb_parser);

static struct pinteger parse_vltime = PINTEGER_INITIALIZER(&parse_vltime,
    "vltime", 0, NULL, "vltime", &command_root.pb_parser);

static const struct kwinst inet6kw[] = {
	  {.k_word = "pltime", .k_nextparser = &parse_pltime.pi_parser}
	, {.k_word = "vltime", .k_nextparser = &parse_vltime.pi_parser}
	, {.k_word = "eui64", .k_key = "eui64", .k_type = KW_T_BOOL,
	   .k_bool = true, .k_nextparser = &command_root.pb_parser}
};

struct pkw ia6flags = PKW_INITIALIZER(&ia6flags, "ia6flags", NULL,
    "ia6flag", ia6flagskw, __arraycount(ia6flagskw), &command_root.pb_parser);
struct pkw inet6 = PKW_INITIALIZER(&inet6, "IPv6 keywords", NULL,
    NULL, inet6kw, __arraycount(inet6kw), NULL);

static struct afswtch in6af = {
	.af_name = "inet6", .af_af = AF_INET6, .af_status = in6_status,
	.af_addr_commit = in6_commit_address,
	.af_addr_tentative = in6_addr_tentative,
	.af_addr_tentative_or_detached = in6_addr_tentative_or_detached
};

static int
prefix(void *val, int size)
{
	u_char *pname = (u_char *)val;
	int byte, bit, plen = 0;

	for (byte = 0; byte < size; byte++, plen += 8)
		if (pname[byte] != 0xff)
			break;
	if (byte == size)
		return (plen);
	for (bit = 7; bit != 0; bit--, plen++)
		if (!(pname[byte] & (1 << bit)))
			break;
	for (; bit != 0; bit--)
		if (pname[byte] & (1 << bit))
			return(0);
	byte++;
	for (; byte < size; byte++)
		if (pname[byte])
			return(0);
	return (plen);
}

int
setia6flags_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
{
	int64_t ia6flag;

	if (!prop_dictionary_get_int64(env, "ia6flag", &ia6flag)) {
		errno = ENOENT;
		return -1;
	}

	if (ia6flag < 0) {
		ia6flag = -ia6flag;
		ifra->ifra_flags &= ~ia6flag;
	} else
		ifra->ifra_flags |= ia6flag;
	return 0;
}

int
setia6pltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
{
	int64_t pltime;

	if (!prop_dictionary_get_int64(env, "pltime", &pltime)) {
		errno = ENOENT;
		return -1;
	}

	return setia6lifetime(env, pltime,
	    &ifra->ifra_lifetime.ia6t_preferred,
	    &ifra->ifra_lifetime.ia6t_pltime);
}

int
setia6vltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
{
	int64_t vltime;

	if (!prop_dictionary_get_int64(env, "vltime", &vltime)) {
		errno = ENOENT;
		return -1;
	}

	return setia6lifetime(env, vltime,
		&ifra->ifra_lifetime.ia6t_expire,
		&ifra->ifra_lifetime.ia6t_vltime);
}

static int
setia6lifetime(prop_dictionary_t env, int64_t val, time_t *timep,
    uint32_t *ivalp)
{
	time_t t;
	int af;

	if ((af = getaf(env)) == -1 || af != AF_INET6) {
		errx(EXIT_FAILURE,
		    "inet6 address lifetime not allowed for the AF");
	}

	t = time(NULL);
	*timep = t + val;
	*ivalp = val;
	return 0;
}

int
setia6eui64_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
{
	char buf[2][80];
	struct ifaddrs *ifap, *ifa;
	const struct sockaddr_in6 *sin6 = NULL;
	const struct in6_addr *lladdr = NULL;
	struct in6_addr *in6;
	const char *ifname;
	bool doit = false;
	int af;

	if (!prop_dictionary_get_bool(env, "eui64", &doit) || !doit) {
		errno = ENOENT;
		return -1;
	}

	if ((ifname = getifname(env)) == NULL)
		return -1;

	af = getaf(env);
	if (af != AF_INET6) {
		errx(EXIT_FAILURE,
		    "eui64 address modifier not allowed for the AF");
	}
 	in6 = &ifra->ifra_addr.sin6_addr;
	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) {
		union {
			struct sockaddr_in6 sin6;
			struct sockaddr sa;
		} any = {.sin6 = {.sin6_family = AF_INET6}};
		memcpy(&any.sin6.sin6_addr, &in6addr_any,
		    sizeof(any.sin6.sin6_addr));
		(void)sockaddr_snprintf(buf[0], sizeof(buf[0]), "%a%%S",
		    &any.sa);
		(void)sockaddr_snprintf(buf[1], sizeof(buf[1]), "%a%%S",
		    (const struct sockaddr *)&ifra->ifra_addr);
		errx(EXIT_FAILURE, "interface index is already filled, %s | %s",
		    buf[0], buf[1]);
	}
	if (getifaddrs(&ifap) != 0)
		err(EXIT_FAILURE, "getifaddrs");
	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
		if (ifa->ifa_addr->sa_family == AF_INET6 &&
		    strcmp(ifa->ifa_name, ifname) == 0) {
			sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
				lladdr = &sin6->sin6_addr;
				break;
			}
		}
	}
	if (lladdr == NULL)
		errx(EXIT_FAILURE, "could not determine link local address"); 

 	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);

	freeifaddrs(ifap);
	return 0;
}

/* XXX not really an alias */
void
in6_alias(struct ifaddrs *ifa, prop_dictionary_t env, prop_dictionary_t oenv)
{
	struct sockaddr_in6 *sin6;
	char hbuf[NI_MAXHOST];
	u_int32_t scopeid;
	const int niflag = Nflag ? 0 : NI_NUMERICHOST;
	char fbuf[1024];

	sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
	inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
	scopeid = sin6->sin6_scope_id;
	if (getnameinfo((const struct sockaddr *)sin6, sin6->sin6_len,
			hbuf, sizeof(hbuf), NULL, 0, niflag))
		strlcpy(hbuf, "", sizeof(hbuf));	/* some message? */
	printf("\tinet6 %s", hbuf);
	inet6_putscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);

	sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask;
	printf("/%d", prefix(&sin6->sin6_addr, sizeof(struct in6_addr)));

	if (ifa->ifa_flags & IFF_POINTOPOINT) {
		sin6 = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
		inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
		hbuf[0] = '\0';
		if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
				hbuf, sizeof(hbuf), NULL, 0, niflag))
			strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
		printf(" -> %s", hbuf);
	}

	(void)snprintb(fbuf, sizeof(fbuf), IN6_IFFBITS, ifa->ifa_addrflags);
	printf(" flags %s", fbuf);

	if (scopeid)
		printf(" scopeid 0x%x", scopeid);

	if (get_flag('L')) {
		int s;
		struct in6_ifreq ifr6;
		struct in6_addrlifetime *lifetime;

		if ((s = getsock(AF_INET6)) == -1) {
			if (errno == EAFNOSUPPORT)
				return;
			err(EXIT_FAILURE, "socket");
		}

		memset(&ifr6, 0, sizeof(ifr6));
		estrlcpy(ifr6.ifr_name, ifa->ifa_name, sizeof(ifr6.ifr_name));
		memcpy(&ifr6.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
		lifetime = &ifr6.ifr_ifru.ifru_lifetime;
		if (prog_ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
			if (errno != EADDRNOTAVAIL)
				warn("SIOCGIFALIFETIME_IN6");
		} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
			time_t t = time(NULL);
			printf(" pltime ");
			if (lifetime->ia6t_preferred) {
				printf("%lu",
				    (unsigned long)(lifetime->ia6t_preferred -
				        MIN(t, lifetime->ia6t_preferred)));
			} else
				printf("infty");

			printf(" vltime ");
			if (lifetime->ia6t_expire) {
				printf("%lu",
				    (unsigned long)(lifetime->ia6t_expire -
				        MIN(t, lifetime->ia6t_expire)));
			} else
				printf("infty");
		}
	}
}

static void
in6_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
{
	struct ifaddrs *ifap, *ifa;
	const char *ifname;
	bool printprefs = false;

	if ((ifname = getifname(env)) == NULL)
		err(EXIT_FAILURE, "%s: getifname", __func__);

	if (getifaddrs(&ifap) != 0)
		err(EXIT_FAILURE, "getifaddrs");
	printprefs = ifa_any_preferences(ifname, ifap, AF_INET6);
	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
		if (strcmp(ifname, ifa->ifa_name) != 0)
			continue;
		if (ifa->ifa_addr->sa_family != AF_INET6)
			continue;
		in6_alias(ifa, env, oenv);
		if (printprefs)
			ifa_print_preference(ifa->ifa_name, ifa->ifa_addr);
		printf("\n");
	}
	freeifaddrs(ifap);
}

static int
in6_pre_aifaddr(prop_dictionary_t env, const struct afparam *param)
{
	struct in6_aliasreq *ifra = param->req.buf;

	setia6eui64_impl(env, ifra);
	setia6vltime_impl(env, ifra);
	setia6pltime_impl(env, ifra);
	setia6flags_impl(env, ifra);
	inet6_putscopeid(&ifra->ifra_addr, INET6_IS_ADDR_LINKLOCAL);
	inet6_putscopeid(&ifra->ifra_dstaddr, INET6_IS_ADDR_LINKLOCAL);

	return 0;
}

static void
in6_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
{
	struct in6_ifreq in6_ifr = {
		.ifr_addr = {
			.sin6_family = AF_INET6,
			.sin6_len = sizeof(in6_ifr.ifr_addr),
			.sin6_addr = {
				.s6_addr =
				    {0xff, 0xff, 0xff, 0xff,
				     0xff, 0xff, 0xff, 0xff}
			}
		}
	};
	static struct sockaddr_in6 in6_defmask = {
		.sin6_family = AF_INET6,
		.sin6_len = sizeof(in6_defmask),
		.sin6_addr = {
			.s6_addr = {0xff, 0xff, 0xff, 0xff,
			            0xff, 0xff, 0xff, 0xff}
		}
	};

	struct in6_aliasreq in6_ifra = {
		.ifra_prefixmask = {
			.sin6_family = AF_INET6,
			.sin6_len = sizeof(in6_ifra.ifra_prefixmask),
			.sin6_addr = {
				.s6_addr =
				    {0xff, 0xff, 0xff, 0xff,
				     0xff, 0xff, 0xff, 0xff}}},
		.ifra_lifetime = {
			  .ia6t_pltime = ND6_INFINITE_LIFETIME
			, .ia6t_vltime = ND6_INFINITE_LIFETIME
		}
	};
	struct afparam in6param = {
		  .req = BUFPARAM(in6_ifra)
		, .dgreq = BUFPARAM(in6_ifr)
		, .name = {
			{.buf = in6_ifr.ifr_name,
			 .buflen = sizeof(in6_ifr.ifr_name)},
			{.buf = in6_ifra.ifra_name,
			 .buflen = sizeof(in6_ifra.ifra_name)}
		  }
		, .dgaddr = BUFPARAM(in6_ifr.ifr_addr)
		, .addr = BUFPARAM(in6_ifra.ifra_addr)
		, .dst = BUFPARAM(in6_ifra.ifra_dstaddr)
		, .brd = BUFPARAM(in6_ifra.ifra_broadaddr)
		, .mask = BUFPARAM(in6_ifra.ifra_prefixmask)
		, .aifaddr = IFADDR_PARAM(SIOCAIFADDR_IN6)
		, .difaddr = IFADDR_PARAM(SIOCDIFADDR_IN6)
		, .gifaddr = IFADDR_PARAM(SIOCGIFADDR_IN6)
		, .defmask = BUFPARAM(in6_defmask)
		, .pre_aifaddr = in6_pre_aifaddr
	};
	commit_address(env, oenv, &in6param);
}

static bool
in6_addr_flags(struct ifaddrs *ifa, int flags)
{
	int s;
	struct in6_ifreq ifr;

	if ((s = getsock(AF_INET6)) == -1)
		err(EXIT_FAILURE, "%s: getsock", __func__);
	memset(&ifr, 0, sizeof(ifr));
	estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
	ifr.ifr_addr = *(struct sockaddr_in6 *)ifa->ifa_addr;
	if (prog_ioctl(s, SIOCGIFAFLAG_IN6, &ifr) == -1)
		err(EXIT_FAILURE, "SIOCGIFAFLAG_IN6");
	return ifr.ifr_ifru.ifru_flags6 & flags ? true : false;
}

static bool
in6_addr_tentative(struct ifaddrs *ifa)
{

	return in6_addr_flags(ifa, IN6_IFF_TENTATIVE);
}

static bool
in6_addr_tentative_or_detached(struct ifaddrs *ifa)
{

	return in6_addr_flags(ifa, IN6_IFF_TENTATIVE | IN6_IFF_DETACHED);
}

static void
in6_usage(prop_dictionary_t env)
{
	fprintf(stderr,
	    "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
	    "\t[ pltime n ] [ vltime n ] "
	    "[ eui64 ]\n");
}

static void
in6_constructor(void)
{
	if (register_flag('L') != 0)
		err(EXIT_FAILURE, __func__);
	register_family(&in6af);
	usage_func_init(&usage, in6_usage);
	register_usage(&usage);
	cmdloop_branch_init(&branch[0], &ia6flags.pk_parser);
	cmdloop_branch_init(&branch[1], &inet6.pk_parser);
	register_cmdloop_branch(&branch[0]);
	register_cmdloop_branch(&branch[1]);
}