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;
89 static struct lwkt_token raw_token = LWKT_TOKEN_INITIALIZER(raw_token);
93 * hooks for multicast routing. They all default to NULL,
94 * so leave them not initialized and rely on BSS being set to 0.
97 /* The socket used to communicate with the multicast routing daemon. */
98 struct socket *ip_mrouter;
100 /* The various mrouter and rsvp functions */
101 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
102 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
103 int (*ip_mrouter_done)(void);
104 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
105 struct ip_moptions *);
106 int (*mrt_ioctl)(int, caddr_t);
107 int (*legal_vif_num)(int);
108 u_long (*ip_mcast_src)(int);
110 int (*rsvp_input_p)(struct mbuf **, int *, int);
111 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
112 void (*ip_rsvp_force_done)(struct socket *);
115 * Nominal space allocated to a raw ip socket.
121 * Raw interface to IP protocol.
125 * Initialize raw connection block queue.
130 in_pcbinfo_init(&ripcbinfo);
131 in_pcbportinfo_init(&ripcbportinfo, 1, FALSE, 0);
133 * XXX We don't use the hash list for raw IP, but it's easier
134 * to allocate a one entry hash list than it is to check all
135 * over the place for hashbase == NULL.
137 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
138 ripcbinfo.portinfo = &ripcbportinfo;
139 ripcbinfo.wildcardhashbase = hashinit(1, M_PCB,
140 &ripcbinfo.wildcardhashmask);
141 ripcbinfo.ipi_size = sizeof(struct inpcb);
145 * Setup generic address and protocol structures
146 * for raw_input routine, then pass them along with
150 rip_input(struct mbuf **mp, int *offp, int proto)
152 struct sockaddr_in ripsrc = { sizeof ripsrc, AF_INET };
153 struct mbuf *m = *mp;
154 struct ip *ip = mtod(m, struct ip *);
156 struct inpcb *last = NULL;
157 struct mbuf *opts = NULL;
161 ripsrc.sin_addr = ip->ip_src;
162 lwkt_gettoken(&raw_token);
163 LIST_FOREACH(inp, &ripcbinfo.pcblisthead, inp_list) {
164 if (inp->inp_flags & INP_PLACEMARKER)
167 if ((inp->inp_vflag & INP_IPV4) == 0)
170 if (inp->inp_ip_p && inp->inp_ip_p != proto)
172 if (inp->inp_laddr.s_addr != INADDR_ANY &&
173 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
175 if (inp->inp_faddr.s_addr != INADDR_ANY &&
176 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
179 struct mbuf *n = m_copypacket(m, MB_DONTWAIT);
182 /* check AH/ESP integrity. */
183 if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
185 ipsecstat.in_polvio++;
186 /* do not inject data to pcb */
190 /* check AH/ESP integrity. */
191 if (ipsec4_in_reject(n, last)) {
193 /* do not inject data to pcb */
195 #endif /*FAST_IPSEC*/
197 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
198 if (last->inp_flags & INP_CONTROLOPTS ||
199 last->inp_socket->so_options & SO_TIMESTAMP)
200 ip_savecontrol(last, &opts, ip, n);
201 if (ssb_appendaddr(&last->inp_socket->so_rcv,
202 (struct sockaddr *)&ripsrc, n,
204 /* should notify about lost packet */
209 sorwakeup(last->inp_socket);
211 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
218 /* check AH/ESP integrity. */
219 if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
221 ipsecstat.in_polvio++;
222 ipstat.ips_delivered--;
223 /* do not inject data to pcb */
227 /* check AH/ESP integrity. */
228 if (last && ipsec4_in_reject(m, last)) {
230 ipstat.ips_delivered--;
231 /* do not inject data to pcb */
233 #endif /*FAST_IPSEC*/
234 /* Check the minimum TTL for socket. */
235 if (last && ip->ip_ttl < last->inp_ip_minttl) {
237 ipstat.ips_delivered--;
239 if (last->inp_flags & INP_CONTROLOPTS ||
240 last->inp_socket->so_options & SO_TIMESTAMP)
241 ip_savecontrol(last, &opts, ip, m);
242 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
243 if (ssb_appendaddr(&last->inp_socket->so_rcv,
244 (struct sockaddr *)&ripsrc, m, opts) == 0) {
249 sorwakeup(last->inp_socket);
251 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
254 ipstat.ips_noproto++;
255 ipstat.ips_delivered--;
257 lwkt_reltoken(&raw_token);
258 return(IPPROTO_DONE);
262 * Generate IP header and pass packet to ip_output.
263 * Tack on options user may have setup with control call.
266 rip_output(struct mbuf *m, struct socket *so, ...)
269 struct inpcb *inp = so->so_pcb;
271 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
275 dst = __va_arg(ap, u_long);
279 * If the user handed us a complete IP packet, use it.
280 * Otherwise, allocate an mbuf for a header and fill it in.
282 if ((inp->inp_flags & INP_HDRINCL) == 0) {
283 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
287 M_PREPEND(m, sizeof(struct ip), MB_WAIT);
290 ip = mtod(m, struct ip *);
291 ip->ip_tos = inp->inp_ip_tos;
293 ip->ip_p = inp->inp_ip_p;
294 ip->ip_len = m->m_pkthdr.len;
295 ip->ip_src = inp->inp_laddr;
296 ip->ip_dst.s_addr = dst;
297 ip->ip_ttl = inp->inp_ip_ttl;
301 if (m->m_pkthdr.len > IP_MAXPACKET) {
305 if (m->m_len < sizeof(struct ip)) {
306 m = m_pullup(m, sizeof(struct ip));
310 ip = mtod(m, struct ip *);
311 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
313 /* Don't allow header length less than the minimum. */
314 if (hlen < sizeof(struct ip)) {
320 * Don't allow both user specified and setsockopt options.
321 * Don't allow packet length sizes that will crash.
323 if ((hlen != sizeof(struct ip) && inp->inp_options) ||
324 ip->ip_len > m->m_pkthdr.len || ip->ip_len < hlen) {
329 ip->ip_id = ip_newid();
331 /* Prevent ip_output from overwriting header fields */
332 flags |= IP_RAWOUTPUT;
336 return ip_output(m, inp->inp_options, &inp->inp_route, flags,
337 inp->inp_moptions, inp);
341 * Raw IP socket option processing.
344 rip_ctloutput(netmsg_t msg)
346 struct socket *so = msg->base.nm_so;
347 struct sockopt *sopt = msg->ctloutput.nm_sopt;
348 struct inpcb *inp = so->so_pcb;
351 if (sopt->sopt_level != IPPROTO_IP) {
358 switch (sopt->sopt_dir) {
360 switch (sopt->sopt_name) {
362 optval = inp->inp_flags & INP_HDRINCL;
363 soopt_from_kbuf(sopt, &optval, sizeof optval);
366 case IP_FW_ADD: /* ADD actually returns the body... */
369 error = ip_fw_sockopt(sopt);
374 case IP_DUMMYNET_GET:
375 error = ip_dn_sockopt(sopt);
386 case MRT_API_SUPPORT:
388 case MRT_ADD_BW_UPCALL:
389 case MRT_DEL_BW_UPCALL:
390 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
396 /* msg invalid now */
402 switch (sopt->sopt_name) {
404 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
409 inp->inp_flags |= INP_HDRINCL;
411 inp->inp_flags &= ~INP_HDRINCL;
420 error = ip_fw_ctl_ptr(sopt);
425 case IP_DUMMYNET_CONFIGURE:
426 case IP_DUMMYNET_DEL:
427 case IP_DUMMYNET_FLUSH:
428 error = ip_dn_sockopt(sopt);
432 error = ip_rsvp_init(so);
436 error = ip_rsvp_done();
440 case IP_RSVP_VIF_OFF:
441 error = ip_rsvp_vif ?
442 ip_rsvp_vif(so, sopt) : EINVAL;
453 case MRT_API_SUPPORT:
455 case MRT_ADD_BW_UPCALL:
456 case MRT_DEL_BW_UPCALL:
457 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
463 /* msg invalid now */
469 lwkt_replymsg(&msg->lmsg, error);
473 * This function exists solely to receive the PRC_IFDOWN messages which
474 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
475 * and calls in_ifadown() to remove all routes corresponding to that address.
476 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
480 rip_ctlinput(netmsg_t msg)
482 int cmd = msg->ctlinput.nm_cmd;
483 struct sockaddr *sa = msg->ctlinput.nm_arg;
484 struct in_ifaddr *ia;
485 struct in_ifaddr_container *iac;
492 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
495 if (ia->ia_ifa.ifa_addr == sa &&
496 (ia->ia_flags & IFA_ROUTE)) {
498 * in_ifscrub kills the interface route.
500 in_ifscrub(ia->ia_ifp, ia);
502 * in_ifadown gets rid of all the rest of
503 * the routes. This is not quite the right
504 * thing to do, but at least if we are running
505 * a routing process they will come back.
507 in_ifadown(&ia->ia_ifa, 0);
515 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
516 if (iac->ia->ia_ifa.ifa_addr == sa) {
521 if (ia == NULL || (ia->ia_flags & IFA_ROUTE))
524 ifp = ia->ia_ifa.ifa_ifp;
528 * Don't add prefix routes for CARP interfaces.
529 * Prefix routes creation is handled by CARP
530 * interfaces themselves.
532 if (ifp->if_type == IFT_CARP)
536 if ((ifp->if_flags & IFF_LOOPBACK) ||
537 (ifp->if_flags & IFF_POINTOPOINT))
540 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
542 ia->ia_flags |= IFA_ROUTE;
546 lwkt_replymsg(&msg->lmsg, 0);
549 u_long rip_sendspace = RIPSNDQ;
550 u_long rip_recvspace = RIPRCVQ;
552 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
553 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
554 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
555 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
558 rip_attach(netmsg_t msg)
560 struct socket *so = msg->base.nm_so;
561 int proto = msg->attach.nm_proto;
562 struct pru_attach_info *ai = msg->attach.nm_ai;
569 error = priv_check_cred(ai->p_ucred, PRIV_NETINET_RAW, NULL_CRED_OKAY);
573 error = soreserve(so, rip_sendspace, rip_recvspace, ai->sb_rlimit);
577 lwkt_gettoken(&raw_token);
578 error = in_pcballoc(so, &ripcbinfo);
580 inp = (struct inpcb *)so->so_pcb;
581 inp->inp_vflag |= INP_IPV4;
582 inp->inp_ip_p = proto;
583 inp->inp_ip_ttl = ip_defttl;
585 lwkt_reltoken(&raw_token);
588 lwkt_replymsg(&msg->lmsg, error);
592 rip_detach(netmsg_t msg)
594 struct socket *so = msg->base.nm_so;
600 if (so == ip_mrouter && ip_mrouter_done)
602 if (ip_rsvp_force_done)
603 ip_rsvp_force_done(so);
607 lwkt_replymsg(&msg->lmsg, 0);
611 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
612 * will sofree() it when we return.
615 rip_abort(netmsg_t msg)
617 struct socket *so = msg->base.nm_so;
620 soisdisconnected(so);
621 if (so->so_state & SS_NOFDREF) { /* XXX not sure why this test */
623 /* msg invalid now */
627 lwkt_replymsg(&msg->lmsg, error);
631 rip_disconnect(netmsg_t msg)
633 struct socket *so = msg->base.nm_so;
636 if (so->so_state & SS_ISCONNECTED) {
639 /* msg invalid now */
644 lwkt_replymsg(&msg->lmsg, error);
648 rip_bind(netmsg_t msg)
650 struct socket *so = msg->base.nm_so;
651 struct sockaddr *nam = msg->bind.nm_nam;
652 struct inpcb *inp = so->so_pcb;
653 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
656 if (nam->sa_len == sizeof(*addr)) {
657 if (TAILQ_EMPTY(&ifnet) ||
658 ((addr->sin_family != AF_INET) &&
659 (addr->sin_family != AF_IMPLINK)) ||
660 (addr->sin_addr.s_addr != INADDR_ANY &&
661 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) {
662 error = EADDRNOTAVAIL;
664 inp->inp_laddr = addr->sin_addr;
670 lwkt_replymsg(&msg->lmsg, error);
674 rip_connect(netmsg_t msg)
676 struct socket *so = msg->base.nm_so;
677 struct sockaddr *nam = msg->connect.nm_nam;
678 struct inpcb *inp = so->so_pcb;
679 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
682 if (nam->sa_len != sizeof(*addr)) {
684 } else if (TAILQ_EMPTY(&ifnet)) {
685 error = EADDRNOTAVAIL;
687 if ((addr->sin_family != AF_INET) &&
688 (addr->sin_family != AF_IMPLINK)) {
689 error = EAFNOSUPPORT;
691 inp->inp_faddr = addr->sin_addr;
696 lwkt_replymsg(&msg->lmsg, error);
700 rip_shutdown(netmsg_t msg)
702 socantsendmore(msg->base.nm_so);
703 lwkt_replymsg(&msg->lmsg, 0);
707 rip_send(netmsg_t msg)
709 struct socket *so = msg->base.nm_so;
710 struct mbuf *m = msg->send.nm_m;
711 /*struct mbuf *control = msg->send.nm_control;*/
712 struct sockaddr *nam = msg->send.nm_addr;
713 /*int flags = msg->send.nm_flags;*/
714 struct inpcb *inp = so->so_pcb;
718 if (so->so_state & SS_ISCONNECTED) {
723 dst = inp->inp_faddr.s_addr;
724 error = rip_output(m, so, dst);
731 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
732 error = rip_output(m, so, dst);
735 lwkt_replymsg(&msg->lmsg, error);
738 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, &ripcbinfo, 0,
739 in_pcblist_global, "S,xinpcb", "List of active raw IP sockets");
741 struct pr_usrreqs rip_usrreqs = {
742 .pru_abort = rip_abort,
743 .pru_accept = pr_generic_notsupp,
744 .pru_attach = rip_attach,
745 .pru_bind = rip_bind,
746 .pru_connect = rip_connect,
747 .pru_connect2 = pr_generic_notsupp,
748 .pru_control = in_control_dispatch,
749 .pru_detach = rip_detach,
750 .pru_disconnect = rip_disconnect,
751 .pru_listen = pr_generic_notsupp,
752 .pru_peeraddr = in_setpeeraddr_dispatch,
753 .pru_rcvd = pr_generic_notsupp,
754 .pru_rcvoob = pr_generic_notsupp,
755 .pru_send = rip_send,
756 .pru_sense = pru_sense_null,
757 .pru_shutdown = rip_shutdown,
758 .pru_sockaddr = in_setsockaddr_dispatch,
759 .pru_sosend = sosend,
760 .pru_soreceive = soreceive