raw_usrreq.c revision 180305
1/*- 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 4. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * @(#)raw_usrreq.c 8.1 (Berkeley) 6/10/93 31 * $FreeBSD: head/sys/net/raw_usrreq.c 180305 2008-07-05 18:03:39Z rwatson $ 32 */ 33 34#include <sys/param.h> 35#include <sys/kernel.h> 36#include <sys/lock.h> 37#include <sys/malloc.h> 38#include <sys/mbuf.h> 39#include <sys/mutex.h> 40#include <sys/priv.h> 41#include <sys/protosw.h> 42#include <sys/signalvar.h> 43#include <sys/socket.h> 44#include <sys/socketvar.h> 45#include <sys/sx.h> 46#include <sys/systm.h> 47 48#include <net/raw_cb.h> 49 50MTX_SYSINIT(rawcb_mtx, &rawcb_mtx, "rawcb", MTX_DEF); 51 52/* 53 * Initialize raw connection block q. 54 */ 55void 56raw_init(void) 57{ 58 59 LIST_INIT(&rawcb_list); 60} 61 62/* 63 * Raw protocol input routine. Find the socket associated with the packet(s) 64 * and move them over. If nothing exists for this packet, drop it. 65 */ 66/* 67 * Raw protocol interface. 68 */ 69void 70raw_input(struct mbuf *m0, struct sockproto *proto, struct sockaddr *src, 71 struct sockaddr *dst) 72{ 73 struct rawcb *rp; 74 struct mbuf *m = m0; 75 struct socket *last; 76 77 last = 0; 78 mtx_lock(&rawcb_mtx); 79 LIST_FOREACH(rp, &rawcb_list, list) { 80 if (rp->rcb_proto.sp_family != proto->sp_family) 81 continue; 82 if (rp->rcb_proto.sp_protocol && 83 rp->rcb_proto.sp_protocol != proto->sp_protocol) 84 continue; 85 /* 86 * We assume the lower level routines have placed the address 87 * in a canonical format suitable for a structure comparison. 88 * 89 * Note that if the lengths are not the same the comparison 90 * will fail at the first byte. 91 */ 92#define equal(a1, a2) \ 93 (bcmp((caddr_t)(a1), (caddr_t)(a2), a1->sa_len) == 0) 94 if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst)) 95 continue; 96 if (rp->rcb_faddr && !equal(rp->rcb_faddr, src)) 97 continue; 98 if (last) { 99 struct mbuf *n; 100 n = m_copy(m, 0, (int)M_COPYALL); 101 if (n) { 102 if (sbappendaddr(&last->so_rcv, src, 103 n, (struct mbuf *)0) == 0) 104 /* should notify about lost packet */ 105 m_freem(n); 106 else 107 sorwakeup(last); 108 } 109 } 110 last = rp->rcb_socket; 111 } 112 if (last) { 113 if (sbappendaddr(&last->so_rcv, src, 114 m, (struct mbuf *)0) == 0) 115 m_freem(m); 116 else 117 sorwakeup(last); 118 } else 119 m_freem(m); 120 mtx_unlock(&rawcb_mtx); 121} 122 123/*ARGSUSED*/ 124void 125raw_ctlinput(int cmd, struct sockaddr *arg, void *dummy) 126{ 127 128 if (cmd < 0 || cmd >= PRC_NCMDS) 129 return; 130 /* INCOMPLETE */ 131} 132 133static void 134raw_uabort(struct socket *so) 135{ 136 struct rawcb *rp = sotorawcb(so); 137 138 KASSERT(rp != NULL, ("raw_uabort: rp == NULL")); 139 raw_disconnect(rp); 140 soisdisconnected(so); 141} 142 143static void 144raw_uclose(struct socket *so) 145{ 146 struct rawcb *rp = sotorawcb(so); 147 148 KASSERT(rp != NULL, ("raw_uabort: rp == NULL")); 149 raw_disconnect(rp); 150 soisdisconnected(so); 151} 152 153/* pru_accept is EOPNOTSUPP */ 154 155static int 156raw_uattach(struct socket *so, int proto, struct thread *td) 157{ 158 int error; 159 160 /* 161 * Implementors of raw sockets will already have allocated the PCB, 162 * so it must be non-NULL here. 163 */ 164 KASSERT(sotorawcb(so) != NULL, ("raw_uattach: so_pcb == NULL")); 165 166 if (td != NULL) { 167 error = priv_check(td, PRIV_NET_RAW); 168 if (error) 169 return error; 170 } 171 return raw_attach(so, proto); 172} 173 174static int 175raw_ubind(struct socket *so, struct sockaddr *nam, struct thread *td) 176{ 177 178 return (EINVAL); 179} 180 181static int 182raw_uconnect(struct socket *so, struct sockaddr *nam, struct thread *td) 183{ 184 185 return (EINVAL); 186} 187 188/* pru_connect2 is EOPNOTSUPP */ 189/* pru_control is EOPNOTSUPP */ 190 191static void 192raw_udetach(struct socket *so) 193{ 194 struct rawcb *rp = sotorawcb(so); 195 196 KASSERT(rp != NULL, ("raw_udetach: rp == NULL")); 197 raw_detach(rp); 198} 199 200static int 201raw_udisconnect(struct socket *so) 202{ 203 struct rawcb *rp = sotorawcb(so); 204 205 KASSERT(rp != NULL, ("raw_udisconnect: rp == NULL")); 206 if (rp->rcb_faddr == 0) 207 return (ENOTCONN); 208 raw_disconnect(rp); 209 soisdisconnected(so); 210 return (0); 211} 212 213/* pru_listen is EOPNOTSUPP */ 214 215static int 216raw_upeeraddr(struct socket *so, struct sockaddr **nam) 217{ 218 struct rawcb *rp = sotorawcb(so); 219 220 KASSERT(rp != NULL, ("raw_upeeraddr: rp == NULL")); 221 if (rp->rcb_faddr == 0) 222 return (ENOTCONN); 223 *nam = sodupsockaddr(rp->rcb_faddr, M_WAITOK); 224 return (0); 225} 226 227/* pru_rcvd is EOPNOTSUPP */ 228/* pru_rcvoob is EOPNOTSUPP */ 229 230static int 231raw_usend(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 232 struct mbuf *control, struct thread *td) 233{ 234 int error; 235 struct rawcb *rp = sotorawcb(so); 236 237 KASSERT(rp != NULL, ("raw_usend: rp == NULL")); 238 239 if (flags & PRUS_OOB) { 240 error = EOPNOTSUPP; 241 goto release; 242 } 243 244 if (control && control->m_len) { 245 error = EOPNOTSUPP; 246 goto release; 247 } 248 if (nam) { 249 if (rp->rcb_faddr) { 250 error = EISCONN; 251 goto release; 252 } 253 rp->rcb_faddr = nam; 254 } else if (rp->rcb_faddr == 0) { 255 error = ENOTCONN; 256 goto release; 257 } 258 error = (*so->so_proto->pr_output)(m, so); 259 m = NULL; 260 if (nam) 261 rp->rcb_faddr = 0; 262release: 263 if (m != NULL) 264 m_freem(m); 265 return (error); 266} 267 268/* pru_sense is null */ 269 270static int 271raw_ushutdown(struct socket *so) 272{ 273 274 KASSERT(sotorawcb(so) != NULL, ("raw_ushutdown: rp == NULL")); 275 socantsendmore(so); 276 return (0); 277} 278 279static int 280raw_usockaddr(struct socket *so, struct sockaddr **nam) 281{ 282 struct rawcb *rp = sotorawcb(so); 283 284 KASSERT(rp != NULL, ("raw_usockaddr: rp == NULL")); 285 if (rp->rcb_laddr == 0) 286 return (EINVAL); 287 *nam = sodupsockaddr(rp->rcb_laddr, M_WAITOK); 288 return (0); 289} 290 291struct pr_usrreqs raw_usrreqs = { 292 .pru_abort = raw_uabort, 293 .pru_attach = raw_uattach, 294 .pru_bind = raw_ubind, 295 .pru_connect = raw_uconnect, 296 .pru_detach = raw_udetach, 297 .pru_disconnect = raw_udisconnect, 298 .pru_peeraddr = raw_upeeraddr, 299 .pru_send = raw_usend, 300 .pru_shutdown = raw_ushutdown, 301 .pru_sockaddr = raw_usockaddr, 302 .pru_close = raw_uclose, 303}; 304