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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/* $NetBSD: ldp_command.c,v 1.14 2013/08/02 07:29:56 kefren Exp $ */

/*-
 * Copyright (c) 2010 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Mihai Chelaru <kefren@NetBSD.org>
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 <arpa/inet.h>

#include <netinet/in.h>
#include <netinet/tcp.h>

#include <sys/socket.h>
#include <sys/queue.h>

#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "label.h"
#include "ldp.h"
#include "ldp_command.h"
#include "ldp_errors.h"
#include "ldp_peer.h"
#include "socketops.h"

struct com_sock csockets[MAX_COMMAND_SOCKETS];
extern int ldp_hello_time, ldp_keepalive_time, ldp_holddown_time,
	min_label, max_label, debug_f, warn_f;

#define	writestr(soc, str) write(soc, str, strlen(str))

#define	MAXSEND 1024
char sendspace[MAXSEND];

static void send_prompt(int);
static void send_pwd_prompt(int);
static int command_match(struct com_func*, int, char*, char*);

static int	verify_root_pwd(char *);
static void	echo_on(int s);
static void	echo_off(int s);

/* Main functions */
static int show_func(int, char *);
static int set_func(int, char *);
static int exit_func(int, char *);
 
/* Show functions */
static int show_debug(int, char *);
static int show_hellos(int, char *);
static int show_parameters(int, char *);
static int show_version(int, char *);
static int show_warning(int, char *);

/* Set functions */
static int set_hello_time(int, char *);
static int set_debug(int, char *);
static int set_warning(int, char *);

static struct com_func main_commands[] = {
	{ "show", show_func },
	{ "set", set_func },
	{ "quit", exit_func },
	{ "exit", exit_func },
	{ "", NULL }
};

static struct com_func show_commands[] = {
	{ "neighbours", show_neighbours },
	{ "bindings", show_bindings },
	{ "debug", show_debug },
	{ "hellos", show_hellos },
	{ "labels", show_labels },
	{ "parameters", show_parameters },
	{ "version", show_version },
	{ "warning", show_warning },
	{ "", NULL }
};

struct com_func set_commands[] = {
	{ "debug", set_debug },
	{ "hello-time", set_hello_time },
	{ "warning", set_warning },
	{ "", NULL }
};

static int
verify_root_pwd(char *pw)
{
	struct passwd *p;

	if ((p = getpwuid(0)) == NULL)
		return 0;

	if (strcmp(crypt(pw, p->pw_passwd), p->pw_passwd))
		return 0;

	return 1;
}


void
init_command_sockets()
{
	int i;

	for (i = 0; i<MAX_COMMAND_SOCKETS; i++) {
		csockets[i].socket = -1;
		csockets[i].auth = 0;
	}
}

int
create_command_socket(int port)
{
	struct sockaddr_in sin;
	int s;

	sin.sin_len = sizeof(sin);
	sin.sin_family = AF_INET;
	sin.sin_port = htons(port);
	sin.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);

	s = socket(PF_INET, SOCK_STREAM, 6);
	if (s < 0)
		return s;

	if (bind(s, (struct sockaddr *) &sin, sizeof(sin))) {
		fatalp("bind: %s", strerror(errno));
		close(s);
		return -1;
	}

	if (listen(s, 5) == -1) {
		fatalp("listen: %s", strerror(errno));
		close(s);
		return -1;
	}
	debugp("Command socket created (%d)\n", s);
	return s;
}

void
command_accept(int s)
{
	int as = accept(s, NULL, 0);

	if (as < 0) {
		fatalp("Cannot accept new command socket %s",
		    strerror(errno));
		return;
	}

	if (add_command_socket(as) != 0) {
		fatalp("Cannot accept command. Too many connections\n");
		close(as);
		return;
	}

	/* auth */
	send_pwd_prompt(as);
}

struct com_sock *
is_command_socket(int s)
{
	int i;

	if (s == -1)
		return NULL;
	for (i=0; i<MAX_COMMAND_SOCKETS; i++)
		if (s == csockets[i].socket)
			return &csockets[i];
	return NULL;
}

