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