1 /* in_pcb.c 4.39 83/01/22 */ 2 3 #include "../h/param.h" 4 #include "../h/systm.h" 5 #include "../h/dir.h" 6 #include "../h/user.h" 7 #include "../h/mbuf.h" 8 #include "../h/socket.h" 9 #include "../h/socketvar.h" 10 #include "../netinet/in.h" 11 #include "../netinet/in_systm.h" 12 #include "../net/if.h" 13 #include "../net/route.h" 14 #include "../netinet/in_pcb.h" 15 #include "../h/protosw.h" 16 17 struct in_addr zeroin_addr; 18 19 in_pcballoc(so, head) 20 struct socket *so; 21 struct inpcb *head; 22 { 23 struct mbuf *m; 24 register struct inpcb *inp; 25 26 m = m_getclr(M_DONTWAIT, MT_PCB); 27 if (m == NULL) 28 return (ENOBUFS); 29 inp = mtod(m, struct inpcb *); 30 inp->inp_head = head; 31 inp->inp_socket = so; 32 insque(inp, head); 33 so->so_pcb = (caddr_t)inp; 34 return (0); 35 } 36 37 in_pcbbind(inp, nam) 38 register struct inpcb *inp; 39 struct mbuf *nam; 40 { 41 register struct socket *so = inp->inp_socket; 42 register struct inpcb *head = inp->inp_head; 43 register struct sockaddr_in *sin; 44 u_short lport = 0; 45 46 if (ifnet == 0) 47 return (EADDRNOTAVAIL); 48 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY) 49 return (EINVAL); 50 if (nam == 0) 51 goto noname; 52 sin = mtod(nam, struct sockaddr_in *); 53 if (nam->m_len != sizeof (*sin)) 54 return (EINVAL); 55 if (sin->sin_addr.s_addr != INADDR_ANY) { 56 int tport = sin->sin_port; 57 58 sin->sin_port = 0; /* yech... */ 59 if (if_ifwithaddr((struct sockaddr *)sin) == 0) 60 return (EADDRNOTAVAIL); 61 sin->sin_port = tport; 62 } 63 lport = sin->sin_port; 64 if (lport) { 65 u_short aport = htons(lport); 66 int wild = 0; 67 68 /* GROSS */ 69 if (aport < IPPORT_RESERVED && u.u_uid != 0) 70 return (EACCES); 71 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 || 72 (so->so_options & SO_ACCEPTCONN) == 0) 73 wild = INPLOOKUP_WILDCARD; 74 if (in_pcblookup(head, 75 zeroin_addr, 0, sin->sin_addr, lport, wild)) 76 return (EADDRINUSE); 77 } 78 inp->inp_laddr = sin->sin_addr; 79 noname: 80 if (lport == 0) 81 do { 82 if (head->inp_lport++ < IPPORT_RESERVED) 83 head->inp_lport = IPPORT_RESERVED; 84 lport = htons(head->inp_lport); 85 } while (in_pcblookup(head, 86 zeroin_addr, 0, inp->inp_laddr, lport, 0)); 87 inp->inp_lport = lport; 88 return (0); 89 } 90 91 /* 92 * Connect from a socket to a specified address. 93 * Both address and port must be specified in argument sin. 94 * If don't have a local address for this socket yet, 95 * then pick one. 96 */ 97 in_pcbconnect(inp, nam) 98 struct inpcb *inp; 99 struct mbuf *nam; 100 { 101 struct ifnet *ifp; 102 struct sockaddr_in *ifaddr; 103 register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 104 105 if (nam->m_len != sizeof (*sin)) 106 return (EINVAL); 107 if (sin->sin_family != AF_INET) 108 return (EAFNOSUPPORT); 109 if (sin->sin_addr.s_addr == INADDR_ANY || sin->sin_port == 0) 110 return (EADDRNOTAVAIL); 111 if (inp->inp_laddr.s_addr == INADDR_ANY) { 112 ifp = if_ifonnetof(in_netof(sin->sin_addr)); 113 if (ifp == 0) { 114 /* 115 * We should select the interface based on 116 * the route to be used, but for udp this would 117 * result in two calls to rtalloc for each packet 118 * sent; hardly worthwhile... 119 */ 120 ifp = if_ifwithaf(AF_INET); 121 if (ifp == 0) 122 return (EADDRNOTAVAIL); 123 } 124 ifaddr = (struct sockaddr_in *)&ifp->if_addr; 125 } 126 if (in_pcblookup(inp->inp_head, 127 sin->sin_addr, 128 sin->sin_port, 129 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr, 130 inp->inp_lport, 131 0)) 132 return (EADDRINUSE); 133 if (inp->inp_laddr.s_addr == INADDR_ANY) 134 inp->inp_laddr = ifaddr->sin_addr; 135 inp->inp_faddr = sin->sin_addr; 136 inp->inp_fport = sin->sin_port; 137 return (0); 138 } 139 140 in_pcbdisconnect(inp) 141 struct inpcb *inp; 142 { 143 144 inp->inp_faddr.s_addr = INADDR_ANY; 145 inp->inp_fport = 0; 146 if (inp->inp_socket->so_state & SS_NOFDREF) 147 in_pcbdetach(inp); 148 } 149 150 in_pcbdetach(inp) 151 struct inpcb *inp; 152 { 153 struct socket *so = inp->inp_socket; 154 155 so->so_pcb = 0; 156 sofree(so); 157 if (inp->inp_route.ro_rt) 158 rtfree(inp->inp_route.ro_rt); 159 remque(inp); 160 (void) m_free(dtom(inp)); 161 } 162 163 in_setsockaddr(inp, nam) 164 register struct inpcb *inp; 165 struct mbuf *nam; 166 { 167 register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 168 169 nam->m_len = sizeof (*sin); 170 sin = mtod(nam, struct sockaddr_in *); 171 bzero((caddr_t)sin, sizeof (*sin)); 172 sin->sin_family = AF_INET; 173 sin->sin_port = inp->inp_lport; 174 sin->sin_addr = inp->inp_laddr; 175 } 176 177 /* 178 * Pass an error to all internet connections 179 * associated with address sin. Call the 180 * protocol specific routine to clean up the 181 * mess afterwards. 182 */ 183 in_pcbnotify(head, dst, errno, abort) 184 struct inpcb *head; 185 register struct in_addr *dst; 186 int errno, (*abort)(); 187 { 188 register struct inpcb *inp, *oinp; 189 int s = splimp(); 190 191 for (inp = head->inp_next; inp != head;) { 192 if (inp->inp_faddr.s_addr != dst->s_addr) { 193 next: 194 inp = inp->inp_next; 195 continue; 196 } 197 if (inp->inp_socket == 0) 198 goto next; 199 inp->inp_socket->so_error = errno; 200 oinp = inp; 201 inp = inp->inp_next; 202 (*abort)(oinp); 203 } 204 splx(s); 205 } 206 207 struct inpcb * 208 in_pcblookup(head, faddr, fport, laddr, lport, flags) 209 struct inpcb *head; 210 struct in_addr faddr, laddr; 211 u_short fport, lport; 212 int flags; 213 { 214 register struct inpcb *inp, *match = 0; 215 int matchwild = 3, wildcard; 216 217 for (inp = head->inp_next; inp != head; inp = inp->inp_next) { 218 if (inp->inp_lport != lport) 219 continue; 220 wildcard = 0; 221 if (inp->inp_laddr.s_addr != INADDR_ANY) { 222 if (laddr.s_addr == INADDR_ANY) 223 wildcard++; 224 else if (inp->inp_laddr.s_addr != laddr.s_addr) 225 continue; 226 } else { 227 if (laddr.s_addr != INADDR_ANY) 228 wildcard++; 229 } 230 if (inp->inp_faddr.s_addr != INADDR_ANY) { 231 if (faddr.s_addr == INADDR_ANY) 232 wildcard++; 233 else if (inp->inp_faddr.s_addr != faddr.s_addr || 234 inp->inp_fport != fport) 235 continue; 236 } else { 237 if (faddr.s_addr != INADDR_ANY) 238 wildcard++; 239 } 240 if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0) 241 continue; 242 if (wildcard < matchwild) { 243 match = inp; 244 matchwild = wildcard; 245 if (matchwild == 0) 246 break; 247 } 248 } 249 return (match); 250 } 251