1 //==========================================================================
3 // src/sys/netinet/raw_ip.c
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
23 * Copyright (c) 1982, 1986, 1988, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
55 * $FreeBSD: src/sys/netinet/raw_ip.c,v 1.64.2.8 2001/07/29 19:32:40 ume Exp $
58 #include <sys/param.h>
59 #include <sys/malloc.h>
61 #include <sys/protosw.h>
62 #include <sys/socket.h>
63 #include <sys/socketvar.h>
64 #include <sys/sysctl.h>
67 #include <net/route.h>
70 #include <netinet/in.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/in_pcb.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip_var.h>
76 #include <netinet/ip_mroute.h>
78 #include <netinet/ip_fw.h>
81 #include <netinet6/ipsec.h>
85 #include <netinet/ip_dummynet.h>
88 struct inpcbhead ripcb;
89 struct inpcbinfo ripcbinfo;
92 * Nominal space allocated to a raw ip socket.
98 * Raw interface to IP protocol.
102 * Initialize raw connection block q.
108 ripcbinfo.listhead = &ripcb;
110 * XXX We don't use the hash list for raw IP, but it's easier
111 * to allocate a one entry hash list than it is to check all
112 * over the place for hashbase == NULL.
114 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
115 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
116 ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
117 maxsockets, ZONE_INTERRUPT, 0);
120 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
122 * Setup generic address and protocol structures
123 * for raw_input routine, then pass them along with
131 register struct ip *ip = mtod(m, struct ip *);
132 register struct inpcb *inp;
133 struct inpcb *last = 0;
134 struct mbuf *opts = 0;
135 int proto = ip->ip_p;
137 ripsrc.sin_addr = ip->ip_src;
138 LIST_FOREACH(inp, &ripcb, inp_list) {
140 if ((inp->inp_vflag & INP_IPV4) == 0)
143 if (inp->inp_ip_p && inp->inp_ip_p != proto)
145 if (inp->inp_laddr.s_addr &&
146 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
148 if (inp->inp_faddr.s_addr &&
149 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
152 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
155 /* check AH/ESP integrity. */
156 if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
158 ipsecstat.in_polvio++;
159 /* do not inject data to pcb */
163 if (last->inp_flags & INP_CONTROLOPTS ||
164 last->inp_socket->so_options & SO_TIMESTAMP)
165 ip_savecontrol(last, &opts, ip, n);
166 if (sbappendaddr(&last->inp_socket->so_rcv,
167 (struct sockaddr *)&ripsrc, n,
169 /* should notify about lost packet */
174 sorwakeup(last->inp_socket);
181 /* check AH/ESP integrity. */
182 if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
184 ipsecstat.in_polvio++;
185 ipstat.ips_delivered--;
186 /* do not inject data to pcb */
190 if (last->inp_flags & INP_CONTROLOPTS ||
191 last->inp_socket->so_options & SO_TIMESTAMP)
192 ip_savecontrol(last, &opts, ip, m);
193 if (sbappendaddr(&last->inp_socket->so_rcv,
194 (struct sockaddr *)&ripsrc, m, opts) == 0) {
199 sorwakeup(last->inp_socket);
202 ipstat.ips_noproto++;
203 ipstat.ips_delivered--;
208 * Generate IP header and pass packet to ip_output.
209 * Tack on options user may have setup with control call.
212 rip_output(m, so, dst)
217 register struct ip *ip;
218 register struct inpcb *inp = sotoinpcb(so);
219 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
222 * If the user handed us a complete IP packet, use it.
223 * Otherwise, allocate an mbuf for a header and fill it in.
225 if ((inp->inp_flags & INP_HDRINCL) == 0) {
226 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
230 M_PREPEND(m, sizeof(struct ip), M_WAIT);
231 ip = mtod(m, struct ip *);
232 ip->ip_tos = inp->inp_ip_tos;
234 ip->ip_p = inp->inp_ip_p;
235 ip->ip_len = m->m_pkthdr.len;
236 ip->ip_src = inp->inp_laddr;
237 ip->ip_dst.s_addr = dst;
238 ip->ip_ttl = inp->inp_ip_ttl;
240 if (m->m_pkthdr.len > IP_MAXPACKET) {
244 ip = mtod(m, struct ip *);
245 /* don't allow both user specified and setsockopt options,
246 and don't allow packet length sizes that will crash */
247 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
249 || (ip->ip_len > m->m_pkthdr.len)
250 || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
256 ip->ip_id = ip_randomid();
258 ip->ip_id = htons(ip_id++);
260 /* XXX prevent ip_output from overwriting header fields */
261 flags |= IP_RAWOUTPUT;
266 if (ipsec_setsocket(m, so) != 0) {
272 return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
277 * Raw IP socket option processing.
280 rip_ctloutput(so, sopt)
282 struct sockopt *sopt;
284 struct inpcb *inp = sotoinpcb(so);
287 if (sopt->sopt_level != IPPROTO_IP)
292 switch (sopt->sopt_dir) {
294 switch (sopt->sopt_name) {
296 optval = inp->inp_flags & INP_HDRINCL;
297 error = sooptcopyout(sopt, &optval, sizeof optval);
302 if (ip_fw_ctl_ptr == 0)
305 error = ip_fw_ctl_ptr(sopt);
309 case IP_DUMMYNET_GET:
310 if (ip_dn_ctl_ptr == NULL)
311 error = ENOPROTOOPT ;
313 error = ip_dn_ctl_ptr(sopt);
315 #endif /* DUMMYNET */
325 error = ip_mrouter_get(so, sopt);
329 error = ip_ctloutput(so, sopt);
335 switch (sopt->sopt_name) {
337 error = sooptcopyin(sopt, &optval, sizeof optval,
342 inp->inp_flags |= INP_HDRINCL;
344 inp->inp_flags &= ~INP_HDRINCL;
352 if (ip_fw_ctl_ptr == 0)
355 error = ip_fw_ctl_ptr(sopt);
359 case IP_DUMMYNET_CONFIGURE:
360 case IP_DUMMYNET_DEL:
361 case IP_DUMMYNET_FLUSH:
362 if (ip_dn_ctl_ptr == NULL)
363 error = ENOPROTOOPT ;
365 error = ip_dn_ctl_ptr(sopt);
370 error = ip_rsvp_init(so);
374 error = ip_rsvp_done();
377 /* XXX - should be combined */
379 error = ip_rsvp_vif_init(so, sopt);
382 case IP_RSVP_VIF_OFF:
383 error = ip_rsvp_vif_done(so, sopt);
394 error = ip_mrouter_set(so, sopt);
398 error = ip_ctloutput(so, sopt);
408 * This function exists solely to receive the PRC_IFDOWN messages which
409 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
410 * and calls in_ifadown() to remove all routes corresponding to that address.
411 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
415 rip_ctlinput(cmd, sa, vip)
420 struct in_ifaddr *ia;
427 for (ia = in_ifaddrhead.tqh_first; ia;
428 ia = ia->ia_link.tqe_next) {
429 if (ia->ia_ifa.ifa_addr == sa
430 && (ia->ia_flags & IFA_ROUTE)) {
432 * in_ifscrub kills the interface route.
434 in_ifscrub(ia->ia_ifp, ia);
436 * in_ifadown gets rid of all the rest of
437 * the routes. This is not quite the right
438 * thing to do, but at least if we are running
439 * a routing process they will come back.
441 in_ifadown(&ia->ia_ifa, 0);
448 for (ia = in_ifaddrhead.tqh_first; ia;
449 ia = ia->ia_link.tqe_next) {
450 if (ia->ia_ifa.ifa_addr == sa)
453 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
456 ifp = ia->ia_ifa.ifa_ifp;
458 if ((ifp->if_flags & IFF_LOOPBACK)
459 || (ifp->if_flags & IFF_POINTOPOINT))
462 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
464 ia->ia_flags |= IFA_ROUTE;
469 u_long rip_sendspace = RIPSNDQ;
470 u_long rip_recvspace = RIPRCVQ;
472 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
473 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
474 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
475 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
478 rip_attach(struct socket *so, int proto, struct proc *p)
487 error = soreserve(so, rip_sendspace, rip_recvspace);
491 error = in_pcballoc(so, &ripcbinfo, p);
495 inp = (struct inpcb *)so->so_pcb;
496 inp->inp_vflag |= INP_IPV4;
497 inp->inp_ip_p = proto;
498 inp->inp_ip_ttl = ip_defttl;
503 rip_detach(struct socket *so)
510 if (so == ip_mrouter)
512 ip_rsvp_force_done(so);
520 rip_abort(struct socket *so)
522 soisdisconnected(so);
523 return rip_detach(so);
527 rip_disconnect(struct socket *so)
529 if ((so->so_state & SS_ISCONNECTED) == 0)
531 return rip_abort(so);
535 rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
537 struct inpcb *inp = sotoinpcb(so);
538 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
540 if (nam->sa_len != sizeof(*addr))
543 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
544 (addr->sin_family != AF_IMPLINK)) ||
545 (addr->sin_addr.s_addr &&
546 ifa_ifwithaddr((struct sockaddr *)addr) == 0))
547 return EADDRNOTAVAIL;
548 inp->inp_laddr = addr->sin_addr;
553 rip_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
555 struct inpcb *inp = sotoinpcb(so);
556 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
558 if (nam->sa_len != sizeof(*addr))
560 if (TAILQ_EMPTY(&ifnet))
561 return EADDRNOTAVAIL;
562 if ((addr->sin_family != AF_INET) &&
563 (addr->sin_family != AF_IMPLINK))
565 inp->inp_faddr = addr->sin_addr;
571 rip_shutdown(struct socket *so)
578 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
579 struct mbuf *control, struct proc *p)
581 struct inpcb *inp = sotoinpcb(so);
584 if (so->so_state & SS_ISCONNECTED) {
589 dst = inp->inp_faddr.s_addr;
595 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
597 return rip_output(m, so, dst);
600 #ifdef CYGPKG_NET_FREEBSD_SYSCTL
602 rip_pcblist(SYSCTL_HANDLER_ARGS)
605 struct inpcb *inp, **inp_list;
610 * The process of preparing the TCB list is too time-consuming and
611 * resource-intensive to repeat twice on every request.
613 if (req->oldptr == 0) {
614 n = ripcbinfo.ipi_count;
615 req->oldidx = 2 * (sizeof xig)
616 + (n + n/8) * sizeof(struct xinpcb);
620 if (req->newptr != 0)
624 * OK, now we're committed to doing something.
627 gencnt = ripcbinfo.ipi_gencnt;
628 n = ripcbinfo.ipi_count;
631 xig.xig_len = sizeof xig;
633 xig.xig_gen = gencnt;
634 xig.xig_sogen = so_gencnt;
635 error = SYSCTL_OUT(req, &xig, sizeof xig);
639 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
644 for (inp = ripcbinfo.listhead->lh_first, i = 0; inp && i < n;
645 inp = inp->inp_list.le_next) {
646 if (inp->inp_gencnt <= gencnt)
653 for (i = 0; i < n; i++) {
655 if (inp->inp_gencnt <= gencnt) {
657 xi.xi_len = sizeof xi;
658 /* XXX should avoid extra copy */
659 bcopy(inp, &xi.xi_inp, sizeof *inp);
661 sotoxsocket(inp->inp_socket, &xi.xi_socket);
662 error = SYSCTL_OUT(req, &xi, sizeof xi);
667 * Give the user an updated idea of our state.
668 * If the generation differs from what we told
669 * her before, she knows that something happened
670 * while we were processing this request, and it
671 * might be necessary to retry.
674 xig.xig_gen = ripcbinfo.ipi_gencnt;
675 xig.xig_sogen = so_gencnt;
676 xig.xig_count = ripcbinfo.ipi_count;
678 error = SYSCTL_OUT(req, &xig, sizeof xig);
680 free(inp_list, M_TEMP);
685 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
686 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
688 struct pr_usrreqs rip_usrreqs = {
689 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
690 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
691 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
692 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
693 in_setsockaddr, sosend, soreceive, sopoll