Merge from vendor branch GCC:
[dragonfly.git] / sys / netinet / raw_ip.c
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
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.
20  *
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.
32  *
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.22 2005/06/02 23:52:42 dillon Exp $
36  */
37
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
40 #include "opt_random_ip_id.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.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>
53
54 #include <machine/stdarg.h>
55
56 #include <vm/vm_zone.h>
57
58 #include <net/if.h>
59 #include <net/route.h>
60
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>
68
69 #include <net/ip_mroute/ip_mroute.h>
70 #include <net/ipfw/ip_fw.h>
71 #include <net/dummynet/ip_dummynet.h>
72
73 #ifdef FAST_IPSEC
74 #include <netproto/ipsec/ipsec.h>
75 #endif /*FAST_IPSEC*/
76
77 #ifdef IPSEC
78 #include <netinet6/ipsec.h>
79 #endif /*IPSEC*/
80
81 struct  inpcbinfo ripcbinfo;
82
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;
86
87 /*
88  * hooks for multicast routing. They all default to NULL,
89  * so leave them not initialized and rely on BSS being set to 0.
90  */
91
92 /* The socket used to communicate with the multicast routing daemon.  */
93 struct socket  *ip_mrouter;
94
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);
104
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 *);
108
109 /*
110  * Nominal space allocated to a raw ip socket.
111  */
112 #define RIPSNDQ         8192
113 #define RIPRCVQ         8192
114
115 /*
116  * Raw interface to IP protocol.
117  */
118
119 /*
120  * Initialize raw connection block queue.
121  */
122 void
123 rip_init(void)
124 {
125         in_pcbinfo_init(&ripcbinfo);
126         /*
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.
130          */
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);
137 }
138
139 /*
140  * Setup generic address and protocol structures
141  * for raw_input routine, then pass them along with
142  * mbuf chain.
143  */
144 void
145 rip_input(struct mbuf *m, ...)
146 {
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;
154
155         __va_start(ap, m);
156         off = __va_arg(ap, int);
157         proto = __va_arg(ap, int);
158         __va_end(ap);
159
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);
178
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 (sbappendaddr(&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;
208                         }
209                 }
210                 last = inp;
211         }
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         if (last) {
230                 if (last->inp_flags & INP_CONTROLOPTS ||
231                     last->inp_socket->so_options & SO_TIMESTAMP)
232                         ip_savecontrol(last, &opts, ip, m);
233                 if (sbappendaddr(&last->inp_socket->so_rcv,
234                     (struct sockaddr *)&ripsrc, m, opts) == 0) {
235                         m_freem(m);
236                         if (opts)
237                             m_freem(opts);
238                 } else
239                         sorwakeup(last->inp_socket);
240         } else {
241                 m_freem(m);
242                 ipstat.ips_noproto++;
243                 ipstat.ips_delivered--;
244         }
245 }
246
247 /*
248  * Generate IP header and pass packet to ip_output.
249  * Tack on options user may have setup with control call.
250  */
251 int
252 rip_output(struct mbuf *m, struct socket *so, ...)
253 {
254         struct ip *ip;
255         struct inpcb *inp = so->so_pcb;
256         __va_list ap;
257         int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
258         u_long dst;
259
260         __va_start(ap, so);
261         dst = __va_arg(ap, u_long);
262         __va_end(ap);
263
264         /*
265          * If the user handed us a complete IP packet, use it.
266          * Otherwise, allocate an mbuf for a header and fill it in.
267          */
268         if ((inp->inp_flags & INP_HDRINCL) == 0) {
269                 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
270                         m_freem(m);
271                         return(EMSGSIZE);
272                 }
273                 M_PREPEND(m, sizeof(struct ip), MB_WAIT);
274                 if (m == NULL)
275                         return(ENOBUFS);
276                 ip = mtod(m, struct ip *);
277                 ip->ip_tos = inp->inp_ip_tos;
278                 ip->ip_off = 0;
279                 ip->ip_p = inp->inp_ip_p;
280                 ip->ip_len = m->m_pkthdr.len;
281                 ip->ip_src = inp->inp_laddr;
282                 ip->ip_dst.s_addr = dst;
283                 ip->ip_ttl = inp->inp_ip_ttl;
284         } else {
285                 if (m->m_pkthdr.len > IP_MAXPACKET) {
286                         m_freem(m);
287                         return(EMSGSIZE);
288                 }
289                 ip = mtod(m, struct ip *);
290                 /* don't allow both user specified and setsockopt options,
291                    and don't allow packet length sizes that will crash */
292                 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2)) &&
293                      inp->inp_options) ||
294                     (ip->ip_len > m->m_pkthdr.len) ||
295                     (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
296                         m_freem(m);
297                         return EINVAL;
298                 }
299                 if (ip->ip_id == 0)
300 #ifdef RANDOM_IP_ID
301                         ip->ip_id = ip_randomid();
302 #else
303                         ip->ip_id = htons(ip_id++);
304 #endif
305                 /* XXX prevent ip_output from overwriting header fields */
306                 flags |= IP_RAWOUTPUT;
307                 ipstat.ips_rawout++;
308         }
309
310         return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
311                           inp->inp_moptions, inp));
312 }
313
314 /*
315  * Raw IP socket option processing.
316  */
317 int
318 rip_ctloutput(struct socket *so, struct sockopt *sopt)
319 {
320         struct  inpcb *inp = so->so_pcb;
321         int     error, optval;
322
323         if (sopt->sopt_level != IPPROTO_IP)
324                 return (EINVAL);
325
326         error = 0;
327
328         switch (sopt->sopt_dir) {
329         case SOPT_GET:
330                 switch (sopt->sopt_name) {
331                 case IP_HDRINCL:
332                         optval = inp->inp_flags & INP_HDRINCL;
333                         error = sooptcopyout(sopt, &optval, sizeof optval);
334                         break;
335
336                 case IP_FW_ADD: /* ADD actually returns the body... */
337                 case IP_FW_GET:
338                         if (IPFW_LOADED)
339                                 error = ip_fw_ctl_ptr(sopt);
340                         else
341                                 error = ENOPROTOOPT;
342                         break;
343
344                 case IP_DUMMYNET_GET:
345                         if (DUMMYNET_LOADED)
346                                 error = ip_dn_ctl_ptr(sopt);
347                         else
348                                 error = ENOPROTOOPT;
349                         break ;
350
351                 case MRT_INIT:
352                 case MRT_DONE:
353                 case MRT_ADD_VIF:
354                 case MRT_DEL_VIF:
355                 case MRT_ADD_MFC:
356                 case MRT_DEL_MFC:
357                 case MRT_VERSION:
358                 case MRT_ASSERT:
359                 case MRT_API_SUPPORT:
360                 case MRT_API_CONFIG:
361                 case MRT_ADD_BW_UPCALL:
362                 case MRT_DEL_BW_UPCALL:
363                         error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
364                                 EOPNOTSUPP;
365                         break;
366
367                 default:
368                         error = ip_ctloutput(so, sopt);
369                         break;
370                 }
371                 break;
372
373         case SOPT_SET:
374                 switch (sopt->sopt_name) {
375                 case IP_HDRINCL:
376                         error = sooptcopyin(sopt, &optval, sizeof optval,
377                                             sizeof optval);
378                         if (error)
379                                 break;
380                         if (optval)
381                                 inp->inp_flags |= INP_HDRINCL;
382                         else
383                                 inp->inp_flags &= ~INP_HDRINCL;
384                         break;
385
386                 case IP_FW_ADD:
387                 case IP_FW_DEL:
388                 case IP_FW_FLUSH:
389                 case IP_FW_ZERO:
390                 case IP_FW_RESETLOG:
391                         if (IPFW_LOADED)
392                                 error = ip_fw_ctl_ptr(sopt);
393                         else
394                                 error = ENOPROTOOPT;
395                         break;
396
397                 case IP_DUMMYNET_CONFIGURE:
398                 case IP_DUMMYNET_DEL:
399                 case IP_DUMMYNET_FLUSH:
400                         if (DUMMYNET_LOADED)
401                                 error = ip_dn_ctl_ptr(sopt);
402                         else
403                                 error = ENOPROTOOPT ;
404                         break ;
405
406                 case IP_RSVP_ON:
407                         error = ip_rsvp_init(so);
408                         break;
409
410                 case IP_RSVP_OFF:
411                         error = ip_rsvp_done();
412                         break;
413
414                 case IP_RSVP_VIF_ON:
415                 case IP_RSVP_VIF_OFF:
416                         error = ip_rsvp_vif ?
417                                 ip_rsvp_vif(so, sopt) : EINVAL;
418                         break;
419
420                 case MRT_INIT:
421                 case MRT_DONE:
422                 case MRT_ADD_VIF:
423                 case MRT_DEL_VIF:
424                 case MRT_ADD_MFC:
425                 case MRT_DEL_MFC:
426                 case MRT_VERSION:
427                 case MRT_ASSERT:
428                 case MRT_API_SUPPORT:
429                 case MRT_API_CONFIG:
430                 case MRT_ADD_BW_UPCALL:
431                 case MRT_DEL_BW_UPCALL:
432                         error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
433                                         EOPNOTSUPP;
434                         break;
435
436                 default:
437                         error = ip_ctloutput(so, sopt);
438                         break;
439                 }
440                 break;
441         }
442
443         return (error);
444 }
445
446 /*
447  * This function exists solely to receive the PRC_IFDOWN messages which
448  * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
449  * and calls in_ifadown() to remove all routes corresponding to that address.
450  * It also receives the PRC_IFUP messages from if_up() and reinstalls the
451  * interface routes.
452  */
453 void
454 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
455 {
456         struct in_ifaddr *ia;
457         struct ifnet *ifp;
458         int err;
459         int flags;
460
461         switch (cmd) {
462         case PRC_IFDOWN:
463                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
464                         if (ia->ia_ifa.ifa_addr == sa &&
465                             (ia->ia_flags & IFA_ROUTE)) {
466                                 /*
467                                  * in_ifscrub kills the interface route.
468                                  */
469                                 in_ifscrub(ia->ia_ifp, ia);
470                                 /*
471                                  * in_ifadown gets rid of all the rest of
472                                  * the routes.  This is not quite the right
473                                  * thing to do, but at least if we are running
474                                  * a routing process they will come back.
475                                  */
476                                 in_ifadown(&ia->ia_ifa, 0);
477                                 break;
478                         }
479                 }
480                 break;
481
482         case PRC_IFUP:
483                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
484                         if (ia->ia_ifa.ifa_addr == sa)
485                                 break;
486                 }
487                 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
488                         return;
489                 flags = RTF_UP;
490                 ifp = ia->ia_ifa.ifa_ifp;
491
492                 if ((ifp->if_flags & IFF_LOOPBACK) ||
493                     (ifp->if_flags & IFF_POINTOPOINT))
494                         flags |= RTF_HOST;
495
496                 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
497                 if (err == 0)
498                         ia->ia_flags |= IFA_ROUTE;
499                 break;
500         }
501 }
502
503 u_long  rip_sendspace = RIPSNDQ;
504 u_long  rip_recvspace = RIPRCVQ;
505
506 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
507     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
508 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
509     &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
510
511 static int
512 rip_attach(struct socket *so, int proto, struct pru_attach_info *ai)
513 {
514         struct inpcb *inp;
515         int error;
516
517         inp = so->so_pcb;
518         if (inp)
519                 panic("rip_attach");
520         if ((error = suser_cred(ai->p_ucred, NULL_CRED_OKAY)) != 0)
521                 return error;
522
523         error = soreserve(so, rip_sendspace, rip_recvspace, ai->sb_rlimit);
524         if (error)
525                 return error;
526         crit_enter();
527         error = in_pcballoc(so, &ripcbinfo);
528         crit_exit();
529         if (error)
530                 return error;
531         inp = (struct inpcb *)so->so_pcb;
532         inp->inp_vflag |= INP_IPV4;
533         inp->inp_ip_p = proto;
534         inp->inp_ip_ttl = ip_defttl;
535         return 0;
536 }
537
538 static int
539 rip_detach(struct socket *so)
540 {
541         struct inpcb *inp;
542
543         inp = so->so_pcb;
544         if (inp == 0)
545                 panic("rip_detach");
546         if (so == ip_mrouter && ip_mrouter_done)
547                 ip_mrouter_done();
548         if (ip_rsvp_force_done)
549                 ip_rsvp_force_done(so);
550         if (so == ip_rsvpd)
551                 ip_rsvp_done();
552         in_pcbdetach(inp);
553         return 0;
554 }
555
556 static int
557 rip_abort(struct socket *so)
558 {
559         soisdisconnected(so);
560         if (so->so_state & SS_NOFDREF)
561                 return rip_detach(so);
562         return 0;
563 }
564
565 static int
566 rip_disconnect(struct socket *so)
567 {
568         if ((so->so_state & SS_ISCONNECTED) == 0)
569                 return ENOTCONN;
570         return rip_abort(so);
571 }
572
573 static int
574 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
575 {
576         struct inpcb *inp = so->so_pcb;
577         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
578
579         if (nam->sa_len != sizeof(*addr))
580                 return EINVAL;
581
582         if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
583                                     (addr->sin_family != AF_IMPLINK)) ||
584             (addr->sin_addr.s_addr != INADDR_ANY &&
585              ifa_ifwithaddr((struct sockaddr *)addr) == 0))
586                 return EADDRNOTAVAIL;
587         inp->inp_laddr = addr->sin_addr;
588         return 0;
589 }
590
591 static int
592 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
593 {
594         struct inpcb *inp = so->so_pcb;
595         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
596
597         if (nam->sa_len != sizeof(*addr))
598                 return EINVAL;
599         if (TAILQ_EMPTY(&ifnet))
600                 return EADDRNOTAVAIL;
601         if ((addr->sin_family != AF_INET) &&
602             (addr->sin_family != AF_IMPLINK))
603                 return EAFNOSUPPORT;
604         inp->inp_faddr = addr->sin_addr;
605         soisconnected(so);
606         return 0;
607 }
608
609 static int
610 rip_shutdown(struct socket *so)
611 {
612         socantsendmore(so);
613         return 0;
614 }
615
616 static int
617 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
618          struct mbuf *control, struct thread *td)
619 {
620         struct inpcb *inp = so->so_pcb;
621         u_long dst;
622
623         if (so->so_state & SS_ISCONNECTED) {
624                 if (nam) {
625                         m_freem(m);
626                         return EISCONN;
627                 }
628                 dst = inp->inp_faddr.s_addr;
629         } else {
630                 if (nam == NULL) {
631                         m_freem(m);
632                         return ENOTCONN;
633                 }
634                 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
635         }
636         return rip_output(m, so, dst);
637 }
638
639 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, &ripcbinfo, 0,
640             in_pcblist_global, "S,xinpcb", "List of active raw IP sockets");
641
642 struct pr_usrreqs rip_usrreqs = {
643         rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
644         pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
645         pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
646         pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
647         in_setsockaddr, sosend, soreceive, sopoll
648 };