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
/*
 * Copyright (c) 2001-2002
 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
 *	All rights reserved.
 * Copyright (c) 2003-2004
 *	Hartmut Brandt.
 *	All rights reserved.
 *
 * Author: Hartmut Brandt <harti@freebsd.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 AUTHOR 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 AUTHOR 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.
 *
 * $Begemot: libunimsg/snmp_atm/snmp_atm.c,v 1.3 2005/05/23 11:46:46 brandt_h Exp $
 *
 * SNMP module for ATM hardware interfaces.
 */

#include "atm.h"
#include "atm_tree.h"
#include "atm_oid.h"

#include <sys/ioctl.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <net/if_types.h>
#include <net/if_media.h>
#include <net/if_atm.h>

struct lmodule *module;

/* list of all (known) ATM interfaces */
struct atmif_list atmif_list = TAILQ_HEAD_INITIALIZER(atmif_list);

/* whether we are started or not */
static int started;

/* last time table was changed */
static uint64_t last_change;

/* for the registration */
static const struct asn_oid oid_begemotAtm = OIDX_begemotAtm;

/* the registration */
static u_int reg_atm;

/*
 * Find an ATM interface by name
 */
struct atmif *
atm_find_if_name(const char *name)
{
	struct atmif_priv *aif;

	TAILQ_FOREACH(aif, &atmif_list, link)
		if (strcmp(aif->pub.ifp->name, name) == 0)
			return (&aif->pub);
	return (NULL);
}

/*
 * get the interface from the interface index
 */
struct atmif *
atm_find_if(u_int ifindex)
{
	struct atmif_priv *aif;

	TAILQ_FOREACH(aif, &atmif_list, link)
		if (aif->index == ifindex)
			return (&aif->pub);
	return (NULL);
}

/*
 * Send notification to all listeners.
 */
void
atmif_send_notification(struct atmif_priv *aif, enum atmif_notify code,
    uintptr_t arg)
{
	struct atmif_reg *r0, *r1;

	r0 = TAILQ_FIRST(&aif->notify);
	while (r0 != NULL) {
		r1 = TAILQ_NEXT(r0, link);
		r0->func(&aif->pub, code, arg, r0->data);
		r0 = r1;
	}
}

/*
 * Destroy an interface
 */
static void
atmif_destroy(struct atmif_priv *aif)
{
	struct atmif_reg *r0;

	atmif_send_notification(aif, ATMIF_NOTIFY_DESTROY,
	    (uintptr_t)0);

	atmif_sys_destroy(aif);

	if (aif->ifpreg != NULL)
		mibif_unnotify(aif->ifpreg);

	while ((r0 = TAILQ_FIRST(&aif->notify)) != NULL) {
		TAILQ_REMOVE(&aif->notify, r0, link);
		free(r0);
	}

	TAILQ_REMOVE(&atmif_list, aif, link);
	free(aif);

	last_change = this_tick;
}

/*
 * Function gets called from the MIB-II module for events on that interface
 */
static void
atmif_notify(struct mibif *ifp __unused, enum mibif_notify event, void *data)
{
	struct atmif_priv *aif = data;

	switch (event) {

	  case MIBIF_NOTIFY_DESTROY:
		atmif_destroy(aif);
		break;
	}
}

/*
 * Check the carrier state of the interface
 */
void
atmif_check_carrier(struct atmif_priv *aif)
{
	struct ifmediareq ifmr;
	enum atmif_carrier_state ost = aif->pub.carrier;

	memset(&ifmr, 0, sizeof(ifmr));
	strcpy(ifmr.ifm_name, aif->pub.ifp->name);

	if (ioctl(mib_netsock, SIOCGIFMEDIA, &ifmr) == -1) {
		aif->pub.carrier = ATMIF_CARRIER_UNKNOWN;
		return;
	}
	if (!(ifmr.ifm_status & IFM_AVALID)) {
		aif->pub.carrier = ATMIF_CARRIER_UNKNOWN;
		return;
	}
	if (ifmr.ifm_status & IFM_ACTIVE)
		aif->pub.carrier = ATMIF_CARRIER_ON;
	else
		aif->pub.carrier = ATMIF_CARRIER_OFF;

	if (ost != aif->pub.carrier)
		atmif_send_notification(aif, ATMIF_NOTIFY_CARRIER,
		    (uintptr_t)ost);
}

