natm.c revision 157983
1/* $NetBSD: natm.c,v 1.5 1996/11/09 03:26:26 chuck Exp $ */ 2/*- 3 * 4 * Copyright (c) 1996 Charles D. Cranor and Washington University. 5 * Copyright (c) 2005-2006 Robert N. M. Watson 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Charles D. Cranor and 19 * Washington University. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35/* 36 * natm.c: native mode ATM access (both aal0 and aal5). 37 */ 38 39#include <sys/cdefs.h> 40__FBSDID("$FreeBSD: head/sys/netnatm/natm.c 157983 2006-04-23 16:04:07Z rwatson $"); 41 42#include <sys/param.h> 43#include <sys/conf.h> 44#include <sys/kernel.h> 45#include <sys/lock.h> 46#include <sys/malloc.h> 47#include <sys/mbuf.h> 48#include <sys/protosw.h> 49#include <sys/signalvar.h> 50#include <sys/socket.h> 51#include <sys/socketvar.h> 52#include <sys/sockio.h> 53#include <sys/sx.h> 54#include <sys/systm.h> 55#include <sys/sysctl.h> 56 57#include <net/if.h> 58#include <net/if_atm.h> 59#include <net/netisr.h> 60 61#include <netinet/in.h> 62 63#include <netnatm/natm.h> 64 65static const u_long natm5_sendspace = 16*1024; 66static const u_long natm5_recvspace = 16*1024; 67 68static const u_long natm0_sendspace = 16*1024; 69static const u_long natm0_recvspace = 16*1024; 70 71struct mtx natm_mtx; 72 73/* 74 * user requests 75 */ 76static int natm_usr_attach(struct socket *, int, d_thread_t *); 77static void natm_usr_detach(struct socket *); 78static int natm_usr_connect(struct socket *, struct sockaddr *, d_thread_t *); 79static int natm_usr_disconnect(struct socket *); 80static int natm_usr_shutdown(struct socket *); 81static int natm_usr_send(struct socket *, int, struct mbuf *, 82 struct sockaddr *, struct mbuf *, d_thread_t *); 83static int natm_usr_peeraddr(struct socket *, struct sockaddr **); 84static int natm_usr_control(struct socket *, u_long, caddr_t, 85 struct ifnet *, d_thread_t *); 86static void natm_usr_abort(struct socket *); 87static int natm_usr_bind(struct socket *, struct sockaddr *, d_thread_t *); 88static int natm_usr_sockaddr(struct socket *, struct sockaddr **); 89 90static int 91natm_usr_attach(struct socket *so, int proto, d_thread_t *p) 92{ 93 struct natmpcb *npcb; 94 int error = 0; 95 96 npcb = (struct natmpcb *)so->so_pcb; 97 KASSERT(npcb == NULL, ("natm_usr_attach: so_pcb != NULL")); 98 99 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 100 if (proto == PROTO_NATMAAL5) 101 error = soreserve(so, natm5_sendspace, natm5_recvspace); 102 else 103 error = soreserve(so, natm0_sendspace, natm0_recvspace); 104 if (error) 105 goto out; 106 } 107 108 so->so_pcb = (caddr_t) (npcb = npcb_alloc(M_WAITOK)); 109 npcb->npcb_socket = so; 110out: 111 return (error); 112} 113 114static void 115natm_usr_detach(struct socket *so) 116{ 117 struct natmpcb *npcb; 118 119 npcb = (struct natmpcb *)so->so_pcb; 120 KASSERT(npcb != NULL, ("natm_usr_detach: npcb == NULL")); 121 122 NATM_LOCK(); 123 npcb_free(npcb, NPCB_DESTROY); /* drain */ 124 so->so_pcb = NULL; 125 NATM_UNLOCK(); 126} 127 128static int 129natm_usr_connect(struct socket *so, struct sockaddr *nam, d_thread_t *p) 130{ 131 struct natmpcb *npcb; 132 struct sockaddr_natm *snatm; 133 struct atmio_openvcc op; 134 struct ifnet *ifp; 135 int error = 0; 136 int proto = so->so_proto->pr_protocol; 137 138 npcb = (struct natmpcb *)so->so_pcb; 139 KASSERT(npcb != NULL, ("natm_usr_connect: npcb == NULL")); 140 141 /* 142 * validate nam and npcb 143 */ 144 NATM_LOCK(); 145 snatm = (struct sockaddr_natm *)nam; 146 if (snatm->snatm_len != sizeof(*snatm) || 147 (npcb->npcb_flags & NPCB_FREE) == 0) { 148 error = EINVAL; 149 goto out; 150 } 151 if (snatm->snatm_family != AF_NATM) { 152 error = EAFNOSUPPORT; 153 goto out; 154 } 155 156 snatm->snatm_if[IFNAMSIZ - 1] = '\0'; /* XXX ensure null termination 157 since ifunit() uses strcmp */ 158 159 /* 160 * convert interface string to ifp, validate. 161 */ 162 ifp = ifunit(snatm->snatm_if); 163 if (ifp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 164 error = ENXIO; 165 goto out; 166 } 167 if (ifp->if_output != atm_output) { 168 error = EAFNOSUPPORT; 169 goto out; 170 } 171 172 /* 173 * register us with the NATM PCB layer 174 */ 175 if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) { 176 error = EADDRINUSE; 177 goto out; 178 } 179 NATM_UNLOCK(); 180 181 /* 182 * open the channel 183 */ 184 bzero(&op, sizeof(op)); 185 op.rxhand = npcb; 186 op.param.flags = ATMIO_FLAG_PVC; 187 op.param.vpi = npcb->npcb_vpi; 188 op.param.vci = npcb->npcb_vci; 189 op.param.rmtu = op.param.tmtu = ifp->if_mtu; 190 op.param.aal = (proto == PROTO_NATMAAL5) ? ATMIO_AAL_5 : ATMIO_AAL_0; 191 op.param.traffic = ATMIO_TRAFFIC_UBR; 192 193 IFF_LOCKGIANT(ifp); 194 if (ifp->if_ioctl == NULL || 195 ifp->if_ioctl(ifp, SIOCATMOPENVCC, (caddr_t)&op) != 0) { 196 IFF_UNLOCKGIANT(ifp); 197 error = EIO; 198 goto out; 199 } 200 IFF_UNLOCKGIANT(ifp); 201 202 soisconnected(so); 203 204 out: 205 return (error); 206} 207 208static int 209natm_usr_disconnect(struct socket *so) 210{ 211 struct natmpcb *npcb; 212 struct atmio_closevcc cl; 213 struct ifnet *ifp; 214 int error = 0; 215 216 npcb = (struct natmpcb *)so->so_pcb; 217 KASSERT(npcb != NULL, ("natm_usr_disconnect: npcb == NULL")); 218 219 NATM_LOCK(); 220 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) { 221 printf("natm: disconnected check\n"); 222 error = EIO; 223 goto out; 224 } 225 ifp = npcb->npcb_ifp; 226 227 /* 228 * disable rx 229 */ 230 cl.vpi = npcb->npcb_vpi; 231 cl.vci = npcb->npcb_vci; 232 NATM_UNLOCK(); 233 if (ifp->if_ioctl != NULL) { 234 IFF_LOCKGIANT(ifp); 235 ifp->if_ioctl(ifp, SIOCATMCLOSEVCC, (caddr_t)&cl); 236 IFF_UNLOCKGIANT(ifp); 237 } 238 NATM_LOCK(); 239 240 soisdisconnected(so); 241 242 out: 243 NATM_UNLOCK(); 244 return (error); 245} 246 247static int 248natm_usr_shutdown(struct socket *so) 249{ 250 socantsendmore(so); 251 return (0); 252} 253 254static int 255natm_usr_send(struct socket *so, int flags, struct mbuf *m, 256 struct sockaddr *nam, struct mbuf *control, d_thread_t *p) 257{ 258 struct natmpcb *npcb; 259 struct atm_pseudohdr *aph; 260 int error = 0; 261 int proto = so->so_proto->pr_protocol; 262 263 npcb = (struct natmpcb *)so->so_pcb; 264 KASSERT(npcb != NULL, ("natm_usr_send: npcb == NULL")); 265 266 NATM_LOCK(); 267 if (control && control->m_len) { 268 m_freem(control); 269 m_freem(m); 270 error = EINVAL; 271 goto out; 272 } 273 274 /* 275 * send the data. we must put an atm_pseudohdr on first 276 */ 277 M_PREPEND(m, sizeof(*aph), M_DONTWAIT); 278 if (m == NULL) { 279 m_freem(control); 280 error = ENOBUFS; 281 goto out; 282 } 283 aph = mtod(m, struct atm_pseudohdr *); 284 ATM_PH_VPI(aph) = npcb->npcb_vpi; 285 ATM_PH_SETVCI(aph, npcb->npcb_vci); 286 ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0; 287 288 error = atm_output(npcb->npcb_ifp, m, NULL, NULL); 289 290 out: 291 NATM_UNLOCK(); 292 return (error); 293} 294 295static int 296natm_usr_peeraddr(struct socket *so, struct sockaddr **nam) 297{ 298 struct natmpcb *npcb; 299 struct sockaddr_natm *snatm, ssnatm; 300 301 npcb = (struct natmpcb *)so->so_pcb; 302 KASSERT(npcb != NULL, ("natm_usr_peeraddr: npcb == NULL")); 303 304 NATM_LOCK(); 305 snatm = &ssnatm; 306 bzero(snatm, sizeof(*snatm)); 307 snatm->snatm_len = sizeof(*snatm); 308 snatm->snatm_family = AF_NATM; 309 strlcpy(snatm->snatm_if, npcb->npcb_ifp->if_xname, 310 sizeof(snatm->snatm_if)); 311 snatm->snatm_vci = npcb->npcb_vci; 312 snatm->snatm_vpi = npcb->npcb_vpi; 313 NATM_UNLOCK(); 314 *nam = sodupsockaddr((struct sockaddr *)snatm, M_WAITOK); 315 return (0); 316} 317 318static int 319natm_usr_control(struct socket *so, u_long cmd, caddr_t arg, 320 struct ifnet *ifp, d_thread_t *p) 321{ 322 struct natmpcb *npcb; 323 int error; 324 325 npcb = (struct natmpcb *)so->so_pcb; 326 KASSERT(npcb != NULL, ("natm_usr_control: npcb == NULL")); 327 328 if (ifp == NULL || ifp->if_ioctl == NULL) 329 return (EOPNOTSUPP); 330 331 IFF_LOCKGIANT(ifp); 332 error = ((*ifp->if_ioctl)(ifp, cmd, arg)); 333 IFF_UNLOCKGIANT(ifp); 334 return (error); 335} 336 337static void 338natm_usr_abort(struct socket *so) 339{ 340 natm_usr_detach(so); 341} 342 343static int 344natm_usr_bind(struct socket *so, struct sockaddr *nam, d_thread_t *p) 345{ 346 return (EOPNOTSUPP); 347} 348 349static int 350natm_usr_sockaddr(struct socket *so, struct sockaddr **nam) 351{ 352 return (EOPNOTSUPP); 353} 354 355/* xxx - should be const */ 356struct pr_usrreqs natm_usrreqs = { 357 .pru_abort = natm_usr_abort, 358 .pru_attach = natm_usr_attach, 359 .pru_bind = natm_usr_bind, 360 .pru_connect = natm_usr_connect, 361 .pru_control = natm_usr_control, 362 .pru_detach = natm_usr_detach, 363 .pru_disconnect = natm_usr_disconnect, 364 .pru_peeraddr = natm_usr_peeraddr, 365 .pru_send = natm_usr_send, 366 .pru_shutdown = natm_usr_shutdown, 367 .pru_sockaddr = natm_usr_sockaddr, 368}; 369 370/* 371 * natmintr: interrupt 372 * 373 * note: we expect a socket pointer in rcvif rather than an interface 374 * pointer. we can get the interface pointer from the so's PCB if 375 * we really need it. 376 */ 377void 378natmintr(struct mbuf *m) 379{ 380 struct socket *so; 381 struct natmpcb *npcb; 382 383#ifdef DIAGNOSTIC 384 M_ASSERTPKTHDR(m); 385#endif 386 387 NATM_LOCK(); 388 npcb = (struct natmpcb *)m->m_pkthdr.rcvif; /* XXX: overloaded */ 389 so = npcb->npcb_socket; 390 391 npcb->npcb_inq--; 392 393 if (npcb->npcb_flags & NPCB_DRAIN) { 394 if (npcb->npcb_inq == 0) 395 FREE(npcb, M_PCB); /* done! */ 396 NATM_UNLOCK(); 397 m_freem(m); 398 return; 399 } 400 401 if (npcb->npcb_flags & NPCB_FREE) { 402 NATM_UNLOCK(); 403 m_freem(m); /* drop */ 404 return; 405 } 406 407#ifdef NEED_TO_RESTORE_IFP 408 m->m_pkthdr.rcvif = npcb->npcb_ifp; 409#else 410#ifdef DIAGNOSTIC 411 m->m_pkthdr.rcvif = NULL; /* null it out to be safe */ 412#endif 413#endif 414 415 if (sbspace(&so->so_rcv) > m->m_pkthdr.len) { 416#ifdef NATM_STAT 417 natm_sookcnt++; 418 natm_sookbytes += m->m_pkthdr.len; 419#endif 420 sbappendrecord(&so->so_rcv, m); 421 sorwakeup(so); 422 NATM_UNLOCK(); 423 } else { 424#ifdef NATM_STAT 425 natm_sodropcnt++; 426 natm_sodropbytes += m->m_pkthdr.len; 427#endif 428 NATM_UNLOCK(); 429 m_freem(m); 430 } 431} 432 433/* 434 * natm0_sysctl: not used, but here in case we want to add something 435 * later... 436 */ 437int 438natm0_sysctl(SYSCTL_HANDLER_ARGS) 439{ 440 /* All sysctl names at this level are terminal. */ 441 return (ENOENT); 442} 443 444/* 445 * natm5_sysctl: not used, but here in case we want to add something 446 * later... 447 */ 448int 449natm5_sysctl(SYSCTL_HANDLER_ARGS) 450{ 451 /* All sysctl names at this level are terminal. */ 452 return (ENOENT); 453} 454