]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/rtsold/rtsock.c
Update ACPICA to 20181003.
[FreeBSD/FreeBSD.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  * 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/socket.h>
37 #include <sys/uio.h>
38 #include <sys/time.h>
39 #include <sys/queue.h>
40
41 #include <net/if.h>
42 #include <net/route.h>
43 #include <net/if_dl.h>
44
45 #include <netinet/in.h>
46 #include <netinet/ip6.h>
47 #include <netinet/icmp6.h>
48
49 #include <time.h>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <stddef.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <string.h>
56 #include <stdlib.h>
57 #include <syslog.h>
58 #include "rtsold.h"
59
60 #define ROUNDUP(a, size) \
61         (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
62
63 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
64         ((caddr_t)(ap) + \
65          ((ap)->sa_len ? ROUNDUP((ap)->sa_len, sizeof(u_long)) \
66                        : sizeof(u_long)))
67
68 #ifdef RTM_IFANNOUNCE   /*NetBSD 1.5 or later*/
69 static int rtsock_input_ifannounce(int, struct rt_msghdr *, char *);
70 #endif
71
72 static struct {
73         u_char type;
74         size_t minlen;
75         int (*func)(int, struct rt_msghdr *, char *);
76 } rtsock_dispatch[] = {
77 #ifdef RTM_IFANNOUNCE   /*NetBSD 1.5 or later*/
78         { RTM_IFANNOUNCE, sizeof(struct if_announcemsghdr),
79           rtsock_input_ifannounce },
80 #endif
81         { 0, 0, NULL },
82 };
83
84 int
85 rtsock_open(void)
86 {
87
88         return (socket(PF_ROUTE, SOCK_RAW, 0));
89 }
90
91 int
92 rtsock_input(int s)
93 {
94         ssize_t n;
95         char msg[2048];
96         char *lim, *next;
97         struct rt_msghdr *rtm;
98         int idx;
99         ssize_t len;
100         int ret = 0;
101         const ssize_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 *)(void *)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, __func__,
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, __func__,
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(int s __unused, struct rt_msghdr *rtm, char *lim)
141 {
142         struct if_announcemsghdr *ifan;
143         struct ifinfo *ifi;
144
145         ifan = (struct if_announcemsghdr *)rtm;
146         if ((char *)(ifan + 1) > lim)
147                 return (-1);
148
149         switch (ifan->ifan_what) {
150         case IFAN_ARRIVAL:
151                 /*
152                  * XXX for NetBSD 1.5, interface index will monotonically be
153                  * increased as new pcmcia card gets inserted.
154                  * we may be able to do a name-based interface match,
155                  * and call ifreconfig() to enable the interface again.
156                  */
157                 warnmsg(LOG_INFO, __func__,
158                     "interface %s inserted", ifan->ifan_name);
159                 break;
160         case IFAN_DEPARTURE:
161                 warnmsg(LOG_WARNING, __func__,
162                     "interface %s removed", ifan->ifan_name);
163                 ifi = find_ifinfo(ifan->ifan_index);
164                 if (ifi) {
165                         if (dflag > 1) {
166                                 warnmsg(LOG_INFO, __func__,
167                                     "bring interface %s to DOWN state",
168                                     ifan->ifan_name);
169                         }
170                         ifi->state = IFS_DOWN;
171                 }
172                 break;
173         }
174
175         return (0);
176 }
177 #endif