/*
 * Retrieve the SUNI mode
 */
static int
atmif_get_mode(struct atmif_priv *aif)
{
	struct ifmediareq ifmr;

	memset(&ifmr, 0, sizeof(ifmr));
	strcpy(ifmr.ifm_name, aif->pub.ifp->name);

	if (ioctl(mib_netsock, SIOCGIFMEDIA, &ifmr) < 0) {
		syslog(LOG_ERR, "SIOCGIFMEDIA: %m");
		aif->pub.mode = ATMIF_SUNI_MODE_UNKNOWN;
		return (SNMP_ERR_GENERR);
	}
	if (ifmr.ifm_current & IFM_ATM_SDH)
		aif->pub.mode = ATMIF_SUNI_MODE_SDH;
	else
		aif->pub.mode = ATMIF_SUNI_MODE_SONET;

	return (SNMP_ERR_NOERROR);
}

/*
 * Change the SUNI mod
 */
static int
atmif_set_mode(struct atmif_priv *aif, int newmode)
{
	struct ifmediareq ifmr;
	struct ifreq ifr;

	memset(&ifmr, 0, sizeof(ifmr));
	strcpy(ifmr.ifm_name, aif->pub.ifp->name);

	/* get current mode */
	if (ioctl(mib_netsock, SIOCGIFMEDIA, &ifmr) < 0) {
		syslog(LOG_ERR, "SIOCGIFMEDIA: %m");
		return (SNMP_ERR_GENERR);
	}

	memset(&ifr, 0, sizeof(ifr));
	strcpy(ifr.ifr_name, aif->pub.ifp->name);

	ifr.ifr_media = ifmr.ifm_current;
	if (newmode == ATMIF_SUNI_MODE_SDH)
		ifr.ifr_media |= IFM_ATM_SDH;
	else
		ifr.ifr_media &= ~IFM_ATM_SDH;

	if (ioctl(mib_netsock, SIOCSIFMEDIA, &ifr) < 0) {
		syslog(LOG_ERR, "SIOCSIFMEDIA: %m");
		return (SNMP_ERR_GENERR);
	}

	aif->pub.mode = newmode;
	return (SNMP_ERR_NOERROR);
}

/*
 * Attach to an ATM interface
 */
static void
attach_if(struct mibif *ifp)
{
	struct atmif_priv *aif;

	/* we should not know it */
	TAILQ_FOREACH(aif, &atmif_list, link)
		if (aif->pub.ifp == ifp) {
			syslog(LOG_CRIT, "new ATM if already known '%s'",
			    ifp->name);
			return;
		}

	/*
	 * tap it
	 */
	if ((aif = malloc(sizeof(*aif))) == NULL) {
		syslog(LOG_ERR, "new atmif: %m");
		return;
	}
	memset(aif, 0, sizeof(*aif));

	aif->pub.ifp = ifp;
	aif->index = ifp->index;
	TAILQ_INIT(&aif->notify);

	if (atmif_sys_attach_if(aif)) {
		free(aif);
		return;
	}

	aif->ifpreg = mibif_notify(ifp, module, atmif_notify, aif);

	aif->pub.carrier = ATMIF_CARRIER_UNKNOWN;
	atmif_check_carrier(aif);
	(void)atmif_get_mode(aif);

	INSERT_OBJECT_INT(aif, &atmif_list);

	last_change = this_tick;

	return;
}

/*
 * Function gets called when a new interface is created. If this is an
 * ATM interface, hook in. Claim the interface in any case even when
 * the creation of our data structures fails.
 */
static int
new_if(struct mibif *ifp)
{
	if (!started || ifp->mib.ifmd_data.ifi_type != IFT_ATM ||
	    ifp->xnotify != NULL)
		return (0);

	attach_if(ifp);
	return (1);
}

/*
 * Start the module
 */
static void
atm_start(void)
{
	struct mibif *ifp;

	reg_atm = or_register(&oid_begemotAtm, 
	    "The Begemot MIB for ATM interfaces.", module);

	started = 1;
	for (ifp = mib_first_if(); ifp != NULL; ifp = mib_next_if(ifp))
		if (ifp->mib.ifmd_data.ifi_type == IFT_ATM &&
		    ifp->xnotify == NULL)
			attach_if(ifp);
}

/*
 * Called when modules is loaded
 */
