Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/sys/netinet/raw_ip.c,v rcsdiff: /ftp/cvs/cvsroot/src/sys/netinet/raw_ip.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.1.1.3 retrieving revision 1.40 diff -u -p -r1.1.1.3 -r1.40 --- src/sys/netinet/raw_ip.c 1998/01/05 09:56:02 1.1.1.3 +++ src/sys/netinet/raw_ip.c 1998/01/12 03:02:53 1.40 @@ -1,3 +1,5 @@ +/* $NetBSD: raw_ip.c,v 1.40 1998/01/12 03:02:53 scottr Exp $ */ + /* * Copyright (c) 1982, 1986, 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -33,6 +35,8 @@ * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95 */ +#include "opt_mrouting.h" + #include #include #include @@ -41,6 +45,7 @@ #include #include #include +#include #include #include @@ -51,8 +56,15 @@ #include #include #include +#include + +#include + +struct inpcbtable rawcbtable; -struct inpcb rawinpcb; +int rip_bind __P((struct inpcb *, struct mbuf *)); +int rip_connect __P((struct inpcb *, struct mbuf *)); +void rip_disconnect __P((struct inpcb *)); /* * Nominal space allocated to a raw ip socket. @@ -71,53 +83,75 @@ void rip_init() { - rawinpcb.inp_next = rawinpcb.inp_prev = &rawinpcb; + in_pcbinit(&rawcbtable, 1, 1); } -struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; /* * Setup generic address and protocol structures * for raw_input routine, then pass them along with * mbuf chain. */ void -rip_input(m) +#if __STDC__ +rip_input(struct mbuf *m, ...) +#else +rip_input(m, va_alist) struct mbuf *m; + va_dcl +#endif { register struct ip *ip = mtod(m, struct ip *); register struct inpcb *inp; - struct socket *last = 0; + struct inpcb *last = 0; + struct mbuf *opts = 0; + struct sockaddr_in ripsrc; + ripsrc.sin_family = AF_INET; + ripsrc.sin_len = sizeof(struct sockaddr_in); ripsrc.sin_addr = ip->ip_src; - for (inp = rawinpcb.inp_next; inp != &rawinpcb; inp = inp->inp_next) { + ripsrc.sin_port = 0; + bzero((caddr_t)ripsrc.sin_zero, sizeof(ripsrc.sin_zero)); + + for (inp = rawcbtable.inpt_queue.cqh_first; + inp != (struct inpcb *)&rawcbtable.inpt_queue; + inp = inp->inp_queue.cqe_next) { if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != ip->ip_p) continue; - if (inp->inp_laddr.s_addr && - inp->inp_laddr.s_addr != ip->ip_dst.s_addr) + if (!in_nullhost(inp->inp_laddr) && + !in_hosteq(inp->inp_laddr, ip->ip_dst)) continue; - if (inp->inp_faddr.s_addr && - inp->inp_faddr.s_addr != ip->ip_src.s_addr) + if (!in_nullhost(inp->inp_faddr) && + !in_hosteq(inp->inp_faddr, ip->ip_src)) continue; if (last) { struct mbuf *n; - if (n = m_copy(m, 0, (int)M_COPYALL)) { - if (sbappendaddr(&last->so_rcv, - (struct sockaddr *)&ripsrc, n, - (struct mbuf *)0) == 0) + if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { + if (last->inp_flags & INP_CONTROLOPTS || + last->inp_socket->so_options & SO_TIMESTAMP) + ip_savecontrol(last, &opts, ip, n); + if (sbappendaddr(&last->inp_socket->so_rcv, + sintosa(&ripsrc), n, opts) == 0) { /* should notify about lost packet */ m_freem(n); - else - sorwakeup(last); + if (opts) + m_freem(opts); + } else + sorwakeup(last->inp_socket); } } - last = inp->inp_socket; + last = inp; } if (last) { - if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&ripsrc, - m, (struct mbuf *)0) == 0) + if (last->inp_flags & INP_CONTROLOPTS || + last->inp_socket->so_options & SO_TIMESTAMP) + ip_savecontrol(last, &opts, ip, m); + if (sbappendaddr(&last->inp_socket->so_rcv, + sintosa(&ripsrc), m, opts) == 0) { m_freem(m); - else - sorwakeup(last); + if (opts) + m_freem(opts); + } else + sorwakeup(last->inp_socket); } else { m_freem(m); ipstat.ips_noproto++; @@ -130,21 +164,37 @@ rip_input(m) * Tack on options user may have setup with control call. */ int -rip_output(m, so, dst) - register struct mbuf *m; - struct socket *so; - u_long dst; +#if __STDC__ +rip_output(struct mbuf *m, ...) +#else +rip_output(m, va_alist) + struct mbuf *m; + va_dcl +#endif { + register struct inpcb *inp; register struct ip *ip; - register struct inpcb *inp = sotoinpcb(so); struct mbuf *opts; - int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST; + int flags; + va_list ap; + + va_start(ap, m); + inp = va_arg(ap, struct inpcb *); + va_end(ap); + + flags = + (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST + | IP_RETURNMTU; /* * If the user handed us a complete IP packet, use it. * Otherwise, allocate an mbuf for a header and fill it in. */ if ((inp->inp_flags & INP_HDRINCL) == 0) { + if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) { + m_freem(m); + return (EMSGSIZE); + } M_PREPEND(m, sizeof(struct ip), M_WAIT); ip = mtod(m, struct ip *); ip->ip_tos = 0; @@ -152,11 +202,19 @@ rip_output(m, so, dst) ip->ip_p = inp->inp_ip.ip_p; ip->ip_len = m->m_pkthdr.len; ip->ip_src = inp->inp_laddr; - ip->ip_dst.s_addr = dst; + ip->ip_dst = inp->inp_faddr; ip->ip_ttl = MAXTTL; opts = inp->inp_options; } else { + if (m->m_pkthdr.len > IP_MAXPACKET) { + m_freem(m); + return (EMSGSIZE); + } ip = mtod(m, struct ip *); + if (m->m_pkthdr.len != ip->ip_len) { + m_freem(m); + return (EINVAL); + } if (ip->ip_id == 0) ip->ip_id = htons(ip_id++); opts = NULL; @@ -164,7 +222,7 @@ rip_output(m, so, dst) flags |= IP_RAWOUTPUT; ipstat.ips_rawout++; } - return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions)); + return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions, &inp->inp_errormtu)); } /* @@ -178,75 +236,116 @@ rip_ctloutput(op, so, level, optname, m) struct mbuf **m; { register struct inpcb *inp = sotoinpcb(so); - register int error; + int error = 0; if (level != IPPROTO_IP) { - if (op == PRCO_SETOPT && *m) + error = ENOPROTOOPT; + if (op == PRCO_SETOPT && *m != 0) (void) m_free(*m); - return (EINVAL); - } + } else switch (op) { - switch (optname) { - - case IP_HDRINCL: - error = 0; - if (op == PRCO_SETOPT) { + case PRCO_SETOPT: + switch (optname) { + case IP_HDRINCL: if (*m == 0 || (*m)->m_len < sizeof (int)) error = EINVAL; - else if (*mtod(*m, int *)) - inp->inp_flags |= INP_HDRINCL; - else - inp->inp_flags &= ~INP_HDRINCL; - if (*m) - (void)m_free(*m); - } else { - *m = m_get(M_WAIT, MT_SOOPTS); - (*m)->m_len = sizeof (int); - *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL; - } - return (error); + else { + if (*mtod(*m, int *)) + inp->inp_flags |= INP_HDRINCL; + else + inp->inp_flags &= ~INP_HDRINCL; + } + if (*m != 0) + (void) m_free(*m); + break; - case DVMRP_INIT: - case DVMRP_DONE: - case DVMRP_ADD_VIF: - case DVMRP_DEL_VIF: - case DVMRP_ADD_LGRP: - case DVMRP_DEL_LGRP: - case DVMRP_ADD_MRT: - case DVMRP_DEL_MRT: #ifdef MROUTING - if (op == PRCO_SETOPT) { - error = ip_mrouter_cmd(optname, so, *m); - if (*m) - (void)m_free(*m); - } else - error = EINVAL; - return (error); -#else - if (op == PRCO_SETOPT && *m) - (void)m_free(*m); - return (EOPNOTSUPP); + case MRT_INIT: + case MRT_DONE: + case MRT_ADD_VIF: + case MRT_DEL_VIF: + case MRT_ADD_MFC: + case MRT_DEL_MFC: + case MRT_ASSERT: + error = ip_mrouter_set(so, optname, m); + break; #endif - default: - if (optname >= DVMRP_INIT) { + default: + error = ip_ctloutput(op, so, level, optname, m); + break; + } + break; + + case PRCO_GETOPT: + switch (optname) { + case IP_HDRINCL: + *m = m_get(M_WAIT, M_SOOPTS); + (*m)->m_len = sizeof (int); + *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL ? 1 : 0; + break; + #ifdef MROUTING - if (op == PRCO_SETOPT) { - error = ip_mrouter_cmd(optname, so, *m); - if (*m) - (void)m_free(*m); - } else - error = EINVAL; - return (error); -#else - if (op == PRCO_SETOPT && *m) - (void)m_free(*m); - return (EOPNOTSUPP); + case MRT_VERSION: + case MRT_ASSERT: + error = ip_mrouter_get(so, optname, m); + break; #endif - } + default: + error = ip_ctloutput(op, so, level, optname, m); + break; + } + break; } - return (ip_ctloutput(op, so, level, optname, m)); + return (error); +} + +int +rip_bind(inp, nam) + struct inpcb *inp; + struct mbuf *nam; +{ + struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *); + + if (nam->m_len != sizeof(*addr)) + return (EINVAL); + if (ifnet.tqh_first == 0) + return (EADDRNOTAVAIL); + if (addr->sin_family != AF_INET && + addr->sin_family != AF_IMPLINK) + return (EAFNOSUPPORT); + if (!in_nullhost(addr->sin_addr) && + ifa_ifwithaddr(sintosa(addr)) == 0) + return (EADDRNOTAVAIL); + inp->inp_laddr = addr->sin_addr; + return (0); +} + +int +rip_connect(inp, nam) + struct inpcb *inp; + struct mbuf *nam; +{ + struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *); + + if (nam->m_len != sizeof(*addr)) + return (EINVAL); + if (ifnet.tqh_first == 0) + return (EADDRNOTAVAIL); + if (addr->sin_family != AF_INET && + addr->sin_family != AF_IMPLINK) + return (EAFNOSUPPORT); + inp->inp_faddr = addr->sin_addr; + return (0); +} + +void +rip_disconnect(inp) + struct inpcb *inp; +{ + + inp->inp_faddr = zeroin_addr; } u_long rip_sendspace = RIPSNDQ; @@ -254,44 +353,58 @@ u_long rip_recvspace = RIPRCVQ; /*ARGSUSED*/ int -rip_usrreq(so, req, m, nam, control) +rip_usrreq(so, req, m, nam, control, p) register struct socket *so; int req; struct mbuf *m, *nam, *control; + struct proc *p; { + register struct inpcb *inp; + int s; register int error = 0; - register struct inpcb *inp = sotoinpcb(so); #ifdef MROUTING extern struct socket *ip_mrouter; #endif + + if (req == PRU_CONTROL) + return (in_control(so, (long)m, (caddr_t)nam, + (struct ifnet *)control, p)); + + s = splsoftnet(); + inp = sotoinpcb(so); +#ifdef DIAGNOSTIC + if (req != PRU_SEND && req != PRU_SENDOOB && control) + panic("rip_usrreq: unexpected control mbuf"); +#endif + if (inp == 0 && req != PRU_ATTACH) { + error = EINVAL; + goto release; + } + switch (req) { case PRU_ATTACH: - if (inp) - panic("rip_attach"); - if ((so->so_state & SS_PRIV) == 0) { + if (inp != 0) { + error = EISCONN; + break; + } + if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) { error = EACCES; break; } - if ((error = soreserve(so, rip_sendspace, rip_recvspace)) || - (error = in_pcballoc(so, &rawinpcb))) + if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { + error = soreserve(so, rip_sendspace, rip_recvspace); + if (error) + break; + } + error = in_pcballoc(so, &rawcbtable); + if (error) break; - inp = (struct inpcb *)so->so_pcb; - inp->inp_ip.ip_p = (int)nam; + inp = sotoinpcb(so); + inp->inp_ip.ip_p = (long)nam; break; - case PRU_DISCONNECT: - if ((so->so_state & SS_ISCONNECTED) == 0) { - error = ENOTCONN; - break; - } - /* FALLTHROUGH */ - case PRU_ABORT: - soisdisconnected(so); - /* FALLTHROUGH */ case PRU_DETACH: - if (inp == 0) - panic("rip_detach"); #ifdef MROUTING if (so == ip_mrouter) ip_mrouter_done(); @@ -300,50 +413,29 @@ rip_usrreq(so, req, m, nam, control) break; case PRU_BIND: - { - struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *); + error = rip_bind(inp, nam); + break; - if (nam->m_len != sizeof(*addr)) { - error = EINVAL; - break; - } - if ((ifnet == 0) || - ((addr->sin_family != AF_INET) && - (addr->sin_family != AF_IMPLINK)) || - (addr->sin_addr.s_addr && - ifa_ifwithaddr((struct sockaddr *)addr) == 0)) { - error = EADDRNOTAVAIL; - break; - } - inp->inp_laddr = addr->sin_addr; + case PRU_LISTEN: + error = EOPNOTSUPP; break; - } - case PRU_CONNECT: - { - struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *); - if (nam->m_len != sizeof(*addr)) { - error = EINVAL; - break; - } - if (ifnet == 0) { - error = EADDRNOTAVAIL; - break; - } - if ((addr->sin_family != AF_INET) && - (addr->sin_family != AF_IMPLINK)) { - error = EAFNOSUPPORT; + case PRU_CONNECT: + error = rip_connect(inp, nam); + if (error) break; - } - inp->inp_faddr = addr->sin_addr; soisconnected(so); break; - } case PRU_CONNECT2: error = EOPNOTSUPP; break; + case PRU_DISCONNECT: + soisdisconnected(so); + rip_disconnect(inp); + break; + /* * Mark the connection as being incapable of further input. */ @@ -351,46 +443,59 @@ rip_usrreq(so, req, m, nam, control) socantsendmore(so); break; + case PRU_RCVD: + error = EOPNOTSUPP; + break; + /* * Ship a packet out. The appropriate raw output * routine handles any massaging necessary. */ case PRU_SEND: - { - register u_long dst; - - if (so->so_state & SS_ISCONNECTED) { - if (nam) { + if (control && control->m_len) { + m_freem(control); + m_freem(m); + error = EINVAL; + break; + } + { + if (nam) { + if ((so->so_state & SS_ISCONNECTED) != 0) { error = EISCONN; + goto die; + } + error = rip_connect(inp, nam); + if (error) { + die: + m_freem(m); break; } - dst = inp->inp_faddr.s_addr; } else { - if (nam == NULL) { + if ((so->so_state & SS_ISCONNECTED) == 0) { error = ENOTCONN; - break; + goto die; } - dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr; } - error = rip_output(m, so, dst); - m = NULL; + error = rip_output(m, inp); + if (nam) + rip_disconnect(inp); + } break; - } case PRU_SENSE: /* * stat: don't bother with a blocksize. */ + splx(s); return (0); - /* - * Not supported. - */ case PRU_RCVOOB: - case PRU_RCVD: - case PRU_LISTEN: - case PRU_ACCEPT: + error = EOPNOTSUPP; + break; + case PRU_SENDOOB: + m_freem(control); + m_freem(m); error = EOPNOTSUPP; break; @@ -405,7 +510,8 @@ rip_usrreq(so, req, m, nam, control) default: panic("rip_usrreq"); } - if (m != NULL) - m_freem(m); + +release: + splx(s); return (error); }