1122394Sharti/*
2122394Sharti * Copyright (c) 2001-2003
3122394Sharti *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4122394Sharti *	All rights reserved.
5122394Sharti *
6122394Sharti * Author: Harti Brandt <harti@freebsd.org>
7310901Sngie *
8133211Sharti * Redistribution and use in source and binary forms, with or without
9133211Sharti * modification, are permitted provided that the following conditions
10133211Sharti * are met:
11133211Sharti * 1. Redistributions of source code must retain the above copyright
12133211Sharti *    notice, this list of conditions and the following disclaimer.
13122394Sharti * 2. Redistributions in binary form must reproduce the above copyright
14122394Sharti *    notice, this list of conditions and the following disclaimer in the
15122394Sharti *    documentation and/or other materials provided with the distribution.
16310901Sngie *
17133211Sharti * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18133211Sharti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19133211Sharti * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20133211Sharti * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21133211Sharti * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22133211Sharti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23133211Sharti * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24133211Sharti * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25133211Sharti * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26133211Sharti * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27133211Sharti * SUCH DAMAGE.
28122394Sharti *
29133211Sharti * $Begemot: bsnmp/snmp_mibII/mibII_nettomedia.c,v 1.8 2004/08/06 08:47:03 brandt Exp $
30122394Sharti *
31122394Sharti * Read-only implementation of the Arp table (ipNetToMediaTable)
32122394Sharti *
33122394Sharti * The problem with the table is, that we don't receive routing message
34122394Sharti * when a) an arp table entry is resolved and b) when an arp table entry is
35122394Sharti * deleted automatically. Therefor we need to poll the table from time to
36122394Sharti * time.
37122394Sharti */
38122394Sharti#include "mibII.h"
39122394Sharti#include "mibII_oid.h"
40122394Sharti
41122394Sharti#define ARPREFRESH	30
42122394Sharti
43122394Shartistruct mibarp *
44122394Shartimib_find_arp(const struct mibif *ifp, struct in_addr in)
45122394Sharti{
46122394Sharti	struct mibarp *at;
47133211Sharti	uint32_t a = ntohl(in.s_addr);
48122394Sharti
49122394Sharti	if (get_ticks() >= mibarpticks + ARPREFRESH)
50122394Sharti		mib_arp_update();
51122394Sharti
52122394Sharti	TAILQ_FOREACH(at, &mibarp_list, link)
53122394Sharti		if (at->index.subs[0] == ifp->index &&
54122394Sharti		    (at->index.subs[1] == ((a >> 24) & 0xff)) &&
55122394Sharti		    (at->index.subs[2] == ((a >> 16) & 0xff)) &&
56122394Sharti		    (at->index.subs[3] == ((a >>  8) & 0xff)) &&
57122394Sharti		    (at->index.subs[4] == ((a >>  0) & 0xff)))
58122394Sharti			return (at);
59122394Sharti	return (NULL);
60122394Sharti}
61122394Sharti
62122394Shartistruct mibarp *
63122394Shartimib_arp_create(const struct mibif *ifp, struct in_addr in, const u_char *phys,
64122394Sharti    size_t physlen)
65122394Sharti{
66122394Sharti	struct mibarp *at;
67133211Sharti	uint32_t a = ntohl(in.s_addr);
68122394Sharti
69122394Sharti	if ((at = malloc(sizeof(*at))) == NULL)
70122394Sharti		return (NULL);
71122394Sharti	at->flags = 0;
72122394Sharti
73122394Sharti	at->index.len = 5;
74122394Sharti	at->index.subs[0] = ifp->index;
75122394Sharti	at->index.subs[1] = (a >> 24) & 0xff;
76122394Sharti	at->index.subs[2] = (a >> 16) & 0xff;
77122394Sharti	at->index.subs[3] = (a >>  8) & 0xff;
78122394Sharti	at->index.subs[4] = (a >>  0) & 0xff;
79122394Sharti	if ((at->physlen = physlen) > sizeof(at->phys))
80122394Sharti		at->physlen = sizeof(at->phys);
81122394Sharti	memcpy(at->phys, phys, at->physlen);
82122394Sharti
83122394Sharti	INSERT_OBJECT_OID(at, &mibarp_list);
84122394Sharti
85122394Sharti	return (at);
86122394Sharti}
87122394Sharti
88122394Shartivoid
89122394Shartimib_arp_delete(struct mibarp *at)
90122394Sharti{
91122394Sharti	TAILQ_REMOVE(&mibarp_list, at, link);
92122394Sharti	free(at);
93122394Sharti}
94122394Sharti
95122394Shartiint
96122394Shartiop_nettomedia(struct snmp_context *ctx __unused, struct snmp_value *value,
97122394Sharti    u_int sub, u_int iidx __unused, enum snmp_op op)
98122394Sharti{
99122394Sharti	struct mibarp *at;
100122394Sharti
101122394Sharti	at = NULL;	/* gcc */
102122394Sharti
103122394Sharti	if (get_ticks() >= mibarpticks + ARPREFRESH)
104122394Sharti		mib_arp_update();
105122394Sharti
106122394Sharti	switch (op) {
107122394Sharti
108122394Sharti	  case SNMP_OP_GETNEXT:
109122394Sharti		if ((at = NEXT_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL)
110122394Sharti			return (SNMP_ERR_NOSUCHNAME);
111122394Sharti		index_append(&value->var, sub, &at->index);
112122394Sharti		break;
113122394Sharti
114122394Sharti	  case SNMP_OP_GET:
115122394Sharti		if ((at = FIND_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL)
116122394Sharti			return (SNMP_ERR_NOSUCHNAME);
117122394Sharti		break;
118122394Sharti
119122394Sharti	  case SNMP_OP_SET:
120122394Sharti		if ((at = FIND_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL)
121122394Sharti			return (SNMP_ERR_NO_CREATION);
122122394Sharti		return (SNMP_ERR_NOT_WRITEABLE);
123122394Sharti
124122394Sharti	  case SNMP_OP_ROLLBACK:
125122394Sharti	  case SNMP_OP_COMMIT:
126122394Sharti		abort();
127122394Sharti	}
128122394Sharti
129122394Sharti	switch (value->var.subs[sub - 1]) {
130122394Sharti
131122394Sharti	  case LEAF_ipNetToMediaIfIndex:
132122394Sharti		value->v.integer = at->index.subs[0];
133122394Sharti		break;
134122394Sharti
135122394Sharti	  case LEAF_ipNetToMediaPhysAddress:
136122394Sharti		return (string_get(value, at->phys, at->physlen));
137122394Sharti
138122394Sharti	  case LEAF_ipNetToMediaNetAddress:
139122394Sharti		value->v.ipaddress[0] = at->index.subs[1];
140122394Sharti		value->v.ipaddress[1] = at->index.subs[2];
141122394Sharti		value->v.ipaddress[2] = at->index.subs[3];
142122394Sharti		value->v.ipaddress[3] = at->index.subs[4];
143122394Sharti		break;
144122394Sharti
145122394Sharti	  case LEAF_ipNetToMediaType:
146122394Sharti		value->v.integer = (at->flags & MIBARP_PERM) ? 4 : 3;
147122394Sharti		break;
148122394Sharti	}
149122394Sharti	return (SNMP_ERR_NOERROR);
150122394Sharti}
151