static int
atm_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)
{
	module = mod;

	/* register to get creation messages for ATM interfaces */
	if (mib_register_newif(new_if, module)) {
		syslog(LOG_ERR, "cannot register newif function: %m");
		return (-1);
	}

	return (0);
}

/*
 * Called when module gets unloaded - free all resources
 */
static int
atm_fini(void)
{
	struct atmif_priv *aif;

	while ((aif = TAILQ_FIRST(&atmif_list)) != NULL)
		atmif_destroy(aif);

	mib_unregister_newif(module);
	or_unregister(reg_atm);

	return (0);
}

/*
 * Other module unloaded/loaded
 */
static void
atm_loading(const struct lmodule *mod, int loading)
{
	struct atmif_priv *aif;
	struct atmif_reg *r0, *r1;

	if (!loading) {
		/* remove notifications for this module */
		TAILQ_FOREACH(aif, &atmif_list, link)
			TAILQ_FOREACH_SAFE(r0, &aif->notify, link, r1) {
				if (r0->mod == mod) {
					TAILQ_REMOVE(&aif->notify, r0, link);
					free(r0);
				}
			}
	}
}

const struct snmp_module config = {
	.comment = "This module implements a private MIB for ATM interfaces.",
	.init =		atm_init,
	.fini =		atm_fini,
	.start =	atm_start,
	.tree =		atm_ctree,
	.tree_size =	atm_CTREE_SIZE,
	.loading =	atm_loading
};

/*
 * Get the interface point for a table access
 */
