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