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/ipfw3/ip_fw.h>
73 #include <net/dummynet/ip_dummynet.h>
74 #include <net/dummynet3/ip_dummynet3.h>
77 #include <netproto/ipsec/ipsec.h>
81 #include <netinet6/ipsec.h>
84 struct inpcbinfo ripcbinfo;
85 struct inpcbportinfo ripcbportinfo;
87 /* control hooks for ipfw and dummynet */
88 ip_fw_ctl_t *ip_fw_ctl_ptr;
89 ip_fw_ctl_t *ip_fw_ctl_x_ptr;
90 ip_dn_ctl_t *ip_dn_ctl_ptr;
94 * hooks for multicast routing. They all default to NULL,
95 * so leave them not initialized and rely on BSS being set to 0.
98 /* The socket used to communicate with the multicast routing daemon. */
99 struct socket *ip_mrouter;
101 /* The various mrouter and rsvp functions */
102 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
103 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
104 int (*ip_mrouter_done)(void);
105 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
106 struct ip_moptions *);
107 int (*mrt_ioctl)(int, caddr_t);
108 int (*legal_vif_num)(int);
109 u_long (*ip_mcast_src)(int);
111 int (*rsvp_input_p)(struct mbuf **, int *, int);
112 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
113 void (*ip_rsvp_force_done)(struct socket *);
116 * Nominal space allocated to a raw ip socket.
122 * Raw interface to IP protocol.
126 * Initialize raw connection block queue.
131 in_pcbinfo_init(&ripcbinfo, 0, FALSE);
132 in_pcbportinfo_init(&ripcbportinfo, 1, 0);
134 * XXX We don't use the hash list for raw IP, but it's easier
135 * to allocate a one entry hash list than it is to check all
136 * over the place for hashbase == NULL.
138 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
139 ripcbinfo.portinfo = &ripcbportinfo;
140 ripcbinfo.wildcardhashbase = hashinit(1, M_PCB,
141 &ripcbinfo.wildcardhashmask);
142 ripcbinfo.ipi_size = sizeof(struct inpcb);
146 * Setup generic address and protocol structures
147 * for raw_input routine, then pass them along with
151 rip_input(struct mbuf **mp, int *offp, int proto)
153 struct sockaddr_in ripsrc = { sizeof ripsrc, AF_INET };
154 struct mbuf *m = *mp;
155 struct ip *ip = mtod(m, struct ip *);
157 struct inpcb *last = NULL;
158 struct mbuf *opts = NULL;
164 ripsrc.sin_addr = ip->ip_src;
165 LIST_FOREACH(inp, &ripcbinfo.pcblisthead, inp_list) {
166 if (inp->inp_flags & INP_PLACEMARKER)
169 if (!INP_ISIPV4(inp))
172 if (inp->inp_ip_p && inp->inp_ip_p != proto)
174 if (inp->inp_laddr.s_addr != INADDR_ANY &&
175 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
177 if (inp->inp_faddr.s_addr != INADDR_ANY &&
178 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
181 struct mbuf *n = m_copypacket(m, M_NOWAIT);
184 /* check AH/ESP integrity. */
185 if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
187 ipsecstat.in_polvio++;
188 /* do not inject data to pcb */
192 /* check AH/ESP integrity. */
193 if (ipsec4_in_reject(n, last)) {
195 /* do not inject data to pcb */
197 #endif /*FAST_IPSEC*/
199 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
200 if (last->inp_flags & INP_CONTROLOPTS ||
201 last->inp_socket->so_options & SO_TIMESTAMP)
202 ip_savecontrol(last, &opts, ip, n);
203 if (ssb_appendaddr(&last->inp_socket->so_rcv,
204 (struct sockaddr *)&ripsrc, n,
206 /* should notify about lost packet */
211 sorwakeup(last->inp_socket);
213 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
220 /* check AH/ESP integrity. */
221 if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
223 ipsecstat.in_polvio++;
224 ipstat.ips_delivered--;
225 /* do not inject data to pcb */
229 /* check AH/ESP integrity. */
230 if (last && ipsec4_in_reject(m, last)) {
232 ipstat.ips_delivered--;
233 /* do not inject data to pcb */
235 #endif /*FAST_IPSEC*/
236 /* Check the minimum TTL for socket. */
237 if (last && ip->ip_ttl < last->inp_ip_minttl) {
239 ipstat.ips_delivered--;
241 if (last->inp_flags & INP_CONTROLOPTS ||
242 last->inp_socket->so_options & SO_TIMESTAMP)
243 ip_savecontrol(last, &opts, ip, m);
244 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
245 if (ssb_appendaddr(&last->inp_socket->so_rcv,
246 (struct sockaddr *)&ripsrc, m, opts) == 0) {
251 sorwakeup(last->inp_socket);
253 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
256 ipstat.ips_noproto++;
257 ipstat.ips_delivered--;
259 return(IPPROTO_DONE);
263 * Generate IP header and pass packet to ip_output.
264 * Tack on options user may have setup with control call.
267 rip_output(struct mbuf *m, struct socket *so, ...)
270 struct inpcb *inp = so->so_pcb;
272 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
278 dst = __va_arg(ap, u_long);
282 * If the user handed us a complete IP packet, use it.
283 * Otherwise, allocate an mbuf for a header and fill it in.
285 if ((inp->inp_flags & INP_HDRINCL) == 0) {
286 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
290 M_PREPEND(m, sizeof(struct ip), M_WAITOK);
293 ip = mtod(m, struct ip *);
294 ip->ip_tos = inp->inp_ip_tos;
296 ip->ip_p = inp->inp_ip_p;
297 ip->ip_len = m->m_pkthdr.len;
298 ip->ip_src = inp->inp_laddr;
299 ip->ip_dst.s_addr = dst;
300 ip->ip_ttl = inp->inp_ip_ttl;
304 if (m->m_pkthdr.len > IP_MAXPACKET) {
308 if (m->m_len < sizeof(struct ip)) {
309 m = m_pullup(m, sizeof(struct ip));
313 ip = mtod(m, struct ip *);
314 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
316 /* Don't allow header length less than the minimum. */
317 if (hlen < sizeof(struct ip)) {
323 * Don't allow both user specified and setsockopt options.
324 * Don't allow packet length sizes that will crash.
326 if ((hlen != sizeof(struct ip) && inp->inp_options) ||
327 ip->ip_len > m->m_pkthdr.len || ip->ip_len < hlen) {
332 ip->ip_id = ip_newid();
334 /* Prevent ip_output from overwriting header fields */
335 flags |= IP_RAWOUTPUT;
339 return ip_output(m, inp->inp_options, &inp->inp_route, flags,
340 inp->inp_moptions, inp);
344 * Raw IP socket option processing.
347 rip_ctloutput(netmsg_t msg)
349 struct socket *so = msg->base.nm_so;
350 struct sockopt *sopt = msg->ctloutput.nm_sopt;
351 struct inpcb *inp = so->so_pcb;
358 /* Get socket's owner cpuid hint */
359 if (sopt->sopt_level == SOL_SOCKET &&
360 sopt->sopt_dir == SOPT_GET &&
361 sopt->sopt_name == SO_CPUHINT) {
363 soopt_from_kbuf(sopt, &optval, sizeof(optval));
367 if (sopt->sopt_level != IPPROTO_IP) {
372 switch (sopt->sopt_dir) {
374 switch (sopt->sopt_name) {
376 optval = inp->inp_flags & INP_HDRINCL;
377 soopt_from_kbuf(sopt, &optval, sizeof optval);
381 error = ip_fw3_sockopt(sopt);
385 case IP_FW_ADD: /* ADD actually returns the body... */
388 error = ip_fw_sockopt(sopt);
393 case IP_DUMMYNET_GET:
394 error = ip_dn_sockopt(sopt);
405 case MRT_API_SUPPORT:
407 case MRT_ADD_BW_UPCALL:
408 case MRT_DEL_BW_UPCALL:
409 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
415 /* msg invalid now */
421 switch (sopt->sopt_name) {
423 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
428 inp->inp_flags |= INP_HDRINCL;
430 inp->inp_flags &= ~INP_HDRINCL;
434 error = ip_fw_ctl_x_ptr(sopt);
444 error = ip_fw_ctl_ptr(sopt);
449 case IP_DUMMYNET_CONFIGURE:
450 case IP_DUMMYNET_DEL:
451 case IP_DUMMYNET_FLUSH:
452 error = ip_dn_sockopt(sopt);
456 error = ip_rsvp_init(so);
460 error = ip_rsvp_done();
464 case IP_RSVP_VIF_OFF:
465 error = ip_rsvp_vif ?
466 ip_rsvp_vif(so, sopt) : EINVAL;
477 case MRT_API_SUPPORT:
479 case MRT_ADD_BW_UPCALL:
480 case MRT_DEL_BW_UPCALL:
481 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
487 /* msg invalid now */
493 lwkt_replymsg(&msg->lmsg, error);
497 * This function exists solely to receive the PRC_IFDOWN messages which
498 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
499 * and calls in_ifadown() to remove all routes corresponding to that address.
500 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
504 rip_ctlinput(netmsg_t msg)
506 int cmd = msg->ctlinput.nm_cmd;
507 struct sockaddr *sa = msg->ctlinput.nm_arg;
508 struct in_ifaddr *ia;
509 struct in_ifaddr_container *iac;
518 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
521 if (ia->ia_ifa.ifa_addr == sa &&
522 (ia->ia_flags & IFA_ROUTE)) {
524 * in_ifscrub kills the interface route.
526 in_ifscrub(ia->ia_ifp, ia);
528 * in_ifadown gets rid of all the rest of
529 * the routes. This is not quite the right
530 * thing to do, but at least if we are running
531 * a routing process they will come back.
533 in_ifadown(&ia->ia_ifa, 0);
541 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
542 if (iac->ia->ia_ifa.ifa_addr == sa) {
547 if (ia == NULL || (ia->ia_flags & IFA_ROUTE))
550 ifp = ia->ia_ifa.ifa_ifp;
554 * Don't add prefix routes for CARP interfaces.
555 * Prefix routes creation is handled by CARP
556 * interfaces themselves.
558 if (ifp->if_type == IFT_CARP)
562 if ((ifp->if_flags & IFF_LOOPBACK) ||
563 (ifp->if_flags & IFF_POINTOPOINT))
566 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
568 ia->ia_flags |= IFA_ROUTE;
572 lwkt_replymsg(&msg->lmsg, 0);
575 u_long rip_sendspace = RIPSNDQ;
576 u_long rip_recvspace = RIPRCVQ;
578 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
579 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
580 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
581 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
584 rip_attach(netmsg_t msg)
586 struct socket *so = msg->base.nm_so;
587 int proto = msg->attach.nm_proto;
588 struct pru_attach_info *ai = msg->attach.nm_ai;
597 error = priv_check_cred(ai->p_ucred, PRIV_NETINET_RAW, NULL_CRED_OKAY);
601 error = soreserve(so, rip_sendspace, rip_recvspace, ai->sb_rlimit);
605 error = in_pcballoc(so, &ripcbinfo);
607 inp = (struct inpcb *)so->so_pcb;
608 inp->inp_ip_p = proto;
609 inp->inp_ip_ttl = ip_defttl;
612 lwkt_replymsg(&msg->lmsg, error);
616 rip_detach(netmsg_t msg)
618 struct socket *so = msg->base.nm_so;
626 if (so == ip_mrouter && ip_mrouter_done)
628 if (ip_rsvp_force_done)
629 ip_rsvp_force_done(so);
633 lwkt_replymsg(&msg->lmsg, 0);
637 rip_abort(netmsg_t msg)
640 * Raw socket does not support listen(2),
641 * so this should never be called.
643 panic("rip_abort is called");
647 rip_disconnect(netmsg_t msg)
649 struct socket *so = msg->base.nm_so;
654 if (so->so_state & SS_ISCONNECTED) {
655 soisdisconnected(so);
660 lwkt_replymsg(&msg->lmsg, error);
664 rip_bind(netmsg_t msg)
666 struct socket *so = msg->base.nm_so;
667 struct sockaddr *nam = msg->bind.nm_nam;
668 struct inpcb *inp = so->so_pcb;
669 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
674 if (nam->sa_len == sizeof(*addr)) {
675 if (ifnet_array_isempty() ||
676 ((addr->sin_family != AF_INET) &&
677 (addr->sin_family != AF_IMPLINK)) ||
678 (addr->sin_addr.s_addr != INADDR_ANY &&
679 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) {
680 error = EADDRNOTAVAIL;
682 inp->inp_laddr = addr->sin_addr;
688 lwkt_replymsg(&msg->lmsg, error);
692 rip_connect(netmsg_t msg)
694 struct socket *so = msg->base.nm_so;
695 struct sockaddr *nam = msg->connect.nm_nam;
696 struct inpcb *inp = so->so_pcb;
697 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
702 if (nam->sa_len != sizeof(*addr)) {
704 } else if (ifnet_array_isempty()) {
705 error = EADDRNOTAVAIL;
707 if ((addr->sin_family != AF_INET) &&
708 (addr->sin_family != AF_IMPLINK)) {
709 error = EAFNOSUPPORT;
711 inp->inp_faddr = addr->sin_addr;
716 lwkt_replymsg(&msg->lmsg, error);
720 rip_shutdown(netmsg_t msg)
724 socantsendmore(msg->base.nm_so);
725 lwkt_replymsg(&msg->lmsg, 0);
729 rip_send(netmsg_t msg)
731 struct socket *so = msg->base.nm_so;
732 struct mbuf *m = msg->send.nm_m;
733 /*struct mbuf *control = msg->send.nm_control;*/
734 struct sockaddr *nam = msg->send.nm_addr;
735 /*int flags = msg->send.nm_flags;*/
736 struct inpcb *inp = so->so_pcb;
742 if (so->so_state & SS_ISCONNECTED) {
747 dst = inp->inp_faddr.s_addr;
748 error = rip_output(m, so, dst);
755 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
756 error = rip_output(m, so, dst);
759 lwkt_replymsg(&msg->lmsg, error);
762 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, &ripcbinfo, 1,
763 in_pcblist_global, "S,xinpcb", "List of active raw IP sockets");
765 struct pr_usrreqs rip_usrreqs = {
766 .pru_abort = rip_abort,
767 .pru_accept = pr_generic_notsupp,
768 .pru_attach = rip_attach,
769 .pru_bind = rip_bind,
770 .pru_connect = rip_connect,
771 .pru_connect2 = pr_generic_notsupp,
772 .pru_control = in_control_dispatch,
773 .pru_detach = rip_detach,
774 .pru_disconnect = rip_disconnect,
775 .pru_listen = pr_generic_notsupp,
776 .pru_peeraddr = in_setpeeraddr_dispatch,
777 .pru_rcvd = pr_generic_notsupp,
778 .pru_rcvoob = pr_generic_notsupp,
779 .pru_send = rip_send,
780 .pru_sense = pru_sense_null,
781 .pru_shutdown = rip_shutdown,
782 .pru_sockaddr = in_setsockaddr_dispatch,
783 .pru_sosend = sosend,
784 .pru_soreceive = soreceive