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.8 (Berkeley) 08/09/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, (int)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 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 if (cmd == PRC_IFDOWN) 236 ns = &((struct sockaddr_ns *)arg)->sns_addr; 237 else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH) 238 ns = (struct ns_addr *)arg; 239 else { 240 errp = (struct ns_errp *)arg; 241 ns = &errp->ns_err_idp.idp_dna; 242 type = errp->ns_err_num; 243 type = ntohs((u_short)type); 244 } 245 switch (type) { 246 247 case NS_ERR_UNREACH_HOST: 248 ns_pcbnotify(ns, (int)nsctlerrmap[cmd], idp_abort, (long)0); 249 break; 250 251 case NS_ERR_NOSOCK: 252 nsp = ns_pcblookup(ns, errp->ns_err_idp.idp_sna.x_port, 253 NS_WILDCARD); 254 if(nsp && idp_donosocks && ! ns_nullhost(nsp->nsp_faddr)) 255 (void) idp_drop(nsp, (int)nsctlerrmap[cmd]); 256 } 257 } 258 259 int idpprintfs = 0; 260 int idpforwarding = 1; 261 /* 262 * Forward a packet. If some error occurs return the sender 263 * an error packet. Note we can't always generate a meaningful 264 * error message because the NS errors don't have a large enough repetoire 265 * of codes and types. 266 */ 267 struct route idp_droute; 268 struct route idp_sroute; 269 270 idp_forward(idp) 271 register struct idp *idp; 272 { 273 register int error, type, code; 274 struct mbuf *mcopy = NULL; 275 int agedelta = 1; 276 int flags = NS_FORWARDING; 277 int ok_there = 0; 278 int ok_back = 0; 279 280 if (idpprintfs) { 281 printf("forward: src "); 282 ns_printhost(&idp->idp_sna); 283 printf(", dst "); 284 ns_printhost(&idp->idp_dna); 285 printf("hop count %d\n", idp->idp_tc); 286 } 287 if (idpforwarding == 0) { 288 /* can't tell difference between net and host */ 289 type = NS_ERR_UNREACH_HOST, code = 0; 290 goto senderror; 291 } 292 idp->idp_tc++; 293 if (idp->idp_tc > NS_MAXHOPS) { 294 type = NS_ERR_TOO_OLD, code = 0; 295 goto senderror; 296 } 297 /* 298 * Save at most 42 bytes of the packet in case 299 * we need to generate an NS error message to the src. 300 */ 301 mcopy = m_copy(dtom(idp), 0, imin(ntohs(idp->idp_len), 42)); 302 303 if ((ok_there = idp_do_route(&idp->idp_dna,&idp_droute))==0) { 304 type = NS_ERR_UNREACH_HOST, code = 0; 305 goto senderror; 306 } 307 /* 308 * Here we think about forwarding broadcast packets, 309 * so we try to insure that it doesn't go back out 310 * on the interface it came in on. Also, if we 311 * are going to physically broadcast this, let us 312 * age the packet so we can eat it safely the second time around. 313 */ 314 if (idp->idp_dna.x_host.c_host[0] & 0x1) { 315 struct ns_ifaddr *ia = ns_iaonnetof(idp->idp_dna.x_net); 316 struct ifnet *ifp; 317 if (ia) { 318 /* I'm gonna hafta eat this packet */ 319 agedelta += NS_MAXHOPS - idp->idp_tc; 320 idp->idp_tc = NS_MAXHOPS; 321 } 322 if ((ok_back = idp_do_route(&idp->idp_sna,&idp_sroute))==0) { 323 /* error = ENETUNREACH; He'll never get it! */ 324 m_freem(dtom(idp)); 325 goto cleanup; 326 } 327 if (idp_droute.ro_rt && 328 (ifp=idp_droute.ro_rt->rt_ifp) && 329 idp_sroute.ro_rt && 330 (ifp!=idp_sroute.ro_rt->rt_ifp)) { 331 flags |= NS_ALLOWBROADCAST; 332 } else { 333 type = NS_ERR_UNREACH_HOST, code = 0; 334 goto senderror; 335 } 336 } 337 /* need to adjust checksum */ 338 if (idp->idp_sum!=0xffff) { 339 union bytes { 340 u_char c[4]; 341 u_short s[2]; 342 long l; 343 } x; 344 register int shift; 345 x.l = 0; x.c[0] = agedelta; 346 shift = (((((int)ntohs(idp->idp_len))+1)>>1)-2) & 0xf; 347 x.l = idp->idp_sum + (x.l << shift); 348 x.l = x.s[0] + x.s[1]; 349 x.l = x.s[0] + x.s[1]; 350 if (x.l==0xffff) idp->idp_sum = 0; else idp->idp_sum = x.l; 351 } 352 if ((error = ns_output(dtom(idp), &idp_droute, flags)) && 353 (mcopy!=NULL)) { 354 idp = mtod(mcopy, struct idp *); 355 type = NS_ERR_UNSPEC_T, code = 0; 356 switch (error) { 357 358 case ENETUNREACH: 359 case EHOSTDOWN: 360 case EHOSTUNREACH: 361 case ENETDOWN: 362 case EPERM: 363 type = NS_ERR_UNREACH_HOST; 364 break; 365 366 case EMSGSIZE: 367 type = NS_ERR_TOO_BIG; 368 code = 576; /* too hard to figure out mtu here */ 369 break; 370 371 case ENOBUFS: 372 type = NS_ERR_UNSPEC_T; 373 break; 374 } 375 mcopy = NULL; 376 senderror: 377 ns_error(dtom(idp), type, code); 378 } 379 cleanup: 380 if (ok_there) 381 idp_undo_route(&idp_droute); 382 if (ok_back) 383 idp_undo_route(&idp_sroute); 384 if (mcopy != NULL) 385 m_freem(mcopy); 386 } 387 388 idp_do_route(src, ro) 389 struct ns_addr *src; 390 struct route *ro; 391 { 392 393 struct sockaddr_ns *dst; 394 395 bzero((caddr_t)ro, sizeof (*ro)); 396 dst = (struct sockaddr_ns *)&ro->ro_dst; 397 398 dst->sns_family = AF_NS; 399 dst->sns_addr = *src; 400 dst->sns_addr.x_port = 0; 401 rtalloc(ro); 402 if (ro->ro_rt == 0 || ro->ro_rt->rt_ifp == 0) { 403 return (0); 404 } 405 ro->ro_rt->rt_use++; 406 return (1); 407 } 408 409 idp_undo_route(ro) 410 register struct route *ro; 411 { 412 if (ro->ro_rt) {RTFREE(ro->ro_rt);} 413 } 414 ns_watch_output(m) 415 struct mbuf *m; 416 { 417 register struct nspcb *nsp; 418 /* 419 * Give any raw listeners a crack at the packet 420 */ 421 for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) { 422 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL); 423 if (m1) idp_input(m1, nsp); 424 } 425 } 426