in_pcb.c revision 1.10 1 /*
2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. 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 8.2 (Berkeley) 1/4/94
34 * $Id: in_pcb.c,v 1.10 1994/05/13 06:06:07 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 #include <sys/errno.h>
46 #include <sys/time.h>
47 #include <sys/proc.h>
48
49 #include <net/if.h>
50 #include <net/route.h>
51
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/in_pcb.h>
56 #include <netinet/in_var.h>
57 #include <netinet/ip_var.h>
58
59 struct in_addr zeroin_addr;
60
61 int
62 in_pcballoc(so, head)
63 struct socket *so;
64 struct inpcb *head;
65 {
66 register struct inpcb *inp;
67
68 MALLOC(inp, struct inpcb *, sizeof(*inp), M_PCB, M_WAITOK);
69 if (inp == NULL)
70 return (ENOBUFS);
71 bzero((caddr_t)inp, sizeof(*inp));
72 inp->inp_head = head;
73 inp->inp_socket = so;
74 insque(inp, head);
75 so->so_pcb = (caddr_t)inp;
76 return (0);
77 }
78
79 int
80 in_pcbbind(inp, nam)
81 register struct inpcb *inp;
82 struct mbuf *nam;
83 {
84 register struct socket *so = inp->inp_socket;
85 register struct inpcb *head = inp->inp_head;
86 register struct sockaddr_in *sin;
87 struct proc *p = curproc; /* XXX */
88 u_short lport = 0;
89 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
90 int error;
91
92 if (in_ifaddr == 0)
93 return (EADDRNOTAVAIL);
94 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
95 return (EINVAL);
96 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
97 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
98 (so->so_options & SO_ACCEPTCONN) == 0))
99 wild = INPLOOKUP_WILDCARD;
100 if (nam) {
101 sin = mtod(nam, struct sockaddr_in *);
102 if (nam->m_len != sizeof (*sin))
103 return (EINVAL);
104 #ifdef notdef
105 /*
106 * We should check the family, but old programs
107 * incorrectly fail to initialize it.
108 */
109 if (sin->sin_family != AF_INET)
110 return (EAFNOSUPPORT);
111 #endif
112 lport = sin->sin_port;
113 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
114 /*
115 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
116 * allow complete duplication of binding if
117 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
118 * and a multicast address is bound on both
119 * new and duplicated sockets.
120 */
121 if (so->so_options & SO_REUSEADDR)
122 reuseport = SO_REUSEADDR|SO_REUSEPORT;
123 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
124 sin->sin_port = 0; /* yech... */
125 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
126 return (EADDRNOTAVAIL);
127 }
128 if (lport) {
129 struct inpcb *t;
130
131 /* GROSS */
132 if (ntohs(lport) < IPPORT_RESERVED &&
133 (error = suser(p->p_ucred, &p->p_acflag)))
134 return (error);
135 t = in_pcblookup(head, zeroin_addr, 0,
136 sin->sin_addr, lport, wild);
137 if (t && (reuseport & t->inp_socket->so_options) == 0)
138 return (EADDRINUSE);
139 }
140 inp->inp_laddr = sin->sin_addr;
141 }
142 if (lport == 0)
143 do {
144 if (head->inp_lport++ < IPPORT_RESERVED ||
145 head->inp_lport > IPPORT_USERRESERVED)
146 head->inp_lport = IPPORT_RESERVED;
147 lport = htons(head->inp_lport);
148 } while (in_pcblookup(head,
149 zeroin_addr, 0, inp->inp_laddr, lport, wild));
150 inp->inp_lport = lport;
151 return (0);
152 }
153
154 /*
155 * Connect from a socket to a specified address.
156 * Both address and port must be specified in argument sin.
157 * If don't have a local address for this socket yet,
158 * then pick one.
159 */
160 int
161 in_pcbconnect(inp, nam)
162 register struct inpcb *inp;
163 struct mbuf *nam;
164 {
165 struct in_ifaddr *ia;
166 struct sockaddr_in *ifaddr;
167 register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
168
169 if (nam->m_len != sizeof (*sin))
170 return (EINVAL);
171 if (sin->sin_family != AF_INET)
172 return (EAFNOSUPPORT);
173 if (sin->sin_port == 0)
174 return (EADDRNOTAVAIL);
175 if (in_ifaddr) {
176 /*
177 * If the destination address is INADDR_ANY,
178 * use the primary local address.
179 * If the supplied address is INADDR_BROADCAST,
180 * and the primary interface supports broadcast,
181 * choose the broadcast address for that interface.
182 */
183 #define satosin(sa) ((struct sockaddr_in *)(sa))
184 #define sintosa(sin) ((struct sockaddr *)(sin))
185 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
186 if (sin->sin_addr.s_addr == INADDR_ANY)
187 sin->sin_addr = IA_SIN(in_ifaddr)->sin_addr;
188 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
189 (in_ifaddr->ia_ifp->if_flags & IFF_BROADCAST))
190 sin->sin_addr = satosin(&in_ifaddr->ia_broadaddr)->sin_addr;
191 }
192 if (inp->inp_laddr.s_addr == INADDR_ANY) {
193 register struct route *ro;
194
195 ia = (struct in_ifaddr *)0;
196 /*
197 * If route is known or can be allocated now,
198 * our src addr is taken from the i/f, else punt.
199 */
200 ro = &inp->inp_route;
201 if (ro->ro_rt &&
202 (satosin(&ro->ro_dst)->sin_addr.s_addr !=
203 sin->sin_addr.s_addr ||
204 inp->inp_socket->so_options & SO_DONTROUTE)) {
205 RTFREE(ro->ro_rt);
206 ro->ro_rt = (struct rtentry *)0;
207 }
208 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
209 (ro->ro_rt == (struct rtentry *)0 ||
210 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
211 /* No route yet, so try to acquire one */
212 ro->ro_dst.sa_family = AF_INET;
213 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
214 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
215 sin->sin_addr;
216 rtalloc(ro);
217 }
218 /*
219 * If we found a route, use the address
220 * corresponding to the outgoing interface
221 * unless it is the loopback (in case a route
222 * to our address on another net goes to loopback).
223 */
224 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
225 ia = ifatoia(ro->ro_rt->rt_ifa);
226 if (ia == 0) {
227 u_short fport = sin->sin_port;
228
229 sin->sin_port = 0;
230 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
231 if (ia == 0)
232 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
233 sin->sin_port = fport;
234 if (ia == 0)
235 ia = in_ifaddr;
236 if (ia == 0)
237 return (EADDRNOTAVAIL);
238 }
239 /*
240 * If the destination address is multicast and an outgoing
241 * interface has been set as a multicast option, use the
242 * address of that interface as our source address.
243 */
244 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
245 inp->inp_moptions != NULL) {
246 struct ip_moptions *imo;
247 struct ifnet *ifp;
248
249 imo = inp->inp_moptions;
250 if (imo->imo_multicast_ifp != NULL) {
251 ifp = imo->imo_multicast_ifp;
252 for (ia = in_ifaddr; ia; ia = ia->ia_next)
253 if (ia->ia_ifp == ifp)
254 break;
255 if (ia == 0)
256 return (EADDRNOTAVAIL);
257 }
258 }
259 ifaddr = (struct sockaddr_in *)&ia->ia_addr;
260 }
261 if (in_pcblookup(inp->inp_head,
262 sin->sin_addr,
263 sin->sin_port,
264 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
265 inp->inp_lport,
266 0))
267 return (EADDRINUSE);
268 if (inp->inp_laddr.s_addr == INADDR_ANY) {
269 if (inp->inp_lport == 0)
270 (void)in_pcbbind(inp, (struct mbuf *)0);
271 inp->inp_laddr = ifaddr->sin_addr;
272 }
273 inp->inp_faddr = sin->sin_addr;
274 inp->inp_fport = sin->sin_port;
275 return (0);
276 }
277
278 int
279 in_pcbdisconnect(inp)
280 struct inpcb *inp;
281 {
282
283 inp->inp_faddr.s_addr = INADDR_ANY;
284 inp->inp_fport = 0;
285 if (inp->inp_socket->so_state & SS_NOFDREF)
286 in_pcbdetach(inp);
287 }
288
289 int
290 in_pcbdetach(inp)
291 struct inpcb *inp;
292 {
293 struct socket *so = inp->inp_socket;
294
295 so->so_pcb = 0;
296 sofree(so);
297 if (inp->inp_options)
298 (void)m_free(inp->inp_options);
299 if (inp->inp_route.ro_rt)
300 rtfree(inp->inp_route.ro_rt);
301 ip_freemoptions(inp->inp_moptions);
302 remque(inp);
303 FREE(inp, M_PCB);
304 }
305
306 int
307 in_setsockaddr(inp, nam)
308 register 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_lport;
319 sin->sin_addr = inp->inp_laddr;
320 }
321
322 int
323 in_setpeeraddr(inp, nam)
324 struct inpcb *inp;
325 struct mbuf *nam;
326 {
327 register struct sockaddr_in *sin;
328
329 nam->m_len = sizeof (*sin);
330 sin = mtod(nam, struct sockaddr_in *);
331 bzero((caddr_t)sin, sizeof (*sin));
332 sin->sin_family = AF_INET;
333 sin->sin_len = sizeof(*sin);
334 sin->sin_port = inp->inp_fport;
335 sin->sin_addr = inp->inp_faddr;
336 }
337
338 /*
339 * Pass some notification to all connections of a protocol
340 * associated with address dst. The local address and/or port numbers
341 * may be specified to limit the search. The "usual action" will be
342 * taken, depending on the ctlinput cmd. The caller must filter any
343 * cmds that are uninteresting (e.g., no error in the map).
344 * Call the protocol specific routine (if any) to report
345 * any errors for each matching socket.
346 *
347 * Must be called at splnet.
348 */
349 int
350 in_pcbnotify(head, dst, fport_arg, laddr, lport_arg, cmd, notify)
351 struct inpcb *head;
352 struct sockaddr *dst;
353 u_int fport_arg, lport_arg;
354 struct in_addr laddr;
355 int cmd;
356 void (*notify) __P((struct inpcb *, int));
357 {
358 extern u_char inetctlerrmap[];
359 register struct inpcb *inp, *oinp;
360 struct in_addr faddr;
361 u_short fport = fport_arg, lport = lport_arg;
362 int errno;
363
364 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET)
365 return;
366 faddr = ((struct sockaddr_in *)dst)->sin_addr;
367 if (faddr.s_addr == INADDR_ANY)
368 return;
369
370 /*
371 * Redirects go to all references to the destination,
372 * and use in_rtchange to invalidate the route cache.
373 * Dead host indications: notify all references to the destination.
374 * Otherwise, if we have knowledge of the local port and address,
375 * deliver only to that socket.
376 */
377 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
378 fport = 0;
379 lport = 0;
380 laddr.s_addr = 0;
381 if (cmd != PRC_HOSTDEAD)
382 notify = in_rtchange;
383 }
384 errno = inetctlerrmap[cmd];
385 for (inp = head->inp_next; inp != head;) {
386 if (inp->inp_faddr.s_addr != faddr.s_addr ||
387 inp->inp_socket == 0 ||
388 (lport && inp->inp_lport != lport) ||
389 (laddr.s_addr && inp->inp_laddr.s_addr != laddr.s_addr) ||
390 (fport && inp->inp_fport != fport)) {
391 inp = inp->inp_next;
392 continue;
393 }
394 oinp = inp;
395 inp = inp->inp_next;
396 if (notify)
397 (*notify)(oinp, errno);
398 }
399 }
400
401 /*
402 * Check for alternatives when higher level complains
403 * about service problems. For now, invalidate cached
404 * routing information. If the route was created dynamically
405 * (by a redirect), time to try a default gateway again.
406 */
407 int
408 in_losing(inp)
409 struct inpcb *inp;
410 {
411 register struct rtentry *rt;
412 struct rt_addrinfo info;
413
414 if ((rt = inp->inp_route.ro_rt)) {
415 inp->inp_route.ro_rt = 0;
416 bzero((caddr_t)&info, sizeof(info));
417 info.rti_info[RTAX_DST] =
418 (struct sockaddr *)&inp->inp_route.ro_dst;
419 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
420 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
421 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
422 if (rt->rt_flags & RTF_DYNAMIC)
423 (void) rtrequest(RTM_DELETE, rt_key(rt),
424 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
425 (struct rtentry **)0);
426 else
427 /*
428 * A new route can be allocated
429 * the next time output is attempted.
430 */
431 rtfree(rt);
432 }
433 }
434
435 /*
436 * After a routing change, flush old routing
437 * and allocate a (hopefully) better one.
438 */
439 void
440 in_rtchange(inp, errno)
441 register struct inpcb *inp;
442 int errno;
443 {
444 if (inp->inp_route.ro_rt) {
445 rtfree(inp->inp_route.ro_rt);
446 inp->inp_route.ro_rt = 0;
447 /*
448 * A new route can be allocated the next time
449 * output is attempted.
450 */
451 }
452 }
453
454 struct inpcb *
455 in_pcblookup(head, faddr, fport_arg, laddr, lport_arg, flags)
456 struct inpcb *head;
457 struct in_addr faddr, laddr;
458 u_int fport_arg, lport_arg;
459 int flags;
460 {
461 register struct inpcb *inp, *match = 0;
462 int matchwild = 3, wildcard;
463 u_short fport = fport_arg, lport = lport_arg;
464
465 for (inp = head->inp_next; inp != head; inp = inp->inp_next) {
466 if (inp->inp_lport != lport)
467 continue;
468 wildcard = 0;
469 if (inp->inp_laddr.s_addr != INADDR_ANY) {
470 if (laddr.s_addr == INADDR_ANY)
471 wildcard++;
472 else if (inp->inp_laddr.s_addr != laddr.s_addr)
473 continue;
474 } else {
475 if (laddr.s_addr != INADDR_ANY)
476 wildcard++;
477 }
478 if (inp->inp_faddr.s_addr != INADDR_ANY) {
479 if (faddr.s_addr == INADDR_ANY)
480 wildcard++;
481 else if (inp->inp_faddr.s_addr != faddr.s_addr ||
482 inp->inp_fport != fport)
483 continue;
484 } else {
485 if (faddr.s_addr != INADDR_ANY)
486 wildcard++;
487 }
488 if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
489 continue;
490 if (wildcard < matchwild) {
491 match = inp;
492 matchwild = wildcard;
493 if (matchwild == 0)
494 break;
495 }
496 }
497 return (match);
498 }
499