| 1 | /* |
| 2 | * Copyright (c) 1980, 1986 Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms are permitted |
| 6 | * provided that the above copyright notice and this paragraph are |
| 7 | * duplicated in all such forms and that any documentation, |
| 8 | * advertising materials, and other materials related to such |
| 9 | * distribution and use acknowledge that the software was developed |
| 10 | * by the University of California, Berkeley. The name of the |
| 11 | * University may not be used to endorse or promote products derived |
| 12 | * from this software without specific prior written permission. |
| 13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 16 | * |
| 17 | * @(#)raw_cb.c 7.9 (Berkeley) %G% |
| 18 | */ |
| 19 | |
| 20 | #include "param.h" |
| 21 | #include "systm.h" |
| 22 | #include "mbuf.h" |
| 23 | #include "socket.h" |
| 24 | #include "socketvar.h" |
| 25 | #include "domain.h" |
| 26 | #include "protosw.h" |
| 27 | #include "errno.h" |
| 28 | |
| 29 | #include "if.h" |
| 30 | #include "route.h" |
| 31 | #include "raw_cb.h" |
| 32 | #include "../netinet/in.h" |
| 33 | |
| 34 | #include "machine/mtpr.h" |
| 35 | |
| 36 | /* |
| 37 | * Routines to manage the raw protocol control blocks. |
| 38 | * |
| 39 | * TODO: |
| 40 | * hash lookups by protocol family/protocol + address family |
| 41 | * take care of unique address problems per AF? |
| 42 | * redo address binding to allow wildcards |
| 43 | */ |
| 44 | |
| 45 | u_long raw_sendspace = RAWSNDQ; |
| 46 | u_long raw_recvspace = RAWRCVQ; |
| 47 | |
| 48 | /* |
| 49 | * Allocate a control block and a nominal amount |
| 50 | * of buffer space for the socket. |
| 51 | */ |
| 52 | raw_attach(so, proto) |
| 53 | register struct socket *so; |
| 54 | int proto; |
| 55 | { |
| 56 | register struct rawcb *rp = sotorawcb(so); |
| 57 | |
| 58 | /* |
| 59 | * It is assumed that raw_attach is called |
| 60 | * after space has been allocated for the |
| 61 | * rawcb. |
| 62 | */ |
| 63 | if (rp == 0) |
| 64 | return (ENOBUFS); |
| 65 | if (sbreserve(&so->so_snd, raw_sendspace) == 0) |
| 66 | goto bad; |
| 67 | if (sbreserve(&so->so_rcv, raw_recvspace) == 0) |
| 68 | goto bad2; |
| 69 | rp->rcb_socket = so; |
| 70 | rp->rcb_proto.sp_family = so->so_proto->pr_domain->dom_family; |
| 71 | rp->rcb_proto.sp_protocol = proto; |
| 72 | insque(rp, &rawcb); |
| 73 | return (0); |
| 74 | bad2: |
| 75 | sbrelease(&so->so_snd); |
| 76 | bad: |
| 77 | return (ENOBUFS); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Detach the raw connection block and discard |
| 82 | * socket resources. |
| 83 | */ |
| 84 | raw_detach(rp) |
| 85 | register struct rawcb *rp; |
| 86 | { |
| 87 | struct socket *so = rp->rcb_socket; |
| 88 | |
| 89 | so->so_pcb = 0; |
| 90 | sofree(so); |
| 91 | remque(rp); |
| 92 | #ifdef notdef |
| 93 | if (rp->rcb_laddr) |
| 94 | m_freem(dtom(rp->rcb_laddr)); |
| 95 | rp->rcb_laddr = 0; |
| 96 | #endif |
| 97 | free((caddr_t)(rp), M_PCB); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Disconnect and possibly release resources. |
| 102 | */ |
| 103 | raw_disconnect(rp) |
| 104 | struct rawcb *rp; |
| 105 | { |
| 106 | |
| 107 | #ifdef notdef |
| 108 | if (rp->rcb_faddr) |
| 109 | m_freem(dtom(rp->rcb_faddr)); |
| 110 | rp->rcb_faddr = 0; |
| 111 | #endif |
| 112 | if (rp->rcb_socket->so_state & SS_NOFDREF) |
| 113 | raw_detach(rp); |
| 114 | } |
| 115 | |
| 116 | #ifdef notdef |
| 117 | raw_bind(so, nam) |
| 118 | register struct socket *so; |
| 119 | struct mbuf *nam; |
| 120 | { |
| 121 | struct sockaddr *addr = mtod(nam, struct sockaddr *); |
| 122 | register struct rawcb *rp; |
| 123 | |
| 124 | if (ifnet == 0) |
| 125 | return (EADDRNOTAVAIL); |
| 126 | rp = sotorawcb(so); |
| 127 | nam = m_copym(nam, 0, M_COPYALL, M_WAITOK); |
| 128 | rp->rcb_laddr = mtod(nam, struct sockaddr *); |
| 129 | return (0); |
| 130 | } |
| 131 | #endif |