int
add_command_socket(int s)
{
	int i;

	for (i=0; i<MAX_COMMAND_SOCKETS; i++)
		if (csockets[i].socket == -1) {
			csockets[i].socket = s;
			csockets[i].auth = 0;
			return 0;
		}
	return -1;
}

void
command_dispatch(struct com_sock *cs)
{
	char recvspace[MAX_COMMAND_SIZE + 1];
	char *nextc = recvspace;
	int r = recv(cs->socket, recvspace, MAX_COMMAND_SIZE, MSG_PEEK);

	if (r < 0) {
		command_close(cs->socket);
		return;
	}

	recv(cs->socket, recvspace, r, MSG_WAITALL);

	if (r < 3) { /*at least \r\n */
	    if (cs->auth) {
		/*writestr(cs->socket, "Unknown command. Use ? for help\n");*/
		send_prompt(cs->socket);
	    } else {
		writestr(cs->socket, "Bad password\n");
		command_close(cs->socket);
	    }
		return;
	}

	recvspace[r - 2] = '\0';

	if (!cs->auth) {
		if (verify_root_pwd(recvspace)) {
			echo_on(cs->socket);
			cs->auth = 1;
			writestr(cs->socket, "\n");
			send_prompt(cs->socket);
		} else {
			echo_on(cs->socket);
			writestr(cs->socket, "Bad password\n");
			command_close(cs->socket);
		}
		return;
	}

	strsep(&nextc, " ");

	command_match(main_commands, cs->socket, recvspace, nextc);

}

void
command_close(int s)
{
	int i;

	for (i=0; i<MAX_COMMAND_SOCKETS; i++)
		if (s == csockets[i].socket) {
			close(s);
			csockets[i].socket = -1;
			csockets[i].auth = 0;
			break;
		}
}

static void
send_prompt(int s) {
	writestr(s, "LDP> ");
}

static void
send_pwd_prompt(int s) {
	echo_off(s);
	writestr(s, "Password: ");
}

static void
echo_off(int s)
{
	char iac_will_echo[3] = { 0xff, 0xfb, 0x01 }, bf[32];
	write(s, iac_will_echo, sizeof(iac_will_echo));
	read(s, bf, sizeof(bf));
}

static void
echo_on(int s)
{
	char iac_wont_echo[3] = { 0xff, 0xfc, 0x01 }, bf[32];
	write(s, iac_wont_echo, sizeof(iac_wont_echo));
	read(s, bf, sizeof(bf));
}

/*
 * Matching function
 * Returns 1 if matched anything
 */
static int
command_match(struct com_func *cf, int s, char *orig, char *next)
{
	size_t i, len;
	int last_match = -1;
	const char *msg = NULL;

	if (orig == NULL || orig[0] == '\0')
		goto out;

	if (!strcmp(orig, "?")) {
		for (i = 0; cf[i].func != NULL; i++) {
			snprintf(sendspace, MAXSEND, "\t%s\n", cf[i].com);
			writestr(s, sendspace);
		}
		goto out;
	}

	len = strlen(orig);
	for (i = 0; cf[i].func != NULL; i++) {
		if (strncasecmp(orig, cf[i].com, len) == 0) {
			if (last_match != -1) {
				msg = "Ambiguous";
				goto out;
			} else
				last_match = i;
		}
	}

	if (last_match == -1) {
		msg = "Unknown";
		goto out;
	}

	if (cf[last_match].func(s, next) != 0)
		send_prompt(s);
	return 1;
out:
	if (msg) {
		writestr(s, msg);
		writestr(s, " command. Use ? for help\n");
	}
	send_prompt(s);
	return 0;
}

/*
 * Main CLI functions
 */
static int
set_func(int s, char *recvspace)
{
	char *nextc = recvspace;

	if (recvspace == NULL || recvspace[0] == '\0') {
		writestr(s, "Unknown set command. Use set ? for help\n");
		return 1;
	}

	strsep(&nextc, " ");

	command_match(set_commands, s, recvspace, nextc);
	return 0;
}

