1 /* 2 * Copyright (c) 1982 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)ns_input.c 6.7 (Berkeley) 07/26/85 7 */ 8 9 #include "param.h" 10 #include "systm.h" 11 #include "mbuf.h" 12 #include "domain.h" 13 #include "protosw.h" 14 #include "socket.h" 15 #include "socketvar.h" 16 #include "errno.h" 17 #include "time.h" 18 #include "kernel.h" 19 20 #include "../net/if.h" 21 #include "../net/route.h" 22 #include "../net/raw_cb.h" 23 24 #include "ns.h" 25 #include "ns_if.h" 26 #include "ns_pcb.h" 27 #include "idp.h" 28 #include "idp_var.h" 29 #include "ns_error.h" 30 31 /* 32 * NS initialization. 33 */ 34 union ns_host ns_thishost; 35 union ns_host ns_zerohost; 36 union ns_host ns_broadhost; 37 38 static char allones[] = {-1, -1, -1, -1, -1, -1}; 39 40 struct nspcb nspcb; 41 struct nspcb nsrawpcb; 42 43 struct ifqueue nsintrq; 44 int nsqmaxlen = IFQ_MAXLEN; 45 46 int idpcksum = 1; 47 long ns_pexseq; 48 49 ns_init() 50 { 51 extern struct timeval time; 52 53 ns_broadhost = * (union ns_host *) allones; 54 nspcb.nsp_next = nspcb.nsp_prev = &nspcb; 55 nsrawpcb.nsp_next = nsrawpcb.nsp_prev = &nsrawpcb; 56 nsintrq.ifq_maxlen = nsqmaxlen; 57 ns_pexseq = time.tv_usec; 58 } 59 60 /* 61 * Idp input routine. Pass to next level. 62 */ 63 int nsintr_getpck = 0; 64 int nsintr_swtch = 0; 65 nsintr() 66 { 67 register struct idp *idp; 68 register struct mbuf *m; 69 register struct nspcb *nsp; 70 struct mbuf *m0; 71 register int i; 72 int len, s, error; 73 char oddpacketp; 74 75 next: 76 /* 77 * Get next datagram off input queue and get IDP header 78 * in first mbuf. 79 */ 80 s = splimp(); 81 IF_DEQUEUE(&nsintrq, m); 82 splx(s); 83 nsintr_getpck++; 84 if (m == 0) 85 return; 86 if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct idp)) && 87 (m = m_pullup(m, sizeof (struct idp))) == 0) { 88 idpstat.idps_toosmall++; 89 goto next; 90 } 91 92 /* 93 * Give any raw listeners a crack at the packet 94 */ 95 for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) { 96 struct mbuf *m1 = m_copy(m, 0, M_COPYALL); 97 if (m1) idp_input(m1, nsp); 98 } 99 100 idp = mtod(m, struct idp *); 101 len = ntohs(idp->idp_len); 102 if (oddpacketp = len & 1) { 103 len++; /* If this packet is of odd length, 104 preserve garbage byte for checksum */ 105 } 106 107 /* 108 * Check that the amount of data in the buffers 109 * is as at least much as the IDP header would have us expect. 110 * Trim mbufs if longer than we expect. 111 * Drop packet if shorter than we expect. 112 */ 113 i = -len; 114 m0 = m; 115 for (;;) { 116 i += m->m_len; 117 if (m->m_next == 0) 118 break; 119 m = m->m_next; 120 } 121 if (i != 0) { 122 if (i < 0) { 123 idpstat.idps_tooshort++; 124 m = m0; 125 goto bad; 126 } 127 if (i <= m->m_len) 128 m->m_len -= i; 129 else 130 m_adj(m0, -i); 131 } 132 m = m0; 133 if (idpcksum && ((i = idp->idp_sum)!=0xffff)) { 134 idp->idp_sum = 0; 135 if (i != (idp->idp_sum = ns_cksum(m,len))) { 136 idpstat.idps_badsum++; 137 idp->idp_sum = i; 138 if (ns_hosteqnh(ns_thishost, idp->idp_dna.x_host)) 139 error = NS_ERR_BADSUM; 140 else 141 error = NS_ERR_BADSUM_T; 142 ns_error(m, error, 0); 143 goto next; 144 } 145 } 146 /* 147 * Is this a directed broadcast? 148 */ 149 if (ns_hosteqnh(ns_broadhost,idp->idp_dna.x_host)) { 150 if ((ns_netof(idp->idp_dna)!=ns_netof(idp->idp_sna)) && 151 (ns_netof(idp->idp_dna)!=-1) && (ns_netof(idp->idp_sna)!=0) 152 && (ns_netof(idp->idp_dna)!=0)) { 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(idp); 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(idp); 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(m0, -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 idp_donosocks = 1; 218 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 int type; 228 229 if (cmd < 0 || cmd > PRC_NCMDS) 230 return; 231 if (nsctlerrmap[cmd] == 0) 232 return; /* XXX */ 233 type = NS_ERR_UNREACH_HOST; 234 if (cmd == PRC_IFDOWN) 235 ns = &((struct sockaddr_ns *)arg)->sns_addr; 236 else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH) 237 ns = (struct ns_addr *)arg; 238 else { 239 errp = (struct ns_errp *)arg; 240 ns = &errp->ns_err_idp.idp_dna; 241 type = errp->ns_err_num; 242 type = ntohs(type); 243 } 244 switch (type) { 245 246 case NS_ERR_UNREACH_HOST: 247 ns_pcbnotify(ns, (int)nsctlerrmap[cmd], idp_abort, 0); 248 break; 249 250 case NS_ERR_NOSOCK: 251 nsp = ns_pcblookup(ns, errp->ns_err_idp.idp_sna.x_port, 252 NS_WILDCARD); 253 if(nsp && idp_donosocks && ! ns_nullhost(nsp->nsp_faddr)) 254 idp_drop(nsp, (int)nsctlerrmap[cmd]); 255 } 256 } 257 258 int idpprintfs = 0; 259 int idpforwarding = 1; 260 /* 261 * Forward a packet. If some error occurs return the sender 262 * an error packet. Note we can't always generate a meaningful 263 * error message because the NS errors don't have a large enough repetoire 264 * of codes and types. 265 */ 266 struct route idp_droute; 267 struct route idp_sroute; 268 269 idp_forward(idp) 270 register struct idp *idp; 271 { 272 register int error, type, code; 273 struct mbuf *mcopy = NULL; 274 int agedelta = 1; 275 int flags = NS_FORWARDING; 276 int ok_there = 0; 277 int ok_back = 0; 278 279 if (idpprintfs) { 280 printf("forward: src "); 281 ns_printhost(&idp->idp_sna); 282 printf(", dst "); 283 ns_printhost(&idp->idp_dna); 284 printf("hop count %d\n", idp->idp_tc); 285 } 286 if (idpforwarding == 0) { 287 /* can't tell difference between net and host */ 288 type = NS_ERR_UNREACH_HOST, code = 0; 289 goto senderror; 290 } 291 idp->idp_tc++; 292 if (idp->idp_tc > NS_MAXHOPS) { 293 type = NS_ERR_TOO_OLD, code = 0; 294 goto senderror; 295 } 296 /* 297 * Save at most 42 bytes of the packet in case 298 * we need to generate an NS error message to the src. 299 */ 300 mcopy = m_copy(dtom(idp), 0, imin(ntohs(idp->idp_len), 42)); 301 302 if ((ok_there = idp_do_route(&idp->idp_dna,&idp_droute))==0) { 303 type = NS_ERR_UNREACH_HOST, code = 0; 304 goto senderror; 305 } 306 /* 307 * Here we think about forwarding broadcast packets, 308 * so we try to insure that it doesn't go back out 309 * on the interface it came in on. Also, if we 310 * are going to physically broadcast this, let us 311 * age the packet so we can eat it safely the second time around. 312 */ 313 if (idp->idp_dna.x_host.c_host[0] & 0x1) { 314 struct ns_ifaddr *ia = ns_iaonnetof(idp->idp_dna.x_net); 315 struct ifnet *ifp; 316 if (ia) { 317 /* I'm gonna hafta eat this packet */ 318 agedelta += NS_MAXHOPS - idp->idp_tc; 319 idp->idp_tc = NS_MAXHOPS; 320 } 321 if ((ok_back = idp_do_route(&idp->idp_sna,&idp_sroute))==0) { 322 /* error = ENETUNREACH; He'll never get it! */ 323 m_freem(dtom(idp)); 324 goto cleanup; 325 } 326 if (idp_droute.ro_rt && 327 (ifp=idp_droute.ro_rt->rt_ifp) && 328 idp_sroute.ro_rt && 329 (ifp!=idp_sroute.ro_rt->rt_ifp)) { 330 flags |= NS_ALLOWBROADCAST; 331 } else { 332 type = NS_ERR_UNREACH_HOST, code = 0; 333 goto senderror; 334 } 335 } 336 /* need to adjust checksum */ 337 if (idp->idp_sum!=0xffff) { 338 union bytes { 339 u_char c[4]; 340 u_short s[2]; 341 long l; 342 } x; 343 register int shift; 344 x.l = 0; x.c[0] = agedelta; 345 shift = (((((int)ntohs(idp->idp_len))+1)>>1)-2) & 0xf; 346 x.l = idp->idp_sum + (x.l << shift); 347 x.l = x.s[0] + x.s[1]; 348 x.l = x.s[0] + x.s[1]; 349 if (x.l==0xffff) idp->idp_sum = 0; else idp->idp_sum = x.l; 350 } 351 if ((error = ns_output(dtom(idp), &idp_droute, flags)) && 352 (mcopy!=NULL)) { 353 idp = mtod(mcopy, struct idp *); 354 type = NS_ERR_UNSPEC_T, code = 0; 355 switch (error) { 356 357 case ENETUNREACH: 358 case EHOSTDOWN: 359 case EHOSTUNREACH: 360 case ENETDOWN: 361 case EPERM: 362 type = NS_ERR_UNREACH_HOST; 363 break; 364 365 case EMSGSIZE: 366 type = NS_ERR_TOO_BIG; 367 code = 576; /* too hard to figure out mtu here */ 368 break; 369 370 case ENOBUFS: 371 type = NS_ERR_UNSPEC_T; 372 break; 373 } 374 mcopy = NULL; 375 senderror: 376 ns_error(dtom(idp), type, code); 377 } 378 cleanup: 379 if (ok_there) 380 idp_undo_route(&idp_droute); 381 if (ok_back) 382 idp_undo_route(&idp_sroute); 383 if (mcopy != NULL) 384 m_freem(mcopy); 385 } 386 387 idp_do_route(src, ro) 388 struct ns_addr *src; 389 struct route *ro; 390 { 391 392 struct sockaddr_ns *dst; 393 394 bzero((caddr_t)ro, sizeof (*ro)); 395 dst = (struct sockaddr_ns *)&ro->ro_dst; 396 397 dst->sns_family = AF_NS; 398 dst->sns_addr = *src; 399 dst->sns_addr.x_port = 0; 400 rtalloc(ro); 401 if (ro->ro_rt == 0 || ro->ro_rt->rt_ifp == 0) { 402 return (0); 403 } 404 ro->ro_rt->rt_use++; 405 return (1); 406 } 407 408 idp_undo_route(ro) 409 register struct route *ro; 410 { 411 if (ro->ro_rt) {RTFREE(ro->ro_rt);} 412 } 413 ns_watch_output(m) 414 struct mbuf *m; 415 { 416 register struct nspcb *nsp; 417 /* 418 * Give any raw listeners a crack at the packet 419 */ 420 for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) { 421 struct mbuf *m1 = m_copy(m, 0, M_COPYALL); 422 if (m1) idp_input(m1, nsp); 423 } 424 } 425