Home | History | Annotate | Line # | Download | only in netinet
in_pcb.c revision 1.7
      1 /*
      2  * Copyright (c) 1982, 1986, 1991 Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	from: @(#)in_pcb.c	7.14 (Berkeley) 4/20/91
     34  *	$Id: in_pcb.c,v 1.7 1993/12/18 00:41:53 mycroft Exp $
     35  */
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/malloc.h>
     40 #include <sys/mbuf.h>
     41 #include <sys/protosw.h>
     42 #include <sys/socket.h>
     43 #include <sys/socketvar.h>
     44 #include <sys/ioctl.h>
     45 
     46 #include <net/if.h>
     47 #include <net/route.h>
     48 
     49 #include <netinet/in.h>
     50 #include <netinet/in_systm.h>
     51 #include <netinet/ip.h>
     52 #include <netinet/in_pcb.h>
     53 #include <netinet/in_var.h>
     54 #ifdef MULTICAST
     55 #include <netinet/ip_var.h>
     56 #endif
     57 
     58 struct	in_addr zeroin_addr;
     59 
     60 in_pcballoc(so, head)
     61 	struct socket *so;
     62 	struct inpcb *head;
     63 {
     64 	struct mbuf *m;
     65 	register struct inpcb *inp;
     66 
     67 	m = m_getclr(M_DONTWAIT, MT_PCB);
     68 	if (m == NULL)
     69 		return (ENOBUFS);
     70 	inp = mtod(m, struct inpcb *);
     71 	inp->inp_head = head;
     72 	inp->inp_socket = so;
     73 	insque(inp, head);
     74 	so->so_pcb = (caddr_t)inp;
     75 	return (0);
     76 }
     77 
     78 in_pcbbind(inp, nam)
     79 	register struct inpcb *inp;
     80 	struct mbuf *nam;
     81 {
     82 	register struct socket *so = inp->inp_socket;
     83 	register struct inpcb *head = inp->inp_head;
     84 	register struct sockaddr_in *sin;
     85 	u_short lport = 0;
     86 	int wild = 0;
     87 
     88 	if (in_ifaddr == 0)
     89 		return (EADDRNOTAVAIL);
     90 	if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
     91 		return (EINVAL);
     92 
     93 	if ((so->so_options & SO_REUSEADDR) == 0 &&
     94 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
     95 	     (so->so_options & SO_ACCEPTCONN) == 0))
     96 		wild = INPLOOKUP_WILDCARD;
     97 
     98 	if (nam == 0)
     99 		goto noname;
    100 	sin = mtod(nam, struct sockaddr_in *);
    101 	if (nam->m_len != sizeof (*sin))
    102 		return (EINVAL);
    103 	if (sin->sin_addr.s_addr != INADDR_ANY) {
    104 		int tport = sin->sin_port;
    105 
    106 		sin->sin_port = 0;		/* yech... */
    107 		if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
    108 			return (EADDRNOTAVAIL);
    109 		sin->sin_port = tport;
    110 	}
    111 	lport = sin->sin_port;
    112 	if (lport) {
    113 		u_short aport = ntohs(lport);
    114 
    115 		/* GROSS */
    116 		if (aport < IPPORT_RESERVED && (so->so_state & SS_PRIV) == 0)
    117 			return (EACCES);
    118 		if (in_pcblookup(head,
    119 		    zeroin_addr, 0, sin->sin_addr, lport, wild))
    120 			return (EADDRINUSE);
    121 	}
    122 	inp->inp_laddr = sin->sin_addr;
    123 noname:
    124 	if (lport == 0)
    125 		do {
    126 			if (head->inp_lport++ < IPPORT_RESERVED ||
    127 			    head->inp_lport > IPPORT_USERRESERVED)
    128 				head->inp_lport = IPPORT_RESERVED;
    129 			lport = htons(head->inp_lport);
    130 		} while (in_pcblookup(head,
    131 			    zeroin_addr, 0, inp->inp_laddr, lport, wild));
    132 	inp->inp_lport = lport;
    133 	return (0);
    134 }
    135 
    136 /*
    137  * Connect from a socket to a specified address.
    138  * Both address and port must be specified in argument sin.
    139  * If don't have a local address for this socket yet,
    140  * then pick one.
    141  */
    142 in_pcbconnect(inp, nam)
    143 	register struct inpcb *inp;
    144 	struct mbuf *nam;
    145 {
    146 	struct in_ifaddr *ia;
    147 	struct sockaddr_in *ifaddr;
    148 	register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
    149 
    150 	if (nam->m_len != sizeof (*sin))
    151 		return (EINVAL);
    152 	if (sin->sin_family != AF_INET)
    153 		return (EAFNOSUPPORT);
    154 	if (sin->sin_port == 0)
    155 		return (EADDRNOTAVAIL);
    156 	if (in_ifaddr) {
    157 		/*
    158 		 * If the destination address is INADDR_ANY,
    159 		 * use the primary local address.
    160 		 * If the supplied address is INADDR_BROADCAST,
    161 		 * and the primary interface supports broadcast,
    162 		 * choose the broadcast address for that interface.
    163 		 */
    164 #define	satosin(sa)	((struct sockaddr_in *)(sa))
    165 		if (sin->sin_addr.s_addr == INADDR_ANY)
    166 		    sin->sin_addr = IA_SIN(in_ifaddr)->sin_addr;
    167 		else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
    168 		  (in_ifaddr->ia_ifp->if_flags & IFF_BROADCAST))
    169 		    sin->sin_addr = satosin(&in_ifaddr->ia_broadaddr)->sin_addr;
    170 	}
    171 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
    172 		register struct route *ro;
    173 		struct ifnet *ifp;
    174 
    175 		ia = (struct in_ifaddr *)0;
    176 		/*
    177 		 * If route is known or can be allocated now,
    178 		 * our src addr is taken from the i/f, else punt.
    179 		 */
    180 		ro = &inp->inp_route;
    181 		if (ro->ro_rt &&
    182 		    (satosin(&ro->ro_dst)->sin_addr.s_addr !=
    183 			sin->sin_addr.s_addr ||
    184 		    inp->inp_socket->so_options & SO_DONTROUTE)) {
    185 			RTFREE(ro->ro_rt);
    186 			ro->ro_rt = (struct rtentry *)0;
    187 		}
    188 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
    189 		    (ro->ro_rt == (struct rtentry *)0 ||
    190 		    ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
    191 			/* No route yet, so try to acquire one */
    192 			ro->ro_dst.sa_family = AF_INET;
    193 			ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
    194 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
    195 				sin->sin_addr;
    196 			rtalloc(ro);
    197 		}
    198 		/*
    199 		 * If we found a route, use the address
    200 		 * corresponding to the outgoing interface
    201 		 * unless it is the loopback (in case a route
    202 		 * to our address on another net goes to loopback).
    203 		 */
    204 		if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp) &&
    205 		    (ifp->if_flags & IFF_LOOPBACK) == 0)
    206 			for (ia = in_ifaddr; ia; ia = ia->ia_next)
    207 				if (ia->ia_ifp == ifp)
    208 					break;
    209 		if (ia == 0) {
    210 			int fport = sin->sin_port;
    211 
    212 			sin->sin_port = 0;
    213 			ia = (struct in_ifaddr *)
    214 			    ifa_ifwithdstaddr((struct sockaddr *)sin);
    215 			sin->sin_port = fport;
    216 			if (ia == 0)
    217 				ia = in_iaonnetof(in_netof(sin->sin_addr));
    218 			if (ia == 0)
    219 				ia = in_ifaddr;
    220 			if (ia == 0)
    221 				return (EADDRNOTAVAIL);
    222 		}
    223 #ifdef MULTICAST
    224 		/*
    225 		 * If the destination address is multicast and an outgoing
    226 		 * interface has been set as a multicast option, use the
    227 		 * address of that interface as our source address.
    228 		 */
    229 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
    230 		    inp->inp_moptions != NULL) {
    231 			struct ip_moptions *imo;
    232 			struct ifnet *ifp;
    233 
    234 			imo = inp->inp_moptions;
    235 			if (imo->imo_multicast_ifp != NULL) {
    236 				ifp = imo->imo_multicast_ifp;
    237 				for (ia = in_ifaddr; ia; ia = ia->ia_next)
    238 					if (ia->ia_ifp == ifp)
    239 						break;
    240 				if (ia == 0)
    241 					return (EADDRNOTAVAIL);
    242 			}
    243 		}
    244 #endif
    245 		ifaddr = (struct sockaddr_in *)&ia->ia_addr;
    246 	}
    247 	if (in_pcblookup(inp->inp_head,
    248 	    sin->sin_addr,
    249 	    sin->sin_port,
    250 	    inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
    251 	    inp->inp_lport,
    252 	    0))
    253 		return (EADDRINUSE);
    254 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
    255 		if (inp->inp_lport == 0)
    256 			(void)in_pcbbind(inp, (struct mbuf *)0);
    257 		inp->inp_laddr = ifaddr->sin_addr;
    258 	}
    259 	inp->inp_faddr = sin->sin_addr;
    260 	inp->inp_fport = sin->sin_port;
    261 	return (0);
    262 }
    263 
    264 in_pcbdisconnect(inp)
    265 	struct inpcb *inp;
    266 {
    267 
    268 	inp->inp_faddr.s_addr = INADDR_ANY;
    269 	inp->inp_fport = 0;
    270 	if (inp->inp_socket->so_state & SS_NOFDREF)
    271 		in_pcbdetach(inp);
    272 }
    273 
    274 in_pcbdetach(inp)
    275 	struct inpcb *inp;
    276 {
    277 	struct socket *so = inp->inp_socket;
    278 
    279 	so->so_pcb = 0;
    280 	sofree(so);
    281 	if (inp->inp_options)
    282 		(void)m_free(inp->inp_options);
    283 	if (inp->inp_route.ro_rt)
    284 		rtfree(inp->inp_route.ro_rt);
    285 #ifdef MULTICAST
    286 	ip_freemoptions(inp->inp_moptions);
    287 #endif
    288 	remque(inp);
    289 	(void) m_free(dtom(inp));
    290 }
    291 
    292 in_setsockaddr(inp, nam)
    293 	register struct inpcb *inp;
    294 	struct mbuf *nam;
    295 {
    296 	register struct sockaddr_in *sin;
    297 
    298 	nam->m_len = sizeof (*sin);
    299 	sin = mtod(nam, struct sockaddr_in *);
    300 	bzero((caddr_t)sin, sizeof (*sin));
    301 	sin->sin_family = AF_INET;
    302 	sin->sin_len = sizeof(*sin);
    303 	sin->sin_port = inp->inp_lport;
    304 	sin->sin_addr = inp->inp_laddr;
    305 }
    306 
    307 in_setpeeraddr(inp, nam)
    308 	struct inpcb *inp;
    309 	struct mbuf *nam;
    310 {
    311 	register struct sockaddr_in *sin;
    312 
    313 	nam->m_len = sizeof (*sin);
    314 	sin = mtod(nam, struct sockaddr_in *);
    315 	bzero((caddr_t)sin, sizeof (*sin));
    316 	sin->sin_family = AF_INET;
    317 	sin->sin_len = sizeof(*sin);
    318 	sin->sin_port = inp->inp_fport;
    319 	sin->sin_addr = inp->inp_faddr;
    320 }
    321 
    322 /*
    323  * Pass some notification to all connections of a protocol
    324  * associated with address dst.  The local address and/or port numbers
    325  * may be specified to limit the search.  The "usual action" will be
    326  * taken, depending on the ctlinput cmd.  The caller must filter any
    327  * cmds that are uninteresting (e.g., no error in the map).
    328  * Call the protocol specific routine (if any) to report
    329  * any errors for each matching socket.
    330  *
    331  * Must be called at splnet.
    332  */
    333 in_pcbnotify(head, dst, fport, laddr, lport, cmd, notify)
    334 	struct inpcb *head;
    335 	struct sockaddr *dst;
    336 	u_short fport, lport;
    337 	struct in_addr laddr;
    338 	int cmd, (*notify)();
    339 {
    340 	register struct inpcb *inp, *oinp;
    341 	struct in_addr faddr;
    342 	int errno;
    343 	int in_rtchange();
    344 	extern u_char inetctlerrmap[];
    345 
    346 	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET)
    347 		return;
    348 	faddr = ((struct sockaddr_in *)dst)->sin_addr;
    349 	if (faddr.s_addr == INADDR_ANY)
    350 		return;
    351 
    352 	/*
    353 	 * Redirects go to all references to the destination,
    354 	 * and use in_rtchange to invalidate the route cache.
    355 	 * Dead host indications: notify all references to the destination.
    356 	 * Otherwise, if we have knowledge of the local port and address,
    357 	 * deliver only to that socket.
    358 	 */
    359 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
    360 		fport = 0;
    361 		lport = 0;
    362 		laddr.s_addr = 0;
    363 		if (cmd != PRC_HOSTDEAD)
    364 			notify = in_rtchange;
    365 	}
    366 	errno = inetctlerrmap[cmd];
    367 	for (inp = head->inp_next; inp != head;) {
    368 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
    369 		    inp->inp_socket == 0 ||
    370 		    (lport && inp->inp_lport != lport) ||
    371 		    (laddr.s_addr && inp->inp_laddr.s_addr != laddr.s_addr) ||
    372 		    (fport && inp->inp_fport != fport)) {
    373 			inp = inp->inp_next;
    374 			continue;
    375 		}
    376 		oinp = inp;
    377 		inp = inp->inp_next;
    378 		if (notify)
    379 			(*notify)(oinp, errno);
    380 	}
    381 }
    382 
    383 /*
    384  * Check for alternatives when higher level complains
    385  * about service problems.  For now, invalidate cached
    386  * routing information.  If the route was created dynamically
    387  * (by a redirect), time to try a default gateway again.
    388  */
    389 in_losing(inp)
    390 	struct inpcb *inp;
    391 {
    392 	register struct rtentry *rt;
    393 
    394 	if ((rt = inp->inp_route.ro_rt)) {
    395 		rt_missmsg(RTM_LOSING, &inp->inp_route.ro_dst,
    396 			    rt->rt_gateway, (struct sockaddr *)rt_mask(rt),
    397 			    (struct sockaddr *)0, rt->rt_flags, 0);
    398 		if (rt->rt_flags & RTF_DYNAMIC)
    399 			(void) rtrequest(RTM_DELETE, rt_key(rt),
    400 				rt->rt_gateway, rt_mask(rt), rt->rt_flags,
    401 				(struct rtentry **)0);
    402 		inp->inp_route.ro_rt = 0;
    403 		rtfree(rt);
    404 		/*
    405 		 * A new route can be allocated
    406 		 * the next time output is attempted.
    407 		 */
    408 	}
    409 }
    410 
    411 /*
    412  * After a routing change, flush old routing
    413  * and allocate a (hopefully) better one.
    414  */
    415 in_rtchange(inp)
    416 	register struct inpcb *inp;
    417 {
    418 	if (inp->inp_route.ro_rt) {
    419 		rtfree(inp->inp_route.ro_rt);
    420 		inp->inp_route.ro_rt = 0;
    421 		/*
    422 		 * A new route can be allocated the next time
    423 		 * output is attempted.
    424 		 */
    425 	}
    426 }
    427 
    428 struct inpcb *
    429 in_pcblookup(head, faddr, fport, laddr, lport, flags)
    430 	struct inpcb *head;
    431 	struct in_addr faddr, laddr;
    432 	u_short fport, lport;
    433 	int flags;
    434 {
    435 	register struct inpcb *inp, *match = 0;
    436 	int matchwild = 3, wildcard;
    437 
    438 	for (inp = head->inp_next; inp != head; inp = inp->inp_next) {
    439 		if (inp->inp_lport != lport)
    440 			continue;
    441 		wildcard = 0;
    442 		if (inp->inp_laddr.s_addr != INADDR_ANY) {
    443 			if (laddr.s_addr == INADDR_ANY)
    444 				wildcard++;
    445 			else if (inp->inp_laddr.s_addr != laddr.s_addr)
    446 				continue;
    447 		} else {
    448 			if (laddr.s_addr != INADDR_ANY)
    449 				wildcard++;
    450 		}
    451 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
    452 			if (faddr.s_addr == INADDR_ANY)
    453 				wildcard++;
    454 			else if (inp->inp_faddr.s_addr != faddr.s_addr ||
    455 			    inp->inp_fport != fport)
    456 				continue;
    457 		} else {
    458 			if (faddr.s_addr != INADDR_ANY)
    459 				wildcard++;
    460 		}
    461 		if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
    462 			continue;
    463 		if (wildcard < matchwild) {
    464 			match = inp;
    465 			matchwild = wildcard;
    466 			if (matchwild == 0)
    467 				break;
    468 		}
    469 	}
    470 	return (match);
    471 }
    472