1 /* $KAME: rtsock.c,v 1.3 2000/10/10 08:46:45 itojun Exp $ */
2 /* $FreeBSD: src/usr.sbin/rtsold/rtsock.c,v 1.1.2.1 2001/07/03 11:02:16 ume Exp $ */
5 * Copyright (C) 2000 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <sys/socket.h>
37 #include <sys/queue.h>
40 #include <net/route.h>
41 #include <net/if_dl.h>
43 #include <netinet/in.h>
44 #include <netinet/ip6.h>
45 #include <netinet/icmp6.h>
58 #define ROUNDUP(a, size) \
59 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
61 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
63 ((ap)->sa_len ? ROUNDUP((ap)->sa_len, sizeof(u_long)) \
66 #ifdef RTM_IFANNOUNCE /*NetBSD 1.5 or later*/
67 static int rtsock_input_ifannounce __P((int, struct rt_msghdr *, char *));
73 int (*func) __P((int, struct rt_msghdr *, char *));
74 } rtsock_dispatch[] = {
75 #ifdef RTM_IFANNOUNCE /*NetBSD 1.5 or later*/
76 { RTM_IFANNOUNCE, sizeof(struct if_announcemsghdr),
77 rtsock_input_ifannounce },
86 return socket(PF_ROUTE, SOCK_RAW, 0);
96 struct rt_msghdr *rtm;
100 const size_t lenlim =
101 offsetof(struct rt_msghdr, rtm_msglen) + sizeof(rtm->rtm_msglen);
103 n = read(s, msg, sizeof(msg));
106 for (next = msg; next < lim; next += len) {
107 rtm = (struct rt_msghdr *)next;
108 if (lim - next < lenlim)
110 len = rtm->rtm_msglen;
115 warnmsg(LOG_INFO, __FUNCTION__,
116 "rtmsg type %d, len=%lu", rtm->rtm_type,
120 for (idx = 0; rtsock_dispatch[idx].func; idx++) {
121 if (rtm->rtm_type != rtsock_dispatch[idx].type)
123 if (rtm->rtm_msglen < rtsock_dispatch[idx].minlen) {
124 warnmsg(LOG_INFO, __FUNCTION__,
125 "rtmsg type %d too short!", rtm->rtm_type);
129 ret = (*rtsock_dispatch[idx].func)(s, rtm, lim);
137 #ifdef RTM_IFANNOUNCE /*NetBSD 1.5 or later*/
139 rtsock_input_ifannounce(s, rtm, lim)
141 struct rt_msghdr *rtm;
144 struct if_announcemsghdr *ifan;
145 struct ifinfo *ifinfo;
147 ifan = (struct if_announcemsghdr *)rtm;
148 if ((char *)(ifan + 1) > lim)
151 switch (ifan->ifan_what) {
154 * XXX for NetBSD 1.5, interface index will monotonically be
155 * increased as new pcmcia card gets inserted.
156 * we may be able to do a name-based interface match,
157 * and call ifreconfig() to enable the interface again.
159 warnmsg(LOG_INFO, __FUNCTION__,
160 "interface %s inserted", ifan->ifan_name);
163 warnmsg(LOG_WARNING, __FUNCTION__,
164 "interface %s removed", ifan->ifan_name);
165 ifinfo = find_ifinfo(ifan->ifan_index);
168 warnmsg(LOG_INFO, __FUNCTION__,
169 "bring interface %s to DOWN state",
172 ifinfo->state = IFS_DOWN;