static int
show_func(int s, char *recvspace)
{
	char *nextc = recvspace;

	if (recvspace == NULL || recvspace[0] == '\0') {
		writestr(s, "Unknown show command. Use show ? for help\n");
		return 1;
	}

	strsep(&nextc, " ");

	command_match(show_commands, s, recvspace, nextc);
	return 0;
}

static int
exit_func(int s, char *recvspace)
{
	command_close(s);
	return 0;
}

/*
 * Show functions
 */
int
show_neighbours(int s, char *recvspace)
{
	struct ldp_peer *p;
	struct ldp_peer_address *wp;
	struct sockaddr_in ssin;
	socklen_t sin_len = sizeof(struct sockaddr_in);
	int enc;
	socklen_t enclen = sizeof(enc);

	SLIST_FOREACH(p, &ldp_peer_head, peers) {
		snprintf(sendspace, MAXSEND, "LDP peer: %s\n",
		    inet_ntoa(p->ldp_id));
		writestr(s, sendspace);
		snprintf(sendspace, MAXSEND, "Transport address: %s\n",
		    satos(p->transport_address));
		writestr(s, sendspace);
		snprintf(sendspace, MAXSEND, "Next-hop address: %s\n",
		    satos(p->address));
		writestr(s, sendspace);
		snprintf(sendspace, MAXSEND, "State: %s\n",
		    ldp_state_to_name(p->state));
		writestr(s, sendspace);
		if (p->state == LDP_PEER_ESTABLISHED) {
			snprintf(sendspace, MAXSEND, "Since: %s",
			    ctime(&p->established_t));
			writestr(s, sendspace);
		}
		snprintf(sendspace, MAXSEND, "Holdtime: %d\nTimeout: %d\n",
			p->holdtime, p->timeout);
		writestr(s, sendspace);

		switch(p->state) {
		    case LDP_PEER_CONNECTING:
		    case LDP_PEER_CONNECTED:
		    case LDP_PEER_ESTABLISHED:
			if (getsockname(p->socket,(struct sockaddr *) &ssin,
			    &sin_len))
				break;

			if (getsockopt(p->socket, IPPROTO_TCP, TCP_MD5SIG,
			    &enc, &enclen) == 0) {
				snprintf(sendspace, MAXSEND,
				    "Authenticated: %s\n",
				    enc != 0 ? "YES" : "NO");
				writestr(s, sendspace);
			}

			snprintf(sendspace, MAXSEND,"Socket: %d\nLocal %s:%d\n",
			    p->socket, inet_ntoa(ssin.sin_addr),
			    ntohs(ssin.sin_port));
			writestr(s, sendspace);

			if (getpeername(p->socket,(struct sockaddr *) &ssin,
			    &sin_len))
				break;
			snprintf(sendspace, MAXSEND, "Remote %s:%d\n",
				inet_ntoa(ssin.sin_addr), ntohs(ssin.sin_port));
			writestr(s, sendspace);
		}

		snprintf(sendspace, MAXSEND,"Addresses bounded to this peer: ");
		writestr(s, sendspace);
		SLIST_FOREACH(wp, &p->ldp_peer_address_head, addresses) {
			/* XXX: TODO */
			if (wp->address.sa.sa_family != AF_INET)
				continue;
			snprintf(sendspace, MAXSEND, "%s ",
			    inet_ntoa(wp->address.sin.sin_addr));
			writestr(s, sendspace);
		}
		sendspace[0] = sendspace[1] = '\n';
		write(s, sendspace, 2);
	}       
	return 1;
}

/* Shows labels grabbed from unsolicited label maps */
int
show_labels(int s, char *recvspace)
{
	struct ldp_peer *p;
	struct label_mapping *lm = NULL;

	SLIST_FOREACH(p, &ldp_peer_head, peers) {
		if (p->state != LDP_PEER_ESTABLISHED)
			continue;
		while ((lm = ldp_peer_lm_right(p, lm)) != NULL) {
			char lma[256];
			/* XXX: TODO */
			if (lm->address.sa.sa_family != AF_INET)
				continue;
			strlcpy(lma, inet_ntoa(lm->address.sin.sin_addr),
			    sizeof(lma));
			snprintf(sendspace, MAXSEND, "%s:%d\t%s/%d\n",
			    inet_ntoa(p->ldp_id), lm->label, lma, lm->prefix);
			writestr(s, sendspace);
		}
	}
	return 1;
}

