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