1 /* 2 * Copyright (c) 1984, 1985, 1986, 1987 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)ns_input.c 7.8 (Berkeley) 06/27/91 8 */ 9 10 #include "param.h" 11 #include "systm.h" 12 #include "malloc.h" 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; 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 54 ns_init() 55 { 56 extern struct timeval time; 57 58 ns_broadhost = * (union ns_host *) allones; 59 ns_broadnet = * (union ns_net *) allones; 60 nspcb.nsp_next = nspcb.nsp_prev = &nspcb; 61 nsrawpcb.nsp_next = nsrawpcb.nsp_prev = &nsrawpcb; 62 nsintrq.ifq_maxlen = nsqmaxlen; 63 ns_pexseq = time.tv_usec; 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; 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; 80 register struct nspcb *nsp; 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(); 91 IF_DEQUEUE(&nsintrq, m); 92 splx(s); 93 nsintr_getpck++; 94 if (m == 0) 95 return; 96 if ((m->m_flags & M_EXT || m->m_len < sizeof (struct idp)) && 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) { 106 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL); 107 if (m1) idp_input(m1, nsp); 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 */ 123 if (m->m_pkthdr.len < len) { 124 idpstat.idps_tooshort++; 125 goto bad; 126 } 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); 133 } 134 if (idpcksum && ((i = idp->idp_sum)!=0xffff)) { 135 idp->idp_sum = 0; 136 if (i != (idp->idp_sum = ns_cksum(m, len))) { 137 idpstat.idps_badsum++; 138 idp->idp_sum = i; 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)) { 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)) ) { 155 /* 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. 164 */ 165 if (idp->idp_tc < NS_MAXHOPS) { 166 idp_forward(m); 167 goto next; 168 } 169 } 170 /* 171 * Is this our packet? If not, forward. 172 */ 173 } else if (!ns_hosteqnh(ns_thishost,idp->idp_dna.x_host)) { 174 idp_forward(m); 175 goto next; 176 } 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) { 187 m_adj(m, -1); 188 } 189 if ((nsp->nsp_flags & NSP_ALL_PACKETS)==0) 190 switch (idp->idp_pt) { 191 192 case NSPROTO_SPP: 193 spp_input(m, nsp); 194 goto next; 195 196 case NSPROTO_ERROR: 197 ns_err_input(m); 198 goto next; 199 } 200 idp_input(m, nsp); 201 } else { 202 ns_error(m, NS_ERR_NOSOCK, 0); 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 219 int idp_donosocks = 1; 220 221 idp_ctlinput(cmd, arg) 222 int cmd; 223 caddr_t arg; 224 { 225 struct ns_addr *ns; 226 struct nspcb *nsp; 227 struct ns_errp *errp; 228 int idp_abort(); 229 extern struct nspcb *idp_drop(); 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; 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; 244 if (sns->sns_family != AF_NS) 245 return; 246 ns = &sns->sns_addr; 247 break; 248 249 default: 250 errp = (struct ns_errp *)arg; 251 ns = &errp->ns_err_idp.idp_dna; 252 type = errp->ns_err_num; 253 type = ntohs((u_short)type); 254 } 255 switch (type) { 256 257 case NS_ERR_UNREACH_HOST: 258 ns_pcbnotify(ns, (int)nsctlerrmap[cmd], idp_abort, (long)0); 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)) 265 (void) idp_drop(nsp, (int)nsctlerrmap[cmd]); 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 280 idp_forward(m) 281 struct mbuf *m; 282 { 283 register struct idp *idp = mtod(m, struct idp *); 284 register int error, type, code; 285 struct mbuf *mcopy = NULL; 286 int agedelta = 1; 287 int flags = NS_FORWARDING; 288 int ok_there = 0; 289 int ok_back = 0; 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 } 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 */ 312 mcopy = m_copy(m, 0, imin((int)ntohs(idp->idp_len), 42)); 313 314 if ((ok_there = idp_do_route(&idp->idp_dna,&idp_droute))==0) { 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) { 326 struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna); 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! */ 335 m_freem(m); 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 } 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; 356 x.l = 0; x.c[0] = agedelta; 357 shift = (((((int)ntohs(idp->idp_len))+1)>>1)-2) & 0xf; 358 x.l = idp->idp_sum + (x.s[0] << shift); 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 } 363 if ((error = ns_output(m, &idp_droute, flags)) && 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; 376 377 case EMSGSIZE: 378 type = NS_ERR_TOO_BIG; 379 code = 576; /* too hard to figure out mtu here */ 380 break; 381 382 case ENOBUFS: 383 type = NS_ERR_UNSPEC_T; 384 break; 385 } 386 mcopy = NULL; 387 senderror: 388 ns_error(m, type, code); 389 } 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); 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 409 dst->sns_len = sizeof(*dst); 410 dst->sns_family = AF_NS; 411 dst->sns_addr = *src; 412 dst->sns_addr.x_port = 0; 413 rtalloc(ro); 414 if (ro->ro_rt == 0 || ro->ro_rt->rt_ifp == 0) { 415 return (0); 416 } 417 ro->ro_rt->rt_use++; 418 return (1); 419 } 420 421 idp_undo_route(ro) 422 register struct route *ro; 423 { 424 if (ro->ro_rt) {RTFREE(ro->ro_rt);} 425 } 426 427 ns_watch_output(m, ifp) 428 struct mbuf *m; 429 struct ifnet *ifp; 430 { 431 register struct nspcb *nsp; 432 register struct ifaddr *ifa; 433 /* 434 * Give any raw listeners a crack at the packet 435 */ 436 for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) { 437 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL); 438 if (m0) { 439 register struct idp *idp; 440 441 M_PREPEND(m0, sizeof (*idp), M_DONTWAIT); 442 if (m0 == NULL) 443 continue; 444 idp = mtod(m0, struct idp *); 445 idp->idp_sna.x_net = ns_zeronet; 446 idp->idp_sna.x_host = ns_thishost; 447 if (ifp && (ifp->if_flags & IFF_POINTOPOINT)) 448 for(ifa = ifp->if_addrlist; ifa; 449 ifa = ifa->ifa_next) { 450 if (ifa->ifa_addr->sa_family==AF_NS) { 451 idp->idp_sna = IA_SNS(ifa)->sns_addr; 452 break; 453 } 454 } 455 idp->idp_len = ntohl(m0->m_pkthdr.len); 456 idp_input(m0, nsp); 457 } 458 } 459 } 460