#ifdef'ed out definition of sleep() now that nothing uses it. This will
[unix-history] / sys / netns / ns_input.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1984, 1985, 1986, 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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 *
136b31f1 33 * from: @(#)ns_input.c 7.8 (Berkeley) 6/27/91
fde1aeb2 34 * $Id: ns_input.c,v 1.4 1993/11/25 01:36:30 wollman Exp $
15637ed4
RG
35 */
36
37#include "param.h"
38#include "systm.h"
39#include "malloc.h"
40#include "mbuf.h"
41#include "domain.h"
42#include "protosw.h"
43#include "socket.h"
44#include "socketvar.h"
45#include "errno.h"
46#include "time.h"
47#include "kernel.h"
48
49#include "../net/if.h"
50#include "../net/route.h"
51#include "../net/raw_cb.h"
52
53#include "ns.h"
54#include "ns_if.h"
55#include "ns_pcb.h"
56#include "idp.h"
57#include "idp_var.h"
58#include "ns_error.h"
59
4c45483e
GW
60static void idp_forward(struct mbuf *);
61static void idp_undo_route(struct route *);
62
15637ed4
RG
63/*
64 * NS initialization.
65 */
66union ns_host ns_thishost;
67union ns_host ns_zerohost;
68union ns_host ns_broadhost;
69union ns_net ns_zeronet;
70union ns_net ns_broadnet;
71struct sockaddr_ns ns_netmask, ns_hostmask;
72
73static u_short allones[] = {-1, -1, -1};
74
75struct nspcb nspcb;
76struct nspcb nsrawpcb;
77
78struct ifqueue nsintrq;
79int nsqmaxlen = IFQ_MAXLEN;
80
81int idpcksum = 1;
82long ns_pexseq;
83
4c45483e 84void
15637ed4
RG
85ns_init()
86{
87 extern struct timeval time;
88
89 ns_broadhost = * (union ns_host *) allones;
90 ns_broadnet = * (union ns_net *) allones;
91 nspcb.nsp_next = nspcb.nsp_prev = &nspcb;
92 nsrawpcb.nsp_next = nsrawpcb.nsp_prev = &nsrawpcb;
93 nsintrq.ifq_maxlen = nsqmaxlen;
94 ns_pexseq = time.tv_usec;
95 ns_netmask.sns_len = 6;
96 ns_netmask.sns_addr.x_net = ns_broadnet;
97 ns_hostmask.sns_len = 12;
98 ns_hostmask.sns_addr.x_net = ns_broadnet;
99 ns_hostmask.sns_addr.x_host = ns_broadhost;
100}
101
102/*
103 * Idp input routine. Pass to next level.
104 */
105int nsintr_getpck = 0;
106int nsintr_swtch = 0;
4c45483e
GW
107
108void
15637ed4
RG
109nsintr()
110{
111 register struct idp *idp;
112 register struct mbuf *m;
113 register struct nspcb *nsp;
114 register int i;
115 int len, s, error;
116 char oddpacketp;
117
118next:
119 /*
120 * Get next datagram off input queue and get IDP header
121 * in first mbuf.
122 */
123 s = splimp();
124 IF_DEQUEUE(&nsintrq, m);
125 splx(s);
126 nsintr_getpck++;
127 if (m == 0)
128 return;
129 if ((m->m_flags & M_EXT || m->m_len < sizeof (struct idp)) &&
130 (m = m_pullup(m, sizeof (struct idp))) == 0) {
131 idpstat.idps_toosmall++;
132 goto next;
133 }
134
135 /*
136 * Give any raw listeners a crack at the packet
137 */
138 for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) {
139 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
140 if (m1) idp_input(m1, nsp);
141 }
142
143 idp = mtod(m, struct idp *);
144 len = ntohs(idp->idp_len);
145 if (oddpacketp = len & 1) {
146 len++; /* If this packet is of odd length,
147 preserve garbage byte for checksum */
148 }
149
150 /*
151 * Check that the amount of data in the buffers
152 * is as at least much as the IDP header would have us expect.
153 * Trim mbufs if longer than we expect.
154 * Drop packet if shorter than we expect.
155 */
156 if (m->m_pkthdr.len < len) {
157 idpstat.idps_tooshort++;
158 goto bad;
159 }
160 if (m->m_pkthdr.len > len) {
161 if (m->m_len == m->m_pkthdr.len) {
162 m->m_len = len;
163 m->m_pkthdr.len = len;
164 } else
165 m_adj(m, len - m->m_pkthdr.len);
166 }
167 if (idpcksum && ((i = idp->idp_sum)!=0xffff)) {
168 idp->idp_sum = 0;
169 if (i != (idp->idp_sum = ns_cksum(m, len))) {
170 idpstat.idps_badsum++;
171 idp->idp_sum = i;
172 if (ns_hosteqnh(ns_thishost, idp->idp_dna.x_host))
173 error = NS_ERR_BADSUM;
174 else
175 error = NS_ERR_BADSUM_T;
176 ns_error(m, error, 0);
177 goto next;
178 }
179 }
180 /*
181 * Is this a directed broadcast?
182 */
183 if (ns_hosteqnh(ns_broadhost,idp->idp_dna.x_host)) {
184 if ((!ns_neteq(idp->idp_dna, idp->idp_sna)) &&
185 (!ns_neteqnn(idp->idp_dna.x_net, ns_broadnet)) &&
186 (!ns_neteqnn(idp->idp_sna.x_net, ns_zeronet)) &&
187 (!ns_neteqnn(idp->idp_dna.x_net, ns_zeronet)) ) {
188 /*
189 * Look to see if I need to eat this packet.
190 * Algorithm is to forward all young packets
191 * and prematurely age any packets which will
192 * by physically broadcasted.
193 * Any very old packets eaten without forwarding
194 * would die anyway.
195 *
196 * Suggestion of Bill Nesheim, Cornell U.
197 */
198 if (idp->idp_tc < NS_MAXHOPS) {
199 idp_forward(m);
200 goto next;
201 }
202 }
203 /*
204 * Is this our packet? If not, forward.
205 */
206 } else if (!ns_hosteqnh(ns_thishost,idp->idp_dna.x_host)) {
207 idp_forward(m);
208 goto next;
209 }
210 /*
211 * Locate pcb for datagram.
212 */
213 nsp = ns_pcblookup(&idp->idp_sna, idp->idp_dna.x_port, NS_WILDCARD);
214 /*
215 * Switch out to protocol's input routine.
216 */
217 nsintr_swtch++;
218 if (nsp) {
219 if (oddpacketp) {
220 m_adj(m, -1);
221 }
222 if ((nsp->nsp_flags & NSP_ALL_PACKETS)==0)
223 switch (idp->idp_pt) {
224
225 case NSPROTO_SPP:
226 spp_input(m, nsp);
227 goto next;
228
229 case NSPROTO_ERROR:
230 ns_err_input(m);
231 goto next;
232 }
233 idp_input(m, nsp);
234 } else {
235 ns_error(m, NS_ERR_NOSOCK, 0);
236 }
237 goto next;
238
239bad:
240 m_freem(m);
241 goto next;
242}
243
244u_char nsctlerrmap[PRC_NCMDS] = {
e549489f
GW
245 ECONNABORTED, /* ifdown */
246 ECONNABORTED, /* routedead */
247 0, /* #2 */
248 0, /* quench2 */
249 0, /* quench */
250 EMSGSIZE, /* msgsize */
251 EHOSTDOWN, /* hostdead */
252 EHOSTUNREACH, /* hostunreach */
253 ENETUNREACH, /* unreachnet */
254 EHOSTUNREACH, /* unreachhost */
255 ECONNREFUSED, /* unreachproto */
256 ECONNREFUSED, /* unreachport */
257 EMSGSIZE, /* old needfrag */
258 0, /* srcfail */
259 0, /* netunknown */
260 0, /* hostunknown */
261 0, /* isolated */
262 0, /* net admin. prohibited */
263 0, /* host admin. prohibited */
264 0, /* tos net unreach */
265 0, /* tos host unreach */
266 0, /* redirect net */
267 0, /* redirect host */
268 0, /* redirect tosnet */
269 0, /* redirect toshost */
270 0, /* time exceeded */
271 0, /* reassembly timeout */
272 0, /* parameter problem */
273 0 /* required option missing */
15637ed4
RG
274};
275
276int idp_donosocks = 1;
277
4c45483e 278void
15637ed4
RG
279idp_ctlinput(cmd, arg)
280 int cmd;
281 caddr_t arg;
282{
283 struct ns_addr *ns;
284 struct nspcb *nsp;
4c45483e 285 struct ns_errp *errp = 0;
15637ed4
RG
286 int type;
287
288 if (cmd < 0 || cmd > PRC_NCMDS)
289 return;
290 if (nsctlerrmap[cmd] == 0)
291 return; /* XXX */
292 type = NS_ERR_UNREACH_HOST;
293 switch (cmd) {
294 struct sockaddr_ns *sns;
295
296 case PRC_IFDOWN:
297 case PRC_HOSTDEAD:
298 case PRC_HOSTUNREACH:
299 sns = (struct sockaddr_ns *)arg;
300 if (sns->sns_family != AF_NS)
301 return;
302 ns = &sns->sns_addr;
303 break;
304
305 default:
306 errp = (struct ns_errp *)arg;
307 ns = &errp->ns_err_idp.idp_dna;
308 type = errp->ns_err_num;
309 type = ntohs((u_short)type);
310 }
311 switch (type) {
312
313 case NS_ERR_UNREACH_HOST:
314 ns_pcbnotify(ns, (int)nsctlerrmap[cmd], idp_abort, (long)0);
315 break;
316
317 case NS_ERR_NOSOCK:
318 nsp = ns_pcblookup(ns, errp->ns_err_idp.idp_sna.x_port,
319 NS_WILDCARD);
320 if(nsp && idp_donosocks && ! ns_nullhost(nsp->nsp_faddr))
321 (void) idp_drop(nsp, (int)nsctlerrmap[cmd]);
322 }
323}
324
325int idpprintfs = 0;
326int idpforwarding = 1;
327/*
328 * Forward a packet. If some error occurs return the sender
329 * an error packet. Note we can't always generate a meaningful
330 * error message because the NS errors don't have a large enough repetoire
331 * of codes and types.
332 */
333struct route idp_droute;
334struct route idp_sroute;
335
4c45483e 336static void
15637ed4 337idp_forward(m)
4c45483e 338 struct mbuf *m;
15637ed4
RG
339{
340 register struct idp *idp = mtod(m, struct idp *);
341 register int error, type, code;
342 struct mbuf *mcopy = NULL;
343 int agedelta = 1;
344 int flags = NS_FORWARDING;
345 int ok_there = 0;
346 int ok_back = 0;
347
348 if (idpprintfs) {
349 printf("forward: src ");
350 ns_printhost(&idp->idp_sna);
351 printf(", dst ");
352 ns_printhost(&idp->idp_dna);
353 printf("hop count %d\n", idp->idp_tc);
354 }
355 if (idpforwarding == 0) {
356 /* can't tell difference between net and host */
357 type = NS_ERR_UNREACH_HOST, code = 0;
358 goto senderror;
359 }
360 idp->idp_tc++;
361 if (idp->idp_tc > NS_MAXHOPS) {
362 type = NS_ERR_TOO_OLD, code = 0;
363 goto senderror;
364 }
365 /*
366 * Save at most 42 bytes of the packet in case
367 * we need to generate an NS error message to the src.
368 */
369 mcopy = m_copy(m, 0, imin((int)ntohs(idp->idp_len), 42));
370
371 if ((ok_there = idp_do_route(&idp->idp_dna,&idp_droute))==0) {
372 type = NS_ERR_UNREACH_HOST, code = 0;
373 goto senderror;
374 }
375 /*
376 * Here we think about forwarding broadcast packets,
377 * so we try to insure that it doesn't go back out
378 * on the interface it came in on. Also, if we
379 * are going to physically broadcast this, let us
380 * age the packet so we can eat it safely the second time around.
381 */
382 if (idp->idp_dna.x_host.c_host[0] & 0x1) {
383 struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna);
384 struct ifnet *ifp;
385 if (ia) {
386 /* I'm gonna hafta eat this packet */
387 agedelta += NS_MAXHOPS - idp->idp_tc;
388 idp->idp_tc = NS_MAXHOPS;
389 }
390 if ((ok_back = idp_do_route(&idp->idp_sna,&idp_sroute))==0) {
391 /* error = ENETUNREACH; He'll never get it! */
392 m_freem(m);
393 goto cleanup;
394 }
395 if (idp_droute.ro_rt &&
396 (ifp=idp_droute.ro_rt->rt_ifp) &&
397 idp_sroute.ro_rt &&
398 (ifp!=idp_sroute.ro_rt->rt_ifp)) {
399 flags |= NS_ALLOWBROADCAST;
400 } else {
401 type = NS_ERR_UNREACH_HOST, code = 0;
402 goto senderror;
403 }
404 }
405 /* need to adjust checksum */
406 if (idp->idp_sum!=0xffff) {
407 union bytes {
408 u_char c[4];
409 u_short s[2];
410 long l;
411 } x;
412 register int shift;
413 x.l = 0; x.c[0] = agedelta;
414 shift = (((((int)ntohs(idp->idp_len))+1)>>1)-2) & 0xf;
415 x.l = idp->idp_sum + (x.s[0] << shift);
416 x.l = x.s[0] + x.s[1];
417 x.l = x.s[0] + x.s[1];
418 if (x.l==0xffff) idp->idp_sum = 0; else idp->idp_sum = x.l;
419 }
420 if ((error = ns_output(m, &idp_droute, flags)) &&
421 (mcopy!=NULL)) {
422 idp = mtod(mcopy, struct idp *);
423 type = NS_ERR_UNSPEC_T, code = 0;
424 switch (error) {
425
426 case ENETUNREACH:
427 case EHOSTDOWN:
428 case EHOSTUNREACH:
429 case ENETDOWN:
430 case EPERM:
431 type = NS_ERR_UNREACH_HOST;
432 break;
433
434 case EMSGSIZE:
435 type = NS_ERR_TOO_BIG;
436 code = 576; /* too hard to figure out mtu here */
437 break;
438
439 case ENOBUFS:
440 type = NS_ERR_UNSPEC_T;
441 break;
442 }
443 mcopy = NULL;
444 senderror:
445 ns_error(m, type, code);
446 }
447cleanup:
448 if (ok_there)
449 idp_undo_route(&idp_droute);
450 if (ok_back)
451 idp_undo_route(&idp_sroute);
452 if (mcopy != NULL)
453 m_freem(mcopy);
454}
455
4c45483e 456int
15637ed4
RG
457idp_do_route(src, ro)
458struct ns_addr *src;
459struct route *ro;
460{
461
462 struct sockaddr_ns *dst;
463
464 bzero((caddr_t)ro, sizeof (*ro));
465 dst = (struct sockaddr_ns *)&ro->ro_dst;
466
467 dst->sns_len = sizeof(*dst);
468 dst->sns_family = AF_NS;
469 dst->sns_addr = *src;
470 dst->sns_addr.x_port = 0;
471 rtalloc(ro);
472 if (ro->ro_rt == 0 || ro->ro_rt->rt_ifp == 0) {
473 return (0);
474 }
475 ro->ro_rt->rt_use++;
476 return (1);
477}
478
4c45483e 479static void
15637ed4 480idp_undo_route(ro)
4c45483e 481 register struct route *ro;
15637ed4
RG
482{
483 if (ro->ro_rt) {RTFREE(ro->ro_rt);}
484}
485
4c45483e 486void
15637ed4 487ns_watch_output(m, ifp)
fde1aeb2
GW
488 struct mbuf *m;
489 struct ifnet *ifp;
15637ed4
RG
490{
491 register struct nspcb *nsp;
492 register struct ifaddr *ifa;
493 /*
494 * Give any raw listeners a crack at the packet
495 */
496 for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) {
497 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
498 if (m0) {
499 register struct idp *idp;
500
501 M_PREPEND(m0, sizeof (*idp), M_DONTWAIT);
502 if (m0 == NULL)
503 continue;
504 idp = mtod(m0, struct idp *);
505 idp->idp_sna.x_net = ns_zeronet;
506 idp->idp_sna.x_host = ns_thishost;
507 if (ifp && (ifp->if_flags & IFF_POINTOPOINT))
508 for(ifa = ifp->if_addrlist; ifa;
509 ifa = ifa->ifa_next) {
510 if (ifa->ifa_addr->sa_family==AF_NS) {
511 idp->idp_sna = IA_SNS(ifa)->sns_addr;
512 break;
513 }
514 }
515 idp->idp_len = ntohl(m0->m_pkthdr.len);
516 idp_input(m0, nsp);
517 }
518 }
519}