1 /* $NetBSD: rtsock.c,v 1.5 2004/01/03 01:40:32 itojun Exp $ */
2 /* $KAME: rtsock.c,v 1.4 2001/09/19 06:59:41 sakane 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);
95 struct rt_msghdr
*rtm
;
100 offsetof(struct rt_msghdr
, rtm_msglen
) + sizeof(rtm
->rtm_msglen
);
102 n
= read(s
, msg
, sizeof(msg
));
105 for (next
= msg
; next
< lim
; next
+= len
) {
106 rtm
= (struct rt_msghdr
*)next
;
107 if (lim
- next
< (intptr_t)lenlim
)
109 len
= rtm
->rtm_msglen
;
114 warnmsg(LOG_INFO
, __func__
,
115 "rtmsg type %d, len=%lu", rtm
->rtm_type
,
119 for (idx
= 0; rtsock_dispatch
[idx
].func
; idx
++) {
120 if (rtm
->rtm_type
!= rtsock_dispatch
[idx
].type
)
122 if (rtm
->rtm_msglen
< rtsock_dispatch
[idx
].minlen
) {
123 warnmsg(LOG_INFO
, __func__
,
124 "rtmsg type %d too short!", rtm
->rtm_type
);
128 ret
= (*rtsock_dispatch
[idx
].func
)(s
, rtm
, lim
);
136 #ifdef RTM_IFANNOUNCE /*NetBSD 1.5 or later*/
138 rtsock_input_ifannounce(int s
, struct rt_msghdr
*rtm
, char *lim
)
140 struct if_announcemsghdr
*ifan
;
141 struct ifinfo
*ifinfo
;
143 ifan
= (struct if_announcemsghdr
*)rtm
;
144 if ((char *)(ifan
+ 1) > lim
)
147 switch (ifan
->ifan_what
) {
150 * XXX for NetBSD 1.5, interface index will monotonically be
151 * increased as new pcmcia card gets inserted.
152 * we may be able to do a name-based interface match,
153 * and call ifreconfig() to enable the interface again.
155 warnmsg(LOG_INFO
, __func__
,
156 "interface %s inserted", ifan
->ifan_name
);
159 warnmsg(LOG_WARNING
, __func__
,
160 "interface %s removed", ifan
->ifan_name
);
161 ifinfo
= find_ifinfo(ifan
->ifan_index
);
164 warnmsg(LOG_INFO
, __func__
,
165 "bring interface %s to DOWN state",
168 ifinfo
->state
= IFS_DOWN
;