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.5 (Berkeley) 06/21/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 48 ns_init() 49 { 50 ns_broadhost = * (union ns_host *) allones; 51 nspcb.nsp_next = nspcb.nsp_prev = &nspcb; 52 nsrawpcb.nsp_next = nsrawpcb.nsp_prev = &nsrawpcb; 53 nsintrq.ifq_maxlen = nsqmaxlen; 54 } 55 56 /* 57 * Idp input routine. Pass to next level. 58 */ 59 int nsintr_getpck = 0; 60 int nsintr_swtch = 0; 61 nsintr() 62 { 63 register struct idp *idp; 64 register struct mbuf *m; 65 struct nspcb *nsp; 66 struct mbuf *m0; 67 register int i; 68 int len, s, error; 69 char oddpacketp; 70 71 next: 72 /* 73 * Get next datagram off input queue and get IDP header 74 * in first mbuf. 75 */ 76 s = splimp(); 77 IF_DEQUEUE(&nsintrq, m); 78 splx(s); 79 nsintr_getpck++; 80 if (m == 0) 81 return; 82 if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct idp)) && 83 (m = m_pullup(m, sizeof (struct idp))) == 0) { 84 idpstat.idps_toosmall++; 85 goto next; 86 } 87 88 /* 89 * Give any raw listeners a crack at the packet 90 */ 91 for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) { 92 struct mbuf *m1 = m_copy(m, 0, M_COPYALL); 93 if (m1) idp_input(m1, nsp); 94 } 95 96 idp = mtod(m, struct idp *); 97 len = ntohs(idp->idp_len); 98 if (oddpacketp = len & 1) { 99 len++; /* If this packet is of odd length, 100 preserve garbage byte for checksum */ 101 } 102 103 /* 104 * Check that the amount of data in the buffers 105 * is as at least much as the IDP header would have us expect. 106 * Trim mbufs if longer than we expect. 107 * Drop packet if shorter than we expect. 108 */ 109 i = -len; 110 m0 = m; 111 for (;;) { 112 i += m->m_len; 113 if (m->m_next == 0) 114 break; 115 m = m->m_next; 116 } 117 if (i != 0) { 118 if (i < 0) { 119 idpstat.idps_tooshort++; 120 m = m0; 121 goto bad; 122 } 123 if (i <= m->m_len) 124 m->m_len -= i; 125 else 126 m_adj(m0, -i); 127 } 128 m = m0; 129 if (idpcksum && ((i = idp->idp_sum)!=0xffff)) { 130 idp->idp_sum = 0; 131 if (i != (idp->idp_sum = ns_cksum(m,len))) { 132 idpstat.idps_badsum++; 133 if (ns_hosteqnh(ns_thishost, idp->idp_dna.x_host)) 134 error = NS_ERR_BADSUM; 135 else 136 error = NS_ERR_BADSUM_T; 137 ns_error(m, error, 0); 138 goto next; 139 } 140 } 141 /* 142 * Is this a directed broadcast? 143 */ 144 if (ns_hosteqnh(ns_broadhost,idp->idp_dna.x_host)) { 145 if ((ns_netof(idp->idp_dna)!=ns_netof(idp->idp_sna)) && 146 (ns_netof(idp->idp_dna)!=-1) && (ns_netof(idp->idp_sna)!=0) 147 && (ns_netof(idp->idp_dna)!=0)) { 148 /* 149 * Look to see if I need to eat this packet. 150 * Algorithm is to forward all young packets 151 * and prematurely age any packets which will 152 * by physically broadcasted. 153 * Any very old packets eaten without forwarding 154 * would die anyway. 155 * 156 * Suggestion of Bill Nesheim, Cornell U. 157 */ 158 if (idp->idp_tc < NS_MAXHOPS) { 159 idp_forward(idp); 160 goto next; 161 } 162 } 163 /* 164 * Is this our packet? If not, forward. 165 */ 166 } else if (!ns_hosteqnh(ns_thishost,idp->idp_dna.x_host)) { 167 idp_forward(idp); 168 goto next; 169 } 170 171 /* 172 * Locate pcb for datagram. 173 */ 174 nsp = ns_pcblookup(&idp->idp_sna, idp->idp_dna.x_port, NS_WILDCARD); 175 176 177 /* 178 * Switch out to protocol's input routine. 179 */ 180 181 nsintr_swtch++; 182 if (nsp) { 183 if (oddpacketp) { 184 m_adj(m0, -1); 185 } 186 switch (idp->idp_pt) { 187 188 case NSPROTO_SPP: 189 spp_input(m,nsp); 190 break; 191 192 case NSPROTO_ERROR: 193 ns_err_input(m); 194 break; 195 default: 196 idp_input(m,nsp); 197 } 198 } else { 199 /* don't send ERROR response for multicast packet */ 200 if (idp->idp_dna.x_host.c_host[0] & 1) 201 goto bad; 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 idp_ctlinput(cmd, arg) 220 int cmd; 221 caddr_t arg; 222 { 223 struct ns_addr *ns; 224 int idp_abort(); 225 int type; 226 227 if (cmd < 0 || cmd > PRC_NCMDS) 228 return; 229 if (nsctlerrmap[cmd] == 0) 230 return; /* XXX */ 231 type = NS_ERR_UNREACH_HOST; 232 if (cmd == PRC_IFDOWN) 233 ns = &((struct sockaddr_ns *)arg)->sns_addr; 234 else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH) 235 ns = (struct ns_addr *)arg; 236 else { 237 ns = &((struct ns_errp *)arg)->ns_err_idp.idp_dna; 238 type = ((struct ns_errp *)arg)->ns_err_num; 239 type = ntohs(type); 240 } 241 switch (type) { 242 243 case NS_ERR_UNREACH_HOST: 244 case NS_ERR_NOSOCK: 245 ns_pcbnotify(ns, (int)nsctlerrmap[cmd], idp_abort, 0); 246 } 247 } 248 249 int idpprintfs = 0; 250 int idpforwarding = 1; 251 /* 252 * Forward a packet. If some error occurs return the sender 253 * an error packet. Note we can't always generate a meaningful 254 * error message because the NS errors don't have a large enough repetoire 255 * of codes and types. 256 */ 257 struct route idp_droute; 258 struct route idp_sroute; 259 260 idp_forward(idp) 261 register struct idp *idp; 262 { 263 register int error, type, code; 264 struct mbuf *mcopy = NULL; 265 int agedelta = 1; 266 int flags = NS_FORWARDING; 267 int ok_there = 0; 268 int ok_back = 0; 269 270 if (idpprintfs) { 271 printf("forward: src "); 272 ns_printhost(&idp->idp_sna); 273 printf(", dst "); 274 ns_printhost(&idp->idp_dna); 275 printf("hop count %d\n", idp->idp_tc); 276 } 277 if (idpforwarding == 0) { 278 /* can't tell difference between net and host */ 279 type = NS_ERR_UNREACH_HOST, code = 0; 280 goto senderror; 281 } 282 idp->idp_tc++; 283 if (idp->idp_tc > NS_MAXHOPS) { 284 type = NS_ERR_TOO_OLD, code = 0; 285 goto senderror; 286 } 287 /* 288 * Save at most 42 bytes of the packet in case 289 * we need to generate an NS error message to the src. 290 */ 291 mcopy = m_copy(dtom(idp), 0, imin(ntohs(idp->idp_len), 42)); 292 293 if ((ok_there = idp_do_route(&idp->idp_dna,&idp_droute))==0) { 294 type = NS_ERR_UNREACH_HOST, code = 0; 295 goto senderror; 296 } 297 /* 298 * Here we think about forwarding broadcast packets, 299 * so we try to insure that it doesn't go back out 300 * on the interface it came in on. Also, if we 301 * are going to physically broadcast this, let us 302 * age the packet so we can eat it safely the second time around. 303 */ 304 if (idp->idp_dna.x_host.c_host[0] & 0x1) { 305 struct ns_ifaddr *ia = ns_iaonnetof(idp->idp_dna.x_net); 306 struct ifnet *ifp; 307 if (ia) { 308 /* I'm gonna hafta eat this packet */ 309 agedelta += NS_MAXHOPS - idp->idp_tc; 310 idp->idp_tc = NS_MAXHOPS; 311 } 312 if ((ok_back = idp_do_route(&idp->idp_sna,&idp_sroute))==0) { 313 /* error = ENETUNREACH; He'll never get it! */ 314 m_freem(dtom(idp)); 315 goto cleanup; 316 } 317 if (idp_droute.ro_rt && 318 (ifp=idp_droute.ro_rt->rt_ifp) && 319 idp_sroute.ro_rt && 320 (ifp!=idp_sroute.ro_rt->rt_ifp)) { 321 flags |= NS_ALLOWBROADCAST; 322 } else { 323 type = NS_ERR_UNREACH_HOST, code = 0; 324 goto senderror; 325 } 326 } 327 /* need to adjust checksum */ 328 if (idp->idp_sum!=0xffff) { 329 union bytes { 330 u_char c[4]; 331 u_short s[2]; 332 long l; 333 } x; 334 register int shift; 335 x.l = 0; x.c[0] = agedelta; 336 shift = (((((int)ntohs(idp->idp_len))+1)>>1)-2) & 0xf; 337 x.l = idp->idp_sum + (x.l << shift); 338 x.l = x.s[0] + x.s[1]; 339 x.l = x.s[0] + x.s[1]; 340 if (x.l==0xffff) idp->idp_sum = 0; else idp->idp_sum = x.l; 341 } 342 if ((error = ns_output(dtom(idp), &idp_droute, flags)) && 343 (mcopy!=NULL)) { 344 idp = mtod(mcopy, struct idp *); 345 type = NS_ERR_UNSPEC_T, code = 0; 346 switch (error) { 347 348 case ENETUNREACH: 349 case EHOSTDOWN: 350 case EHOSTUNREACH: 351 case ENETDOWN: 352 case EPERM: 353 type = NS_ERR_UNREACH_HOST; 354 break; 355 356 case EMSGSIZE: 357 type = NS_ERR_TOO_BIG; 358 code = 576; /* too hard to figure out mtu here */ 359 break; 360 361 case ENOBUFS: 362 type = NS_ERR_UNSPEC_T; 363 break; 364 } 365 mcopy = NULL; 366 senderror: 367 ns_error(dtom(idp), type, code); 368 } 369 cleanup: 370 if (ok_there) 371 idp_undo_route(&idp_droute); 372 if (ok_back) 373 idp_undo_route(&idp_sroute); 374 if (mcopy != NULL) 375 m_freem(mcopy); 376 } 377 378 idp_do_route(src, ro) 379 struct ns_addr *src; 380 struct route *ro; 381 { 382 383 struct sockaddr_ns *dst; 384 385 bzero((caddr_t)ro, sizeof (*ro)); 386 dst = (struct sockaddr_ns *)&ro->ro_dst; 387 388 dst->sns_family = AF_NS; 389 dst->sns_addr = *src; 390 dst->sns_addr.x_port = 0; 391 rtalloc(ro); 392 if (ro->ro_rt == 0 || ro->ro_rt->rt_ifp == 0) { 393 return (0); 394 } 395 ro->ro_rt->rt_use++; 396 return (1); 397 } 398 399 idp_undo_route(ro) 400 register struct route *ro; 401 { 402 if (ro->ro_rt) {RTFREE(ro->ro_rt);} 403 } 404 ns_watch_output(m) 405 struct mbuf *m; 406 { 407 register struct nspcb *nsp; 408 /* 409 * Give any raw listeners a crack at the packet 410 */ 411 for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) { 412 struct mbuf *m1 = m_copy(m, 0, M_COPYALL); 413 if (m1) idp_input(m1, nsp); 414 } 415 } 416