1 /* udp_usrreq.c 4.32 82/09/26 */ 2 3 #include "../h/param.h" 4 #include "../h/dir.h" 5 #include "../h/user.h" 6 #include "../h/mbuf.h" 7 #include "../h/protosw.h" 8 #include "../h/socket.h" 9 #include "../h/socketvar.h" 10 #include "../net/in.h" 11 #include "../net/if.h" 12 #include "../net/route.h" 13 #include "../net/in_pcb.h" 14 #include "../net/in_systm.h" 15 #include "../net/ip.h" 16 #include "../net/ip_var.h" 17 #include "../net/ip_icmp.h" 18 #include "../net/udp.h" 19 #include "../net/udp_var.h" 20 #include <errno.h> 21 22 /* 23 * UDP protocol implementation. 24 * Per RFC 768, August, 1980. 25 */ 26 udp_init() 27 { 28 29 udb.inp_next = udb.inp_prev = &udb; 30 } 31 32 int udpcksum; 33 struct sockaddr_in udp_in = { AF_INET }; 34 35 udp_input(m0) 36 struct mbuf *m0; 37 { 38 register struct udpiphdr *ui; 39 register struct inpcb *inp; 40 register struct mbuf *m; 41 int len; 42 43 /* 44 * Get IP and UDP header together in first mbuf. 45 */ 46 m = m0; 47 if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct udpiphdr)) && 48 (m = m_pullup(m, sizeof (struct udpiphdr))) == 0) { 49 udpstat.udps_hdrops++; 50 return; 51 } 52 ui = mtod(m, struct udpiphdr *); 53 if (((struct ip *)ui)->ip_hl > (sizeof (struct ip) >> 2)) 54 ip_stripoptions((struct ip *)ui, (struct mbuf *)0); 55 56 /* 57 * Make mbuf data length reflect UDP length. 58 * If not enough data to reflect UDP length, drop. 59 */ 60 len = ntohs((u_short)ui->ui_ulen); 61 if (((struct ip *)ui)->ip_len != len) { 62 if (len > ((struct ip *)ui)->ip_len) { 63 udpstat.udps_badlen++; 64 goto bad; 65 } 66 m_adj(m, ((struct ip *)ui)->ip_len - len); 67 /* (struct ip *)ui->ip_len = len; */ 68 } 69 70 /* 71 * Checksum extended UDP header and data. 72 */ 73 if (udpcksum) { 74 ui->ui_next = ui->ui_prev = 0; 75 ui->ui_x1 = 0; 76 ui->ui_len = htons((u_short)len); 77 if (ui->ui_sum = in_cksum(m, len + sizeof (struct ip))) { 78 udpstat.udps_badsum++; 79 printf("udp cksum %x\n", ui->ui_sum); 80 m_freem(m); 81 return; 82 } 83 } 84 85 /* 86 * Locate pcb for datagram. 87 */ 88 inp = in_pcblookup(&udb, 89 ui->ui_src, ui->ui_sport, ui->ui_dst, ui->ui_dport, 90 INPLOOKUP_WILDCARD); 91 if (inp == 0) { 92 struct in_addr broadcastaddr; 93 94 broadcastaddr = if_makeaddr(ui->ui_dst.s_net, INADDR_ANY); 95 if (ui->ui_dst.s_addr == broadcastaddr.s_addr) 96 goto bad; 97 icmp_error((struct ip *)ui, ICMP_UNREACH, ICMP_UNREACH_PORT); 98 return; 99 } 100 101 /* 102 * Construct sockaddr format source address. 103 * Stuff source address and datagram in user buffer. 104 */ 105 udp_in.sin_port = ui->ui_sport; 106 udp_in.sin_addr = ui->ui_src; 107 m->m_len -= sizeof (struct udpiphdr); 108 m->m_off += sizeof (struct udpiphdr); 109 if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, m) == 0) 110 goto bad; 111 sorwakeup(inp->inp_socket); 112 return; 113 bad: 114 m_freem(m); 115 } 116 117 udp_abort(inp) 118 struct inpcb *inp; 119 { 120 struct socket *so = inp->inp_socket; 121 122 in_pcbdisconnect(inp); 123 soisdisconnected(so); 124 } 125 126 udp_ctlinput(cmd, arg) 127 int cmd; 128 caddr_t arg; 129 { 130 struct in_addr *sin; 131 extern u_char inetctlerrmap[]; 132 133 if (cmd < 0 || cmd > PRC_NCMDS) 134 return; 135 switch (cmd) { 136 137 case PRC_ROUTEDEAD: 138 break; 139 140 case PRC_QUENCH: 141 break; 142 143 /* these are handled by ip */ 144 case PRC_IFDOWN: 145 case PRC_HOSTDEAD: 146 case PRC_HOSTUNREACH: 147 break; 148 149 default: 150 sin = &((struct icmp *)arg)->icmp_ip.ip_dst; 151 in_pcbnotify(&udb, sin, inetctlerrmap[cmd], udp_abort); 152 } 153 } 154 155 udp_output(inp, m0) 156 struct inpcb *inp; 157 struct mbuf *m0; 158 { 159 register struct mbuf *m; 160 register struct udpiphdr *ui; 161 register struct socket *so; 162 register int len = 0; 163 164 /* 165 * Calculate data length and get a mbuf 166 * for UDP and IP headers. 167 */ 168 for (m = m0; m; m = m->m_next) 169 len += m->m_len; 170 m = m_get(M_DONTWAIT); 171 if (m == 0) { 172 m_freem(m0); 173 return (ENOBUFS); 174 } 175 176 /* 177 * Fill in mbuf with extended UDP header 178 * and addresses and length put into network format. 179 */ 180 m->m_off = MMAXOFF - sizeof (struct udpiphdr); 181 m->m_len = sizeof (struct udpiphdr); 182 m->m_next = m0; 183 ui = mtod(m, struct udpiphdr *); 184 ui->ui_next = ui->ui_prev = 0; 185 ui->ui_x1 = 0; 186 ui->ui_pr = IPPROTO_UDP; 187 ui->ui_len = len + sizeof (struct udphdr); 188 ui->ui_src = inp->inp_laddr; 189 ui->ui_dst = inp->inp_faddr; 190 ui->ui_sport = inp->inp_lport; 191 ui->ui_dport = inp->inp_fport; 192 ui->ui_ulen = htons((u_short)ui->ui_len); 193 194 /* 195 * Stuff checksum and output datagram. 196 */ 197 ui->ui_sum = 0; 198 ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len); 199 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 200 ((struct ip *)ui)->ip_ttl = MAXTTL; 201 so = inp->inp_socket; 202 return (ip_output(m, (struct mbuf *)0, 203 (so->so_options & SO_DONTROUTE) ? &routetoif : (struct route *)0, 204 so->so_state & SS_PRIV)); 205 } 206 207 udp_usrreq(so, req, m, nam, opt) 208 struct socket *so; 209 int req; 210 struct mbuf *m, *nam; 211 struct socketopt *opt; 212 { 213 struct inpcb *inp = sotoinpcb(so); 214 int error = 0; 215 216 if (inp == 0 && req != PRU_ATTACH) 217 return (EINVAL); 218 switch (req) { 219 220 case PRU_ATTACH: 221 if (inp != 0) 222 return (EINVAL); 223 error = in_pcballoc(so, &udb); 224 if (error) 225 break; 226 error = in_pcbreserve(so, 2048, 2048); 227 if (error) 228 break; 229 break; 230 231 case PRU_DETACH: 232 if (inp == 0) 233 return (ENOTCONN); 234 in_pcbdetach(inp); 235 break; 236 237 case PRU_BIND: 238 error = in_pcbbind(inp, nam); 239 break; 240 241 case PRU_LISTEN: 242 error = EOPNOTSUPP; 243 break; 244 245 case PRU_CONNECT: 246 if (inp->inp_faddr.s_addr) 247 return (EISCONN); 248 error = in_pcbconnect(inp, nam); 249 if (error == 0) 250 soisconnected(so); 251 break; 252 253 case PRU_ACCEPT: 254 return (EOPNOTSUPP); 255 256 case PRU_DISCONNECT: 257 if (inp->inp_faddr.s_addr == 0) 258 return (ENOTCONN); 259 in_pcbdisconnect(inp); 260 soisdisconnected(so); 261 break; 262 263 case PRU_SHUTDOWN: 264 socantsendmore(so); 265 break; 266 267 case PRU_SEND: { 268 struct in_addr laddr; 269 270 if (nam) { 271 laddr = inp->inp_laddr; 272 if (inp->inp_faddr.s_addr) 273 return (EISCONN); 274 error = in_pcbconnect(inp, nam); 275 if (error) 276 break; 277 } else { 278 if (inp->inp_faddr.s_addr == 0) 279 return (ENOTCONN); 280 } 281 error = udp_output(inp, m); 282 if (nam) { 283 in_pcbdisconnect(inp); 284 inp->inp_laddr = laddr; 285 } 286 } 287 break; 288 289 case PRU_ABORT: 290 in_pcbdetach(inp); 291 sofree(so); 292 soisdisconnected(so); 293 break; 294 295 case PRU_CONTROL: 296 return (EOPNOTSUPP); 297 298 case PRU_SOCKADDR: 299 in_setsockaddr(inp, nam); 300 break; 301 302 default: 303 panic("udp_usrreq"); 304 } 305 return (error); 306 } 307