Much to my astonishment, packets in the loopback net were being
[unix-history] / sys / netinet / in_pcb.c
CommitLineData
15637ed4
RG
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 *
38e82238 33 * from: @(#)in_pcb.c 7.14 (Berkeley) 4/20/91
fde1aeb2 34 * $Id: in_pcb.c,v 1.4 1993/11/25 01:35:05 wollman Exp $
15637ed4
RG
35 */
36
37#include "param.h"
38#include "systm.h"
39#include "malloc.h"
40#include "mbuf.h"
41#include "protosw.h"
42#include "socket.h"
43#include "socketvar.h"
44#include "ioctl.h"
45
46#include "../net/if.h"
47#include "../net/route.h"
48
49#include "in.h"
50#include "in_systm.h"
51#include "ip.h"
52#include "in_pcb.h"
53#include "in_var.h"
54
4c45483e 55int
15637ed4
RG
56in_pcballoc(so, head)
57 struct socket *so;
58 struct inpcb *head;
59{
60 struct mbuf *m;
61 register struct inpcb *inp;
62
63 m = m_getclr(M_DONTWAIT, MT_PCB);
64 if (m == NULL)
65 return (ENOBUFS);
66 inp = mtod(m, struct inpcb *);
67 inp->inp_head = head;
68 inp->inp_socket = so;
69 insque(inp, head);
70 so->so_pcb = (caddr_t)inp;
71 return (0);
72}
73
4c45483e 74int
15637ed4
RG
75in_pcbbind(inp, nam)
76 register struct inpcb *inp;
77 struct mbuf *nam;
78{
79 register struct socket *so = inp->inp_socket;
80 register struct inpcb *head = inp->inp_head;
81 register struct sockaddr_in *sin;
82 u_short lport = 0;
83
84 if (in_ifaddr == 0)
85 return (EADDRNOTAVAIL);
86 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
87 return (EINVAL);
88 if (nam == 0)
89 goto noname;
90 sin = mtod(nam, struct sockaddr_in *);
91 if (nam->m_len != sizeof (*sin))
92 return (EINVAL);
93 if (sin->sin_addr.s_addr != INADDR_ANY) {
94 int tport = sin->sin_port;
95
96 sin->sin_port = 0; /* yech... */
97 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
98 return (EADDRNOTAVAIL);
99 sin->sin_port = tport;
100 }
101 lport = sin->sin_port;
102 if (lport) {
103 u_short aport = ntohs(lport);
104 int wild = 0;
105
106 /* GROSS */
107 if (aport < IPPORT_RESERVED && (so->so_state & SS_PRIV) == 0)
108 return (EACCES);
109 /* even GROSSER, but this is the Internet */
110 if ((so->so_options & SO_REUSEADDR) == 0 &&
111 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
112 (so->so_options & SO_ACCEPTCONN) == 0))
113 wild = INPLOOKUP_WILDCARD;
114 if (in_pcblookup(head,
115 zeroin_addr, 0, sin->sin_addr, lport, wild))
116 return (EADDRINUSE);
117 }
118 inp->inp_laddr = sin->sin_addr;
119noname:
120 if (lport == 0)
121 do {
122 if (head->inp_lport++ < IPPORT_RESERVED ||
123 head->inp_lport > IPPORT_USERRESERVED)
124 head->inp_lport = IPPORT_RESERVED;
125 lport = htons(head->inp_lport);
126 } while (in_pcblookup(head,
127 zeroin_addr, 0, inp->inp_laddr, lport, 0));
128 inp->inp_lport = lport;
129 return (0);
130}
131
132/*
133 * Connect from a socket to a specified address.
134 * Both address and port must be specified in argument sin.
135 * If don't have a local address for this socket yet,
136 * then pick one.
137 */
4c45483e 138int
15637ed4
RG
139in_pcbconnect(inp, nam)
140 register struct inpcb *inp;
141 struct mbuf *nam;
142{
143 struct in_ifaddr *ia;
4c45483e 144 struct sockaddr_in *ifaddr = 0;
15637ed4
RG
145 register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
146
147 if (nam->m_len != sizeof (*sin))
148 return (EINVAL);
149 if (sin->sin_family != AF_INET)
150 return (EAFNOSUPPORT);
151 if (sin->sin_port == 0)
152 return (EADDRNOTAVAIL);
153 if (in_ifaddr) {
154 /*
155 * If the destination address is INADDR_ANY,
156 * use the primary local address.
157 * If the supplied address is INADDR_BROADCAST,
158 * and the primary interface supports broadcast,
159 * choose the broadcast address for that interface.
160 */
161#define satosin(sa) ((struct sockaddr_in *)(sa))
162 if (sin->sin_addr.s_addr == INADDR_ANY)
163 sin->sin_addr = IA_SIN(in_ifaddr)->sin_addr;
164 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
165 (in_ifaddr->ia_ifp->if_flags & IFF_BROADCAST))
166 sin->sin_addr = satosin(&in_ifaddr->ia_broadaddr)->sin_addr;
167 }
168 if (inp->inp_laddr.s_addr == INADDR_ANY) {
169 register struct route *ro;
170 struct ifnet *ifp;
171
172 ia = (struct in_ifaddr *)0;
173 /*
174 * If route is known or can be allocated now,
175 * our src addr is taken from the i/f, else punt.
176 */
177 ro = &inp->inp_route;
178 if (ro->ro_rt &&
179 (satosin(&ro->ro_dst)->sin_addr.s_addr !=
180 sin->sin_addr.s_addr ||
181 inp->inp_socket->so_options & SO_DONTROUTE)) {
182 RTFREE(ro->ro_rt);
183 ro->ro_rt = (struct rtentry *)0;
184 }
185 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
186 (ro->ro_rt == (struct rtentry *)0 ||
187 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
188 /* No route yet, so try to acquire one */
189 ro->ro_dst.sa_family = AF_INET;
190 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
191 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
192 sin->sin_addr;
193 rtalloc(ro);
194 }
195 /*
196 * If we found a route, use the address
197 * corresponding to the outgoing interface
198 * unless it is the loopback (in case a route
199 * to our address on another net goes to loopback).
200 */
201 if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp) &&
202 (ifp->if_flags & IFF_LOOPBACK) == 0)
203 for (ia = in_ifaddr; ia; ia = ia->ia_next)
204 if (ia->ia_ifp == ifp)
205 break;
206 if (ia == 0) {
207 int fport = sin->sin_port;
208
209 sin->sin_port = 0;
210 ia = (struct in_ifaddr *)
211 ifa_ifwithdstaddr((struct sockaddr *)sin);
212 sin->sin_port = fport;
213 if (ia == 0)
214 ia = in_iaonnetof(in_netof(sin->sin_addr));
215 if (ia == 0)
216 ia = in_ifaddr;
217 if (ia == 0)
218 return (EADDRNOTAVAIL);
219 }
220 ifaddr = (struct sockaddr_in *)&ia->ia_addr;
221 }
222 if (in_pcblookup(inp->inp_head,
223 sin->sin_addr,
224 sin->sin_port,
225 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
226 inp->inp_lport,
227 0))
228 return (EADDRINUSE);
229 if (inp->inp_laddr.s_addr == INADDR_ANY) {
230 if (inp->inp_lport == 0)
231 (void)in_pcbbind(inp, (struct mbuf *)0);
232 inp->inp_laddr = ifaddr->sin_addr;
233 }
234 inp->inp_faddr = sin->sin_addr;
235 inp->inp_fport = sin->sin_port;
2cb63509
GW
236#ifdef MTUDISC
237 /*
238 * If the upper layer asked for PMTU discovery services, see
239 * if we can get an idea of what the MTU should be...
240 */
241 in_pcbmtu(inp);
242#endif /* MTUDISC */
15637ed4
RG
243 return (0);
244}
245
4c45483e 246void
15637ed4
RG
247in_pcbdisconnect(inp)
248 struct inpcb *inp;
249{
250
251 inp->inp_faddr.s_addr = INADDR_ANY;
252 inp->inp_fport = 0;
2cb63509
GW
253#ifdef MTUDISC
254 inp->inp_flags &= ~INP_MTUDISCOVERED;
255#endif
15637ed4
RG
256 if (inp->inp_socket->so_state & SS_NOFDREF)
257 in_pcbdetach(inp);
258}
259
4c45483e 260void
15637ed4
RG
261in_pcbdetach(inp)
262 struct inpcb *inp;
263{
264 struct socket *so = inp->inp_socket;
265
266 so->so_pcb = 0;
267 sofree(so);
268 if (inp->inp_options)
269 (void)m_free(inp->inp_options);
270 if (inp->inp_route.ro_rt)
271 rtfree(inp->inp_route.ro_rt);
272 remque(inp);
273 (void) m_free(dtom(inp));
274}
275
4c45483e 276void
15637ed4
RG
277in_setsockaddr(inp, nam)
278 register struct inpcb *inp;
279 struct mbuf *nam;
280{
281 register struct sockaddr_in *sin;
282
283 nam->m_len = sizeof (*sin);
284 sin = mtod(nam, struct sockaddr_in *);
285 bzero((caddr_t)sin, sizeof (*sin));
286 sin->sin_family = AF_INET;
287 sin->sin_len = sizeof(*sin);
288 sin->sin_port = inp->inp_lport;
289 sin->sin_addr = inp->inp_laddr;
290}
291
4c45483e 292void
15637ed4
RG
293in_setpeeraddr(inp, nam)
294 struct inpcb *inp;
295 struct mbuf *nam;
296{
297 register struct sockaddr_in *sin;
298
299 nam->m_len = sizeof (*sin);
300 sin = mtod(nam, struct sockaddr_in *);
301 bzero((caddr_t)sin, sizeof (*sin));
302 sin->sin_family = AF_INET;
303 sin->sin_len = sizeof(*sin);
304 sin->sin_port = inp->inp_fport;
305 sin->sin_addr = inp->inp_faddr;
306}
307
308/*
309 * Pass some notification to all connections of a protocol
310 * associated with address dst. The local address and/or port numbers
311 * may be specified to limit the search. The "usual action" will be
312 * taken, depending on the ctlinput cmd. The caller must filter any
313 * cmds that are uninteresting (e.g., no error in the map).
314 * Call the protocol specific routine (if any) to report
315 * any errors for each matching socket.
316 *
317 * Must be called at splnet.
318 */
2cb63509 319void
15637ed4
RG
320in_pcbnotify(head, dst, fport, laddr, lport, cmd, notify)
321 struct inpcb *head;
322 struct sockaddr *dst;
323 u_short fport, lport;
324 struct in_addr laddr;
4c45483e
GW
325 int cmd;
326 void (*notify)(struct inpcb *, int);
15637ed4
RG
327{
328 register struct inpcb *inp, *oinp;
329 struct in_addr faddr;
330 int errno;
15637ed4
RG
331
332 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET)
333 return;
334 faddr = ((struct sockaddr_in *)dst)->sin_addr;
335 if (faddr.s_addr == INADDR_ANY)
336 return;
337
338 /*
339 * Redirects go to all references to the destination,
340 * and use in_rtchange to invalidate the route cache.
341 * Dead host indications: notify all references to the destination.
2cb63509 342 * MTU change indications: same thing.
15637ed4
RG
343 * Otherwise, if we have knowledge of the local port and address,
344 * deliver only to that socket.
345 */
2cb63509
GW
346 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD
347 || cmd == PRC_MTUCHANGED) {
15637ed4
RG
348 fport = 0;
349 lport = 0;
350 laddr.s_addr = 0;
2cb63509 351 if (cmd != PRC_HOSTDEAD && cmd != PRC_MTUCHANGED)
15637ed4
RG
352 notify = in_rtchange;
353 }
354 errno = inetctlerrmap[cmd];
355 for (inp = head->inp_next; inp != head;) {
356 if (inp->inp_faddr.s_addr != faddr.s_addr ||
357 inp->inp_socket == 0 ||
358 (lport && inp->inp_lport != lport) ||
359 (laddr.s_addr && inp->inp_laddr.s_addr != laddr.s_addr) ||
360 (fport && inp->inp_fport != fport)) {
361 inp = inp->inp_next;
362 continue;
363 }
364 oinp = inp;
365 inp = inp->inp_next;
366 if (notify)
367 (*notify)(oinp, errno);
368 }
369}
370
371/*
372 * Check for alternatives when higher level complains
373 * about service problems. For now, invalidate cached
374 * routing information. If the route was created dynamically
375 * (by a redirect), time to try a default gateway again.
376 */
2cb63509 377void
15637ed4
RG
378in_losing(inp)
379 struct inpcb *inp;
380{
381 register struct rtentry *rt;
382
383 if ((rt = inp->inp_route.ro_rt)) {
384 rt_missmsg(RTM_LOSING, &inp->inp_route.ro_dst,
385 rt->rt_gateway, (struct sockaddr *)rt_mask(rt),
386 (struct sockaddr *)0, rt->rt_flags, 0);
387 if (rt->rt_flags & RTF_DYNAMIC)
388 (void) rtrequest(RTM_DELETE, rt_key(rt),
389 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
390 (struct rtentry **)0);
391 inp->inp_route.ro_rt = 0;
392 rtfree(rt);
2cb63509
GW
393
394#ifdef MTUDISC
15637ed4 395 /*
2cb63509
GW
396 * When doing MTU discovery, we want to find out as
397 * quickly as possible what the MTU of the new route is.
15637ed4 398 */
2cb63509
GW
399 in_pcbmtu(inp);
400#endif /* MTUDISC */
15637ed4
RG
401 }
402}
403
404/*
405 * After a routing change, flush old routing
406 * and allocate a (hopefully) better one.
407 */
4c45483e
GW
408void
409in_rtchange(inp, errno)
15637ed4 410 register struct inpcb *inp;
4c45483e 411 int errno;
15637ed4
RG
412{
413 if (inp->inp_route.ro_rt) {
414 rtfree(inp->inp_route.ro_rt);
415 inp->inp_route.ro_rt = 0;
2cb63509 416#ifdef MTUDISC
15637ed4
RG
417 /*
418 * A new route can be allocated the next time
2cb63509
GW
419 * output is attempted, but make sure to let
420 * MTU discovery know about it.
15637ed4 421 */
2cb63509
GW
422 in_pcbmtu(inp);
423#endif /* MTUDISC */
15637ed4
RG
424 }
425}
426
427struct inpcb *
428in_pcblookup(head, faddr, fport, laddr, lport, flags)
429 struct inpcb *head;
430 struct in_addr faddr, laddr;
431 u_short fport, lport;
432 int flags;
433{
434 register struct inpcb *inp, *match = 0;
435 int matchwild = 3, wildcard;
436
437 for (inp = head->inp_next; inp != head; inp = inp->inp_next) {
438 if (inp->inp_lport != lport)
439 continue;
440 wildcard = 0;
441 if (inp->inp_laddr.s_addr != INADDR_ANY) {
442 if (laddr.s_addr == INADDR_ANY)
443 wildcard++;
444 else if (inp->inp_laddr.s_addr != laddr.s_addr)
445 continue;
446 } else {
447 if (laddr.s_addr != INADDR_ANY)
448 wildcard++;
449 }
450 if (inp->inp_faddr.s_addr != INADDR_ANY) {
451 if (faddr.s_addr == INADDR_ANY)
452 wildcard++;
453 else if (inp->inp_faddr.s_addr != faddr.s_addr ||
454 inp->inp_fport != fport)
455 continue;
456 } else {
457 if (faddr.s_addr != INADDR_ANY)
458 wildcard++;
459 }
460 if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
461 continue;
462 if (wildcard < matchwild) {
463 match = inp;
464 matchwild = wildcard;
465 if (matchwild == 0)
466 break;
467 }
468 }
469 return (match);
470}