Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/sys/netinet/ip_input.c,v retrieving revision 1.74.2.1 retrieving revision 1.75 diff -u -p -r1.74.2.1 -r1.75 --- src/sys/netinet/ip_input.c 1998/12/11 04:53:08 1.74.2.1 +++ src/sys/netinet/ip_input.c 1998/12/18 21:35:11 1.75 @@ -1,4 +1,4 @@ -/* $NetBSD: ip_input.c,v 1.74.2.1 1998/12/11 04:53:08 kenh Exp $ */ +/* $NetBSD: ip_input.c,v 1.75 1998/12/18 21:35:11 thorpej Exp $ */ /* * Copyright (c) 1982, 1986, 1988, 1993 @@ -164,7 +164,59 @@ struct ifqueue ipintrq; struct ipstat ipstat; u_int16_t ip_id; int ip_defttl; + struct ipqhead ipq; +int ipq_locked; + +static __inline int ipq_lock_try __P((void)); +static __inline void ipq_unlock __P((void)); + +static __inline int +ipq_lock_try() +{ + int s; + + s = splimp(); + if (ipq_locked) { + splx(s); + return (0); + } + ipq_locked = 1; + splx(s); + return (1); +} + +static __inline void +ipq_unlock() +{ + int s; + + s = splimp(); + ipq_locked = 0; + splx(s); +} + +#ifdef DIAGNOSTIC +#define IPQ_LOCK() \ +do { \ + if (ipq_lock_try() == 0) { \ + printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \ + panic("ipq_lock"); \ + } \ +} while (0) +#define IPQ_LOCK_CHECK() \ +do { \ + if (ipq_locked == 0) { \ + printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \ + panic("ipq lock check"); \ + } \ +} while (0) +#else +#define IPQ_LOCK() (void) ipq_lock_try() +#define IPQ_LOCK_CHECK() /* nothing */ +#endif + +#define IPQ_UNLOCK() ipq_unlock() struct pool ipqent_pool; @@ -359,22 +411,13 @@ next: /* * Check our list of addresses, to see if the packet is for us. */ - s = splimp(); INADDR_TO_IA(ip->ip_dst, ia); - if (ia != NULL) { - ifa_delref(&ia->ia_ifa); - splx(s); - goto ours; - } + if (ia != NULL) goto ours; if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first; ifa != NULL; ifa = ifa->ifa_list.tqe_next) { if (ifa->ifa_addr->sa_family != AF_INET) continue; ia = ifatoia(ifa); - /* - * Don't ifa_addref ia as we don't use it after - * we splx(s) below - */ if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) || in_hosteq(ip->ip_dst, ia->ia_netbroadcast) || /* @@ -382,21 +425,16 @@ next: * either for subnet or net. */ ip->ip_dst.s_addr == ia->ia_subnet || - ip->ip_dst.s_addr == ia->ia_net) { - splx(s); + ip->ip_dst.s_addr == ia->ia_net) goto ours; - } /* * An interface with IP address zero accepts * all packets that arrive on that interface. */ - if (in_nullhost(ia->ia_addr.sin_addr)) { - splx(s); + if (in_nullhost(ia->ia_addr.sin_addr)) goto ours; - } } } - splx(s); if (IN_MULTICAST(ip->ip_dst.s_addr)) { struct in_multi *inm; #ifdef MROUTING @@ -480,6 +518,7 @@ ours: * Look for queue of fragments * of this datagram. */ + IPQ_LOCK(); for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next) if (ip->ip_id == fp->ipq_id && in_hosteq(ip->ip_src, fp->ipq_src) && @@ -503,6 +542,7 @@ found: */ if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { ipstat.ips_badfrags++; + IPQ_UNLOCK(); goto bad; } } @@ -518,20 +558,24 @@ found: ipqe = pool_get(&ipqent_pool, PR_NOWAIT); if (ipqe == NULL) { ipstat.ips_rcvmemdrop++; + IPQ_UNLOCK(); goto bad; } ipqe->ipqe_mff = mff; ipqe->ipqe_m = m; ipqe->ipqe_ip = ip; m = ip_reass(ipqe, fp); - if (m == 0) + if (m == 0) { + IPQ_UNLOCK(); goto next; + } ipstat.ips_reassembled++; ip = mtod(m, struct ip *); hlen = ip->ip_hl << 2; } else if (fp) ip_freef(fp); + IPQ_UNLOCK(); } else ip->ip_len -= hlen; @@ -564,6 +608,8 @@ ip_reass(ipqe, fp) int hlen = ipqe->ipqe_ip->ip_hl << 2; int i, next; + IPQ_LOCK_CHECK(); + /* * Presence of header sizes in mbufs * would confuse code below. @@ -718,6 +764,8 @@ ip_freef(fp) { register struct ipqent *q, *p; + IPQ_LOCK_CHECK(); + for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) { p = q->ipqe_q.le_next; m_freem(q->ipqe_m); @@ -739,6 +787,7 @@ ip_slowtimo() register struct ipq *fp, *nfp; int s = splsoftnet(); + IPQ_LOCK(); for (fp = ipq.lh_first; fp != NULL; fp = nfp) { nfp = fp->ipq_q.le_next; if (--fp->ipq_ttl == 0) { @@ -746,6 +795,7 @@ ip_slowtimo() ip_freef(fp); } } + IPQ_UNLOCK(); #ifdef GATEWAY ipflow_slowtimo(); #endif @@ -759,10 +809,19 @@ void ip_drain() { + /* + * We may be called from a device's interrupt context. If + * the ipq is already busy, just bail out now. + */ + if (ipq_lock_try() == 0) + return; + while (ipq.lh_first != NULL) { ipstat.ips_fragdropped++; ip_freef(ipq.lh_first); } + + IPQ_UNLOCK(); } /* @@ -845,7 +904,6 @@ ip_dooptions(m) * End of source route. Should be for us. */ save_rte(cp, ip->ip_src); - ifa_delref(&ia->ia_ifa); break; } /* @@ -853,7 +911,6 @@ ip_dooptions(m) */ bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, sizeof(ipaddr.sin_addr)); - ifa_delref(&ia->ia_ifa); if (opt == IPOPT_SSRR) { #define INA struct in_ifaddr * #define SA struct sockaddr * @@ -873,7 +930,6 @@ ip_dooptions(m) * Let ip_intr's mcast routing check handle mcast pkts */ forward = !IN_MULTICAST(ip->ip_dst.s_addr); - ifa_delref(&ia->ia_ifa); break; case IPOPT_RR: @@ -902,7 +958,6 @@ ip_dooptions(m) bcopy((caddr_t)&ia->ia_addr.sin_addr, (caddr_t)(cp + off), sizeof(struct in_addr)); cp[IPOPT_OFFSET] += sizeof(struct in_addr); - ifa_delref(&ia->ia_ifa); break; case IPOPT_TS: @@ -932,7 +987,6 @@ ip_dooptions(m) continue; bcopy((caddr_t)&ia->ia_addr.sin_addr, (caddr_t)sin, sizeof(struct in_addr)); - ifa_delref(&ia->ia_ifa); ipt->ipt_ptr += sizeof(struct in_addr); break; @@ -942,10 +996,8 @@ ip_dooptions(m) goto bad; bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, sizeof(struct in_addr)); - ia = (INA)ifa_ifwithaddr((SA)&ipaddr); - if (ia == 0) + if (ifa_ifwithaddr((SA)&ipaddr) == 0) continue; - ifa_delref(&ia->ia_ifa); ipt->ipt_ptr += sizeof(struct in_addr); break; @@ -984,8 +1036,6 @@ ip_rtaddr(dst) struct in_addr dst; { register struct sockaddr_in *sin; - struct in_ifaddr *ia; - int s; sin = satosin(&ipforward_rt.ro_dst); @@ -1002,11 +1052,7 @@ ip_rtaddr(dst) } if (ipforward_rt.ro_rt == 0) return ((struct in_ifaddr *)0); - s = splimp(); - ia = (ifatoia(ipforward_rt.ro_rt->rt_ifa)); - ifa_addref(&ia->ia_ifa); - splx(s); - return ia; + return (ifatoia(ipforward_rt.ro_rt->rt_ifa)); } /*