2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sysctl.h>
48 #include <vm/vm_zone.h>
51 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/ip_mroute.h>
62 #include <netinet/ip_fw.h>
66 #include <netinet/ip_dummynet.h>
69 struct inpcbhead ripcb;
70 struct inpcbinfo ripcbinfo;
73 * Nominal space allocated to a raw ip socket.
79 * Raw interface to IP protocol.
83 * Initialize raw connection block q.
89 ripcbinfo.listhead = &ripcb;
91 * XXX We don't use the hash list for raw IP, but it's easier
92 * to allocate a one entry hash list than it is to check all
93 * over the place for hashbase == NULL.
95 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
96 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
97 ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
98 maxsockets, ZONE_INTERRUPT, 0);
101 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
103 * Setup generic address and protocol structures
104 * for raw_input routine, then pass them along with
112 register struct ip *ip = mtod(m, struct ip *);
113 register struct inpcb *inp;
114 struct inpcb *last = 0;
115 struct mbuf *opts = 0;
117 ripsrc.sin_addr = ip->ip_src;
118 for (inp = ripcb.lh_first; inp != NULL; inp = inp->inp_list.le_next) {
119 if (inp->inp_ip_p && inp->inp_ip_p != ip->ip_p)
121 if (inp->inp_laddr.s_addr &&
122 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
124 if (inp->inp_faddr.s_addr &&
125 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
128 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
130 if (last->inp_flags & INP_CONTROLOPTS ||
131 last->inp_socket->so_options & SO_TIMESTAMP)
132 ip_savecontrol(last, &opts, ip, n);
133 if (sbappendaddr(&last->inp_socket->so_rcv,
134 (struct sockaddr *)&ripsrc, n,
136 /* should notify about lost packet */
141 sorwakeup(last->inp_socket);
148 if (last->inp_flags & INP_CONTROLOPTS ||
149 last->inp_socket->so_options & SO_TIMESTAMP)
150 ip_savecontrol(last, &opts, ip, m);
151 if (sbappendaddr(&last->inp_socket->so_rcv,
152 (struct sockaddr *)&ripsrc, m, opts) == 0) {
157 sorwakeup(last->inp_socket);
160 ipstat.ips_noproto++;
161 ipstat.ips_delivered--;
166 * Generate IP header and pass packet to ip_output.
167 * Tack on options user may have setup with control call.
170 rip_output(m, so, dst)
171 register struct mbuf *m;
175 register struct ip *ip;
176 register struct inpcb *inp = sotoinpcb(so);
177 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
180 * If the user handed us a complete IP packet, use it.
181 * Otherwise, allocate an mbuf for a header and fill it in.
183 if ((inp->inp_flags & INP_HDRINCL) == 0) {
184 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
188 M_PREPEND(m, sizeof(struct ip), M_WAIT);
189 ip = mtod(m, struct ip *);
192 ip->ip_p = inp->inp_ip_p;
193 ip->ip_len = m->m_pkthdr.len;
194 ip->ip_src = inp->inp_laddr;
195 ip->ip_dst.s_addr = dst;
198 if (m->m_pkthdr.len > IP_MAXPACKET) {
202 ip = mtod(m, struct ip *);
203 /* don't allow both user specified and setsockopt options,
204 and don't allow packet length sizes that will crash */
205 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
207 || (ip->ip_len > m->m_pkthdr.len)
208 || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
213 ip->ip_id = htons(ip_id++);
214 /* XXX prevent ip_output from overwriting header fields */
215 flags |= IP_RAWOUTPUT;
218 return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
223 * Raw IP socket option processing.
226 rip_ctloutput(so, sopt)
228 struct sockopt *sopt;
230 struct inpcb *inp = sotoinpcb(so);
233 if (sopt->sopt_level != IPPROTO_IP)
238 switch (sopt->sopt_dir) {
240 switch (sopt->sopt_name) {
242 optval = inp->inp_flags & INP_HDRINCL;
243 error = sooptcopyout(sopt, &optval, sizeof optval);
247 if (ip_fw_ctl_ptr == 0)
250 error = ip_fw_ctl_ptr(sopt);
254 case IP_DUMMYNET_GET:
255 if (ip_dn_ctl_ptr == NULL)
256 error = ENOPROTOOPT ;
258 error = ip_dn_ctl_ptr(sopt);
260 #endif /* DUMMYNET */
270 error = ip_mrouter_get(so, sopt);
274 error = ip_ctloutput(so, sopt);
280 switch (sopt->sopt_name) {
282 error = sooptcopyin(sopt, &optval, sizeof optval,
287 inp->inp_flags |= INP_HDRINCL;
289 inp->inp_flags &= ~INP_HDRINCL;
297 if (ip_fw_ctl_ptr == 0)
300 error = ip_fw_ctl_ptr(sopt);
304 case IP_DUMMYNET_CONFIGURE:
305 case IP_DUMMYNET_DEL:
306 case IP_DUMMYNET_FLUSH:
307 if (ip_dn_ctl_ptr == NULL)
308 error = ENOPROTOOPT ;
310 error = ip_dn_ctl_ptr(sopt);
315 error = ip_rsvp_init(so);
319 error = ip_rsvp_done();
322 /* XXX - should be combined */
324 error = ip_rsvp_vif_init(so, sopt);
327 case IP_RSVP_VIF_OFF:
328 error = ip_rsvp_vif_done(so, sopt);
339 error = ip_mrouter_set(so, sopt);
343 error = ip_ctloutput(so, sopt);
353 * This function exists solely to receive the PRC_IFDOWN messages which
354 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
355 * and calls in_ifadown() to remove all routes corresponding to that address.
356 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
360 rip_ctlinput(cmd, sa, vip)
365 struct in_ifaddr *ia;
372 for (ia = in_ifaddrhead.tqh_first; ia;
373 ia = ia->ia_link.tqe_next) {
374 if (ia->ia_ifa.ifa_addr == sa
375 && (ia->ia_flags & IFA_ROUTE)) {
377 * in_ifscrub kills the interface route.
379 in_ifscrub(ia->ia_ifp, ia);
381 * in_ifadown gets rid of all the rest of
382 * the routes. This is not quite the right
383 * thing to do, but at least if we are running
384 * a routing process they will come back.
386 in_ifadown(&ia->ia_ifa);
393 for (ia = in_ifaddrhead.tqh_first; ia;
394 ia = ia->ia_link.tqe_next) {
395 if (ia->ia_ifa.ifa_addr == sa)
398 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
401 ifp = ia->ia_ifa.ifa_ifp;
403 if ((ifp->if_flags & IFF_LOOPBACK)
404 || (ifp->if_flags & IFF_POINTOPOINT))
407 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
409 ia->ia_flags |= IFA_ROUTE;
414 u_long rip_sendspace = RIPSNDQ;
415 u_long rip_recvspace = RIPRCVQ;
417 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
418 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
419 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
420 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
423 rip_attach(struct socket *so, int proto, struct proc *p)
431 if (p && (error = suser(p)) != 0)
435 error = in_pcballoc(so, &ripcbinfo, p);
439 error = soreserve(so, rip_sendspace, rip_recvspace);
442 inp = (struct inpcb *)so->so_pcb;
443 inp->inp_ip_p = proto;
448 rip_detach(struct socket *so)
455 if (so == ip_mrouter)
457 ip_rsvp_force_done(so);
465 rip_abort(struct socket *so)
467 soisdisconnected(so);
468 return rip_detach(so);
472 rip_disconnect(struct socket *so)
474 if ((so->so_state & SS_ISCONNECTED) == 0)
476 return rip_abort(so);
480 rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
482 struct inpcb *inp = sotoinpcb(so);
483 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
485 if (nam->sa_len != sizeof(*addr))
488 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
489 (addr->sin_family != AF_IMPLINK)) ||
490 (addr->sin_addr.s_addr &&
491 ifa_ifwithaddr((struct sockaddr *)addr) == 0))
492 return EADDRNOTAVAIL;
493 inp->inp_laddr = addr->sin_addr;
498 rip_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
500 struct inpcb *inp = sotoinpcb(so);
501 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
503 if (nam->sa_len != sizeof(*addr))
505 if (TAILQ_EMPTY(&ifnet))
506 return EADDRNOTAVAIL;
507 if ((addr->sin_family != AF_INET) &&
508 (addr->sin_family != AF_IMPLINK))
510 inp->inp_faddr = addr->sin_addr;
516 rip_shutdown(struct socket *so)
523 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
524 struct mbuf *control, struct proc *p)
526 struct inpcb *inp = sotoinpcb(so);
529 if (so->so_state & SS_ISCONNECTED) {
534 dst = inp->inp_faddr.s_addr;
540 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
542 return rip_output(m, so, dst);
546 rip_pcblist SYSCTL_HANDLER_ARGS
549 struct inpcb *inp, **inp_list;
554 * The process of preparing the TCB list is too time-consuming and
555 * resource-intensive to repeat twice on every request.
557 if (req->oldptr == 0) {
558 n = ripcbinfo.ipi_count;
559 req->oldidx = 2 * (sizeof xig)
560 + (n + n/8) * sizeof(struct xinpcb);
564 if (req->newptr != 0)
568 * OK, now we're committed to doing something.
571 gencnt = ripcbinfo.ipi_gencnt;
572 n = ripcbinfo.ipi_count;
575 xig.xig_len = sizeof xig;
577 xig.xig_gen = gencnt;
578 xig.xig_sogen = so_gencnt;
579 error = SYSCTL_OUT(req, &xig, sizeof xig);
583 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
588 for (inp = ripcbinfo.listhead->lh_first, i = 0; inp && i < n;
589 inp = inp->inp_list.le_next) {
590 if (inp->inp_gencnt <= gencnt)
597 for (i = 0; i < n; i++) {
599 if (inp->inp_gencnt <= gencnt) {
601 xi.xi_len = sizeof xi;
602 /* XXX should avoid extra copy */
603 bcopy(inp, &xi.xi_inp, sizeof *inp);
605 sotoxsocket(inp->inp_socket, &xi.xi_socket);
606 error = SYSCTL_OUT(req, &xi, sizeof xi);
611 * Give the user an updated idea of our state.
612 * If the generation differs from what we told
613 * her before, she knows that something happened
614 * while we were processing this request, and it
615 * might be necessary to retry.
618 xig.xig_gen = ripcbinfo.ipi_gencnt;
619 xig.xig_sogen = so_gencnt;
620 xig.xig_count = ripcbinfo.ipi_count;
622 error = SYSCTL_OUT(req, &xig, sizeof xig);
624 free(inp_list, M_TEMP);
628 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
629 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
631 struct pr_usrreqs rip_usrreqs = {
632 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
633 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
634 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
635 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
636 in_setsockaddr, sosend, soreceive, sopoll