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