rsrr.c revision 1.2 1 1.2 thorpej /* $NetBSD: rsrr.c,v 1.2 1995/10/09 03:51:56 thorpej Exp $ */
2 1.2 thorpej
3 1.1 mycroft /*
4 1.1 mycroft * Copyright (c) 1993 by the University of Southern California
5 1.1 mycroft * All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * Permission to use, copy, modify, and distribute this software and its
8 1.1 mycroft * documentation in source and binary forms for non-commercial purposes
9 1.1 mycroft * and without fee is hereby granted, provided that the above copyright
10 1.1 mycroft * notice appear in all copies and that both the copyright notice and
11 1.1 mycroft * this permission notice appear in supporting documentation. and that
12 1.1 mycroft * any documentation, advertising materials, and other materials related
13 1.1 mycroft * to such distribution and use acknowledge that the software was
14 1.1 mycroft * developed by the University of Southern California, Information
15 1.1 mycroft * Sciences Institute. The name of the University may not be used to
16 1.1 mycroft * endorse or promote products derived from this software without
17 1.1 mycroft * specific prior written permission.
18 1.1 mycroft *
19 1.1 mycroft * THE UNIVERSITY OF SOUTHERN CALIFORNIA makes no representations about
20 1.1 mycroft * the suitability of this software for any purpose. THIS SOFTWARE IS
21 1.1 mycroft * PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
22 1.1 mycroft * INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
23 1.1 mycroft * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 1.1 mycroft *
25 1.1 mycroft * Other copyrights might apply to parts of this software and are so
26 1.1 mycroft * noted when applicable.
27 1.1 mycroft */
28 1.1 mycroft
29 1.1 mycroft /* RSRR code written by Daniel Zappala, USC Information Sciences Institute,
30 1.1 mycroft * April 1995.
31 1.1 mycroft */
32 1.1 mycroft
33 1.1 mycroft #ifdef RSRR
34 1.1 mycroft
35 1.1 mycroft #include "defs.h"
36 1.1 mycroft
37 1.1 mycroft /* Taken from prune.c */
38 1.1 mycroft /*
39 1.1 mycroft * checks for scoped multicast addresses
40 1.1 mycroft */
41 1.1 mycroft #define GET_SCOPE(gt) { \
42 1.1 mycroft register int _i; \
43 1.1 mycroft if (((gt)->gt_mcastgrp & 0xff000000) == 0xef000000) \
44 1.1 mycroft for (_i = 0; _i < numvifs; _i++) \
45 1.1 mycroft if (scoped_addr(_i, (gt)->gt_mcastgrp)) \
46 1.1 mycroft VIFM_SET(_i, (gt)->gt_scope); \
47 1.1 mycroft }
48 1.1 mycroft
49 1.1 mycroft /*
50 1.1 mycroft * Exported variables.
51 1.1 mycroft */
52 1.1 mycroft int rsrr_socket; /* interface to reservation protocol */
53 1.1 mycroft
54 1.1 mycroft /*
55 1.1 mycroft * Global RSRR variables.
56 1.1 mycroft */
57 1.1 mycroft char rsrr_recv_buf[RSRR_MAX_LEN]; /* RSRR receive buffer */
58 1.1 mycroft char rsrr_send_buf[RSRR_MAX_LEN]; /* RSRR send buffer */
59 1.1 mycroft
60 1.1 mycroft /*
61 1.1 mycroft * Procedure definitions needed internally.
62 1.1 mycroft */
63 1.1 mycroft void rsrr_accept();
64 1.1 mycroft void rsrr_send();
65 1.1 mycroft void rsrr_accept_iq();
66 1.1 mycroft void rsrr_accept_rq();
67 1.1 mycroft
68 1.1 mycroft /* Initialize RSRR socket */
69 1.1 mycroft void
70 1.1 mycroft rsrr_init()
71 1.1 mycroft {
72 1.1 mycroft int servlen;
73 1.1 mycroft struct sockaddr_un serv_addr;
74 1.1 mycroft
75 1.1 mycroft if ((rsrr_socket = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0)
76 1.1 mycroft log(LOG_ERR, errno, "Can't create RSRR socket");
77 1.1 mycroft
78 1.1 mycroft unlink(RSRR_SERV_PATH);
79 1.1 mycroft bzero((char *) &serv_addr, sizeof(serv_addr));
80 1.1 mycroft serv_addr.sun_family = AF_UNIX;
81 1.1 mycroft strcpy(serv_addr.sun_path, RSRR_SERV_PATH);
82 1.1 mycroft servlen = sizeof(serv_addr.sun_family) + strlen(serv_addr.sun_path);
83 1.1 mycroft
84 1.1 mycroft if (bind(rsrr_socket, (struct sockaddr *) &serv_addr, servlen) < 0)
85 1.1 mycroft log(LOG_ERR, errno, "Can't bind RSRR socket");
86 1.1 mycroft
87 1.1 mycroft if (register_input_handler(rsrr_socket,rsrr_read) < 0)
88 1.1 mycroft log(LOG_WARNING, 0, "Couldn't register RSRR as an input handler");
89 1.1 mycroft }
90 1.1 mycroft
91 1.1 mycroft /* Read a message from the RSRR socket */
92 1.1 mycroft void
93 1.1 mycroft rsrr_read()
94 1.1 mycroft {
95 1.1 mycroft register int rsrr_recvlen;
96 1.1 mycroft struct sockaddr_un client_addr;
97 1.1 mycroft int client_length = sizeof(client_addr);
98 1.1 mycroft register int omask;
99 1.1 mycroft
100 1.1 mycroft bzero((char *) &client_addr, sizeof(client_addr));
101 1.1 mycroft rsrr_recvlen = recvfrom(rsrr_socket, rsrr_recv_buf, sizeof(rsrr_recv_buf),
102 1.1 mycroft 0, &client_addr, &client_length);
103 1.1 mycroft if (rsrr_recvlen < 0) {
104 1.1 mycroft if (errno != EINTR)
105 1.1 mycroft log(LOG_ERR, errno, "RSRR recvfrom");
106 1.1 mycroft return;
107 1.1 mycroft }
108 1.1 mycroft /* Use of omask taken from main() */
109 1.1 mycroft omask = sigblock(sigmask(SIGALRM));
110 1.1 mycroft rsrr_accept(rsrr_recvlen,&client_addr,client_length);
111 1.1 mycroft (void)sigsetmask(omask);
112 1.1 mycroft }
113 1.1 mycroft
114 1.1 mycroft /* Accept a message from the reservation protocol and take
115 1.1 mycroft * appropriate action.
116 1.1 mycroft */
117 1.1 mycroft void
118 1.1 mycroft rsrr_accept(recvlen,client_addr,client_length)
119 1.1 mycroft int recvlen;
120 1.1 mycroft struct sockaddr_un *client_addr;
121 1.1 mycroft int client_length;
122 1.1 mycroft {
123 1.1 mycroft struct rsrr_header *rsrr;
124 1.1 mycroft struct rsrr_rq *route_query;
125 1.1 mycroft
126 1.1 mycroft if (recvlen < RSRR_HEADER_LEN) {
127 1.1 mycroft log(LOG_WARNING, 0,
128 1.1 mycroft "Received RSRR packet of %d bytes, which is less than min size",
129 1.1 mycroft recvlen);
130 1.1 mycroft return;
131 1.1 mycroft }
132 1.1 mycroft
133 1.1 mycroft rsrr = (struct rsrr_header *) rsrr_recv_buf;
134 1.1 mycroft
135 1.1 mycroft if (rsrr->version > RSRR_MAX_VERSION) {
136 1.1 mycroft log(LOG_WARNING, 0,
137 1.1 mycroft "Received RSRR packet version %d, which I don't understand",
138 1.1 mycroft rsrr->version);
139 1.1 mycroft return;
140 1.1 mycroft }
141 1.1 mycroft
142 1.1 mycroft switch (rsrr->version) {
143 1.1 mycroft case 1:
144 1.1 mycroft switch (rsrr->type) {
145 1.1 mycroft case RSRR_INITIAL_QUERY:
146 1.1 mycroft /* Send Initial Reply to client */
147 1.1 mycroft log(LOG_INFO, 0, "Received Initial Query\n");
148 1.1 mycroft rsrr_accept_iq(client_addr,client_length);
149 1.1 mycroft break;
150 1.1 mycroft case RSRR_ROUTE_QUERY:
151 1.1 mycroft /* Check size */
152 1.1 mycroft if (recvlen < RSRR_RQ_LEN) {
153 1.1 mycroft log(LOG_WARNING, 0,
154 1.1 mycroft "Received Route Query of %d bytes, which is too small",
155 1.1 mycroft recvlen);
156 1.1 mycroft break;
157 1.1 mycroft }
158 1.1 mycroft /* Get the query */
159 1.1 mycroft route_query = (struct rsrr_rq *) (rsrr_recv_buf + RSRR_HEADER_LEN);
160 1.1 mycroft log(LOG_INFO, 0,
161 1.1 mycroft "Received Route Query for src %s grp %s notification %d",
162 1.1 mycroft inet_fmt(route_query->source_addr.s_addr, s1),
163 1.1 mycroft inet_fmt(route_query->dest_addr.s_addr,s2),
164 1.1 mycroft BIT_TST(rsrr->flags,RSRR_NOTIFICATION_BIT));
165 1.1 mycroft /* Send Route Reply to client */
166 1.1 mycroft rsrr_accept_rq(rsrr,route_query,client_addr,client_length);
167 1.1 mycroft break;
168 1.1 mycroft default:
169 1.1 mycroft log(LOG_WARNING, 0,
170 1.1 mycroft "Received RSRR packet type %d, which I don't handle",
171 1.1 mycroft rsrr->type);
172 1.1 mycroft break;
173 1.1 mycroft }
174 1.1 mycroft break;
175 1.1 mycroft
176 1.1 mycroft default:
177 1.1 mycroft log(LOG_WARNING, 0,
178 1.1 mycroft "Received RSRR packet version %d, which I don't understand",
179 1.1 mycroft rsrr->version);
180 1.1 mycroft break;
181 1.1 mycroft }
182 1.1 mycroft }
183 1.1 mycroft
184 1.1 mycroft /* Send an Initial Reply to the reservation protocol. */
185 1.1 mycroft void
186 1.1 mycroft rsrr_accept_iq(client_addr,client_length)
187 1.1 mycroft struct sockaddr_un *client_addr;
188 1.1 mycroft int client_length;
189 1.1 mycroft {
190 1.1 mycroft struct rsrr_header *rsrr;
191 1.1 mycroft struct rsrr_vif *vif_list;
192 1.1 mycroft struct uvif *v;
193 1.1 mycroft int vifi, sendlen;
194 1.1 mycroft
195 1.1 mycroft /* Check for space. There should be room for plenty of vifs,
196 1.1 mycroft * but we should check anyway.
197 1.1 mycroft */
198 1.1 mycroft if (numvifs > RSRR_MAX_VIFS) {
199 1.1 mycroft log(LOG_WARNING, 0,
200 1.1 mycroft "Can't send RSRR Route Reply because %d is too many vifs %d",
201 1.1 mycroft numvifs);
202 1.1 mycroft return;
203 1.1 mycroft }
204 1.1 mycroft
205 1.1 mycroft /* Set up message */
206 1.1 mycroft rsrr = (struct rsrr_header *) rsrr_send_buf;
207 1.1 mycroft rsrr->version = 1;
208 1.1 mycroft rsrr->type = RSRR_INITIAL_REPLY;
209 1.1 mycroft rsrr->flags = 0;
210 1.1 mycroft rsrr->num = numvifs;
211 1.1 mycroft
212 1.1 mycroft vif_list = (struct rsrr_vif *) (rsrr_send_buf + RSRR_HEADER_LEN);
213 1.1 mycroft
214 1.1 mycroft /* Include the vif list. */
215 1.1 mycroft for (vifi=0, v = uvifs; vifi < numvifs; vifi++, v++) {
216 1.1 mycroft vif_list[vifi].id = vifi;
217 1.1 mycroft vif_list[vifi].status = 0;
218 1.1 mycroft if (v->uv_flags & VIFF_DISABLED)
219 1.1 mycroft BIT_SET(vif_list[vifi].status,RSRR_DISABLED_BIT);
220 1.1 mycroft vif_list[vifi].threshold = v->uv_threshold;
221 1.1 mycroft vif_list[vifi].local_addr.s_addr = v->uv_lcl_addr;
222 1.1 mycroft }
223 1.1 mycroft
224 1.1 mycroft /* Get the size. */
225 1.1 mycroft sendlen = RSRR_HEADER_LEN + numvifs*RSRR_VIF_LEN;
226 1.1 mycroft
227 1.1 mycroft /* Send it. */
228 1.1 mycroft log(LOG_INFO, 0, "Send RSRR Initial Reply");
229 1.1 mycroft rsrr_send(sendlen,client_addr,client_length);
230 1.1 mycroft }
231 1.1 mycroft
232 1.1 mycroft /* Send a Route Reply to the reservation protocol. */
233 1.1 mycroft void
234 1.1 mycroft rsrr_accept_rq(rsrr_in,route_query,client_addr,client_length)
235 1.1 mycroft struct rsrr_header *rsrr_in;
236 1.1 mycroft struct rsrr_rq *route_query;
237 1.1 mycroft struct sockaddr_un *client_addr;
238 1.1 mycroft int client_length;
239 1.1 mycroft {
240 1.1 mycroft struct rsrr_header *rsrr;
241 1.1 mycroft struct rsrr_rr *route_reply;
242 1.1 mycroft struct gtable *gt,local_g;
243 1.1 mycroft struct rtentry *r;
244 1.1 mycroft int sendlen,i;
245 1.1 mycroft u_long mcastgrp;
246 1.1 mycroft
247 1.1 mycroft /* Set up message */
248 1.1 mycroft rsrr = (struct rsrr_header *) rsrr_send_buf;
249 1.1 mycroft rsrr->version = 1;
250 1.1 mycroft rsrr->type = RSRR_ROUTE_REPLY;
251 1.1 mycroft rsrr->flags = rsrr_in->flags;
252 1.1 mycroft rsrr->num = 0;
253 1.1 mycroft
254 1.1 mycroft route_reply = (struct rsrr_rr *) (rsrr_send_buf + RSRR_HEADER_LEN);
255 1.1 mycroft route_reply->dest_addr.s_addr = route_query->dest_addr.s_addr;
256 1.1 mycroft route_reply->source_addr.s_addr = route_query->source_addr.s_addr;
257 1.1 mycroft route_reply->query_id = route_query->query_id;
258 1.1 mycroft
259 1.1 mycroft /* Blank routing entry for error. */
260 1.1 mycroft route_reply->in_vif = 0;
261 1.1 mycroft route_reply->reserved = 0;
262 1.1 mycroft route_reply->out_vif_bm = 0;
263 1.1 mycroft
264 1.1 mycroft /* Clear error bit. */
265 1.1 mycroft BIT_CLR(rsrr->flags,RSRR_ERROR_BIT);
266 1.1 mycroft /* Turn notification off. We don't do it yet. */
267 1.1 mycroft BIT_CLR(rsrr->flags,RSRR_NOTIFICATION_BIT);
268 1.1 mycroft
269 1.1 mycroft /* First check kernel. Code taken from add_table_entry() */
270 1.1 mycroft if (find_src_grp(route_query->source_addr.s_addr, 0,
271 1.1 mycroft route_query->dest_addr.s_addr)) {
272 1.1 mycroft gt = gtp ? gtp->gt_gnext : kernel_table;
273 1.1 mycroft
274 1.1 mycroft /* Include the routing entry. */
275 1.1 mycroft route_reply->in_vif = gt->gt_route->rt_parent;
276 1.1 mycroft route_reply->out_vif_bm = gt->gt_grpmems;
277 1.1 mycroft
278 1.1 mycroft } else {
279 1.1 mycroft /* No kernel entry; use routing table. */
280 1.1 mycroft r = determine_route(route_query->source_addr.s_addr);
281 1.1 mycroft
282 1.1 mycroft if (r != NULL) {
283 1.1 mycroft /* We need to mimic what will happen if a data packet
284 1.1 mycroft * is forwarded by multicast routing -- the kernel will
285 1.1 mycroft * make an upcall and mrouted will install a route in the kernel.
286 1.1 mycroft * Our outgoing vif bitmap should reflect what that table
287 1.1 mycroft * will look like. Grab code from add_table_entry().
288 1.1 mycroft * This is gross, but it's probably better to be accurate.
289 1.1 mycroft */
290 1.1 mycroft
291 1.1 mycroft gt = &local_g;
292 1.1 mycroft mcastgrp = route_query->dest_addr.s_addr;
293 1.1 mycroft
294 1.1 mycroft gt->gt_mcastgrp = mcastgrp;
295 1.1 mycroft gt->gt_grpmems = 0;
296 1.1 mycroft gt->gt_scope = 0;
297 1.1 mycroft gt->gt_route = r;
298 1.1 mycroft
299 1.1 mycroft /* obtain the multicast group membership list */
300 1.1 mycroft for (i = 0; i < numvifs; i++) {
301 1.1 mycroft if (VIFM_ISSET(i, r->rt_children) &&
302 1.1 mycroft !(VIFM_ISSET(i, r->rt_leaves)))
303 1.1 mycroft VIFM_SET(i, gt->gt_grpmems);
304 1.1 mycroft
305 1.1 mycroft if (VIFM_ISSET(i, r->rt_leaves) && grplst_mem(i, mcastgrp))
306 1.1 mycroft VIFM_SET(i, gt->gt_grpmems);
307 1.1 mycroft }
308 1.1 mycroft
309 1.1 mycroft GET_SCOPE(gt);
310 1.1 mycroft gt->gt_grpmems &= ~gt->gt_scope;
311 1.1 mycroft
312 1.1 mycroft /* Include the routing entry. */
313 1.1 mycroft route_reply->in_vif = gt->gt_route->rt_parent;
314 1.1 mycroft route_reply->out_vif_bm = gt->gt_grpmems;
315 1.1 mycroft
316 1.1 mycroft } else {
317 1.1 mycroft /* Set error bit. */
318 1.1 mycroft BIT_SET(rsrr->flags,RSRR_ERROR_BIT);
319 1.1 mycroft }
320 1.1 mycroft }
321 1.1 mycroft
322 1.1 mycroft /* Get the size. */
323 1.1 mycroft sendlen = RSRR_RR_LEN;
324 1.1 mycroft
325 1.1 mycroft log(LOG_INFO, 0, "Send RSRR Route Reply for src %s grp %s ",
326 1.1 mycroft inet_fmt(route_reply->source_addr.s_addr,s1),
327 1.1 mycroft inet_fmt(route_reply->dest_addr.s_addr,s2));
328 1.1 mycroft log(LOG_INFO, 0, "in vif %d out vif %d\n",
329 1.1 mycroft route_reply->in_vif,route_reply->out_vif_bm);
330 1.1 mycroft
331 1.1 mycroft /* Send it. */
332 1.1 mycroft rsrr_send(sendlen,client_addr,client_length);
333 1.1 mycroft }
334 1.1 mycroft
335 1.1 mycroft /* Send an RSRR message. */
336 1.1 mycroft void
337 1.1 mycroft rsrr_send(sendlen,client_addr,client_length)
338 1.1 mycroft int sendlen;
339 1.1 mycroft struct sockaddr_un *client_addr;
340 1.1 mycroft int client_length;
341 1.1 mycroft {
342 1.1 mycroft int error;
343 1.1 mycroft
344 1.1 mycroft /* Send it. */
345 1.1 mycroft error = sendto(rsrr_socket, rsrr_send_buf, sendlen, 0,
346 1.1 mycroft *client_addr, client_length);
347 1.1 mycroft
348 1.1 mycroft /* Check for errors. */
349 1.1 mycroft if (error < 0) {
350 1.1 mycroft log(LOG_WARNING, errno, "Failed send on RSRR socket");
351 1.1 mycroft return;
352 1.1 mycroft }
353 1.1 mycroft if (error != sendlen) {
354 1.1 mycroft log(LOG_WARNING, 0,
355 1.1 mycroft "Sent only %d out of %d bytes on RSRR socket\n", error, sendlen);
356 1.1 mycroft return;
357 1.1 mycroft }
358 1.1 mycroft }
359 1.1 mycroft
360 1.1 mycroft void
361 1.1 mycroft rsrr_clean()
362 1.1 mycroft {
363 1.1 mycroft unlink(RSRR_SERV_PATH);
364 1.1 mycroft }
365 1.1 mycroft
366 1.1 mycroft #endif RSRR
367