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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
30 * $FreeBSD: src/sys/netinet/raw_ip.c,v 1.64.2.16 2003/08/24 08:24:38 hsu Exp $
33 #include "opt_inet6.h"
34 #include "opt_ipsec.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/malloc.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
50 #include <sys/thread2.h>
51 #include <sys/socketvar2.h>
52 #include <sys/msgport2.h>
54 #include <machine/stdarg.h>
58 #include <net/if_types.h>
60 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/in_pcb.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip_var.h>
70 #include <net/ip_mroute/ip_mroute.h>
71 #include <net/ipfw/ip_fw.h>
72 #include <net/dummynet/ip_dummynet.h>
75 #include <netproto/ipsec/ipsec.h>
79 #include <netinet6/ipsec.h>
82 struct inpcbinfo ripcbinfo;
83 struct inpcbportinfo ripcbportinfo;
85 /* control hooks for ipfw and dummynet */
86 ip_fw_ctl_t *ip_fw_ctl_ptr;
87 ip_dn_ctl_t *ip_dn_ctl_ptr;
90 * hooks for multicast routing. They all default to NULL,
91 * so leave them not initialized and rely on BSS being set to 0.
94 /* The socket used to communicate with the multicast routing daemon. */
95 struct socket *ip_mrouter;
97 /* The various mrouter and rsvp functions */
98 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
99 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
100 int (*ip_mrouter_done)(void);
101 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
102 struct ip_moptions *);
103 int (*mrt_ioctl)(int, caddr_t);
104 int (*legal_vif_num)(int);
105 u_long (*ip_mcast_src)(int);
107 int (*rsvp_input_p)(struct mbuf **, int *, int);
108 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
109 void (*ip_rsvp_force_done)(struct socket *);
112 * Nominal space allocated to a raw ip socket.
118 * Raw interface to IP protocol.
122 * Initialize raw connection block queue.
127 in_pcbinfo_init(&ripcbinfo, 0, FALSE);
128 in_pcbportinfo_init(&ripcbportinfo, 1, FALSE, 0);
130 * XXX We don't use the hash list for raw IP, but it's easier
131 * to allocate a one entry hash list than it is to check all
132 * over the place for hashbase == NULL.
134 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
135 ripcbinfo.portinfo = &ripcbportinfo;
136 ripcbinfo.wildcardhashbase = hashinit(1, M_PCB,
137 &ripcbinfo.wildcardhashmask);
138 ripcbinfo.ipi_size = sizeof(struct inpcb);
142 * Setup generic address and protocol structures
143 * for raw_input routine, then pass them along with
147 rip_input(struct mbuf **mp, int *offp, int proto)
149 struct sockaddr_in ripsrc = { sizeof ripsrc, AF_INET };
150 struct mbuf *m = *mp;
151 struct ip *ip = mtod(m, struct ip *);
153 struct inpcb *last = NULL;
154 struct mbuf *opts = NULL;
156 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
161 ripsrc.sin_addr = ip->ip_src;
162 LIST_FOREACH(inp, &ripcbinfo.pcblisthead, inp_list) {
163 if (inp->inp_flags & INP_PLACEMARKER)
166 if (!INP_ISIPV4(inp))
169 if (inp->inp_ip_p && inp->inp_ip_p != proto)
171 if (inp->inp_laddr.s_addr != INADDR_ANY &&
172 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
174 if (inp->inp_faddr.s_addr != INADDR_ANY &&
175 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
178 struct mbuf *n = m_copypacket(m, MB_DONTWAIT);
181 /* check AH/ESP integrity. */
182 if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
184 ipsecstat.in_polvio++;
185 /* do not inject data to pcb */
189 /* check AH/ESP integrity. */
190 if (ipsec4_in_reject(n, last)) {
192 /* do not inject data to pcb */
194 #endif /*FAST_IPSEC*/
196 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
197 if (last->inp_flags & INP_CONTROLOPTS ||
198 last->inp_socket->so_options & SO_TIMESTAMP)
199 ip_savecontrol(last, &opts, ip, n);
200 if (ssb_appendaddr(&last->inp_socket->so_rcv,
201 (struct sockaddr *)&ripsrc, n,
203 /* should notify about lost packet */
208 sorwakeup(last->inp_socket);
210 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
217 /* check AH/ESP integrity. */
218 if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
220 ipsecstat.in_polvio++;
221 ipstat.ips_delivered--;
222 /* do not inject data to pcb */
226 /* check AH/ESP integrity. */
227 if (last && ipsec4_in_reject(m, last)) {
229 ipstat.ips_delivered--;
230 /* do not inject data to pcb */
232 #endif /*FAST_IPSEC*/
233 /* Check the minimum TTL for socket. */
234 if (last && ip->ip_ttl < last->inp_ip_minttl) {
236 ipstat.ips_delivered--;
238 if (last->inp_flags & INP_CONTROLOPTS ||
239 last->inp_socket->so_options & SO_TIMESTAMP)
240 ip_savecontrol(last, &opts, ip, m);
241 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
242 if (ssb_appendaddr(&last->inp_socket->so_rcv,
243 (struct sockaddr *)&ripsrc, m, opts) == 0) {
248 sorwakeup(last->inp_socket);
250 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
253 ipstat.ips_noproto++;
254 ipstat.ips_delivered--;
256 return(IPPROTO_DONE);
260 * Generate IP header and pass packet to ip_output.
261 * Tack on options user may have setup with control call.
264 rip_output(struct mbuf *m, struct socket *so, ...)
267 struct inpcb *inp = so->so_pcb;
269 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
272 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
276 dst = __va_arg(ap, u_long);
280 * If the user handed us a complete IP packet, use it.
281 * Otherwise, allocate an mbuf for a header and fill it in.
283 if ((inp->inp_flags & INP_HDRINCL) == 0) {
284 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
288 M_PREPEND(m, sizeof(struct ip), MB_WAIT);
291 ip = mtod(m, struct ip *);
292 ip->ip_tos = inp->inp_ip_tos;
294 ip->ip_p = inp->inp_ip_p;
295 ip->ip_len = m->m_pkthdr.len;
296 ip->ip_src = inp->inp_laddr;
297 ip->ip_dst.s_addr = dst;
298 ip->ip_ttl = inp->inp_ip_ttl;
302 if (m->m_pkthdr.len > IP_MAXPACKET) {
306 if (m->m_len < sizeof(struct ip)) {
307 m = m_pullup(m, sizeof(struct ip));
311 ip = mtod(m, struct ip *);
312 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
314 /* Don't allow header length less than the minimum. */
315 if (hlen < sizeof(struct ip)) {
321 * Don't allow both user specified and setsockopt options.
322 * Don't allow packet length sizes that will crash.
324 if ((hlen != sizeof(struct ip) && inp->inp_options) ||
325 ip->ip_len > m->m_pkthdr.len || ip->ip_len < hlen) {
330 ip->ip_id = ip_newid();
332 /* Prevent ip_output from overwriting header fields */
333 flags |= IP_RAWOUTPUT;
337 return ip_output(m, inp->inp_options, &inp->inp_route, flags,
338 inp->inp_moptions, inp);
342 * Raw IP socket option processing.
345 rip_ctloutput(netmsg_t msg)
347 struct socket *so = msg->base.nm_so;
348 struct sockopt *sopt = msg->ctloutput.nm_sopt;
349 struct inpcb *inp = so->so_pcb;
352 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
357 /* Get socket's owner cpuid hint */
358 if (sopt->sopt_level == SOL_SOCKET &&
359 sopt->sopt_dir == SOPT_GET &&
360 sopt->sopt_name == SO_CPUHINT) {
362 soopt_from_kbuf(sopt, &optval, sizeof(optval));
366 if (sopt->sopt_level != IPPROTO_IP) {
371 switch (sopt->sopt_dir) {
373 switch (sopt->sopt_name) {
375 optval = inp->inp_flags & INP_HDRINCL;
376 soopt_from_kbuf(sopt, &optval, sizeof optval);
379 case IP_FW_ADD: /* ADD actually returns the body... */
382 error = ip_fw_sockopt(sopt);
387 case IP_DUMMYNET_GET:
388 error = ip_dn_sockopt(sopt);
399 case MRT_API_SUPPORT:
401 case MRT_ADD_BW_UPCALL:
402 case MRT_DEL_BW_UPCALL:
403 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
409 /* msg invalid now */
415 switch (sopt->sopt_name) {
417 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
422 inp->inp_flags |= INP_HDRINCL;
424 inp->inp_flags &= ~INP_HDRINCL;
433 error = ip_fw_ctl_ptr(sopt);
438 case IP_DUMMYNET_CONFIGURE:
439 case IP_DUMMYNET_DEL:
440 case IP_DUMMYNET_FLUSH:
441 error = ip_dn_sockopt(sopt);
445 error = ip_rsvp_init(so);
449 error = ip_rsvp_done();
453 case IP_RSVP_VIF_OFF:
454 error = ip_rsvp_vif ?
455 ip_rsvp_vif(so, sopt) : EINVAL;
466 case MRT_API_SUPPORT:
468 case MRT_ADD_BW_UPCALL:
469 case MRT_DEL_BW_UPCALL:
470 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
476 /* msg invalid now */
482 lwkt_replymsg(&msg->lmsg, error);
486 * This function exists solely to receive the PRC_IFDOWN messages which
487 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
488 * and calls in_ifadown() to remove all routes corresponding to that address.
489 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
493 rip_ctlinput(netmsg_t msg)
495 int cmd = msg->ctlinput.nm_cmd;
496 struct sockaddr *sa = msg->ctlinput.nm_arg;
497 struct in_ifaddr *ia;
498 struct in_ifaddr_container *iac;
503 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
508 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
511 if (ia->ia_ifa.ifa_addr == sa &&
512 (ia->ia_flags & IFA_ROUTE)) {
514 * in_ifscrub kills the interface route.
516 in_ifscrub(ia->ia_ifp, ia);
518 * in_ifadown gets rid of all the rest of
519 * the routes. This is not quite the right
520 * thing to do, but at least if we are running
521 * a routing process they will come back.
523 in_ifadown(&ia->ia_ifa, 0);
531 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
532 if (iac->ia->ia_ifa.ifa_addr == sa) {
537 if (ia == NULL || (ia->ia_flags & IFA_ROUTE))
540 ifp = ia->ia_ifa.ifa_ifp;
544 * Don't add prefix routes for CARP interfaces.
545 * Prefix routes creation is handled by CARP
546 * interfaces themselves.
548 if (ifp->if_type == IFT_CARP)
552 if ((ifp->if_flags & IFF_LOOPBACK) ||
553 (ifp->if_flags & IFF_POINTOPOINT))
556 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
558 ia->ia_flags |= IFA_ROUTE;
562 lwkt_replymsg(&msg->lmsg, 0);
565 u_long rip_sendspace = RIPSNDQ;
566 u_long rip_recvspace = RIPRCVQ;
568 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
569 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
570 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
571 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
574 rip_attach(netmsg_t msg)
576 struct socket *so = msg->base.nm_so;
577 int proto = msg->attach.nm_proto;
578 struct pru_attach_info *ai = msg->attach.nm_ai;
582 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
588 error = priv_check_cred(ai->p_ucred, PRIV_NETINET_RAW, NULL_CRED_OKAY);
592 error = soreserve(so, rip_sendspace, rip_recvspace, ai->sb_rlimit);
596 error = in_pcballoc(so, &ripcbinfo);
598 inp = (struct inpcb *)so->so_pcb;
599 inp->inp_ip_p = proto;
600 inp->inp_ip_ttl = ip_defttl;
603 lwkt_replymsg(&msg->lmsg, error);
607 rip_detach(netmsg_t msg)
609 struct socket *so = msg->base.nm_so;
612 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
618 if (so == ip_mrouter && ip_mrouter_done)
620 if (ip_rsvp_force_done)
621 ip_rsvp_force_done(so);
625 lwkt_replymsg(&msg->lmsg, 0);
629 rip_abort(netmsg_t msg)
632 * Raw socket does not support listen(2),
633 * so this should never be called.
635 panic("rip_abort is called");
639 rip_disconnect(netmsg_t msg)
641 struct socket *so = msg->base.nm_so;
644 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
647 if (so->so_state & SS_ISCONNECTED) {
648 soisdisconnected(so);
653 lwkt_replymsg(&msg->lmsg, error);
657 rip_bind(netmsg_t msg)
659 struct socket *so = msg->base.nm_so;
660 struct sockaddr *nam = msg->bind.nm_nam;
661 struct inpcb *inp = so->so_pcb;
662 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
665 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
668 if (nam->sa_len == sizeof(*addr)) {
669 if (TAILQ_EMPTY(&ifnet) ||
670 ((addr->sin_family != AF_INET) &&
671 (addr->sin_family != AF_IMPLINK)) ||
672 (addr->sin_addr.s_addr != INADDR_ANY &&
673 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) {
674 error = EADDRNOTAVAIL;
676 inp->inp_laddr = addr->sin_addr;
682 lwkt_replymsg(&msg->lmsg, error);
686 rip_connect(netmsg_t msg)
688 struct socket *so = msg->base.nm_so;
689 struct sockaddr *nam = msg->connect.nm_nam;
690 struct inpcb *inp = so->so_pcb;
691 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
694 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
697 if (nam->sa_len != sizeof(*addr)) {
699 } else if (TAILQ_EMPTY(&ifnet)) {
700 error = EADDRNOTAVAIL;
702 if ((addr->sin_family != AF_INET) &&
703 (addr->sin_family != AF_IMPLINK)) {
704 error = EAFNOSUPPORT;
706 inp->inp_faddr = addr->sin_addr;
711 lwkt_replymsg(&msg->lmsg, error);
715 rip_shutdown(netmsg_t msg)
717 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
720 socantsendmore(msg->base.nm_so);
721 lwkt_replymsg(&msg->lmsg, 0);
725 rip_send(netmsg_t msg)
727 struct socket *so = msg->base.nm_so;
728 struct mbuf *m = msg->send.nm_m;
729 /*struct mbuf *control = msg->send.nm_control;*/
730 struct sockaddr *nam = msg->send.nm_addr;
731 /*int flags = msg->send.nm_flags;*/
732 struct inpcb *inp = so->so_pcb;
736 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
739 if (so->so_state & SS_ISCONNECTED) {
744 dst = inp->inp_faddr.s_addr;
745 error = rip_output(m, so, dst);
752 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
753 error = rip_output(m, so, dst);
756 lwkt_replymsg(&msg->lmsg, error);
759 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, &ripcbinfo, 1,
760 in_pcblist_global, "S,xinpcb", "List of active raw IP sockets");
762 struct pr_usrreqs rip_usrreqs = {
763 .pru_abort = rip_abort,
764 .pru_accept = pr_generic_notsupp,
765 .pru_attach = rip_attach,
766 .pru_bind = rip_bind,
767 .pru_connect = rip_connect,
768 .pru_connect2 = pr_generic_notsupp,
769 .pru_control = in_control_dispatch,
770 .pru_detach = rip_detach,
771 .pru_disconnect = rip_disconnect,
772 .pru_listen = pr_generic_notsupp,
773 .pru_peeraddr = in_setpeeraddr_dispatch,
774 .pru_rcvd = pr_generic_notsupp,
775 .pru_rcvoob = pr_generic_notsupp,
776 .pru_send = rip_send,
777 .pru_sense = pru_sense_null,
778 .pru_shutdown = rip_shutdown,
779 .pru_sockaddr = in_setsockaddr_dispatch,
780 .pru_sosend = sosend,
781 .pru_soreceive = soreceive