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*37500Smckusick * @(#)raw_cb.c 7.9 (Berkeley) 04/25/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 34*37500Smckusick #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 4536823Skarels u_long raw_sendspace = RAWSNDQ; 4636823Skarels u_long raw_recvspace = RAWRCVQ; 4736823Skarels 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 { 5637472Ssklower register struct rawcb *rp = sotorawcb(so); 575635Sroot 5837472Ssklower /* 5937472Ssklower * It is assumed that raw_attach is called 6037472Ssklower * after space has been allocated for the 6137472Ssklower * rawcb. 6237472Ssklower */ 6337472Ssklower if (rp == 0) 645635Sroot return (ENOBUFS); 6536823Skarels if (sbreserve(&so->so_snd, raw_sendspace) == 0) 665635Sroot goto bad; 6736823Skarels if (sbreserve(&so->so_rcv, raw_recvspace) == 0) 685635Sroot goto bad2; 695635Sroot rp->rcb_socket = so; 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 return (ENOBUFS); 785635Sroot } 795635Sroot 805635Sroot /* 815635Sroot * Detach the raw connection block and discard 825635Sroot * socket resources. 835635Sroot */ 845635Sroot raw_detach(rp) 855635Sroot register struct rawcb *rp; 865635Sroot { 875635Sroot struct socket *so = rp->rcb_socket; 885635Sroot 895635Sroot so->so_pcb = 0; 905635Sroot sofree(so); 915635Sroot remque(rp); 9237472Ssklower #ifdef notdef 9337472Ssklower if (rp->rcb_laddr) 9437472Ssklower m_freem(dtom(rp->rcb_laddr)); 9537472Ssklower rp->rcb_laddr = 0; 9637472Ssklower #endif 9737472Ssklower free((caddr_t)(rp), M_PCB); 985635Sroot } 995635Sroot 1005635Sroot /* 1015635Sroot * Disconnect and possibly release resources. 1025635Sroot */ 1035635Sroot raw_disconnect(rp) 1045635Sroot struct rawcb *rp; 1055635Sroot { 1068975Sroot 10737472Ssklower #ifdef notdef 10837472Ssklower if (rp->rcb_faddr) 10937472Ssklower m_freem(dtom(rp->rcb_faddr)); 11037472Ssklower rp->rcb_faddr = 0; 11137472Ssklower #endif 1127517Sroot if (rp->rcb_socket->so_state & SS_NOFDREF) 1135635Sroot raw_detach(rp); 1145635Sroot } 1155635Sroot 11637472Ssklower #ifdef notdef 1178394Swnj raw_bind(so, nam) 1188394Swnj register struct socket *so; 1198394Swnj struct mbuf *nam; 1208394Swnj { 1218394Swnj struct sockaddr *addr = mtod(nam, struct sockaddr *); 1228394Swnj register struct rawcb *rp; 1238394Swnj 1248394Swnj if (ifnet == 0) 1258394Swnj return (EADDRNOTAVAIL); 1269184Ssam rp = sotorawcb(so); 12737472Ssklower nam = m_copym(nam, 0, M_COPYALL, M_WAITOK); 12837472Ssklower rp->rcb_laddr = mtod(nam, struct sockaddr *); 1298394Swnj return (0); 1308394Swnj } 13137472Ssklower #endif 132