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