1 /* $KAME: rtsock.c,v 1.3 2000/10/10 08:46:45 itojun Exp $ */ 2 /* $FreeBSD$ */ 3 4 /*- 5 * SPDX-License-Identifier: BSD-3-Clause 6 * 7 * Copyright (C) 2000 WIDE Project. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the project nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/capsicum.h> 37 #include <sys/queue.h> 38 #include <sys/socket.h> 39 #include <sys/time.h> 40 #include <sys/uio.h> 41 42 #include <net/if.h> 43 #include <net/route.h> 44 #include <net/if_dl.h> 45 46 #include <netinet/in.h> 47 #include <netinet/ip6.h> 48 #include <netinet/icmp6.h> 49 50 #include <capsicum_helpers.h> 51 #include <time.h> 52 #include <unistd.h> 53 #include <stdio.h> 54 #include <stddef.h> 55 #include <err.h> 56 #include <errno.h> 57 #include <string.h> 58 #include <stdlib.h> 59 #include <syslog.h> 60 #include "rtsold.h" 61 62 static int rtsock_input_ifannounce(int, struct rt_msghdr *, char *); 63 64 static struct { 65 u_char type; 66 size_t minlen; 67 int (*func)(int, struct rt_msghdr *, char *); 68 } rtsock_dispatch[] = { 69 { RTM_IFANNOUNCE, sizeof(struct if_announcemsghdr), 70 rtsock_input_ifannounce }, 71 { 0, 0, NULL }, 72 }; 73 74 int 75 rtsock_open(void) 76 { 77 cap_rights_t rights; 78 int error, s; 79 80 s = socket(PF_ROUTE, SOCK_RAW, 0); 81 if (s < 0) 82 return (s); 83 cap_rights_init(&rights, CAP_EVENT, CAP_READ); 84 if (caph_rights_limit(s, &rights) != 0) { 85 error = errno; 86 (void)close(s); 87 errno = errno; 88 return (-1); 89 } 90 return (s); 91 } 92 93 int 94 rtsock_input(int s) 95 { 96 ssize_t n; 97 char msg[2048]; 98 char *lim, *next; 99 struct rt_msghdr *rtm; 100 int idx; 101 ssize_t len; 102 int ret = 0; 103 const ssize_t lenlim = 104 offsetof(struct rt_msghdr, rtm_msglen) + sizeof(rtm->rtm_msglen); 105 106 n = read(s, msg, sizeof(msg)); 107 108 lim = msg + n; 109 for (next = msg; next < lim; next += len) { 110 rtm = (struct rt_msghdr *)(void *)next; 111 if (lim - next < lenlim) 112 break; 113 len = rtm->rtm_msglen; 114 if (len < lenlim) 115 break; 116 117 if (dflag > 1) { 118 warnmsg(LOG_INFO, __func__, 119 "rtmsg type %d, len=%lu", rtm->rtm_type, 120 (u_long)len); 121 } 122 123 for (idx = 0; rtsock_dispatch[idx].func; idx++) { 124 if (rtm->rtm_type != rtsock_dispatch[idx].type) 125 continue; 126 if (rtm->rtm_msglen < rtsock_dispatch[idx].minlen) { 127 warnmsg(LOG_INFO, __func__, 128 "rtmsg type %d too short!", rtm->rtm_type); 129 continue; 130 } 131 132 ret = (*rtsock_dispatch[idx].func)(s, rtm, lim); 133 break; 134 } 135 } 136 137 return (ret); 138 } 139 140 static int 141 rtsock_input_ifannounce(int s __unused, struct rt_msghdr *rtm, char *lim) 142 { 143 struct if_announcemsghdr *ifan; 144 struct ifinfo *ifi; 145 146 ifan = (struct if_announcemsghdr *)rtm; 147 if ((char *)(ifan + 1) > lim) 148 return (-1); 149 150 switch (ifan->ifan_what) { 151 case IFAN_ARRIVAL: 152 /* 153 * XXX for NetBSD 1.5, interface index will monotonically be 154 * increased as new pcmcia card gets inserted. 155 * we may be able to do a name-based interface match, 156 * and call ifreconfig() to enable the interface again. 157 */ 158 warnmsg(LOG_INFO, __func__, 159 "interface %s inserted", ifan->ifan_name); 160 break; 161 case IFAN_DEPARTURE: 162 warnmsg(LOG_WARNING, __func__, 163 "interface %s removed", ifan->ifan_name); 164 ifi = find_ifinfo(ifan->ifan_index); 165 if (ifi) { 166 if (dflag > 1) { 167 warnmsg(LOG_INFO, __func__, 168 "bring interface %s to DOWN state", 169 ifan->ifan_name); 170 } 171 ifi->state = IFS_DOWN; 172 } 173 break; 174 } 175 176 return (0); 177 } 178