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 "opt_inet6.h"
38 #include "opt_ipsec.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sysctl.h>
51 #include <vm/vm_zone.h>
54 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/ip_mroute.h>
65 #include <netinet/ip_fw.h>
68 #include <netinet6/ipsec.h>
73 #include <netinet/ip_dummynet.h>
76 struct inpcbhead ripcb;
77 struct inpcbinfo ripcbinfo;
80 * Nominal space allocated to a raw ip socket.
86 * Raw interface to IP protocol.
90 * Initialize raw connection block q.
96 ripcbinfo.listhead = &ripcb;
98 * XXX We don't use the hash list for raw IP, but it's easier
99 * to allocate a one entry hash list than it is to check all
100 * over the place for hashbase == NULL.
102 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
103 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
104 ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
105 maxsockets, ZONE_INTERRUPT, 0);
108 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
110 * Setup generic address and protocol structures
111 * for raw_input routine, then pass them along with
115 rip_input(m, off, proto)
119 register struct ip *ip = mtod(m, struct ip *);
120 register struct inpcb *inp;
121 struct inpcb *last = 0;
122 struct mbuf *opts = 0;
124 ripsrc.sin_addr = ip->ip_src;
125 LIST_FOREACH(inp, &ripcb, inp_list) {
127 if ((inp->inp_vflag & INP_IPV4) == 0)
130 if (inp->inp_ip_p && inp->inp_ip_p != proto)
132 if (inp->inp_laddr.s_addr &&
133 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
135 if (inp->inp_faddr.s_addr &&
136 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
139 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
141 if (last->inp_flags & INP_CONTROLOPTS ||
142 last->inp_socket->so_options & SO_TIMESTAMP)
143 ip_savecontrol(last, &opts, ip, n);
144 if (sbappendaddr(&last->inp_socket->so_rcv,
145 (struct sockaddr *)&ripsrc, n,
147 /* should notify about lost packet */
152 sorwakeup(last->inp_socket);
159 if (last->inp_flags & INP_CONTROLOPTS ||
160 last->inp_socket->so_options & SO_TIMESTAMP)
161 ip_savecontrol(last, &opts, ip, m);
162 if (sbappendaddr(&last->inp_socket->so_rcv,
163 (struct sockaddr *)&ripsrc, m, opts) == 0) {
168 sorwakeup(last->inp_socket);
171 ipstat.ips_noproto++;
172 ipstat.ips_delivered--;
177 * Generate IP header and pass packet to ip_output.
178 * Tack on options user may have setup with control call.
181 rip_output(m, so, dst)
186 register struct ip *ip;
187 register struct inpcb *inp = sotoinpcb(so);
188 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
191 * If the user handed us a complete IP packet, use it.
192 * Otherwise, allocate an mbuf for a header and fill it in.
194 if ((inp->inp_flags & INP_HDRINCL) == 0) {
195 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
199 M_PREPEND(m, sizeof(struct ip), M_WAIT);
200 ip = mtod(m, struct ip *);
203 ip->ip_p = inp->inp_ip_p;
204 ip->ip_len = m->m_pkthdr.len;
205 ip->ip_src = inp->inp_laddr;
206 ip->ip_dst.s_addr = dst;
209 if (m->m_pkthdr.len > IP_MAXPACKET) {
213 ip = mtod(m, struct ip *);
214 /* don't allow both user specified and setsockopt options,
215 and don't allow packet length sizes that will crash */
216 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
218 || (ip->ip_len > m->m_pkthdr.len)
219 || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
224 ip->ip_id = htons(ip_id++);
225 /* XXX prevent ip_output from overwriting header fields */
226 flags |= IP_RAWOUTPUT;
231 ipsec_setsocket(m, so);
234 return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
239 * Raw IP socket option processing.
242 rip_ctloutput(so, sopt)
244 struct sockopt *sopt;
246 struct inpcb *inp = sotoinpcb(so);
249 if (sopt->sopt_level != IPPROTO_IP)
254 switch (sopt->sopt_dir) {
256 switch (sopt->sopt_name) {
258 optval = inp->inp_flags & INP_HDRINCL;
259 error = sooptcopyout(sopt, &optval, sizeof optval);
264 if (ip_fw_ctl_ptr == 0)
267 error = ip_fw_ctl_ptr(sopt);
271 case IP_DUMMYNET_GET:
272 if (ip_dn_ctl_ptr == NULL)
273 error = ENOPROTOOPT ;
275 error = ip_dn_ctl_ptr(sopt);
277 #endif /* DUMMYNET */
287 error = ip_mrouter_get(so, sopt);
291 error = ip_ctloutput(so, sopt);
297 switch (sopt->sopt_name) {
299 error = sooptcopyin(sopt, &optval, sizeof optval,
304 inp->inp_flags |= INP_HDRINCL;
306 inp->inp_flags &= ~INP_HDRINCL;
314 if (ip_fw_ctl_ptr == 0)
317 error = ip_fw_ctl_ptr(sopt);
321 case IP_DUMMYNET_CONFIGURE:
322 case IP_DUMMYNET_DEL:
323 case IP_DUMMYNET_FLUSH:
324 if (ip_dn_ctl_ptr == NULL)
325 error = ENOPROTOOPT ;
327 error = ip_dn_ctl_ptr(sopt);
332 error = ip_rsvp_init(so);
336 error = ip_rsvp_done();
339 /* XXX - should be combined */
341 error = ip_rsvp_vif_init(so, sopt);
344 case IP_RSVP_VIF_OFF:
345 error = ip_rsvp_vif_done(so, sopt);
356 error = ip_mrouter_set(so, sopt);
360 error = ip_ctloutput(so, sopt);
370 * This function exists solely to receive the PRC_IFDOWN messages which
371 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
372 * and calls in_ifadown() to remove all routes corresponding to that address.
373 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
377 rip_ctlinput(cmd, sa, vip)
382 struct in_ifaddr *ia;
389 for (ia = in_ifaddrhead.tqh_first; ia;
390 ia = ia->ia_link.tqe_next) {
391 if (ia->ia_ifa.ifa_addr == sa
392 && (ia->ia_flags & IFA_ROUTE)) {
394 * in_ifscrub kills the interface route.
396 in_ifscrub(ia->ia_ifp, ia);
398 * in_ifadown gets rid of all the rest of
399 * the routes. This is not quite the right
400 * thing to do, but at least if we are running
401 * a routing process they will come back.
403 in_ifadown(&ia->ia_ifa);
410 for (ia = in_ifaddrhead.tqh_first; ia;
411 ia = ia->ia_link.tqe_next) {
412 if (ia->ia_ifa.ifa_addr == sa)
415 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
418 ifp = ia->ia_ifa.ifa_ifp;
420 if ((ifp->if_flags & IFF_LOOPBACK)
421 || (ifp->if_flags & IFF_POINTOPOINT))
424 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
426 ia->ia_flags |= IFA_ROUTE;
431 u_long rip_sendspace = RIPSNDQ;
432 u_long rip_recvspace = RIPRCVQ;
434 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
435 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
436 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
437 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
440 rip_attach(struct socket *so, int proto, struct proc *p)
448 if (p && (error = suser(p)) != 0)
451 error = soreserve(so, rip_sendspace, rip_recvspace);
455 error = in_pcballoc(so, &ripcbinfo, p);
459 inp = (struct inpcb *)so->so_pcb;
460 inp->inp_vflag |= INP_IPV4;
461 inp->inp_ip_p = proto;
463 error = ipsec_init_policy(so, &inp->inp_sp);
473 rip_detach(struct socket *so)
480 if (so == ip_mrouter)
482 ip_rsvp_force_done(so);
490 rip_abort(struct socket *so)
492 soisdisconnected(so);
493 return rip_detach(so);
497 rip_disconnect(struct socket *so)
499 if ((so->so_state & SS_ISCONNECTED) == 0)
501 return rip_abort(so);
505 rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
507 struct inpcb *inp = sotoinpcb(so);
508 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
510 if (nam->sa_len != sizeof(*addr))
513 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
514 (addr->sin_family != AF_IMPLINK)) ||
515 (addr->sin_addr.s_addr &&
516 ifa_ifwithaddr((struct sockaddr *)addr) == 0))
517 return EADDRNOTAVAIL;
518 inp->inp_laddr = addr->sin_addr;
523 rip_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
525 struct inpcb *inp = sotoinpcb(so);
526 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
528 if (nam->sa_len != sizeof(*addr))
530 if (TAILQ_EMPTY(&ifnet))
531 return EADDRNOTAVAIL;
532 if ((addr->sin_family != AF_INET) &&
533 (addr->sin_family != AF_IMPLINK))
535 inp->inp_faddr = addr->sin_addr;
541 rip_shutdown(struct socket *so)
548 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
549 struct mbuf *control, struct proc *p)
551 struct inpcb *inp = sotoinpcb(so);
554 if (so->so_state & SS_ISCONNECTED) {
559 dst = inp->inp_faddr.s_addr;
565 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
567 return rip_output(m, so, dst);
571 rip_pcblist(SYSCTL_HANDLER_ARGS)
574 struct inpcb *inp, **inp_list;
579 * The process of preparing the TCB list is too time-consuming and
580 * resource-intensive to repeat twice on every request.
582 if (req->oldptr == 0) {
583 n = ripcbinfo.ipi_count;
584 req->oldidx = 2 * (sizeof xig)
585 + (n + n/8) * sizeof(struct xinpcb);
589 if (req->newptr != 0)
593 * OK, now we're committed to doing something.
596 gencnt = ripcbinfo.ipi_gencnt;
597 n = ripcbinfo.ipi_count;
600 xig.xig_len = sizeof xig;
602 xig.xig_gen = gencnt;
603 xig.xig_sogen = so_gencnt;
604 error = SYSCTL_OUT(req, &xig, sizeof xig);
608 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
613 for (inp = ripcbinfo.listhead->lh_first, i = 0; inp && i < n;
614 inp = inp->inp_list.le_next) {
615 if (inp->inp_gencnt <= gencnt)
622 for (i = 0; i < n; i++) {
624 if (inp->inp_gencnt <= gencnt) {
626 xi.xi_len = sizeof xi;
627 /* XXX should avoid extra copy */
628 bcopy(inp, &xi.xi_inp, sizeof *inp);
630 sotoxsocket(inp->inp_socket, &xi.xi_socket);
631 error = SYSCTL_OUT(req, &xi, sizeof xi);
636 * Give the user an updated idea of our state.
637 * If the generation differs from what we told
638 * her before, she knows that something happened
639 * while we were processing this request, and it
640 * might be necessary to retry.
643 xig.xig_gen = ripcbinfo.ipi_gencnt;
644 xig.xig_sogen = so_gencnt;
645 xig.xig_count = ripcbinfo.ipi_count;
647 error = SYSCTL_OUT(req, &xig, sizeof xig);
649 free(inp_list, M_TEMP);
653 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
654 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
656 struct pr_usrreqs rip_usrreqs = {
657 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
658 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
659 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
660 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
661 in_setsockaddr, sosend, soreceive, sopoll