Really fix indent.
[dragonfly.git] / sys / netinet / raw_ip.c
blob22c8e443bab900411aedddec5d807b434fd259f0
1 /*
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
7 * are met:
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
31 * SUCH DAMAGE.
33 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
34 * $FreeBSD: src/sys/netinet/raw_ip.c,v 1.64.2.16 2003/08/24 08:24:38 hsu Exp $
35 * $DragonFly: src/sys/netinet/raw_ip.c,v 1.33 2008/10/28 03:07:28 sephe Exp $
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/jail.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/proc.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/thread2.h>
54 #include <machine/stdarg.h>
56 #include <vm/vm_zone.h>
58 #include <net/if.h>
59 #include <net/route.h>
61 #define _IP_VHL
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/in_pcb.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip_var.h>
69 #include <net/ip_mroute/ip_mroute.h>
70 #include <net/ipfw/ip_fw.h>
71 #include <net/dummynet/ip_dummynet.h>
73 #ifdef FAST_IPSEC
74 #include <netproto/ipsec/ipsec.h>
75 #endif /*FAST_IPSEC*/
77 #ifdef IPSEC
78 #include <netinet6/ipsec.h>
79 #endif /*IPSEC*/
81 struct inpcbinfo ripcbinfo;
83 /* control hooks for ipfw and dummynet */
84 ip_fw_ctl_t *ip_fw_ctl_ptr;
85 ip_dn_ctl_t *ip_dn_ctl_ptr;
88 * hooks for multicast routing. They all default to NULL,
89 * so leave them not initialized and rely on BSS being set to 0.
92 /* The socket used to communicate with the multicast routing daemon. */
93 struct socket *ip_mrouter;
95 /* The various mrouter and rsvp functions */
96 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
97 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
98 int (*ip_mrouter_done)(void);
99 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
100 struct ip_moptions *);
101 int (*mrt_ioctl)(int, caddr_t);
102 int (*legal_vif_num)(int);
103 u_long (*ip_mcast_src)(int);
105 void (*rsvp_input_p)(struct mbuf *m, ...);
106 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
107 void (*ip_rsvp_force_done)(struct socket *);
110 * Nominal space allocated to a raw ip socket.
112 #define RIPSNDQ 8192
113 #define RIPRCVQ 8192
116 * Raw interface to IP protocol.
120 * Initialize raw connection block queue.
122 void
123 rip_init(void)
125 in_pcbinfo_init(&ripcbinfo);
127 * XXX We don't use the hash list for raw IP, but it's easier
128 * to allocate a one entry hash list than it is to check all
129 * over the place for hashbase == NULL.
131 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
132 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
133 ripcbinfo.wildcardhashbase = hashinit(1, M_PCB,
134 &ripcbinfo.wildcardhashmask);
135 ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
136 maxsockets, ZONE_INTERRUPT, 0);
140 * Setup generic address and protocol structures
141 * for raw_input routine, then pass them along with
142 * mbuf chain.
144 void
145 rip_input(struct mbuf *m, ...)
147 struct sockaddr_in ripsrc = { sizeof ripsrc, AF_INET };
148 struct ip *ip = mtod(m, struct ip *);
149 struct inpcb *inp;
150 struct inpcb *last = NULL;
151 struct mbuf *opts = NULL;
152 int off, proto;
153 __va_list ap;
155 __va_start(ap, m);
156 off = __va_arg(ap, int);
157 proto = __va_arg(ap, int);
158 __va_end(ap);
160 ripsrc.sin_addr = ip->ip_src;
161 LIST_FOREACH(inp, &ripcbinfo.pcblisthead, inp_list) {
162 if (inp->inp_flags & INP_PLACEMARKER)
163 continue;
164 #ifdef INET6
165 if ((inp->inp_vflag & INP_IPV4) == 0)
166 continue;
167 #endif
168 if (inp->inp_ip_p && inp->inp_ip_p != proto)
169 continue;
170 if (inp->inp_laddr.s_addr != INADDR_ANY &&
171 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
172 continue;
173 if (inp->inp_faddr.s_addr != INADDR_ANY &&
174 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
175 continue;
176 if (last) {
177 struct mbuf *n = m_copypacket(m, MB_DONTWAIT);
179 #ifdef IPSEC
180 /* check AH/ESP integrity. */
181 if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
182 m_freem(n);
183 ipsecstat.in_polvio++;
184 /* do not inject data to pcb */
185 } else
186 #endif /*IPSEC*/
187 #ifdef FAST_IPSEC
188 /* check AH/ESP integrity. */
189 if (ipsec4_in_reject(n, last)) {
190 m_freem(n);
191 /* do not inject data to pcb */
192 } else
193 #endif /*FAST_IPSEC*/
194 if (n) {
195 if (last->inp_flags & INP_CONTROLOPTS ||
196 last->inp_socket->so_options & SO_TIMESTAMP)
197 ip_savecontrol(last, &opts, ip, n);
198 if (ssb_appendaddr(&last->inp_socket->so_rcv,
199 (struct sockaddr *)&ripsrc, n,
200 opts) == 0) {
201 /* should notify about lost packet */
202 m_freem(n);
203 if (opts)
204 m_freem(opts);
205 } else
206 sorwakeup(last->inp_socket);
207 opts = 0;
210 last = inp;
212 #ifdef IPSEC
213 /* check AH/ESP integrity. */
214 if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
215 m_freem(m);
216 ipsecstat.in_polvio++;
217 ipstat.ips_delivered--;
218 /* do not inject data to pcb */
219 } else
220 #endif /*IPSEC*/
221 #ifdef FAST_IPSEC
222 /* check AH/ESP integrity. */
223 if (last && ipsec4_in_reject(m, last)) {
224 m_freem(m);
225 ipstat.ips_delivered--;
226 /* do not inject data to pcb */
227 } else
228 #endif /*FAST_IPSEC*/
229 /* Check the minimum TTL for socket. */
230 if (last && ip->ip_ttl < last->inp_ip_minttl) {
231 m_freem(opts);
232 ipstat.ips_delivered--;
233 } else if (last) {
234 if (last->inp_flags & INP_CONTROLOPTS ||
235 last->inp_socket->so_options & SO_TIMESTAMP)
236 ip_savecontrol(last, &opts, ip, m);
237 if (ssb_appendaddr(&last->inp_socket->so_rcv,
238 (struct sockaddr *)&ripsrc, m, opts) == 0) {
239 m_freem(m);
240 if (opts)
241 m_freem(opts);
242 } else
243 sorwakeup(last->inp_socket);
244 } else {
245 m_freem(m);
246 ipstat.ips_noproto++;
247 ipstat.ips_delivered--;
252 * Generate IP header and pass packet to ip_output.
253 * Tack on options user may have setup with control call.
256 rip_output(struct mbuf *m, struct socket *so, ...)
258 struct ip *ip;
259 struct inpcb *inp = so->so_pcb;
260 __va_list ap;
261 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
262 u_long dst;
264 __va_start(ap, so);
265 dst = __va_arg(ap, u_long);
266 __va_end(ap);
269 * If the user handed us a complete IP packet, use it.
270 * Otherwise, allocate an mbuf for a header and fill it in.
272 if ((inp->inp_flags & INP_HDRINCL) == 0) {
273 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
274 m_freem(m);
275 return(EMSGSIZE);
277 M_PREPEND(m, sizeof(struct ip), MB_WAIT);
278 if (m == NULL)
279 return(ENOBUFS);
280 ip = mtod(m, struct ip *);
281 ip->ip_tos = inp->inp_ip_tos;
282 ip->ip_off = 0;
283 ip->ip_p = inp->inp_ip_p;
284 ip->ip_len = m->m_pkthdr.len;
285 ip->ip_src = inp->inp_laddr;
286 ip->ip_dst.s_addr = dst;
287 ip->ip_ttl = inp->inp_ip_ttl;
288 } else {
289 int hlen;
291 if (m->m_pkthdr.len > IP_MAXPACKET) {
292 m_freem(m);
293 return(EMSGSIZE);
295 if (m->m_len < sizeof(struct ip)) {
296 m = m_pullup(m, sizeof(struct ip));
297 if (m == NULL)
298 return ENOBUFS;
300 ip = mtod(m, struct ip *);
301 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
303 /* Don't allow header length less than the minimum. */
304 if (hlen < sizeof(struct ip)) {
305 m_freem(m);
306 return EINVAL;
310 * Don't allow both user specified and setsockopt options.
311 * Don't allow packet length sizes that will crash.
313 if ((hlen != sizeof(struct ip) && inp->inp_options) ||
314 ip->ip_len > m->m_pkthdr.len || ip->ip_len < hlen) {
315 m_freem(m);
316 return EINVAL;
318 if (ip->ip_id == 0)
319 ip->ip_id = ip_newid();
321 /* Prevent ip_output from overwriting header fields */
322 flags |= IP_RAWOUTPUT;
323 ipstat.ips_rawout++;
326 return ip_output(m, inp->inp_options, &inp->inp_route, flags,
327 inp->inp_moptions, inp);
331 * Raw IP socket option processing.
334 rip_ctloutput(struct socket *so, struct sockopt *sopt)
336 struct inpcb *inp = so->so_pcb;
337 int error, optval;
339 if (sopt->sopt_level != IPPROTO_IP)
340 return (EINVAL);
342 error = 0;
344 switch (sopt->sopt_dir) {
345 case SOPT_GET:
346 switch (sopt->sopt_name) {
347 case IP_HDRINCL:
348 optval = inp->inp_flags & INP_HDRINCL;
349 soopt_from_kbuf(sopt, &optval, sizeof optval);
350 break;
352 case IP_FW_ADD: /* ADD actually returns the body... */
353 case IP_FW_GET:
354 if (IPFW_LOADED)
355 error = ip_fw_sockopt(sopt);
356 else
357 error = ENOPROTOOPT;
358 break;
360 case IP_DUMMYNET_GET:
361 error = ip_dn_sockopt(sopt);
362 break ;
364 case MRT_INIT:
365 case MRT_DONE:
366 case MRT_ADD_VIF:
367 case MRT_DEL_VIF:
368 case MRT_ADD_MFC:
369 case MRT_DEL_MFC:
370 case MRT_VERSION:
371 case MRT_ASSERT:
372 case MRT_API_SUPPORT:
373 case MRT_API_CONFIG:
374 case MRT_ADD_BW_UPCALL:
375 case MRT_DEL_BW_UPCALL:
376 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
377 EOPNOTSUPP;
378 break;
380 default:
381 error = ip_ctloutput(so, sopt);
382 break;
384 break;
386 case SOPT_SET:
387 switch (sopt->sopt_name) {
388 case IP_HDRINCL:
389 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
390 sizeof optval);
391 if (error)
392 break;
393 if (optval)
394 inp->inp_flags |= INP_HDRINCL;
395 else
396 inp->inp_flags &= ~INP_HDRINCL;
397 break;
399 case IP_FW_ADD:
400 case IP_FW_DEL:
401 case IP_FW_FLUSH:
402 case IP_FW_ZERO:
403 case IP_FW_RESETLOG:
404 if (IPFW_LOADED)
405 error = ip_fw_ctl_ptr(sopt);
406 else
407 error = ENOPROTOOPT;
408 break;
410 case IP_DUMMYNET_CONFIGURE:
411 case IP_DUMMYNET_DEL:
412 case IP_DUMMYNET_FLUSH:
413 error = ip_dn_sockopt(sopt);
414 break ;
416 case IP_RSVP_ON:
417 error = ip_rsvp_init(so);
418 break;
420 case IP_RSVP_OFF:
421 error = ip_rsvp_done();
422 break;
424 case IP_RSVP_VIF_ON:
425 case IP_RSVP_VIF_OFF:
426 error = ip_rsvp_vif ?
427 ip_rsvp_vif(so, sopt) : EINVAL;
428 break;
430 case MRT_INIT:
431 case MRT_DONE:
432 case MRT_ADD_VIF:
433 case MRT_DEL_VIF:
434 case MRT_ADD_MFC:
435 case MRT_DEL_MFC:
436 case MRT_VERSION:
437 case MRT_ASSERT:
438 case MRT_API_SUPPORT:
439 case MRT_API_CONFIG:
440 case MRT_ADD_BW_UPCALL:
441 case MRT_DEL_BW_UPCALL:
442 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
443 EOPNOTSUPP;
444 break;
446 default:
447 error = ip_ctloutput(so, sopt);
448 break;
450 break;
453 return (error);
457 * This function exists solely to receive the PRC_IFDOWN messages which
458 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
459 * and calls in_ifadown() to remove all routes corresponding to that address.
460 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
461 * interface routes.
463 void
464 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
466 struct in_ifaddr *ia;
467 struct in_ifaddr_container *iac;
468 struct ifnet *ifp;
469 int err;
470 int flags;
472 switch (cmd) {
473 case PRC_IFDOWN:
474 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
475 ia = iac->ia;
477 if (ia->ia_ifa.ifa_addr == sa &&
478 (ia->ia_flags & IFA_ROUTE)) {
480 * in_ifscrub kills the interface route.
482 in_ifscrub(ia->ia_ifp, ia);
484 * in_ifadown gets rid of all the rest of
485 * the routes. This is not quite the right
486 * thing to do, but at least if we are running
487 * a routing process they will come back.
489 in_ifadown(&ia->ia_ifa, 0);
490 break;
493 break;
495 case PRC_IFUP:
496 ia = NULL;
497 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
498 if (iac->ia->ia_ifa.ifa_addr == sa) {
499 ia = iac->ia;
500 break;
503 if (ia == NULL || (ia->ia_flags & IFA_ROUTE))
504 return;
505 flags = RTF_UP;
506 ifp = ia->ia_ifa.ifa_ifp;
508 if ((ifp->if_flags & IFF_LOOPBACK) ||
509 (ifp->if_flags & IFF_POINTOPOINT))
510 flags |= RTF_HOST;
512 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
513 if (err == 0)
514 ia->ia_flags |= IFA_ROUTE;
515 break;
519 u_long rip_sendspace = RIPSNDQ;
520 u_long rip_recvspace = RIPRCVQ;
522 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
523 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
524 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
525 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
527 static int
528 rip_attach(struct socket *so, int proto, struct pru_attach_info *ai)
530 struct inpcb *inp;
531 int error;
532 int flag;
534 if (jailed(ai->p_ucred) && jail_allow_raw_sockets)
535 flag = NULL_CRED_OKAY | PRISON_ROOT;
536 else
537 flag = NULL_CRED_OKAY;
539 inp = so->so_pcb;
540 if (inp)
541 panic("rip_attach");
542 if ((error = suser_cred(ai->p_ucred, flag)) != 0)
543 return error;
545 error = soreserve(so, rip_sendspace, rip_recvspace, ai->sb_rlimit);
546 if (error)
547 return error;
548 crit_enter();
549 error = in_pcballoc(so, &ripcbinfo);
550 crit_exit();
551 if (error)
552 return error;
553 inp = (struct inpcb *)so->so_pcb;
554 inp->inp_vflag |= INP_IPV4;
555 inp->inp_ip_p = proto;
556 inp->inp_ip_ttl = ip_defttl;
557 return 0;
560 static int
561 rip_detach(struct socket *so)
563 struct inpcb *inp;
565 inp = so->so_pcb;
566 if (inp == 0)
567 panic("rip_detach");
568 if (so == ip_mrouter && ip_mrouter_done)
569 ip_mrouter_done();
570 if (ip_rsvp_force_done)
571 ip_rsvp_force_done(so);
572 if (so == ip_rsvpd)
573 ip_rsvp_done();
574 in_pcbdetach(inp);
575 return 0;
578 static int
579 rip_abort(struct socket *so)
581 soisdisconnected(so);
582 if (so->so_state & SS_NOFDREF)
583 return rip_detach(so);
584 return 0;
587 static int
588 rip_disconnect(struct socket *so)
590 if ((so->so_state & SS_ISCONNECTED) == 0)
591 return ENOTCONN;
592 return rip_abort(so);
595 static int
596 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
598 struct inpcb *inp = so->so_pcb;
599 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
601 if (nam->sa_len != sizeof(*addr))
602 return EINVAL;
604 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
605 (addr->sin_family != AF_IMPLINK)) ||
606 (addr->sin_addr.s_addr != INADDR_ANY &&
607 ifa_ifwithaddr((struct sockaddr *)addr) == 0))
608 return EADDRNOTAVAIL;
609 inp->inp_laddr = addr->sin_addr;
610 return 0;
613 static int
614 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
616 struct inpcb *inp = so->so_pcb;
617 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
619 if (nam->sa_len != sizeof(*addr))
620 return EINVAL;
621 if (TAILQ_EMPTY(&ifnet))
622 return EADDRNOTAVAIL;
623 if ((addr->sin_family != AF_INET) &&
624 (addr->sin_family != AF_IMPLINK))
625 return EAFNOSUPPORT;
626 inp->inp_faddr = addr->sin_addr;
627 soisconnected(so);
628 return 0;
631 static int
632 rip_shutdown(struct socket *so)
634 socantsendmore(so);
635 return 0;
638 static int
639 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
640 struct mbuf *control, struct thread *td)
642 struct inpcb *inp = so->so_pcb;
643 u_long dst;
645 if (so->so_state & SS_ISCONNECTED) {
646 if (nam) {
647 m_freem(m);
648 return EISCONN;
650 dst = inp->inp_faddr.s_addr;
651 } else {
652 if (nam == NULL) {
653 m_freem(m);
654 return ENOTCONN;
656 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
658 return rip_output(m, so, dst);
661 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, &ripcbinfo, 0,
662 in_pcblist_global, "S,xinpcb", "List of active raw IP sockets");
664 struct pr_usrreqs rip_usrreqs = {
665 .pru_abort = rip_abort,
666 .pru_accept = pru_accept_notsupp,
667 .pru_attach = rip_attach,
668 .pru_bind = rip_bind,
669 .pru_connect = rip_connect,
670 .pru_connect2 = pru_connect2_notsupp,
671 .pru_control = in_control,
672 .pru_detach = rip_detach,
673 .pru_disconnect = rip_disconnect,
674 .pru_listen = pru_listen_notsupp,
675 .pru_peeraddr = in_setpeeraddr,
676 .pru_rcvd = pru_rcvd_notsupp,
677 .pru_rcvoob = pru_rcvoob_notsupp,
678 .pru_send = rip_send,
679 .pru_sense = pru_sense_null,
680 .pru_shutdown = rip_shutdown,
681 .pru_sockaddr = in_setsockaddr,
682 .pru_sosend = sosend,
683 .pru_soreceive = soreceive,
684 .pru_sopoll = sopoll