2 * Copyright (c) 1984, 1985, 1986, 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $
35 * $DragonFly: src/sys/netproto/ns/ns_input.c,v 1.4 2003/08/07 21:17:38 dillon Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.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>
48 #include <sys/kernel.h>
51 #include <net/route.h>
52 #include <net/raw_cb.h>
53 #include <net/netisr.h>
54 #include <net/intrq.h>
66 union ns_host ns_thishost;
67 union ns_host ns_zerohost;
68 union ns_host ns_broadhost;
69 union ns_net ns_zeronet;
70 union ns_net ns_broadnet;
71 struct sockaddr_ns ns_netmask, ns_hostmask;
73 static u_short allones[] = {-1, -1, -1};
76 struct nspcb nsrawpcb;
78 int nsqmaxlen = IFQ_MAXLEN;
83 const int nsintrq_present = 1;
87 extern struct timeval time;
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 register_netisr(NETISR_NS, nsintr);
104 * Idp input routine. Pass to next level.
106 int nsintr_getpck = 0;
107 int nsintr_swtch = 0;
120 * Get next datagram off input queue and get IDP header
124 IF_DEQUEUE(&nsintrq, m);
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++;
136 * Give any raw listeners a crack at the packet
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);
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 */
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.
156 if (m->m_pkthdr.len < len) {
157 idpstat.idps_tooshort++;
160 if (m->m_pkthdr.len > len) {
161 if (m->m_len == m->m_pkthdr.len) {
163 m->m_pkthdr.len = len;
165 m_adj(m, len - m->m_pkthdr.len);
167 if (idpcksum && ((i = idp->idp_sum)!=0xffff)) {
169 if (i != (idp->idp_sum = ns_cksum(m, len))) {
170 idpstat.idps_badsum++;
172 if (ns_hosteqnh(ns_thishost, idp->idp_dna.x_host))
173 error = NS_ERR_BADSUM;
175 error = NS_ERR_BADSUM_T;
176 ns_error(m, error, 0);
181 * Is this a directed broadcast?
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)) ) {
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
196 * Suggestion of Bill Nesheim, Cornell U.
198 if (idp->idp_tc < NS_MAXHOPS) {
204 * Is this our packet? If not, forward.
206 } else if (!ns_hosteqnh(ns_thishost,idp->idp_dna.x_host)) {
211 * Locate pcb for datagram.
213 nsp = ns_pcblookup(&idp->idp_sna, idp->idp_dna.x_port, NS_WILDCARD);
215 * Switch out to protocol's input routine.
222 if ((nsp->nsp_flags & NSP_ALL_PACKETS)==0)
223 switch (idp->idp_pt) {
235 ns_error(m, NS_ERR_NOSOCK, 0);
244 u_char nsctlerrmap[PRC_NCMDS] = {
245 ECONNABORTED, ECONNABORTED, 0, 0,
246 0, 0, EHOSTDOWN, EHOSTUNREACH,
247 ENETUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
252 int idp_donosocks = 1;
254 idp_ctlinput(cmd, arg)
260 struct ns_errp *errp;
262 extern struct nspcb *idp_drop();
265 if (cmd < 0 || cmd > PRC_NCMDS)
267 if (nsctlerrmap[cmd] == 0)
269 type = NS_ERR_UNREACH_HOST;
271 struct sockaddr_ns *sns;
275 case PRC_HOSTUNREACH:
276 sns = (struct sockaddr_ns *)arg;
277 if (sns->sns_family != AF_NS)
283 errp = (struct ns_errp *)arg;
284 ns = &errp->ns_err_idp.idp_dna;
285 type = errp->ns_err_num;
286 type = ntohs((u_short)type);
290 case NS_ERR_UNREACH_HOST:
291 ns_pcbnotify(ns, (int)nsctlerrmap[cmd], idp_abort, (long)0);
295 nsp = ns_pcblookup(ns, errp->ns_err_idp.idp_sna.x_port,
297 if(nsp && idp_donosocks && ! ns_nullhost(nsp->nsp_faddr))
298 (void) idp_drop(nsp, (int)nsctlerrmap[cmd]);
303 int idpforwarding = 1;
305 * Forward a packet. If some error occurs return the sender
306 * an error packet. Note we can't always generate a meaningful
307 * error message because the NS errors don't have a large enough repetoire
308 * of codes and types.
310 struct route idp_droute;
311 struct route idp_sroute;
316 struct idp *idp = mtod(m, struct idp *);
317 int error, type, code;
318 struct mbuf *mcopy = NULL;
320 int flags = NS_FORWARDING;
325 printf("forward: src ");
326 ns_printhost(&idp->idp_sna);
328 ns_printhost(&idp->idp_dna);
329 printf("hop count %d\n", idp->idp_tc);
331 if (idpforwarding == 0) {
332 /* can't tell difference between net and host */
333 type = NS_ERR_UNREACH_HOST, code = 0;
337 if (idp->idp_tc > NS_MAXHOPS) {
338 type = NS_ERR_TOO_OLD, code = 0;
342 * Save at most 42 bytes of the packet in case
343 * we need to generate an NS error message to the src.
345 mcopy = m_copy(m, 0, imin((int)ntohs(idp->idp_len), 42));
347 if ((ok_there = idp_do_route(&idp->idp_dna,&idp_droute))==0) {
348 type = NS_ERR_UNREACH_HOST, code = 0;
352 * Here we think about forwarding broadcast packets,
353 * so we try to insure that it doesn't go back out
354 * on the interface it came in on. Also, if we
355 * are going to physically broadcast this, let us
356 * age the packet so we can eat it safely the second time around.
358 if (idp->idp_dna.x_host.c_host[0] & 0x1) {
359 struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna);
362 /* I'm gonna hafta eat this packet */
363 agedelta += NS_MAXHOPS - idp->idp_tc;
364 idp->idp_tc = NS_MAXHOPS;
366 if ((ok_back = idp_do_route(&idp->idp_sna,&idp_sroute))==0) {
367 /* error = ENETUNREACH; He'll never get it! */
371 if (idp_droute.ro_rt &&
372 (ifp=idp_droute.ro_rt->rt_ifp) &&
374 (ifp!=idp_sroute.ro_rt->rt_ifp)) {
375 flags |= NS_ALLOWBROADCAST;
377 type = NS_ERR_UNREACH_HOST, code = 0;
381 /* need to adjust checksum */
382 if (idp->idp_sum!=0xffff) {
389 x.l = 0; x.c[0] = agedelta;
390 shift = (((((int)ntohs(idp->idp_len))+1)>>1)-2) & 0xf;
391 x.l = idp->idp_sum + (x.s[0] << shift);
392 x.l = x.s[0] + x.s[1];
393 x.l = x.s[0] + x.s[1];
394 if (x.l==0xffff) idp->idp_sum = 0; else idp->idp_sum = x.l;
396 if ((error = ns_output(m, &idp_droute, flags)) &&
398 idp = mtod(mcopy, struct idp *);
399 type = NS_ERR_UNSPEC_T, code = 0;
407 type = NS_ERR_UNREACH_HOST;
411 type = NS_ERR_TOO_BIG;
412 code = 576; /* too hard to figure out mtu here */
416 type = NS_ERR_UNSPEC_T;
421 ns_error(m, type, code);
425 idp_undo_route(&idp_droute);
427 idp_undo_route(&idp_sroute);
432 idp_do_route(src, ro)
437 struct sockaddr_ns *dst;
439 bzero((caddr_t)ro, sizeof (*ro));
440 dst = (struct sockaddr_ns *)&ro->ro_dst;
442 dst->sns_len = sizeof(*dst);
443 dst->sns_family = AF_NS;
444 dst->sns_addr = *src;
445 dst->sns_addr.x_port = 0;
447 if (ro->ro_rt == 0 || ro->ro_rt->rt_ifp == 0) {
457 if (ro->ro_rt) {RTFREE(ro->ro_rt);}
460 ns_watch_output(m, ifp)
467 * Give any raw listeners a crack at the packet
469 for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) {
470 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
474 M_PREPEND(m0, sizeof (*idp), M_DONTWAIT);
477 idp = mtod(m0, struct idp *);
478 idp->idp_sna.x_net = ns_zeronet;
479 idp->idp_sna.x_host = ns_thishost;
480 if (ifp && (ifp->if_flags & IFF_POINTOPOINT))
481 for(ifa = ifp->if_addrlist; ifa;
482 ifa = ifa->ifa_next) {
483 if (ifa->ifa_addr->sa_family==AF_NS) {
484 idp->idp_sna = IA_SNS(ifa)->sns_addr;
488 idp->idp_len = ntohl(m0->m_pkthdr.len);