1 /* $NetBSD: raw_usrreq.c,v 1.36 2011/01/11 10:52:42 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)raw_usrreq.c 8.1 (Berkeley) 6/10/93 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: raw_usrreq.c,v 1.36 2011/01/11 10:52:42 pooka Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/mbuf.h> 39 #include <sys/domain.h> 40 #include <sys/protosw.h> 41 #include <sys/socket.h> 42 #include <sys/socketvar.h> 43 #include <sys/errno.h> 44 #include <sys/systm.h> 45 #include <sys/proc.h> 46 #include <sys/kauth.h> 47 48 #include <net/if.h> 49 #include <net/route.h> 50 #include <net/netisr.h> 51 #include <net/raw_cb.h> 52 53 #include <machine/stdarg.h> 54 55 /* 56 * Initialize raw connection block q. 57 */ 58 void 59 raw_init(void) 60 { 61 62 LIST_INIT(&rawcb); 63 } 64 65 static inline int 66 equal(const struct sockaddr *a1, const struct sockaddr *a2) 67 { 68 return memcmp(a1, a2, a1->sa_len) == 0; 69 } 70 71 /* 72 * Raw protocol input routine. Find the socket 73 * associated with the packet(s) and move them over. If 74 * nothing exists for this packet, drop it. 75 */ 76 /* 77 * Raw protocol interface. 78 */ 79 void 80 raw_input(struct mbuf *m0, ...) 81 { 82 struct rawcb *rp; 83 struct mbuf *m = m0; 84 struct socket *last; 85 va_list ap; 86 struct sockproto *proto; 87 struct sockaddr *src, *dst; 88 89 KASSERT(mutex_owned(softnet_lock)); 90 91 va_start(ap, m0); 92 proto = va_arg(ap, struct sockproto *); 93 src = va_arg(ap, struct sockaddr *); 94 dst = va_arg(ap, struct sockaddr *); 95 va_end(ap); 96 97 last = NULL; 98 LIST_FOREACH(rp, &rawcb, rcb_list) { 99 if (rp->rcb_proto.sp_family != proto->sp_family) 100 continue; 101 if (rp->rcb_proto.sp_protocol && 102 rp->rcb_proto.sp_protocol != proto->sp_protocol) 103 continue; 104 /* 105 * We assume the lower level routines have 106 * placed the address in a canonical format 107 * suitable for a structure comparison. 108 * 109 * Note that if the lengths are not the same 110 * the comparison will fail at the first byte. 111 */ 112 if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst)) 113 continue; 114 if (rp->rcb_faddr && !equal(rp->rcb_faddr, src)) 115 continue; 116 if (last != NULL) { 117 struct mbuf *n; 118 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) 119 ; 120 else if (sbappendaddr(&last->so_rcv, src, n, NULL) == 0) 121 /* should notify about lost packet */ 122 m_freem(n); 123 else { 124 sorwakeup(last); 125 } 126 } 127 last = rp->rcb_socket; 128 } 129 if (last == NULL || sbappendaddr(&last->so_rcv, src, m, NULL) == 0) 130 m_freem(m); 131 else { 132 sorwakeup(last); 133 } 134 } 135 136 /*ARGSUSED*/ 137 void * 138 raw_ctlinput(int cmd, const struct sockaddr *arg, void *d) 139 { 140 141 if ((unsigned)cmd >= PRC_NCMDS) 142 return NULL; 143 return NULL; 144 /* INCOMPLETE */ 145 } 146 147 void 148 raw_setsockaddr(struct rawcb *rp, struct mbuf *nam) 149 { 150 151 nam->m_len = rp->rcb_laddr->sa_len; 152 memcpy(mtod(nam, void *), rp->rcb_laddr, (size_t)nam->m_len); 153 } 154 155 void 156 raw_setpeeraddr(struct rawcb *rp, struct mbuf *nam) 157 { 158 159 nam->m_len = rp->rcb_faddr->sa_len; 160 memcpy(mtod(nam, void *), rp->rcb_faddr, (size_t)nam->m_len); 161 } 162 163 /*ARGSUSED*/ 164 int 165 raw_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, 166 struct mbuf *control, struct lwp *l) 167 { 168 struct rawcb *rp; 169 int s; 170 int error = 0; 171 172 if (req == PRU_CONTROL) 173 return (EOPNOTSUPP); 174 175 s = splsoftnet(); 176 KERNEL_LOCK(1, NULL); 177 rp = sotorawcb(so); 178 #ifdef DIAGNOSTIC 179 if (req != PRU_SEND && req != PRU_SENDOOB && control) 180 panic("raw_usrreq: unexpected control mbuf"); 181 #endif 182 if (rp == NULL && req != PRU_ATTACH) { 183 error = EINVAL; 184 goto release; 185 } 186 187 switch (req) { 188 189 /* 190 * Allocate a raw control block and fill in the 191 * necessary info to allow packets to be routed to 192 * the appropriate raw interface routine. 193 */ 194 case PRU_ATTACH: 195 sosetlock(so); 196 if (l == NULL) 197 break; 198 199 /* XXX: raw socket permissions are checked in socreate() */ 200 201 error = raw_attach(so, (int)(long)nam); 202 break; 203 204 /* 205 * Destroy state just before socket deallocation. 206 * Flush data or not depending on the options. 207 */ 208 case PRU_DETACH: 209 raw_detach(rp); 210 break; 211 212 /* 213 * If a socket isn't bound to a single address, 214 * the raw input routine will hand it anything 215 * within that protocol family (assuming there's 216 * nothing else around it should go to). 217 */ 218 case PRU_BIND: 219 case PRU_LISTEN: 220 case PRU_CONNECT: 221 case PRU_CONNECT2: 222 error = EOPNOTSUPP; 223 break; 224 225 case PRU_DISCONNECT: 226 soisdisconnected(so); 227 raw_disconnect(rp); 228 break; 229 230 /* 231 * Mark the connection as being incapable of further input. 232 */ 233 case PRU_SHUTDOWN: 234 socantsendmore(so); 235 break; 236 237 case PRU_RCVD: 238 error = EOPNOTSUPP; 239 break; 240 241 /* 242 * Ship a packet out. The appropriate raw output 243 * routine handles any massaging necessary. 244 */ 245 case PRU_SEND: 246 if (control && control->m_len) { 247 m_freem(control); 248 m_freem(m); 249 error = EINVAL; 250 break; 251 } 252 if (nam) { 253 if ((so->so_state & SS_ISCONNECTED) != 0) { 254 error = EISCONN; 255 goto die; 256 } 257 error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, 258 NULL, nam, NULL, l); 259 if (error) { 260 die: 261 m_freem(m); 262 break; 263 } 264 } else { 265 if ((so->so_state & SS_ISCONNECTED) == 0) { 266 error = ENOTCONN; 267 goto die; 268 } 269 } 270 error = (*so->so_proto->pr_output)(m, so); 271 if (nam) 272 raw_disconnect(rp); 273 break; 274 275 case PRU_SENSE: 276 /* 277 * stat: don't bother with a blocksize. 278 */ 279 error = 0; 280 break; 281 282 /* 283 * Not supported. 284 */ 285 case PRU_RCVOOB: 286 error = EOPNOTSUPP; 287 break; 288 289 case PRU_SENDOOB: 290 m_freem(control); 291 m_freem(m); 292 error = EOPNOTSUPP; 293 break; 294 295 case PRU_SOCKADDR: 296 if (rp->rcb_laddr == NULL) { 297 error = EINVAL; 298 break; 299 } 300 raw_setsockaddr(rp, nam); 301 break; 302 303 case PRU_PEERADDR: 304 if (rp->rcb_faddr == NULL) { 305 error = ENOTCONN; 306 break; 307 } 308 raw_setpeeraddr(rp, nam); 309 break; 310 311 default: 312 panic("raw_usrreq"); 313 } 314 315 release: 316 KERNEL_UNLOCK_ONE(NULL); 317 splx(s); 318 return (error); 319 } 320