123160Smckusick /* 229066Smckusick * Copyright (c) 1980, 1986 Regents of the University of California. 333183Sbostic * All rights reserved. 423160Smckusick * 533183Sbostic * Redistribution and use in source and binary forms are permitted 634844Sbostic * provided that the above copyright notice and this paragraph are 734844Sbostic * duplicated in all such forms and that any documentation, 834844Sbostic * advertising materials, and other materials related to such 934844Sbostic * distribution and use acknowledge that the software was developed 1034844Sbostic * by the University of California, Berkeley. The name of the 1134844Sbostic * University may not be used to endorse or promote products derived 1234844Sbostic * from this software without specific prior written permission. 1334844Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434844Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534844Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1633183Sbostic * 17*36823Skarels * @(#)raw_cb.c 7.7 (Berkeley) 02/17/89 1823160Smckusick */ 195635Sroot 2017066Sbloom #include "param.h" 2117066Sbloom #include "systm.h" 2217066Sbloom #include "mbuf.h" 2317066Sbloom #include "socket.h" 2417066Sbloom #include "socketvar.h" 2521769Skarels #include "domain.h" 2621769Skarels #include "protosw.h" 2717066Sbloom #include "errno.h" 2810890Ssam 2917066Sbloom #include "if.h" 3017066Sbloom #include "route.h" 3117066Sbloom #include "raw_cb.h" 3212783Ssam #include "../netinet/in.h" 335635Sroot 3429922Skarels #include "../machine/mtpr.h" 3510890Ssam 365635Sroot /* 375635Sroot * Routines to manage the raw protocol control blocks. 385635Sroot * 395635Sroot * TODO: 405635Sroot * hash lookups by protocol family/protocol + address family 416339Ssam * take care of unique address problems per AF? 426045Swnj * redo address binding to allow wildcards 435635Sroot */ 445635Sroot 45*36823Skarels u_long raw_sendspace = RAWSNDQ; 46*36823Skarels u_long raw_recvspace = RAWRCVQ; 47*36823Skarels 485635Sroot /* 495635Sroot * Allocate a control block and a nominal amount 505635Sroot * of buffer space for the socket. 515635Sroot */ 5221769Skarels raw_attach(so, proto) 535635Sroot register struct socket *so; 5421769Skarels int proto; 555635Sroot { 565635Sroot struct mbuf *m; 575635Sroot register struct rawcb *rp; 585635Sroot 599638Ssam m = m_getclr(M_DONTWAIT, MT_PCB); 605635Sroot if (m == 0) 615635Sroot return (ENOBUFS); 62*36823Skarels if (sbreserve(&so->so_snd, raw_sendspace) == 0) 635635Sroot goto bad; 64*36823Skarels if (sbreserve(&so->so_rcv, raw_recvspace) == 0) 655635Sroot goto bad2; 665635Sroot rp = mtod(m, struct rawcb *); 675635Sroot rp->rcb_socket = so; 685635Sroot so->so_pcb = (caddr_t)rp; 695635Sroot rp->rcb_pcb = 0; 7021769Skarels rp->rcb_proto.sp_family = so->so_proto->pr_domain->dom_family; 7121769Skarels rp->rcb_proto.sp_protocol = proto; 7221769Skarels insque(rp, &rawcb); 735635Sroot return (0); 745635Sroot bad2: 755635Sroot sbrelease(&so->so_snd); 765635Sroot bad: 775635Sroot (void) m_free(m); 785635Sroot return (ENOBUFS); 795635Sroot } 805635Sroot 815635Sroot /* 825635Sroot * Detach the raw connection block and discard 835635Sroot * socket resources. 845635Sroot */ 855635Sroot raw_detach(rp) 865635Sroot register struct rawcb *rp; 875635Sroot { 885635Sroot struct socket *so = rp->rcb_socket; 895635Sroot 9024774Skarels if (rp->rcb_route.ro_rt) 9124774Skarels rtfree(rp->rcb_route.ro_rt); 925635Sroot so->so_pcb = 0; 935635Sroot sofree(so); 945635Sroot remque(rp); 9526035Skarels if (rp->rcb_options) 9629569Skarels m_freem(rp->rcb_options); 978975Sroot m_freem(dtom(rp)); 985635Sroot } 995635Sroot 1005635Sroot /* 1015635Sroot * Disconnect and possibly release resources. 1025635Sroot */ 1035635Sroot raw_disconnect(rp) 1045635Sroot struct rawcb *rp; 1055635Sroot { 1068975Sroot 1076509Ssam rp->rcb_flags &= ~RAW_FADDR; 1087517Sroot if (rp->rcb_socket->so_state & SS_NOFDREF) 1095635Sroot raw_detach(rp); 1105635Sroot } 1115635Sroot 1128394Swnj raw_bind(so, nam) 1138394Swnj register struct socket *so; 1148394Swnj struct mbuf *nam; 1158394Swnj { 1168394Swnj struct sockaddr *addr = mtod(nam, struct sockaddr *); 1178394Swnj register struct rawcb *rp; 1188394Swnj 1198394Swnj if (ifnet == 0) 1208394Swnj return (EADDRNOTAVAIL); 1218394Swnj /* BEGIN DUBIOUS */ 1228394Swnj /* 1238394Swnj * Should we verify address not already in use? 1248394Swnj * Some say yes, others no. 1258394Swnj */ 1268394Swnj switch (addr->sa_family) { 1278394Swnj 12825647Skarels #ifdef INET 1298394Swnj case AF_IMPLINK: 13012783Ssam case AF_INET: { 1318394Swnj if (((struct sockaddr_in *)addr)->sin_addr.s_addr && 13218409Skarels ifa_ifwithaddr(addr) == 0) 1338394Swnj return (EADDRNOTAVAIL); 1348394Swnj break; 13512783Ssam } 13612783Ssam #endif 1378394Swnj 1388394Swnj default: 1398394Swnj return (EAFNOSUPPORT); 1408394Swnj } 1418394Swnj /* END DUBIOUS */ 1429184Ssam rp = sotorawcb(so); 1438394Swnj bcopy((caddr_t)addr, (caddr_t)&rp->rcb_laddr, sizeof (*addr)); 1448394Swnj rp->rcb_flags |= RAW_LADDR; 1458394Swnj return (0); 1468394Swnj } 1478394Swnj 1485635Sroot /* 1495635Sroot * Associate a peer's address with a 1505635Sroot * raw connection block. 1515635Sroot */ 1528394Swnj raw_connaddr(rp, nam) 1535635Sroot struct rawcb *rp; 1548394Swnj struct mbuf *nam; 1555635Sroot { 1568394Swnj struct sockaddr *addr = mtod(nam, struct sockaddr *); 1578394Swnj 1586509Ssam bcopy((caddr_t)addr, (caddr_t)&rp->rcb_faddr, sizeof(*addr)); 1596509Ssam rp->rcb_flags |= RAW_FADDR; 1605635Sroot } 161