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