int
atmif_get_aif(struct snmp_value *value, u_int sub, enum snmp_op op,
    struct atmif_priv **aifp)
{
	switch (op) {

	  case SNMP_OP_GETNEXT:
		if ((*aifp = NEXT_OBJECT_INT(&atmif_list,
		    &value->var, sub)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		value->var.len = sub + 1;
		value->var.subs[sub] = (*aifp)->index;
		break;

	  case SNMP_OP_GET:
		if ((*aifp = FIND_OBJECT_INT(&atmif_list,
		    &value->var, sub)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		break;

	  case SNMP_OP_SET:
		if ((*aifp = FIND_OBJECT_INT(&atmif_list,
		    &value->var, sub)) == NULL)
			return (SNMP_ERR_NO_CREATION);
		break;

	  case SNMP_OP_ROLLBACK:
	  case SNMP_OP_COMMIT:
		if ((*aifp = FIND_OBJECT_INT(&atmif_list,
		    &value->var, sub)) == NULL)
			abort();
		return (SNMP_ERR_NOERROR);
	}

	if ((*aifp)->pub.mib->pcr == 0) {
		mib_fetch_ifmib((*aifp)->pub.ifp);
		atmif_sys_fill_mib(*aifp);
		atmif_check_carrier(*aifp);
	}

	return (SNMP_ERR_NOERROR);
}

/* 
 * Table of all ATM interfaces
 */
int
op_atmif(struct snmp_context *ctx __unused, struct snmp_value *value,
    u_int sub, u_int vindex __unused, enum snmp_op op)
{
	struct atmif_priv *aif;
	int err;

	if ((err = atmif_get_aif(value, sub, op, &aif)) != SNMP_ERR_NOERROR)
		return (err);

	if (op == SNMP_OP_SET) {
		switch (value->var.subs[sub - 1]) {

		  default:
			return (SNMP_ERR_NOT_WRITEABLE);

		  case LEAF_begemotAtmIfMode:
			if ((err = atmif_get_mode(aif)) != SNMP_ERR_NOERROR)
				return (err);
			if (aif->pub.mode == ATMIF_SUNI_MODE_UNKNOWN)
				return (SNMP_ERR_INCONS_VALUE);
			if (value->v.integer != ATMIF_SUNI_MODE_SONET &&
			    value->v.integer != ATMIF_SUNI_MODE_SDH)
				return (SNMP_ERR_WRONG_VALUE);
			if ((u_int)value->v.integer == aif->pub.mode)
				return (SNMP_ERR_NOERROR);
			return (atmif_set_mode(aif, value->v.integer));
		}
		abort();
	}

	switch (value->var.subs[sub - 1]) {

	  case LEAF_begemotAtmIfName:
		return (string_get(value, aif->pub.ifp->name, -1));

	  case LEAF_begemotAtmIfPcr:
		value->v.uint32 = aif->pub.mib->pcr;
		return (SNMP_ERR_NOERROR);

	  case LEAF_begemotAtmIfMedia:
		value->v.integer = aif->pub.mib->media;
		return (SNMP_ERR_NOERROR);

	  case LEAF_begemotAtmIfVpiBits:
		value->v.uint32 = aif->pub.mib->vpi_bits;
		return (SNMP_ERR_NOERROR);

	  case LEAF_begemotAtmIfVciBits:
		value->v.uint32 = aif->pub.mib->vci_bits;
		return (SNMP_ERR_NOERROR);

	  case LEAF_begemotAtmIfMaxVpcs:
		value->v.uint32 = aif->pub.mib->max_vpcs;
		return (SNMP_ERR_NOERROR);

	  case LEAF_begemotAtmIfMaxVccs:
		value->v.uint32 = aif->pub.mib->max_vccs;
		return (SNMP_ERR_NOERROR);

	  case LEAF_begemotAtmIfEsi:
		return (string_get(value, aif->pub.mib->esi, 6));

	  case LEAF_begemotAtmIfCarrierStatus:
		value->v.integer = aif->pub.carrier;
		return (SNMP_ERR_NOERROR);

	  case LEAF_begemotAtmIfMode:
		if ((err = atmif_get_mode(aif)) != SNMP_ERR_NOERROR)
			return (err);
		value->v.integer = aif->pub.mode;
		return (SNMP_ERR_NOERROR);
	}
	abort();
}

/* 
 * Hardware table
 */
int
op_atmhw(struct snmp_context *ctx __unused, struct snmp_value *value,
    u_int sub, u_int vindex __unused, enum snmp_op op)
{
	struct atmif_priv *aif;
	int err;

	if ((err = atmif_get_aif(value, sub, op, &aif)) != SNMP_ERR_NOERROR)
		return (err);
	if (op == SNMP_OP_SET)
		return (SNMP_ERR_NOT_WRITEABLE);

	switch (value->var.subs[sub - 1]) {

	  case LEAF_begemotAtmHWVendor:
		return (atm_sys_get_hw_vendor(aif, value));

	  case LEAF_begemotAtmHWDevice:
		return (atm_sys_get_hw_device(aif, value));

	  case LEAF_begemotAtmHWSerial:
		value->v.uint32 = aif->pub.mib->serial;
		return (SNMP_ERR_NOERROR);

	  case LEAF_begemotAtmHWVersion:
		value->v.uint32 = aif->pub.mib->hw_version;
		return (SNMP_ERR_NOERROR);

	  case LEAF_begemotAtmHWSoftVersion:
		value->v.uint32 = aif->pub.mib->sw_version;
		return (SNMP_ERR_NOERROR);

	}
	abort();
}

/*
 * Scalars
 */
int
op_atm(struct snmp_context *ctx __unused, struct snmp_value *value,
    u_int sub, u_int vindex __unused, enum snmp_op op)
{
	switch (op) {

	  case SNMP_OP_GETNEXT:
		abort();

	  case SNMP_OP_GET:
		switch (value->var.subs[sub - 1]) {

		  case LEAF_begemotAtmIfTableLastChange:
			value->v.uint32 =
			    (last_change == 0 ? 0 : last_change - start_tick);
			return (SNMP_ERR_NOERROR);
		}
		abort();

	  case SNMP_OP_SET:
		return (SNMP_ERR_NOT_WRITEABLE);

	  case SNMP_OP_ROLLBACK:
	  case SNMP_OP_COMMIT:
		abort();
	}
	abort();
}

/*
 * Register for interface notifications
 */
void *
atm_notify_aif(struct atmif *pub, const struct lmodule *mod,
    atmif_event_f func, void *arg)
{
	struct atmif_priv *aif = (struct atmif_priv *)pub;
	struct atmif_reg *r0;

	if ((r0 = malloc(sizeof(*r0))) == NULL) {
		syslog(LOG_CRIT, "out of memory");
		return (NULL);
	}
	r0->func = func;
	r0->mod = mod;
	r0->data = arg;
	r0->aif = aif;

	TAILQ_INSERT_TAIL(&aif->notify, r0, link);

	return (r0);
}

/*
 * Unregister it
 */
void
atm_unnotify_aif(void *arg)
{
	struct atmif_reg *r0 = arg;

	TAILQ_REMOVE(&r0->aif->notify, r0, link);
	free(r0);
}