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