Commit | Line | Data |
---|---|---|
f832270b | 1 | /* raw_cb.c 4.11 82/07/24 */ |
785d93ac BJ |
2 | |
3 | #include "../h/param.h" | |
4 | #include "../h/systm.h" | |
5 | #include "../h/mbuf.h" | |
6 | #include "../h/socket.h" | |
7 | #include "../h/socketvar.h" | |
8 | #include "../h/mtpr.h" | |
9 | #include "../net/in.h" | |
10 | #include "../net/in_systm.h" | |
11 | #include "../net/if.h" | |
12 | #include "../net/raw_cb.h" | |
ee787340 | 13 | #include "../net/pup.h" |
c47a5909 | 14 | #include <errno.h> |
785d93ac BJ |
15 | |
16 | /* | |
17 | * Routines to manage the raw protocol control blocks. | |
18 | * | |
19 | * TODO: | |
20 | * hash lookups by protocol family/protocol + address family | |
ee787340 | 21 | * take care of unique address problems per AF? |
1e25d807 | 22 | * redo address binding to allow wildcards |
785d93ac BJ |
23 | */ |
24 | ||
25 | /* | |
26 | * Allocate a control block and a nominal amount | |
27 | * of buffer space for the socket. | |
28 | */ | |
29 | raw_attach(so, addr) | |
30 | register struct socket *so; | |
31 | struct sockaddr *addr; | |
32 | { | |
33 | struct mbuf *m; | |
34 | register struct rawcb *rp; | |
785d93ac | 35 | |
ee787340 SL |
36 | if (ifnet == 0) |
37 | return (EADDRNOTAVAIL); | |
785d93ac BJ |
38 | /* |
39 | * Should we verify address not already in use? | |
40 | * Some say yes, others no. | |
41 | */ | |
42 | if (addr) switch (addr->sa_family) { | |
43 | ||
a2cd4df7 | 44 | case AF_IMPLINK: |
ee787340 SL |
45 | case AF_INET: |
46 | if (((struct sockaddr_in *)addr)->sin_addr.s_addr && | |
47 | if_ifwithaddr(addr) == 0) | |
48 | return (EADDRNOTAVAIL); | |
785d93ac | 49 | break; |
785d93ac | 50 | |
ee787340 SL |
51 | #ifdef PUP |
52 | /* | |
53 | * Curious, we convert PUP address format to internet | |
54 | * to allow us to verify we're asking for an Ethernet | |
55 | * interface. This is wrong, but things are heavily | |
56 | * oriented towards the internet addressing scheme, and | |
57 | * converting internet to PUP would be very expensive. | |
58 | */ | |
59 | case AF_PUP: { | |
60 | struct sockaddr_pup *spup = (struct sockaddr_pup *)addr; | |
61 | struct sockaddr_in inpup; | |
62 | ||
63 | bzero((caddr_t)&inpup, sizeof(inpup)); | |
126472ab | 64 | inpup.sin_family = AF_INET; |
ee787340 SL |
65 | inpup.sin_addr.s_net = spup->sp_net; |
66 | inpup.sin_addr.s_impno = spup->sp_host; | |
67 | if (inpup.sin_addr.s_addr && | |
68 | if_ifwithaddr((struct sockaddr *)&inpup) == 0) | |
69 | return (EADDRNOTAVAIL); | |
1e25d807 | 70 | break; |
ee787340 SL |
71 | } |
72 | #endif | |
1e25d807 | 73 | |
785d93ac BJ |
74 | default: |
75 | return (EAFNOSUPPORT); | |
76 | } | |
785d93ac BJ |
77 | m = m_getclr(M_DONTWAIT); |
78 | if (m == 0) | |
79 | return (ENOBUFS); | |
80 | if (sbreserve(&so->so_snd, RAWSNDQ) == 0) | |
81 | goto bad; | |
82 | if (sbreserve(&so->so_rcv, RAWRCVQ) == 0) | |
83 | goto bad2; | |
84 | rp = mtod(m, struct rawcb *); | |
85 | rp->rcb_socket = so; | |
86 | insque(rp, &rawcb); | |
87 | so->so_pcb = (caddr_t)rp; | |
88 | rp->rcb_pcb = 0; | |
02122dcc | 89 | if (addr) { |
126472ab | 90 | bcopy((caddr_t)addr, (caddr_t)&rp->rcb_laddr, sizeof(*addr)); |
02122dcc SL |
91 | rp->rcb_flags |= RAW_LADDR; |
92 | } | |
785d93ac BJ |
93 | return (0); |
94 | bad2: | |
95 | sbrelease(&so->so_snd); | |
96 | bad: | |
97 | (void) m_free(m); | |
98 | return (ENOBUFS); | |
99 | } | |
100 | ||
101 | /* | |
102 | * Detach the raw connection block and discard | |
103 | * socket resources. | |
104 | */ | |
105 | raw_detach(rp) | |
106 | register struct rawcb *rp; | |
107 | { | |
108 | struct socket *so = rp->rcb_socket; | |
109 | ||
785d93ac BJ |
110 | so->so_pcb = 0; |
111 | sofree(so); | |
112 | remque(rp); | |
ee787340 | 113 | (void) m_freem(dtom(rp)); |
785d93ac BJ |
114 | } |
115 | ||
116 | /* | |
117 | * Disconnect and possibly release resources. | |
118 | */ | |
119 | raw_disconnect(rp) | |
120 | struct rawcb *rp; | |
121 | { | |
126472ab | 122 | rp->rcb_flags &= ~RAW_FADDR; |
f832270b | 123 | if (rp->rcb_socket->so_state & SS_NOFDREF) |
785d93ac BJ |
124 | raw_detach(rp); |
125 | } | |
126 | ||
127 | /* | |
128 | * Associate a peer's address with a | |
129 | * raw connection block. | |
130 | */ | |
131 | raw_connaddr(rp, addr) | |
132 | struct rawcb *rp; | |
133 | struct sockaddr *addr; | |
134 | { | |
126472ab SL |
135 | bcopy((caddr_t)addr, (caddr_t)&rp->rcb_faddr, sizeof(*addr)); |
136 | rp->rcb_flags |= RAW_FADDR; | |
785d93ac | 137 | } |