int
show_bindings(int s, char *recvspace)
{
	struct label *l = NULL;

	snprintf(sendspace, MAXSEND, "Local label\tNetwork\t\t\t\tNexthop\n");
	writestr(s, sendspace);
	while((l = label_get_right(l)) != NULL) {
		snprintf(sendspace, MAXSEND, "%d\t\t%s/", l->binding,
		    satos(&l->so_dest.sa));
		writestr(s, sendspace);
		snprintf(sendspace, MAXSEND, "%s", satos(&l->so_pref.sa));
		writestr(s, sendspace);
		if (l->p)
			snprintf(sendspace, MAXSEND, "\t%s:%d\n",
			    satos(l->p->address), l->label);
		else
			snprintf(sendspace, MAXSEND, "\n");
		writestr(s, sendspace);
	}
	return 1;
}

static int
show_debug(int s, char *recvspace)
{
	if (recvspace) {
		writestr(s, "Invalid command\n");
		return 1;
	}

	snprintf(sendspace, MAXSEND, "Debug: %s\n",
		debug_f ? "YES" : "NO");
	writestr(s, sendspace);
	return 1;
}

static int
show_hellos(int s, char *recvspace)
{
	struct hello_info *hi;

	SLIST_FOREACH(hi, &hello_info_head, infos) {
		snprintf(sendspace, MAXSEND,
		    "ID: %s\nKeepalive: %ds\nTransport address: %s\n\n",
		    inet_ntoa(hi->ldp_id),
		    hi->keepalive,
		    hi->transport_address.sa.sa_family != 0 ?
		    satos(&hi->transport_address.sa) : "None");
		writestr(s, sendspace);
	}
	return 1;
}

static int
show_parameters(int s, char *recvspace)
{
	snprintf(sendspace, MAXSEND, "LDP ID: %s\nProtocol version: %d\n"
		 "Hello time: %d\nKeepalive time: %d\nHoldtime: %d\n"
		 "Minimum label: %d\nMaximum label: %d\n",
		my_ldp_id,
		LDP_VERSION,
		ldp_hello_time,
		ldp_keepalive_time,
		ldp_holddown_time,
		min_label,
		max_label);
	writestr(s, sendspace);
	return 1;
}

static int
show_version(int s, char *recvspace)
{
	if (recvspace) {	/* Nothing more after this */
		writestr(s, "Invalid command\n");
		return 1;
	}

	snprintf(sendspace, MAXSEND, "NetBSD LDP daemon version: %s\n",
	    LDPD_VER);
	writestr(s, sendspace);
	return 1;
}

static int
show_warning(int s, char *recvspace)
{
	if (recvspace) {
		writestr(s, "Invalid command\n");
		return 1;
	}

	snprintf(sendspace, MAXSEND, "Warnings: %s\n",
		warn_f ? "YES" : "NO");
	writestr(s, sendspace);
	return 1;
}

/* Set commands */
static int
set_hello_time(int s, char *recvspace)
{
	if (!recvspace || atoi(recvspace) < 1) {
		writestr(s, "Invalid timeout\n");
		return 1;
	}

	ldp_hello_time = atoi(recvspace);
	return 1;
}

static int
set_debug(int s, char *recvspace)
{
	if (!recvspace || atoi(recvspace) < 0) {
		writestr(s, "Invalid command\n");
		return 1;
	}

	debug_f = atoi(recvspace);
	return 1;
}

static int
set_warning(int s, char *recvspace)
{
	if (!recvspace || atoi(recvspace) < 0) {
		writestr(s, "Invalid command\n");
		return 1;
	}

	warn_f = atoi(recvspace);
	return 1;
}