1*6529Ssam /* raw_usrreq.c 4.14 82/04/11 */ 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 */ 31*6529Ssam raw_input(m0, proto, src, dst) 325612Swnj struct mbuf *m0; 336509Ssam struct sockproto *proto; 34*6529Ssam 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; 84*6529Ssam 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 */ 108*6529Ssam #define equal(a1, a2) \ 109*6529Ssam (bcmp((caddr_t)&(a1), (caddr_t)&(a2), sizeof (struct sockaddr)) == 0) 1106509Ssam if ((rp->rcb_flags & RAW_LADDR) && 111*6529Ssam !equal(rp->rcb_laddr, rh->raw_dst)) 1125612Swnj continue; 1136509Ssam if ((rp->rcb_flags & RAW_FADDR) && 114*6529Ssam !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 } 1305612Swnj if (last == 0) 1315612Swnj goto drop; 1326509Ssam m = m_free(m); /* header */ 1336509Ssam if (sbappendaddr(&last->so_rcv, &rh->raw_src, m) == 0) 1345646Ssam goto drop; 1355612Swnj sorwakeup(last); 1365612Swnj goto next; 1375121Swnj drop: 1385121Swnj m_freem(m); 1395121Swnj goto next; 1405121Swnj } 1415121Swnj 1425121Swnj /*ARGSUSED*/ 1435121Swnj raw_usrreq(so, req, m, addr) 1445121Swnj struct socket *so; 1455121Swnj int req; 1465121Swnj struct mbuf *m; 1475121Swnj caddr_t addr; 1485121Swnj { 1495612Swnj register struct rawcb *rp = sotorawcb(so); 1505612Swnj int error = 0; 1515121Swnj 1525121Swnj COUNT(RAW_USRREQ); 1535612Swnj if (rp == 0 && req != PRU_ATTACH) 1545612Swnj return (EINVAL); 1555121Swnj 1565612Swnj switch (req) { 1575612Swnj 1585612Swnj /* 1595612Swnj * Allocate a raw control block and fill in the 1605612Swnj * necessary info to allow packets to be routed to 1615612Swnj * the appropriate raw interface routine. 1625612Swnj */ 1635612Swnj case PRU_ATTACH: 1646211Swnj if ((so->so_state & SS_PRIV) == 0) 1656211Swnj return (EPERM); 1665612Swnj if (rp) 1676211Swnj return (EINVAL); 1685612Swnj error = raw_attach(so, (struct sockaddr *)addr); 1695612Swnj break; 1705612Swnj 1715612Swnj /* 1725612Swnj * Destroy state just before socket deallocation. 1735612Swnj * Flush data or not depending on the options. 1745612Swnj */ 1755612Swnj case PRU_DETACH: 1765612Swnj if (rp == 0) 1775612Swnj return (ENOTCONN); 1785612Swnj raw_detach(rp); 1795612Swnj break; 1805612Swnj 1815612Swnj /* 1825612Swnj * If a socket isn't bound to a single address, 1835612Swnj * the raw input routine will hand it anything 1845612Swnj * within that protocol family (assuming there's 1855612Swnj * nothing else around it should go to). 1865612Swnj */ 1875612Swnj case PRU_CONNECT: 1886509Ssam if (rp->rcb_flags & RAW_FADDR) 1895612Swnj return (EISCONN); 1905612Swnj raw_connaddr(rp, (struct sockaddr *)addr); 1915612Swnj soisconnected(so); 1925612Swnj break; 1935612Swnj 1945612Swnj case PRU_DISCONNECT: 1956509Ssam if ((rp->rcb_flags & RAW_FADDR) == 0) 1965612Swnj return (ENOTCONN); 1975612Swnj raw_disconnect(rp); 1985612Swnj soisdisconnected(so); 1995612Swnj break; 2005612Swnj 2015612Swnj /* 2025612Swnj * Mark the connection as being incapable of further input. 2035612Swnj */ 2045612Swnj case PRU_SHUTDOWN: 2055612Swnj socantsendmore(so); 2065612Swnj break; 2075612Swnj 2085612Swnj /* 2095612Swnj * Ship a packet out. The appropriate raw output 2105612Swnj * routine handles any massaging necessary. 2115612Swnj */ 2125612Swnj case PRU_SEND: 2135612Swnj if (addr) { 2146509Ssam if (rp->rcb_flags & RAW_FADDR) 2155612Swnj return (EISCONN); 2165612Swnj raw_connaddr(rp, (struct sockaddr *)addr); 2176509Ssam } else if ((rp->rcb_flags & RAW_FADDR) == 0) 2185612Swnj return (ENOTCONN); 2196505Ssam error = (*so->so_proto->pr_output)(m, so); 2205612Swnj if (addr) 2216509Ssam rp->rcb_flags &= ~RAW_FADDR; 2225612Swnj break; 2235612Swnj 2245612Swnj case PRU_ABORT: 2255612Swnj raw_disconnect(rp); 2265612Swnj sofree(so); 2275612Swnj soisdisconnected(so); 2285612Swnj break; 2295612Swnj 2305612Swnj /* 2315612Swnj * Not supported. 2325612Swnj */ 2335612Swnj case PRU_ACCEPT: 2345612Swnj case PRU_RCVD: 2355612Swnj case PRU_CONTROL: 2365612Swnj case PRU_SENSE: 2375612Swnj case PRU_RCVOOB: 2385612Swnj case PRU_SENDOOB: 2395612Swnj error = EOPNOTSUPP; 2405612Swnj break; 2415612Swnj 2426509Ssam case PRU_SOCKADDR: 2436509Ssam bcopy(addr, (caddr_t)&rp->rcb_laddr, sizeof (struct sockaddr)); 2446509Ssam break; 2456509Ssam 2465612Swnj default: 2475612Swnj panic("raw_usrreq"); 2485612Swnj } 2495612Swnj return (error); 2505121Swnj } 251