1/*	$KAME: rtsock.c,v 1.3 2000/10/10 08:46:45 itojun Exp $	*/
2
3/*-
4 * SPDX-License-Identifier: BSD-3-Clause
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/capsicum.h>
36#include <sys/queue.h>
37#include <sys/socket.h>
38#include <sys/time.h>
39#include <sys/uio.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 <capsicum_helpers.h>
50#include <time.h>
51#include <unistd.h>
52#include <stdio.h>
53#include <stddef.h>
54#include <err.h>
55#include <errno.h>
56#include <string.h>
57#include <stdlib.h>
58#include <syslog.h>
59#include "rtsold.h"
60
61static int rtsock_input_ifannounce(int, struct rt_msghdr *, char *);
62
63static struct {
64	u_char type;
65	size_t minlen;
66	int (*func)(int, struct rt_msghdr *, char *);
67} rtsock_dispatch[] = {
68	{ RTM_IFANNOUNCE, sizeof(struct if_announcemsghdr),
69	  rtsock_input_ifannounce },
70	{ 0, 0, NULL },
71};
72
73int
74rtsock_open(void)
75{
76	cap_rights_t rights;
77	int error, s;
78
79	s = socket(PF_ROUTE, SOCK_RAW, 0);
80	if (s < 0)
81		return (s);
82	cap_rights_init(&rights, CAP_EVENT, CAP_READ);
83	if (caph_rights_limit(s, &rights) != 0) {
84		error = errno;
85		(void)close(s);
86		errno = error;
87		return (-1);
88	}
89	return (s);
90}
91
92int
93rtsock_input(int s)
94{
95	ssize_t n;
96	char msg[2048];
97	char *lim, *next;
98	struct rt_msghdr *rtm;
99	int idx;
100	ssize_t len;
101	int ret = 0;
102	const ssize_t lenlim =
103	    offsetof(struct rt_msghdr, rtm_msglen) + sizeof(rtm->rtm_msglen);
104
105	n = read(s, msg, sizeof(msg));
106
107	lim = msg + n;
108	for (next = msg; next < lim; next += len) {
109		rtm = (struct rt_msghdr *)(void *)next;
110		if (lim - next < lenlim)
111			break;
112		len = rtm->rtm_msglen;
113		if (len < lenlim)
114			break;
115
116		if (dflag > 1) {
117			warnmsg(LOG_INFO, __func__,
118			    "rtmsg type %d, len=%lu", rtm->rtm_type,
119			    (u_long)len);
120		}
121
122		for (idx = 0; rtsock_dispatch[idx].func; idx++) {
123			if (rtm->rtm_type != rtsock_dispatch[idx].type)
124				continue;
125			if (rtm->rtm_msglen < rtsock_dispatch[idx].minlen) {
126				warnmsg(LOG_INFO, __func__,
127				    "rtmsg type %d too short!", rtm->rtm_type);
128				continue;
129			}
130
131			ret = (*rtsock_dispatch[idx].func)(s, rtm, lim);
132			break;
133		}
134	}
135
136	return (ret);
137}
138
139static int
140rtsock_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