]> git.saurik.com Git - apple/network_cmds.git/blame - rtsol.tproj/rtsock.c
network_cmds-307.1.1.tar.gz
[apple/network_cmds.git] / rtsol.tproj / rtsock.c
CommitLineData
7ba0088d
A
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
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*/
67static int rtsock_input_ifannounce __P((int, struct rt_msghdr *, char *));
68#endif
69
70static struct {
71 u_char type;
72 size_t minlen;
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 },
78#endif
b8dff150 79 { 0, 0UL, NULL },
7ba0088d
A
80};
81
82int
83rtsock_open()
84{
85
86 return socket(PF_ROUTE, SOCK_RAW, 0);
87}
88
89int
90rtsock_input(s)
91 int s;
92{
93 ssize_t n;
94 char msg[2048];
95 char *lim, *next;
96 struct rt_msghdr *rtm;
97 int idx;
98 size_t len;
99 int ret = 0;
100 const size_t lenlim =
101 offsetof(struct rt_msghdr, rtm_msglen) + sizeof(rtm->rtm_msglen);
102
103 n = read(s, msg, sizeof(msg));
104
105 lim = msg + n;
106 for (next = msg; next < lim; next += len) {
107 rtm = (struct rt_msghdr *)next;
108 if (lim - next < lenlim)
109 break;
110 len = rtm->rtm_msglen;
111 if (len < lenlim)
112 break;
113
114 if (dflag > 1) {
115 warnmsg(LOG_INFO, __FUNCTION__,
116 "rtmsg type %d, len=%lu", rtm->rtm_type,
117 (u_long)len);
118 }
119
120 for (idx = 0; rtsock_dispatch[idx].func; idx++) {
121 if (rtm->rtm_type != rtsock_dispatch[idx].type)
122 continue;
123 if (rtm->rtm_msglen < rtsock_dispatch[idx].minlen) {
124 warnmsg(LOG_INFO, __FUNCTION__,
125 "rtmsg type %d too short!", rtm->rtm_type);
126 continue;
127 }
128
129 ret = (*rtsock_dispatch[idx].func)(s, rtm, lim);
130 break;
131 }
132 }
133
134 return ret;
135}
136
137#ifdef RTM_IFANNOUNCE /*NetBSD 1.5 or later*/
138static int
139rtsock_input_ifannounce(s, rtm, lim)
140 int s;
141 struct rt_msghdr *rtm;
142 char *lim;
143{
144 struct if_announcemsghdr *ifan;
145 struct ifinfo *ifinfo;
146
147 ifan = (struct if_announcemsghdr *)rtm;
148 if ((char *)(ifan + 1) > lim)
149 return -1;
150
151 switch (ifan->ifan_what) {
152 case IFAN_ARRIVAL:
153 /*
154 * XXX for NetBSD 1.5, interface index will monotonically be
155 * increased as new pcmcia card gets inserted.
156 * we may be able to do a name-based interface match,
157 * and call ifreconfig() to enable the interface again.
158 */
159 warnmsg(LOG_INFO, __FUNCTION__,
160 "interface %s inserted", ifan->ifan_name);
161 break;
162 case IFAN_DEPARTURE:
163 warnmsg(LOG_WARNING, __FUNCTION__,
164 "interface %s removed", ifan->ifan_name);
165 ifinfo = find_ifinfo(ifan->ifan_index);
166 if (ifinfo) {
167 if (dflag > 1) {
168 warnmsg(LOG_INFO, __FUNCTION__,
169 "bring interface %s to DOWN state",
170 ifan->ifan_name);
171 }
172 ifinfo->state = IFS_DOWN;
173 }
174 break;
175 }
176
177 return 0;
178}
179#endif