priv: Use PRIV_NETINET_RAW
[dragonfly.git] / sys / netinet / raw_ip.c
blobb88cb4348e5f62b16f940325f7a0df3dadacd3f6
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/priv.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/thread2.h>
55 #include <machine/stdarg.h>
57 #include <vm/vm_zone.h>
59 #include <net/if.h>
60 #include <net/route.h>
62 #define _IP_VHL
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>
74 #ifdef FAST_IPSEC
75 #include <netproto/ipsec/ipsec.h>
76 #endif /*FAST_IPSEC*/
78 #ifdef IPSEC
79 #include <netinet6/ipsec.h>
80 #endif /*IPSEC*/
82 struct inpcbinfo ripcbinfo;
84 /* control hooks for ipfw and dummynet */
85 ip_fw_ctl_t *ip_fw_ctl_ptr;
86 ip_dn_ctl_t *ip_dn_ctl_ptr;
89 * hooks for multicast routing. They all default to NULL,
90 * so leave them not initialized and rely on BSS being set to 0.
93 /* The socket used to communicate with the multicast routing daemon. */
94 struct socket *ip_mrouter;
96 /* The various mrouter and rsvp functions */
97 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
98 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
99 int (*ip_mrouter_done)(void);
100 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
101 struct ip_moptions *);
102 int (*mrt_ioctl)(int, caddr_t);
103 int (*legal_vif_num)(int);
104 u_long (*ip_mcast_src)(int);
106 void (*rsvp_input_p)(struct mbuf *m, ...);
107 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
108 void (*ip_rsvp_force_done)(struct socket *);
111 * Nominal space allocated to a raw ip socket.
113 #define RIPSNDQ 8192
114 #define RIPRCVQ 8192
117 * Raw interface to IP protocol.
121 * Initialize raw connection block queue.
123 void
124 rip_init(void)
126 in_pcbinfo_init(&ripcbinfo);
128 * XXX We don't use the hash list for raw IP, but it's easier
129 * to allocate a one entry hash list than it is to check all
130 * over the place for hashbase == NULL.
132 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
133 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
134 ripcbinfo.wildcardhashbase = hashinit(1, M_PCB,
135 &ripcbinfo.wildcardhashmask);
136 ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
137 maxsockets, ZONE_INTERRUPT, 0);
141 * Setup generic address and protocol structures
142 * for raw_input routine, then pass them along with
143 * mbuf chain.
145 void
146 rip_input(struct mbuf *m, ...)
148 struct sockaddr_in ripsrc = { sizeof ripsrc, AF_INET };
149 struct ip *ip = mtod(m, struct ip *);
150 struct inpcb *inp;
151 struct inpcb *last = NULL;
152 struct mbuf *opts = NULL;
153 int off, proto;
154 __va_list ap;
156 __va_start(ap, m);
157 off = __va_arg(ap, int);
158 proto = __va_arg(ap, int);
159 __va_end(ap);
161 ripsrc.sin_addr = ip->ip_src;
162 LIST_FOREACH(inp, &ripcbinfo.pcblisthead, inp_list) {
163 if (inp->inp_flags & INP_PLACEMARKER)
164 continue;
165 #ifdef INET6
166 if ((inp->inp_vflag & INP_IPV4) == 0)
167 continue;
168 #endif
169 if (inp->inp_ip_p && inp->inp_ip_p != proto)
170 continue;
171 if (inp->inp_laddr.s_addr != INADDR_ANY &&
172 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
173 continue;
174 if (inp->inp_faddr.s_addr != INADDR_ANY &&
175 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
176 continue;
177 if (last) {
178 struct mbuf *n = m_copypacket(m, MB_DONTWAIT);
180 #ifdef IPSEC
181 /* check AH/ESP integrity. */
182 if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
183 m_freem(n);
184 ipsecstat.in_polvio++;
185 /* do not inject data to pcb */
186 } else
187 #endif /*IPSEC*/
188 #ifdef FAST_IPSEC
189 /* check AH/ESP integrity. */
190 if (ipsec4_in_reject(n, last)) {
191 m_freem(n);
192 /* do not inject data to pcb */
193 } else
194 #endif /*FAST_IPSEC*/
195 if (n) {
196 if (last->inp_flags & INP_CONTROLOPTS ||
197 last->inp_socket->so_options & SO_TIMESTAMP)
198 ip_savecontrol(last, &opts, ip, n);
199 if (ssb_appendaddr(&last->inp_socket->so_rcv,
200 (struct sockaddr *)&ripsrc, n,
201 opts) == 0) {
202 /* should notify about lost packet */
203 m_freem(n);
204 if (opts)
205 m_freem(opts);
206 } else
207 sorwakeup(last->inp_socket);
208 opts = 0;
211 last = inp;
213 #ifdef IPSEC
214 /* check AH/ESP integrity. */
215 if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
216 m_freem(m);
217 ipsecstat.in_polvio++;
218 ipstat.ips_delivered--;
219 /* do not inject data to pcb */
220 } else
221 #endif /*IPSEC*/
222 #ifdef FAST_IPSEC
223 /* check AH/ESP integrity. */
224 if (last && ipsec4_in_reject(m, last)) {
225 m_freem(m);
226 ipstat.ips_delivered--;
227 /* do not inject data to pcb */
228 } else
229 #endif /*FAST_IPSEC*/
230 /* Check the minimum TTL for socket. */
231 if (last && ip->ip_ttl < last->inp_ip_minttl) {
232 m_freem(opts);
233 ipstat.ips_delivered--;
234 } else if (last) {
235 if (last->inp_flags & INP_CONTROLOPTS ||
236 last->inp_socket->so_options & SO_TIMESTAMP)
237 ip_savecontrol(last, &opts, ip, m);
238 if (ssb_appendaddr(&last->inp_socket->so_rcv,
239 (struct sockaddr *)&ripsrc, m, opts) == 0) {
240 m_freem(m);
241 if (opts)
242 m_freem(opts);
243 } else
244 sorwakeup(last->inp_socket);
245 } else {
246 m_freem(m);
247 ipstat.ips_noproto++;
248 ipstat.ips_delivered--;
253 * Generate IP header and pass packet to ip_output.
254 * Tack on options user may have setup with control call.
257 rip_output(struct mbuf *m, struct socket *so, ...)
259 struct ip *ip;
260 struct inpcb *inp = so->so_pcb;
261 __va_list ap;
262 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
263 u_long dst;
265 __va_start(ap, so);
266 dst = __va_arg(ap, u_long);
267 __va_end(ap);
270 * If the user handed us a complete IP packet, use it.
271 * Otherwise, allocate an mbuf for a header and fill it in.
273 if ((inp->inp_flags & INP_HDRINCL) == 0) {
274 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
275 m_freem(m);
276 return(EMSGSIZE);
278 M_PREPEND(m, sizeof(struct ip), MB_WAIT);
279 if (m == NULL)
280 return(ENOBUFS);
281 ip = mtod(m, struct ip *);
282 ip->ip_tos = inp->inp_ip_tos;
283 ip->ip_off = 0;
284 ip->ip_p = inp->inp_ip_p;
285 ip->ip_len = m->m_pkthdr.len;
286 ip->ip_src = inp->inp_laddr;
287 ip->ip_dst.s_addr = dst;
288 ip->ip_ttl = inp->inp_ip_ttl;
289 } else {
290 int hlen;
292 if (m->m_pkthdr.len > IP_MAXPACKET) {
293 m_freem(m);
294 return(EMSGSIZE);
296 if (m->m_len < sizeof(struct ip)) {
297 m = m_pullup(m, sizeof(struct ip));
298 if (m == NULL)
299 return ENOBUFS;
301 ip = mtod(m, struct ip *);
302 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
304 /* Don't allow header length less than the minimum. */
305 if (hlen < sizeof(struct ip)) {
306 m_freem(m);
307 return EINVAL;
311 * Don't allow both user specified and setsockopt options.
312 * Don't allow packet length sizes that will crash.
314 if ((hlen != sizeof(struct ip) && inp->inp_options) ||
315 ip->ip_len > m->m_pkthdr.len || ip->ip_len < hlen) {
316 m_freem(m);
317 return EINVAL;
319 if (ip->ip_id == 0)
320 ip->ip_id = ip_newid();
322 /* Prevent ip_output from overwriting header fields */
323 flags |= IP_RAWOUTPUT;
324 ipstat.ips_rawout++;
327 return ip_output(m, inp->inp_options, &inp->inp_route, flags,
328 inp->inp_moptions, inp);
332 * Raw IP socket option processing.
335 rip_ctloutput(struct socket *so, struct sockopt *sopt)
337 struct inpcb *inp = so->so_pcb;
338 int error, optval;
340 if (sopt->sopt_level != IPPROTO_IP)
341 return (EINVAL);
343 error = 0;
345 switch (sopt->sopt_dir) {
346 case SOPT_GET:
347 switch (sopt->sopt_name) {
348 case IP_HDRINCL:
349 optval = inp->inp_flags & INP_HDRINCL;
350 soopt_from_kbuf(sopt, &optval, sizeof optval);
351 break;
353 case IP_FW_ADD: /* ADD actually returns the body... */
354 case IP_FW_GET:
355 if (IPFW_LOADED)
356 error = ip_fw_sockopt(sopt);
357 else
358 error = ENOPROTOOPT;
359 break;
361 case IP_DUMMYNET_GET:
362 error = ip_dn_sockopt(sopt);
363 break ;
365 case MRT_INIT:
366 case MRT_DONE:
367 case MRT_ADD_VIF:
368 case MRT_DEL_VIF:
369 case MRT_ADD_MFC:
370 case MRT_DEL_MFC:
371 case MRT_VERSION:
372 case MRT_ASSERT:
373 case MRT_API_SUPPORT:
374 case MRT_API_CONFIG:
375 case MRT_ADD_BW_UPCALL:
376 case MRT_DEL_BW_UPCALL:
377 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
378 EOPNOTSUPP;
379 break;
381 default:
382 error = ip_ctloutput(so, sopt);
383 break;
385 break;
387 case SOPT_SET:
388 switch (sopt->sopt_name) {
389 case IP_HDRINCL:
390 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
391 sizeof optval);
392 if (error)
393 break;
394 if (optval)
395 inp->inp_flags |= INP_HDRINCL;
396 else
397 inp->inp_flags &= ~INP_HDRINCL;
398 break;
400 case IP_FW_ADD:
401 case IP_FW_DEL:
402 case IP_FW_FLUSH:
403 case IP_FW_ZERO:
404 case IP_FW_RESETLOG:
405 if (IPFW_LOADED)
406 error = ip_fw_ctl_ptr(sopt);
407 else
408 error = ENOPROTOOPT;
409 break;
411 case IP_DUMMYNET_CONFIGURE:
412 case IP_DUMMYNET_DEL:
413 case IP_DUMMYNET_FLUSH:
414 error = ip_dn_sockopt(sopt);
415 break ;
417 case IP_RSVP_ON:
418 error = ip_rsvp_init(so);
419 break;
421 case IP_RSVP_OFF:
422 error = ip_rsvp_done();
423 break;
425 case IP_RSVP_VIF_ON:
426 case IP_RSVP_VIF_OFF:
427 error = ip_rsvp_vif ?
428 ip_rsvp_vif(so, sopt) : EINVAL;
429 break;
431 case MRT_INIT:
432 case MRT_DONE:
433 case MRT_ADD_VIF:
434 case MRT_DEL_VIF:
435 case MRT_ADD_MFC:
436 case MRT_DEL_MFC:
437 case MRT_VERSION:
438 case MRT_ASSERT:
439 case MRT_API_SUPPORT:
440 case MRT_API_CONFIG:
441 case MRT_ADD_BW_UPCALL:
442 case MRT_DEL_BW_UPCALL:
443 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
444 EOPNOTSUPP;
445 break;
447 default:
448 error = ip_ctloutput(so, sopt);
449 break;
451 break;
454 return (error);
458 * This function exists solely to receive the PRC_IFDOWN messages which
459 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
460 * and calls in_ifadown() to remove all routes corresponding to that address.
461 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
462 * interface routes.
464 void
465 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
467 struct in_ifaddr *ia;
468 struct in_ifaddr_container *iac;
469 struct ifnet *ifp;
470 int err;
471 int flags;
473 switch (cmd) {
474 case PRC_IFDOWN:
475 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
476 ia = iac->ia;
478 if (ia->ia_ifa.ifa_addr == sa &&
479 (ia->ia_flags & IFA_ROUTE)) {
481 * in_ifscrub kills the interface route.
483 in_ifscrub(ia->ia_ifp, ia);
485 * in_ifadown gets rid of all the rest of
486 * the routes. This is not quite the right
487 * thing to do, but at least if we are running
488 * a routing process they will come back.
490 in_ifadown(&ia->ia_ifa, 0);
491 break;
494 break;
496 case PRC_IFUP:
497 ia = NULL;
498 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
499 if (iac->ia->ia_ifa.ifa_addr == sa) {
500 ia = iac->ia;
501 break;
504 if (ia == NULL || (ia->ia_flags & IFA_ROUTE))
505 return;
506 flags = RTF_UP;
507 ifp = ia->ia_ifa.ifa_ifp;
509 if ((ifp->if_flags & IFF_LOOPBACK) ||
510 (ifp->if_flags & IFF_POINTOPOINT))
511 flags |= RTF_HOST;
513 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
514 if (err == 0)
515 ia->ia_flags |= IFA_ROUTE;
516 break;
520 u_long rip_sendspace = RIPSNDQ;
521 u_long rip_recvspace = RIPRCVQ;
523 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
524 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
525 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
526 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
528 static int
529 rip_attach(struct socket *so, int proto, struct pru_attach_info *ai)
531 struct inpcb *inp;
532 int error;
534 inp = so->so_pcb;
535 if (inp)
536 panic("rip_attach");
537 error = priv_check_cred(ai->p_ucred, PRIV_NETINET_RAW, NULL_CRED_OKAY);
538 if (error)
539 return error;
541 error = soreserve(so, rip_sendspace, rip_recvspace, ai->sb_rlimit);
542 if (error)
543 return error;
544 crit_enter();
545 error = in_pcballoc(so, &ripcbinfo);
546 crit_exit();
547 if (error)
548 return error;
549 inp = (struct inpcb *)so->so_pcb;
550 inp->inp_vflag |= INP_IPV4;
551 inp->inp_ip_p = proto;
552 inp->inp_ip_ttl = ip_defttl;
553 return 0;
556 static int
557 rip_detach(struct socket *so)
559 struct inpcb *inp;
561 inp = so->so_pcb;
562 if (inp == 0)
563 panic("rip_detach");
564 if (so == ip_mrouter && ip_mrouter_done)
565 ip_mrouter_done();
566 if (ip_rsvp_force_done)
567 ip_rsvp_force_done(so);
568 if (so == ip_rsvpd)
569 ip_rsvp_done();
570 in_pcbdetach(inp);
571 return 0;
574 static int
575 rip_abort(struct socket *so)
577 soisdisconnected(so);
578 if (so->so_state & SS_NOFDREF)
579 return rip_detach(so);
580 return 0;
583 static int
584 rip_disconnect(struct socket *so)
586 if ((so->so_state & SS_ISCONNECTED) == 0)
587 return ENOTCONN;
588 return rip_abort(so);
591 static int
592 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
594 struct inpcb *inp = so->so_pcb;
595 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
597 if (nam->sa_len != sizeof(*addr))
598 return EINVAL;
600 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
601 (addr->sin_family != AF_IMPLINK)) ||
602 (addr->sin_addr.s_addr != INADDR_ANY &&
603 ifa_ifwithaddr((struct sockaddr *)addr) == 0))
604 return EADDRNOTAVAIL;
605 inp->inp_laddr = addr->sin_addr;
606 return 0;
609 static int
610 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
612 struct inpcb *inp = so->so_pcb;
613 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
615 if (nam->sa_len != sizeof(*addr))
616 return EINVAL;
617 if (TAILQ_EMPTY(&ifnet))
618 return EADDRNOTAVAIL;
619 if ((addr->sin_family != AF_INET) &&
620 (addr->sin_family != AF_IMPLINK))
621 return EAFNOSUPPORT;
622 inp->inp_faddr = addr->sin_addr;
623 soisconnected(so);
624 return 0;
627 static int
628 rip_shutdown(struct socket *so)
630 socantsendmore(so);
631 return 0;
634 static int
635 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
636 struct mbuf *control, struct thread *td)
638 struct inpcb *inp = so->so_pcb;
639 u_long dst;
641 if (so->so_state & SS_ISCONNECTED) {
642 if (nam) {
643 m_freem(m);
644 return EISCONN;
646 dst = inp->inp_faddr.s_addr;
647 } else {
648 if (nam == NULL) {
649 m_freem(m);
650 return ENOTCONN;
652 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
654 return rip_output(m, so, dst);
657 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, &ripcbinfo, 0,
658 in_pcblist_global, "S,xinpcb", "List of active raw IP sockets");
660 struct pr_usrreqs rip_usrreqs = {
661 .pru_abort = rip_abort,
662 .pru_accept = pru_accept_notsupp,
663 .pru_attach = rip_attach,
664 .pru_bind = rip_bind,
665 .pru_connect = rip_connect,
666 .pru_connect2 = pru_connect2_notsupp,
667 .pru_control = in_control,
668 .pru_detach = rip_detach,
669 .pru_disconnect = rip_disconnect,
670 .pru_listen = pru_listen_notsupp,
671 .pru_peeraddr = in_setpeeraddr,
672 .pru_rcvd = pru_rcvd_notsupp,
673 .pru_rcvoob = pru_rcvoob_notsupp,
674 .pru_send = rip_send,
675 .pru_sense = pru_sense_null,
676 .pru_shutdown = rip_shutdown,
677 .pru_sockaddr = in_setsockaddr,
678 .pru_sosend = sosend,
679 .pru_soreceive = soreceive,
680 .pru_sopoll = sopoll