1f06ca4afSHartmut Brandt /* 2f06ca4afSHartmut Brandt * Copyright (c) 2001-2003 3f06ca4afSHartmut Brandt * Fraunhofer Institute for Open Communication Systems (FhG Fokus). 4f06ca4afSHartmut Brandt * All rights reserved. 5f06ca4afSHartmut Brandt * 6f06ca4afSHartmut Brandt * Author: Harti Brandt <harti@freebsd.org> 7f06ca4afSHartmut Brandt * 8896052c1SHartmut Brandt * Redistribution and use in source and binary forms, with or without 9896052c1SHartmut Brandt * modification, are permitted provided that the following conditions 10896052c1SHartmut Brandt * are met: 11896052c1SHartmut Brandt * 1. Redistributions of source code must retain the above copyright 12896052c1SHartmut Brandt * notice, this list of conditions and the following disclaimer. 13f06ca4afSHartmut Brandt * 2. Redistributions in binary form must reproduce the above copyright 14f06ca4afSHartmut Brandt * notice, this list of conditions and the following disclaimer in the 15f06ca4afSHartmut Brandt * documentation and/or other materials provided with the distribution. 16f06ca4afSHartmut Brandt * 17896052c1SHartmut Brandt * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18896052c1SHartmut Brandt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19896052c1SHartmut Brandt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20896052c1SHartmut Brandt * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 21896052c1SHartmut Brandt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22896052c1SHartmut Brandt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23896052c1SHartmut Brandt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24896052c1SHartmut Brandt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25896052c1SHartmut Brandt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26896052c1SHartmut Brandt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27896052c1SHartmut Brandt * SUCH DAMAGE. 28f06ca4afSHartmut Brandt * 29896052c1SHartmut Brandt * $Begemot: bsnmp/snmp_mibII/mibII_nettomedia.c,v 1.8 2004/08/06 08:47:03 brandt Exp $ 30f06ca4afSHartmut Brandt * 31f06ca4afSHartmut Brandt * Read-only implementation of the Arp table (ipNetToMediaTable) 32f06ca4afSHartmut Brandt * 33f06ca4afSHartmut Brandt * The problem with the table is, that we don't receive routing message 34f06ca4afSHartmut Brandt * when a) an arp table entry is resolved and b) when an arp table entry is 35f06ca4afSHartmut Brandt * deleted automatically. Therefor we need to poll the table from time to 36f06ca4afSHartmut Brandt * time. 37f06ca4afSHartmut Brandt */ 38f06ca4afSHartmut Brandt #include "mibII.h" 39f06ca4afSHartmut Brandt #include "mibII_oid.h" 40f06ca4afSHartmut Brandt 41f06ca4afSHartmut Brandt #define ARPREFRESH 30 42f06ca4afSHartmut Brandt 43f06ca4afSHartmut Brandt struct mibarp * 44f06ca4afSHartmut Brandt mib_find_arp(const struct mibif *ifp, struct in_addr in) 45f06ca4afSHartmut Brandt { 46f06ca4afSHartmut Brandt struct mibarp *at; 47896052c1SHartmut Brandt uint32_t a = ntohl(in.s_addr); 48f06ca4afSHartmut Brandt 49f06ca4afSHartmut Brandt if (get_ticks() >= mibarpticks + ARPREFRESH) 50f06ca4afSHartmut Brandt mib_arp_update(); 51f06ca4afSHartmut Brandt 52f06ca4afSHartmut Brandt TAILQ_FOREACH(at, &mibarp_list, link) 53f06ca4afSHartmut Brandt if (at->index.subs[0] == ifp->index && 54f06ca4afSHartmut Brandt (at->index.subs[1] == ((a >> 24) & 0xff)) && 55f06ca4afSHartmut Brandt (at->index.subs[2] == ((a >> 16) & 0xff)) && 56f06ca4afSHartmut Brandt (at->index.subs[3] == ((a >> 8) & 0xff)) && 57f06ca4afSHartmut Brandt (at->index.subs[4] == ((a >> 0) & 0xff))) 58f06ca4afSHartmut Brandt return (at); 59f06ca4afSHartmut Brandt return (NULL); 60f06ca4afSHartmut Brandt } 61f06ca4afSHartmut Brandt 62f06ca4afSHartmut Brandt struct mibarp * 63f06ca4afSHartmut Brandt mib_arp_create(const struct mibif *ifp, struct in_addr in, const u_char *phys, 64f06ca4afSHartmut Brandt size_t physlen) 65f06ca4afSHartmut Brandt { 66f06ca4afSHartmut Brandt struct mibarp *at; 67896052c1SHartmut Brandt uint32_t a = ntohl(in.s_addr); 68f06ca4afSHartmut Brandt 69f06ca4afSHartmut Brandt if ((at = malloc(sizeof(*at))) == NULL) 70f06ca4afSHartmut Brandt return (NULL); 71f06ca4afSHartmut Brandt at->flags = 0; 72f06ca4afSHartmut Brandt 73f06ca4afSHartmut Brandt at->index.len = 5; 74f06ca4afSHartmut Brandt at->index.subs[0] = ifp->index; 75f06ca4afSHartmut Brandt at->index.subs[1] = (a >> 24) & 0xff; 76f06ca4afSHartmut Brandt at->index.subs[2] = (a >> 16) & 0xff; 77f06ca4afSHartmut Brandt at->index.subs[3] = (a >> 8) & 0xff; 78f06ca4afSHartmut Brandt at->index.subs[4] = (a >> 0) & 0xff; 79f06ca4afSHartmut Brandt if ((at->physlen = physlen) > sizeof(at->phys)) 80f06ca4afSHartmut Brandt at->physlen = sizeof(at->phys); 81f06ca4afSHartmut Brandt memcpy(at->phys, phys, at->physlen); 82f06ca4afSHartmut Brandt 83f06ca4afSHartmut Brandt INSERT_OBJECT_OID(at, &mibarp_list); 84f06ca4afSHartmut Brandt 85f06ca4afSHartmut Brandt return (at); 86f06ca4afSHartmut Brandt } 87f06ca4afSHartmut Brandt 88f06ca4afSHartmut Brandt void 89f06ca4afSHartmut Brandt mib_arp_delete(struct mibarp *at) 90f06ca4afSHartmut Brandt { 91f06ca4afSHartmut Brandt TAILQ_REMOVE(&mibarp_list, at, link); 92f06ca4afSHartmut Brandt free(at); 93f06ca4afSHartmut Brandt } 94f06ca4afSHartmut Brandt 95f06ca4afSHartmut Brandt int 96f06ca4afSHartmut Brandt op_nettomedia(struct snmp_context *ctx __unused, struct snmp_value *value, 97f06ca4afSHartmut Brandt u_int sub, u_int iidx __unused, enum snmp_op op) 98f06ca4afSHartmut Brandt { 99f06ca4afSHartmut Brandt struct mibarp *at; 100f06ca4afSHartmut Brandt 101f06ca4afSHartmut Brandt at = NULL; /* gcc */ 102f06ca4afSHartmut Brandt 103f06ca4afSHartmut Brandt if (get_ticks() >= mibarpticks + ARPREFRESH) 104f06ca4afSHartmut Brandt mib_arp_update(); 105f06ca4afSHartmut Brandt 106f06ca4afSHartmut Brandt switch (op) { 107f06ca4afSHartmut Brandt 108f06ca4afSHartmut Brandt case SNMP_OP_GETNEXT: 109f06ca4afSHartmut Brandt if ((at = NEXT_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL) 110f06ca4afSHartmut Brandt return (SNMP_ERR_NOSUCHNAME); 111f06ca4afSHartmut Brandt index_append(&value->var, sub, &at->index); 112f06ca4afSHartmut Brandt break; 113f06ca4afSHartmut Brandt 114f06ca4afSHartmut Brandt case SNMP_OP_GET: 115f06ca4afSHartmut Brandt if ((at = FIND_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL) 116f06ca4afSHartmut Brandt return (SNMP_ERR_NOSUCHNAME); 117f06ca4afSHartmut Brandt break; 118f06ca4afSHartmut Brandt 119f06ca4afSHartmut Brandt case SNMP_OP_SET: 120f06ca4afSHartmut Brandt if ((at = FIND_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL) 121f06ca4afSHartmut Brandt return (SNMP_ERR_NO_CREATION); 122f06ca4afSHartmut Brandt return (SNMP_ERR_NOT_WRITEABLE); 123f06ca4afSHartmut Brandt 124f06ca4afSHartmut Brandt case SNMP_OP_ROLLBACK: 125f06ca4afSHartmut Brandt case SNMP_OP_COMMIT: 126f06ca4afSHartmut Brandt abort(); 127f06ca4afSHartmut Brandt } 128f06ca4afSHartmut Brandt 129f06ca4afSHartmut Brandt switch (value->var.subs[sub - 1]) { 130f06ca4afSHartmut Brandt 131f06ca4afSHartmut Brandt case LEAF_ipNetToMediaIfIndex: 132f06ca4afSHartmut Brandt value->v.integer = at->index.subs[0]; 133f06ca4afSHartmut Brandt break; 134f06ca4afSHartmut Brandt 135f06ca4afSHartmut Brandt case LEAF_ipNetToMediaPhysAddress: 136f06ca4afSHartmut Brandt return (string_get(value, at->phys, at->physlen)); 137f06ca4afSHartmut Brandt 138f06ca4afSHartmut Brandt case LEAF_ipNetToMediaNetAddress: 139f06ca4afSHartmut Brandt value->v.ipaddress[0] = at->index.subs[1]; 140f06ca4afSHartmut Brandt value->v.ipaddress[1] = at->index.subs[2]; 141f06ca4afSHartmut Brandt value->v.ipaddress[2] = at->index.subs[3]; 142f06ca4afSHartmut Brandt value->v.ipaddress[3] = at->index.subs[4]; 143f06ca4afSHartmut Brandt break; 144f06ca4afSHartmut Brandt 145f06ca4afSHartmut Brandt case LEAF_ipNetToMediaType: 146f06ca4afSHartmut Brandt value->v.integer = (at->flags & MIBARP_PERM) ? 4 : 3; 147f06ca4afSHartmut Brandt break; 148f06ca4afSHartmut Brandt } 149f06ca4afSHartmut Brandt return (SNMP_ERR_NOERROR); 150f06ca4afSHartmut Brandt } 151