1 /* raw_usrreq.c 4.19 82/10/09 */ 2 3 #include "../h/param.h" 4 #include "../h/mbuf.h" 5 #include "../h/protosw.h" 6 #include "../h/socket.h" 7 #include "../h/socketvar.h" 8 #include "../h/mtpr.h" 9 #include "../net/if.h" 10 #include "../net/netisr.h" 11 #include "../net/raw_cb.h" 12 #include <errno.h> 13 14 int rawqmaxlen = IFQ_MAXLEN; 15 16 /* 17 * Initialize raw connection block q. 18 */ 19 raw_init() 20 { 21 22 rawcb.rcb_next = rawcb.rcb_prev = &rawcb; 23 rawintrq.ifq_maxlen = IFQ_MAXLEN; 24 } 25 26 /* 27 * Raw protocol interface. 28 */ 29 raw_input(m0, proto, src, dst) 30 struct mbuf *m0; 31 struct sockproto *proto; 32 struct sockaddr *src, *dst; 33 { 34 register struct mbuf *m; 35 struct raw_header *rh; 36 int s; 37 38 /* 39 * Rip off an mbuf for a generic header. 40 */ 41 m = m_get(M_DONTWAIT); 42 if (m == 0) { 43 m_freem(m0); 44 return; 45 } 46 m->m_next = m0; 47 m->m_len = sizeof(struct raw_header); 48 rh = mtod(m, struct raw_header *); 49 rh->raw_dst = *dst; 50 rh->raw_src = *src; 51 rh->raw_proto = *proto; 52 53 /* 54 * Header now contains enough info to decide 55 * which socket to place packet in (if any). 56 * Queue it up for the raw protocol process 57 * running at software interrupt level. 58 */ 59 s = splimp(); 60 if (IF_QFULL(&rawintrq)) 61 m_freem(m); 62 else 63 IF_ENQUEUE(&rawintrq, m); 64 splx(s); 65 schednetisr(NETISR_RAW); 66 } 67 68 /* 69 * Raw protocol input routine. Process packets entered 70 * into the queue at interrupt time. Find the socket 71 * associated with the packet(s) and move them over. If 72 * nothing exists for this packet, drop it. 73 */ 74 rawintr() 75 { 76 int s; 77 struct mbuf *m; 78 register struct rawcb *rp; 79 register struct protosw *lproto; 80 register struct raw_header *rh; 81 struct socket *last; 82 83 next: 84 s = splimp(); 85 IF_DEQUEUE(&rawintrq, m); 86 splx(s); 87 if (m == 0) 88 return; 89 rh = mtod(m, struct raw_header *); 90 last = 0; 91 for (rp = rawcb.rcb_next; rp != &rawcb; rp = rp->rcb_next) { 92 lproto = rp->rcb_socket->so_proto; 93 if (lproto->pr_family != rh->raw_proto.sp_family) 94 continue; 95 if (lproto->pr_protocol && 96 lproto->pr_protocol != rh->raw_proto.sp_protocol) 97 continue; 98 /* 99 * We assume the lower level routines have 100 * placed the address in a canonical format 101 * suitable for a structure comparison. 102 */ 103 #define equal(a1, a2) \ 104 (bcmp((caddr_t)&(a1), (caddr_t)&(a2), sizeof (struct sockaddr)) == 0) 105 if ((rp->rcb_flags & RAW_LADDR) && 106 !equal(rp->rcb_laddr, rh->raw_dst)) 107 continue; 108 if ((rp->rcb_flags & RAW_FADDR) && 109 !equal(rp->rcb_faddr, rh->raw_src)) 110 continue; 111 if (last) { 112 struct mbuf *n; 113 if ((n = m_copy(m->m_next, 0, (int)M_COPYALL)) == 0) 114 goto nospace; 115 if (sbappendaddr(&last->so_rcv, &rh->raw_src, n)==0) { 116 /* should notify about lost packet */ 117 m_freem(n); 118 goto nospace; 119 } 120 sorwakeup(last); 121 } 122 nospace: 123 last = rp->rcb_socket; 124 } 125 if (last) { 126 m = m_free(m); /* header */ 127 if (sbappendaddr(&last->so_rcv, &rh->raw_src, m) == 0) 128 goto drop; 129 sorwakeup(last); 130 goto next; 131 } 132 drop: 133 m_freem(m); 134 goto next; 135 } 136 137 raw_ctlinput(cmd, arg) 138 int cmd; 139 caddr_t arg; 140 { 141 142 if (cmd < 0 || cmd > PRC_NCMDS) 143 return; 144 } 145 146 /*ARGSUSED*/ 147 raw_usrreq(so, req, m, nam, opt) 148 struct socket *so; 149 int req; 150 struct mbuf *m, *nam; 151 struct socketopt *opt; 152 { 153 register struct rawcb *rp = sotorawcb(so); 154 int error = 0; 155 156 if (rp == 0 && req != PRU_ATTACH) 157 return (EINVAL); 158 159 switch (req) { 160 161 /* 162 * Allocate a raw control block and fill in the 163 * necessary info to allow packets to be routed to 164 * the appropriate raw interface routine. 165 */ 166 case PRU_ATTACH: 167 if ((so->so_state & SS_PRIV) == 0) 168 return (EACCES); 169 if (rp) 170 return (EINVAL); 171 error = raw_attach(so); 172 break; 173 174 /* 175 * Destroy state just before socket deallocation. 176 * Flush data or not depending on the options. 177 */ 178 case PRU_DETACH: 179 if (rp == 0) 180 return (ENOTCONN); 181 raw_detach(rp); 182 break; 183 184 /* 185 * If a socket isn't bound to a single address, 186 * the raw input routine will hand it anything 187 * within that protocol family (assuming there's 188 * nothing else around it should go to). 189 */ 190 case PRU_CONNECT: 191 if (rp->rcb_flags & RAW_FADDR) 192 return (EISCONN); 193 raw_connaddr(rp, nam); 194 soisconnected(so); 195 break; 196 197 case PRU_DISCONNECT: 198 if ((rp->rcb_flags & RAW_FADDR) == 0) 199 return (ENOTCONN); 200 raw_disconnect(rp); 201 soisdisconnected(so); 202 break; 203 204 /* 205 * Mark the connection as being incapable of further input. 206 */ 207 case PRU_SHUTDOWN: 208 socantsendmore(so); 209 break; 210 211 /* 212 * Ship a packet out. The appropriate raw output 213 * routine handles any massaging necessary. 214 */ 215 case PRU_SEND: 216 if (nam) { 217 if (rp->rcb_flags & RAW_FADDR) 218 return (EISCONN); 219 raw_connaddr(rp, nam); 220 } else if ((rp->rcb_flags & RAW_FADDR) == 0) 221 return (ENOTCONN); 222 error = (*so->so_proto->pr_output)(m, so); 223 if (nam) 224 rp->rcb_flags &= ~RAW_FADDR; 225 break; 226 227 case PRU_ABORT: 228 raw_disconnect(rp); 229 sofree(so); 230 soisdisconnected(so); 231 break; 232 233 /* 234 * Not supported. 235 */ 236 case PRU_ACCEPT: 237 case PRU_RCVD: 238 case PRU_CONTROL: 239 case PRU_SENSE: 240 case PRU_RCVOOB: 241 case PRU_SENDOOB: 242 error = EOPNOTSUPP; 243 break; 244 245 case PRU_SOCKADDR: 246 bcopy((caddr_t)&rp->rcb_laddr, mtod(nam, struct sockaddr *), 247 sizeof (struct sockaddr)); 248 nam->m_len = sizeof (struct sockaddr); 249 break; 250 251 default: 252 panic("raw_usrreq"); 253 } 254 return (error); 255 } 256