Make vinum use libedit instead of libreadline.
[dragonfly.git] / sys / netinet / raw_ip.c
blobcb61c86eb8fd79cbe3089653321681c1daacbf56
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.26 2007/04/22 01:13:14 dillon 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/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/proc.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/thread2.h>
53 #include <machine/stdarg.h>
55 #include <vm/vm_zone.h>
57 #include <net/if.h>
58 #include <net/route.h>
60 #define _IP_VHL
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip_var.h>
68 #include <net/ip_mroute/ip_mroute.h>
69 #include <net/ipfw/ip_fw.h>
70 #include <net/dummynet/ip_dummynet.h>
72 #ifdef FAST_IPSEC
73 #include <netproto/ipsec/ipsec.h>
74 #endif /*FAST_IPSEC*/
76 #ifdef IPSEC
77 #include <netinet6/ipsec.h>
78 #endif /*IPSEC*/
80 struct inpcbinfo ripcbinfo;
82 /* control hooks for ipfw and dummynet */
83 ip_fw_ctl_t *ip_fw_ctl_ptr;
84 ip_dn_ctl_t *ip_dn_ctl_ptr;
87 * hooks for multicast routing. They all default to NULL,
88 * so leave them not initialized and rely on BSS being set to 0.
91 /* The socket used to communicate with the multicast routing daemon. */
92 struct socket *ip_mrouter;
94 /* The various mrouter and rsvp functions */
95 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
96 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
97 int (*ip_mrouter_done)(void);
98 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
99 struct ip_moptions *);
100 int (*mrt_ioctl)(int, caddr_t);
101 int (*legal_vif_num)(int);
102 u_long (*ip_mcast_src)(int);
104 void (*rsvp_input_p)(struct mbuf *m, ...);
105 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
106 void (*ip_rsvp_force_done)(struct socket *);
109 * Nominal space allocated to a raw ip socket.
111 #define RIPSNDQ 8192
112 #define RIPRCVQ 8192
115 * Raw interface to IP protocol.
119 * Initialize raw connection block queue.
121 void
122 rip_init(void)
124 in_pcbinfo_init(&ripcbinfo);
126 * XXX We don't use the hash list for raw IP, but it's easier
127 * to allocate a one entry hash list than it is to check all
128 * over the place for hashbase == NULL.
130 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
131 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
132 ripcbinfo.wildcardhashbase = hashinit(1, M_PCB,
133 &ripcbinfo.wildcardhashmask);
134 ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
135 maxsockets, ZONE_INTERRUPT, 0);
139 * Setup generic address and protocol structures
140 * for raw_input routine, then pass them along with
141 * mbuf chain.
143 void
144 rip_input(struct mbuf *m, ...)
146 struct sockaddr_in ripsrc = { sizeof ripsrc, AF_INET };
147 struct ip *ip = mtod(m, struct ip *);
148 struct inpcb *inp;
149 struct inpcb *last = NULL;
150 struct mbuf *opts = NULL;
151 int off, proto;
152 __va_list ap;
154 __va_start(ap, m);
155 off = __va_arg(ap, int);
156 proto = __va_arg(ap, int);
157 __va_end(ap);
159 ripsrc.sin_addr = ip->ip_src;
160 LIST_FOREACH(inp, &ripcbinfo.pcblisthead, inp_list) {
161 if (inp->inp_flags & INP_PLACEMARKER)
162 continue;
163 #ifdef INET6
164 if ((inp->inp_vflag & INP_IPV4) == 0)
165 continue;
166 #endif
167 if (inp->inp_ip_p && inp->inp_ip_p != proto)
168 continue;
169 if (inp->inp_laddr.s_addr != INADDR_ANY &&
170 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
171 continue;
172 if (inp->inp_faddr.s_addr != INADDR_ANY &&
173 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
174 continue;
175 if (last) {
176 struct mbuf *n = m_copypacket(m, MB_DONTWAIT);
178 #ifdef IPSEC
179 /* check AH/ESP integrity. */
180 if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
181 m_freem(n);
182 ipsecstat.in_polvio++;
183 /* do not inject data to pcb */
184 } else
185 #endif /*IPSEC*/
186 #ifdef FAST_IPSEC
187 /* check AH/ESP integrity. */
188 if (ipsec4_in_reject(n, last)) {
189 m_freem(n);
190 /* do not inject data to pcb */
191 } else
192 #endif /*FAST_IPSEC*/
193 if (n) {
194 if (last->inp_flags & INP_CONTROLOPTS ||
195 last->inp_socket->so_options & SO_TIMESTAMP)
196 ip_savecontrol(last, &opts, ip, n);
197 if (ssb_appendaddr(&last->inp_socket->so_rcv,
198 (struct sockaddr *)&ripsrc, n,
199 opts) == 0) {
200 /* should notify about lost packet */
201 m_freem(n);
202 if (opts)
203 m_freem(opts);
204 } else
205 sorwakeup(last->inp_socket);
206 opts = 0;
209 last = inp;
211 #ifdef IPSEC
212 /* check AH/ESP integrity. */
213 if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
214 m_freem(m);
215 ipsecstat.in_polvio++;
216 ipstat.ips_delivered--;
217 /* do not inject data to pcb */
218 } else
219 #endif /*IPSEC*/
220 #ifdef FAST_IPSEC
221 /* check AH/ESP integrity. */
222 if (last && ipsec4_in_reject(m, last)) {
223 m_freem(m);
224 ipstat.ips_delivered--;
225 /* do not inject data to pcb */
226 } else
227 #endif /*FAST_IPSEC*/
228 /* Check the minimum TTL for socket. */
229 if (last && ip->ip_ttl < last->inp_ip_minttl) {
230 m_freem(opts);
231 ipstat.ips_delivered--;
232 } else if (last) {
233 if (last->inp_flags & INP_CONTROLOPTS ||
234 last->inp_socket->so_options & SO_TIMESTAMP)
235 ip_savecontrol(last, &opts, ip, m);
236 if (ssb_appendaddr(&last->inp_socket->so_rcv,
237 (struct sockaddr *)&ripsrc, m, opts) == 0) {
238 m_freem(m);
239 if (opts)
240 m_freem(opts);
241 } else
242 sorwakeup(last->inp_socket);
243 } else {
244 m_freem(m);
245 ipstat.ips_noproto++;
246 ipstat.ips_delivered--;
251 * Generate IP header and pass packet to ip_output.
252 * Tack on options user may have setup with control call.
255 rip_output(struct mbuf *m, struct socket *so, ...)
257 struct ip *ip;
258 struct inpcb *inp = so->so_pcb;
259 __va_list ap;
260 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
261 u_long dst;
263 __va_start(ap, so);
264 dst = __va_arg(ap, u_long);
265 __va_end(ap);
268 * If the user handed us a complete IP packet, use it.
269 * Otherwise, allocate an mbuf for a header and fill it in.
271 if ((inp->inp_flags & INP_HDRINCL) == 0) {
272 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
273 m_freem(m);
274 return(EMSGSIZE);
276 M_PREPEND(m, sizeof(struct ip), MB_WAIT);
277 if (m == NULL)
278 return(ENOBUFS);
279 ip = mtod(m, struct ip *);
280 ip->ip_tos = inp->inp_ip_tos;
281 ip->ip_off = 0;
282 ip->ip_p = inp->inp_ip_p;
283 ip->ip_len = m->m_pkthdr.len;
284 ip->ip_src = inp->inp_laddr;
285 ip->ip_dst.s_addr = dst;
286 ip->ip_ttl = inp->inp_ip_ttl;
287 } else {
288 if (m->m_pkthdr.len > IP_MAXPACKET) {
289 m_freem(m);
290 return(EMSGSIZE);
292 ip = mtod(m, struct ip *);
293 /* don't allow both user specified and setsockopt options,
294 and don't allow packet length sizes that will crash */
295 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2)) &&
296 inp->inp_options) ||
297 (ip->ip_len > m->m_pkthdr.len) ||
298 (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
299 m_freem(m);
300 return EINVAL;
302 if (ip->ip_id == 0)
303 ip->ip_id = ip_newid();
304 /* XXX prevent ip_output from overwriting header fields */
305 flags |= IP_RAWOUTPUT;
306 ipstat.ips_rawout++;
309 return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
310 inp->inp_moptions, inp));
314 * Raw IP socket option processing.
317 rip_ctloutput(struct socket *so, struct sockopt *sopt)
319 struct inpcb *inp = so->so_pcb;
320 int error, optval;
322 if (sopt->sopt_level != IPPROTO_IP)
323 return (EINVAL);
325 error = 0;
327 switch (sopt->sopt_dir) {
328 case SOPT_GET:
329 switch (sopt->sopt_name) {
330 case IP_HDRINCL:
331 optval = inp->inp_flags & INP_HDRINCL;
332 error = sooptcopyout(sopt, &optval, sizeof optval);
333 break;
335 case IP_FW_ADD: /* ADD actually returns the body... */
336 case IP_FW_GET:
337 if (IPFW_LOADED)
338 error = ip_fw_ctl_ptr(sopt);
339 else
340 error = ENOPROTOOPT;
341 break;
343 case IP_DUMMYNET_GET:
344 if (DUMMYNET_LOADED)
345 error = ip_dn_ctl_ptr(sopt);
346 else
347 error = ENOPROTOOPT;
348 break ;
350 case MRT_INIT:
351 case MRT_DONE:
352 case MRT_ADD_VIF:
353 case MRT_DEL_VIF:
354 case MRT_ADD_MFC:
355 case MRT_DEL_MFC:
356 case MRT_VERSION:
357 case MRT_ASSERT:
358 case MRT_API_SUPPORT:
359 case MRT_API_CONFIG:
360 case MRT_ADD_BW_UPCALL:
361 case MRT_DEL_BW_UPCALL:
362 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
363 EOPNOTSUPP;
364 break;
366 default:
367 error = ip_ctloutput(so, sopt);
368 break;
370 break;
372 case SOPT_SET:
373 switch (sopt->sopt_name) {
374 case IP_HDRINCL:
375 error = sooptcopyin(sopt, &optval, sizeof optval,
376 sizeof optval);
377 if (error)
378 break;
379 if (optval)
380 inp->inp_flags |= INP_HDRINCL;
381 else
382 inp->inp_flags &= ~INP_HDRINCL;
383 break;
385 case IP_FW_ADD:
386 case IP_FW_DEL:
387 case IP_FW_FLUSH:
388 case IP_FW_ZERO:
389 case IP_FW_RESETLOG:
390 if (IPFW_LOADED)
391 error = ip_fw_ctl_ptr(sopt);
392 else
393 error = ENOPROTOOPT;
394 break;
396 case IP_DUMMYNET_CONFIGURE:
397 case IP_DUMMYNET_DEL:
398 case IP_DUMMYNET_FLUSH:
399 if (DUMMYNET_LOADED)
400 error = ip_dn_ctl_ptr(sopt);
401 else
402 error = ENOPROTOOPT ;
403 break ;
405 case IP_RSVP_ON:
406 error = ip_rsvp_init(so);
407 break;
409 case IP_RSVP_OFF:
410 error = ip_rsvp_done();
411 break;
413 case IP_RSVP_VIF_ON:
414 case IP_RSVP_VIF_OFF:
415 error = ip_rsvp_vif ?
416 ip_rsvp_vif(so, sopt) : EINVAL;
417 break;
419 case MRT_INIT:
420 case MRT_DONE:
421 case MRT_ADD_VIF:
422 case MRT_DEL_VIF:
423 case MRT_ADD_MFC:
424 case MRT_DEL_MFC:
425 case MRT_VERSION:
426 case MRT_ASSERT:
427 case MRT_API_SUPPORT:
428 case MRT_API_CONFIG:
429 case MRT_ADD_BW_UPCALL:
430 case MRT_DEL_BW_UPCALL:
431 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
432 EOPNOTSUPP;
433 break;
435 default:
436 error = ip_ctloutput(so, sopt);
437 break;
439 break;
442 return (error);
446 * This function exists solely to receive the PRC_IFDOWN messages which
447 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
448 * and calls in_ifadown() to remove all routes corresponding to that address.
449 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
450 * interface routes.
452 void
453 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
455 struct in_ifaddr *ia;
456 struct ifnet *ifp;
457 int err;
458 int flags;
460 switch (cmd) {
461 case PRC_IFDOWN:
462 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
463 if (ia->ia_ifa.ifa_addr == sa &&
464 (ia->ia_flags & IFA_ROUTE)) {
466 * in_ifscrub kills the interface route.
468 in_ifscrub(ia->ia_ifp, ia);
470 * in_ifadown gets rid of all the rest of
471 * the routes. This is not quite the right
472 * thing to do, but at least if we are running
473 * a routing process they will come back.
475 in_ifadown(&ia->ia_ifa, 0);
476 break;
479 break;
481 case PRC_IFUP:
482 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
483 if (ia->ia_ifa.ifa_addr == sa)
484 break;
486 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
487 return;
488 flags = RTF_UP;
489 ifp = ia->ia_ifa.ifa_ifp;
491 if ((ifp->if_flags & IFF_LOOPBACK) ||
492 (ifp->if_flags & IFF_POINTOPOINT))
493 flags |= RTF_HOST;
495 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
496 if (err == 0)
497 ia->ia_flags |= IFA_ROUTE;
498 break;
502 u_long rip_sendspace = RIPSNDQ;
503 u_long rip_recvspace = RIPRCVQ;
505 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
506 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
507 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
508 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
510 static int
511 rip_attach(struct socket *so, int proto, struct pru_attach_info *ai)
513 struct inpcb *inp;
514 int error;
516 inp = so->so_pcb;
517 if (inp)
518 panic("rip_attach");
519 if ((error = suser_cred(ai->p_ucred, NULL_CRED_OKAY)) != 0)
520 return error;
522 error = soreserve(so, rip_sendspace, rip_recvspace, ai->sb_rlimit);
523 if (error)
524 return error;
525 crit_enter();
526 error = in_pcballoc(so, &ripcbinfo);
527 crit_exit();
528 if (error)
529 return error;
530 inp = (struct inpcb *)so->so_pcb;
531 inp->inp_vflag |= INP_IPV4;
532 inp->inp_ip_p = proto;
533 inp->inp_ip_ttl = ip_defttl;
534 return 0;
537 static int
538 rip_detach(struct socket *so)
540 struct inpcb *inp;
542 inp = so->so_pcb;
543 if (inp == 0)
544 panic("rip_detach");
545 if (so == ip_mrouter && ip_mrouter_done)
546 ip_mrouter_done();
547 if (ip_rsvp_force_done)
548 ip_rsvp_force_done(so);
549 if (so == ip_rsvpd)
550 ip_rsvp_done();
551 in_pcbdetach(inp);
552 return 0;
555 static int
556 rip_abort(struct socket *so)
558 soisdisconnected(so);
559 if (so->so_state & SS_NOFDREF)
560 return rip_detach(so);
561 return 0;
564 static int
565 rip_disconnect(struct socket *so)
567 if ((so->so_state & SS_ISCONNECTED) == 0)
568 return ENOTCONN;
569 return rip_abort(so);
572 static int
573 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
575 struct inpcb *inp = so->so_pcb;
576 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
578 if (nam->sa_len != sizeof(*addr))
579 return EINVAL;
581 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
582 (addr->sin_family != AF_IMPLINK)) ||
583 (addr->sin_addr.s_addr != INADDR_ANY &&
584 ifa_ifwithaddr((struct sockaddr *)addr) == 0))
585 return EADDRNOTAVAIL;
586 inp->inp_laddr = addr->sin_addr;
587 return 0;
590 static int
591 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
593 struct inpcb *inp = so->so_pcb;
594 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
596 if (nam->sa_len != sizeof(*addr))
597 return EINVAL;
598 if (TAILQ_EMPTY(&ifnet))
599 return EADDRNOTAVAIL;
600 if ((addr->sin_family != AF_INET) &&
601 (addr->sin_family != AF_IMPLINK))
602 return EAFNOSUPPORT;
603 inp->inp_faddr = addr->sin_addr;
604 soisconnected(so);
605 return 0;
608 static int
609 rip_shutdown(struct socket *so)
611 socantsendmore(so);
612 return 0;
615 static int
616 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
617 struct mbuf *control, struct thread *td)
619 struct inpcb *inp = so->so_pcb;
620 u_long dst;
622 if (so->so_state & SS_ISCONNECTED) {
623 if (nam) {
624 m_freem(m);
625 return EISCONN;
627 dst = inp->inp_faddr.s_addr;
628 } else {
629 if (nam == NULL) {
630 m_freem(m);
631 return ENOTCONN;
633 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
635 return rip_output(m, so, dst);
638 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, &ripcbinfo, 0,
639 in_pcblist_global, "S,xinpcb", "List of active raw IP sockets");
641 struct pr_usrreqs rip_usrreqs = {
642 .pru_abort = rip_abort,
643 .pru_accept = pru_accept_notsupp,
644 .pru_attach = rip_attach,
645 .pru_bind = rip_bind,
646 .pru_connect = rip_connect,
647 .pru_connect2 = pru_connect2_notsupp,
648 .pru_control = in_control,
649 .pru_detach = rip_detach,
650 .pru_disconnect = rip_disconnect,
651 .pru_listen = pru_listen_notsupp,
652 .pru_peeraddr = in_setpeeraddr,
653 .pru_rcvd = pru_rcvd_notsupp,
654 .pru_rcvoob = pru_rcvoob_notsupp,
655 .pru_send = rip_send,
656 .pru_sense = pru_sense_null,
657 .pru_shutdown = rip_shutdown,
658 .pru_sockaddr = in_setsockaddr,
659 .pru_sosend = sosend,
660 .pru_soreceive = soreceive,
661 .pru_sopoll = sopoll