]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.sbin/rtsold/rtsock.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.sbin / rtsold / rtsock.c
1 /*      $KAME: rtsock.c,v 1.3 2000/10/10 08:46:45 itojun Exp $  */
2 /*      $FreeBSD$       */
3
4 /*
5  * Copyright (C) 2000 WIDE Project.
6  * All rights reserved.
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  * 
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
30  * SUCH DAMAGE.
31  */
32
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/uio.h>
36 #include <sys/time.h>
37 #include <sys/queue.h>
38
39 #include <net/if.h>
40 #include <net/route.h>
41 #include <net/if_dl.h>
42
43 #include <netinet/in.h>
44 #include <netinet/ip6.h>
45 #include <netinet/icmp6.h>
46
47 #include <time.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 #include <stddef.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <string.h>
54 #include <stdlib.h>
55 #include <syslog.h>
56 #include "rtsold.h"
57
58 #define ROUNDUP(a, size) \
59         (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
60
61 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
62         ((caddr_t)(ap) + \
63          ((ap)->sa_len ? ROUNDUP((ap)->sa_len, sizeof(u_long)) \
64                        : sizeof(u_long)))
65
66 #ifdef RTM_IFANNOUNCE   /*NetBSD 1.5 or later*/
67 static int rtsock_input_ifannounce(int, struct rt_msghdr *, char *);
68 #endif
69
70 static struct {
71         u_char type;
72         size_t minlen;
73         int (*func)(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 },
78 #endif
79         { 0, 0, NULL },
80 };
81
82 int
83 rtsock_open(void)
84 {
85
86         return (socket(PF_ROUTE, SOCK_RAW, 0));
87 }
88
89 int
90 rtsock_input(int s)
91 {
92         ssize_t n;
93         char msg[2048];
94         char *lim, *next;
95         struct rt_msghdr *rtm;
96         int idx;
97         ssize_t len;
98         int ret = 0;
99         const ssize_t lenlim =
100             offsetof(struct rt_msghdr, rtm_msglen) + sizeof(rtm->rtm_msglen);
101
102         n = read(s, msg, sizeof(msg));
103
104         lim = msg + n;
105         for (next = msg; next < lim; next += len) {
106                 rtm = (struct rt_msghdr *)(void *)next;
107                 if (lim - next < lenlim)
108                         break;
109                 len = rtm->rtm_msglen;
110                 if (len < lenlim)
111                         break;
112
113                 if (dflag > 1) {
114                         warnmsg(LOG_INFO, __func__,
115                             "rtmsg type %d, len=%lu", rtm->rtm_type,
116                             (u_long)len);
117                 }
118
119                 for (idx = 0; rtsock_dispatch[idx].func; idx++) {
120                         if (rtm->rtm_type != rtsock_dispatch[idx].type)
121                                 continue;
122                         if (rtm->rtm_msglen < rtsock_dispatch[idx].minlen) {
123                                 warnmsg(LOG_INFO, __func__,
124                                     "rtmsg type %d too short!", rtm->rtm_type);
125                                 continue;
126                         }
127
128                         ret = (*rtsock_dispatch[idx].func)(s, rtm, lim);
129                         break;
130                 }
131         }
132
133         return (ret);
134 }
135
136 #ifdef RTM_IFANNOUNCE   /*NetBSD 1.5 or later*/
137 static int
138 rtsock_input_ifannounce(int s __unused, struct rt_msghdr *rtm, char *lim)
139 {
140         struct if_announcemsghdr *ifan;
141         struct ifinfo *ifi;
142
143         ifan = (struct if_announcemsghdr *)rtm;
144         if ((char *)(ifan + 1) > lim)
145                 return (-1);
146
147         switch (ifan->ifan_what) {
148         case IFAN_ARRIVAL:
149                 /*
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.
154                  */
155                 warnmsg(LOG_INFO, __func__,
156                     "interface %s inserted", ifan->ifan_name);
157                 break;
158         case IFAN_DEPARTURE:
159                 warnmsg(LOG_WARNING, __func__,
160                     "interface %s removed", ifan->ifan_name);
161                 ifi = find_ifinfo(ifan->ifan_index);
162                 if (ifi) {
163                         if (dflag > 1) {
164                                 warnmsg(LOG_INFO, __func__,
165                                     "bring interface %s to DOWN state",
166                                     ifan->ifan_name);
167                         }
168                         ifi->state = IFS_DOWN;
169                 }
170                 break;
171         }
172
173         return (0);
174 }
175 #endif