1*6584Ssam /* raw_usrreq.c 4.15 82/04/24 */ 25121Swnj 35121Swnj #include "../h/param.h" 45121Swnj #include "../h/mbuf.h" 55612Swnj #include "../h/protosw.h" 65121Swnj #include "../h/socket.h" 75121Swnj #include "../h/socketvar.h" 85121Swnj #include "../h/mtpr.h" 95121Swnj #include "../net/in.h" 105121Swnj #include "../net/in_systm.h" 115121Swnj #include "../net/if.h" 125612Swnj #include "../net/raw_cb.h" 136509Ssam #include <errno.h> 145121Swnj 156211Swnj int rawqmaxlen = IFQ_MAXLEN; 166211Swnj 175121Swnj /* 185612Swnj * Initialize raw connection block q. 196211Swnj */ 205612Swnj raw_init() 215612Swnj { 226211Swnj 235612Swnj COUNT(RAW_INIT); 245612Swnj rawcb.rcb_next = rawcb.rcb_prev = &rawcb; 256211Swnj rawintrq.ifq_maxlen = IFQ_MAXLEN; 265612Swnj } 275612Swnj 285612Swnj /* 295121Swnj * Raw protocol interface. 305121Swnj */ 316529Ssam raw_input(m0, proto, src, dst) 325612Swnj struct mbuf *m0; 336509Ssam struct sockproto *proto; 346529Ssam struct sockaddr *src, *dst; 355121Swnj { 365612Swnj register struct mbuf *m; 375612Swnj struct raw_header *rh; 385121Swnj int s; 395121Swnj 405668Ssam COUNT(RAW_INPUT); 415612Swnj /* 425612Swnj * Rip off an mbuf for a generic header. 435612Swnj */ 445612Swnj m = m_get(M_DONTWAIT); 455612Swnj if (m == 0) { 465612Swnj m_freem(m0); 475612Swnj return; 485612Swnj } 495612Swnj m->m_next = m0; 505612Swnj m->m_off = MMINOFF; 515612Swnj m->m_len = sizeof(struct raw_header); 525612Swnj rh = mtod(m, struct raw_header *); 536509Ssam rh->raw_dst = *dst; 546509Ssam rh->raw_src = *src; 556509Ssam rh->raw_proto = *proto; 565612Swnj 575612Swnj /* 585612Swnj * Header now contains enough info to decide 595612Swnj * which socket to place packet in (if any). 605612Swnj * Queue it up for the raw protocol process 615612Swnj * running at software interrupt level. 625612Swnj */ 635121Swnj s = splimp(); 646211Swnj if (IF_QFULL(&rawintrq)) 656211Swnj m_freem(m); 666211Swnj else 676211Swnj IF_ENQUEUE(&rawintrq, m); 685121Swnj splx(s); 696263Swnj schednetisr(NETISR_RAW); 705121Swnj } 715121Swnj 725612Swnj /* 735612Swnj * Raw protocol input routine. Process packets entered 745612Swnj * into the queue at interrupt time. Find the socket 755612Swnj * associated with the packet(s) and move them over. If 765612Swnj * nothing exists for this packet, drop it. 775612Swnj */ 785121Swnj rawintr() 795121Swnj { 805121Swnj int s; 815121Swnj struct mbuf *m; 825612Swnj register struct rawcb *rp; 836509Ssam register struct protosw *lproto; 846529Ssam register struct raw_header *rh; 855612Swnj struct socket *last; 865121Swnj 875121Swnj COUNT(RAWINTR); 885121Swnj next: 895121Swnj s = splimp(); 905121Swnj IF_DEQUEUE(&rawintrq, m); 915121Swnj splx(s); 925121Swnj if (m == 0) 935121Swnj return; 946509Ssam rh = mtod(m, struct raw_header *); 955612Swnj last = 0; 965612Swnj for (rp = rawcb.rcb_next; rp != &rawcb; rp = rp->rcb_next) { 976509Ssam lproto = rp->rcb_socket->so_proto; 986509Ssam if (lproto->pr_family != rh->raw_proto.sp_family) 995612Swnj continue; 1006509Ssam if (lproto->pr_protocol && 1016509Ssam lproto->pr_protocol != rh->raw_proto.sp_protocol) 1025612Swnj continue; 1035612Swnj /* 1045612Swnj * We assume the lower level routines have 1055612Swnj * placed the address in a canonical format 1066509Ssam * suitable for a structure comparison. 1075612Swnj */ 1086529Ssam #define equal(a1, a2) \ 1096529Ssam (bcmp((caddr_t)&(a1), (caddr_t)&(a2), sizeof (struct sockaddr)) == 0) 1106509Ssam if ((rp->rcb_flags & RAW_LADDR) && 1116529Ssam !equal(rp->rcb_laddr, rh->raw_dst)) 1125612Swnj continue; 1136509Ssam if ((rp->rcb_flags & RAW_FADDR) && 1146529Ssam !equal(rp->rcb_faddr, rh->raw_src)) 1156509Ssam continue; 1165612Swnj if (last) { 1175612Swnj struct mbuf *n; 1186161Ssam if (n = m_copy(m->m_next, 0, (int)M_COPYALL)) 1195612Swnj goto nospace; 1206509Ssam if (sbappendaddr(&last->so_rcv, &rh->raw_src, n)==0) { 1216509Ssam /* should notify about lost packet */ 1225646Ssam m_freem(n); 1235646Ssam goto nospace; 1245646Ssam } 1255612Swnj sorwakeup(last); 1265612Swnj } 1275612Swnj nospace: 1286509Ssam last = rp->rcb_socket; 1295612Swnj } 130*6584Ssam if (last) { 131*6584Ssam m = m_free(m); /* header */ 132*6584Ssam if (sbappendaddr(&last->so_rcv, &rh->raw_src, m) == 0) 133*6584Ssam goto drop; 134*6584Ssam sorwakeup(last); 135*6584Ssam goto next; 136*6584Ssam } 1375121Swnj drop: 1385121Swnj m_freem(m); 1395121Swnj goto next; 1405121Swnj } 1415121Swnj 142*6584Ssam raw_ctlinput(cmd, arg) 143*6584Ssam int cmd; 144*6584Ssam caddr_t arg; 145*6584Ssam { 146*6584Ssam COUNT(RAW_CTLINPUT); 147*6584Ssam } 148*6584Ssam 1495121Swnj /*ARGSUSED*/ 1505121Swnj raw_usrreq(so, req, m, addr) 1515121Swnj struct socket *so; 1525121Swnj int req; 1535121Swnj struct mbuf *m; 1545121Swnj caddr_t addr; 1555121Swnj { 1565612Swnj register struct rawcb *rp = sotorawcb(so); 1575612Swnj int error = 0; 1585121Swnj 1595121Swnj COUNT(RAW_USRREQ); 1605612Swnj if (rp == 0 && req != PRU_ATTACH) 1615612Swnj return (EINVAL); 1625121Swnj 1635612Swnj switch (req) { 1645612Swnj 1655612Swnj /* 1665612Swnj * Allocate a raw control block and fill in the 1675612Swnj * necessary info to allow packets to be routed to 1685612Swnj * the appropriate raw interface routine. 1695612Swnj */ 1705612Swnj case PRU_ATTACH: 1716211Swnj if ((so->so_state & SS_PRIV) == 0) 172*6584Ssam return (EACCES); 1735612Swnj if (rp) 1746211Swnj return (EINVAL); 1755612Swnj error = raw_attach(so, (struct sockaddr *)addr); 1765612Swnj break; 1775612Swnj 1785612Swnj /* 1795612Swnj * Destroy state just before socket deallocation. 1805612Swnj * Flush data or not depending on the options. 1815612Swnj */ 1825612Swnj case PRU_DETACH: 1835612Swnj if (rp == 0) 1845612Swnj return (ENOTCONN); 1855612Swnj raw_detach(rp); 1865612Swnj break; 1875612Swnj 1885612Swnj /* 1895612Swnj * If a socket isn't bound to a single address, 1905612Swnj * the raw input routine will hand it anything 1915612Swnj * within that protocol family (assuming there's 1925612Swnj * nothing else around it should go to). 1935612Swnj */ 1945612Swnj case PRU_CONNECT: 1956509Ssam if (rp->rcb_flags & RAW_FADDR) 1965612Swnj return (EISCONN); 1975612Swnj raw_connaddr(rp, (struct sockaddr *)addr); 1985612Swnj soisconnected(so); 1995612Swnj break; 2005612Swnj 2015612Swnj case PRU_DISCONNECT: 2026509Ssam if ((rp->rcb_flags & RAW_FADDR) == 0) 2035612Swnj return (ENOTCONN); 2045612Swnj raw_disconnect(rp); 2055612Swnj soisdisconnected(so); 2065612Swnj break; 2075612Swnj 2085612Swnj /* 2095612Swnj * Mark the connection as being incapable of further input. 2105612Swnj */ 2115612Swnj case PRU_SHUTDOWN: 2125612Swnj socantsendmore(so); 2135612Swnj break; 2145612Swnj 2155612Swnj /* 2165612Swnj * Ship a packet out. The appropriate raw output 2175612Swnj * routine handles any massaging necessary. 2185612Swnj */ 2195612Swnj case PRU_SEND: 2205612Swnj if (addr) { 2216509Ssam if (rp->rcb_flags & RAW_FADDR) 2225612Swnj return (EISCONN); 2235612Swnj raw_connaddr(rp, (struct sockaddr *)addr); 2246509Ssam } else if ((rp->rcb_flags & RAW_FADDR) == 0) 2255612Swnj return (ENOTCONN); 2266505Ssam error = (*so->so_proto->pr_output)(m, so); 2275612Swnj if (addr) 2286509Ssam rp->rcb_flags &= ~RAW_FADDR; 2295612Swnj break; 2305612Swnj 2315612Swnj case PRU_ABORT: 2325612Swnj raw_disconnect(rp); 2335612Swnj sofree(so); 2345612Swnj soisdisconnected(so); 2355612Swnj break; 2365612Swnj 2375612Swnj /* 2385612Swnj * Not supported. 2395612Swnj */ 2405612Swnj case PRU_ACCEPT: 2415612Swnj case PRU_RCVD: 2425612Swnj case PRU_CONTROL: 2435612Swnj case PRU_SENSE: 2445612Swnj case PRU_RCVOOB: 2455612Swnj case PRU_SENDOOB: 2465612Swnj error = EOPNOTSUPP; 2475612Swnj break; 2485612Swnj 2496509Ssam case PRU_SOCKADDR: 2506509Ssam bcopy(addr, (caddr_t)&rp->rcb_laddr, sizeof (struct sockaddr)); 2516509Ssam break; 2526509Ssam 2535612Swnj default: 2545612Swnj panic("raw_usrreq"); 2555612Swnj } 2565612Swnj return (error); 2575121Swnj } 258